| 1 |
/* |
| 2 |
* vim:ts=4:sw=4:expandtab |
| 3 |
* |
| 4 |
* i3bar - an xcb-based status- and ws-bar for i3 |
| 5 |
* © 2010 Axel Wagner and contributors (see also: LICENSE) |
| 6 |
* |
| 7 |
*/ |
| 8 |
#include "common.h" |
| 9 |
|
| 10 |
#include <ev.h> |
| 11 |
#include <getopt.h> |
| 12 |
#include <glob.h> |
| 13 |
#include <i3/ipc.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
struct ev_loop *main_loop; |
| 19 |
|
| 20 |
/* |
| 21 |
* Having verboselog(), errorlog() and debuglog() 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 |
void debuglog(char *fmt, ...) { |
| 41 |
} |
| 42 |
|
| 43 |
/* |
| 44 |
* Glob path, i.e. expand ~ |
| 45 |
* |
| 46 |
*/ |
| 47 |
static char *expand_path(char *path) { |
| 48 |
static glob_t globbuf; |
| 49 |
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) { |
| 50 |
ELOG("glob() failed\n"); |
| 51 |
exit(EXIT_FAILURE); |
| 52 |
} |
| 53 |
char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path); |
| 54 |
globfree(&globbuf); |
| 55 |
return result; |
| 56 |
} |
| 57 |
|
| 58 |
static void print_usage(char *elf_name) { |
| 59 |
printf("Usage: %s [-b bar_id] [-s sock_path] [-t] [-h] [-v] [-V]\n", elf_name); |
| 60 |
printf("\n"); |
| 61 |
printf("-b, --bar_id <bar_id>\tBar ID for which to get the configuration, defaults to the first bar from the i3 config\n"); |
| 62 |
printf("-s, --socket <sock_path>\tConnect to i3 via <sock_path>\n"); |
| 63 |
printf("-t, --transparency Enable transparency (RGBA colors)\n"); |
| 64 |
printf("-h, --help Display this help message and exit\n"); |
| 65 |
printf("-v, --version Display version number and exit\n"); |
| 66 |
printf("-V, --verbose Enable verbose mode\n"); |
| 67 |
printf("\n"); |
| 68 |
printf(" PLEASE NOTE that i3bar will be automatically started by i3\n" |
| 69 |
" as soon as there is a 'bar' configuration block in your\n" |
| 70 |
" config file. You should never need to start it manually.\n"); |
| 71 |
printf("\n"); |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* We watch various signals, that are there to make our application stop. |
| 76 |
* If we get one of those, we ev_unloop() and invoke the cleanup routines |
| 77 |
* in main() with that |
| 78 |
* |
| 79 |
*/ |
| 80 |
static void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) { |
| 81 |
switch (watcher->signum) { |
| 82 |
case SIGTERM: |
| 83 |
DLOG("Got a SIGTERM, stopping\n"); |
| 84 |
break; |
| 85 |
case SIGINT: |
| 86 |
DLOG("Got a SIGINT, stopping\n"); |
| 87 |
break; |
| 88 |
case SIGHUP: |
| 89 |
DLOG("Got a SIGHUP, stopping\n"); |
| 90 |
} |
| 91 |
ev_unloop(main_loop, EVUNLOOP_ALL); |
| 92 |
} |
| 93 |
|
| 94 |
int main(int argc, char **argv) { |
| 95 |
char *socket_path = NULL; |
| 96 |
|
| 97 |
/* Initialize the standard config to use 0 as default */ |
| 98 |
memset(&config, '\0', sizeof(config_t)); |
| 99 |
|
| 100 |
static struct option long_opt[] = { |
| 101 |
{"socket", required_argument, 0, 's'}, |
| 102 |
{"bar_id", required_argument, 0, 'b'}, |
| 103 |
{"transparency", no_argument, 0, 't'}, |
| 104 |
{"help", no_argument, 0, 'h'}, |
| 105 |
{"version", no_argument, 0, 'v'}, |
| 106 |
{"verbose", no_argument, 0, 'V'}, |
| 107 |
{NULL, 0, 0, 0}}; |
| 108 |
|
| 109 |
int opt; |
| 110 |
int option_index = 0; |
| 111 |
while ((opt = getopt_long(argc, argv, "b:s:thvV", long_opt, &option_index)) != -1) { |
| 112 |
switch (opt) { |
| 113 |
case 's': |
| 114 |
socket_path = expand_path(optarg); |
| 115 |
break; |
| 116 |
case 'v': |
| 117 |
printf("i3bar version " I3_VERSION " © 2010 Axel Wagner and contributors\n"); |
| 118 |
exit(EXIT_SUCCESS); |
| 119 |
break; |
| 120 |
case 'b': |
| 121 |
config.bar_id = sstrdup(optarg); |
| 122 |
break; |
| 123 |
case 't': |
| 124 |
config.transparency = true; |
| 125 |
break; |
| 126 |
case 'V': |
| 127 |
config.verbose = true; |
| 128 |
break; |
| 129 |
default: |
| 130 |
print_usage(argv[0]); |
| 131 |
exit(EXIT_FAILURE); |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
LOG("i3bar version " I3_VERSION "\n"); |
| 137 |
|
| 138 |
main_loop = ev_default_loop(0); /* needed in init_xcb_early */ |
| 139 |
char *atom_sock_path = init_xcb_early(); |
| 140 |
|
| 141 |
/* Select a socket_path if the user hasn't specified one */ |
| 142 |
if (socket_path == NULL) { |
| 143 |
socket_path = getenv("I3SOCK"); |
| 144 |
if (socket_path != NULL) { |
| 145 |
socket_path = sstrdup(socket_path); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if (socket_path == NULL) { |
| 150 |
socket_path = atom_sock_path; |
| 151 |
} else { |
| 152 |
free(atom_sock_path); |
| 153 |
} |
| 154 |
|
| 155 |
if (socket_path == NULL) { |
| 156 |
char *i3_default_sock_path = "/tmp/i3-ipc.sock"; |
| 157 |
ELOG("No socket path specified, default to %s\n", i3_default_sock_path); |
| 158 |
socket_path = sstrdup(i3_default_sock_path); |
| 159 |
} |
| 160 |
|
| 161 |
init_dpi(); |
| 162 |
|
| 163 |
init_outputs(); |
| 164 |
|
| 165 |
init_connection(socket_path); |
| 166 |
/* Request the bar configuration. When it arrives, we fill the config |
| 167 |
* array. In case that config.bar_id is empty, we will receive a list of |
| 168 |
* available configs and then request the configuration for the first bar. |
| 169 |
* See got_bar_config for more. */ |
| 170 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id); |
| 171 |
free(socket_path); |
| 172 |
|
| 173 |
/* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main loop. |
| 174 |
* We only need those watchers on the stack, so putting them on the stack saves us |
| 175 |
* some calls to free() */ |
| 176 |
ev_signal *sig_term = smalloc(sizeof(ev_signal)); |
| 177 |
ev_signal *sig_int = smalloc(sizeof(ev_signal)); |
| 178 |
ev_signal *sig_hup = smalloc(sizeof(ev_signal)); |
| 179 |
|
| 180 |
ev_signal_init(sig_term, &sig_cb, SIGTERM); |
| 181 |
ev_signal_init(sig_int, &sig_cb, SIGINT); |
| 182 |
ev_signal_init(sig_hup, &sig_cb, SIGHUP); |
| 183 |
|
| 184 |
ev_signal_start(main_loop, sig_term); |
| 185 |
ev_signal_start(main_loop, sig_int); |
| 186 |
ev_signal_start(main_loop, sig_hup); |
| 187 |
|
| 188 |
atexit(kill_children_at_exit); |
| 189 |
|
| 190 |
/* From here on everything should run smooth for itself, just start listening for |
| 191 |
* events. We stop simply stop the event loop, when we are finished */ |
| 192 |
ev_loop(main_loop, 0); |
| 193 |
|
| 194 |
clean_xcb(); |
| 195 |
ev_default_destroy(); |
| 196 |
|
| 197 |
return 0; |
| 198 |
} |