2018-11-25 12:12:48 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2015-11-29 16:26:50 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-12-13 14:35:39 +01:00
|
|
|
#include <string.h>
|
2015-12-13 21:40:19 +01:00
|
|
|
#include <stdbool.h>
|
2015-12-16 04:08:09 +01:00
|
|
|
#include <getopt.h>
|
2016-09-01 14:18:37 +02:00
|
|
|
#include "swaybar/bar.h"
|
2015-12-13 21:40:19 +01:00
|
|
|
#include "ipc-client.h"
|
2019-01-20 19:51:12 +01:00
|
|
|
#include "log.h"
|
2015-11-29 16:58:18 +01:00
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
static struct swaybar swaybar;
|
2015-12-22 17:34:37 +01:00
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
void sig_handler(int signal) {
|
2019-02-14 16:43:34 +01:00
|
|
|
swaybar.running = false;
|
2015-12-21 11:46:15 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 16:26:50 +01:00
|
|
|
int main(int argc, char **argv) {
|
2015-12-16 04:08:09 +01:00
|
|
|
char *socket_path = NULL;
|
2016-01-04 01:22:20 +01:00
|
|
|
bool debug = false;
|
2015-12-16 04:08:09 +01:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
2016-01-23 20:22:27 +01:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2015-12-16 04:08:09 +01:00
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{"socket", required_argument, NULL, 's'},
|
|
|
|
{"bar_id", required_argument, NULL, 'b'},
|
2016-01-23 20:21:34 +01:00
|
|
|
{"debug", no_argument, NULL, 'd'},
|
2015-12-16 04:08:09 +01:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2016-01-23 20:22:27 +01:00
|
|
|
const char *usage =
|
2016-02-22 17:27:17 +01:00
|
|
|
"Usage: swaybar [options...]\n"
|
2016-01-23 20:22:27 +01: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 04:08:09 +01:00
|
|
|
int c;
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
2016-01-23 20:22:27 +01:00
|
|
|
c = getopt_long(argc, argv, "hvs:b:d", long_options, &option_index);
|
2015-12-16 04:08:09 +01:00
|
|
|
if (c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 's': // Socket
|
|
|
|
socket_path = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'b': // Type
|
2018-09-30 16:09:09 +02:00
|
|
|
swaybar.id = strdup(optarg);
|
2015-12-16 04:08:09 +01:00
|
|
|
break;
|
|
|
|
case 'v':
|
2021-01-16 19:49:44 +01:00
|
|
|
printf("swaybar version " SWAY_VERSION "\n");
|
2015-12-16 04:08:09 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
2016-01-04 01:22:20 +01:00
|
|
|
case 'd': // Debug
|
|
|
|
debug = true;
|
|
|
|
break;
|
2015-12-16 04:08:09 +01:00
|
|
|
default:
|
2016-01-23 20:22:27 +01:00
|
|
|
fprintf(stderr, "%s", usage);
|
2015-12-16 04:08:09 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:22:20 +01:00
|
|
|
if (debug) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log_init(SWAY_DEBUG, NULL);
|
2016-01-04 01:22:20 +01:00
|
|
|
} else {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log_init(SWAY_INFO, NULL);
|
2018-03-29 05:04:20 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 16:09:09 +02:00
|
|
|
if (!swaybar.id) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "No bar_id passed. "
|
2018-03-29 05:04:20 +02:00
|
|
|
"Provide --bar_id or let sway start swaybar");
|
|
|
|
return 1;
|
2016-01-04 01:22:20 +01:00
|
|
|
}
|
2015-12-16 11:13:12 +01:00
|
|
|
|
2015-12-16 04:22:22 +01:00
|
|
|
if (!socket_path) {
|
|
|
|
socket_path = get_socketpath();
|
|
|
|
if (!socket_path) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Unable to retrieve socket path");
|
2018-03-29 05:04:20 +02:00
|
|
|
return 1;
|
2015-12-16 04:22:22 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-25 15:29:36 +01:00
|
|
|
|
2018-09-30 16:09:09 +02:00
|
|
|
if (!bar_setup(&swaybar, socket_path)) {
|
2018-09-24 19:42:25 +02:00
|
|
|
free(socket_path);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-12-16 04:22:22 +01:00
|
|
|
|
2016-01-24 02:19:08 +01:00
|
|
|
free(socket_path);
|
2015-12-21 11:46:15 +01:00
|
|
|
|
2019-02-14 16:43:34 +01:00
|
|
|
signal(SIGINT, sig_handler);
|
|
|
|
signal(SIGTERM, sig_handler);
|
|
|
|
|
|
|
|
swaybar.running = true;
|
2016-01-24 02:34:20 +01:00
|
|
|
bar_run(&swaybar);
|
|
|
|
bar_teardown(&swaybar);
|
2015-11-29 16:26:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|