]> git.sur5r.net Git - i3/i3/blob - i3-config-wizard/xcb.c
i3-config-wizard: use IPC functions from libi3
[i3/i3] / i3-config-wizard / xcb.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <err.h>
16
17 #include <xcb/xcb.h>
18 #include <xcb/xcb_keysyms.h>
19
20 #include <X11/keysym.h>
21
22 #include "xcb.h"
23
24 extern xcb_window_t root;
25 unsigned int xcb_numlock_mask;
26
27 /*
28  * Convenience-wrapper around xcb_change_gc which saves us declaring a variable
29  *
30  */
31 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
32         xcb_change_gc(conn, gc, mask, &value);
33 }
34
35 /*
36  * Returns the colorpixel to use for the given hex color (think of HTML).
37  *
38  * The hex_color has to start with #, for example #FF00FF.
39  *
40  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
41  * This has to be done by the caller.
42  *
43  */
44 uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) {
45         char strgroups[3][3] = {{hex[1], hex[2], '\0'},
46                                 {hex[3], hex[4], '\0'},
47                                 {hex[5], hex[6], '\0'}};
48         uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
49                              (strtol(strgroups[1], NULL, 16)),
50                              (strtol(strgroups[2], NULL, 16))};
51
52         return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
53 }
54
55 /*
56  * Returns the mask for Mode_switch (to be used for looking up keysymbols by
57  * keycode).
58  *
59  */
60 uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode) {
61         xcb_key_symbols_t *symbols = xcb_key_symbols_alloc(conn);
62
63         xcb_get_modifier_mapping_reply_t *modmap_r;
64         xcb_keycode_t *modmap, kc;
65         xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
66         if (modeswitchcodes == NULL)
67                 return 0;
68
69         modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
70         modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
71
72         for (int i = 0; i < 8; i++)
73                 for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
74                         kc = modmap[i * modmap_r->keycodes_per_modifier + j];
75                         for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
76                                 if (*ktest != kc)
77                                         continue;
78
79                                 free(modeswitchcodes);
80                                 free(modmap_r);
81                                 return (1 << i);
82                         }
83                 }
84
85         return 0;
86 }
87
88 /*
89  * Opens the window we use for input/output and maps it
90  *
91  */
92 xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height) {
93         xcb_window_t win = xcb_generate_id(conn);
94         //xcb_cursor_t cursor_id = xcb_generate_id(conn);
95
96 #if 0
97         /* Use the default cursor (left pointer) */
98         if (cursor > -1) {
99                 i3Font *cursor_font = load_font(conn, "cursor");
100                 xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
101                                 XCB_CURSOR_LEFT_PTR, XCB_CURSOR_LEFT_PTR + 1,
102                                 0, 0, 0, 65535, 65535, 65535);
103         }
104 #endif
105
106         uint32_t mask = 0;
107         uint32_t values[3];
108
109         mask |= XCB_CW_BACK_PIXEL;
110         values[0] = 0;
111
112         mask |= XCB_CW_EVENT_MASK;
113         values[1] = XCB_EVENT_MASK_EXPOSURE |
114                     XCB_EVENT_MASK_BUTTON_PRESS;
115
116         xcb_create_window(conn,
117                           XCB_COPY_FROM_PARENT,
118                           win, /* the window id */
119                           root, /* parent == root */
120                           490, 297, width, height, /* dimensions */
121                           0, /* border = 0, we draw our own */
122                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
123                           XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
124                           mask,
125                           values);
126
127 #if 0
128         if (cursor > -1)
129                 xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
130 #endif
131
132         /* Map the window (= make it visible) */
133         xcb_map_window(conn, win);
134
135         return win;
136 }
137
138 /*
139  * Returns the ID of the font matching the given pattern and stores the height
140  * of the font (in pixels) in *font_height. die()s if no font matches.
141  *
142  */
143 int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height) {
144         xcb_void_cookie_t font_cookie;
145         xcb_list_fonts_with_info_cookie_t info_cookie;
146
147         /* Send all our requests first */
148         int result;
149         result = xcb_generate_id(conn);
150         font_cookie = xcb_open_font_checked(conn, result, strlen(pattern), pattern);
151         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
152
153         xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
154         if (error != NULL) {
155                 fprintf(stderr, "ERROR: Could not open font: %d\n", error->error_code);
156                 exit(1);
157         }
158
159         /* Get information (height/name) for this font */
160         xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
161         if (reply == NULL)
162                 errx(1, "Could not load font \"%s\"\n", pattern);
163
164         *font_height = reply->font_ascent + reply->font_descent;
165
166         return result;
167 }
168
169 /*
170  * Finds out which modifier mask is the one for numlock, as the user may change this.
171  *
172  */
173 void xcb_get_numlock_mask(xcb_connection_t *conn) {
174     xcb_key_symbols_t *keysyms;
175     xcb_get_modifier_mapping_cookie_t cookie;
176     xcb_get_modifier_mapping_reply_t *reply;
177     xcb_keycode_t *modmap;
178     int mask, i;
179     const int masks[8] = { XCB_MOD_MASK_SHIFT,
180                            XCB_MOD_MASK_LOCK,
181                            XCB_MOD_MASK_CONTROL,
182                            XCB_MOD_MASK_1,
183                            XCB_MOD_MASK_2,
184                            XCB_MOD_MASK_3,
185                            XCB_MOD_MASK_4,
186                            XCB_MOD_MASK_5 };
187
188     /* Request the modifier map */
189     cookie = xcb_get_modifier_mapping_unchecked(conn);
190
191     /* Get the keysymbols */
192     keysyms = xcb_key_symbols_alloc(conn);
193
194     if ((reply = xcb_get_modifier_mapping_reply(conn, cookie, NULL)) == NULL) {
195         xcb_key_symbols_free(keysyms);
196         return;
197     }
198
199     modmap = xcb_get_modifier_mapping_keycodes(reply);
200
201     /* Get the keycode for numlock */
202 #ifdef OLD_XCB_KEYSYMS_API
203     xcb_keycode_t numlock = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
204 #else
205     /* For now, we only use the first keysymbol. */
206     xcb_keycode_t *numlock_syms = xcb_key_symbols_get_keycode(keysyms, XCB_NUM_LOCK);
207     if (numlock_syms == NULL)
208         return;
209     xcb_keycode_t numlock = *numlock_syms;
210     free(numlock_syms);
211 #endif
212
213     /* Check all modifiers (Mod1-Mod5, Shift, Control, Lock) */
214     for (mask = 0; mask < 8; mask++)
215         for (i = 0; i < reply->keycodes_per_modifier; i++)
216             if (modmap[(mask * reply->keycodes_per_modifier) + i] == numlock)
217                 xcb_numlock_mask = masks[mask];
218
219     xcb_key_symbols_free(keysyms);
220     free(reply);
221 }
222