diff options
Diffstat (limited to 'sway/commands/output.c')
-rw-r--r-- | sway/commands/output.c | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/sway/commands/output.c b/sway/commands/output.c new file mode 100644 index 00000000..6c1c55b5 --- /dev/null +++ b/sway/commands/output.c | |||
@@ -0,0 +1,182 @@ | |||
1 | #include <ctype.h> | ||
2 | #include <libgen.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <unistd.h> | ||
6 | #include <wordexp.h> | ||
7 | #include "sway/commands.h" | ||
8 | #include "sway/config.h" | ||
9 | #include "list.h" | ||
10 | #include "log.h" | ||
11 | #include "stringop.h" | ||
12 | |||
13 | static char *bg_options[] = { | ||
14 | "stretch", | ||
15 | "center", | ||
16 | "fill", | ||
17 | "fit", | ||
18 | "tile" | ||
19 | }; | ||
20 | |||
21 | struct cmd_results *cmd_output(int argc, char **argv) { | ||
22 | struct cmd_results *error = NULL; | ||
23 | if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) { | ||
24 | return error; | ||
25 | } | ||
26 | const char *name = argv[0]; | ||
27 | |||
28 | struct output_config *output = calloc(1, sizeof(struct output_config)); | ||
29 | output->x = output->y = output->width = output->height = -1; | ||
30 | output->name = strdup(name); | ||
31 | output->enabled = -1; | ||
32 | output->scale = 1; | ||
33 | |||
34 | // TODO: atoi doesn't handle invalid numbers | ||
35 | |||
36 | int i; | ||
37 | for (i = 1; i < argc; ++i) { | ||
38 | const char *command = argv[i]; | ||
39 | |||
40 | if (strcasecmp(command, "disable") == 0) { | ||
41 | output->enabled = 0; | ||
42 | } else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) { | ||
43 | if (++i >= argc) { | ||
44 | return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument."); | ||
45 | } | ||
46 | char *res = argv[i]; | ||
47 | char *x = strchr(res, 'x'); | ||
48 | int width = -1, height = -1; | ||
49 | if (x != NULL) { | ||
50 | // Format is 1234x4321 | ||
51 | *x = '\0'; | ||
52 | width = atoi(res); | ||
53 | height = atoi(x + 1); | ||
54 | *x = 'x'; | ||
55 | } else { | ||
56 | // Format is 1234 4321 | ||
57 | width = atoi(res); | ||
58 | if (++i >= argc) { | ||
59 | return cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height)."); | ||
60 | } | ||
61 | res = argv[i]; | ||
62 | height = atoi(res); | ||
63 | } | ||
64 | output->width = width; | ||
65 | output->height = height; | ||
66 | } else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) { | ||
67 | if (++i >= argc) { | ||
68 | return cmd_results_new(CMD_INVALID, "output", "Missing position argument."); | ||
69 | } | ||
70 | char *res = argv[i]; | ||
71 | char *c = strchr(res, ','); | ||
72 | int x = -1, y = -1; | ||
73 | if (c != NULL) { | ||
74 | // Format is 1234,4321 | ||
75 | *c = '\0'; | ||
76 | x = atoi(res); | ||
77 | y = atoi(c + 1); | ||
78 | *c = ','; | ||
79 | } else { | ||
80 | // Format is 1234 4321 | ||
81 | x = atoi(res); | ||
82 | if (++i >= argc) { | ||
83 | return cmd_results_new(CMD_INVALID, "output", "Missing position argument (y)."); | ||
84 | } | ||
85 | res = argv[i]; | ||
86 | y = atoi(res); | ||
87 | } | ||
88 | output->x = x; | ||
89 | output->y = y; | ||
90 | } else if (strcasecmp(command, "scale") == 0) { | ||
91 | if (++i >= argc) { | ||
92 | return cmd_results_new(CMD_INVALID, "output", "Missing scale parameter."); | ||
93 | } | ||
94 | output->scale = atoi(argv[i]); | ||
95 | } else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) { | ||
96 | wordexp_t p; | ||
97 | if (++i >= argc) { | ||
98 | return cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification."); | ||
99 | } | ||
100 | if (i + 1 >= argc) { | ||
101 | return cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`."); | ||
102 | } | ||
103 | if (strcasecmp(argv[argc - 1], "solid_color") == 0) { | ||
104 | output->background = strdup(argv[argc - 2]); | ||
105 | output->background_option = strdup("solid_color"); | ||
106 | } else { | ||
107 | char *src = join_args(argv + i, argc - i - 1); | ||
108 | char *mode = argv[argc - 1]; | ||
109 | if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) { | ||
110 | return cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src); | ||
111 | } | ||
112 | free(src); | ||
113 | src = p.we_wordv[0]; | ||
114 | if (config->reading && *src != '/') { | ||
115 | char *conf = strdup(config->current_config); | ||
116 | char *conf_path = dirname(conf); | ||
117 | src = malloc(strlen(conf_path) + strlen(src) + 2); | ||
118 | sprintf(src, "%s/%s", conf_path, p.we_wordv[0]); | ||
119 | free(conf); | ||
120 | } | ||
121 | if (access(src, F_OK) == -1) { | ||
122 | return cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src); | ||
123 | } | ||
124 | for (char *m = mode; *m; ++m) *m = tolower(*m); | ||
125 | // Check mode | ||
126 | bool valid = false; | ||
127 | size_t j; | ||
128 | for (j = 0; j < sizeof(bg_options) / sizeof(char *); ++j) { | ||
129 | if (strcasecmp(mode, bg_options[j]) == 0) { | ||
130 | valid = true; | ||
131 | break; | ||
132 | } | ||
133 | } | ||
134 | if (!valid) { | ||
135 | return cmd_results_new(CMD_INVALID, "output", "Invalid background scaling mode."); | ||
136 | } | ||
137 | output->background = strdup(src); | ||
138 | output->background_option = strdup(mode); | ||
139 | if (src != p.we_wordv[0]) { | ||
140 | free(src); | ||
141 | } | ||
142 | wordfree(&p); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | |||
147 | i = list_seq_find(config->output_configs, output_name_cmp, name); | ||
148 | if (i >= 0) { | ||
149 | // merge existing config | ||
150 | struct output_config *oc = config->output_configs->items[i]; | ||
151 | merge_output_config(oc, output); | ||
152 | free_output_config(output); | ||
153 | output = oc; | ||
154 | } else { | ||
155 | list_add(config->output_configs, output); | ||
156 | } | ||
157 | |||
158 | sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ %d, %d scale %d) (bg %s %s)", | ||
159 | output->name, output->enabled, output->width, | ||
160 | output->height, output->x, output->y, output->scale, | ||
161 | output->background, output->background_option); | ||
162 | |||
163 | if (output->name) { | ||
164 | // Try to find the output container and apply configuration now. If | ||
165 | // this is during startup then there will be no container and config | ||
166 | // will be applied during normal "new output" event from wlc. | ||
167 | swayc_t *cont = NULL; | ||
168 | for (int i = 0; i < root_container.children->length; ++i) { | ||
169 | cont = root_container.children->items[i]; | ||
170 | if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) { | ||
171 | apply_output_config(output, cont); | ||
172 | |||
173 | if (strcmp(output->name, "*") != 0) { | ||
174 | // stop looking if the output config isn't applicable to all outputs | ||
175 | break; | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | } | ||
180 | |||
181 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
182 | } | ||