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 layout_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 * lightdm_get_layouts:
54 * Get a list of keyboard layouts to present to the user.
56 * Return value: (element-type LightDMLayout) (transfer none): A list of #LightDMLayout that should be presented to the user.
59 lightdm_get_layouts (void)
61 XklConfigRegistry *registry;
66 display = XOpenDisplay (NULL);
67 xkl_engine = xkl_engine_get_instance (display);
68 xkl_config = xkl_config_rec_new ();
69 if (!xkl_config_rec_get_from_server (xkl_config, xkl_engine))
70 g_warning ("Failed to get Xkl configuration from server");
72 registry = xkl_config_registry_get_instance (xkl_engine);
73 xkl_config_registry_load (registry, FALSE);
74 xkl_config_registry_foreach_layout (registry, layout_cb, NULL);
75 g_object_unref (registry);
84 * @layout: The layout to use
86 * Set the layout for this session.
89 lightdm_set_layout (LightDMLayout *layout)
93 g_return_if_fail (layout != NULL);
95 g_debug ("Setting keyboard layout to %s", lightdm_layout_get_name (layout));
97 config = xkl_config_rec_new ();
98 config->layouts = g_malloc (sizeof (gchar *) * 2);
99 config->model = g_strdup (xkl_config->model);
100 config->layouts[0] = g_strdup (lightdm_layout_get_name (layout));
101 config->layouts[1] = NULL;
102 if (!xkl_config_rec_activate (config, xkl_engine))
103 g_warning ("Failed to activate XKL config");
104 g_object_unref (config);
108 * lightdm_get_layout:
110 * Get the current keyboard layout.
112 * Return value: (transfer none): The currently active layout for this user.
115 lightdm_get_layout (void)
117 lightdm_get_layouts ();
119 return (LightDMLayout *) g_list_first (layouts)->data;
125 * lightdm_layout_get_name:
126 * @layout: A #LightDMLayout
128 * Get the name of a layout.
130 * Return value: The name of the layout
133 lightdm_layout_get_name (LightDMLayout *layout)
135 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
136 return GET_PRIVATE (layout)->name;
140 * lightdm_layout_get_short_description:
141 * @layout: A #LightDMLayout
143 * Get the short description of a layout.
145 * Return value: A short description of the layout
148 lightdm_layout_get_short_description (LightDMLayout *layout)
150 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
151 return GET_PRIVATE (layout)->short_description;
155 * lightdm_layout_get_description:
156 * @layout: A #LightDMLayout
158 * Get the long description of a layout.
160 * Return value: A long description of the layout
163 lightdm_layout_get_description (LightDMLayout *layout)
165 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
166 return GET_PRIVATE (layout)->description;
170 lightdm_layout_init (LightDMLayout *layout)
175 lightdm_layout_set_property (GObject *object,
180 LightDMLayout *self = LIGHTDM_LAYOUT (object);
181 LightDMLayoutPrivate *priv = GET_PRIVATE (self);
186 priv->name = g_strdup (g_value_get_string (value));
188 case PROP_SHORT_DESCRIPTION:
189 g_free (priv->short_description);
190 priv->short_description = g_strdup (g_value_get_string (value));
192 case PROP_DESCRIPTION:
193 g_free (priv->description);
194 priv->description = g_strdup (g_value_get_string (value));
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 lightdm_layout_get_property (GObject *object,
210 self = LIGHTDM_LAYOUT (object);
214 g_value_set_string (value, lightdm_layout_get_name (self));
216 case PROP_SHORT_DESCRIPTION:
217 g_value_set_string (value, lightdm_layout_get_short_description (self));
219 case PROP_DESCRIPTION:
220 g_value_set_string (value, lightdm_layout_get_description (self));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 lightdm_layout_class_init (LightDMLayoutClass *klass)
231 GObjectClass *object_class = G_OBJECT_CLASS (klass);
233 g_type_class_add_private (klass, sizeof (LightDMLayoutPrivate));
235 object_class->set_property = lightdm_layout_set_property;
236 object_class->get_property = lightdm_layout_get_property;
238 g_object_class_install_property (object_class,
240 g_param_spec_string ("name",
242 "Name of the layout",
244 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
245 g_object_class_install_property (object_class,
246 PROP_SHORT_DESCRIPTION,
247 g_param_spec_string ("short-description",
249 "Short description of the layout",
251 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
252 g_object_class_install_property (object_class,
254 g_param_spec_string ("description",
256 "Long description of the layout",
258 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));