4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
15 #include <sys/types.h>
22 #include <X11/XKBlib.h>
23 #include <X11/extensions/XKB.h>
26 #include <xcb/xcb_atom.h>
27 #include <xcb/xcb_aux.h>
28 #include <xcb/xcb_event.h>
29 #include <xcb/xcb_property.h>
30 #include <xcb/xcb_keysyms.h>
31 #include <xcb/xcb_icccm.h>
32 #include <xcb/xinerama.h>
49 /* This is the path to i3, copied from argv[0] when starting up */
52 /* This is our connection to X11 for use with XKB */
55 /* The list of key bindings */
56 struct bindings_head bindings = TAILQ_HEAD_INITIALIZER(bindings);
58 /* The list of exec-lines */
59 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
61 /* The list of assignments */
62 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
64 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
65 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
67 /* The event handlers need to be global because they are accessed by our custom event handler
68 in handle_button_press(), needed for graphical resizing */
69 xcb_event_handlers_t evenths;
70 xcb_atom_t atoms[NUM_ATOMS];
75 * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
76 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
79 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
80 /* empty, because xcb_prepare_cb and xcb_check_cb are used */
84 * Flush before blocking (and waiting for new events)
87 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
92 * Instead of polling the X connection socket we leave this to
93 * xcb_poll_for_event() which knows better than we can ever know.
96 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
97 xcb_generic_event_t *event;
99 while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
100 xcb_event_handle(&evenths, event);
105 int main(int argc, char *argv[], char *env[]) {
107 char *override_configpath = NULL;
108 bool autostart = true;
109 xcb_connection_t *conn;
110 xcb_property_handlers_t prophs;
112 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
114 setlocale(LC_ALL, "");
116 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
117 if (!isatty(fileno(stdout)))
118 setbuf(stdout, NULL);
122 while ((opt = getopt(argc, argv, "c:va")) != -1) {
125 LOG("Autostart disabled using -a\n");
129 override_configpath = sstrdup(optarg);
132 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
135 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
140 LOG("i3 version " I3_VERSION " starting\n");
142 /* Initialize the table data structures for each workspace */
145 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
146 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
148 conn = xcb_connect(NULL, &screens);
150 if (xcb_connection_has_error(conn))
151 die("Cannot open display\n");
153 load_configuration(conn, override_configpath);
155 /* Place requests for the atoms we need as soon as possible */
156 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
158 REQUEST_ATOM(_NET_SUPPORTED);
159 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
160 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
161 REQUEST_ATOM(_NET_WM_NAME);
162 REQUEST_ATOM(_NET_WM_STATE);
163 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
164 REQUEST_ATOM(_NET_WM_DESKTOP);
165 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
166 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
167 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
168 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
169 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
170 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
171 REQUEST_ATOM(WM_PROTOCOLS);
172 REQUEST_ATOM(WM_DELETE_WINDOW);
173 REQUEST_ATOM(UTF8_STRING);
174 REQUEST_ATOM(WM_STATE);
176 /* TODO: this has to be more beautiful somewhen */
177 int major, minor, error;
179 major = XkbMajorVersion;
180 minor = XkbMinorVersion;
184 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
185 fprintf(stderr, "XkbOpenDisplay() failed\n");
190 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
191 fprintf(stderr, "XKB not supported by X-server\n");
194 /* end of ugliness */
196 /* Initialize event loop using libev */
197 struct ev_loop *loop = ev_default_loop(0);
199 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
201 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
202 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
203 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
205 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
206 ev_io_start(loop, xcb_watcher);
208 ev_check_init(xcb_check, xcb_check_cb);
209 ev_check_start(loop, xcb_check);
211 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
212 ev_prepare_start(loop, xcb_prepare);
214 /* Grab the server to delay any events until we enter the eventloop */
215 xcb_grab_server(conn);
217 xcb_event_handlers_init(conn, &evenths);
219 /* DEBUG: Trap all events and print them */
220 for (i = 2; i < 128; ++i)
221 xcb_event_set_handler(&evenths, i, handle_event, 0);
223 for (i = 0; i < 256; ++i)
224 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
226 /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
227 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
229 /* Key presses/releases are pretty obvious, I think */
230 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
231 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
233 /* Enter window = user moved his mouse over the window */
234 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
236 /* Button press = user pushed a mouse button over one of our windows */
237 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
239 /* Map notify = there is a new window */
240 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
242 /* Unmap notify = window disappeared. When sent from a client, we don’t manage
243 it any longer. Usually, the client destroys the window shortly afterwards. */
244 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
246 /* Configure notify = window’s configuration (geometry, stacking, …). We only need
247 it to set up ignore the following enter_notify events */
248 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
250 /* Configure request = window tried to change size on its own */
251 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
253 /* Client message are sent to the root window. The only interesting client message
254 for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
255 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
257 /* Initialize the property handlers */
258 xcb_property_handlers_init(&prophs, &evenths);
260 /* Watch size hints (to obey correct aspect ratio) */
261 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
263 /* Get the root window and set the event mask */
264 root = xcb_aux_get_screen(conn, screens)->root;
266 uint32_t mask = XCB_CW_EVENT_MASK;
267 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
268 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
269 projector), the root window gets a
271 XCB_EVENT_MASK_PROPERTY_CHANGE |
272 XCB_EVENT_MASK_ENTER_WINDOW };
273 xcb_change_window_attributes(conn, root, mask, values);
275 /* Setup NetWM atoms */
276 #define GET_ATOM(name) { \
277 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
279 LOG("Could not get atom " #name "\n"); \
282 atoms[name] = reply->atom; \
286 GET_ATOM(_NET_SUPPORTED);
287 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
288 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
289 GET_ATOM(_NET_WM_NAME);
290 GET_ATOM(_NET_WM_STATE);
291 GET_ATOM(_NET_WM_WINDOW_TYPE);
292 GET_ATOM(_NET_WM_DESKTOP);
293 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
294 GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
295 GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
296 GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
297 GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
298 GET_ATOM(_NET_WM_STRUT_PARTIAL);
299 GET_ATOM(WM_PROTOCOLS);
300 GET_ATOM(WM_DELETE_WINDOW);
301 GET_ATOM(UTF8_STRING);
304 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
305 /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
307 /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
308 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
310 /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
311 xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
313 /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
314 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
316 /* Watch WM_CLASS (= class of the window) */
317 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
319 /* Set up the atoms we support */
320 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
321 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
322 /* Set up the window manager’s name */
323 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
324 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
326 xcb_get_numlock_mask(conn);
328 /* Grab the bound keys */
330 TAILQ_FOREACH(bind, &bindings, bindings) {
331 LOG("Grabbing %d\n", bind->keycode);
332 if (bind->mods & BIND_MODE_SWITCH)
333 xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
335 /* Grab the key in all combinations */
336 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
337 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
338 GRAB_KEY(bind->mods);
339 GRAB_KEY(bind->mods | xcb_numlock_mask);
340 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
344 /* Autostarting exec-lines */
345 struct Autostart *exec;
347 TAILQ_FOREACH(exec, &autostarts, autostarts) {
348 LOG("auto-starting %s\n", exec->command);
349 start_application(exec->command);
353 /* check for Xinerama */
354 LOG("Checking for Xinerama...\n");
355 initialize_xinerama(conn);
359 manage_existing_windows(conn, &prophs, root);
361 /* Get pointer position to see on which screen we’re starting */
362 xcb_query_pointer_reply_t *reply;
363 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
364 LOG("Could not get pointer position\n");
368 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
369 if (screen == NULL) {
370 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
373 if (screen->current_workspace != 0) {
374 LOG("Ok, I need to go to the other workspace\n");
375 c_ws = &workspaces[screen->current_workspace];
378 /* Handle the events which arrived until now */
379 xcb_check_cb(NULL, NULL, 0);
381 /* Ungrab the server to receive events and enter libev’s eventloop */
382 xcb_ungrab_server(conn);