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>
33 /* These masks are constants and the modifier keys are bound to them as
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)
39 #define ALL_MASKS 0xff /* an or'ing of all 8 keyboard masks */
41 /* Get the bitflag for the n'th modifier mask */
42 #define nth_mask(n) (1 << n)
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
);
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
];
55 static gboolean alt_l
= FALSE
;
56 static gboolean meta_l
= FALSE
;
57 static gboolean super_l
= FALSE
;
58 static gboolean hyper_l
= FALSE
;
60 static gboolean started
= FALSE
;
62 static XIM xim
= NULL
;
63 static XIMStyle xim_style
= 0;
64 static GSList
*xic_all
= NULL
;
66 void obt_keyboard_reload(void)
70 if (started
) obt_keyboard_shutdown(); /* free stuff */
75 /* reset the keys to not be bound to any masks */
76 for (i
= 0; i
< OBT_KEYBOARD_NUM_MODKEYS
; ++i
)
79 modmap
= XGetModifierMapping(obt_display
);
80 /* note: modmap->max_keypermod can be 0 when there is no valid key layout
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
);
88 alt_l
= meta_l
= super_l
= hyper_l
= FALSE
;
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
) {
95 /* get a keycode that is bound to the mask (i) */
96 KeyCode keycode
= modmap
->modifiermap
[i
*modmap
->max_keypermod
+ j
];
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
+
102 if (sym
!= NoSymbol
) {
103 /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
104 set_modkey_mask(nth_mask(i
), sym
);
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
;
117 void obt_keyboard_shutdown(void)
121 XFreeModifiermap(modmap
);
125 for (it
= xic_all
; it
; it
= g_slist_next(it
)) {
126 ObtIC
* ic
= it
->data
;
132 if (xim
) XCloseIM(xim
);
141 gchar
*aname
, *aclass
;
143 aname
= g_strdup(g_get_prgname());
144 if (!aname
) aname
= g_strdup("obt");
146 aclass
= g_strdup(aname
);
147 if (g_ascii_islower(aclass
[0]))
148 aclass
[0] = g_ascii_toupper(aclass
[0]);
150 xim
= XOpenIM(obt_display
, NULL
, aname
, aclass
);
153 g_message("Failed to open an Input Method");
155 XIMStyles
*xim_styles
= NULL
;
158 /* get the input method styles */
159 r
= XGetIMValues(xim
, XNQueryInputStyle
, &xim_styles
, NULL
);
160 if (r
|| !xim_styles
)
161 g_message("Input Method does not support any styles");
165 /* find a style that doesnt need preedit or status */
166 for (i
= 0; i
< xim_styles
->count_styles
; ++i
) {
167 if (xim_styles
->supported_styles
[i
] ==
168 (XIMPreeditNothing
| XIMStatusNothing
))
170 xim_style
= xim_styles
->supported_styles
[i
];
178 g_message("Input Method does not support a usable style");
185 /* any existing contexts need to be recreated for the new input method */
186 for (it
= xic_all
; it
; it
= g_slist_next(it
))
187 obt_keyboard_context_renew(it
->data
);
193 guint
obt_keyboard_keyevent_to_modmask(XEvent
*e
)
197 g_return_val_if_fail(e
->type
== KeyPress
|| e
->type
== KeyRelease
,
198 OBT_KEYBOARD_MODKEY_NONE
);
200 for (masknum
= 0; masknum
< NUM_MASKS
; ++masknum
)
201 for (i
= 0; i
< modmap
->max_keypermod
; ++i
) {
202 KeyCode c
= modmap
->modifiermap
[masknum
*modmap
->max_keypermod
+ i
];
203 if (c
== e
->xkey
.keycode
)
209 guint
obt_keyboard_only_modmasks(guint mask
)
212 /* strip off these lock keys. they shouldn't affect key bindings */
213 mask
&= ~LockMask
; /* use the LockMask, not what capslock is bound to,
214 because you could bind it to something else and it
215 should work as that modifier then. i think capslock
217 mask
&= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_NUMLOCK
);
218 mask
&= ~obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SCROLLLOCK
);
222 guint
obt_keyboard_modkey_to_modmask(ObtModkeysKey key
)
224 return modkeys_keys
[key
];
227 static void set_modkey_mask(guchar mask
, KeySym sym
)
229 /* find what key this is, and bind it to the mask */
231 if (sym
== XK_Num_Lock
)
232 modkeys_keys
[OBT_KEYBOARD_MODKEY_NUMLOCK
] |= mask
;
233 else if (sym
== XK_Scroll_Lock
)
234 modkeys_keys
[OBT_KEYBOARD_MODKEY_SCROLLLOCK
] |= mask
;
236 else if (sym
== XK_Super_L
&& super_l
)
237 modkeys_keys
[OBT_KEYBOARD_MODKEY_SUPER
] |= mask
;
238 else if (sym
== XK_Super_L
&& !super_l
)
239 /* left takes precident over right, so erase any masks the right
241 modkeys_keys
[OBT_KEYBOARD_MODKEY_SUPER
] = mask
, super_l
= TRUE
;
242 else if (sym
== XK_Super_R
&& !super_l
)
243 modkeys_keys
[OBT_KEYBOARD_MODKEY_SUPER
] |= mask
;
245 else if (sym
== XK_Hyper_L
&& hyper_l
)
246 modkeys_keys
[OBT_KEYBOARD_MODKEY_HYPER
] |= mask
;
247 else if (sym
== XK_Hyper_L
&& !hyper_l
)
248 modkeys_keys
[OBT_KEYBOARD_MODKEY_HYPER
] = mask
, hyper_l
= TRUE
;
249 else if (sym
== XK_Hyper_R
&& !hyper_l
)
250 modkeys_keys
[OBT_KEYBOARD_MODKEY_HYPER
] |= mask
;
252 else if (sym
== XK_Alt_L
&& alt_l
)
253 modkeys_keys
[OBT_KEYBOARD_MODKEY_ALT
] |= mask
;
254 else if (sym
== XK_Alt_L
&& !alt_l
)
255 modkeys_keys
[OBT_KEYBOARD_MODKEY_ALT
] = mask
, alt_l
= TRUE
;
256 else if (sym
== XK_Alt_R
&& !alt_l
)
257 modkeys_keys
[OBT_KEYBOARD_MODKEY_ALT
] |= mask
;
259 else if (sym
== XK_Meta_L
&& meta_l
)
260 modkeys_keys
[OBT_KEYBOARD_MODKEY_META
] |= mask
;
261 else if (sym
== XK_Meta_L
&& !meta_l
)
262 modkeys_keys
[OBT_KEYBOARD_MODKEY_META
] = mask
, meta_l
= TRUE
;
263 else if (sym
== XK_Meta_R
&& !meta_l
)
264 modkeys_keys
[OBT_KEYBOARD_MODKEY_META
] |= mask
;
266 /* CapsLock, Shift, and Control are special and hard-coded */
269 KeyCode
* obt_keyboard_keysym_to_keycode(KeySym sym
)
274 ret
= g_new(KeyCode
, 1);
278 /* go through each keycode and look for the keysym */
279 for (i
= min_keycode
; i
<= max_keycode
; ++i
)
280 for (j
= 0; j
< keysyms_per_keycode
; ++j
)
281 if (sym
== keymap
[(i
-min_keycode
) * keysyms_per_keycode
+ j
]) {
282 ret
= g_renew(KeyCode
, ret
, ++n
+ 1);
289 gunichar
obt_keyboard_keypress_to_unichar(ObtIC
*ic
, XEvent
*ev
)
294 gchar
*buf
, fixbuf
[4]; /* 4 is enough for a utf8 char */
296 gboolean got_string
= FALSE
;
298 g_return_val_if_fail(ev
->type
== KeyPress
, 0);
301 g_warning("Using obt_keyboard_keypress_to_unichar() without an "
302 "Input Context. No i18n support!");
306 bufsz
= sizeof(fixbuf
);
308 #ifdef X_HAVE_UTF8_STRING
309 len
= Xutf8LookupString(ic
->xic
, &ev
->xkey
, buf
, bufsz
, &sym
, &status
);
311 len
= XmbLookupString(ic
->xic
, &ev
->xkey
, buf
, bufsz
, &sym
, &status
);
314 if (status
== XBufferOverflow
) {
315 buf
= g_new(char, len
);
318 #ifdef X_HAVE_UTF8_STRING
319 len
= Xutf8LookupString(ic
->xic
, &ev
->xkey
, buf
, bufsz
, &sym
,
322 len
= XmbLookupString(ic
->xic
, &ev
->xkey
, buf
, bufsz
, &sym
,
327 if ((status
== XLookupChars
|| status
== XLookupBoth
)) {
328 if ((guchar
)buf
[0] >= 32) { /* not an ascii control character */
329 #ifndef X_HAVE_UTF8_STRING
330 /* convert to utf8 */
332 buf
= g_locale_to_utf8(buf2
, r
, NULL
, NULL
, NULL
);
339 else if (status
== XLookupKeySym
)
340 /* this key doesn't have a text representation, it is a command
343 g_message("Bad keycode lookup. Keysym 0x%x Status: %s\n",
345 (status
== XBufferOverflow
? "BufferOverflow" :
346 status
== XLookupNone
? "XLookupNone" :
347 status
== XLookupKeySym
? "XLookupKeySym" :
352 bufsz
= sizeof(fixbuf
);
353 len
= XLookupString(&ev
->xkey
, buf
, bufsz
, &sym
, NULL
);
354 if ((guchar
)buf
[0] >= 32) /* not an ascii control character */
359 gunichar u
= g_utf8_get_char_validated(buf
, len
);
360 if (u
&& u
!= (gunichar
)-1 && u
!= (gunichar
)-2)
364 if (buf
!= fixbuf
) g_free(buf
);
369 KeySym
obt_keyboard_keypress_to_keysym(XEvent
*ev
)
373 g_return_val_if_fail(ev
->type
== KeyPress
, None
);
376 XLookupString(&ev
->xkey
, NULL
, 0, &sym
, NULL
);
380 void obt_keyboard_context_renew(ObtIC
*ic
)
383 ic
->xic
= XCreateIC(xim
,
384 XNInputStyle
, xim_style
,
385 XNClientWindow
, ic
->client
,
386 XNFocusWindow
, ic
->focus
,
389 g_message("Error creating Input Context for window 0x%x 0x%x\n",
390 (guint
)ic
->client
, (guint
)ic
->focus
);
394 ObtIC
* obt_keyboard_context_new(Window client
, Window focus
)
398 g_return_val_if_fail(client
!= None
&& focus
!= None
, NULL
);
400 ic
= g_slice_new(ObtIC
);
406 obt_keyboard_context_renew(ic
);
407 xic_all
= g_slist_prepend(xic_all
, ic
);
412 void obt_keyboard_context_ref(ObtIC
*ic
)
417 void obt_keyboard_context_unref(ObtIC
*ic
)
420 xic_all
= g_slist_remove(xic_all
, ic
);
423 g_slice_free(ObtIC
, ic
);