source: proiecte/PPPP/gdm/gui/simple-greeter/gdm-layouts.c @ 134

Last change on this file since 134 was 134, checked in by (none), 14 years ago

gdm sources with the modifications for webcam

File size: 7.2 KB
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright 2008  Red Hat, Inc,
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Written by : Matthias Clasen
20 */
21
22#include "config.h"
23
24#include <string.h>
25
26#include <glib.h>
27
28#include <gdk/gdkx.h>
29
30#ifdef HAVE_LIBXKLAVIER
31#include <libxklavier/xklavier.h>
32#endif
33
34#include <gconf/gconf-client.h>
35
36#include "gdm-layouts.h"
37
38typedef struct {
39        GSList *list;
40        const char *layout;
41} LayoutData;
42
43#ifdef HAVE_LIBXKLAVIER
44static XklEngine         *engine = NULL;
45static XklConfigRegistry *config_registry = NULL;
46static XklConfigRec      *initial_config = NULL;
47
48static void
49init_xkl (void)
50{
51        if (config_registry == NULL) {
52                engine = xkl_engine_get_instance (GDK_DISPLAY ());
53                xkl_engine_backup_names_prop (engine);
54                config_registry = xkl_config_registry_get_instance (engine);
55                xkl_config_registry_load (config_registry, FALSE);
56
57                initial_config = xkl_config_rec_new ();
58                if (!xkl_config_rec_get_from_backup (initial_config, engine)) {
59                        g_warning ("failed to load XKB configuration");
60                        initial_config->model = g_strdup ("pc105");
61                }
62        }
63}
64
65static char *
66xci_desc_to_utf8 (XklConfigItem * ci)
67{
68        char *sd = g_strstrip (ci->description);
69        return sd[0] == 0 ? g_strdup (ci->name) :
70                g_locale_to_utf8 (sd, -1, NULL, NULL, NULL);
71}
72
73static void
74add_variant (XklConfigRegistry   *config,
75             const XklConfigItem *item,
76             gpointer             data)
77{
78        LayoutData *ldata = data;
79
80        ldata->list = g_slist_prepend (ldata->list, g_strdup_printf  ("%s\t%s", ldata->layout, item->name));
81}
82
83static void
84add_layout (XklConfigRegistry   *config,
85            const XklConfigItem *item,
86            gpointer             data)
87{
88        LayoutData *ldata = data;
89
90        ldata->layout = item->name;
91        ldata->list = g_slist_prepend (ldata->list, g_strdup (item->name));
92        xkl_config_registry_foreach_layout_variant (config, item->name, add_variant, data);
93        ldata->layout = NULL;
94}
95#endif
96
97gchar *
98gdm_get_layout_from_name (const char *name)
99{
100#ifdef HAVE_LIBXKLAVIER
101        XklConfigItem *item;
102        char          *layout;
103        char          *variant;
104        char          *result;
105        char          *id1;
106        char          *id2;
107        char          *p;
108
109        init_xkl ();
110
111        id1 = g_strdup (name);
112        p = strchr (id1, '\t');
113
114        if (p != NULL) {
115                id2 = p + 1;
116                *p = 0;
117        } else {
118                id2 = NULL;
119        }
120
121        item = xkl_config_item_new ();
122
123        g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", id1);
124        if (xkl_config_registry_find_layout (config_registry, item)) {
125                layout = xci_desc_to_utf8 (item);
126        } else {
127                layout =  g_strdup_printf ("Layout %s", id1);
128        }
129
130        if (id2 != NULL) {
131                g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", id2);
132                if (xkl_config_registry_find_variant (config_registry, id1, item))
133                        variant = xci_desc_to_utf8 (item);
134                else
135                        variant = g_strdup_printf ("Variant %s", id2);
136        } else {
137                variant = NULL;
138        }
139
140        g_object_unref (item);
141
142        g_free (id1);
143
144        if (variant != NULL) {
145                result = g_strdup_printf ("%s (%s)", layout, variant);
146                g_free (layout);
147                g_free (variant);
148        } else {
149                result = layout;
150        }
151
152        return result;
153#else
154        return NULL;
155#endif
156}
157
158char **
159gdm_get_all_layout_names (void)
160{
161#ifdef HAVE_LIBXKLAVIER
162        GSList    *l;
163        int        len;
164        int        i;
165        char     **layouts;
166        LayoutData data;
167
168        data.list = NULL;
169        data.layout = NULL;
170
171        init_xkl ();
172
173        xkl_config_registry_foreach_layout (config_registry, add_layout, &data);
174
175        len = g_slist_length (data.list);
176
177        layouts = g_new (char *, len + 1);
178        layouts[len] = NULL;
179
180        for (i = 0, l = data.list; i < len; i++, l = l->next) {
181                layouts[len - i - 1] = l->data;
182        }
183
184        g_slist_free (data.list);
185
186        return layouts;
187#else
188        return NULL;
189#endif
190}
191
192gboolean
193gdm_layout_is_valid (const char *layout_variant)
194{
195#ifdef HAVE_LIBXKLAVIER
196        XklConfigItem *item;
197        char          *layout;
198        char          *variant;
199        gboolean       retval;
200
201        layout = g_strdup (layout_variant);
202        variant = strchr (layout, '\t');
203        if (variant != NULL) {
204                variant[0] = '\0';
205                variant++;
206        }
207
208        item = xkl_config_item_new ();
209        g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", layout);
210
211        retval = xkl_config_registry_find_layout (config_registry, item);
212
213        if (retval && variant != NULL) {
214                g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", variant);
215                retval = xkl_config_registry_find_variant (config_registry, layout, item);
216        }
217
218        g_object_unref (item);
219        g_free (layout);
220
221        return retval;
222#else
223        return TRUE;
224#endif
225}
226
227const char *
228gdm_layout_get_default_layout (void)
229{
230#ifdef HAVE_LIBXKLAVIER
231        init_xkl ();
232
233        if (initial_config->layouts)
234                return initial_config->layouts[0];
235        else
236                return NULL;
237#else
238        return NULL;
239#endif
240}
241
242void
243gdm_layout_activate (const char *layout)
244{
245#ifdef HAVE_LIBXKLAVIER
246        XklConfigRec *config;
247        char         *p;
248
249        init_xkl ();
250
251        config = xkl_config_rec_new ();
252        config->model = g_strdup (initial_config->model);
253
254        if (layout == NULL) {
255                config->layouts = g_strdupv (initial_config->layouts);
256                config->variants = g_strdupv (initial_config->variants);
257                config->options = g_strdupv (initial_config->options);
258        } else {
259                config->layouts = g_new0 (char *, 2);
260                config->layouts[0] = g_strdup (layout);
261
262                p = strchr (config->layouts[0], '\t');
263                if (p != NULL) {
264                        config->variants = g_new0 (char *, 2);
265                        config->layouts[0][p - config->layouts[0]] = 0;
266                        config->variants[0] = g_strdup (p + 1);
267                }
268        }
269
270        xkl_config_rec_activate (config, engine);
271
272        g_object_unref (config);
273#endif
274}
275
Note: See TracBrowser for help on using the repository browser.