]> git.sur5r.net Git - i3/i3/blob - i3-input/xcb.c
i3-input: Bugfix: font must be dynamically allocated, we FREE it later
[i3/i3] / i3-input / 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
16 #include <xcb/xcb.h>
17 #include <xcb/xcb_keysyms.h>
18
19 #include <X11/keysym.h>
20
21 #include "i3-input.h"
22
23 /*
24  * Returns the mask for Mode_switch (to be used for looking up keysymbols by
25  * keycode).
26  *
27  */
28 uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode) {
29         xcb_key_symbols_t *symbols = xcb_key_symbols_alloc(conn);
30
31         xcb_get_modifier_mapping_reply_t *modmap_r;
32         xcb_keycode_t *modmap, kc;
33         xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode);
34         if (modeswitchcodes == NULL)
35                 return 0;
36
37         modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL);
38         modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
39
40         for (int i = 0; i < 8; i++)
41                 for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) {
42                         kc = modmap[i * modmap_r->keycodes_per_modifier + j];
43                         for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) {
44                                 if (*ktest != kc)
45                                         continue;
46
47                                 free(modeswitchcodes);
48                                 free(modmap_r);
49                                 return (1 << i);
50                         }
51                 }
52
53         return 0;
54 }
55
56 /*
57  * Opens the window we use for input/output and maps it
58  *
59  */
60 xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height) {
61         xcb_window_t win = xcb_generate_id(conn);
62         //xcb_cursor_t cursor_id = xcb_generate_id(conn);
63
64 #if 0
65         /* Use the default cursor (left pointer) */
66         if (cursor > -1) {
67                 i3Font *cursor_font = load_font(conn, "cursor");
68                 xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
69                                 XCB_CURSOR_LEFT_PTR, XCB_CURSOR_LEFT_PTR + 1,
70                                 0, 0, 0, 65535, 65535, 65535);
71         }
72 #endif
73
74         uint32_t mask = 0;
75         uint32_t values[3];
76
77         mask |= XCB_CW_BACK_PIXEL;
78         values[0] = 0;
79
80         mask |= XCB_CW_OVERRIDE_REDIRECT;
81         values[1] = 1;
82
83         mask |= XCB_CW_EVENT_MASK;
84         values[2] = XCB_EVENT_MASK_EXPOSURE;
85
86         xcb_create_window(conn,
87                           XCB_COPY_FROM_PARENT,
88                           win, /* the window id */
89                           root, /* parent == root */
90                           50, 50, width, height, /* dimensions */
91                           0, /* border = 0, we draw our own */
92                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
93                           XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
94                           mask,
95                           values);
96
97 #if 0
98         if (cursor > -1)
99                 xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
100 #endif
101
102         /* Map the window (= make it visible) */
103         xcb_map_window(conn, win);
104
105         return win;
106 }
107
108 /*
109  * Returns the ID of the font matching the given pattern and stores the height
110  * of the font (in pixels) in *font_height. die()s if no font matches.
111  *
112  */
113 int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height) {
114         xcb_void_cookie_t font_cookie;
115         xcb_list_fonts_with_info_cookie_t info_cookie;
116
117         /* Send all our requests first */
118         int result;
119         result = xcb_generate_id(conn);
120         font_cookie = xcb_open_font_checked(conn, result, strlen(pattern), pattern);
121         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
122
123         xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
124         if (error != NULL) {
125                 fprintf(stderr, "ERROR: Could not open font: %d\n", error->error_code);
126                 exit(1);
127         }
128
129         /* Get information (height/name) for this font */
130         xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
131         if (reply == NULL)
132                 die("Could not load font \"%s\"\n", pattern);
133
134         *font_height = reply->font_ascent + reply->font_descent;
135
136         return result;
137 }