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 * Callback for activity on the connection to the X server
78 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
79 xcb_generic_event_t *event;
81 /* When an event is available… */
82 while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
83 /* …we handle all events in a row: */
85 xcb_event_handle(&evenths, event);
86 xcb_aux_sync(evenths.c);
88 } while ((event = xcb_poll_for_event(evenths.c)));
90 /* Make sure all replies are handled/discarded */
91 xcb_aux_sync(evenths.c);
93 /* Afterwards, there may be new events available which would
94 * not trigger the select() (libev) immediately, so we check
95 * again (and don’t bail out of the loop). */
98 /* Make sure all replies are handled/discarded */
99 xcb_aux_sync(evenths.c);
102 int main(int argc, char *argv[], char *env[]) {
104 char *override_configpath = NULL;
105 bool autostart = true;
106 xcb_connection_t *conn;
107 xcb_property_handlers_t prophs;
109 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
111 setlocale(LC_ALL, "");
113 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
114 if (!isatty(fileno(stdout)))
115 setbuf(stdout, NULL);
119 while ((opt = getopt(argc, argv, "c:va")) != -1) {
122 LOG("Autostart disabled using -a\n");
126 override_configpath = sstrdup(optarg);
129 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
132 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
137 LOG("i3 version " I3_VERSION " starting\n");
139 /* Initialize the table data structures for each workspace */
142 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
143 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
145 conn = xcb_connect(NULL, &screens);
147 if (xcb_connection_has_error(conn))
148 die("Cannot open display\n");
150 load_configuration(conn, override_configpath);
152 /* Place requests for the atoms we need as soon as possible */
153 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
155 REQUEST_ATOM(_NET_SUPPORTED);
156 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
157 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
158 REQUEST_ATOM(_NET_WM_NAME);
159 REQUEST_ATOM(_NET_WM_STATE);
160 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
161 REQUEST_ATOM(_NET_WM_DESKTOP);
162 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
163 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
164 REQUEST_ATOM(WM_PROTOCOLS);
165 REQUEST_ATOM(WM_DELETE_WINDOW);
166 REQUEST_ATOM(UTF8_STRING);
167 REQUEST_ATOM(WM_STATE);
169 /* TODO: this has to be more beautiful somewhen */
170 int major, minor, error;
172 major = XkbMajorVersion;
173 minor = XkbMinorVersion;
177 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
178 fprintf(stderr, "XkbOpenDisplay() failed\n");
183 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
184 fprintf(stderr, "XKB not supported by X-server\n");
187 /* end of ugliness */
189 xcb_event_handlers_init(conn, &evenths);
191 /* DEBUG: Trap all events and print them */
192 for (i = 2; i < 128; ++i)
193 xcb_event_set_handler(&evenths, i, handle_event, 0);
195 for (i = 0; i < 256; ++i)
196 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
198 /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
199 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
201 /* Key presses/releases are pretty obvious, I think */
202 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
203 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
205 /* Enter window = user moved his mouse over the window */
206 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
208 /* Button press = user pushed a mouse button over one of our windows */
209 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
211 /* Map notify = there is a new window */
212 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
214 /* Unmap notify = window disappeared. When sent from a client, we don’t manage
215 it any longer. Usually, the client destroys the window shortly afterwards. */
216 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
218 /* Configure notify = window’s configuration (geometry, stacking, …). We only need
219 it to set up ignore the following enter_notify events */
220 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
222 /* Configure request = window tried to change size on its own */
223 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
225 /* Client message are sent to the root window. The only interesting client message
226 for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
227 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
229 /* Initialize the property handlers */
230 xcb_property_handlers_init(&prophs, &evenths);
232 /* Watch size hints (to obey correct aspect ratio) */
233 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
235 /* Get the root window and set the event mask */
236 root = xcb_aux_get_screen(conn, screens)->root;
238 uint32_t mask = XCB_CW_EVENT_MASK;
239 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
240 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
241 projector), the root window gets a
243 XCB_EVENT_MASK_PROPERTY_CHANGE |
244 XCB_EVENT_MASK_ENTER_WINDOW };
245 xcb_change_window_attributes(conn, root, mask, values);
247 /* Setup NetWM atoms */
248 #define GET_ATOM(name) { \
249 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
251 LOG("Could not get atom " #name "\n"); \
254 atoms[name] = reply->atom; \
258 GET_ATOM(_NET_SUPPORTED);
259 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
260 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
261 GET_ATOM(_NET_WM_NAME);
262 GET_ATOM(_NET_WM_STATE);
263 GET_ATOM(_NET_WM_WINDOW_TYPE);
264 GET_ATOM(_NET_WM_DESKTOP);
265 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
266 GET_ATOM(_NET_WM_STRUT_PARTIAL);
267 GET_ATOM(WM_PROTOCOLS);
268 GET_ATOM(WM_DELETE_WINDOW);
269 GET_ATOM(UTF8_STRING);
272 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
273 /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
275 /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
276 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
278 /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
279 xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
281 /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
282 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
284 /* Watch WM_CLASS (= class of the window) */
285 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
287 /* Set up the atoms we support */
288 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
289 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
290 /* Set up the window manager’s name */
291 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
292 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
294 xcb_get_numlock_mask(conn);
296 /* Grab the bound keys */
298 TAILQ_FOREACH(bind, &bindings, bindings) {
299 LOG("Grabbing %d\n", bind->keycode);
300 if (bind->mods & BIND_MODE_SWITCH)
301 xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
303 /* Grab the key in all combinations */
304 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
305 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
306 GRAB_KEY(bind->mods);
307 GRAB_KEY(bind->mods | xcb_numlock_mask);
308 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
312 /* Autostarting exec-lines */
313 struct Autostart *exec;
315 TAILQ_FOREACH(exec, &autostarts, autostarts) {
316 LOG("auto-starting %s\n", exec->command);
317 start_application(exec->command);
321 /* check for Xinerama */
322 LOG("Checking for Xinerama...\n");
323 initialize_xinerama(conn);
327 manage_existing_windows(conn, &prophs, root);
329 /* Get pointer position to see on which screen we’re starting */
330 xcb_query_pointer_reply_t *reply;
331 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
332 LOG("Could not get pointer position\n");
336 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
337 if (screen == NULL) {
338 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
341 if (screen->current_workspace != 0) {
342 LOG("Ok, I need to go to the other workspace\n");
343 c_ws = &workspaces[screen->current_workspace];
347 /* Initialize event loop using libev */
348 struct ev_loop *loop = ev_default_loop(0);
350 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
352 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
353 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
355 /* Call the handler to work all events which arrived before the libev-stuff was set up */
356 xcb_got_event(NULL, xcb_watcher, 0);
358 /* Enter the libev eventloop */
359 ev_io_start(loop, xcb_watcher);