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