|
| 1 | +#define _XOPEN_SOURCE 700 |
| 2 | +#define _POSIX_C_SOURCE 200112L |
| 3 | +#include <getopt.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <wordexp.h> |
| 6 | +#include "log.h" |
| 7 | +#include "list.h" |
| 8 | +#include "readline.h" |
| 9 | +#include "swaynag/nagbar.h" |
| 10 | +#include "swaynag/types.h" |
| 11 | +#include "wlr-layer-shell-unstable-v1-client-protocol.h" |
| 12 | + |
| 13 | +static char *read_from_stdin() { |
| 14 | + char *buffer = NULL; |
| 15 | + while (!feof(stdin)) { |
| 16 | + char *line = read_line(stdin); |
| 17 | + if (!line) { |
| 18 | + continue; |
| 19 | + } |
| 20 | + |
| 21 | + if (!buffer) { |
| 22 | + buffer = strdup(line); |
| 23 | + } else { |
| 24 | + buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); |
| 25 | + strcat(buffer, line); |
| 26 | + strcat(buffer, "\n"); |
| 27 | + } |
| 28 | + |
| 29 | + free(line); |
| 30 | + } |
| 31 | + |
| 32 | + if (buffer && buffer[strlen(buffer) - 1] == '\n') { |
| 33 | + buffer[strlen(buffer) - 1] = '\0'; |
| 34 | + } |
| 35 | + |
| 36 | + return buffer; |
| 37 | +} |
| 38 | + |
| 39 | +int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, |
| 40 | + list_t *types, char **config, bool *debug) { |
| 41 | + static struct option opts[] = { |
| 42 | + {"button", required_argument, NULL, 'b'}, |
| 43 | + {"config", required_argument, NULL, 'c'}, |
| 44 | + {"debug", no_argument, NULL, 'd'}, |
| 45 | + {"edge", required_argument, NULL, 'e'}, |
| 46 | + {"font", required_argument, NULL, 'f'}, |
| 47 | + {"help", no_argument, NULL, 'h'}, |
| 48 | + {"detailed-message", no_argument, NULL, 'l'}, |
| 49 | + {"detailed-button", required_argument, NULL, 'L'}, |
| 50 | + {"message", required_argument, NULL, 'm'}, |
| 51 | + {"output", required_argument, NULL, 'o'}, |
| 52 | + {"dismiss-button", required_argument, NULL, 's'}, |
| 53 | + {"type", required_argument, NULL, 't'}, |
| 54 | + {"version", no_argument, NULL, 'v'}, |
| 55 | + {0, 0, 0, 0} |
| 56 | + }; |
| 57 | + |
| 58 | + const char *usage = |
| 59 | + "Usage: swaynag [options...]\n" |
| 60 | + "\n" |
| 61 | + " -b, --button <text> <action> Create a button with text that " |
| 62 | + "executes action when pressed. Multiple buttons can be defined.\n" |
| 63 | + " -c, --config <path> Path to config file.\n" |
| 64 | + " -d, --debug Enable debugging.\n" |
| 65 | + " -e, --edge top|bottom Set the edge to use.\n" |
| 66 | + " -f, --font <font> Set the font to use.\n" |
| 67 | + " -h, --help Show help message and quit.\n" |
| 68 | + " -l, --detailed-message Read a detailed message from stdin.\n" |
| 69 | + " -L, --detailed-button <text> Set the text of the detail button.\n" |
| 70 | + " -m, --message <msg> Set the message text.\n" |
| 71 | + " -o, --output <output> Set the output to use.\n" |
| 72 | + " -s, --dismiss-button <text> Set the dismiss button text.\n" |
| 73 | + " -t, --type <type> Set the message type.\n" |
| 74 | + " -v, --version Show the version number and quit.\n"; |
| 75 | + |
| 76 | + optind = 1; |
| 77 | + while (1) { |
| 78 | + int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); |
| 79 | + if (c == -1) { |
| 80 | + break; |
| 81 | + } |
| 82 | + switch (c) { |
| 83 | + case 'b': // Button |
| 84 | + if (nagbar) { |
| 85 | + if (optind >= argc) { |
| 86 | + fprintf(stderr, "Missing action for button %s\n", optarg); |
| 87 | + return EXIT_FAILURE; |
| 88 | + } |
| 89 | + struct sway_nagbar_button *button; |
| 90 | + button = calloc(sizeof(struct sway_nagbar_button), 1); |
| 91 | + button->text = strdup(optarg); |
| 92 | + button->type = NAGBAR_ACTION_COMMAND; |
| 93 | + button->action = strdup(argv[optind]); |
| 94 | + list_add(nagbar->buttons, button); |
| 95 | + } |
| 96 | + optind++; |
| 97 | + break; |
| 98 | + case 'c': // Config |
| 99 | + if (config) { |
| 100 | + *config = strdup(optarg); |
| 101 | + } |
| 102 | + break; |
| 103 | + case 'd': // Debug |
| 104 | + if (debug) { |
| 105 | + *debug = true; |
| 106 | + } |
| 107 | + break; |
| 108 | + case 'e': // Edge |
| 109 | + if (nagbar) { |
| 110 | + if (strcmp(optarg, "top") == 0) { |
| 111 | + nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
| 112 | + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
| 113 | + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; |
| 114 | + } else if (strcmp(optarg, "bottom") == 0) { |
| 115 | + nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
| 116 | + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
| 117 | + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; |
| 118 | + } else { |
| 119 | + fprintf(stderr, "Invalid edge: %s\n", optarg); |
| 120 | + return EXIT_FAILURE; |
| 121 | + } |
| 122 | + } |
| 123 | + break; |
| 124 | + case 'f': // Font |
| 125 | + if (nagbar) { |
| 126 | + free(nagbar->font); |
| 127 | + nagbar->font = strdup(optarg); |
| 128 | + } |
| 129 | + break; |
| 130 | + case 'l': // Detailed Message |
| 131 | + if (nagbar) { |
| 132 | + free(nagbar->details.message); |
| 133 | + nagbar->details.message = read_from_stdin(); |
| 134 | + nagbar->details.button_up.text = strdup("▲"); |
| 135 | + nagbar->details.button_down.text = strdup("▼"); |
| 136 | + } |
| 137 | + break; |
| 138 | + case 'L': // Detailed Button Text |
| 139 | + if (nagbar) { |
| 140 | + free(nagbar->details.button_details.text); |
| 141 | + nagbar->details.button_details.text = strdup(optarg); |
| 142 | + } |
| 143 | + break; |
| 144 | + case 'm': // Message |
| 145 | + if (nagbar) { |
| 146 | + free(nagbar->message); |
| 147 | + nagbar->message = strdup(optarg); |
| 148 | + } |
| 149 | + break; |
| 150 | + case 'o': // Output |
| 151 | + if (nagbar) { |
| 152 | + free(nagbar->output.name); |
| 153 | + nagbar->output.name = strdup(optarg); |
| 154 | + } |
| 155 | + break; |
| 156 | + case 's': // Dismiss Button Text |
| 157 | + if (nagbar) { |
| 158 | + struct sway_nagbar_button *button_close; |
| 159 | + button_close = nagbar->buttons->items[0]; |
| 160 | + free(button_close->text); |
| 161 | + button_close->text = strdup(optarg); |
| 162 | + } |
| 163 | + break; |
| 164 | + case 't': // Type |
| 165 | + if (nagbar) { |
| 166 | + nagbar->type = nagbar_type_get(types, optarg); |
| 167 | + if (!nagbar->type) { |
| 168 | + fprintf(stderr, "Unknown type %s\n", optarg); |
| 169 | + return EXIT_FAILURE; |
| 170 | + } |
| 171 | + } |
| 172 | + break; |
| 173 | + case 'v': // Version |
| 174 | + fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); |
| 175 | + return -1; |
| 176 | + default: // Help or unknown flag |
| 177 | + fprintf(c == 'h' ? stdout : stderr, "%s", usage); |
| 178 | + return -1; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return 0; |
| 183 | +} |
| 184 | + |
| 185 | +static bool file_exists(const char *path) { |
| 186 | + return path && access(path, R_OK) != -1; |
| 187 | +} |
| 188 | + |
| 189 | +char *nagbar_get_config_path(void) { |
| 190 | + static const char *config_paths[] = { |
| 191 | + "$HOME/.swaynag/config", |
| 192 | + "$XDG_CONFIG_HOME/swaynag/config", |
| 193 | + SYSCONFDIR "/swaynag/config", |
| 194 | + }; |
| 195 | + |
| 196 | + if (!getenv("XDG_CONFIG_HOME")) { |
| 197 | + char *home = getenv("HOME"); |
| 198 | + char *config_home = malloc(strlen(home) + strlen("/.config") + 1); |
| 199 | + if (!config_home) { |
| 200 | + wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config"); |
| 201 | + } else { |
| 202 | + strcpy(config_home, home); |
| 203 | + strcat(config_home, "/.config"); |
| 204 | + setenv("XDG_CONFIG_HOME", config_home, 1); |
| 205 | + wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); |
| 206 | + free(config_home); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + wordexp_t p; |
| 211 | + char *path; |
| 212 | + for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { |
| 213 | + if (wordexp(config_paths[i], &p, 0) == 0) { |
| 214 | + path = strdup(p.we_wordv[0]); |
| 215 | + wordfree(&p); |
| 216 | + if (file_exists(path)) { |
| 217 | + return path; |
| 218 | + } |
| 219 | + free(path); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return NULL; |
| 224 | +} |
| 225 | + |
| 226 | +int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { |
| 227 | + FILE *config = fopen(path, "r"); |
| 228 | + if (!config) { |
| 229 | + fprintf(stderr, "Failed to read config. Running without it.\n"); |
| 230 | + return 0; |
| 231 | + } |
| 232 | + struct sway_nagbar_type *type = NULL; |
| 233 | + char *line; |
| 234 | + int line_number = 0; |
| 235 | + while (!feof(config)) { |
| 236 | + line = read_line(config); |
| 237 | + if (!line) { |
| 238 | + continue; |
| 239 | + } |
| 240 | + |
| 241 | + line_number++; |
| 242 | + if (line[0] == '#') { |
| 243 | + free(line); |
| 244 | + continue; |
| 245 | + } |
| 246 | + if (strlen(line) == 0) { |
| 247 | + free(line); |
| 248 | + continue; |
| 249 | + } |
| 250 | + |
| 251 | + if (line[0] == '[') { |
| 252 | + char *close = strchr(line, ']'); |
| 253 | + if (!close) { |
| 254 | + free(line); |
| 255 | + fclose(config); |
| 256 | + fprintf(stderr, "Closing bracket not found on line %d\n", |
| 257 | + line_number); |
| 258 | + return 1; |
| 259 | + } |
| 260 | + char *name = calloc(1, close - line); |
| 261 | + strncat(name, line + 1, close - line - 1); |
| 262 | + type = nagbar_type_get(types, name); |
| 263 | + if (!type) { |
| 264 | + type = calloc(1, sizeof(struct sway_nagbar_type)); |
| 265 | + type->name = strdup(name); |
| 266 | + list_add(types, type); |
| 267 | + } |
| 268 | + free(name); |
| 269 | + } else { |
| 270 | + char flag[strlen(line) + 3]; |
| 271 | + sprintf(flag, "--%s", line); |
| 272 | + char *argv[] = {"swaynag", flag}; |
| 273 | + int result; |
| 274 | + if (type) { |
| 275 | + result = nagbar_parse_type(2, argv, type); |
| 276 | + } else { |
| 277 | + result = nagbar_parse_options(2, argv, nagbar, types, |
| 278 | + NULL, NULL); |
| 279 | + } |
| 280 | + if (result != 0) { |
| 281 | + free(line); |
| 282 | + fclose(config); |
| 283 | + return result; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + free(line); |
| 288 | + } |
| 289 | + fclose(config); |
| 290 | + return 0; |
| 291 | +} |
| 292 | + |
0 commit comments