]> git.sur5r.net Git - i3/i3/blob - src/main.c
Implement 'fullscreen global'
[i3/i3] / src / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4 #include <ev.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include "all.h"
8
9 static int xkb_event_base;
10
11 int xkb_current_group;
12
13 extern Con *focused;
14
15 char **start_argv;
16
17 xcb_connection_t *conn;
18
19 xcb_window_t root;
20 uint8_t root_depth;
21
22 xcb_key_symbols_t *keysyms;
23
24 /* Those are our connections to X11 for use with libXcursor and XKB */
25 Display *xlibdpy, *xkbdpy;
26
27 /* The list of key bindings */
28 struct bindings_head *bindings;
29
30 /* The list of exec-lines */
31 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
32
33 /* The list of assignments */
34 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
35
36 /* The list of workspace assignments (which workspace should end up on which
37  * output) */
38 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
39
40 /* We hope that those are supported and set them to true */
41 bool xcursor_supported = true;
42 bool xkb_supported = true;
43
44 /*
45  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
46  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
47  *
48  */
49 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
50     /* empty, because xcb_prepare_cb and xcb_check_cb are used */
51 }
52
53 /*
54  * Flush before blocking (and waiting for new events)
55  *
56  */
57 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
58     xcb_flush(conn);
59 }
60
61 /*
62  * Instead of polling the X connection socket we leave this to
63  * xcb_poll_for_event() which knows better than we can ever know.
64  *
65  */
66 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
67     xcb_generic_event_t *event;
68
69     while ((event = xcb_poll_for_event(conn)) != NULL) {
70         if (event->response_type == 0) {
71             ELOG("X11 Error received! sequence %x\n", event->sequence);
72             continue;
73         }
74
75         /* Strip off the highest bit (set if the event is generated) */
76         int type = (event->response_type & 0x7F);
77
78         handle_event(type, event);
79
80         free(event);
81     }
82 }
83
84
85 /*
86  * When using xmodmap to change the keyboard mapping, this event
87  * is only sent via XKB. Therefore, we need this special handler.
88  *
89  */
90 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
91     DLOG("Handling XKB event\n");
92     XkbEvent ev;
93
94     /* When using xmodmap, every change (!) gets an own event.
95      * Therefore, we just read all events and only handle the
96      * mapping_notify once. */
97     bool mapping_changed = false;
98     while (XPending(xkbdpy)) {
99         XNextEvent(xkbdpy, (XEvent*)&ev);
100         /* While we should never receive a non-XKB event,
101          * better do sanity checking */
102         if (ev.type != xkb_event_base)
103             continue;
104
105         if (ev.any.xkb_type == XkbMapNotify) {
106             mapping_changed = true;
107             continue;
108         }
109
110         if (ev.any.xkb_type != XkbStateNotify) {
111             ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
112             continue;
113         }
114
115         /* See The XKB Extension: Library Specification, section 14.1 */
116         /* We check if the current group (each group contains
117          * two levels) has been changed. Mode_switch activates
118          * group XkbGroup2Index */
119         if (xkb_current_group == ev.state.group)
120             continue;
121
122         xkb_current_group = ev.state.group;
123
124         if (ev.state.group == XkbGroup2Index) {
125             DLOG("Mode_switch enabled\n");
126             grab_all_keys(conn, true);
127         }
128
129         if (ev.state.group == XkbGroup1Index) {
130             DLOG("Mode_switch disabled\n");
131             ungrab_all_keys(conn);
132             grab_all_keys(conn, false);
133         }
134     }
135
136     if (!mapping_changed)
137         return;
138
139     DLOG("Keyboard mapping changed, updating keybindings\n");
140     xcb_key_symbols_free(keysyms);
141     keysyms = xcb_key_symbols_alloc(conn);
142
143     xcb_get_numlock_mask(conn);
144
145     ungrab_all_keys(conn);
146     DLOG("Re-grabbing...\n");
147     translate_keysyms();
148     grab_all_keys(conn, (xkb_current_group == XkbGroup2Index));
149     DLOG("Done\n");
150 }
151
152 int main(int argc, char *argv[]) {
153     //parse_cmd("[ foo ] attach, attach ; focus");
154     int screens;
155     char *override_configpath = NULL;
156     bool autostart = true;
157     char *layout_path = NULL;
158     bool delete_layout_path = false;
159     bool only_check_config = false;
160     bool force_xinerama = false;
161     bool disable_signalhandler = false;
162     static struct option long_options[] = {
163         {"no-autostart", no_argument, 0, 'a'},
164         {"config", required_argument, 0, 'c'},
165         {"version", no_argument, 0, 'v'},
166         {"help", no_argument, 0, 'h'},
167         {"layout", required_argument, 0, 'L'},
168         {"restart", required_argument, 0, 0},
169         {"force-xinerama", no_argument, 0, 0},
170         {"disable-signalhandler", no_argument, 0, 0},
171         {0, 0, 0, 0}
172     };
173     int option_index = 0, opt;
174
175     setlocale(LC_ALL, "");
176
177     /* Disable output buffering to make redirects in .xsession actually useful for debugging */
178     if (!isatty(fileno(stdout)))
179         setbuf(stdout, NULL);
180
181     start_argv = argv;
182
183     while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
184         switch (opt) {
185             case 'a':
186                 LOG("Autostart disabled using -a\n");
187                 autostart = false;
188                 break;
189             case 'L':
190                 FREE(layout_path);
191                 layout_path = sstrdup(optarg);
192                 delete_layout_path = false;
193                 break;
194             case 'c':
195                 FREE(override_configpath);
196                 override_configpath = sstrdup(optarg);
197                 break;
198             case 'C':
199                 LOG("Checking configuration file only (-C)\n");
200                 only_check_config = true;
201                 break;
202             case 'v':
203                 printf("i3 version " I3_VERSION " © 2009-2011 Michael Stapelberg and contributors\n");
204                 exit(EXIT_SUCCESS);
205             case 'V':
206                 set_verbosity(true);
207                 break;
208             case 'd':
209                 LOG("Enabling debug loglevel %s\n", optarg);
210                 add_loglevel(optarg);
211                 break;
212             case 'l':
213                 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
214                 break;
215             case 0:
216                 if (strcmp(long_options[option_index].name, "force-xinerama") == 0) {
217                     force_xinerama = true;
218                     ELOG("Using Xinerama instead of RandR. This option should be "
219                          "avoided at all cost because it does not refresh the list "
220                          "of screens, so you cannot configure displays at runtime. "
221                          "Please check if your driver really does not support RandR "
222                          "and disable this option as soon as you can.\n");
223                     break;
224                 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
225                     disable_signalhandler = true;
226                     break;
227                 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
228                     FREE(layout_path);
229                     layout_path = sstrdup(optarg);
230                     delete_layout_path = true;
231                     break;
232                 }
233                 /* fall-through */
234             default:
235                 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
236                 fprintf(stderr, "\n");
237                 fprintf(stderr, "-a: disable autostart\n");
238                 fprintf(stderr, "-L <layoutfile>: load the layout from <layoutfile>\n");
239                 fprintf(stderr, "-v: display version and exit\n");
240                 fprintf(stderr, "-V: enable verbose mode\n");
241                 fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
242                 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
243                 fprintf(stderr, "-C: check configuration file and exit\n");
244                 fprintf(stderr, "--force-xinerama: Use Xinerama instead of RandR. This "
245                                 "option should only be used if you are stuck with the "
246                                 "nvidia closed source driver which does not support RandR.\n");
247                 exit(EXIT_FAILURE);
248         }
249     }
250
251     LOG("i3 (tree) version " I3_VERSION " starting\n");
252
253     conn = xcb_connect(NULL, &screens);
254     if (xcb_connection_has_error(conn))
255         errx(EXIT_FAILURE, "Cannot open display\n");
256
257     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
258     root = root_screen->root;
259     root_depth = root_screen->root_depth;
260     xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
261
262     load_configuration(conn, override_configpath, false);
263     if (only_check_config) {
264         LOG("Done checking configuration file. Exiting.\n");
265         exit(0);
266     }
267
268     if (config.ipc_socket_path == NULL) {
269         /* Fall back to a file name in /tmp/ based on the PID */
270         if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
271             config.ipc_socket_path = get_process_filename("ipc-socket");
272         else
273             config.ipc_socket_path = sstrdup(config.ipc_socket_path);
274     }
275
276     uint32_t mask = XCB_CW_EVENT_MASK;
277     uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
278                           XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
279                                                                            projector), the root window gets a
280                                                                            ConfigureNotify */
281                           XCB_EVENT_MASK_POINTER_MOTION |
282                           XCB_EVENT_MASK_PROPERTY_CHANGE |
283                           XCB_EVENT_MASK_ENTER_WINDOW };
284     xcb_void_cookie_t cookie;
285     cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
286     check_error(conn, cookie, "Another window manager seems to be running");
287
288     xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
289     if (greply == NULL) {
290         ELOG("Could not get geometry of the root window, exiting\n");
291         return 1;
292     }
293     DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
294
295     /* Place requests for the atoms we need as soon as possible */
296     #define xmacro(atom) \
297         xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
298     #include "atoms.xmacro"
299     #undef xmacro
300
301     /* Initialize the Xlib connection */
302     xlibdpy = xkbdpy = XOpenDisplay(NULL);
303
304     /* Try to load the X cursors and initialize the XKB extension */
305     if (xlibdpy == NULL) {
306         ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
307         xcursor_supported = false;
308         xkb_supported = false;
309     } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
310         ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
311         return 1;
312     } else {
313         xcursor_load_cursors();
314         /*init_xkb();*/
315     }
316
317     if (xkb_supported) {
318         int errBase,
319             major = XkbMajorVersion,
320             minor = XkbMinorVersion;
321
322         if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
323             fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
324             return 1;
325         }
326
327         int i1;
328         if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
329             fprintf(stderr, "XKB not supported by X-server\n");
330             return 1;
331         }
332         /* end of ugliness */
333
334         if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
335                              XkbMapNotifyMask | XkbStateNotifyMask,
336                              XkbMapNotifyMask | XkbStateNotifyMask)) {
337             fprintf(stderr, "Could not set XKB event mask\n");
338             return 1;
339         }
340     }
341
342     /* Setup NetWM atoms */
343     #define xmacro(name) \
344         do { \
345             xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
346             if (!reply) { \
347                 ELOG("Could not get atom " #name "\n"); \
348                 exit(-1); \
349             } \
350             A_ ## name = reply->atom; \
351             free(reply); \
352         } while (0);
353     #include "atoms.xmacro"
354     #undef xmacro
355
356     property_handlers_init();
357
358     /* Set up the atoms we support */
359     xcb_atom_t supported_atoms[] = {
360 #define xmacro(atom) A_ ## atom,
361 #include "atoms.xmacro"
362 #undef xmacro
363     };
364     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, A_ATOM, 32, 7, supported_atoms);
365     /* Set up the window manager’s name */
366     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, A_WINDOW, 32, 1, &root);
367     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
368
369     keysyms = xcb_key_symbols_alloc(conn);
370
371     xcb_get_numlock_mask(conn);
372
373     translate_keysyms();
374     grab_all_keys(conn, false);
375
376     bool needs_tree_init = true;
377     if (layout_path) {
378         LOG("Trying to restore the layout from %s...", layout_path);
379         needs_tree_init = !tree_restore(layout_path, greply);
380         if (delete_layout_path)
381             unlink(layout_path);
382         free(layout_path);
383     }
384     if (needs_tree_init)
385         tree_init(greply);
386
387     free(greply);
388
389     if (force_xinerama) {
390         xinerama_init();
391     } else {
392         DLOG("Checking for XRandR...\n");
393         randr_init(&randr_base);
394     }
395
396     tree_render();
397
398     struct ev_loop *loop = ev_loop_new(0);
399     if (loop == NULL)
400             die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
401
402     /* Create the UNIX domain socket for IPC */
403     int ipc_socket = ipc_create_socket(config.ipc_socket_path);
404     if (ipc_socket == -1) {
405         ELOG("Could not create the IPC socket, IPC disabled\n");
406     } else {
407         free(config.ipc_socket_path);
408         struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
409         ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
410         ev_io_start(loop, ipc_io);
411     }
412
413     /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
414     x_set_i3_atoms();
415
416     struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
417     struct ev_io *xkb = scalloc(sizeof(struct ev_io));
418     struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
419     struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
420
421     ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
422     ev_io_start(loop, xcb_watcher);
423
424
425     if (xkb_supported) {
426         ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
427         ev_io_start(loop, xkb);
428
429         /* Flush the buffer so that libev can properly get new events */
430         XFlush(xkbdpy);
431     }
432
433     ev_check_init(xcb_check, xcb_check_cb);
434     ev_check_start(loop, xcb_check);
435
436     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
437     ev_prepare_start(loop, xcb_prepare);
438
439     xcb_flush(conn);
440
441     manage_existing_windows(root);
442
443     if (!disable_signalhandler)
444         setup_signal_handler();
445
446     /* Ignore SIGPIPE to survive errors when an IPC client disconnects
447      * while we are sending him a message */
448     signal(SIGPIPE, SIG_IGN);
449
450     /* Autostarting exec-lines */
451     if (autostart) {
452         struct Autostart *exec;
453         TAILQ_FOREACH(exec, &autostarts, autostarts) {
454             LOG("auto-starting %s\n", exec->command);
455             start_application(exec->command);
456         }
457     }
458
459     ev_loop(loop, 0);
460 }