summaryrefslogtreecommitdiffstats
path: root/sway/commands/output.c
blob: 7988e3e404c68118004f2038c8b814073afe73f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#define _XOPEN_SOURCE 500
#include <ctype.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <wordexp.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "list.h"
#include "log.h"
#include "stringop.h"

static char *bg_options[] = {
	"stretch",
	"center",
	"fill",
	"fit",
	"tile",
};

struct cmd_results *cmd_output(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) {
		return error;
	}
	const char *name = argv[0];

	struct output_config *output = new_output_config();
	if (!output) {
		sway_log(L_ERROR, "Failed to allocate output config");
		return NULL;
	}
	output->name = strdup(name);

	int i;
	for (i = 1; i < argc; ++i) {
		const char *command = argv[i];

		if (strcasecmp(command, "disable") == 0) {
			output->enabled = 0;
		} else if (strcasecmp(command, "mode") == 0 ||
				strcasecmp(command, "resolution") == 0 ||
				strcasecmp(command, "res") == 0) {
			if (++i >= argc) {
				error = cmd_results_new(CMD_INVALID, "output",
					"Missing mode argument.");
				goto fail;
			}

			int width = -1, height = -1;
			float refresh_rate = -1;

			char *end;
			width = strtol(argv[i], &end, 10);
			if (*end) {
				// Format is 1234x4321
				if (*end != 'x') {
					error = cmd_results_new(CMD_INVALID, "output",
						"Invalid mode width.");
					goto fail;
				}
				++end;
				height = strtol(end, &end, 10);
				if (*end) {
					if (*end != '@') {
						error = cmd_results_new(CMD_INVALID, "output",
							"Invalid mode height.");
						goto fail;
					}
					++end;
					refresh_rate = strtof(end, &end);
					if (strcasecmp("Hz", end) != 0) {
						error = cmd_results_new(CMD_INVALID, "output",
							"Invalid mode refresh rate.");
						goto fail;
					}
				}
			} else {
				// Format is 1234 4321
				if (++i >= argc) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Missing mode argument (height).");
					goto fail;
				}
				height = strtol(argv[i], &end, 10);
				if (*end) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Invalid mode height.");
					goto fail;
				}
			}
			output->width = width;
			output->height = height;
			output->refresh_rate = refresh_rate;
		} else if (strcasecmp(command, "position") == 0 ||
				strcasecmp(command, "pos") == 0) {
			if (++i >= argc) {
				error = cmd_results_new(CMD_INVALID, "output",
					"Missing position argument.");
				goto fail;
			}

			int x = -1, y = -1;

			char *end;
			x = strtol(argv[i], &end, 10);
			if (*end) {
				// Format is 1234,4321
				if (*end != ',') {
					error = cmd_results_new(CMD_INVALID, "output",
						"Invalid position x.");
					goto fail;
				}
				++end;
				y = strtol(end, &end, 10);
				if (*end) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Invalid position y.");
					goto fail;
				}
			} else {
				// Format is 1234 4321 (legacy)
				if (++i >= argc) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Missing position argument (y).");
					goto fail;
				}
				y = strtol(argv[i], &end, 10);
				if (*end) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Invalid position y.");
					goto fail;
				}
			}

			output->x = x;
			output->y = y;
		} else if (strcasecmp(command, "scale") == 0) {
			if (++i >= argc) {
				error = cmd_results_new(CMD_INVALID, "output",
					"Missing scale parameter.");
				goto fail;
			}
			char *end;
			output->scale = strtof(argv[i], &end);
			if (*end) {
				error = cmd_results_new(CMD_INVALID, "output",
					"Invalid scale.");
				goto fail;
			}
		} else if (strcasecmp(command, "transform") == 0) {
			if (++i >= argc) {
				error = cmd_results_new(CMD_INVALID, "output",
					"Missing transform parameter.");
				goto fail;
			}
			char *value = argv[i];
			if (strcmp(value, "normal") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
			} else if (strcmp(value, "90") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_90;
			} else if (strcmp(value, "180") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_180;
			} else if (strcmp(value, "270") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_270;
			} else if (strcmp(value, "flipped") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
			} else if (strcmp(value, "flipped-90") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
			} else if (strcmp(value, "flipped-180") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
			} else if (strcmp(value, "flipped-270") == 0) {
				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
			} else {
				error = cmd_results_new(CMD_INVALID, "output",
					"Invalid output transform.");
				goto fail;
			}
		} else if (strcasecmp(command, "background") == 0 ||
				strcasecmp(command, "bg") == 0) {
			wordexp_t p;
			if (++i >= argc) {
				error = cmd_results_new(CMD_INVALID, "output",
					"Missing background file or color specification.");
				goto fail;
			}
			if (i + 1 >= argc) {
				error = cmd_results_new(CMD_INVALID, "output",
					"Missing background scaling mode or `solid_color`.");
				goto fail;
			}
			if (strcasecmp(argv[i + 1], "solid_color") == 0) {
				output->background = strdup(argv[argc - 2]);
				output->background_option = strdup("solid_color");
			} else {
				// argv[i+j]=bg_option
				bool valid = false;
				char *mode;
				size_t j;
				for (j = 0; j < (size_t) (argc - i); ++j) {
					mode = argv[i + j];
					size_t n = sizeof(bg_options) / sizeof(char *);
					for (size_t k = 0; k < n; ++k) {
						if (strcasecmp(mode, bg_options[k]) == 0) {
							valid = true;
							break;
						}
					}
					if (valid) {
						break;
					}
				}
				if (!valid) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Missing background scaling mode.");
					goto fail;
				}

				char *src = join_args(argv + i, j);
				if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Invalid syntax (%s).", src);
					goto fail;
				}
				free(src);
				src = p.we_wordv[0];
				if (config->reading && *src != '/') {
					char *conf = strdup(config->current_config);
					if (conf) {
						char *conf_path = dirname(conf);
						src = malloc(strlen(conf_path) + strlen(src) + 2);
						if (src) {
							sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
						} else {
							sway_log(L_ERROR,
								"Unable to allocate background source");
						}
						free(conf);
					} else {
						sway_log(L_ERROR,
							"Unable to allocate background source");
					}
				}
				if (!src || access(src, F_OK) == -1) {
					error = cmd_results_new(CMD_INVALID, "output",
						"Background file unreadable (%s).", src);
					wordfree(&p);
					goto fail;
				}

				output->background = strdup(src);
				output->background_option = strdup(mode);
				if (src != p.we_wordv[0]) {
					free(src);
				}
				wordfree(&p);

				i += j;
			}
		} else {
			error = cmd_results_new(CMD_INVALID, "output",
				"Invalid output subcommand: %s.", command);
			goto fail;
		}
	}

	i = list_seq_find(config->output_configs, output_name_cmp, name);
	if (i >= 0) {
		// merge existing config
		struct output_config *oc = config->output_configs->items[i];
		merge_output_config(oc, output);
		free_output_config(output);
		output = oc;
	} else {
		list_add(config->output_configs, output);
	}

	sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz "
		"position %d,%d scale %f transform %d) (bg %s %s)",
		output->name, output->enabled, output->width, output->height,
		output->refresh_rate, output->x, output->y, output->scale,
		output->transform, output->background, output->background_option);

	if (output->name) {
		// Try to find the output container and apply configuration now. If
		// this is during startup then there will be no container and config
		// will be applied during normal "new output" event from wlroots.
		swayc_t *cont = NULL;
		for (int i = 0; i < root_container.children->length; ++i) {
			cont = root_container.children->items[i];
			if (cont->name && ((strcmp(cont->name, output->name) == 0) ||
					(strcmp(output->name, "*") == 0))) {
				apply_output_config(output, cont);

				if (strcmp(output->name, "*") != 0) {
					// Stop looking if the output config isn't applicable to all
					// outputs
					break;
				}
			}
		}
	}

	return cmd_results_new(CMD_SUCCESS, NULL, NULL);

fail:
	free_output_config(output);
	return error;
}