]> Dogcows Code - chaz/openbox/blob - openbox/translate.c
add support for _NET_RESTACK_WINDOW
[chaz/openbox] / 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) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
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.
11
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.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox.h"
21 #include "mouse.h"
22 #include "modkeys.h"
23 #include "gettext.h"
24 #include <glib.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 static guint translate_modifier(gchar *str)
29 {
30 guint mask = 0;
31
32 if (!g_ascii_strcasecmp("Mod1", str)) mask = Mod1Mask;
33 else if (!g_ascii_strcasecmp("Mod2", str)) mask = Mod2Mask;
34 else if (!g_ascii_strcasecmp("Mod3", str)) mask = Mod3Mask;
35 else if (!g_ascii_strcasecmp("Mod4", str)) mask = Mod4Mask;
36 else if (!g_ascii_strcasecmp("Mod5", str)) mask = Mod5Mask;
37
38 else if (!g_ascii_strcasecmp("Control", str) ||
39 !g_ascii_strcasecmp("C", str))
40 mask = modkeys_key_to_mask(OB_MODKEY_KEY_CONTROL);
41 else if (!g_ascii_strcasecmp("Alt", str) ||
42 !g_ascii_strcasecmp("A", str))
43 mask = modkeys_key_to_mask(OB_MODKEY_KEY_ALT);
44 else if (!g_ascii_strcasecmp("Meta", str) ||
45 !g_ascii_strcasecmp("M", str))
46 mask = modkeys_key_to_mask(OB_MODKEY_KEY_META);
47 /* W = windows key, is linked to the Super_L/R buttons */
48 else if (!g_ascii_strcasecmp("Super", str) ||
49 !g_ascii_strcasecmp("W", str))
50 mask = modkeys_key_to_mask(OB_MODKEY_KEY_SUPER);
51 else if (!g_ascii_strcasecmp("Shift", str) ||
52 !g_ascii_strcasecmp("S", str))
53 mask = modkeys_key_to_mask(OB_MODKEY_KEY_SHIFT);
54 else if (!g_ascii_strcasecmp("Hyper", str) ||
55 !g_ascii_strcasecmp("H", str))
56 mask = modkeys_key_to_mask(OB_MODKEY_KEY_HYPER);
57 else
58 g_message(_("Invalid modifier key '%s' in key/mouse binding"), str);
59
60 return mask;
61 }
62
63 gboolean translate_button(const gchar *str, guint *state, guint *button)
64 {
65 gchar **parsed;
66 gchar *l;
67 gint i;
68 gboolean ret = FALSE;
69
70 parsed = g_strsplit(str, "-", -1);
71
72 /* first, find the button (last token) */
73 l = NULL;
74 for (i = 0; parsed[i] != NULL; ++i)
75 l = parsed[i];
76 if (l == NULL)
77 goto translation_fail;
78
79 /* figure out the mod mask */
80 *state = 0;
81 for (i = 0; parsed[i] != l; ++i) {
82 guint m = translate_modifier(parsed[i]);
83 if (!m) goto translation_fail;
84 *state |= m;
85 }
86
87 /* figure out the button */
88 if (!g_ascii_strcasecmp("Left", l)) *button = 1;
89 else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
90 else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
91 else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
92 else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
93 else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
94 if (!*button)
95 goto translation_fail;
96
97 ret = TRUE;
98
99 translation_fail:
100 g_strfreev(parsed);
101 return ret;
102 }
103
104 gboolean translate_key(const gchar *str, guint *state, guint *keycode)
105 {
106 gchar **parsed;
107 gchar *l;
108 gint i;
109 gboolean ret = FALSE;
110 KeySym sym;
111
112 parsed = g_strsplit(str, "-", -1);
113
114 /* first, find the key (last token) */
115 l = NULL;
116 for (i = 0; parsed[i] != NULL; ++i)
117 l = parsed[i];
118 if (l == NULL)
119 goto translation_fail;
120
121 /* figure out the mod mask */
122 *state = 0;
123 for (i = 0; parsed[i] != l; ++i) {
124 guint m = translate_modifier(parsed[i]);
125 if (!m) goto translation_fail;
126 *state |= m;
127 }
128
129 if (!g_ascii_strncasecmp("0x", l, 2)) {
130 gchar *end;
131
132 /* take it directly */
133 *keycode = strtol(l, &end, 16);
134 if (*l == '\0' || *end != '\0') {
135 g_message(_("Invalid key code '%s' in key binding"), l);
136 goto translation_fail;
137 }
138 } else {
139 /* figure out the keycode */
140 sym = XStringToKeysym(l);
141 if (sym == NoSymbol) {
142 g_message(_("Invalid key name '%s' in key binding"), l);
143 goto translation_fail;
144 }
145 *keycode = XKeysymToKeycode(ob_display, sym);
146 }
147 if (!*keycode) {
148 g_message(_("Requested key '%s' does not exist on the display"), l);
149 goto translation_fail;
150 }
151
152 ret = TRUE;
153
154 translation_fail:
155 g_strfreev(parsed);
156 return ret;
157 }
158
159 const gchar *translate_keycode(guint keycode)
160 {
161 KeySym sym;
162 const gchar *ret = NULL;
163
164 if ((sym = XKeycodeToKeysym(ob_display, keycode, 0)) != NoSymbol)
165 ret = XKeysymToString(sym);
166 return g_locale_to_utf8(ret, -1, NULL, NULL, NULL);
167 }
168
169 gunichar translate_unichar(guint keycode)
170 {
171 gunichar unikey = 0;
172
173 const char *key;
174 if ((key = translate_keycode(keycode)) != NULL &&
175 /* don't accept keys that aren't a single letter, like "space" */
176 key[1] == '\0')
177 {
178 unikey = g_utf8_get_char_validated(key, -1);
179 if (unikey == (gunichar)-1 || unikey == (gunichar)-2 || unikey == 0)
180 unikey = 0;
181 }
182 return unikey;
183 }
This page took 0.043448 seconds and 4 git commands to generate.