diff options
author | Drew DeVault <sir@cmpwn.com> | 2017-12-13 20:36:06 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-13 20:36:06 -0500 |
commit | 1aab9ae3e74d15e2c346acbab2ef2def59db72eb (patch) | |
tree | 6a3479fef295dc673c6bcdaac822f44523f94d8e | |
parent | Merge pull request #1501 from emersion/command-include (diff) | |
parent | Use strtol instead of atoi in output command (diff) | |
download | sway-1aab9ae3e74d15e2c346acbab2ef2def59db72eb.tar.gz sway-1aab9ae3e74d15e2c346acbab2ef2def59db72eb.tar.zst sway-1aab9ae3e74d15e2c346acbab2ef2def59db72eb.zip |
Merge pull request #1503 from emersion/output-config
Add output config
-rw-r--r-- | include/sway/config.h | 6 | ||||
-rw-r--r-- | include/sway/container.h | 5 | ||||
-rw-r--r-- | include/sway/layout.h | 8 | ||||
-rw-r--r-- | include/sway/output.h | 2 | ||||
-rw-r--r-- | sway/commands.c | 1 | ||||
-rw-r--r-- | sway/commands/output.c | 311 | ||||
-rw-r--r-- | sway/config/output.c | 185 | ||||
-rw-r--r-- | sway/desktop/output.c | 46 | ||||
-rw-r--r-- | sway/desktop/xwayland.c | 6 | ||||
-rw-r--r-- | sway/main.c | 4 | ||||
-rw-r--r-- | sway/meson.build | 2 | ||||
-rw-r--r-- | sway/tree/container.c | 64 | ||||
-rw-r--r-- | sway/tree/layout.c | 37 |
13 files changed, 643 insertions, 34 deletions
diff --git a/include/sway/config.h b/include/sway/config.h index 7de85ab7..139d7800 100644 --- a/include/sway/config.h +++ b/include/sway/config.h | |||
@@ -80,8 +80,11 @@ struct output_config { | |||
80 | char *name; | 80 | char *name; |
81 | int enabled; | 81 | int enabled; |
82 | int width, height; | 82 | int width, height; |
83 | float refresh_rate; | ||
83 | int x, y; | 84 | int x, y; |
84 | int scale; | 85 | int scale; |
86 | int32_t transform; | ||
87 | |||
85 | char *background; | 88 | char *background; |
86 | char *background_option; | 89 | char *background_option; |
87 | }; | 90 | }; |
@@ -360,9 +363,8 @@ void apply_input_config(struct input_config *ic, struct libinput_device *dev); | |||
360 | void free_input_config(struct input_config *ic); | 363 | void free_input_config(struct input_config *ic); |
361 | 364 | ||
362 | int output_name_cmp(const void *item, const void *data); | 365 | int output_name_cmp(const void *item, const void *data); |
366 | struct output_config *new_output_config(); | ||
363 | void merge_output_config(struct output_config *dst, struct output_config *src); | 367 | void merge_output_config(struct output_config *dst, struct output_config *src); |
364 | /** Sets up a WLC output handle based on a given output_config. | ||
365 | */ | ||
366 | void apply_output_config(struct output_config *oc, swayc_t *output); | 368 | void apply_output_config(struct output_config *oc, swayc_t *output); |
367 | void free_output_config(struct output_config *oc); | 369 | void free_output_config(struct output_config *oc); |
368 | 370 | ||
diff --git a/include/sway/container.h b/include/sway/container.h index 08a98ed9..b15e0428 100644 --- a/include/sway/container.h +++ b/include/sway/container.h | |||
@@ -57,9 +57,9 @@ enum swayc_border_types { | |||
57 | B_NORMAL, /**< Normal border with title bar */ | 57 | B_NORMAL, /**< Normal border with title bar */ |
58 | }; | 58 | }; |
59 | 59 | ||
60 | struct sway_root; | ||
60 | struct sway_output; | 61 | struct sway_output; |
61 | struct sway_view; | 62 | struct sway_view; |
62 | struct wlr_output_layout; | ||
63 | 63 | ||
64 | /** | 64 | /** |
65 | * Stores information about a container. | 65 | * Stores information about a container. |
@@ -69,7 +69,7 @@ struct wlr_output_layout; | |||
69 | struct sway_container { | 69 | struct sway_container { |
70 | union { | 70 | union { |
71 | // TODO: Encapsulate state for other node types as well like C_CONTAINER | 71 | // TODO: Encapsulate state for other node types as well like C_CONTAINER |
72 | struct wlr_output_layout *output_layout; // C_ROOT | 72 | struct sway_root *sway_root; // C_ROOT |
73 | struct sway_output *sway_output; // C_OUTPUT | 73 | struct sway_output *sway_output; // C_OUTPUT |
74 | struct sway_view *sway_view; // C_VIEW | 74 | struct sway_view *sway_view; // C_VIEW |
75 | }; | 75 | }; |
@@ -132,6 +132,7 @@ swayc_t *new_output(struct sway_output *sway_output); | |||
132 | swayc_t *new_workspace(swayc_t *output, const char *name); | 132 | swayc_t *new_workspace(swayc_t *output, const char *name); |
133 | swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); | 133 | swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); |
134 | 134 | ||
135 | swayc_t *destroy_output(swayc_t *output); | ||
135 | swayc_t *destroy_view(swayc_t *view); | 136 | swayc_t *destroy_view(swayc_t *view); |
136 | 137 | ||
137 | swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); | 138 | swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); |
diff --git a/include/sway/layout.h b/include/sway/layout.h index f3b62b05..bfd96a02 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h | |||
@@ -1,8 +1,16 @@ | |||
1 | #ifndef _SWAY_LAYOUT_H | 1 | #ifndef _SWAY_LAYOUT_H |
2 | #define _SWAY_LAYOUT_H | 2 | #define _SWAY_LAYOUT_H |
3 | 3 | ||
4 | #include <wlr/types/wlr_output_layout.h> | ||
5 | |||
4 | struct sway_container; | 6 | struct sway_container; |
5 | 7 | ||
8 | struct sway_root { | ||
9 | struct wlr_output_layout *output_layout; | ||
10 | |||
11 | struct wl_listener output_layout_change; | ||
12 | }; | ||
13 | |||
6 | void init_layout(void); | 14 | void init_layout(void); |
7 | void add_child(struct sway_container *parent, struct sway_container *child); | 15 | void add_child(struct sway_container *parent, struct sway_container *child); |
8 | struct sway_container *remove_child(struct sway_container *child); | 16 | struct sway_container *remove_child(struct sway_container *child); |
diff --git a/include/sway/output.h b/include/sway/output.h index 895cb07d..7ca02d7b 100644 --- a/include/sway/output.h +++ b/include/sway/output.h | |||
@@ -12,8 +12,8 @@ struct sway_output { | |||
12 | struct sway_container *swayc; | 12 | struct sway_container *swayc; |
13 | struct sway_server *server; | 13 | struct sway_server *server; |
14 | struct timespec last_frame; | 14 | struct timespec last_frame; |
15 | |||
15 | struct wl_listener frame; | 16 | struct wl_listener frame; |
16 | struct wl_listener resolution; | ||
17 | }; | 17 | }; |
18 | 18 | ||
19 | #endif | 19 | #endif |
diff --git a/sway/commands.c b/sway/commands.c index 05a66a7f..d6cf7a64 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -96,6 +96,7 @@ static struct cmd_handler handlers[] = { | |||
96 | { "exec_always", cmd_exec_always }, | 96 | { "exec_always", cmd_exec_always }, |
97 | { "exit", cmd_exit }, | 97 | { "exit", cmd_exit }, |
98 | { "include", cmd_include }, | 98 | { "include", cmd_include }, |
99 | { "output", cmd_output }, | ||
99 | }; | 100 | }; |
100 | 101 | ||
101 | static int handler_compare(const void *_a, const void *_b) { | 102 | static int handler_compare(const void *_a, const void *_b) { |
diff --git a/sway/commands/output.c b/sway/commands/output.c new file mode 100644 index 00000000..d71e4d8d --- /dev/null +++ b/sway/commands/output.c | |||
@@ -0,0 +1,311 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <ctype.h> | ||
3 | #include <libgen.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <string.h> | ||
6 | #include <strings.h> | ||
7 | #include <unistd.h> | ||
8 | #include <wordexp.h> | ||
9 | #include "sway/commands.h" | ||
10 | #include "sway/config.h" | ||
11 | #include "list.h" | ||
12 | #include "log.h" | ||
13 | #include "stringop.h" | ||
14 | |||
15 | static char *bg_options[] = { | ||
16 | "stretch", | ||
17 | "center", | ||
18 | "fill", | ||
19 | "fit", | ||
20 | "tile", | ||
21 | }; | ||
22 | |||
23 | struct cmd_results *cmd_output(int argc, char **argv) { | ||
24 | struct cmd_results *error = NULL; | ||
25 | if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { | ||
26 | return error; | ||
27 | } | ||
28 | const char *name = argv[0]; | ||
29 | |||
30 | struct output_config *output = new_output_config(); | ||
31 | if (!output) { | ||
32 | sway_log(L_ERROR, "Failed to allocate output config"); | ||
33 | return NULL; | ||
34 | } | ||
35 | output->name = strdup(name); | ||
36 | |||
37 | int i; | ||
38 | for (i = 1; i < argc; ++i) { | ||
39 | const char *command = argv[i]; | ||
40 | |||
41 | if (strcasecmp(command, "disable") == 0) { | ||
42 | output->enabled = 0; | ||
43 | } else if (strcasecmp(command, "mode") == 0 || | ||
44 | strcasecmp(command, "resolution") == 0 || | ||
45 | strcasecmp(command, "res") == 0) { | ||
46 | if (++i >= argc) { | ||
47 | error = cmd_results_new(CMD_INVALID, "output", | ||
48 | "Missing mode argument."); | ||
49 | goto fail; | ||
50 | } | ||
51 | |||
52 | int width = -1, height = -1; | ||
53 | float refresh_rate = -1; | ||
54 | |||
55 | char *end; | ||
56 | width = strtol(argv[i], &end, 10); | ||
57 | if (*end) { | ||
58 | // Format is 1234x4321 | ||
59 | if (*end != 'x') { | ||
60 | error = cmd_results_new(CMD_INVALID, "output", | ||
61 | "Invalid mode width."); | ||
62 | goto fail; | ||
63 | } | ||
64 | ++end; | ||
65 | height = strtol(end, &end, 10); | ||
66 | if (*end) { | ||
67 | if (*end != '@') { | ||
68 | error = cmd_results_new(CMD_INVALID, "output", | ||
69 | "Invalid mode height."); | ||
70 | goto fail; | ||
71 | } | ||
72 | ++end; | ||
73 | refresh_rate = strtof(end, &end); | ||
74 | if (strcasecmp("Hz", end) != 0) { | ||
75 | error = cmd_results_new(CMD_INVALID, "output", | ||
76 | "Invalid mode refresh rate."); | ||
77 | goto fail; | ||
78 | } | ||
79 | } | ||
80 | } else { | ||
81 | // Format is 1234 4321 | ||
82 | if (++i >= argc) { | ||
83 | error = cmd_results_new(CMD_INVALID, "output", | ||
84 | "Missing mode argument (height)."); | ||
85 | goto fail; | ||
86 | } | ||
87 | height = strtol(argv[i], &end, 10); | ||
88 | if (*end) { | ||
89 | error = cmd_results_new(CMD_INVALID, "output", | ||
90 | "Invalid mode height."); | ||
91 | goto fail; | ||
92 | } | ||
93 | } | ||
94 | output->width = width; | ||
95 | output->height = height; | ||
96 | output->refresh_rate = refresh_rate; | ||
97 | } else if (strcasecmp(command, "position") == 0 || | ||
98 | strcasecmp(command, "pos") == 0) { | ||
99 | if (++i >= argc) { | ||
100 | error = cmd_results_new(CMD_INVALID, "output", | ||
101 | "Missing position argument."); | ||
102 | goto fail; | ||
103 | } | ||
104 | |||
105 | int x = -1, y = -1; | ||
106 | |||
107 | char *end; | ||
108 | x = strtol(argv[i], &end, 10); | ||
109 | if (*end) { | ||
110 | // Format is 1234,4321 | ||
111 | if (*end != ',') { | ||
112 | error = cmd_results_new(CMD_INVALID, "output", | ||
113 | "Invalid position x."); | ||
114 | goto fail; | ||
115 | } | ||
116 | ++end; | ||
117 | y = strtol(end, &end, 10); | ||
118 | if (*end) { | ||
119 | error = cmd_results_new(CMD_INVALID, "output", | ||
120 | "Invalid position y."); | ||
121 | goto fail; | ||
122 | } | ||
123 | } else { | ||
124 | // Format is 1234 4321 (legacy) | ||
125 | if (++i >= argc) { | ||
126 | error = cmd_results_new(CMD_INVALID, "output", | ||
127 | "Missing position argument (y)."); | ||
128 | goto fail; | ||
129 | } | ||
130 | y = strtol(argv[i], &end, 10); | ||
131 | if (*end) { | ||
132 | error = cmd_results_new(CMD_INVALID, "output", | ||
133 | "Invalid position y."); | ||
134 | goto fail; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | output->x = x; | ||
139 | output->y = y; | ||
140 | } else if (strcasecmp(command, "scale") == 0) { | ||
141 | if (++i >= argc) { | ||
142 | error = cmd_results_new(CMD_INVALID, "output", | ||
143 | "Missing scale parameter."); | ||
144 | goto fail; | ||
145 | } | ||
146 | char *end; | ||
147 | output->scale = strtol(argv[i], &end, 10); | ||
148 | if (*end) { | ||
149 | error = cmd_results_new(CMD_INVALID, "output", | ||
150 | "Invalid scale."); | ||
151 | goto fail; | ||
152 | } | ||
153 | } else if (strcasecmp(command, "transform") == 0) { | ||
154 | if (++i >= argc) { | ||
155 | error = cmd_results_new(CMD_INVALID, "output", | ||
156 | "Missing transform parameter."); | ||
157 | goto fail; | ||
158 | } | ||
159 | char *value = argv[i]; | ||
160 | if (strcmp(value, "normal") == 0) { | ||
161 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; | ||
162 | } else if (strcmp(value, "90") == 0) { | ||
163 | output->transform = WL_OUTPUT_TRANSFORM_90; | ||
164 | } else if (strcmp(value, "180") == 0) { | ||
165 | output->transform = WL_OUTPUT_TRANSFORM_180; | ||
166 | } else if (strcmp(value, "270") == 0) { | ||
167 | output->transform = WL_OUTPUT_TRANSFORM_270; | ||
168 | } else if (strcmp(value, "flipped") == 0) { | ||
169 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; | ||
170 | } else if (strcmp(value, "flipped-90") == 0) { | ||
171 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; | ||
172 | } else if (strcmp(value, "flipped-180") == 0) { | ||
173 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; | ||
174 | } else if (strcmp(value, "flipped-270") == 0) { | ||
175 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; | ||
176 | } else { | ||
177 | error = cmd_results_new(CMD_INVALID, "output", | ||
178 | "Invalid output transform."); | ||
179 | goto fail; | ||
180 | } | ||
181 | } else if (strcasecmp(command, "background") == 0 || | ||
182 | strcasecmp(command, "bg") == 0) { | ||
183 | wordexp_t p; | ||
184 | if (++i >= argc) { | ||
185 | error = cmd_results_new(CMD_INVALID, "output", | ||
186 | "Missing background file or color specification."); | ||
187 | goto fail; | ||
188 | } | ||
189 | if (i + 1 >= argc) { | ||
190 | error = cmd_results_new(CMD_INVALID, "output", | ||
191 | "Missing background scaling mode or `solid_color`."); | ||
192 | goto fail; | ||
193 | } | ||
194 | if (strcasecmp(argv[i + 1], "solid_color") == 0) { | ||
195 | output->background = strdup(argv[argc - 2]); | ||
196 | output->background_option = strdup("solid_color"); | ||
197 | } else { | ||
198 | // argv[i+j]=bg_option | ||
199 | bool valid = false; | ||
200 | char *mode; | ||
201 | size_t j; | ||
202 | for (j = 0; j < (size_t) (argc - i); ++j) { | ||
203 | mode = argv[i + j]; | ||
204 | size_t n = sizeof(bg_options) / sizeof(char *); | ||
205 | for (size_t k = 0; k < n; ++k) { | ||
206 | if (strcasecmp(mode, bg_options[k]) == 0) { | ||
207 | valid = true; | ||
208 | break; | ||
209 | } | ||
210 | } | ||
211 | if (valid) { | ||
212 | break; | ||
213 | } | ||
214 | } | ||
215 | if (!valid) { | ||
216 | error = cmd_results_new(CMD_INVALID, "output", | ||
217 | "Missing background scaling mode."); | ||
218 | goto fail; | ||
219 | } | ||
220 | |||
221 | char *src = join_args(argv + i, j); | ||
222 | if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { | ||
223 | error = cmd_results_new(CMD_INVALID, "output", | ||
224 | "Invalid syntax (%s).", src); | ||
225 | goto fail; | ||
226 | } | ||
227 | free(src); | ||
228 | src = p.we_wordv[0]; | ||
229 | if (config->reading && *src != '/') { | ||
230 | char *conf = strdup(config->current_config); | ||
231 | if (conf) { | ||
232 | char *conf_path = dirname(conf); | ||
233 | src = malloc(strlen(conf_path) + strlen(src) + 2); | ||
234 | if (src) { | ||
235 | sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); | ||
236 | } else { | ||
237 | sway_log(L_ERROR, | ||
238 | "Unable to allocate background source"); | ||
239 | } | ||
240 | free(conf); | ||
241 | } else { | ||
242 | sway_log(L_ERROR, | ||
243 | "Unable to allocate background source"); | ||
244 | } | ||
245 | } | ||
246 | if (!src || access(src, F_OK) == -1) { | ||
247 | error = cmd_results_new(CMD_INVALID, "output", | ||
248 | "Background file unreadable (%s).", src); | ||
249 | wordfree(&p); | ||
250 | goto fail; | ||
251 | } | ||
252 | |||
253 | output->background = strdup(src); | ||
254 | output->background_option = strdup(mode); | ||
255 | if (src != p.we_wordv[0]) { | ||
256 | free(src); | ||
257 | } | ||
258 | wordfree(&p); | ||
259 | |||
260 | i += j; | ||
261 | } | ||
262 | } else { | ||
263 | error = cmd_results_new(CMD_INVALID, "output", | ||
264 | "Invalid output subcommand: %s.", command); | ||
265 | goto fail; | ||
266 | } | ||
267 | } | ||
268 | |||
269 | i = list_seq_find(config->output_configs, output_name_cmp, name); | ||
270 | if (i >= 0) { | ||
271 | // merge existing config | ||
272 | struct output_config *oc = config->output_configs->items[i]; | ||
273 | merge_output_config(oc, output); | ||
274 | free_output_config(output); | ||
275 | output = oc; | ||
276 | } else { | ||
277 | list_add(config->output_configs, output); | ||
278 | } | ||
279 | |||
280 | sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " | ||
281 | "position %d,%d scale %d transform %d) (bg %s %s)", | ||
282 | output->name, output->enabled, output->width, output->height, | ||
283 | output->refresh_rate, output->x, output->y, output->scale, | ||
284 | output->transform, output->background, output->background_option); | ||
285 | |||
286 | if (output->name) { | ||
287 | // Try to find the output container and apply configuration now. If | ||
288 | // this is during startup then there will be no container and config | ||
289 | // will be applied during normal "new output" event from wlroots. | ||
290 | swayc_t *cont = NULL; | ||
291 | for (int i = 0; i < root_container.children->length; ++i) { | ||
292 | cont = root_container.children->items[i]; | ||
293 | if (cont->name && ((strcmp(cont->name, output->name) == 0) || | ||
294 | (strcmp(output->name, "*") == 0))) { | ||
295 | apply_output_config(output, cont); | ||
296 | |||
297 | if (strcmp(output->name, "*") != 0) { | ||
298 | // Stop looking if the output config isn't applicable to all | ||
299 | // outputs | ||
300 | break; | ||
301 | } | ||
302 | } | ||
303 | } | ||
304 | } | ||
305 | |||
306 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
307 | |||
308 | fail: | ||
309 | free_output_config(output); | ||
310 | return error; | ||
311 | } | ||
diff --git a/sway/config/output.c b/sway/config/output.c new file mode 100644 index 00000000..dc9ee37c --- /dev/null +++ b/sway/config/output.c | |||
@@ -0,0 +1,185 @@ | |||
1 | #define _XOPEN_SOURCE 700 | ||
2 | #include <string.h> | ||
3 | #include <assert.h> | ||
4 | #include <wlr/types/wlr_output.h> | ||
5 | #include <wlr/types/wlr_output_layout.h> | ||
6 | #include "sway/config.h" | ||
7 | #include "sway/output.h" | ||
8 | #include "log.h" | ||
9 | |||
10 | int output_name_cmp(const void *item, const void *data) { | ||
11 | const struct output_config *output = item; | ||
12 | const char *name = data; | ||
13 | |||
14 | return strcmp(output->name, name); | ||
15 | } | ||
16 | |||
17 | struct output_config *new_output_config() { | ||
18 | struct output_config *oc = calloc(1, sizeof(struct output_config)); | ||
19 | if (oc == NULL) { | ||
20 | return NULL; | ||
21 | } | ||
22 | oc->enabled = -1; | ||
23 | oc->width = oc->height = -1; | ||
24 | oc->refresh_rate = -1; | ||
25 | oc->x = oc->y = -1; | ||
26 | oc->scale = -1; | ||
27 | oc->transform = -1; | ||
28 | return oc; | ||
29 | } | ||
30 | |||
31 | void merge_output_config(struct output_config *dst, struct output_config *src) { | ||
32 | if (src->name) { | ||
33 | free(dst->name); | ||
34 | dst->name = strdup(src->name); | ||
35 | } | ||
36 | if (src->enabled != -1) { | ||
37 | dst->enabled = src->enabled; | ||
38 | } | ||
39 | if (src->width != -1) { | ||
40 | dst->width = src->width; | ||
41 | } | ||
42 | if (src->height != -1) { | ||
43 | dst->height = src->height; | ||
44 | } | ||
45 | if (src->x != -1) { | ||
46 | dst->x = src->x; | ||
47 | } | ||
48 | if (src->y != -1) { | ||
49 | dst->y = src->y; | ||
50 | } | ||
51 | if (src->scale != -1) { | ||
52 | dst->scale = src->scale; | ||
53 | } | ||
54 | if (src->refresh_rate != -1) { | ||
55 | dst->refresh_rate = src->refresh_rate; | ||
56 | } | ||
57 | if (src->transform != -1) { | ||
58 | dst->transform = src->transform; | ||
59 | } | ||
60 | if (src->background) { | ||
61 | free(dst->background); | ||
62 | dst->background = strdup(src->background); | ||
63 | } | ||
64 | if (src->background_option) { | ||
65 | free(dst->background_option); | ||
66 | dst->background_option = strdup(src->background_option); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | static void set_mode(struct wlr_output *output, int width, int height, | ||
71 | float refresh_rate) { | ||
72 | int mhz = (int)(refresh_rate * 1000); | ||
73 | if (wl_list_empty(&output->modes)) { | ||
74 | sway_log(L_DEBUG, "Assigning custom mode to %s", output->name); | ||
75 | wlr_output_set_custom_mode(output, width, height, mhz); | ||
76 | return; | ||
77 | } | ||
78 | |||
79 | struct wlr_output_mode *mode, *best = NULL; | ||
80 | wl_list_for_each(mode, &output->modes, link) { | ||
81 | if (mode->width == width && mode->height == height) { | ||
82 | if (mode->refresh == mhz) { | ||
83 | best = mode; | ||
84 | break; | ||
85 | } | ||
86 | best = mode; | ||
87 | } | ||
88 | } | ||
89 | if (!best) { | ||
90 | sway_log(L_ERROR, "Configured mode for %s not available", output->name); | ||
91 | } else { | ||
92 | sway_log(L_DEBUG, "Assigning configured mode to %s", output->name); | ||
93 | wlr_output_set_mode(output, best); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | void apply_output_config(struct output_config *oc, swayc_t *output) { | ||
98 | assert(output->type == C_OUTPUT); | ||
99 | |||
100 | struct wlr_output *wlr_output = output->sway_output->wlr_output; | ||
101 | if (oc && oc->enabled == 0) { | ||
102 | wlr_output_layout_remove(root_container.sway_root->output_layout, | ||
103 | wlr_output); | ||
104 | destroy_output(output); | ||
105 | return; | ||
106 | } | ||
107 | |||
108 | if (oc && oc->width > 0 && oc->height > 0) { | ||
109 | sway_log(L_DEBUG, "Set %s mode to %dx%d (%f GHz)", oc->name, oc->width, | ||
110 | oc->height, oc->refresh_rate); | ||
111 | set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate); | ||
112 | } | ||
113 | if (oc && oc->scale > 0) { | ||
114 | sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale); | ||
115 | wlr_output_set_scale(wlr_output, oc->scale); | ||
116 | } | ||
117 | if (oc && oc->transform >= 0) { | ||
118 | sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform); | ||
119 | wlr_output_set_transform(wlr_output, oc->transform); | ||
120 | } | ||
121 | |||
122 | // Find position for it | ||
123 | if (oc && (oc->x != -1 || oc->y != -1)) { | ||
124 | sway_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y); | ||
125 | wlr_output_layout_add(root_container.sway_root->output_layout, | ||
126 | wlr_output, oc->x, oc->y); | ||
127 | } else { | ||
128 | wlr_output_layout_add_auto(root_container.sway_root->output_layout, | ||
129 | wlr_output); | ||
130 | } | ||
131 | |||
132 | if (!oc || !oc->background) { | ||
133 | // Look for a * config for background | ||
134 | int i = list_seq_find(config->output_configs, output_name_cmp, "*"); | ||
135 | if (i >= 0) { | ||
136 | oc = config->output_configs->items[i]; | ||
137 | } else { | ||
138 | oc = NULL; | ||
139 | } | ||
140 | } | ||
141 | |||
142 | int output_i; | ||
143 | for (output_i = 0; output_i < root_container.children->length; ++output_i) { | ||
144 | if (root_container.children->items[output_i] == output) { | ||
145 | break; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | if (oc && oc->background) { | ||
150 | // TODO swaybg | ||
151 | /*if (output->bg_pid != 0) { | ||
152 | terminate_swaybg(output->bg_pid); | ||
153 | } | ||
154 | |||
155 | sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); | ||
156 | |||
157 | size_t bufsize = 12; | ||
158 | char output_id[bufsize]; | ||
159 | snprintf(output_id, bufsize, "%d", output_i); | ||
160 | output_id[bufsize-1] = 0; | ||
161 | |||
162 | char *const cmd[] = { | ||
163 | "swaybg", | ||
164 | output_id, | ||
165 | oc->background, | ||
166 | oc->background_option, | ||
167 | NULL, | ||
168 | }; | ||
169 | |||
170 | output->bg_pid = fork(); | ||
171 | if (output->bg_pid == 0) { | ||
172 | execvp(cmd[0], cmd); | ||
173 | }*/ | ||
174 | } | ||
175 | } | ||
176 | |||
177 | void free_output_config(struct output_config *oc) { | ||
178 | if (!oc) { | ||
179 | return; | ||
180 | } | ||
181 | free(oc->name); | ||
182 | free(oc->background); | ||
183 | free(oc->background_option); | ||
184 | free(oc); | ||
185 | } | ||
diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 7eb48bdf..ad843b31 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c | |||
@@ -72,8 +72,7 @@ static void output_frame_view(swayc_t *view, void *data) { | |||
72 | } | 72 | } |
73 | 73 | ||
74 | static void output_frame_notify(struct wl_listener *listener, void *data) { | 74 | static void output_frame_notify(struct wl_listener *listener, void *data) { |
75 | struct sway_output *soutput = wl_container_of( | 75 | struct sway_output *soutput = wl_container_of(listener, soutput, frame); |
76 | listener, soutput, frame); | ||
77 | struct wlr_output *wlr_output = data; | 76 | struct wlr_output *wlr_output = data; |
78 | struct sway_server *server = soutput->server; | 77 | struct sway_server *server = soutput->server; |
79 | 78 | ||
@@ -92,40 +91,51 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { | |||
92 | soutput->last_frame = now; | 91 | soutput->last_frame = now; |
93 | } | 92 | } |
94 | 93 | ||
95 | static void output_resolution_notify(struct wl_listener *listener, void *data) { | ||
96 | struct sway_output *soutput = wl_container_of( | ||
97 | listener, soutput, resolution); | ||
98 | arrange_windows(soutput->swayc, -1, -1); | ||
99 | } | ||
100 | |||
101 | void output_add_notify(struct wl_listener *listener, void *data) { | 94 | void output_add_notify(struct wl_listener *listener, void *data) { |
102 | struct sway_server *server = wl_container_of(listener, server, output_add); | 95 | struct sway_server *server = wl_container_of(listener, server, output_add); |
103 | struct wlr_output *wlr_output = data; | 96 | struct wlr_output *wlr_output = data; |
104 | sway_log(L_DEBUG, "New output %p: %s", wlr_output, wlr_output->name); | 97 | sway_log(L_DEBUG, "New output %p: %s", wlr_output, wlr_output->name); |
105 | 98 | ||
106 | struct sway_output *output = calloc(1, sizeof(struct sway_output)); | 99 | struct sway_output *output = calloc(1, sizeof(struct sway_output)); |
100 | if (!output) { | ||
101 | return; | ||
102 | } | ||
107 | output->wlr_output = wlr_output; | 103 | output->wlr_output = wlr_output; |
108 | output->server = server; | 104 | output->server = server; |
109 | output->swayc = new_output(output); | ||
110 | 105 | ||
111 | if (wl_list_length(&wlr_output->modes) > 0) { | 106 | if (!wl_list_empty(&wlr_output->modes)) { |
112 | struct wlr_output_mode *mode = NULL; | 107 | struct wlr_output_mode *mode = |
113 | mode = wl_container_of((&wlr_output->modes)->prev, mode, link); | 108 | wl_container_of(wlr_output->modes.prev, mode, link); |
114 | wlr_output_set_mode(wlr_output, mode); | 109 | wlr_output_set_mode(wlr_output, mode); |
115 | } | 110 | } |
116 | 111 | ||
112 | output->swayc = new_output(output); | ||
113 | if (!output->swayc) { | ||
114 | free(output); | ||
115 | return; | ||
116 | } | ||
117 | |||
117 | output->frame.notify = output_frame_notify; | 118 | output->frame.notify = output_frame_notify; |
118 | wl_signal_add(&wlr_output->events.frame, &output->frame); | 119 | wl_signal_add(&wlr_output->events.frame, &output->frame); |
119 | |||
120 | output->resolution.notify = output_resolution_notify; | ||
121 | wl_signal_add(&wlr_output->events.resolution, &output->resolution); | ||
122 | |||
123 | arrange_windows(output->swayc, -1, -1); | ||
124 | } | 120 | } |
125 | 121 | ||
126 | void output_remove_notify(struct wl_listener *listener, void *data) { | 122 | void output_remove_notify(struct wl_listener *listener, void *data) { |
127 | struct sway_server *server = wl_container_of(listener, server, output_remove); | 123 | struct sway_server *server = wl_container_of(listener, server, output_remove); |
128 | struct wlr_output *wlr_output = data; | 124 | struct wlr_output *wlr_output = data; |
129 | sway_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); | 125 | sway_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name); |
130 | // TODO | 126 | |
127 | swayc_t *output_container = NULL; | ||
128 | for (int i = 0 ; i < root_container.children->length; ++i) { | ||
129 | swayc_t *child = root_container.children->items[i]; | ||
130 | if (child->type == C_OUTPUT && | ||
131 | child->sway_output->wlr_output == wlr_output) { | ||
132 | output_container = child; | ||
133 | break; | ||
134 | } | ||
135 | } | ||
136 | if (!output_container) { | ||
137 | return; | ||
138 | } | ||
139 | |||
140 | destroy_output(output_container); | ||
131 | } | 141 | } |
diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 65c7e1ec..e3799d2d 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c | |||
@@ -55,7 +55,7 @@ static void set_position(struct sway_view *view, double ox, double oy) { | |||
55 | if (!sway_assert(root, "output must be within tree to set position")) { | 55 | if (!sway_assert(root, "output must be within tree to set position")) { |
56 | return; | 56 | return; |
57 | } | 57 | } |
58 | struct wlr_output_layout *layout = root->output_layout; | 58 | struct wlr_output_layout *layout = root->sway_root->output_layout; |
59 | struct wlr_output_layout_output *loutput = | 59 | struct wlr_output_layout_output *loutput = |
60 | wlr_output_layout_get(layout, output->sway_output->wlr_output); | 60 | wlr_output_layout_get(layout, output->sway_output->wlr_output); |
61 | if (!sway_assert(loutput, "output must be within layout to set position")) { | 61 | if (!sway_assert(loutput, "output must be within layout to set position")) { |
@@ -147,14 +147,14 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { | |||
147 | // TODO remove from the tree when the surface goes away (unmapped) | 147 | // TODO remove from the tree when the surface goes away (unmapped) |
148 | sway_view->surface = xsurface->surface; | 148 | sway_view->surface = xsurface->surface; |
149 | sway_surface->view = sway_view; | 149 | sway_surface->view = sway_view; |
150 | 150 | ||
151 | // TODO: | 151 | // TODO: |
152 | // - Wire up listeners | 152 | // - Wire up listeners |
153 | // - Handle popups | 153 | // - Handle popups |
154 | // - Look up pid and open on appropriate workspace | 154 | // - Look up pid and open on appropriate workspace |
155 | // - Set new view to maximized so it behaves nicely | 155 | // - Set new view to maximized so it behaves nicely |
156 | // - Criteria | 156 | // - Criteria |
157 | 157 | ||
158 | sway_surface->commit.notify = handle_commit; | 158 | sway_surface->commit.notify = handle_commit; |
159 | wl_signal_add(&xsurface->surface->events.commit, &sway_surface->commit); | 159 | wl_signal_add(&xsurface->surface->events.commit, &sway_surface->commit); |
160 | sway_surface->destroy.notify = handle_destroy; | 160 | sway_surface->destroy.notify = handle_destroy; |
diff --git a/sway/main.c b/sway/main.c index bc843591..8952f997 100644 --- a/sway/main.c +++ b/sway/main.c | |||
@@ -404,6 +404,10 @@ int main(int argc, char **argv) { | |||
404 | 404 | ||
405 | security_sanity_check(); | 405 | security_sanity_check(); |
406 | 406 | ||
407 | // TODO: wait for server to be ready | ||
408 | // TODO: consume config->cmd_queue | ||
409 | config->active = true; | ||
410 | |||
407 | if (!terminate_request) { | 411 | if (!terminate_request) { |
408 | server_run(&server); | 412 | server_run(&server); |
409 | } | 413 | } |
diff --git a/sway/meson.build b/sway/meson.build index ab863e87..e16691a8 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -6,7 +6,9 @@ sway_sources = files( | |||
6 | 'commands/exec.c', | 6 | 'commands/exec.c', |
7 | 'commands/exec_always.c', | 7 | 'commands/exec_always.c', |
8 | 'commands/include.c', | 8 | 'commands/include.c', |
9 | 'commands/output.c', | ||
9 | 'config.c', | 10 | 'config.c', |
11 | 'config/output.c', | ||
10 | 'ipc-json.c', | 12 | 'ipc-json.c', |
11 | 'ipc-server.c', | 13 | 'ipc-server.c', |
12 | 'desktop/output.c', | 14 | 'desktop/output.c', |
diff --git a/sway/tree/container.c b/sway/tree/container.c index e205fbcf..5df10bcb 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c | |||
@@ -2,10 +2,13 @@ | |||
2 | #include <stdint.h> | 2 | #include <stdint.h> |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <strings.h> | ||
5 | #include <wlr/types/wlr_output_layout.h> | 6 | #include <wlr/types/wlr_output_layout.h> |
7 | #include "sway/config.h" | ||
6 | #include "sway/container.h" | 8 | #include "sway/container.h" |
7 | #include "sway/layout.h" | 9 | #include "sway/layout.h" |
8 | #include "sway/output.h" | 10 | #include "sway/output.h" |
11 | #include "sway/server.h" | ||
9 | #include "sway/view.h" | 12 | #include "sway/view.h" |
10 | #include "sway/workspace.h" | 13 | #include "sway/workspace.h" |
11 | #include "log.h" | 14 | #include "log.h" |
@@ -44,19 +47,38 @@ static swayc_t *new_swayc(enum swayc_types type) { | |||
44 | 47 | ||
45 | swayc_t *new_output(struct sway_output *sway_output) { | 48 | swayc_t *new_output(struct sway_output *sway_output) { |
46 | struct wlr_box size; | 49 | struct wlr_box size; |
47 | wlr_output_effective_resolution( | 50 | wlr_output_effective_resolution(sway_output->wlr_output, &size.width, |
48 | sway_output->wlr_output, &size.width, &size.height); | 51 | &size.height); |
49 | const char *name = sway_output->wlr_output->name; | 52 | const char *name = sway_output->wlr_output->name; |
50 | 53 | ||
54 | struct output_config *oc = NULL, *all = NULL; | ||
55 | for (int i = 0; i < config->output_configs->length; ++i) { | ||
56 | struct output_config *cur = config->output_configs->items[i]; | ||
57 | if (strcasecmp(name, cur->name) == 0) { | ||
58 | sway_log(L_DEBUG, "Matched output config for %s", name); | ||
59 | oc = cur; | ||
60 | } | ||
61 | if (strcasecmp("*", cur->name) == 0) { | ||
62 | sway_log(L_DEBUG, "Matched wildcard output config for %s", name); | ||
63 | all = cur; | ||
64 | } | ||
65 | |||
66 | if (oc && all) { | ||
67 | break; | ||
68 | } | ||
69 | } | ||
70 | if (!oc) { | ||
71 | oc = all; | ||
72 | } | ||
73 | if (oc && !oc->enabled) { | ||
74 | return NULL; | ||
75 | } | ||
76 | |||
51 | swayc_t *output = new_swayc(C_OUTPUT); | 77 | swayc_t *output = new_swayc(C_OUTPUT); |
52 | output->sway_output = sway_output; | 78 | output->sway_output = sway_output; |
53 | output->name = name ? strdup(name) : NULL; | 79 | output->name = name ? strdup(name) : NULL; |
54 | output->width = size.width; | ||
55 | output->height = size.width; | ||
56 | 80 | ||
57 | // TODO configure output layout position | 81 | apply_output_config(oc, output); |
58 | wlr_output_layout_add_auto(root_container.output_layout, | ||
59 | sway_output->wlr_output); | ||
60 | 82 | ||
61 | add_child(&root_container, output); | 83 | add_child(&root_container, output); |
62 | 84 | ||
@@ -139,6 +161,34 @@ static void free_swayc(swayc_t *cont) { | |||
139 | free(cont); | 161 | free(cont); |
140 | } | 162 | } |
141 | 163 | ||
164 | swayc_t *destroy_output(swayc_t *output) { | ||
165 | if (!sway_assert(output, "null output passed to destroy_output")) { | ||
166 | return NULL; | ||
167 | } | ||
168 | |||
169 | if (output->children->length > 0) { | ||
170 | // TODO save workspaces when there are no outputs. | ||
171 | // TODO also check if there will ever be no outputs except for exiting | ||
172 | // program | ||
173 | if (root_container.children->length > 1) { | ||
174 | int p = root_container.children->items[0] == output; | ||
175 | // Move workspace from this output to another output | ||
176 | while (output->children->length) { | ||
177 | swayc_t *child = output->children->items[0]; | ||
178 | remove_child(child); | ||
179 | add_child(root_container.children->items[p], child); | ||
180 | } | ||
181 | sort_workspaces(root_container.children->items[p]); | ||
182 | arrange_windows(root_container.children->items[p], -1, -1); | ||
183 | } | ||
184 | } | ||
185 | |||
186 | sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name); | ||
187 | free_swayc(output); | ||
188 | |||
189 | return &root_container; | ||
190 | } | ||
191 | |||
142 | swayc_t *destroy_view(swayc_t *view) { | 192 | swayc_t *destroy_view(swayc_t *view) { |
143 | if (!sway_assert(view, "null view passed to destroy_view")) { | 193 | if (!sway_assert(view, "null view passed to destroy_view")) { |
144 | return NULL; | 194 | return NULL; |
diff --git a/sway/tree/layout.c b/sway/tree/layout.c index cb39a361..4bcf0e2f 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c | |||
@@ -7,6 +7,7 @@ | |||
7 | #include <wlr/types/wlr_output.h> | 7 | #include <wlr/types/wlr_output.h> |
8 | #include <wlr/types/wlr_output_layout.h> | 8 | #include <wlr/types/wlr_output_layout.h> |
9 | #include "sway/container.h" | 9 | #include "sway/container.h" |
10 | #include "sway/layout.h" | ||
10 | #include "sway/output.h" | 11 | #include "sway/output.h" |
11 | #include "sway/view.h" | 12 | #include "sway/view.h" |
12 | #include "list.h" | 13 | #include "list.h" |
@@ -14,13 +15,47 @@ | |||
14 | 15 | ||
15 | swayc_t root_container; | 16 | swayc_t root_container; |
16 | 17 | ||
18 | static void output_layout_change_notify(struct wl_listener *listener, void *data) { | ||
19 | struct wlr_box *layout_box = wlr_output_layout_get_box( | ||
20 | root_container.sway_root->output_layout, NULL); | ||
21 | root_container.width = layout_box->width; | ||
22 | root_container.height = layout_box->height; | ||
23 | |||
24 | for (int i = 0 ; i < root_container.children->length; ++i) { | ||
25 | swayc_t *output_container = root_container.children->items[i]; | ||
26 | if (output_container->type != C_OUTPUT) { | ||
27 | continue; | ||
28 | } | ||
29 | struct sway_output *output = output_container->sway_output; | ||
30 | |||
31 | struct wlr_box *output_box = wlr_output_layout_get_box( | ||
32 | root_container.sway_root->output_layout, output->wlr_output); | ||
33 | if (!output_box) { | ||
34 | continue; | ||
35 | } | ||
36 | output_container->x = output_box->x; | ||
37 | output_container->y = output_box->y; | ||
38 | output_container->width = output_box->width; | ||
39 | output_container->height = output_box->height; | ||
40 | } | ||
41 | |||
42 | arrange_windows(&root_container, -1, -1); | ||
43 | } | ||
44 | |||
17 | void init_layout(void) { | 45 | void init_layout(void) { |
18 | root_container.id = 0; // normally assigned in new_swayc() | 46 | root_container.id = 0; // normally assigned in new_swayc() |
19 | root_container.type = C_ROOT; | 47 | root_container.type = C_ROOT; |
20 | root_container.layout = L_NONE; | 48 | root_container.layout = L_NONE; |
21 | root_container.name = strdup("root"); | 49 | root_container.name = strdup("root"); |
22 | root_container.children = create_list(); | 50 | root_container.children = create_list(); |
23 | root_container.output_layout = wlr_output_layout_create(); | 51 | |
52 | root_container.sway_root = calloc(1, sizeof(*root_container.sway_root)); | ||
53 | root_container.sway_root->output_layout = wlr_output_layout_create(); | ||
54 | |||
55 | root_container.sway_root->output_layout_change.notify = | ||
56 | output_layout_change_notify; | ||
57 | wl_signal_add(&root_container.sway_root->output_layout->events.change, | ||
58 | &root_container.sway_root->output_layout_change); | ||
24 | } | 59 | } |
25 | 60 | ||
26 | void add_child(swayc_t *parent, swayc_t *child) { | 61 | void add_child(swayc_t *parent, swayc_t *child) { |