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)) return Mod3Mask;
31 else if (!g_ascii_strcasecmp("Mod4", str) ||
32 !g_ascii_strcasecmp("W", str)) return Mod4Mask;
33 else if (!g_ascii_strcasecmp("Mod5", str)) return Mod5Mask;
34 else if (!g_ascii_strcasecmp("Control", str) ||
35 !g_ascii_strcasecmp("C", str)) return ControlMask;
36 else if (!g_ascii_strcasecmp("Shift", str) ||
37 !g_ascii_strcasecmp("S", str)) return ShiftMask;
38 g_warning("Invalid modifier '%s' in binding.", str);
42 gboolean translate_button(const gchar *str, guint *state, guint *button)
49 parsed = g_strsplit(str, "-", -1);
51 /* first, find the button (last token) */
53 for (i = 0; parsed[i] != NULL; ++i)
56 goto translation_fail;
58 /* figure out the mod mask */
60 for (i = 0; parsed[i] != l; ++i) {
61 guint m = translate_modifier(parsed[i]);
62 if (!m) goto translation_fail;
66 /* figure out the button */
67 if (!g_ascii_strcasecmp("Left", l)) *button = 1;
68 else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
69 else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
70 else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
71 else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
72 else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
74 g_warning("Invalid button '%s' in pointer binding.", l);
75 goto translation_fail;
85 gboolean translate_key(const gchar *str, guint *state, guint *keycode)
93 parsed = g_strsplit(str, "-", -1);
95 /* first, find the key (last token) */
97 for (i = 0; parsed[i] != NULL; ++i)
100 goto translation_fail;
102 /* figure out the mod mask */
104 for (i = 0; parsed[i] != l; ++i) {
105 guint m = translate_modifier(parsed[i]);
106 if (!m) goto translation_fail;
110 if (!g_ascii_strncasecmp("0x", l, 2)) {
113 /* take it directly */
114 *keycode = strtol(l, &end, 16);
115 if (*l == '\0' || *end != '\0') {
116 g_warning("Invalid key code '%s' in key binding.", l);
117 goto translation_fail;
120 /* figure out the keycode */
121 sym = XStringToKeysym(l);
122 if (sym == NoSymbol) {
123 g_warning("Invalid key name '%s' in key binding.", l);
124 goto translation_fail;
126 *keycode = XKeysymToKeycode(ob_display, sym);
129 g_warning("Key '%s' does not exist on the display.", l);
130 goto translation_fail;