diff options
author | emersion <contact@emersion.fr> | 2017-12-06 12:36:06 +0100 |
---|---|---|
committer | emersion <contact@emersion.fr> | 2017-12-06 12:36:06 +0100 |
commit | aaae59026ff3751190b93277ac6d7566e373c892 (patch) | |
tree | 2dc365f9ceeb6a08a98e08d2f79fcc940624b199 /sway/commands/output.c | |
parent | Merge pull request #1498 from emersion/config (diff) | |
download | sway-aaae59026ff3751190b93277ac6d7566e373c892.tar.gz sway-aaae59026ff3751190b93277ac6d7566e373c892.tar.zst sway-aaae59026ff3751190b93277ac6d7566e373c892.zip |
Add output config
Diffstat (limited to 'sway/commands/output.c')
-rw-r--r-- | sway/commands/output.c | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/sway/commands/output.c b/sway/commands/output.c new file mode 100644 index 00000000..c964bef7 --- /dev/null +++ b/sway/commands/output.c | |||
@@ -0,0 +1,253 @@ | |||
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 = 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->x = output->y = output->width = output->height = -1; | ||
35 | output->name = strdup(name); | ||
36 | output->enabled = -1; | ||
37 | output->scale = 1; | ||
38 | |||
39 | // TODO: atoi doesn't handle invalid numbers | ||
40 | |||
41 | int i; | ||
42 | for (i = 1; i < argc; ++i) { | ||
43 | const char *command = argv[i]; | ||
44 | |||
45 | if (strcasecmp(command, "disable") == 0) { | ||
46 | output->enabled = 0; | ||
47 | } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { | ||
48 | if (++i >= argc) { | ||
49 | error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument."); | ||
50 | goto fail; | ||
51 | } | ||
52 | char *res = argv[i]; | ||
53 | char *x = strchr(res, 'x'); | ||
54 | int width = -1, height = -1; | ||
55 | if (x != NULL) { | ||
56 | // Format is 1234x4321 | ||
57 | *x = '\0'; | ||
58 | width = atoi(res); | ||
59 | height = atoi(x + 1); | ||
60 | *x = 'x'; | ||
61 | } else { | ||
62 | // Format is 1234 4321 | ||
63 | width = atoi(res); | ||
64 | if (++i >= argc) { | ||
65 | error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height)."); | ||
66 | goto fail; | ||
67 | } | ||
68 | res = argv[i]; | ||
69 | height = atoi(res); | ||
70 | } | ||
71 | output->width = width; | ||
72 | output->height = height; | ||
73 | } else if (strcasecmp(command, "refresh_rate") == 0) { | ||
74 | if (++i >= argc) { | ||
75 | error = cmd_results_new(CMD_INVALID, "output", "Missing refresh_rate argument."); | ||
76 | goto fail; | ||
77 | } | ||
78 | output->refresh_rate = atof(argv[i]); | ||
79 | } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { | ||
80 | if (++i >= argc) { | ||
81 | error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); | ||
82 | goto fail; | ||
83 | } | ||
84 | char *res = argv[i]; | ||
85 | char *c = strchr(res, ','); | ||
86 | int x = -1, y = -1; | ||
87 | if (c != NULL) { | ||
88 | // Format is 1234,4321 | ||
89 | *c = '\0'; | ||
90 | x = atoi(res); | ||
91 | y = atoi(c + 1); | ||
92 | *c = ','; | ||
93 | } else { | ||
94 | // Format is 1234 4321 | ||
95 | x = atoi(res); | ||
96 | if (++i >= argc) { | ||
97 | error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); | ||
98 | goto fail; | ||
99 | } | ||
100 | res = argv[i]; | ||
101 | y = atoi(res); | ||
102 | } | ||
103 | output->x = x; | ||
104 | output->y = y; | ||
105 | } else if (strcasecmp(command, "scale") == 0) { | ||
106 | if (++i >= argc) { | ||
107 | error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); | ||
108 | goto fail; | ||
109 | } | ||
110 | output->scale = atoi(argv[i]); | ||
111 | } else if (strcasecmp(command, "transform") == 0) { | ||
112 | if (++i >= argc) { | ||
113 | error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter."); | ||
114 | goto fail; | ||
115 | } | ||
116 | char *value = argv[i]; | ||
117 | if (strcmp(value, "normal") == 0) { | ||
118 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; | ||
119 | } else if (strcmp(value, "90") == 0) { | ||
120 | output->transform = WL_OUTPUT_TRANSFORM_90; | ||
121 | } else if (strcmp(value, "180") == 0) { | ||
122 | output->transform = WL_OUTPUT_TRANSFORM_180; | ||
123 | } else if (strcmp(value, "270") == 0) { | ||
124 | output->transform = WL_OUTPUT_TRANSFORM_270; | ||
125 | } else if (strcmp(value, "flipped") == 0) { | ||
126 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; | ||
127 | } else if (strcmp(value, "flipped-90") == 0) { | ||
128 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; | ||
129 | } else if (strcmp(value, "flipped-180") == 0) { | ||
130 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; | ||
131 | } else if (strcmp(value, "flipped-270") == 0) { | ||
132 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; | ||
133 | } else { | ||
134 | error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform."); | ||
135 | goto fail; | ||
136 | } | ||
137 | } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { | ||
138 | wordexp_t p; | ||
139 | if (++i >= argc) { | ||
140 | error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); | ||
141 | goto fail; | ||
142 | } | ||
143 | if (i + 1 >= argc) { | ||
144 | error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); | ||
145 | goto fail; | ||
146 | } | ||
147 | if (strcasecmp(argv[i + 1], "solid_color") == 0) { | ||
148 | output->background = strdup(argv[argc - 2]); | ||
149 | output->background_option = strdup("solid_color"); | ||
150 | } else { | ||
151 | // argv[i+j]=bg_option | ||
152 | bool valid = false; | ||
153 | char *mode; | ||
154 | size_t j; | ||
155 | for (j = 0; j < (size_t) (argc - i); ++j) { | ||
156 | mode = argv[i + j]; | ||
157 | for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) { | ||
158 | if (strcasecmp(mode, bg_options[k]) == 0) { | ||
159 | valid = true; | ||
160 | break; | ||
161 | } | ||
162 | } | ||
163 | if (valid) { | ||
164 | break; | ||
165 | } | ||
166 | } | ||
167 | if (!valid) { | ||
168 | error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode."); | ||
169 | goto fail; | ||
170 | } | ||
171 | |||
172 | char *src = join_args(argv + i, j); | ||
173 | if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { | ||
174 | error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src); | ||
175 | goto fail; | ||
176 | } | ||
177 | free(src); | ||
178 | src = p.we_wordv[0]; | ||
179 | if (config->reading && *src != '/') { | ||
180 | char *conf = strdup(config->current_config); | ||
181 | if (conf) { | ||
182 | char *conf_path = dirname(conf); | ||
183 | src = malloc(strlen(conf_path) + strlen(src) + 2); | ||
184 | if (src) { | ||
185 | sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); | ||
186 | } else { | ||
187 | sway_log(L_ERROR, "Unable to allocate background source"); | ||
188 | } | ||
189 | free(conf); | ||
190 | } else { | ||
191 | sway_log(L_ERROR, "Unable to allocate background source"); | ||
192 | } | ||
193 | } | ||
194 | if (!src || access(src, F_OK) == -1) { | ||
195 | error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); | ||
196 | wordfree(&p); | ||
197 | goto fail; | ||
198 | } | ||
199 | |||
200 | output->background = strdup(src); | ||
201 | output->background_option = strdup(mode); | ||
202 | if (src != p.we_wordv[0]) { | ||
203 | free(src); | ||
204 | } | ||
205 | wordfree(&p); | ||
206 | |||
207 | i += j; | ||
208 | } | ||
209 | } | ||
210 | } | ||
211 | |||
212 | i = list_seq_find(config->output_configs, output_name_cmp, name); | ||
213 | if (i >= 0) { | ||
214 | // merge existing config | ||
215 | struct output_config *oc = config->output_configs->items[i]; | ||
216 | merge_output_config(oc, output); | ||
217 | free_output_config(output); | ||
218 | output = oc; | ||
219 | } else { | ||
220 | list_add(config->output_configs, output); | ||
221 | } | ||
222 | |||
223 | sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ " | ||
224 | "%d, %d scale %d transform %d refresh_rate %f) (bg %s %s)", | ||
225 | output->name, output->enabled, output->width, | ||
226 | output->height, output->x, output->y, output->scale, | ||
227 | output->transform, output->refresh_rate, | ||
228 | output->background, output->background_option); | ||
229 | |||
230 | if (output->name) { | ||
231 | // Try to find the output container and apply configuration now. If | ||
232 | // this is during startup then there will be no container and config | ||
233 | // will be applied during normal "new output" event from wlc. | ||
234 | swayc_t *cont = NULL; | ||
235 | for (int i = 0; i < root_container.children->length; ++i) { | ||
236 | cont = root_container.children->items[i]; | ||
237 | if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { | ||
238 | apply_output_config(output, cont); | ||
239 | |||
240 | if (strcmp(output->name, "*") != 0) { | ||
241 | // stop looking if the output config isn't applicable to all outputs | ||
242 | break; | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | } | ||
247 | |||
248 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
249 | |||
250 | fail: | ||
251 | free_output_config(output); | ||
252 | return error; | ||
253 | } | ||