2010-08-07 18:05:16 +02:00
|
|
|
/*
|
2011-10-09 15:28:20 +02:00
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
2010-08-07 18:05:16 +02:00
|
|
|
* i3bar - an xcb-based status- and ws-bar for i3
|
2012-08-13 13:27:00 +02:00
|
|
|
* © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
|
2010-08-07 18:05:16 +02:00
|
|
|
*
|
|
|
|
*/
|
2010-07-23 04:43:43 +02:00
|
|
|
#include <stdio.h>
|
2010-07-23 05:04:13 +02:00
|
|
|
#include <i3/ipc.h>
|
2010-07-23 04:43:43 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2010-07-26 23:51:51 +02:00
|
|
|
#include <stdlib.h>
|
2010-08-04 03:34:18 +02:00
|
|
|
#include <errno.h>
|
2010-07-23 04:43:43 +02:00
|
|
|
#include <ev.h>
|
2010-08-06 03:32:05 +02:00
|
|
|
#include <getopt.h>
|
2010-08-06 04:11:44 +02:00
|
|
|
#include <glob.h>
|
2010-07-23 04:43:43 +02:00
|
|
|
|
2010-07-22 01:15:18 +02:00
|
|
|
#include "common.h"
|
2010-08-04 03:34:18 +02:00
|
|
|
|
2012-08-13 13:27:00 +02:00
|
|
|
/*
|
2013-12-24 10:35:56 +01:00
|
|
|
* Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
|
2012-08-13 13:27:00 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void verboselog(char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(stdout, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void errorlog(char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:35:56 +01:00
|
|
|
void debuglog(char *fmt, ...) {
|
|
|
|
}
|
|
|
|
|
2010-09-17 05:26:31 +02:00
|
|
|
/*
|
|
|
|
* Glob path, i.e. expand ~
|
|
|
|
*
|
|
|
|
*/
|
2010-08-06 04:11:44 +02:00
|
|
|
char *expand_path(char *path) {
|
|
|
|
static glob_t globbuf;
|
|
|
|
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
|
2010-09-17 06:49:28 +02:00
|
|
|
ELOG("glob() failed\n");
|
2010-08-06 04:11:44 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2011-10-21 20:30:46 +02:00
|
|
|
char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
|
2010-08-06 04:11:44 +02:00
|
|
|
globfree(&globbuf);
|
|
|
|
return result;
|
|
|
|
}
|
2010-08-06 03:32:05 +02:00
|
|
|
|
2010-08-26 00:01:24 +02:00
|
|
|
void print_usage(char *elf_name) {
|
2013-03-28 20:11:05 +01:00
|
|
|
printf("Usage: %s -b bar_id [-s sock_path] [-h] [-v]\n", elf_name);
|
2011-10-21 19:59:59 +02:00
|
|
|
printf("\n");
|
2013-03-28 20:11:05 +01:00
|
|
|
printf("-b, --bar_id <bar_id>\tBar ID for which to get the configuration\n");
|
|
|
|
printf("-s, --socket <sock_path>\tConnect to i3 via <sock_path>\n");
|
2015-03-23 20:56:49 +01:00
|
|
|
printf("-h, --help Display this help message and exit\n");
|
2013-03-28 20:11:05 +01:00
|
|
|
printf("-v, --version Display version number and exit\n");
|
2011-10-21 19:59:59 +02:00
|
|
|
printf("\n");
|
|
|
|
printf(" PLEASE NOTE that i3bar will be automatically started by i3\n"
|
|
|
|
" as soon as there is a 'bar' configuration block in your\n"
|
|
|
|
" config file. You should never need to start it manually.\n");
|
|
|
|
printf("\n");
|
2010-08-26 00:01:24 +02:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:47:30 +01:00
|
|
|
/*
|
|
|
|
* We watch various signals, that are there to make our application stop.
|
2015-03-23 20:56:49 +01:00
|
|
|
* If we get one of those, we ev_unloop() and invoke the cleanup routines
|
2010-11-11 02:47:30 +01:00
|
|
|
* in main() with that
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void sig_cb(struct ev_loop *loop, ev_signal *watcher, int revents) {
|
|
|
|
switch (watcher->signum) {
|
|
|
|
case SIGTERM:
|
|
|
|
DLOG("Got a SIGTERM, stopping\n");
|
|
|
|
break;
|
|
|
|
case SIGINT:
|
|
|
|
DLOG("Got a SIGINT, stopping\n");
|
|
|
|
break;
|
|
|
|
case SIGHUP:
|
|
|
|
DLOG("Got a SIGHUP, stopping\n");
|
|
|
|
}
|
|
|
|
ev_unloop(main_loop, EVUNLOOP_ALL);
|
|
|
|
}
|
|
|
|
|
2010-07-22 01:15:18 +02:00
|
|
|
int main(int argc, char **argv) {
|
2010-08-06 03:32:05 +02:00
|
|
|
int opt;
|
|
|
|
int option_index = 0;
|
2011-01-11 05:02:55 +01:00
|
|
|
char *socket_path = getenv("I3SOCK");
|
2011-01-22 17:41:24 +01:00
|
|
|
char *i3_default_sock_path = "/tmp/i3-ipc.sock";
|
2010-08-06 03:32:05 +02:00
|
|
|
|
2011-10-19 23:58:19 +02:00
|
|
|
/* Initialize the standard config to use 0 as default */
|
|
|
|
memset(&config, '\0', sizeof(config_t));
|
2010-08-26 00:02:35 +02:00
|
|
|
|
2010-08-06 03:32:05 +02:00
|
|
|
static struct option long_opt[] = {
|
2014-06-15 19:07:02 +02:00
|
|
|
{"socket", required_argument, 0, 's'},
|
|
|
|
{"bar_id", required_argument, 0, 'b'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{NULL, 0, 0, 0}};
|
2010-08-06 03:32:05 +02:00
|
|
|
|
2013-03-28 20:11:05 +01:00
|
|
|
while ((opt = getopt_long(argc, argv, "b:s:hv", long_opt, &option_index)) != -1) {
|
2010-08-06 03:32:05 +02:00
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
2010-08-06 04:11:44 +02:00
|
|
|
socket_path = expand_path(optarg);
|
2010-08-06 03:32:05 +02:00
|
|
|
break;
|
2010-08-07 03:50:22 +02:00
|
|
|
case 'v':
|
2014-01-24 23:49:46 +01:00
|
|
|
printf("i3bar version " I3_VERSION " © 2010-2014 Axel Wagner and contributors\n");
|
2010-08-07 03:50:22 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-08-26 00:01:24 +02:00
|
|
|
break;
|
2013-03-28 20:11:05 +01:00
|
|
|
case 'b':
|
|
|
|
config.bar_id = sstrdup(optarg);
|
2011-03-20 19:29:30 +01:00
|
|
|
break;
|
2010-08-06 03:32:05 +02:00
|
|
|
default:
|
2010-08-26 00:01:24 +02:00
|
|
|
print_usage(argv[0]);
|
2010-08-06 03:32:05 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 23:58:19 +02:00
|
|
|
if (!config.bar_id) {
|
|
|
|
/* TODO: maybe we want -f which will automatically ask i3 for the first
|
|
|
|
* configured bar (and error out if there are too many)? */
|
|
|
|
ELOG("No bar_id passed. Please let i3 start i3bar or specify --bar_id\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2011-02-21 15:47:58 +01:00
|
|
|
}
|
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
main_loop = ev_default_loop(0);
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2011-10-19 23:58:19 +02:00
|
|
|
char *atom_sock_path = init_xcb_early();
|
2011-03-19 22:06:08 +01:00
|
|
|
|
|
|
|
if (socket_path == NULL) {
|
|
|
|
socket_path = atom_sock_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socket_path == NULL) {
|
2015-03-24 13:41:16 +01:00
|
|
|
ELOG("No socket path specified, default to %s\n", i3_default_sock_path);
|
2011-03-19 22:06:08 +01:00
|
|
|
socket_path = expand_path(i3_default_sock_path);
|
|
|
|
}
|
2010-10-24 05:24:51 +02:00
|
|
|
|
2010-08-06 03:32:05 +02:00
|
|
|
init_outputs();
|
2011-07-31 18:26:52 +02:00
|
|
|
if (init_connection(socket_path)) {
|
2011-10-19 23:58:19 +02:00
|
|
|
/* Request the bar configuration. When it arrives, we fill the config array. */
|
|
|
|
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id);
|
2011-04-21 20:24:02 +02:00
|
|
|
}
|
2010-07-23 05:04:13 +02:00
|
|
|
|
2015-03-23 20:56:49 +01:00
|
|
|
/* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main loop.
|
2010-11-11 02:47:30 +01:00
|
|
|
* We only need those watchers on the stack, so putting them on the stack saves us
|
|
|
|
* some calls to free() */
|
2011-10-21 20:30:46 +02:00
|
|
|
ev_signal *sig_term = smalloc(sizeof(ev_signal));
|
|
|
|
ev_signal *sig_int = smalloc(sizeof(ev_signal));
|
|
|
|
ev_signal *sig_hup = smalloc(sizeof(ev_signal));
|
2011-04-28 19:54:31 +02:00
|
|
|
|
|
|
|
ev_signal_init(sig_term, &sig_cb, SIGTERM);
|
|
|
|
ev_signal_init(sig_int, &sig_cb, SIGINT);
|
|
|
|
ev_signal_init(sig_hup, &sig_cb, SIGHUP);
|
2010-11-11 02:47:30 +01:00
|
|
|
|
2011-04-28 19:54:31 +02:00
|
|
|
ev_signal_start(main_loop, sig_term);
|
|
|
|
ev_signal_start(main_loop, sig_int);
|
|
|
|
ev_signal_start(main_loop, sig_hup);
|
2010-11-11 02:47:30 +01:00
|
|
|
|
2010-09-17 05:26:31 +02:00
|
|
|
/* From here on everything should run smooth for itself, just start listening for
|
2015-03-23 20:56:49 +01:00
|
|
|
* events. We stop simply stop the event loop, when we are finished */
|
2010-08-03 21:20:11 +02:00
|
|
|
ev_loop(main_loop, 0);
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-05 05:09:59 +02:00
|
|
|
kill_child();
|
2010-08-04 04:07:16 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
clean_xcb();
|
2010-08-05 05:09:59 +02:00
|
|
|
ev_default_destroy();
|
2010-07-30 03:11:54 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
free_workspaces();
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
return 0;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|