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