]> git.sur5r.net Git - i3/i3/blob - i3bar/src/main.c
Make i3bar get its config from i3 via IPC
[i3/i3] / i3bar / src / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3bar - an xcb-based status- and ws-bar for i3
5  *
6  * © 2010-2011 Axel Wagner and contributors
7  *
8  * See file LICNSE for license information
9  *
10  */
11 #include <stdio.h>
12 #include <i3/ipc.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <ev.h>
18 #include <getopt.h>
19 #include <glob.h>
20
21 #include "common.h"
22 #include "libi3.h"
23
24 /*
25  * Glob path, i.e. expand ~
26  *
27  */
28 char *expand_path(char *path) {
29     static glob_t globbuf;
30     if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
31         ELOG("glob() failed\n");
32         exit(EXIT_FAILURE);
33     }
34     char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
35     if (result == NULL) {
36         ELOG("malloc() failed: %s\n", strerror(errno));
37         exit(EXIT_FAILURE);
38     }
39     globfree(&globbuf);
40     return result;
41 }
42
43 void print_usage(char *elf_name) {
44     printf("Usage: %s [-s sock_path] [-h] [-v]\n", elf_name);
45     printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
46     printf("-h\t\tDisplay this help-message and exit\n");
47     printf("-v\t\tDisplay version number and exit\n");
48 }
49
50 /*
51  * We watch various signals, that are there to make our application stop.
52  * If we get one of those, we ev_unloop() and invoke the cleanup-routines
53  * in main() with that
54  *
55  */
56 void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
57     switch (watcher->signum) {
58         case SIGTERM:
59             DLOG("Got a SIGTERM, stopping\n");
60             break;
61         case SIGINT:
62             DLOG("Got a SIGINT, stopping\n");
63             break;
64         case SIGHUP:
65             DLOG("Got a SIGHUP, stopping\n");
66     }
67     ev_unloop(main_loop, EVUNLOOP_ALL);
68 }
69
70 int main(int argc, char **argv) {
71     int opt;
72     int option_index = 0;
73     char *socket_path = getenv("I3SOCK");
74     char *i3_default_sock_path = "/tmp/i3-ipc.sock";
75
76     /* Initialize the standard config to use 0 as default */
77     memset(&config, '\0', sizeof(config_t));
78
79     static struct option long_opt[] = {
80         { "socket",               required_argument, 0, 's' },
81         { "bar_id",               required_argument, 0, 0 },
82         { "help",                 no_argument,       0, 'h' },
83         { "version",              no_argument,       0, 'v' },
84         { NULL,                   0,                 0, 0}
85     };
86
87     while ((opt = getopt_long(argc, argv, "s:hv", long_opt, &option_index)) != -1) {
88         switch (opt) {
89             case 's':
90                 socket_path = expand_path(optarg);
91                 break;
92             case 'v':
93                 printf("i3bar version " I3_VERSION " © 2010-2011 Axel Wagner and contributors\n");
94                 exit(EXIT_SUCCESS);
95                 break;
96             case 0:
97                 if (!strcmp(long_opt[option_index].name, "bar_id")) {
98                     FREE(config.bar_id);
99                     config.bar_id = sstrdup(optarg);
100                 }
101                 break;
102             default:
103                 print_usage(argv[0]);
104                 exit(EXIT_SUCCESS);
105                 break;
106         }
107     }
108
109     if (!config.bar_id) {
110         /* TODO: maybe we want -f which will automatically ask i3 for the first
111          * configured bar (and error out if there are too many)? */
112         ELOG("No bar_id passed. Please let i3 start i3bar or specify --bar_id\n");
113         exit(EXIT_FAILURE);
114     }
115
116     main_loop = ev_default_loop(0);
117
118     char *atom_sock_path = init_xcb_early();
119
120     if (socket_path == NULL) {
121         socket_path = atom_sock_path;
122     }
123
124     if (socket_path == NULL) {
125         ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
126         socket_path = expand_path(i3_default_sock_path);
127     }
128
129     init_outputs();
130     if (init_connection(socket_path)) {
131         /* Request the bar configuration. When it arrives, we fill the config array. */
132         i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id);
133     }
134
135     /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
136      * We only need those watchers on the stack, so putting them on the stack saves us
137      * some calls to free() */
138     ev_signal *sig_term = malloc(sizeof(ev_signal));
139     ev_signal *sig_int = malloc(sizeof(ev_signal));
140     ev_signal *sig_hup = malloc(sizeof(ev_signal));
141
142     if (sig_term == NULL || sig_int == NULL || sig_hup == NULL) {
143         ELOG("malloc() failed: %s\n", strerror(errno));
144         exit(EXIT_FAILURE);
145     }
146
147     ev_signal_init(sig_term, &sig_cb, SIGTERM);
148     ev_signal_init(sig_int, &sig_cb, SIGINT);
149     ev_signal_init(sig_hup, &sig_cb, SIGHUP);
150
151     ev_signal_start(main_loop, sig_term);
152     ev_signal_start(main_loop, sig_int);
153     ev_signal_start(main_loop, sig_hup);
154
155     /* From here on everything should run smooth for itself, just start listening for
156      * events. We stop simply stop the event-loop, when we are finished */
157     ev_loop(main_loop, 0);
158
159     kill_child();
160
161     FREE(statusline_buffer);
162
163     clean_xcb();
164     ev_default_destroy();
165
166     free_workspaces();
167
168     return 0;
169 }