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