aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/output.c
diff options
context:
space:
mode:
authorLibravatar emersion <contact@emersion.fr>2017-12-14 02:23:33 +0100
committerLibravatar emersion <contact@emersion.fr>2017-12-14 02:23:33 +0100
commitcba592b3d2c516cab9f4e8325037dfdf6e30cb9a (patch)
tree6a3479fef295dc673c6bcdaac822f44523f94d8e /sway/commands/output.c
parentFail if unknown output subcommand (diff)
downloadsway-cba592b3d2c516cab9f4e8325037dfdf6e30cb9a.tar.gz
sway-cba592b3d2c516cab9f4e8325037dfdf6e30cb9a.tar.zst
sway-cba592b3d2c516cab9f4e8325037dfdf6e30cb9a.zip
Use strtol instead of atoi in output command
Diffstat (limited to 'sway/commands/output.c')
-rw-r--r--sway/commands/output.c114
1 files changed, 74 insertions, 40 deletions
diff --git a/sway/commands/output.c b/sway/commands/output.c
index 16b711f1..d71e4d8d 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -34,8 +34,6 @@ struct cmd_results *cmd_output(int argc, char **argv) {
34 } 34 }
35 output->name = strdup(name); 35 output->name = strdup(name);
36 36
37 // TODO: atoi doesn't handle invalid numbers
38
39 int i; 37 int i;
40 for (i = 1; i < argc; ++i) { 38 for (i = 1; i < argc; ++i) {
41 const char *command = argv[i]; 39 const char *command = argv[i];
@@ -80,9 +78,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
80 } 78 }
81 } 79 }
82 } else { 80 } else {
83 // Format is 1234 4321 (legacy) 81 // Format is 1234 4321
84 ++i; 82 if (++i >= argc) {
85 if (i >= argc) {
86 error = cmd_results_new(CMD_INVALID, "output", 83 error = cmd_results_new(CMD_INVALID, "output",
87 "Missing mode argument (height)."); 84 "Missing mode argument (height).");
88 goto fail; 85 goto fail;
@@ -97,41 +94,66 @@ struct cmd_results *cmd_output(int argc, char **argv) {
97 output->width = width; 94 output->width = width;
98 output->height = height; 95 output->height = height;
99 output->refresh_rate = refresh_rate; 96 output->refresh_rate = refresh_rate;
100 } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { 97 } else if (strcasecmp(command, "position") == 0 ||
98 strcasecmp(command, "pos") == 0) {
101 if (++i >= argc) { 99 if (++i >= argc) {
102 error = cmd_results_new(CMD_INVALID, "output", "Missing position argument."); 100 error = cmd_results_new(CMD_INVALID, "output",
101 "Missing position argument.");
103 goto fail; 102 goto fail;
104 } 103 }
105 char *pos = argv[i]; 104
106 char *c = strchr(pos, ',');
107 int x = -1, y = -1; 105 int x = -1, y = -1;
108 if (c != NULL) { 106
107 char *end;
108 x = strtol(argv[i], &end, 10);
109 if (*end) {
109 // Format is 1234,4321 110 // Format is 1234,4321
110 *c = '\0'; 111 if (*end != ',') {
111 x = atoi(pos); 112 error = cmd_results_new(CMD_INVALID, "output",
112 y = atoi(c + 1); 113 "Invalid position x.");
113 *c = ','; 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 }
114 } else { 123 } else {
115 // Format is 1234 4321 124 // Format is 1234 4321 (legacy)
116 x = atoi(pos);
117 if (++i >= argc) { 125 if (++i >= argc) {
118 error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); 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.");
119 goto fail; 134 goto fail;
120 } 135 }
121 pos = argv[i];
122 y = atoi(pos);
123 } 136 }
137
124 output->x = x; 138 output->x = x;
125 output->y = y; 139 output->y = y;
126 } else if (strcasecmp(command, "scale") == 0) { 140 } else if (strcasecmp(command, "scale") == 0) {
127 if (++i >= argc) { 141 if (++i >= argc) {
128 error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); 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.");
129 goto fail; 151 goto fail;
130 } 152 }
131 output->scale = atoi(argv[i]);
132 } else if (strcasecmp(command, "transform") == 0) { 153 } else if (strcasecmp(command, "transform") == 0) {
133 if (++i >= argc) { 154 if (++i >= argc) {
134 error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter."); 155 error = cmd_results_new(CMD_INVALID, "output",
156 "Missing transform parameter.");
135 goto fail; 157 goto fail;
136 } 158 }
137 char *value = argv[i]; 159 char *value = argv[i];
@@ -152,17 +174,21 @@ struct cmd_results *cmd_output(int argc, char **argv) {
152 } else if (strcmp(value, "flipped-270") == 0) { 174 } else if (strcmp(value, "flipped-270") == 0) {
153 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; 175 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
154 } else { 176 } else {
155 error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform."); 177 error = cmd_results_new(CMD_INVALID, "output",
178 "Invalid output transform.");
156 goto fail; 179 goto fail;
157 } 180 }
158 } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { 181 } else if (strcasecmp(command, "background") == 0 ||
182 strcasecmp(command, "bg") == 0) {
159 wordexp_t p; 183 wordexp_t p;
160 if (++i >= argc) { 184 if (++i >= argc) {
161 error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); 185 error = cmd_results_new(CMD_INVALID, "output",
186 "Missing background file or color specification.");
162 goto fail; 187 goto fail;
163 } 188 }
164 if (i + 1 >= argc) { 189 if (i + 1 >= argc) {
165 error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); 190 error = cmd_results_new(CMD_INVALID, "output",
191 "Missing background scaling mode or `solid_color`.");
166 goto fail; 192 goto fail;
167 } 193 }
168 if (strcasecmp(argv[i + 1], "solid_color") == 0) { 194 if (strcasecmp(argv[i + 1], "solid_color") == 0) {
@@ -175,7 +201,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
175 size_t j; 201 size_t j;
176 for (j = 0; j < (size_t) (argc - i); ++j) { 202 for (j = 0; j < (size_t) (argc - i); ++j) {
177 mode = argv[i + j]; 203 mode = argv[i + j];
178 for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) { 204 size_t n = sizeof(bg_options) / sizeof(char *);
205 for (size_t k = 0; k < n; ++k) {
179 if (strcasecmp(mode, bg_options[k]) == 0) { 206 if (strcasecmp(mode, bg_options[k]) == 0) {
180 valid = true; 207 valid = true;
181 break; 208 break;
@@ -186,13 +213,15 @@ struct cmd_results *cmd_output(int argc, char **argv) {
186 } 213 }
187 } 214 }
188 if (!valid) { 215 if (!valid) {
189 error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode."); 216 error = cmd_results_new(CMD_INVALID, "output",
217 "Missing background scaling mode.");
190 goto fail; 218 goto fail;
191 } 219 }
192 220
193 char *src = join_args(argv + i, j); 221 char *src = join_args(argv + i, j);
194 if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { 222 if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
195 error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s).", src); 223 error = cmd_results_new(CMD_INVALID, "output",
224 "Invalid syntax (%s).", src);
196 goto fail; 225 goto fail;
197 } 226 }
198 free(src); 227 free(src);
@@ -205,15 +234,18 @@ struct cmd_results *cmd_output(int argc, char **argv) {
205 if (src) { 234 if (src) {
206 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); 235 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
207 } else { 236 } else {
208 sway_log(L_ERROR, "Unable to allocate background source"); 237 sway_log(L_ERROR,
238 "Unable to allocate background source");
209 } 239 }
210 free(conf); 240 free(conf);
211 } else { 241 } else {
212 sway_log(L_ERROR, "Unable to allocate background source"); 242 sway_log(L_ERROR,
243 "Unable to allocate background source");
213 } 244 }
214 } 245 }
215 if (!src || access(src, F_OK) == -1) { 246 if (!src || access(src, F_OK) == -1) {
216 error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s).", src); 247 error = cmd_results_new(CMD_INVALID, "output",
248 "Background file unreadable (%s).", src);
217 wordfree(&p); 249 wordfree(&p);
218 goto fail; 250 goto fail;
219 } 251 }
@@ -228,7 +260,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
228 i += j; 260 i += j;
229 } 261 }
230 } else { 262 } else {
231 error = cmd_results_new(CMD_INVALID, "output", "Invalid output subcommand: %s.", command); 263 error = cmd_results_new(CMD_INVALID, "output",
264 "Invalid output subcommand: %s.", command);
232 goto fail; 265 goto fail;
233 } 266 }
234 } 267 }
@@ -245,11 +278,10 @@ struct cmd_results *cmd_output(int argc, char **argv) {
245 } 278 }
246 279
247 sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz " 280 sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz "
248 "position %d,%d scale %d transform %d) (bg %s %s)", 281 "position %d,%d scale %d transform %d) (bg %s %s)",
249 output->name, output->enabled, output->width, 282 output->name, output->enabled, output->width, output->height,
250 output->height, output->refresh_rate, output->x, output->y, 283 output->refresh_rate, output->x, output->y, output->scale,
251 output->scale, output->transform, 284 output->transform, output->background, output->background_option);
252 output->background, output->background_option);
253 285
254 if (output->name) { 286 if (output->name) {
255 // Try to find the output container and apply configuration now. If 287 // Try to find the output container and apply configuration now. If
@@ -258,11 +290,13 @@ struct cmd_results *cmd_output(int argc, char **argv) {
258 swayc_t *cont = NULL; 290 swayc_t *cont = NULL;
259 for (int i = 0; i < root_container.children->length; ++i) { 291 for (int i = 0; i < root_container.children->length; ++i) {
260 cont = root_container.children->items[i]; 292 cont = root_container.children->items[i];
261 if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { 293 if (cont->name && ((strcmp(cont->name, output->name) == 0) ||
294 (strcmp(output->name, "*") == 0))) {
262 apply_output_config(output, cont); 295 apply_output_config(output, cont);
263 296
264 if (strcmp(output->name, "*") != 0) { 297 if (strcmp(output->name, "*") != 0) {
265 // stop looking if the output config isn't applicable to all outputs 298 // Stop looking if the output config isn't applicable to all
299 // outputs
266 break; 300 break;
267 } 301 }
268 } 302 }