]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Bugfix: Correctly initialize the new atom, don’t float dock windows
[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
49 /* This is the path to i3, copied from argv[0] when starting up */
50 char **start_argv;
51
52 /* This is our connection to X11 for use with XKB */
53 Display *xkbdpy;
54
55 /* The list of key bindings */
56 struct bindings_head bindings = TAILQ_HEAD_INITIALIZER(bindings);
57
58 /* The list of exec-lines */
59 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
60
61 /* The list of assignments */
62 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
63
64 /* This is a list of Stack_Windows, global, for easier/faster access on expose events */
65 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
66
67 /* The event handlers need to be global because they are accessed by our custom event handler
68    in handle_button_press(), needed for graphical resizing */
69 xcb_event_handlers_t evenths;
70 xcb_atom_t atoms[NUM_ATOMS];
71
72 int num_screens = 0;
73
74 /*
75  * Callback for activity on the connection to the X server
76  *
77  */
78 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
79         xcb_generic_event_t *event;
80
81         /* When an event is available… */
82         while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
83                 /* …we handle all events in a row: */
84                 do {
85                         xcb_event_handle(&evenths, event);
86                         xcb_aux_sync(evenths.c);
87                         free(event);
88                 } while ((event = xcb_poll_for_event(evenths.c)));
89
90                 /* Make sure all replies are handled/discarded */
91                 xcb_aux_sync(evenths.c);
92
93                 /* Afterwards, there may be new events available which would
94                  * not trigger the select() (libev) immediately, so we check
95                  * again (and don’t bail out of the loop). */
96         }
97
98         /* Make sure all replies are handled/discarded */
99         xcb_aux_sync(evenths.c);
100 }
101
102 int main(int argc, char *argv[], char *env[]) {
103         int i, screens, opt;
104         char *override_configpath = NULL;
105         bool autostart = true;
106         xcb_connection_t *conn;
107         xcb_property_handlers_t prophs;
108         xcb_window_t root;
109         xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
110
111         setlocale(LC_ALL, "");
112
113         /* Disable output buffering to make redirects in .xsession actually useful for debugging */
114         if (!isatty(fileno(stdout)))
115                 setbuf(stdout, NULL);
116
117         start_argv = argv;
118
119         while ((opt = getopt(argc, argv, "c:va")) != -1) {
120                 switch (opt) {
121                         case 'a':
122                                 LOG("Autostart disabled using -a\n");
123                                 autostart = false;
124                                 break;
125                         case 'c':
126                                 override_configpath = sstrdup(optarg);
127                                 break;
128                         case 'v':
129                                 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
130                                 exit(EXIT_SUCCESS);
131                         default:
132                                 fprintf(stderr, "Usage: %s [-c configfile]\n", argv[0]);
133                                 exit(EXIT_FAILURE);
134                 }
135         }
136
137         LOG("i3 version " I3_VERSION " starting\n");
138
139         /* Initialize the table data structures for each workspace */
140         init_table();
141
142         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
143         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
144
145         conn = xcb_connect(NULL, &screens);
146
147         if (xcb_connection_has_error(conn))
148                 die("Cannot open display\n");
149
150         load_configuration(conn, override_configpath);
151
152         /* Place requests for the atoms we need as soon as possible */
153         #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
154
155         REQUEST_ATOM(_NET_SUPPORTED);
156         REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
157         REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
158         REQUEST_ATOM(_NET_WM_NAME);
159         REQUEST_ATOM(_NET_WM_STATE);
160         REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
161         REQUEST_ATOM(_NET_WM_DESKTOP);
162         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
163         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
164         REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
165         REQUEST_ATOM(WM_PROTOCOLS);
166         REQUEST_ATOM(WM_DELETE_WINDOW);
167         REQUEST_ATOM(UTF8_STRING);
168         REQUEST_ATOM(WM_STATE);
169
170         /* TODO: this has to be more beautiful somewhen */
171         int major, minor, error;
172
173         major = XkbMajorVersion;
174         minor = XkbMinorVersion;
175
176         int evBase, errBase;
177
178         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
179                 fprintf(stderr, "XkbOpenDisplay() failed\n");
180                 return 1;
181         }
182
183         int i1;
184         if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
185                 fprintf(stderr, "XKB not supported by X-server\n");
186                 return 1;
187         }
188         /* end of ugliness */
189
190         xcb_event_handlers_init(conn, &evenths);
191
192         /* DEBUG: Trap all events and print them */
193         for (i = 2; i < 128; ++i)
194                 xcb_event_set_handler(&evenths, i, handle_event, 0);
195
196         for (i = 0; i < 256; ++i)
197                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
198
199         /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
200         xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
201
202         /* Key presses/releases are pretty obvious, I think */
203         xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
204         xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
205
206         /* Enter window = user moved his mouse over the window */
207         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
208
209         /* Button press = user pushed a mouse button over one of our windows */
210         xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
211
212         /* Map notify = there is a new window */
213         xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
214
215         /* Unmap notify = window disappeared. When sent from a client, we don’t manage
216            it any longer. Usually, the client destroys the window shortly afterwards. */
217         xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
218
219         /* Configure notify = window’s configuration (geometry, stacking, …). We only need
220            it to set up ignore the following enter_notify events */
221         xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
222
223         /* Configure request = window tried to change size on its own */
224         xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
225
226         /* Client message are sent to the root window. The only interesting client message
227            for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
228         xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
229
230         /* Initialize the property handlers */
231         xcb_property_handlers_init(&prophs, &evenths);
232
233         /* Watch size hints (to obey correct aspect ratio) */
234         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
235
236         /* Get the root window and set the event mask */
237         root = xcb_aux_get_screen(conn, screens)->root;
238
239         uint32_t mask = XCB_CW_EVENT_MASK;
240         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
241                               XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
242                                                                            projector), the root window gets a
243                                                                            ConfigureNotify */
244                               XCB_EVENT_MASK_PROPERTY_CHANGE |
245                               XCB_EVENT_MASK_ENTER_WINDOW };
246         xcb_change_window_attributes(conn, root, mask, values);
247
248         /* Setup NetWM atoms */
249         #define GET_ATOM(name) { \
250                 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
251                 if (!reply) { \
252                         LOG("Could not get atom " #name "\n"); \
253                         exit(-1); \
254                 } \
255                 atoms[name] = reply->atom; \
256                 free(reply); \
257         }
258
259         GET_ATOM(_NET_SUPPORTED);
260         GET_ATOM(_NET_WM_STATE_FULLSCREEN);
261         GET_ATOM(_NET_SUPPORTING_WM_CHECK);
262         GET_ATOM(_NET_WM_NAME);
263         GET_ATOM(_NET_WM_STATE);
264         GET_ATOM(_NET_WM_WINDOW_TYPE);
265         GET_ATOM(_NET_WM_DESKTOP);
266         GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
267         GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
268         GET_ATOM(_NET_WM_STRUT_PARTIAL);
269         GET_ATOM(WM_PROTOCOLS);
270         GET_ATOM(WM_DELETE_WINDOW);
271         GET_ATOM(UTF8_STRING);
272         GET_ATOM(WM_STATE);
273
274         xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
275         /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
276
277         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
278         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
279
280         /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
281         xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
282
283         /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
284         xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
285
286         /* Watch WM_CLASS (= class of the window) */
287         xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
288
289         /* Set up the atoms we support */
290         check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
291                        ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
292         /* Set up the window manager’s name */
293         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
294         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
295
296         xcb_get_numlock_mask(conn);
297
298         /* Grab the bound keys */
299         Binding *bind;
300         TAILQ_FOREACH(bind, &bindings, bindings) {
301                 LOG("Grabbing %d\n", bind->keycode);
302                 if (bind->mods & BIND_MODE_SWITCH)
303                         xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
304                 else {
305                         /* Grab the key in all combinations */
306                         #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
307                                                                 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
308                         GRAB_KEY(bind->mods);
309                         GRAB_KEY(bind->mods | xcb_numlock_mask);
310                         GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
311                 }
312         }
313
314         /* Autostarting exec-lines */
315         struct Autostart *exec;
316         if (autostart) {
317                 TAILQ_FOREACH(exec, &autostarts, autostarts) {
318                         LOG("auto-starting %s\n", exec->command);
319                         start_application(exec->command);
320                 }
321         }
322
323         /* check for Xinerama */
324         LOG("Checking for Xinerama...\n");
325         initialize_xinerama(conn);
326
327         xcb_flush(conn);
328
329         manage_existing_windows(conn, &prophs, root);
330
331         /* Get pointer position to see on which screen we’re starting */
332         xcb_query_pointer_reply_t *reply;
333         if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
334                 LOG("Could not get pointer position\n");
335                 return 1;
336         }
337
338         i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
339         if (screen == NULL) {
340                 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
341                 return 0;
342         }
343         if (screen->current_workspace != 0) {
344                 LOG("Ok, I need to go to the other workspace\n");
345                 c_ws = &workspaces[screen->current_workspace];
346         }
347
348
349         /* Initialize event loop using libev */
350         struct ev_loop *loop = ev_default_loop(0);
351         if (loop == NULL)
352                 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
353
354         struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
355         ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
356
357         /* Call the handler to work all events which arrived before the libev-stuff was set up */
358         xcb_got_event(NULL, xcb_watcher, 0);
359
360         /* Enter the libev eventloop */
361         ev_io_start(loop, xcb_watcher);
362         ev_loop(loop, 0);
363
364         /* not reached */
365         return 0;
366 }