]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Bugfix: Track the user’s pointer over the root window and update current workspace...
[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
22 #include <X11/XKBlib.h>
23 #include <X11/extensions/XKB.h>
24
25 #include <xcb/xcb.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>
33
34 #include <ev.h>
35
36 #include "config.h"
37 #include "data.h"
38 #include "debug.h"
39 #include "handlers.h"
40 #include "i3.h"
41 #include "layout.h"
42 #include "queue.h"
43 #include "table.h"
44 #include "util.h"
45 #include "xcb.h"
46 #include "xinerama.h"
47 #include "manage.h"
48 #include "ipc.h"
49
50 xcb_connection_t *global_conn;
51
52 /* This is the path to i3, copied from argv[0] when starting up */
53 char **start_argv;
54
55 /* This is our connection to X11 for use with XKB */
56 Display *xkbdpy;
57
58 /* The list of key bindings */
59 struct bindings_head bindings = TAILQ_HEAD_INITIALIZER(bindings);
60
61 /* The list of exec-lines */
62 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
63
64 /* The list of assignments */
65 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
66
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);
69
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];
74
75 xcb_window_t root;
76 int num_screens = 0;
77
78 /* The depth of the root screen (used e.g. for creating new pixmaps later) */
79 uint8_t root_depth;
80
81 /*
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
84  *
85  */
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 */
88 }
89
90 /*
91  * Flush before blocking (and waiting for new events)
92  *
93  */
94 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
95         xcb_flush(evenths.c);
96 }
97
98 /*
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.
101  *
102  */
103 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
104         xcb_generic_event_t *event;
105
106         while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
107                 xcb_event_handle(&evenths, event);
108                 free(event);
109         }
110 }
111
112 int main(int argc, char *argv[], char *env[]) {
113         int i, screens, opt;
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];
119
120         setlocale(LC_ALL, "");
121
122         /* Disable output buffering to make redirects in .xsession actually useful for debugging */
123         if (!isatty(fileno(stdout)))
124                 setbuf(stdout, NULL);
125
126         start_argv = argv;
127
128         while ((opt = getopt(argc, argv, "c:va")) != -1) {
129                 switch (opt) {
130                         case 'a':
131                                 LOG("Autostart disabled using -a\n");
132                                 autostart = false;
133                                 break;
134                         case 'c':
135                                 override_configpath = sstrdup(optarg);
136                                 break;
137                         case 'v':
138                                 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
139                                 exit(EXIT_SUCCESS);
140                         default:
141                                 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
142                                 exit(EXIT_FAILURE);
143                 }
144         }
145
146         LOG("i3 version " I3_VERSION " starting\n");
147
148         /* Initialize the table data structures for each workspace */
149         init_table();
150
151         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
152         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
153
154         conn = global_conn = xcb_connect(NULL, &screens);
155
156         if (xcb_connection_has_error(conn))
157                 die("Cannot open display\n");
158
159         load_configuration(conn, override_configpath, false);
160
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);
163
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);
182
183         /* TODO: this has to be more beautiful somewhen */
184         int major, minor, error;
185
186         major = XkbMajorVersion;
187         minor = XkbMinorVersion;
188
189         int evBase, errBase;
190
191         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
192                 fprintf(stderr, "XkbOpenDisplay() failed\n");
193                 return 1;
194         }
195
196         int i1;
197         if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
198                 fprintf(stderr, "XKB not supported by X-server\n");
199                 return 1;
200         }
201         /* end of ugliness */
202
203         /* Initialize event loop using libev */
204         struct ev_loop *loop = ev_default_loop(0);
205         if (loop == NULL)
206                 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
207
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));
211
212         ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
213         ev_io_start(loop, xcb_watcher);
214
215         ev_check_init(xcb_check, xcb_check_cb);
216         ev_check_start(loop, xcb_check);
217
218         ev_prepare_init(xcb_prepare, xcb_prepare_cb);
219         ev_prepare_start(loop, xcb_prepare);
220
221         /* Grab the server to delay any events until we enter the eventloop */
222         xcb_grab_server(conn);
223
224         xcb_event_handlers_init(conn, &evenths);
225
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);
229
230         for (i = 0; i < 256; ++i)
231                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
232
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);
235
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);
239
240         /* Enter window = user moved his mouse over the window */
241         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
242
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);
245
246         /* Map notify = there is a new window */
247         xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
248
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);
252
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);
256
257         /* Configure request = window tried to change size on its own */
258         xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
259
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);
263
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);
267
268         /* Initialize the property handlers */
269         xcb_property_handlers_init(&prophs, &evenths);
270
271         /* Watch size hints (to obey correct aspect ratio) */
272         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
273
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;
278
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
283                                                                            ConfigureNotify */
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);
288
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); \
292                 if (!reply) { \
293                         LOG("Could not get atom " #name "\n"); \
294                         exit(-1); \
295                 } \
296                 atoms[name] = reply->atom; \
297                 free(reply); \
298         }
299
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);
316         GET_ATOM(WM_STATE);
317         GET_ATOM(WM_CLIENT_LEADER);
318
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 */
321
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);
324
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);
327
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);
330
331         /* Watch WM_CLASS (= class of the window) */
332         xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
333
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);
336
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");
343
344         xcb_get_numlock_mask(conn);
345
346         grab_all_keys(conn);
347
348         /* Autostarting exec-lines */
349         struct Autostart *exec;
350         if (autostart) {
351                 TAILQ_FOREACH(exec, &autostarts, autostarts) {
352                         LOG("auto-starting %s\n", exec->command);
353                         start_application(exec->command);
354                 }
355         }
356
357         /* check for Xinerama */
358         LOG("Checking for Xinerama...\n");
359         initialize_xinerama(conn);
360
361         xcb_flush(conn);
362
363         manage_existing_windows(conn, &prophs, root);
364
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");
369                 return 1;
370         }
371
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);
375                 return 0;
376         }
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];
380         }
381
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");
387                 } else {
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);
391                 }
392         }
393
394         /* Handle the events which arrived until now */
395         xcb_check_cb(NULL, NULL, 0);
396
397         /* Ungrab the server to receive events and enter libev’s eventloop */
398         xcb_ungrab_server(conn);
399         ev_loop(loop, 0);
400
401         /* not reached */
402         return 0;
403 }