change codename
[awesome.git] / key.c
blob6460e38f359e3439bdb813dbe0b0cc2fdf5ec4c6
1 /*
2 * key.c - Key bindings configuration management
4 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
5 * Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* XStringToKeysym() and XKeysymToString */
24 #include <X11/Xlib.h>
26 /* XCB doesn't provide keysyms definition */
27 #include <X11/keysym.h>
28 #include <X11/XF86keysym.h>
30 #include "structs.h"
31 #include "common/xutil.h"
32 #include "common/tokenize.h"
34 LUA_OBJECT_FUNCS(keyb_t, key, "key")
36 static void
37 key_unref_simplified(keyb_t **b)
39 key_unref(globalconf.L, *b);
42 ARRAY_FUNCS(keyb_t *, key, key_unref_simplified)
43 DO_LUA_TOSTRING(keyb_t, key, "key")
45 /** Garbage collect a key.
46 * \param L The Lua VM state.
47 * \return 0.
49 static int
50 luaA_key_gc(lua_State *L)
52 keyb_t *kbp = luaL_checkudata(L, 1, "key");
53 luaA_ref_array_wipe(&kbp->refs);
54 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, kbp->press);
55 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, kbp->release);
56 return 0;
59 /** Grab key on a window.
60 * \param win The window.
61 * \param k The key.
63 static void
64 window_grabkey(xcb_window_t win, keyb_t *k)
66 if(k->keycode)
67 xcb_grab_key(globalconf.connection, true, win,
68 k->mod, k->keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
69 else if(k->keysym)
71 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym);
72 if(keycodes)
74 for(xcb_keycode_t *kc = keycodes; *kc; kc++)
75 xcb_grab_key(globalconf.connection, true, win,
76 k->mod, *kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
77 p_delete(&keycodes);
82 void
83 window_grabkeys(xcb_window_t win, key_array_t *keys)
85 foreach(k, *keys)
86 window_grabkey(win, *k);
89 /** XCB equivalent of XLookupString which translate the keycode given
90 * by PressEvent to a KeySym and a string
91 * \todo use XKB!
93 static unsigned short const
94 keysym_to_unicode_1a1_1ff[] =
96 0x0104, 0x02d8, 0x0141, 0x0000, 0x013d, 0x015a, 0x0000, /* 0x01a0-0x01a7 */
97 0x0000, 0x0160, 0x015e, 0x0164, 0x0179, 0x0000, 0x017d, 0x017b, /* 0x01a8-0x01af */
98 0x0000, 0x0105, 0x02db, 0x0142, 0x0000, 0x013e, 0x015b, 0x02c7, /* 0x01b0-0x01b7 */
99 0x0000, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c, /* 0x01b8-0x01bf */
100 0x0154, 0x0000, 0x0000, 0x0102, 0x0000, 0x0139, 0x0106, 0x0000, /* 0x01c0-0x01c7 */
101 0x010c, 0x0000, 0x0118, 0x0000, 0x011a, 0x0000, 0x0000, 0x010e, /* 0x01c8-0x01cf */
102 0x0110, 0x0143, 0x0147, 0x0000, 0x0000, 0x0150, 0x0000, 0x0000, /* 0x01d0-0x01d7 */
103 0x0158, 0x016e, 0x0000, 0x0170, 0x0000, 0x0000, 0x0162, 0x0000, /* 0x01d8-0x01df */
104 0x0155, 0x0000, 0x0000, 0x0103, 0x0000, 0x013a, 0x0107, 0x0000, /* 0x01e0-0x01e7 */
105 0x010d, 0x0000, 0x0119, 0x0000, 0x011b, 0x0000, 0x0000, 0x010f, /* 0x01e8-0x01ef */
106 0x0111, 0x0144, 0x0148, 0x0000, 0x0000, 0x0151, 0x0000, 0x0000, /* 0x01f0-0x01f7 */
107 0x0159, 0x016f, 0x0000, 0x0171, 0x0000, 0x0000, 0x0163, 0x02d9 /* 0x01f8-0x01ff */
110 static unsigned short const
111 keysym_to_unicode_2a1_2fe[] =
113 0x0126, 0x0000, 0x0000, 0x0000, 0x0000, 0x0124, 0x0000, /* 0x02a0-0x02a7 */
114 0x0000, 0x0130, 0x0000, 0x011e, 0x0134, 0x0000, 0x0000, 0x0000, /* 0x02a8-0x02af */
115 0x0000, 0x0127, 0x0000, 0x0000, 0x0000, 0x0000, 0x0125, 0x0000, /* 0x02b0-0x02b7 */
116 0x0000, 0x0131, 0x0000, 0x011f, 0x0135, 0x0000, 0x0000, 0x0000, /* 0x02b8-0x02bf */
117 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x010a, 0x0108, 0x0000, /* 0x02c0-0x02c7 */
118 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x02c8-0x02cf */
119 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0120, 0x0000, 0x0000, /* 0x02d0-0x02d7 */
120 0x011c, 0x0000, 0x0000, 0x0000, 0x0000, 0x016c, 0x015c, 0x0000, /* 0x02d8-0x02df */
121 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x010b, 0x0109, 0x0000, /* 0x02e0-0x02e7 */
122 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x02e8-0x02ef */
123 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0121, 0x0000, 0x0000, /* 0x02f0-0x02f7 */
124 0x011d, 0x0000, 0x0000, 0x0000, 0x0000, 0x016d, 0x015d /* 0x02f8-0x02ff */
127 static unsigned short const
128 keysym_to_unicode_3a2_3fe[] =
130 0x0138, 0x0156, 0x0000, 0x0128, 0x013b, 0x0000, /* 0x03a0-0x03a7 */
131 0x0000, 0x0000, 0x0112, 0x0122, 0x0166, 0x0000, 0x0000, 0x0000, /* 0x03a8-0x03af */
132 0x0000, 0x0000, 0x0000, 0x0157, 0x0000, 0x0129, 0x013c, 0x0000, /* 0x03b0-0x03b7 */
133 0x0000, 0x0000, 0x0113, 0x0123, 0x0167, 0x014a, 0x0000, 0x014b, /* 0x03b8-0x03bf */
134 0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x012e, /* 0x03c0-0x03c7 */
135 0x0000, 0x0000, 0x0000, 0x0000, 0x0116, 0x0000, 0x0000, 0x012a, /* 0x03c8-0x03cf */
136 0x0000, 0x0145, 0x014c, 0x0136, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x03d0-0x03d7 */
137 0x0000, 0x0172, 0x0000, 0x0000, 0x0000, 0x0168, 0x016a, 0x0000, /* 0x03d8-0x03df */
138 0x0101, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x012f, /* 0x03e0-0x03e7 */
139 0x0000, 0x0000, 0x0000, 0x0000, 0x0117, 0x0000, 0x0000, 0x012b, /* 0x03e8-0x03ef */
140 0x0000, 0x0146, 0x014d, 0x0137, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x03f0-0x03f7 */
141 0x0000, 0x0173, 0x0000, 0x0000, 0x0000, 0x0169, 0x016b /* 0x03f8-0x03ff */
144 static unsigned short const
145 keysym_to_unicode_4a1_4df[] =
147 0x3002, 0x3008, 0x3009, 0x3001, 0x30fb, 0x30f2, 0x30a1, /* 0x04a0-0x04a7 */
148 0x30a3, 0x30a5, 0x30a7, 0x30a9, 0x30e3, 0x30e5, 0x30e7, 0x30c3, /* 0x04a8-0x04af */
149 0x30fc, 0x30a2, 0x30a4, 0x30a6, 0x30a8, 0x30aa, 0x30ab, 0x30ad, /* 0x04b0-0x04b7 */
150 0x30af, 0x30b1, 0x30b3, 0x30b5, 0x30b7, 0x30b9, 0x30bb, 0x30bd, /* 0x04b8-0x04bf */
151 0x30bf, 0x30c1, 0x30c4, 0x30c6, 0x30c8, 0x30ca, 0x30cb, 0x30cc, /* 0x04c0-0x04c7 */
152 0x30cd, 0x30ce, 0x30cf, 0x30d2, 0x30d5, 0x30d8, 0x30db, 0x30de, /* 0x04c8-0x04cf */
153 0x30df, 0x30e0, 0x30e1, 0x30e2, 0x30e4, 0x30e6, 0x30e8, 0x30e9, /* 0x04d0-0x04d7 */
154 0x30ea, 0x30eb, 0x30ec, 0x30ed, 0x30ef, 0x30f3, 0x309b, 0x309c /* 0x04d8-0x04df */
157 static unsigned short const
158 keysym_to_unicode_590_5fe[] =
160 0x06f0, 0x06f1, 0x06f2, 0x06f3, 0x06f4, 0x06f5, 0x06f6, 0x06f7, /* 0x0590-0x0597 */
161 0x06f8, 0x06f9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x0598-0x059f */
162 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x066a, 0x0670, 0x0679, /* 0x05a0-0x05a7 */
164 0x067e, 0x0686, 0x0688, 0x0691, 0x060c, 0x0000, 0x06d4, 0x0000, /* 0x05ac-0x05af */
165 0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667, /* 0x05b0-0x05b7 */
166 0x0668, 0x0669, 0x0000, 0x061b, 0x0000, 0x0000, 0x0000, 0x061f, /* 0x05b8-0x05bf */
167 0x0000, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627, /* 0x05c0-0x05c7 */
168 0x0628, 0x0629, 0x062a, 0x062b, 0x062c, 0x062d, 0x062e, 0x062f, /* 0x05c8-0x05cf */
169 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637, /* 0x05d0-0x05d7 */
170 0x0638, 0x0639, 0x063a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x05d8-0x05df */
171 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, /* 0x05e0-0x05e7 */
172 0x0648, 0x0649, 0x064a, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f, /* 0x05e8-0x05ef */
173 0x0650, 0x0651, 0x0652, 0x0653, 0x0654, 0x0655, 0x0698, 0x06a4, /* 0x05f0-0x05f7 */
174 0x06a9, 0x06af, 0x06ba, 0x06be, 0x06cc, 0x06d2, 0x06c1 /* 0x05f8-0x05fe */
177 static unsigned short const
178 keysym_to_unicode_680_6ff[] =
180 0x0492, 0x0496, 0x049a, 0x049c, 0x04a2, 0x04ae, 0x04b0, 0x04b2, /* 0x0680-0x0687 */
181 0x04b6, 0x04b8, 0x04ba, 0x0000, 0x04d8, 0x04e2, 0x04e8, 0x04ee, /* 0x0688-0x068f */
182 0x0493, 0x0497, 0x049b, 0x049d, 0x04a3, 0x04af, 0x04b1, 0x04b3, /* 0x0690-0x0697 */
183 0x04b7, 0x04b9, 0x04bb, 0x0000, 0x04d9, 0x04e3, 0x04e9, 0x04ef, /* 0x0698-0x069f */
184 0x0000, 0x0452, 0x0453, 0x0451, 0x0454, 0x0455, 0x0456, 0x0457, /* 0x06a0-0x06a7 */
185 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x0491, 0x045e, 0x045f, /* 0x06a8-0x06af */
186 0x2116, 0x0402, 0x0403, 0x0401, 0x0404, 0x0405, 0x0406, 0x0407, /* 0x06b0-0x06b7 */
187 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x0490, 0x040e, 0x040f, /* 0x06b8-0x06bf */
188 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, /* 0x06c0-0x06c7 */
189 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, /* 0x06c8-0x06cf */
190 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, /* 0x06d0-0x06d7 */
191 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, /* 0x06d8-0x06df */
192 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, /* 0x06e0-0x06e7 */
193 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, /* 0x06e8-0x06ef */
194 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, /* 0x06f0-0x06f7 */
195 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a /* 0x06f8-0x06ff */
198 static unsigned short const
199 keysym_to_unicode_7a1_7f9[] =
201 0x0386, 0x0388, 0x0389, 0x038a, 0x03aa, 0x0000, 0x038c, /* 0x07a0-0x07a7 */
202 0x038e, 0x03ab, 0x0000, 0x038f, 0x0000, 0x0000, 0x0385, 0x2015, /* 0x07a8-0x07af */
203 0x0000, 0x03ac, 0x03ad, 0x03ae, 0x03af, 0x03ca, 0x0390, 0x03cc, /* 0x07b0-0x07b7 */
204 0x03cd, 0x03cb, 0x03b0, 0x03ce, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x07b8-0x07bf */
205 0x0000, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, /* 0x07c0-0x07c7 */
206 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, /* 0x07c8-0x07cf */
207 0x03a0, 0x03a1, 0x03a3, 0x0000, 0x03a4, 0x03a5, 0x03a6, 0x03a7, /* 0x07d0-0x07d7 */
208 0x03a8, 0x03a9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x07d8-0x07df */
209 0x0000, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, /* 0x07e0-0x07e7 */
210 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, /* 0x07e8-0x07ef */
211 0x03c0, 0x03c1, 0x03c3, 0x03c2, 0x03c4, 0x03c5, 0x03c6, 0x03c7, /* 0x07f0-0x07f7 */
212 0x03c8, 0x03c9 /* 0x07f8-0x07ff */
215 static unsigned short const
216 keysym_to_unicode_8a4_8fe[] =
218 0x2320, 0x2321, 0x0000, 0x231c, /* 0x08a0-0x08a7 */
219 0x231d, 0x231e, 0x231f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x08a8-0x08af */
220 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x08b0-0x08b7 */
221 0x0000, 0x0000, 0x0000, 0x0000, 0x2264, 0x2260, 0x2265, 0x222b, /* 0x08b8-0x08bf */
222 0x2234, 0x0000, 0x221e, 0x0000, 0x0000, 0x2207, 0x0000, 0x0000, /* 0x08c0-0x08c7 */
223 0x2245, 0x2246, 0x0000, 0x0000, 0x0000, 0x0000, 0x22a2, 0x0000, /* 0x08c8-0x08cf */
224 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x221a, 0x0000, /* 0x08d0-0x08d7 */
225 0x0000, 0x0000, 0x2282, 0x2283, 0x2229, 0x222a, 0x2227, 0x2228, /* 0x08d8-0x08df */
226 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x08e0-0x08e7 */
227 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x08e8-0x08ef */
228 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0192, 0x0000, /* 0x08f0-0x08f7 */
229 0x0000, 0x0000, 0x0000, 0x2190, 0x2191, 0x2192, 0x2193 /* 0x08f8-0x08ff */
232 static unsigned short const
233 keysym_to_unicode_9df_9f8[] =
235 0x2422, /* 0x09d8-0x09df */
236 0x2666, 0x25a6, 0x2409, 0x240c, 0x240d, 0x240a, 0x0000, 0x0000, /* 0x09e0-0x09e7 */
237 0x240a, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x2500, /* 0x09e8-0x09ef */
238 0x0000, 0x0000, 0x0000, 0x0000, 0x251c, 0x2524, 0x2534, 0x252c, /* 0x09f0-0x09f7 */
239 0x2502 /* 0x09f8-0x09ff */
242 static unsigned short const
243 keysym_to_unicode_aa1_afe[] =
245 0x2003, 0x2002, 0x2004, 0x2005, 0x2007, 0x2008, 0x2009, /* 0x0aa0-0x0aa7 */
246 0x200a, 0x2014, 0x2013, 0x0000, 0x0000, 0x0000, 0x2026, 0x2025, /* 0x0aa8-0x0aaf */
247 0x2153, 0x2154, 0x2155, 0x2156, 0x2157, 0x2158, 0x2159, 0x215a, /* 0x0ab0-0x0ab7 */
248 0x2105, 0x0000, 0x0000, 0x2012, 0x2039, 0x2024, 0x203a, 0x0000, /* 0x0ab8-0x0abf */
249 0x0000, 0x0000, 0x0000, 0x215b, 0x215c, 0x215d, 0x215e, 0x0000, /* 0x0ac0-0x0ac7 */
250 0x0000, 0x2122, 0x2120, 0x0000, 0x25c1, 0x25b7, 0x25cb, 0x25ad, /* 0x0ac8-0x0acf */
251 0x2018, 0x2019, 0x201c, 0x201d, 0x211e, 0x0000, 0x2032, 0x2033, /* 0x0ad0-0x0ad7 */
252 0x0000, 0x271d, 0x0000, 0x220e, 0x25c2, 0x2023, 0x25cf, 0x25ac, /* 0x0ad8-0x0adf */
253 0x25e6, 0x25ab, 0x25ae, 0x25b5, 0x25bf, 0x2606, 0x2022, 0x25aa, /* 0x0ae0-0x0ae7 */
254 0x25b4, 0x25be, 0x261a, 0x261b, 0x2663, 0x2666, 0x2665, 0x0000, /* 0x0ae8-0x0aef */
255 0x2720, 0x2020, 0x2021, 0x2713, 0x2612, 0x266f, 0x266d, 0x2642, /* 0x0af0-0x0af7 */
256 0x2640, 0x2121, 0x2315, 0x2117, 0x2038, 0x201a, 0x201e /* 0x0af8-0x0aff */
259 /* none of the APL keysyms match the Unicode characters */
260 static unsigned short const
261 keysym_to_unicode_cdf_cfa[] =
263 0x2017, /* 0x0cd8-0x0cdf */
264 0x05d0, 0x05d1, 0x05d2, 0x05d3, 0x05d4, 0x05d5, 0x05d6, 0x05d7, /* 0x0ce0-0x0ce7 */
265 0x05d8, 0x05d9, 0x05da, 0x05db, 0x05dc, 0x05dd, 0x05de, 0x05df, /* 0x0ce8-0x0cef */
266 0x05e0, 0x05e1, 0x05e2, 0x05e3, 0x05e4, 0x05e5, 0x05e6, 0x05e7, /* 0x0cf0-0x0cf7 */
267 0x05e8, 0x05e9, 0x05ea /* 0x0cf8-0x0cff */
270 static unsigned short const
271 keysym_to_unicode_da1_df9[] =
273 0x0e01, 0x0e02, 0x0e03, 0x0e04, 0x0e05, 0x0e06, 0x0e07, /* 0x0da0-0x0da7 */
274 0x0e08, 0x0e09, 0x0e0a, 0x0e0b, 0x0e0c, 0x0e0d, 0x0e0e, 0x0e0f, /* 0x0da8-0x0daf */
275 0x0e10, 0x0e11, 0x0e12, 0x0e13, 0x0e14, 0x0e15, 0x0e16, 0x0e17, /* 0x0db0-0x0db7 */
276 0x0e18, 0x0e19, 0x0e1a, 0x0e1b, 0x0e1c, 0x0e1d, 0x0e1e, 0x0e1f, /* 0x0db8-0x0dbf */
277 0x0e20, 0x0e21, 0x0e22, 0x0e23, 0x0e24, 0x0e25, 0x0e26, 0x0e27, /* 0x0dc0-0x0dc7 */
278 0x0e28, 0x0e29, 0x0e2a, 0x0e2b, 0x0e2c, 0x0e2d, 0x0e2e, 0x0e2f, /* 0x0dc8-0x0dcf */
279 0x0e30, 0x0e31, 0x0e32, 0x0e33, 0x0e34, 0x0e35, 0x0e36, 0x0e37, /* 0x0dd0-0x0dd7 */
280 0x0e38, 0x0e39, 0x0e3a, 0x0000, 0x0000, 0x0000, 0x0e3e, 0x0e3f, /* 0x0dd8-0x0ddf */
281 0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47, /* 0x0de0-0x0de7 */
282 0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x0000, 0x0000, /* 0x0de8-0x0def */
283 0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57, /* 0x0df0-0x0df7 */
284 0x0e58, 0x0e59 /* 0x0df8-0x0dff */
287 static unsigned short const
288 keysym_to_unicode_ea0_eff[] =
290 0x0000, 0x1101, 0x1101, 0x11aa, 0x1102, 0x11ac, 0x11ad, 0x1103, /* 0x0ea0-0x0ea7 */
291 0x1104, 0x1105, 0x11b0, 0x11b1, 0x11b2, 0x11b3, 0x11b4, 0x11b5, /* 0x0ea8-0x0eaf */
292 0x11b6, 0x1106, 0x1107, 0x1108, 0x11b9, 0x1109, 0x110a, 0x110b, /* 0x0eb0-0x0eb7 */
293 0x110c, 0x110d, 0x110e, 0x110f, 0x1110, 0x1111, 0x1112, 0x1161, /* 0x0eb8-0x0ebf */
294 0x1162, 0x1163, 0x1164, 0x1165, 0x1166, 0x1167, 0x1168, 0x1169, /* 0x0ec0-0x0ec7 */
295 0x116a, 0x116b, 0x116c, 0x116d, 0x116e, 0x116f, 0x1170, 0x1171, /* 0x0ec8-0x0ecf */
296 0x1172, 0x1173, 0x1174, 0x1175, 0x11a8, 0x11a9, 0x11aa, 0x11ab, /* 0x0ed0-0x0ed7 */
297 0x11ac, 0x11ad, 0x11ae, 0x11af, 0x11b0, 0x11b1, 0x11b2, 0x11b3, /* 0x0ed8-0x0edf */
298 0x11b4, 0x11b5, 0x11b6, 0x11b7, 0x11b8, 0x11b9, 0x11ba, 0x11bb, /* 0x0ee0-0x0ee7 */
299 0x11bc, 0x11bd, 0x11be, 0x11bf, 0x11c0, 0x11c1, 0x11c2, 0x0000, /* 0x0ee8-0x0eef */
300 0x0000, 0x0000, 0x1140, 0x0000, 0x0000, 0x1159, 0x119e, 0x0000, /* 0x0ef0-0x0ef7 */
301 0x11eb, 0x0000, 0x11f9, 0x0000, 0x0000, 0x0000, 0x0000, 0x20a9, /* 0x0ef8-0x0eff */
304 static unsigned short const
305 keysym_to_unicode_12a1_12fe[] =
307 0x1e02, 0x1e03, 0x0000, 0x0000, 0x0000, 0x1e0a, 0x0000, /* 0x12a0-0x12a7 */
308 0x1e80, 0x0000, 0x1e82, 0x1e0b, 0x1ef2, 0x0000, 0x0000, 0x0000, /* 0x12a8-0x12af */
309 0x1e1e, 0x1e1f, 0x0000, 0x0000, 0x1e40, 0x1e41, 0x0000, 0x1e56, /* 0x12b0-0x12b7 */
310 0x1e81, 0x1e57, 0x1e83, 0x1e60, 0x1ef3, 0x1e84, 0x1e85, 0x1e61, /* 0x12b8-0x12bf */
311 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x12c0-0x12c7 */
312 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x12c8-0x12cf */
313 0x0174, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1e6a, /* 0x12d0-0x12d7 */
314 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0176, 0x0000, /* 0x12d8-0x12df */
315 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x12e0-0x12e7 */
316 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x12e8-0x12ef */
317 0x0175, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1e6b, /* 0x12f0-0x12f7 */
318 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0177 /* 0x12f0-0x12ff */
321 static unsigned short const
322 keysym_to_unicode_13bc_13be[] =
324 0x0152, 0x0153, 0x0178 /* 0x13b8-0x13bf */
327 static unsigned short const
328 keysym_to_unicode_14a1_14ff[] =
330 0x2741, 0x00a7, 0x0589, 0x0029, 0x0028, 0x00bb, 0x00ab, /* 0x14a0-0x14a7 */
331 0x2014, 0x002e, 0x055d, 0x002c, 0x2013, 0x058a, 0x2026, 0x055c, /* 0x14a8-0x14af */
332 0x055b, 0x055e, 0x0531, 0x0561, 0x0532, 0x0562, 0x0533, 0x0563, /* 0x14b0-0x14b7 */
333 0x0534, 0x0564, 0x0535, 0x0565, 0x0536, 0x0566, 0x0537, 0x0567, /* 0x14b8-0x14bf */
334 0x0538, 0x0568, 0x0539, 0x0569, 0x053a, 0x056a, 0x053b, 0x056b, /* 0x14c0-0x14c7 */
335 0x053c, 0x056c, 0x053d, 0x056d, 0x053e, 0x056e, 0x053f, 0x056f, /* 0x14c8-0x14cf */
336 0x0540, 0x0570, 0x0541, 0x0571, 0x0542, 0x0572, 0x0543, 0x0573, /* 0x14d0-0x14d7 */
337 0x0544, 0x0574, 0x0545, 0x0575, 0x0546, 0x0576, 0x0547, 0x0577, /* 0x14d8-0x14df */
338 0x0548, 0x0578, 0x0549, 0x0579, 0x054a, 0x057a, 0x054b, 0x057b, /* 0x14e0-0x14e7 */
339 0x054c, 0x057c, 0x054d, 0x057d, 0x054e, 0x057e, 0x054f, 0x057f, /* 0x14e8-0x14ef */
340 0x0550, 0x0580, 0x0551, 0x0581, 0x0552, 0x0582, 0x0553, 0x0583, /* 0x14f0-0x14f7 */
341 0x0554, 0x0584, 0x0555, 0x0585, 0x0556, 0x0586, 0x2019, 0x0027, /* 0x14f8-0x14ff */
344 static unsigned short const
345 keysym_to_unicode_15d0_15f6[] =
347 0x10d0, 0x10d1, 0x10d2, 0x10d3, 0x10d4, 0x10d5, 0x10d6, 0x10d7, /* 0x15d0-0x15d7 */
348 0x10d8, 0x10d9, 0x10da, 0x10db, 0x10dc, 0x10dd, 0x10de, 0x10df, /* 0x15d8-0x15df */
349 0x10e0, 0x10e1, 0x10e2, 0x10e3, 0x10e4, 0x10e5, 0x10e6, 0x10e7, /* 0x15e0-0x15e7 */
350 0x10e8, 0x10e9, 0x10ea, 0x10eb, 0x10ec, 0x10ed, 0x10ee, 0x10ef, /* 0x15e8-0x15ef */
351 0x10f0, 0x10f1, 0x10f2, 0x10f3, 0x10f4, 0x10f5, 0x10f6 /* 0x15f0-0x15f7 */
354 static unsigned short const
355 keysym_to_unicode_16a0_16f6[] =
357 0x0000, 0x0000, 0xf0a2, 0x1e8a, 0x0000, 0xf0a5, 0x012c, 0xf0a7, /* 0x16a0-0x16a7 */
358 0xf0a8, 0x01b5, 0x01e6, 0x0000, 0x0000, 0x0000, 0x0000, 0x019f, /* 0x16a8-0x16af */
359 0x0000, 0x0000, 0xf0b2, 0x1e8b, 0x01d1, 0xf0b5, 0x012d, 0xf0b7, /* 0x16b0-0x16b7 */
360 0xf0b8, 0x01b6, 0x01e7, 0x0000, 0x0000, 0x01d2, 0x0000, 0x0275, /* 0x16b8-0x16bf */
361 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x018f, 0x0000, /* 0x16c0-0x16c7 */
362 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x16c8-0x16cf */
363 0x0000, 0x1e36, 0xf0d2, 0xf0d3, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x16d0-0x16d7 */
364 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x16d8-0x16df */
365 0x0000, 0x1e37, 0xf0e2, 0xf0e3, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x16e0-0x16e7 */
366 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 0x16e8-0x16ef */
367 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0259 /* 0x16f0-0x16f6 */
370 static unsigned short const
371 keysym_to_unicode_1e9f_1eff[] =
373 0x0303,
374 0x1ea0, 0x1ea1, 0x1ea2, 0x1ea3, 0x1ea4, 0x1ea5, 0x1ea6, 0x1ea7, /* 0x1ea0-0x1ea7 */
375 0x1ea8, 0x1ea9, 0x1eaa, 0x1eab, 0x1eac, 0x1ead, 0x1eae, 0x1eaf, /* 0x1ea8-0x1eaf */
376 0x1eb0, 0x1eb1, 0x1eb2, 0x1eb3, 0x1eb4, 0x1eb5, 0x1eb6, 0x1eb7, /* 0x1eb0-0x1eb7 */
377 0x1eb8, 0x1eb9, 0x1eba, 0x1ebb, 0x1ebc, 0x1ebd, 0x1ebe, 0x1ebf, /* 0x1eb8-0x1ebf */
378 0x1ec0, 0x1ec1, 0x1ec2, 0x1ec3, 0x1ec4, 0x1ec5, 0x1ec6, 0x1ec7, /* 0x1ec0-0x1ec7 */
379 0x1ec8, 0x1ec9, 0x1eca, 0x1ecb, 0x1ecc, 0x1ecd, 0x1ece, 0x1ecf, /* 0x1ec8-0x1ecf */
380 0x1ed0, 0x1ed1, 0x1ed2, 0x1ed3, 0x1ed4, 0x1ed5, 0x1ed6, 0x1ed7, /* 0x1ed0-0x1ed7 */
381 0x1ed8, 0x1ed9, 0x1eda, 0x1edb, 0x1edc, 0x1edd, 0x1ede, 0x1edf, /* 0x1ed8-0x1edf */
382 0x1ee0, 0x1ee1, 0x1ee2, 0x1ee3, 0x1ee4, 0x1ee5, 0x1ee6, 0x1ee7, /* 0x1ee0-0x1ee7 */
383 0x1ee8, 0x1ee9, 0x1eea, 0x1eeb, 0x1eec, 0x1eed, 0x1eee, 0x1eef, /* 0x1ee8-0x1eef */
384 0x1ef0, 0x1ef1, 0x0300, 0x0301, 0x1ef4, 0x1ef5, 0x1ef6, 0x1ef7, /* 0x1ef0-0x1ef7 */
385 0x1ef8, 0x1ef9, 0x01a0, 0x01a1, 0x01af, 0x01b0, 0x0309, 0x0323 /* 0x1ef8-0x1eff */
388 static unsigned short const
389 keysym_to_unicode_20a0_20ac[] =
391 0x20a0, 0x20a1, 0x20a2, 0x20a3, 0x20a4, 0x20a5, 0x20a6, 0x20a7, /* 0x20a0-0x20a7 */
392 0x20a8, 0x20a9, 0x20aa, 0x20ab, 0x20ac /* 0x20a8-0x20af */
395 static uint8_t const __utf8_mark[7] = {
396 0x00, 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
399 static int8_t const __utf32_clz_to_len[32] = {
400 7, /* 0x80000000 */
401 6, 6, 6, 6, 6, /* 0x04000000 */
402 5, 5, 5, 5, 5, /* 0x00200000 */
403 4, 4, 4, 4, 4, /* 0x00010000 */
404 3, 3, 3, 3, 3, /* 0x00000800 */
405 2, 2, 2, 2, /* 0x00000080 */
406 1, 1, 1, 1, 1, 1, 1 /* 0x00000001 */
408 #define utf8clen(c) __utf32_clz_to_len[__builtin_clz((uint32_t)(c) | 1)]
410 static bool
411 keysym_to_utf8(char *buf, int len, const xcb_keysym_t ksym)
413 unsigned int ksym_conv;
414 int count;
416 /* Unicode keysym */
417 if((ksym & 0xff000000) == 0x01000000)
418 ksym_conv = ksym & 0x00ffffff;
419 else if(ksym > 0 && ksym < 0x100)
420 ksym_conv = ksym;
421 else if(ksym > 0x1a0 && ksym < 0x200)
422 ksym_conv = keysym_to_unicode_1a1_1ff[ksym - 0x1a1];
423 else if(ksym > 0x2a0 && ksym < 0x2ff)
424 ksym_conv = keysym_to_unicode_2a1_2fe[ksym - 0x2a1];
425 else if(ksym > 0x3a1 && ksym < 0x3ff)
426 ksym_conv = keysym_to_unicode_3a2_3fe[ksym - 0x3a2];
427 else if(ksym > 0x4a0 && ksym < 0x4e0)
428 ksym_conv = keysym_to_unicode_4a1_4df[ksym - 0x4a1];
429 else if(ksym > 0x589 && ksym < 0x5ff)
430 ksym_conv = keysym_to_unicode_590_5fe[ksym - 0x590];
431 else if(ksym > 0x67f && ksym < 0x700)
432 ksym_conv = keysym_to_unicode_680_6ff[ksym - 0x680];
433 else if(ksym > 0x7a0 && ksym < 0x7fa)
434 ksym_conv = keysym_to_unicode_7a1_7f9[ksym - 0x7a1];
435 else if(ksym > 0x8a3 && ksym < 0x8ff)
436 ksym_conv = keysym_to_unicode_8a4_8fe[ksym - 0x8a4];
437 else if(ksym > 0x9de && ksym < 0x9f9)
438 ksym_conv = keysym_to_unicode_9df_9f8[ksym - 0x9df];
439 else if(ksym > 0xaa0 && ksym < 0xaff)
440 ksym_conv = keysym_to_unicode_aa1_afe[ksym - 0xaa1];
441 else if(ksym > 0xcde && ksym < 0xcfb)
442 ksym_conv = keysym_to_unicode_cdf_cfa[ksym - 0xcdf];
443 else if(ksym > 0xda0 && ksym < 0xdfa)
444 ksym_conv = keysym_to_unicode_da1_df9[ksym - 0xda1];
445 else if(ksym > 0xe9f && ksym < 0xf00)
446 ksym_conv = keysym_to_unicode_ea0_eff[ksym - 0xea0];
447 else if(ksym > 0x12a0 && ksym < 0x12ff)
448 ksym_conv = keysym_to_unicode_12a1_12fe[ksym - 0x12a1];
449 else if(ksym > 0x13bb && ksym < 0x13bf)
450 ksym_conv = keysym_to_unicode_13bc_13be[ksym - 0x13bc];
451 else if(ksym > 0x14a0 && ksym < 0x1500)
452 ksym_conv = keysym_to_unicode_14a1_14ff[ksym - 0x14a1];
453 else if(ksym > 0x15cf && ksym < 0x15f7)
454 ksym_conv = keysym_to_unicode_15d0_15f6[ksym - 0x15d0];
455 else if(ksym > 0x169f && ksym < 0x16f7)
456 ksym_conv = keysym_to_unicode_16a0_16f6[ksym - 0x16a0];
457 else if(ksym > 0x1e9e && ksym < 0x1f00)
458 ksym_conv = keysym_to_unicode_1e9f_1eff[ksym - 0x1e9f];
459 else if(ksym > 0x209f && ksym < 0x20ad)
460 ksym_conv = keysym_to_unicode_20a0_20ac[ksym - 0x20a0];
461 else
462 return false;
464 count = utf8clen(ksym_conv);
465 switch(count)
467 case 7: return false;
468 case 6: buf[5] = (ksym_conv | 0x80) & 0xbf; ksym_conv >>= 6;
469 case 5: buf[4] = (ksym_conv | 0x80) & 0xbf; ksym_conv >>= 6;
470 case 4: buf[3] = (ksym_conv | 0x80) & 0xbf; ksym_conv >>= 6;
471 case 3: buf[2] = (ksym_conv | 0x80) & 0xbf; ksym_conv >>= 6;
472 case 2: buf[1] = (ksym_conv | 0x80) & 0xbf; ksym_conv >>= 6;
473 case 1: buf[0] = (ksym_conv | __utf8_mark[count]);
475 buf[count] = '\0';
476 return true;
479 static bool
480 keysym_to_str(char *buf, ssize_t len, const xcb_keysym_t ksym)
482 switch(ksym)
484 #define CASE(k) case XK_##k: a_strcpy(buf, len, #k); return true
485 CASE(BackSpace);
486 CASE(Tab);
487 CASE(Clear);
488 CASE(Return);
489 CASE(Pause);
490 CASE(Scroll_Lock);
491 CASE(Sys_Req);
492 CASE(Escape);
493 CASE(Delete);
495 CASE(Home);
496 CASE(Left);
497 CASE(Up);
498 CASE(Right);
499 CASE(Down);
500 CASE(Page_Up);
501 CASE(Page_Down);
502 CASE(End);
503 CASE(Begin);
505 CASE(Select);
506 CASE(Print);
507 CASE(Execute);
508 CASE(Insert);
509 CASE(Undo);
510 CASE(Redo);
511 CASE(Menu);
512 CASE(Find);
513 CASE(Cancel);
514 CASE(Help);
515 CASE(Break);
516 CASE(Mode_switch);
517 CASE(Num_Lock);
519 case XK_KP_Space:
520 /* Patch encoding botch */
521 buf[0] = XK_space & 0x7F;
522 break;
523 CASE(KP_Tab);
524 CASE(KP_Enter);
525 CASE(KP_F1);
526 CASE(KP_F2);
527 CASE(KP_F3);
528 CASE(KP_F4);
529 CASE(KP_Home);
530 CASE(KP_Left);
531 CASE(KP_Up);
532 CASE(KP_Right);
533 CASE(KP_Down);
534 CASE(KP_Page_Up);
535 CASE(KP_Page_Down);
536 CASE(KP_End);
537 CASE(KP_Begin);
538 CASE(KP_Insert);
539 CASE(KP_Delete);
540 CASE(KP_Separator);
542 CASE(F1); CASE(F2); CASE(F3); CASE(F4); CASE(F5); CASE(F6);
543 CASE(F7); CASE(F8); CASE(F9); CASE(F10); CASE(F11); CASE(F12);
544 CASE(F13); CASE(F14); CASE(F15); CASE(F16); CASE(F17); CASE(F18);
545 CASE(F19); CASE(F20); CASE(F21); CASE(F22); CASE(F23); CASE(F24);
546 CASE(F25); CASE(F26); CASE(F27); CASE(F28); CASE(F29); CASE(F30);
547 CASE(F31); CASE(F32); CASE(F33); CASE(F34); CASE(F35);
549 CASE(Shift_L);
550 CASE(Shift_R);
551 CASE(Control_L);
552 CASE(Control_R);
553 CASE(Caps_Lock);
554 CASE(Shift_Lock);
556 CASE(Meta_L);
557 CASE(Meta_R);
558 CASE(Alt_L);
559 CASE(Alt_R);
560 CASE(Super_L);
561 CASE(Super_R);
562 CASE(Hyper_L);
563 CASE(Hyper_R);
564 default:
565 buf[0] = ksym & 0x7F;
566 break;
569 buf[1] = '\0';
570 return true;
573 static bool
574 keysym_to_xkb(char *buf, ssize_t len, const xcb_keysym_t ksym)
576 switch(ksym)
578 CASE(ISO_Lock);
579 CASE(ISO_Level2_Latch);
580 CASE(ISO_Level3_Shift);
581 CASE(ISO_Level3_Latch);
582 CASE(ISO_Level3_Lock);
583 CASE(ISO_Level5_Shift);
584 CASE(ISO_Level5_Latch);
585 CASE(ISO_Level5_Lock);
586 CASE(ISO_Group_Shift);
587 CASE(ISO_Group_Latch);
588 CASE(ISO_Group_Lock);
589 CASE(ISO_Next_Group);
590 CASE(ISO_Next_Group_Lock);
591 CASE(ISO_Prev_Group);
592 CASE(ISO_Prev_Group_Lock);
593 CASE(ISO_First_Group);
594 CASE(ISO_First_Group_Lock);
595 CASE(ISO_Last_Group);
596 CASE(ISO_Last_Group_Lock);
597 CASE(ISO_Left_Tab);
598 CASE(ISO_Move_Line_Up);
599 CASE(ISO_Move_Line_Down);
600 CASE(ISO_Partial_Line_Up);
601 CASE(ISO_Partial_Line_Down);
602 CASE(ISO_Partial_Space_Left);
603 CASE(ISO_Partial_Space_Right);
604 CASE(ISO_Set_Margin_Left);
605 CASE(ISO_Set_Margin_Right);
606 CASE(ISO_Release_Margin_Left);
607 CASE(ISO_Release_Margin_Right);
608 CASE(ISO_Release_Both_Margins);
609 CASE(ISO_Fast_Cursor_Left);
610 CASE(ISO_Fast_Cursor_Right);
611 CASE(ISO_Fast_Cursor_Up);
612 CASE(ISO_Fast_Cursor_Down);
613 CASE(ISO_Continuous_Underline);
614 CASE(ISO_Discontinuous_Underline);
615 CASE(ISO_Emphasize);
616 CASE(ISO_Center_Object);
617 CASE(ISO_Enter);
618 CASE(dead_grave);
619 CASE(dead_acute);
620 CASE(dead_circumflex);
621 CASE(dead_tilde);
622 CASE(dead_macron);
623 CASE(dead_breve);
624 CASE(dead_abovedot);
625 CASE(dead_diaeresis);
626 CASE(dead_abovering);
627 CASE(dead_doubleacute);
628 CASE(dead_caron);
629 CASE(dead_cedilla);
630 CASE(dead_ogonek);
631 CASE(dead_iota);
632 CASE(dead_voiced_sound);
633 CASE(dead_semivoiced_sound);
634 CASE(dead_belowdot);
635 CASE(dead_hook);
636 CASE(dead_horn);
637 CASE(dead_stroke);
638 CASE(dead_abovecomma);
639 CASE(dead_abovereversedcomma);
640 CASE(dead_doublegrave);
641 CASE(dead_belowring);
642 CASE(dead_belowmacron);
643 CASE(dead_belowcircumflex);
644 CASE(dead_belowtilde);
645 CASE(dead_belowbreve);
646 CASE(dead_belowdiaeresis);
647 CASE(dead_invertedbreve);
648 CASE(dead_belowcomma);
649 CASE(dead_currency);
650 CASE(dead_a);
651 CASE(dead_A);
652 CASE(dead_e);
653 CASE(dead_E);
654 CASE(dead_i);
655 CASE(dead_I);
656 CASE(dead_o);
657 CASE(dead_O);
658 CASE(dead_u);
659 CASE(dead_U);
660 CASE(dead_small_schwa);
661 CASE(dead_capital_schwa);
662 CASE(First_Virtual_Screen);
663 CASE(Prev_Virtual_Screen);
664 CASE(Next_Virtual_Screen);
665 CASE(Last_Virtual_Screen);
666 CASE(Terminate_Server);
667 CASE(AccessX_Enable);
668 CASE(AccessX_Feedback_Enable);
669 CASE(RepeatKeys_Enable);
670 CASE(SlowKeys_Enable);
671 CASE(BounceKeys_Enable);
672 CASE(StickyKeys_Enable);
673 CASE(MouseKeys_Enable);
674 CASE(MouseKeys_Accel_Enable);
675 CASE(Overlay1_Enable);
676 CASE(Overlay2_Enable);
677 CASE(AudibleBell_Enable);
678 CASE(Pointer_Left);
679 CASE(Pointer_Right);
680 CASE(Pointer_Up);
681 CASE(Pointer_Down);
682 CASE(Pointer_UpLeft);
683 CASE(Pointer_UpRight);
684 CASE(Pointer_DownLeft);
685 CASE(Pointer_DownRight);
686 CASE(Pointer_Button_Dflt);
687 CASE(Pointer_Button1);
688 CASE(Pointer_Button2);
689 CASE(Pointer_Button3);
690 CASE(Pointer_Button4);
691 CASE(Pointer_Button5);
692 CASE(Pointer_DblClick_Dflt);
693 CASE(Pointer_DblClick1);
694 CASE(Pointer_DblClick2);
695 CASE(Pointer_DblClick3);
696 CASE(Pointer_DblClick4);
697 CASE(Pointer_DblClick5);
698 CASE(Pointer_Drag_Dflt);
699 CASE(Pointer_Drag1);
700 CASE(Pointer_Drag2);
701 CASE(Pointer_Drag3);
702 CASE(Pointer_Drag4);
703 CASE(Pointer_Drag5);
704 CASE(Pointer_EnableKeys);
705 CASE(Pointer_Accelerate);
706 CASE(Pointer_DfltBtnNext);
707 CASE(Pointer_DfltBtnPrev);
708 default:
709 return false;
712 return true;
714 #undef CASE
716 static bool
717 keysym_to_xf86(char *buf, ssize_t len, const xcb_keysym_t ksym)
719 switch(ksym)
721 #define CASE(k) case XF86XK_##k: a_strcpy(buf, len, "XF86"); a_strcat(buf, len, #k); return true
722 CASE(ModeLock);
723 CASE(MonBrightnessUp);
724 CASE(MonBrightnessDown);
725 CASE(KbdLightOnOff);
726 CASE(KbdBrightnessUp);
727 CASE(KbdBrightnessDown);
728 CASE(Standby);
729 CASE(AudioLowerVolume);
730 CASE(AudioMute);
731 CASE(AudioRaiseVolume);
732 CASE(AudioPlay);
733 CASE(AudioStop);
734 CASE(AudioPrev);
735 CASE(AudioNext);
736 CASE(HomePage);
737 CASE(Mail);
738 CASE(Start);
739 CASE(Search);
740 CASE(AudioRecord);
741 CASE(Calculator);
742 CASE(Memo);
743 CASE(ToDoList);
744 CASE(Calendar);
745 CASE(PowerDown);
746 CASE(ContrastAdjust);
747 CASE(RockerUp);
748 CASE(RockerDown);
749 CASE(RockerEnter);
750 CASE(Back);
751 CASE(Forward);
752 CASE(Stop);
753 CASE(Refresh);
754 CASE(PowerOff);
755 CASE(WakeUp);
756 CASE(Eject);
757 CASE(ScreenSaver);
758 CASE(WWW);
759 CASE(Sleep);
760 CASE(Favorites);
761 CASE(AudioPause);
762 CASE(AudioMedia);
763 CASE(MyComputer);
764 CASE(VendorHome);
765 CASE(LightBulb);
766 CASE(Shop);
767 CASE(History);
768 CASE(OpenURL);
769 CASE(AddFavorite);
770 CASE(HotLinks);
771 CASE(BrightnessAdjust);
772 CASE(Finance);
773 CASE(Community);
774 CASE(AudioRewind);
775 CASE(BackForward);
776 CASE(Launch0);
777 CASE(Launch1);
778 CASE(Launch2);
779 CASE(Launch3);
780 CASE(Launch4);
781 CASE(Launch5);
782 CASE(Launch6);
783 CASE(Launch7);
784 CASE(Launch8);
785 CASE(Launch9);
786 CASE(LaunchA);
787 CASE(LaunchB);
788 CASE(LaunchC);
789 CASE(LaunchD);
790 CASE(LaunchE);
791 CASE(LaunchF);
792 CASE(ApplicationLeft);
793 CASE(ApplicationRight);
794 CASE(Book);
795 CASE(CD);
796 CASE(Calculater);
797 CASE(Clear);
798 CASE(Close);
799 CASE(Copy);
800 CASE(Cut);
801 CASE(Display);
802 CASE(DOS);
803 CASE(Documents);
804 CASE(Excel);
805 CASE(Explorer);
806 CASE(Game);
807 CASE(Go);
808 CASE(iTouch);
809 CASE(LogOff);
810 CASE(Market);
811 CASE(Meeting);
812 CASE(MenuKB);
813 CASE(MenuPB);
814 CASE(MySites);
815 CASE(New);
816 CASE(News);
817 CASE(OfficeHome);
818 CASE(Open);
819 CASE(Option);
820 CASE(Paste);
821 CASE(Phone);
822 CASE(Q);
823 CASE(Reply);
824 CASE(Reload);
825 CASE(RotateWindows);
826 CASE(RotationPB);
827 CASE(RotationKB);
828 CASE(Save);
829 CASE(ScrollUp);
830 CASE(ScrollDown);
831 CASE(ScrollClick);
832 CASE(Send);
833 CASE(Spell);
834 CASE(SplitScreen);
835 CASE(Support);
836 CASE(TaskPane);
837 CASE(Terminal);
838 CASE(Tools);
839 CASE(Travel);
840 CASE(UserPB);
841 CASE(User1KB);
842 CASE(User2KB);
843 CASE(Video);
844 CASE(WheelButton);
845 CASE(Word);
846 CASE(Xfer);
847 CASE(ZoomIn);
848 CASE(ZoomOut);
849 CASE(Away);
850 CASE(Messenger);
851 CASE(WebCam);
852 CASE(MailForward);
853 CASE(Pictures);
854 CASE(Music);
855 CASE(Battery);
856 CASE(Bluetooth);
857 CASE(WLAN);
858 CASE(UWB);
859 CASE(AudioForward);
860 CASE(AudioRepeat);
861 CASE(AudioRandomPlay);
862 CASE(Subtitle);
863 CASE(AudioCycleTrack);
864 CASE(CycleAngle);
865 CASE(FrameBack);
866 CASE(FrameForward);
867 CASE(Time);
868 CASE(Select);
869 CASE(View);
870 CASE(TopMenu);
871 CASE(Red);
872 CASE(Green);
873 CASE(Yellow);
874 CASE(Blue);
875 CASE(Suspend);
876 CASE(Hibernate);
877 CASE(Switch_VT_1);
878 CASE(Switch_VT_2);
879 CASE(Switch_VT_3);
880 CASE(Switch_VT_4);
881 CASE(Switch_VT_5);
882 CASE(Switch_VT_6);
883 CASE(Switch_VT_7);
884 CASE(Switch_VT_8);
885 CASE(Switch_VT_9);
886 CASE(Switch_VT_10);
887 CASE(Switch_VT_11);
888 CASE(Switch_VT_12);
889 CASE(Ungrab);
890 CASE(ClearGrab);
891 CASE(Next_VMode);
892 CASE(Prev_VMode);
893 default:
894 return false;
897 return true;
899 #undef CASE
901 bool
902 key_press_lookup_string(xcb_keysym_t ksym,
903 char *buf, ssize_t buf_len)
905 /* Handle special KeySym (Tab, Newline...) */
906 if((ksym & 0xffffff00) == 0xff00)
907 return keysym_to_str(buf, buf_len, ksym);
908 else if((ksym & 0xfffffe00) == 0xfe00)
909 return keysym_to_xkb(buf, buf_len, ksym);
910 else if((ksym & 0x1008F000) == 0x1008F000)
911 return keysym_to_xf86(buf, buf_len, ksym);
913 /* Handle other KeySym (like unicode...) */
914 return keysym_to_utf8(buf, buf_len, ksym);
917 /** Return the keysym from keycode.
918 * \param detail The keycode received.
919 * \param state The modifier state.
920 * \return A keysym.
922 xcb_keysym_t
923 key_getkeysym(xcb_keycode_t detail, uint16_t state)
925 xcb_keysym_t k0, k1;
927 /* 'col' (third parameter) is used to get the proper KeySym
928 * according to modifier (XCB doesn't provide an equivalent to
929 * XLookupString()).
931 * If Mode_Switch is ON we look into second group.
933 if(state & globalconf.modeswitchmask)
935 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 2);
936 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 3);
938 else
940 k0 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 0);
941 k1 = xcb_key_symbols_get_keysym(globalconf.keysyms, detail, 1);
944 /* If the second column does not exists use the first one. */
945 if(k1 == XCB_NO_SYMBOL)
946 k1 = k0;
948 /* The numlock modifier is on and the second KeySym is a keypad
949 * KeySym */
950 if((state & globalconf.numlockmask) && xcb_is_keypad_key(k1))
952 /* The Shift modifier is on, or if the Lock modifier is on and
953 * is interpreted as ShiftLock, use the first KeySym */
954 if((state & XCB_MOD_MASK_SHIFT)
955 || (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
956 return k0;
957 else
958 return k1;
961 /* The Shift and Lock modifers are both off, use the first
962 * KeySym */
963 else if(!(state & XCB_MOD_MASK_SHIFT) && !(state & XCB_MOD_MASK_LOCK))
964 return k0;
966 /* The Shift modifier is off and the Lock modifier is on and is
967 * interpreted as CapsLock */
968 else if(!(state & XCB_MOD_MASK_SHIFT)
969 && (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
970 /* The first Keysym is used but if that KeySym is lowercase
971 * alphabetic, then the corresponding uppercase KeySym is used
972 * instead */
973 return k1;
975 /* The Shift modifier is on, and the Lock modifier is on and is
976 * interpreted as CapsLock */
977 else if((state & XCB_MOD_MASK_SHIFT)
978 && (state & XCB_MOD_MASK_LOCK && (state & globalconf.capslockmask)))
979 /* The second Keysym is used but if that KeySym is lowercase
980 * alphabetic, then the corresponding uppercase KeySym is used
981 * instead */
982 return k1;
984 /* The Shift modifer is on, or the Lock modifier is on and is
985 * interpreted as ShiftLock, or both */
986 else if((state & XCB_MOD_MASK_SHIFT)
987 || (state & XCB_MOD_MASK_LOCK && (state & globalconf.shiftlockmask)))
988 return k1;
990 return XCB_NO_SYMBOL;
993 static void
994 luaA_keystore(keyb_t *key, const char *str, ssize_t len)
996 if(len)
998 if(*str != '#')
1000 key->keysym = XStringToKeysym(str);
1001 if(!key->keysym)
1003 if(len == 1)
1004 key->keysym = *str;
1005 else
1006 warn("there's no keysym named \"%s\"", str);
1009 else
1010 key->keycode = atoi(str + 1);
1014 /** Define a global key binding. This key binding will always be available.
1015 * \param L The Lua VM state.
1017 * \luastack
1018 * \lparam A table with modifier keys: can be Control or Ctrl, Shift, Lock,
1019 * Mod1, Mod2, Mod3, Mod4, Mod5 or Any.
1020 * \lparam A key name.
1021 * \lparam A function to execute on key press.
1022 * \lparam A function to execute on key release.
1023 * \lreturn The key.
1025 static int
1026 luaA_key_new(lua_State *L)
1028 size_t len;
1029 keyb_t *k;
1030 const char *key;
1031 luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
1033 /* arg 2 is key mod table */
1034 luaA_checktable(L, 2);
1035 /* arg 3 is key */
1036 key = luaL_checklstring(L, 3, &len);
1038 if(!lua_isnil(L, 4))
1039 luaA_registerfct(L, 4, &press);
1041 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1042 luaA_registerfct(L, 5, &release);
1044 k = key_new(L);
1045 luaA_keystore(k, key, len);
1047 k->press = press;
1048 k->release = release;
1050 luaA_setmodifiers(L, 2, &k->mod);
1052 return 1;
1055 /** Set a key array with a Lua table.
1056 * \param L The Lua VM state.
1057 * \param idx The index of the Lua table.
1058 * \param keys The array key to fill.
1060 void
1061 luaA_key_array_set(lua_State *L, int idx, key_array_t *keys)
1063 luaA_checktable(L, idx);
1065 key_array_wipe(keys);
1066 key_array_init(keys);
1068 lua_pushnil(L);
1069 while(lua_next(L, idx))
1070 key_array_append(keys, key_ref(L));
1073 /** Push an array of key as an Lua table onto the stack.
1074 * \param L The Lua VM state.
1075 * \param keys The key array to push.
1076 * \return The number of elements pushed on stack.
1079 luaA_key_array_get(lua_State *L, key_array_t *keys)
1081 lua_createtable(L, keys->len, 0);
1082 for(int i = 0; i < keys->len; i++)
1084 key_push(L, keys->tab[i]);
1085 lua_rawseti(L, -2, i + 1);
1087 return 1;
1090 /** Push a modifier set to a Lua table.
1091 * \param L The Lua VM state.
1092 * \param mod The modifier.
1093 * \return The number of elements pushed on stack.
1096 luaA_pushmodifiers(lua_State *L, uint16_t modifiers)
1098 lua_newtable(L);
1100 int i = 1;
1101 for(uint32_t maski = XCB_MOD_MASK_SHIFT; maski <= XCB_BUTTON_MASK_ANY; maski <<= 1)
1102 if(maski & modifiers)
1104 const char *mod;
1105 size_t slen;
1106 xutil_key_mask_tostr(maski, &mod, &slen);
1107 lua_pushlstring(L, mod, slen);
1108 lua_rawseti(L, -2, i++);
1111 return 1;
1114 /** Take a modifier table from the stack and set modifiers in mod.
1115 * \param L The Lua VM state.
1116 * \param ud The index of the table.
1117 * \param mod Where to set the modifiers.
1119 void
1120 luaA_setmodifiers(lua_State *L, int ud, uint16_t *mod)
1122 luaA_checktable(L, ud);
1123 ssize_t len = lua_objlen(L, ud);
1124 for(int i = 1; i <= len; i++)
1126 lua_rawgeti(L, ud, i);
1127 size_t blen;
1128 const char *key = luaL_checklstring(L, -1, &blen);
1129 *mod |= xutil_key_mask_fromstr(key, blen);
1130 lua_pop(L, 1);
1134 /** Key object.
1135 * \param L The Lua VM state.
1136 * \return The number of elements pushed on stack.
1137 * \luastack
1138 * \lfield key The key to press to triggers an event.
1139 * \lfield keysym Same as key, but return the name of the key symbol. It can
1140 * be identical to key, but for characters like '.' it will return 'period'.
1141 * \lfield modifiers The modifier key that should be pressed while the key is
1142 * pressed. An array with all the modifiers. Valid modifiers are: Any, Mod1,
1143 * Mod2, Mod3, Mod4, Mod5, Shift, Lock and Control.
1144 * \lfield press The function which is called when the key combination is pressed.
1145 * \lfield release The function which is called when the key combination is released.
1147 static int
1148 luaA_key_index(lua_State *L)
1150 size_t len;
1151 keyb_t *k = luaL_checkudata(L, 1, "key");
1152 const char *attr = luaL_checklstring(L, 2, &len);
1154 if(luaA_usemetatable(L, 1, 2))
1155 return 1;
1157 switch(a_tokenize(attr, len))
1159 case A_TK_KEY:
1160 if(k->keycode)
1162 char buf[12];
1163 int slen = snprintf(buf, sizeof(buf), "#%u", k->keycode);
1164 lua_pushlstring(L, buf, slen);
1166 else
1168 char buf[MAX(MB_LEN_MAX, 32)];
1169 if(!key_press_lookup_string(k->keysym, buf, countof(buf)))
1170 return 0;
1172 lua_pushstring(L, buf);
1174 break;
1175 case A_TK_KEYSYM:
1176 if(k->keysym)
1177 lua_pushstring(L, XKeysymToString(k->keysym));
1178 else
1179 return 0;
1180 break;
1181 case A_TK_MODIFIERS:
1182 luaA_pushmodifiers(L, k->mod);
1183 break;
1184 case A_TK_PRESS:
1185 lua_rawgeti(L, LUA_REGISTRYINDEX, k->press);
1186 break;
1187 case A_TK_RELEASE:
1188 lua_rawgeti(L, LUA_REGISTRYINDEX, k->release);
1189 break;
1190 default:
1191 break;
1193 return 1;
1196 /** Key object newindex.
1197 * \param L The Lua VM state.
1198 * \return The number of elements pushed on stack.
1200 static int
1201 luaA_key_newindex(lua_State *L)
1203 size_t len;
1204 keyb_t *k = luaL_checkudata(L, 1, "key");
1205 const char *attr = luaL_checklstring(L, 2, &len);
1207 switch(a_tokenize(attr, len))
1209 case A_TK_KEY:
1211 size_t klen;
1212 const char *key = luaL_checklstring(L, 3, &klen);
1213 luaA_keystore(k, key, klen);
1215 break;
1216 case A_TK_MODIFIERS:
1217 luaA_setmodifiers(L, 3, &k->mod);
1218 break;
1219 case A_TK_PRESS:
1220 luaA_registerfct(L, 3, &k->press);
1221 break;
1222 case A_TK_RELEASE:
1223 luaA_registerfct(L, 3, &k->release);
1224 break;
1225 default:
1226 break;
1228 return 0;
1231 const struct luaL_reg awesome_key_methods[] =
1233 { "__call", luaA_key_new },
1234 { NULL, NULL }
1236 const struct luaL_reg awesome_key_meta[] =
1238 { "__tostring", luaA_key_tostring },
1239 { "__index", luaA_key_index },
1240 { "__newindex", luaA_key_newindex },
1241 { "__gc", luaA_key_gc },
1242 { NULL, NULL },