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