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