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-window.cc
1409 lines (1254 loc) · 55.7 KB
/
sommelier-window.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 2021 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sommelier-window.h" // NOLINT(build/include_directory)
#include <algorithm>
#include <climits>
#include <fstream>
#include <assert.h>
#include <cstdint>
#include <wayland-client-protocol.h>
#include "sommelier.h" // NOLINT(build/include_directory)
#include "sommelier-logging.h" // NOLINT(build/include_directory)
#include "sommelier-tracing.h" // NOLINT(build/include_directory)
#include "sommelier-transform.h" // NOLINT(build/include_directory)
#include "viewporter-shim.h" // NOLINT(build/include_directory)
#include "xcb/xcb-shim.h"
#include "aura-shell-client-protocol.h" // NOLINT(build/include_directory)
#include "viewporter-client-protocol.h" // NOLINT(build/include_directory)
#include "xdg-shell-client-protocol.h" // NOLINT(build/include_directory)
#ifdef QUIRKS_SUPPORT
#include "quirks/sommelier-quirks.h"
#endif
#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 X11_PROPERTY_APPLICATION_ID_FORMAT \
APPLICATION_ID_FORMAT_PREFIX ".xprop.%s"
sl_window::sl_window(struct sl_context* ctx,
xcb_window_t id,
int x,
int y,
int width,
int height,
int border_width)
: ctx(ctx),
id(id),
x(x),
y(y),
width(width),
height(height),
border_width(border_width) {
wl_list_insert(&ctx->unpaired_windows, &link);
pixman_region32_init(&shape_rectangles);
}
sl_window::~sl_window() {
if (this == ctx->host_focus_window) {
ctx->host_focus_window = nullptr;
ctx->needs_set_input_focus = 1;
}
free(name);
free(clazz);
free(startup_id);
wl_list_remove(&link);
pixman_region32_fini(&shape_rectangles);
}
void sl_configure_window(struct sl_window* window) {
TRACE_EVENT("surface", "sl_configure_window", "id", window->id);
assert(!window->pending_config.serial);
if (window->next_config.mask) {
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);
uint32_t values[5];
// Populate values[0~3] with x,y,w,h
// x and y need to be 0. We are configuring the child window, not the frame
// window, and the child window should always have a zero offset from its
// frame (parent) window. See
// https://en.wikipedia.org/wiki/Re-parenting_window_manager
values[0] = 0;
values[1] = 0;
sl_window_get_width_height(window, &values[2], &values[3]);
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;
}
void sl_send_configure_notify(struct sl_window* window) {
// Send a "synthetic" ConfigureNotify event.
xcb_configure_notify_event_t event = {};
event.response_type = XCB_CONFIGURE_NOTIFY;
event.pad0 = 0;
event.event = window->id;
event.window = window->id;
event.above_sibling = XCB_WINDOW_NONE;
// Per ICCCM, synthetic ConfigureNotify events use root coordinates
// even if the window has been reparented.
uint32_t xywh[4];
sl_window_get_x_y(window, &xywh[0], &xywh[1]);
sl_window_get_width_height(window, &xywh[2], &xywh[3]);
event.x = static_cast<int16_t>(xywh[0]);
event.y = static_cast<int16_t>(xywh[1]);
event.width = static_cast<uint16_t>(xywh[2]);
event.height = static_cast<uint16_t>(xywh[3]);
event.border_width = static_cast<uint16_t>(window->border_width);
event.override_redirect = 0;
event.pad1 = 0;
xcb()->send_event(window->ctx->connection, 0, window->id,
XCB_EVENT_MASK_STRUCTURE_NOTIFY,
reinterpret_cast<char*>(&event));
}
int sl_process_pending_configure_acks(struct sl_window* window,
struct sl_host_surface* host_surface) {
if (!window->pending_config.serial)
return 0;
#ifdef COMMIT_LOOP_FIX
// Do not commit/ack if there is nothing to change.
//
// TODO(b/181077580): we should never do this, but avoiding it requires a
// more systemic fix
if (!window->pending_config.mask && window->pending_config.states_length == 0)
return 0;
#endif
if (window->managed && host_surface) {
if (window->viewport_override) {
// If viewport override is set, only check viewport size and completely
// ignore window size for containerized windows.
if (sl_window_is_containerized(window) &&
(window->viewport_height != window->viewport_height_realized ||
window->viewport_width != window->viewport_width_realized)) {
return 0;
}
} else {
uint32_t width = window->width + window->border_width * 2;
uint32_t 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;
}
void sl_commit(struct sl_window* window, struct sl_host_surface* host_surface) {
if (sl_process_pending_configure_acks(window, host_surface)) {
if (host_surface)
wl_surface_commit(host_surface->proxy);
}
}
static const struct xdg_popup_listener sl_internal_xdg_popup_listener = {
/*configure=*/DoNothing, /*done=*/DoNothing};
static void sl_internal_xdg_surface_configure(void* data,
struct xdg_surface* xdg_surface,
uint32_t serial) {
TRACE_EVENT("surface", "sl_internal_xdg_surface_configure");
struct sl_window* window =
static_cast<sl_window*>(xdg_surface_get_user_data(xdg_surface));
window->next_config.serial = serial;
if (window->configure_event_barrier) {
window->coalesced_next_config = window->next_config;
window->next_config.serial = 0;
} else if (!window->pending_config.serial) {
struct wl_resource* host_resource;
struct sl_host_surface* host_surface = nullptr;
host_resource =
wl_client_get_object(window->ctx->client, window->host_surface_id);
if (host_resource)
host_surface = static_cast<sl_host_surface*>(
wl_resource_get_user_data(host_resource));
sl_configure_window(window);
sl_commit(window, host_surface);
}
}
static const struct xdg_surface_listener sl_internal_xdg_surface_listener = {
sl_internal_xdg_surface_configure};
static void sl_internal_xdg_surface_configure_barrier_done(
void* data, struct wl_callback* callback, uint32_t serial) {
struct sl_window* window =
static_cast<sl_window*>(wl_callback_get_user_data(callback));
window->configure_event_barrier = nullptr;
if (window->coalesced_next_config.serial) {
window->next_config = window->coalesced_next_config;
window->coalesced_next_config.serial = 0;
sl_internal_xdg_surface_configure(data, window->xdg_surface,
window->next_config.serial);
}
}
////////////////////////////////////////////////////////////////////////////////
// common toplevel code
static const int32_t kUnspecifiedCoord = INT32_MIN;
void sl_window_update_should_be_containerized_from_pid(
struct sl_window* window) {
bool quirk_enabled = false;
#ifdef QUIRKS_SUPPORT
quirk_enabled = window->ctx->quirks.IsEnabled(
window, quirks::FEATURE_CONTAINERIZE_WINDOWS);
#endif
if (!quirk_enabled || !window->pid) {
return;
}
// If the binary name contains certain keywords, it is probably not actually a
// game.
char comm_file_path[50];
snprintf(comm_file_path, sizeof(comm_file_path), "/proc/%d/comm",
window->pid);
std::ifstream proc_comm_file(comm_file_path);
if (proc_comm_file.is_open()) {
std::string process_name;
if (getline(proc_comm_file, process_name)) {
std::transform(process_name.begin(), process_name.end(),
process_name.begin(), tolower);
window->should_be_containerized_from_pid =
(process_name.find("launcher") == std::string::npos &&
process_name.find("easyanticheat") == std::string::npos &&
process_name.find("battleeye") == std::string::npos &&
process_name.find("nprotect") == std::string::npos);
}
proc_comm_file.close();
}
// TODO(endlesspring): Consider other aspects of the process - memory
// usage, library usage, etc to determine.
}
bool sl_window_is_containerized(struct sl_window* window) {
bool window_containerized = false;
#ifdef QUIRKS_SUPPORT
bool probably_game_window =
// Steam game ID property is set
window->steam_game_id &&
// Window type is normal
window->type ==
window->ctx->atoms[ATOM_NET_WM_WINDOW_TYPE_NORMAL].value &&
// Based on the PID and derived information, the window is probably a game
// window.
window->should_be_containerized_from_pid &&
// Window max dimensions are either not set or if set, bigger (in
// half-perimeter) than specified value.
((window->max_width + window->max_height == 0) ||
(window->max_width + window->max_height >= 400));
if (window_containerized && !probably_game_window) {
LOG(VERBOSE) << window
<< " is not a game window! max_width=" << window->max_width;
}
window_containerized =
probably_game_window && window->ctx->quirks.IsEnabled(
window, quirks::FEATURE_CONTAINERIZE_WINDOWS);
#endif
return window_containerized;
}
void sl_window_reset_viewport(struct sl_window* window) {
LOG(VERBOSE) << window << " viewport override unset";
window->viewport_width = -1;
window->viewport_height = -1;
window->viewport_override = false;
// Note that viewport destination is reset during surface_commit.
}
// Handle a configure event on a toplevel object from the compositor.
//
// window: The window being configured.
// x: The configured X coordinate in host logical space, or kUnspecifiedCoord
// for toplevel objects that don't send positions.
// y: The configured Y coordinate in host logical space, or kUnspecifiedCoord
// for toplevel objects that don't send positions.
// width, height: Configured size in host logical space.
// states: Array of XDG_TOPLEVEL_STATE_* enum values.
void sl_internal_toplevel_configure(struct sl_window* window,
int32_t x,
int32_t y,
int32_t width,
int32_t height,
struct wl_array* states) {
if (!window->managed ||
(window->ctx->ignore_stateless_toplevel_configure && states->size == 0)) {
// Ignore requests for non-managed windows.
// Ignore requests with no states if --ignore-stateless-toplevel-configure
// is set.
return;
}
bool window_containerized = sl_window_is_containerized(window);
if (window_containerized) {
// Process window states first, so that window position and size can be
// configured based on the states. If the window is not containerized, we
// process the states last.
sl_internal_toplevel_configure_state_containerized(window, states);
}
if (width && height) {
int32_t width_in_pixels = width;
int32_t height_in_pixels = height;
// We are receiving a request to resize a window (in logical dimensions)
// If the request is equal to the cached values we used to make adjustments
// do not recalculate the values
// However, if the request is not equal to the cached values, try
// and keep the buffer same size as what was previously set
// by the application.
struct sl_host_surface* paired_surface = window->paired_surface;
if (paired_surface && paired_surface->has_own_scale) {
if (width != paired_surface->cached_logical_width ||
height != paired_surface->cached_logical_height) {
sl_transform_try_window_scale(window->ctx, paired_surface,
window->width, window->height);
}
}
sl_transform_host_to_guest(window->ctx, window->paired_surface,
&width_in_pixels, &height_in_pixels);
int config_idx = 0;
window->next_config.mask = 0;
// Refer to enum xcb_config_window_t for the order of configuration,
// position must be done before size.
sl_internal_toplevel_configure_position(window, x, y, width_in_pixels,
height_in_pixels, config_idx);
// We don't need to resize windows using viewports that are windowed and
// resizble.
if (window_containerized) {
sl_internal_toplevel_configure_size_containerized(
window, x, y, width, height, width_in_pixels, height_in_pixels,
config_idx);
} else {
sl_internal_toplevel_configure_size(window, x, y, width, height,
width_in_pixels, height_in_pixels,
config_idx);
}
}
if (!window_containerized) {
sl_internal_toplevel_configure_state(window, states);
}
}
void sl_internal_toplevel_configure_size_containerized(struct sl_window* window,
int32_t x,
int32_t y,
int32_t host_width,
int32_t host_height,
int32_t width_in_pixels,
int32_t height_in_pixels,
int& mut_config_idx) {
// Check the game is windowed (with decorations, indicating that this is not
// borderless windowed), and has hinted that it can be resized to the size
// that Exo requested. Note that max size 0 means no limits. Note that min&max
// dimensions are strictly set by the X11 client only (and forwarded to Exo
// immediately).
bool windowed_and_resizable =
(!window->fullscreen && window->decorated) &&
(window->max_width >= width_in_pixels || !window->max_width) &&
window->min_width <= width_in_pixels &&
(window->max_height >= height_in_pixels || !window->max_height) &&
window->min_height <= height_in_pixels;
// Forward resizes to the client if requested size fits within the min&max
// dimensions.
if (windowed_and_resizable && !window->use_emulated_rects) {
// TODO(b/330639760): Consider unset aspect ratio every frame in
// surface_commit.
zaura_surface_set_aspect_ratio(window->aura_surface, -1, -1);
sl_window_reset_viewport(window);
window->next_config.mask |= XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH;
window->next_config.values[mut_config_idx++] = width_in_pixels;
window->next_config.values[mut_config_idx++] = height_in_pixels;
window->next_config.values[mut_config_idx++] = 0;
LOG(VERBOSE) << window << " size set to " << width_in_pixels << "x"
<< height_in_pixels;
return;
}
auto output = window->paired_surface->output.get();
// Set the window size to be either max or min window size, set by the X11
// client. If width/height min and max are 0, set the window size to the size
// of the screen. If the window is fullscreen (by the client), ignore size
// hints and set it to the size of the screen.
// We are effectively maximizing the window as much as we can within the
// specified range, then down-scaling. This is to have consistent window
// rendering experience with the most-expected use-case (fullscreen game,
// user hits fullscreen toggle key). However, once we have better upscaling
// algorithm, we should consider minimizing the size (or average?) then
// upscaling in viewports instead.
int safe_window_width =
window->max_width ? window->max_width : window->min_width;
int safe_window_height =
window->max_height ? window->max_height : window->min_height;
if (window->use_emulated_rects) {
LOG(VERBOSE) << "using emulated rects " << window->emulated_width << "x"
<< window->emulated_height;
// If screen size emulation is set by XWayland, set the window size to the
// emulated size and adjust the viewport size as Exo had requested it to be.
safe_window_width = window->emulated_width;
safe_window_height = window->emulated_height;
} else if (!safe_window_width || !safe_window_height ||
window->max_width > output->width ||
window->max_height > output->height || window->fullscreen) {
safe_window_width = output->width;
safe_window_height = output->height;
}
window->next_config.mask |= XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH;
window->next_config.values[mut_config_idx++] = safe_window_width;
window->next_config.values[mut_config_idx++] = safe_window_height;
window->next_config.values[mut_config_idx++] = 0;
LOG(VERBOSE) << window << " size(safe) set to " << safe_window_width << "x"
<< safe_window_height;
if (window->use_emulated_rects && window->compositor_fullscreen) {
// Some games using emulated rects have xwayland send the wrong viewport
// destination, causing the game to render in a small box. This overrides
// that via quirk to set the destination to the size of the screen.
if (sl_window_should_fix_randr_emu(window)) {
window->viewport_override = true;
window->viewport_width = host_width;
window->viewport_height = host_height;
window->viewport_pointer_scale =
static_cast<float>(output->logical_width) / window->viewport_width;
return;
}
// If we are using emulated rects and the window is fullscreen in
// compositor, we only have to report the emulated size (done above). Aspect
// ratio changes are moot (since compositor fullscreen). We don't have to
// set viewport overrides since we can fully rely on viewport setup by
// Xwayland.
// If the window is windowed in compositor, then we have to do additional
// viewport operations to make the window fit into the size that compositor
// wants it to be, without disrupting the emulation. This has identical code
// path as non-emulated mode, except pointer movement scaling (see below).
sl_window_reset_viewport(window);
return;
}
// Use viewport to resize surface as per Exo request if we are not using
// emulated rects, or window is not fullscreen in Exo (and using emulated
// rects).
window->viewport_override = true;
int32_t safe_window_width_in_wl = safe_window_width;
int32_t safe_window_height_in_wl = safe_window_height;
sl_transform_guest_to_host(window->ctx, window->paired_surface,
&safe_window_width_in_wl,
&safe_window_height_in_wl);
// TODO(endlesspring): consider ignoring aspect ratio and filling the entire
// screen if Exo wants the window to be fullscreen. This wills require having
// pointer scale in x and y, instead of one.
// if (window->compositor_fullscreen) {
// window->viewport_width = output->logical_width;
// window->viewport_height = output->logical_height;
// window->viewport_pointer_scale =
// static_cast<float>(safe_window_width_in_wl) / window->viewport_width;
// return;
// }
// TODO(b/330639760): Once aspect ratio is working correctly, we can entirely
// get rid of this ratio calculation, in theory.
// Adjust viewport while maintaining aspect ratio
float width_ratio = static_cast<float>(safe_window_width) / width_in_pixels;
float height_ratio =
static_cast<float>(safe_window_height) / height_in_pixels;
if (std::abs(width_ratio - height_ratio) < 0.005) {
window->viewport_width = host_width;
window->viewport_height = host_height;
// If Exo sets a size that is of a different aspect ratio we shrink
// the window to maintain aspect ratio. We do this by always shrinking
// the side that is now proportionally larger as if we expand the
// smaller side it might result in the window becoming larger than the
// allowed bounds of the screen.
} else if (width_ratio < height_ratio) {
window->viewport_width =
(static_cast<float>(safe_window_width) * host_height) /
safe_window_height;
window->viewport_height = host_height;
} else if (width_ratio > height_ratio) {
window->viewport_height =
(static_cast<float>(safe_window_height) * host_width) /
safe_window_width;
window->viewport_width = host_width;
}
LOG(VERBOSE) << window << " viewport set to " << window->viewport_width << "x"
<< window->viewport_height;
// TODO(b/330639760): For some reason, set_aspect_ratio includes the title
// bar in the surface ratio. Figure out a way to mitigate this.
// Also consider setting aspect ratio every frame in surface_commit.
zaura_surface_set_aspect_ratio(
window->aura_surface, window->viewport_width,
window->viewport_height + (window->compositor_fullscreen ? 0 : 32));
if (window->use_emulated_rects) {
// Pointer scaling is being done in XWayland as well, assuming the viewport
// is the size of the screen. Map the movement from viewport to logical
// width of the screen (which XWayland is expecting).
window->viewport_pointer_scale =
static_cast<float>(output->logical_width) / window->viewport_width;
} else {
// Map movement from viewport to the window's space.
window->viewport_pointer_scale =
static_cast<float>(safe_window_width_in_wl) / window->viewport_width;
}
if (window->xdg_toplevel) {
// Override X11 client-defined min and max size to a value relative to
// display screen size.
// When client client updates min/max, window->min/max_width/height values
// are updated, then xdg_toplevel calls are made (see
// sl_handle_property_notify for details). This results in Exo adjusting
// the window size appropriately, resulting in this snippet
// (part of toplevel_configure) to run and override the max/min again. This
// allows the X11 client to continue resize itself but also allow the user
// to resize the window at will within the constraints.
xdg_toplevel_set_max_size(window->xdg_toplevel, output->logical_width * 0.8,
output->logical_height * 0.8);
xdg_toplevel_set_min_size(window->xdg_toplevel, output->logical_width * 0.4,
output->logical_height * 0.4);
}
}
void sl_internal_toplevel_configure_size(struct sl_window* window,
int32_t x,
int32_t y,
int32_t host_width,
int32_t host_height,
int32_t width_in_pixels,
int32_t height_in_pixels,
int& mut_config_idx) {
// For games with issues with incorrect viewport destination when using randr
// emulation, this will override the viewport destination with the screen size
// when in fullscreen mode.
if (window->use_emulated_rects && window->compositor_fullscreen &&
sl_window_should_fix_randr_emu(window)) {
window->viewport_override = true;
window->viewport_width = host_width;
window->viewport_height = host_height;
window->viewport_pointer_scale =
static_cast<float>(window->width) / window->viewport_width;
// Workaround using viewport for when Exo sets sizes that are not
// within the bounds the client requested.
// TODO(b/316990641): Implement resizing via viewports if
// window->fullscreen==true but window->compositor_fullscreen==false.
// Only do this when --only-client-can-exit-fullscreen is set.
} else if (window->ctx->viewport_resize &&
((window->max_width != 0 && width_in_pixels > window->max_width) ||
(window->min_width != 0 && width_in_pixels < window->min_width) ||
(window->max_height != 0 &&
height_in_pixels > window->max_height) ||
(window->min_height != 0 &&
height_in_pixels < window->min_height))) {
window->viewport_override = true;
float width_ratio = static_cast<float>(window->width) / width_in_pixels;
float height_ratio = static_cast<float>(window->height) / height_in_pixels;
if (std::abs(width_ratio - height_ratio) < 0.01) {
window->viewport_override = true;
window->viewport_width = host_width;
window->viewport_height = host_height;
// If Exo sets a size that is of a different aspect ratio we shrink
// the window to maintain aspect ratio. We do this by always shrinking
// the side that is now proportionally larger as if we expand the
// smaller side it might result in the window becoming larger than the
// allowed bounds of the screen.
} else if (width_ratio < height_ratio) {
window->viewport_width =
(static_cast<float>(window->width) * host_height) / window->height;
window->viewport_height = host_height;
} else if (width_ratio > height_ratio) {
window->viewport_height =
(static_cast<float>(window->height) * host_width) / window->width;
window->viewport_width = host_width;
}
int32_t window_width = window->width;
int32_t window_height = window->height;
sl_transform_guest_to_host(window->ctx, window->paired_surface,
&window_width, &window_height);
window->viewport_pointer_scale =
static_cast<float>(window_width) / window->viewport_width;
xdg_toplevel_set_min_size(window->xdg_toplevel, window->viewport_width,
window->viewport_height);
xdg_toplevel_set_max_size(window->xdg_toplevel, window->viewport_width,
window->viewport_height);
} else if (window->viewport_override) {
sl_window_reset_viewport(window);
wp_viewport_shim()->set_destination(window->paired_surface->viewport,
window->viewport_width,
window->viewport_height);
}
window->next_config.mask |= XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH;
if (window->viewport_override) {
LOG(VERBOSE) << window << " viewport override, size set to "
<< window->width << "x" << window->height;
window->next_config.values[mut_config_idx++] = window->width;
window->next_config.values[mut_config_idx++] = window->height;
window->next_config.values[mut_config_idx++] = 0;
} else if (window->use_emulated_rects) {
LOG(VERBOSE) << window << " emulated, size set to "
<< window->emulated_width << "x" << window->emulated_height;
window->next_config.values[mut_config_idx++] = window->emulated_width;
window->next_config.values[mut_config_idx++] = window->emulated_height;
window->next_config.values[mut_config_idx++] = 0;
} else {
LOG(VERBOSE) << window << " size set to " << width_in_pixels << "x"
<< height_in_pixels;
window->next_config.values[mut_config_idx++] = width_in_pixels;
window->next_config.values[mut_config_idx++] = height_in_pixels;
window->next_config.values[mut_config_idx++] = 0;
}
}
void sl_internal_toplevel_configure_position(struct sl_window* window,
uint32_t x,
uint32_t y,
int32_t width_in_pixels,
int32_t height_in_pixels,
int& mut_config_idx) {
if (window->use_emulated_rects) {
// Ignore requests from the compositor and set the coordinates as emulation
// requested.
window->next_config.mask |= XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
window->next_config.values[mut_config_idx++] = window->emulated_x;
window->next_config.values[mut_config_idx++] = window->emulated_y;
LOG(VERBOSE) << window << " position set to emulated " << window->emulated_x
<< "," << window->emulated_y;
} else if (x != kUnspecifiedCoord && y != kUnspecifiedCoord) {
// Convert to virtual coordinates
int32_t guest_x = x;
int32_t guest_y = y;
sl_transform_host_position_to_guest_position(
window->ctx, window->paired_surface, &guest_x, &guest_y);
window->next_config.mask |= XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
window->next_config.values[mut_config_idx++] = guest_x;
window->next_config.values[mut_config_idx++] = guest_y;
LOG(VERBOSE) << window << " position set to specified " << guest_x << ","
<< guest_y;
} else if (!(window->size_flags & (US_POSITION | P_POSITION))) {
uint32_t new_x = 0;
uint32_t new_y = 0;
const sl_host_output* output =
window->paired_surface ? window->paired_surface->output.get() : nullptr;
if (output) {
if (sl_window_is_containerized(window) && window->viewport_override &&
window->fullscreen && !window->compositor_fullscreen) {
new_x = output->virt_x;
new_y = output->virt_y;
} else {
new_x =
output->virt_x + (output->virt_rotated_width - width_in_pixels) / 2;
new_y = (output->virt_rotated_height - height_in_pixels) / 2;
}
} else {
new_x = window->ctx->screen->width_in_pixels / 2 - width_in_pixels / 2;
new_y = window->ctx->screen->height_in_pixels / 2 - height_in_pixels / 2;
}
window->next_config.mask |= XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
window->next_config.values[mut_config_idx++] = new_x;
window->next_config.values[mut_config_idx++] = new_y;
LOG(VERBOSE) << window << " position set to new " << new_x << "," << new_y;
}
}
void sl_internal_toplevel_configure_set_activated(struct sl_window* window,
bool activated) {
if (activated != window->activated) {
if (activated != (window->ctx->host_focus_window == window)) {
window->ctx->host_focus_window = activated ? window : nullptr;
window->ctx->needs_set_input_focus = 1;
}
window->activated = activated;
}
}
void sl_internal_toplevel_configure_state(struct sl_window* window,
struct wl_array* states) {
// States: https://wayland.app/protocols/xdg-shell#xdg_toplevel:enum:state
// Note that if there are no states specified, it is not-maximized,
// not-fullscreen, not-currently-being-resized and not-activated window.
bool activated = false;
window->allow_resize = 1;
window->compositor_fullscreen = 0;
uint32_t* state;
int state_idx = 0;
if (window->ctx->only_client_can_exit_fullscreen && window->fullscreen) {
// If the window has been set to full screen by the client, do not try to
// revert it back. Many games and applications fail to adapt to changes
// enforced by the WM (ie. the fullscreen state must be toggled via the
// game's UI).
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_FULLSCREEN].value;
window->allow_resize = 0;
}
sl_array_for_each(state, states) {
if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) {
if (state_idx == 0) {
// Only add fullscreen state if it was previously not added by
// --only-client-can-exit-fullscreen
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_FULLSCREEN].value;
}
window->allow_resize = 0;
window->compositor_fullscreen = 1;
}
if (*state == XDG_TOPLEVEL_STATE_MAXIMIZED) {
bool force_x11_unmaximize = false;
#ifdef QUIRKS_SUPPORT
force_x11_unmaximize = window->ctx->quirks.IsEnabled(
window, quirks::FEATURE_FORCE_X11_UNMAXIMIZE);
#endif
window->allow_resize = 0;
if (!force_x11_unmaximize) {
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_MAXIMIZED_VERT].value;
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_MAXIMIZED_HORZ].value;
}
}
if (*state == XDG_TOPLEVEL_STATE_ACTIVATED) {
activated = true;
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_FOCUSED].value;
}
if (*state == XDG_TOPLEVEL_STATE_RESIZING)
window->allow_resize = 0;
}
sl_internal_toplevel_configure_set_activated(window, activated);
LOG(VERBOSE) << window << " state change, fullscreen=" << window->fullscreen
<< " compositor_fullscreen=" << window->compositor_fullscreen;
// Override previous state definitions
window->next_config.states_length = state_idx;
}
void sl_internal_toplevel_configure_state_containerized(
struct sl_window* window, struct wl_array* states) {
// States: https://wayland.app/protocols/xdg-shell#xdg_toplevel:enum:state
// Note that if there are no states specified, it is not-maximized,
// not-fullscreen, not-currently-being-resized and not-activated window.
bool activated = false;
window->allow_resize = 1;
window->compositor_fullscreen = 0;
uint32_t* state;
int state_idx = 0;
// Keep the fullscreen state if X11 client decided to be fullscreen. Ignore
// compositor's request.
if (window->fullscreen) {
window->allow_resize = 0;
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_FULLSCREEN].value;
}
// Ditto as above, but maximize state.
if (window->maximized) {
bool force_x11_unmaximize = false;
#ifdef QUIRKS_SUPPORT
force_x11_unmaximize = window->ctx->quirks.IsEnabled(
window, quirks::FEATURE_FORCE_X11_UNMAXIMIZE);
#endif
window->allow_resize = 0;
if (!force_x11_unmaximize) {
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_MAXIMIZED_VERT].value;
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_MAXIMIZED_HORZ].value;
}
}
sl_array_for_each(state, states) {
// Note that we are ignoring maximized state for reasons specified above. We
// are not even taking note of what the compositor wants since we don't have
// to for window containerization.
if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) {
// Mark as compositor request to be fullscreen, so that other sizing
// operations can operate to fulfill Exo's request.
window->compositor_fullscreen = 1;
}
if (*state == XDG_TOPLEVEL_STATE_ACTIVATED) {
activated = true;
window->next_config.states[state_idx++] =
window->ctx->atoms[ATOM_NET_WM_STATE_FOCUSED].value;
}
if (*state == XDG_TOPLEVEL_STATE_RESIZING) {
window->allow_resize = 0;
}
}
// Set focus appropriately.
sl_internal_toplevel_configure_set_activated(window, activated);
if (!window->compositor_fullscreen) {
// Ignore the window decoration settings done by the X11 client, and show
// frame decorations if Exo treats the surface as non-fullscreen.
zaura_surface_set_frame(window->aura_surface,
ZAURA_SURFACE_FRAME_TYPE_NORMAL);
}
LOG(VERBOSE) << window << " state change, fullscreen=" << window->fullscreen
<< " compositor_fullscreen=" << window->compositor_fullscreen;
// Override previous state definitions
window->next_config.states_length = state_idx;
}
////////////////////////////////////////////////////////////////////////////////
// xdg_toplevel event listeners
//
// https://crsrc.org/s/?q=f:sommelier/protocol/xdg-shell.xml%20name=\"xdg_toplevel
//
// In Exo, this is sent from
// https://crsrc.org/s/?q=f:exo%2Fwayland.*cc%20xdg_toplevel_send_configure
static void sl_internal_xdg_toplevel_configure(
void* unused_data,
struct xdg_toplevel* xdg_toplevel,
int32_t width,
int32_t height,
struct wl_array* states) {
TRACE_EVENT("other", "sl_internal_xdg_toplevel_configure");
struct sl_window* window =
static_cast<sl_window*>(xdg_toplevel_get_user_data(xdg_toplevel));
sl_internal_toplevel_configure(window, kUnspecifiedCoord, kUnspecifiedCoord,
width, height, states);
}
// In Exo, this is sent from
// https://crsrc.org/s/?q=f:exo%2Fwayland.*cc%20xdg_toplevel_send_close
static void sl_internal_xdg_toplevel_close(void* data,
struct xdg_toplevel* xdg_toplevel) {
TRACE_EVENT("other", "sl_internal_xdg_toplevel_close");
struct sl_window* window =
static_cast<sl_window*>(xdg_toplevel_get_user_data(xdg_toplevel));
xcb_client_message_event_t event = {};
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.window = window->id;
event.type = window->ctx->atoms[ATOM_WM_PROTOCOLS].value;
event.data.data32[0] = window->ctx->atoms[ATOM_WM_DELETE_WINDOW].value;
event.data.data32[1] = 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};
////////////////////////////////////////////////////////////////////////////////
// zaura_toplevel event listeners
//
// https://crsrc.org/s/?q=f:sommelier/protocol/aura-shell.xml%20name=\"zaura_toplevel
// Sent from Exo here:
// https://crsrc.org/s/?q=f:exo%2Fwayland.*cc%20zaura_toplevel_send_configure
static void sl_internal_zaura_toplevel_configure(
void* unused_data,
struct zaura_toplevel* zaura_toplevel,
int32_t x,
int32_t y,
int32_t width,
int32_t height,
struct wl_array* states) {
TRACE_EVENT("other", "sl_internal_zaura_toplevel_configure");
struct sl_window* window =
static_cast<sl_window*>(zaura_toplevel_get_user_data(zaura_toplevel));
// aura_toplevel.configure replaces xdg_toplevel.configure for surfaces on
// which zaura_toplevel_set_supports_screen_coordinates() has been called.
// So we shouldn't get duplicate events.
//
// TODO(cpelling): Handle aura-specific states.
sl_internal_toplevel_configure(window, x, y, width, height, states);
}
// Sent from Exo here:
// https://crsrc.org/s/?q=f:exo%2Fwayland.*cc%20zaura_toplevel_send_origin_change
static void sl_internal_zaura_toplevel_origin_change(
void* data, struct zaura_toplevel* zaura_toplevel, int32_t x, int32_t y) {
// aura_toplevel.origin_change is not part of the normal configuration
// lifecycle, and is not followed by xdg_surface.configure. So just apply
// this change immediately.
sl_window* window =
static_cast<sl_window*>(zaura_toplevel_get_user_data(zaura_toplevel));
if (window->configure_event_barrier) {
// TODO(cpelling): Coalesce origin_change events instead of dropping them.
return;
}
int32_t guest_x = x;
int32_t guest_y = y;
sl_transform_host_position_to_guest_position(
window->ctx, window->paired_surface, &guest_x, &guest_y);
window->x = guest_x;
window->y = guest_y;
uint32_t values[2];
sl_window_get_x_y(window, &values[0], &values[1]);
xcb()->configure_window(window->ctx->connection, window->frame_id,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values);
}
static const struct zaura_toplevel_listener
sl_internal_zaura_toplevel_listener = {
sl_internal_zaura_toplevel_configure,
sl_internal_zaura_toplevel_origin_change};