Repository navigation
Expand file tree
/
Copy pathforward.c
More file actions
2497 lines (2251 loc) · 98.9 KB
/
Copy pathforward.c
File metadata and controls
2497 lines (2251 loc) · 98.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "swaylock.h"
#include "log.h"
#include "loop.h"
#include <wayland-client-core.h>
#include <wayland-client-protocol.h>
#include <wayland-server-core.h>
#include <wayland-server-protocol.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include "color-management-v1-server-protocol.h"
#include "color-representation-v1-server-protocol.h"
#include "ext-session-lock-v1-client-protocol.h"
#include "fractional-scale-v1-server-protocol.h"
#include "linux-dmabuf-unstable-v1-server-protocol.h"
#include "viewporter-server-protocol.h"
#include "wayland-drm-server-protocol.h"
#include "wlr-layer-shell-unstable-v1-server-protocol.h"
static const struct wl_surface_interface surface_impl;
static const struct wl_subsurface_interface subsurface_impl;
static const struct wl_buffer_interface buffer_impl;
static const struct wl_shm_pool_interface shm_pool_impl;
static const struct wl_compositor_interface compositor_impl;
static const struct zwp_linux_buffer_params_v1_interface linux_dmabuf_params_impl;
static const struct zwp_linux_dmabuf_feedback_v1_interface linux_dmabuf_feedback_v1_impl;
static const struct wp_viewport_interface viewport_impl;
static const struct wp_fractional_scale_v1_interface fractional_scale_impl;
static const struct wp_image_description_creator_icc_v1_interface desc_creator_icc_impl;
static const struct wp_image_description_creator_params_v1_interface desc_creator_params_impl;
static const struct wp_color_management_surface_v1_interface color_surface_impl;
static const struct wp_image_description_v1_interface image_desc_impl;
static const struct wp_color_representation_surface_v1_interface color_rep_surface_impl;
static void delete_image_desc_if_unreferenced(struct forward_image_desc* desc);
static void apply_pending_surface_updates(struct forward_surface *surface,
struct augmented_surface *background, struct forward_state *state, bool synchronized);
struct forward_params {
struct zwp_linux_buffer_params_v1* params;
struct wl_resource *resource;
int32_t width;
int32_t height;
};
static bool does_transform_transpose_size(int32_t transform) {
switch (transform) {
default:
case WL_OUTPUT_TRANSFORM_NORMAL:
case WL_OUTPUT_TRANSFORM_180:
case WL_OUTPUT_TRANSFORM_FLIPPED:
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
return false;
case WL_OUTPUT_TRANSFORM_90:
case WL_OUTPUT_TRANSFORM_270:
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
return true;
}
}
static void wl_list_insert_before(struct wl_list *ref, struct wl_list *elm) {
elm->next = ref;
elm->prev = ref->prev;
ref->prev = elm;
elm->prev->next = elm;
}
static void nested_surface_destroy(struct wl_client *client,
struct wl_resource *resource) {
// this will also destroy the user_data.
wl_resource_destroy(resource);
}
static void nested_surface_attach(struct wl_client *client,
struct wl_resource *resource, struct wl_resource *buffer, int32_t x, int32_t y) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct forward_surface *surface = wl_resource_get_user_data(resource);
struct forward_buffer *f_buffer = NULL;
if (buffer) {
assert(wl_resource_instance_of(buffer, &wl_buffer_interface, &buffer_impl));
f_buffer = wl_resource_get_user_data(buffer);
assert(f_buffer);
}
if (surface->pending.attachment == f_buffer) {
/* no change */
return;
}
if (surface->pending.attachment != NULL && surface->pending.attachment != BUFFER_COMMITTED) {
/* Dereference pending buffer */
struct forward_buffer *old_buf = surface->pending.attachment;
wl_list_remove(&surface->pending.attachment_link);
/* Remove old buffer if no links to it left */
if (old_buf->resource == NULL && wl_list_empty(&old_buf->pending_surfaces)) {
assert(wl_list_empty(&old_buf->committed_surfaces));
wl_buffer_destroy(old_buf->buffer);
free(old_buf);
}
}
if (f_buffer) {
wl_list_insert(&f_buffer->pending_surfaces, &surface->pending.attachment_link);
}
surface->pending.attachment = f_buffer;
}
static void nested_surface_damage(struct wl_client *client,
struct wl_resource *resource, int32_t x, int32_t y,
int32_t width, int32_t height) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct forward_surface *surface = wl_resource_get_user_data(resource);
struct damage_record *new_damage = realloc(surface->old_damage, sizeof(struct damage_record) * (surface->old_damage_len + 1));
if (new_damage) {
surface->old_damage = new_damage;
surface->old_damage[surface->old_damage_len].x = x;
surface->old_damage[surface->old_damage_len].y = y;
surface->old_damage[surface->old_damage_len].w = width;
surface->old_damage[surface->old_damage_len].h = height;
surface->old_damage_len++;
}
}
static void frame_callback_handle_resource_destroy(struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_callback_interface, NULL));
wl_list_remove(wl_resource_get_link(resource));
}
static void nested_surface_frame(struct wl_client *client,
struct wl_resource *resource, uint32_t callback) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct wl_resource *callback_resource = wl_resource_create(client, &wl_callback_interface,
wl_resource_get_version(resource), callback);
if (callback_resource == NULL) {
wl_client_post_no_memory(client);
return;
}
wl_resource_set_implementation(callback_resource, NULL,
NULL, frame_callback_handle_resource_destroy);
struct forward_surface *surface = wl_resource_get_user_data(resource);
struct wl_list *link = wl_resource_get_link(callback_resource);
wl_list_insert(&surface->frame_callbacks, link);
}
static void nested_surface_set_opaque_region(struct wl_client *client,
struct wl_resource *resource, struct wl_resource *region) {
// do nothing, swaylock doesn't need to know about regions
}
static void nested_surface_set_input_region(struct wl_client *client,
struct wl_resource *resource, struct wl_resource *region) {
// do nothing, swaylock doesn't need to know about regions
}
void add_serial_pair(struct forward_surface *surf, uint32_t upstream_serial,
uint32_t downstream_serial, uint32_t width, uint32_t height, bool local_only) {
surf->serial_table = realloc(surf->serial_table, sizeof(struct serial_pair) * (surf->serial_table_len + 1));
assert(surf->serial_table);
surf->serial_table[surf->serial_table_len] = (struct serial_pair) {
.plugin_serial = downstream_serial,
.upstream_serial = upstream_serial,
.config_width = width,
.config_height = height,
.local_only = local_only,
};
surf->serial_table_len++;
}
static void bg_frame_handle_done(void *data, struct wl_callback *callback,
uint32_t dummy) {
(void)dummy;
struct frame_callbacks *callbacks = data;
// Trigger all frame callbacks for the background
struct wl_resource *plugin_cb, *tmp;
wl_resource_for_each_safe(plugin_cb, tmp, &callbacks->list) {
wl_callback_send_done(plugin_cb, 0);
wl_list_remove(wl_resource_get_link(plugin_cb));
wl_list_init(wl_resource_get_link(plugin_cb));
wl_resource_destroy(plugin_cb);
}
assert(wl_list_empty(&callbacks->list));
free(data);
}
static const struct wl_callback_listener bg_frame_listener = {
.done = bg_frame_handle_done,
};
static void apply_pending_subsurf_entry(struct subsurface_entry *entry,
struct forward_surface *surface, struct wl_surface *last_surface, bool synchronized) {
if (!entry->surface->ext_subsurface) {
struct wl_surface *parent = surface->sway_surface
? surface->sway_surface->surface.surface : surface->ext_surface.surface;
entry->surface->ext_subsurface =
wl_subcompositor_get_subsurface(surface->state->subcompositor,
entry->surface->ext_surface.surface, parent);
}
if (entry->is_desync) {
wl_subsurface_set_desync(entry->surface->ext_subsurface);
} else {
wl_subsurface_set_sync(entry->surface->ext_subsurface);
synchronized = true;
}
wl_subsurface_set_position(entry->surface->ext_subsurface, entry->pos_x, entry->pos_y);
assert(&entry->surface->subsurf_pending_entry == entry);
entry->surface->subsurf_committed_entry.is_desync = entry->is_desync;
entry->surface->subsurf_committed_entry.pos_x = entry->pos_x;
entry->surface->subsurf_committed_entry.pos_y = entry->pos_y;
if (synchronized) {
// TODO: does the synchronized content update mode require special handling?
}
}
static void apply_pending_surface_updates(struct forward_surface *surface,
struct augmented_surface *background, struct forward_state *state, bool synchronized) {
// TODO: the "committed" state should actually be a property of the swaylock_surface
// as it sticks around between client replacements; and subsurfaces should be implemented
// as longer-lived "swaylock_subsurface" objects so that restarting a plugin that always
// uses the same subsurfaces appears seamless
if (surface->committed.buffer_scale != surface->pending.buffer_scale) {
wl_surface_set_buffer_scale(background->surface, surface->pending.buffer_scale);
surface->committed.buffer_scale = surface->pending.buffer_scale;
}
if (surface->committed.buffer_transform != surface->pending.buffer_transform) {
wl_surface_set_buffer_transform(background->surface, surface->pending.buffer_transform);
surface->committed.buffer_transform = surface->pending.buffer_transform;
}
if (surface->committed.viewport_dest_width != surface->pending.viewport_dest_width ||
surface->committed.viewport_dest_height != surface->pending.viewport_dest_height) {
assert(background->viewport);
wp_viewport_set_destination(background->viewport, surface->pending.viewport_dest_width,
surface->pending.viewport_dest_height);
surface->committed.viewport_dest_width = surface->pending.viewport_dest_width;
surface->committed.viewport_dest_height = surface->pending.viewport_dest_height;
}
if (surface->committed.viewport_source_x != surface->pending.viewport_source_x ||
surface->committed.viewport_source_y != surface->pending.viewport_source_y ||
surface->committed.viewport_source_w != surface->pending.viewport_source_w ||
surface->committed.viewport_source_h != surface->pending.viewport_source_h) {
assert(background->viewport);
wp_viewport_set_source(background->viewport,
surface->pending.viewport_source_x, surface->pending.viewport_source_y,
surface->pending.viewport_source_w, surface->pending.viewport_source_h);
surface->committed.viewport_source_x = surface->pending.viewport_source_x;
surface->committed.viewport_source_y = surface->pending.viewport_source_y;
surface->committed.viewport_source_w = surface->pending.viewport_source_w;
surface->committed.viewport_source_h = surface->pending.viewport_source_h;
}
if (surface->committed.has_alpha_mode != surface->pending.has_alpha_mode ||
surface->committed.alpha_mode != surface->pending.alpha_mode ||
surface->committed.has_chroma_location != surface->pending.has_chroma_location ||
surface->committed.chroma_location != surface->pending.chroma_location ||
surface->committed.has_coef_range != surface->pending.has_coef_range ||
surface->committed.coefficients != surface->pending.coefficients ||
surface->committed.range != surface->pending.range) {
assert(background->color_rep_surface);
// There is no way to reset color representation parameters to default
// other than unsetting and recreating the surface. To simplify the logic,
// recreate the color rep surface on every change.
wp_color_representation_surface_v1_destroy(background->color_rep_surface);
background->color_rep_surface = wp_color_representation_manager_v1_get_surface(
state->color_representation, background->surface);
if (surface->pending.has_alpha_mode) {
wp_color_representation_surface_v1_set_alpha_mode(
background->color_rep_surface, surface->pending.alpha_mode);
}
if (surface->pending.has_chroma_location) {
wp_color_representation_surface_v1_set_chroma_location(
background->color_rep_surface, surface->pending.chroma_location);
}
if (surface->pending.has_coef_range) {
wp_color_representation_surface_v1_set_coefficients_and_range(
background->color_rep_surface, surface->pending.coefficients,
surface->pending.range);
}
surface->committed.has_alpha_mode = surface->pending.has_alpha_mode;
surface->committed.alpha_mode = surface->pending.alpha_mode;
surface->committed.has_chroma_location = surface->pending.has_chroma_location;
surface->committed.chroma_location = surface->pending.chroma_location;
surface->committed.has_coef_range = surface->pending.has_coef_range;
surface->committed.coefficients = surface->pending.coefficients;
surface->committed.range = surface->pending.range;
}
if (surface->committed.image_desc != surface->pending.image_desc ||
surface->committed.render_intent != surface->pending.render_intent) {
assert(background->color_surface);
if (!surface->pending.image_desc) {
wp_color_management_surface_v1_unset_image_description(
background->color_surface);
} else {
wp_color_management_surface_v1_set_image_description(
background->color_surface, surface->pending.image_desc->description,
surface->pending.render_intent);
}
if (surface->committed.image_desc != surface->pending.image_desc) {
if (surface->committed.image_desc) {
wl_list_remove(&surface->committed.image_desc_link);
delete_image_desc_if_unreferenced(surface->committed.image_desc);
}
if (surface->pending.image_desc) {
surface->committed.image_desc = surface->pending.image_desc;
wl_list_insert(&surface->pending.image_desc->committed_surfaces,
&surface->committed.image_desc_link);
} else {
surface->committed.image_desc = NULL;
wl_list_init(&surface->committed.image_desc_link);
}
}
surface->committed.render_intent = surface->pending.render_intent;
}
int surface_version = wl_resource_get_version(surface->surface);
// The protocol does not make this fully explicit, but the buffer should
// be attached _each time_ that any damage is sent alongside it, even if
// the buffer is the same. This is also necessary to ensure that the
// appropriate release events are sent
if (surface->pending.attachment != BUFFER_COMMITTED) {
/* unlink the committed attachment */
if (surface->committed.attachment != NULL && surface->committed.attachment != BUFFER_UNREACHABLE) {
assert(surface->committed.attachment->resource != NULL);
wl_list_remove(&surface->committed.attachment_link);
}
if (surface->pending.attachment != NULL) {
struct forward_buffer *upstream_buffer = surface->pending.attachment;
int32_t offset_x = surface_version >= 5 ? 0 : surface->pending.offset_x;
int32_t offset_y = surface_version >= 5 ? 0 : surface->pending.offset_y;
wl_surface_attach(background->surface,
upstream_buffer ? upstream_buffer->buffer : NULL,
offset_x, offset_y);
if (surface_version < 5) {
surface->committed.offset_x = surface->pending.offset_x;
surface->committed.offset_y = surface->pending.offset_y;
}
surface->committed.attachment = surface->pending.attachment;
surface->committed_buffer_width = upstream_buffer->width;
surface->committed_buffer_height = upstream_buffer->height;
wl_list_insert(&upstream_buffer->committed_surfaces, &surface->committed.attachment_link);
} else {
/* See above: null attachments are either bad wallpaper program
* behavior or need no commit, and should not occur for the main
* surface */
assert(!surface->layer_surface);
surface->committed.attachment = NULL;
}
}
/* If there was an offset change, but no buffer value change */
if ((surface->committed.offset_x != surface->pending.offset_x ||
surface->committed.offset_y != surface->pending.offset_y) &&
surface_version >= 5) {
wl_surface_offset(background->surface, surface->pending.offset_x, surface->pending.offset_y);
surface->committed.offset_x = surface->pending.offset_x;
surface->committed.offset_y = surface->pending.offset_y;
}
/* apply and clear damage */
for (size_t i = 0; i < surface->buffer_damage_len; i++) {
wl_surface_damage_buffer(background->surface, surface->buffer_damage[i].x,
surface->buffer_damage[i].y,
surface->buffer_damage[i].w,
surface->buffer_damage[i].h);
}
for (size_t i = 0; i < surface->old_damage_len; i++) {
wl_surface_damage(background->surface, surface->old_damage[i].x,
surface->old_damage[i].y,
surface->old_damage[i].w,
surface->old_damage[i].h);
}
free(surface->buffer_damage);
surface->buffer_damage = NULL;
surface->buffer_damage_len = 0;
free(surface->old_damage);
surface->old_damage = NULL;
surface->old_damage_len = 0;
// TODO: fix handling of sync surface content updates
struct subsurface_entry *entry, *tmp_entry;
wl_list_for_each_safe(entry, tmp_entry, &surface->committed.subsurfaces_above, link) {
// TODO: remove the committed surface if necessary
wl_list_remove(&entry->link);
wl_list_init(&entry->link);
}
wl_list_for_each_reverse_safe(entry, tmp_entry, &surface->committed.subsurfaces_below, link) {
// TODO: remove the committed surface if necessary
wl_list_remove(&entry->link);
wl_list_init(&entry->link);
}
struct wl_surface *last_surface = background->surface;
wl_list_for_each(entry, &surface->pending.subsurfaces_above, link) {
apply_pending_subsurf_entry(entry, surface, last_surface, synchronized);
wl_subsurface_place_above(entry->surface->ext_subsurface, last_surface);
last_surface = entry->surface->ext_surface.surface;
wl_list_insert_before(&surface->committed.subsurfaces_above,
&entry->surface->subsurf_committed_entry.link);
}
last_surface = background->surface;
wl_list_for_each_reverse(entry, &surface->pending.subsurfaces_below, link) {
apply_pending_subsurf_entry(entry, surface, last_surface, synchronized);
wl_subsurface_place_below(entry->surface->ext_subsurface, last_surface);
last_surface = entry->surface->ext_surface.surface;
wl_list_insert(&surface->committed.subsurfaces_below,
&entry->surface->subsurf_committed_entry.link);
}
if (!wl_list_empty(&surface->frame_callbacks)) {
/* plugin has requested frame callbacks, so make a request now. Move the
* list of callbacks to trigger to a new object to avoid triggering newer
* callbacks and to better handle early surface destruction */
struct wl_callback *callback = wl_surface_frame(background->surface);
struct frame_callbacks *callbacks = calloc(1, sizeof(struct frame_callbacks));
assert(callbacks); // TODO handle error
callbacks->list = surface->frame_callbacks;
callbacks->list.next->prev = &callbacks->list;
callbacks->list.prev->next = &callbacks->list;
wl_list_init(&surface->frame_callbacks);
// TODO: if the callback fails to trigger, this leaks; all the frame callbacks
// should be forcibly triggered when the surface is destroyed
wl_callback_add_listener(callback, &bg_frame_listener, callbacks);
}
}
static void nested_surface_commit(struct wl_client *client,
struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct forward_surface *surface = wl_resource_get_user_data(resource);
if (surface->inert) {
return;
}
if (surface->subsurface_parent) {
/* This surface has a subsurface role, so forward any commit messages */
apply_pending_surface_updates(surface, &surface->ext_surface, surface->state, false);
wl_surface_commit(surface->ext_surface.surface);
}
if (!surface->sway_surface) {
/* Clients can create and commit to any number of wl_surfaces; however,
* these have no impact until the surface is given a role. Ignore these
* commits. */
return;
}
if (!surface->has_been_configured) {
/* send initial configure */
struct swaylock_bg_client *bg_client = surface->sway_surface->client ?
surface->sway_surface->client : surface->sway_surface->state->server.main_client;
uint32_t plugin_serial = bg_client->serial++;
uint32_t config_width = surface->sway_surface->width, config_height = surface->sway_surface->height;
if (config_width == 0 || config_height == 0) {
swaylock_log(LOG_ERROR, "committing nested surface before main surface dimensions known");
}
// When committing a plugin surface for the first time,
// if the upstream surface is also new, then forward the configure;
// but if the upstream surface was configured long ago, then
// keep the configure local
if (!surface->sway_surface->used_first_configure) {
add_serial_pair(surface, surface->sway_surface->first_configure_serial, plugin_serial,
config_width, config_height, false);
surface->sway_surface->used_first_configure = true;
} else if (surface->sway_surface->has_newer_serial) {
/* In this case, the swaylock surface has received
* unacknowledged configures that the previous client
* for the surface did not acknowledge. Since we are
* giving this client an up to date size, acknowledge
* the corresponding configure when the client finally
* responds. */
add_serial_pair(surface, surface->sway_surface->newest_serial, plugin_serial,
config_width, config_height, false);
} else {
/* Swallow plugin's configure event -- all upstream configures
* were acknowledged by past clients */
add_serial_pair(surface, 0, plugin_serial, config_width, config_height, true);
}
zwlr_layer_surface_v1_send_configure(surface->layer_surface, plugin_serial,
config_width, config_height);
surface->has_been_configured = true;
// todo: handle unmap/remap logic -- is it an error to attach a buffer
// after unmapping?
assert(!surface->pending.attachment);
/* The first commit should not be forwarded, because the main swaylock
* process already made such a commit in order to receive its
* own configure event. Thus, return here. */
return;
}
if (surface->committed.attachment == NULL && surface->pending.attachment == NULL) {
/* In this scenario, no buffer has been attached yet; there is no point in making
* a second or further commit without a buffer, so don't bother committing anything.
* (Note: other than the buffer, the surface state has nothing that risks dangling
* if it neglects to commit, and there is no attached buffer.) */
return;
}
if (surface->committed.attachment != NULL && surface->pending.attachment == NULL) {
/* Good wallpaper clients should never unmap their surfaces. Kill it. */
wl_resource_post_error(resource, 1000, "The wallpaper program should not unmap any layer shell surface");
return;
}
struct swaylock_surface *sw_surf = surface->sway_surface;
struct augmented_surface *background = &sw_surf->surface;
apply_pending_surface_updates(surface, &sw_surf->surface, surface->state, false);
/* Verify that the surface dimensions exactly match the output */
wl_fixed_t n = wl_fixed_from_int(-1);
bool viewport_dst_on = surface->committed.viewport_dest_width != -1;
bool viewport_src_on = surface->committed.viewport_source_w != n;
uint32_t output_width, output_height;
if (viewport_dst_on) {
output_width = surface->committed.viewport_dest_width;
output_height = surface->committed.viewport_dest_height;
} else if (viewport_src_on) {
output_width = wl_fixed_to_int(surface->committed.viewport_source_w);
output_height = wl_fixed_to_int(surface->committed.viewport_source_h);
if (wl_fixed_from_int(output_width) != surface->committed.viewport_source_w ||
wl_fixed_from_int(output_height) != surface->committed.viewport_source_h) {
wl_resource_post_error(surface->viewport, WP_VIEWPORT_ERROR_BAD_SIZE, "width/height not integral");
return;
}
// TODO: technically, should also validate that the viewport dimensions fall inside the
// (transformed) buffer bounding box
} else {
if (surface->committed_buffer_width % surface->committed.buffer_scale != 0 ||
surface->committed_buffer_height % surface->committed.buffer_scale != 0) {
wl_resource_post_error(resource, WL_SURFACE_ERROR_INVALID_SIZE, "buffer dimensions not divisible by scale");
return;
}
output_width = surface->committed_buffer_width / surface->committed.buffer_scale;
output_height = surface->committed_buffer_height / surface->committed.buffer_scale;
if (does_transform_transpose_size(surface->committed.buffer_transform)) {
uint32_t tmp = output_width;
output_width = output_height;
output_height = tmp;
}
}
if (output_width != surface->last_acked_width || output_height != surface->last_acked_height) {
swaylock_log(LOG_ERROR, "Wallpaper program committed surface at size %d x %d, which does not exactly match last acknowledged W x H = %d x %d",
output_width, output_height, surface->last_acked_width, surface->last_acked_height);
wl_resource_post_error(resource, 1000, "The wallpaper program should exactly match the configure width/height");
return;
}
/* Finally, commit updates to corresponding upstream background surface */
if (surface->committed.attachment) {
// permit subsurface drawing
surface->sway_surface->has_buffer = true;
}
if (sw_surf->has_pending_ack_conf) {
/* Submit this right before the commit, to avoid race conditions
* between injected commits from the swaylock rendering and
* the gap between ack and commit from the plugin */
ext_session_lock_surface_v1_ack_configure(sw_surf->ext_session_lock_surface_v1, sw_surf->pending_upstream_serial);
sw_surf->has_pending_ack_conf = false;
if (sw_surf->pending_upstream_serial == sw_surf->newest_serial) {
sw_surf->has_newer_serial = false;
}
}
if (sw_surf->client_submission_timer) {
/* Disarm timer, indicating that plugin has responded on time
* for this output. */
loop_remove_timer(sw_surf->state->eventloop, sw_surf->client_submission_timer);
sw_surf->client_submission_timer = NULL;
}
wl_surface_commit(background->surface);
}
static void nested_surface_set_buffer_transform(struct wl_client *client,
struct wl_resource *resource, int32_t transform) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct forward_surface *surface = wl_resource_get_user_data(resource);
surface->pending.buffer_transform = transform;
// TODO: validate that the transform is valid;
}
static void nested_surface_set_buffer_scale(struct wl_client *client,
struct wl_resource *resource, int32_t scale) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct forward_surface *surface = wl_resource_get_user_data(resource);
if (scale <= 0) {
wl_resource_post_error(resource, WL_SURFACE_ERROR_INVALID_SCALE, "scale is < 1");
return;
}
surface->pending.buffer_scale = scale;
}
static void nested_surface_damage_buffer(struct wl_client *client,
struct wl_resource *resource, int32_t x, int32_t y,
int32_t width, int32_t height) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct forward_surface *surface = wl_resource_get_user_data(resource);
struct damage_record *new_damage = realloc(surface->buffer_damage, sizeof(struct damage_record) * (surface->buffer_damage_len + 1));
if (new_damage) {
surface->buffer_damage = new_damage;
surface->buffer_damage[surface->buffer_damage_len].x = x;
surface->buffer_damage[surface->buffer_damage_len].y = y;
surface->buffer_damage[surface->buffer_damage_len].w = width;
surface->buffer_damage[surface->buffer_damage_len].h = height;
surface->buffer_damage_len++;
}
}
static const struct wl_surface_interface surface_impl = {
.destroy = nested_surface_destroy,
.attach = nested_surface_attach,
.damage = nested_surface_damage,
.frame = nested_surface_frame,
.set_opaque_region = nested_surface_set_opaque_region,
.set_input_region = nested_surface_set_input_region,
.commit = nested_surface_commit,
.set_buffer_transform = nested_surface_set_buffer_transform,
.set_buffer_scale = nested_surface_set_buffer_scale,
.damage_buffer = nested_surface_damage_buffer,
};
static void destroy_subsurface_parts(struct forward_surface *fwd_surface) {
if (fwd_surface->ext_subsurface) {
wl_subsurface_destroy(fwd_surface->ext_subsurface);
fwd_surface->ext_subsurface = NULL;
}
if (fwd_surface->ext_surface.surface) {
wl_surface_destroy(fwd_surface->ext_surface.surface);
fwd_surface->ext_surface.surface = NULL;
}
if (fwd_surface->ext_surface.viewport) {
wp_viewport_destroy(fwd_surface->ext_surface.viewport);
fwd_surface->ext_surface.viewport = NULL;
}
if (fwd_surface->ext_surface.color_surface) {
wp_color_management_surface_v1_destroy(fwd_surface->ext_surface.color_surface);
fwd_surface->ext_surface.color_surface = NULL;
}
if (fwd_surface->ext_surface.color_rep_surface) {
wp_color_representation_surface_v1_destroy(fwd_surface->ext_surface.color_rep_surface);
fwd_surface->ext_surface.color_rep_surface = NULL;
}
if (!wl_list_empty(&fwd_surface->subsurf_pending_entry.link)) {
wl_list_remove(&fwd_surface->subsurf_pending_entry.link);
wl_list_init(&fwd_surface->subsurf_pending_entry.link);
}
if (!wl_list_empty(&fwd_surface->subsurf_committed_entry.link)) {
wl_list_remove(&fwd_surface->subsurf_committed_entry.link);
wl_list_init(&fwd_surface->subsurf_committed_entry.link);
}
fwd_surface->subsurface_parent = NULL;
fwd_surface->inert = true;
}
static void surface_handle_resource_destroy(struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl));
struct forward_surface *fwd_surface = wl_resource_get_user_data(resource);
if (fwd_surface->sway_surface) {
fwd_surface->sway_surface->plugin_surface = NULL;
}
struct wl_resource *cb_resource, *tmp;
wl_resource_for_each_safe(cb_resource, tmp, &fwd_surface->frame_callbacks) {
// the callback resource, on destruction, will try to remove itself,
// so set it up with an empty list (on which _remove() is safe)
wl_list_remove(wl_resource_get_link(cb_resource));
wl_list_init(wl_resource_get_link(cb_resource));
}
if (fwd_surface->pending.attachment
&& fwd_surface->pending.attachment != BUFFER_UNREACHABLE
&& fwd_surface->pending.attachment != BUFFER_COMMITTED) {
assert(fwd_surface->pending.attachment->resource != NULL);
wl_list_remove(&fwd_surface->pending.attachment_link);
}
if (fwd_surface->committed.attachment
&& fwd_surface->committed.attachment != BUFFER_UNREACHABLE
&& fwd_surface->committed.attachment != BUFFER_COMMITTED) {
assert(fwd_surface->committed.attachment->resource != NULL);
wl_list_remove(&fwd_surface->committed.attachment_link);
}
if (fwd_surface->pending.image_desc) {
wl_list_remove(&fwd_surface->pending.image_desc_link);
delete_image_desc_if_unreferenced(fwd_surface->pending.image_desc);
}
if (fwd_surface->committed.image_desc) {
wl_list_remove(&fwd_surface->committed.image_desc_link);
delete_image_desc_if_unreferenced(fwd_surface->committed.image_desc);
}
free(fwd_surface->buffer_damage);
free(fwd_surface->old_damage);
free(fwd_surface->serial_table);
if (fwd_surface->viewport) {
wl_resource_set_user_data(fwd_surface->viewport, NULL);
}
if (fwd_surface->fractional_scale) {
wl_resource_set_user_data(fwd_surface->fractional_scale, NULL);
}
if (fwd_surface->color_surface) {
wl_resource_set_user_data(fwd_surface->color_surface, NULL);
}
if (fwd_surface->color_representation) {
wl_resource_set_user_data(fwd_surface->color_representation, NULL);
}
if (fwd_surface->subsurface) {
wl_resource_set_user_data(fwd_surface->subsurface, NULL);
}
destroy_subsurface_parts(fwd_surface);
free(fwd_surface);
}
static void default_surface_state(struct surface_state *state) {
wl_fixed_t n = wl_fixed_from_int(-1);
state->viewport_dest_height = -1;
state->viewport_dest_width = -1;
state->viewport_source_x = n;
state->viewport_source_y = n;
state->viewport_source_w = n;
state->viewport_source_h = n;
state->buffer_scale = 1;
state->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
state->offset_x = 0;
state->offset_y = 0;
state->attachment = NULL;
// state->attachment_link is only used when attachment is not NULL
wl_list_init(&state->subsurfaces_above);
wl_list_init(&state->subsurfaces_below);
}
static void compositor_create_surface(struct wl_client *client,
struct wl_resource *resource, uint32_t id) {
assert(wl_resource_instance_of(resource, &wl_compositor_interface, &compositor_impl));
struct forward_state *state = wl_resource_get_user_data(resource);
struct wl_resource *surf_resource = wl_resource_create(client, &wl_surface_interface,
wl_resource_get_version(resource), id);
if (surf_resource == NULL) {
wl_client_post_no_memory(client);
return;
}
struct forward_surface *fwd_surface = calloc(1, sizeof(struct forward_surface));
if (!fwd_surface) {
wl_client_post_no_memory(client);
return;
}
fwd_surface->surface = resource;
fwd_surface->state = state;
wl_list_init(&fwd_surface->frame_callbacks);
default_surface_state(&fwd_surface->pending);
default_surface_state(&fwd_surface->committed);
fwd_surface->subsurf_pending_entry.surface = fwd_surface;
fwd_surface->subsurf_committed_entry.surface = fwd_surface;
wl_list_init(&fwd_surface->subsurf_pending_entry.link);
wl_list_init(&fwd_surface->subsurf_committed_entry.link);
wl_resource_set_implementation(surf_resource, &surface_impl,
fwd_surface, surface_handle_resource_destroy);
// do not listen for events, because the plugin has no input anyway
}
static void region_add(struct wl_client *client, struct wl_resource *resource,
int32_t x, int32_t y, int32_t width, int32_t height) {
// do nothing, swaylock doesn't need to know about regions
}
static void region_subtract(struct wl_client *client, struct wl_resource *resource,
int32_t x, int32_t y, int32_t width, int32_t height) {
// do nothing, swaylock doesn't need to know about regions
}
static void region_destroy(struct wl_client *client, struct wl_resource *resource) {
wl_resource_destroy(resource);
}
static const struct wl_region_interface region_impl = {
.destroy = region_destroy,
.add = region_add,
.subtract = region_subtract,
};
static void compositor_create_region(struct wl_client *client,
struct wl_resource *resource, uint32_t id) {
/* for nested clients, regions are ignored entirely */
struct wl_resource *region_resource = wl_resource_create(client,
&wl_region_interface, wl_resource_get_version(resource), id);
if (region_resource == NULL) {
wl_client_post_no_memory(client);
return;
}
wl_resource_set_implementation(region_resource, ®ion_impl, NULL, NULL);
}
static const struct wl_compositor_interface compositor_impl = {
.create_surface = compositor_create_surface,
.create_region = compositor_create_region,
};
void bind_wl_compositor(struct wl_client *client, void *data,
uint32_t version, uint32_t id) {
struct wl_resource *resource =
wl_resource_create(client, &wl_compositor_interface, version, id);
if (resource == NULL) {
wl_client_post_no_memory(client);
return;
}
wl_resource_set_implementation(resource, &compositor_impl, data, NULL);
}
static void nested_subsurface_destroy(struct wl_client *client, struct wl_resource *resource) {
// TODO: update parent surface?
wl_resource_destroy(resource);
}
static void nested_subsurface_set_position(struct wl_client *client,
struct wl_resource *resource, int32_t x, int32_t y) {
assert(wl_resource_instance_of(resource, &wl_subsurface_interface, &subsurface_impl));
struct forward_surface *fwd_surface = wl_resource_get_user_data(resource);
if (!fwd_surface->subsurface_parent || fwd_surface->inert) {
// Subsurface parent destroyed, ignore
return;
}
assert(!wl_list_empty(&fwd_surface->subsurf_pending_entry.link));
fwd_surface->subsurf_pending_entry.pos_x = x;
fwd_surface->subsurf_pending_entry.pos_y = y;
}
static void nested_subsurface_place_above(struct wl_client *client,
struct wl_resource *resource, struct wl_resource *sibling) {
assert(wl_resource_instance_of(resource, &wl_subsurface_interface, &subsurface_impl));
assert(wl_resource_instance_of(sibling, &wl_surface_interface, &surface_impl));
struct forward_surface *fwd_surface = wl_resource_get_user_data(resource);
struct forward_surface *sib_surface = wl_resource_get_user_data(sibling);
if (fwd_surface == sib_surface) {
wl_resource_post_error(resource, WL_SUBSURFACE_ERROR_BAD_SURFACE,
"surface cannot be placed above itself");
return;
}
struct forward_surface *parent = fwd_surface->subsurface_parent;
if (!fwd_surface->subsurface_parent || fwd_surface->inert) {
// Subsurface parent destroyed, ignore
return;
}
assert(!wl_list_empty(&fwd_surface->subsurf_pending_entry.link));
if (parent == sib_surface) {
wl_list_remove(&fwd_surface->subsurf_pending_entry.link);
wl_list_insert(&parent->pending.subsurfaces_above,
&fwd_surface->subsurf_pending_entry.link);
} else if (parent == sib_surface->subsurface_parent) {
wl_list_remove(&fwd_surface->subsurf_pending_entry.link);
wl_list_insert(&sib_surface->subsurf_pending_entry.link,
&fwd_surface->subsurf_pending_entry.link);
} else {
wl_resource_post_error(resource, WL_SUBSURFACE_ERROR_BAD_SURFACE,
"surface neither sibling nor parent");
return;
}
}
static void nested_subsurface_place_below(struct wl_client *client,
struct wl_resource *resource, struct wl_resource *sibling) {
assert(wl_resource_instance_of(resource, &wl_subsurface_interface, &subsurface_impl));
assert(wl_resource_instance_of(sibling, &wl_surface_interface, &surface_impl));
struct forward_surface *fwd_surface = wl_resource_get_user_data(resource);
struct forward_surface *sib_surface = wl_resource_get_user_data(sibling);
if (fwd_surface == sib_surface) {
wl_resource_post_error(resource, WL_SUBSURFACE_ERROR_BAD_SURFACE,
"surface cannot be placed below itself");
return;
}
struct forward_surface *parent = fwd_surface->subsurface_parent;
if (!fwd_surface->subsurface_parent || fwd_surface->inert) {
// Subsurface parent destroyed, ignore
return;
}
assert(!wl_list_empty(&fwd_surface->subsurf_pending_entry.link));
if (parent == sib_surface) {
wl_list_remove(&fwd_surface->subsurf_pending_entry.link);
wl_list_insert_before(&parent->pending.subsurfaces_below,
&fwd_surface->subsurf_pending_entry.link);
} else if (parent == sib_surface->subsurface_parent) {
wl_list_remove(&fwd_surface->subsurf_pending_entry.link);
wl_list_insert_before(&sib_surface->subsurf_pending_entry.link,
&fwd_surface->subsurf_pending_entry.link);
} else {
wl_resource_post_error(resource, WL_SUBSURFACE_ERROR_BAD_SURFACE,
"surface neither sibling nor parent");
}
}
static void nested_subsurface_set_sync(struct wl_client *client,
struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_subsurface_interface, &subsurface_impl));
struct forward_surface *fwd_surface = wl_resource_get_user_data(resource);
if (!fwd_surface->subsurface_parent || fwd_surface->inert) {
// Subsurface parent destroyed, ignore
return;
}
assert(!wl_list_empty(&fwd_surface->subsurf_pending_entry.link));
fwd_surface->subsurf_pending_entry.is_desync = false;
}
static void nested_subsurface_set_desync(struct wl_client *client,
struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_subsurface_interface, &subsurface_impl));
struct forward_surface *fwd_surface = wl_resource_get_user_data(resource);
if (!fwd_surface->subsurface_parent || fwd_surface->inert) {
// Subsurface parent destroyed, ignore
return;
}
assert(!wl_list_empty(&fwd_surface->subsurf_pending_entry.link));
fwd_surface->subsurf_pending_entry.is_desync = true;
}
static const struct wl_subsurface_interface subsurface_impl = {
.destroy = nested_subsurface_destroy,
.set_position = nested_subsurface_set_position,
.place_above = nested_subsurface_place_above,
.place_below = nested_subsurface_place_below,
.set_sync = nested_subsurface_set_sync,
.set_desync = nested_subsurface_set_desync
};
static void subsurface_handle_resource_destroy(struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_subsurface_interface, &subsurface_impl));
struct forward_surface *fwd_surface = wl_resource_get_user_data(resource);
if (!fwd_surface) {
return;
}
destroy_subsurface_parts(fwd_surface);
}
static void subcompositor_destroy(struct wl_client *client, struct wl_resource *resource) {
wl_resource_destroy(resource);
}
static void subcompositor_get_subsurface(struct wl_client *client,
struct wl_resource *resource, uint32_t id, struct wl_resource *child,
struct wl_resource *parent) {
assert(wl_resource_instance_of(child, &wl_surface_interface, &surface_impl));
assert(wl_resource_instance_of(parent, &wl_surface_interface, &surface_impl));
struct forward_surface *child_surf = wl_resource_get_user_data(child);
struct forward_surface *parent_surf = wl_resource_get_user_data(parent);
if (child_surf->layer_surface) {
wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
"child surface already has a role (layer surface)");
return;
}
if (child_surf->subsurface_parent) {
wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
"child surface already has a subsurface");
return;
}
struct forward_surface *ancestor = parent_surf;
do {
if (ancestor == child_surf) {
wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_PARENT,
"parent surface is or descends from child");
return;