1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 translate.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
25 static guint translate_modifier(gchar *str)
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);
43 gboolean translate_button(const gchar *str, guint *state, guint *button)
50 parsed = g_strsplit(str, "-", -1);
52 /* first, find the button (last token) */
54 for (i = 0; parsed[i] != NULL; ++i)
57 goto translation_fail;
59 /* figure out the mod mask */
61 for (i = 0; parsed[i] != l; ++i) {
62 guint m = translate_modifier(parsed[i]);
63 if (!m) goto translation_fail;
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);
75 g_warning("Invalid button '%s' in pointer binding.", l);
76 goto translation_fail;
86 gboolean translate_key(const gchar *str, guint *state, guint *keycode)
94 parsed = g_strsplit(str, "-", -1);
96 /* first, find the key (last token) */
98 for (i = 0; parsed[i] != NULL; ++i)
101 goto translation_fail;
103 /* figure out the mod mask */
105 for (i = 0; parsed[i] != l; ++i) {
106 guint m = translate_modifier(parsed[i]);
107 if (!m) goto translation_fail;
111 if (!g_ascii_strncasecmp("0x", l, 2)) {
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;
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;
127 *keycode = XKeysymToKeycode(ob_display, sym);
130 g_warning("Key '%s' does not exist on the display.", l);
131 goto translation_fail;