]> git.sur5r.net Git - i3/i3/blob - src/main.c
Replace http:// with https:// where applicable
[i3/i3] / src / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * main.c: Initialization, main loop
8  *
9  */
10 #include "all.h"
11
12 #include <ev.h>
13 #include <fcntl.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <sys/time.h>
18 #include <sys/resource.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <libgen.h>
22 #include "shmlog.h"
23
24 #ifdef I3_ASAN_ENABLED
25 #include <sanitizer/lsan_interface.h>
26 #endif
27
28 #include "sd-daemon.h"
29
30 /* The original value of RLIMIT_CORE when i3 was started. We need to restore
31  * this before starting any other process, since we set RLIMIT_CORE to
32  * RLIM_INFINITY for i3 debugging versions. */
33 struct rlimit original_rlimit_core;
34
35 /** The number of file descriptors passed via socket activation. */
36 int listen_fds;
37
38 /* We keep the xcb_check watcher around to be able to enable and disable it
39  * temporarily for drag_pointer(). */
40 static struct ev_check *xcb_check;
41
42 extern Con *focused;
43
44 char **start_argv;
45
46 xcb_connection_t *conn;
47 /* The screen (0 when you are using DISPLAY=:0) of the connection 'conn' */
48 int conn_screen;
49
50 /* Display handle for libstartup-notification */
51 SnDisplay *sndisplay;
52
53 /* The last timestamp we got from X11 (timestamps are included in some events
54  * and are used for some things, like determining a unique ID in startup
55  * notification). */
56 xcb_timestamp_t last_timestamp = XCB_CURRENT_TIME;
57
58 xcb_screen_t *root_screen;
59 xcb_window_t root;
60
61 /* Color depth, visual id and colormap to use when creating windows and
62  * pixmaps. Will use 32 bit depth and an appropriate visual, if available,
63  * otherwise the root window’s default (usually 24 bit TrueColor). */
64 uint8_t root_depth;
65 xcb_visualtype_t *visual_type;
66 xcb_colormap_t colormap;
67
68 struct ev_loop *main_loop;
69
70 xcb_key_symbols_t *keysyms;
71
72 /* Default shmlog size if not set by user. */
73 const int default_shmlog_size = 25 * 1024 * 1024;
74
75 /* The list of key bindings */
76 struct bindings_head *bindings;
77
78 /* The list of exec-lines */
79 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
80
81 /* The list of exec_always lines */
82 struct autostarts_always_head autostarts_always = TAILQ_HEAD_INITIALIZER(autostarts_always);
83
84 /* The list of assignments */
85 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
86
87 /* The list of workspace assignments (which workspace should end up on which
88  * output) */
89 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
90
91 /* We hope that those are supported and set them to true */
92 bool xcursor_supported = true;
93 bool xkb_supported = true;
94
95 /*
96  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
97  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
98  *
99  */
100 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
101     /* empty, because xcb_prepare_cb and xcb_check_cb are used */
102 }
103
104 /*
105  * Flush before blocking (and waiting for new events)
106  *
107  */
108 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
109     xcb_flush(conn);
110 }
111
112 /*
113  * Instead of polling the X connection socket we leave this to
114  * xcb_poll_for_event() which knows better than we can ever know.
115  *
116  */
117 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
118     xcb_generic_event_t *event;
119
120     while ((event = xcb_poll_for_event(conn)) != NULL) {
121         if (event->response_type == 0) {
122             if (event_is_ignored(event->sequence, 0))
123                 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
124             else {
125                 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
126                 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
127                      error->sequence, error->error_code);
128             }
129             free(event);
130             continue;
131         }
132
133         /* Strip off the highest bit (set if the event is generated) */
134         int type = (event->response_type & 0x7F);
135
136         handle_event(type, event);
137
138         free(event);
139     }
140 }
141
142 /*
143  * Enable or disable the main X11 event handling function.
144  * This is used by drag_pointer() which has its own, modal event handler, which
145  * takes precedence over the normal event handler.
146  *
147  */
148 void main_set_x11_cb(bool enable) {
149     DLOG("Setting main X11 callback to enabled=%d\n", enable);
150     if (enable) {
151         ev_check_start(main_loop, xcb_check);
152         /* Trigger the watcher explicitly to handle all remaining X11 events.
153          * drag_pointer()’s event handler exits in the middle of the loop. */
154         ev_feed_event(main_loop, xcb_check, 0);
155     } else {
156         ev_check_stop(main_loop, xcb_check);
157     }
158 }
159
160 /*
161  * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
162  *
163  */
164 static void i3_exit(void) {
165 /* We need ev >= 4 for the following code. Since it is not *that* important (it
166  * only makes sure that there are no i3-nagbar instances left behind) we still
167  * support old systems with libev 3. */
168 #if EV_VERSION_MAJOR >= 4
169     ev_loop_destroy(main_loop);
170 #endif
171
172     if (*shmlogname != '\0') {
173         fprintf(stderr, "Closing SHM log \"%s\"\n", shmlogname);
174         fflush(stderr);
175         shm_unlink(shmlogname);
176     }
177 }
178
179 /*
180  * (One-shot) Handler for all signals with default action "Term", see signal(7)
181  *
182  * Unlinks the SHM log and re-raises the signal.
183  *
184  */
185 static void handle_signal(int sig, siginfo_t *info, void *data) {
186     if (*shmlogname != '\0') {
187         shm_unlink(shmlogname);
188     }
189     raise(sig);
190 }
191
192 int main(int argc, char *argv[]) {
193     /* Keep a symbol pointing to the I3_VERSION string constant so that we have
194      * it in gdb backtraces. */
195     static const char *_i3_version __attribute__((used)) = I3_VERSION;
196     char *override_configpath = NULL;
197     bool autostart = true;
198     char *layout_path = NULL;
199     bool delete_layout_path = false;
200     bool force_xinerama = false;
201     bool disable_randr15 = false;
202     char *fake_outputs = NULL;
203     bool disable_signalhandler = false;
204     bool only_check_config = false;
205     static struct option long_options[] = {
206         {"no-autostart", no_argument, 0, 'a'},
207         {"config", required_argument, 0, 'c'},
208         {"version", no_argument, 0, 'v'},
209         {"moreversion", no_argument, 0, 'm'},
210         {"more-version", no_argument, 0, 'm'},
211         {"more_version", no_argument, 0, 'm'},
212         {"help", no_argument, 0, 'h'},
213         {"layout", required_argument, 0, 'L'},
214         {"restart", required_argument, 0, 0},
215         {"force-xinerama", no_argument, 0, 0},
216         {"force_xinerama", no_argument, 0, 0},
217         {"disable-randr15", no_argument, 0, 0},
218         {"disable_randr15", no_argument, 0, 0},
219         {"disable-signalhandler", no_argument, 0, 0},
220         {"shmlog-size", required_argument, 0, 0},
221         {"shmlog_size", required_argument, 0, 0},
222         {"get-socketpath", no_argument, 0, 0},
223         {"get_socketpath", no_argument, 0, 0},
224         {"fake_outputs", required_argument, 0, 0},
225         {"fake-outputs", required_argument, 0, 0},
226         {"force-old-config-parser-v4.4-only", no_argument, 0, 0},
227         {0, 0, 0, 0}};
228     int option_index = 0, opt;
229
230     setlocale(LC_ALL, "");
231
232     /* Get the RLIMIT_CORE limit at startup time to restore this before
233      * starting processes. */
234     getrlimit(RLIMIT_CORE, &original_rlimit_core);
235
236     /* Disable output buffering to make redirects in .xsession actually useful for debugging */
237     if (!isatty(fileno(stdout)))
238         setbuf(stdout, NULL);
239
240     srand(time(NULL));
241
242     /* Init logging *before* initializing debug_build to guarantee early
243      * (file) logging. */
244     init_logging();
245
246     /* On release builds, disable SHM logging by default. */
247     shmlog_size = (is_debug_build() || strstr(argv[0], "i3-with-shmlog") != NULL ? default_shmlog_size : 0);
248
249     start_argv = argv;
250
251     while ((opt = getopt_long(argc, argv, "c:CvmaL:hld:V", long_options, &option_index)) != -1) {
252         switch (opt) {
253             case 'a':
254                 LOG("Autostart disabled using -a\n");
255                 autostart = false;
256                 break;
257             case 'L':
258                 FREE(layout_path);
259                 layout_path = sstrdup(optarg);
260                 delete_layout_path = false;
261                 break;
262             case 'c':
263                 FREE(override_configpath);
264                 override_configpath = sstrdup(optarg);
265                 break;
266             case 'C':
267                 LOG("Checking configuration file only (-C)\n");
268                 only_check_config = true;
269                 break;
270             case 'v':
271                 printf("i3 version %s © 2009 Michael Stapelberg and contributors\n", i3_version);
272                 exit(EXIT_SUCCESS);
273                 break;
274             case 'm':
275                 printf("Binary i3 version:  %s © 2009 Michael Stapelberg and contributors\n", i3_version);
276                 display_running_version();
277                 exit(EXIT_SUCCESS);
278                 break;
279             case 'V':
280                 set_verbosity(true);
281                 break;
282             case 'd':
283                 LOG("Enabling debug logging\n");
284                 set_debug_logging(true);
285                 break;
286             case 'l':
287                 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
288                 break;
289             case 0:
290                 if (strcmp(long_options[option_index].name, "force-xinerama") == 0 ||
291                     strcmp(long_options[option_index].name, "force_xinerama") == 0) {
292                     force_xinerama = true;
293                     ELOG("Using Xinerama instead of RandR. This option should be "
294                          "avoided at all cost because it does not refresh the list "
295                          "of screens, so you cannot configure displays at runtime. "
296                          "Please check if your driver really does not support RandR "
297                          "and disable this option as soon as you can.\n");
298                     break;
299                 } else if (strcmp(long_options[option_index].name, "disable-randr15") == 0 ||
300                            strcmp(long_options[option_index].name, "disable_randr15") == 0) {
301                     disable_randr15 = true;
302                     break;
303                 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
304                     disable_signalhandler = true;
305                     break;
306                 } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 ||
307                            strcmp(long_options[option_index].name, "get_socketpath") == 0) {
308                     char *socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
309                     if (socket_path) {
310                         printf("%s\n", socket_path);
311                         exit(EXIT_SUCCESS);
312                     }
313
314                     exit(EXIT_FAILURE);
315                 } else if (strcmp(long_options[option_index].name, "shmlog-size") == 0 ||
316                            strcmp(long_options[option_index].name, "shmlog_size") == 0) {
317                     shmlog_size = atoi(optarg);
318                     /* Re-initialize logging immediately to get as many
319                      * logmessages as possible into the SHM log. */
320                     init_logging();
321                     LOG("Limiting SHM log size to %d bytes\n", shmlog_size);
322                     break;
323                 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
324                     FREE(layout_path);
325                     layout_path = sstrdup(optarg);
326                     delete_layout_path = true;
327                     break;
328                 } else if (strcmp(long_options[option_index].name, "fake-outputs") == 0 ||
329                            strcmp(long_options[option_index].name, "fake_outputs") == 0) {
330                     LOG("Initializing fake outputs: %s\n", optarg);
331                     fake_outputs = sstrdup(optarg);
332                     break;
333                 } else if (strcmp(long_options[option_index].name, "force-old-config-parser-v4.4-only") == 0) {
334                     ELOG("You are passing --force-old-config-parser-v4.4-only, but that flag was removed by now.\n");
335                     break;
336                 }
337             /* fall-through */
338             default:
339                 fprintf(stderr, "Usage: %s [-c configfile] [-d all] [-a] [-v] [-V] [-C]\n", argv[0]);
340                 fprintf(stderr, "\n");
341                 fprintf(stderr, "\t-a          disable autostart ('exec' lines in config)\n");
342                 fprintf(stderr, "\t-c <file>   use the provided configfile instead\n");
343                 fprintf(stderr, "\t-C          validate configuration file and exit\n");
344                 fprintf(stderr, "\t-d all      enable debug output\n");
345                 fprintf(stderr, "\t-L <file>   path to the serialized layout during restarts\n");
346                 fprintf(stderr, "\t-v          display version and exit\n");
347                 fprintf(stderr, "\t-V          enable verbose mode\n");
348                 fprintf(stderr, "\n");
349                 fprintf(stderr, "\t--force-xinerama\n"
350                                 "\tUse Xinerama instead of RandR.\n"
351                                 "\tThis option should only be used if you are stuck with the\n"
352                                 "\told nVidia closed source driver (older than 302.17), which does\n"
353                                 "\tnot support RandR.\n");
354                 fprintf(stderr, "\n");
355                 fprintf(stderr, "\t--get-socketpath\n"
356                                 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
357                 fprintf(stderr, "\n");
358                 fprintf(stderr, "\t--shmlog-size <limit>\n"
359                                 "\tLimits the size of the i3 SHM log to <limit> bytes. Setting this\n"
360                                 "\tto 0 disables SHM logging entirely.\n"
361                                 "\tThe default is %d bytes.\n",
362                         shmlog_size);
363                 fprintf(stderr, "\n");
364                 fprintf(stderr, "If you pass plain text arguments, i3 will interpret them as a command\n"
365                                 "to send to a currently running i3 (like i3-msg). This allows you to\n"
366                                 "use nice and logical commands, such as:\n"
367                                 "\n"
368                                 "\ti3 border none\n"
369                                 "\ti3 floating toggle\n"
370                                 "\ti3 kill window\n"
371                                 "\n");
372                 exit(EXIT_FAILURE);
373         }
374     }
375
376     if (only_check_config) {
377         exit(parse_configuration(override_configpath, false) ? 0 : 1);
378     }
379
380     /* If the user passes more arguments, we act like i3-msg would: Just send
381      * the arguments as an IPC message to i3. This allows for nice semantic
382      * commands such as 'i3 border none'. */
383     if (optind < argc) {
384         /* We enable verbose mode so that the user knows what’s going on.
385          * This should make it easier to find mistakes when the user passes
386          * arguments by mistake. */
387         set_verbosity(true);
388
389         LOG("Additional arguments passed. Sending them as a command to i3.\n");
390         char *payload = NULL;
391         while (optind < argc) {
392             if (!payload) {
393                 payload = sstrdup(argv[optind]);
394             } else {
395                 char *both;
396                 sasprintf(&both, "%s %s", payload, argv[optind]);
397                 free(payload);
398                 payload = both;
399             }
400             optind++;
401         }
402         DLOG("Command is: %s (%zd bytes)\n", payload, strlen(payload));
403         char *socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
404         if (!socket_path) {
405             ELOG("Could not get i3 IPC socket path\n");
406             return 1;
407         }
408
409         int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
410         if (sockfd == -1)
411             err(EXIT_FAILURE, "Could not create socket");
412
413         struct sockaddr_un addr;
414         memset(&addr, 0, sizeof(struct sockaddr_un));
415         addr.sun_family = AF_LOCAL;
416         strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
417         FREE(socket_path);
418         if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
419             err(EXIT_FAILURE, "Could not connect to i3");
420
421         if (ipc_send_message(sockfd, strlen(payload), I3_IPC_MESSAGE_TYPE_RUN_COMMAND,
422                              (uint8_t *)payload) == -1)
423             err(EXIT_FAILURE, "IPC: write()");
424         FREE(payload);
425
426         uint32_t reply_length;
427         uint32_t reply_type;
428         uint8_t *reply;
429         int ret;
430         if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
431             if (ret == -1)
432                 err(EXIT_FAILURE, "IPC: read()");
433             return 1;
434         }
435         if (reply_type != I3_IPC_REPLY_TYPE_COMMAND)
436             errx(EXIT_FAILURE, "IPC: received reply of type %d but expected %d (COMMAND)", reply_type, I3_IPC_REPLY_TYPE_COMMAND);
437         printf("%.*s\n", reply_length, reply);
438         FREE(reply);
439         return 0;
440     }
441
442     /* Enable logging to handle the case when the user did not specify --shmlog-size */
443     init_logging();
444
445     /* Try to enable core dumps by default when running a debug build */
446     if (is_debug_build()) {
447         struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY};
448         setrlimit(RLIMIT_CORE, &limit);
449
450         /* The following code is helpful, but not required. We thus don’t pay
451          * much attention to error handling, non-linux or other edge cases. */
452         LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
453         size_t cwd_size = 1024;
454         char *cwd = smalloc(cwd_size);
455         char *cwd_ret;
456         while ((cwd_ret = getcwd(cwd, cwd_size)) == NULL && errno == ERANGE) {
457             cwd_size = cwd_size * 2;
458             cwd = srealloc(cwd, cwd_size);
459         }
460         if (cwd_ret != NULL)
461             LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
462         int patternfd;
463         if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
464             memset(cwd, '\0', cwd_size);
465             if (read(patternfd, cwd, cwd_size) > 0)
466                 /* a trailing newline is included in cwd */
467                 LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
468             close(patternfd);
469         }
470         free(cwd);
471     }
472
473     LOG("i3 %s starting\n", i3_version);
474
475     conn = xcb_connect(NULL, &conn_screen);
476     if (xcb_connection_has_error(conn))
477         errx(EXIT_FAILURE, "Cannot open display\n");
478
479     sndisplay = sn_xcb_display_new(conn, NULL, NULL);
480
481     /* Initialize the libev event loop. This needs to be done before loading
482      * the config file because the parser will install an ev_child watcher
483      * for the nagbar when config errors are found. */
484     main_loop = EV_DEFAULT;
485     if (main_loop == NULL)
486         die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
487
488     root_screen = xcb_aux_get_screen(conn, conn_screen);
489     root = root_screen->root;
490
491 /* Place requests for the atoms we need as soon as possible */
492 #define xmacro(atom) \
493     xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
494 #include "atoms.xmacro"
495 #undef xmacro
496
497     root_depth = root_screen->root_depth;
498     colormap = root_screen->default_colormap;
499     visual_type = xcb_aux_find_visual_by_attrs(root_screen, -1, 32);
500     if (visual_type != NULL) {
501         root_depth = xcb_aux_get_depth_of_visual(root_screen, visual_type->visual_id);
502         colormap = xcb_generate_id(conn);
503
504         xcb_void_cookie_t cm_cookie = xcb_create_colormap_checked(conn,
505                                                                   XCB_COLORMAP_ALLOC_NONE,
506                                                                   colormap,
507                                                                   root,
508                                                                   visual_type->visual_id);
509
510         xcb_generic_error_t *error = xcb_request_check(conn, cm_cookie);
511         if (error != NULL) {
512             ELOG("Could not create colormap. Error code: %d\n", error->error_code);
513             exit(EXIT_FAILURE);
514         }
515     } else {
516         visual_type = get_visualtype(root_screen);
517     }
518
519     init_dpi();
520
521     DLOG("root_depth = %d, visual_id = 0x%08x.\n", root_depth, visual_type->visual_id);
522     DLOG("root_screen->height_in_pixels = %d, root_screen->height_in_millimeters = %d\n",
523          root_screen->height_in_pixels, root_screen->height_in_millimeters);
524     DLOG("One logical pixel corresponds to %d physical pixels on this display.\n", logical_px(1));
525
526     xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
527     xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
528
529 /* Setup NetWM atoms */
530 #define xmacro(name)                                                                       \
531     do {                                                                                   \
532         xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
533         if (!reply) {                                                                      \
534             ELOG("Could not get atom " #name "\n");                                        \
535             exit(-1);                                                                      \
536         }                                                                                  \
537         A_##name = reply->atom;                                                            \
538         free(reply);                                                                       \
539     } while (0);
540 #include "atoms.xmacro"
541 #undef xmacro
542
543     load_configuration(conn, override_configpath, false);
544
545     if (config.ipc_socket_path == NULL) {
546         /* Fall back to a file name in /tmp/ based on the PID */
547         if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
548             config.ipc_socket_path = get_process_filename("ipc-socket");
549         else
550             config.ipc_socket_path = sstrdup(config.ipc_socket_path);
551     }
552
553     xcb_void_cookie_t cookie;
554     cookie = xcb_change_window_attributes_checked(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
555     xcb_generic_error_t *error = xcb_request_check(conn, cookie);
556     if (error != NULL) {
557         ELOG("Another window manager seems to be running (X error %d)\n", error->error_code);
558 #ifdef I3_ASAN_ENABLED
559         __lsan_do_leak_check();
560 #endif
561         return 1;
562     }
563
564     xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
565     if (greply == NULL) {
566         ELOG("Could not get geometry of the root window, exiting\n");
567         return 1;
568     }
569     DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
570
571     xcursor_load_cursors();
572
573     /* Set a cursor for the root window (otherwise the root window will show no
574        cursor until the first client is launched). */
575     if (xcursor_supported)
576         xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
577     else
578         xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
579
580     const xcb_query_extension_reply_t *extreply;
581     extreply = xcb_get_extension_data(conn, &xcb_xkb_id);
582     xkb_supported = extreply->present;
583     if (!extreply->present) {
584         DLOG("xkb is not present on this server\n");
585     } else {
586         DLOG("initializing xcb-xkb\n");
587         xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
588         xcb_xkb_select_events(conn,
589                               XCB_XKB_ID_USE_CORE_KBD,
590                               XCB_XKB_EVENT_TYPE_STATE_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY,
591                               0,
592                               XCB_XKB_EVENT_TYPE_STATE_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY,
593                               0xff,
594                               0xff,
595                               NULL);
596
597         /* Setting both, XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE and
598          * XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED, will lead to the
599          * X server sending us the full XKB state in KeyPress and KeyRelease:
600          * https://sources.debian.net/src/xorg-server/2:1.17.2-1.1/xkb/xkbEvents.c/?hl=927#L927
601          */
602         xcb_xkb_per_client_flags_reply_t *pcf_reply;
603         /* The last three parameters are unset because they are only relevant
604          * when using a feature called “automatic reset of boolean controls”:
605          * https://www.x.org/releases/X11R7.7/doc/kbproto/xkbproto.html#Automatic_Reset_of_Boolean_Controls
606          * */
607         pcf_reply = xcb_xkb_per_client_flags_reply(
608             conn,
609             xcb_xkb_per_client_flags(
610                 conn,
611                 XCB_XKB_ID_USE_CORE_KBD,
612                 XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE | XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED,
613                 XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE | XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED,
614                 0 /* uint32_t ctrlsToChange */,
615                 0 /* uint32_t autoCtrls */,
616                 0 /* uint32_t autoCtrlsValues */),
617             NULL);
618         if (pcf_reply == NULL ||
619             !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE)) {
620             ELOG("Could not set XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE\n");
621         }
622         if (pcf_reply == NULL ||
623             !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED)) {
624             ELOG("Could not set XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED\n");
625         }
626         free(pcf_reply);
627         xkb_base = extreply->first_event;
628     }
629
630     restore_connect();
631
632     property_handlers_init();
633
634     ewmh_setup_hints();
635
636     keysyms = xcb_key_symbols_alloc(conn);
637
638     xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
639
640     if (!load_keymap())
641         die("Could not load keymap\n");
642
643     translate_keysyms();
644     grab_all_keys(conn);
645
646     bool needs_tree_init = true;
647     if (layout_path != NULL) {
648         LOG("Trying to restore the layout from \"%s\".\n", layout_path);
649         needs_tree_init = !tree_restore(layout_path, greply);
650         if (delete_layout_path) {
651             unlink(layout_path);
652             const char *dir = dirname(layout_path);
653             /* possibly fails with ENOTEMPTY if there are files (or
654              * sockets) left. */
655             rmdir(dir);
656         }
657     }
658     if (needs_tree_init)
659         tree_init(greply);
660
661     free(greply);
662
663     /* Setup fake outputs for testing */
664     if (fake_outputs == NULL && config.fake_outputs != NULL)
665         fake_outputs = config.fake_outputs;
666
667     if (fake_outputs != NULL) {
668         fake_outputs_init(fake_outputs);
669         FREE(fake_outputs);
670         config.fake_outputs = NULL;
671     } else if (force_xinerama || config.force_xinerama) {
672         /* Force Xinerama (for drivers which don't support RandR yet, esp. the
673          * nVidia binary graphics driver), when specified either in the config
674          * file or on command-line */
675         xinerama_init();
676     } else {
677         DLOG("Checking for XRandR...\n");
678         randr_init(&randr_base, disable_randr15 || config.disable_randr15);
679     }
680
681     /* We need to force disabling outputs which have been loaded from the
682      * layout file but are no longer active. This can happen if the output has
683      * been disabled in the short time between writing the restart layout file
684      * and restarting i3. See #2326. */
685     if (layout_path != NULL && randr_base > -1) {
686         Con *con;
687         TAILQ_FOREACH(con, &(croot->nodes_head), nodes) {
688             Output *output;
689             TAILQ_FOREACH(output, &outputs, outputs) {
690                 if (output->active || strcmp(con->name, output_primary_name(output)) != 0)
691                     continue;
692
693                 /* This will correctly correlate the output with its content
694                  * container. We need to make the connection to properly
695                  * disable the output. */
696                 if (output->con == NULL) {
697                     output_init_con(output);
698                     output->changed = false;
699                 }
700
701                 output->to_be_disabled = true;
702                 randr_disable_output(output);
703             }
704         }
705     }
706     FREE(layout_path);
707
708     scratchpad_fix_resolution();
709
710     xcb_query_pointer_reply_t *pointerreply;
711     Output *output = NULL;
712     if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
713         ELOG("Could not query pointer position, using first screen\n");
714     } else {
715         DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
716         output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
717         if (!output) {
718             ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
719                  pointerreply->root_x, pointerreply->root_y);
720             output = get_first_output();
721         }
722
723         con_focus(con_descend_focused(output_get_content(output->con)));
724         free(pointerreply);
725     }
726
727     tree_render();
728
729     /* Create the UNIX domain socket for IPC */
730     int ipc_socket = ipc_create_socket(config.ipc_socket_path);
731     if (ipc_socket == -1) {
732         ELOG("Could not create the IPC socket, IPC disabled\n");
733     } else {
734         struct ev_io *ipc_io = scalloc(1, sizeof(struct ev_io));
735         ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
736         ev_io_start(main_loop, ipc_io);
737     }
738
739     /* Also handle the UNIX domain sockets passed via socket activation. The
740      * parameter 1 means "remove the environment variables", we don’t want to
741      * pass these to child processes. */
742     listen_fds = sd_listen_fds(0);
743     if (listen_fds < 0)
744         ELOG("socket activation: Error in sd_listen_fds\n");
745     else if (listen_fds == 0)
746         DLOG("socket activation: no sockets passed\n");
747     else {
748         int flags;
749         for (int fd = SD_LISTEN_FDS_START;
750              fd < (SD_LISTEN_FDS_START + listen_fds);
751              fd++) {
752             DLOG("socket activation: also listening on fd %d\n", fd);
753
754             /* sd_listen_fds() enables FD_CLOEXEC by default.
755              * However, we need to keep the file descriptors open for in-place
756              * restarting, therefore we explicitly disable FD_CLOEXEC. */
757             if ((flags = fcntl(fd, F_GETFD)) < 0 ||
758                 fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) < 0) {
759                 ELOG("Could not disable FD_CLOEXEC on fd %d\n", fd);
760             }
761
762             struct ev_io *ipc_io = scalloc(1, sizeof(struct ev_io));
763             ev_io_init(ipc_io, ipc_new_client, fd, EV_READ);
764             ev_io_start(main_loop, ipc_io);
765         }
766     }
767
768     /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
769     x_set_i3_atoms();
770     ewmh_update_workarea();
771
772     /* Set the ewmh desktop properties. */
773     ewmh_update_current_desktop();
774     ewmh_update_number_of_desktops();
775     ewmh_update_desktop_names();
776     ewmh_update_desktop_viewport();
777
778     struct ev_io *xcb_watcher = scalloc(1, sizeof(struct ev_io));
779     xcb_check = scalloc(1, sizeof(struct ev_check));
780     struct ev_prepare *xcb_prepare = scalloc(1, sizeof(struct ev_prepare));
781
782     ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
783     ev_io_start(main_loop, xcb_watcher);
784
785     ev_check_init(xcb_check, xcb_check_cb);
786     ev_check_start(main_loop, xcb_check);
787
788     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
789     ev_prepare_start(main_loop, xcb_prepare);
790
791     xcb_flush(conn);
792
793     /* What follows is a fugly consequence of X11 protocol race conditions like
794      * the following: In an i3 in-place restart, i3 will reparent all windows
795      * to the root window, then exec() itself. In the new process, it calls
796      * manage_existing_windows. However, in case any application sent a
797      * generated UnmapNotify message to the WM (as GIMP does), this message
798      * will be handled by i3 *after* managing the window, thus i3 thinks the
799      * window just closed itself. In reality, the message was sent in the time
800      * period where i3 wasn’t running yet.
801      *
802      * To prevent this, we grab the server (disables processing of any other
803      * connections), then discard all pending events (since we didn’t do
804      * anything, there cannot be any meaningful responses), then ungrab the
805      * server. */
806     xcb_grab_server(conn);
807     {
808         xcb_aux_sync(conn);
809         xcb_generic_event_t *event;
810         while ((event = xcb_poll_for_event(conn)) != NULL) {
811             if (event->response_type == 0) {
812                 free(event);
813                 continue;
814             }
815
816             /* Strip off the highest bit (set if the event is generated) */
817             int type = (event->response_type & 0x7F);
818
819             /* We still need to handle MapRequests which are sent in the
820              * timespan starting from when we register as a window manager and
821              * this piece of code which drops events. */
822             if (type == XCB_MAP_REQUEST)
823                 handle_event(type, event);
824
825             free(event);
826         }
827         manage_existing_windows(root);
828     }
829     xcb_ungrab_server(conn);
830
831     if (autostart) {
832         LOG("This is not an in-place restart, copying root window contents to a pixmap\n");
833         xcb_screen_t *root = xcb_aux_get_screen(conn, conn_screen);
834         uint16_t width = root->width_in_pixels;
835         uint16_t height = root->height_in_pixels;
836         xcb_pixmap_t pixmap = xcb_generate_id(conn);
837         xcb_gcontext_t gc = xcb_generate_id(conn);
838
839         xcb_create_pixmap(conn, root->root_depth, pixmap, root->root, width, height);
840
841         xcb_create_gc(conn, gc, root->root,
842                       XCB_GC_FUNCTION | XCB_GC_PLANE_MASK | XCB_GC_FILL_STYLE | XCB_GC_SUBWINDOW_MODE,
843                       (uint32_t[]){XCB_GX_COPY, ~0, XCB_FILL_STYLE_SOLID, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS});
844
845         xcb_copy_area(conn, root->root, pixmap, gc, 0, 0, 0, 0, width, height);
846         xcb_change_window_attributes(conn, root->root, XCB_CW_BACK_PIXMAP, (uint32_t[]){pixmap});
847         xcb_flush(conn);
848         xcb_free_gc(conn, gc);
849         xcb_free_pixmap(conn, pixmap);
850     }
851
852 #if defined(__OpenBSD__)
853     if (pledge("stdio rpath wpath cpath proc exec unix", NULL) == -1)
854         err(EXIT_FAILURE, "pledge");
855 #endif
856
857     struct sigaction action;
858
859     action.sa_sigaction = handle_signal;
860     action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
861     sigemptyset(&action.sa_mask);
862
863     if (!disable_signalhandler)
864         setup_signal_handler();
865     else {
866         /* Catch all signals with default action "Core", see signal(7) */
867         if (sigaction(SIGQUIT, &action, NULL) == -1 ||
868             sigaction(SIGILL, &action, NULL) == -1 ||
869             sigaction(SIGABRT, &action, NULL) == -1 ||
870             sigaction(SIGFPE, &action, NULL) == -1 ||
871             sigaction(SIGSEGV, &action, NULL) == -1)
872             ELOG("Could not setup signal handler.\n");
873     }
874
875     /* Catch all signals with default action "Term", see signal(7) */
876     if (sigaction(SIGHUP, &action, NULL) == -1 ||
877         sigaction(SIGINT, &action, NULL) == -1 ||
878         sigaction(SIGALRM, &action, NULL) == -1 ||
879         sigaction(SIGUSR1, &action, NULL) == -1 ||
880         sigaction(SIGUSR2, &action, NULL) == -1)
881         ELOG("Could not setup signal handler.\n");
882
883     /* Ignore SIGPIPE to survive errors when an IPC client disconnects
884      * while we are sending them a message */
885     signal(SIGPIPE, SIG_IGN);
886
887     /* Autostarting exec-lines */
888     if (autostart) {
889         while (!TAILQ_EMPTY(&autostarts)) {
890             struct Autostart *exec = TAILQ_FIRST(&autostarts);
891
892             LOG("auto-starting %s\n", exec->command);
893             start_application(exec->command, exec->no_startup_id);
894
895             FREE(exec->command);
896             TAILQ_REMOVE(&autostarts, exec, autostarts);
897             FREE(exec);
898         }
899     }
900
901     /* Autostarting exec_always-lines */
902     while (!TAILQ_EMPTY(&autostarts_always)) {
903         struct Autostart *exec_always = TAILQ_FIRST(&autostarts_always);
904
905         LOG("auto-starting (always!) %s\n", exec_always->command);
906         start_application(exec_always->command, exec_always->no_startup_id);
907
908         FREE(exec_always->command);
909         TAILQ_REMOVE(&autostarts_always, exec_always, autostarts_always);
910         FREE(exec_always);
911     }
912
913     /* Start i3bar processes for all configured bars */
914     Barconfig *barconfig;
915     TAILQ_FOREACH(barconfig, &barconfigs, configs) {
916         char *command = NULL;
917         sasprintf(&command, "%s --bar_id=%s --socket=\"%s\"",
918                   barconfig->i3bar_command ? barconfig->i3bar_command : "i3bar",
919                   barconfig->id, current_socketpath);
920         LOG("Starting bar process: %s\n", command);
921         start_application(command, true);
922         free(command);
923     }
924
925     /* Make sure to destroy the event loop to invoke the cleeanup callbacks
926      * when calling exit() */
927     atexit(i3_exit);
928
929     ev_loop(main_loop, 0);
930 }