aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/output
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-06-02 21:33:16 -0400
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2018-06-03 10:26:06 -0400
commit5ea4a4d3eec897b915003ad7fc2b2e274d1a59c1 (patch)
tree8d077f70c7bc462f1987e8d57003a2b11de718ca /sway/commands/output
parentMerge pull request #2093 from emersion/damage-debug (diff)
downloadsway-5ea4a4d3eec897b915003ad7fc2b2e274d1a59c1.tar.gz
sway-5ea4a4d3eec897b915003ad7fc2b2e274d1a59c1.tar.zst
sway-5ea4a4d3eec897b915003ad7fc2b2e274d1a59c1.zip
Refactor cmd_output to use config_subcommand
Diffstat (limited to 'sway/commands/output')
-rw-r--r--sway/commands/output/background.c105
-rw-r--r--sway/commands/output/disable.c13
-rw-r--r--sway/commands/output/dpms.c24
-rw-r--r--sway/commands/output/enable.c14
-rw-r--r--sway/commands/output/mode.c55
-rw-r--r--sway/commands/output/position.c46
-rw-r--r--sway/commands/output/scale.c23
-rw-r--r--sway/commands/output/transform.c39
8 files changed, 319 insertions, 0 deletions
diff --git a/sway/commands/output/background.c b/sway/commands/output/background.c
new file mode 100644
index 00000000..f039c9c9
--- /dev/null
+++ b/sway/commands/output/background.c
@@ -0,0 +1,105 @@
1#define _XOPEN_SOURCE 500
2#include <libgen.h>
3#include <strings.h>
4#include <unistd.h>
5#include <wordexp.h>
6#include "sway/commands.h"
7#include "sway/config.h"
8#include "log.h"
9#include "stringop.h"
10
11static char *bg_options[] = {
12 "stretch",
13 "center",
14 "fill",
15 "fit",
16 "tile",
17};
18
19struct cmd_results *output_cmd_background(int argc, char **argv) {
20 if (!config->handler_context.output_config) {
21 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
22 }
23 if (!argc) {
24 return cmd_results_new(CMD_INVALID, "output",
25 "Missing background file or color specification.");
26 }
27 if (argc < 2) {
28 return cmd_results_new(CMD_INVALID, "output",
29 "Missing background scaling mode or `solid_color`.");
30 }
31
32 struct output_config *output = config->handler_context.output_config;
33
34 if (strcasecmp(argv[1], "solid_color") == 0) {
35 output->background = calloc(1, strlen(argv[0]) + 3);
36 snprintf(output->background, strlen(argv[0]) + 3, "\"%s\"", argv[0]);
37 output->background_option = strdup("solid_color");
38 argc -= 2; argv += 2;
39 } else {
40 bool valid = false;
41 char *mode;
42 size_t j;
43 for (j = 0; j < (size_t)argc; ++j) {
44 mode = argv[j];
45 size_t n = sizeof(bg_options) / sizeof(char *);
46 for (size_t k = 0; k < n; ++k) {
47 if (strcasecmp(mode, bg_options[k]) == 0) {
48 valid = true;
49 break;
50 }
51 }
52 if (valid) {
53 break;
54 }
55 }
56 if (!valid) {
57 return cmd_results_new(CMD_INVALID, "output",
58 "Missing background scaling mode.");
59 }
60
61 wordexp_t p;
62 char *src = join_args(argv, j);
63 if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
64 return cmd_results_new(CMD_INVALID, "output",
65 "Invalid syntax (%s).", src);
66 }
67 free(src);
68 src = p.we_wordv[0];
69 if (config->reading && *src != '/') {
70 char *conf = strdup(config->current_config);
71 if (conf) {
72 char *conf_path = dirname(conf);
73 src = malloc(strlen(conf_path) + strlen(src) + 2);
74 if (src) {
75 sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
76 } else {
77 wlr_log(L_ERROR,
78 "Unable to allocate background source");
79 }
80 free(conf);
81 } else {
82 wlr_log(L_ERROR, "Unable to allocate background source");
83 }
84 }
85 if (!src || access(src, F_OK) == -1) {
86 wordfree(&p);
87 return cmd_results_new(CMD_INVALID, "output",
88 "Background file unreadable (%s).", src);
89 }
90
91 output->background = strdup(src);
92 output->background_option = strdup(mode);
93 if (src != p.we_wordv[0]) {
94 free(src);
95 }
96 wordfree(&p);
97
98 argc -= j + 1; argv += j + 1;
99 }
100
101 config->handler_context.leftovers.argc = argc;
102 config->handler_context.leftovers.argv = argv;
103 return NULL;
104}
105
diff --git a/sway/commands/output/disable.c b/sway/commands/output/disable.c
new file mode 100644
index 00000000..65517c49
--- /dev/null
+++ b/sway/commands/output/disable.c
@@ -0,0 +1,13 @@
1#include "sway/commands.h"
2#include "sway/config.h"
3
4struct cmd_results *output_cmd_disable(int argc, char **argv) {
5 if (!config->handler_context.output_config) {
6 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
7 }
8 config->handler_context.output_config->enabled = 0;
9
10 config->handler_context.leftovers.argc = argc;
11 config->handler_context.leftovers.argv = argv;
12 return NULL;
13}
diff --git a/sway/commands/output/dpms.c b/sway/commands/output/dpms.c
new file mode 100644
index 00000000..0959ea6b
--- /dev/null
+++ b/sway/commands/output/dpms.c
@@ -0,0 +1,24 @@
1#include "sway/commands.h"
2#include "sway/config.h"
3
4struct cmd_results *output_cmd_dpms(int argc, char **argv) {
5 if (!config->handler_context.output_config) {
6 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
7 }
8 if (!argc) {
9 return cmd_results_new(CMD_INVALID, "output", "Missing dpms argument.");
10 }
11
12 if (strcmp(*argv, "on") == 0) {
13 config->handler_context.output_config->dpms_state = DPMS_ON;
14 } else if (strcmp(*argv, "off") == 0) {
15 config->handler_context.output_config->dpms_state = DPMS_OFF;
16 } else {
17 return cmd_results_new(CMD_INVALID, "output",
18 "Invalid dpms state, valid states are on/off.");
19 }
20
21 config->handler_context.leftovers.argc = argc - 1;
22 config->handler_context.leftovers.argv = argv + 1;
23 return NULL;
24}
diff --git a/sway/commands/output/enable.c b/sway/commands/output/enable.c
new file mode 100644
index 00000000..8e3314f8
--- /dev/null
+++ b/sway/commands/output/enable.c
@@ -0,0 +1,14 @@
1#include "sway/commands.h"
2#include "sway/config.h"
3
4struct cmd_results *output_cmd_enable(int argc, char **argv) {
5 if (!config->handler_context.output_config) {
6 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
7 }
8 config->handler_context.output_config->enabled = 1;
9
10 config->handler_context.leftovers.argc = argc;
11 config->handler_context.leftovers.argv = argv;
12 return NULL;
13}
14
diff --git a/sway/commands/output/mode.c b/sway/commands/output/mode.c
new file mode 100644
index 00000000..daec6d44
--- /dev/null
+++ b/sway/commands/output/mode.c
@@ -0,0 +1,55 @@
1#include <strings.h>
2#include "sway/commands.h"
3#include "sway/config.h"
4
5struct cmd_results *output_cmd_mode(int argc, char **argv) {
6 if (!config->handler_context.output_config) {
7 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
8 }
9 if (!argc) {
10 return cmd_results_new(CMD_INVALID, "output", "Missing mode argument.");
11 }
12
13 struct output_config *output = config->handler_context.output_config;
14
15 char *end;
16 output->width = strtol(*argv, &end, 10);
17 if (*end) {
18 // Format is 1234x4321
19 if (*end != 'x') {
20 return cmd_results_new(CMD_INVALID, "output",
21 "Invalid mode width.");
22 }
23 ++end;
24 output->height = strtol(end, &end, 10);
25 if (*end) {
26 if (*end != '@') {
27 return cmd_results_new(CMD_INVALID, "output",
28 "Invalid mode height.");
29 }
30 ++end;
31 output->refresh_rate = strtof(end, &end);
32 if (strcasecmp("Hz", end) != 0) {
33 return cmd_results_new(CMD_INVALID, "output",
34 "Invalid mode refresh rate.");
35 }
36 }
37 } else {
38 // Format is 1234 4321
39 if (!argc) {
40 return cmd_results_new(CMD_INVALID, "output",
41 "Missing mode argument (height).");
42 }
43 argc--; argv++;
44 output->height = strtol(*argv, &end, 10);
45 if (*end) {
46 return cmd_results_new(CMD_INVALID, "output",
47 "Invalid mode height.");
48 }
49 }
50
51 config->handler_context.leftovers.argc = argc - 1;
52 config->handler_context.leftovers.argv = argv + 1;
53 return NULL;
54}
55
diff --git a/sway/commands/output/position.c b/sway/commands/output/position.c
new file mode 100644
index 00000000..c2aeb281
--- /dev/null
+++ b/sway/commands/output/position.c
@@ -0,0 +1,46 @@
1#include <strings.h>
2#include "sway/commands.h"
3#include "sway/config.h"
4
5struct cmd_results *output_cmd_position(int argc, char **argv) {
6 if (!config->handler_context.output_config) {
7 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
8 }
9 if (!argc) {
10 return cmd_results_new(CMD_INVALID, "output",
11 "Missing position argument.");
12 }
13
14 char *end;
15 config->handler_context.output_config->x = strtol(*argv, &end, 10);
16 if (*end) {
17 // Format is 1234,4321
18 if (*end != ',') {
19 return cmd_results_new(CMD_INVALID, "output",
20 "Invalid position x.");
21 }
22 ++end;
23 config->handler_context.output_config->y = strtol(end, &end, 10);
24 if (*end) {
25 return cmd_results_new(CMD_INVALID, "output",
26 "Invalid position y.");
27 }
28 } else {
29 // Format is 1234 4321 (legacy)
30 if (!argc) {
31 return cmd_results_new(CMD_INVALID, "output",
32 "Missing position argument (y).");
33 }
34 argc--; argv++;
35 config->handler_context.output_config->y = strtol(*argv, &end, 10);
36 if (*end) {
37 return cmd_results_new(CMD_INVALID, "output",
38 "Invalid position y.");
39 }
40 }
41
42 config->handler_context.leftovers.argc = argc - 1;
43 config->handler_context.leftovers.argv = argv + 1;
44 return NULL;
45}
46
diff --git a/sway/commands/output/scale.c b/sway/commands/output/scale.c
new file mode 100644
index 00000000..0b4cc131
--- /dev/null
+++ b/sway/commands/output/scale.c
@@ -0,0 +1,23 @@
1#include <strings.h>
2#include "sway/commands.h"
3#include "sway/config.h"
4
5struct cmd_results *output_cmd_scale(int argc, char **argv) {
6 if (!config->handler_context.output_config) {
7 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
8 }
9 if (!argc) {
10 return cmd_results_new(CMD_INVALID, "output",
11 "Missing scale argument.");
12 }
13
14 char *end;
15 config->handler_context.output_config->scale = strtof(*argv, &end);
16 if (*end) {
17 return cmd_results_new(CMD_INVALID, "output", "Invalid scale.");
18 }
19
20 config->handler_context.leftovers.argc = argc - 1;
21 config->handler_context.leftovers.argv = argv + 1;
22 return NULL;
23}
diff --git a/sway/commands/output/transform.c b/sway/commands/output/transform.c
new file mode 100644
index 00000000..f9a94d64
--- /dev/null
+++ b/sway/commands/output/transform.c
@@ -0,0 +1,39 @@
1#include <string.h>
2#include "sway/commands.h"
3#include "sway/config.h"
4
5struct cmd_results *output_cmd_transform(int argc, char **argv) {
6 if (!config->handler_context.output_config) {
7 return cmd_results_new(CMD_FAILURE, "output", "Missing output config");
8 }
9 if (!argc) {
10 return cmd_results_new(CMD_INVALID, "output",
11 "Missing transform argument.");
12 }
13
14 struct output_config *output = config->handler_context.output_config;
15 if (strcmp(*argv, "normal") == 0) {
16 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
17 } else if (strcmp(*argv, "90") == 0) {
18 output->transform = WL_OUTPUT_TRANSFORM_90;
19 } else if (strcmp(*argv, "180") == 0) {
20 output->transform = WL_OUTPUT_TRANSFORM_180;
21 } else if (strcmp(*argv, "270") == 0) {
22 output->transform = WL_OUTPUT_TRANSFORM_270;
23 } else if (strcmp(*argv, "flipped") == 0) {
24 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
25 } else if (strcmp(*argv, "flipped-90") == 0) {
26 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
27 } else if (strcmp(*argv, "flipped-180") == 0) {
28 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
29 } else if (strcmp(*argv, "flipped-270") == 0) {
30 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
31 } else {
32 return cmd_results_new(CMD_INVALID, "output",
33 "Invalid output transform.");
34 }
35
36 config->handler_context.leftovers.argc = argc - 1;
37 config->handler_context.leftovers.argv = argv + 1;
38 return NULL;
39}