Make callbacks to rules.execute optional
[awesome.git] / common / xutil.c
blob8e96b2462aa0ddbe5336f868ce2d1ea60a8831bb
1 /*
2 * common/xutil.c - X-related useful functions
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
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 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "common/xutil.h"
24 /* XCB doesn't provide keysyms definition */
25 #include <X11/keysym.h>
27 #include <xcb/xcb.h>
28 #include <xcb/xcb_icccm.h>
30 /** Get the lock masks (shiftlock, numlock, capslock, modeswitch).
31 * \param connection The X connection.
32 * \param cookie The cookie of the request.
33 * \param keysyms Key symbols.
34 * \param numlockmask Numlock mask.
35 * \param shiftlockmask Shiftlock mask.
36 * \param capslockmask Capslock mask.
37 * \todo Split this.
39 void
40 xutil_lock_mask_get(xcb_connection_t *connection,
41 xcb_get_modifier_mapping_cookie_t cookie,
42 xcb_key_symbols_t *keysyms,
43 uint16_t *numlockmask,
44 uint16_t *shiftlockmask,
45 uint16_t *capslockmask,
46 uint16_t *modeswitchmask)
48 xcb_get_modifier_mapping_reply_t *modmap_r;
49 xcb_keycode_t *modmap, kc;
50 xcb_keycode_t *numlockcodes = xcb_key_symbols_get_keycode(keysyms, XK_Num_Lock);
51 xcb_keycode_t *shiftlockcodes = xcb_key_symbols_get_keycode(keysyms, XK_Shift_Lock);
52 xcb_keycode_t *capslockcodes = xcb_key_symbols_get_keycode(keysyms, XK_Caps_Lock);
53 xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(keysyms, XK_Mode_switch);
55 modmap_r = xcb_get_modifier_mapping_reply(connection, cookie, NULL);
56 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
58 /* reset */
59 *numlockmask = *shiftlockmask = *capslockmask = *modeswitchmask = 0;
61 int i;
62 for(i = 0; i < 8; i++)
63 for(int j = 0; j < modmap_r->keycodes_per_modifier; j++)
65 kc = modmap[i * modmap_r->keycodes_per_modifier + j];
67 #define LOOK_FOR(mask, codes) \
68 if(*mask == 0 && codes) \
69 for(xcb_keycode_t *ktest = codes; *ktest; ktest++) \
70 if(*ktest == kc) \
71 { \
72 *mask = (1 << i); \
73 break; \
76 LOOK_FOR(numlockmask, numlockcodes)
77 LOOK_FOR(shiftlockmask, shiftlockcodes)
78 LOOK_FOR(capslockmask, capslockcodes)
79 LOOK_FOR(modeswitchmask, modeswitchcodes)
80 #undef LOOK_FOR
82 p_delete(&numlockcodes);
83 p_delete(&shiftlockcodes);
84 p_delete(&capslockcodes);
85 p_delete(&modeswitchcodes);
86 p_delete(&modmap_r);
89 uint16_t
90 xutil_key_mask_fromstr(const char *keyname)
92 if (A_STREQ(keyname, "Shift"))
93 return XCB_MOD_MASK_SHIFT;
94 if (A_STREQ(keyname, "Lock"))
95 return XCB_MOD_MASK_LOCK;
96 if (A_STREQ(keyname, "Ctrl") || A_STREQ(keyname, "Control"))
97 return XCB_MOD_MASK_CONTROL;
98 if (A_STREQ(keyname, "Mod1"))
99 return XCB_MOD_MASK_1;
100 if(A_STREQ(keyname, "Mod2"))
101 return XCB_MOD_MASK_2;
102 if(A_STREQ(keyname, "Mod3"))
103 return XCB_MOD_MASK_3;
104 if(A_STREQ(keyname, "Mod4"))
105 return XCB_MOD_MASK_4;
106 if(A_STREQ(keyname, "Mod5"))
107 return XCB_MOD_MASK_5;
108 if(A_STREQ(keyname, "Any"))
109 /* this is misnamed but correct */
110 return XCB_BUTTON_MASK_ANY;
111 return XCB_NO_SYMBOL;
114 void
115 xutil_key_mask_tostr(uint16_t mask, const char **name, size_t *len)
117 switch(mask)
119 #define SET_RESULT(res) \
120 *name = #res; \
121 *len = sizeof(#res) - 1; \
122 return;
123 case XCB_MOD_MASK_SHIFT: SET_RESULT(Shift)
124 case XCB_MOD_MASK_LOCK: SET_RESULT(Lock)
125 case XCB_MOD_MASK_CONTROL: SET_RESULT(Control)
126 case XCB_MOD_MASK_1: SET_RESULT(Mod1)
127 case XCB_MOD_MASK_2: SET_RESULT(Mod2)
128 case XCB_MOD_MASK_3: SET_RESULT(Mod3)
129 case XCB_MOD_MASK_4: SET_RESULT(Mod4)
130 case XCB_MOD_MASK_5: SET_RESULT(Mod5)
131 case XCB_BUTTON_MASK_ANY: SET_RESULT(Any)
132 default: SET_RESULT(Unknown)
133 #undef SET_RESULT
137 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80