diff options
Diffstat (limited to 'sway/config/output.c')
-rw-r--r-- | sway/config/output.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/sway/config/output.c b/sway/config/output.c new file mode 100644 index 00000000..4ed2c531 --- /dev/null +++ b/sway/config/output.c | |||
@@ -0,0 +1,181 @@ | |||
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 | |||
10 | int 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 | |||
17 | void output_config_defaults(struct output_config *oc) { | ||
18 | oc->enabled = -1; | ||
19 | oc->width = oc->height -1; | ||
20 | oc->refresh_rate = -1; | ||
21 | oc->x = oc->y = -1; | ||
22 | oc->scale = -1; | ||
23 | oc->transform = -1; | ||
24 | } | ||
25 | |||
26 | void merge_output_config(struct output_config *dst, struct output_config *src) { | ||
27 | if (src->name) { | ||
28 | if (dst->name) { | ||
29 | free(dst->name); | ||
30 | } | ||
31 | dst->name = strdup(src->name); | ||
32 | } | ||
33 | if (src->enabled != -1) { | ||
34 | dst->enabled = src->enabled; | ||
35 | } | ||
36 | if (src->width != -1) { | ||
37 | dst->width = src->width; | ||
38 | } | ||
39 | if (src->height != -1) { | ||
40 | dst->height = src->height; | ||
41 | } | ||
42 | if (src->x != -1) { | ||
43 | dst->x = src->x; | ||
44 | } | ||
45 | if (src->y != -1) { | ||
46 | dst->y = src->y; | ||
47 | } | ||
48 | if (src->scale != -1) { | ||
49 | dst->scale = src->scale; | ||
50 | } | ||
51 | if (src->refresh_rate != -1) { | ||
52 | dst->refresh_rate = src->refresh_rate; | ||
53 | } | ||
54 | if (src->transform != -1) { | ||
55 | dst->transform = src->transform; | ||
56 | } | ||
57 | if (src->background) { | ||
58 | if (dst->background) { | ||
59 | free(dst->background); | ||
60 | } | ||
61 | dst->background = strdup(src->background); | ||
62 | } | ||
63 | if (src->background_option) { | ||
64 | if (dst->background_option) { | ||
65 | free(dst->background_option); | ||
66 | } | ||
67 | dst->background_option = strdup(src->background_option); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | static void set_mode(struct wlr_output *output, int width, int height, | ||
72 | float refresh_rate) { | ||
73 | struct wlr_output_mode *mode, *best = NULL; | ||
74 | int mhz = (int)(refresh_rate * 1000); | ||
75 | wl_list_for_each(mode, &output->modes, link) { | ||
76 | if (mode->width == width && mode->height == height) { | ||
77 | if (mode->refresh == mhz) { | ||
78 | best = mode; | ||
79 | break; | ||
80 | } | ||
81 | best = mode; | ||
82 | } | ||
83 | } | ||
84 | if (!best) { | ||
85 | sway_log(L_ERROR, "Configured mode for %s not available", output->name); | ||
86 | } else { | ||
87 | sway_log(L_DEBUG, "Assigning configured mode to %s", output->name); | ||
88 | wlr_output_set_mode(output, best); | ||
89 | } | ||
90 | } | ||
91 | |||
92 | void apply_output_config(struct output_config *oc, swayc_t *output) { | ||
93 | assert(output->type == C_OUTPUT); | ||
94 | |||
95 | if (oc && oc->enabled == 0) { | ||
96 | destroy_output(output); | ||
97 | return; | ||
98 | } | ||
99 | |||
100 | struct wlr_output *wlr_output = output->sway_output->wlr_output; | ||
101 | if (oc && oc->width > 0 && oc->height > 0) { | ||
102 | set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate); | ||
103 | } | ||
104 | if (oc && oc->scale > 0) { | ||
105 | sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale); | ||
106 | wlr_output->scale = oc->scale; | ||
107 | } | ||
108 | if (oc && oc->transform >= 0) { | ||
109 | sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform); | ||
110 | wlr_output_transform(wlr_output, oc->transform); | ||
111 | } | ||
112 | |||
113 | // Find position for it | ||
114 | if (oc && (oc->x != -1 || oc->y != -1)) { | ||
115 | sway_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y); | ||
116 | wlr_output_layout_add(root_container.output_layout, wlr_output, oc->x, | ||
117 | oc->y); | ||
118 | } else { | ||
119 | wlr_output_layout_add_auto(root_container.output_layout, wlr_output); | ||
120 | } | ||
121 | struct wlr_box *output_layout_box = | ||
122 | wlr_output_layout_get_box(root_container.output_layout, wlr_output); | ||
123 | output->x = output_layout_box->x; | ||
124 | output->y = output_layout_box->y; | ||
125 | output->width = output_layout_box->width; | ||
126 | output->height = output_layout_box->height; | ||
127 | |||
128 | if (!oc || !oc->background) { | ||
129 | // Look for a * config for background | ||
130 | int i = list_seq_find(config->output_configs, output_name_cmp, "*"); | ||
131 | if (i >= 0) { | ||
132 | oc = config->output_configs->items[i]; | ||
133 | } else { | ||
134 | oc = NULL; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | int output_i; | ||
139 | for (output_i = 0; output_i < root_container.children->length; ++output_i) { | ||
140 | if (root_container.children->items[output_i] == output) { | ||
141 | break; | ||
142 | } | ||
143 | } | ||
144 | |||
145 | if (oc && oc->background) { | ||
146 | // TODO swaybg | ||
147 | /*if (output->bg_pid != 0) { | ||
148 | terminate_swaybg(output->bg_pid); | ||
149 | } | ||
150 | |||
151 | sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); | ||
152 | |||
153 | size_t bufsize = 12; | ||
154 | char output_id[bufsize]; | ||
155 | snprintf(output_id, bufsize, "%d", output_i); | ||
156 | output_id[bufsize-1] = 0; | ||
157 | |||
158 | char *const cmd[] = { | ||
159 | "swaybg", | ||
160 | output_id, | ||
161 | oc->background, | ||
162 | oc->background_option, | ||
163 | NULL, | ||
164 | }; | ||
165 | |||
166 | output->bg_pid = fork(); | ||
167 | if (output->bg_pid == 0) { | ||
168 | execvp(cmd[0], cmd); | ||
169 | }*/ | ||
170 | } | ||
171 | } | ||
172 | |||
173 | void free_output_config(struct output_config *oc) { | ||
174 | if (!oc) { | ||
175 | return; | ||
176 | } | ||
177 | free(oc->name); | ||
178 | free(oc->background); | ||
179 | free(oc->background_option); | ||
180 | free(oc); | ||
181 | } | ||