aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/output.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-11-18 11:22:02 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2017-11-18 11:22:02 -0500
commit733993a651c71f7e2198d505960d6bbd31e0e107 (patch)
treee51732c5872b624e73355f9e5b3f762101f3cd0d /sway/commands/output.c
parentInitial (awful) pass on xdg shell support (diff)
downloadsway-733993a651c71f7e2198d505960d6bbd31e0e107.tar.gz
sway-733993a651c71f7e2198d505960d6bbd31e0e107.tar.zst
sway-733993a651c71f7e2198d505960d6bbd31e0e107.zip
Move everything to sway/old/
Diffstat (limited to 'sway/commands/output.c')
-rw-r--r--sway/commands/output.c219
1 files changed, 0 insertions, 219 deletions
diff --git a/sway/commands/output.c b/sway/commands/output.c
deleted file mode 100644
index 911391d2..00000000
--- a/sway/commands/output.c
+++ /dev/null
@@ -1,219 +0,0 @@
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->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, "position") == 0 || strcasecmp(command, "pos") == 0) {
74 if (++i >= argc) {
75 error = cmd_results_new(CMD_INVALID, "output", "Missing position argument.");
76 goto fail;
77 }
78 char *res = argv[i];
79 char *c = strchr(res, ',');
80 int x = -1, y = -1;
81 if (c != NULL) {
82 // Format is 1234,4321
83 *c = '\0';
84 x = atoi(res);
85 y = atoi(c + 1);
86 *c = ',';
87 } else {
88 // Format is 1234 4321
89 x = atoi(res);
90 if (++i >= argc) {
91 error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y).");
92 goto fail;
93 }
94 res = argv[i];
95 y = atoi(res);
96 }
97 output->x = x;
98 output->y = y;
99 } else if (strcasecmp(command, "scale") == 0) {
100 if (++i >= argc) {
101 error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter.");
102 goto fail;
103 }
104 output->scale = atoi(argv[i]);
105 } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) {
106 wordexp_t p;
107 if (++i >= argc) {
108 error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification.");
109 goto fail;
110 }
111 if (i + 1 >= argc) {
112 error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`.");
113 goto fail;
114 }
115 if (strcasecmp(argv[i + 1], "solid_color") == 0) {
116 output->background = strdup(argv[argc - 2]);
117 output->background_option = strdup("solid_color");
118 } else {
119 // argv[i+j]=bg_option
120 bool valid = false;
121 char *mode;
122 size_t j;
123 for (j = 0; j < (size_t) (argc - i); ++j) {
124 mode = argv[i + j];
125 for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) {
126 if (strcasecmp(mode, bg_options[k]) == 0) {
127 valid = true;
128 break;
129 }
130 }
131 if (valid) {
132 break;
133 }
134 }
135 if (!valid) {
136 error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode.");
137 goto fail;
138 }
139
140 char *src = join_args(argv + i, j);
141 if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
142 error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
143 goto fail;
144 }
145 free(src);
146 src = p.we_wordv[0];
147 if (config->reading && *src != '/') {
148 char *conf = strdup(config->current_config);
149 if (conf) {
150 char *conf_path = dirname(conf);
151 src = malloc(strlen(conf_path) + strlen(src) + 2);
152 if (src) {
153 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
154 } else {
155 sway_log(L_ERROR, "Unable to allocate background source");
156 }
157 free(conf);
158 } else {
159 sway_log(L_ERROR, "Unable to allocate background source");
160 }
161 }
162 if (!src || access(src, F_OK) == -1) {
163 error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
164 wordfree(&p);
165 goto fail;
166 }
167
168 output->background = strdup(src);
169 output->background_option = strdup(mode);
170 if (src != p.we_wordv[0]) {
171 free(src);
172 }
173 wordfree(&p);
174
175 i += j;
176 }
177 }
178 }
179
180 i = list_seq_find(config->output_configs, output_name_cmp, name);
181 if (i >= 0) {
182 // merge existing config
183 struct output_config *oc = config->output_configs->items[i];
184 merge_output_config(oc, output);
185 free_output_config(output);
186 output = oc;
187 } else {
188 list_add(config->output_configs, output);
189 }
190
191 sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d scale %d) (bg %s %s)",
192 output->name, output->enabled, output->width,
193 output->height, output->x, output->y, output->scale,
194 output->background, output->background_option);
195
196 if (output->name) {
197 // Try to find the output container and apply configuration now. If
198 // this is during startup then there will be no container and config
199 // will be applied during normal "new output" event from wlc.
200 swayc_t *cont = NULL;
201 for (int i = 0; i < root_container.children->length; ++i) {
202 cont = root_container.children->items[i];
203 if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) {
204 apply_output_config(output, cont);
205
206 if (strcmp(output->name, "*") != 0) {
207 // stop looking if the output config isn't applicable to all outputs
208 break;
209 }
210 }
211 }
212 }
213
214 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
215
216fail:
217 free_output_config(output);
218 return error;
219}