4 * i3 - an improved dynamic tiling window manager
6 * © 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>
46 #define TERMINAL "/usr/pkg/bin/urxvt"
50 TAILQ_HEAD(bindings_head, Binding) bindings = TAILQ_HEAD_INITIALIZER(bindings);
51 xcb_event_handlers_t evenths;
53 xcb_window_t root_win;
57 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
62 * TODO: what exactly does this, what happens if we leave stuff out?
65 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
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)
74 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
77 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
79 printf("Window 0x%08x is not mapped. Ignoring.\n", window);
84 wa.u.override_redirect = attr->override_redirect;
86 if(!wa.u.override_redirect && table_get(byChild, window))
88 printf("Window 0x%08x already managed. Ignoring.\n", window);
92 if(wa.u.override_redirect)
94 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
98 geomc = xcb_get_geometry(c, d);
102 wa.u.cookie = xcb_get_window_attributes(c, window);
103 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
105 geom = xcb_get_geometry_reply(c, geomc, 0);
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);
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.
119 * Essentially, this is the point, where we take over control.
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) {
126 Client *new = table_get(byChild, child);
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 */
139 /* Insert into the currently active container */
140 CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
142 /* Update the data structures */
143 CUR_CELL->currently_focused = new;
144 new->container = CUR_CELL;
146 new->frame = xcb_generate_id(conn);
148 new->rect.width = width;
149 new->rect.height = height;
151 /* Don’t generate events for our new window, it should *not* be managed */
152 mask |= XCB_CW_OVERRIDE_REDIRECT;
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 */
162 printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
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);
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 */
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);
176 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
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);
182 /* Put our data structure (Client) into the table */
183 table_put(byParent, new->frame, new);
184 table_put(byChild, child, new);
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");
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");
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 */);
205 /* Focus the new window */
206 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, new->child, XCB_CURRENT_TIME);
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;
215 xcb_window_t *children;
216 xcb_get_window_attributes_cookie_t *cookies;
218 wintree = xcb_query_tree(c, root);
219 rep = xcb_query_tree_reply(c, wintree, 0);
222 len = xcb_query_tree_children_length(rep);
223 cookies = malloc(len * sizeof(*cookies));
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)
234 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
235 manage_window(prophs, c, children[i], wa);
240 int main(int argc, char *argv[], char *env[]) {
243 xcb_property_handlers_t prophs;
246 /* Initialize the table data structures for each workspace */
249 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
250 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
252 byChild = alloc_table();
253 byParent = alloc_table();
255 c = xcb_connect(NULL, &screens);
257 /* TODO: this has to be more beautiful somewhen */
258 int major, minor, error;
260 major = XkbMajorVersion;
261 minor = XkbMinorVersion;
265 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
266 fprintf(stderr, "XkbOpenDisplay() failed\n");
271 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
272 fprintf(stderr, "XKB not supported by X-server\n");
275 /* end of ugliness */
277 xcb_event_handlers_init(c, &evenths);
278 for(i = 2; i < 128; ++i)
279 xcb_event_set_handler(&evenths, i, handle_event, 0);
281 for(i = 0; i < 256; ++i)
282 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
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);
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);
292 /* Enter window = user moved his mouse over the window */
293 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
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);
298 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
300 xcb_property_handlers_init(&prophs, &evenths);
301 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
303 xcb_event_set_client_message_handler(&evenths, handle_client_message, 0);
305 xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
307 root = xcb_aux_get_screen(c, screens)->root;
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);
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); \
319 printf("Could not get atom " #name "\n"); \
322 atoms[name] = reply->atom; \
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);
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");
335 xcb_change_property(c, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
337 xcb_change_property(c, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME] , atoms[UTF8_STRING], 8, strlen("i3"), "i3");
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); \
348 BIND(38, BIND_MODE_SWITCH, "foo");
350 BIND(30, 0, "exec /usr/pkg/bin/urxvt");
352 BIND(41, BIND_MOD_1, "f");
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");
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");
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");
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");
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);
388 /* check for Xinerama */
389 printf("Checking for Xinerama...\n");
390 initialize_xinerama(c);
392 start_application(TERMINAL);
396 manage_existing_windows(c, &prophs, root);
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);
402 printf("Could not get pointer position\n");
406 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
407 if (screen == NULL) {
408 printf("ERROR: No such screen\n");
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];
416 xcb_event_wait_for_event_loop(&evenths);