2018-07-28 15:34:25 +02:00
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2018-07-26 03:57:19 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <signal.h>
|
2018-07-28 15:34:25 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wordexp.h>
|
2018-07-26 03:57:19 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "list.h"
|
2018-07-27 07:30:35 +02:00
|
|
|
#include "readline.h"
|
2018-07-27 17:19:42 +02:00
|
|
|
#include "swaynag/nagbar.h"
|
2018-07-28 15:34:25 +02:00
|
|
|
#include "swaynag/types.h"
|
2018-07-26 03:57:19 +02:00
|
|
|
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
|
|
|
|
|
|
|
static struct sway_nagbar nagbar;
|
|
|
|
|
|
|
|
void sig_handler(int signal) {
|
|
|
|
nagbar_destroy(&nagbar);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sway_terminate(int code) {
|
|
|
|
nagbar_destroy(&nagbar);
|
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:30:35 +02:00
|
|
|
static char *read_from_stdin() {
|
|
|
|
char *buffer = NULL;
|
|
|
|
while (!feof(stdin)) {
|
|
|
|
char *line = read_line(stdin);
|
|
|
|
if (!line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buffer) {
|
|
|
|
buffer = strdup(line);
|
|
|
|
} else {
|
|
|
|
buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2);
|
|
|
|
strcat(buffer, line);
|
|
|
|
strcat(buffer, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer && buffer[strlen(buffer) - 1] == '\n') {
|
|
|
|
buffer[strlen(buffer) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-07-28 15:34:25 +02:00
|
|
|
static int parse_options(int argc, char **argv, struct sway_nagbar *nagbar,
|
|
|
|
list_t *types, char **config, bool *debug) {
|
2018-07-27 07:30:35 +02:00
|
|
|
static struct option opts[] = {
|
2018-07-26 03:57:19 +02:00
|
|
|
{"button", required_argument, NULL, 'b'},
|
2018-07-28 15:34:25 +02:00
|
|
|
{"config", required_argument, NULL, 'c'},
|
2018-07-26 03:57:19 +02:00
|
|
|
{"debug", no_argument, NULL, 'd'},
|
|
|
|
{"edge", required_argument, NULL, 'e'},
|
|
|
|
{"font", required_argument, NULL, 'f'},
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
2018-07-27 17:19:42 +02:00
|
|
|
{"detailed-message", no_argument, NULL, 'l'},
|
2018-07-27 07:30:35 +02:00
|
|
|
{"detailed-button", required_argument, NULL, 'L'},
|
2018-07-26 03:57:19 +02:00
|
|
|
{"message", required_argument, NULL, 'm'},
|
|
|
|
{"output", required_argument, NULL, 'o'},
|
2018-07-27 07:30:35 +02:00
|
|
|
{"dismiss-button", required_argument, NULL, 's'},
|
2018-07-26 03:57:19 +02:00
|
|
|
{"type", required_argument, NULL, 't'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *usage =
|
2018-07-27 17:19:42 +02:00
|
|
|
"Usage: swaynag [options...]\n"
|
2018-07-26 03:57:19 +02:00
|
|
|
"\n"
|
|
|
|
" -b, --button <text> <action> Create a button with text that "
|
|
|
|
"executes action when pressed. Multiple buttons can be defined.\n"
|
2018-07-28 15:34:25 +02:00
|
|
|
" -c, --config <path> Path to config file.\n"
|
2018-07-26 03:57:19 +02:00
|
|
|
" -d, --debug Enable debugging.\n"
|
|
|
|
" -e, --edge top|bottom Set the edge to use.\n"
|
|
|
|
" -f, --font <font> Set the font to use.\n"
|
|
|
|
" -h, --help Show help message and quit.\n"
|
2018-07-27 17:19:42 +02:00
|
|
|
" -l, --detailed-message Read a detailed message from stdin.\n"
|
2018-07-27 07:30:35 +02:00
|
|
|
" -L, --detailed-button <text> Set the text of the detail button.\n"
|
2018-07-26 03:57:19 +02:00
|
|
|
" -m, --message <msg> Set the message text.\n"
|
|
|
|
" -o, --output <output> Set the output to use.\n"
|
2018-07-27 07:30:35 +02:00
|
|
|
" -s, --dismiss-button <text> Set the dismiss button text.\n"
|
2018-07-28 15:34:25 +02:00
|
|
|
" -t, --type <type> Set the message type.\n"
|
2018-07-26 03:57:19 +02:00
|
|
|
" -v, --version Show the version number and quit.\n";
|
|
|
|
|
2018-07-28 15:34:25 +02:00
|
|
|
optind = 1;
|
2018-07-26 03:57:19 +02:00
|
|
|
while (1) {
|
2018-07-28 15:34:25 +02:00
|
|
|
int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL);
|
2018-07-26 03:57:19 +02:00
|
|
|
if (c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 'b': // Button
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
if (optind >= argc) {
|
|
|
|
fprintf(stderr, "Missing action for button %s\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
struct sway_nagbar_button *button;
|
|
|
|
button = calloc(sizeof(struct sway_nagbar_button), 1);
|
|
|
|
button->text = strdup(optarg);
|
|
|
|
button->type = NAGBAR_ACTION_COMMAND;
|
|
|
|
button->action = strdup(argv[optind]);
|
|
|
|
optind++;
|
|
|
|
list_add(nagbar->buttons, button);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c': // Config
|
|
|
|
if (config) {
|
|
|
|
*config = strdup(optarg);
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
break;
|
|
|
|
case 'd': // Debug
|
2018-07-28 15:34:25 +02:00
|
|
|
if (debug) {
|
|
|
|
*debug = true;
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
break;
|
|
|
|
case 'e': // Edge
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
if (strcmp(optarg, "top") == 0) {
|
|
|
|
nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
} else if (strcmp(optarg, "bottom") == 0) {
|
|
|
|
nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Invalid edge: %s\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f': // Font
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
free(nagbar->font);
|
|
|
|
nagbar->font = strdup(optarg);
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
break;
|
2018-07-27 07:30:35 +02:00
|
|
|
case 'l': // Detailed Message
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
free(nagbar->details.message);
|
|
|
|
nagbar->details.message = read_from_stdin();
|
|
|
|
nagbar->details.button_up.text = strdup("â–²");
|
|
|
|
nagbar->details.button_down.text = strdup("â–¼");
|
|
|
|
}
|
2018-07-27 07:30:35 +02:00
|
|
|
break;
|
|
|
|
case 'L': // Detailed Button Text
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
free(nagbar->details.button_details.text);
|
|
|
|
nagbar->details.button_details.text = strdup(optarg);
|
|
|
|
}
|
2018-07-27 07:30:35 +02:00
|
|
|
break;
|
2018-07-26 03:57:19 +02:00
|
|
|
case 'm': // Message
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
free(nagbar->message);
|
|
|
|
nagbar->message = strdup(optarg);
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
break;
|
|
|
|
case 'o': // Output
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
free(nagbar->output.name);
|
|
|
|
nagbar->output.name = strdup(optarg);
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
break;
|
2018-07-27 07:30:35 +02:00
|
|
|
case 's': // Dismiss Button Text
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
struct sway_nagbar_button *button_close;
|
|
|
|
button_close = nagbar->buttons->items[0];
|
|
|
|
free(button_close->text);
|
|
|
|
button_close->text = strdup(optarg);
|
|
|
|
}
|
2018-07-27 07:30:35 +02:00
|
|
|
break;
|
2018-07-26 03:57:19 +02:00
|
|
|
case 't': // Type
|
2018-07-28 15:34:25 +02:00
|
|
|
if (nagbar) {
|
|
|
|
nagbar->type = nagbar_type_get(types, optarg);
|
|
|
|
if (!nagbar->type) {
|
|
|
|
fprintf(stderr, "Unknown type %s\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'v': // Version
|
2018-07-27 17:19:42 +02:00
|
|
|
fprintf(stdout, "swaynag version " SWAY_VERSION "\n");
|
2018-07-28 15:34:25 +02:00
|
|
|
return -1;
|
2018-07-26 03:57:19 +02:00
|
|
|
default: // Help or unknown flag
|
|
|
|
fprintf(c == 'h' ? stdout : stderr, "%s", usage);
|
2018-07-28 15:34:25 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool file_exists(const char *path) {
|
|
|
|
return path && access(path, R_OK) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_config_path(void) {
|
|
|
|
static const char *config_paths[] = {
|
|
|
|
"$HOME/.swaynag/config",
|
|
|
|
"$XDG_CONFIG_HOME/swaynag/config",
|
|
|
|
SYSCONFDIR "/swaynag/config",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!getenv("XDG_CONFIG_HOME")) {
|
|
|
|
char *home = getenv("HOME");
|
|
|
|
char *config_home = malloc(strlen(home) + strlen("/.config") + 1);
|
|
|
|
if (!config_home) {
|
|
|
|
wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config");
|
|
|
|
} else {
|
|
|
|
strcpy(config_home, home);
|
|
|
|
strcat(config_home, "/.config");
|
|
|
|
setenv("XDG_CONFIG_HOME", config_home, 1);
|
|
|
|
wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home);
|
|
|
|
free(config_home);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wordexp_t p;
|
|
|
|
char *path;
|
|
|
|
for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) {
|
|
|
|
if (wordexp(config_paths[i], &p, 0) == 0) {
|
|
|
|
path = strdup(p.we_wordv[0]);
|
|
|
|
wordfree(&p);
|
|
|
|
if (file_exists(path)) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_config(char *path, struct sway_nagbar *nagbar, list_t *types) {
|
|
|
|
FILE *config = fopen(path, "r");
|
|
|
|
if (!config) {
|
|
|
|
fprintf(stderr, "Failed to read config. Running without it.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
struct sway_nagbar_type *type = NULL;
|
|
|
|
char *line;
|
|
|
|
int line_number = 0;
|
|
|
|
while (!feof(config)) {
|
|
|
|
line = read_line(config);
|
|
|
|
if (!line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_number++;
|
|
|
|
if (line[0] == '#') {
|
|
|
|
free(line);
|
|
|
|
continue;
|
2018-07-26 03:57:19 +02:00
|
|
|
}
|
2018-07-28 15:34:25 +02:00
|
|
|
if (strlen(line) == 0) {
|
|
|
|
free(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[0] == '[') {
|
|
|
|
char *close = strchr(line, ']');
|
|
|
|
if (!close) {
|
|
|
|
free(line);
|
|
|
|
fclose(config);
|
|
|
|
fprintf(stderr, "Closing bracket not found on line %d\n",
|
|
|
|
line_number);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char *name = calloc(1, close - line);
|
|
|
|
strncat(name, line + 1, close - line - 1);
|
|
|
|
type = nagbar_type_get(types, name);
|
|
|
|
if (!type) {
|
|
|
|
type = calloc(1, sizeof(struct sway_nagbar_type));
|
|
|
|
type->name = strdup(name);
|
|
|
|
list_add(types, type);
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
} else {
|
|
|
|
char flag[strlen(line) + 3];
|
|
|
|
sprintf(flag, "--%s", line);
|
|
|
|
char *argv[] = {"swaynag", flag};
|
|
|
|
int result;
|
|
|
|
if (type) {
|
|
|
|
result = nagbar_parse_type(2, argv, type);
|
|
|
|
} else {
|
|
|
|
result = parse_options(2, argv, nagbar, types, NULL, NULL);
|
|
|
|
}
|
|
|
|
if (result != 0) {
|
|
|
|
free(line);
|
|
|
|
fclose(config);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
2018-07-26 03:57:19 +02:00
|
|
|
}
|
2018-07-28 15:34:25 +02:00
|
|
|
fclose(config);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int exit_code = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
list_t *types = create_list();
|
|
|
|
nagbar_types_add_default(types);
|
|
|
|
|
|
|
|
memset(&nagbar, 0, sizeof(nagbar));
|
|
|
|
nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
nagbar.font = strdup("pango:monospace 10");
|
|
|
|
nagbar.buttons = create_list();
|
|
|
|
|
|
|
|
struct sway_nagbar_button *button_close =
|
|
|
|
calloc(sizeof(struct sway_nagbar_button), 1);
|
|
|
|
button_close->text = strdup("X");
|
|
|
|
button_close->type = NAGBAR_ACTION_DISMISS;
|
|
|
|
list_add(nagbar.buttons, button_close);
|
|
|
|
|
|
|
|
nagbar.details.button_details.text = strdup("Toggle Details");
|
|
|
|
nagbar.details.button_details.type = NAGBAR_ACTION_EXPAND;
|
2018-07-26 03:57:19 +02:00
|
|
|
|
2018-07-28 15:34:25 +02:00
|
|
|
|
|
|
|
char *config_path = NULL;
|
|
|
|
bool debug = false;
|
|
|
|
int launch_status = parse_options(argc, argv, NULL, NULL,
|
|
|
|
&config_path, &debug);
|
|
|
|
if (launch_status != 0) {
|
|
|
|
exit_code = launch_status;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-07-26 03:57:19 +02:00
|
|
|
wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL);
|
|
|
|
|
2018-07-28 15:34:25 +02:00
|
|
|
if (!config_path) {
|
|
|
|
config_path = get_config_path();
|
|
|
|
}
|
|
|
|
if (config_path) {
|
|
|
|
wlr_log(WLR_DEBUG, "Loading config file: %s", config_path);
|
|
|
|
int config_status = load_config(config_path, &nagbar, types);
|
|
|
|
free(config_path);
|
|
|
|
if (config_status != 0) {
|
|
|
|
exit_code = config_status;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
int result = parse_options(argc, argv, &nagbar, types, NULL, NULL);
|
|
|
|
if (result != 0) {
|
|
|
|
exit_code = result;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 03:57:19 +02:00
|
|
|
if (!nagbar.message) {
|
|
|
|
wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m");
|
|
|
|
exit_code = EXIT_FAILURE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-07-28 15:34:25 +02:00
|
|
|
if (!nagbar.type) {
|
|
|
|
nagbar.type = nagbar_type_get(types, "error");
|
|
|
|
}
|
|
|
|
|
|
|
|
nagbar.type = nagbar_type_clone(nagbar.type);
|
|
|
|
nagbar_types_free(types);
|
|
|
|
|
2018-07-27 07:30:35 +02:00
|
|
|
if (nagbar.details.message) {
|
2018-07-28 15:34:25 +02:00
|
|
|
list_add(nagbar.buttons, &nagbar.details.button_details);
|
2018-07-27 07:30:35 +02:00
|
|
|
} else {
|
2018-07-28 15:34:25 +02:00
|
|
|
free(nagbar.details.button_details.text);
|
2018-07-27 07:30:35 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 03:57:19 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name);
|
|
|
|
wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors);
|
2018-07-28 15:34:25 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Type: %s", nagbar.type->name);
|
2018-07-26 03:57:19 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Message: %s", nagbar.message);
|
|
|
|
wlr_log(WLR_DEBUG, "Font: %s", nagbar.font);
|
|
|
|
wlr_log(WLR_DEBUG, "Buttons");
|
|
|
|
for (int i = 0; i < nagbar.buttons->length; i++) {
|
|
|
|
struct sway_nagbar_button *button = nagbar.buttons->items[i];
|
|
|
|
wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action);
|
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGTERM, sig_handler);
|
|
|
|
|
|
|
|
nagbar_setup(&nagbar);
|
|
|
|
nagbar_run(&nagbar);
|
|
|
|
return exit_code;
|
|
|
|
|
|
|
|
cleanup:
|
2018-07-28 15:34:25 +02:00
|
|
|
nagbar_types_free(types);
|
|
|
|
free(nagbar.details.button_details.text);
|
2018-07-26 03:57:19 +02:00
|
|
|
nagbar_destroy(&nagbar);
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|