]> git.sur5r.net Git - i3/i3/blob - src/main.c
958ea65342f5e12619c0cd57b1b541dad4e0d58e
[i3/i3] / src / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * main.c: Initialization, main loop
8  *
9  */
10 #include <ev.h>
11 #include <fcntl.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include "all.h"
16
17 #include "sd-daemon.h"
18
19 static int xkb_event_base;
20
21 int xkb_current_group;
22
23 extern Con *focused;
24
25 char **start_argv;
26
27 xcb_connection_t *conn;
28 /* The screen (0 when you are using DISPLAY=:0) of the connection 'conn' */
29 int conn_screen;
30
31 /* Display handle for libstartup-notification */
32 SnDisplay *sndisplay;
33
34 /* The last timestamp we got from X11 (timestamps are included in some events
35  * and are used for some things, like determining a unique ID in startup
36  * notification). */
37 xcb_timestamp_t last_timestamp = XCB_CURRENT_TIME;
38
39 xcb_screen_t *root_screen;
40 xcb_window_t root;
41 uint8_t root_depth;
42
43 struct ev_loop *main_loop;
44
45 xcb_key_symbols_t *keysyms;
46
47 /* Those are our connections to X11 for use with libXcursor and XKB */
48 Display *xlibdpy, *xkbdpy;
49
50 /* The list of key bindings */
51 struct bindings_head *bindings;
52
53 /* The list of exec-lines */
54 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
55
56 /* The list of exec_always lines */
57 struct autostarts_always_head autostarts_always = TAILQ_HEAD_INITIALIZER(autostarts_always);
58
59 /* The list of assignments */
60 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
61
62 /* The list of workspace assignments (which workspace should end up on which
63  * output) */
64 struct ws_assignments_head ws_assignments = TAILQ_HEAD_INITIALIZER(ws_assignments);
65
66 /* We hope that those are supported and set them to true */
67 bool xcursor_supported = true;
68 bool xkb_supported = true;
69
70 /* This will be set to true when -C is used so that functions can behave
71  * slightly differently. We don’t want i3-nagbar to be started when validating
72  * the config, for example. */
73 bool only_check_config = false;
74
75 /*
76  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
77  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
78  *
79  */
80 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
81     /* empty, because xcb_prepare_cb and xcb_check_cb are used */
82 }
83
84 /*
85  * Flush before blocking (and waiting for new events)
86  *
87  */
88 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
89     xcb_flush(conn);
90 }
91
92 /*
93  * Instead of polling the X connection socket we leave this to
94  * xcb_poll_for_event() which knows better than we can ever know.
95  *
96  */
97 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
98     xcb_generic_event_t *event;
99
100     while ((event = xcb_poll_for_event(conn)) != NULL) {
101         if (event->response_type == 0) {
102             if (event_is_ignored(event->sequence, 0))
103                 DLOG("Expected X11 Error received for sequence %x\n", event->sequence);
104             else {
105                 xcb_generic_error_t *error = (xcb_generic_error_t*)event;
106                 ELOG("X11 Error received! sequence 0x%x, error_code = %d\n",
107                      error->sequence, error->error_code);
108             }
109             free(event);
110             continue;
111         }
112
113         /* Strip off the highest bit (set if the event is generated) */
114         int type = (event->response_type & 0x7F);
115
116         handle_event(type, event);
117
118         free(event);
119     }
120 }
121
122
123 /*
124  * When using xmodmap to change the keyboard mapping, this event
125  * is only sent via XKB. Therefore, we need this special handler.
126  *
127  */
128 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
129     DLOG("Handling XKB event\n");
130     XkbEvent ev;
131
132     /* When using xmodmap, every change (!) gets an own event.
133      * Therefore, we just read all events and only handle the
134      * mapping_notify once. */
135     bool mapping_changed = false;
136     while (XPending(xkbdpy)) {
137         XNextEvent(xkbdpy, (XEvent*)&ev);
138         /* While we should never receive a non-XKB event,
139          * better do sanity checking */
140         if (ev.type != xkb_event_base)
141             continue;
142
143         if (ev.any.xkb_type == XkbMapNotify) {
144             mapping_changed = true;
145             continue;
146         }
147
148         if (ev.any.xkb_type != XkbStateNotify) {
149             ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
150             continue;
151         }
152
153         /* See The XKB Extension: Library Specification, section 14.1 */
154         /* We check if the current group (each group contains
155          * two levels) has been changed. Mode_switch activates
156          * group XkbGroup2Index */
157         if (xkb_current_group == ev.state.group)
158             continue;
159
160         xkb_current_group = ev.state.group;
161
162         if (ev.state.group == XkbGroup2Index) {
163             DLOG("Mode_switch enabled\n");
164             grab_all_keys(conn, true);
165         }
166
167         if (ev.state.group == XkbGroup1Index) {
168             DLOG("Mode_switch disabled\n");
169             ungrab_all_keys(conn);
170             grab_all_keys(conn, false);
171         }
172     }
173
174     if (!mapping_changed)
175         return;
176
177     DLOG("Keyboard mapping changed, updating keybindings\n");
178     xcb_key_symbols_free(keysyms);
179     keysyms = xcb_key_symbols_alloc(conn);
180
181     xcb_get_numlock_mask(conn);
182
183     ungrab_all_keys(conn);
184     DLOG("Re-grabbing...\n");
185     translate_keysyms();
186     grab_all_keys(conn, (xkb_current_group == XkbGroup2Index));
187     DLOG("Done\n");
188 }
189
190 /*
191  * Exit handler which destroys the main_loop. Will trigger cleanup handlers.
192  *
193  */
194 static void i3_exit() {
195 /* We need ev >= 4 for the following code. Since it is not *that* important (it
196  * only makes sure that there are no i3-nagbar instances left behind) we still
197  * support old systems with libev 3. */
198 #if EV_VERSION_MAJOR >= 4
199     ev_loop_destroy(main_loop);
200 #endif
201 }
202
203 int main(int argc, char *argv[]) {
204     char *override_configpath = NULL;
205     bool autostart = true;
206     char *layout_path = NULL;
207     bool delete_layout_path = false;
208     bool force_xinerama = false;
209     bool disable_signalhandler = false;
210     static struct option long_options[] = {
211         {"no-autostart", no_argument, 0, 'a'},
212         {"config", required_argument, 0, 'c'},
213         {"version", no_argument, 0, 'v'},
214         {"help", no_argument, 0, 'h'},
215         {"layout", required_argument, 0, 'L'},
216         {"restart", required_argument, 0, 0},
217         {"force-xinerama", no_argument, 0, 0},
218         {"force_xinerama", no_argument, 0, 0},
219         {"disable-signalhandler", no_argument, 0, 0},
220         {"get-socketpath", no_argument, 0, 0},
221         {"get_socketpath", no_argument, 0, 0},
222         {0, 0, 0, 0}
223     };
224     int option_index = 0, opt;
225
226     setlocale(LC_ALL, "");
227
228     /* Disable output buffering to make redirects in .xsession actually useful for debugging */
229     if (!isatty(fileno(stdout)))
230         setbuf(stdout, NULL);
231
232     srand(time(NULL));
233
234     init_logging();
235
236     start_argv = argv;
237
238     while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
239         switch (opt) {
240             case 'a':
241                 LOG("Autostart disabled using -a\n");
242                 autostart = false;
243                 break;
244             case 'L':
245                 FREE(layout_path);
246                 layout_path = sstrdup(optarg);
247                 delete_layout_path = false;
248                 break;
249             case 'c':
250                 FREE(override_configpath);
251                 override_configpath = sstrdup(optarg);
252                 break;
253             case 'C':
254                 LOG("Checking configuration file only (-C)\n");
255                 only_check_config = true;
256                 break;
257             case 'v':
258                 printf("i3 version " I3_VERSION " © 2009-2011 Michael Stapelberg and contributors\n");
259                 exit(EXIT_SUCCESS);
260             case 'V':
261                 set_verbosity(true);
262                 break;
263             case 'd':
264                 LOG("Enabling debug loglevel %s\n", optarg);
265                 add_loglevel(optarg);
266                 break;
267             case 'l':
268                 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
269                 break;
270             case 0:
271                 if (strcmp(long_options[option_index].name, "force-xinerama") == 0 ||
272                     strcmp(long_options[option_index].name, "force_xinerama") == 0) {
273                     force_xinerama = true;
274                     ELOG("Using Xinerama instead of RandR. This option should be "
275                          "avoided at all cost because it does not refresh the list "
276                          "of screens, so you cannot configure displays at runtime. "
277                          "Please check if your driver really does not support RandR "
278                          "and disable this option as soon as you can.\n");
279                     break;
280                 } else if (strcmp(long_options[option_index].name, "disable-signalhandler") == 0) {
281                     disable_signalhandler = true;
282                     break;
283                 } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 ||
284                            strcmp(long_options[option_index].name, "get_socketpath") == 0) {
285                     char *socket_path = socket_path_from_x11();
286                     if (socket_path) {
287                         printf("%s\n", socket_path);
288                         return 0;
289                     }
290
291                     return 1;
292                 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
293                     FREE(layout_path);
294                     layout_path = sstrdup(optarg);
295                     delete_layout_path = true;
296                     break;
297                 }
298                 /* fall-through */
299             default:
300                 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
301                 fprintf(stderr, "\n");
302                 fprintf(stderr, "\t-a          disable autostart ('exec' lines in config)\n");
303                 fprintf(stderr, "\t-c <file>   use the provided configfile instead\n");
304                 fprintf(stderr, "\t-C          validate configuration file and exit\n");
305                 fprintf(stderr, "\t-d <level>  enable debug output with the specified loglevel\n");
306                 fprintf(stderr, "\t-L <file>   path to the serialized layout during restarts\n");
307                 fprintf(stderr, "\t-v          display version and exit\n");
308                 fprintf(stderr, "\t-V          enable verbose mode\n");
309                 fprintf(stderr, "\n");
310                 fprintf(stderr, "\t--force-xinerama\n"
311                                 "\tUse Xinerama instead of RandR.\n"
312                                 "\tThis option should only be used if you are stuck with the\n"
313                                 "\tnvidia closed source driver which does not support RandR.\n");
314                 fprintf(stderr, "\n");
315                 fprintf(stderr, "\t--get-socketpath\n"
316                                 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
317                 fprintf(stderr, "\n");
318                 fprintf(stderr, "If you pass plain text arguments, i3 will interpret them as a command\n"
319                                 "to send to a currently running i3 (like i3-msg). This allows you to\n"
320                                 "use nice and logical commands, such as:\n"
321                                 "\n"
322                                 "\ti3 border none\n"
323                                 "\ti3 floating toggle\n"
324                                 "\ti3 kill window\n"
325                                 "\n");
326                 exit(EXIT_FAILURE);
327         }
328     }
329
330     /* If the user passes more arguments, we act like i3-msg would: Just send
331      * the arguments as an IPC message to i3. This allows for nice semantic
332      * commands such as 'i3 border none'. */
333     if (optind < argc) {
334         /* We enable verbose mode so that the user knows what’s going on.
335          * This should make it easier to find mistakes when the user passes
336          * arguments by mistake. */
337         set_verbosity(true);
338
339         LOG("Additional arguments passed. Sending them as a command to i3.\n");
340         char *payload = NULL;
341         while (optind < argc) {
342             if (!payload) {
343                 payload = sstrdup(argv[optind]);
344             } else {
345                 char *both;
346                 sasprintf(&both, "%s %s", payload, argv[optind]);
347                 free(payload);
348                 payload = both;
349             }
350             optind++;
351         }
352         LOG("Command is: %s (%d bytes)\n", payload, strlen(payload));
353         char *socket_path = socket_path_from_x11();
354         if (!socket_path) {
355             ELOG("Could not get i3 IPC socket path\n");
356             return 1;
357         }
358
359         int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
360         if (sockfd == -1)
361             err(EXIT_FAILURE, "Could not create socket");
362
363         struct sockaddr_un addr;
364         memset(&addr, 0, sizeof(struct sockaddr_un));
365         addr.sun_family = AF_LOCAL;
366         strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
367         if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
368             err(EXIT_FAILURE, "Could not connect to i3");
369
370         if (ipc_send_message(sockfd, strlen(payload), I3_IPC_MESSAGE_TYPE_COMMAND,
371                              (uint8_t*)payload) == -1)
372             err(EXIT_FAILURE, "IPC: write()");
373
374         uint32_t reply_length;
375         uint8_t *reply;
376         int ret;
377         if ((ret = ipc_recv_message(sockfd, I3_IPC_MESSAGE_TYPE_COMMAND,
378                                     &reply_length, &reply)) != 0) {
379             if (ret == -1)
380                 err(EXIT_FAILURE, "IPC: read()");
381             return 1;
382         }
383         printf("%.*s\n", reply_length, reply);
384         return 0;
385     }
386
387     LOG("i3 (tree) version " I3_VERSION " starting\n");
388
389     conn = xcb_connect(NULL, &conn_screen);
390     if (xcb_connection_has_error(conn))
391         errx(EXIT_FAILURE, "Cannot open display\n");
392
393     sndisplay = sn_xcb_display_new(conn, NULL, NULL);
394
395     /* Initialize the libev event loop. This needs to be done before loading
396      * the config file because the parser will install an ev_child watcher
397      * for the nagbar when config errors are found. */
398     main_loop = EV_DEFAULT;
399     if (main_loop == NULL)
400             die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
401
402     root_screen = xcb_aux_get_screen(conn, conn_screen);
403     root = root_screen->root;
404     root_depth = root_screen->root_depth;
405     xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(conn, root);
406     xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(conn, root);
407
408     load_configuration(conn, override_configpath, false);
409     if (only_check_config) {
410         LOG("Done checking configuration file. Exiting.\n");
411         exit(0);
412     }
413
414     if (config.ipc_socket_path == NULL) {
415         /* Fall back to a file name in /tmp/ based on the PID */
416         if ((config.ipc_socket_path = getenv("I3SOCK")) == NULL)
417             config.ipc_socket_path = get_process_filename("ipc-socket");
418         else
419             config.ipc_socket_path = sstrdup(config.ipc_socket_path);
420     }
421
422     uint32_t mask = XCB_CW_EVENT_MASK;
423     uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
424                           XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
425                                                                            projector), the root window gets a
426                                                                            ConfigureNotify */
427                           XCB_EVENT_MASK_POINTER_MOTION |
428                           XCB_EVENT_MASK_PROPERTY_CHANGE |
429                           XCB_EVENT_MASK_ENTER_WINDOW };
430     xcb_void_cookie_t cookie;
431     cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
432     check_error(conn, cookie, "Another window manager seems to be running");
433
434     xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(conn, gcookie, NULL);
435     if (greply == NULL) {
436         ELOG("Could not get geometry of the root window, exiting\n");
437         return 1;
438     }
439     DLOG("root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
440
441     /* Place requests for the atoms we need as soon as possible */
442     #define xmacro(atom) \
443         xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
444     #include "atoms.xmacro"
445     #undef xmacro
446
447     /* Initialize the Xlib connection */
448     xlibdpy = xkbdpy = XOpenDisplay(NULL);
449
450     /* Try to load the X cursors and initialize the XKB extension */
451     if (xlibdpy == NULL) {
452         ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
453         xcursor_supported = false;
454         xkb_supported = false;
455     } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
456         ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
457         return 1;
458     } else {
459         xcursor_load_cursors();
460         /*init_xkb();*/
461     }
462
463     /* Set a cursor for the root window (otherwise the root window will show no
464        cursor until the first client is launched). */
465     if (xcursor_supported)
466         xcursor_set_root_cursor(XCURSOR_CURSOR_POINTER);
467     else xcb_set_root_cursor(XCURSOR_CURSOR_POINTER);
468
469     if (xkb_supported) {
470         int errBase,
471             major = XkbMajorVersion,
472             minor = XkbMinorVersion;
473
474         if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
475             fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
476             return 1;
477         }
478
479         int i1;
480         if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
481             fprintf(stderr, "XKB not supported by X-server\n");
482             return 1;
483         }
484         /* end of ugliness */
485
486         if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
487                              XkbMapNotifyMask | XkbStateNotifyMask,
488                              XkbMapNotifyMask | XkbStateNotifyMask)) {
489             fprintf(stderr, "Could not set XKB event mask\n");
490             return 1;
491         }
492     }
493
494     /* Setup NetWM atoms */
495     #define xmacro(name) \
496         do { \
497             xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
498             if (!reply) { \
499                 ELOG("Could not get atom " #name "\n"); \
500                 exit(-1); \
501             } \
502             A_ ## name = reply->atom; \
503             free(reply); \
504         } while (0);
505     #include "atoms.xmacro"
506     #undef xmacro
507
508     property_handlers_init();
509
510     /* Set up the atoms we support */
511     xcb_atom_t supported_atoms[] = {
512 #define xmacro(atom) A_ ## atom,
513 #include "atoms.xmacro"
514 #undef xmacro
515     };
516     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTED, XCB_ATOM_ATOM, 32, 16, supported_atoms);
517     /* Set up the window manager’s name */
518     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_SUPPORTING_WM_CHECK, XCB_ATOM_WINDOW, 32, 1, &root);
519     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A__NET_WM_NAME, A_UTF8_STRING, 8, strlen("i3"), "i3");
520
521     keysyms = xcb_key_symbols_alloc(conn);
522
523     xcb_get_numlock_mask(conn);
524
525     translate_keysyms();
526     grab_all_keys(conn, false);
527
528     bool needs_tree_init = true;
529     if (layout_path) {
530         LOG("Trying to restore the layout from %s...", layout_path);
531         needs_tree_init = !tree_restore(layout_path, greply);
532         if (delete_layout_path)
533             unlink(layout_path);
534         free(layout_path);
535     }
536     if (needs_tree_init)
537         tree_init(greply);
538
539     free(greply);
540
541     /* Force Xinerama (for drivers which don't support RandR yet, esp. the
542      * nVidia binary graphics driver), when specified either in the config
543      * file or on command-line */
544     if (force_xinerama || config.force_xinerama) {
545         xinerama_init();
546     } else {
547         DLOG("Checking for XRandR...\n");
548         randr_init(&randr_base);
549     }
550
551     xcb_query_pointer_reply_t *pointerreply;
552     Output *output = NULL;
553     if (!(pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL))) {
554         ELOG("Could not query pointer position, using first screen\n");
555         output = get_first_output();
556     } else {
557         DLOG("Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
558         output = get_output_containing(pointerreply->root_x, pointerreply->root_y);
559         if (!output) {
560             ELOG("ERROR: No screen at (%d, %d), starting on the first screen\n",
561                  pointerreply->root_x, pointerreply->root_y);
562             output = get_first_output();
563         }
564
565         con_focus(con_descend_focused(output_get_content(output->con)));
566     }
567
568     tree_render();
569
570     /* Create the UNIX domain socket for IPC */
571     int ipc_socket = ipc_create_socket(config.ipc_socket_path);
572     if (ipc_socket == -1) {
573         ELOG("Could not create the IPC socket, IPC disabled\n");
574     } else {
575         free(config.ipc_socket_path);
576         struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
577         ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
578         ev_io_start(main_loop, ipc_io);
579     }
580
581     /* Also handle the UNIX domain sockets passed via socket activation */
582     int fds = sd_listen_fds(1);
583     if (fds < 0)
584         ELOG("socket activation: Error in sd_listen_fds\n");
585     else if (fds == 0)
586         DLOG("socket activation: no sockets passed\n");
587     else {
588         for (int fd = SD_LISTEN_FDS_START; fd < (SD_LISTEN_FDS_START + fds); fd++) {
589             DLOG("socket activation: also listening on fd %d\n", fd);
590             struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
591             ev_io_init(ipc_io, ipc_new_client, fd, EV_READ);
592             ev_io_start(main_loop, ipc_io);
593         }
594     }
595
596     /* Set up i3 specific atoms like I3_SOCKET_PATH and I3_CONFIG_PATH */
597     x_set_i3_atoms();
598
599     struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
600     struct ev_io *xkb = scalloc(sizeof(struct ev_io));
601     struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
602     struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
603
604     ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
605     ev_io_start(main_loop, xcb_watcher);
606
607
608     if (xkb_supported) {
609         ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
610         ev_io_start(main_loop, xkb);
611
612         /* Flush the buffer so that libev can properly get new events */
613         XFlush(xkbdpy);
614     }
615
616     ev_check_init(xcb_check, xcb_check_cb);
617     ev_check_start(main_loop, xcb_check);
618
619     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
620     ev_prepare_start(main_loop, xcb_prepare);
621
622     xcb_flush(conn);
623
624     manage_existing_windows(root);
625
626     if (!disable_signalhandler)
627         setup_signal_handler();
628
629     /* Ignore SIGPIPE to survive errors when an IPC client disconnects
630      * while we are sending him a message */
631     signal(SIGPIPE, SIG_IGN);
632
633     /* Autostarting exec-lines */
634     if (autostart) {
635         struct Autostart *exec;
636         TAILQ_FOREACH(exec, &autostarts, autostarts) {
637             LOG("auto-starting %s\n", exec->command);
638             start_application(exec->command);
639         }
640     }
641
642     /* Autostarting exec_always-lines */
643     struct Autostart *exec_always;
644     TAILQ_FOREACH(exec_always, &autostarts_always, autostarts_always) {
645         LOG("auto-starting (always!) %s\n", exec_always->command);
646         start_application(exec_always->command);
647     }
648
649     /* Start i3bar processes for all configured bars */
650     Barconfig *barconfig;
651     TAILQ_FOREACH(barconfig, &barconfigs, configs) {
652         char *command = NULL;
653         sasprintf(&command, "i3bar --bar_id=%s --socket=\"%s\"",
654                   barconfig->id, current_socketpath);
655         LOG("Starting bar process: %s\n", command);
656         start_application(command);
657         free(command);
658     }
659
660     /* Make sure to destroy the event loop to invoke the cleeanup callbacks
661      * when calling exit() */
662     atexit(i3_exit);
663
664     ev_loop(main_loop, 0);
665 }