1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 modkeys.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.
24 /* These masks are constants and the modifier keys are bound to them as
26 ShiftMask (1<<0), LockMask (1<<1), ControlMask (1<<2), Mod1Mask (1<<3),
27 Mod2Mask (1<<4), Mod3Mask (1<<5), Mod4Mask (1<<6), Mod5Mask (1<<7)
30 #define ALL_MASKS 0xf /* an or'ing of all 8 keyboard masks */
32 /* Get the bitflag for the n'th modifier mask */
33 #define nth_mask(n) (1 << n)
35 static void set_modkey_mask(guchar mask, KeySym sym);
37 static XModifierKeymap *modmap;
38 /* This is a bitmask of the different masks for each modifier key */
39 static guchar modkeys_keys[OB_MODKEY_NUM_KEYS];
41 void modkeys_startup(gboolean reconfigure)
43 /* keycodes for the modifier keys which will be bound to the masks */
46 modmap = XGetModifierMapping(ob_display);
47 g_assert(modmap->max_keypermod > 0);
49 /* reset the keys to not be bound to any masks */
50 for (i = 0; i < OB_MODKEY_NUM_KEYS; ++i)
53 /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
54 for (i = 0; i < NUM_MASKS; ++i) {
55 /* go through each keycode that is bound to the mask */
56 for (j = 0; j < modmap->max_keypermod; ++j) {
58 /* get a keycode that is bound to the mask (i) */
59 KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
60 /* go through each keysym bound to the given keycode */
62 sym = XKeycodeToKeysym(ob_display, keycode, k);
63 if (sym == NoSymbol) break;
65 /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
66 set_modkey_mask(nth_mask(i), sym);
72 void modkeys_shutdown(gboolean reconfigure)
74 XFreeModifiermap(modmap);
77 guint modkeys_keycode_to_mask(guint keycode)
82 if (keycode == NoSymbol) return 0;
84 /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
85 for (i = 0; i < NUM_MASKS; ++i) {
86 /* go through each keycode that is bound to the mask */
87 for (j = 0; j < modmap->max_keypermod; ++j) {
88 /* compare with a keycode that is bound to the mask (i) */
89 if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
96 guint modkeys_only_modifier_masks(guint mask)
99 /* strip off these lock keys. they shouldn't affect key bindings */
100 mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_CAPSLOCK);
101 mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_NUMLOCK);
102 mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_SCROLLLOCK);
106 guint modkeys_key_to_mask(ObModkeysKey key)
108 return modkeys_keys[key];
111 static void set_modkey_mask(guchar mask, KeySym sym)
113 /* find what key this is, and bind it to the mask */
115 if (sym == XK_Num_Lock)
116 modkeys_keys[OB_MODKEY_KEY_NUMLOCK] |= mask;
117 else if (sym == XK_Scroll_Lock)
118 modkeys_keys[OB_MODKEY_KEY_SCROLLLOCK] |= mask;
119 else if (sym == XK_Caps_Lock)
120 modkeys_keys[OB_MODKEY_KEY_CAPSLOCK] |= mask;
121 else if (sym == XK_Shift_L || sym == XK_Shift_R)
122 modkeys_keys[OB_MODKEY_KEY_SHIFT] |= mask;
123 else if (sym == XK_Control_L || sym == XK_Control_R)
124 modkeys_keys[OB_MODKEY_KEY_CONTROL] |= mask;
125 else if (sym == XK_Super_L || sym == XK_Super_R)
126 modkeys_keys[OB_MODKEY_KEY_SUPER] |= mask;
127 else if (sym == XK_Hyper_L || sym == XK_Hyper_R)
128 modkeys_keys[OB_MODKEY_KEY_HYPER] |= mask;
129 else if (sym == XK_Alt_L || sym == XK_Alt_R)
130 modkeys_keys[OB_MODKEY_KEY_ALT] |= mask;
131 else if (sym == XK_Meta_L || sym == XK_Meta_R)
132 modkeys_keys[OB_MODKEY_KEY_META] |= mask;