2 * vim:ts=4:sw=4:expandtab
9 static int xkb_event_base;
11 int xkb_current_group;
17 xcb_connection_t *conn;
22 xcb_key_symbols_t *keysyms;
24 /* Those are our connections to X11 for use with libXcursor and XKB */
25 Display *xlibdpy, *xkbdpy;
27 /* The list of key bindings */
28 struct bindings_head *bindings;
30 /* The list of exec-lines */
31 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
33 /* The list of assignments */
34 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
36 /* The list of workspace assignments (which workspace should end up on which
38 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
40 struct real_assignments_head real_assignments = TAILQ_HEAD_INITIALIZER(real_assignments);
42 /* We hope that those are supported and set them to true */
43 bool xcursor_supported = true;
44 bool xkb_supported = true;
47 * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
48 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
51 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
52 /* empty, because xcb_prepare_cb and xcb_check_cb are used */
56 * Flush before blocking (and waiting for new events)
59 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
64 * Instead of polling the X connection socket we leave this to
65 * xcb_poll_for_event() which knows better than we can ever know.
68 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
69 xcb_generic_event_t *event;
71 while ((event = xcb_poll_for_event(conn)) != NULL) {
72 if (event->response_type == 0) {
73 ELOG("X11 Error received! sequence %x\n", event->sequence);
77 /* Strip off the highest bit (set if the event is generated) */
78 int type = (event->response_type & 0x7F);
80 handle_event(type, event);
88 * When using xmodmap to change the keyboard mapping, this event
89 * is only sent via XKB. Therefore, we need this special handler.
92 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
93 DLOG("Handling XKB event\n");
96 /* When using xmodmap, every change (!) gets an own event.
97 * Therefore, we just read all events and only handle the
98 * mapping_notify once. */
99 bool mapping_changed = false;
100 while (XPending(xkbdpy)) {
101 XNextEvent(xkbdpy, (XEvent*)&ev);
102 /* While we should never receive a non-XKB event,
103 * better do sanity checking */
104 if (ev.type != xkb_event_base)
107 if (ev.any.xkb_type == XkbMapNotify) {
108 mapping_changed = true;
112 if (ev.any.xkb_type != XkbStateNotify) {
113 ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
117 /* See The XKB Extension: Library Specification, section 14.1 */
118 /* We check if the current group (each group contains
119 * two levels) has been changed. Mode_switch activates
120 * group XkbGroup2Index */
121 if (xkb_current_group == ev.state.group)
124 xkb_current_group = ev.state.group;
126 if (ev.state.group == XkbGroup2Index) {
127 DLOG("Mode_switch enabled\n");
128 grab_all_keys(conn, true);
131 if (ev.state.group == XkbGroup1Index) {
132 DLOG("Mode_switch disabled\n");
133 ungrab_all_keys(conn);
134 grab_all_keys(conn, false);
138 if (!mapping_changed)
141 DLOG("Keyboard mapping changed, updating keybindings\n");
142 xcb_key_symbols_free(keysyms);
143 keysyms = xcb_key_symbols_alloc(conn);
145 xcb_get_numlock_mask(conn);
147 ungrab_all_keys(conn);
148 DLOG("Re-grabbing...\n");
150 grab_all_keys(conn, (xkb_current_group == XkbGroup2Index));
154 int main(int argc, char *argv[]) {
155 //parse_cmd("[ foo ] attach, attach ; focus");
157 char *override_configpath = NULL;
158 bool autostart = true;
159 char *layout_path = NULL;
160 bool delete_layout_path = false;
161 bool only_check_config = false;
162 bool force_xinerama = false;
163 bool disable_signalhandler = false;
164 static struct option long_options[] = {
165 {"no-autostart", no_argument, 0, 'a'},
166 {"config", required_argument, 0, 'c'},
167 {"version", no_argument, 0, 'v'},
168 {"help", no_argument, 0, 'h'},
169 {"layout", required_argument, 0, 'L'},
170 {"restart", required_argument, 0, 0},
171 {"force-xinerama", no_argument, 0, 0},
172 {"disable-signalhandler", no_argument, 0, 0},
175 int option_index = 0, opt;
177 setlocale(LC_ALL, "");
179 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
180 if (!isatty(fileno(stdout)))
181 setbuf(stdout, NULL);
185 while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
188 LOG("Autostart disabled using -a\n");
193 layout_path = sstrdup(optarg);
194 delete_layout_path = false;
197 FREE(override_configpath);
198 override_configpath = sstrdup(optarg);
201 LOG("Checking configuration file only (-C)\n");
202 only_check_config = true;
205 printf("i3 version " I3_VERSION " © 2009-2011 Michael Stapelberg and contributors\n");
211 LOG("Enabling debug loglevel %s\n", optarg);
212 add_loglevel(optarg);
215 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
218 if (strcmp(long_options[option_index].name, "force-xinerama") == 0) {
219 force_xinerama = true;
220 ELOG("Using Xinerama instead of RandR. This option should be "
221 "avoided at all cost because it does not refresh the list "
222 "of screens, so you cannot configure displays at runtime. "
223 "Please check if your driver really does not support RandR "
224 "and disable this option as soon as you can.\n");
226 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
227 disable_signalhandler = true;
229 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
231 layout_path = sstrdup(optarg);
232 delete_layout_path = true;
237 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
238 fprintf(stderr, "\n");
239 fprintf(stderr, "-a: disable autostart\n");
240 fprintf(stderr, "-L <layoutfile>: load the layout from <layoutfile>\n");
241 fprintf(stderr, "-v: display version and exit\n");
242 fprintf(stderr, "-V: enable verbose mode\n");
243 fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
244 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
245 fprintf(stderr, "-C: check configuration file and exit\n");
246 fprintf(stderr, "--force-xinerama: Use Xinerama instead of RandR. This "
247 "option should only be used if you are stuck with the "
248 "nvidia closed source driver which does not support RandR.\n");
253 LOG("i3 (tree) version " I3_VERSION " starting\n");
255 conn = xcb_connect(NULL, &screens);
256 if (xcb_connection_has_error(conn))
257 errx(EXIT_FAILURE, "Cannot open display\n");
259 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
260 root = root_screen->root;
261 root_depth = root_screen->root_depth;
263 load_configuration(conn, override_configpath, false);
264 if (only_check_config) {
265 LOG("Done checking configuration file. Exiting.\n");
269 if (config.ipc_socket_path == NULL) {
270 /* Fall back to a file name in /tmp/ based on the PID */
271 if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
272 config.ipc_socket_path = get_process_filename("ipc-socket");
274 config.ipc_socket_path = sstrdup(config.ipc_socket_path);
277 uint32_t mask = XCB_CW_EVENT_MASK;
278 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
279 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
280 projector), the root window gets a
282 XCB_EVENT_MASK_POINTER_MOTION |
283 XCB_EVENT_MASK_PROPERTY_CHANGE |
284 XCB_EVENT_MASK_ENTER_WINDOW };
285 xcb_void_cookie_t cookie;
286 cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
287 check_error(conn, cookie, "Another window manager seems to be running");
289 /* Place requests for the atoms we need as soon as possible */
290 #define xmacro(atom) \
291 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
292 #include "atoms.xmacro"
295 /* Initialize the Xlib connection */
296 xlibdpy = xkbdpy = XOpenDisplay(NULL);
298 /* Try to load the X cursors and initialize the XKB extension */
299 if (xlibdpy == NULL) {
300 ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
301 xcursor_supported = false;
302 xkb_supported = false;
303 } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
304 ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
307 xcursor_load_cursors();
313 major = XkbMajorVersion,
314 minor = XkbMinorVersion;
316 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
317 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
322 if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
323 fprintf(stderr, "XKB not supported by X-server\n");
326 /* end of ugliness */
328 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
329 XkbMapNotifyMask | XkbStateNotifyMask,
330 XkbMapNotifyMask | XkbStateNotifyMask)) {
331 fprintf(stderr, "Could not set XKB event mask\n");
336 /* Setup NetWM atoms */
337 #define xmacro(name) \
339 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
341 ELOG("Could not get atom " #name "\n"); \
344 A_ ## name = reply->atom; \
347 #include "atoms.xmacro"
350 property_handlers_init();
352 /* Set up the atoms we support */
353 xcb_atom_t supported_atoms[] = {
354 #define xmacro(atom) A_ ## atom,
355 #include "atoms.xmacro"
358 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, A_ATOM, 32, 7, supported_atoms);
359 /* Set up the window manager’s name */
360 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, A_WINDOW, 32, 1, &root);
361 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
363 keysyms = xcb_key_symbols_alloc(conn);
365 xcb_get_numlock_mask(conn);
368 grab_all_keys(conn, false);
370 bool needs_tree_init = true;
372 LOG("Trying to restore the layout from %s...", layout_path);
373 needs_tree_init = !tree_restore(layout_path);
374 if (delete_layout_path)
381 if (force_xinerama) {
384 DLOG("Checking for XRandR...\n");
385 randr_init(&randr_base);
390 struct ev_loop *loop = ev_loop_new(0);
392 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
394 /* Create the UNIX domain socket for IPC */
395 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
396 if (ipc_socket == -1) {
397 ELOG("Could not create the IPC socket, IPC disabled\n");
399 free(config.ipc_socket_path);
400 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
401 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
402 ev_io_start(loop, ipc_io);
405 /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
408 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
409 struct ev_io *xkb = scalloc(sizeof(struct ev_io));
410 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
411 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
413 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
414 ev_io_start(loop, xcb_watcher);
418 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
419 ev_io_start(loop, xkb);
421 /* Flush the buffer so that libev can properly get new events */
425 ev_check_init(xcb_check, xcb_check_cb);
426 ev_check_start(loop, xcb_check);
428 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
429 ev_prepare_start(loop, xcb_prepare);
433 manage_existing_windows(root);
435 if (!disable_signalhandler)
436 setup_signal_handler();
438 /* Ignore SIGPIPE to survive errors when an IPC client disconnects
439 * while we are sending him a message */
440 signal(SIGPIPE, SIG_IGN);
442 /* Autostarting exec-lines */
444 struct Autostart *exec;
445 TAILQ_FOREACH(exec, &autostarts, autostarts) {
446 LOG("auto-starting %s\n", exec->command);
447 start_application(exec->command);