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