aboutsummaryrefslogtreecommitdiffstats
path: root/sway/desktop/output.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/desktop/output.c')
-rw-r--r--sway/desktop/output.c1087
1 files changed, 355 insertions, 732 deletions
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index 5edc8f96..2722e556 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -1,42 +1,61 @@
1#define _POSIX_C_SOURCE 200809L
2#include <assert.h> 1#include <assert.h>
3#include <stdlib.h> 2#include <stdlib.h>
4#include <strings.h> 3#include <strings.h>
5#include <time.h> 4#include <time.h>
6#include <wayland-server-core.h> 5#include <wayland-server-core.h>
6#include <wlr/config.h>
7#include <wlr/backend/headless.h>
8#include <wlr/render/swapchain.h>
7#include <wlr/render/wlr_renderer.h> 9#include <wlr/render/wlr_renderer.h>
8#include <wlr/types/wlr_box.h>
9#include <wlr/types/wlr_buffer.h> 10#include <wlr/types/wlr_buffer.h>
11#include <wlr/types/wlr_gamma_control_v1.h>
10#include <wlr/types/wlr_matrix.h> 12#include <wlr/types/wlr_matrix.h>
11#include <wlr/types/wlr_output_damage.h>
12#include <wlr/types/wlr_output_layout.h> 13#include <wlr/types/wlr_output_layout.h>
14#include <wlr/types/wlr_output_management_v1.h>
15#include <wlr/types/wlr_output_power_management_v1.h>
13#include <wlr/types/wlr_output.h> 16#include <wlr/types/wlr_output.h>
14#include <wlr/types/wlr_presentation_time.h> 17#include <wlr/types/wlr_presentation_time.h>
15#include <wlr/types/wlr_surface.h> 18#include <wlr/types/wlr_compositor.h>
16#include <wlr/util/region.h> 19#include <wlr/util/region.h>
20#include <wlr/util/transform.h>
17#include "config.h" 21#include "config.h"
18#include "log.h" 22#include "log.h"
19#include "sway/config.h" 23#include "sway/config.h"
20#include "sway/desktop/transaction.h" 24#include "sway/desktop/transaction.h"
21#include "sway/input/input-manager.h" 25#include "sway/input/input-manager.h"
22#include "sway/input/seat.h" 26#include "sway/input/seat.h"
27#include "sway/ipc-server.h"
23#include "sway/layers.h" 28#include "sway/layers.h"
24#include "sway/output.h" 29#include "sway/output.h"
30#include "sway/scene_descriptor.h"
25#include "sway/server.h" 31#include "sway/server.h"
26#include "sway/surface.h"
27#include "sway/tree/arrange.h" 32#include "sway/tree/arrange.h"
28#include "sway/tree/container.h" 33#include "sway/tree/container.h"
29#include "sway/tree/root.h" 34#include "sway/tree/root.h"
30#include "sway/tree/view.h" 35#include "sway/tree/view.h"
31#include "sway/tree/workspace.h" 36#include "sway/tree/workspace.h"
32 37
38#if WLR_HAS_DRM_BACKEND
39#include <wlr/backend/drm.h>
40#include <wlr/types/wlr_drm_lease_v1.h>
41#endif
42
43bool output_match_name_or_id(struct sway_output *output,
44 const char *name_or_id) {
45 if (strcmp(name_or_id, "*") == 0) {
46 return true;
47 }
48
49 char identifier[128];
50 output_get_identifier(identifier, sizeof(identifier), output);
51 return strcasecmp(identifier, name_or_id) == 0
52 || strcasecmp(output->wlr_output->name, name_or_id) == 0;
53}
54
33struct sway_output *output_by_name_or_id(const char *name_or_id) { 55struct sway_output *output_by_name_or_id(const char *name_or_id) {
34 for (int i = 0; i < root->outputs->length; ++i) { 56 for (int i = 0; i < root->outputs->length; ++i) {
35 struct sway_output *output = root->outputs->items[i]; 57 struct sway_output *output = root->outputs->items[i];
36 char identifier[128]; 58 if (output_match_name_or_id(output, name_or_id)) {
37 output_get_identifier(identifier, sizeof(identifier), output);
38 if (strcasecmp(identifier, name_or_id) == 0
39 || strcasecmp(output->wlr_output->name, name_or_id) == 0) {
40 return output; 59 return output;
41 } 60 }
42 } 61 }
@@ -46,583 +65,217 @@ struct sway_output *output_by_name_or_id(const char *name_or_id) {
46struct sway_output *all_output_by_name_or_id(const char *name_or_id) { 65struct sway_output *all_output_by_name_or_id(const char *name_or_id) {
47 struct sway_output *output; 66 struct sway_output *output;
48 wl_list_for_each(output, &root->all_outputs, link) { 67 wl_list_for_each(output, &root->all_outputs, link) {
49 char identifier[128]; 68 if (output_match_name_or_id(output, name_or_id)) {
50 output_get_identifier(identifier, sizeof(identifier), output);
51 if (strcasecmp(identifier, name_or_id) == 0
52 || strcasecmp(output->wlr_output->name, name_or_id) == 0) {
53 return output; 69 return output;
54 } 70 }
55 } 71 }
56 return NULL; 72 return NULL;
57} 73}
58 74
59/**
60 * Rotate a child's position relative to a parent. The parent size is (pw, ph),
61 * the child position is (*sx, *sy) and its size is (sw, sh).
62 */
63static void rotate_child_position(double *sx, double *sy, double sw, double sh,
64 double pw, double ph, float rotation) {
65 if (rotation == 0.0f) {
66 return;
67 }
68 75
69 // Coordinates relative to the center of the subsurface 76struct sway_workspace *output_get_active_workspace(struct sway_output *output) {
70 double ox = *sx - pw/2 + sw/2, 77 struct sway_seat *seat = input_manager_current_seat();
71 oy = *sy - ph/2 + sh/2; 78 struct sway_node *focus = seat_get_active_tiling_child(seat, &output->node);
72 // Rotated coordinates 79 if (!focus) {
73 double rx = cos(-rotation)*ox - sin(-rotation)*oy, 80 if (!output->workspaces->length) {
74 ry = cos(-rotation)*oy + sin(-rotation)*ox; 81 return NULL;
75 *sx = rx + pw/2 - sw/2; 82 }
76 *sy = ry + ph/2 - sh/2; 83 return output->workspaces->items[0];
84 }
85 return focus->sway_workspace;
77} 86}
78 87
79struct surface_iterator_data { 88struct send_frame_done_data {
80 sway_surface_iterator_func_t user_iterator; 89 struct timespec when;
81 void *user_data; 90 int msec_until_refresh;
82
83 struct sway_output *output; 91 struct sway_output *output;
84 struct sway_view *view;
85 double ox, oy;
86 int width, height;
87 float rotation;
88}; 92};
89 93
90static bool get_surface_box(struct surface_iterator_data *data, 94struct buffer_timer {
91 struct wlr_surface *surface, int sx, int sy, 95 struct wl_listener destroy;
92 struct wlr_box *surface_box) { 96 struct wl_event_source *frame_done_timer;
93 struct sway_output *output = data->output; 97};
94
95 if (!wlr_surface_has_buffer(surface)) {
96 return false;
97 }
98
99 int sw = surface->current.width;
100 int sh = surface->current.height;
101
102 double _sx = sx + surface->sx;
103 double _sy = sy + surface->sy;
104 rotate_child_position(&_sx, &_sy, sw, sh, data->width, data->height,
105 data->rotation);
106
107 struct wlr_box box = {
108 .x = data->ox + _sx,
109 .y = data->oy + _sy,
110 .width = sw,
111 .height = sh,
112 };
113 if (surface_box != NULL) {
114 memcpy(surface_box, &box, sizeof(struct wlr_box));
115 }
116
117 struct wlr_box rotated_box;
118 wlr_box_rotated_bounds(&rotated_box, &box, data->rotation);
119
120 struct wlr_box output_box = {
121 .width = output->width,
122 .height = output->height,
123 };
124
125 struct wlr_box intersection;
126 return wlr_box_intersection(&intersection, &output_box, &rotated_box);
127}
128
129static void output_for_each_surface_iterator(struct wlr_surface *surface,
130 int sx, int sy, void *_data) {
131 struct surface_iterator_data *data = _data;
132
133 struct wlr_box box;
134 bool intersects = get_surface_box(data, surface, sx, sy, &box);
135 if (!intersects) {
136 return;
137 }
138 98
139 data->user_iterator(data->output, data->view, surface, &box, data->rotation, 99static int handle_buffer_timer(void *data) {
140 data->user_data); 100 struct wlr_scene_buffer *buffer = data;
141}
142 101
143void output_surface_for_each_surface(struct sway_output *output, 102 struct timespec now;
144 struct wlr_surface *surface, double ox, double oy, 103 clock_gettime(CLOCK_MONOTONIC, &now);
145 sway_surface_iterator_func_t iterator, void *user_data) { 104 wlr_scene_buffer_send_frame_done(buffer, &now);
146 struct surface_iterator_data data = { 105 return 0;
147 .user_iterator = iterator,
148 .user_data = user_data,
149 .output = output,
150 .view = NULL,
151 .ox = ox,
152 .oy = oy,
153 .width = surface->current.width,
154 .height = surface->current.height,
155 .rotation = 0,
156 };
157
158 wlr_surface_for_each_surface(surface,
159 output_for_each_surface_iterator, &data);
160} 106}
161 107
162void output_view_for_each_surface(struct sway_output *output, 108static void handle_buffer_timer_destroy(struct wl_listener *listener,
163 struct sway_view *view, sway_surface_iterator_func_t iterator, 109 void *data) {
164 void *user_data) { 110 struct buffer_timer *timer = wl_container_of(listener, timer, destroy);
165 struct surface_iterator_data data = {
166 .user_iterator = iterator,
167 .user_data = user_data,
168 .output = output,
169 .view = view,
170 .ox = view->container->surface_x - output->lx
171 - view->geometry.x,
172 .oy = view->container->surface_y - output->ly
173 - view->geometry.y,
174 .width = view->container->current.content_width,
175 .height = view->container->current.content_height,
176 .rotation = 0, // TODO
177 };
178
179 view_for_each_surface(view, output_for_each_surface_iterator, &data);
180}
181 111
182void output_view_for_each_popup_surface(struct sway_output *output, 112 wl_list_remove(&timer->destroy.link);
183 struct sway_view *view, sway_surface_iterator_func_t iterator, 113 wl_event_source_remove(timer->frame_done_timer);
184 void *user_data) { 114 free(timer);
185 struct surface_iterator_data data = {
186 .user_iterator = iterator,
187 .user_data = user_data,
188 .output = output,
189 .view = view,
190 .ox = view->container->surface_x - output->lx
191 - view->geometry.x,
192 .oy = view->container->surface_y - output->ly
193 - view->geometry.y,
194 .width = view->container->current.content_width,
195 .height = view->container->current.content_height,
196 .rotation = 0, // TODO
197 };
198
199 view_for_each_popup_surface(view, output_for_each_surface_iterator, &data);
200} 115}
201 116
202void output_layer_for_each_surface(struct sway_output *output, 117static struct buffer_timer *buffer_timer_get_or_create(struct wlr_scene_buffer *buffer) {
203 struct wl_list *layer_surfaces, sway_surface_iterator_func_t iterator, 118 struct buffer_timer *timer =
204 void *user_data) { 119 scene_descriptor_try_get(&buffer->node, SWAY_SCENE_DESC_BUFFER_TIMER);
205 struct sway_layer_surface *layer_surface; 120 if (timer) {
206 wl_list_for_each(layer_surface, layer_surfaces, link) { 121 return timer;
207 struct wlr_layer_surface_v1 *wlr_layer_surface_v1 =
208 layer_surface->layer_surface;
209 output_surface_for_each_surface(output, wlr_layer_surface_v1->surface,
210 layer_surface->geo.x, layer_surface->geo.y, iterator,
211 user_data);
212
213 struct wlr_xdg_popup *state;
214 wl_list_for_each(state, &wlr_layer_surface_v1->popups, link) {
215 struct wlr_xdg_surface *popup = state->base;
216 if (!popup->configured) {
217 continue;
218 }
219
220 double popup_sx, popup_sy;
221 popup_sx = layer_surface->geo.x +
222 popup->popup->geometry.x - popup->geometry.x;
223 popup_sy = layer_surface->geo.y +
224 popup->popup->geometry.y - popup->geometry.y;
225
226 struct wlr_surface *surface = popup->surface;
227
228 struct surface_iterator_data data = {
229 .user_iterator = iterator,
230 .user_data = user_data,
231 .output = output,
232 .view = NULL,
233 .ox = popup_sx,
234 .oy = popup_sy,
235 .width = surface->current.width,
236 .height = surface->current.height,
237 .rotation = 0,
238 };
239
240 wlr_xdg_surface_for_each_surface(
241 popup, output_for_each_surface_iterator, &data);
242 }
243 } 122 }
244}
245 123
246void output_layer_for_each_toplevel_surface(struct sway_output *output, 124 timer = calloc(1, sizeof(struct buffer_timer));
247 struct wl_list *layer_surfaces, sway_surface_iterator_func_t iterator, 125 if (!timer) {
248 void *user_data) { 126 return NULL;
249 struct sway_layer_surface *layer_surface;
250 wl_list_for_each(layer_surface, layer_surfaces, link) {
251 struct wlr_layer_surface_v1 *wlr_layer_surface_v1 =
252 layer_surface->layer_surface;
253 output_surface_for_each_surface(output, wlr_layer_surface_v1->surface,
254 layer_surface->geo.x, layer_surface->geo.y, iterator,
255 user_data);
256 } 127 }
257}
258
259 128
260void output_layer_for_each_popup_surface(struct sway_output *output, 129 timer->frame_done_timer = wl_event_loop_add_timer(server.wl_event_loop,
261 struct wl_list *layer_surfaces, sway_surface_iterator_func_t iterator, 130 handle_buffer_timer, buffer);
262 void *user_data) { 131 if (!timer->frame_done_timer) {
263 struct sway_layer_surface *layer_surface; 132 free(timer);
264 wl_list_for_each(layer_surface, layer_surfaces, link) { 133 return NULL;
265 struct wlr_layer_surface_v1 *wlr_layer_surface_v1 =
266 layer_surface->layer_surface;
267
268 struct wlr_xdg_popup *state;
269 wl_list_for_each(state, &wlr_layer_surface_v1->popups, link) {
270 struct wlr_xdg_surface *popup = state->base;
271 if (!popup->configured) {
272 continue;
273 }
274
275 double popup_sx, popup_sy;
276 popup_sx = layer_surface->geo.x +
277 popup->popup->geometry.x - popup->geometry.x;
278 popup_sy = layer_surface->geo.y +
279 popup->popup->geometry.y - popup->geometry.y;
280
281 struct wlr_surface *surface = popup->surface;
282
283 struct surface_iterator_data data = {
284 .user_iterator = iterator,
285 .user_data = user_data,
286 .output = output,
287 .view = NULL,
288 .ox = popup_sx,
289 .oy = popup_sy,
290 .width = surface->current.width,
291 .height = surface->current.height,
292 .rotation = 0,
293 };
294
295 wlr_xdg_surface_for_each_surface(
296 popup, output_for_each_surface_iterator, &data);
297 }
298 } 134 }
299}
300 135
301#if HAVE_XWAYLAND 136 scene_descriptor_assign(&buffer->node, SWAY_SCENE_DESC_BUFFER_TIMER, timer);
302void output_unmanaged_for_each_surface(struct sway_output *output,
303 struct wl_list *unmanaged, sway_surface_iterator_func_t iterator,
304 void *user_data) {
305 struct sway_xwayland_unmanaged *unmanaged_surface;
306 wl_list_for_each(unmanaged_surface, unmanaged, link) {
307 struct wlr_xwayland_surface *xsurface =
308 unmanaged_surface->wlr_xwayland_surface;
309 double ox = unmanaged_surface->lx - output->lx;
310 double oy = unmanaged_surface->ly - output->ly;
311
312 output_surface_for_each_surface(output, xsurface->surface, ox, oy,
313 iterator, user_data);
314 }
315}
316#endif
317 137
318void output_drag_icons_for_each_surface(struct sway_output *output, 138 timer->destroy.notify = handle_buffer_timer_destroy;
319 struct wl_list *drag_icons, sway_surface_iterator_func_t iterator, 139 wl_signal_add(&buffer->node.events.destroy, &timer->destroy);
320 void *user_data) {
321 struct sway_drag_icon *drag_icon;
322 wl_list_for_each(drag_icon, drag_icons, link) {
323 double ox = drag_icon->x - output->lx;
324 double oy = drag_icon->y - output->ly;
325
326 if (drag_icon->wlr_drag_icon->mapped) {
327 output_surface_for_each_surface(output,
328 drag_icon->wlr_drag_icon->surface, ox, oy,
329 iterator, user_data);
330 }
331 }
332}
333
334static void for_each_surface_container_iterator(struct sway_container *con,
335 void *_data) {
336 if (!con->view || !view_is_visible(con->view)) {
337 return;
338 }
339 140
340 struct surface_iterator_data *data = _data; 141 return timer;
341 output_view_for_each_surface(data->output, con->view,
342 data->user_iterator, data->user_data);
343} 142}
344 143
345static void output_for_each_surface(struct sway_output *output, 144static void send_frame_done_iterator(struct wlr_scene_buffer *buffer,
346 sway_surface_iterator_func_t iterator, void *user_data) { 145 int x, int y, void *user_data) {
347 if (output_has_opaque_overlay_layer_surface(output)) { 146 struct send_frame_done_data *data = user_data;
348 goto overlay; 147 struct sway_output *output = data->output;
349 } 148 int view_max_render_time = 0;
350 149
351 struct surface_iterator_data data = { 150 if (buffer->primary_output != data->output->scene_output) {
352 .user_iterator = iterator, 151 return;
353 .user_data = user_data,
354 .output = output,
355 .view = NULL,
356 };
357
358 struct sway_workspace *workspace = output_get_active_workspace(output);
359 struct sway_container *fullscreen_con = root->fullscreen_global;
360 if (!fullscreen_con) {
361 if (!workspace) {
362 return;
363 }
364 fullscreen_con = workspace->current.fullscreen;
365 }
366 if (fullscreen_con) {
367 for_each_surface_container_iterator(fullscreen_con, &data);
368 container_for_each_child(fullscreen_con,
369 for_each_surface_container_iterator, &data);
370
371 // TODO: Show transient containers for fullscreen global
372 if (fullscreen_con == workspace->current.fullscreen) {
373 for (int i = 0; i < workspace->current.floating->length; ++i) {
374 struct sway_container *floater =
375 workspace->current.floating->items[i];
376 if (container_is_transient_for(floater, fullscreen_con)) {
377 for_each_surface_container_iterator(floater, &data);
378 }
379 }
380 }
381#if HAVE_XWAYLAND
382 output_unmanaged_for_each_surface(output, &root->xwayland_unmanaged,
383 iterator, user_data);
384#endif
385 } else {
386 output_layer_for_each_surface(output,
387 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND],
388 iterator, user_data);
389 output_layer_for_each_surface(output,
390 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM],
391 iterator, user_data);
392
393 workspace_for_each_container(workspace,
394 for_each_surface_container_iterator, &data);
395
396#if HAVE_XWAYLAND
397 output_unmanaged_for_each_surface(output, &root->xwayland_unmanaged,
398 iterator, user_data);
399#endif
400 output_layer_for_each_surface(output,
401 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP],
402 iterator, user_data);
403 } 152 }
404 153
405overlay: 154 struct wlr_scene_node *current = &buffer->node;
406 output_layer_for_each_surface(output, 155 while (true) {
407 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY], 156 struct sway_view *view = scene_descriptor_try_get(current,
408 iterator, user_data); 157 SWAY_SCENE_DESC_VIEW);
409 output_drag_icons_for_each_surface(output, &root->drag_icons, 158 if (view) {
410 iterator, user_data); 159 view_max_render_time = view->max_render_time;
411} 160 break;
412
413static int scale_length(int length, int offset, float scale) {
414 return round((offset + length) * scale) - round(offset * scale);
415}
416
417void scale_box(struct wlr_box *box, float scale) {
418 box->width = scale_length(box->width, box->x, scale);
419 box->height = scale_length(box->height, box->y, scale);
420 box->x = round(box->x * scale);
421 box->y = round(box->y * scale);
422}
423
424struct sway_workspace *output_get_active_workspace(struct sway_output *output) {
425 struct sway_seat *seat = input_manager_current_seat();
426 struct sway_node *focus = seat_get_active_tiling_child(seat, &output->node);
427 if (!focus) {
428 if (!output->workspaces->length) {
429 return NULL;
430 } 161 }
431 return output->workspaces->items[0];
432 }
433 return focus->sway_workspace;
434}
435 162
436bool output_has_opaque_overlay_layer_surface(struct sway_output *output) { 163 if (!current->parent) {
437 struct sway_layer_surface *sway_layer_surface; 164 break;
438 wl_list_for_each(sway_layer_surface,
439 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY], link) {
440 struct wlr_surface *wlr_surface = sway_layer_surface->layer_surface->surface;
441 pixman_box32_t output_box = {
442 .x2 = output->width,
443 .y2 = output->height,
444 };
445 pixman_region32_t surface_opaque_box;
446 pixman_region32_init(&surface_opaque_box);
447 pixman_region32_copy(&surface_opaque_box, &wlr_surface->opaque_region);
448 pixman_region32_translate(&surface_opaque_box,
449 sway_layer_surface->geo.x, sway_layer_surface->geo.y);
450 pixman_region_overlap_t contains =
451 pixman_region32_contains_rectangle(&surface_opaque_box, &output_box);
452 pixman_region32_fini(&surface_opaque_box);
453
454 if (contains == PIXMAN_REGION_IN) {
455 return true;
456 } 165 }
457 }
458 return false;
459}
460 166
461struct send_frame_done_data { 167 current = &current->parent->node;
462 struct timespec when;
463 int msec_until_refresh;
464};
465
466static void send_frame_done_iterator(struct sway_output *output, struct sway_view *view,
467 struct wlr_surface *surface, struct wlr_box *box, float rotation,
468 void *user_data) {
469 int view_max_render_time = 0;
470 if (view != NULL) {
471 view_max_render_time = view->max_render_time;
472 } 168 }
473 169
474 struct send_frame_done_data *data = user_data;
475
476 int delay = data->msec_until_refresh - output->max_render_time 170 int delay = data->msec_until_refresh - output->max_render_time
477 - view_max_render_time; 171 - view_max_render_time;
478 172
479 if (output->max_render_time == 0 || view_max_render_time == 0 || delay < 1) { 173 struct buffer_timer *timer = NULL;
480 wlr_surface_send_frame_done(surface, &data->when);
481 } else {
482 struct sway_surface *sway_surface = surface->data;
483 wl_event_source_timer_update(sway_surface->frame_done_timer, delay);
484 }
485}
486
487static void send_frame_done(struct sway_output *output, struct send_frame_done_data *data) {
488 output_for_each_surface(output, send_frame_done_iterator, data);
489}
490
491static void count_surface_iterator(struct sway_output *output, struct sway_view *view,
492 struct wlr_surface *surface, struct wlr_box *_box, float rotation,
493 void *data) {
494 size_t *n = data;
495 (*n)++;
496}
497 174
498static bool scan_out_fullscreen_view(struct sway_output *output, 175 if (output->max_render_time != 0 && view_max_render_time != 0 && delay > 0) {
499 struct sway_view *view) { 176 timer = buffer_timer_get_or_create(buffer);
500 struct wlr_output *wlr_output = output->wlr_output;
501 struct sway_workspace *workspace = output->current.active_workspace;
502 if (!sway_assert(workspace, "Expected an active workspace")) {
503 return false;
504 } 177 }
505 178
506 if (!wl_list_empty(&view->saved_buffers)) { 179 if (timer) {
507 return false; 180 wl_event_source_timer_update(timer->frame_done_timer, delay);
181 } else {
182 wlr_scene_buffer_send_frame_done(buffer, &data->when);
508 } 183 }
184}
509 185
510 for (int i = 0; i < workspace->current.floating->length; ++i) { 186static enum wlr_scale_filter_mode get_scale_filter(struct sway_output *output,
511 struct sway_container *floater = 187 struct wlr_scene_buffer *buffer) {
512 workspace->current.floating->items[i]; 188 // if we are scaling down, we should always choose linear
513 if (container_is_transient_for(floater, view->container)) { 189 if (buffer->dst_width > 0 && buffer->dst_height > 0 && (
514 return false; 190 buffer->dst_width < buffer->buffer_width ||
515 } 191 buffer->dst_height < buffer->buffer_height)) {
192 return WLR_SCALE_FILTER_BILINEAR;
516 } 193 }
517 194
518#if HAVE_XWAYLAND 195 switch (output->scale_filter) {
519 if (!wl_list_empty(&root->xwayland_unmanaged)) { 196 case SCALE_FILTER_LINEAR:
520 return false; 197 return WLR_SCALE_FILTER_BILINEAR;
198 case SCALE_FILTER_NEAREST:
199 return WLR_SCALE_FILTER_NEAREST;
200 default:
201 abort(); // unreachable
521 } 202 }
522#endif 203}
523 204
524 if (!wl_list_empty(&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY])) { 205static void output_configure_scene(struct sway_output *output,
525 return false; 206 struct wlr_scene_node *node, float opacity) {
526 } 207 if (!node->enabled) {
527 if (!wl_list_empty(&root->drag_icons)) { 208 return;
528 return false;
529 } 209 }
530 210
531 struct wlr_surface *surface = view->surface; 211 struct sway_container *con =
532 if (surface == NULL) { 212 scene_descriptor_try_get(node, SWAY_SCENE_DESC_CONTAINER);
533 return false; 213 if (con) {
534 } 214 opacity = con->alpha;
535 size_t n_surfaces = 0;
536 output_view_for_each_surface(output, view,
537 count_surface_iterator, &n_surfaces);
538 if (n_surfaces != 1) {
539 return false;
540 } 215 }
541 216
542 if (surface->buffer == NULL) { 217 if (node->type == WLR_SCENE_NODE_BUFFER) {
543 return false; 218 struct wlr_scene_buffer *buffer = wlr_scene_buffer_from_node(node);
544 }
545 219
546 if ((float)surface->current.scale != wlr_output->scale || 220 // hack: don't call the scene setter because that will damage all outputs
547 surface->current.transform != wlr_output->transform) { 221 // We don't want to damage outputs that aren't our current output that
548 return false; 222 // we're configuring
549 } 223 buffer->filter_mode = get_scale_filter(output, buffer);
550 224
551 wlr_output_attach_buffer(wlr_output, &surface->buffer->base); 225 wlr_scene_buffer_set_opacity(buffer, opacity);
552 if (!wlr_output_test(wlr_output)) { 226 } else if (node->type == WLR_SCENE_NODE_TREE) {
553 return false; 227 struct wlr_scene_tree *tree = wlr_scene_tree_from_node(node);
228 struct wlr_scene_node *node;
229 wl_list_for_each(node, &tree->children, link) {
230 output_configure_scene(output, node, opacity);
231 }
554 } 232 }
555
556 wlr_presentation_surface_sampled_on_output(server.presentation, surface,
557 wlr_output);
558
559 return wlr_output_commit(wlr_output);
560} 233}
561 234
562static int output_repaint_timer_handler(void *data) { 235static int output_repaint_timer_handler(void *data) {
563 struct sway_output *output = data; 236 struct sway_output *output = data;
564 if (output->wlr_output == NULL) {
565 return 0;
566 }
567 237
568 output->wlr_output->frame_pending = false; 238 if (!output->enabled) {
569
570 struct sway_workspace *workspace = output->current.active_workspace;
571 if (workspace == NULL) {
572 return 0; 239 return 0;
573 } 240 }
574 241
575 struct sway_container *fullscreen_con = root->fullscreen_global; 242 output->wlr_output->frame_pending = false;
576 if (!fullscreen_con) {
577 fullscreen_con = workspace->current.fullscreen;
578 }
579 243
580 if (fullscreen_con && fullscreen_con->view) { 244 output_configure_scene(output, &root->root_scene->tree.node, 1.0f);
581 // Try to scan-out the fullscreen view
582 static bool last_scanned_out = false;
583 bool scanned_out =
584 scan_out_fullscreen_view(output, fullscreen_con->view);
585 245
586 if (scanned_out && !last_scanned_out) { 246 if (output->gamma_lut_changed) {
587 sway_log(SWAY_DEBUG, "Scanning out fullscreen view on %s", 247 struct wlr_output_state pending;
588 output->wlr_output->name); 248 wlr_output_state_init(&pending);
249 if (!wlr_scene_output_build_state(output->scene_output, &pending, NULL)) {
250 return 0;
589 } 251 }
590 if (last_scanned_out && !scanned_out) { 252
591 sway_log(SWAY_DEBUG, "Stopping fullscreen view scan out on %s", 253 output->gamma_lut_changed = false;
592 output->wlr_output->name); 254 struct wlr_gamma_control_v1 *gamma_control =
255 wlr_gamma_control_manager_v1_get_control(
256 server.gamma_control_manager_v1, output->wlr_output);
257 if (!wlr_gamma_control_v1_apply(gamma_control, &pending)) {
258 wlr_output_state_finish(&pending);
259 return 0;
593 } 260 }
594 last_scanned_out = scanned_out;
595 261
596 if (scanned_out) { 262 if (!wlr_output_commit_state(output->wlr_output, &pending)) {
263 wlr_gamma_control_v1_send_failed_and_destroy(gamma_control);
264 wlr_output_state_finish(&pending);
597 return 0; 265 return 0;
598 } 266 }
599 }
600 267
601 bool needs_frame; 268 wlr_output_state_finish(&pending);
602 pixman_region32_t damage;
603 pixman_region32_init(&damage);
604 if (!wlr_output_damage_attach_render(output->damage,
605 &needs_frame, &damage)) {
606 return 0; 269 return 0;
607 } 270 }
608 271
609 if (needs_frame) { 272 wlr_scene_output_commit(output->scene_output, NULL);
610 struct timespec now;
611 clock_gettime(CLOCK_MONOTONIC, &now);
612
613 output_render(output, &now, &damage);
614 } else {
615 wlr_output_rollback(output->wlr_output);
616 }
617
618 pixman_region32_fini(&damage);
619
620 return 0; 273 return 0;
621} 274}
622 275
623static void damage_handle_frame(struct wl_listener *listener, void *user_data) { 276static void handle_frame(struct wl_listener *listener, void *user_data) {
624 struct sway_output *output = 277 struct sway_output *output =
625 wl_container_of(listener, output, damage_frame); 278 wl_container_of(listener, output, frame);
626 if (!output->enabled || !output->wlr_output->enabled) { 279 if (!output->enabled || !output->wlr_output->enabled) {
627 return; 280 return;
628 } 281 }
@@ -633,9 +286,7 @@ static void damage_handle_frame(struct wl_listener *listener, void *user_data) {
633 286
634 if (output->max_render_time != 0) { 287 if (output->max_render_time != 0) {
635 struct timespec now; 288 struct timespec now;
636 clockid_t presentation_clock 289 clock_gettime(CLOCK_MONOTONIC, &now);
637 = wlr_backend_get_presentation_clock(server.backend);
638 clock_gettime(presentation_clock, &now);
639 290
640 const long NSEC_IN_SECONDS = 1000000000; 291 const long NSEC_IN_SECONDS = 1000000000;
641 struct timespec predicted_refresh = output->last_presentation; 292 struct timespec predicted_refresh = output->last_presentation;
@@ -682,124 +333,8 @@ static void damage_handle_frame(struct wl_listener *listener, void *user_data) {
682 struct send_frame_done_data data = {0}; 333 struct send_frame_done_data data = {0};
683 clock_gettime(CLOCK_MONOTONIC, &data.when); 334 clock_gettime(CLOCK_MONOTONIC, &data.when);
684 data.msec_until_refresh = msec_until_refresh; 335 data.msec_until_refresh = msec_until_refresh;
685 send_frame_done(output, &data); 336 data.output = output;
686} 337 wlr_scene_output_for_each_buffer(output->scene_output, send_frame_done_iterator, &data);
687
688void output_damage_whole(struct sway_output *output) {
689 // The output can exist with no wlr_output if it's just been disconnected
690 // and the transaction to evacuate it has't completed yet.
691 if (output && output->wlr_output && output->damage) {
692 wlr_output_damage_add_whole(output->damage);
693 }
694}
695
696static void damage_surface_iterator(struct sway_output *output, struct sway_view *view,
697 struct wlr_surface *surface, struct wlr_box *_box, float rotation,
698 void *_data) {
699 bool *data = _data;
700 bool whole = *data;
701
702 struct wlr_box box = *_box;
703 scale_box(&box, output->wlr_output->scale);
704
705 int center_x = box.x + box.width/2;
706 int center_y = box.y + box.height/2;
707
708 if (pixman_region32_not_empty(&surface->buffer_damage)) {
709 pixman_region32_t damage;
710 pixman_region32_init(&damage);
711 wlr_surface_get_effective_damage(surface, &damage);
712 wlr_region_scale(&damage, &damage, output->wlr_output->scale);
713 if (ceil(output->wlr_output->scale) > surface->current.scale) {
714 // When scaling up a surface, it'll become blurry so we need to
715 // expand the damage region
716 wlr_region_expand(&damage, &damage,
717 ceil(output->wlr_output->scale) - surface->current.scale);
718 }
719 pixman_region32_translate(&damage, box.x, box.y);
720 wlr_region_rotated_bounds(&damage, &damage, rotation,
721 center_x, center_y);
722 wlr_output_damage_add(output->damage, &damage);
723 pixman_region32_fini(&damage);
724 }
725
726 if (whole) {
727 wlr_box_rotated_bounds(&box, &box, rotation);
728 wlr_output_damage_add_box(output->damage, &box);
729 }
730
731 if (!wl_list_empty(&surface->current.frame_callback_list)) {
732 wlr_output_schedule_frame(output->wlr_output);
733 }
734}
735
736void output_damage_surface(struct sway_output *output, double ox, double oy,
737 struct wlr_surface *surface, bool whole) {
738 output_surface_for_each_surface(output, surface, ox, oy,
739 damage_surface_iterator, &whole);
740}
741
742void output_damage_from_view(struct sway_output *output,
743 struct sway_view *view) {
744 if (!view_is_visible(view)) {
745 return;
746 }
747 bool whole = false;
748 output_view_for_each_surface(output, view, damage_surface_iterator, &whole);
749}
750
751// Expecting an unscaled box in layout coordinates
752void output_damage_box(struct sway_output *output, struct wlr_box *_box) {
753 struct wlr_box box;
754 memcpy(&box, _box, sizeof(struct wlr_box));
755 box.x -= output->lx;
756 box.y -= output->ly;
757 scale_box(&box, output->wlr_output->scale);
758 wlr_output_damage_add_box(output->damage, &box);
759}
760
761static void damage_child_views_iterator(struct sway_container *con,
762 void *data) {
763 if (!con->view || !view_is_visible(con->view)) {
764 return;
765 }
766 struct sway_output *output = data;
767 bool whole = true;
768 output_view_for_each_surface(output, con->view, damage_surface_iterator,
769 &whole);
770}
771
772void output_damage_whole_container(struct sway_output *output,
773 struct sway_container *con) {
774 // Pad the box by 1px, because the width is a double and might be a fraction
775 struct wlr_box box = {
776 .x = con->current.x - output->lx - 1,
777 .y = con->current.y - output->ly - 1,
778 .width = con->current.width + 2,
779 .height = con->current.height + 2,
780 };
781 scale_box(&box, output->wlr_output->scale);
782 wlr_output_damage_add_box(output->damage, &box);
783 // Damage subsurfaces as well, which may extend outside the box
784 if (con->view) {
785 damage_child_views_iterator(con, output);
786 } else {
787 container_for_each_child(con, damage_child_views_iterator, output);
788 }
789}
790
791static void damage_handle_destroy(struct wl_listener *listener, void *data) {
792 struct sway_output *output =
793 wl_container_of(listener, output, damage_destroy);
794 if (!output->enabled) {
795 return;
796 }
797 output_disable(output);
798
799 wl_list_remove(&output->damage_destroy.link);
800 wl_list_remove(&output->damage_frame.link);
801
802 transaction_commit_dirty();
803} 338}
804 339
805static void update_output_manager_config(struct sway_server *server) { 340static void update_output_manager_config(struct sway_server *server) {
@@ -808,73 +343,61 @@ static void update_output_manager_config(struct sway_server *server) {
808 343
809 struct sway_output *output; 344 struct sway_output *output;
810 wl_list_for_each(output, &root->all_outputs, link) { 345 wl_list_for_each(output, &root->all_outputs, link) {
811 if (output == root->noop_output) { 346 if (output == root->fallback_output) {
812 continue; 347 continue;
813 } 348 }
814 struct wlr_output_configuration_head_v1 *config_head = 349 struct wlr_output_configuration_head_v1 *config_head =
815 wlr_output_configuration_head_v1_create(config, output->wlr_output); 350 wlr_output_configuration_head_v1_create(config, output->wlr_output);
816 struct wlr_box *output_box = wlr_output_layout_get_box( 351 struct wlr_box output_box;
817 root->output_layout, output->wlr_output); 352 wlr_output_layout_get_box(root->output_layout,
818 // We mark the output enabled even if it is switched off by DPMS 353 output->wlr_output, &output_box);
819 config_head->state.enabled = output->enabled; 354 // We mark the output enabled when it's switched off but not disabled
820 config_head->state.mode = output->current_mode; 355 config_head->state.enabled = !wlr_box_empty(&output_box);
821 if (output_box) { 356 config_head->state.x = output_box.x;
822 config_head->state.x = output_box->x; 357 config_head->state.y = output_box.y;
823 config_head->state.y = output_box->y;
824 }
825 } 358 }
826 359
827 wlr_output_manager_v1_set_configuration(server->output_manager_v1, config); 360 wlr_output_manager_v1_set_configuration(server->output_manager_v1, config);
361
362 ipc_event_output();
828} 363}
829 364
830static void handle_destroy(struct wl_listener *listener, void *data) { 365static void begin_destroy(struct sway_output *output) {
831 struct sway_output *output = wl_container_of(listener, output, destroy);
832 struct sway_server *server = output->server; 366 struct sway_server *server = output->server;
833 wl_signal_emit(&output->events.destroy, output);
834 367
835 if (output->enabled) { 368 if (output->enabled) {
836 output_disable(output); 369 output_disable(output);
837 } 370 }
371
838 output_begin_destroy(output); 372 output_begin_destroy(output);
839 373
374 wl_list_remove(&output->link);
375
376 wl_list_remove(&output->layout_destroy.link);
840 wl_list_remove(&output->destroy.link); 377 wl_list_remove(&output->destroy.link);
841 wl_list_remove(&output->commit.link); 378 wl_list_remove(&output->commit.link);
842 wl_list_remove(&output->mode.link);
843 wl_list_remove(&output->present.link); 379 wl_list_remove(&output->present.link);
380 wl_list_remove(&output->frame.link);
381 wl_list_remove(&output->request_state.link);
382
383 wlr_scene_output_destroy(output->scene_output);
384 output->scene_output = NULL;
385 output->wlr_output->data = NULL;
386 output->wlr_output = NULL;
844 387
845 transaction_commit_dirty(); 388 transaction_commit_dirty();
846 389
847 update_output_manager_config(server); 390 update_output_manager_config(server);
848} 391}
849 392
850static void handle_mode(struct wl_listener *listener, void *data) { 393static void handle_destroy(struct wl_listener *listener, void *data) {
851 struct sway_output *output = wl_container_of(listener, output, mode); 394 struct sway_output *output = wl_container_of(listener, output, destroy);
852 if (!output->enabled && !output->enabling) { 395 begin_destroy(output);
853 struct output_config *oc = find_output_config(output);
854 if (output->wlr_output->current_mode != NULL &&
855 (!oc || oc->enabled)) {
856 // We want to enable this output, but it didn't work last time,
857 // possibly because we hadn't enough CRTCs. Try again now that the
858 // output has a mode.
859 sway_log(SWAY_DEBUG, "Output %s has gained a CRTC, "
860 "trying to enable it", output->wlr_output->name);
861 apply_output_config(oc, output);
862 }
863 return;
864 }
865 if (!output->enabled) {
866 return;
867 }
868 arrange_layers(output);
869 arrange_output(output);
870 transaction_commit_dirty();
871
872 update_output_manager_config(output->server);
873} 396}
874 397
875static void update_textures(struct sway_container *con, void *data) { 398static void handle_layout_destroy(struct wl_listener *listener, void *data) {
876 container_update_title_textures(con); 399 struct sway_output *output = wl_container_of(listener, output, layout_destroy);
877 container_update_marks_textures(con); 400 begin_destroy(output);
878} 401}
879 402
880static void handle_commit(struct wl_listener *listener, void *data) { 403static void handle_commit(struct wl_listener *listener, void *data) {
@@ -885,24 +408,28 @@ static void handle_commit(struct wl_listener *listener, void *data) {
885 return; 408 return;
886 } 409 }
887 410
888 if (event->committed & WLR_OUTPUT_STATE_SCALE) { 411 if (event->state->committed & (
889 output_for_each_container(output, update_textures, NULL); 412 WLR_OUTPUT_STATE_MODE |
890 } 413 WLR_OUTPUT_STATE_TRANSFORM |
891 414 WLR_OUTPUT_STATE_SCALE)) {
892 if (event->committed & (WLR_OUTPUT_STATE_TRANSFORM | WLR_OUTPUT_STATE_SCALE)) {
893 arrange_layers(output); 415 arrange_layers(output);
894 arrange_output(output); 416 arrange_output(output);
895 transaction_commit_dirty(); 417 transaction_commit_dirty();
896 418
897 update_output_manager_config(output->server); 419 update_output_manager_config(output->server);
898 } 420 }
421
422 // Next time the output is enabled, try to re-apply the gamma LUT
423 if ((event->state->committed & WLR_OUTPUT_STATE_ENABLED) && !output->wlr_output->enabled) {
424 output->gamma_lut_changed = true;
425 }
899} 426}
900 427
901static void handle_present(struct wl_listener *listener, void *data) { 428static void handle_present(struct wl_listener *listener, void *data) {
902 struct sway_output *output = wl_container_of(listener, output, present); 429 struct sway_output *output = wl_container_of(listener, output, present);
903 struct wlr_output_event_present *output_event = data; 430 struct wlr_output_event_present *output_event = data;
904 431
905 if (!output->enabled) { 432 if (!output->enabled || !output_event->presented) {
906 return; 433 return;
907 } 434 }
908 435
@@ -910,37 +437,91 @@ static void handle_present(struct wl_listener *listener, void *data) {
910 output->refresh_nsec = output_event->refresh; 437 output->refresh_nsec = output_event->refresh;
911} 438}
912 439
440static void handle_request_state(struct wl_listener *listener, void *data) {
441 struct sway_output *output =
442 wl_container_of(listener, output, request_state);
443 const struct wlr_output_event_request_state *event = data;
444 wlr_output_commit_state(output->wlr_output, event->state);
445}
446
447static unsigned int last_headless_num = 0;
448
913void handle_new_output(struct wl_listener *listener, void *data) { 449void handle_new_output(struct wl_listener *listener, void *data) {
914 struct sway_server *server = wl_container_of(listener, server, new_output); 450 struct sway_server *server = wl_container_of(listener, server, new_output);
915 struct wlr_output *wlr_output = data; 451 struct wlr_output *wlr_output = data;
916 sway_log(SWAY_DEBUG, "New output %p: %s", wlr_output, wlr_output->name); 452
453 if (wlr_output == root->fallback_output->wlr_output) {
454 return;
455 }
456
457 if (wlr_output_is_headless(wlr_output)) {
458 char name[64];
459 snprintf(name, sizeof(name), "HEADLESS-%u", ++last_headless_num);
460 wlr_output_set_name(wlr_output, name);
461 }
462
463 sway_log(SWAY_DEBUG, "New output %p: %s (non-desktop: %d)",
464 wlr_output, wlr_output->name, wlr_output->non_desktop);
465
466 if (wlr_output->non_desktop) {
467 sway_log(SWAY_DEBUG, "Not configuring non-desktop output");
468 struct sway_output_non_desktop *non_desktop = output_non_desktop_create(wlr_output);
469#if WLR_HAS_DRM_BACKEND
470 if (server->drm_lease_manager) {
471 wlr_drm_lease_v1_manager_offer_output(server->drm_lease_manager,
472 wlr_output);
473 }
474#endif
475 list_add(root->non_desktop_outputs, non_desktop);
476 return;
477 }
478
479 if (!wlr_output_init_render(wlr_output, server->allocator,
480 server->renderer)) {
481 sway_log(SWAY_ERROR, "Failed to init output render");
482 return;
483 }
484
485 // Create the scene output here so we're not accidentally creating one for
486 // the fallback output
487 struct wlr_scene_output *scene_output =
488 wlr_scene_output_create(root->root_scene, wlr_output);
489 if (!scene_output) {
490 sway_log(SWAY_ERROR, "Failed to create a scene output");
491 return;
492 }
917 493
918 struct sway_output *output = output_create(wlr_output); 494 struct sway_output *output = output_create(wlr_output);
919 if (!output) { 495 if (!output) {
496 sway_log(SWAY_ERROR, "Failed to create a sway output");
497 wlr_scene_output_destroy(scene_output);
920 return; 498 return;
921 } 499 }
500
922 output->server = server; 501 output->server = server;
923 output->damage = wlr_output_damage_create(wlr_output); 502 output->scene_output = scene_output;
924 503
504 wl_signal_add(&root->output_layout->events.destroy, &output->layout_destroy);
505 output->layout_destroy.notify = handle_layout_destroy;
925 wl_signal_add(&wlr_output->events.destroy, &output->destroy); 506 wl_signal_add(&wlr_output->events.destroy, &output->destroy);
926 output->destroy.notify = handle_destroy; 507 output->destroy.notify = handle_destroy;
927 wl_signal_add(&wlr_output->events.commit, &output->commit); 508 wl_signal_add(&wlr_output->events.commit, &output->commit);
928 output->commit.notify = handle_commit; 509 output->commit.notify = handle_commit;
929 wl_signal_add(&wlr_output->events.mode, &output->mode);
930 output->mode.notify = handle_mode;
931 wl_signal_add(&wlr_output->events.present, &output->present); 510 wl_signal_add(&wlr_output->events.present, &output->present);
932 output->present.notify = handle_present; 511 output->present.notify = handle_present;
933 wl_signal_add(&output->damage->events.frame, &output->damage_frame); 512 wl_signal_add(&wlr_output->events.frame, &output->frame);
934 output->damage_frame.notify = damage_handle_frame; 513 output->frame.notify = handle_frame;
935 wl_signal_add(&output->damage->events.destroy, &output->damage_destroy); 514 wl_signal_add(&wlr_output->events.request_state, &output->request_state);
936 output->damage_destroy.notify = damage_handle_destroy; 515 output->request_state.notify = handle_request_state;
937 516
938 output->repaint_timer = wl_event_loop_add_timer(server->wl_event_loop, 517 output->repaint_timer = wl_event_loop_add_timer(server->wl_event_loop,
939 output_repaint_timer_handler, output); 518 output_repaint_timer_handler, output);
940 519
941 struct output_config *oc = find_output_config(output); 520 if (server->session_lock.lock) {
942 apply_output_config(oc, output); 521 sway_session_lock_add_output(server->session_lock.lock, output);
943 free_output_config(oc); 522 }
523
524 apply_all_output_configs();
944 525
945 transaction_commit_dirty(); 526 transaction_commit_dirty();
946 527
@@ -954,62 +535,104 @@ void handle_output_layout_change(struct wl_listener *listener,
954 update_output_manager_config(server); 535 update_output_manager_config(server);
955} 536}
956 537
538void handle_gamma_control_set_gamma(struct wl_listener *listener, void *data) {
539 struct sway_server *server =
540 wl_container_of(listener, server, gamma_control_set_gamma);
541 const struct wlr_gamma_control_manager_v1_set_gamma_event *event = data;
542
543 struct sway_output *output = event->output->data;
544
545 if(!output) {
546 return;
547 }
548
549 output->gamma_lut_changed = true;
550 wlr_output_schedule_frame(output->wlr_output);
551}
552
553static struct output_config *output_config_for_config_head(
554 struct wlr_output_configuration_head_v1 *config_head,
555 struct sway_output *output) {
556 struct output_config *oc = new_output_config(output->wlr_output->name);
557 oc->enabled = config_head->state.enabled;
558 if (!oc->enabled) {
559 return oc;
560 }
561
562 if (config_head->state.mode != NULL) {
563 struct wlr_output_mode *mode = config_head->state.mode;
564 oc->width = mode->width;
565 oc->height = mode->height;
566 oc->refresh_rate = mode->refresh / 1000.f;
567 } else {
568 oc->width = config_head->state.custom_mode.width;
569 oc->height = config_head->state.custom_mode.height;
570 oc->refresh_rate =
571 config_head->state.custom_mode.refresh / 1000.f;
572 }
573 oc->x = config_head->state.x;
574 oc->y = config_head->state.y;
575 oc->transform = config_head->state.transform;
576 oc->scale = config_head->state.scale;
577 oc->adaptive_sync = config_head->state.adaptive_sync_enabled;
578 return oc;
579}
580
957static void output_manager_apply(struct sway_server *server, 581static void output_manager_apply(struct sway_server *server,
958 struct wlr_output_configuration_v1 *config, bool test_only) { 582 struct wlr_output_configuration_v1 *config, bool test_only) {
959 // TODO: perform atomic tests on the whole backend atomically 583 size_t configs_len = wl_list_length(&root->all_outputs);
960 584 struct matched_output_config *configs = calloc(configs_len, sizeof(*configs));
961 struct wlr_output_configuration_head_v1 *config_head; 585 if (!configs) {
962 // First disable outputs we need to disable 586 return;
963 bool ok = true; 587 }
964 wl_list_for_each(config_head, &config->heads, link) { 588
965 struct wlr_output *wlr_output = config_head->state.output; 589 int config_idx = 0;
966 struct sway_output *output = wlr_output->data; 590 struct sway_output *sway_output;
967 if (!output->enabled || config_head->state.enabled) { 591 wl_list_for_each(sway_output, &root->all_outputs, link) {
592 if (sway_output == root->fallback_output) {
593 configs_len--;
968 continue; 594 continue;
969 } 595 }
970 struct output_config *oc = new_output_config(output->wlr_output->name);
971 oc->enabled = false;
972 596
973 if (test_only) { 597 struct matched_output_config *cfg = &configs[config_idx++];
974 ok &= test_output_config(oc, output); 598 cfg->output = sway_output;
975 } else {
976 oc = store_output_config(oc);
977 ok &= apply_output_config(oc, output);
978 }
979 }
980 599
981 // Then enable outputs that need to 600 struct wlr_output_configuration_head_v1 *config_head;
982 wl_list_for_each(config_head, &config->heads, link) { 601 wl_list_for_each(config_head, &config->heads, link) {
983 struct wlr_output *wlr_output = config_head->state.output; 602 if (config_head->state.output == sway_output->wlr_output) {
984 struct sway_output *output = wlr_output->data; 603 cfg->config = output_config_for_config_head(config_head, sway_output);
985 if (!config_head->state.enabled) { 604 break;
986 continue; 605 }
987 } 606 }
988 struct output_config *oc = new_output_config(output->wlr_output->name); 607 if (!cfg->config) {
989 oc->enabled = true; 608 cfg->config = find_output_config(sway_output);
990 if (config_head->state.mode != NULL) {
991 struct wlr_output_mode *mode = config_head->state.mode;
992 oc->width = mode->width;
993 oc->height = mode->height;
994 oc->refresh_rate = mode->refresh / 1000.f;
995 } else {
996 oc->width = config_head->state.custom_mode.width;
997 oc->height = config_head->state.custom_mode.height;
998 oc->refresh_rate =
999 config_head->state.custom_mode.refresh / 1000.f;
1000 } 609 }
1001 oc->x = config_head->state.x; 610 }
1002 oc->y = config_head->state.y;
1003 oc->transform = config_head->state.transform;
1004 oc->scale = config_head->state.scale;
1005 611
1006 if (test_only) { 612 sort_output_configs_by_priority(configs, configs_len);
1007 ok &= test_output_config(oc, output); 613 bool ok = apply_output_configs(configs, configs_len, test_only, false);
614 for (size_t idx = 0; idx < configs_len; idx++) {
615 struct matched_output_config *cfg = &configs[idx];
616
617 // Only store new configs for successful non-test commits. Old configs,
618 // test-only and failed commits just get freed.
619 bool store_config = false;
620 if (!test_only && ok) {
621 struct wlr_output_configuration_head_v1 *config_head;
622 wl_list_for_each(config_head, &config->heads, link) {
623 if (config_head->state.output == cfg->output->wlr_output) {
624 store_config = true;
625 break;
626 }
627 }
628 }
629 if (store_config) {
630 store_output_config(cfg->config);
1008 } else { 631 } else {
1009 oc = store_output_config(oc); 632 free_output_config(cfg->config);
1010 ok &= apply_output_config(oc, output);
1011 } 633 }
1012 } 634 }
635 free(configs);
1013 636
1014 if (ok) { 637 if (ok) {
1015 wlr_output_configuration_v1_send_succeeded(config); 638 wlr_output_configuration_v1_send_succeeded(config);
@@ -1047,12 +670,12 @@ void handle_output_power_manager_set_mode(struct wl_listener *listener,
1047 struct output_config *oc = new_output_config(output->wlr_output->name); 670 struct output_config *oc = new_output_config(output->wlr_output->name);
1048 switch (event->mode) { 671 switch (event->mode) {
1049 case ZWLR_OUTPUT_POWER_V1_MODE_OFF: 672 case ZWLR_OUTPUT_POWER_V1_MODE_OFF:
1050 oc->dpms_state = DPMS_OFF; 673 oc->power = 0;
1051 break; 674 break;
1052 case ZWLR_OUTPUT_POWER_V1_MODE_ON: 675 case ZWLR_OUTPUT_POWER_V1_MODE_ON:
1053 oc->dpms_state = DPMS_ON; 676 oc->power = 1;
1054 break; 677 break;
1055 } 678 }
1056 oc = store_output_config(oc); 679 store_output_config(oc);
1057 apply_output_config(oc, output); 680 apply_all_output_configs();
1058} 681}