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_icccm.h>
30 #include "common/xutil.h"
32 /** Get the lock masks (shiftlock, numlock, capslock, modeswitch).
33 * \param connection The X connection.
34 * \param cookie The cookie of the request.
35 * \param keysyms Key symbols.
36 * \param numlockmask Numlock mask.
37 * \param shiftlockmask Shiftlock mask.
38 * \param capslockmask Capslock mask.
42 xutil_lock_mask_get(xcb_connection_t
*connection
,
43 xcb_get_modifier_mapping_cookie_t cookie
,
44 xcb_key_symbols_t
*keysyms
,
45 uint16_t *numlockmask
,
46 uint16_t *shiftlockmask
,
47 uint16_t *capslockmask
,
48 uint16_t *modeswitchmask
)
50 xcb_get_modifier_mapping_reply_t
*modmap_r
;
51 xcb_keycode_t
*modmap
, kc
;
52 xcb_keycode_t
*numlockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Num_Lock
);
53 xcb_keycode_t
*shiftlockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Shift_Lock
);
54 xcb_keycode_t
*capslockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Caps_Lock
);
55 xcb_keycode_t
*modeswitchcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Mode_switch
);
57 modmap_r
= xcb_get_modifier_mapping_reply(connection
, cookie
, NULL
);
58 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
61 *numlockmask
= *shiftlockmask
= *capslockmask
= *modeswitchmask
= 0;
64 for(i
= 0; i
< 8; i
++)
65 for(int j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++)
67 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
+ j
];
69 #define LOOK_FOR(mask, codes) \
70 if(*mask == 0 && codes) \
71 for(xcb_keycode_t *ktest = codes; *ktest; ktest++) \
78 LOOK_FOR(numlockmask
, numlockcodes
)
79 LOOK_FOR(shiftlockmask
, shiftlockcodes
)
80 LOOK_FOR(capslockmask
, capslockcodes
)
81 LOOK_FOR(modeswitchmask
, modeswitchcodes
)
84 p_delete(&numlockcodes
);
85 p_delete(&shiftlockcodes
);
86 p_delete(&capslockcodes
);
87 p_delete(&modeswitchcodes
);
92 xutil_key_mask_fromstr(const char *keyname
)
94 if (A_STREQ(keyname
, "Shift"))
95 return XCB_MOD_MASK_SHIFT
;
96 if (A_STREQ(keyname
, "Lock"))
97 return XCB_MOD_MASK_LOCK
;
98 if (A_STREQ(keyname
, "Ctrl") || A_STREQ(keyname
, "Control"))
99 return XCB_MOD_MASK_CONTROL
;
100 if (A_STREQ(keyname
, "Mod1"))
101 return XCB_MOD_MASK_1
;
102 if(A_STREQ(keyname
, "Mod2"))
103 return XCB_MOD_MASK_2
;
104 if(A_STREQ(keyname
, "Mod3"))
105 return XCB_MOD_MASK_3
;
106 if(A_STREQ(keyname
, "Mod4"))
107 return XCB_MOD_MASK_4
;
108 if(A_STREQ(keyname
, "Mod5"))
109 return XCB_MOD_MASK_5
;
110 if(A_STREQ(keyname
, "Any"))
111 /* this is misnamed but correct */
112 return XCB_BUTTON_MASK_ANY
;
113 return XCB_NO_SYMBOL
;
117 xutil_key_mask_tostr(uint16_t mask
, const char **name
, size_t *len
)
121 #define SET_RESULT(res) \
123 *len = sizeof(#res) - 1; \
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
)
139 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80