]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Various code improvements
[i3/i3] / src / mainx.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <stdbool.h>
9 #include <assert.h>
10
11 #include <xcb/xcb.h>
12
13 #include <X11/XKBlib.h>
14 #include <X11/extensions/XKB.h>
15
16 #include <xcb/xcb_wm.h>
17 #include <xcb/xcb_aux.h>
18 #include <xcb/xcb_event.h>
19 #include <xcb/xcb_property.h>
20 #include <xcb/xcb_keysyms.h>
21 #include <xcb/xcb_icccm.h>
22 #include <xcb/xinerama.h>
23 #include "data.h"
24
25 #include "queue.h"
26 #include "table.h"
27 #include "font.h"
28 #include "layout.h"
29 #include "debug.h"
30 #include "handlers.h"
31 #include "util.h"
32
33 #define TERMINAL "/usr/pkg/bin/urxvt"
34
35 Display *xkbdpy;
36
37 TAILQ_HEAD(bindings_head, Binding) bindings;
38 xcb_event_handlers_t evenths;
39
40 /* hm, xcb_wm wants us to implement this. */
41 table_t *byChild = 0;
42 table_t *byParent = 0;
43 xcb_window_t root_win;
44
45 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
46 int num_screens = 0;
47
48 /*
49  *
50  * TODO: what exactly does this, what happens if we leave stuff out?
51  *
52  */
53 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
54 {
55         printf("managing window.\n");
56         xcb_drawable_t d = { window };
57         xcb_get_geometry_cookie_t geomc;
58         xcb_get_geometry_reply_t *geom;
59         xcb_get_window_attributes_reply_t *attr = 0;
60         if(wa.tag == TAG_COOKIE)
61         {
62                 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
63                 if(!attr)
64                         return;
65                 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
66                 {
67                         printf("Window 0x%08x is not mapped. Ignoring.\n", window);
68                         free(attr);
69                         return;
70                 }
71                 wa.tag = TAG_VALUE;
72                 wa.u.override_redirect = attr->override_redirect;
73         }
74         if(!wa.u.override_redirect && table_get(byChild, window))
75         {
76                 printf("Window 0x%08x already managed. Ignoring.\n", window);
77                 free(attr);
78                 return;
79         }
80         if(wa.u.override_redirect)
81         {
82                 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
83                 free(attr);
84                 return;
85         }
86         geomc = xcb_get_geometry(c, d);
87         if(!attr)
88         {
89                 wa.tag = TAG_COOKIE;
90                 wa.u.cookie = xcb_get_window_attributes(c, window);
91                 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
92         }
93         geom = xcb_get_geometry_reply(c, geomc, 0);
94         if(attr && geom)
95         {
96                 reparent_window(c, window, attr->visual, geom->root, geom->depth, geom->x, geom->y, geom->width, geom->height);
97                 xcb_property_changed(prophs, XCB_PROPERTY_NEW_VALUE, window, WM_NAME);
98         }
99         free(attr);
100         free(geom);
101 }
102
103 /*
104  * reparent_window() gets called when a new window was opened and becomes a child of the root
105  * window, or it gets called by us when we manage the already existing windows at startup.
106  *
107  * Essentially, this is the point, where we take over control.
108  *
109  */
110 void reparent_window(xcb_connection_t *conn, xcb_window_t child,
111                 xcb_visualid_t visual, xcb_window_t root, uint8_t depth,
112                 int16_t x, int16_t y, uint16_t width, uint16_t height) {
113
114         Client *new = table_get(byChild, child);
115         if (new == NULL) {
116                 /* TODO: When does this happen for existing clients? Is that a bug? */
117                 printf("oh, it's new\n");
118                 new = calloc(sizeof(Client), 1);
119                 /* We initialize x and y with the invalid coordinates -1 so that they will
120                    get updated at the next render_layout() at any case */
121                 new->x = -1;
122                 new->y = -1;
123         }
124         uint32_t mask = 0;
125         uint32_t values[3];
126
127         /* Insert into the currently active container */
128         CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
129
130         /* Update the data structures */
131         CUR_CELL->currently_focused = new;
132         new->container = CUR_CELL;
133
134         new->frame = xcb_generate_id(conn);
135         new->child = child;
136         new->width = width;
137         new->height = height;
138
139         /* Don’t generate events for our new window, it should *not* be managed */
140         mask |= XCB_CW_OVERRIDE_REDIRECT;
141         values[0] = 1;
142
143         /* We want to know when… */
144         mask |= XCB_CW_EVENT_MASK;
145         values[1] =     XCB_EVENT_MASK_BUTTON_PRESS |   /* …mouse is pressed/released */
146                         XCB_EVENT_MASK_BUTTON_RELEASE |
147                         XCB_EVENT_MASK_EXPOSURE |       /* …our window needs to be redrawn */
148                         XCB_EVENT_MASK_ENTER_WINDOW;    /* …user moves cursor inside our window */
149
150         printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
151
152         i3Font *font = load_font(conn, pattern);
153
154         /* Yo dawg, I heard you like windows, so I create a window around your window… */
155         xcb_create_window(conn,
156                         depth,
157                         new->frame,
158                         root,
159                         x,
160                         y,
161                         width + 2 + 2,                  /* 2 px border at each side */
162                         height + 2 + 2 + font->height,  /* 2 px border plus font’s height */
163                         0,                              /* border_width = 0, we draw our own borders */
164                         XCB_WINDOW_CLASS_INPUT_OUTPUT,
165                         visual,
166                         mask,
167                         values);
168         xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
169
170         /* Map the window on the screen (= make it visible) */
171         xcb_map_window(conn, new->frame);
172
173         /* Generate a graphics context for the titlebar */
174         new->titlegc = xcb_generate_id(conn);
175         xcb_create_gc(conn, new->titlegc, new->frame, 0, 0);
176
177         /* Put our data structure (Client) into the table */
178         table_put(byParent, new->frame, new);
179         table_put(byChild, child, new);
180
181         /* Moves the original window into the new frame we've created for it */
182         xcb_reparent_window(conn, child, new->frame, 0, font->height);
183
184         /* We are interested in property changes */
185         mask = XCB_CW_EVENT_MASK;
186         values[0] =     XCB_EVENT_MASK_PROPERTY_CHANGE |
187                         XCB_EVENT_MASK_STRUCTURE_NOTIFY |
188                         XCB_EVENT_MASK_ENTER_WINDOW |
189                         XCB_EVENT_MASK_BUTTON_PRESS;
190         xcb_change_window_attributes(conn, child, mask, values);
191
192         /* We need to grab the mouse buttons for click to focus */
193         xcb_grab_button(conn, false, child, XCB_EVENT_MASK_BUTTON_PRESS,
194                         XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
195                         1 /* left mouse button */,
196                         XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */);
197
198         /* Focus the new window */
199         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->child, XCB_CURRENT_TIME);
200
201         render_layout(conn);
202 }
203
204 void manage_existing_windows(xcb_connection_t *c, xcb_property_handlers_t *prophs, xcb_window_t root) {
205         xcb_query_tree_cookie_t wintree;
206         xcb_query_tree_reply_t *rep;
207         int i, len;
208         xcb_window_t *children;
209         xcb_get_window_attributes_cookie_t *cookies;
210
211         wintree = xcb_query_tree(c, root);
212         rep = xcb_query_tree_reply(c, wintree, 0);
213         if(!rep)
214                 return;
215         len = xcb_query_tree_children_length(rep);
216         cookies = malloc(len * sizeof(*cookies));
217         if(!cookies)
218         {
219                 free(rep);
220                 return;
221         }
222         children = xcb_query_tree_children(rep);
223         for(i = 0; i < len; ++i)
224                 cookies[i] = xcb_get_window_attributes(c, children[i]);
225         for(i = 0; i < len; ++i)
226         {
227                 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
228                 manage_window(prophs, c, children[i], wa);
229         }
230         free(rep);
231 }
232
233 static void initialize_xinerama(xcb_connection_t *conn) {
234         xcb_xinerama_query_screens_reply_t *reply;
235         xcb_xinerama_screen_info_t *screen_info;
236         int screen;
237
238         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
239                 printf("Xinerama extension not found, disabling.\n");
240                 return;
241         }
242
243         if (!xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL)->state) {
244                 printf("Xinerama is not active (in your X-Server), disabling.\n");
245                 return;
246         }
247
248
249         reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
250         /* TODO: error check */
251         screen_info = xcb_xinerama_query_screens_screen_info(reply);
252
253         num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
254
255         /* Just go through each workspace and associate as many screens as we can. */
256         for (screen = 0; screen < num_screens; screen++) {
257                 workspaces[screen].x = screen_info[screen].x_org;
258                 workspaces[screen].y = screen_info[screen].y_org;
259                 workspaces[screen].width = screen_info[screen].width;
260                 workspaces[screen].height = screen_info[screen].height;
261                 workspaces[screen].screen_num = screen;
262
263                 printf("found Xinerama screen: %d x %d at %d x %d\n",
264                                 screen_info[screen].width, screen_info[screen].height,
265                                 screen_info[screen].x_org, screen_info[screen].y_org);
266         }
267
268         free(screen_info);
269 }
270
271 int main(int argc, char *argv[], char *env[]) {
272         int i, screens;
273         xcb_connection_t *c;
274         xcb_property_handlers_t prophs;
275         xcb_window_t root;
276
277         /* Initialize the table data structures for each workspace */
278         init_table();
279
280         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
281         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
282
283         byChild = alloc_table();
284         byParent = alloc_table();
285
286         TAILQ_INIT(&bindings);
287
288         c = xcb_connect(NULL, &screens);
289
290         /* TODO: this has to be more beautiful somewhen */
291         int major, minor, error;
292
293         major = XkbMajorVersion;
294         minor = XkbMinorVersion;
295
296         int evBase, errBase;
297
298         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
299                 fprintf(stderr, "XkbOpenDisplay() failed\n");
300                 return 1;
301         }
302
303         int i1;
304         if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
305                 fprintf(stderr, "XKB not supported by X-server\n");
306                 return 1;
307         }
308         /* end of ugliness */
309
310         xcb_event_handlers_init(c, &evenths);
311         for(i = 2; i < 128; ++i)
312                 xcb_event_set_handler(&evenths, i, handle_event, 0);
313
314         for(i = 0; i < 256; ++i)
315                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
316
317         /* Expose = an Application should redraw itself. That is, we have to redraw our
318          * contents (= top/bottom bar, titlebars for each window) */
319         xcb_event_set_expose_handler(&evenths, handle_expose_event, 0);
320
321         /* Key presses/releases are pretty obvious, I think */
322         xcb_event_set_key_press_handler(&evenths, handle_key_press, 0);
323         xcb_event_set_key_release_handler(&evenths, handle_key_release, 0);
324
325         /* Enter window = user moved his mouse over the window */
326         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
327
328         /* Button press = user pushed a mouse button over one of our windows */
329         xcb_event_set_button_press_handler(&evenths, handle_button_press, 0);
330
331         xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
332
333         xcb_property_handlers_init(&prophs, &evenths);
334         xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
335
336         xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
337
338         root = xcb_aux_get_screen(c, screens)->root;
339         root_win = root;
340
341         uint32_t mask = XCB_CW_EVENT_MASK;
342         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE };
343         xcb_change_window_attributes(c, root, mask, values);
344
345         #define BIND(key, modifier, cmd) { \
346                 Binding *new = malloc(sizeof(Binding)); \
347                 new->keycode = key; \
348                 new->mods = modifier; \
349                 new->command = cmd; \
350                 TAILQ_INSERT_TAIL(&bindings, new, bindings); \
351         }
352
353         /* 38 = 'a' */
354         BIND(38, BIND_MODE_SWITCH, "foo");
355
356         BIND(30, 0, "exec /usr/pkg/bin/urxvt");
357
358         BIND(44, BIND_MOD_1, "h");
359         BIND(45, BIND_MOD_1, "j");
360         BIND(46, BIND_MOD_1, "k");
361         BIND(47, BIND_MOD_1, "l");
362
363         BIND(44, BIND_MOD_1 | BIND_CONTROL, "sh");
364         BIND(45, BIND_MOD_1 | BIND_CONTROL, "sj");
365         BIND(46, BIND_MOD_1 | BIND_CONTROL, "sk");
366         BIND(47, BIND_MOD_1 | BIND_CONTROL, "sl");
367
368         BIND(44, BIND_MOD_1 | BIND_SHIFT, "mh");
369         BIND(45, BIND_MOD_1 | BIND_SHIFT, "mj");
370         BIND(46, BIND_MOD_1 | BIND_SHIFT, "mk");
371         BIND(47, BIND_MOD_1 | BIND_SHIFT, "ml");
372
373         BIND(10, BIND_MOD_1 , "1");
374         BIND(11, BIND_MOD_1 , "2");
375         BIND(12, BIND_MOD_1 , "3");
376         BIND(13, BIND_MOD_1 , "4");
377         BIND(14, BIND_MOD_1 , "5");
378         BIND(15, BIND_MOD_1 , "6");
379         BIND(16, BIND_MOD_1 , "7");
380         BIND(17, BIND_MOD_1 , "8");
381         BIND(18, BIND_MOD_1 , "9");
382         BIND(19, BIND_MOD_1 , "0");
383
384         Binding *bind;
385         TAILQ_FOREACH(bind, &bindings, bindings) {
386                 printf("Grabbing %d\n", bind->keycode);
387                 if (bind->mods & BIND_MODE_SWITCH)
388                         xcb_grab_key(c, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
389                 else xcb_grab_key(c, 0, root, bind->mods, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
390         }
391
392         /* check for Xinerama */
393         printf("Checking for Xinerama...\n");
394         initialize_xinerama(c);
395
396         start_application(TERMINAL);
397
398         xcb_flush(c);
399
400         manage_existing_windows(c, &prophs, root);
401
402         xcb_event_wait_for_event_loop(&evenths);
403
404         /* not reached */
405         return 0;
406 }