]> icculus.org git repositories - dana/openbox.git/blob - obt/keyboard.c
check the xkb version before using it
[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 /* 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 void obt_keyboard_shutdown();
38
39 static XModifierKeymap *modmap;
40 static KeySym *keymap;
41 static gint min_keycode, max_keycode, keysyms_per_keycode;
42 /* This is a bitmask of the different masks for each modifier key */
43 static guchar modkeys_keys[OBT_KEYBOARD_NUM_MODKEYS];
44
45 static gboolean alt_l = FALSE;
46 static gboolean meta_l = FALSE;
47 static gboolean super_l = FALSE;
48 static gboolean hyper_l = FALSE;
49
50 static gboolean started = FALSE;
51
52 void obt_keyboard_reload(void)
53 {
54     gint i, j, k;
55
56     if (started) obt_keyboard_shutdown(); /* free stuff */
57     started = TRUE;
58
59     /* reset the keys to not be bound to any masks */
60     for (i = 0; i < OBT_KEYBOARD_NUM_MODKEYS; ++i)
61         modkeys_keys[i] = 0;
62
63     modmap = XGetModifierMapping(obt_display);
64     /* note: modmap->max_keypermod can be 0 when there is no valid key layout
65        available */
66
67     XDisplayKeycodes(obt_display, &min_keycode, &max_keycode);
68     keymap = XGetKeyboardMapping(obt_display, min_keycode,
69                                  max_keycode - min_keycode + 1,
70                                  &keysyms_per_keycode);
71
72     alt_l = meta_l = super_l = hyper_l = FALSE;
73
74     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
75     for (i = 0; i < NUM_MASKS; ++i) {
76         /* go through each keycode that is bound to the mask */
77         for (j = 0; j < modmap->max_keypermod; ++j) {
78             KeySym sym;
79             /* get a keycode that is bound to the mask (i) */
80             KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
81             if (keycode) {
82                 /* go through each keysym bound to the given keycode */
83                 for (k = 0; k < keysyms_per_keycode; ++k) {
84                     sym = keymap[(keycode-min_keycode) * keysyms_per_keycode +
85                                  k];
86                     if (sym != NoSymbol) {
87                         /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
88                         set_modkey_mask(nth_mask(i), sym);
89                     }
90                 }
91             }
92         }
93     }
94
95     /* CapsLock, Shift, and Control are special and hard-coded */
96     modkeys_keys[OBT_KEYBOARD_MODKEY_CAPSLOCK] = LockMask;
97     modkeys_keys[OBT_KEYBOARD_MODKEY_SHIFT] = ShiftMask;
98     modkeys_keys[OBT_KEYBOARD_MODKEY_CONTROL] = ControlMask;
99 }
100
101 void obt_keyboard_shutdown(void)
102 {
103     XFreeModifiermap(modmap);
104     modmap = NULL;
105     XFree(keymap);
106     keymap = NULL;
107     started = FALSE;
108 }
109
110 guint obt_keyboard_keycode_to_modmask(guint keycode)
111 {
112     gint i, j;
113     guint mask = 0;
114
115     if (keycode == NoSymbol) return 0;
116
117     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
118     for (i = 0; i < NUM_MASKS; ++i) {
119         /* go through each keycode that is bound to the mask */
120         for (j = 0; j < modmap->max_keypermod; ++j) {
121             /* compare with a keycode that is bound to the mask (i) */
122             if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
123                 mask |= nth_mask(i);
124         }
125     }
126     return mask;
127 }
128
129 guint obt_keyboard_only_modmasks(guint mask)
130 {
131     mask &= ALL_MASKS;
132     /* strip off these lock keys. they shouldn't affect key bindings */
133     mask &= ~LockMask; /* use the LockMask, not what capslock is bound to,
134                           because you could bind it to something else and it
135                           should work as that modifier then. i think capslock
136                           is weird in xkb. */
137     mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_NUMLOCK);
138     mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SCROLLLOCK);
139     return mask;
140 }
141
142 guint obt_keyboard_modkey_to_modmask(ObtModkeysKey key)
143 {
144     return modkeys_keys[key];
145 }
146
147 static void set_modkey_mask(guchar mask, KeySym sym)
148 {
149     /* find what key this is, and bind it to the mask */
150
151     if (sym == XK_Num_Lock)
152         modkeys_keys[OBT_KEYBOARD_MODKEY_NUMLOCK] |= mask;
153     else if (sym == XK_Scroll_Lock)
154         modkeys_keys[OBT_KEYBOARD_MODKEY_SCROLLLOCK] |= mask;
155
156     else if (sym == XK_Super_L && super_l)
157         modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] |= mask;
158     else if (sym == XK_Super_L && !super_l)
159         /* left takes precident over right, so erase any masks the right
160            key may have set */
161         modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] = mask, super_l = TRUE;
162     else if (sym == XK_Super_R && !super_l)
163         modkeys_keys[OBT_KEYBOARD_MODKEY_SUPER] |= mask;
164
165     else if (sym == XK_Hyper_L && hyper_l)
166         modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] |= mask;
167     else if (sym == XK_Hyper_L && !hyper_l)
168         modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] = mask, hyper_l = TRUE;
169     else if (sym == XK_Hyper_R && !hyper_l)
170         modkeys_keys[OBT_KEYBOARD_MODKEY_HYPER] |= mask;
171
172     else if (sym == XK_Alt_L && alt_l)
173         modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] |= mask;
174     else if (sym == XK_Alt_L && !alt_l)
175         modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] = mask, alt_l = TRUE;
176     else if (sym == XK_Alt_R && !alt_l)
177         modkeys_keys[OBT_KEYBOARD_MODKEY_ALT] |= mask;
178
179     else if (sym == XK_Meta_L && meta_l)
180         modkeys_keys[OBT_KEYBOARD_MODKEY_META] |= mask;
181     else if (sym == XK_Meta_L && !meta_l)
182         modkeys_keys[OBT_KEYBOARD_MODKEY_META] = mask, meta_l = TRUE;
183     else if (sym == XK_Meta_R && !meta_l)
184         modkeys_keys[OBT_KEYBOARD_MODKEY_META] |= mask;
185
186     /* CapsLock, Shift, and Control are special and hard-coded */
187 }
188
189 KeyCode* obt_keyboard_keysym_to_keycode(KeySym sym)
190 {
191     KeyCode *ret;
192     gint i, j, n;
193
194     ret = g_new(KeyCode, 1);
195     n = 0;
196     ret[n] = 0;
197
198     /* go through each keycode and look for the keysym */
199     for (i = min_keycode; i <= max_keycode; ++i)
200         for (j = 0; j < keysyms_per_keycode; ++j)
201             if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j]) {
202                 ret = g_renew(KeyCode, ret, ++n);
203                 ret[n-1] = i;
204                 ret[n] = 0;
205             }
206     return ret;
207 }
208
209 gchar *obt_keyboard_keycode_to_string(guint keycode)
210 {
211     KeySym sym;
212
213     if ((sym = XKeycodeToKeysym(obt_display, keycode, 0)) != NoSymbol)
214         return g_locale_to_utf8(XKeysymToString(sym), -1, NULL, NULL, NULL);
215     return NULL;
216 }
217
218 gunichar obt_keyboard_keycode_to_unichar(guint keycode)
219 {
220     gunichar unikey = 0;
221     char *key;
222
223     if ((key = obt_keyboard_keycode_to_string(keycode)) != NULL &&
224         /* don't accept keys that aren't a single letter, like "space" */
225         key[1] == '\0')
226     {
227         unikey = g_utf8_get_char_validated(key, -1);
228         if (unikey == (gunichar)-1 || unikey == (gunichar)-2 || unikey == 0)
229             unikey = 0;
230     }
231     g_free(key);
232     return unikey;
233 }