2 * vim:ts=4:sw=4:expandtab
10 static int xkb_event_base;
12 int xkb_current_group;
18 xcb_connection_t *conn;
20 xcb_screen_t *root_screen;
24 struct ev_loop *main_loop;
26 xcb_key_symbols_t *keysyms;
28 /* Those are our connections to X11 for use with libXcursor and XKB */
29 Display *xlibdpy, *xkbdpy;
31 /* The list of key bindings */
32 struct bindings_head *bindings;
34 /* The list of exec-lines */
35 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
37 /* The list of exec_always lines */
38 struct autostarts_always_head autostarts_always = TAILQ_HEAD_INITIALIZER(autostarts_always);
40 /* The list of assignments */
41 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
43 /* The list of workspace assignments (which workspace should end up on which
45 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
47 /* We hope that those are supported and set them to true */
48 bool xcursor_supported = true;
49 bool xkb_supported = true;
52 * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
53 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
56 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
57 /* empty, because xcb_prepare_cb and xcb_check_cb are used */
61 * Flush before blocking (and waiting for new events)
64 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
69 * Instead of polling the X connection socket we leave this to
70 * xcb_poll_for_event() which knows better than we can ever know.
73 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
74 xcb_generic_event_t *event;
76 while ((event = xcb_poll_for_event(conn)) != NULL) {
77 if (event->response_type == 0) {
78 if (event_is_ignored(event->sequence, 0))
79 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
81 xcb_generic_error_t *error = (xcb_generic_error_t*)event;
82 ELOG("X11 Error received! sequence 0x%x, error_code = %d\n",
83 error->sequence, error->error_code);
89 /* Strip off the highest bit (set if the event is generated) */
90 int type = (event->response_type & 0x7F);
92 handle_event(type, event);
100 * When using xmodmap to change the keyboard mapping, this event
101 * is only sent via XKB. Therefore, we need this special handler.
104 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
105 DLOG("Handling XKB event\n");
108 /* When using xmodmap, every change (!) gets an own event.
109 * Therefore, we just read all events and only handle the
110 * mapping_notify once. */
111 bool mapping_changed = false;
112 while (XPending(xkbdpy)) {
113 XNextEvent(xkbdpy, (XEvent*)&ev);
114 /* While we should never receive a non-XKB event,
115 * better do sanity checking */
116 if (ev.type != xkb_event_base)
119 if (ev.any.xkb_type == XkbMapNotify) {
120 mapping_changed = true;
124 if (ev.any.xkb_type != XkbStateNotify) {
125 ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
129 /* See The XKB Extension: Library Specification, section 14.1 */
130 /* We check if the current group (each group contains
131 * two levels) has been changed. Mode_switch activates
132 * group XkbGroup2Index */
133 if (xkb_current_group == ev.state.group)
136 xkb_current_group = ev.state.group;
138 if (ev.state.group == XkbGroup2Index) {
139 DLOG("Mode_switch enabled\n");
140 grab_all_keys(conn, true);
143 if (ev.state.group == XkbGroup1Index) {
144 DLOG("Mode_switch disabled\n");
145 ungrab_all_keys(conn);
146 grab_all_keys(conn, false);
150 if (!mapping_changed)
153 DLOG("Keyboard mapping changed, updating keybindings\n");
154 xcb_key_symbols_free(keysyms);
155 keysyms = xcb_key_symbols_alloc(conn);
157 xcb_get_numlock_mask(conn);
159 ungrab_all_keys(conn);
160 DLOG("Re-grabbing...\n");
162 grab_all_keys(conn, (xkb_current_group == XkbGroup2Index));
167 * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
170 static void i3_exit() {
171 ev_loop_destroy(main_loop);
174 int main(int argc, char *argv[]) {
176 char *override_configpath = NULL;
177 bool autostart = true;
178 char *layout_path = NULL;
179 bool delete_layout_path = false;
180 bool only_check_config = false;
181 bool force_xinerama = false;
182 bool disable_signalhandler = false;
183 static struct option long_options[] = {
184 {"no-autostart", no_argument, 0, 'a'},
185 {"config", required_argument, 0, 'c'},
186 {"version", no_argument, 0, 'v'},
187 {"help", no_argument, 0, 'h'},
188 {"layout", required_argument, 0, 'L'},
189 {"restart", required_argument, 0, 0},
190 {"force-xinerama", no_argument, 0, 0},
191 {"disable-signalhandler", no_argument, 0, 0},
192 {"get-socketpath", no_argument, 0, 0},
195 int option_index = 0, opt;
197 setlocale(LC_ALL, "");
199 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
200 if (!isatty(fileno(stdout)))
201 setbuf(stdout, NULL);
207 while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
210 LOG("Autostart disabled using -a\n");
215 layout_path = sstrdup(optarg);
216 delete_layout_path = false;
219 FREE(override_configpath);
220 override_configpath = sstrdup(optarg);
223 LOG("Checking configuration file only (-C)\n");
224 only_check_config = true;
227 printf("i3 version " I3_VERSION " © 2009-2011 Michael Stapelberg and contributors\n");
233 LOG("Enabling debug loglevel %s\n", optarg);
234 add_loglevel(optarg);
237 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
240 if (strcmp(long_options[option_index].name, "force-xinerama") == 0) {
241 force_xinerama = true;
242 ELOG("Using Xinerama instead of RandR. This option should be "
243 "avoided at all cost because it does not refresh the list "
244 "of screens, so you cannot configure displays at runtime. "
245 "Please check if your driver really does not support RandR "
246 "and disable this option as soon as you can.\n");
248 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
249 disable_signalhandler = true;
251 } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0) {
252 char *socket_path = socket_path_from_x11();
254 printf("%s\n", socket_path);
259 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
261 layout_path = sstrdup(optarg);
262 delete_layout_path = true;
267 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
268 fprintf(stderr, "\n");
269 fprintf(stderr, "\t-a disable autostart ('exec' lines in config)\n");
270 fprintf(stderr, "\t-c <file> use the provided configfile instead\n");
271 fprintf(stderr, "\t-C validate configuration file and exit\n");
272 fprintf(stderr, "\t-d <level> enable debug output with the specified loglevel\n");
273 fprintf(stderr, "\t-L <file> path to the serialized layout during restarts\n");
274 fprintf(stderr, "\t-v display version and exit\n");
275 fprintf(stderr, "\t-V enable verbose mode\n");
276 fprintf(stderr, "\n");
277 fprintf(stderr, "\t--force-xinerama\n"
278 "\tUse Xinerama instead of RandR.\n"
279 "\tThis option should only be used if you are stuck with the\n"
280 "\tnvidia closed source driver which does not support RandR.\n");
281 fprintf(stderr, "\n");
282 fprintf(stderr, "\t--get-socketpath\n"
283 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
288 LOG("i3 (tree) version " I3_VERSION " starting\n");
290 conn = xcb_connect(NULL, &screens);
291 if (xcb_connection_has_error(conn))
292 errx(EXIT_FAILURE, "Cannot open display\n");
294 /* Initialize the libev event loop. This needs to be done before loading
295 * the config file because the parser will install an ev_child watcher
296 * for the nagbar when config errors are found. */
297 main_loop = EV_DEFAULT;
298 if (main_loop == NULL)
299 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
301 root_screen = xcb_aux_get_screen(conn, screens);
302 root = root_screen->root;
303 root_depth = root_screen->root_depth;
304 xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
305 xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
307 load_configuration(conn, override_configpath, false);
308 if (only_check_config) {
309 LOG("Done checking configuration file. Exiting.\n");
313 if (config.ipc_socket_path == NULL) {
314 /* Fall back to a file name in /tmp/ based on the PID */
315 if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
316 config.ipc_socket_path = get_process_filename("ipc-socket");
318 config.ipc_socket_path = sstrdup(config.ipc_socket_path);
321 uint32_t mask = XCB_CW_EVENT_MASK;
322 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
323 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
324 projector), the root window gets a
326 XCB_EVENT_MASK_POINTER_MOTION |
327 XCB_EVENT_MASK_PROPERTY_CHANGE |
328 XCB_EVENT_MASK_ENTER_WINDOW };
329 xcb_void_cookie_t cookie;
330 cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
331 check_error(conn, cookie, "Another window manager seems to be running");
333 xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
334 if (greply == NULL) {
335 ELOG("Could not get geometry of the root window, exiting\n");
338 DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
340 /* Place requests for the atoms we need as soon as possible */
341 #define xmacro(atom) \
342 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
343 #include "atoms.xmacro"
346 /* Initialize the Xlib connection */
347 xlibdpy = xkbdpy = XOpenDisplay(NULL);
349 /* Try to load the X cursors and initialize the XKB extension */
350 if (xlibdpy == NULL) {
351 ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
352 xcursor_supported = false;
353 xkb_supported = false;
354 } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
355 ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
358 xcursor_load_cursors();
362 /* Set a cursor for the root window (otherwise the root window will show no
363 cursor until the first client is launched). */
364 if (xcursor_supported) {
365 xcursor_set_root_cursor();
367 xcb_cursor_t cursor_id = xcb_generate_id(conn);
368 i3Font cursor_font = load_font("cursor", false);
369 int xcb_cursor = xcursor_get_xcb_cursor(XCURSOR_CURSOR_POINTER);
370 xcb_create_glyph_cursor(conn, cursor_id, cursor_font.id, cursor_font.id,
371 xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535);
372 xcb_change_window_attributes(conn, root, XCB_CW_CURSOR, &cursor_id);
373 xcb_free_cursor(conn, cursor_id);
378 major = XkbMajorVersion,
379 minor = XkbMinorVersion;
381 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
382 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
387 if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
388 fprintf(stderr, "XKB not supported by X-server\n");
391 /* end of ugliness */
393 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
394 XkbMapNotifyMask | XkbStateNotifyMask,
395 XkbMapNotifyMask | XkbStateNotifyMask)) {
396 fprintf(stderr, "Could not set XKB event mask\n");
401 /* Setup NetWM atoms */
402 #define xmacro(name) \
404 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
406 ELOG("Could not get atom " #name "\n"); \
409 A_ ## name = reply->atom; \
412 #include "atoms.xmacro"
415 property_handlers_init();
417 /* Set up the atoms we support */
418 xcb_atom_t supported_atoms[] = {
419 #define xmacro(atom) A_ ## atom,
420 #include "atoms.xmacro"
423 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, 16, supported_atoms);
424 /* Set up the window manager’s name */
425 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &root);
426 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
428 keysyms = xcb_key_symbols_alloc(conn);
430 xcb_get_numlock_mask(conn);
433 grab_all_keys(conn, false);
435 bool needs_tree_init = true;
437 LOG("Trying to restore the layout from %s...", layout_path);
438 needs_tree_init = !tree_restore(layout_path, greply);
439 if (delete_layout_path)
448 /* Force Xinerama (for drivers which don't support RandR yet, esp. the
449 * nVidia binary graphics driver), when specified either in the config
450 * file or on command-line */
451 if (force_xinerama || config.force_xinerama) {
454 DLOG("Checking for XRandR...\n");
455 randr_init(&randr_base);
458 xcb_query_pointer_reply_t *pointerreply;
459 Output *output = NULL;
460 if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
461 ELOG("Could not query pointer position, using first screen\n");
462 output = get_first_output();
464 DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
465 output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
467 ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
468 pointerreply->root_x, pointerreply->root_y);
469 output = get_first_output();
472 con_focus(con_descend_focused(output_get_content(output->con)));
477 /* Create the UNIX domain socket for IPC */
478 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
479 if (ipc_socket == -1) {
480 ELOG("Could not create the IPC socket, IPC disabled\n");
482 free(config.ipc_socket_path);
483 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
484 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
485 ev_io_start(main_loop, ipc_io);
488 /* Also handle the UNIX domain sockets passed via socket activation */
489 int fds = sd_listen_fds(1);
491 ELOG("socket activation: Error in sd_listen_fds\n");
493 DLOG("socket activation: no sockets passed\n");
495 for (int fd = SD_LISTEN_FDS_START; fd < (SD_LISTEN_FDS_START + fds); fd++) {
496 DLOG("socket activation: also listening on fd %d\n", fd);
497 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
498 ev_io_init(ipc_io, ipc_new_client, fd, EV_READ);
499 ev_io_start(main_loop, ipc_io);
503 /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
506 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
507 struct ev_io *xkb = scalloc(sizeof(struct ev_io));
508 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
509 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
511 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
512 ev_io_start(main_loop, xcb_watcher);
516 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
517 ev_io_start(main_loop, xkb);
519 /* Flush the buffer so that libev can properly get new events */
523 ev_check_init(xcb_check, xcb_check_cb);
524 ev_check_start(main_loop, xcb_check);
526 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
527 ev_prepare_start(main_loop, xcb_prepare);
531 manage_existing_windows(root);
533 if (!disable_signalhandler)
534 setup_signal_handler();
536 /* Ignore SIGPIPE to survive errors when an IPC client disconnects
537 * while we are sending him a message */
538 signal(SIGPIPE, SIG_IGN);
540 /* Autostarting exec-lines */
542 struct Autostart *exec;
543 TAILQ_FOREACH(exec, &autostarts, autostarts) {
544 LOG("auto-starting %s\n", exec->command);
545 start_application(exec->command);
549 /* Autostarting exec_always-lines */
550 struct Autostart *exec_always;
551 TAILQ_FOREACH(exec_always, &autostarts_always, autostarts_always) {
552 LOG("auto-starting (always!) %s\n", exec_always->command);
553 start_application(exec_always->command);
556 /* Make sure to destroy the event loop to invoke the cleeanup callbacks
557 * when calling exit() */
560 ev_loop(main_loop, 0);