]> icculus.org git repositories - dana/openbox.git/blob - plugins/keyboard/translate.c
mouse and key bindings plugins work. segfault somewhere still on shutdown
[dana/openbox.git] / plugins / keyboard / translate.c
1 #include "../../kernel/openbox.h"
2 #include <glib.h>
3 #include <string.h>
4
5 static guint translate_modifier(char *str)
6 {
7     if (!strcmp("Mod1", str) || !strcmp("A", str)) return Mod1Mask;
8     else if (!strcmp("Mod2", str)) return Mod2Mask;
9     else if (!strcmp("Mod3", str)) return Mod3Mask;
10     else if (!strcmp("Mod4", str) || !strcmp("W", str)) return Mod4Mask;
11     else if (!strcmp("Mod5", str)) return Mod5Mask;
12     else if (!strcmp("C", str)) return ControlMask;
13     else if (!strcmp("S", str)) return ShiftMask;
14     g_warning("Invalid modifier '%s' in binding.", str);
15     return 0;
16 }
17
18 gboolean translate_key(char *str, guint *state, guint *keycode)
19 {
20     char **parsed;
21     char *l;
22     int i;
23     gboolean ret = FALSE;
24     KeySym sym;
25
26     parsed = g_strsplit(str, "-", -1);
27     
28     /* first, find the key (last token) */
29     l = NULL;
30     for (i = 0; parsed[i] != NULL; ++i)
31         l = parsed[i];
32     if (l == NULL)
33         goto translation_fail;
34
35     /* figure out the mod mask */
36     *state = 0;
37     for (i = 0; parsed[i] != l; ++i) {
38         guint m = translate_modifier(parsed[i]);
39         if (!m) goto translation_fail;
40         *state |= m;
41     }
42
43     /* figure out the keycode */
44     sym = XStringToKeysym(l);
45     if (sym == NoSymbol) {
46         g_warning("Invalid key name '%s' in key binding.", l);
47         goto translation_fail;
48     }
49     *keycode = XKeysymToKeycode(ob_display, sym);
50     if (!keycode) {
51         g_warning("Key '%s' does not exist on the display.", l); 
52         goto translation_fail;
53     }
54
55     ret = TRUE;
56
57 translation_fail:
58     g_strfreev(parsed);
59     return ret;
60 }