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