allow the use of keycodes directly in key bindings
[dana/openbox.git] / openbox / translate.c
1 #include "openbox.h"
2 #include "mouse.h"
3 #include <glib.h>
4 #include <string.h>
5 #include <stdlib.h>
6
7 static guint translate_modifier(char *str)
8 {
9     if (!g_ascii_strcasecmp("Mod1", str) ||
10         !g_ascii_strcasecmp("A", str)) return Mod1Mask;
11     else if (!g_ascii_strcasecmp("Mod2", str)) return Mod2Mask;
12     else if (!g_ascii_strcasecmp("Mod3", str)) return Mod3Mask;
13     else if (!g_ascii_strcasecmp("Mod4", str) ||
14              !g_ascii_strcasecmp("W", str)) return Mod4Mask;
15     else if (!g_ascii_strcasecmp("Mod5", str)) return Mod5Mask;
16     else if (!g_ascii_strcasecmp("Control", str) ||
17              !g_ascii_strcasecmp("C", str)) return ControlMask;
18     else if (!g_ascii_strcasecmp("Shift", str) ||
19              !g_ascii_strcasecmp("S", str)) return ShiftMask;
20     g_warning("Invalid modifier '%s' in binding.", str);
21     return 0;
22 }
23
24 gboolean translate_button(char *str, guint *state, guint *button)
25 {
26     char **parsed;
27     char *l;
28     int i;
29     gboolean ret = FALSE;
30
31     parsed = g_strsplit(str, "-", -1);
32     
33     /* first, find the button (last token) */
34     l = NULL;
35     for (i = 0; parsed[i] != NULL; ++i)
36         l = parsed[i];
37     if (l == NULL)
38         goto translation_fail;
39
40     /* figure out the mod mask */
41     *state = 0;
42     for (i = 0; parsed[i] != l; ++i) {
43         guint m = translate_modifier(parsed[i]);
44         if (!m) goto translation_fail;
45         *state |= m;
46     }
47
48     /* figure out the button */
49     if (!g_ascii_strcasecmp("Left", l)) *button = 1;
50     else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
51     else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
52     else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
53     else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
54     else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
55     if (!*button) {
56         g_warning("Invalid button '%s' in pointer binding.", l);
57         goto translation_fail;
58     }
59
60     ret = TRUE;
61
62 translation_fail:
63     g_strfreev(parsed);
64     return ret;
65 }
66
67 gboolean translate_key(char *str, guint *state, guint *keycode)
68 {
69     char **parsed;
70     char *l;
71     int i;
72     gboolean ret = FALSE;
73     KeySym sym;
74
75     parsed = g_strsplit(str, "-", -1);
76     
77     /* first, find the key (last token) */
78     l = NULL;
79     for (i = 0; parsed[i] != NULL; ++i)
80         l = parsed[i];
81     if (l == NULL)
82         goto translation_fail;
83
84     /* figure out the mod mask */
85     *state = 0;
86     for (i = 0; parsed[i] != l; ++i) {
87         guint m = translate_modifier(parsed[i]);
88         if (!m) goto translation_fail;
89         *state |= m;
90     }
91
92     if (!g_ascii_strncasecmp("0x", l, 2)) {
93         gchar *end;
94
95         /* take it directly */
96         *keycode = strtol(l, &end, 16);
97         if (*l == '\0' || *end != '\0') {
98             g_warning("Invalid key code '%s' in key binding.", l);
99             goto translation_fail;
100         }
101     } else {
102         /* figure out the keycode */
103         sym = XStringToKeysym(l);
104         if (sym == NoSymbol) {
105             g_warning("Invalid key name '%s' in key binding.", l);
106             goto translation_fail;
107         }
108         *keycode = XKeysymToKeycode(ob_display, sym);
109     }
110     if (!*keycode) {
111         g_warning("Key '%s' does not exist on the display.", l); 
112         goto translation_fail;
113     }
114
115     ret = TRUE;
116
117 translation_fail:
118     g_strfreev(parsed);
119     return ret;
120 }