Partly revert cd53eb8d3f131360dc9bef4a0fd1ffffc0fbafb9
[awesome.git] / common / xutil.c
blob3d80e8d6cc56a36389b73d017a616b39cc7bb6f9
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 /* XCB doesn't provide keysyms definition */
23 #include <X11/keysym.h>
25 #include "common/util.h"
27 #include <xcb/xcb.h>
28 #include <xcb/xcb_atom.h>
29 #include <xcb/xcb_icccm.h>
31 #include "common/xutil.h"
32 #include "common/tokenize.h"
34 /** Get the lock masks (shiftlock, numlock, capslock, modeswitch).
35 * \param connection The X connection.
36 * \param cookie The cookie of the request.
37 * \param keysyms Key symbols.
38 * \param numlockmask Numlock mask.
39 * \param shiftlockmask Shiftlock mask.
40 * \param capslockmask Capslock mask.
41 * \todo Split this.
43 void
44 xutil_lock_mask_get(xcb_connection_t *connection,
45 xcb_get_modifier_mapping_cookie_t cookie,
46 xcb_key_symbols_t *keysyms,
47 uint16_t *numlockmask,
48 uint16_t *shiftlockmask,
49 uint16_t *capslockmask,
50 uint16_t *modeswitchmask)
52 xcb_get_modifier_mapping_reply_t *modmap_r;
53 xcb_keycode_t *modmap, kc;
54 xcb_keycode_t *numlockcodes = xcb_key_symbols_get_keycode(keysyms, XK_Num_Lock);
55 xcb_keycode_t *shiftlockcodes = xcb_key_symbols_get_keycode(keysyms, XK_Shift_Lock);
56 xcb_keycode_t *capslockcodes = xcb_key_symbols_get_keycode(keysyms, XK_Caps_Lock);
57 xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(keysyms, XK_Mode_switch);
59 modmap_r = xcb_get_modifier_mapping_reply(connection, cookie, NULL);
60 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
62 /* reset */
63 *numlockmask = *shiftlockmask = *capslockmask = *modeswitchmask = 0;
65 int i;
66 for(i = 0; i < 8; i++)
67 for(int j = 0; j < modmap_r->keycodes_per_modifier; j++)
69 kc = modmap[i * modmap_r->keycodes_per_modifier + j];
71 #define LOOK_FOR(mask, codes) \
72 if(*mask == 0 && codes) \
73 for(xcb_keycode_t *ktest = codes; *ktest; ktest++) \
74 if(*ktest == kc) \
75 { \
76 *mask = (1 << i); \
77 break; \
80 LOOK_FOR(numlockmask, numlockcodes)
81 LOOK_FOR(shiftlockmask, shiftlockcodes)
82 LOOK_FOR(capslockmask, capslockcodes)
83 LOOK_FOR(modeswitchmask, modeswitchcodes)
84 #undef LOOK_FOR
86 p_delete(&numlockcodes);
87 p_delete(&shiftlockcodes);
88 p_delete(&capslockcodes);
89 p_delete(&modeswitchcodes);
90 p_delete(&modmap_r);
93 /* Number of different errors */
94 #define ERRORS_NBR 256
96 uint16_t
97 xutil_key_mask_fromstr(const char *keyname, size_t len)
99 switch(a_tokenize(keyname, len))
101 case A_TK_SHIFT: return XCB_MOD_MASK_SHIFT;
102 case A_TK_LOCK: return XCB_MOD_MASK_LOCK;
103 case A_TK_CTRL:
104 case A_TK_CONTROL: return XCB_MOD_MASK_CONTROL;
105 case A_TK_MOD1: return XCB_MOD_MASK_1;
106 case A_TK_MOD2: return XCB_MOD_MASK_2;
107 case A_TK_MOD3: return XCB_MOD_MASK_3;
108 case A_TK_MOD4: return XCB_MOD_MASK_4;
109 case A_TK_MOD5: return XCB_MOD_MASK_5;
110 /* this is misnamed but correct */
111 case A_TK_ANY: return XCB_BUTTON_MASK_ANY;
112 default: return XCB_NO_SYMBOL;
116 void
117 xutil_key_mask_tostr(uint16_t mask, const char **name, size_t *len)
119 switch(mask)
121 #define SET_RESULT(res) \
122 *name = #res; \
123 *len = sizeof(#res) - 1; \
124 return;
125 case XCB_MOD_MASK_SHIFT: SET_RESULT(Shift)
126 case XCB_MOD_MASK_LOCK: SET_RESULT(Lock)
127 case XCB_MOD_MASK_CONTROL: SET_RESULT(Control)
128 case XCB_MOD_MASK_1: SET_RESULT(Mod1)
129 case XCB_MOD_MASK_2: SET_RESULT(Mod2)
130 case XCB_MOD_MASK_3: SET_RESULT(Mod3)
131 case XCB_MOD_MASK_4: SET_RESULT(Mod4)
132 case XCB_MOD_MASK_5: SET_RESULT(Mod5)
133 case XCB_BUTTON_MASK_ANY: SET_RESULT(Any)
134 default: SET_RESULT(Unknown)
135 #undef SET_RESULT
139 /** Convert a root window a physical screen ID.
140 * \param connection The connection to the X server.
141 * \param root Root window.
142 * \return A physical screen number.
145 xutil_root2screen(xcb_connection_t *connection, xcb_window_t root)
147 xcb_screen_iterator_t iter;
148 int phys_screen = 0;
150 for(iter = xcb_setup_roots_iterator(xcb_get_setup(connection));
151 iter.rem && iter.data->root != root; xcb_screen_next(&iter), ++phys_screen);
153 return phys_screen;
156 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80