]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Add support for WM_CLIENT_LEADER, put floating windows mapping to (0x0) to center...
[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         /* Client message are sent to the root window. The only interesting client message
261            for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
262         xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
263
264         /* Initialize the property handlers */
265         xcb_property_handlers_init(&prophs, &evenths);
266
267         /* Watch size hints (to obey correct aspect ratio) */
268         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
269
270         /* Get the root window and set the event mask */
271         xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
272         root = root_screen->root;
273         root_depth = root_screen->root_depth;
274
275         uint32_t mask = XCB_CW_EVENT_MASK;
276         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
277                               XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
278                                                                            projector), the root window gets a
279                                                                            ConfigureNotify */
280                               XCB_EVENT_MASK_PROPERTY_CHANGE |
281                               XCB_EVENT_MASK_ENTER_WINDOW };
282         xcb_change_window_attributes(conn, root, mask, values);
283
284         /* Setup NetWM atoms */
285         #define GET_ATOM(name) { \
286                 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
287                 if (!reply) { \
288                         LOG("Could not get atom " #name "\n"); \
289                         exit(-1); \
290                 } \
291                 atoms[name] = reply->atom; \
292                 free(reply); \
293         }
294
295         GET_ATOM(_NET_SUPPORTED);
296         GET_ATOM(_NET_WM_STATE_FULLSCREEN);
297         GET_ATOM(_NET_SUPPORTING_WM_CHECK);
298         GET_ATOM(_NET_WM_NAME);
299         GET_ATOM(_NET_WM_STATE);
300         GET_ATOM(_NET_WM_WINDOW_TYPE);
301         GET_ATOM(_NET_WM_DESKTOP);
302         GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
303         GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
304         GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
305         GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
306         GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
307         GET_ATOM(_NET_WM_STRUT_PARTIAL);
308         GET_ATOM(WM_PROTOCOLS);
309         GET_ATOM(WM_DELETE_WINDOW);
310         GET_ATOM(UTF8_STRING);
311         GET_ATOM(WM_STATE);
312         GET_ATOM(WM_CLIENT_LEADER);
313
314         xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
315         /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
316
317         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
318         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
319
320         /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
321         xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
322
323         /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
324         xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
325
326         /* Watch WM_CLASS (= class of the window) */
327         xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
328
329         /* Watch WM_CLIENT_LEADER (= logical parent window for toolbars etc.) */
330         xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
331
332         /* Set up the atoms we support */
333         check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
334                        ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
335         /* Set up the window manager’s name */
336         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
337         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
338
339         xcb_get_numlock_mask(conn);
340
341         grab_all_keys(conn);
342
343         /* Autostarting exec-lines */
344         struct Autostart *exec;
345         if (autostart) {
346                 TAILQ_FOREACH(exec, &autostarts, autostarts) {
347                         LOG("auto-starting %s\n", exec->command);
348                         start_application(exec->command);
349                 }
350         }
351
352         /* check for Xinerama */
353         LOG("Checking for Xinerama...\n");
354         initialize_xinerama(conn);
355
356         xcb_flush(conn);
357
358         manage_existing_windows(conn, &prophs, root);
359
360         /* Get pointer position to see on which screen we’re starting */
361         xcb_query_pointer_reply_t *reply;
362         if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
363                 LOG("Could not get pointer position\n");
364                 return 1;
365         }
366
367         i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
368         if (screen == NULL) {
369                 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
370                 return 0;
371         }
372         if (screen->current_workspace != 0) {
373                 LOG("Ok, I need to go to the other workspace\n");
374                 c_ws = &workspaces[screen->current_workspace];
375         }
376
377         /* Create the UNIX domain socket for IPC */
378         if (config.ipc_socket_path != NULL) {
379                 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
380                 if (ipc_socket == -1) {
381                         LOG("Could not create the IPC socket, IPC disabled\n");
382                 } else {
383                         struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
384                         ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
385                         ev_io_start(loop, ipc_io);
386                 }
387         }
388
389         /* Handle the events which arrived until now */
390         xcb_check_cb(NULL, NULL, 0);
391
392         /* Ungrab the server to receive events and enter libev’s eventloop */
393         xcb_ungrab_server(conn);
394         ev_loop(loop, 0);
395
396         /* not reached */
397         return 0;
398 }