]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Implement the urgency hint for windows/workspaces
[i3/i3] / src / mainx.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdio.h>
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <stdbool.h>
18 #include <assert.h>
19 #include <limits.h>
20 #include <locale.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23
24 #include <X11/XKBlib.h>
25 #include <X11/extensions/XKB.h>
26
27 #include <xcb/xcb.h>
28 #include <xcb/xcb_atom.h>
29 #include <xcb/xcb_aux.h>
30 #include <xcb/xcb_event.h>
31 #include <xcb/xcb_property.h>
32 #include <xcb/xcb_keysyms.h>
33 #include <xcb/xcb_icccm.h>
34 #include <xcb/xinerama.h>
35
36 #include <ev.h>
37
38 #include "config.h"
39 #include "data.h"
40 #include "debug.h"
41 #include "handlers.h"
42 #include "i3.h"
43 #include "layout.h"
44 #include "queue.h"
45 #include "table.h"
46 #include "util.h"
47 #include "xcb.h"
48 #include "xinerama.h"
49 #include "manage.h"
50 #include "ipc.h"
51
52 xcb_connection_t *global_conn;
53
54 /* This is the path to i3, copied from argv[0] when starting up */
55 char **start_argv;
56
57 /* This is our connection to X11 for use with XKB */
58 Display *xkbdpy;
59
60 xcb_key_symbols_t *keysyms;
61
62 /* The list of key bindings */
63 struct bindings_head bindings = TAILQ_HEAD_INITIALIZER(bindings);
64
65 /* The list of exec-lines */
66 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
67
68 /* The list of assignments */
69 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
70
71 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
72 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
73
74 /* The event handlers need to be global because they are accessed by our custom event handler
75    in handle_button_press(), needed for graphical resizing */
76 xcb_event_handlers_t evenths;
77 xcb_atom_t atoms[NUM_ATOMS];
78
79 xcb_window_t root;
80 int num_screens = 0;
81
82 /* The depth of the root screen (used e.g. for creating new pixmaps later) */
83 uint8_t root_depth;
84
85 /*
86  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
87  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
88  *
89  */
90 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
91         /* empty, because xcb_prepare_cb and xcb_check_cb are used */
92 }
93
94 /*
95  * Flush before blocking (and waiting for new events)
96  *
97  */
98 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
99         xcb_flush(evenths.c);
100 }
101
102 /*
103  * Instead of polling the X connection socket we leave this to
104  * xcb_poll_for_event() which knows better than we can ever know.
105  *
106  */
107 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
108         xcb_generic_event_t *event;
109
110         while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
111                 xcb_event_handle(&evenths, event);
112                 free(event);
113         }
114 }
115
116 /*
117  * When using xmodmap to change the keyboard mapping, this event
118  * is only sent via XKB. Therefore, we need this special handler.
119  *
120  */
121 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
122         LOG("got xkb event, yay\n");
123         XEvent ev;
124         /* When using xmodmap, every change (!) gets an own event.
125          * Therefore, we just read all events and only handle the
126          * mapping_notify once (we do not receive any other XKB
127          * events anyway). */
128         while (XPending(xkbdpy))
129                 XNextEvent(xkbdpy, &ev);
130
131         xcb_key_symbols_free(keysyms);
132         keysyms = xcb_key_symbols_alloc(global_conn);
133
134         xcb_get_numlock_mask(global_conn);
135
136         ungrab_all_keys(global_conn);
137         LOG("Re-grabbing...\n");
138         grab_all_keys(global_conn);
139         LOG("Done\n");
140
141 }
142
143
144 int main(int argc, char *argv[], char *env[]) {
145         int i, screens, opt;
146         char *override_configpath = NULL;
147         bool autostart = true;
148         xcb_connection_t *conn;
149         xcb_property_handlers_t prophs;
150         xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
151         static struct option long_options[] = {
152                 {"no-autostart", no_argument, 0, 'a'},
153                 {"config", required_argument, 0, 'c'},
154                 {"version", no_argument, 0, 'v'},
155                 {"help", no_argument, 0, 'h'},
156                 {0, 0, 0, 0}
157         };
158         int option_index = 0;
159
160         setlocale(LC_ALL, "");
161
162         /* Disable output buffering to make redirects in .xsession actually useful for debugging */
163         if (!isatty(fileno(stdout)))
164                 setbuf(stdout, NULL);
165
166         start_argv = argv;
167
168         while ((opt = getopt_long(argc, argv, "c:vah", long_options, &option_index)) != -1) {
169                 switch (opt) {
170                         case 'a':
171                                 LOG("Autostart disabled using -a\n");
172                                 autostart = false;
173                                 break;
174                         case 'c':
175                                 override_configpath = sstrdup(optarg);
176                                 break;
177                         case 'v':
178                                 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
179                                 exit(EXIT_SUCCESS);
180                         default:
181                                 fprintf(stderr, "Usage: %s [-c configfile] [-a] [-v]\n", argv[0]);
182                                 fprintf(stderr, "\n");
183                                 fprintf(stderr, "-a: disable autostart\n");
184                                 fprintf(stderr, "-v: display version and exit\n");
185                                 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
186                                 exit(EXIT_FAILURE);
187                 }
188         }
189
190         LOG("i3 version " I3_VERSION " starting\n");
191
192         /* Initialize the table data structures for each workspace */
193         init_table();
194
195         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
196         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
197
198         conn = global_conn = xcb_connect(NULL, &screens);
199
200         if (xcb_connection_has_error(conn))
201                 die("Cannot open display\n");
202
203         load_configuration(conn, override_configpath, false);
204
205         /* Place requests for the atoms we need as soon as possible */
206         #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
207
208         REQUEST_ATOM(_NET_SUPPORTED);
209         REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
210         REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
211         REQUEST_ATOM(_NET_WM_NAME);
212         REQUEST_ATOM(_NET_WM_STATE);
213         REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
214         REQUEST_ATOM(_NET_WM_DESKTOP);
215         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
216         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
217         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
218         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
219         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
220         REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
221         REQUEST_ATOM(WM_PROTOCOLS);
222         REQUEST_ATOM(WM_DELETE_WINDOW);
223         REQUEST_ATOM(UTF8_STRING);
224         REQUEST_ATOM(WM_STATE);
225         REQUEST_ATOM(WM_CLIENT_LEADER);
226
227         /* TODO: this has to be more beautiful somewhen */
228         int major, minor, error;
229
230         major = XkbMajorVersion;
231         minor = XkbMinorVersion;
232
233         int evBase, errBase;
234
235         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
236                 fprintf(stderr, "XkbOpenDisplay() failed\n");
237                 return 1;
238         }
239
240         if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
241                 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
242                 return 1;
243         }
244
245         int i1;
246         if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
247                 fprintf(stderr, "XKB not supported by X-server\n");
248                 return 1;
249         }
250         /* end of ugliness */
251
252         if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd, XkbMapNotifyMask, XkbMapNotifyMask)) {
253                 fprintf(stderr, "Could not set XKB event mask\n");
254                 return 1;
255         }
256
257         /* Initialize event loop using libev */
258         struct ev_loop *loop = ev_default_loop(0);
259         if (loop == NULL)
260                 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
261
262         struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
263         struct ev_io *xkb = scalloc(sizeof(struct ev_io));
264         struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
265         struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
266
267         ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
268         ev_io_start(loop, xcb_watcher);
269
270         ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
271         ev_io_start(loop, xkb);
272
273         /* Flush the buffer so that libev can properly get new events */
274         XFlush(xkbdpy);
275
276         ev_check_init(xcb_check, xcb_check_cb);
277         ev_check_start(loop, xcb_check);
278
279         ev_prepare_init(xcb_prepare, xcb_prepare_cb);
280         ev_prepare_start(loop, xcb_prepare);
281
282         /* Grab the server to delay any events until we enter the eventloop */
283         xcb_grab_server(conn);
284
285         xcb_event_handlers_init(conn, &evenths);
286
287         /* DEBUG: Trap all events and print them */
288         for (i = 2; i < 128; ++i)
289                 xcb_event_set_handler(&evenths, i, handle_event, 0);
290
291         for (i = 0; i < 256; ++i)
292                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
293
294         /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
295         xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
296
297         /* Key presses/releases are pretty obvious, I think */
298         xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
299         xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
300
301         /* Enter window = user moved his mouse over the window */
302         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
303
304         /* Button press = user pushed a mouse button over one of our windows */
305         xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
306
307         /* Map notify = there is a new window */
308         xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
309
310         /* Unmap notify = window disappeared. When sent from a client, we don’t manage
311            it any longer. Usually, the client destroys the window shortly afterwards. */
312         xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
313
314         /* Configure notify = window’s configuration (geometry, stacking, …). We only need
315            it to set up ignore the following enter_notify events */
316         xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
317
318         /* Configure request = window tried to change size on its own */
319         xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
320
321         /* Motion notify = user moved his cursor (over the root window and may
322          * cross virtual screen boundaries doing that) */
323         xcb_event_set_motion_notify_handler(&evenths, handle_motion_notify, NULL);
324
325         /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
326         xcb_event_set_mapping_notify_handler(&evenths, handle_mapping_notify, NULL);
327
328         /* Client message are sent to the root window. The only interesting client message
329            for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
330         xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
331
332         /* Initialize the property handlers */
333         xcb_property_handlers_init(&prophs, &evenths);
334
335         /* Watch size hints (to obey correct aspect ratio) */
336         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
337
338         /* Get the root window and set the event mask */
339         xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
340         root = root_screen->root;
341         root_depth = root_screen->root_depth;
342
343         uint32_t mask = XCB_CW_EVENT_MASK;
344         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
345                               XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
346                                                                            projector), the root window gets a
347                                                                            ConfigureNotify */
348                               XCB_EVENT_MASK_POINTER_MOTION |
349                               XCB_EVENT_MASK_PROPERTY_CHANGE |
350                               XCB_EVENT_MASK_ENTER_WINDOW };
351         xcb_change_window_attributes(conn, root, mask, values);
352
353         /* Setup NetWM atoms */
354         #define GET_ATOM(name) { \
355                 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
356                 if (!reply) { \
357                         LOG("Could not get atom " #name "\n"); \
358                         exit(-1); \
359                 } \
360                 atoms[name] = reply->atom; \
361                 free(reply); \
362         }
363
364         GET_ATOM(_NET_SUPPORTED);
365         GET_ATOM(_NET_WM_STATE_FULLSCREEN);
366         GET_ATOM(_NET_SUPPORTING_WM_CHECK);
367         GET_ATOM(_NET_WM_NAME);
368         GET_ATOM(_NET_WM_STATE);
369         GET_ATOM(_NET_WM_WINDOW_TYPE);
370         GET_ATOM(_NET_WM_DESKTOP);
371         GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
372         GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
373         GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
374         GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
375         GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
376         GET_ATOM(_NET_WM_STRUT_PARTIAL);
377         GET_ATOM(WM_PROTOCOLS);
378         GET_ATOM(WM_DELETE_WINDOW);
379         GET_ATOM(UTF8_STRING);
380         GET_ATOM(WM_STATE);
381         GET_ATOM(WM_CLIENT_LEADER);
382
383         xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
384         /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
385
386         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
387         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
388
389         /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
390         xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
391
392         /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
393         xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
394
395         /* Watch WM_CLASS (= class of the window) */
396         xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
397
398         /* Watch WM_CLIENT_LEADER (= logical parent window for toolbars etc.) */
399         xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
400
401         /* Watch WM_HINTS (contains the urgent property) */
402         xcb_property_set_handler(&prophs, WM_HINTS, UINT_MAX, handle_hints, NULL);
403
404         /* Set up the atoms we support */
405         check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
406                        ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
407         /* Set up the window manager’s name */
408         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
409         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
410
411         keysyms = xcb_key_symbols_alloc(conn);
412
413         xcb_get_numlock_mask(conn);
414
415         grab_all_keys(conn);
416
417         /* Autostarting exec-lines */
418         struct Autostart *exec;
419         if (autostart) {
420                 TAILQ_FOREACH(exec, &autostarts, autostarts) {
421                         LOG("auto-starting %s\n", exec->command);
422                         start_application(exec->command);
423                 }
424         }
425
426         /* check for Xinerama */
427         LOG("Checking for Xinerama...\n");
428         initialize_xinerama(conn);
429
430         xcb_flush(conn);
431
432         /* Get pointer position to see on which screen we’re starting */
433         xcb_query_pointer_reply_t *reply;
434         if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
435                 LOG("Could not get pointer position\n");
436                 return 1;
437         }
438
439         i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
440         if (screen == NULL) {
441                 LOG("ERROR: No screen at %d x %d, starting on the first screen\n",
442                     reply->root_x, reply->root_y);
443                 screen = TAILQ_FIRST(virtual_screens);
444         }
445
446         LOG("Starting on %d\n", screen->current_workspace);
447         c_ws = &workspaces[screen->current_workspace];
448
449         manage_existing_windows(conn, &prophs, root);
450
451         /* Create the UNIX domain socket for IPC */
452         if (config.ipc_socket_path != NULL) {
453                 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
454                 if (ipc_socket == -1) {
455                         LOG("Could not create the IPC socket, IPC disabled\n");
456                 } else {
457                         struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
458                         ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
459                         ev_io_start(loop, ipc_io);
460                 }
461         }
462
463         /* Handle the events which arrived until now */
464         xcb_check_cb(NULL, NULL, 0);
465
466         /* Ungrab the server to receive events and enter libev’s eventloop */
467         xcb_ungrab_server(conn);
468         ev_loop(loop, 0);
469
470         /* not reached */
471         return 0;
472 }