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>
47 /* This is the path to i3, copied from argv[0] when starting up */
50 /* This is our connection to X11 for use with XKB */
53 /* The list of key bindings */
54 struct bindings_head bindings = TAILQ_HEAD_INITIALIZER(bindings);
56 /* The list of exec-lines */
57 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
59 /* The list of assignments */
60 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
62 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
63 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
65 /* The event handlers need to be global because they are accessed by our custom event handler
66 in handle_button_press(), needed for graphical resizing */
67 xcb_event_handlers_t evenths;
68 xcb_atom_t atoms[NUM_ATOMS];
72 int main(int argc, char *argv[], char *env[]) {
74 char *override_configpath = NULL;
75 bool autostart = true;
76 xcb_connection_t *conn;
77 xcb_property_handlers_t prophs;
79 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
81 setlocale(LC_ALL, "");
83 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
84 if (!isatty(fileno(stdout)))
89 while ((opt = getopt(argc, argv, "c:va")) != -1) {
92 LOG("Autostart disabled using -a\n");
96 override_configpath = sstrdup(optarg);
99 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
102 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
107 LOG("i3 version " I3_VERSION " starting\n");
109 /* Initialize the table data structures for each workspace */
112 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
113 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
115 conn = xcb_connect(NULL, &screens);
117 load_configuration(conn, override_configpath);
119 /* Place requests for the atoms we need as soon as possible */
120 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
122 REQUEST_ATOM(_NET_SUPPORTED);
123 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
124 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
125 REQUEST_ATOM(_NET_WM_NAME);
126 REQUEST_ATOM(_NET_WM_STATE);
127 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
128 REQUEST_ATOM(_NET_WM_DESKTOP);
129 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
130 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
131 REQUEST_ATOM(WM_PROTOCOLS);
132 REQUEST_ATOM(WM_DELETE_WINDOW);
133 REQUEST_ATOM(UTF8_STRING);
134 REQUEST_ATOM(WM_STATE);
136 /* TODO: this has to be more beautiful somewhen */
137 int major, minor, error;
139 major = XkbMajorVersion;
140 minor = XkbMinorVersion;
144 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
145 fprintf(stderr, "XkbOpenDisplay() failed\n");
150 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
151 fprintf(stderr, "XKB not supported by X-server\n");
154 /* end of ugliness */
156 xcb_event_handlers_init(conn, &evenths);
158 /* DEBUG: Trap all events and print them */
159 for (i = 2; i < 128; ++i)
160 xcb_event_set_handler(&evenths, i, handle_event, 0);
162 for (i = 0; i < 256; ++i)
163 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
165 /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
166 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
168 /* Key presses/releases are pretty obvious, I think */
169 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
170 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
172 /* Enter window = user moved his mouse over the window */
173 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
175 /* Button press = user pushed a mouse button over one of our windows */
176 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
178 /* Map notify = there is a new window */
179 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
181 /* Unmap notify = window disappeared. When sent from a client, we don’t manage
182 it any longer. Usually, the client destroys the window shortly afterwards. */
183 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
185 /* Configure notify = window’s configuration (geometry, stacking, …). We only need
186 it to set up ignore the following enter_notify events */
187 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
189 /* Configure request = window tried to change size on its own */
190 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
192 /* Client message are sent to the root window. The only interesting client message
193 for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
194 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
196 /* Initialize the property handlers */
197 xcb_property_handlers_init(&prophs, &evenths);
199 /* Watch size hints (to obey correct aspect ratio) */
200 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
202 /* Get the root window and set the event mask */
203 root = xcb_aux_get_screen(conn, screens)->root;
205 uint32_t mask = XCB_CW_EVENT_MASK;
206 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
207 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
208 projector), the root window gets a
210 XCB_EVENT_MASK_PROPERTY_CHANGE |
211 XCB_EVENT_MASK_ENTER_WINDOW };
212 xcb_change_window_attributes(conn, root, mask, values);
214 /* Setup NetWM atoms */
215 #define GET_ATOM(name) { \
216 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
218 LOG("Could not get atom " #name "\n"); \
221 atoms[name] = reply->atom; \
225 GET_ATOM(_NET_SUPPORTED);
226 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
227 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
228 GET_ATOM(_NET_WM_NAME);
229 GET_ATOM(_NET_WM_STATE);
230 GET_ATOM(_NET_WM_WINDOW_TYPE);
231 GET_ATOM(_NET_WM_DESKTOP);
232 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
233 GET_ATOM(_NET_WM_STRUT_PARTIAL);
234 GET_ATOM(WM_PROTOCOLS);
235 GET_ATOM(WM_DELETE_WINDOW);
236 GET_ATOM(UTF8_STRING);
239 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
240 /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
242 /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
243 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
245 /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
246 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
248 /* Watch WM_CLASS (= class of the window) */
249 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
251 /* Set up the atoms we support */
252 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
253 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
254 /* Set up the window manager’s name */
255 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
256 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
258 xcb_get_numlock_mask(conn);
260 /* Grab the bound keys */
262 TAILQ_FOREACH(bind, &bindings, bindings) {
263 LOG("Grabbing %d\n", bind->keycode);
264 if (bind->mods & BIND_MODE_SWITCH)
265 xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
267 /* Grab the key in all combinations */
268 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
269 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
270 GRAB_KEY(bind->mods);
271 GRAB_KEY(bind->mods | xcb_numlock_mask);
272 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
276 /* Autostarting exec-lines */
277 struct Autostart *exec;
279 TAILQ_FOREACH(exec, &autostarts, autostarts) {
280 LOG("auto-starting %s\n", exec->command);
281 start_application(exec->command);
285 /* check for Xinerama */
286 LOG("Checking for Xinerama...\n");
287 initialize_xinerama(conn);
291 manage_existing_windows(conn, &prophs, root);
293 /* Get pointer position to see on which screen we’re starting */
294 xcb_query_pointer_reply_t *reply;
295 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
296 LOG("Could not get pointer position\n");
300 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
301 if (screen == NULL) {
302 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
305 if (screen->current_workspace != 0) {
306 LOG("Ok, I need to go to the other workspace\n");
307 c_ws = &workspaces[screen->current_workspace];
310 /* Enter xcb’s event handler */
311 xcb_event_wait_for_event_loop(&evenths);