4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
14 #include <sys/types.h>
23 #include <X11/XKBlib.h>
24 #include <X11/extensions/XKB.h>
27 #include <xcb/xcb_atom.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>
53 xcb_connection_t *global_conn;
55 /* This is the path to i3, copied from argv[0] when starting up */
58 /* This is our connection to X11 for use with XKB */
61 xcb_key_symbols_t *keysyms;
63 /* The list of key bindings */
64 struct bindings_head *bindings;
66 /* The list of exec-lines */
67 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
69 /* The list of assignments */
70 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
72 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
73 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
75 /* The event handlers need to be global because they are accessed by our custom event handler
76 in handle_button_press(), needed for graphical resizing */
77 xcb_event_handlers_t evenths;
78 xcb_atom_t atoms[NUM_ATOMS];
83 /* The depth of the root screen (used e.g. for creating new pixmaps later) */
86 /* We hope that XKB is supported and set this to false */
87 bool xkb_supported = true;
90 * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
91 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
94 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
95 /* empty, because xcb_prepare_cb and xcb_check_cb are used */
99 * Flush before blocking (and waiting for new events)
102 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
103 xcb_flush(evenths.c);
107 * Instead of polling the X connection socket we leave this to
108 * xcb_poll_for_event() which knows better than we can ever know.
111 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
112 xcb_generic_event_t *event;
114 while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
115 xcb_event_handle(&evenths, event);
121 * When using xmodmap to change the keyboard mapping, this event
122 * is only sent via XKB. Therefore, we need this special handler.
125 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
126 DLOG("Handling XKB event\n");
128 /* When using xmodmap, every change (!) gets an own event.
129 * Therefore, we just read all events and only handle the
130 * mapping_notify once (we do not receive any other XKB
132 while (XPending(xkbdpy))
133 XNextEvent(xkbdpy, &ev);
135 xcb_key_symbols_free(keysyms);
136 keysyms = xcb_key_symbols_alloc(global_conn);
138 xcb_get_numlock_mask(global_conn);
140 ungrab_all_keys(global_conn);
141 DLOG("Re-grabbing...\n");
142 grab_all_keys(global_conn);
148 int main(int argc, char *argv[], char *env[]) {
150 char *override_configpath = NULL;
151 bool autostart = true;
152 xcb_connection_t *conn;
153 xcb_property_handlers_t prophs;
154 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
155 static struct option long_options[] = {
156 {"no-autostart", no_argument, 0, 'a'},
157 {"config", required_argument, 0, 'c'},
158 {"version", no_argument, 0, 'v'},
159 {"help", no_argument, 0, 'h'},
162 int option_index = 0;
164 setlocale(LC_ALL, "");
166 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
167 if (!isatty(fileno(stdout)))
168 setbuf(stdout, NULL);
172 while ((opt = getopt_long(argc, argv, "c:vahld:V", long_options, &option_index)) != -1) {
175 LOG("Autostart disabled using -a\n");
179 override_configpath = sstrdup(optarg);
182 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
188 LOG("Enabling debug loglevel %s\n", optarg);
189 add_loglevel(optarg);
192 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
195 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V]\n", argv[0]);
196 fprintf(stderr, "\n");
197 fprintf(stderr, "-a: disable autostart\n");
198 fprintf(stderr, "-v: display version and exit\n");
199 fprintf(stderr, "-V: enable verbose mode\n");
200 fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
201 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
206 LOG("i3 version " I3_VERSION " starting\n");
208 /* Initialize the table data structures for each workspace */
211 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
212 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
214 conn = global_conn = xcb_connect(NULL, &screens);
216 if (xcb_connection_has_error(conn))
217 die("Cannot open display\n");
219 load_configuration(conn, override_configpath, false);
221 /* Create the initial container on the first workspace. This used to
222 * be part of init_table, but since it possibly requires an X
223 * connection and a loaded configuration (default mode for new
224 * containers may be stacking, which requires a new window to be
225 * created), it had to be delayed. */
226 expand_table_cols(TAILQ_FIRST(workspaces));
227 expand_table_rows(TAILQ_FIRST(workspaces));
229 /* Place requests for the atoms we need as soon as possible */
230 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
232 REQUEST_ATOM(_NET_SUPPORTED);
233 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
234 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
235 REQUEST_ATOM(_NET_WM_NAME);
236 REQUEST_ATOM(_NET_WM_STATE);
237 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
238 REQUEST_ATOM(_NET_WM_DESKTOP);
239 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
240 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
241 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
242 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
243 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
244 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
245 REQUEST_ATOM(WM_PROTOCOLS);
246 REQUEST_ATOM(WM_DELETE_WINDOW);
247 REQUEST_ATOM(UTF8_STRING);
248 REQUEST_ATOM(WM_STATE);
249 REQUEST_ATOM(WM_CLIENT_LEADER);
250 REQUEST_ATOM(_NET_CURRENT_DESKTOP);
251 REQUEST_ATOM(_NET_ACTIVE_WINDOW);
252 REQUEST_ATOM(_NET_WORKAREA);
254 /* TODO: this has to be more beautiful somewhen */
255 int major, minor, error;
257 major = XkbMajorVersion;
258 minor = XkbMinorVersion;
262 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
263 ELOG("ERROR: XkbOpenDisplay() failed, disabling XKB support\n");
264 xkb_supported = false;
268 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
269 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
274 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
275 fprintf(stderr, "XKB not supported by X-server\n");
278 /* end of ugliness */
280 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd, XkbMapNotifyMask, XkbMapNotifyMask)) {
281 fprintf(stderr, "Could not set XKB event mask\n");
286 /* Initialize event loop using libev */
287 struct ev_loop *loop = ev_default_loop(0);
289 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
291 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
292 struct ev_io *xkb = scalloc(sizeof(struct ev_io));
293 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
294 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
296 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
297 ev_io_start(loop, xcb_watcher);
300 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
301 ev_io_start(loop, xkb);
303 /* Flush the buffer so that libev can properly get new events */
307 ev_check_init(xcb_check, xcb_check_cb);
308 ev_check_start(loop, xcb_check);
310 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
311 ev_prepare_start(loop, xcb_prepare);
313 /* Grab the server to delay any events until we enter the eventloop */
314 xcb_grab_server(conn);
316 xcb_event_handlers_init(conn, &evenths);
318 /* DEBUG: Trap all events and print them */
319 for (i = 2; i < 128; ++i)
320 xcb_event_set_handler(&evenths, i, handle_event, 0);
322 for (i = 0; i < 256; ++i)
323 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
325 /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
326 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
328 /* Key presses/releases are pretty obvious, I think */
329 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
330 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
332 /* Enter window = user moved his mouse over the window */
333 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
335 /* Button press = user pushed a mouse button over one of our windows */
336 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
338 /* Map notify = there is a new window */
339 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
341 /* Unmap notify = window disappeared. When sent from a client, we don’t manage
342 it any longer. Usually, the client destroys the window shortly afterwards. */
343 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
345 /* Configure notify = window’s configuration (geometry, stacking, …). We only need
346 it to set up ignore the following enter_notify events */
347 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
349 /* Configure request = window tried to change size on its own */
350 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
352 /* Motion notify = user moved his cursor (over the root window and may
353 * cross virtual screen boundaries doing that) */
354 xcb_event_set_motion_notify_handler(&evenths, handle_motion_notify, NULL);
356 /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
357 xcb_event_set_mapping_notify_handler(&evenths, handle_mapping_notify, NULL);
359 /* Client message are sent to the root window. The only interesting client message
360 for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
361 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
363 /* Initialize the property handlers */
364 xcb_property_handlers_init(&prophs, &evenths);
366 /* Watch size hints (to obey correct aspect ratio) */
367 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
369 /* Get the root window and set the event mask */
370 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
371 root = root_screen->root;
372 root_depth = root_screen->root_depth;
374 uint32_t mask = XCB_CW_EVENT_MASK;
375 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
376 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
377 projector), the root window gets a
379 XCB_EVENT_MASK_POINTER_MOTION |
380 XCB_EVENT_MASK_PROPERTY_CHANGE |
381 XCB_EVENT_MASK_ENTER_WINDOW };
382 xcb_void_cookie_t cookie;
383 cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
384 check_error(conn, cookie, "Another window manager seems to be running");
386 /* Setup NetWM atoms */
387 #define GET_ATOM(name) { \
388 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
390 ELOG("Could not get atom " #name "\n"); \
393 atoms[name] = reply->atom; \
397 GET_ATOM(_NET_SUPPORTED);
398 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
399 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
400 GET_ATOM(_NET_WM_NAME);
401 GET_ATOM(_NET_WM_STATE);
402 GET_ATOM(_NET_WM_WINDOW_TYPE);
403 GET_ATOM(_NET_WM_DESKTOP);
404 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
405 GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
406 GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
407 GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
408 GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
409 GET_ATOM(_NET_WM_STRUT_PARTIAL);
410 GET_ATOM(WM_PROTOCOLS);
411 GET_ATOM(WM_DELETE_WINDOW);
412 GET_ATOM(UTF8_STRING);
414 GET_ATOM(WM_CLIENT_LEADER);
415 GET_ATOM(_NET_CURRENT_DESKTOP);
416 GET_ATOM(_NET_ACTIVE_WINDOW);
417 GET_ATOM(_NET_WORKAREA);
419 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
420 /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
422 /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
423 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
425 /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
426 xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
428 /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
429 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
431 /* Watch WM_CLASS (= class of the window) */
432 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
434 /* Watch WM_CLIENT_LEADER (= logical parent window for toolbars etc.) */
435 xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
437 /* Watch WM_HINTS (contains the urgent property) */
438 xcb_property_set_handler(&prophs, WM_HINTS, UINT_MAX, handle_hints, NULL);
440 /* Set up the atoms we support */
441 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
442 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
443 /* Set up the window manager’s name */
444 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
445 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
447 keysyms = xcb_key_symbols_alloc(conn);
449 xcb_get_numlock_mask(conn);
453 /* Autostarting exec-lines */
454 struct Autostart *exec;
456 TAILQ_FOREACH(exec, &autostarts, autostarts) {
457 LOG("auto-starting %s\n", exec->command);
458 start_application(exec->command);
462 /* check for Xinerama */
463 DLOG("Checking for Xinerama...\n");
464 initialize_xinerama(conn);
468 /* Get pointer position to see on which screen we’re starting */
469 xcb_query_pointer_reply_t *reply;
470 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
471 ELOG("Could not get pointer position\n");
475 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
476 if (screen == NULL) {
477 ELOG("ERROR: No screen at %d x %d, starting on the first screen\n",
478 reply->root_x, reply->root_y);
479 screen = TAILQ_FIRST(virtual_screens);
482 DLOG("Starting on %d\n", screen->current_workspace);
483 c_ws = screen->current_workspace;
485 manage_existing_windows(conn, &prophs, root);
487 /* Create the UNIX domain socket for IPC */
488 if (config.ipc_socket_path != NULL) {
489 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
490 if (ipc_socket == -1) {
491 ELOG("Could not create the IPC socket, IPC disabled\n");
493 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
494 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
495 ev_io_start(loop, ipc_io);
499 /* Handle the events which arrived until now */
500 xcb_check_cb(NULL, NULL, 0);
502 /* Ungrab the server to receive events and enter libev’s eventloop */
503 xcb_ungrab_server(conn);