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 xcb_connection_t *conn;
76 xcb_property_handlers_t prophs;
78 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
80 setlocale(LC_ALL, "");
82 /* Disable output buffering to make redirects in .xsession actually useful for debugging */
83 if (!isatty(fileno(stdout)))
88 while ((opt = getopt(argc, argv, "c:v")) != -1) {
91 override_configpath = sstrdup(optarg);
94 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
97 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
102 LOG("i3 version " I3_VERSION " starting\n");
104 /* Initialize the table data structures for each workspace */
107 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
108 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
110 load_configuration(override_configpath);
112 conn = xcb_connect(NULL, &screens);
114 /* Place requests for the atoms we need as soon as possible */
115 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
117 REQUEST_ATOM(_NET_SUPPORTED);
118 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
119 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
120 REQUEST_ATOM(_NET_WM_NAME);
121 REQUEST_ATOM(_NET_WM_STATE);
122 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
123 REQUEST_ATOM(_NET_WM_DESKTOP);
124 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
125 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
126 REQUEST_ATOM(WM_PROTOCOLS);
127 REQUEST_ATOM(WM_DELETE_WINDOW);
128 REQUEST_ATOM(UTF8_STRING);
129 REQUEST_ATOM(WM_STATE);
131 /* TODO: this has to be more beautiful somewhen */
132 int major, minor, error;
134 major = XkbMajorVersion;
135 minor = XkbMinorVersion;
139 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
140 fprintf(stderr, "XkbOpenDisplay() failed\n");
145 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
146 fprintf(stderr, "XKB not supported by X-server\n");
149 /* end of ugliness */
151 xcb_event_handlers_init(conn, &evenths);
153 /* DEBUG: Trap all events and print them */
154 for (i = 2; i < 128; ++i)
155 xcb_event_set_handler(&evenths, i, handle_event, 0);
157 for (i = 0; i < 256; ++i)
158 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
160 /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
161 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
163 /* Key presses/releases are pretty obvious, I think */
164 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
165 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
167 /* Enter window = user moved his mouse over the window */
168 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
170 /* Button press = user pushed a mouse button over one of our windows */
171 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
173 /* Map notify = there is a new window */
174 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
176 /* Unmap notify = window disappeared. When sent from a client, we don’t manage
177 it any longer. Usually, the client destroys the window shortly afterwards. */
178 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
180 /* Configure notify = window’s configuration (geometry, stacking, …). We only need
181 it to set up ignore the following enter_notify events */
182 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
184 /* Configure request = window tried to change size on its own */
185 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
187 /* Client message are sent to the root window. The only interesting client message
188 for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
189 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
191 /* Initialize the property handlers */
192 xcb_property_handlers_init(&prophs, &evenths);
194 /* Watch size hints (to obey correct aspect ratio) */
195 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
197 /* Get the root window and set the event mask */
198 root = xcb_aux_get_screen(conn, screens)->root;
200 uint32_t mask = XCB_CW_EVENT_MASK;
201 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
202 XCB_EVENT_MASK_STRUCTURE_NOTIFY | /* when the user adds a screen (e.g. video
203 projector), the root window gets a
205 XCB_EVENT_MASK_PROPERTY_CHANGE |
206 XCB_EVENT_MASK_ENTER_WINDOW };
207 xcb_change_window_attributes(conn, root, mask, values);
209 /* Setup NetWM atoms */
210 #define GET_ATOM(name) { \
211 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
213 LOG("Could not get atom " #name "\n"); \
216 atoms[name] = reply->atom; \
220 GET_ATOM(_NET_SUPPORTED);
221 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
222 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
223 GET_ATOM(_NET_WM_NAME);
224 GET_ATOM(_NET_WM_STATE);
225 GET_ATOM(_NET_WM_WINDOW_TYPE);
226 GET_ATOM(_NET_WM_DESKTOP);
227 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
228 GET_ATOM(_NET_WM_STRUT_PARTIAL);
229 GET_ATOM(WM_PROTOCOLS);
230 GET_ATOM(WM_DELETE_WINDOW);
231 GET_ATOM(UTF8_STRING);
234 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
235 /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
237 /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
238 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
240 /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
241 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
243 /* Watch WM_CLASS (= class of the window) */
244 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
246 /* Set up the atoms we support */
247 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
248 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
249 /* Set up the window manager’s name */
250 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
251 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
253 xcb_get_numlock_mask(conn);
255 /* Grab the bound keys */
257 TAILQ_FOREACH(bind, &bindings, bindings) {
258 LOG("Grabbing %d\n", bind->keycode);
259 if (bind->mods & BIND_MODE_SWITCH)
260 xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
262 /* Grab the key in all combinations */
263 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
264 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
265 GRAB_KEY(bind->mods);
266 GRAB_KEY(bind->mods | xcb_numlock_mask);
267 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
271 /* Autostarting exec-lines */
272 struct Autostart *exec;
273 TAILQ_FOREACH(exec, &autostarts, autostarts) {
274 LOG("auto-starting %s\n", exec->command);
275 start_application(exec->command);
278 /* check for Xinerama */
279 LOG("Checking for Xinerama...\n");
280 initialize_xinerama(conn);
284 manage_existing_windows(conn, &prophs, root);
286 /* Get pointer position to see on which screen we’re starting */
287 xcb_query_pointer_reply_t *reply;
288 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
289 LOG("Could not get pointer position\n");
293 i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
294 if (screen == NULL) {
295 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
298 if (screen->current_workspace != 0) {
299 LOG("Ok, I need to go to the other workspace\n");
300 c_ws = &workspaces[screen->current_workspace];
303 /* Enter xcb’s event handler */
304 xcb_event_wait_for_event_loop(&evenths);