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>
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);
166 height = min(height, c_ws->rect.y + c_ws->rect.height);
167 width = min(width, c_ws->rect.x + c_ws->rect.width);
169 /* Yo dawg, I heard you like windows, so I create a window around your window… */
170 xcb_void_cookie_t cookie = xcb_create_window_checked(conn,
176 width + 2 + 2, /* 2 px border at each side */
177 height + 2 + 2 + font->height, /* 2 px border plus font’s height */
178 0, /* border_width = 0, we draw our own borders */
179 XCB_WINDOW_CLASS_INPUT_OUTPUT,
180 XCB_WINDOW_CLASS_COPY_FROM_PARENT,
183 printf("x = %d, y = %d, width = %d, height = %d\n", x, y, width, height);
184 check_error(conn, cookie, "Could not create frame");
185 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
187 /* Map the window on the screen (= make it visible) */
188 xcb_map_window(conn, new->frame);
190 /* Generate a graphics context for the titlebar */
191 new->titlegc = xcb_generate_id(conn);
192 xcb_create_gc(conn, new->titlegc, new->frame, 0, 0);
194 /* Put our data structure (Client) into the table */
195 table_put(byParent, new->frame, new);
196 table_put(byChild, child, new);
198 /* Moves the original window into the new frame we've created for it */
199 new->awaiting_useless_unmap = true;
200 cookie = xcb_reparent_window_checked(conn, child, new->frame, 0, font->height);
201 check_error(conn, cookie, "Could not reparent window");
203 /* We are interested in property changes */
204 mask = XCB_CW_EVENT_MASK;
205 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
206 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
207 XCB_EVENT_MASK_ENTER_WINDOW;
208 cookie = xcb_change_window_attributes_checked(conn, child, mask, values);
209 check_error(conn, cookie, "Could not change window attributes");
211 /* We need to grab the mouse buttons for click to focus */
212 xcb_grab_button(conn, false, child, XCB_EVENT_MASK_BUTTON_PRESS,
213 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
214 1 /* left mouse button */,
215 XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */);
217 /* Focus the new window */
218 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, new->child, XCB_CURRENT_TIME);
223 void manage_existing_windows(xcb_connection_t *c, xcb_property_handlers_t *prophs, xcb_window_t root) {
224 xcb_query_tree_cookie_t wintree;
225 xcb_query_tree_reply_t *rep;
227 xcb_window_t *children;
228 xcb_get_window_attributes_cookie_t *cookies;
230 wintree = xcb_query_tree(c, root);
231 rep = xcb_query_tree_reply(c, wintree, 0);
234 len = xcb_query_tree_children_length(rep);
235 cookies = malloc(len * sizeof(*cookies));
241 children = xcb_query_tree_children(rep);
242 for(i = 0; i < len; ++i)
243 cookies[i] = xcb_get_window_attributes(c, children[i]);
244 for(i = 0; i < len; ++i)
246 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
247 manage_window(prophs, c, children[i], wa);
252 int main(int argc, char *argv[], char *env[]) {
255 xcb_property_handlers_t prophs;
258 /* Initialize the table data structures for each workspace */
261 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
262 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
264 byChild = alloc_table();
265 byParent = alloc_table();
267 c = xcb_connect(NULL, &screens);
269 /* TODO: this has to be more beautiful somewhen */
270 int major, minor, error;
272 major = XkbMajorVersion;
273 minor = XkbMinorVersion;
277 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
278 fprintf(stderr, "XkbOpenDisplay() failed\n");
283 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
284 fprintf(stderr, "XKB not supported by X-server\n");
287 /* end of ugliness */
289 xcb_event_handlers_init(c, &evenths);
290 for(i = 2; i < 128; ++i)
291 xcb_event_set_handler(&evenths, i, handle_event, 0);
293 for(i = 0; i < 256; ++i)
294 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
296 /* Expose = an Application should redraw itself. That is, we have to redraw our
297 * contents (= top/bottom bar, titlebars for each window) */
298 xcb_event_set_expose_handler(&evenths, handle_expose_event, 0);
300 /* Key presses/releases are pretty obvious, I think */
301 xcb_event_set_key_press_handler(&evenths, handle_key_press, 0);
302 xcb_event_set_key_release_handler(&evenths, handle_key_release, 0);
304 /* Enter window = user moved his mouse over the window */
305 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
307 /* Button press = user pushed a mouse button over one of our windows */
308 xcb_event_set_button_press_handler(&evenths, handle_button_press, 0);
310 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
312 xcb_property_handlers_init(&prophs, &evenths);
313 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
315 xcb_event_set_client_message_handler(&evenths, handle_client_message, 0);
317 xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
319 root = xcb_aux_get_screen(c, screens)->root;
322 uint32_t mask = XCB_CW_EVENT_MASK;
323 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_ENTER_WINDOW};
324 xcb_change_window_attributes(c, root, mask, values);
326 /* Setup NetWM atoms */
327 /* TODO: needs cleanup, needs more xcb (asynchronous), needs more error checking */
328 #define GET_ATOM(name) { \
329 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(c, xcb_intern_atom(c, 0, strlen(#name), #name), NULL); \
331 printf("Could not get atom " #name "\n"); \
334 atoms[name] = reply->atom; \
338 GET_ATOM(_NET_SUPPORTED);
339 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
340 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
341 GET_ATOM(_NET_WM_NAME);
342 GET_ATOM(UTF8_STRING);
343 GET_ATOM(_NET_WM_STATE);
345 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");
347 xcb_change_property(c, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
349 xcb_change_property(c, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME] , atoms[UTF8_STRING], 8, strlen("i3"), "i3");
351 #define BIND(key, modifier, cmd) { \
352 Binding *new = malloc(sizeof(Binding)); \
353 new->keycode = key; \
354 new->mods = modifier; \
355 new->command = cmd; \
356 TAILQ_INSERT_TAIL(&bindings, new, bindings); \
360 BIND(38, BIND_MODE_SWITCH, "foo");
362 BIND(30, 0, "exec /usr/pkg/bin/urxvt");
364 BIND(41, BIND_MOD_1, "f");
366 BIND(44, BIND_MOD_1, "h");
367 BIND(45, BIND_MOD_1, "j");
368 BIND(46, BIND_MOD_1, "k");
369 BIND(47, BIND_MOD_1, "l");
371 BIND(44, BIND_MOD_1 | BIND_CONTROL, "sh");
372 BIND(45, BIND_MOD_1 | BIND_CONTROL, "sj");
373 BIND(46, BIND_MOD_1 | BIND_CONTROL, "sk");
374 BIND(47, BIND_MOD_1 | BIND_CONTROL, "sl");
376 BIND(44, BIND_MOD_1 | BIND_SHIFT, "mh");
377 BIND(45, BIND_MOD_1 | BIND_SHIFT, "mj");
378 BIND(46, BIND_MOD_1 | BIND_SHIFT, "mk");
379 BIND(47, BIND_MOD_1 | BIND_SHIFT, "ml");
381 BIND(10, BIND_MOD_1 , "1");
382 BIND(11, BIND_MOD_1 , "2");
383 BIND(12, BIND_MOD_1 , "3");
384 BIND(13, BIND_MOD_1 , "4");
385 BIND(14, BIND_MOD_1 , "5");
386 BIND(15, BIND_MOD_1 , "6");
387 BIND(16, BIND_MOD_1 , "7");
388 BIND(17, BIND_MOD_1 , "8");
389 BIND(18, BIND_MOD_1 , "9");
390 BIND(19, BIND_MOD_1 , "0");
393 TAILQ_FOREACH(bind, &bindings, bindings) {
394 printf("Grabbing %d\n", bind->keycode);
395 if (bind->mods & BIND_MODE_SWITCH)
396 xcb_grab_key(c, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
397 else xcb_grab_key(c, 0, root, bind->mods, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
400 /* check for Xinerama */
401 printf("Checking for Xinerama...\n");
402 initialize_xinerama(c);
404 start_application(TERMINAL);
408 manage_existing_windows(c, &prophs, root);
410 /* Get pointer position to see on which screen we’re starting */
411 xcb_query_pointer_cookie_t pointer_cookie = xcb_query_pointer(c, root);
412 xcb_query_pointer_reply_t *reply = xcb_query_pointer_reply(c, pointer_cookie, NULL);
414 printf("Could not get pointer position\n");
418 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
419 if (screen == NULL) {
420 printf("ERROR: No such screen\n");
423 if (screen->current_workspace != 0) {
424 printf("Ok, I need to go to the other workspace\n");
425 c_ws = &workspaces[screen->current_workspace];
428 xcb_event_wait_for_event_loop(&evenths);