diff options
Diffstat (limited to 'swaynagbar/main.c')
-rw-r--r-- | swaynagbar/main.c | 76 |
1 files changed, 71 insertions, 5 deletions
diff --git a/swaynagbar/main.c b/swaynagbar/main.c index f5ed064e..389118c6 100644 --- a/swaynagbar/main.c +++ b/swaynagbar/main.c | |||
@@ -3,6 +3,7 @@ | |||
3 | #include <signal.h> | 3 | #include <signal.h> |
4 | #include "log.h" | 4 | #include "log.h" |
5 | #include "list.h" | 5 | #include "list.h" |
6 | #include "readline.h" | ||
6 | #include "swaynagbar/nagbar.h" | 7 | #include "swaynagbar/nagbar.h" |
7 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | 8 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" |
8 | 9 | ||
@@ -34,6 +35,32 @@ static void set_nagbar_colors() { | |||
34 | } | 35 | } |
35 | } | 36 | } |
36 | 37 | ||
38 | static char *read_from_stdin() { | ||
39 | char *buffer = NULL; | ||
40 | while (!feof(stdin)) { | ||
41 | char *line = read_line(stdin); | ||
42 | if (!line) { | ||
43 | continue; | ||
44 | } | ||
45 | |||
46 | if (!buffer) { | ||
47 | buffer = strdup(line); | ||
48 | } else { | ||
49 | buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); | ||
50 | strcat(buffer, line); | ||
51 | strcat(buffer, "\n"); | ||
52 | } | ||
53 | |||
54 | free(line); | ||
55 | } | ||
56 | |||
57 | if (buffer && buffer[strlen(buffer) - 1] == '\n') { | ||
58 | buffer[strlen(buffer) - 1] = '\0'; | ||
59 | } | ||
60 | |||
61 | return buffer; | ||
62 | } | ||
63 | |||
37 | int main(int argc, char **argv) { | 64 | int main(int argc, char **argv) { |
38 | int exit_code = EXIT_SUCCESS; | 65 | int exit_code = EXIT_SUCCESS; |
39 | bool debug = false; | 66 | bool debug = false; |
@@ -50,17 +77,25 @@ int main(int argc, char **argv) { | |||
50 | struct sway_nagbar_button *button_close = | 77 | struct sway_nagbar_button *button_close = |
51 | calloc(sizeof(struct sway_nagbar_button), 1); | 78 | calloc(sizeof(struct sway_nagbar_button), 1); |
52 | button_close->text = strdup("X"); | 79 | button_close->text = strdup("X"); |
53 | button_close->action = NULL; | 80 | button_close->type = NAGBAR_ACTION_DISMISS; |
54 | list_add(nagbar.buttons, button_close); | 81 | list_add(nagbar.buttons, button_close); |
55 | 82 | ||
56 | static struct option long_options[] = { | 83 | struct sway_nagbar_button *button_details = |
84 | calloc(sizeof(struct sway_nagbar_button), 1); | ||
85 | button_details->text = strdup("Toggle Details"); | ||
86 | button_details->type = NAGBAR_ACTION_EXPAND; | ||
87 | |||
88 | static struct option opts[] = { | ||
57 | {"button", required_argument, NULL, 'b'}, | 89 | {"button", required_argument, NULL, 'b'}, |
58 | {"debug", no_argument, NULL, 'd'}, | 90 | {"debug", no_argument, NULL, 'd'}, |
59 | {"edge", required_argument, NULL, 'e'}, | 91 | {"edge", required_argument, NULL, 'e'}, |
60 | {"font", required_argument, NULL, 'f'}, | 92 | {"font", required_argument, NULL, 'f'}, |
61 | {"help", no_argument, NULL, 'h'}, | 93 | {"help", no_argument, NULL, 'h'}, |
94 | {"detailed-message", required_argument, NULL, 'l'}, | ||
95 | {"detailed-button", required_argument, NULL, 'L'}, | ||
62 | {"message", required_argument, NULL, 'm'}, | 96 | {"message", required_argument, NULL, 'm'}, |
63 | {"output", required_argument, NULL, 'o'}, | 97 | {"output", required_argument, NULL, 'o'}, |
98 | {"dismiss-button", required_argument, NULL, 's'}, | ||
64 | {"type", required_argument, NULL, 't'}, | 99 | {"type", required_argument, NULL, 't'}, |
65 | {"version", no_argument, NULL, 'v'}, | 100 | {"version", no_argument, NULL, 'v'}, |
66 | {0, 0, 0, 0} | 101 | {0, 0, 0, 0} |
@@ -75,26 +110,30 @@ int main(int argc, char **argv) { | |||
75 | " -e, --edge top|bottom Set the edge to use.\n" | 110 | " -e, --edge top|bottom Set the edge to use.\n" |
76 | " -f, --font <font> Set the font to use.\n" | 111 | " -f, --font <font> Set the font to use.\n" |
77 | " -h, --help Show help message and quit.\n" | 112 | " -h, --help Show help message and quit.\n" |
113 | " -l, --detailed-message <msg> Set a detailed message.\n" | ||
114 | " -L, --detailed-button <text> Set the text of the detail button.\n" | ||
78 | " -m, --message <msg> Set the message text.\n" | 115 | " -m, --message <msg> Set the message text.\n" |
79 | " -o, --output <output> Set the output to use.\n" | 116 | " -o, --output <output> Set the output to use.\n" |
117 | " -s, --dismiss-button <text> Set the dismiss button text.\n" | ||
80 | " -t, --type error|warning Set the message type.\n" | 118 | " -t, --type error|warning Set the message type.\n" |
81 | " -v, --version Show the version number and quit.\n"; | 119 | " -v, --version Show the version number and quit.\n"; |
82 | 120 | ||
83 | while (1) { | 121 | while (1) { |
84 | int c = getopt_long(argc, argv, "b:de:f:hm:o:t:v", long_options, NULL); | 122 | int c = getopt_long(argc, argv, "b:de:f:hl:L:m:o:s:t:v", opts, NULL); |
85 | if (c == -1) { | 123 | if (c == -1) { |
86 | break; | 124 | break; |
87 | } | 125 | } |
88 | switch (c) { | 126 | switch (c) { |
89 | case 'b': // Button | 127 | case 'b': // Button |
90 | if (optind >= argc) { | 128 | if (optind >= argc) { |
91 | fprintf(stderr, "Missing action for button %s", optarg); | 129 | fprintf(stderr, "Missing action for button %s\n", optarg); |
92 | exit_code = EXIT_FAILURE; | 130 | exit_code = EXIT_FAILURE; |
93 | goto cleanup; | 131 | goto cleanup; |
94 | } | 132 | } |
95 | struct sway_nagbar_button *button; | 133 | struct sway_nagbar_button *button; |
96 | button = calloc(sizeof(struct sway_nagbar_button), 1); | 134 | button = calloc(sizeof(struct sway_nagbar_button), 1); |
97 | button->text = strdup(optarg); | 135 | button->text = strdup(optarg); |
136 | button->type = NAGBAR_ACTION_COMMAND; | ||
98 | button->action = strdup(argv[optind]); | 137 | button->action = strdup(argv[optind]); |
99 | optind++; | 138 | optind++; |
100 | list_add(nagbar.buttons, button); | 139 | list_add(nagbar.buttons, button); |
@@ -121,6 +160,20 @@ int main(int argc, char **argv) { | |||
121 | free(nagbar.font); | 160 | free(nagbar.font); |
122 | nagbar.font = strdup(optarg); | 161 | nagbar.font = strdup(optarg); |
123 | break; | 162 | break; |
163 | case 'l': // Detailed Message | ||
164 | free(nagbar.details.message); | ||
165 | if (strcmp(optarg, "-") == 0) { | ||
166 | nagbar.details.message = read_from_stdin(); | ||
167 | } else { | ||
168 | nagbar.details.message = strdup(optarg); | ||
169 | } | ||
170 | nagbar.details.button_up.text = strdup("▲"); | ||
171 | nagbar.details.button_down.text = strdup("▼"); | ||
172 | break; | ||
173 | case 'L': // Detailed Button Text | ||
174 | free(button_details->text); | ||
175 | button_details->text = strdup(optarg); | ||
176 | break; | ||
124 | case 'm': // Message | 177 | case 'm': // Message |
125 | free(nagbar.message); | 178 | free(nagbar.message); |
126 | nagbar.message = strdup(optarg); | 179 | nagbar.message = strdup(optarg); |
@@ -129,13 +182,17 @@ int main(int argc, char **argv) { | |||
129 | free(nagbar.output.name); | 182 | free(nagbar.output.name); |
130 | nagbar.output.name = strdup(optarg); | 183 | nagbar.output.name = strdup(optarg); |
131 | break; | 184 | break; |
185 | case 's': // Dismiss Button Text | ||
186 | free(button_close->text); | ||
187 | button_close->text = strdup(optarg); | ||
188 | break; | ||
132 | case 't': // Type | 189 | case 't': // Type |
133 | if (strcmp(optarg, "error") == 0) { | 190 | if (strcmp(optarg, "error") == 0) { |
134 | nagbar.type = NAGBAR_ERROR; | 191 | nagbar.type = NAGBAR_ERROR; |
135 | } else if (strcmp(optarg, "warning") == 0) { | 192 | } else if (strcmp(optarg, "warning") == 0) { |
136 | nagbar.type = NAGBAR_WARNING; | 193 | nagbar.type = NAGBAR_WARNING; |
137 | } else { | 194 | } else { |
138 | fprintf(stderr, "Type must be either 'error' or 'warning'"); | 195 | fprintf(stderr, "Type must be either 'error' or 'warning'\n"); |
139 | exit_code = EXIT_FAILURE; | 196 | exit_code = EXIT_FAILURE; |
140 | goto cleanup; | 197 | goto cleanup; |
141 | } | 198 | } |
@@ -160,6 +217,13 @@ int main(int argc, char **argv) { | |||
160 | goto cleanup; | 217 | goto cleanup; |
161 | } | 218 | } |
162 | 219 | ||
220 | if (nagbar.details.message) { | ||
221 | list_add(nagbar.buttons, button_details); | ||
222 | } else { | ||
223 | free(button_details->text); | ||
224 | free(button_details); | ||
225 | } | ||
226 | |||
163 | wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); | 227 | wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); |
164 | wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); | 228 | wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); |
165 | wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); | 229 | wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); |
@@ -178,6 +242,8 @@ int main(int argc, char **argv) { | |||
178 | return exit_code; | 242 | return exit_code; |
179 | 243 | ||
180 | cleanup: | 244 | cleanup: |
245 | free(button_details->text); | ||
246 | free(button_details); | ||
181 | nagbar_destroy(&nagbar); | 247 | nagbar_destroy(&nagbar); |
182 | return exit_code; | 248 | return exit_code; |
183 | } | 249 | } |