]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Also set DIALOG, UTILITY and SPLASH windows floating automatically
[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_WINDOW_TYPE_UTILITY);
165         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
166         REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
167         REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
168         REQUEST_ATOM(WM_PROTOCOLS);
169         REQUEST_ATOM(WM_DELETE_WINDOW);
170         REQUEST_ATOM(UTF8_STRING);
171         REQUEST_ATOM(WM_STATE);
172
173         /* TODO: this has to be more beautiful somewhen */
174         int major, minor, error;
175
176         major = XkbMajorVersion;
177         minor = XkbMinorVersion;
178
179         int evBase, errBase;
180
181         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
182                 fprintf(stderr, "XkbOpenDisplay() failed\n");
183                 return 1;
184         }
185
186         int i1;
187         if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
188                 fprintf(stderr, "XKB not supported by X-server\n");
189                 return 1;
190         }
191         /* end of ugliness */
192
193         xcb_event_handlers_init(conn, &evenths);
194
195         /* DEBUG: Trap all events and print them */
196         for (i = 2; i < 128; ++i)
197                 xcb_event_set_handler(&evenths, i, handle_event, 0);
198
199         for (i = 0; i < 256; ++i)
200                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
201
202         /* Expose = an Application should redraw itself, in this case it’s our titlebars. */
203         xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
204
205         /* Key presses/releases are pretty obvious, I think */
206         xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
207         xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
208
209         /* Enter window = user moved his mouse over the window */
210         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
211
212         /* Button press = user pushed a mouse button over one of our windows */
213         xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
214
215         /* Map notify = there is a new window */
216         xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
217
218         /* Unmap notify = window disappeared. When sent from a client, we don’t manage
219            it any longer. Usually, the client destroys the window shortly afterwards. */
220         xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
221
222         /* Configure notify = window’s configuration (geometry, stacking, …). We only need
223            it to set up ignore the following enter_notify events */
224         xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
225
226         /* Configure request = window tried to change size on its own */
227         xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
228
229         /* Client message are sent to the root window. The only interesting client message
230            for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
231         xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
232
233         /* Initialize the property handlers */
234         xcb_property_handlers_init(&prophs, &evenths);
235
236         /* Watch size hints (to obey correct aspect ratio) */
237         xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
238
239         /* Get the root window and set the event mask */
240         root = xcb_aux_get_screen(conn, screens)->root;
241
242         uint32_t mask = XCB_CW_EVENT_MASK;
243         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
244                               XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
245                                                                            projector), the root window gets a
246                                                                            ConfigureNotify */
247                               XCB_EVENT_MASK_PROPERTY_CHANGE |
248                               XCB_EVENT_MASK_ENTER_WINDOW };
249         xcb_change_window_attributes(conn, root, mask, values);
250
251         /* Setup NetWM atoms */
252         #define GET_ATOM(name) { \
253                 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
254                 if (!reply) { \
255                         LOG("Could not get atom " #name "\n"); \
256                         exit(-1); \
257                 } \
258                 atoms[name] = reply->atom; \
259                 free(reply); \
260         }
261
262         GET_ATOM(_NET_SUPPORTED);
263         GET_ATOM(_NET_WM_STATE_FULLSCREEN);
264         GET_ATOM(_NET_SUPPORTING_WM_CHECK);
265         GET_ATOM(_NET_WM_NAME);
266         GET_ATOM(_NET_WM_STATE);
267         GET_ATOM(_NET_WM_WINDOW_TYPE);
268         GET_ATOM(_NET_WM_DESKTOP);
269         GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
270         GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
271         GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
272         GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
273         GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
274         GET_ATOM(_NET_WM_STRUT_PARTIAL);
275         GET_ATOM(WM_PROTOCOLS);
276         GET_ATOM(WM_DELETE_WINDOW);
277         GET_ATOM(UTF8_STRING);
278         GET_ATOM(WM_STATE);
279
280         xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
281         /* TODO: In order to comply with EWMH, we have to watch _NET_WM_STRUT_PARTIAL */
282
283         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
284         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
285
286         /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
287         xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
288
289         /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
290         xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
291
292         /* Watch WM_CLASS (= class of the window) */
293         xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
294
295         /* Set up the atoms we support */
296         check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
297                        ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
298         /* Set up the window manager’s name */
299         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
300         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
301
302         xcb_get_numlock_mask(conn);
303
304         /* Grab the bound keys */
305         Binding *bind;
306         TAILQ_FOREACH(bind, &bindings, bindings) {
307                 LOG("Grabbing %d\n", bind->keycode);
308                 if (bind->mods & BIND_MODE_SWITCH)
309                         xcb_grab_key(conn, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
310                 else {
311                         /* Grab the key in all combinations */
312                         #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
313                                                                 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
314                         GRAB_KEY(bind->mods);
315                         GRAB_KEY(bind->mods | xcb_numlock_mask);
316                         GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
317                 }
318         }
319
320         /* Autostarting exec-lines */
321         struct Autostart *exec;
322         if (autostart) {
323                 TAILQ_FOREACH(exec, &autostarts, autostarts) {
324                         LOG("auto-starting %s\n", exec->command);
325                         start_application(exec->command);
326                 }
327         }
328
329         /* check for Xinerama */
330         LOG("Checking for Xinerama...\n");
331         initialize_xinerama(conn);
332
333         xcb_flush(conn);
334
335         manage_existing_windows(conn, &prophs, root);
336
337         /* Get pointer position to see on which screen we’re starting */
338         xcb_query_pointer_reply_t *reply;
339         if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
340                 LOG("Could not get pointer position\n");
341                 return 1;
342         }
343
344         i3Screen *screen = get_screen_containing(reply->root_x, reply->root_y);
345         if (screen == NULL) {
346                 LOG("ERROR: No screen at %d x %d\n", reply->root_x, reply->root_y);
347                 return 0;
348         }
349         if (screen->current_workspace != 0) {
350                 LOG("Ok, I need to go to the other workspace\n");
351                 c_ws = &workspaces[screen->current_workspace];
352         }
353
354
355         /* Initialize event loop using libev */
356         struct ev_loop *loop = ev_default_loop(0);
357         if (loop == NULL)
358                 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
359
360         struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
361         ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
362
363         /* Call the handler to work all events which arrived before the libev-stuff was set up */
364         xcb_got_event(NULL, xcb_watcher, 0);
365
366         /* Enter the libev eventloop */
367         ev_io_start(loop, xcb_watcher);
368         ev_loop(loop, 0);
369
370         /* not reached */
371         return 0;
372 }