]> git.sur5r.net Git - i3/i3/blob - src/xcb.c
remove unused cached_fonts TAILQ
[i3/i3] / src / xcb.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * xcb.c: Helper functions for easier usage of XCB
8  *
9  */
10 #include "all.h"
11
12 unsigned int xcb_numlock_mask;
13
14 /*
15  * Loads a font for usage, also getting its height. If fallback is true,
16  * i3 loads 'fixed' or '-misc-*' if the font cannot be found instead of
17  * exiting.
18  *
19  */
20 i3Font load_font(const char *pattern, bool fallback) {
21     i3Font new;
22     xcb_void_cookie_t font_cookie;
23     xcb_list_fonts_with_info_cookie_t info_cookie;
24
25     /* Send all our requests first */
26     new.id = xcb_generate_id(conn);
27     font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
28     info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
29
30     /* Check for errors. If errors, fall back to default font. */
31     xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
32
33     /* If we fail to open font, fall back to 'fixed'. If opening 'fixed' fails fall back to '-misc-*' */
34     if (fallback && error != NULL) {
35         ELOG("Could not open font %s (X error %d). Reverting to backup font.\n", pattern, error->error_code);
36         pattern = "fixed";
37         font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
38         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
39
40         /* Check if we managed to open 'fixed' */
41         xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
42
43         /* Fall back to '-misc-*' if opening 'fixed' fails. */
44         if (error != NULL) {
45             ELOG("Could not open fallback font '%s', trying with '-misc-*'\n",pattern);
46             pattern = "-misc-*";
47             font_cookie = xcb_open_font_checked(conn, new.id, strlen(pattern), pattern);
48             info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
49
50             check_error(conn, font_cookie, "Could open neither requested font nor fallback (fixed or -misc-*");
51         }
52     }
53
54     /* Get information (height/name) for this font */
55     xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
56     exit_if_null(reply, "Could not load font \"%s\"\n", pattern);
57
58     new.height = reply->font_ascent + reply->font_descent;
59
60     free(reply);
61
62     return new;
63 }
64
65 /*
66  * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking
67  * for errors.
68  *
69  */
70 xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class,
71         enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values) {
72     xcb_window_t result = xcb_generate_id(conn);
73
74     /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */
75     uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT);
76
77     xcb_create_window(conn,
78             depth,
79             result, /* the window id */
80             root, /* parent == root */
81             dims.x, dims.y, dims.width, dims.height, /* dimensions */
82             0, /* border = 0, we draw our own */
83             window_class,
84             XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
85             mask,
86             values);
87
88     /* Set the cursor */
89     if (xcursor_supported) {
90         mask = XCB_CW_CURSOR;
91         values[0] = xcursor_get_cursor(cursor);
92         xcb_change_window_attributes(conn, result, mask, values);
93     } else {
94         xcb_cursor_t cursor_id = xcb_generate_id(conn);
95         i3Font cursor_font = load_font("cursor", false);
96         int xcb_cursor = xcursor_get_xcb_cursor(cursor);
97         xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
98                 xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
99         xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
100         xcb_free_cursor(conn, cursor_id);
101     }
102
103     /* Map the window (= make it visible) */
104     if (map)
105         xcb_map_window(conn, result);
106
107     return result;
108 }
109
110 /*
111  * Draws a line from x,y to to_x,to_y using the given color
112  *
113  */
114 void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
115                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y) {
116     xcb_change_gc(conn, gc, XCB_GC_FOREGROUND, (uint32_t[]){ colorpixel });
117     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2,
118                   (xcb_point_t[]) { {x, y}, {to_x, to_y} });
119 }
120
121 /*
122  * Draws a rectangle from x,y with width,height using the given color
123  *
124  */
125 void xcb_draw_rect(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc,
126                    uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
127     xcb_change_gc(conn, gc, XCB_GC_FOREGROUND, (uint32_t[]){ colorpixel });
128     xcb_rectangle_t rect = {x, y, width, height};
129     xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
130 }
131
132 /*
133  * Generates a configure_notify_event with absolute coordinates (relative to the X root
134  * window, not to the client’s frame) for the given client.
135  *
136  */
137 void fake_absolute_configure_notify(Con *con) {
138     xcb_rectangle_t absolute;
139     if (con->window == NULL)
140         return;
141
142     absolute.x = con->rect.x + con->window_rect.x;
143     absolute.y = con->rect.y + con->window_rect.y;
144     absolute.width = con->window_rect.width;
145     absolute.height = con->window_rect.height;
146
147     DLOG("fake rect = (%d, %d, %d, %d)\n", absolute.x, absolute.y, absolute.width, absolute.height);
148
149     fake_configure_notify(conn, absolute, con->window->id, con->border_width);
150 }
151
152 /*
153  * Sends the WM_TAKE_FOCUS ClientMessage to the given window
154  *
155  */
156 void send_take_focus(xcb_window_t window) {
157     /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
158      * In order to properly initialize these bytes, we allocate 32 bytes even
159      * though we only need less for an xcb_configure_notify_event_t */
160     void *event = scalloc(32);
161     xcb_client_message_event_t *ev = event;
162
163     ev->response_type = XCB_CLIENT_MESSAGE;
164     ev->window = window;
165     ev->type = A_WM_PROTOCOLS;
166     ev->format = 32;
167     ev->data.data32[0] = A_WM_TAKE_FOCUS;
168     ev->data.data32[1] = XCB_CURRENT_TIME;
169
170     DLOG("Sending WM_TAKE_FOCUS to the client\n");
171     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
172     free(event);
173 }
174
175 /*
176  * Raises the given window (typically client->frame) above all other windows
177  *
178  */
179 void xcb_raise_window(xcb_connection_t *conn, xcb_window_t window) {
180     uint32_t values[] = { XCB_STACK_MODE_ABOVE };
181     xcb_configure_window(conn, window, XCB_CONFIG_WINDOW_STACK_MODE, values);
182 }
183
184 /*
185  * Query the width of the given text (16-bit characters, UCS) with given real
186  * length (amount of glyphs) using the given font.
187  *
188  */
189 int predict_text_width(char *text, int length) {
190     xcb_query_text_extents_cookie_t cookie;
191     xcb_query_text_extents_reply_t *reply;
192     xcb_generic_error_t *error;
193     int width;
194
195     cookie = xcb_query_text_extents(conn, config.font.id, length, (xcb_char2b_t*)text);
196     if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) {
197         ELOG("Could not get text extents (X error code %d)\n",
198              error->error_code);
199         /* We return the rather safe guess of 7 pixels, because a
200          * rendering error is better than a crash. Plus, the user will
201          * see the error in his log. */
202         return 7;
203     }
204
205     width = reply->overall_width;
206     free(reply);
207     return width;
208 }
209
210 /*
211  * Configures the given window to have the size/position specified by given rect
212  *
213  */
214 void xcb_set_window_rect(xcb_connection_t *conn, xcb_window_t window, Rect r) {
215     xcb_void_cookie_t cookie;
216     cookie = xcb_configure_window(conn, window,
217                          XCB_CONFIG_WINDOW_X |
218                          XCB_CONFIG_WINDOW_Y |
219                          XCB_CONFIG_WINDOW_WIDTH |
220                          XCB_CONFIG_WINDOW_HEIGHT,
221                          &(r.x));
222     /* ignore events which are generated because we configured a window */
223     add_ignore_event(cookie.sequence, -1);
224 }
225
226 /*
227  * Returns true if the given reply contains the given atom.
228  *
229  */
230 bool xcb_reply_contains_atom(xcb_get_property_reply_t *prop, xcb_atom_t atom) {
231     if (prop == NULL || xcb_get_property_value_length(prop) == 0)
232         return false;
233
234     xcb_atom_t *atoms;
235     if ((atoms = xcb_get_property_value(prop)) == NULL)
236         return false;
237
238     for (int i = 0; i < xcb_get_property_value_length(prop) / (prop->format / 8); i++)
239         if (atoms[i] == atom)
240             return true;
241
242     return false;
243
244 }
245
246 /**
247  * Moves the mouse pointer into the middle of rect.
248  *
249  */
250 void xcb_warp_pointer_rect(xcb_connection_t *conn, Rect *rect) {
251     int mid_x = rect->x + (rect->width / 2);
252     int mid_y = rect->y + (rect->height / 2);
253
254     LOG("warp pointer to: %d %d\n", mid_x, mid_y);
255     xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
256 }
257
258 /*
259  * Set the cursor of the root window to the given cursor id.
260  * This function should only be used if xcursor_supported == false.
261  * Otherwise, use xcursor_set_root_cursor().
262  *
263  */
264 void xcb_set_root_cursor(int cursor) {
265     xcb_cursor_t cursor_id = xcb_generate_id(conn);
266     i3Font cursor_font = load_font("cursor", false);
267     int xcb_cursor = xcursor_get_xcb_cursor(cursor);
268     xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
269             xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
270     xcb_change_window_attributes(conn, root, XCB_CW_CURSOR, &cursor_id);
271     xcb_free_cursor(conn, cursor_id);
272     xcb_flush(conn);
273 }