aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-12-13 20:36:06 -0500
committerLibravatar GitHub <noreply@github.com>2017-12-13 20:36:06 -0500
commit1aab9ae3e74d15e2c346acbab2ef2def59db72eb (patch)
tree6a3479fef295dc673c6bcdaac822f44523f94d8e /sway/config
parentMerge pull request #1501 from emersion/command-include (diff)
parentUse strtol instead of atoi in output command (diff)
downloadsway-1aab9ae3e74d15e2c346acbab2ef2def59db72eb.tar.gz
sway-1aab9ae3e74d15e2c346acbab2ef2def59db72eb.tar.zst
sway-1aab9ae3e74d15e2c346acbab2ef2def59db72eb.zip
Merge pull request #1503 from emersion/output-config
Add output config
Diffstat (limited to 'sway/config')
-rw-r--r--sway/config/output.c185
1 files changed, 185 insertions, 0 deletions
diff --git a/sway/config/output.c b/sway/config/output.c
new file mode 100644
index 00000000..dc9ee37c
--- /dev/null
+++ b/sway/config/output.c
@@ -0,0 +1,185 @@
1#define _XOPEN_SOURCE 700
2#include <string.h>
3#include <assert.h>
4#include <wlr/types/wlr_output.h>
5#include <wlr/types/wlr_output_layout.h>
6#include "sway/config.h"
7#include "sway/output.h"
8#include "log.h"
9
10int output_name_cmp(const void *item, const void *data) {
11 const struct output_config *output = item;
12 const char *name = data;
13
14 return strcmp(output->name, name);
15}
16
17struct output_config *new_output_config() {
18 struct output_config *oc = calloc(1, sizeof(struct output_config));
19 if (oc == NULL) {
20 return NULL;
21 }
22 oc->enabled = -1;
23 oc->width = oc->height = -1;
24 oc->refresh_rate = -1;
25 oc->x = oc->y = -1;
26 oc->scale = -1;
27 oc->transform = -1;
28 return oc;
29}
30
31void merge_output_config(struct output_config *dst, struct output_config *src) {
32 if (src->name) {
33 free(dst->name);
34 dst->name = strdup(src->name);
35 }
36 if (src->enabled != -1) {
37 dst->enabled = src->enabled;
38 }
39 if (src->width != -1) {
40 dst->width = src->width;
41 }
42 if (src->height != -1) {
43 dst->height = src->height;
44 }
45 if (src->x != -1) {
46 dst->x = src->x;
47 }
48 if (src->y != -1) {
49 dst->y = src->y;
50 }
51 if (src->scale != -1) {
52 dst->scale = src->scale;
53 }
54 if (src->refresh_rate != -1) {
55 dst->refresh_rate = src->refresh_rate;
56 }
57 if (src->transform != -1) {
58 dst->transform = src->transform;
59 }
60 if (src->background) {
61 free(dst->background);
62 dst->background = strdup(src->background);
63 }
64 if (src->background_option) {
65 free(dst->background_option);
66 dst->background_option = strdup(src->background_option);
67 }
68}
69
70static void set_mode(struct wlr_output *output, int width, int height,
71 float refresh_rate) {
72 int mhz = (int)(refresh_rate * 1000);
73 if (wl_list_empty(&output->modes)) {
74 sway_log(L_DEBUG, "Assigning custom mode to %s", output->name);
75 wlr_output_set_custom_mode(output, width, height, mhz);
76 return;
77 }
78
79 struct wlr_output_mode *mode, *best = NULL;
80 wl_list_for_each(mode, &output->modes, link) {
81 if (mode->width == width && mode->height == height) {
82 if (mode->refresh == mhz) {
83 best = mode;
84 break;
85 }
86 best = mode;
87 }
88 }
89 if (!best) {
90 sway_log(L_ERROR, "Configured mode for %s not available", output->name);
91 } else {
92 sway_log(L_DEBUG, "Assigning configured mode to %s", output->name);
93 wlr_output_set_mode(output, best);
94 }
95}
96
97void apply_output_config(struct output_config *oc, swayc_t *output) {
98 assert(output->type == C_OUTPUT);
99
100 struct wlr_output *wlr_output = output->sway_output->wlr_output;
101 if (oc && oc->enabled == 0) {
102 wlr_output_layout_remove(root_container.sway_root->output_layout,
103 wlr_output);
104 destroy_output(output);
105 return;
106 }
107
108 if (oc && oc->width > 0 && oc->height > 0) {
109 sway_log(L_DEBUG, "Set %s mode to %dx%d (%f GHz)", oc->name, oc->width,
110 oc->height, oc->refresh_rate);
111 set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
112 }
113 if (oc && oc->scale > 0) {
114 sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale);
115 wlr_output_set_scale(wlr_output, oc->scale);
116 }
117 if (oc && oc->transform >= 0) {
118 sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
119 wlr_output_set_transform(wlr_output, oc->transform);
120 }
121
122 // Find position for it
123 if (oc && (oc->x != -1 || oc->y != -1)) {
124 sway_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y);
125 wlr_output_layout_add(root_container.sway_root->output_layout,
126 wlr_output, oc->x, oc->y);
127 } else {
128 wlr_output_layout_add_auto(root_container.sway_root->output_layout,
129 wlr_output);
130 }
131
132 if (!oc || !oc->background) {
133 // Look for a * config for background
134 int i = list_seq_find(config->output_configs, output_name_cmp, "*");
135 if (i >= 0) {
136 oc = config->output_configs->items[i];
137 } else {
138 oc = NULL;
139 }
140 }
141
142 int output_i;
143 for (output_i = 0; output_i < root_container.children->length; ++output_i) {
144 if (root_container.children->items[output_i] == output) {
145 break;
146 }
147 }
148
149 if (oc && oc->background) {
150 // TODO swaybg
151 /*if (output->bg_pid != 0) {
152 terminate_swaybg(output->bg_pid);
153 }
154
155 sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background);
156
157 size_t bufsize = 12;
158 char output_id[bufsize];
159 snprintf(output_id, bufsize, "%d", output_i);
160 output_id[bufsize-1] = 0;
161
162 char *const cmd[] = {
163 "swaybg",
164 output_id,
165 oc->background,
166 oc->background_option,
167 NULL,
168 };
169
170 output->bg_pid = fork();
171 if (output->bg_pid == 0) {
172 execvp(cmd[0], cmd);
173 }*/
174 }
175}
176
177void free_output_config(struct output_config *oc) {
178 if (!oc) {
179 return;
180 }
181 free(oc->name);
182 free(oc->background);
183 free(oc->background_option);
184 free(oc);
185}