This repository was archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsommelier.cc
4542 lines (4019 loc) · 162 KB
/
sommelier.cc
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
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "compositor/sommelier-linux-dmabuf.h" // NOLINT(build/include_directory)
#include "sommelier.h" // NOLINT(build/include_directory)
#include <cstdint>
#include <cstring>
#include "sommelier-logging.h" // NOLINT(build/include_directory)
#include "sommelier-scope-timer.h" // NOLINT(build/include_directory)
#include "sommelier-tracing.h" // NOLINT(build/include_directory)
#include "sommelier-transform.h" // NOLINT(build/include_directory)
#include "sommelier-window.h" // NOLINT(build/include_directory)
#include "sommelier-xdg-shell.h" // NOLINT(build/include_directory)
#include "sommelier-xshape.h" // NOLINT(build/include_directory)
#ifdef GAMEPAD_SUPPORT
#include "libevdev/libevdev-shim.h"
#endif
#include "viewporter-shim.h" // NOLINT(build/include_directory)
#include "xcb/xcb-shim.h"
#include <assert.h>
#include <cstdlib>
#include <errno.h>
#include <fcntl.h>
#include <gbm.h>
#include <libgen.h>
#include <limits>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include <string>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>
#include <wayland-client.h>
#include <xcb/composite.h>
#include <xcb/shape.h>
#include <xcb/xcb.h>
#include <xcb/xfixes.h>
#include <xcb/xproto.h>
#include "aura-shell-client-protocol.h" // NOLINT(build/include_directory)
#include "drm-server-protocol.h" // NOLINT(build/include_directory)
#include "fractional-scale-v1-client-protocol.h" // NOLINT(build/include_directory)
#ifdef GAMEPAD_SUPPORT
#include "gaming-input-unstable-v2-client-protocol.h" // NOLINT(build/include_directory)
#endif
#include "idle-inhibit-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "keyboard-extension-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "linux-dmabuf-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "linux-explicit-synchronization-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "pointer-constraints-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "relative-pointer-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "stylus-unstable-v2-client-protocol.h" // NOLINT(build/include_directory)
#include "text-input-extension-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "text-input-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "viewporter-client-protocol.h" // NOLINT(build/include_directory)
#include "xdg-output-unstable-v1-client-protocol.h" // NOLINT(build/include_directory)
#include "xdg-shell-client-protocol.h" // NOLINT(build/include_directory)
#include "xdg-shell-shim.h" // NOLINT(build/include_directory)
#ifdef QUIRKS_SUPPORT
#include "quirks/sommelier-quirks.h"
#endif
// Check that required macro definitions exist.
#ifndef XWAYLAND_PATH
#error XWAYLAND_PATH must be defined
#endif
#ifndef XWAYLAND_GL_DRIVER_PATH
#error XWAYLAND_GL_DRIVER_PATH must be defined
#endif
#ifndef FRAME_COLOR
#error FRAME_COLOR must be defined
#endif
#ifndef DARK_FRAME_COLOR
#error DARK_FRAME_COLOR must be defined
#endif
struct sl_data_source {
struct sl_context* ctx;
struct wl_data_source* internal;
};
#define SEND_EVENT_MASK 0x80
#define MIN_SCALE 0.1
#define MAX_SCALE 10.0
#define MIN_DPI 72
#define MAX_DPI 9600
#define XCURSOR_SIZE_BASE 24
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif
#define LOCK_SUFFIX ".lock"
#define LOCK_SUFFIXLEN 5
#define MIN_AURA_SHELL_VERSION 6
#define MAX_AURA_SHELL_VERSION 38
static const char STEAM_APP_CLASS_PREFIX[] = "steam_app_";
int sl_open_wayland_socket(const char* socket_name,
struct sockaddr_un* addr,
int* lock_fd,
int* sock_fd);
const char* net_wm_state_to_string(int i) {
switch (i) {
case NET_WM_STATE_REMOVE:
return "NET_WM_STATE_REMOVE";
case NET_WM_STATE_ADD:
return "NET_WM_STATE_ADD";
case NET_WM_STATE_TOGGLE:
return "NET_WM_STATE_TOGGLE";
}
return "<unknown NET_WM_STATE>";
}
struct sl_sync_point* sl_sync_point_create(int fd) {
TRACE_EVENT("sync", "sl_sync_point_create");
struct sl_sync_point* sync_point = new sl_sync_point();
sync_point->fd = fd;
sync_point->sync = nullptr;
return sync_point;
}
void sl_sync_point_destroy(struct sl_sync_point* sync_point) {
TRACE_EVENT("sync", "sl_sync_point_destroy");
close(sync_point->fd);
delete sync_point;
}
static void sl_internal_xdg_shell_ping(void* data,
struct xdg_wm_base* xdg_shell,
uint32_t serial) {
TRACE_EVENT("shell", "sl_internal_xdg_shell_ping");
xdg_wm_base_pong(xdg_shell, serial);
}
static const struct xdg_wm_base_listener sl_internal_xdg_shell_listener = {
sl_internal_xdg_shell_ping};
static void sl_adjust_window_size_for_screen_size(struct sl_window* window) {
TRACE_EVENT("surface", "sl_adjust_window_size_for_screen_size", "id",
window->id);
struct sl_context* ctx = window->ctx;
if (!sl_window_is_containerized(window)) {
// Clamp size to screen if the window is not containerized. Containerized
// window's size can only be modified by the X11 client.
window->width = MIN(window->width, ctx->screen->width_in_pixels);
window->height = MIN(window->height, ctx->screen->height_in_pixels);
}
}
static void sl_adjust_window_position_for_screen_size(
struct sl_window* window) {
struct sl_context* ctx = window->ctx;
const sl_host_output* output =
window->paired_surface ? window->paired_surface->output.get() : nullptr;
if (output) {
window->x =
output->virt_x + (output->virt_rotated_width - window->width) / 2;
window->y = (output->virt_rotated_height - window->height) / 2;
} else {
// Center horizontally/vertically.
window->x = ctx->screen->width_in_pixels / 2 - window->width / 2;
window->y = ctx->screen->height_in_pixels / 2 - window->height / 2;
}
}
static void sl_set_input_focus(struct sl_context* ctx,
struct sl_window* window) {
if (window) {
xcb_client_message_event_t event;
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.window = window->id;
event.type = ctx->atoms[ATOM_WM_PROTOCOLS].value;
event.data.data32[0] = ctx->atoms[ATOM_WM_TAKE_FOCUS].value;
event.data.data32[1] = XCB_CURRENT_TIME;
if (!window->managed)
return;
if (window->focus_model_take_focus) {
xcb_send_event(ctx->connection, 0, window->id, XCB_EVENT_MASK_NO_EVENT,
reinterpret_cast<char*>(&event));
}
xcb_set_input_focus(ctx->connection, XCB_INPUT_FOCUS_NONE, window->id,
XCB_CURRENT_TIME);
} else {
xcb_set_input_focus(ctx->connection, XCB_INPUT_FOCUS_NONE, XCB_NONE,
XCB_CURRENT_TIME);
}
}
void sl_restack_windows(struct sl_context* ctx, uint32_t focus_resource_id) {
struct sl_window* sibling;
uint32_t values[1];
wl_list_for_each(sibling, &ctx->windows, link) {
if (!sibling->managed)
continue;
// Move focus window to the top and all other windows to the bottom.
values[0] = sibling->host_surface_id == focus_resource_id
? XCB_STACK_MODE_ABOVE
: XCB_STACK_MODE_BELOW;
xcb_configure_window(ctx->connection, sibling->frame_id,
XCB_CONFIG_WINDOW_STACK_MODE, values);
}
}
void sl_roundtrip(struct sl_context* ctx) {
TRACE_EVENT("other", "sl_roundtrip", "id",
ctx->application_id != nullptr ? ctx->application_id : "<null>");
free(xcb_get_input_focus_reply(
ctx->connection, xcb_get_input_focus(ctx->connection), nullptr));
}
static void sl_window_set_wm_state(struct sl_window* window, int state) {
TRACE_EVENT("surface", "sl_window_set_wm_state", "id", window->id);
struct sl_context* ctx = window->ctx;
uint32_t values[2];
values[0] = state;
values[1] = XCB_WINDOW_NONE;
xcb_change_property(ctx->connection, XCB_PROP_MODE_REPLACE, window->id,
ctx->atoms[ATOM_WM_STATE].value,
ctx->atoms[ATOM_WM_STATE].value, 32, 2, values);
}
static void sl_host_buffer_destroy(struct wl_client* client,
struct wl_resource* resource) {
TRACE_EVENT("surface", "sl_host_buffer_destroy");
wl_resource_destroy(resource);
}
static const struct wl_buffer_interface sl_buffer_implementation = {
sl_host_buffer_destroy};
static void sl_buffer_release(void* data, struct wl_buffer* buffer) {
struct sl_host_buffer* host =
static_cast<sl_host_buffer*>(wl_buffer_get_user_data(buffer));
auto resource_id = host->resource ? wl_resource_get_id(host->resource) : -1;
TRACE_EVENT("surface", "sl_buffer_release", "resource_id", resource_id);
if (host->ctx->timing != nullptr) {
host->ctx->timing->UpdateLastRelease(resource_id);
}
wl_buffer_send_release(host->resource);
}
static const struct wl_buffer_listener sl_buffer_listener = {sl_buffer_release};
static void sl_destroy_host_buffer(struct wl_resource* resource) {
TRACE_EVENT("surface", "sl_destroy_host_buffer", "resource_id",
resource ? wl_resource_get_id(resource) : -1);
struct sl_host_buffer* host =
static_cast<sl_host_buffer*>(wl_resource_get_user_data(resource));
if (host->proxy)
wl_buffer_destroy(host->proxy);
if (host->shm_mmap) {
host->shm_mmap->buffer_resource = nullptr;
sl_mmap_unref(host->shm_mmap);
}
if (host->sync_point) {
sl_sync_point_destroy(host->sync_point);
}
wl_resource_set_user_data(resource, nullptr);
delete host;
}
struct sl_host_buffer* sl_create_host_buffer(struct sl_context* ctx,
struct wl_client* client,
uint32_t id,
struct wl_buffer* proxy,
int32_t width,
int32_t height,
bool is_drm) {
TRACE_EVENT("surface", "sl_create_host_buffer", "id", id);
struct sl_host_buffer* host_buffer = new sl_host_buffer();
host_buffer->ctx = ctx;
host_buffer->width = width;
host_buffer->height = height;
host_buffer->resource =
wl_resource_create(client, &wl_buffer_interface, 1, id);
wl_resource_set_implementation(host_buffer->resource,
&sl_buffer_implementation, host_buffer,
sl_destroy_host_buffer);
host_buffer->shm_mmap = nullptr;
host_buffer->shm_format = 0;
host_buffer->proxy = proxy;
if (host_buffer->proxy) {
wl_buffer_add_listener(host_buffer->proxy, &sl_buffer_listener,
host_buffer);
}
host_buffer->sync_point = nullptr;
host_buffer->is_drm = is_drm;
return host_buffer;
}
static void sl_internal_data_offer_destroy(struct sl_data_offer* host) {
TRACE_EVENT("other", "sl_internal_data_offer_destroy");
wl_data_offer_destroy(host->internal);
wl_array_release(&host->atoms);
wl_array_release(&host->cookies);
delete host;
}
static void sl_set_selection(struct sl_context* ctx,
struct sl_data_offer* data_offer) {
TRACE_EVENT("other", "sl_set_selection");
if (ctx->selection_data_offer) {
sl_internal_data_offer_destroy(ctx->selection_data_offer);
ctx->selection_data_offer = nullptr;
}
if (ctx->clipboard_manager) {
if (!data_offer) {
if (ctx->selection_owner == ctx->selection_window)
xcb_set_selection_owner(ctx->connection, XCB_ATOM_NONE,
ctx->atoms[ATOM_CLIPBOARD].value,
ctx->selection_timestamp);
return;
}
int atoms = data_offer->cookies.size / sizeof(xcb_intern_atom_cookie_t);
wl_array_add(&data_offer->atoms, sizeof(xcb_atom_t) * (atoms + 2));
(reinterpret_cast<xcb_atom_t*>(data_offer->atoms.data))[0] =
ctx->atoms[ATOM_TARGETS].value;
(reinterpret_cast<xcb_atom_t*>(data_offer->atoms.data))[1] =
ctx->atoms[ATOM_TIMESTAMP].value;
for (int i = 0; i < atoms; i++) {
xcb_intern_atom_cookie_t cookie =
(reinterpret_cast<xcb_intern_atom_cookie_t*>(
data_offer->cookies.data))[i];
xcb_intern_atom_reply_t* reply =
xcb_intern_atom_reply(ctx->connection, cookie, nullptr);
if (reply) {
(reinterpret_cast<xcb_atom_t*>(data_offer->atoms.data))[i + 2] =
reply->atom;
free(reply);
}
}
xcb_set_selection_owner(ctx->connection, ctx->selection_window,
ctx->atoms[ATOM_CLIPBOARD].value, XCB_CURRENT_TIME);
}
ctx->selection_data_offer = data_offer;
}
static void sl_internal_data_offer_offer(void* data,
struct wl_data_offer* data_offer,
const char* type) {
TRACE_EVENT("other", "sl_internal_data_offer_offer");
struct sl_data_offer* host = static_cast<sl_data_offer*>(data);
xcb_intern_atom_cookie_t* cookie = static_cast<xcb_intern_atom_cookie_t*>(
wl_array_add(&host->cookies, sizeof(xcb_intern_atom_cookie_t)));
*cookie = xcb_intern_atom(host->ctx->connection, 0, strlen(type), type);
}
static void sl_internal_data_offer_source_actions(
void* data, struct wl_data_offer* data_offer, uint32_t source_actions) {
TRACE_EVENT("other", "sl_internal_data_offer_source_actions");
}
static void sl_internal_data_offer_action(void* data,
struct wl_data_offer* data_offer,
uint32_t dnd_action) {
TRACE_EVENT("other", "sl_internal_data_offer_action");
}
static const struct wl_data_offer_listener sl_internal_data_offer_listener = {
sl_internal_data_offer_offer, sl_internal_data_offer_source_actions,
sl_internal_data_offer_action};
static void sl_internal_data_device_data_offer(
void* data,
struct wl_data_device* data_device,
struct wl_data_offer* data_offer) {
struct sl_context* ctx = (struct sl_context*)data;
struct sl_data_offer* host_data_offer = new sl_data_offer();
host_data_offer->ctx = ctx;
host_data_offer->internal = data_offer;
wl_array_init(&host_data_offer->atoms);
wl_array_init(&host_data_offer->cookies);
wl_data_offer_add_listener(host_data_offer->internal,
&sl_internal_data_offer_listener, host_data_offer);
}
static void sl_internal_data_device_selection(
void* data,
struct wl_data_device* data_device,
struct wl_data_offer* data_offer) {
struct sl_context* ctx = (struct sl_context*)data;
struct sl_data_offer* host_data_offer =
data_offer
? static_cast<sl_data_offer*>(wl_data_offer_get_user_data(data_offer))
: nullptr;
sl_set_selection(ctx, host_data_offer);
}
static const struct wl_data_device_listener sl_internal_data_device_listener = {
sl_internal_data_device_data_offer,
/*enter=*/DoNothing,
/*leave=*/DoNothing,
/*motion=*/DoNothing,
/*drop=*/DoNothing,
sl_internal_data_device_selection};
void sl_host_seat_added(struct sl_host_seat* host) {
struct sl_context* ctx = host->seat->ctx;
if (ctx->default_seat)
return;
ctx->default_seat = host;
// Get data device for selections.
if (ctx->data_device_manager && ctx->data_device_manager->internal) {
ctx->selection_data_device = wl_data_device_manager_get_data_device(
ctx->data_device_manager->internal, host->proxy);
wl_data_device_add_listener(ctx->selection_data_device,
&sl_internal_data_device_listener, ctx);
}
#ifdef GAMEPAD_SUPPORT
sl_gaming_seat_add_listener(ctx);
#endif
}
void sl_host_seat_removed(struct sl_host_seat* host) {
TRACE_EVENT("other", "sl_host_seat_removed");
if (host->seat->ctx->default_seat == host)
host->seat->ctx->default_seat = nullptr;
}
bool sl_client_supports_interface(const sl_context* ctx,
const wl_client* client,
const wl_interface* interface) {
if (ctx->client == client) {
return true;
}
// Clients created for IME support only receive the minimal set of required
// interfaces.
const char* name = interface->name;
return strcmp(name, "wl_seat") == 0 ||
strcmp(name, "zwp_text_input_manager_v1") == 0 ||
strcmp(name, "zcr_text_input_extension_v1") == 0 ||
strcmp(name, "zcr_text_input_crostini_manager_v1") == 0 ||
strcmp(name, "zcr_text_input_x11_v1") == 0;
}
static void sl_global_destroy(struct sl_global* global) {
TRACE_EVENT("other", "sl_global_destroy");
struct sl_host_registry* registry;
wl_list_for_each(registry, &global->ctx->registries, link) {
if (sl_client_supports_interface(global->ctx,
wl_resource_get_client(registry->resource),
global->interface)) {
wl_resource_post_event(registry->resource, WL_REGISTRY_GLOBAL_REMOVE,
global->name);
}
}
wl_list_remove(&global->link);
free(global);
}
// Called on each "wl_registry::global" event from the host compositor,
// giving Sommelier an opportunity to bind to the new global object
// (so we can receive events or invoke requests on it), and/or forward the
// wl_registry::global event on to our clients.
void sl_registry_handler(void* data,
struct wl_registry* registry,
uint32_t id,
const char* interface,
uint32_t version) {
struct sl_context* ctx = (struct sl_context*)data;
TRACE_EVENT("other", "sl_registry_handler", "id", id);
if (strcmp(interface, "wl_compositor") == 0) {
sl_compositor_init_context(ctx, registry, id, version);
} else if (strcmp(interface, "wl_subcompositor") == 0) {
struct sl_subcompositor* subcompositor =
static_cast<sl_subcompositor*>(malloc(sizeof(struct sl_subcompositor)));
assert(subcompositor);
subcompositor->ctx = ctx;
subcompositor->id = id;
assert(!ctx->subcompositor);
ctx->subcompositor = subcompositor;
subcompositor->host_global = sl_subcompositor_global_create(ctx);
} else if (strcmp(interface, "wl_shm") == 0) {
struct sl_shm* shm = static_cast<sl_shm*>(malloc(sizeof(struct sl_shm)));
assert(shm);
shm->ctx = ctx;
shm->id = id;
shm->internal = static_cast<wl_shm*>(
wl_registry_bind(registry, id, &wl_shm_interface, 1));
assert(!ctx->shm);
ctx->shm = shm;
shm->host_global = sl_shm_global_create(ctx);
} else if (strcmp(interface, "wl_shell") == 0) {
struct sl_shell* shell =
static_cast<sl_shell*>(malloc(sizeof(struct sl_shell)));
assert(shell);
shell->ctx = ctx;
shell->id = id;
assert(!ctx->shell);
ctx->shell = shell;
shell->host_global = sl_shell_global_create(ctx);
} else if (strcmp(interface, "wl_output") == 0) {
struct sl_output* output =
static_cast<sl_output*>(malloc(sizeof(struct sl_output)));
assert(output);
output->ctx = ctx;
output->id = id;
output->version = MIN(3, version);
output->host_global = sl_output_global_create(output);
wl_list_insert(&ctx->outputs, &output->link);
output->host_output = nullptr;
} else if (strcmp(interface, "wl_seat") == 0) {
struct sl_seat* seat =
static_cast<sl_seat*>(malloc(sizeof(struct sl_seat)));
assert(seat);
seat->ctx = ctx;
seat->id = id;
seat->version = MIN(5, version);
seat->last_serial = 0;
seat->stylus_tablet = nullptr;
seat->host_global = sl_seat_global_create(seat);
wl_list_insert(&ctx->seats, &seat->link);
} else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0) {
struct sl_relative_pointer_manager* relative_pointer =
static_cast<sl_relative_pointer_manager*>(
malloc(sizeof(struct sl_relative_pointer_manager)));
assert(relative_pointer);
relative_pointer->ctx = ctx;
relative_pointer->id = id;
assert(!ctx->relative_pointer_manager);
ctx->relative_pointer_manager = relative_pointer;
relative_pointer->host_global =
sl_relative_pointer_manager_global_create(ctx);
} else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0) {
struct sl_pointer_constraints* pointer_constraints =
static_cast<sl_pointer_constraints*>(
malloc(sizeof(struct sl_pointer_constraints)));
assert(pointer_constraints);
pointer_constraints->ctx = ctx;
pointer_constraints->id = id;
assert(!ctx->pointer_constraints);
ctx->pointer_constraints = pointer_constraints;
pointer_constraints->host_global =
sl_pointer_constraints_global_create(ctx);
} else if (strcmp(interface, "wl_data_device_manager") == 0) {
struct sl_data_device_manager* data_device_manager =
static_cast<sl_data_device_manager*>(
malloc(sizeof(struct sl_data_device_manager)));
assert(data_device_manager);
data_device_manager->ctx = ctx;
data_device_manager->id = id;
data_device_manager->version = MIN(3, version);
data_device_manager->internal = nullptr;
data_device_manager->host_global = nullptr;
assert(!ctx->data_device_manager);
ctx->data_device_manager = data_device_manager;
if (ctx->xwayland) {
data_device_manager->internal = static_cast<wl_data_device_manager*>(
wl_registry_bind(registry, id, &wl_data_device_manager_interface,
data_device_manager->version));
} else {
data_device_manager->host_global =
sl_data_device_manager_global_create(ctx);
}
} else if (strcmp(interface, "xdg_wm_base") == 0) {
struct sl_xdg_shell* xdg_shell =
static_cast<sl_xdg_shell*>(malloc(sizeof(struct sl_xdg_shell)));
assert(xdg_shell);
xdg_shell->ctx = ctx;
xdg_shell->id = id;
xdg_shell->version = MIN(SL_XDG_SHELL_MAX_VERSION, version);
xdg_shell->internal = nullptr;
xdg_shell->host_global = nullptr;
assert(!ctx->xdg_shell);
ctx->xdg_shell = xdg_shell;
if (ctx->xwayland) {
xdg_shell->internal = static_cast<xdg_wm_base*>(wl_registry_bind(
registry, id, &xdg_wm_base_interface, xdg_shell->version));
xdg_wm_base_add_listener(xdg_shell->internal,
&sl_internal_xdg_shell_listener, nullptr);
} else {
xdg_shell->host_global =
sl_xdg_shell_global_create(ctx, xdg_shell->version);
}
} else if (strcmp(interface, "zaura_shell") == 0) {
if (version >= MIN_AURA_SHELL_VERSION) {
struct sl_aura_shell* aura_shell =
static_cast<sl_aura_shell*>(malloc(sizeof(struct sl_aura_shell)));
assert(aura_shell);
aura_shell->ctx = ctx;
aura_shell->id = id;
aura_shell->version = MIN(MAX_AURA_SHELL_VERSION, version);
aura_shell->host_gtk_shell_global = nullptr;
aura_shell->internal = static_cast<zaura_shell*>(wl_registry_bind(
registry, id, &zaura_shell_interface, aura_shell->version));
assert(!ctx->aura_shell);
ctx->aura_shell = aura_shell;
aura_shell->host_gtk_shell_global = sl_gtk_shell_global_create(ctx);
}
} else if (strcmp(interface, "wp_viewporter") == 0) {
struct sl_viewporter* viewporter =
static_cast<sl_viewporter*>(malloc(sizeof(struct sl_viewporter)));
assert(viewporter);
viewporter->ctx = ctx;
viewporter->id = id;
viewporter->host_viewporter_global = nullptr;
viewporter->internal = static_cast<wp_viewporter*>(
wl_registry_bind(registry, id, &wp_viewporter_interface, 1));
assert(!ctx->viewporter);
ctx->viewporter = viewporter;
viewporter->host_viewporter_global = sl_viewporter_global_create(ctx);
// Allow non-integer scale.
ctx->scale = MIN(MAX_SCALE, MAX(MIN_SCALE, ctx->desired_scale));
} else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
struct sl_linux_dmabuf* linux_dmabuf =
static_cast<sl_linux_dmabuf*>(malloc(sizeof(struct sl_linux_dmabuf)));
assert(linux_dmabuf);
linux_dmabuf->ctx = ctx;
linux_dmabuf->id = id;
linux_dmabuf->version = MIN(SL_LINUX_DMABUF_MAX_VERSION, version);
linux_dmabuf->host_drm_global = sl_drm_global_create(ctx, linux_dmabuf);
if (ctx->enable_linux_dmabuf) {
linux_dmabuf->host_linux_dmabuf_global =
sl_linux_dmabuf_global_create(ctx, linux_dmabuf);
}
if (linux_dmabuf->version >= 2) {
linux_dmabuf->proxy_v2 = static_cast<zwp_linux_dmabuf_v1*>(
wl_registry_bind(registry, id, &zwp_linux_dmabuf_v1_interface, 2));
}
ctx->linux_dmabuf = linux_dmabuf;
} else if (strcmp(interface, "zwp_linux_explicit_synchronization_v1") == 0) {
struct sl_linux_explicit_synchronization* linux_explicit_synchronization =
static_cast<sl_linux_explicit_synchronization*>(
malloc(sizeof(struct sl_linux_explicit_synchronization)));
assert(linux_explicit_synchronization);
linux_explicit_synchronization->ctx = ctx;
linux_explicit_synchronization->id = id;
linux_explicit_synchronization->internal =
static_cast<zwp_linux_explicit_synchronization_v1*>(wl_registry_bind(
registry, id, &zwp_linux_explicit_synchronization_v1_interface, 1));
assert(!ctx->linux_explicit_synchronization);
ctx->linux_explicit_synchronization = linux_explicit_synchronization;
} else if (strcmp(interface, "zcr_keyboard_extension_v1") == 0) {
struct sl_keyboard_extension* keyboard_extension =
static_cast<sl_keyboard_extension*>(
malloc(sizeof(struct sl_keyboard_extension)));
assert(keyboard_extension);
keyboard_extension->ctx = ctx;
keyboard_extension->id = id;
keyboard_extension->internal =
static_cast<zcr_keyboard_extension_v1*>(wl_registry_bind(
registry, id, &zcr_keyboard_extension_v1_interface, 1));
assert(!ctx->keyboard_extension);
ctx->keyboard_extension = keyboard_extension;
} else if (strcmp(interface, "zwp_text_input_manager_v1") == 0) {
struct sl_text_input_manager* text_input_manager =
static_cast<sl_text_input_manager*>(
malloc(sizeof(struct sl_text_input_manager)));
assert(text_input_manager);
text_input_manager->ctx = ctx;
text_input_manager->id = id;
text_input_manager->host_global =
sl_text_input_manager_global_create(ctx, version);
text_input_manager->host_crostini_manager_global =
sl_text_input_crostini_manager_global_create(ctx);
text_input_manager->host_x11_global = sl_text_input_x11_global_create(ctx);
assert(!ctx->text_input_manager);
ctx->text_input_manager = text_input_manager;
} else if (strcmp(interface, "zcr_text_input_extension_v1") == 0) {
struct sl_text_input_extension* text_input_extension =
static_cast<sl_text_input_extension*>(
malloc(sizeof(struct sl_text_input_extension)));
assert(text_input_extension);
text_input_extension->ctx = ctx;
text_input_extension->id = id;
text_input_extension->host_global =
sl_text_input_extension_global_create(ctx, version);
assert(!ctx->text_input_extension);
ctx->text_input_extension = text_input_extension;
#ifdef GAMEPAD_SUPPORT
} else if (strcmp(interface, "zcr_gaming_input_v2") == 0) {
struct sl_gaming_input_manager* gaming_input_manager =
static_cast<sl_gaming_input_manager*>(
malloc(sizeof(struct sl_gaming_input_manager)));
assert(gaming_input_manager);
gaming_input_manager->ctx = ctx;
gaming_input_manager->id = id;
gaming_input_manager->internal = static_cast<zcr_gaming_input_v2*>(
wl_registry_bind(registry, id, &zcr_gaming_input_v2_interface, 2));
assert(!ctx->gaming_input_manager);
ctx->gaming_input_manager = gaming_input_manager;
#endif
} else if (strcmp(interface, "zcr_stylus_v2") == 0) {
struct sl_stylus_input_manager* stylus_input_manager =
static_cast<sl_stylus_input_manager*>(
malloc(sizeof(struct sl_stylus_input_manager)));
assert(stylus_input_manager);
stylus_input_manager->ctx = ctx;
stylus_input_manager->id = id;
stylus_input_manager->internal = static_cast<zcr_stylus_v2*>(
wl_registry_bind(registry, id, &zcr_stylus_v2_interface, 1));
// Note: This does not forward the stylus-unstable-v2 protcol to the
// clients. Instead, it exposes tablet-unstable-v2 protocol to the clients.
// Note: This is the only user of ctx->stylus_input_manager
stylus_input_manager->tablet_host_global =
sl_stylus_to_tablet_manager_global_create(ctx);
assert(!ctx->stylus_input_manager);
ctx->stylus_input_manager = stylus_input_manager;
} else if (strcmp(interface, "zxdg_output_manager_v1") == 0 &&
ctx->use_direct_scale) {
// This protocol cannot be bound unconditionally as doing so
// causes issues under Crostini. For this reason we will only
// bind it when we need to do so (direct mode enabled).
struct sl_xdg_output_manager* output_manager =
static_cast<sl_xdg_output_manager*>(
malloc(sizeof(struct sl_xdg_output_manager)));
assert(output_manager);
output_manager->ctx = ctx;
output_manager->id = id;
output_manager->internal =
static_cast<zxdg_output_manager_v1*>(wl_registry_bind(
registry, id, &zxdg_output_manager_v1_interface, MIN(3, version)));
ctx->xdg_output_manager = output_manager;
} else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) ==
0) {
struct sl_fractional_scale_manager* fractional_scale_manager =
static_cast<sl_fractional_scale_manager*>(
malloc(sizeof(struct sl_fractional_scale_manager)));
assert(fractional_scale_manager);
fractional_scale_manager->ctx = ctx;
fractional_scale_manager->id = id;
fractional_scale_manager->host_fractional_scale_manager_global = nullptr;
fractional_scale_manager->internal =
static_cast<wp_fractional_scale_manager_v1*>(wl_registry_bind(
registry, id, &wp_fractional_scale_manager_v1_interface, 1));
assert(!ctx->fractional_scale_manager);
ctx->fractional_scale_manager = fractional_scale_manager;
fractional_scale_manager->host_fractional_scale_manager_global =
sl_fractional_scale_manager_global_create(ctx);
} else if (strcmp(interface, "zwp_idle_inhibit_manager_v1") == 0) {
struct sl_idle_inhibit_manager* idle_inhibit =
static_cast<sl_idle_inhibit_manager*>(
malloc(sizeof(struct sl_idle_inhibit_manager)));
assert(idle_inhibit);
idle_inhibit->ctx = ctx;
idle_inhibit->id = id;
assert(!ctx->idle_inhibit_manager);
ctx->idle_inhibit_manager = idle_inhibit;
idle_inhibit->host_global = sl_idle_inhibit_manager_global_create(ctx);
}
}
void sl_registry_remover(void* data,
struct wl_registry* registry,
uint32_t id) {
TRACE_EVENT("other", "sl_registry_remover");
struct sl_context* ctx = (struct sl_context*)data;
struct sl_output* output;
struct sl_seat* seat;
if (ctx->compositor && ctx->compositor->id == id) {
sl_global_destroy(ctx->compositor->host_global);
wl_compositor_destroy(ctx->compositor->internal);
free(ctx->compositor);
ctx->compositor = nullptr;
return;
}
if (ctx->subcompositor && ctx->subcompositor->id == id) {
sl_global_destroy(ctx->subcompositor->host_global);
wl_shm_destroy(ctx->shm->internal);
free(ctx->subcompositor);
ctx->subcompositor = nullptr;
return;
}
if (ctx->shm && ctx->shm->id == id) {
sl_global_destroy(ctx->shm->host_global);
free(ctx->shm);
ctx->shm = nullptr;
return;
}
if (ctx->shell && ctx->shell->id == id) {
sl_global_destroy(ctx->shell->host_global);
free(ctx->shell);
ctx->shell = nullptr;
return;
}
if (ctx->data_device_manager && ctx->data_device_manager->id == id) {
if (ctx->data_device_manager->host_global)
sl_global_destroy(ctx->data_device_manager->host_global);
if (ctx->data_device_manager->internal)
wl_data_device_manager_destroy(ctx->data_device_manager->internal);
free(ctx->data_device_manager);
ctx->data_device_manager = nullptr;
return;
}
if (ctx->xdg_shell && ctx->xdg_shell->id == id) {
if (ctx->xdg_shell->host_global)
sl_global_destroy(ctx->xdg_shell->host_global);
if (ctx->xdg_shell->internal)
xdg_wm_base_destroy(ctx->xdg_shell->internal);
free(ctx->xdg_shell);
ctx->xdg_shell = nullptr;
return;
}
if (ctx->aura_shell && ctx->aura_shell->id == id) {
if (ctx->aura_shell->host_gtk_shell_global)
sl_global_destroy(ctx->aura_shell->host_gtk_shell_global);
zaura_shell_destroy(ctx->aura_shell->internal);
free(ctx->aura_shell);
ctx->aura_shell = nullptr;
return;
}
if (ctx->viewporter && ctx->viewporter->id == id) {
if (ctx->viewporter->host_viewporter_global)
sl_global_destroy(ctx->viewporter->host_viewporter_global);
wp_viewporter_destroy(ctx->viewporter->internal);
free(ctx->viewporter);
ctx->viewporter = nullptr;
return;
}
if (ctx->linux_dmabuf && ctx->linux_dmabuf->id == id) {
if (ctx->linux_dmabuf->host_drm_global)
sl_global_destroy(ctx->linux_dmabuf->host_drm_global);
if (ctx->linux_dmabuf->proxy_v2)
zwp_linux_dmabuf_v1_destroy(ctx->linux_dmabuf->proxy_v2);
if (ctx->linux_dmabuf->host_linux_dmabuf_global)
sl_global_destroy(ctx->linux_dmabuf->host_linux_dmabuf_global);
free(ctx->linux_dmabuf);
ctx->linux_dmabuf = nullptr;
return;
}
if (ctx->linux_explicit_synchronization &&
ctx->linux_explicit_synchronization->id == id) {
zwp_linux_explicit_synchronization_v1_destroy(
ctx->linux_explicit_synchronization->internal);
free(ctx->linux_explicit_synchronization);
ctx->linux_explicit_synchronization = nullptr;
return;
}
if (ctx->keyboard_extension && ctx->keyboard_extension->id == id) {
zcr_keyboard_extension_v1_destroy(ctx->keyboard_extension->internal);
free(ctx->keyboard_extension);
ctx->keyboard_extension = nullptr;
return;
}
if (ctx->text_input_manager && ctx->text_input_manager->id == id) {
sl_global_destroy(ctx->text_input_manager->host_global);
free(ctx->text_input_manager);
ctx->text_input_manager = nullptr;
return;
}
if (ctx->text_input_extension && ctx->text_input_extension->id == id) {
sl_global_destroy(ctx->text_input_extension->host_global);
free(ctx->text_input_extension);
ctx->text_input_extension = nullptr;
return;
}
#ifdef GAMEPAD_SUPPORT
if (ctx->gaming_input_manager && ctx->gaming_input_manager->id == id) {
zcr_gaming_input_v2_destroy(ctx->gaming_input_manager->internal);
free(ctx->gaming_input_manager);
ctx->gaming_input_manager = nullptr;
return;
}
#endif
if (ctx->stylus_input_manager && ctx->stylus_input_manager->id == id) {
sl_global_destroy(ctx->stylus_input_manager->tablet_host_global);
free(ctx->stylus_input_manager);
ctx->stylus_input_manager = nullptr;
}
if (ctx->relative_pointer_manager &&
ctx->relative_pointer_manager->id == id) {
sl_global_destroy(ctx->relative_pointer_manager->host_global);
free(ctx->relative_pointer_manager);
ctx->relative_pointer_manager = nullptr;
return;
}
if (ctx->pointer_constraints && ctx->pointer_constraints->id == id) {
sl_global_destroy(ctx->pointer_constraints->host_global);
free(ctx->pointer_constraints);
ctx->pointer_constraints = nullptr;
return;
}
if (ctx->idle_inhibit_manager && ctx->idle_inhibit_manager->id == id) {
sl_global_destroy(ctx->idle_inhibit_manager->host_global);
free(ctx->idle_inhibit_manager);
ctx->idle_inhibit_manager = nullptr;
return;
}
wl_list_for_each(output, &ctx->outputs, link) {
if (output->id == id) {
sl_global_destroy(output->host_global);
if (output->host_output)
wl_resource_destroy(output->host_output->resource);
wl_list_remove(&output->link);
free(output);
return;
}
}
wl_list_for_each(seat, &ctx->seats, link) {
if (seat->id == id) {
sl_global_destroy(seat->host_global);
wl_list_remove(&seat->link);
free(seat);
return;
}
}
// Not reached.
assert(0);
}
const struct wl_registry_listener sl_registry_listener = {sl_registry_handler,
sl_registry_remover};
static int sl_handle_event(int fd, uint32_t mask, void* data) {
TRACE_EVENT("other", "sl_handle_event");
struct sl_context* ctx = (struct sl_context*)data;
int count = 0;
if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
wl_client_flush(ctx->client);
exit(EXIT_SUCCESS);
}
if (mask & WL_EVENT_READABLE)
count = wl_display_dispatch(ctx->display);
if (mask & WL_EVENT_WRITABLE)
wl_display_flush(ctx->display);
if (mask == 0) {
count = wl_display_dispatch_pending(ctx->display);
wl_display_flush(ctx->display);
}
return count;
}
void sl_create_window(struct sl_context* ctx,
xcb_window_t id,
int x,
int y,
int width,
int height,
int border_width) {
TRACE_EVENT("surface", "sl_create_window");
sl_window* window = new sl_window(ctx, id, x, y, width, height, border_width);
uint32_t values[1];
values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_FOCUS_CHANGE;
xcb()->change_window_attributes(ctx->connection, window->id,
XCB_CW_EVENT_MASK, values);
// Also enable shape events for this window to come in if the xshape
// flag has been enabled