2010-08-07 12:05:16 -04:00
|
|
|
/*
|
|
|
|
* i3bar - an xcb-based status- and ws-bar for i3
|
|
|
|
*
|
|
|
|
* © 2010 Axel Wagner and contributors
|
|
|
|
*
|
|
|
|
* See file LICNSE for license information
|
|
|
|
*
|
|
|
|
*/
|
2010-07-22 22:43:43 -04:00
|
|
|
#include <stdio.h>
|
2010-07-22 23:04:13 -04:00
|
|
|
#include <i3/ipc.h>
|
2010-07-22 22:43:43 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2010-07-26 17:51:51 -04:00
|
|
|
#include <stdlib.h>
|
2010-08-03 21:34:18 -04:00
|
|
|
#include <errno.h>
|
2010-07-22 22:43:43 -04:00
|
|
|
#include <ev.h>
|
2010-08-05 21:32:05 -04:00
|
|
|
#include <getopt.h>
|
2010-08-05 22:11:44 -04:00
|
|
|
#include <glob.h>
|
2010-07-22 22:43:43 -04:00
|
|
|
|
2010-07-21 19:15:18 -04:00
|
|
|
#include "common.h"
|
2010-08-03 21:34:18 -04:00
|
|
|
|
2010-09-16 23:26:31 -04:00
|
|
|
/*
|
|
|
|
* Glob path, i.e. expand ~
|
|
|
|
*
|
|
|
|
*/
|
2010-08-05 22:11:44 -04:00
|
|
|
char *expand_path(char *path) {
|
|
|
|
static glob_t globbuf;
|
|
|
|
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) {
|
2010-09-17 00:49:28 -04:00
|
|
|
ELOG("glob() failed\n");
|
2010-08-05 22:11:44 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
|
|
|
|
if (result == NULL) {
|
2011-01-01 10:48:30 -05:00
|
|
|
ELOG("malloc() failed: %s\n", strerror(errno));
|
2010-08-05 22:11:44 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
globfree(&globbuf);
|
|
|
|
return result;
|
|
|
|
}
|
2010-08-05 21:32:05 -04:00
|
|
|
|
2010-11-04 07:27:10 -04:00
|
|
|
static void read_color(char **color) {
|
2010-10-23 23:24:51 -04:00
|
|
|
int len = strlen(optarg);
|
|
|
|
if (len == 6 || (len == 7 && optarg[0] == '#')) {
|
|
|
|
int offset = len - 6;
|
|
|
|
int good = 1, i;
|
|
|
|
for (i = offset; good && i < 6 + offset; ++i) {
|
|
|
|
char c = optarg[i];
|
|
|
|
if (!(c >= 'a' && c <= 'f')
|
|
|
|
&& !(c >= 'A' && c <= 'F')
|
|
|
|
&& !(c >= '0' && c <= '9')) {
|
|
|
|
good = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (good) {
|
|
|
|
*color = strdup(optarg + offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Bad color value \"%s\"\n", optarg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2010-11-04 07:27:10 -04:00
|
|
|
static void free_colors(struct xcb_color_strings_t *colors) {
|
2010-10-23 23:24:51 -04:00
|
|
|
#define FREE_COLOR(x) \
|
|
|
|
do { \
|
|
|
|
if (colors->x) \
|
|
|
|
free(colors->x); \
|
|
|
|
} while (0)
|
|
|
|
FREE_COLOR(bar_fg);
|
|
|
|
FREE_COLOR(bar_bg);
|
|
|
|
FREE_COLOR(active_ws_fg);
|
|
|
|
FREE_COLOR(active_ws_bg);
|
|
|
|
FREE_COLOR(inactive_ws_fg);
|
|
|
|
FREE_COLOR(inactive_ws_bg);
|
|
|
|
FREE_COLOR(urgent_ws_fg);
|
|
|
|
FREE_COLOR(urgent_ws_bg);
|
|
|
|
#undef FREE_COLOR
|
|
|
|
}
|
|
|
|
|
2010-08-25 18:01:24 -04:00
|
|
|
void print_usage(char *elf_name) {
|
2010-09-17 00:49:28 -04:00
|
|
|
printf("Usage: %s [-s sock_path] [-c command] [-m] [-f font] [-V] [-h]\n", elf_name);
|
2010-08-25 18:01:24 -04:00
|
|
|
printf("-s <sock_path>\tConnect to i3 via <sock_path>\n");
|
|
|
|
printf("-c <command>\tExecute <command> to get stdin\n");
|
|
|
|
printf("-m\t\tHide the bars, when mod4 is not pressed.\n");
|
|
|
|
printf("\t\tIf -c is specified, the childprocess is sent a SIGSTOP on hiding,\n");
|
|
|
|
printf("\t\tand a SIGCONT on unhiding of the bars\n");
|
|
|
|
printf("-f <font>\tUse X-Core-Font <font> for display\n");
|
2010-09-17 00:49:28 -04:00
|
|
|
printf("-V\t\tBe (very) verbose with the debug-output\n");
|
2010-08-25 18:01:24 -04:00
|
|
|
printf("-h\t\tDisplay this help-message and exit\n");
|
|
|
|
}
|
|
|
|
|
2010-11-10 20:47:30 -05:00
|
|
|
/*
|
|
|
|
* We watch various signals, that are there to make our application stop.
|
|
|
|
* If we get one of those, we ev_unloop() and invoke the cleanup-routines
|
|
|
|
* 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-21 19:15:18 -04:00
|
|
|
int main(int argc, char **argv) {
|
2010-08-05 21:32:05 -04:00
|
|
|
int opt;
|
|
|
|
int option_index = 0;
|
|
|
|
char *socket_path = NULL;
|
|
|
|
char *command = NULL;
|
|
|
|
char *fontname = NULL;
|
2010-09-16 21:04:40 -04:00
|
|
|
char *i3_default_sock_path = "~/.i3/ipc.sock";
|
2010-11-04 07:27:10 -04:00
|
|
|
struct xcb_color_strings_t colors = { NULL, };
|
2010-08-05 21:32:05 -04:00
|
|
|
|
2010-08-25 18:02:35 -04:00
|
|
|
/* Definition of the standard-config */
|
|
|
|
config.hide_on_modifier = 0;
|
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
static struct option long_opt[] = {
|
2010-10-23 23:24:51 -04:00
|
|
|
{ "socket", required_argument, 0, 's' },
|
|
|
|
{ "command", required_argument, 0, 'c' },
|
|
|
|
{ "hide", no_argument, 0, 'm' },
|
|
|
|
{ "font", required_argument, 0, 'f' },
|
|
|
|
{ "help", no_argument, 0, 'h' },
|
|
|
|
{ "version", no_argument, 0, 'v' },
|
|
|
|
{ "verbose", no_argument, 0, 'V' },
|
|
|
|
{ "color-bar-fg", required_argument, 0, 'A' },
|
|
|
|
{ "color-bar-bg", required_argument, 0, 'B' },
|
|
|
|
{ "color-active-ws-fg", required_argument, 0, 'C' },
|
|
|
|
{ "color-active-ws-bg", required_argument, 0, 'D' },
|
|
|
|
{ "color-inactive-ws-fg", required_argument, 0, 'E' },
|
|
|
|
{ "color-inactive-ws-bg", required_argument, 0, 'F' },
|
|
|
|
{ "color-urgent-ws-bg", required_argument, 0, 'G' },
|
|
|
|
{ "color-urgent-ws-fg", required_argument, 0, 'H' },
|
|
|
|
{ NULL, 0, 0, 0}
|
2010-08-05 21:32:05 -04:00
|
|
|
};
|
|
|
|
|
2010-11-10 20:37:50 -05:00
|
|
|
while ((opt = getopt_long(argc, argv, "s:c:mf:hvVA:B:C:D:E:F:G:H:", long_opt, &option_index)) != -1) {
|
2010-08-05 21:32:05 -04:00
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
2010-08-05 22:11:44 -04:00
|
|
|
socket_path = expand_path(optarg);
|
2010-08-05 21:32:05 -04:00
|
|
|
break;
|
|
|
|
case 'c':
|
2010-08-05 22:11:44 -04:00
|
|
|
command = strdup(optarg);
|
2010-08-05 21:32:05 -04:00
|
|
|
break;
|
2010-08-25 18:02:35 -04:00
|
|
|
case 'm':
|
|
|
|
config.hide_on_modifier = 1;
|
|
|
|
break;
|
2010-08-05 21:32:05 -04:00
|
|
|
case 'f':
|
2010-08-05 22:11:44 -04:00
|
|
|
fontname = strdup(optarg);
|
2010-08-05 21:32:05 -04:00
|
|
|
break;
|
2010-08-06 21:50:22 -04:00
|
|
|
case 'v':
|
|
|
|
printf("i3bar version " I3BAR_VERSION " © 2010 Axel Wagner and contributors\n");
|
|
|
|
exit(EXIT_SUCCESS);
|
2010-08-25 18:01:24 -04:00
|
|
|
break;
|
2010-09-17 00:49:28 -04:00
|
|
|
case 'V':
|
|
|
|
config.verbose = 1;
|
|
|
|
break;
|
2010-10-23 23:24:51 -04:00
|
|
|
case 'A':
|
|
|
|
read_color(&colors.bar_fg);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
read_color(&colors.bar_bg);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
read_color(&colors.active_ws_fg);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
read_color(&colors.active_ws_bg);
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
read_color(&colors.inactive_ws_fg);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
read_color(&colors.inactive_ws_bg);
|
|
|
|
break;
|
|
|
|
case 'G':
|
2010-12-03 11:59:16 -05:00
|
|
|
read_color(&colors.urgent_ws_bg);
|
2010-10-23 23:24:51 -04:00
|
|
|
break;
|
|
|
|
case 'H':
|
2010-12-03 11:59:16 -05:00
|
|
|
read_color(&colors.urgent_ws_fg);
|
2010-10-23 23:24:51 -04:00
|
|
|
break;
|
2010-08-05 21:32:05 -04:00
|
|
|
default:
|
2010-08-25 18:01:24 -04:00
|
|
|
print_usage(argv[0]);
|
2010-08-05 21:32:05 -04:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fontname == NULL) {
|
2010-09-16 23:26:31 -04:00
|
|
|
/* This is a very restrictive default. More sensefull would be something like
|
|
|
|
* "-misc-*-*-*-*--*-*-*-*-*-*-*-*". But since that produces very ugly results
|
|
|
|
* on my machine, let's stick with this until we have a configfile */
|
2010-08-05 21:32:05 -04:00
|
|
|
fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socket_path == NULL) {
|
2010-09-17 00:49:28 -04:00
|
|
|
ELOG("No Socket Path Specified, default to %s\n", i3_default_sock_path);
|
2010-08-05 22:11:44 -04:00
|
|
|
socket_path = expand_path(i3_default_sock_path);
|
2010-08-05 21:32:05 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
main_loop = ev_default_loop(0);
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-11-04 08:46:33 -04:00
|
|
|
init_colors(&colors);
|
2010-08-05 21:32:05 -04:00
|
|
|
init_xcb(fontname);
|
2010-10-23 23:24:51 -04:00
|
|
|
|
|
|
|
free_colors(&colors);
|
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
init_outputs();
|
|
|
|
init_connection(socket_path);
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-09-16 23:26:31 -04:00
|
|
|
/* We subscribe to the i3-events we need */
|
2010-08-03 15:20:11 -04:00
|
|
|
subscribe_events();
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-09-16 23:26:31 -04:00
|
|
|
/* We initiate the main-function by requesting infos about the outputs and
|
|
|
|
* workspaces. Everything else (creating the bars, showing the right workspace-
|
|
|
|
* buttons and more) is taken care of by the event-driveniness of the code */
|
2010-08-03 15:20:11 -04:00
|
|
|
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
|
|
|
|
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL);
|
2010-07-22 23:04:13 -04:00
|
|
|
|
2010-09-16 23:26:31 -04:00
|
|
|
/* The name of this function is actually misleading. Even if no -c is specified,
|
|
|
|
* this function initiates the watchers to listen on stdin and react accordingly */
|
2010-08-05 21:32:05 -04:00
|
|
|
start_child(command);
|
2010-08-03 21:34:18 -04:00
|
|
|
|
2010-11-10 20:47:30 -05:00
|
|
|
/* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main-loop.
|
|
|
|
* We only need those watchers on the stack, so putting them on the stack saves us
|
|
|
|
* some calls to free() */
|
2010-11-10 21:01:40 -05:00
|
|
|
ev_signal sig_term, sig_int, sig_hup;
|
2010-11-10 20:47:30 -05: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);
|
|
|
|
|
|
|
|
ev_signal_start(main_loop, &sig_term);
|
|
|
|
ev_signal_start(main_loop, &sig_int);
|
|
|
|
ev_signal_start(main_loop, &sig_hup);
|
|
|
|
|
2010-09-16 23:26:31 -04:00
|
|
|
/* From here on everything should run smooth for itself, just start listening for
|
|
|
|
* events. We stop simply stop the event-loop, when we are finished */
|
2010-08-03 15:20:11 -04:00
|
|
|
ev_loop(main_loop, 0);
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-04 23:09:59 -04:00
|
|
|
kill_child();
|
2010-08-03 22:07:16 -04:00
|
|
|
|
2010-12-26 13:29:57 -05:00
|
|
|
FREE(socket_path);
|
|
|
|
|
2010-08-03 22:07:16 -04:00
|
|
|
FREE(statusline);
|
2010-07-26 17:51:51 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
clean_xcb();
|
2010-08-04 23:09:59 -04:00
|
|
|
ev_default_destroy();
|
2010-07-29 21:11:54 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
free_workspaces();
|
|
|
|
FREE_SLIST(outputs, i3_output);
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
return 0;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|