1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/keyboard.c for the Openbox window manager
4 Copyright (c) 2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
19 #include "obt/display.h"
20 #include "obt/keyboard.h"
23 #include <X11/keysym.h>
31 /* These masks are constants and the modifier keys are bound to them as
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)
37 #define ALL_MASKS 0xff /* an or'ing of all 8 keyboard masks */
39 /* Get the bitflag for the n'th modifier mask */
40 #define nth_mask(n) (1 << n)
42 static void set_modkey_mask(guchar mask, KeySym sym);
43 static void xim_init(void);
44 void obt_keyboard_shutdown();
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];
52 static gboolean alt_l = FALSE;
53 static gboolean meta_l = FALSE;
54 static gboolean super_l = FALSE;
55 static gboolean hyper_l = FALSE;
57 static gboolean started = FALSE;
59 static XIM xim = NULL;
60 static XIMStyle xim_style = 0;
62 void obt_keyboard_reload(void)
66 if (started) obt_keyboard_shutdown(); /* free stuff */
71 /* reset the keys to not be bound to any masks */
72 for (i = 0; i < OBT_KEYBOARD_NUM_MODKEYS; ++i)
75 modmap = XGetModifierMapping(obt_display);
76 /* note: modmap->max_keypermod can be 0 when there is no valid key layout
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);
84 alt_l = meta_l = super_l = hyper_l = FALSE;
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) {
91 /* get a keycode that is bound to the mask (i) */
92 KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
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 +
98 if (sym != NoSymbol) {
99 /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
100 set_modkey_mask(nth_mask(i), sym);
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;
113 void obt_keyboard_shutdown(void)
115 XFreeModifiermap(modmap);
119 if (xim) XCloseIM(xim);
127 gchar *aname, *aclass;
129 aname = g_strdup(g_get_prgname());
130 if (!aname) aname = g_strdup("obt");
132 aclass = g_strdup(aname);
133 if (g_ascii_islower(aclass[0]))
134 aclass[0] = g_ascii_toupper(aclass[0]);
136 g_print("Opening Input Method for %s %s\n", aname, aclass);
137 xim = XOpenIM(obt_display, NULL, aname, aclass);
140 g_message("Failed to open an Input Method");
142 XIMStyles *xim_styles = NULL;
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");
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))
157 xim_style = xim_styles->supported_styles[i];
165 g_message("Input Method does not support a usable style");
176 guint obt_keyboard_keycode_to_modmask(guint keycode)
181 if (keycode == NoSymbol) return 0;
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)
195 guint obt_keyboard_only_modmasks(guint mask)
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
203 mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_NUMLOCK);
204 mask &= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SCROLLLOCK);
208 guint obt_keyboard_modkey_to_modmask(ObtModkeysKey key)
210 return modkeys_keys[key];
213 static void set_modkey_mask(guchar mask, KeySym sym)
215 /* find what key this is, and bind it to the mask */
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;
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
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;
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;
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;
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;
252 /* CapsLock, Shift, and Control are special and hard-coded */
255 KeyCode* obt_keyboard_keysym_to_keycode(KeySym sym)
260 ret = g_new(KeyCode, 1);
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);
275 gchar *obt_keyboard_keycode_to_string(guint keycode)
279 if ((sym = XKeycodeToKeysym(obt_display, keycode, 0)) != NoSymbol)
280 return g_locale_to_utf8(XKeysymToString(sym), -1, NULL, NULL, NULL);
284 gunichar obt_keyboard_keycode_to_unichar(guint keycode)
289 if ((key = obt_keyboard_keycode_to_string(keycode)) != NULL &&
290 /* don't accept keys that aren't a single letter, like "space" */
293 unikey = g_utf8_get_char_validated(key, -1);
294 if (unikey == (gunichar)-1 || unikey == (gunichar)-2 || unikey == 0)
301 ObtIC* obt_keyboard_context_new(Window w)
306 ic = g_new(ObtIC, 1);
311 ic->xic = XCreateIC(xim,
312 XNInputStyle, xim_style,
320 void obt_keyboard_context_ref(ObtIC *ic)
325 void obt_keyboard_context_unref(ObtIC *ic)