mirror of
https://github.com/swaywm/sway.git
synced 2024-11-10 12:33:50 +01:00
1211a81aad
This commit mostly duplicates the wlr_log functions, although with a sway_* prefix. (This is very similar to PR #2009.) However, the logging function no longer needs to be replaceable, so sway_log_init's second argument is used to set the exit callback for sway_abort. wlr_log_init is still invoked in sway/main.c This commit makes it easier to remove the wlroots dependency for the helper programs swaymsg, swaybg, swaybar, and swaynag.
109 lines
2.3 KiB
C
109 lines
2.3 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include <getopt.h>
|
|
#include "swaybar/bar.h"
|
|
#include "ipc-client.h"
|
|
#include "log.h"
|
|
|
|
static struct swaybar swaybar;
|
|
|
|
void sig_handler(int signal) {
|
|
bar_teardown(&swaybar);
|
|
exit(0);
|
|
}
|
|
|
|
void sway_terminate(int code) {
|
|
bar_teardown(&swaybar);
|
|
exit(code);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
char *socket_path = NULL;
|
|
bool debug = false;
|
|
|
|
static struct option long_options[] = {
|
|
{"help", no_argument, NULL, 'h'},
|
|
{"version", no_argument, NULL, 'v'},
|
|
{"socket", required_argument, NULL, 's'},
|
|
{"bar_id", required_argument, NULL, 'b'},
|
|
{"debug", no_argument, NULL, 'd'},
|
|
{0, 0, 0, 0}
|
|
};
|
|
|
|
const char *usage =
|
|
"Usage: swaybar [options...]\n"
|
|
"\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";
|
|
|
|
int c;
|
|
while (1) {
|
|
int option_index = 0;
|
|
c = getopt_long(argc, argv, "hvs:b:d", long_options, &option_index);
|
|
if (c == -1) {
|
|
break;
|
|
}
|
|
switch (c) {
|
|
case 's': // Socket
|
|
socket_path = strdup(optarg);
|
|
break;
|
|
case 'b': // Type
|
|
swaybar.id = strdup(optarg);
|
|
break;
|
|
case 'v':
|
|
fprintf(stdout, "swaybar version " SWAY_VERSION "\n");
|
|
exit(EXIT_SUCCESS);
|
|
break;
|
|
case 'd': // Debug
|
|
debug = true;
|
|
break;
|
|
default:
|
|
fprintf(stderr, "%s", usage);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
if (debug) {
|
|
sway_log_init(SWAY_DEBUG, NULL);
|
|
} else {
|
|
sway_log_init(SWAY_INFO, NULL);
|
|
}
|
|
|
|
if (!swaybar.id) {
|
|
sway_log(SWAY_ERROR, "No bar_id passed. "
|
|
"Provide --bar_id or let sway start swaybar");
|
|
return 1;
|
|
}
|
|
|
|
if (!socket_path) {
|
|
socket_path = get_socketpath();
|
|
if (!socket_path) {
|
|
sway_log(SWAY_ERROR, "Unable to retrieve socket path");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
signal(SIGTERM, sig_handler);
|
|
|
|
if (!bar_setup(&swaybar, socket_path)) {
|
|
free(socket_path);
|
|
return 1;
|
|
}
|
|
|
|
free(socket_path);
|
|
|
|
bar_run(&swaybar);
|
|
bar_teardown(&swaybar);
|
|
return 0;
|
|
}
|