]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/translate.c
Mod3 = M
[mikachu/openbox.git] / openbox / translate.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    translate.c for the Openbox window manager
4    Copyright (c) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "openbox.h"
20 #include "mouse.h"
21 #include <glib.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 static guint translate_modifier(gchar *str)
26 {
27     if (!g_ascii_strcasecmp("Mod1", str) ||
28         !g_ascii_strcasecmp("A", str)) return Mod1Mask;
29     else if (!g_ascii_strcasecmp("Mod2", str)) return Mod2Mask;
30     else if (!g_ascii_strcasecmp("Mod3", str) ||
31              !g_ascii_strcasecmp("M", str)) return Mod3Mask;
32     else if (!g_ascii_strcasecmp("Mod4", str) ||
33              !g_ascii_strcasecmp("W", str)) return Mod4Mask;
34     else if (!g_ascii_strcasecmp("Mod5", str)) return Mod5Mask;
35     else if (!g_ascii_strcasecmp("Control", str) ||
36              !g_ascii_strcasecmp("C", str)) return ControlMask;
37     else if (!g_ascii_strcasecmp("Shift", str) ||
38              !g_ascii_strcasecmp("S", str)) return ShiftMask;
39     g_warning("Invalid modifier '%s' in binding.", str);
40     return 0;
41 }
42
43 gboolean translate_button(const gchar *str, guint *state, guint *button)
44 {
45     gchar **parsed;
46     gchar *l;
47     gint i;
48     gboolean ret = FALSE;
49
50     parsed = g_strsplit(str, "-", -1);
51     
52     /* first, find the button (last token) */
53     l = NULL;
54     for (i = 0; parsed[i] != NULL; ++i)
55         l = parsed[i];
56     if (l == NULL)
57         goto translation_fail;
58
59     /* figure out the mod mask */
60     *state = 0;
61     for (i = 0; parsed[i] != l; ++i) {
62         guint m = translate_modifier(parsed[i]);
63         if (!m) goto translation_fail;
64         *state |= m;
65     }
66
67     /* figure out the button */
68     if (!g_ascii_strcasecmp("Left", l)) *button = 1;
69     else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
70     else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
71     else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
72     else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
73     else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
74     if (!*button) {
75         g_warning("Invalid button '%s' in pointer binding.", l);
76         goto translation_fail;
77     }
78
79     ret = TRUE;
80
81 translation_fail:
82     g_strfreev(parsed);
83     return ret;
84 }
85
86 gboolean translate_key(const gchar *str, guint *state, guint *keycode)
87 {
88     gchar **parsed;
89     gchar *l;
90     gint i;
91     gboolean ret = FALSE;
92     KeySym sym;
93
94     parsed = g_strsplit(str, "-", -1);
95     
96     /* first, find the key (last token) */
97     l = NULL;
98     for (i = 0; parsed[i] != NULL; ++i)
99         l = parsed[i];
100     if (l == NULL)
101         goto translation_fail;
102
103     /* figure out the mod mask */
104     *state = 0;
105     for (i = 0; parsed[i] != l; ++i) {
106         guint m = translate_modifier(parsed[i]);
107         if (!m) goto translation_fail;
108         *state |= m;
109     }
110
111     if (!g_ascii_strncasecmp("0x", l, 2)) {
112         gchar *end;
113
114         /* take it directly */
115         *keycode = strtol(l, &end, 16);
116         if (*l == '\0' || *end != '\0') {
117             g_warning("Invalid key code '%s' in key binding.", l);
118             goto translation_fail;
119         }
120     } else {
121         /* figure out the keycode */
122         sym = XStringToKeysym(l);
123         if (sym == NoSymbol) {
124             g_warning("Invalid key name '%s' in key binding.", l);
125             goto translation_fail;
126         }
127         *keycode = XKeysymToKeycode(ob_display, sym);
128     }
129     if (!*keycode) {
130         g_warning("Key '%s' does not exist on the display.", l); 
131         goto translation_fail;
132     }
133
134     ret = TRUE;
135
136 translation_fail:
137     g_strfreev(parsed);
138     return ret;
139 }