diff options
-rw-r--r-- | include/swaynag/config.h | 13 | ||||
-rw-r--r-- | include/swaynag/render.h | 7 | ||||
-rw-r--r-- | include/swaynag/swaynag.h | 100 | ||||
-rw-r--r-- | include/swaynag/types.h | 39 | ||||
-rw-r--r-- | meson.build | 3 | ||||
-rw-r--r-- | sway/desktop/layer_shell.c | 7 | ||||
-rw-r--r-- | swaynag/config.c | 401 | ||||
-rw-r--r-- | swaynag/main.c | 129 | ||||
-rw-r--r-- | swaynag/meson.build | 23 | ||||
-rw-r--r-- | swaynag/render.c | 308 | ||||
-rw-r--r-- | swaynag/swaynag.1.scd | 106 | ||||
-rw-r--r-- | swaynag/swaynag.5.scd | 100 | ||||
-rw-r--r-- | swaynag/swaynag.c | 451 | ||||
-rw-r--r-- | swaynag/types.c | 156 |
14 files changed, 1843 insertions, 0 deletions
diff --git a/include/swaynag/config.h b/include/swaynag/config.h new file mode 100644 index 00000000..0d8889de --- /dev/null +++ b/include/swaynag/config.h | |||
@@ -0,0 +1,13 @@ | |||
1 | #ifndef _SWAYNAG_CONFIG_H | ||
2 | #define _SWAYNAG_CONFIG_H | ||
3 | #include "swaynag/swaynag.h" | ||
4 | #include "list.h" | ||
5 | |||
6 | int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, | ||
7 | list_t *types, struct swaynag_type *type, char **config, bool *debug); | ||
8 | |||
9 | char *swaynag_get_config_path(void); | ||
10 | |||
11 | int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types); | ||
12 | |||
13 | #endif | ||
diff --git a/include/swaynag/render.h b/include/swaynag/render.h new file mode 100644 index 00000000..d09e5929 --- /dev/null +++ b/include/swaynag/render.h | |||
@@ -0,0 +1,7 @@ | |||
1 | #ifndef _SWAYNAG_RENDER_H | ||
2 | #define _SWAYNAG_RENDER_H | ||
3 | #include "swaynag/swaynag.h" | ||
4 | |||
5 | void render_frame(struct swaynag *swaynag); | ||
6 | |||
7 | #endif | ||
diff --git a/include/swaynag/swaynag.h b/include/swaynag/swaynag.h new file mode 100644 index 00000000..1bf8b640 --- /dev/null +++ b/include/swaynag/swaynag.h | |||
@@ -0,0 +1,100 @@ | |||
1 | #ifndef _SWAYNAG_SWAYNAG_H | ||
2 | #define _SWAYNAG_SWAYNAG_H | ||
3 | #include <stdint.h> | ||
4 | #include <strings.h> | ||
5 | #include "list.h" | ||
6 | #include "pool-buffer.h" | ||
7 | #include "swaynag/types.h" | ||
8 | #include "xdg-output-unstable-v1-client-protocol.h" | ||
9 | |||
10 | #define SWAYNAG_MAX_HEIGHT 500 | ||
11 | |||
12 | struct swaynag; | ||
13 | |||
14 | enum swaynag_action_type { | ||
15 | SWAYNAG_ACTION_DISMISS, | ||
16 | SWAYNAG_ACTION_EXPAND, | ||
17 | SWAYNAG_ACTION_COMMAND, | ||
18 | }; | ||
19 | |||
20 | struct swaynag_pointer { | ||
21 | struct wl_pointer *pointer; | ||
22 | uint32_t serial; | ||
23 | struct wl_cursor_theme *cursor_theme; | ||
24 | struct wl_cursor_image *cursor_image; | ||
25 | struct wl_surface *cursor_surface; | ||
26 | int x; | ||
27 | int y; | ||
28 | }; | ||
29 | |||
30 | struct swaynag_output { | ||
31 | char *name; | ||
32 | struct wl_output *wl_output; | ||
33 | uint32_t wl_name; | ||
34 | uint32_t scale; | ||
35 | struct swaynag *swaynag; | ||
36 | struct wl_list link; | ||
37 | }; | ||
38 | |||
39 | struct swaynag_button { | ||
40 | char *text; | ||
41 | enum swaynag_action_type type; | ||
42 | char *action; | ||
43 | int x; | ||
44 | int y; | ||
45 | int width; | ||
46 | int height; | ||
47 | }; | ||
48 | |||
49 | struct swaynag_details { | ||
50 | bool visible; | ||
51 | char *message; | ||
52 | |||
53 | int x; | ||
54 | int y; | ||
55 | int width; | ||
56 | int height; | ||
57 | |||
58 | int offset; | ||
59 | int visible_lines; | ||
60 | int total_lines; | ||
61 | struct swaynag_button button_details; | ||
62 | struct swaynag_button button_up; | ||
63 | struct swaynag_button button_down; | ||
64 | }; | ||
65 | |||
66 | struct swaynag { | ||
67 | bool run_display; | ||
68 | int querying_outputs; | ||
69 | |||
70 | struct wl_display *display; | ||
71 | struct wl_compositor *compositor; | ||
72 | struct wl_seat *seat; | ||
73 | struct wl_shm *shm; | ||
74 | struct swaynag_pointer pointer; | ||
75 | struct zxdg_output_manager_v1 *xdg_output_manager; | ||
76 | struct wl_list outputs; // swaynag_output::link | ||
77 | struct swaynag_output *output; | ||
78 | struct zwlr_layer_shell_v1 *layer_shell; | ||
79 | struct zwlr_layer_surface_v1 *layer_surface; | ||
80 | struct wl_surface *surface; | ||
81 | |||
82 | uint32_t width; | ||
83 | uint32_t height; | ||
84 | int32_t scale; | ||
85 | struct pool_buffer buffers[2]; | ||
86 | struct pool_buffer *current_buffer; | ||
87 | |||
88 | struct swaynag_type *type; | ||
89 | char *message; | ||
90 | list_t *buttons; | ||
91 | struct swaynag_details details; | ||
92 | }; | ||
93 | |||
94 | void swaynag_setup(struct swaynag *swaynag); | ||
95 | |||
96 | void swaynag_run(struct swaynag *swaynag); | ||
97 | |||
98 | void swaynag_destroy(struct swaynag *swaynag); | ||
99 | |||
100 | #endif | ||
diff --git a/include/swaynag/types.h b/include/swaynag/types.h new file mode 100644 index 00000000..2183ce22 --- /dev/null +++ b/include/swaynag/types.h | |||
@@ -0,0 +1,39 @@ | |||
1 | #ifndef _SWAYNAG_TYPES_H | ||
2 | #define _SWAYNAG_TYPES_H | ||
3 | |||
4 | struct swaynag_type { | ||
5 | char *name; | ||
6 | |||
7 | char *font; | ||
8 | char *output; | ||
9 | uint32_t anchors; | ||
10 | |||
11 | uint32_t button_background; | ||
12 | uint32_t background; | ||
13 | uint32_t text; | ||
14 | uint32_t border; | ||
15 | uint32_t border_bottom; | ||
16 | |||
17 | uint32_t bar_border_thickness; | ||
18 | uint32_t message_padding; | ||
19 | uint32_t details_border_thickness; | ||
20 | uint32_t button_border_thickness; | ||
21 | uint32_t button_gap; | ||
22 | uint32_t button_gap_close; | ||
23 | uint32_t button_margin_right; | ||
24 | uint32_t button_padding; | ||
25 | }; | ||
26 | |||
27 | void swaynag_types_add_default(list_t *types); | ||
28 | |||
29 | struct swaynag_type *swaynag_type_get(list_t *types, char *name); | ||
30 | |||
31 | struct swaynag_type *swaynag_type_clone(struct swaynag_type *type); | ||
32 | |||
33 | void swaynag_type_merge(struct swaynag_type *dest, struct swaynag_type *src); | ||
34 | |||
35 | void swaynag_type_free(struct swaynag_type *type); | ||
36 | |||
37 | void swaynag_types_free(list_t *types); | ||
38 | |||
39 | #endif | ||
diff --git a/meson.build b/meson.build index 0d75978f..2a020323 100644 --- a/meson.build +++ b/meson.build | |||
@@ -82,6 +82,8 @@ if scdoc.found() | |||
82 | 'swaylock/swaylock.1.scd', | 82 | 'swaylock/swaylock.1.scd', |
83 | 'swaymsg/swaymsg.1.scd', | 83 | 'swaymsg/swaymsg.1.scd', |
84 | 'swayidle/swayidle.1.scd', | 84 | 'swayidle/swayidle.1.scd', |
85 | 'swaynag/swaynag.1.scd', | ||
86 | 'swaynag/swaynag.5.scd', | ||
85 | ] | 87 | ] |
86 | foreach filename : man_files | 88 | foreach filename : man_files |
87 | topic = filename.split('.')[-3].split('/')[-1] | 89 | topic = filename.split('.')[-3].split('/')[-1] |
@@ -130,6 +132,7 @@ subdir('swaybg') | |||
130 | subdir('swaybar') | 132 | subdir('swaybar') |
131 | subdir('swaylock') | 133 | subdir('swaylock') |
132 | subdir('swayidle') | 134 | subdir('swayidle') |
135 | subdir('swaynag') | ||
133 | 136 | ||
134 | config = configuration_data() | 137 | config = configuration_data() |
135 | config.set('sysconfdir', join_paths(prefix, sysconfdir)) | 138 | config.set('sysconfdir', join_paths(prefix, sysconfdir)) |
diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index a7d96717..a2935883 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c | |||
@@ -7,11 +7,13 @@ | |||
7 | #include <wlr/types/wlr_output_damage.h> | 7 | #include <wlr/types/wlr_output_damage.h> |
8 | #include <wlr/types/wlr_output.h> | 8 | #include <wlr/types/wlr_output.h> |
9 | #include <wlr/util/log.h> | 9 | #include <wlr/util/log.h> |
10 | #include "sway/desktop/transaction.h" | ||
10 | #include "sway/input/input-manager.h" | 11 | #include "sway/input/input-manager.h" |
11 | #include "sway/input/seat.h" | 12 | #include "sway/input/seat.h" |
12 | #include "sway/layers.h" | 13 | #include "sway/layers.h" |
13 | #include "sway/output.h" | 14 | #include "sway/output.h" |
14 | #include "sway/server.h" | 15 | #include "sway/server.h" |
16 | #include "sway/tree/arrange.h" | ||
15 | #include "sway/tree/layout.h" | 17 | #include "sway/tree/layout.h" |
16 | #include "log.h" | 18 | #include "log.h" |
17 | 19 | ||
@@ -245,6 +247,9 @@ static void handle_surface_commit(struct wl_listener *listener, void *data) { | |||
245 | output_damage_surface(output, layer->geo.x, layer->geo.y, | 247 | output_damage_surface(output, layer->geo.x, layer->geo.y, |
246 | layer_surface->surface, false); | 248 | layer_surface->surface, false); |
247 | } | 249 | } |
250 | |||
251 | arrange_windows(output->swayc); | ||
252 | transaction_commit_dirty(); | ||
248 | } | 253 | } |
249 | 254 | ||
250 | static void unmap(struct sway_layer_surface *sway_layer) { | 255 | static void unmap(struct sway_layer_surface *sway_layer) { |
@@ -282,6 +287,8 @@ static void handle_destroy(struct wl_listener *listener, void *data) { | |||
282 | struct sway_output *output = sway_layer->layer_surface->output->data; | 287 | struct sway_output *output = sway_layer->layer_surface->output->data; |
283 | if (output != NULL && output->swayc != NULL) { | 288 | if (output != NULL && output->swayc != NULL) { |
284 | arrange_layers(output); | 289 | arrange_layers(output); |
290 | arrange_windows(output->swayc); | ||
291 | transaction_commit_dirty(); | ||
285 | } | 292 | } |
286 | wl_list_remove(&sway_layer->output_destroy.link); | 293 | wl_list_remove(&sway_layer->output_destroy.link); |
287 | sway_layer->layer_surface->output = NULL; | 294 | sway_layer->layer_surface->output = NULL; |
diff --git a/swaynag/config.c b/swaynag/config.c new file mode 100644 index 00000000..d6c5739d --- /dev/null +++ b/swaynag/config.c | |||
@@ -0,0 +1,401 @@ | |||
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/swaynag.h" | ||
10 | #include "swaynag/types.h" | ||
11 | #include "util.h" | ||
12 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||
13 | |||
14 | static char *read_from_stdin() { | ||
15 | char *buffer = NULL; | ||
16 | while (!feof(stdin)) { | ||
17 | char *line = read_line(stdin); | ||
18 | if (!line) { | ||
19 | continue; | ||
20 | } | ||
21 | |||
22 | size_t curlen = buffer ? strlen(buffer) : 0; | ||
23 | buffer = realloc(buffer, curlen + strlen(line) + 2); | ||
24 | snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line); | ||
25 | |||
26 | free(line); | ||
27 | } | ||
28 | |||
29 | while (buffer && buffer[strlen(buffer) - 1] == '\n') { | ||
30 | buffer[strlen(buffer) - 1] = '\0'; | ||
31 | } | ||
32 | |||
33 | return buffer; | ||
34 | } | ||
35 | |||
36 | int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, | ||
37 | list_t *types, struct swaynag_type *type, char **config, bool *debug) { | ||
38 | enum type_options { | ||
39 | TO_COLOR_BACKGROUND = 256, | ||
40 | TO_COLOR_BORDER, | ||
41 | TO_COLOR_BORDER_BOTTOM, | ||
42 | TO_COLOR_BUTTON, | ||
43 | TO_COLOR_TEXT, | ||
44 | TO_THICK_BAR_BORDER, | ||
45 | TO_PADDING_MESSAGE, | ||
46 | TO_THICK_DET_BORDER, | ||
47 | TO_THICK_BTN_BORDER, | ||
48 | TO_GAP_BTN, | ||
49 | TO_GAP_BTN_DISMISS, | ||
50 | TO_MARGIN_BTN_RIGHT, | ||
51 | TO_PADDING_BTN, | ||
52 | }; | ||
53 | |||
54 | static struct option opts[] = { | ||
55 | {"button", required_argument, NULL, 'b'}, | ||
56 | {"config", required_argument, NULL, 'c'}, | ||
57 | {"debug", no_argument, NULL, 'd'}, | ||
58 | {"edge", required_argument, NULL, 'e'}, | ||
59 | {"font", required_argument, NULL, 'f'}, | ||
60 | {"help", no_argument, NULL, 'h'}, | ||
61 | {"detailed-message", no_argument, NULL, 'l'}, | ||
62 | {"detailed-button", required_argument, NULL, 'L'}, | ||
63 | {"message", required_argument, NULL, 'm'}, | ||
64 | {"output", required_argument, NULL, 'o'}, | ||
65 | {"dismiss-button", required_argument, NULL, 's'}, | ||
66 | {"type", required_argument, NULL, 't'}, | ||
67 | {"version", no_argument, NULL, 'v'}, | ||
68 | |||
69 | {"background", required_argument, NULL, TO_COLOR_BACKGROUND}, | ||
70 | {"border", required_argument, NULL, TO_COLOR_BORDER}, | ||
71 | {"border-bottom", required_argument, NULL, TO_COLOR_BORDER_BOTTOM}, | ||
72 | {"button-background", required_argument, NULL, TO_COLOR_BUTTON}, | ||
73 | {"text", required_argument, NULL, TO_COLOR_TEXT}, | ||
74 | {"border-bottom-size", required_argument, NULL, TO_THICK_BAR_BORDER}, | ||
75 | {"message-padding", required_argument, NULL, TO_PADDING_MESSAGE}, | ||
76 | {"details-border-size", required_argument, NULL, TO_THICK_DET_BORDER}, | ||
77 | {"button-border-size", required_argument, NULL, TO_THICK_BTN_BORDER}, | ||
78 | {"button-gap", required_argument, NULL, TO_GAP_BTN}, | ||
79 | {"button-dismiss-gap", required_argument, NULL, TO_GAP_BTN_DISMISS}, | ||
80 | {"button-margin-right", required_argument, NULL, TO_MARGIN_BTN_RIGHT}, | ||
81 | {"button-padding", required_argument, NULL, TO_PADDING_BTN}, | ||
82 | |||
83 | {0, 0, 0, 0} | ||
84 | }; | ||
85 | |||
86 | const char *usage = | ||
87 | "Usage: swaynag [options...]\n" | ||
88 | "\n" | ||
89 | " -b, --button <text> <action> Create a button with text that " | ||
90 | "executes action when pressed. Multiple buttons can be defined.\n" | ||
91 | " -c, --config <path> Path to config file.\n" | ||
92 | " -d, --debug Enable debugging.\n" | ||
93 | " -e, --edge top|bottom Set the edge to use.\n" | ||
94 | " -f, --font <font> Set the font to use.\n" | ||
95 | " -h, --help Show help message and quit.\n" | ||
96 | " -l, --detailed-message Read a detailed message from stdin.\n" | ||
97 | " -L, --detailed-button <text> Set the text of the detail button.\n" | ||
98 | " -m, --message <msg> Set the message text.\n" | ||
99 | " -o, --output <output> Set the output to use.\n" | ||
100 | " -s, --dismiss-button <text> Set the dismiss button text.\n" | ||
101 | " -t, --type <type> Set the message type.\n" | ||
102 | " -v, --version Show the version number and quit.\n" | ||
103 | "\n" | ||
104 | "The following appearance options can also be given:\n" | ||
105 | " --background RRGGBB[AA] Background color.\n" | ||
106 | " --border RRGGBB[AA] Border color.\n" | ||
107 | " --border-bottom RRGGBB[AA] Bottom border color.\n" | ||
108 | " --button-background RRGGBB[AA] Button background color.\n" | ||
109 | " --text RRGGBB[AA] Text color.\n" | ||
110 | " --border-bottom-size size Thickness of the bar border.\n" | ||
111 | " --message-padding padding Padding for the message.\n" | ||
112 | " --details-border-size size Thickness for the details border.\n" | ||
113 | " --button-border-size size Thickness for the button border.\n" | ||
114 | " --button-gap gap Size of the gap between buttons\n" | ||
115 | " --button-dismiss-gap gap Size of the gap for dismiss button.\n" | ||
116 | " --button-margin-right margin Margin from dismiss button to edge.\n" | ||
117 | " --button-padding padding Padding for the button text.\n"; | ||
118 | |||
119 | optind = 1; | ||
120 | while (1) { | ||
121 | int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); | ||
122 | if (c == -1) { | ||
123 | break; | ||
124 | } | ||
125 | switch (c) { | ||
126 | case 'b': // Button | ||
127 | if (swaynag) { | ||
128 | if (optind >= argc) { | ||
129 | fprintf(stderr, "Missing action for button %s\n", optarg); | ||
130 | return EXIT_FAILURE; | ||
131 | } | ||
132 | struct swaynag_button *button; | ||
133 | button = calloc(sizeof(struct swaynag_button), 1); | ||
134 | button->text = strdup(optarg); | ||
135 | button->type = SWAYNAG_ACTION_COMMAND; | ||
136 | button->action = strdup(argv[optind]); | ||
137 | list_add(swaynag->buttons, button); | ||
138 | } | ||
139 | optind++; | ||
140 | break; | ||
141 | case 'c': // Config | ||
142 | if (config) { | ||
143 | *config = strdup(optarg); | ||
144 | } | ||
145 | break; | ||
146 | case 'd': // Debug | ||
147 | if (debug) { | ||
148 | *debug = true; | ||
149 | } | ||
150 | break; | ||
151 | case 'e': // Edge | ||
152 | if (type) { | ||
153 | if (strcmp(optarg, "top") == 0) { | ||
154 | type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ||
155 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ||
156 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; | ||
157 | } else if (strcmp(optarg, "bottom") == 0) { | ||
158 | type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ||
159 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ||
160 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; | ||
161 | } else { | ||
162 | fprintf(stderr, "Invalid edge: %s\n", optarg); | ||
163 | return EXIT_FAILURE; | ||
164 | } | ||
165 | } | ||
166 | break; | ||
167 | case 'f': // Font | ||
168 | if (type) { | ||
169 | free(type->font); | ||
170 | type->font = strdup(optarg); | ||
171 | } | ||
172 | break; | ||
173 | case 'l': // Detailed Message | ||
174 | if (swaynag) { | ||
175 | free(swaynag->details.message); | ||
176 | swaynag->details.message = read_from_stdin(); | ||
177 | swaynag->details.button_up.text = strdup("â–²"); | ||
178 | swaynag->details.button_down.text = strdup("â–¼"); | ||
179 | } | ||
180 | break; | ||
181 | case 'L': // Detailed Button Text | ||
182 | if (swaynag) { | ||
183 | free(swaynag->details.button_details.text); | ||
184 | swaynag->details.button_details.text = strdup(optarg); | ||
185 | } | ||
186 | break; | ||
187 | case 'm': // Message | ||
188 | if (swaynag) { | ||
189 | free(swaynag->message); | ||
190 | swaynag->message = strdup(optarg); | ||
191 | } | ||
192 | break; | ||
193 | case 'o': // Output | ||
194 | if (type) { | ||
195 | free(type->output); | ||
196 | type->output = strdup(optarg); | ||
197 | } | ||
198 | break; | ||
199 | case 's': // Dismiss Button Text | ||
200 | if (swaynag) { | ||
201 | struct swaynag_button *button_close; | ||
202 | button_close = swaynag->buttons->items[0]; | ||
203 | free(button_close->text); | ||
204 | button_close->text = strdup(optarg); | ||
205 | } | ||
206 | break; | ||
207 | case 't': // Type | ||
208 | if (swaynag) { | ||
209 | swaynag->type = swaynag_type_get(types, optarg); | ||
210 | if (!swaynag->type) { | ||
211 | fprintf(stderr, "Unknown type %s\n", optarg); | ||
212 | return EXIT_FAILURE; | ||
213 | } | ||
214 | } | ||
215 | break; | ||
216 | case 'v': // Version | ||
217 | fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); | ||
218 | return -1; | ||
219 | case TO_COLOR_BACKGROUND: // Background color | ||
220 | if (type) { | ||
221 | type->background = parse_color(optarg); | ||
222 | } | ||
223 | break; | ||
224 | case TO_COLOR_BORDER: // Border color | ||
225 | if (type) { | ||
226 | type->border = parse_color(optarg); | ||
227 | } | ||
228 | break; | ||
229 | case TO_COLOR_BORDER_BOTTOM: // Bottom border color | ||
230 | if (type) { | ||
231 | type->border_bottom = parse_color(optarg); | ||
232 | } | ||
233 | break; | ||
234 | case TO_COLOR_BUTTON: // Button background color | ||
235 | if (type) { | ||
236 | type->button_background = parse_color(optarg); | ||
237 | } | ||
238 | break; | ||
239 | case TO_COLOR_TEXT: // Text color | ||
240 | if (type) { | ||
241 | type->text = parse_color(optarg); | ||
242 | } | ||
243 | break; | ||
244 | case TO_THICK_BAR_BORDER: // Bottom border thickness | ||
245 | if (type) { | ||
246 | type->bar_border_thickness = strtol(optarg, NULL, 0); | ||
247 | } | ||
248 | break; | ||
249 | case TO_PADDING_MESSAGE: // Message padding | ||
250 | if (type) { | ||
251 | type->message_padding = strtol(optarg, NULL, 0); | ||
252 | } | ||
253 | break; | ||
254 | case TO_THICK_DET_BORDER: // Details border thickness | ||
255 | if (type) { | ||
256 | type->details_border_thickness = strtol(optarg, NULL, 0); | ||
257 | } | ||
258 | break; | ||
259 | case TO_THICK_BTN_BORDER: // Button border thickness | ||
260 | if (type) { | ||
261 | type->button_border_thickness = strtol(optarg, NULL, 0); | ||
262 | } | ||
263 | break; | ||
264 | case TO_GAP_BTN: // Gap between buttons | ||
265 | if (type) { | ||
266 | type->button_gap = strtol(optarg, NULL, 0); | ||
267 | } | ||
268 | break; | ||
269 | case TO_GAP_BTN_DISMISS: // Gap between dismiss button | ||
270 | if (type) { | ||
271 | type->button_gap_close = strtol(optarg, NULL, 0); | ||
272 | } | ||
273 | break; | ||
274 | case TO_MARGIN_BTN_RIGHT: // Margin on the right side of button area | ||
275 | if (type) { | ||
276 | type->button_margin_right = strtol(optarg, NULL, 0); | ||
277 | } | ||
278 | break; | ||
279 | case TO_PADDING_BTN: // Padding for the button text | ||
280 | if (type) { | ||
281 | type->button_padding = strtol(optarg, NULL, 0); | ||
282 | } | ||
283 | break; | ||
284 | default: // Help or unknown flag | ||
285 | fprintf(c == 'h' ? stdout : stderr, "%s", usage); | ||
286 | return -1; | ||
287 | } | ||
288 | } | ||
289 | |||
290 | return 0; | ||
291 | } | ||
292 | |||
293 | static bool file_exists(const char *path) { | ||
294 | return path && access(path, R_OK) != -1; | ||
295 | } | ||
296 | |||
297 | char *swaynag_get_config_path(void) { | ||
298 | static const char *config_paths[] = { | ||
299 | "$HOME/.swaynag/config", | ||
300 | "$XDG_CONFIG_HOME/swaynag/config", | ||
301 | SYSCONFDIR "/swaynag/config", | ||
302 | }; | ||
303 | |||
304 | if (!getenv("XDG_CONFIG_HOME")) { | ||
305 | char *home = getenv("HOME"); | ||
306 | char *config_home = malloc(strlen(home) + strlen("/.config") + 1); | ||
307 | if (!config_home) { | ||
308 | wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config"); | ||
309 | } else { | ||
310 | strcpy(config_home, home); | ||
311 | strcat(config_home, "/.config"); | ||
312 | setenv("XDG_CONFIG_HOME", config_home, 1); | ||
313 | wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); | ||
314 | free(config_home); | ||
315 | } | ||
316 | } | ||
317 | |||
318 | wordexp_t p; | ||
319 | char *path; | ||
320 | for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { | ||
321 | if (wordexp(config_paths[i], &p, 0) == 0) { | ||
322 | path = strdup(p.we_wordv[0]); | ||
323 | wordfree(&p); | ||
324 | if (file_exists(path)) { | ||
325 | return path; | ||
326 | } | ||
327 | free(path); | ||
328 | } | ||
329 | } | ||
330 | |||
331 | return NULL; | ||
332 | } | ||
333 | |||
334 | int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { | ||
335 | FILE *config = fopen(path, "r"); | ||
336 | if (!config) { | ||
337 | fprintf(stderr, "Failed to read config. Running without it.\n"); | ||
338 | return 0; | ||
339 | } | ||
340 | |||
341 | struct swaynag_type *type; | ||
342 | type = calloc(1, sizeof(struct swaynag_type)); | ||
343 | type->name = strdup("<config>"); | ||
344 | list_add(types, type); | ||
345 | |||
346 | char *line; | ||
347 | int line_number = 0; | ||
348 | while (!feof(config)) { | ||
349 | line = read_line(config); | ||
350 | if (!line) { | ||
351 | continue; | ||
352 | } | ||
353 | |||
354 | line_number++; | ||
355 | if (line[0] == '#') { | ||
356 | free(line); | ||
357 | continue; | ||
358 | } | ||
359 | if (strlen(line) == 0) { | ||
360 | free(line); | ||
361 | continue; | ||
362 | } | ||
363 | |||
364 | if (line[0] == '[') { | ||
365 | char *close = strchr(line, ']'); | ||
366 | if (!close) { | ||
367 | free(line); | ||
368 | fclose(config); | ||
369 | fprintf(stderr, "Closing bracket not found on line %d\n", | ||
370 | line_number); | ||
371 | return 1; | ||
372 | } | ||
373 | char *name = calloc(1, close - line); | ||
374 | strncat(name, line + 1, close - line - 1); | ||
375 | type = swaynag_type_get(types, name); | ||
376 | if (!type) { | ||
377 | type = calloc(1, sizeof(struct swaynag_type)); | ||
378 | type->name = strdup(name); | ||
379 | list_add(types, type); | ||
380 | } | ||
381 | free(name); | ||
382 | } else { | ||
383 | char flag[strlen(line) + 3]; | ||
384 | sprintf(flag, "--%s", line); | ||
385 | char *argv[] = {"swaynag", flag}; | ||
386 | int result; | ||
387 | result = swaynag_parse_options(2, argv, swaynag, types, type, | ||
388 | NULL, NULL); | ||
389 | if (result != 0) { | ||
390 | free(line); | ||
391 | fclose(config); | ||
392 | return result; | ||
393 | } | ||
394 | } | ||
395 | |||
396 | free(line); | ||
397 | } | ||
398 | fclose(config); | ||
399 | return 0; | ||
400 | } | ||
401 | |||
diff --git a/swaynag/main.c b/swaynag/main.c new file mode 100644 index 00000000..854368e5 --- /dev/null +++ b/swaynag/main.c | |||
@@ -0,0 +1,129 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <signal.h> | ||
3 | #include "log.h" | ||
4 | #include "list.h" | ||
5 | #include "swaynag/config.h" | ||
6 | #include "swaynag/swaynag.h" | ||
7 | #include "swaynag/types.h" | ||
8 | |||
9 | static struct swaynag swaynag; | ||
10 | |||
11 | void sig_handler(int signal) { | ||
12 | swaynag_destroy(&swaynag); | ||
13 | exit(EXIT_FAILURE); | ||
14 | } | ||
15 | |||
16 | void sway_terminate(int code) { | ||
17 | swaynag_destroy(&swaynag); | ||
18 | exit(code); | ||
19 | } | ||
20 | |||
21 | int main(int argc, char **argv) { | ||
22 | int exit_code = EXIT_SUCCESS; | ||
23 | |||
24 | list_t *types = create_list(); | ||
25 | swaynag_types_add_default(types); | ||
26 | |||
27 | memset(&swaynag, 0, sizeof(swaynag)); | ||
28 | swaynag.buttons = create_list(); | ||
29 | |||
30 | struct swaynag_button *button_close = | ||
31 | calloc(sizeof(struct swaynag_button), 1); | ||
32 | button_close->text = strdup("X"); | ||
33 | button_close->type = SWAYNAG_ACTION_DISMISS; | ||
34 | list_add(swaynag.buttons, button_close); | ||
35 | |||
36 | swaynag.details.button_details.text = strdup("Toggle Details"); | ||
37 | swaynag.details.button_details.type = SWAYNAG_ACTION_EXPAND; | ||
38 | |||
39 | |||
40 | char *config_path = NULL; | ||
41 | bool debug = false; | ||
42 | int launch_status = swaynag_parse_options(argc, argv, NULL, NULL, NULL, | ||
43 | &config_path, &debug); | ||
44 | if (launch_status != 0) { | ||
45 | exit_code = launch_status; | ||
46 | goto cleanup; | ||
47 | } | ||
48 | wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); | ||
49 | |||
50 | if (!config_path) { | ||
51 | config_path = swaynag_get_config_path(); | ||
52 | } | ||
53 | if (config_path) { | ||
54 | wlr_log(WLR_DEBUG, "Loading config file: %s", config_path); | ||
55 | int config_status = swaynag_load_config(config_path, &swaynag, types); | ||
56 | free(config_path); | ||
57 | if (config_status != 0) { | ||
58 | exit_code = config_status; | ||
59 | goto cleanup; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | if (argc > 1) { | ||
64 | struct swaynag_type *type_args; | ||
65 | type_args = calloc(1, sizeof(struct swaynag_type)); | ||
66 | type_args->name = strdup("<args>"); | ||
67 | list_add(types, type_args); | ||
68 | |||
69 | int result = swaynag_parse_options(argc, argv, &swaynag, types, | ||
70 | type_args, NULL, NULL); | ||
71 | if (result != 0) { | ||
72 | exit_code = result; | ||
73 | goto cleanup; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | if (!swaynag.message) { | ||
78 | wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m"); | ||
79 | exit_code = EXIT_FAILURE; | ||
80 | goto cleanup; | ||
81 | } | ||
82 | |||
83 | if (!swaynag.type) { | ||
84 | swaynag.type = swaynag_type_get(types, "error"); | ||
85 | } | ||
86 | |||
87 | // Construct a new type using the config defaults as base, then merging | ||
88 | // config type defaults on top, then merging arguments on top of that, and | ||
89 | // finally merging defaults on top. | ||
90 | struct swaynag_type *type = calloc(1, sizeof(struct swaynag_type)); | ||
91 | type->name = strdup(swaynag.type->name); | ||
92 | swaynag_type_merge(type, swaynag_type_get(types, "<args>")); | ||
93 | swaynag_type_merge(type, swaynag.type); | ||
94 | swaynag_type_merge(type, swaynag_type_get(types, "<config>")); | ||
95 | swaynag_type_merge(type, swaynag_type_get(types, "<defaults>")); | ||
96 | swaynag.type = type; | ||
97 | |||
98 | swaynag_types_free(types); | ||
99 | |||
100 | if (swaynag.details.message) { | ||
101 | list_add(swaynag.buttons, &swaynag.details.button_details); | ||
102 | } else { | ||
103 | free(swaynag.details.button_details.text); | ||
104 | } | ||
105 | |||
106 | wlr_log(WLR_DEBUG, "Output: %s", swaynag.type->output); | ||
107 | wlr_log(WLR_DEBUG, "Anchors: %d", swaynag.type->anchors); | ||
108 | wlr_log(WLR_DEBUG, "Type: %s", swaynag.type->name); | ||
109 | wlr_log(WLR_DEBUG, "Message: %s", swaynag.message); | ||
110 | wlr_log(WLR_DEBUG, "Font: %s", swaynag.type->font); | ||
111 | wlr_log(WLR_DEBUG, "Buttons"); | ||
112 | for (int i = 0; i < swaynag.buttons->length; i++) { | ||
113 | struct swaynag_button *button = swaynag.buttons->items[i]; | ||
114 | wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action); | ||
115 | } | ||
116 | |||
117 | signal(SIGTERM, sig_handler); | ||
118 | |||
119 | swaynag_setup(&swaynag); | ||
120 | swaynag_run(&swaynag); | ||
121 | return exit_code; | ||
122 | |||
123 | cleanup: | ||
124 | swaynag_types_free(types); | ||
125 | free(swaynag.details.button_details.text); | ||
126 | swaynag_destroy(&swaynag); | ||
127 | return exit_code; | ||
128 | } | ||
129 | |||
diff --git a/swaynag/meson.build b/swaynag/meson.build new file mode 100644 index 00000000..2ba3ed95 --- /dev/null +++ b/swaynag/meson.build | |||
@@ -0,0 +1,23 @@ | |||
1 | executable( | ||
2 | 'swaynag', [ | ||
3 | 'config.c', | ||
4 | 'main.c', | ||
5 | 'render.c', | ||
6 | 'swaynag.c', | ||
7 | 'types.c', | ||
8 | ], | ||
9 | include_directories: [sway_inc], | ||
10 | dependencies: [ | ||
11 | cairo, | ||
12 | client_protos, | ||
13 | gdk_pixbuf, | ||
14 | math, | ||
15 | pango, | ||
16 | pangocairo, | ||
17 | wayland_client, | ||
18 | wayland_cursor, | ||
19 | wlroots, | ||
20 | ], | ||
21 | link_with: [lib_sway_common, lib_sway_client], | ||
22 | install: true | ||
23 | ) | ||
diff --git a/swaynag/render.c b/swaynag/render.c new file mode 100644 index 00000000..766409e4 --- /dev/null +++ b/swaynag/render.c | |||
@@ -0,0 +1,308 @@ | |||
1 | #include <stdint.h> | ||
2 | #include "cairo.h" | ||
3 | #include "log.h" | ||
4 | #include "pango.h" | ||
5 | #include "pool-buffer.h" | ||
6 | #include "swaynag/swaynag.h" | ||
7 | #include "swaynag/types.h" | ||
8 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||
9 | |||
10 | static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { | ||
11 | uint32_t height = swaynag->height * swaynag->scale; | ||
12 | height -= swaynag->type->bar_border_thickness * swaynag->scale; | ||
13 | |||
14 | int text_width, text_height; | ||
15 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, | ||
16 | swaynag->scale, true, "%s", swaynag->message); | ||
17 | |||
18 | int padding = swaynag->type->message_padding * swaynag->scale; | ||
19 | |||
20 | uint32_t ideal_height = text_height + padding * 2; | ||
21 | uint32_t ideal_surface_height = ideal_height / swaynag->scale; | ||
22 | if (swaynag->height < ideal_surface_height) { | ||
23 | return ideal_surface_height; | ||
24 | } | ||
25 | |||
26 | cairo_set_source_u32(cairo, swaynag->type->text); | ||
27 | cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); | ||
28 | pango_printf(cairo, swaynag->type->font, swaynag->scale, false, | ||
29 | "%s", swaynag->message); | ||
30 | |||
31 | return ideal_surface_height; | ||
32 | } | ||
33 | |||
34 | static void render_details_scroll_button(cairo_t *cairo, | ||
35 | struct swaynag *swaynag, struct swaynag_button *button) { | ||
36 | int text_width, text_height; | ||
37 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, | ||
38 | swaynag->scale, true, "%s", button->text); | ||
39 | |||
40 | int border = swaynag->type->button_border_thickness * swaynag->scale; | ||
41 | int padding = swaynag->type->button_padding * swaynag->scale; | ||
42 | |||
43 | cairo_set_source_u32(cairo, swaynag->type->border); | ||
44 | cairo_rectangle(cairo, button->x, button->y, | ||
45 | button->width, button->height); | ||
46 | cairo_fill(cairo); | ||
47 | |||
48 | cairo_set_source_u32(cairo, swaynag->type->button_background); | ||
49 | cairo_rectangle(cairo, button->x + border, button->y + border, | ||
50 | button->width - (border * 2), button->height - (border * 2)); | ||
51 | cairo_fill(cairo); | ||
52 | |||
53 | cairo_set_source_u32(cairo, swaynag->type->text); | ||
54 | cairo_move_to(cairo, button->x + border + padding, | ||
55 | button->y + border + (button->height - text_height) / 2); | ||
56 | pango_printf(cairo, swaynag->type->font, swaynag->scale, true, | ||
57 | "%s", button->text); | ||
58 | } | ||
59 | |||
60 | static int get_detailed_scroll_button_width(cairo_t *cairo, | ||
61 | struct swaynag *swaynag) { | ||
62 | int up_width, down_width, temp_height; | ||
63 | get_text_size(cairo, swaynag->type->font, &up_width, &temp_height, | ||
64 | swaynag->scale, true, | ||
65 | "%s", swaynag->details.button_up.text); | ||
66 | get_text_size(cairo, swaynag->type->font, &down_width, &temp_height, | ||
67 | swaynag->scale, true, | ||
68 | "%s", swaynag->details.button_down.text); | ||
69 | |||
70 | int text_width = up_width > down_width ? up_width : down_width; | ||
71 | int border = swaynag->type->button_border_thickness * swaynag->scale; | ||
72 | int padding = swaynag->type->button_padding * swaynag->scale; | ||
73 | |||
74 | return text_width + border * 2 + padding * 2; | ||
75 | } | ||
76 | |||
77 | static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, | ||
78 | uint32_t y) { | ||
79 | uint32_t width = swaynag->width * swaynag->scale; | ||
80 | uint32_t height = swaynag->height * swaynag->scale; | ||
81 | height -= swaynag->type->bar_border_thickness * swaynag->scale; | ||
82 | |||
83 | int border = swaynag->type->details_border_thickness * swaynag->scale; | ||
84 | int padding = swaynag->type->message_padding * swaynag->scale; | ||
85 | int decor = padding + border; | ||
86 | |||
87 | swaynag->details.x = decor; | ||
88 | swaynag->details.y = y * swaynag->scale + decor; | ||
89 | swaynag->details.width = width - decor * 2; | ||
90 | |||
91 | PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font, | ||
92 | swaynag->details.message, swaynag->scale, false); | ||
93 | pango_layout_set_width(layout, | ||
94 | (swaynag->details.width - padding * 2) * PANGO_SCALE); | ||
95 | pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); | ||
96 | pango_layout_set_single_paragraph_mode(layout, false); | ||
97 | pango_cairo_update_layout(cairo, layout); | ||
98 | swaynag->details.total_lines = pango_layout_get_line_count(layout); | ||
99 | |||
100 | PangoLayoutLine *line; | ||
101 | line = pango_layout_get_line_readonly(layout, swaynag->details.offset); | ||
102 | gint offset = line->start_index; | ||
103 | const char *text = pango_layout_get_text(layout); | ||
104 | pango_layout_set_text(layout, text + offset, strlen(text) - offset); | ||
105 | |||
106 | int text_width, text_height; | ||
107 | pango_cairo_update_layout(cairo, layout); | ||
108 | pango_layout_get_pixel_size(layout, &text_width, &text_height); | ||
109 | |||
110 | bool show_buttons = swaynag->details.offset > 0; | ||
111 | int button_width = get_detailed_scroll_button_width(cairo, swaynag); | ||
112 | if (show_buttons) { | ||
113 | swaynag->details.width -= button_width; | ||
114 | pango_layout_set_width(layout, | ||
115 | (swaynag->details.width - padding * 2) * PANGO_SCALE); | ||
116 | } | ||
117 | |||
118 | uint32_t ideal_height; | ||
119 | do { | ||
120 | ideal_height = swaynag->details.y + text_height + decor + padding * 2; | ||
121 | if (ideal_height > SWAYNAG_MAX_HEIGHT) { | ||
122 | ideal_height = SWAYNAG_MAX_HEIGHT; | ||
123 | |||
124 | if (!show_buttons) { | ||
125 | show_buttons = true; | ||
126 | swaynag->details.width -= button_width; | ||
127 | pango_layout_set_width(layout, | ||
128 | (swaynag->details.width - padding * 2) * PANGO_SCALE); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | swaynag->details.height = ideal_height - swaynag->details.y - decor; | ||
133 | pango_layout_set_height(layout, | ||
134 | (swaynag->details.height - padding * 2) * PANGO_SCALE); | ||
135 | pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | ||
136 | pango_cairo_update_layout(cairo, layout); | ||
137 | pango_layout_get_pixel_size(layout, &text_width, &text_height); | ||
138 | } while (text_height != (swaynag->details.height - padding * 2)); | ||
139 | |||
140 | swaynag->details.visible_lines = pango_layout_get_line_count(layout); | ||
141 | |||
142 | if (show_buttons) { | ||
143 | swaynag->details.button_up.x = | ||
144 | swaynag->details.x + swaynag->details.width; | ||
145 | swaynag->details.button_up.y = swaynag->details.y; | ||
146 | swaynag->details.button_up.width = button_width; | ||
147 | swaynag->details.button_up.height = swaynag->details.height / 2; | ||
148 | render_details_scroll_button(cairo, swaynag, | ||
149 | &swaynag->details.button_up); | ||
150 | |||
151 | swaynag->details.button_down.x = | ||
152 | swaynag->details.x + swaynag->details.width; | ||
153 | swaynag->details.button_down.y = | ||
154 | swaynag->details.button_up.y + swaynag->details.button_up.height; | ||
155 | swaynag->details.button_down.width = button_width; | ||
156 | swaynag->details.button_down.height = swaynag->details.height / 2; | ||
157 | render_details_scroll_button(cairo, swaynag, | ||
158 | &swaynag->details.button_down); | ||
159 | } | ||
160 | |||
161 | cairo_set_source_u32(cairo, swaynag->type->border); | ||
162 | cairo_rectangle(cairo, swaynag->details.x, swaynag->details.y, | ||
163 | swaynag->details.width, swaynag->details.height); | ||
164 | cairo_fill(cairo); | ||
165 | |||
166 | cairo_move_to(cairo, swaynag->details.x + padding, | ||
167 | swaynag->details.y + padding); | ||
168 | cairo_set_source_u32(cairo, swaynag->type->text); | ||
169 | pango_cairo_show_layout(cairo, layout); | ||
170 | g_object_unref(layout); | ||
171 | |||
172 | return ideal_height / swaynag->scale; | ||
173 | } | ||
174 | |||
175 | static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, | ||
176 | int button_index, int *x) { | ||
177 | uint32_t height = swaynag->height * swaynag->scale; | ||
178 | height -= swaynag->type->bar_border_thickness * swaynag->scale; | ||
179 | struct swaynag_button *button = swaynag->buttons->items[button_index]; | ||
180 | |||
181 | int text_width, text_height; | ||
182 | get_text_size(cairo, swaynag->type->font, &text_width, &text_height, | ||
183 | swaynag->scale, true, "%s", button->text); | ||
184 | |||
185 | int border = swaynag->type->button_border_thickness * swaynag->scale; | ||
186 | int padding = swaynag->type->button_padding * swaynag->scale; | ||
187 | |||
188 | uint32_t ideal_height = text_height + padding * 2 + border * 2; | ||
189 | uint32_t ideal_surface_height = ideal_height / swaynag->scale; | ||
190 | if (swaynag->height < ideal_surface_height) { | ||
191 | return ideal_surface_height; | ||
192 | } | ||
193 | |||
194 | button->x = *x - border - text_width - padding * 2; | ||
195 | button->y = (int)(ideal_height - text_height) / 2 - padding; | ||
196 | button->width = text_width + padding * 2; | ||
197 | button->height = text_height + padding * 2; | ||
198 | |||
199 | cairo_set_source_u32(cairo, swaynag->type->border); | ||
200 | cairo_rectangle(cairo, button->x - border, button->y - border, | ||
201 | button->width + border * 2, button->height + border * 2); | ||
202 | cairo_fill(cairo); | ||
203 | |||
204 | cairo_set_source_u32(cairo, swaynag->type->button_background); | ||
205 | cairo_rectangle(cairo, button->x, button->y, | ||
206 | button->width, button->height); | ||
207 | cairo_fill(cairo); | ||
208 | |||
209 | cairo_set_source_u32(cairo, swaynag->type->text); | ||
210 | cairo_move_to(cairo, button->x + padding, button->y + padding); | ||
211 | pango_printf(cairo, swaynag->type->font, swaynag->scale, true, | ||
212 | "%s", button->text); | ||
213 | |||
214 | *x = button->x - border; | ||
215 | |||
216 | return ideal_surface_height; | ||
217 | } | ||
218 | |||
219 | static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { | ||
220 | uint32_t max_height = 0; | ||
221 | |||
222 | cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); | ||
223 | cairo_set_source_u32(cairo, swaynag->type->background); | ||
224 | cairo_paint(cairo); | ||
225 | |||
226 | uint32_t h = render_message(cairo, swaynag); | ||
227 | max_height = h > max_height ? h : max_height; | ||
228 | |||
229 | int x = swaynag->width - swaynag->type->button_margin_right; | ||
230 | x *= swaynag->scale; | ||
231 | for (int i = 0; i < swaynag->buttons->length; i++) { | ||
232 | h = render_button(cairo, swaynag, i, &x); | ||
233 | max_height = h > max_height ? h : max_height; | ||
234 | x -= swaynag->type->button_gap * swaynag->scale; | ||
235 | if (i == 0) { | ||
236 | x -= swaynag->type->button_gap_close * swaynag->scale; | ||
237 | } | ||
238 | } | ||
239 | |||
240 | if (swaynag->details.visible) { | ||
241 | h = render_detailed(cairo, swaynag, max_height); | ||
242 | max_height = h > max_height ? h : max_height; | ||
243 | } | ||
244 | |||
245 | int border = swaynag->type->bar_border_thickness * swaynag->scale; | ||
246 | if (max_height > swaynag->height) { | ||
247 | max_height += border; | ||
248 | } | ||
249 | cairo_set_source_u32(cairo, swaynag->type->border_bottom); | ||
250 | cairo_rectangle(cairo, 0, | ||
251 | swaynag->height * swaynag->scale - border, | ||
252 | swaynag->width * swaynag->scale, | ||
253 | border); | ||
254 | cairo_fill(cairo); | ||
255 | |||
256 | return max_height; | ||
257 | } | ||
258 | |||
259 | void render_frame(struct swaynag *swaynag) { | ||
260 | if (!swaynag->run_display) { | ||
261 | return; | ||
262 | } | ||
263 | |||
264 | cairo_surface_t *recorder = cairo_recording_surface_create( | ||
265 | CAIRO_CONTENT_COLOR_ALPHA, NULL); | ||
266 | cairo_t *cairo = cairo_create(recorder); | ||
267 | cairo_save(cairo); | ||
268 | cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); | ||
269 | cairo_paint(cairo); | ||
270 | cairo_restore(cairo); | ||
271 | uint32_t height = render_to_cairo(cairo, swaynag); | ||
272 | if (height != swaynag->height) { | ||
273 | zwlr_layer_surface_v1_set_size(swaynag->layer_surface, 0, height); | ||
274 | zwlr_layer_surface_v1_set_exclusive_zone(swaynag->layer_surface, | ||
275 | height); | ||
276 | wl_surface_commit(swaynag->surface); | ||
277 | wl_display_roundtrip(swaynag->display); | ||
278 | } else { | ||
279 | swaynag->current_buffer = get_next_buffer(swaynag->shm, | ||
280 | swaynag->buffers, | ||
281 | swaynag->width * swaynag->scale, | ||
282 | swaynag->height * swaynag->scale); | ||
283 | if (!swaynag->current_buffer) { | ||
284 | wlr_log(WLR_DEBUG, "Failed to get buffer. Skipping frame."); | ||
285 | goto cleanup; | ||
286 | } | ||
287 | |||
288 | cairo_t *shm = swaynag->current_buffer->cairo; | ||
289 | cairo_save(shm); | ||
290 | cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); | ||
291 | cairo_paint(shm); | ||
292 | cairo_restore(shm); | ||
293 | cairo_set_source_surface(shm, recorder, 0.0, 0.0); | ||
294 | cairo_paint(shm); | ||
295 | |||
296 | wl_surface_set_buffer_scale(swaynag->surface, swaynag->scale); | ||
297 | wl_surface_attach(swaynag->surface, | ||
298 | swaynag->current_buffer->buffer, 0, 0); | ||
299 | wl_surface_damage(swaynag->surface, 0, 0, | ||
300 | swaynag->width, swaynag->height); | ||
301 | wl_surface_commit(swaynag->surface); | ||
302 | wl_display_roundtrip(swaynag->display); | ||
303 | } | ||
304 | |||
305 | cleanup: | ||
306 | cairo_surface_destroy(recorder); | ||
307 | cairo_destroy(cairo); | ||
308 | } | ||
diff --git a/swaynag/swaynag.1.scd b/swaynag/swaynag.1.scd new file mode 100644 index 00000000..1c395aee --- /dev/null +++ b/swaynag/swaynag.1.scd | |||
@@ -0,0 +1,106 @@ | |||
1 | swaynag(1) | ||
2 | |||
3 | # NAME | ||
4 | |||
5 | swaynag - Show a warning or error message with buttons | ||
6 | |||
7 | # SYNOPSIS | ||
8 | |||
9 | _swaynag_ [options...] | ||
10 | |||
11 | # OPTIONS | ||
12 | |||
13 | *-b, --button* <text> <action> | ||
14 | Create a button with the text _text_ that executes _action_ when pressed. | ||
15 | Multiple buttons can be defined by providing the flag multiple times. | ||
16 | |||
17 | *-c, --config* <path> | ||
18 | The config file to use. By default, the following paths are checked: | ||
19 | _$HOME/.swaynag/config_, _$XDG\_CONFIG\_HOME/swaynag/config_, and | ||
20 | _SYSCONFDIR/swaynag/config_. All flags aside from this one and _debug_ are | ||
21 | valid options in the configuration file using the format | ||
22 | _long-option=value_. All leading dashes should be omitted and the equals | ||
23 | sign is required. See swaynag(5) for more information. | ||
24 | |||
25 | *-d, --debug* | ||
26 | Enable debugging. | ||
27 | |||
28 | *-e, --edge* top|bottom | ||
29 | Set the edge to use. | ||
30 | |||
31 | *-f, --font* <font> | ||
32 | Set the font to use. | ||
33 | |||
34 | *-h, --help* | ||
35 | Show help message and quit. | ||
36 | |||
37 | *-l, --detailed-message* | ||
38 | Read a detailed message from stdin. A button to toggle details will be | ||
39 | added. Details are shown in a scrollable multi-line text area. | ||
40 | |||
41 | *-L, --detailed-button* <text> | ||
42 | Set the text for the button that toggles details. This has no effect if | ||
43 | there is not a detailed message. The default is _Toggle Details_. | ||
44 | |||
45 | *-m, --message* <msg> | ||
46 | Set the message text. | ||
47 | |||
48 | *-o, --output* <output> | ||
49 | Set the output to use. This should be the name of a _xdg\_output_. | ||
50 | |||
51 | *-s, --dismiss-button* <text> | ||
52 | Sets the text for the dismiss nagbar button. The default is _X_. | ||
53 | |||
54 | *-t, --type* <type> | ||
55 | Set the message type. Two types are created by default _error_ and | ||
56 | _warning_. Custom types can be defined in the config file. See | ||
57 | _--config_ and swaynag(5) for details. Both of the default types can be | ||
58 | overridden in the config file as well. | ||
59 | |||
60 | *-v, --version* | ||
61 | Show the version number and quit. | ||
62 | |||
63 | # APPEARANCE OPTIONS | ||
64 | |||
65 | *--background* <RRGGBB[AA]> | ||
66 | Set the color of the background. | ||
67 | |||
68 | *--border* <RRGGBB[AA]> | ||
69 | Set the color of the border. | ||
70 | |||
71 | *--border-bottom* <RRGGBB[AA]> | ||
72 | Set the color of the bottom border. | ||
73 | |||
74 | *--button-background* <RRGGBB[AA]> | ||
75 | Set the color for the background for buttons. | ||
76 | |||
77 | *--text* <RRGGBB[AA]> | ||
78 | Set the text color. | ||
79 | |||
80 | *--border-bottom-size* <size> | ||
81 | Set the thickness of the bottom border. | ||
82 | |||
83 | *--message-padding* <padding> | ||
84 | Set the padding for the message. | ||
85 | |||
86 | *--details-border-size* <size> | ||
87 | Set the thickness for the details border. | ||
88 | |||
89 | *--button-border-size* <size> | ||
90 | Set the thickness for the button border. | ||
91 | |||
92 | *--button-gap* <gap> | ||
93 | Set the size of the gap between buttons. | ||
94 | |||
95 | *--button-dismiss-gap* <gap> | ||
96 | Set the size of the gap between the dismiss button and another button. | ||
97 | |||
98 | *--button-margin-right* <margin> | ||
99 | Set the margin from the right of the dismiss button to edge. | ||
100 | |||
101 | *--button-padding* <padding> | ||
102 | Set the padding for the button text. | ||
103 | |||
104 | # SEE | ||
105 | |||
106 | swaynag(5) | ||
diff --git a/swaynag/swaynag.5.scd b/swaynag/swaynag.5.scd new file mode 100644 index 00000000..d3daadf7 --- /dev/null +++ b/swaynag/swaynag.5.scd | |||
@@ -0,0 +1,100 @@ | |||
1 | swaynag(5) | ||
2 | |||
3 | # NAME | ||
4 | |||
5 | swaynag - swaynag configuration file | ||
6 | |||
7 | # SYNOPSIS | ||
8 | |||
9 | $HOME/.swaynag/config, $XDG\_CONFIG\_HOME/swaynag/config, | ||
10 | SYSCONFDIR/swaynag/config | ||
11 | |||
12 | # CONFIG FILE | ||
13 | |||
14 | At the top of the config file, _swaynag_ options can be set using the format | ||
15 | _long-option=value_. These will be used as default values if _swaynag_ is not | ||
16 | given the option. This can be useful for setting a preferred font, output, and | ||
17 | edge. | ||
18 | |||
19 | Below the options, custom types may be defined. To define a type, use the | ||
20 | following format: | ||
21 | |||
22 | ``` | ||
23 | [name-of-type] | ||
24 | option=value | ||
25 | ``` | ||
26 | |||
27 | All colors may be given in the form _RRGGBB_ or _RRGGBBAA_. The following | ||
28 | colors can be set: | ||
29 | |||
30 | *background=<color>* | ||
31 | The background color for _swaynag_. | ||
32 | |||
33 | *border=<color>* | ||
34 | The color to use for borders of buttons. | ||
35 | |||
36 | *border-bottom=<color>* | ||
37 | The color of the border line at the bottom of _swaynag_. | ||
38 | |||
39 | *button-background=<color>* | ||
40 | The background color for the buttons. | ||
41 | |||
42 | *text=<color>* | ||
43 | The color of the text. | ||
44 | |||
45 | The following sizing options can also be set: | ||
46 | |||
47 | *border-bottom-size=<size>* | ||
48 | Set the thickness of the bottom border. | ||
49 | |||
50 | *message-padding=<padding>* | ||
51 | Set the padding for the message. | ||
52 | |||
53 | *details-border-size=<size>* | ||
54 | Set the thickness for the details border. | ||
55 | |||
56 | *button-border-size=<size>* | ||
57 | Set the thickness for the button border. | ||
58 | |||
59 | *button-gap=<gap>* | ||
60 | Set the size of the gap between buttons. | ||
61 | |||
62 | *button-dismiss-gap=<gap>* | ||
63 | Set the size of the gap between the dismiss button and another button. | ||
64 | |||
65 | *button-margin-right=<margin>* | ||
66 | Set the margin from the right of the dismiss button to edge. | ||
67 | |||
68 | *button-padding=<padding>* | ||
69 | Set the padding for the button text. | ||
70 | |||
71 | Additionally, the following options can be assigned a default per-type: | ||
72 | |||
73 | *edge=top|bottom* | ||
74 | Set the edge to use. | ||
75 | |||
76 | *font=<font>* | ||
77 | Set the font to use. | ||
78 | |||
79 | *output=<output>* | ||
80 | Set the output to use. This should be the name of a _xdg\_output_. | ||
81 | |||
82 | # EXAMPLE | ||
83 | |||
84 | ``` | ||
85 | font=Monospace 12 | ||
86 | edge=bottom | ||
87 | |||
88 | [green] | ||
89 | edge=top | ||
90 | background=00AA00 | ||
91 | border=006600 | ||
92 | border-bottom=004400 | ||
93 | text=FFFFFF | ||
94 | button-background=00CC00 | ||
95 | message-padding=10 | ||
96 | ``` | ||
97 | |||
98 | # SEE | ||
99 | |||
100 | swaynag(1) | ||
diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c new file mode 100644 index 00000000..3966277d --- /dev/null +++ b/swaynag/swaynag.c | |||
@@ -0,0 +1,451 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <assert.h> | ||
3 | #include <sys/stat.h> | ||
4 | #include <sys/wait.h> | ||
5 | #include <wayland-client.h> | ||
6 | #include <wayland-cursor.h> | ||
7 | #include "log.h" | ||
8 | #include "list.h" | ||
9 | #include "swaynag/render.h" | ||
10 | #include "swaynag/swaynag.h" | ||
11 | #include "swaynag/types.h" | ||
12 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||
13 | |||
14 | static void nop() { | ||
15 | // Intentionally left blank | ||
16 | } | ||
17 | |||
18 | static bool terminal_execute(char *terminal, char *command) { | ||
19 | char fname[] = "/tmp/swaynagXXXXXX"; | ||
20 | FILE *tmp= fdopen(mkstemp(fname), "w"); | ||
21 | if (!tmp) { | ||
22 | wlr_log(WLR_ERROR, "Failed to create temp script"); | ||
23 | return false; | ||
24 | } | ||
25 | wlr_log(WLR_DEBUG, "Created temp script: %s", fname); | ||
26 | fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); | ||
27 | fclose(tmp); | ||
28 | chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); | ||
29 | char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1]; | ||
30 | sprintf(cmd, "%s -e %s", terminal, fname); | ||
31 | execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); | ||
32 | return true; | ||
33 | } | ||
34 | |||
35 | static void swaynag_button_execute(struct swaynag *swaynag, | ||
36 | struct swaynag_button *button) { | ||
37 | wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); | ||
38 | if (button->type == SWAYNAG_ACTION_DISMISS) { | ||
39 | swaynag->run_display = false; | ||
40 | } else if (button->type == SWAYNAG_ACTION_EXPAND) { | ||
41 | swaynag->details.visible = !swaynag->details.visible; | ||
42 | render_frame(swaynag); | ||
43 | } else { | ||
44 | if (fork() == 0) { | ||
45 | // Child process. Will be used to prevent zombie processes | ||
46 | setsid(); | ||
47 | if (fork() == 0) { | ||
48 | // Child of the child. Will be reparented to the init process | ||
49 | char *terminal = getenv("TERMINAL"); | ||
50 | if (terminal && strlen(terminal)) { | ||
51 | wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); | ||
52 | if (!terminal_execute(terminal, button->action)) { | ||
53 | swaynag_destroy(swaynag); | ||
54 | exit(EXIT_FAILURE); | ||
55 | } | ||
56 | } else { | ||
57 | wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly"); | ||
58 | execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); | ||
59 | } | ||
60 | } | ||
61 | exit(EXIT_SUCCESS); | ||
62 | } | ||
63 | } | ||
64 | wait(0); | ||
65 | } | ||
66 | |||
67 | static void layer_surface_configure(void *data, | ||
68 | struct zwlr_layer_surface_v1 *surface, | ||
69 | uint32_t serial, uint32_t width, uint32_t height) { | ||
70 | struct swaynag *swaynag = data; | ||
71 | swaynag->width = width; | ||
72 | swaynag->height = height; | ||
73 | zwlr_layer_surface_v1_ack_configure(surface, serial); | ||
74 | render_frame(swaynag); | ||
75 | } | ||
76 | |||
77 | static void layer_surface_closed(void *data, | ||
78 | struct zwlr_layer_surface_v1 *surface) { | ||
79 | struct swaynag *swaynag = data; | ||
80 | swaynag_destroy(swaynag); | ||
81 | } | ||
82 | |||
83 | static struct zwlr_layer_surface_v1_listener layer_surface_listener = { | ||
84 | .configure = layer_surface_configure, | ||
85 | .closed = layer_surface_closed, | ||
86 | }; | ||
87 | |||
88 | static void surface_enter(void *data, struct wl_surface *surface, | ||
89 | struct wl_output *output) { | ||
90 | struct swaynag *swaynag = data; | ||
91 | struct swaynag_output *swaynag_output; | ||
92 | wl_list_for_each(swaynag_output, &swaynag->outputs, link) { | ||
93 | if (swaynag_output->wl_output == output) { | ||
94 | wlr_log(WLR_DEBUG, "Surface enter on output %s", | ||
95 | swaynag_output->name); | ||
96 | swaynag->output = swaynag_output; | ||
97 | swaynag->scale = swaynag->output->scale; | ||
98 | render_frame(swaynag); | ||
99 | break; | ||
100 | } | ||
101 | }; | ||
102 | } | ||
103 | |||
104 | static struct wl_surface_listener surface_listener = { | ||
105 | .enter = surface_enter, | ||
106 | .leave = nop, | ||
107 | }; | ||
108 | |||
109 | static void update_cursor(struct swaynag *swaynag) { | ||
110 | struct swaynag_pointer *pointer = &swaynag->pointer; | ||
111 | pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * swaynag->scale, | ||
112 | swaynag->shm); | ||
113 | struct wl_cursor *cursor = | ||
114 | wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); | ||
115 | pointer->cursor_image = cursor->images[0]; | ||
116 | wl_surface_set_buffer_scale(pointer->cursor_surface, | ||
117 | swaynag->scale); | ||
118 | wl_surface_attach(pointer->cursor_surface, | ||
119 | wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); | ||
120 | wl_pointer_set_cursor(pointer->pointer, pointer->serial, | ||
121 | pointer->cursor_surface, | ||
122 | pointer->cursor_image->hotspot_x / swaynag->scale, | ||
123 | pointer->cursor_image->hotspot_y / swaynag->scale); | ||
124 | wl_surface_commit(pointer->cursor_surface); | ||
125 | } | ||
126 | |||
127 | static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, | ||
128 | uint32_t serial, struct wl_surface *surface, | ||
129 | wl_fixed_t surface_x, wl_fixed_t surface_y) { | ||
130 | struct swaynag *swaynag = data; | ||
131 | struct swaynag_pointer *pointer = &swaynag->pointer; | ||
132 | pointer->serial = serial; | ||
133 | update_cursor(swaynag); | ||
134 | } | ||
135 | |||
136 | static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, | ||
137 | uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { | ||
138 | struct swaynag *swaynag = data; | ||
139 | swaynag->pointer.x = wl_fixed_to_int(surface_x); | ||
140 | swaynag->pointer.y = wl_fixed_to_int(surface_y); | ||
141 | } | ||
142 | |||
143 | static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, | ||
144 | uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { | ||
145 | struct swaynag *swaynag = data; | ||
146 | |||
147 | if (state != WL_POINTER_BUTTON_STATE_PRESSED) { | ||
148 | return; | ||
149 | } | ||
150 | |||
151 | double x = swaynag->pointer.x * swaynag->scale; | ||
152 | double y = swaynag->pointer.y * swaynag->scale; | ||
153 | for (int i = 0; i < swaynag->buttons->length; i++) { | ||
154 | struct swaynag_button *nagbutton = swaynag->buttons->items[i]; | ||
155 | if (x >= nagbutton->x | ||
156 | && y >= nagbutton->y | ||
157 | && x < nagbutton->x + nagbutton->width | ||
158 | && y < nagbutton->y + nagbutton->height) { | ||
159 | swaynag_button_execute(swaynag, nagbutton); | ||
160 | return; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | if (swaynag->details.visible && | ||
165 | swaynag->details.total_lines != swaynag->details.visible_lines) { | ||
166 | struct swaynag_button button_up = swaynag->details.button_up; | ||
167 | if (x >= button_up.x | ||
168 | && y >= button_up.y | ||
169 | && x < button_up.x + button_up.width | ||
170 | && y < button_up.y + button_up.height | ||
171 | && swaynag->details.offset > 0) { | ||
172 | swaynag->details.offset--; | ||
173 | render_frame(swaynag); | ||
174 | return; | ||
175 | } | ||
176 | |||
177 | struct swaynag_button button_down = swaynag->details.button_down; | ||
178 | int bot = swaynag->details.total_lines; | ||
179 | bot -= swaynag->details.visible_lines; | ||
180 | if (x >= button_down.x | ||
181 | && y >= button_down.y | ||
182 | && x < button_down.x + button_down.width | ||
183 | && y < button_down.y + button_down.height | ||
184 | && swaynag->details.offset < bot) { | ||
185 | swaynag->details.offset++; | ||
186 | render_frame(swaynag); | ||
187 | return; | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, | ||
193 | uint32_t time, uint32_t axis, wl_fixed_t value) { | ||
194 | struct swaynag *swaynag = data; | ||
195 | if (!swaynag->details.visible | ||
196 | || swaynag->pointer.x < swaynag->details.x | ||
197 | || swaynag->pointer.y < swaynag->details.y | ||
198 | || swaynag->pointer.x >= swaynag->details.x + swaynag->details.width | ||
199 | || swaynag->pointer.y >= swaynag->details.y + swaynag->details.height | ||
200 | || swaynag->details.total_lines == swaynag->details.visible_lines) { | ||
201 | return; | ||
202 | } | ||
203 | |||
204 | int direction = wl_fixed_to_int(value); | ||
205 | int bot = swaynag->details.total_lines - swaynag->details.visible_lines; | ||
206 | if (direction < 0 && swaynag->details.offset > 0) { | ||
207 | swaynag->details.offset--; | ||
208 | } else if (direction > 0 && swaynag->details.offset < bot) { | ||
209 | swaynag->details.offset++; | ||
210 | } | ||
211 | |||
212 | render_frame(swaynag); | ||
213 | } | ||
214 | |||
215 | static struct wl_pointer_listener pointer_listener = { | ||
216 | .enter = wl_pointer_enter, | ||
217 | .leave = nop, | ||
218 | .motion = wl_pointer_motion, | ||
219 | .button = wl_pointer_button, | ||
220 | .axis = wl_pointer_axis, | ||
221 | .frame = nop, | ||
222 | .axis_source = nop, | ||
223 | .axis_stop = nop, | ||
224 | .axis_discrete = nop, | ||
225 | }; | ||
226 | |||
227 | static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, | ||
228 | enum wl_seat_capability caps) { | ||
229 | struct swaynag *swaynag = data; | ||
230 | if ((caps & WL_SEAT_CAPABILITY_POINTER)) { | ||
231 | swaynag->pointer.pointer = wl_seat_get_pointer(wl_seat); | ||
232 | wl_pointer_add_listener(swaynag->pointer.pointer, &pointer_listener, | ||
233 | swaynag); | ||
234 | } | ||
235 | } | ||
236 | |||
237 | const struct wl_seat_listener seat_listener = { | ||
238 | .capabilities = seat_handle_capabilities, | ||
239 | .name = nop, | ||
240 | }; | ||
241 | |||
242 | static void output_scale(void *data, struct wl_output *output, | ||
243 | int32_t factor) { | ||
244 | struct swaynag_output *swaynag_output = data; | ||
245 | swaynag_output->scale = factor; | ||
246 | if (swaynag_output->swaynag->output == swaynag_output) { | ||
247 | swaynag_output->swaynag->scale = swaynag_output->scale; | ||
248 | update_cursor(swaynag_output->swaynag); | ||
249 | render_frame(swaynag_output->swaynag); | ||
250 | } | ||
251 | } | ||
252 | |||
253 | static struct wl_output_listener output_listener = { | ||
254 | .geometry = nop, | ||
255 | .mode = nop, | ||
256 | .done = nop, | ||
257 | .scale = output_scale, | ||
258 | }; | ||
259 | |||
260 | static void xdg_output_handle_name(void *data, | ||
261 | struct zxdg_output_v1 *xdg_output, const char *name) { | ||
262 | struct swaynag_output *swaynag_output = data; | ||
263 | char *outname = swaynag_output->swaynag->type->output; | ||
264 | wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); | ||
265 | if (!swaynag_output->swaynag->output && outname && name | ||
266 | && strcmp(outname, name) == 0) { | ||
267 | wlr_log(WLR_DEBUG, "Using output %s", name); | ||
268 | swaynag_output->swaynag->output = swaynag_output; | ||
269 | } | ||
270 | swaynag_output->name = strdup(name); | ||
271 | zxdg_output_v1_destroy(xdg_output); | ||
272 | swaynag_output->swaynag->querying_outputs--; | ||
273 | } | ||
274 | |||
275 | static struct zxdg_output_v1_listener xdg_output_listener = { | ||
276 | .logical_position = nop, | ||
277 | .logical_size = nop, | ||
278 | .done = nop, | ||
279 | .name = xdg_output_handle_name, | ||
280 | .description = nop, | ||
281 | }; | ||
282 | |||
283 | static void handle_global(void *data, struct wl_registry *registry, | ||
284 | uint32_t name, const char *interface, uint32_t version) { | ||
285 | struct swaynag *swaynag = data; | ||
286 | if (strcmp(interface, wl_compositor_interface.name) == 0) { | ||
287 | swaynag->compositor = wl_registry_bind(registry, name, | ||
288 | &wl_compositor_interface, 3); | ||
289 | } else if (strcmp(interface, wl_seat_interface.name) == 0) { | ||
290 | swaynag->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); | ||
291 | wl_seat_add_listener(swaynag->seat, &seat_listener, swaynag); | ||
292 | } else if (strcmp(interface, wl_shm_interface.name) == 0) { | ||
293 | swaynag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); | ||
294 | } else if (strcmp(interface, wl_output_interface.name) == 0) { | ||
295 | if (!swaynag->output && swaynag->xdg_output_manager) { | ||
296 | swaynag->querying_outputs++; | ||
297 | struct swaynag_output *output = | ||
298 | calloc(1, sizeof(struct swaynag_output)); | ||
299 | output->wl_output = wl_registry_bind(registry, name, | ||
300 | &wl_output_interface, 3); | ||
301 | output->wl_name = name; | ||
302 | output->scale = 1; | ||
303 | output->swaynag = swaynag; | ||
304 | wl_list_insert(&swaynag->outputs, &output->link); | ||
305 | wl_output_add_listener(output->wl_output, | ||
306 | &output_listener, output); | ||
307 | |||
308 | struct zxdg_output_v1 *xdg_output; | ||
309 | xdg_output = zxdg_output_manager_v1_get_xdg_output( | ||
310 | swaynag->xdg_output_manager, output->wl_output); | ||
311 | zxdg_output_v1_add_listener(xdg_output, | ||
312 | &xdg_output_listener, output); | ||
313 | } | ||
314 | } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { | ||
315 | swaynag->layer_shell = wl_registry_bind( | ||
316 | registry, name, &zwlr_layer_shell_v1_interface, 1); | ||
317 | } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 | ||
318 | && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { | ||
319 | swaynag->xdg_output_manager = wl_registry_bind(registry, name, | ||
320 | &zxdg_output_manager_v1_interface, | ||
321 | ZXDG_OUTPUT_V1_NAME_SINCE_VERSION); | ||
322 | } | ||
323 | } | ||
324 | |||
325 | static void handle_global_remove(void *data, struct wl_registry *registry, | ||
326 | uint32_t name) { | ||
327 | struct swaynag *swaynag = data; | ||
328 | if (swaynag->output->wl_name == name) { | ||
329 | swaynag->run_display = false; | ||
330 | } | ||
331 | } | ||
332 | |||
333 | static const struct wl_registry_listener registry_listener = { | ||
334 | .global = handle_global, | ||
335 | .global_remove = handle_global_remove, | ||
336 | }; | ||
337 | |||
338 | void swaynag_setup(struct swaynag *swaynag) { | ||
339 | swaynag->display = wl_display_connect(NULL); | ||
340 | assert(swaynag->display); | ||
341 | |||
342 | swaynag->scale = 1; | ||
343 | wl_list_init(&swaynag->outputs); | ||
344 | |||
345 | struct wl_registry *registry = wl_display_get_registry(swaynag->display); | ||
346 | wl_registry_add_listener(registry, ®istry_listener, swaynag); | ||
347 | wl_display_roundtrip(swaynag->display); | ||
348 | assert(swaynag->compositor && swaynag->layer_shell && swaynag->shm); | ||
349 | |||
350 | while (swaynag->querying_outputs > 0) { | ||
351 | wl_display_roundtrip(swaynag->display); | ||
352 | } | ||
353 | |||
354 | if (!swaynag->output && swaynag->type->output) { | ||
355 | wlr_log(WLR_ERROR, "Output '%s' not found", swaynag->type->output); | ||
356 | swaynag_destroy(swaynag); | ||
357 | exit(EXIT_FAILURE); | ||
358 | } | ||
359 | |||
360 | struct swaynag_pointer *pointer = &swaynag->pointer; | ||
361 | pointer->cursor_surface = wl_compositor_create_surface(swaynag->compositor); | ||
362 | assert(pointer->cursor_surface); | ||
363 | |||
364 | swaynag->surface = wl_compositor_create_surface(swaynag->compositor); | ||
365 | assert(swaynag->surface); | ||
366 | wl_surface_add_listener(swaynag->surface, &surface_listener, swaynag); | ||
367 | |||
368 | swaynag->layer_surface = zwlr_layer_shell_v1_get_layer_surface( | ||
369 | swaynag->layer_shell, swaynag->surface, | ||
370 | swaynag->output ? swaynag->output->wl_output : NULL, | ||
371 | ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag"); | ||
372 | assert(swaynag->layer_surface); | ||
373 | zwlr_layer_surface_v1_add_listener(swaynag->layer_surface, | ||
374 | &layer_surface_listener, swaynag); | ||
375 | zwlr_layer_surface_v1_set_anchor(swaynag->layer_surface, | ||
376 | swaynag->type->anchors); | ||
377 | |||
378 | wl_registry_destroy(registry); | ||
379 | } | ||
380 | |||
381 | void swaynag_run(struct swaynag *swaynag) { | ||
382 | swaynag->run_display = true; | ||
383 | render_frame(swaynag); | ||
384 | while (swaynag->run_display | ||
385 | && wl_display_dispatch(swaynag->display) != -1) { | ||
386 | // This is intentionally left blank | ||
387 | } | ||
388 | } | ||
389 | |||
390 | void swaynag_destroy(struct swaynag *swaynag) { | ||
391 | swaynag->run_display = false; | ||
392 | |||
393 | free(swaynag->message); | ||
394 | while (swaynag->buttons->length) { | ||
395 | struct swaynag_button *button = swaynag->buttons->items[0]; | ||
396 | list_del(swaynag->buttons, 0); | ||
397 | free(button->text); | ||
398 | free(button->action); | ||
399 | free(button); | ||
400 | } | ||
401 | list_free(swaynag->buttons); | ||
402 | free(swaynag->details.message); | ||
403 | free(swaynag->details.button_up.text); | ||
404 | free(swaynag->details.button_down.text); | ||
405 | |||
406 | if (swaynag->type) { | ||
407 | swaynag_type_free(swaynag->type); | ||
408 | } | ||
409 | |||
410 | if (swaynag->layer_surface) { | ||
411 | zwlr_layer_surface_v1_destroy(swaynag->layer_surface); | ||
412 | } | ||
413 | |||
414 | if (swaynag->surface) { | ||
415 | wl_surface_destroy(swaynag->surface); | ||
416 | } | ||
417 | |||
418 | if (swaynag->pointer.cursor_theme) { | ||
419 | wl_cursor_theme_destroy(swaynag->pointer.cursor_theme); | ||
420 | } | ||
421 | |||
422 | if (&swaynag->buffers[0]) { | ||
423 | destroy_buffer(&swaynag->buffers[0]); | ||
424 | } | ||
425 | |||
426 | if (&swaynag->buffers[1]) { | ||
427 | destroy_buffer(&swaynag->buffers[1]); | ||
428 | } | ||
429 | |||
430 | if (swaynag->outputs.prev || swaynag->outputs.next) { | ||
431 | struct swaynag_output *output, *temp; | ||
432 | wl_list_for_each_safe(output, temp, &swaynag->outputs, link) { | ||
433 | wl_output_destroy(output->wl_output); | ||
434 | free(output->name); | ||
435 | wl_list_remove(&output->link); | ||
436 | free(output); | ||
437 | }; | ||
438 | } | ||
439 | |||
440 | if (swaynag->compositor) { | ||
441 | wl_compositor_destroy(swaynag->compositor); | ||
442 | } | ||
443 | |||
444 | if (swaynag->shm) { | ||
445 | wl_shm_destroy(swaynag->shm); | ||
446 | } | ||
447 | |||
448 | if (swaynag->display) { | ||
449 | wl_display_disconnect(swaynag->display); | ||
450 | } | ||
451 | } | ||
diff --git a/swaynag/types.c b/swaynag/types.c new file mode 100644 index 00000000..1e0a138b --- /dev/null +++ b/swaynag/types.c | |||
@@ -0,0 +1,156 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <getopt.h> | ||
3 | #include <stdbool.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <stdint.h> | ||
6 | #include <string.h> | ||
7 | #include <strings.h> | ||
8 | #include "list.h" | ||
9 | #include "swaynag/config.h" | ||
10 | #include "swaynag/types.h" | ||
11 | #include "util.h" | ||
12 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | ||
13 | |||
14 | void swaynag_types_add_default(list_t *types) { | ||
15 | struct swaynag_type *type_defaults; | ||
16 | type_defaults = calloc(1, sizeof(struct swaynag_type)); | ||
17 | type_defaults->name = strdup("<defaults>"); | ||
18 | type_defaults->font = strdup("pango:Monospace 10"); | ||
19 | type_defaults->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ||
20 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ||
21 | | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; | ||
22 | type_defaults->button_background = 0x333333FF; | ||
23 | type_defaults->background = 0x323232FF; | ||
24 | type_defaults->text = 0xFFFFFFFF; | ||
25 | type_defaults->border = 0x222222FF; | ||
26 | type_defaults->border_bottom = 0x444444FF; | ||
27 | type_defaults->bar_border_thickness = 2; | ||
28 | type_defaults->message_padding = 8; | ||
29 | type_defaults->details_border_thickness = 3; | ||
30 | type_defaults->button_border_thickness = 3; | ||
31 | type_defaults->button_gap = 20; | ||
32 | type_defaults->button_gap_close = 15; | ||
33 | type_defaults->button_margin_right = 2; | ||
34 | type_defaults->button_padding = 3; | ||
35 | list_add(types, type_defaults); | ||
36 | |||
37 | struct swaynag_type *type_error; | ||
38 | type_error = calloc(1, sizeof(struct swaynag_type)); | ||
39 | type_error->button_background = 0x680A0AFF; | ||
40 | type_error->background = 0x900000FF; | ||
41 | type_error->text = 0xFFFFFFFF; | ||
42 | type_error->border = 0xD92424FF; | ||
43 | type_error->border_bottom = 0x470909FF; | ||
44 | type_error->name = strdup("error"); | ||
45 | list_add(types, type_error); | ||
46 | |||
47 | struct swaynag_type *type_warning; | ||
48 | type_warning = calloc(1, sizeof(struct swaynag_type)); | ||
49 | type_warning->name = strdup("warning"); | ||
50 | type_warning->button_background = 0xFFC100FF; | ||
51 | type_warning->background = 0xFFA800FF; | ||
52 | type_warning->text = 0x000000FF; | ||
53 | type_warning->border = 0xAB7100FF; | ||
54 | type_warning->border_bottom = 0xAB7100FF; | ||
55 | list_add(types, type_warning); | ||
56 | } | ||
57 | |||
58 | struct swaynag_type *swaynag_type_get(list_t *types, char *name) { | ||
59 | for (int i = 0; i < types->length; i++) { | ||
60 | struct swaynag_type *type = types->items[i]; | ||
61 | if (strcasecmp(type->name, name) == 0) { | ||
62 | return type; | ||
63 | } | ||
64 | } | ||
65 | return NULL; | ||
66 | } | ||
67 | |||
68 | void swaynag_type_merge(struct swaynag_type *dest, struct swaynag_type *src) { | ||
69 | if (!dest || !src) { | ||
70 | return; | ||
71 | } | ||
72 | |||
73 | if (!dest->font && src->font) { | ||
74 | dest->font = strdup(src->font); | ||
75 | } | ||
76 | |||
77 | if (!dest->output && src->output) { | ||
78 | dest->output = strdup(src->output); | ||
79 | } | ||
80 | |||
81 | if (dest->anchors == 0 && src->anchors > 0) { | ||
82 | dest->anchors = src->anchors; | ||
83 | } | ||
84 | |||
85 | // Colors | ||
86 | if (dest->button_background == 0 && src->button_background > 0) { | ||
87 | dest->button_background = src->button_background; | ||
88 | } | ||
89 | |||
90 | if (dest->background == 0 && src->background > 0) { | ||
91 | dest->background = src->background; | ||
92 | } | ||
93 | |||
94 | if (dest->text == 0 && src->text > 0) { | ||
95 | dest->text = src->text; | ||
96 | } | ||
97 | |||
98 | if (dest->border == 0 && src->border > 0) { | ||
99 | dest->border = src->border; | ||
100 | } | ||
101 | |||
102 | if (dest->border_bottom == 0 && src->border_bottom > 0) { | ||
103 | dest->border_bottom = src->border_bottom; | ||
104 | } | ||
105 | |||
106 | // Sizing | ||
107 | if (dest->bar_border_thickness == 0 && src->bar_border_thickness > 0) { | ||
108 | dest->bar_border_thickness = src->bar_border_thickness; | ||
109 | } | ||
110 | |||
111 | if (dest->message_padding == 0 && src->message_padding > 0) { | ||
112 | dest->message_padding = src->message_padding; | ||
113 | } | ||
114 | |||
115 | if (dest->details_border_thickness == 0 | ||
116 | && src->details_border_thickness > 0) { | ||
117 | dest->details_border_thickness = src->details_border_thickness; | ||
118 | } | ||
119 | |||
120 | if (dest->button_border_thickness == 0 | ||
121 | && src->button_border_thickness > 0) { | ||
122 | dest->button_border_thickness = src->button_border_thickness; | ||
123 | } | ||
124 | |||
125 | if (dest->button_gap == 0 && src->button_gap > 0) { | ||
126 | dest->button_gap = src->button_gap; | ||
127 | } | ||
128 | |||
129 | if (dest->button_gap_close == 0 && src->button_gap_close > 0) { | ||
130 | dest->button_gap_close = src->button_gap_close; | ||
131 | } | ||
132 | |||
133 | if (dest->button_margin_right == 0 && src->button_margin_right > 0) { | ||
134 | dest->button_margin_right = src->button_margin_right; | ||
135 | } | ||
136 | |||
137 | if (dest->button_padding == 0 && src->button_padding > 0) { | ||
138 | dest->button_padding = src->button_padding; | ||
139 | } | ||
140 | } | ||
141 | |||
142 | void swaynag_type_free(struct swaynag_type *type) { | ||
143 | free(type->name); | ||
144 | free(type->font); | ||
145 | free(type->output); | ||
146 | free(type); | ||
147 | } | ||
148 | |||
149 | void swaynag_types_free(list_t *types) { | ||
150 | while (types->length) { | ||
151 | struct swaynag_type *type = types->items[0]; | ||
152 | swaynag_type_free(type); | ||
153 | list_del(types, 0); | ||
154 | } | ||
155 | list_free(types); | ||
156 | } | ||