swayfx/swaybar/main.c

108 lines
2.3 KiB
C
Raw Normal View History

2017-03-11 15:41:24 +11:00
#define _XOPEN_SOURCE 500
2015-11-30 02:26:50 +11:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2015-12-14 07:40:19 +11:00
#include <stdbool.h>
2015-12-16 14:08:09 +11:00
#include <getopt.h>
#include <wlr/util/log.h>
2016-09-01 22:18:37 +10:00
#include "swaybar/bar.h"
2015-12-14 07:40:19 +11:00
#include "ipc-client.h"
static struct swaybar swaybar;
void sig_handler(int signal) {
2016-01-24 12:34:20 +11:00
bar_teardown(&swaybar);
exit(0);
2015-11-30 02:26:50 +11:00
}
void sway_terminate(int code) {
2016-01-24 12:34:20 +11:00
bar_teardown(&swaybar);
exit(code);
}
2015-11-30 02:26:50 +11:00
int main(int argc, char **argv) {
2015-12-16 14:08:09 +11:00
char *socket_path = NULL;
char *bar_id = NULL;
bool debug = false;
2015-12-16 14:08:09 +11:00
static struct option long_options[] = {
2016-01-24 06:22:27 +11:00
{"help", no_argument, NULL, 'h'},
2015-12-16 14:08:09 +11:00
{"version", no_argument, NULL, 'v'},
{"socket", required_argument, NULL, 's'},
{"bar_id", required_argument, NULL, 'b'},
{"debug", no_argument, NULL, 'd'},
2015-12-16 14:08:09 +11:00
{0, 0, 0, 0}
};
2016-01-24 06:22:27 +11:00
const char *usage =
"Usage: swaybar [options...]\n"
2016-01-24 06:22:27 +11:00
"\n"
" -h, --help Show help message and quit.\n"
" -v, --version Show the version number and quit.\n"
" -s, --socket <socket> Connect to sway via socket.\n"
" -b, --bar_id <id> Bar ID for which to get the configuration.\n"
" -d, --debug Enable debugging.\n"
"\n"
" PLEASE NOTE that swaybar will be automatically started by sway as\n"
" soon as there is a 'bar' configuration block in your config file.\n"
" You should never need to start it manually.\n";
2015-12-16 14:08:09 +11:00
int c;
while (1) {
int option_index = 0;
2016-01-24 06:22:27 +11:00
c = getopt_long(argc, argv, "hvs:b:d", long_options, &option_index);
2015-12-16 14:08:09 +11:00
if (c == -1) {
break;
}
switch (c) {
case 's': // Socket
socket_path = strdup(optarg);
break;
case 'b': // Type
bar_id = strdup(optarg);
break;
case 'v':
fprintf(stdout, "sway version " SWAY_VERSION "\n");
2015-12-16 14:08:09 +11:00
exit(EXIT_SUCCESS);
break;
case 'd': // Debug
debug = true;
break;
2015-12-16 14:08:09 +11:00
default:
2016-01-24 06:22:27 +11:00
fprintf(stderr, "%s", usage);
2015-12-16 14:08:09 +11:00
exit(EXIT_FAILURE);
}
}
if (debug) {
wlr_log_init(L_DEBUG, NULL);
} else {
wlr_log_init(L_ERROR, NULL);
}
if (!bar_id) {
wlr_log(L_ERROR, "No bar_id passed. "
"Provide --bar_id or let sway start swaybar");
return 1;
}
2015-12-16 21:13:12 +11:00
2015-12-16 14:22:22 +11:00
if (!socket_path) {
socket_path = get_socketpath();
if (!socket_path) {
wlr_log(L_ERROR, "Unable to retrieve socket path");
return 1;
2015-12-16 14:22:22 +11:00
}
}
signal(SIGTERM, sig_handler);
bar_setup(&swaybar, socket_path, bar_id);
2015-12-16 14:22:22 +11:00
free(socket_path);
free(bar_id);
2016-01-24 12:34:20 +11:00
bar_run(&swaybar);
bar_teardown(&swaybar);
2015-11-30 02:26:50 +11:00
return 0;
}