]> icculus.org git repositories - dana/openbox.git/blob - obt/keyboard.c
make control keys work in menus/dialogs/etc with the new obt code, using XLookup...
[dana/openbox.git] / 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 struct _ObtIC
26 {
27     guint ref;
28     XIC xic;
29     Window client;
30     Window focus;
31 };
32
33 /* These masks are constants and the modifier keys are bound to them as
34    anyone sees fit:
35         ShiftMask (1<<0), LockMask (1<<1), ControlMask (1<<2), Mod1Mask (1<<3),
36         Mod2Mask (1<<4), Mod3Mask (1<<5), Mod4Mask (1<<6), Mod5Mask (1<<7)
37 */
38 #define NUM_MASKS 8
39 #define ALL_MASKS 0xff /* an or'ing of all 8 keyboard masks */
40
41 /* Get the bitflag for the n'th modifier mask */
42 #define nth_mask(n) (1 << n)
43
44 static void set_modkey_mask(guchar mask, KeySym sym);
45 static void xim_init(void);
46 void obt_keyboard_shutdown();
47 void obt_keyboard_context_renew(ObtIC *ic);
48
49 static XModifierKeymap *modmap;
50 static KeySym *keymap;
51 static gint min_keycode, max_keycode, keysyms_per_keycode;
52 /* This is a bitmask of the different masks for each modifier key */
53 static guchar modkeys_keys[OBT_KEYBOARD_NUM_MODKEYS];
54
55 static gboolean alt_l = FALSE;
56 static gboolean meta_l = FALSE;
57 static gboolean super_l = FALSE;
58 static gboolean hyper_l = FALSE;
59
60 static gboolean started = FALSE;
61
62 static XIM xim = NULL;
63 static XIMStyle xim_style = 0;
64 static GSList *xic_all = NULL;
65
66 void obt_keyboard_reload(void)
67 {
68     gint i, j, k;
69
70     if (started) obt_keyboard_shutdown(); /* free stuff */
71     started = TRUE;
72
73     xim_init();
74
75     /* reset the keys to not be bound to any masks */
76     for (i = 0; i < OBT_KEYBOARD_NUM_MODKEYS; ++i)
77         modkeys_keys[i] = 0;
78
79     modmap = XGetModifierMapping(obt_display);
80     /* note: modmap->max_keypermod can be 0 when there is no valid key layout
81        available */
82
83     XDisplayKeycodes(obt_display, &min_keycode, &max_keycode);
84     keymap = XGetKeyboardMapping(obt_display, min_keycode,
85                                  max_keycode - min_keycode + 1,
86                                  &keysyms_per_keycode);
87
88     alt_l = meta_l = super_l = hyper_l = FALSE;
89
90     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
91     for (i = 0; i < NUM_MASKS; ++i) {
92         /* go through each keycode that is bound to the mask */
93         for (j = 0; j < modmap->max_keypermod; ++j) {
94             KeySym sym;
95             /* get a keycode that is bound to the mask (i) */
96             KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
97             if (keycode) {
98                 /* go through each keysym bound to the given keycode */
99                 for (k = 0; k < keysyms_per_keycode; ++k) {
100                     sym = keymap[(keycode-min_keycode) * keysyms_per_keycode +
101                                  k];
102                     if (sym != NoSymbol) {
103                         /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
104                         set_modkey_mask(nth_mask(i), sym);
105                     }
106                 }
107             }
108         }
109     }
110
111     /* CapsLock, Shift, and Control are special and hard-coded */
112     modkeys_keys[OBT_KEYBOARD_MODKEY_CAPSLOCK] = LockMask;
113     modkeys_keys[OBT_KEYBOARD_MODKEY_SHIFT] = ShiftMask;
114     modkeys_keys[OBT_KEYBOARD_MODKEY_CONTROL] = ControlMask;
115 }
116
117 void obt_keyboard_shutdown(void)
118 {
119     XFreeModifiermap(modmap);
120     modmap = NULL;
121     XFree(keymap);
122     keymap = NULL;
123     if (xim) XCloseIM(xim);
124     xim = NULL;
125     xim_style = 0;
126     started = FALSE;
127 }
128
129 void xim_init(void)
130 {
131     GSList *it;
132     gchar *aname, *aclass;
133
134     aname = g_strdup(g_get_prgname());
135     if (!aname) aname = g_strdup("obt");
136
137     aclass = g_strdup(aname);
138     if (g_ascii_islower(aclass[0]))
139         aclass[0] = g_ascii_toupper(aclass[0]);
140
141     g_print("Opening Input Method for %s %s\n", aname, aclass);
142     xim = XOpenIM(obt_display, NULL, aname, aclass);
143
144     if (!xim)
145         g_message("Failed to open an Input Method");
146     else {
147         XIMStyles *xim_styles = NULL;
148         char *r;
149
150         /* get the input method styles */
151         r = XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL);
152         if (r || !xim_styles)
153             g_message("Input Method does not support any styles");
154         if (xim_styles) {
155             int i;
156
157             /* find a style that doesnt need preedit or status */
158             for (i = 0; i < xim_styles->count_styles; ++i) {
159                 if (xim_styles->supported_styles[i] == 
160                     (XIMPreeditNothing | XIMStatusNothing))
161                 {
162                     xim_style = xim_styles->supported_styles[i];
163                     break;
164                 }
165             }
166             XFree(xim_styles);
167         }
168
169         if (!xim_style) {
170             g_message("Input Method does not support a usable style");
171
172             XCloseIM(xim);
173             xim = NULL;
174         }
175     }
176
177     /* any existing contexts need to be recreated for the new input method */
178     for (it = xic_all; it; it = g_slist_next(it))
179         obt_keyboard_context_renew(it->data);
180
181     g_free(aclass);
182     g_free(aname);
183 }
184
185 guint obt_keyboard_keycode_to_modmask(guint keycode)
186 {
187     gint i, j;
188     guint mask = 0;
189
190     if (keycode == NoSymbol) return 0;
191
192     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
193     for (i = 0; i < NUM_MASKS; ++i) {
194         /* go through each keycode that is bound to the mask */
195         for (j = 0; j < modmap->max_keypermod; ++j) {
196             /* compare with a keycode that is bound to the mask (i) */
197             if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
198                 mask |= nth_mask(i);
199         }
200     }
201     return mask;
202 }
203
204 guint obt_keyboard_only_modmasks(guint mask)
205 {
206     mask &= ALL_MASKS;
207     /* strip off these lock keys. they shouldn't affect key bindings */
208     mask &= ~LockMask; /* use the LockMask, not what capslock is bound to,
209                           because you could bind it to something else and it
210                           should work as that modifier then. i think capslock
211                           is weird in xkb. */
212     mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_NUMLOCK);
213     mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SCROLLLOCK);
214     return mask;
215 }
216
217 guint obt_keyboard_modkey_to_modmask(ObtModkeysKey key)
218 {
219     return modkeys_keys[key];
220 }
221
222 static void set_modkey_mask(guchar mask, KeySym sym)
223 {
224     /* find what key this is, and bind it to the mask */
225
226     if (sym == XK_Num_Lock)
227         modkeys_keys[OBT_KEYBOARD_MODKEY_NUMLOCK] |= mask;
228     else if (sym == XK_Scroll_Lock)
229         modkeys_keys[OBT_KEYBOARD_MODKEY_SCROLLLOCK] |= mask;
230
231     else if (sym == XK_Super_L && super_l)
232         modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] |= mask;
233     else if (sym == XK_Super_L && !super_l)
234         /* left takes precident over right, so erase any masks the right
235            key may have set */
236         modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] = mask, super_l = TRUE;
237     else if (sym == XK_Super_R && !super_l)
238         modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] |= mask;
239
240     else if (sym == XK_Hyper_L && hyper_l)
241         modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] |= mask;
242     else if (sym == XK_Hyper_L && !hyper_l)
243         modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] = mask, hyper_l = TRUE;
244     else if (sym == XK_Hyper_R && !hyper_l)
245         modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] |= mask;
246
247     else if (sym == XK_Alt_L && alt_l)
248         modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] |= mask;
249     else if (sym == XK_Alt_L && !alt_l)
250         modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] = mask, alt_l = TRUE;
251     else if (sym == XK_Alt_R && !alt_l)
252         modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] |= mask;
253
254     else if (sym == XK_Meta_L && meta_l)
255         modkeys_keys[OBT_KEYBOARD_MODKEY_META] |= mask;
256     else if (sym == XK_Meta_L && !meta_l)
257         modkeys_keys[OBT_KEYBOARD_MODKEY_META] = mask, meta_l = TRUE;
258     else if (sym == XK_Meta_R && !meta_l)
259         modkeys_keys[OBT_KEYBOARD_MODKEY_META] |= mask;
260
261     /* CapsLock, Shift, and Control are special and hard-coded */
262 }
263
264 KeyCode* obt_keyboard_keysym_to_keycode(KeySym sym)
265 {
266     KeyCode *ret;
267     gint i, j, n;
268
269     ret = g_new(KeyCode, 1);
270     n = 0;
271     ret[n] = 0;
272
273     /* go through each keycode and look for the keysym */
274     for (i = min_keycode; i <= max_keycode; ++i)
275         for (j = 0; j < keysyms_per_keycode; ++j)
276             if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j]) {
277                 ret = g_renew(KeyCode, ret, ++n);
278                 ret[n-1] = i;
279                 ret[n] = 0;
280             }
281     return ret;
282 }
283
284 gunichar obt_keyboard_keypress_to_unichar(ObtIC *ic, XEvent *ev)
285 {
286     gunichar unikey = 0;
287     KeySym sym;
288     Status status;
289     gchar *buf, fixbuf[4]; /* 4 is enough for a utf8 char */
290     gint len, bufsz;
291     gboolean got_string = FALSE;
292
293     g_return_val_if_fail(ev->type == KeyPress, 0);
294
295     if (!ic)
296         g_warning("Using obt_keyboard_keypress_to_unichar() without an "
297                   "Input Context.  No i18n support!");
298
299     if (ic && ic->xic) {
300         buf = fixbuf;
301         bufsz = sizeof(fixbuf);
302
303 #ifdef X_HAVE_UTF8_STRING
304         len = Xutf8LookupString(ic->xic, &ev->xkey, buf, bufsz, &sym, &status);
305 #else
306         len = XmbLookupString(ic->xic, &ev->xkey, buf, bufsz, &sym, &status);
307 #endif
308
309         if (status == XBufferOverflow) {
310             buf = g_new(char, len);
311             bufsz = len;
312
313 #ifdef X_HAVE_UTF8_STRING
314             len = Xutf8LookupString(ic->xic, &ev->xkey, buf, bufsz, &sym,
315                                     &status);
316 #else
317             len = XmbLookupString(ic->xic, &ev->xkey, buf, bufsz, &sym,
318                                   &status);
319 #endif
320         }
321
322         if ((status == XLookupChars || status == XLookupBoth)) {
323             if ((guchar)buf[0] >= 32) { /* not an ascii control character */
324 #ifndef X_HAVE_UTF8_STRING
325                 /* convert to utf8 */
326                 gchar *buf2 = buf;
327                 buf = g_locale_to_utf8(buf2, r, NULL, NULL, NULL);
328                 g_free(buf2);
329 #endif
330
331                 got_string = TRUE;
332             }
333         }
334         else
335             g_message("Bad keycode lookup. Keysym 0x%x Status: %s\n",
336                       (guint) sym,
337                       (status == XBufferOverflow ? "BufferOverflow" :
338                        status == XLookupNone ? "XLookupNone" :
339                        status == XLookupKeySym ? "XLookupKeySym" :
340                        "Unknown status"));
341     }
342     else {
343         buf = fixbuf;
344         bufsz = sizeof(fixbuf);
345         len = XLookupString(&ev->xkey, buf, bufsz, &sym, NULL);
346         if ((guchar)buf[0] >= 32) /* not an ascii control character */
347             got_string = TRUE;
348     }
349
350     if (got_string) {
351         gunichar u = g_utf8_get_char_validated(buf, len);
352         if (u && u != (gunichar)-1 && u != (gunichar)-2)
353             unikey = u;
354     }
355
356     if (buf != fixbuf) g_free(buf);
357
358     return unikey;
359 }
360
361 KeySym obt_keyboard_keypress_to_keysym(XEvent *ev)
362 {
363     KeySym sym;
364     gint r;
365
366     g_return_val_if_fail(ev->type == KeyPress, None);
367
368     sym = None;
369     r = XLookupString(&ev->xkey, NULL, 0, &sym, NULL);
370     return sym;
371 }
372
373 void obt_keyboard_context_renew(ObtIC *ic)
374 {
375     if (ic->xic) {
376         XDestroyIC(ic->xic);
377         ic->xic = NULL;
378     }
379
380     if (xim) {
381         ic->xic = XCreateIC(xim,
382                             XNInputStyle, xim_style,
383                             XNClientWindow, ic->client,
384                             XNFocusWindow, ic->focus,
385                             NULL);
386         if (!ic->xic)
387             g_message("Error creating Input Context for window 0x%x 0x%x\n",
388                       (guint)ic->client, (guint)ic->focus);
389     }
390 }
391
392 ObtIC* obt_keyboard_context_new(Window client, Window focus)
393 {
394     ObtIC *ic;
395
396     g_return_val_if_fail(client != None && focus != None, NULL);
397
398     ic = g_new(ObtIC, 1);
399     ic->ref = 1;
400     ic->client = client;
401     ic->focus = focus;
402     ic->xic = NULL;
403
404     obt_keyboard_context_renew(ic);
405     xic_all = g_slist_prepend(xic_all, ic);
406
407     return ic;
408 }
409
410 void obt_keyboard_context_ref(ObtIC *ic)
411 {
412     ++ic->ref;
413 }
414
415 void obt_keyboard_context_unref(ObtIC *ic)
416 {
417     if (--ic->ref < 1) {
418         xic_all = g_slist_remove(xic_all, ic);
419         XDestroyIC(ic->xic);
420         g_free(ic);
421     }
422 }