1 /* -*- Mode: C; indent-tabs-mode:nil; tab-width:4 -*-
3 * Copyright (C) 2010 Robert Ancell.
4 * Author: Robert Ancell <robert.ancell@canonical.com>
6 * This library is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by the Free
8 * Software Foundation; either version 2 or version 3 of the License.
9 * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
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;
39 static LightDMLayout *default_layout = NULL;
42 make_layout_string (const gchar *layout, const gchar *variant)
44 if (!layout || layout[0] == 0)
46 else if (!variant || variant[0] == 0)
47 return g_strdup (layout);
49 return g_strdup_printf ("%s\t%s", layout, variant);
53 parse_layout_string (const gchar *name, gchar **layout, gchar **variant)
63 split = g_strsplit (name, "\t", 2);
66 *layout = g_strdup (split[0]);
68 *variant = g_strdup (split[1]);
74 variant_cb (XklConfigRegistry *config,
75 const XklConfigItem *item,
78 LightDMLayout *layout;
81 full_name = make_layout_string (data, item->name);
83 layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", full_name, "short-description", item->short_description, "description", item->description, NULL);
84 layouts = g_list_append (layouts, layout);
90 layout_cb (XklConfigRegistry *config,
91 const XklConfigItem *item,
94 LightDMLayout *layout;
96 layout = g_object_new (LIGHTDM_TYPE_LAYOUT, "name", item->name, "short-description", item->short_description, "description", item->description, NULL);
97 layouts = g_list_append (layouts, layout);
99 xkl_config_registry_foreach_layout_variant (config, item->name, variant_cb, (gpointer) item->name);
103 * lightdm_get_layouts:
105 * Get a list of keyboard layouts to present to the user.
107 * Return value: (element-type LightDMLayout) (transfer none): A list of #LightDMLayout that should be presented to the user.
110 lightdm_get_layouts (void)
112 XklConfigRegistry *registry;
117 display = XOpenDisplay (NULL);
121 xkl_engine = xkl_engine_get_instance (display);
122 xkl_config = xkl_config_rec_new ();
123 if (!xkl_config_rec_get_from_server (xkl_config, xkl_engine))
124 g_warning ("Failed to get Xkl configuration from server");
126 registry = xkl_config_registry_get_instance (xkl_engine);
127 xkl_config_registry_load (registry, FALSE);
128 xkl_config_registry_foreach_layout (registry, layout_cb, NULL);
129 g_object_unref (registry);
137 * lightdm_set_layout:
138 * @layout: The layout to use
140 * Set the layout for this session.
143 lightdm_set_layout (LightDMLayout *dmlayout)
145 XklConfigRec *config;
146 gchar *layout, *variant;
148 g_return_if_fail (dmlayout != NULL);
150 g_debug ("Setting keyboard layout to '%s'", lightdm_layout_get_name (dmlayout));
152 parse_layout_string (lightdm_layout_get_name (dmlayout), &layout, &variant);
154 config = xkl_config_rec_new ();
155 config->layouts = g_malloc (sizeof (gchar *) * 2);
156 config->variants = g_malloc (sizeof (gchar *) * 2);
157 config->model = g_strdup (xkl_config->model);
158 config->layouts[0] = layout;
159 config->layouts[1] = NULL;
160 config->variants[0] = variant;
161 config->variants[1] = NULL;
162 if (!xkl_config_rec_activate (config, xkl_engine))
163 g_warning ("Failed to activate XKL config");
164 g_object_unref (config);
168 * lightdm_get_layout:
170 * Get the current keyboard layout.
172 * Return value: (transfer none): The currently active layout for this user.
175 lightdm_get_layout (void)
177 lightdm_get_layouts ();
179 if (layouts && xkl_config && !default_layout)
184 full_name = make_layout_string (xkl_config->layouts ? xkl_config->layouts[0] : NULL,
185 xkl_config->variants ? xkl_config->variants[0] : NULL);
187 for (item = layouts; item; item = item->next)
189 LightDMLayout *iter_layout = (LightDMLayout *) item->data;
190 if (g_strcmp0 (lightdm_layout_get_name (iter_layout), full_name) == 0)
192 default_layout = iter_layout;
200 return default_layout;
204 * lightdm_layout_get_name:
205 * @layout: A #LightDMLayout
207 * Get the name of a layout.
209 * Return value: The name of the layout
212 lightdm_layout_get_name (LightDMLayout *layout)
214 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
215 return GET_PRIVATE (layout)->name;
219 * lightdm_layout_get_short_description:
220 * @layout: A #LightDMLayout
222 * Get the short description of a layout.
224 * Return value: A short description of the layout
227 lightdm_layout_get_short_description (LightDMLayout *layout)
229 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
230 return GET_PRIVATE (layout)->short_description;
234 * lightdm_layout_get_description:
235 * @layout: A #LightDMLayout
237 * Get the long description of a layout.
239 * Return value: A long description of the layout
242 lightdm_layout_get_description (LightDMLayout *layout)
244 g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
245 return GET_PRIVATE (layout)->description;
249 lightdm_layout_init (LightDMLayout *layout)
254 lightdm_layout_set_property (GObject *object,
259 LightDMLayout *self = LIGHTDM_LAYOUT (object);
260 LightDMLayoutPrivate *priv = GET_PRIVATE (self);
265 priv->name = g_strdup (g_value_get_string (value));
267 case PROP_SHORT_DESCRIPTION:
268 g_free (priv->short_description);
269 priv->short_description = g_strdup (g_value_get_string (value));
271 case PROP_DESCRIPTION:
272 g_free (priv->description);
273 priv->description = g_strdup (g_value_get_string (value));
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 lightdm_layout_get_property (GObject *object,
289 self = LIGHTDM_LAYOUT (object);
293 g_value_set_string (value, lightdm_layout_get_name (self));
295 case PROP_SHORT_DESCRIPTION:
296 g_value_set_string (value, lightdm_layout_get_short_description (self));
298 case PROP_DESCRIPTION:
299 g_value_set_string (value, lightdm_layout_get_description (self));
302 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 lightdm_layout_class_init (LightDMLayoutClass *klass)
310 GObjectClass *object_class = G_OBJECT_CLASS (klass);
312 g_type_class_add_private (klass, sizeof (LightDMLayoutPrivate));
314 object_class->set_property = lightdm_layout_set_property;
315 object_class->get_property = lightdm_layout_get_property;
317 g_object_class_install_property (object_class,
319 g_param_spec_string ("name",
321 "Name of the layout",
323 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
324 g_object_class_install_property (object_class,
325 PROP_SHORT_DESCRIPTION,
326 g_param_spec_string ("short-description",
328 "Short description of the layout",
330 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
331 g_object_class_install_property (object_class,
333 g_param_spec_string ("description",
335 "Long description of the layout",
337 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));