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>
50 xcb_connection_t *global_conn;
52 /* This is the path to i3, copied from argv[0] when starting up */
55 /* This is our connection to X11 for use with XKB */
58 /* The list of key bindings */
59 struct bindings_head bindings = TAILQ_HEAD_INITIALIZER(bindings);
61 /* The list of exec-lines */
62 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
64 /* The list of assignments */
65 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
67 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
68 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
70 /* The event handlers need to be global because they are accessed by our custom event handler
71 in handle_button_press(), needed for graphical resizing */
72 xcb_event_handlers_t evenths;
73 xcb_atom_t atoms[NUM_ATOMS];
78 /* The depth of the root screen (used e.g. for creating new pixmaps later) */
82 * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
83 * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
86 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
87 /* empty, because xcb_prepare_cb and xcb_check_cb are used */
91 * Flush before blocking (and waiting for new events)
94 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
99 * Instead of polling the X connection socket we leave this to
100 * xcb_poll_for_event() which knows better than we can ever know.
103 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
104 xcb_generic_event_t *event;
106 while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
107 xcb_event_handle(&evenths, event);
112 int main(int argc, char *argv[], char *env[]) {
114 char *override_configpath = NULL;
115 bool autostart = true;
116 xcb_connection_t *conn;
117 xcb_property_handlers_t prophs;
118 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
120 setlocale(LC_ALL, "");
122 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
123 if (!isatty(fileno(stdout)))
124 setbuf(stdout, NULL);
128 while ((opt = getopt(argc, argv, "c:va")) != -1) {
131 LOG("Autostart disabled using -a\n");
135 override_configpath = sstrdup(optarg);
138 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
141 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
146 LOG("i3 version " I3_VERSION " starting\n");
148 /* Initialize the table data structures for each workspace */
151 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
152 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
154 conn = global_conn = xcb_connect(NULL, &screens);
156 if (xcb_connection_has_error(conn))
157 die("Cannot open display\n");
159 load_configuration(conn, override_configpath, false);
161 /* Place requests for the atoms we need as soon as possible */
162 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
164 REQUEST_ATOM(_NET_SUPPORTED);
165 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
166 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
167 REQUEST_ATOM(_NET_WM_NAME);
168 REQUEST_ATOM(_NET_WM_STATE);
169 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
170 REQUEST_ATOM(_NET_WM_DESKTOP);
171 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
172 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
173 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
174 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
175 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
176 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
177 REQUEST_ATOM(WM_PROTOCOLS);
178 REQUEST_ATOM(WM_DELETE_WINDOW);
179 REQUEST_ATOM(UTF8_STRING);
180 REQUEST_ATOM(WM_STATE);
181 REQUEST_ATOM(WM_CLIENT_LEADER);
183 /* TODO: this has to be more beautiful somewhen */
184 int major, minor, error;
186 major = XkbMajorVersion;
187 minor = XkbMinorVersion;
191 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
192 fprintf(stderr, "XkbOpenDisplay() failed\n");
197 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
198 fprintf(stderr, "XKB not supported by X-server\n");
201 /* end of ugliness */
203 /* Initialize event loop using libev */
204 struct ev_loop *loop = ev_default_loop(0);
206 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
208 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
209 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
210 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
212 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
213 ev_io_start(loop, xcb_watcher);
215 ev_check_init(xcb_check, xcb_check_cb);
216 ev_check_start(loop, xcb_check);
218 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
219 ev_prepare_start(loop, xcb_prepare);
221 /* Grab the server to delay any events until we enter the eventloop */
222 xcb_grab_server(conn);
224 xcb_event_handlers_init(conn, &evenths);
226 /* DEBUG: Trap all events and print them */
227 for (i = 2; i < 128; ++i)
228 xcb_event_set_handler(&evenths, i, handle_event, 0);
230 for (i = 0; i < 256; ++i)
231 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
233 /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
234 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
236 /* Key presses/releases are pretty obvious, I think */
237 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
238 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
240 /* Enter window = user moved his mouse over the window */
241 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
243 /* Button press = user pushed a mouse button over one of our windows */
244 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
246 /* Map notify = there is a new window */
247 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
249 /* Unmap notify = window disappeared. When sent from a client, we don’t manage
250 it any longer. Usually, the client destroys the window shortly afterwards. */
251 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
253 /* Configure notify = window’s configuration (geometry, stacking, …). We only need
254 it to set up ignore the following enter_notify events */
255 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
257 /* Configure request = window tried to change size on its own */
258 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
260 /* Motion notify = user moved his cursor (over the root window and may
261 * cross virtual screen boundaries doing that) */
262 xcb_event_set_motion_notify_handler(&evenths, handle_motion_notify, NULL);
264 /* Client message are sent to the root window. The only interesting client message
265 for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
266 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
268 /* Initialize the property handlers */
269 xcb_property_handlers_init(&prophs, &evenths);
271 /* Watch size hints (to obey correct aspect ratio) */
272 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
274 /* Get the root window and set the event mask */
275 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
276 root = root_screen->root;
277 root_depth = root_screen->root_depth;
279 uint32_t mask = XCB_CW_EVENT_MASK;
280 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
281 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
282 projector), the root window gets a
284 XCB_EVENT_MASK_POINTER_MOTION |
285 XCB_EVENT_MASK_PROPERTY_CHANGE |
286 XCB_EVENT_MASK_ENTER_WINDOW };
287 xcb_change_window_attributes(conn, root, mask, values);
289 /* Setup NetWM atoms */
290 #define GET_ATOM(name) { \
291 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
293 LOG("Could not get atom " #name "\n"); \
296 atoms[name] = reply->atom; \
300 GET_ATOM(_NET_SUPPORTED);
301 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
302 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
303 GET_ATOM(_NET_WM_NAME);
304 GET_ATOM(_NET_WM_STATE);
305 GET_ATOM(_NET_WM_WINDOW_TYPE);
306 GET_ATOM(_NET_WM_DESKTOP);
307 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
308 GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
309 GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
310 GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
311 GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
312 GET_ATOM(_NET_WM_STRUT_PARTIAL);
313 GET_ATOM(WM_PROTOCOLS);
314 GET_ATOM(WM_DELETE_WINDOW);
315 GET_ATOM(UTF8_STRING);
317 GET_ATOM(WM_CLIENT_LEADER);
319 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
320 /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
322 /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
323 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
325 /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
326 xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
328 /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
329 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
331 /* Watch WM_CLASS (= class of the window) */
332 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
334 /* Watch WM_CLIENT_LEADER (= logical parent window for toolbars etc.) */
335 xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
337 /* Set up the atoms we support */
338 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
339 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
340 /* Set up the window manager’s name */
341 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
342 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
344 xcb_get_numlock_mask(conn);
348 /* Autostarting exec-lines */
349 struct Autostart *exec;
351 TAILQ_FOREACH(exec, &autostarts, autostarts) {
352 LOG("auto-starting %s\n", exec->command);
353 start_application(exec->command);
357 /* check for Xinerama */
358 LOG("Checking for Xinerama...\n");
359 initialize_xinerama(conn);
363 manage_existing_windows(conn, &prophs, root);
365 /* Get pointer position to see on which screen we’re starting */
366 xcb_query_pointer_reply_t *reply;
367 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
368 LOG("Could not get pointer position\n");
372 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
373 if (screen == NULL) {
374 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
377 if (screen->current_workspace != 0) {
378 LOG("Ok, I need to go to the other workspace\n");
379 c_ws = &workspaces[screen->current_workspace];
382 /* Create the UNIX domain socket for IPC */
383 if (config.ipc_socket_path != NULL) {
384 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
385 if (ipc_socket == -1) {
386 LOG("Could not create the IPC socket, IPC disabled\n");
388 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
389 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
390 ev_io_start(loop, ipc_io);
394 /* Handle the events which arrived until now */
395 xcb_check_cb(NULL, NULL, 0);
397 /* Ungrab the server to receive events and enter libev’s eventloop */
398 xcb_ungrab_server(conn);