diff options
Diffstat (limited to 'swaynagbar/main.c')
-rw-r--r-- | swaynagbar/main.c | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/swaynagbar/main.c b/swaynagbar/main.c new file mode 100644 index 00000000..f5ed064e --- /dev/null +++ b/swaynagbar/main.c | |||
@@ -0,0 +1,184 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <getopt.h> | ||
3 | #include <signal.h> | ||
4 | #include "log.h" | ||
5 | #include "list.h" | ||
6 | #include "swaynagbar/nagbar.h" | ||
7 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||
8 | |||
9 | static struct sway_nagbar nagbar; | ||
10 | |||
11 | void sig_handler(int signal) { | ||
12 | nagbar_destroy(&nagbar); | ||
13 | exit(EXIT_FAILURE); | ||
14 | } | ||
15 | |||
16 | void sway_terminate(int code) { | ||
17 | nagbar_destroy(&nagbar); | ||
18 | exit(code); | ||
19 | } | ||
20 | |||
21 | static void set_nagbar_colors() { | ||
22 | if (nagbar.type == NAGBAR_ERROR) { | ||
23 | nagbar.colors.button_background = 0x680A0AFF; | ||
24 | nagbar.colors.background = 0x900000FF; | ||
25 | nagbar.colors.text = 0xFFFFFFFF; | ||
26 | nagbar.colors.border = 0xD92424FF; | ||
27 | nagbar.colors.border_bottom = 0x470909FF; | ||
28 | } else if (nagbar.type == NAGBAR_WARNING) { | ||
29 | nagbar.colors.button_background = 0xFFC100FF; | ||
30 | nagbar.colors.background = 0xFFA800FF; | ||
31 | nagbar.colors.text = 0x000000FF; | ||
32 | nagbar.colors.border = 0xAB7100FF; | ||
33 | nagbar.colors.border_bottom = 0xAB7100FF; | ||
34 | } | ||
35 | } | ||
36 | |||
37 | int main(int argc, char **argv) { | ||
38 | int exit_code = EXIT_SUCCESS; | ||
39 | bool debug = false; | ||
40 | |||
41 | memset(&nagbar, 0, sizeof(nagbar)); | ||
42 | nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ||
43 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ||
44 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; | ||
45 | nagbar.type = NAGBAR_ERROR; | ||
46 | set_nagbar_colors(); | ||
47 | nagbar.font = strdup("pango:monospace 8"); | ||
48 | nagbar.buttons = create_list(); | ||
49 | |||
50 | struct sway_nagbar_button *button_close = | ||
51 | calloc(sizeof(struct sway_nagbar_button), 1); | ||
52 | button_close->text = strdup("X"); | ||
53 | button_close->action = NULL; | ||
54 | list_add(nagbar.buttons, button_close); | ||
55 | |||
56 | static struct option long_options[] = { | ||
57 | {"button", required_argument, NULL, 'b'}, | ||
58 | {"debug", no_argument, NULL, 'd'}, | ||
59 | {"edge", required_argument, NULL, 'e'}, | ||
60 | {"font", required_argument, NULL, 'f'}, | ||
61 | {"help", no_argument, NULL, 'h'}, | ||
62 | {"message", required_argument, NULL, 'm'}, | ||
63 | {"output", required_argument, NULL, 'o'}, | ||
64 | {"type", required_argument, NULL, 't'}, | ||
65 | {"version", no_argument, NULL, 'v'}, | ||
66 | {0, 0, 0, 0} | ||
67 | }; | ||
68 | |||
69 | const char *usage = | ||
70 | "Usage: swaynagbar [options...]\n" | ||
71 | "\n" | ||
72 | " -b, --button <text> <action> Create a button with text that " | ||
73 | "executes action when pressed. Multiple buttons can be defined.\n" | ||
74 | " -d, --debug Enable debugging.\n" | ||
75 | " -e, --edge top|bottom Set the edge to use.\n" | ||
76 | " -f, --font <font> Set the font to use.\n" | ||
77 | " -h, --help Show help message and quit.\n" | ||
78 | " -m, --message <msg> Set the message text.\n" | ||
79 | " -o, --output <output> Set the output to use.\n" | ||
80 | " -t, --type error|warning Set the message type.\n" | ||
81 | " -v, --version Show the version number and quit.\n"; | ||
82 | |||
83 | while (1) { | ||
84 | int c = getopt_long(argc, argv, "b:de:f:hm:o:t:v", long_options, NULL); | ||
85 | if (c == -1) { | ||
86 | break; | ||
87 | } | ||
88 | switch (c) { | ||
89 | case 'b': // Button | ||
90 | if (optind >= argc) { | ||
91 | fprintf(stderr, "Missing action for button %s", optarg); | ||
92 | exit_code = EXIT_FAILURE; | ||
93 | goto cleanup; | ||
94 | } | ||
95 | struct sway_nagbar_button *button; | ||
96 | button = calloc(sizeof(struct sway_nagbar_button), 1); | ||
97 | button->text = strdup(optarg); | ||
98 | button->action = strdup(argv[optind]); | ||
99 | optind++; | ||
100 | list_add(nagbar.buttons, button); | ||
101 | break; | ||
102 | case 'd': // Debug | ||
103 | debug = true; | ||
104 | break; | ||
105 | case 'e': // Edge | ||
106 | if (strcmp(optarg, "top") == 0) { | ||
107 | nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ||
108 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ||
109 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; | ||
110 | } else if (strcmp(optarg, "bottom") == 0) { | ||
111 | nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ||
112 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ||
113 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; | ||
114 | } else { | ||
115 | fprintf(stderr, "Invalid edge: %s\n", optarg); | ||
116 | exit_code = EXIT_FAILURE; | ||
117 | goto cleanup; | ||
118 | } | ||
119 | break; | ||
120 | case 'f': // Font | ||
121 | free(nagbar.font); | ||
122 | nagbar.font = strdup(optarg); | ||
123 | break; | ||
124 | case 'm': // Message | ||
125 | free(nagbar.message); | ||
126 | nagbar.message = strdup(optarg); | ||
127 | break; | ||
128 | case 'o': // Output | ||
129 | free(nagbar.output.name); | ||
130 | nagbar.output.name = strdup(optarg); | ||
131 | break; | ||
132 | case 't': // Type | ||
133 | if (strcmp(optarg, "error") == 0) { | ||
134 | nagbar.type = NAGBAR_ERROR; | ||
135 | } else if (strcmp(optarg, "warning") == 0) { | ||
136 | nagbar.type = NAGBAR_WARNING; | ||
137 | } else { | ||
138 | fprintf(stderr, "Type must be either 'error' or 'warning'"); | ||
139 | exit_code = EXIT_FAILURE; | ||
140 | goto cleanup; | ||
141 | } | ||
142 | set_nagbar_colors(); | ||
143 | break; | ||
144 | case 'v': // Version | ||
145 | fprintf(stdout, "sway version " SWAY_VERSION "\n"); | ||
146 | exit_code = EXIT_SUCCESS; | ||
147 | goto cleanup; | ||
148 | default: // Help or unknown flag | ||
149 | fprintf(c == 'h' ? stdout : stderr, "%s", usage); | ||
150 | exit_code = c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE; | ||
151 | goto cleanup; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); | ||
156 | |||
157 | if (!nagbar.message) { | ||
158 | wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m"); | ||
159 | exit_code = EXIT_FAILURE; | ||
160 | goto cleanup; | ||
161 | } | ||
162 | |||
163 | wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); | ||
164 | wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); | ||
165 | wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); | ||
166 | wlr_log(WLR_DEBUG, "Message: %s", nagbar.message); | ||
167 | wlr_log(WLR_DEBUG, "Font: %s", nagbar.font); | ||
168 | wlr_log(WLR_DEBUG, "Buttons"); | ||
169 | for (int i = 0; i < nagbar.buttons->length; i++) { | ||
170 | struct sway_nagbar_button *button = nagbar.buttons->items[i]; | ||
171 | wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action); | ||
172 | } | ||
173 | |||
174 | signal(SIGTERM, sig_handler); | ||
175 | |||
176 | nagbar_setup(&nagbar); | ||
177 | nagbar_run(&nagbar); | ||
178 | return exit_code; | ||
179 | |||
180 | cleanup: | ||
181 | nagbar_destroy(&nagbar); | ||
182 | return exit_code; | ||
183 | } | ||
184 | |||