2 * Copyright (C) 2010 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3 of the License, or (at your option) any
8 * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
12 #include <libxklavier/xklavier.h>
14 #include "lightdm/layout.h"
19 PROP_SHORT_DESCRIPTION,
26 gchar *short_description;
28 } LightDMLayoutPrivate;
30 G_DEFINE_TYPE (LightDMLayout, lightdm_layout, G_TYPE_OBJECT);
32 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_LAYOUT, LightDMLayoutPrivate)
34 static gboolean have_layouts = FALSE;
35 static Display *display = NULL;
36 static XklEngine *xkl_engine = NULL;
37 static XklConfigRec *xkl_config = NULL;
38 static GList *layouts = NULL;
41 variant_cb (XklConfigRegistry *config,
42 const XklConfigItem *item,
45 LightDMLayout *layout;
47 layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", item->name, "short-description", item->short_description, "description", item->description, NULL);
48 layouts = g_list_append (layouts, layout);
52 layout_cb (XklConfigRegistry *config,
53 const XklConfigItem *item,
56 LightDMLayout *layout;
58 layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", item->name, "short-description", item->short_description, "description", item->description, NULL);
59 layouts = g_list_append (layouts, layout);
61 xkl_config_registry_foreach_layout_variant (config, item->name, variant_cb, NULL);
65 * lightdm_get_layouts:
67 * Get a list of keyboard layouts to present to the user.
69 * Return value: (element-type LightDMLayout) (transfer none): A list of #LightDMLayout that should be presented to the user.
72 lightdm_get_layouts (void)
74 XklConfigRegistry *registry;
79 display = XOpenDisplay (NULL);
80 xkl_engine = xkl_engine_get_instance (display);
81 xkl_config = xkl_config_rec_new ();
82 if (!xkl_config_rec_get_from_server (xkl_config, xkl_engine))
83 g_warning ("Failed to get Xkl configuration from server");
85 registry = xkl_config_registry_get_instance (xkl_engine);
86 xkl_config_registry_load (registry, FALSE);
87 xkl_config_registry_foreach_layout (registry, layout_cb, NULL);
88 g_object_unref (registry);
97 * @layout: The layout to use
99 * Set the layout for this session.
102 lightdm_set_layout (LightDMLayout *layout)
104 XklConfigRec *config;
106 g_return_if_fail (layout != NULL);
108 g_debug ("Setting keyboard layout to %s", lightdm_layout_get_name (layout));
110 config = xkl_config_rec_new ();
111 config->layouts = g_malloc (sizeof (gchar *) * 2);
112 config->model = g_strdup (xkl_config->model);
113 config->layouts[0] = g_strdup (lightdm_layout_get_name (layout));
114 config->layouts[1] = NULL;
115 if (!xkl_config_rec_activate (config, xkl_engine))
116 g_warning ("Failed to activate XKL config");
117 g_object_unref (config);
121 * lightdm_get_layout:
123 * Get the current keyboard layout.
125 * Return value: (transfer none): The currently active layout for this user.
128 lightdm_get_layout (void)
130 lightdm_get_layouts ();
132 return (LightDMLayout *) g_list_first (layouts)->data;
138 * lightdm_layout_get_name:
139 * @layout: A #LightDMLayout
141 * Get the name of a layout.
143 * Return value: The name of the layout
146 lightdm_layout_get_name (LightDMLayout *layout)
148 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
149 return GET_PRIVATE (layout)->name;
153 * lightdm_layout_get_short_description:
154 * @layout: A #LightDMLayout
156 * Get the short description of a layout.
158 * Return value: A short description of the layout
161 lightdm_layout_get_short_description (LightDMLayout *layout)
163 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
164 return GET_PRIVATE (layout)->short_description;
168 * lightdm_layout_get_description:
169 * @layout: A #LightDMLayout
171 * Get the long description of a layout.
173 * Return value: A long description of the layout
176 lightdm_layout_get_description (LightDMLayout *layout)
178 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
179 return GET_PRIVATE (layout)->description;
183 lightdm_layout_init (LightDMLayout *layout)
188 lightdm_layout_set_property (GObject *object,
193 LightDMLayout *self = LIGHTDM_LAYOUT (object);
194 LightDMLayoutPrivate *priv = GET_PRIVATE (self);
199 priv->name = g_strdup (g_value_get_string (value));
201 case PROP_SHORT_DESCRIPTION:
202 g_free (priv->short_description);
203 priv->short_description = g_strdup (g_value_get_string (value));
205 case PROP_DESCRIPTION:
206 g_free (priv->description);
207 priv->description = g_strdup (g_value_get_string (value));
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 lightdm_layout_get_property (GObject *object,
223 self = LIGHTDM_LAYOUT (object);
227 g_value_set_string (value, lightdm_layout_get_name (self));
229 case PROP_SHORT_DESCRIPTION:
230 g_value_set_string (value, lightdm_layout_get_short_description (self));
232 case PROP_DESCRIPTION:
233 g_value_set_string (value, lightdm_layout_get_description (self));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 lightdm_layout_class_init (LightDMLayoutClass *klass)
244 GObjectClass *object_class = G_OBJECT_CLASS (klass);
246 g_type_class_add_private (klass, sizeof (LightDMLayoutPrivate));
248 object_class->set_property = lightdm_layout_set_property;
249 object_class->get_property = lightdm_layout_get_property;
251 g_object_class_install_property (object_class,
253 g_param_spec_string ("name",
255 "Name of the layout",
257 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
258 g_object_class_install_property (object_class,
259 PROP_SHORT_DESCRIPTION,
260 g_param_spec_string ("short-description",
262 "Short description of the layout",
264 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
265 g_object_class_install_property (object_class,
267 g_param_spec_string ("description",
269 "Long description of the layout",
271 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));