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