]> Dogcows Code - chaz/openbox/blob - obt/keyboard.c
bd7ec7b601de64924718c1e87c4d60193fdb0daf
[chaz/openbox] / obt / keyboard.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/keyboard.c for the Openbox window manager
4 Copyright (c) 2007 Dana 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 "obt/display.h"
20 #include "obt/keyboard.h"
21
22 #include <X11/Xlib.h>
23 #include <X11/keysym.h>
24
25 /* These masks are constants and the modifier keys are bound to them as
26 anyone sees fit:
27 ShiftMask (1<<0), LockMask (1<<1), ControlMask (1<<2), Mod1Mask (1<<3),
28 Mod2Mask (1<<4), Mod3Mask (1<<5), Mod4Mask (1<<6), Mod5Mask (1<<7)
29 */
30 #define NUM_MASKS 8
31 #define ALL_MASKS 0xff /* an or'ing of all 8 keyboard masks */
32
33 /* Get the bitflag for the n'th modifier mask */
34 #define nth_mask(n) (1 << n)
35
36 static void set_modkey_mask(guchar mask, KeySym sym);
37 static void xim_init(void);
38 void obt_keyboard_shutdown();
39
40 static XModifierKeymap *modmap;
41 static KeySym *keymap;
42 static gint min_keycode, max_keycode, keysyms_per_keycode;
43 /* This is a bitmask of the different masks for each modifier key */
44 static guchar modkeys_keys[OBT_KEYBOARD_NUM_MODKEYS];
45
46 static gboolean alt_l = FALSE;
47 static gboolean meta_l = FALSE;
48 static gboolean super_l = FALSE;
49 static gboolean hyper_l = FALSE;
50
51 static gboolean started = FALSE;
52
53 static XIM xim = NULL;
54 static XIMStyle xim_style = 0;
55
56 void obt_keyboard_reload(void)
57 {
58 gint i, j, k;
59
60 if (started) obt_keyboard_shutdown(); /* free stuff */
61 started = TRUE;
62
63 xim_init();
64
65 /* reset the keys to not be bound to any masks */
66 for (i = 0; i < OBT_KEYBOARD_NUM_MODKEYS; ++i)
67 modkeys_keys[i] = 0;
68
69 modmap = XGetModifierMapping(obt_display);
70 /* note: modmap->max_keypermod can be 0 when there is no valid key layout
71 available */
72
73 XDisplayKeycodes(obt_display, &min_keycode, &max_keycode);
74 keymap = XGetKeyboardMapping(obt_display, min_keycode,
75 max_keycode - min_keycode + 1,
76 &keysyms_per_keycode);
77
78 alt_l = meta_l = super_l = hyper_l = FALSE;
79
80 /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
81 for (i = 0; i < NUM_MASKS; ++i) {
82 /* go through each keycode that is bound to the mask */
83 for (j = 0; j < modmap->max_keypermod; ++j) {
84 KeySym sym;
85 /* get a keycode that is bound to the mask (i) */
86 KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
87 if (keycode) {
88 /* go through each keysym bound to the given keycode */
89 for (k = 0; k < keysyms_per_keycode; ++k) {
90 sym = keymap[(keycode-min_keycode) * keysyms_per_keycode +
91 k];
92 if (sym != NoSymbol) {
93 /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
94 set_modkey_mask(nth_mask(i), sym);
95 }
96 }
97 }
98 }
99 }
100
101 /* CapsLock, Shift, and Control are special and hard-coded */
102 modkeys_keys[OBT_KEYBOARD_MODKEY_CAPSLOCK] = LockMask;
103 modkeys_keys[OBT_KEYBOARD_MODKEY_SHIFT] = ShiftMask;
104 modkeys_keys[OBT_KEYBOARD_MODKEY_CONTROL] = ControlMask;
105 }
106
107 void obt_keyboard_shutdown(void)
108 {
109 XFreeModifiermap(modmap);
110 modmap = NULL;
111 XFree(keymap);
112 keymap = NULL;
113 if (xim) XCloseIM(xim);
114 xim = NULL;
115 xim_style = 0;
116 started = FALSE;
117 }
118
119 void xim_init(void)
120 {
121 gchar *aname, *aclass;
122
123 aname = g_strdup(g_get_prgname());
124 if (!aname) aname = g_strdup("obt");
125
126 aclass = g_strdup(aname);
127 if (g_ascii_islower(aclass[0]))
128 aclass[0] = g_ascii_toupper(aclass[0]);
129
130 g_print("Opening Input Method for %s %s\n", aname, aclass);
131 xim = XOpenIM(obt_display, NULL, aname, aclass);
132
133 if (!xim)
134 g_message("Failed to open an Input Method");
135 else {
136 XIMStyles *xim_styles = NULL;
137 char *r;
138
139 /* get the input method styles */
140 r = XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
141 if (r || !xim_styles)
142 g_message("Input Method does not support any styles");
143 if (xim_styles) {
144 int i;
145
146 /* find a style that doesnt need preedit or status */
147 for (i = 0; i < xim_styles->count_styles; ++i) {
148 if (xim_styles->supported_styles[i] ==
149 (XIMPreeditNothing | XIMStatusNothing))
150 {
151 xim_style = xim_styles->supported_styles[i];
152 break;
153 }
154 }
155 XFree(xim_styles);
156 }
157
158 if (!xim_style) {
159 g_message("Input Method does not support a usable style");
160
161 XCloseIM(xim);
162 xim = NULL;
163 }
164 }
165
166 g_free(aclass);
167 g_free(aname);
168 }
169
170 guint obt_keyboard_keycode_to_modmask(guint keycode)
171 {
172 gint i, j;
173 guint mask = 0;
174
175 if (keycode == NoSymbol) return 0;
176
177 /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
178 for (i = 0; i < NUM_MASKS; ++i) {
179 /* go through each keycode that is bound to the mask */
180 for (j = 0; j < modmap->max_keypermod; ++j) {
181 /* compare with a keycode that is bound to the mask (i) */
182 if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
183 mask |= nth_mask(i);
184 }
185 }
186 return mask;
187 }
188
189 guint obt_keyboard_only_modmasks(guint mask)
190 {
191 mask &= ALL_MASKS;
192 /* strip off these lock keys. they shouldn't affect key bindings */
193 mask &= ~LockMask; /* use the LockMask, not what capslock is bound to,
194 because you could bind it to something else and it
195 should work as that modifier then. i think capslock
196 is weird in xkb. */
197 mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_NUMLOCK);
198 mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SCROLLLOCK);
199 return mask;
200 }
201
202 guint obt_keyboard_modkey_to_modmask(ObtModkeysKey key)
203 {
204 return modkeys_keys[key];
205 }
206
207 static void set_modkey_mask(guchar mask, KeySym sym)
208 {
209 /* find what key this is, and bind it to the mask */
210
211 if (sym == XK_Num_Lock)
212 modkeys_keys[OBT_KEYBOARD_MODKEY_NUMLOCK] |= mask;
213 else if (sym == XK_Scroll_Lock)
214 modkeys_keys[OBT_KEYBOARD_MODKEY_SCROLLLOCK] |= mask;
215
216 else if (sym == XK_Super_L && super_l)
217 modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] |= mask;
218 else if (sym == XK_Super_L && !super_l)
219 /* left takes precident over right, so erase any masks the right
220 key may have set */
221 modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] = mask, super_l = TRUE;
222 else if (sym == XK_Super_R && !super_l)
223 modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] |= mask;
224
225 else if (sym == XK_Hyper_L && hyper_l)
226 modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] |= mask;
227 else if (sym == XK_Hyper_L && !hyper_l)
228 modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] = mask, hyper_l = TRUE;
229 else if (sym == XK_Hyper_R && !hyper_l)
230 modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] |= mask;
231
232 else if (sym == XK_Alt_L && alt_l)
233 modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] |= mask;
234 else if (sym == XK_Alt_L && !alt_l)
235 modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] = mask, alt_l = TRUE;
236 else if (sym == XK_Alt_R && !alt_l)
237 modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] |= mask;
238
239 else if (sym == XK_Meta_L && meta_l)
240 modkeys_keys[OBT_KEYBOARD_MODKEY_META] |= mask;
241 else if (sym == XK_Meta_L && !meta_l)
242 modkeys_keys[OBT_KEYBOARD_MODKEY_META] = mask, meta_l = TRUE;
243 else if (sym == XK_Meta_R && !meta_l)
244 modkeys_keys[OBT_KEYBOARD_MODKEY_META] |= mask;
245
246 /* CapsLock, Shift, and Control are special and hard-coded */
247 }
248
249 KeyCode* obt_keyboard_keysym_to_keycode(KeySym sym)
250 {
251 KeyCode *ret;
252 gint i, j, n;
253
254 ret = g_new(KeyCode, 1);
255 n = 0;
256 ret[n] = 0;
257
258 /* go through each keycode and look for the keysym */
259 for (i = min_keycode; i <= max_keycode; ++i)
260 for (j = 0; j < keysyms_per_keycode; ++j)
261 if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j]) {
262 ret = g_renew(KeyCode, ret, ++n);
263 ret[n-1] = i;
264 ret[n] = 0;
265 }
266 return ret;
267 }
268
269 gchar *obt_keyboard_keycode_to_string(guint keycode)
270 {
271 KeySym sym;
272
273 if ((sym = XKeycodeToKeysym(obt_display, keycode, 0)) != NoSymbol)
274 return g_locale_to_utf8(XKeysymToString(sym), -1, NULL, NULL, NULL);
275 return NULL;
276 }
277
278 gunichar obt_keyboard_keycode_to_unichar(guint keycode)
279 {
280 gunichar unikey = 0;
281 char *key;
282
283 if ((key = obt_keyboard_keycode_to_string(keycode)) != NULL &&
284 /* don't accept keys that aren't a single letter, like "space" */
285 key[1] == '\0')
286 {
287 unikey = g_utf8_get_char_validated(key, -1);
288 if (unikey == (gunichar)-1 || unikey == (gunichar)-2 || unikey == 0)
289 unikey = 0;
290 }
291 g_free(key);
292 return unikey;
293 }
This page took 0.047944 seconds and 3 git commands to generate.