aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/config.h5
-rw-r--r--include/sway/container.h1
-rw-r--r--sway/commands.c1
-rw-r--r--sway/commands/output.c251
-rw-r--r--sway/config/output.c181
-rw-r--r--sway/main.c4
-rw-r--r--sway/meson.build2
-rw-r--r--sway/tree/container.c87
8 files changed, 525 insertions, 7 deletions
diff --git a/include/sway/config.h b/include/sway/config.h
index 7de85ab7..231356f2 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -6,6 +6,7 @@
6#include <libinput.h> 6#include <libinput.h>
7#include <stdint.h> 7#include <stdint.h>
8#include <string.h> 8#include <string.h>
9#include <wayland-server.h>
9#include <wlr/types/wlr_box.h> 10#include <wlr/types/wlr_box.h>
10#include <xkbcommon/xkbcommon.h> 11#include <xkbcommon/xkbcommon.h>
11#include <time.h> 12#include <time.h>
@@ -80,8 +81,11 @@ struct output_config {
80 char *name; 81 char *name;
81 int enabled; 82 int enabled;
82 int width, height; 83 int width, height;
84 float refresh_rate;
83 int x, y; 85 int x, y;
84 int scale; 86 int scale;
87 int32_t transform;
88
85 char *background; 89 char *background;
86 char *background_option; 90 char *background_option;
87}; 91};
@@ -360,6 +364,7 @@ void apply_input_config(struct input_config *ic, struct libinput_device *dev);
360void free_input_config(struct input_config *ic); 364void free_input_config(struct input_config *ic);
361 365
362int output_name_cmp(const void *item, const void *data); 366int output_name_cmp(const void *item, const void *data);
367void output_config_defaults(struct output_config *oc);
363void merge_output_config(struct output_config *dst, struct output_config *src); 368void merge_output_config(struct output_config *dst, struct output_config *src);
364/** Sets up a WLC output handle based on a given output_config. 369/** Sets up a WLC output handle based on a given output_config.
365 */ 370 */
diff --git a/include/sway/container.h b/include/sway/container.h
index 08a98ed9..e3f84fc6 100644
--- a/include/sway/container.h
+++ b/include/sway/container.h
@@ -132,6 +132,7 @@ swayc_t *new_output(struct sway_output *sway_output);
132swayc_t *new_workspace(swayc_t *output, const char *name); 132swayc_t *new_workspace(swayc_t *output, const char *name);
133swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view); 133swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view);
134 134
135swayc_t *destroy_output(swayc_t *output);
135swayc_t *destroy_view(swayc_t *view); 136swayc_t *destroy_view(swayc_t *view);
136 137
137swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); 138swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type);
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
101static int handler_compare(const void *_a, const void *_b) { 102static 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..bbf8efc3
--- /dev/null
+++ b/sway/commands/output.c
@@ -0,0 +1,251 @@
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
15static char *bg_options[] = {
16 "stretch",
17 "center",
18 "fill",
19 "fit",
20 "tile",
21};
22
23struct 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 = calloc(1, sizeof(struct output_config));
31 if (!output) {
32 return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config");
33 }
34 output_config_defaults(output);
35 output->name = strdup(name);
36
37 // TODO: atoi doesn't handle invalid numbers
38
39 int i;
40 for (i = 1; i < argc; ++i) {
41 const char *command = argv[i];
42
43 if (strcasecmp(command, "disable") == 0) {
44 output->enabled = 0;
45 } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) {
46 if (++i >= argc) {
47 error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument.");
48 goto fail;
49 }
50 char *res = argv[i];
51 char *x = strchr(res, 'x');
52 int width = -1, height = -1;
53 if (x != NULL) {
54 // Format is 1234x4321
55 *x = '\0';
56 width = atoi(res);
57 height = atoi(x + 1);
58 *x = 'x';
59 } else {
60 // Format is 1234 4321
61 width = atoi(res);
62 if (++i >= argc) {
63 error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height).");
64 goto fail;
65 }
66 res = argv[i];
67 height = atoi(res);
68 }
69 output->width = width;
70 output->height = height;
71 } else if (strcasecmp(command, "refresh_rate") == 0) {
72 if (++i >= argc) {
73 error = cmd_results_new(CMD_INVALID, "output", "Missing refresh_rate argument.");
74 goto fail;
75 }
76 output->refresh_rate = atof(argv[i]);
77 } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) {
78 if (++i >= argc) {
79 error = cmd_results_new(CMD_INVALID, "output", "Missing position argument.");
80 goto fail;
81 }
82 char *res = argv[i];
83 char *c = strchr(res, ',');
84 int x = -1, y = -1;
85 if (c != NULL) {
86 // Format is 1234,4321
87 *c = '\0';
88 x = atoi(res);
89 y = atoi(c + 1);
90 *c = ',';
91 } else {
92 // Format is 1234 4321
93 x = atoi(res);
94 if (++i >= argc) {
95 error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y).");
96 goto fail;
97 }
98 res = argv[i];
99 y = atoi(res);
100 }
101 output->x = x;
102 output->y = y;
103 } else if (strcasecmp(command, "scale") == 0) {
104 if (++i >= argc) {
105 error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter.");
106 goto fail;
107 }
108 output->scale = atoi(argv[i]);
109 } else if (strcasecmp(command, "transform") == 0) {
110 if (++i >= argc) {
111 error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter.");
112 goto fail;
113 }
114 char *value = argv[i];
115 if (strcmp(value, "normal") == 0) {
116 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
117 } else if (strcmp(value, "90") == 0) {
118 output->transform = WL_OUTPUT_TRANSFORM_90;
119 } else if (strcmp(value, "180") == 0) {
120 output->transform = WL_OUTPUT_TRANSFORM_180;
121 } else if (strcmp(value, "270") == 0) {
122 output->transform = WL_OUTPUT_TRANSFORM_270;
123 } else if (strcmp(value, "flipped") == 0) {
124 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
125 } else if (strcmp(value, "flipped-90") == 0) {
126 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
127 } else if (strcmp(value, "flipped-180") == 0) {
128 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
129 } else if (strcmp(value, "flipped-270") == 0) {
130 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
131 } else {
132 error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform.");
133 goto fail;
134 }
135 } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) {
136 wordexp_t p;
137 if (++i >= argc) {
138 error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification.");
139 goto fail;
140 }
141 if (i + 1 >= argc) {
142 error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`.");
143 goto fail;
144 }
145 if (strcasecmp(argv[i + 1], "solid_color") == 0) {
146 output->background = strdup(argv[argc - 2]);
147 output->background_option = strdup("solid_color");
148 } else {
149 // argv[i+j]=bg_option
150 bool valid = false;
151 char *mode;
152 size_t j;
153 for (j = 0; j < (size_t) (argc - i); ++j) {
154 mode = argv[i + j];
155 for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) {
156 if (strcasecmp(mode, bg_options[k]) == 0) {
157 valid = true;
158 break;
159 }
160 }
161 if (valid) {
162 break;
163 }
164 }
165 if (!valid) {
166 error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode.");
167 goto fail;
168 }
169
170 char *src = join_args(argv + i, j);
171 if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
172 error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
173 goto fail;
174 }
175 free(src);
176 src = p.we_wordv[0];
177 if (config->reading && *src != '/') {
178 char *conf = strdup(config->current_config);
179 if (conf) {
180 char *conf_path = dirname(conf);
181 src = malloc(strlen(conf_path) + strlen(src) + 2);
182 if (src) {
183 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
184 } else {
185 sway_log(L_ERROR, "Unable to allocate background source");
186 }
187 free(conf);
188 } else {
189 sway_log(L_ERROR, "Unable to allocate background source");
190 }
191 }
192 if (!src || access(src, F_OK) == -1) {
193 error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
194 wordfree(&p);
195 goto fail;
196 }
197
198 output->background = strdup(src);
199 output->background_option = strdup(mode);
200 if (src != p.we_wordv[0]) {
201 free(src);
202 }
203 wordfree(&p);
204
205 i += j;
206 }
207 }
208 }
209
210 i = list_seq_find(config->output_configs, output_name_cmp, name);
211 if (i >= 0) {
212 // merge existing config
213 struct output_config *oc = config->output_configs->items[i];
214 merge_output_config(oc, output);
215 free_output_config(output);
216 output = oc;
217 } else {
218 list_add(config->output_configs, output);
219 }
220
221 sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ "
222 "%d, %d scale %d transform %d refresh_rate %f) (bg %s %s)",
223 output->name, output->enabled, output->width,
224 output->height, output->x, output->y, output->scale,
225 output->transform, output->refresh_rate,
226 output->background, output->background_option);
227
228 if (output->name) {
229 // Try to find the output container and apply configuration now. If
230 // this is during startup then there will be no container and config
231 // will be applied during normal "new output" event from wlc.
232 swayc_t *cont = NULL;
233 for (int i = 0; i < root_container.children->length; ++i) {
234 cont = root_container.children->items[i];
235 if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) {
236 apply_output_config(output, cont);
237
238 if (strcmp(output->name, "*") != 0) {
239 // stop looking if the output config isn't applicable to all outputs
240 break;
241 }
242 }
243 }
244 }
245
246 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
247
248fail:
249 free_output_config(output);
250 return error;
251}
diff --git a/sway/config/output.c b/sway/config/output.c
new file mode 100644
index 00000000..4ed2c531
--- /dev/null
+++ b/sway/config/output.c
@@ -0,0 +1,181 @@
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
10int 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
17void output_config_defaults(struct output_config *oc) {
18 oc->enabled = -1;
19 oc->width = oc->height -1;
20 oc->refresh_rate = -1;
21 oc->x = oc->y = -1;
22 oc->scale = -1;
23 oc->transform = -1;
24}
25
26void merge_output_config(struct output_config *dst, struct output_config *src) {
27 if (src->name) {
28 if (dst->name) {
29 free(dst->name);
30 }
31 dst->name = strdup(src->name);
32 }
33 if (src->enabled != -1) {
34 dst->enabled = src->enabled;
35 }
36 if (src->width != -1) {
37 dst->width = src->width;
38 }
39 if (src->height != -1) {
40 dst->height = src->height;
41 }
42 if (src->x != -1) {
43 dst->x = src->x;
44 }
45 if (src->y != -1) {
46 dst->y = src->y;
47 }
48 if (src->scale != -1) {
49 dst->scale = src->scale;
50 }
51 if (src->refresh_rate != -1) {
52 dst->refresh_rate = src->refresh_rate;
53 }
54 if (src->transform != -1) {
55 dst->transform = src->transform;
56 }
57 if (src->background) {
58 if (dst->background) {
59 free(dst->background);
60 }
61 dst->background = strdup(src->background);
62 }
63 if (src->background_option) {
64 if (dst->background_option) {
65 free(dst->background_option);
66 }
67 dst->background_option = strdup(src->background_option);
68 }
69}
70
71static void set_mode(struct wlr_output *output, int width, int height,
72 float refresh_rate) {
73 struct wlr_output_mode *mode, *best = NULL;
74 int mhz = (int)(refresh_rate * 1000);
75 wl_list_for_each(mode, &output->modes, link) {
76 if (mode->width == width && mode->height == height) {
77 if (mode->refresh == mhz) {
78 best = mode;
79 break;
80 }
81 best = mode;
82 }
83 }
84 if (!best) {
85 sway_log(L_ERROR, "Configured mode for %s not available", output->name);
86 } else {
87 sway_log(L_DEBUG, "Assigning configured mode to %s", output->name);
88 wlr_output_set_mode(output, best);
89 }
90}
91
92void apply_output_config(struct output_config *oc, swayc_t *output) {
93 assert(output->type == C_OUTPUT);
94
95 if (oc && oc->enabled == 0) {
96 destroy_output(output);
97 return;
98 }
99
100 struct wlr_output *wlr_output = output->sway_output->wlr_output;
101 if (oc && oc->width > 0 && oc->height > 0) {
102 set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
103 }
104 if (oc && oc->scale > 0) {
105 sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale);
106 wlr_output->scale = oc->scale;
107 }
108 if (oc && oc->transform >= 0) {
109 sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
110 wlr_output_transform(wlr_output, oc->transform);
111 }
112
113 // Find position for it
114 if (oc && (oc->x != -1 || oc->y != -1)) {
115 sway_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y);
116 wlr_output_layout_add(root_container.output_layout, wlr_output, oc->x,
117 oc->y);
118 } else {
119 wlr_output_layout_add_auto(root_container.output_layout, wlr_output);
120 }
121 struct wlr_box *output_layout_box =
122 wlr_output_layout_get_box(root_container.output_layout, wlr_output);
123 output->x = output_layout_box->x;
124 output->y = output_layout_box->y;
125 output->width = output_layout_box->width;
126 output->height = output_layout_box->height;
127
128 if (!oc || !oc->background) {
129 // Look for a * config for background
130 int i = list_seq_find(config->output_configs, output_name_cmp, "*");
131 if (i >= 0) {
132 oc = config->output_configs->items[i];
133 } else {
134 oc = NULL;
135 }
136 }
137
138 int output_i;
139 for (output_i = 0; output_i < root_container.children->length; ++output_i) {
140 if (root_container.children->items[output_i] == output) {
141 break;
142 }
143 }
144
145 if (oc && oc->background) {
146 // TODO swaybg
147 /*if (output->bg_pid != 0) {
148 terminate_swaybg(output->bg_pid);
149 }
150
151 sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background);
152
153 size_t bufsize = 12;
154 char output_id[bufsize];
155 snprintf(output_id, bufsize, "%d", output_i);
156 output_id[bufsize-1] = 0;
157
158 char *const cmd[] = {
159 "swaybg",
160 output_id,
161 oc->background,
162 oc->background_option,
163 NULL,
164 };
165
166 output->bg_pid = fork();
167 if (output->bg_pid == 0) {
168 execvp(cmd[0], cmd);
169 }*/
170 }
171}
172
173void free_output_config(struct output_config *oc) {
174 if (!oc) {
175 return;
176 }
177 free(oc->name);
178 free(oc->background);
179 free(oc->background_option);
180 free(oc);
181}
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..ba305efa 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -2,7 +2,9 @@
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"
@@ -23,6 +25,30 @@ void swayc_descendants_of_type(swayc_t *root, enum swayc_types type,
23 } 25 }
24} 26}
25 27
28static void update_root_geometry() {
29 int width = 0;
30 int height = 0;
31 swayc_t *child;
32 int child_width;
33 int child_height;
34
35 for (int i = 0; i < root_container.children->length; ++i) {
36 child = root_container.children->items[i];
37 child_width = child->width + child->x;
38 child_height = child->height + child->y;
39 if (child_width > width) {
40 width = child_width;
41 }
42
43 if (child_height > height) {
44 height = child_height;
45 }
46 }
47
48 root_container.width = width;
49 root_container.height = height;
50}
51
26static swayc_t *new_swayc(enum swayc_types type) { 52static swayc_t *new_swayc(enum swayc_types type) {
27 // next id starts at 1 because 0 is assigned to root_container in layout.c 53 // next id starts at 1 because 0 is assigned to root_container in layout.c
28 static size_t next_id = 1; 54 static size_t next_id = 1;
@@ -44,19 +70,38 @@ static swayc_t *new_swayc(enum swayc_types type) {
44 70
45swayc_t *new_output(struct sway_output *sway_output) { 71swayc_t *new_output(struct sway_output *sway_output) {
46 struct wlr_box size; 72 struct wlr_box size;
47 wlr_output_effective_resolution( 73 wlr_output_effective_resolution(sway_output->wlr_output, &size.width,
48 sway_output->wlr_output, &size.width, &size.height); 74 &size.height);
49 const char *name = sway_output->wlr_output->name; 75 const char *name = sway_output->wlr_output->name;
50 76
77 struct output_config *oc = NULL, *all = NULL;
78 for (int i = 0; i < config->output_configs->length; ++i) {
79 struct output_config *cur = config->output_configs->items[i];
80 if (strcasecmp(name, cur->name) == 0) {
81 sway_log(L_DEBUG, "Matched output config for %s", name);
82 oc = cur;
83 }
84 if (strcasecmp("*", cur->name) == 0) {
85 sway_log(L_DEBUG, "Matched wildcard output config for %s", name);
86 all = cur;
87 }
88
89 if (oc && all) {
90 break;
91 }
92 }
93 if (!oc) {
94 oc = all;
95 }
96 if (oc && !oc->enabled) {
97 return NULL;
98 }
99
51 swayc_t *output = new_swayc(C_OUTPUT); 100 swayc_t *output = new_swayc(C_OUTPUT);
52 output->sway_output = sway_output; 101 output->sway_output = sway_output;
53 output->name = name ? strdup(name) : NULL; 102 output->name = name ? strdup(name) : NULL;
54 output->width = size.width;
55 output->height = size.width;
56 103
57 // TODO configure output layout position 104 apply_output_config(oc, output);
58 wlr_output_layout_add_auto(root_container.output_layout,
59 sway_output->wlr_output);
60 105
61 add_child(&root_container, output); 106 add_child(&root_container, output);
62 107
@@ -139,6 +184,34 @@ static void free_swayc(swayc_t *cont) {
139 free(cont); 184 free(cont);
140} 185}
141 186
187swayc_t *destroy_output(swayc_t *output) {
188 if (!sway_assert(output, "null output passed to destroy_output")) {
189 return NULL;
190 }
191 if (output->children->length > 0) {
192 // TODO save workspaces when there are no outputs.
193 // TODO also check if there will ever be no outputs except for exiting
194 // program
195 if (root_container.children->length > 1) {
196 int p = root_container.children->items[0] == output;
197 // Move workspace from this output to another output
198 while (output->children->length) {
199 swayc_t *child = output->children->items[0];
200 remove_child(child);
201 add_child(root_container.children->items[p], child);
202 }
203 sort_workspaces(root_container.children->items[p]);
204 // TODO WLR: is this needed anymore?
205 //update_visibility(root_container.children->items[p]);
206 arrange_windows(root_container.children->items[p], -1, -1);
207 }
208 }
209 sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name);
210 free_swayc(output);
211 update_root_geometry();
212 return &root_container;
213}
214
142swayc_t *destroy_view(swayc_t *view) { 215swayc_t *destroy_view(swayc_t *view) {
143 if (!sway_assert(view, "null view passed to destroy_view")) { 216 if (!sway_assert(view, "null view passed to destroy_view")) {
144 return NULL; 217 return NULL;