]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-gobject/layout.c
fix typo that prevented lightdm_get_layout from working
[sojka/lightdm.git] / liblightdm-gobject / layout.c
1 /*
2  * Copyright (C) 2010 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  * 
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
9  * license.
10  */
11
12 #include <libxklavier/xklavier.h>
13
14 #include "lightdm/layout.h"
15
16 enum {
17     PROP_0,
18     PROP_NAME,
19     PROP_SHORT_DESCRIPTION,
20     PROP_DESCRIPTION
21 };
22
23 typedef struct
24 {
25     gchar *name;
26     gchar *short_description;
27     gchar *description;
28 } LightDMLayoutPrivate;
29
30 G_DEFINE_TYPE (LightDMLayout, lightdm_layout, G_TYPE_OBJECT);
31
32 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_LAYOUT, LightDMLayoutPrivate)
33
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
40 static void
41 layout_cb (XklConfigRegistry *config,
42            const XklConfigItem *item,
43            gpointer data)
44 {
45     LightDMLayout *layout;
46
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);
49 }
50
51 /**
52  * lightdm_get_layouts:
53  *
54  * Get a list of keyboard layouts to present to the user.
55  *
56  * Return value: (element-type LightDMLayout) (transfer none): A list of #LightDMLayout that should be presented to the user.
57  **/
58 GList *
59 lightdm_get_layouts (void)
60 {
61     XklConfigRegistry *registry;
62
63     if (have_layouts)
64         return layouts;
65
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");
71
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);
76
77     have_layouts = TRUE;
78
79     return layouts;
80 }
81
82 /**
83  * lightdm_set_layout:
84  * @layout: The layout to use
85  *
86  * Set the layout for this session.
87  **/
88 void
89 lightdm_set_layout (LightDMLayout *layout)
90 {
91     XklConfigRec *config;
92
93     g_return_if_fail (layout != NULL);
94
95     g_debug ("Setting keyboard layout to %s", lightdm_layout_get_name (layout));
96
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);
105 }
106
107 /**
108  * lightdm_get_layout:
109  *
110  * Get the current keyboard layout.
111  *
112  * Return value: (transfer none): The currently active layout for this user.
113  **/
114 LightDMLayout *
115 lightdm_get_layout (void)
116 {
117     lightdm_get_layouts ();
118     if (layouts)
119         return (LightDMLayout *) g_list_first (layouts)->data;
120     else
121         return NULL;
122 }
123
124 /**
125  * lightdm_layout_get_name:
126  * @layout: A #LightDMLayout
127  * 
128  * Get the name of a layout.
129  * 
130  * Return value: The name of the layout
131  **/
132 const gchar *
133 lightdm_layout_get_name (LightDMLayout *layout)
134 {
135     g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
136     return GET_PRIVATE (layout)->name;
137 }
138
139 /**
140  * lightdm_layout_get_short_description:
141  * @layout: A #LightDMLayout
142  * 
143  * Get the short description of a layout.
144  *
145  * Return value: A short description of the layout
146  **/
147 const gchar *
148 lightdm_layout_get_short_description (LightDMLayout *layout)
149 {
150     g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
151     return GET_PRIVATE (layout)->short_description;
152 }
153
154 /**
155  * lightdm_layout_get_description:
156  * @layout: A #LightDMLayout
157  * 
158  * Get the long description of a layout.
159  * 
160  * Return value: A long description of the layout
161  **/
162 const gchar *
163 lightdm_layout_get_description (LightDMLayout *layout)
164 {
165     g_return_val_if_fail (LIGHTDM_IS_LAYOUT (layout), NULL);
166     return GET_PRIVATE (layout)->description;
167 }
168
169 static void
170 lightdm_layout_init (LightDMLayout *layout)
171 {
172 }
173
174 static void
175 lightdm_layout_set_property (GObject      *object,
176                          guint         prop_id,
177                          const GValue *value,
178                          GParamSpec   *pspec)
179 {
180     LightDMLayout *self = LIGHTDM_LAYOUT (object);
181     LightDMLayoutPrivate *priv = GET_PRIVATE (self);
182
183     switch (prop_id) {
184     case PROP_NAME:
185         g_free (priv->name);
186         priv->name = g_strdup (g_value_get_string (value));
187         break;
188     case PROP_SHORT_DESCRIPTION:
189         g_free (priv->short_description);
190         priv->short_description = g_strdup (g_value_get_string (value));
191         break;
192     case PROP_DESCRIPTION:
193         g_free (priv->description);
194         priv->description = g_strdup (g_value_get_string (value));
195         break;
196     default:
197         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198         break;
199     }
200 }
201
202 static void
203 lightdm_layout_get_property (GObject    *object,
204                          guint       prop_id,
205                          GValue     *value,
206                          GParamSpec *pspec)
207 {
208     LightDMLayout *self;
209
210     self = LIGHTDM_LAYOUT (object);
211
212     switch (prop_id) {
213     case PROP_NAME:
214         g_value_set_string (value, lightdm_layout_get_name (self));
215         break;
216     case PROP_SHORT_DESCRIPTION:
217         g_value_set_string (value, lightdm_layout_get_short_description (self));
218         break;
219     case PROP_DESCRIPTION:
220         g_value_set_string (value, lightdm_layout_get_description (self));
221         break;
222     default:
223         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224         break;
225     }
226 }
227
228 static void
229 lightdm_layout_class_init (LightDMLayoutClass *klass)
230 {
231     GObjectClass *object_class = G_OBJECT_CLASS (klass);
232   
233     g_type_class_add_private (klass, sizeof (LightDMLayoutPrivate));
234
235     object_class->set_property = lightdm_layout_set_property;
236     object_class->get_property = lightdm_layout_get_property;
237
238     g_object_class_install_property (object_class,
239                                      PROP_NAME,
240                                      g_param_spec_string ("name",
241                                                           "name",
242                                                           "Name of the layout",
243                                                           NULL,
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",
248                                                           "short-description",
249                                                           "Short description of the layout",
250                                                           NULL,
251                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
252     g_object_class_install_property (object_class,
253                                      PROP_DESCRIPTION,
254                                      g_param_spec_string ("description",
255                                                           "description",
256                                                           "Long description of the layout",
257                                                           NULL,
258                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
259 }