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"
28 #include <xcb/xcb_atom.h>
29 #include <xcb/xcb_icccm.h>
31 #include "common/xutil.h"
33 /** Get the lock masks (shiftlock, numlock, capslock, modeswitch).
34 * \param connection The X connection.
35 * \param cookie The cookie of the request.
36 * \param keysyms Key symbols.
37 * \param numlockmask Numlock mask.
38 * \param shiftlockmask Shiftlock mask.
39 * \param capslockmask Capslock mask.
43 xutil_lock_mask_get(xcb_connection_t
*connection
,
44 xcb_get_modifier_mapping_cookie_t cookie
,
45 xcb_key_symbols_t
*keysyms
,
46 uint16_t *numlockmask
,
47 uint16_t *shiftlockmask
,
48 uint16_t *capslockmask
,
49 uint16_t *modeswitchmask
)
51 xcb_get_modifier_mapping_reply_t
*modmap_r
;
52 xcb_keycode_t
*modmap
, kc
;
53 xcb_keycode_t
*numlockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Num_Lock
);
54 xcb_keycode_t
*shiftlockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Shift_Lock
);
55 xcb_keycode_t
*capslockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Caps_Lock
);
56 xcb_keycode_t
*modeswitchcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Mode_switch
);
58 modmap_r
= xcb_get_modifier_mapping_reply(connection
, cookie
, NULL
);
59 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
62 *numlockmask
= *shiftlockmask
= *capslockmask
= *modeswitchmask
= 0;
65 for(i
= 0; i
< 8; i
++)
66 for(int j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++)
68 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
+ j
];
70 #define LOOK_FOR(mask, codes) \
71 if(*mask == 0 && codes) \
72 for(xcb_keycode_t *ktest = codes; *ktest; ktest++) \
79 LOOK_FOR(numlockmask
, numlockcodes
)
80 LOOK_FOR(shiftlockmask
, shiftlockcodes
)
81 LOOK_FOR(capslockmask
, capslockcodes
)
82 LOOK_FOR(modeswitchmask
, modeswitchcodes
)
85 p_delete(&numlockcodes
);
86 p_delete(&shiftlockcodes
);
87 p_delete(&capslockcodes
);
88 p_delete(&modeswitchcodes
);
93 xutil_key_mask_fromstr(const char *keyname
)
95 if(a_strcmp(keyname
, "Shift") == 0)
96 return XCB_MOD_MASK_SHIFT
;
97 if(a_strcmp(keyname
, "Lock") == 0)
98 return XCB_MOD_MASK_LOCK
;
99 if(a_strcmp(keyname
, "Ctrl") == 0 || a_strcmp(keyname
, "Control") == 0)
100 return XCB_MOD_MASK_CONTROL
;
101 if(a_strcmp(keyname
, "Mod1") == 0)
102 return XCB_MOD_MASK_1
;
103 if(a_strcmp(keyname
, "Mod2") == 0)
104 return XCB_MOD_MASK_2
;
105 if(a_strcmp(keyname
, "Mod3") == 0)
106 return XCB_MOD_MASK_3
;
107 if(a_strcmp(keyname
, "Mod4") == 0)
108 return XCB_MOD_MASK_4
;
109 if(a_strcmp(keyname
, "Mod5") == 0)
110 return XCB_MOD_MASK_5
;
111 if(a_strcmp(keyname
, "Any") == 0)
112 /* this is misnamed but correct */
113 return XCB_BUTTON_MASK_ANY
;
114 return XCB_NO_SYMBOL
;
118 xutil_key_mask_tostr(uint16_t mask
, const char **name
, size_t *len
)
122 #define SET_RESULT(res) \
124 *len = sizeof(#res) - 1; \
126 case XCB_MOD_MASK_SHIFT
: SET_RESULT(Shift
)
127 case XCB_MOD_MASK_LOCK
: SET_RESULT(Lock
)
128 case XCB_MOD_MASK_CONTROL
: SET_RESULT(Control
)
129 case XCB_MOD_MASK_1
: SET_RESULT(Mod1
)
130 case XCB_MOD_MASK_2
: SET_RESULT(Mod2
)
131 case XCB_MOD_MASK_3
: SET_RESULT(Mod3
)
132 case XCB_MOD_MASK_4
: SET_RESULT(Mod4
)
133 case XCB_MOD_MASK_5
: SET_RESULT(Mod5
)
134 case XCB_BUTTON_MASK_ANY
: SET_RESULT(Any
)
135 default: SET_RESULT(Unknown
)
140 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80