2 * Copyright (c) 2008 Jiri Benc <jbenc@upir.cz>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <sys/types.h>
23 #include <glib/gstdio.h>
24 #include <gconf/gconf.h>
25 #include <gconf/gconf-client.h>
26 #include "../vkb_compiler.h"
27 #include "ukbdcreator.h"
29 static gchar *saved_layout = NULL;
30 static gchar *saved_lang = NULL;
31 static gchar *act_layout = NULL;
40 static void error(void *p, int line, char *msg)
43 disp_compile_error(line, msg);
46 static int warning(void *p, int line, char *msg)
49 disp_compile_error(line, msg);
53 static int get_line(void *p, void *buf, size_t size)
55 struct priv *priv = p;
59 while (priv->buf[priv->pos]) {
61 dbuf[dpos++] = priv->buf[priv->pos];
62 if (priv->buf[priv->pos++] == '\n')
66 return (priv->buf[priv->pos] || dpos) ? 0 : -1;
69 static int write_buf(void *p, void *buf, size_t size)
71 struct priv *priv = p;
73 if (write(priv->fout, buf, size) != (ssize_t)size) {
74 disp_error("Error writing temporary file");
80 static off_t tell_buf(void *p)
82 struct priv *priv = p;
84 return lseek(priv->fout, 0, SEEK_CUR);
87 static void seek_buf(void *p, off_t pos)
89 struct priv *priv = p;
91 lseek(priv->fout, pos, SEEK_SET);
94 static void return_lang(void *p, char *lang)
96 struct priv *priv = p;
98 priv->lang = g_strdup(lang);
101 static struct compiler_ops ops = {
102 .get_line = get_line,
108 .return_lang = return_lang,
111 gboolean compile_layout(gchar *buf, gchar **fname, gchar **lang)
120 *fname = g_build_filename(g_get_tmp_dir(), "ukbdXXXXXX", NULL);
121 priv.fout = g_mkstemp(*fname);
124 disp_error("Error creating temporary file");
127 res = compile(&ops, &priv);
139 static gchar *get_lang(GConfClient *conf)
141 return gconf_client_get_string(conf,
142 "/apps/osso/inputmethod/hildon-im-languages/language-0", NULL);
145 static void set_lang(GConfClient *conf, gchar *val)
147 gconf_client_set_string(conf,
148 "/apps/osso/inputmethod/hildon-im-languages/language-0", val, NULL);
151 void test_layout(GConfClient *conf, gchar *fname, gchar *lang)
157 restore_layout(conf, FALSE);
158 cmd = g_strdup_printf("sudo /usr/libexec/ukeyboard-set -s %s %s", fname, lang);
161 if (!WIFEXITED(res)) {
162 disp_error("Cannot execute helper script");
165 res = WEXITSTATUS(res);
167 disp_error("Redefining a system language is not possible");
171 disp_error("Activating of the layout failed");
174 saved_lang = g_strdup(lang);
175 saved_layout = get_lang(conf);
176 set_lang(conf, "en_GB");
177 set_lang(conf, lang);
179 g_unlink(act_layout);
182 act_layout = g_strdup(fname);
183 disp_info("Layout activated");
186 void restore_layout(GConfClient *conf, gboolean warn)
193 disp_info("No layout to restore");
196 cmd = g_strdup_printf("sudo /usr/libexec/ukeyboard-set -r %s", saved_lang);
199 if (!WIFEXITED(res)) {
200 disp_error("Cannot execute helper script");
203 res = WEXITSTATUS(res);
205 disp_error("Restoring of original layout failed");
208 set_lang(conf, "en_GB");
209 set_lang(conf, saved_layout);
211 g_free(saved_layout);
212 saved_lang = saved_layout = NULL;
214 g_unlink(act_layout);
219 disp_info("Layout restored");