aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config/output.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/config/output.c')
-rw-r--r--sway/config/output.c213
1 files changed, 213 insertions, 0 deletions
diff --git a/sway/config/output.c b/sway/config/output.c
new file mode 100644
index 00000000..1c298d37
--- /dev/null
+++ b/sway/config/output.c
@@ -0,0 +1,213 @@
1#define _XOPEN_SOURCE 700
2#include <assert.h>
3#include <stdbool.h>
4#include <string.h>
5#include <signal.h>
6#include <sys/wait.h>
7#include <unistd.h>
8#include <wlr/types/wlr_output.h>
9#include <wlr/types/wlr_output_layout.h>
10#include "sway/config.h"
11#include "sway/output.h"
12#include "log.h"
13
14int output_name_cmp(const void *item, const void *data) {
15 const struct output_config *output = item;
16 const char *name = data;
17
18 return strcmp(output->name, name);
19}
20
21void output_get_identifier(char *identifier, size_t len,
22 struct sway_output *output) {
23 struct wlr_output *wlr_output = output->wlr_output;
24 snprintf(identifier, len, "%s %s %s", wlr_output->make, wlr_output->model,
25 wlr_output->serial);
26}
27
28struct output_config *new_output_config(const char *name) {
29 struct output_config *oc = calloc(1, sizeof(struct output_config));
30 if (oc == NULL) {
31 return NULL;
32 }
33 oc->name = strdup(name);
34 if (oc->name == NULL) {
35 free(oc);
36 return NULL;
37 }
38 oc->enabled = -1;
39 oc->width = oc->height = -1;
40 oc->refresh_rate = -1;
41 oc->x = oc->y = -1;
42 oc->scale = -1;
43 oc->transform = -1;
44 return oc;
45}
46
47void merge_output_config(struct output_config *dst, struct output_config *src) {
48 if (src->name) {
49 free(dst->name);
50 dst->name = strdup(src->name);
51 }
52 if (src->enabled != -1) {
53 dst->enabled = src->enabled;
54 }
55 if (src->width != -1) {
56 dst->width = src->width;
57 }
58 if (src->height != -1) {
59 dst->height = src->height;
60 }
61 if (src->x != -1) {
62 dst->x = src->x;
63 }
64 if (src->y != -1) {
65 dst->y = src->y;
66 }
67 if (src->scale != -1) {
68 dst->scale = src->scale;
69 }
70 if (src->refresh_rate != -1) {
71 dst->refresh_rate = src->refresh_rate;
72 }
73 if (src->transform != -1) {
74 dst->transform = src->transform;
75 }
76 if (src->background) {
77 free(dst->background);
78 dst->background = strdup(src->background);
79 }
80 if (src->background_option) {
81 free(dst->background_option);
82 dst->background_option = strdup(src->background_option);
83 }
84}
85
86static void set_mode(struct wlr_output *output, int width, int height,
87 float refresh_rate) {
88 int mhz = (int)(refresh_rate * 1000);
89 if (wl_list_empty(&output->modes)) {
90 wlr_log(L_DEBUG, "Assigning custom mode to %s", output->name);
91 wlr_output_set_custom_mode(output, width, height, mhz);
92 return;
93 }
94
95 struct wlr_output_mode *mode, *best = NULL;
96 wl_list_for_each(mode, &output->modes, link) {
97 if (mode->width == width && mode->height == height) {
98 if (mode->refresh == mhz) {
99 best = mode;
100 break;
101 }
102 best = mode;
103 }
104 }
105 if (!best) {
106 wlr_log(L_ERROR, "Configured mode for %s not available", output->name);
107 } else {
108 wlr_log(L_DEBUG, "Assigning configured mode to %s", output->name);
109 wlr_output_set_mode(output, best);
110 }
111}
112
113void terminate_swaybg(pid_t pid) {
114 int ret = kill(pid, SIGTERM);
115 if (ret != 0) {
116 wlr_log(L_ERROR, "Unable to terminate swaybg [pid: %d]", pid);
117 } else {
118 int status;
119 waitpid(pid, &status, 0);
120 }
121}
122
123void apply_output_config(struct output_config *oc, struct sway_container *output) {
124 assert(output->type == C_OUTPUT);
125
126 struct wlr_output_layout *output_layout =
127 root_container.sway_root->output_layout;
128 struct wlr_output *wlr_output = output->sway_output->wlr_output;
129
130 if (oc && oc->enabled == 0) {
131 container_destroy(output);
132 wlr_output_layout_remove(root_container.sway_root->output_layout,
133 wlr_output);
134 return;
135 }
136
137 if (oc && oc->width > 0 && oc->height > 0) {
138 wlr_log(L_DEBUG, "Set %s mode to %dx%d (%f GHz)", oc->name, oc->width,
139 oc->height, oc->refresh_rate);
140 set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
141 }
142 if (oc && oc->scale > 0) {
143 wlr_log(L_DEBUG, "Set %s scale to %f", oc->name, oc->scale);
144 wlr_output_set_scale(wlr_output, oc->scale);
145 }
146 if (oc && oc->transform >= 0) {
147 wlr_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
148 wlr_output_set_transform(wlr_output, oc->transform);
149 }
150
151 // Find position for it
152 if (oc && (oc->x != -1 || oc->y != -1)) {
153 wlr_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y);
154 wlr_output_layout_add(output_layout, wlr_output, oc->x, oc->y);
155 } else {
156 wlr_output_layout_add_auto(output_layout, wlr_output);
157 }
158
159 if (!oc || !oc->background) {
160 // Look for a * config for background
161 int i = list_seq_find(config->output_configs, output_name_cmp, "*");
162 if (i >= 0) {
163 oc = config->output_configs->items[i];
164 } else {
165 oc = NULL;
166 }
167 }
168
169 int output_i;
170 for (output_i = 0; output_i < root_container.children->length; ++output_i) {
171 if (root_container.children->items[output_i] == output) {
172 break;
173 }
174 }
175
176 if (oc && oc->background) {
177 if (output->sway_output->bg_pid != 0) {
178 terminate_swaybg(output->sway_output->bg_pid);
179 }
180
181 wlr_log(L_DEBUG, "Setting background for output %d to %s",
182 output_i, oc->background);
183
184 size_t len = snprintf(NULL, 0, "%s %d %s %s",
185 config->swaybg_command ? config->swaybg_command : "swaybg",
186 output_i, oc->background, oc->background_option);
187 char *command = malloc(len + 1);
188 if (!command) {
189 wlr_log(L_DEBUG, "Unable to allocate swaybg command");
190 return;
191 }
192 snprintf(command, len + 1, "%s %d %s %s",
193 config->swaybg_command ? config->swaybg_command : "swaybg",
194 output_i, oc->background, oc->background_option);
195 wlr_log(L_DEBUG, "-> %s", command);
196
197 char *const cmd[] = { "sh", "-c", command, NULL };
198 output->sway_output->bg_pid = fork();
199 if (output->sway_output->bg_pid == 0) {
200 execvp(cmd[0], cmd);
201 }
202 }
203}
204
205void free_output_config(struct output_config *oc) {
206 if (!oc) {
207 return;
208 }
209 free(oc->name);
210 free(oc->background);
211 free(oc->background_option);
212 free(oc);
213}