1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 translate.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
27 static guint translate_modifier(gchar *str)
29 if (!g_ascii_strcasecmp("Mod1", str) ||
30 !g_ascii_strcasecmp("A", str)) return Mod1Mask;
31 else if (!g_ascii_strcasecmp("Mod2", str)) return Mod2Mask;
32 else if (!g_ascii_strcasecmp("Mod3", str) ||
33 !g_ascii_strcasecmp("M", str)) return Mod3Mask;
34 else if (!g_ascii_strcasecmp("Mod4", str) ||
35 !g_ascii_strcasecmp("W", str)) return Mod4Mask;
36 else if (!g_ascii_strcasecmp("Mod5", str)) return Mod5Mask;
37 else if (!g_ascii_strcasecmp("Control", str) ||
38 !g_ascii_strcasecmp("C", str)) return ControlMask;
39 else if (!g_ascii_strcasecmp("Shift", str) ||
40 !g_ascii_strcasecmp("S", str)) return ShiftMask;
41 g_message(_("Invalid modifier key '%s' in key/pointer binding"), str);
45 gboolean translate_button(const gchar *str, guint *state, guint *button)
52 parsed = g_strsplit(str, "-", -1);
54 /* first, find the button (last token) */
56 for (i = 0; parsed[i] != NULL; ++i)
59 goto translation_fail;
61 /* figure out the mod mask */
63 for (i = 0; parsed[i] != l; ++i) {
64 guint m = translate_modifier(parsed[i]);
65 if (!m) goto translation_fail;
69 /* figure out the button */
70 if (!g_ascii_strcasecmp("Left", l)) *button = 1;
71 else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
72 else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
73 else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
74 else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
75 else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
77 g_message(_("Invalid button '%s' in pointer binding"), l);
78 goto translation_fail;
88 gboolean translate_key(const gchar *str, guint *state, guint *keycode)
96 parsed = g_strsplit(str, "-", -1);
98 /* first, find the key (last token) */
100 for (i = 0; parsed[i] != NULL; ++i)
103 goto translation_fail;
105 /* figure out the mod mask */
107 for (i = 0; parsed[i] != l; ++i) {
108 guint m = translate_modifier(parsed[i]);
109 if (!m) goto translation_fail;
113 if (!g_ascii_strncasecmp("0x", l, 2)) {
116 /* take it directly */
117 *keycode = strtol(l, &end, 16);
118 if (*l == '\0' || *end != '\0') {
119 g_message(_("Invalid key code '%s' in key binding"), l);
120 goto translation_fail;
123 /* figure out the keycode */
124 sym = XStringToKeysym(l);
125 if (sym == NoSymbol) {
126 g_message(_("Invalid key name '%s' in key binding"), l);
127 goto translation_fail;
129 *keycode = XKeysymToKeycode(ob_display, sym);
132 g_message(_("Requested key '%s' does not exist on the display"), l);
133 goto translation_fail;
143 const gchar *translate_keycode(guint keycode)
146 const gchar *ret = NULL;
148 if ((sym = XKeycodeToKeysym(ob_display, keycode, 0)) != NoSymbol)
149 ret = XKeysymToString(sym);
150 return g_locale_to_utf8(ret, -1, NULL, NULL, NULL);
153 gunichar translate_unichar(guint keycode)
158 if ((key = translate_keycode(keycode)) != NULL &&
159 /* don't accept keys that aren't a single letter, like "space" */
162 unikey = g_utf8_get_char_validated(key, -1);
163 if (unikey == (gunichar)-1 || unikey == (gunichar)-2 || unikey == 0)