sway/swaynag/main.c

120 lines
3.1 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
2018-07-26 01:57:19 +00:00
#include <signal.h>
#include "log.h"
#include "list.h"
#include "swaynag/config.h"
#include "swaynag/swaynag.h"
2018-07-28 13:34:25 +00:00
#include "swaynag/types.h"
2018-07-26 01:57:19 +00:00
static struct swaynag swaynag;
2018-07-26 01:57:19 +00:00
void sig_handler(int signal) {
swaynag_destroy(&swaynag);
2018-07-26 01:57:19 +00:00
exit(EXIT_FAILURE);
}
void sway_terminate(int code) {
swaynag_destroy(&swaynag);
2018-07-26 01:57:19 +00:00
exit(code);
}
2018-07-28 13:34:25 +00:00
int main(int argc, char **argv) {
int status = EXIT_SUCCESS;
2018-07-28 13:34:25 +00:00
list_t *types = create_list();
swaynag_types_add_default(types);
2018-07-28 13:34:25 +00:00
swaynag.buttons = create_list();
wl_list_init(&swaynag.outputs);
wl_list_init(&swaynag.seats);
2018-07-28 13:34:25 +00:00
char *config_path = NULL;
bool debug = false;
status = swaynag_parse_options(argc, argv, NULL, NULL, NULL,
2018-07-28 13:34:25 +00:00
&config_path, &debug);
if (status != 0) {
2018-07-28 13:34:25 +00:00
goto cleanup;
}
sway_log_init(debug ? SWAY_DEBUG : SWAY_ERROR, NULL);
2018-07-26 01:57:19 +00:00
2018-07-28 13:34:25 +00:00
if (!config_path) {
config_path = swaynag_get_config_path();
2018-07-28 13:34:25 +00:00
}
if (config_path) {
sway_log(SWAY_DEBUG, "Loading config file: %s", config_path);
status = swaynag_load_config(config_path, &swaynag, types);
if (status != 0) {
2018-07-28 13:34:25 +00:00
goto cleanup;
}
}
swaynag.details.button_details.text = strdup("Toggle details");
swaynag.details.button_details.type = SWAYNAG_ACTION_EXPAND;
2018-07-28 13:34:25 +00:00
if (argc > 1) {
struct swaynag_type *type_args = swaynag_type_new("<args>");
2018-07-30 02:42:03 +00:00
list_add(types, type_args);
status = swaynag_parse_options(argc, argv, &swaynag, types,
2018-07-30 02:42:03 +00:00
type_args, NULL, NULL);
if (status != 0) {
2018-07-28 13:34:25 +00:00
goto cleanup;
}
}
if (!swaynag.message) {
sway_log(SWAY_ERROR, "No message passed. Please provide --message/-m");
status = EXIT_FAILURE;
2018-07-26 01:57:19 +00:00
goto cleanup;
}
if (!swaynag.type) {
swaynag.type = swaynag_type_get(types, "error");
2018-07-28 13:34:25 +00:00
}
// Construct a new type with the defaults as the base, the general config
// on top of that, followed by the type config, and finally any command
// line arguments
struct swaynag_type *type = swaynag_type_new(swaynag.type->name);
2018-07-30 02:42:03 +00:00
swaynag_type_merge(type, swaynag_type_get(types, "<defaults>"));
swaynag_type_merge(type, swaynag_type_get(types, "<config>"));
swaynag_type_merge(type, swaynag.type);
swaynag_type_merge(type, swaynag_type_get(types, "<args>"));
2018-07-30 02:42:03 +00:00
swaynag.type = type;
swaynag_types_free(types);
2018-07-28 13:34:25 +00:00
struct swaynag_button button_close = { 0 };
button_close.text = strdup("X");
button_close.type = SWAYNAG_ACTION_DISMISS;
list_add(swaynag.buttons, &button_close);
if (swaynag.details.message) {
list_add(swaynag.buttons, &swaynag.details.button_details);
}
sway_log(SWAY_DEBUG, "Output: %s", swaynag.type->output);
2020-06-04 13:43:42 +00:00
sway_log(SWAY_DEBUG, "Anchors: %" PRIu32, swaynag.type->anchors);
sway_log(SWAY_DEBUG, "Type: %s", swaynag.type->name);
sway_log(SWAY_DEBUG, "Message: %s", swaynag.message);
sway_log(SWAY_DEBUG, "Font: %s", swaynag.type->font);
sway_log(SWAY_DEBUG, "Buttons");
for (int i = 0; i < swaynag.buttons->length; i++) {
struct swaynag_button *button = swaynag.buttons->items[i];
sway_log(SWAY_DEBUG, "\t[%s] `%s`", button->text, button->action);
2018-07-26 01:57:19 +00:00
}
signal(SIGTERM, sig_handler);
swaynag_setup(&swaynag);
swaynag_run(&swaynag);
return status;
2018-07-26 01:57:19 +00:00
cleanup:
swaynag_types_free(types);
free(swaynag.details.button_details.text);
swaynag_destroy(&swaynag);
return status;
2018-07-26 01:57:19 +00:00
}