Definition of IM_CONF_DIR moved to prefs.h
[ukeyboard.git] / cpanel / hw.c
blobfdb040a7b1ed385339b99ee4a57af4a04505ae0e
1 /*
2 * Copyright (c) 2008 Jiri Benc <jbenc@upir.cz>
3 * Copyright (c) 2009 Roman Moravcik <roman.moravcik@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <stdio.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <gtk/gtk.h>
23 #include <hildon/hildon-caption.h>
24 #include <hildon/hildon.h>
25 #include <libosso.h>
26 #include <gconf/gconf.h>
27 #include <gconf/gconf-client.h>
28 #include "prefs.h"
29 #include "hw.h"
31 #define GETTEXT_PACKAGE "osso-applet-textinput"
32 #include <glib/gi18n-lib.h>
34 struct layout {
35 gchar *model;
36 gchar *layout;
37 gchar *name;
40 struct data {
41 GList *layouts;
42 HildonTouchSelector *combo;
45 typedef struct {
46 gchar *layout;
47 gchar *name;
48 } layouts;
50 static layouts layout_names[] = {
51 {"cz", "Čeština"},
52 {"cz_qwerty", "Čeština - QWERTY"},
53 {"dano", "Dansk, Norsk"},
54 {"de", "Deutsch"},
55 {"us", "English, Nederlands"},
56 {"ptes", "Español, Français (Canada), Português"},
57 {"fr", "Français (France)"},
58 {"it", "Italiano"},
59 {"pl", "Polski"},
60 {"fise", "Suomi, Svenska"},
61 {"ch", "Suisse, Schweiz"},
62 {"ru", "Русский"},
63 {"sk", "Slovenčina"},
64 {"sk_qwerty", "Slovenčina - QWERTY"},
65 {"aren", "Arabic, English"},
66 {"dv", "Dvorak"},
67 {"gr", "Ελληνικά"},
68 {"bg_phonetic", "Български - Phonetic"},
69 {"faen", "Persian, English"},
70 {"mk", "Македонски"},
71 {"ru_phonetic", "Русский - Phonetic"},
72 {"he_phonetic", "עברית - Phonetic"},
73 {"ge", "Georgian - Latin"},
74 {"lv", "Latviešu"},
75 {NULL, NULL}
78 static gchar *resolve_layout_name(const gchar *layout)
80 unsigned char i = 0;
82 while (layout_names[i].layout != NULL)
84 if (!strcmp(layout_names[i].layout, layout))
85 return layout_names[i].name;
86 i++;
88 return NULL;
91 static char *strip(char *s)
93 while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
94 s++;
95 return s;
98 static GList *get_layouts(gchar *path, gchar *model, GList *list)
100 FILE *f;
101 char *buf, *s, *s2;
102 gchar *layout = NULL;
103 gchar *name = NULL;
104 struct layout *lay;
106 f = fopen(path, "r");
107 if (!f)
108 return list;
109 buf = g_malloc(512);
110 if (!buf) {
111 fclose(f);
112 return list;
114 while (fgets(buf, 512, f)) {
115 s = strip(buf);
116 if (!strncmp(s, "xkb_symbols", 11)) {
117 if (layout) {
118 g_free(layout);
119 layout = NULL;
121 s = strip(s + 11);
122 if (*s != '"')
123 continue;
124 s++;
125 s2 = strchr(s, '"');
126 if (!s2)
127 continue;
128 *s2 = '\0';
129 layout = g_strdup(s);
131 /* ignore nordic layout */
132 if (!strcmp(layout, "nordic")) {
133 layout = NULL;
134 continue;
137 /* WORKAROUND: ignore cz_qwerty nokiarx51 layout,
138 because it's broken in PR1.1 and PR1.1.1 */
139 if (!strcmp(model, "nokiarx51")) {
140 if (!strcmp(layout, "cz_qwerty")) {
141 layout = NULL;
142 continue;
146 name = resolve_layout_name(layout);
147 if (name)
149 lay = g_malloc(sizeof(struct layout));
150 lay->model = g_strdup(model);
151 lay->layout = layout;
152 lay->name = g_strdup(name);
153 layout = NULL;
154 list = g_list_append(list, lay);
155 continue;
157 } else if (!strncmp(s, "name", 4) && layout) {
158 s = strip(s + 4);
159 if (*s != '[')
160 continue;
161 s2 = strchr(s, ']');
162 if (!s2)
163 continue;
164 s = strip(s2 + 1);
165 if (*s != '=')
166 continue;
167 s = strip(s + 1);
168 if (*s != '"')
169 continue;
170 s++;
171 s2 = strchr(s, '"');
172 if (!s2)
173 continue;
174 *s2 = '\0';
176 lay = g_malloc(sizeof(struct layout));
177 lay->model = g_strdup(model);
178 lay->layout = layout;
179 lay->name = g_strdup(s);
180 layout = NULL;
181 list = g_list_append(list, lay);
184 fclose(f);
185 return list;
188 static void free_layouts(GList *list)
190 GList *item;
191 struct layout *lay;
193 for (item = list; item; item = g_list_next(item)) {
194 lay = item->data;
195 g_free(lay->model);
196 g_free(lay->layout);
197 g_free(lay->name);
198 g_free(lay);
200 g_list_free(list);
203 static gint layouts_compare_func(gconstpointer a, gconstpointer b)
205 struct layout *layout_a = (struct layout *) a;
206 struct layout *layout_b = (struct layout *) b;
208 return g_utf8_collate (layout_a->name, layout_b->name);
211 static GtkWidget *start(GConfClient *client, GtkWidget *win, void **data)
213 struct data *d;
214 GList *item;
215 gchar *omodel, *olayout;
216 struct layout *lay;
217 unsigned i;
219 GtkWidget *button;
221 (void)win;
223 if (!internal_kbd) {
224 *data = NULL;
225 return NULL;
228 d = g_malloc(sizeof(struct data));
230 omodel = get_str(client, "int_kb_model");
231 olayout = get_str(client, "int_kb_layout");
232 d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/rx-51", "nokiarx51", NULL);
233 d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/ukeyboard", "ukeyboard", d->layouts);
234 d->layouts = g_list_sort(d->layouts, layouts_compare_func);
236 d->combo = HILDON_TOUCH_SELECTOR(hildon_touch_selector_new_text());
238 button = hildon_picker_button_new(HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
239 hildon_button_set_title(HILDON_BUTTON(button), _("tein_fi_keyboard_layout"));
240 hildon_picker_button_set_selector(HILDON_PICKER_BUTTON (button), d->combo);
241 hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
242 hildon_button_set_title_alignment(HILDON_BUTTON(button), 0.0, 0.5);
243 hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
245 /* WORKAROUND: if int_kb_model is set to nokiarx44 on rx-51 device,
246 then set omodel to nokiarx51. Without this workaround, no hardwere
247 keyboard was automatically selected after first run */
248 if (omodel && !strcmp(omodel, "nokiarx44")) {
249 g_free (omodel);
250 omodel = g_strdup("nokiarx51");
253 for (item = d->layouts, i = 0; item; item = g_list_next(item), i++) {
254 lay = item->data;
255 hildon_touch_selector_append_text(d->combo, lay->name);
256 if (omodel && olayout && !strcmp(lay->model, omodel) && !strcmp(lay->layout, olayout))
257 hildon_touch_selector_set_active(d->combo, 0, i);
260 g_free(olayout);
261 g_free(omodel);
263 *data = d;
265 gtk_widget_show(button);
267 return button;
270 static void action(GConfClient *client, void *data)
272 struct data *d = data;
273 struct layout *lay;
274 int res;
276 if (!d)
277 return;
278 res = hildon_touch_selector_get_active(d->combo, 0);
279 if (res >= 0) {
280 lay = g_list_nth_data(d->layouts, res);
281 if (lay) {
282 set_str(client, "int_kb_model", lay->model);
283 set_str(client, "int_kb_layout", lay->layout);
288 static void stop(GConfClient *client, void *data)
290 struct data *d = data;
292 (void)client;
293 if (d) {
294 free_layouts(d->layouts);
295 g_free(d);
299 void prefs_hw_init(struct prefs *prefs)
301 prefs->start = start;
302 prefs->action = action;
303 prefs->stop = stop;
304 prefs->name = "Hardware";