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"
32 #include "common/atoms.h"
33 #include "common/tokenize.h"
35 /** Get the string value of an atom.
36 * \param conn X connection.
38 * \param atom The atom.
39 * \param text Buffer to fill.
40 * \param len Length of the filled buffer.
41 * \return True on sucess, false on failure.
44 xutil_text_prop_get(xcb_connection_t
*conn
, xcb_window_t w
, xcb_atom_t atom
,
45 char **text
, ssize_t
*len
)
47 xcb_get_text_property_reply_t reply
;
51 if(!xcb_get_text_property_reply(conn
,
52 xcb_get_text_property_unchecked(conn
, w
,
55 !reply
.name_len
|| reply
.format
!= 8)
57 xcb_get_text_property_reply_wipe(&reply
);
63 /* Check whether the returned property value is just an ascii
64 * string, an UTF-8 string or just some random multibyte in any other
66 if(reply
.encoding
== STRING
67 || reply
.encoding
== UTF8_STRING
68 || reply
.encoding
== COMPOUND_TEXT
)
70 *text
= p_new(char, reply
.name_len
+ 1);
71 /* Use memcpy() because the property name is not be \0
73 memcpy(*text
, reply
.name
, reply
.name_len
);
74 (*text
)[reply
.name_len
] = '\0';
75 *len
= reply
.name_len
;
84 xcb_get_text_property_reply_wipe(&reply
);
88 /** Get the lock masks (shiftlock, numlock, capslock, modeswitch).
89 * \param connection The X connection.
90 * \param cookie The cookie of the request.
91 * \param keysyms Key symbols.
92 * \param numlockmask Numlock mask.
93 * \param shiftlockmask Shiftlock mask.
94 * \param capslockmask Capslock mask.
98 xutil_lock_mask_get(xcb_connection_t
*connection
,
99 xcb_get_modifier_mapping_cookie_t cookie
,
100 xcb_key_symbols_t
*keysyms
,
101 uint16_t *numlockmask
,
102 uint16_t *shiftlockmask
,
103 uint16_t *capslockmask
,
104 uint16_t *modeswitchmask
)
106 xcb_get_modifier_mapping_reply_t
*modmap_r
;
107 xcb_keycode_t
*modmap
, kc
;
108 xcb_keycode_t
*numlockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Num_Lock
);
109 xcb_keycode_t
*shiftlockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Shift_Lock
);
110 xcb_keycode_t
*capslockcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Caps_Lock
);
111 xcb_keycode_t
*modeswitchcodes
= xcb_key_symbols_get_keycode(keysyms
, XK_Mode_switch
);
113 modmap_r
= xcb_get_modifier_mapping_reply(connection
, cookie
, NULL
);
114 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
117 *numlockmask
= *shiftlockmask
= *capslockmask
= *modeswitchmask
= 0;
120 for(i
= 0; i
< 8; i
++)
121 for(int j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++)
123 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
+ j
];
125 #define LOOK_FOR(mask, codes) \
126 if(*mask == 0 && codes) \
127 for(xcb_keycode_t *ktest = codes; *ktest; ktest++) \
134 LOOK_FOR(numlockmask
, numlockcodes
)
135 LOOK_FOR(shiftlockmask
, shiftlockcodes
)
136 LOOK_FOR(capslockmask
, capslockcodes
)
137 LOOK_FOR(modeswitchmask
, modeswitchcodes
)
140 p_delete(&numlockcodes
);
141 p_delete(&shiftlockcodes
);
142 p_delete(&capslockcodes
);
143 p_delete(&modeswitchcodes
);
147 /* Number of different errors */
148 #define ERRORS_NBR 256
151 xutil_error_handler_catch_all_set(xcb_event_handlers_t
*evenths
,
152 xcb_generic_error_handler_t handler
,
156 for(err_num
= 0; err_num
< ERRORS_NBR
; err_num
++)
157 xcb_event_set_error_handler(evenths
, err_num
, handler
, data
);
161 xutil_key_mask_fromstr(const char *keyname
, size_t len
)
163 switch(a_tokenize(keyname
, len
))
165 case A_TK_SHIFT
: return XCB_MOD_MASK_SHIFT
;
166 case A_TK_LOCK
: return XCB_MOD_MASK_LOCK
;
168 case A_TK_CONTROL
: return XCB_MOD_MASK_CONTROL
;
169 case A_TK_MOD1
: return XCB_MOD_MASK_1
;
170 case A_TK_MOD2
: return XCB_MOD_MASK_2
;
171 case A_TK_MOD3
: return XCB_MOD_MASK_3
;
172 case A_TK_MOD4
: return XCB_MOD_MASK_4
;
173 case A_TK_MOD5
: return XCB_MOD_MASK_5
;
174 /* this is misnamed but correct */
175 case A_TK_ANY
: return XCB_BUTTON_MASK_ANY
;
176 default: return XCB_NO_SYMBOL
;
181 xutil_key_mask_tostr(uint16_t mask
, const char **name
, size_t *len
)
185 #define SET_RESULT(res) \
187 *len = sizeof(#res) - 1; \
189 case XCB_MOD_MASK_SHIFT
: SET_RESULT(Shift
)
190 case XCB_MOD_MASK_LOCK
: SET_RESULT(Lock
)
191 case XCB_MOD_MASK_CONTROL
: SET_RESULT(Control
)
192 case XCB_MOD_MASK_1
: SET_RESULT(Mod1
)
193 case XCB_MOD_MASK_2
: SET_RESULT(Mod2
)
194 case XCB_MOD_MASK_3
: SET_RESULT(Mod3
)
195 case XCB_MOD_MASK_4
: SET_RESULT(Mod4
)
196 case XCB_MOD_MASK_5
: SET_RESULT(Mod5
)
197 case XCB_BUTTON_MASK_ANY
: SET_RESULT(Any
)
198 default: SET_RESULT(Unknown
)
203 /** Convert a root window a physical screen ID.
204 * \param connection The connection to the X server.
205 * \param root Root window.
206 * \return A physical screen number.
209 xutil_root2screen(xcb_connection_t
*connection
, xcb_window_t root
)
211 xcb_screen_iterator_t iter
;
214 for(iter
= xcb_setup_roots_iterator(xcb_get_setup(connection
));
215 iter
.rem
&& iter
.data
->root
!= root
; xcb_screen_next(&iter
), ++phys_screen
);
220 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80