4 * i3 - an improved dynamic tiling window manager
6 * (c) 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
16 #include <sys/types.h>
24 #include <X11/XKBlib.h>
25 #include <X11/extensions/XKB.h>
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>
45 #define TERMINAL "/usr/pkg/bin/urxvt"
49 TAILQ_HEAD(bindings_head, Binding) bindings;
50 xcb_event_handlers_t evenths;
52 /* hm, xcb_wm wants us to implement this. */
54 table_t *byParent = 0;
55 xcb_window_t root_win;
59 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
64 * TODO: what exactly does this, what happens if we leave stuff out?
67 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
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)
76 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
79 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
81 printf("Window 0x%08x is not mapped. Ignoring.\n", window);
86 wa.u.override_redirect = attr->override_redirect;
88 if(!wa.u.override_redirect && table_get(byChild, window))
90 printf("Window 0x%08x already managed. Ignoring.\n", window);
94 if(wa.u.override_redirect)
96 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
100 geomc = xcb_get_geometry(c, d);
104 wa.u.cookie = xcb_get_window_attributes(c, window);
105 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
107 geom = xcb_get_geometry_reply(c, geomc, 0);
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);
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.
121 * Essentially, this is the point, where we take over control.
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) {
128 Client *new = table_get(byChild, child);
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 */
141 /* Insert into the currently active container */
142 CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
144 /* Update the data structures */
145 CUR_CELL->currently_focused = new;
146 new->container = CUR_CELL;
148 new->frame = xcb_generate_id(conn);
151 new->height = height;
153 /* Don’t generate events for our new window, it should *not* be managed */
154 mask |= XCB_CW_OVERRIDE_REDIRECT;
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 */
164 printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
166 i3Font *font = load_font(conn, pattern);
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,
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,
182 check_error(conn, cookie, "Could not create frame");
183 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
185 /* Map the window on the screen (= make it visible) */
186 xcb_map_window(conn, new->frame);
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);
192 /* Put our data structure (Client) into the table */
193 table_put(byParent, new->frame, new);
194 table_put(byChild, child, new);
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);
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);
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 */);
213 /* Focus the new window */
214 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->child, XCB_CURRENT_TIME);
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;
223 xcb_window_t *children;
224 xcb_get_window_attributes_cookie_t *cookies;
226 wintree = xcb_query_tree(c, root);
227 rep = xcb_query_tree_reply(c, wintree, 0);
230 len = xcb_query_tree_children_length(rep);
231 cookies = malloc(len * sizeof(*cookies));
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)
242 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
243 manage_window(prophs, c, children[i], wa);
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;
253 if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
254 printf("Xinerama extension not found, disabling.\n");
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");
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);
268 num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
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;
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);
286 int main(int argc, char *argv[], char *env[]) {
289 xcb_property_handlers_t prophs;
292 /* Initialize the table data structures for each workspace */
295 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
296 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
298 byChild = alloc_table();
299 byParent = alloc_table();
301 TAILQ_INIT(&bindings);
303 c = xcb_connect(NULL, &screens);
305 /* TODO: this has to be more beautiful somewhen */
306 int major, minor, error;
308 major = XkbMajorVersion;
309 minor = XkbMinorVersion;
313 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
314 fprintf(stderr, "XkbOpenDisplay() failed\n");
319 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
320 fprintf(stderr, "XKB not supported by X-server\n");
323 /* end of ugliness */
325 xcb_event_handlers_init(c, &evenths);
326 for(i = 2; i < 128; ++i)
327 xcb_event_set_handler(&evenths, i, handle_event, 0);
329 for(i = 0; i < 256; ++i)
330 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
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);
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);
340 /* Enter window = user moved his mouse over the window */
341 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
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);
346 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
348 xcb_property_handlers_init(&prophs, &evenths);
349 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
351 xcb_event_set_client_message_handler(&evenths, handle_client_message, 0);
353 xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
355 root = xcb_aux_get_screen(c, screens)->root;
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);
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); \
367 printf("Could not get atom " #name "\n"); \
370 atoms[name] = reply->atom; \
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);
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");
383 xcb_change_property(c, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
385 xcb_change_property(c, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME] , atoms[UTF8_STRING], 8, strlen("i3"), "i3");
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); \
396 BIND(38, BIND_MODE_SWITCH, "foo");
398 BIND(30, 0, "exec /usr/pkg/bin/urxvt");
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");
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");
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");
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");
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);
434 /* check for Xinerama */
435 printf("Checking for Xinerama...\n");
436 initialize_xinerama(c);
438 start_application(TERMINAL);
442 manage_existing_windows(c, &prophs, root);
444 xcb_event_wait_for_event_loop(&evenths);