]> git.sur5r.net Git - i3/i3/blob - i3-config-wizard/xcb.c
Makefile: properly make 'clean'
[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 extern xcb_window_t root;
23
24 /*
25  * Convenience-wrapper around xcb_change_gc which saves us declaring a variable
26  *
27  */
28 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
29         xcb_change_gc(conn, gc, mask, &value);
30 }
31
32 /*
33  * Returns the colorpixel to use for the given hex color (think of HTML).
34  *
35  * The hex_color has to start with #, for example #FF00FF.
36  *
37  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
38  * This has to be done by the caller.
39  *
40  */
41 uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) {
42         char strgroups[3][3] = {{hex[1], hex[2], '\0'},
43                                 {hex[3], hex[4], '\0'},
44                                 {hex[5], hex[6], '\0'}};
45         uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
46                              (strtol(strgroups[1], NULL, 16)),
47                              (strtol(strgroups[2], NULL, 16))};
48
49         return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
50 }
51
52 /*
53  * Returns the mask for Mode_switch (to be used for looking up keysymbols by
54  * keycode).
55  *
56  */
57 uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode) {
58         xcb_key_symbols_t *symbols = xcb_key_symbols_alloc(conn);
59
60         xcb_get_modifier_mapping_reply_t *modmap_r;
61         xcb_keycode_t *modmap, kc;
62         xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
63         if (modeswitchcodes == NULL)
64                 return 0;
65
66         modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
67         modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
68
69         for (int i = 0; i < 8; i++)
70                 for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
71                         kc = modmap[i * modmap_r->keycodes_per_modifier + j];
72                         for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
73                                 if (*ktest != kc)
74                                         continue;
75
76                                 free(modeswitchcodes);
77                                 free(modmap_r);
78                                 return (1 << i);
79                         }
80                 }
81
82         return 0;
83 }
84
85 /*
86  * Opens the window we use for input/output and maps it
87  *
88  */
89 xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height) {
90         xcb_window_t win = xcb_generate_id(conn);
91         //xcb_cursor_t cursor_id = xcb_generate_id(conn);
92
93 #if 0
94         /* Use the default cursor (left pointer) */
95         if (cursor > -1) {
96                 i3Font *cursor_font = load_font(conn, "cursor");
97                 xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
98                                 XCB_CURSOR_LEFT_PTR, XCB_CURSOR_LEFT_PTR + 1,
99                                 0, 0, 0, 65535, 65535, 65535);
100         }
101 #endif
102
103         uint32_t mask = 0;
104         uint32_t values[3];
105
106         mask |= XCB_CW_BACK_PIXEL;
107         values[0] = 0;
108
109         mask |= XCB_CW_OVERRIDE_REDIRECT;
110         values[1] = 1;
111
112         mask |= XCB_CW_EVENT_MASK;
113         values[2] = XCB_EVENT_MASK_EXPOSURE;
114
115         xcb_create_window(conn,
116                           XCB_COPY_FROM_PARENT,
117                           win, /* the window id */
118                           root, /* parent == root */
119                           490, 297, width, height, /* dimensions */
120                           0, /* border = 0, we draw our own */
121                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
122                           XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
123                           mask,
124                           values);
125
126 #if 0
127         if (cursor > -1)
128                 xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
129 #endif
130
131         /* Map the window (= make it visible) */
132         xcb_map_window(conn, win);
133
134         return win;
135 }
136
137 /*
138  * Returns the ID of the font matching the given pattern and stores the height
139  * of the font (in pixels) in *font_height. die()s if no font matches.
140  *
141  */
142 int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height) {
143         xcb_void_cookie_t font_cookie;
144         xcb_list_fonts_with_info_cookie_t info_cookie;
145
146         /* Send all our requests first */
147         int result;
148         result = xcb_generate_id(conn);
149         font_cookie = xcb_open_font_checked(conn, result, strlen(pattern), pattern);
150         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
151
152         xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
153         if (error != NULL) {
154                 fprintf(stderr, "ERROR: Could not open font: %d\n", error->error_code);
155                 exit(1);
156         }
157
158         /* Get information (height/name) for this font */
159         xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
160         if (reply == NULL)
161                 errx(1, "Could not load font \"%s\"\n", pattern);
162
163         *font_height = reply->font_ascent + reply->font_descent;
164
165         return result;
166 }