forked from colemickens/platform2-sommelier
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sommelier.c
4395 lines (3824 loc) · 146 KB
/
sommelier.c
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 Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sommelier.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <gbm.h>
#include <libgen.h>
#include <linux/virtwl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#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 <wayland-client.h>
#include <xcb/composite.h>
#include <xcb/xfixes.h>
#include <xcb/xproto.h>
#ifdef __FreeBSD__
#include <signal.h>
#include <sys/ucred.h>
#endif
#include "aura-shell-client-protocol.h"
#include "drm-server-protocol.h"
#include "keyboard-extension-unstable-v1-client-protocol.h"
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
#include "text-input-unstable-v1-client-protocol.h"
#include "viewporter-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#define errno_assert(rv) \
{ \
int macro_private_assert_value = (rv); \
if (!macro_private_assert_value) { \
fprintf(stderr, "Unexpected error: %s\n", strerror(errno)); \
assert(false); \
} \
}
// 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 XWAYLAND_SHM_DRIVER
#error XWAYLAND_SHM_DRIVER must be defined
#endif
#ifndef SHM_DRIVER
#error SHM_DRIVER must be defined
#endif
#ifndef PEER_CMD_PREFIX
#error PEER_CMD_PREFIX 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;
};
enum {
PROPERTY_WM_NAME,
PROPERTY_WM_CLASS,
PROPERTY_WM_TRANSIENT_FOR,
PROPERTY_WM_NORMAL_HINTS,
PROPERTY_WM_CLIENT_LEADER,
PROPERTY_WM_PROTOCOLS,
PROPERTY_MOTIF_WM_HINTS,
PROPERTY_NET_STARTUP_ID,
PROPERTY_NET_WM_STATE,
PROPERTY_GTK_THEME_VARIANT,
};
#define US_POSITION (1L << 0)
#define US_SIZE (1L << 1)
#define P_POSITION (1L << 2)
#define P_SIZE (1L << 3)
#define P_MIN_SIZE (1L << 4)
#define P_MAX_SIZE (1L << 5)
#define P_RESIZE_INC (1L << 6)
#define P_ASPECT (1L << 7)
#define P_BASE_SIZE (1L << 8)
#define P_WIN_GRAVITY (1L << 9)
struct sl_wm_size_hints {
uint32_t flags;
int32_t x, y;
int32_t width, height;
int32_t min_width, min_height;
int32_t max_width, max_height;
int32_t width_inc, height_inc;
struct {
int32_t x;
int32_t y;
} min_aspect, max_aspect;
int32_t base_width, base_height;
int32_t win_gravity;
};
// WM_HINTS is defined at: https://tronche.com/gui/x/icccm/sec-4.html
#define WM_HINTS_FLAG_INPUT (1L << 0)
#define WM_HINTS_FLAG_STATE (1L << 1)
#define WM_HINTS_FLAG_ICON_PIXMAP (1L << 2)
#define WM_HINTS_FLAG_ICON_WINDOW (1L << 3)
#define WM_HINTS_FLAG_ICON_POSITION (1L << 4)
#define WM_HINTS_FLAG_ICON_MASK (1L << 5)
#define WM_HINTS_FLAG_WINDOW_GROUP (1L << 6)
#define WM_HINTS_FLAG_MESSAGE (1L << 7)
#define WM_HINTS_FLAG_URGENCY (1L << 8)
struct sl_wm_hints {
uint32_t flags;
uint32_t input;
uint32_t initiali_state;
xcb_pixmap_t icon_pixmap;
xcb_window_t icon_window;
int32_t icon_x;
int32_t icon_y;
xcb_pixmap_t icon_mask;
};
#define MWM_HINTS_FUNCTIONS (1L << 0)
#define MWM_HINTS_DECORATIONS (1L << 1)
#define MWM_HINTS_INPUT_MODE (1L << 2)
#define MWM_HINTS_STATUS (1L << 3)
#define MWM_DECOR_ALL (1L << 0)
#define MWM_DECOR_BORDER (1L << 1)
#define MWM_DECOR_RESIZEH (1L << 2)
#define MWM_DECOR_TITLE (1L << 3)
#define MWM_DECOR_MENU (1L << 4)
#define MWM_DECOR_MINIMIZE (1L << 5)
#define MWM_DECOR_MAXIMIZE (1L << 6)
struct sl_mwm_hints {
uint32_t flags;
uint32_t functions;
uint32_t decorations;
int32_t input_mode;
uint32_t status;
};
#define NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
#define NET_WM_MOVERESIZE_SIZE_TOP 1
#define NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
#define NET_WM_MOVERESIZE_SIZE_RIGHT 3
#define NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
#define NET_WM_MOVERESIZE_SIZE_BOTTOM 5
#define NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
#define NET_WM_MOVERESIZE_SIZE_LEFT 7
#define NET_WM_MOVERESIZE_MOVE 8
#define NET_WM_STATE_REMOVE 0
#define NET_WM_STATE_ADD 1
#define NET_WM_STATE_TOGGLE 2
#define WM_STATE_WITHDRAWN 0
#define WM_STATE_NORMAL 1
#define WM_STATE_ICONIC 3
#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 APPLICATION_ID_FORMAT_PREFIX "org.chromium.termina"
#define XID_APPLICATION_ID_FORMAT APPLICATION_ID_FORMAT_PREFIX ".xid.%d"
#define WM_CLIENT_LEADER_APPLICATION_ID_FORMAT \
APPLICATION_ID_FORMAT_PREFIX ".wmclientleader.%d"
#define WM_CLASS_APPLICATION_ID_FORMAT \
APPLICATION_ID_FORMAT_PREFIX ".wmclass.%s"
#define MIN_AURA_SHELL_VERSION 6
#define MAX_AURA_SHELL_VERSION 10
// Performs an asprintf operation and checks the result for validity and calls
// abort() if there's a failure. Returns a newly allocated string rather than
// taking a double pointer argument like asprintf.
__attribute__((__format__(__printf__, 1, 0))) static char* sl_xasprintf(
const char* fmt, ...) {
char* str;
va_list args;
va_start(args, fmt);
int rv = vasprintf(&str, fmt, args);
assert(rv >= 0);
UNUSED(rv);
va_end(args);
return str;
}
struct sl_mmap* sl_mmap_create(int fd,
size_t size,
size_t bpp,
size_t num_planes,
size_t offset0,
size_t stride0,
size_t offset1,
size_t stride1,
size_t y_ss0,
size_t y_ss1) {
struct sl_mmap* map;
map = malloc(sizeof(*map));
map->refcount = 1;
map->fd = fd;
map->size = size;
map->num_planes = num_planes;
map->bpp = bpp;
map->offset[0] = offset0;
map->stride[0] = stride0;
map->offset[1] = offset1;
map->stride[1] = stride1;
map->y_ss[0] = y_ss0;
map->y_ss[1] = y_ss1;
map->begin_write = NULL;
map->end_write = NULL;
map->buffer_resource = NULL;
map->addr =
mmap(NULL, size + offset0, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
errno_assert(map->addr != MAP_FAILED);
return map;
}
struct sl_mmap* sl_mmap_ref(struct sl_mmap* map) {
map->refcount++;
return map;
}
void sl_mmap_unref(struct sl_mmap* map) {
if (map->refcount-- == 1) {
munmap(map->addr, map->size + map->offset[0]);
if (map->fd != -1)
close(map->fd);
free(map);
}
}
struct sl_sync_point* sl_sync_point_create(int fd) {
struct sl_sync_point* sync_point;
sync_point = malloc(sizeof(*sync_point));
sync_point->fd = fd;
sync_point->sync = NULL;
return sync_point;
}
void sl_sync_point_destroy(struct sl_sync_point* sync_point) {
close(sync_point->fd);
free(sync_point);
}
static void sl_internal_xdg_shell_ping(void* data,
struct xdg_wm_base* xdg_shell,
uint32_t serial) {
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_send_configure_notify(struct sl_window* window) {
xcb_configure_notify_event_t event = {
.response_type = XCB_CONFIGURE_NOTIFY,
.event = window->id,
.window = window->id,
.above_sibling = XCB_WINDOW_NONE,
.x = window->x,
.y = window->y,
.width = window->width,
.height = window->height,
.border_width = window->border_width,
.override_redirect = 0,
.pad0 = 0,
.pad1 = 0,
};
xcb_send_event(window->ctx->connection, 0, window->id,
XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)&event);
}
static void sl_adjust_window_size_for_screen_size(struct sl_window* window) {
struct sl_context* ctx = window->ctx;
// Clamp size to screen.
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;
// 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_configure_window(struct sl_window* window) {
assert(!window->pending_config.serial);
if (window->next_config.mask) {
int values[5];
int x = window->x;
int y = window->y;
int i = 0;
xcb_configure_window(window->ctx->connection, window->frame_id,
window->next_config.mask, window->next_config.values);
if (window->next_config.mask & XCB_CONFIG_WINDOW_X)
x = window->next_config.values[i++];
if (window->next_config.mask & XCB_CONFIG_WINDOW_Y)
y = window->next_config.values[i++];
if (window->next_config.mask & XCB_CONFIG_WINDOW_WIDTH)
window->width = window->next_config.values[i++];
if (window->next_config.mask & XCB_CONFIG_WINDOW_HEIGHT)
window->height = window->next_config.values[i++];
if (window->next_config.mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
window->border_width = window->next_config.values[i++];
// Set x/y to origin in case window gravity is not northwest as expected.
assert(window->managed);
values[0] = 0;
values[1] = 0;
values[2] = window->width;
values[3] = window->height;
values[4] = window->border_width;
xcb_configure_window(
window->ctx->connection, window->id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_BORDER_WIDTH,
values);
if (x != window->x || y != window->y) {
window->x = x;
window->y = y;
sl_send_configure_notify(window);
}
}
if (window->managed) {
xcb_change_property(window->ctx->connection, XCB_PROP_MODE_REPLACE,
window->id, window->ctx->atoms[ATOM_NET_WM_STATE].value,
XCB_ATOM_ATOM, 32, window->next_config.states_length,
window->next_config.states);
}
window->pending_config = window->next_config;
window->next_config.serial = 0;
window->next_config.mask = 0;
window->next_config.states_length = 0;
}
static void sl_set_input_focus(struct sl_context* ctx,
struct sl_window* window) {
if (window) {
xcb_client_message_event_t event = {
.response_type = XCB_CLIENT_MESSAGE,
.format = 32,
.window = window->id,
.type = ctx->atoms[ATOM_WM_PROTOCOLS].value,
.data.data32 =
{
ctx->atoms[ATOM_WM_TAKE_FOCUS].value,
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,
(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) {
free(xcb_get_input_focus_reply(ctx->connection,
xcb_get_input_focus(ctx->connection), NULL));
}
int sl_process_pending_configure_acks(struct sl_window* window,
struct sl_host_surface* host_surface) {
if (!window->pending_config.serial)
return 0;
if (window->managed && host_surface) {
int width = window->width + window->border_width * 2;
int height = window->height + window->border_width * 2;
// Early out if we expect contents to match window size at some point in
// the future.
if (width != host_surface->contents_width ||
height != host_surface->contents_height) {
return 0;
}
}
if (window->xdg_surface) {
xdg_surface_ack_configure(window->xdg_surface,
window->pending_config.serial);
}
window->pending_config.serial = 0;
if (window->next_config.serial)
sl_configure_window(window);
return 1;
}
static void sl_internal_xdg_surface_configure(void* data,
struct xdg_surface* xdg_surface,
uint32_t serial) {
struct sl_window* window = xdg_surface_get_user_data(xdg_surface);
window->next_config.serial = serial;
if (!window->pending_config.serial) {
struct wl_resource* host_resource;
struct sl_host_surface* host_surface = NULL;
host_resource =
wl_client_get_object(window->ctx->client, window->host_surface_id);
if (host_resource)
host_surface = wl_resource_get_user_data(host_resource);
sl_configure_window(window);
host_surface->defer_commits_until_configure = 0;
if (sl_process_pending_configure_acks(window, host_surface)) {
if (host_surface)
wl_surface_commit(host_surface->proxy);
}
}
}
static const struct xdg_surface_listener sl_internal_xdg_surface_listener = {
sl_internal_xdg_surface_configure};
static void sl_internal_xdg_toplevel_configure(
void* data,
struct xdg_toplevel* xdg_toplevel,
int32_t width,
int32_t height,
struct wl_array* states) {
struct sl_window* window = xdg_toplevel_get_user_data(xdg_toplevel);
int activated = 0;
uint32_t* state;
int i = 0;
if (!window->managed)
return;
if (width && height) {
int32_t width_in_pixels = width * window->ctx->scale;
int32_t height_in_pixels = height * window->ctx->scale;
int i = 0;
window->next_config.mask = XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH;
if (!(window->size_flags & (US_POSITION | P_POSITION))) {
window->next_config.mask |= XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
window->next_config.values[i++] =
window->ctx->screen->width_in_pixels / 2 - width_in_pixels / 2;
window->next_config.values[i++] =
window->ctx->screen->height_in_pixels / 2 - height_in_pixels / 2;
}
window->next_config.values[i++] = width_in_pixels;
window->next_config.values[i++] = height_in_pixels;
window->next_config.values[i++] = 0;
}
window->allow_resize = 1;
wl_array_for_each(state, states) {
if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) {
window->allow_resize = 0;
window->next_config.states[i++] =
window->ctx->atoms[ATOM_NET_WM_STATE_FULLSCREEN].value;
}
if (*state == XDG_TOPLEVEL_STATE_MAXIMIZED) {
window->allow_resize = 0;
window->next_config.states[i++] =
window->ctx->atoms[ATOM_NET_WM_STATE_MAXIMIZED_VERT].value;
window->next_config.states[i++] =
window->ctx->atoms[ATOM_NET_WM_STATE_MAXIMIZED_HORZ].value;
}
if (*state == XDG_TOPLEVEL_STATE_ACTIVATED)
activated = 1;
if (*state == XDG_TOPLEVEL_STATE_RESIZING)
window->allow_resize = 0;
}
if (activated != window->activated) {
if (activated != (window->ctx->host_focus_window == window)) {
window->ctx->host_focus_window = activated ? window : NULL;
window->ctx->needs_set_input_focus = 1;
}
window->activated = activated;
}
window->next_config.states_length = i;
}
static void sl_internal_xdg_toplevel_close(void* data,
struct xdg_toplevel* xdg_toplevel) {
struct sl_window* window = xdg_toplevel_get_user_data(xdg_toplevel);
xcb_client_message_event_t event = {
.response_type = XCB_CLIENT_MESSAGE,
.format = 32,
.window = window->id,
.type = window->ctx->atoms[ATOM_WM_PROTOCOLS].value,
.data.data32 =
{
window->ctx->atoms[ATOM_WM_DELETE_WINDOW].value,
XCB_CURRENT_TIME,
},
};
xcb_send_event(window->ctx->connection, 0, window->id,
XCB_EVENT_MASK_NO_EVENT, (const char*)&event);
}
static const struct xdg_toplevel_listener sl_internal_xdg_toplevel_listener = {
sl_internal_xdg_toplevel_configure, sl_internal_xdg_toplevel_close};
static void sl_internal_xdg_popup_configure(void* data,
struct xdg_popup* xdg_popup,
int32_t x,
int32_t y,
int32_t width,
int32_t height) {}
static void sl_internal_xdg_popup_done(void* data,
struct xdg_popup* xdg_popup) {}
static const struct xdg_popup_listener sl_internal_xdg_popup_listener = {
sl_internal_xdg_popup_configure, sl_internal_xdg_popup_done};
static void sl_window_set_wm_state(struct sl_window* window, int state) {
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);
}
void sl_update_application_id(struct sl_context* ctx,
struct sl_window* window) {
if (!window->aura_surface)
return;
if (ctx->application_id) {
zaura_surface_set_application_id(window->aura_surface, ctx->application_id);
return;
}
// Don't set application id for X11 override redirect. This prevents
// aura shell from thinking that these are regular application windows
// that should appear in application lists.
if (!ctx->xwayland || window->managed) {
char* application_id_str;
if (window->clazz) {
application_id_str =
sl_xasprintf(WM_CLASS_APPLICATION_ID_FORMAT, window->clazz);
} else if (window->client_leader != XCB_WINDOW_NONE) {
application_id_str = sl_xasprintf(WM_CLIENT_LEADER_APPLICATION_ID_FORMAT,
window->client_leader);
} else {
application_id_str = sl_xasprintf(XID_APPLICATION_ID_FORMAT, window->id);
}
zaura_surface_set_application_id(window->aura_surface, application_id_str);
free(application_id_str);
}
}
void sl_window_update(struct sl_window* window) {
struct wl_resource* host_resource = NULL;
struct sl_host_surface* host_surface;
struct sl_context* ctx = window->ctx;
struct sl_window* parent = NULL;
if (window->host_surface_id) {
host_resource = wl_client_get_object(ctx->client, window->host_surface_id);
if (host_resource && window->unpaired) {
wl_list_remove(&window->link);
wl_list_insert(&ctx->windows, &window->link);
window->unpaired = 0;
}
} else if (!window->unpaired) {
wl_list_remove(&window->link);
wl_list_insert(&ctx->unpaired_windows, &window->link);
window->unpaired = 1;
}
if (!host_resource) {
if (window->aura_surface) {
zaura_surface_destroy(window->aura_surface);
window->aura_surface = NULL;
}
if (window->xdg_toplevel) {
xdg_toplevel_destroy(window->xdg_toplevel);
window->xdg_toplevel = NULL;
}
if (window->xdg_popup) {
xdg_popup_destroy(window->xdg_popup);
window->xdg_popup = NULL;
}
if (window->xdg_surface) {
xdg_surface_destroy(window->xdg_surface);
window->xdg_surface = NULL;
}
window->realized = 0;
return;
}
host_surface = wl_resource_get_user_data(host_resource);
assert(host_surface);
assert(!host_surface->has_role);
assert(ctx->xdg_shell);
assert(ctx->xdg_shell->internal);
if (window->managed) {
if (window->transient_for != XCB_WINDOW_NONE) {
struct sl_window* sibling;
wl_list_for_each(sibling, &ctx->windows, link) {
if (sibling->id == window->transient_for) {
if (sibling->xdg_toplevel)
parent = sibling;
break;
}
}
}
}
// If we have a transient parent, but could not find it in the list of
// realized windows, then pick the window that had the last event for the
// parent. We update this again when we gain focus, so if we picked the wrong
// one it can get corrected at that point (but it's also possible the parent
// will never be realized, which is why selecting one here is important).
if (!window->managed ||
(!parent && window->transient_for != XCB_WINDOW_NONE)) {
struct sl_window* sibling;
uint32_t parent_last_event_serial = 0;
wl_list_for_each(sibling, &ctx->windows, link) {
struct wl_resource* sibling_host_resource;
struct sl_host_surface* sibling_host_surface;
if (!sibling->realized)
continue;
sibling_host_resource =
wl_client_get_object(ctx->client, sibling->host_surface_id);
if (!sibling_host_resource)
continue;
// Any parent will do but prefer last event window.
sibling_host_surface = wl_resource_get_user_data(sibling_host_resource);
if (parent_last_event_serial > sibling_host_surface->last_event_serial)
continue;
// Do not use ourselves as the parent.
if (sibling->host_surface_id == window->host_surface_id)
continue;
parent = sibling;
parent_last_event_serial = sibling_host_surface->last_event_serial;
}
}
if (!window->depth) {
xcb_get_geometry_reply_t* geometry_reply = xcb_get_geometry_reply(
ctx->connection, xcb_get_geometry(ctx->connection, window->id), NULL);
if (geometry_reply) {
window->depth = geometry_reply->depth;
free(geometry_reply);
}
}
if (!window->xdg_surface) {
window->xdg_surface = xdg_wm_base_get_xdg_surface(ctx->xdg_shell->internal,
host_surface->proxy);
xdg_surface_set_user_data(window->xdg_surface, window);
xdg_surface_add_listener(window->xdg_surface,
&sl_internal_xdg_surface_listener, window);
}
if (ctx->aura_shell) {
uint32_t frame_color;
if (!window->aura_surface) {
window->aura_surface = zaura_shell_get_aura_surface(
ctx->aura_shell->internal, host_surface->proxy);
}
zaura_surface_set_frame(window->aura_surface,
window->decorated ? ZAURA_SURFACE_FRAME_TYPE_NORMAL
: window->depth == 32
? ZAURA_SURFACE_FRAME_TYPE_NONE
: ZAURA_SURFACE_FRAME_TYPE_SHADOW);
frame_color = window->dark_frame ? ctx->dark_frame_color : ctx->frame_color;
zaura_surface_set_frame_colors(window->aura_surface, frame_color,
frame_color);
zaura_surface_set_startup_id(window->aura_surface, window->startup_id);
sl_update_application_id(ctx, window);
if (ctx->aura_shell->version >=
ZAURA_SURFACE_SET_FULLSCREEN_MODE_SINCE_VERSION) {
zaura_surface_set_fullscreen_mode(window->aura_surface,
ctx->fullscreen_mode);
}
}
// Always use top-level surface for X11 windows as we can't control when the
// window is closed.
if (ctx->xwayland || !parent) {
if (!window->xdg_toplevel) {
window->xdg_toplevel = xdg_surface_get_toplevel(window->xdg_surface);
xdg_toplevel_set_user_data(window->xdg_toplevel, window);
xdg_toplevel_add_listener(window->xdg_toplevel,
&sl_internal_xdg_toplevel_listener, window);
}
if (parent)
xdg_toplevel_set_parent(window->xdg_toplevel, parent->xdg_toplevel);
if (window->name)
xdg_toplevel_set_title(window->xdg_toplevel, window->name);
if (window->size_flags & P_MIN_SIZE) {
xdg_toplevel_set_min_size(window->xdg_toplevel,
window->min_width / ctx->scale,
window->min_height / ctx->scale);
}
if (window->size_flags & P_MAX_SIZE) {
xdg_toplevel_set_max_size(window->xdg_toplevel,
window->max_width / ctx->scale,
window->max_height / ctx->scale);
}
if (window->maximized) {
xdg_toplevel_set_maximized(window->xdg_toplevel);
}
} else if (!window->xdg_popup) {
struct xdg_positioner* positioner;
positioner = xdg_wm_base_create_positioner(ctx->xdg_shell->internal);
assert(positioner);
xdg_positioner_set_anchor(positioner, XDG_POSITIONER_ANCHOR_TOP_LEFT);
xdg_positioner_set_gravity(positioner, XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT);
xdg_positioner_set_anchor_rect(positioner,
(window->x - parent->x) / ctx->scale,
(window->y - parent->y) / ctx->scale, 1, 1);
window->xdg_popup = xdg_surface_get_popup(window->xdg_surface,
parent->xdg_surface, positioner);
xdg_popup_set_user_data(window->xdg_popup, window);
xdg_popup_add_listener(window->xdg_popup, &sl_internal_xdg_popup_listener,
window);
xdg_positioner_destroy(positioner);
}
if ((window->size_flags & (US_POSITION | P_POSITION)) && parent &&
ctx->aura_shell) {
zaura_surface_set_parent(window->aura_surface, parent->aura_surface,
(window->x - parent->x) / ctx->scale,
(window->y - parent->y) / ctx->scale);
}
wl_surface_commit(host_surface->proxy);
if (host_surface->contents_width && host_surface->contents_height)
window->realized = 1;
}
static void sl_host_buffer_destroy(struct wl_client* client,
struct wl_resource* resource) {
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 = wl_buffer_get_user_data(buffer);
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) {
struct sl_host_buffer* host = wl_resource_get_user_data(resource);
if (host->proxy)
wl_buffer_destroy(host->proxy);
if (host->shm_mmap) {
host->shm_mmap->buffer_resource = NULL;
sl_mmap_unref(host->shm_mmap);
}
if (host->sync_point) {
sl_sync_point_destroy(host->sync_point);
}
wl_resource_set_user_data(resource, NULL);
free(host);
}
struct sl_host_buffer* sl_create_host_buffer(struct wl_client* client,
uint32_t id,
struct wl_buffer* proxy,
int32_t width,
int32_t height) {
struct sl_host_buffer* host_buffer;
host_buffer = malloc(sizeof(*host_buffer));
assert(host_buffer);
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 = NULL;
host_buffer->shm_format = 0;
host_buffer->proxy = proxy;
if (host_buffer->proxy) {
wl_buffer_set_user_data(host_buffer->proxy, host_buffer);
wl_buffer_add_listener(host_buffer->proxy, &sl_buffer_listener,
host_buffer);
}
host_buffer->sync_point = NULL;
return host_buffer;
}
static void sl_internal_data_offer_destroy(struct sl_data_offer* host) {
wl_data_offer_destroy(host->internal);
wl_array_release(&host->atoms);
wl_array_release(&host->cookies);
free(host);
}
static void sl_set_selection(struct sl_context* ctx,
struct sl_data_offer* data_offer) {
if (ctx->selection_data_offer) {
sl_internal_data_offer_destroy(ctx->selection_data_offer);
ctx->selection_data_offer = NULL;
}
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));
((xcb_atom_t*)data_offer->atoms.data)[0] = ctx->atoms[ATOM_TARGETS].value;
((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 =
((xcb_intern_atom_cookie_t*)data_offer->cookies.data)[i];
xcb_intern_atom_reply_t* reply =
xcb_intern_atom_reply(ctx->connection, cookie, NULL);
if (reply) {
((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) {
struct sl_data_offer* host = data;
xcb_intern_atom_cookie_t* cookie =
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) {}
static void sl_internal_data_offer_action(void* data,
struct wl_data_offer* data_offer,
uint32_t dnd_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;
host_data_offer = malloc(sizeof(*host_data_offer));
assert(host_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_enter(void* data,
struct wl_data_device* data_device,
uint32_t serial,
struct wl_surface* surface,
wl_fixed_t x,
wl_fixed_t y,
struct wl_data_offer* data_offer) {}
static void sl_internal_data_device_leave(void* data,
struct wl_data_device* data_device) {}
static void sl_internal_data_device_motion(void* data,
struct wl_data_device* data_device,
uint32_t time,
wl_fixed_t x,
wl_fixed_t y) {}