File: zcr_remote_shell_impl.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (1602 lines) | stat: -rw-r--r-- 67,786 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/wayland/zcr_remote_shell_impl.h"

#include "ash/public/cpp/arc_resize_lock_type.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shell.h"
#include "ash/wm/window_resizer.h"
#include "base/command_line.h"
#include "base/task/single_thread_task_runner.h"
#include "chromeos/ui/base/window_pin_type.h"
#include "components/exo/display.h"
#include "components/exo/wayland/server_util.h"
#include "components/exo/wm_helper.h"
#include "ui/display/screen.h"
#include "ui/views/window/caption_button_types.h"
#include "ui/wm/core/window_animations.h"

namespace exo {
namespace wayland {

// Ensure that V1 and V2 constants remain identical.
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_SYSTEMUI_VISIBILITY_STATE_VISIBLE) ==
        static_cast<int>(
            ZCR_REMOTE_SURFACE_V2_SYSTEMUI_VISIBILITY_STATE_VISIBLE),
    "ZCR_REMOTE_SURFACE_V1_SYSTEMUI_VISIBILITY_STATE_VISIBLE and "
    "ZCR_REMOTE_SURFACE_V2_SYSTEMUI_VISIBILITY_STATE_VISIBLE should be equal");
static_assert(
    static_cast<int>(
        ZCR_REMOTE_SURFACE_V1_SYSTEMUI_VISIBILITY_STATE_AUTOHIDE_NON_STICKY) ==
        static_cast<int>(
            ZCR_REMOTE_SURFACE_V2_SYSTEMUI_VISIBILITY_STATE_AUTOHIDE_NON_STICKY),
    "ZCR_REMOTE_SURFACE_V1_SYSTEMUI_VISIBILITY_STATE_AUTOHIDE_NON_STICKY and "
    "ZCR_REMOTE_SURFACE_V2_SYSTEMUI_VISIBILITY_STATE_AUTOHIDE_NON_STICKY "
    "should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_OUTPUT_V1_SYSTEMUI_BEHAVIOR_VISIBLE) ==
        static_cast<int>(ZCR_REMOTE_OUTPUT_V2_SYSTEMUI_BEHAVIOR_VISIBLE),
    "ZCR_REMOTE_OUTPUT_V1_SYSTEMUI_BEHAVIOR_VISIBLE and "
    "ZCR_REMOTE_OUTPUT_V2_SYSTEMUI_BEHAVIOR_VISIBLE should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_OUTPUT_V1_SYSTEMUI_BEHAVIOR_HIDDEN) ==
        static_cast<int>(ZCR_REMOTE_OUTPUT_V2_SYSTEMUI_BEHAVIOR_HIDDEN),
    "ZCR_REMOTE_OUTPUT_V1_SYSTEMUI_BEHAVIOR_HIDDEN and "
    "ZCR_REMOTE_OUTPUT_V2_SYSTEMUI_BEHAVIOR_HIDDEN should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_NONE) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_NONE),
              "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_NONE and "
              "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_NONE should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOP) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_TOP),
              "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOP and "
              "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_TOP should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPRIGHT) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_TOPRIGHT),
    "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPRIGHT and "
    "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_TOPRIGHT should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_RIGHT) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_RIGHT),
    "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_RIGHT and "
    "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_RIGHT should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMRIGHT) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_BOTTOMRIGHT),
    "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMRIGHT and "
    "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_BOTTOMRIGHT should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOM) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_BOTTOM),
    "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOM and "
    "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_BOTTOM should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMLEFT) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_BOTTOMLEFT),
    "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMLEFT and "
    "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_BOTTOMLEFT should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_LEFT) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_LEFT),
              "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_LEFT and "
              "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_LEFT should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPLEFT) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_TOPLEFT),
    "ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPLEFT and "
    "ZCR_REMOTE_SURFACE_V2_RESIZE_DIRECTION_TOPLEFT should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_TABLET) ==
                  static_cast<int>(ZCR_REMOTE_SHELL_V2_LAYOUT_MODE_TABLET),
              "ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_TABLET and "
              "ZCR_REMOTE_SHELL_V2_LAYOUT_MODE_TABLET should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_WINDOWED) ==
                  static_cast<int>(ZCR_REMOTE_SHELL_V2_LAYOUT_MODE_WINDOWED),
              "ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_WINDOWED and "
              "ZCR_REMOTE_SHELL_V2_LAYOUT_MODE_WINDOWED should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_CLIENT_FOCUSED) ==
        static_cast<int>(
            ZCR_REMOTE_SHELL_V2_DESKTOP_FOCUS_STATE_CLIENT_FOCUSED),
    "ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_CLIENT_FOCUSED and "
    "ZCR_REMOTE_SHELL_V2_DESKTOP_FOCUS_STATE_CLIENT_FOCUSED should be equal");
static_assert(
    static_cast<int>(
        ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_OTHER_CLIENT_FOCUSED) ==
        static_cast<int>(
            ZCR_REMOTE_SHELL_V2_DESKTOP_FOCUS_STATE_OTHER_CLIENT_FOCUSED),
    "ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_OTHER_CLIENT_FOCUSED and "
    "ZCR_REMOTE_SHELL_V2_DESKTOP_FOCUS_STATE_OTHER_CLIENT_FOCUSED should be "
    "equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_NO_FOCUS) ==
        static_cast<int>(ZCR_REMOTE_SHELL_V2_DESKTOP_FOCUS_STATE_NO_FOCUS),
    "ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_NO_FOCUS and "
    "ZCR_REMOTE_SHELL_V2_DESKTOP_FOCUS_STATE_NO_FOCUS should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_RESIZE) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_RESIZE),
    "ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_RESIZE and "
    "ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_RESIZE should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_MOVE) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_MOVE),
    "ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_MOVE and "
    "ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_MOVE should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_PIP) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_PIP),
    "ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_PIP and "
    "ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_PIP should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_DRAG_RESIZE) ==
        static_cast<int>(
            ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_DRAG_RESIZE),
    "ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_DRAG_RESIZE and "
    "ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_DRAG_RESIZE should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_DRAG_MOVE) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_DRAG_MOVE),
    "ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_DRAG_MOVE and "
    "ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_DRAG_MOVE should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_SNAP_TO_LEFT) ==
        static_cast<int>(
            ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_SNAP_TO_LEFT),
    "ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_SNAP_TO_LEFT and "
    "ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_SNAP_TO_LEFT should be equal");
static_assert(
    static_cast<int>(
        ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_SNAP_TO_RIGHT) ==
        static_cast<int>(
            ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_SNAP_TO_RIGHT),
    "ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_SNAP_TO_RIGHT and "
    "ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_SNAP_TO_RIGHT should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_NORMAL) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_NORMAL),
              "ZCR_REMOTE_SHELL_V1_STATE_TYPE_NORMAL and "
              "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_NORMAL should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_MINIMIZED) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_MINIMIZED),
              "ZCR_REMOTE_SHELL_V1_STATE_TYPE_MINIMIZED and "
              "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_MINIMIZED should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_MAXIMIZED) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_MAXIMIZED),
              "ZCR_REMOTE_SHELL_V1_STATE_TYPE_MAXIMIZED and "
              "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_MAXIMIZED should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_FULLSCREEN) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_FULLSCREEN),
              "ZCR_REMOTE_SHELL_V1_STATE_TYPE_FULLSCREEN and "
              "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_FULLSCREEN should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_PINNED) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_PINNED),
              "ZCR_REMOTE_SHELL_V1_STATE_TYPE_PINNED and "
              "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_PINNED should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_TRUSTED_PINNED) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_TRUSTED_PINNED),
    "ZCR_REMOTE_SHELL_V1_STATE_TYPE_TRUSTED_PINNED and "
    "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_TRUSTED_PINNED should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_LEFT_SNAPPED) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_LEFT_SNAPPED),
    "ZCR_REMOTE_SHELL_V1_STATE_TYPE_LEFT_SNAPPED and "
    "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_LEFT_SNAPPED should be equal");
static_assert(
    static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_RIGHT_SNAPPED) ==
        static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_RIGHT_SNAPPED),
    "ZCR_REMOTE_SHELL_V1_STATE_TYPE_RIGHT_SNAPPED and "
    "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_RIGHT_SNAPPED should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SHELL_V1_STATE_TYPE_PIP) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_STATE_TYPE_PIP),
              "ZCR_REMOTE_SHELL_V1_STATE_TYPE_PIP and "
              "ZCR_REMOTE_SURFACE_V2_STATE_TYPE_PIP should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_IN) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_ZOOM_CHANGE_IN),
              "ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_IN and "
              "ZCR_REMOTE_SURFACE_V2_ZOOM_CHANGE_IN should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_OUT) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_ZOOM_CHANGE_OUT),
              "ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_OUT and "
              "ZCR_REMOTE_SURFACE_V2_ZOOM_CHANGE_OUT should be equal");
static_assert(static_cast<int>(ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_RESET) ==
                  static_cast<int>(ZCR_REMOTE_SURFACE_V2_ZOOM_CHANGE_RESET),
              "ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_RESET and "
              "ZCR_REMOTE_SURFACE_V2_ZOOM_CHANGE_RESET should be equal");

using chromeos::WindowStateType;

// We don't send configure immediately after tablet mode switch
// because layout can change due to orientation lock state or accelerometer.
constexpr int kConfigureDelayAfterLayoutSwitchMs = 300;

constexpr int kRemoteShellSeatObserverPriority = 0;
static_assert(Seat::IsValidObserverPriority(kRemoteShellSeatObserverPriority),
              "kRemoteShellSeatObserverPriority is not in the valid range.");

// Convert to 8.24 fixed format.
int32_t To8_24Fixed(double value) {
  constexpr int kDecimalBits = 24;
  return static_cast<int32_t>(value * (1 << kDecimalBits));
}

ash::ShelfLayoutManager* GetShelfLayoutManagerForDisplay(
    const display::Display& display) {
  auto* root = ash::Shell::GetRootWindowForDisplayId(display.id());
  return ash::Shelf::ForWindow(root)->shelf_layout_manager();
}

int SystemUiVisibility(const display::Display& display) {
  auto* shelf_layout_manager = GetShelfLayoutManagerForDisplay(display);
  switch (shelf_layout_manager->visibility_state()) {
    case ash::SHELF_VISIBLE:
      return ZCR_REMOTE_SURFACE_V1_SYSTEMUI_VISIBILITY_STATE_VISIBLE;
    case ash::SHELF_AUTO_HIDE:
    case ash::SHELF_HIDDEN:
      return ZCR_REMOTE_SURFACE_V1_SYSTEMUI_VISIBILITY_STATE_AUTOHIDE_NON_STICKY;
  }
  NOTREACHED() << "Got unexpected shelf visibility state "
               << shelf_layout_manager->visibility_state();
  return 0;
}

int SystemUiBehavior(const display::Display& display) {
  auto* shelf_layout_manager = GetShelfLayoutManagerForDisplay(display);
  switch (shelf_layout_manager->auto_hide_behavior()) {
    case ash::ShelfAutoHideBehavior::kNever:
      return ZCR_REMOTE_OUTPUT_V1_SYSTEMUI_BEHAVIOR_VISIBLE;
    case ash::ShelfAutoHideBehavior::kAlways:
    case ash::ShelfAutoHideBehavior::kAlwaysHidden:
      return ZCR_REMOTE_OUTPUT_V1_SYSTEMUI_BEHAVIOR_HIDDEN;
  }
  NOTREACHED() << "Got unexpected shelf visibility behavior.";
  return 0;
}

uint32_t ResizeDirection(int component) {
  switch (component) {
    case HTCAPTION:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_NONE;
    case HTTOP:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOP;
    case HTTOPRIGHT:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPRIGHT;
    case HTRIGHT:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_RIGHT;
    case HTBOTTOMRIGHT:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMRIGHT;
    case HTBOTTOM:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOM;
    case HTBOTTOMLEFT:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMLEFT;
    case HTLEFT:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_LEFT;
    case HTTOPLEFT:
      return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPLEFT;
    default:
      LOG(ERROR) << "Unknown component:" << component;
      break;
  }
  NOTREACHED();
  return ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_NONE;
}

// This is a workaround for b/148977363
void MaybeApplyCTSHack(int layout_mode,
                       const gfx::Size& size_in_pixel,
                       gfx::Insets* insets_in_client_pixel,
                       gfx::Insets* stable_insets_in_client_pixel) {
  constexpr int kBadBottomInsets = 90;
  if (layout_mode == ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_TABLET &&
      size_in_pixel.width() == 3000 && size_in_pixel.height() == 2000 &&
      stable_insets_in_client_pixel->bottom() == kBadBottomInsets) {
    stable_insets_in_client_pixel->set_bottom(kBadBottomInsets + 1);
    if (insets_in_client_pixel->bottom() == kBadBottomInsets)
      insets_in_client_pixel->set_bottom(kBadBottomInsets + 1);
  }
}

bool WaylandRemoteOutput::SendDisplayMetrics(const display::Display& display,
                                             uint32_t changed_metrics) {
  if (wl_resource_get_version(resource_) <
      event_mapping_.stable_insets_since_version) {
    return false;
  }

  if (initial_config_sent_ &&
      !(changed_metrics & display::DisplayObserver::DISPLAY_METRIC_WORK_AREA)) {
    return false;
  }

  if (!initial_config_sent_) {
    initial_config_sent_ = true;

    uint32_t display_id_hi = static_cast<uint32_t>(display.id() >> 32);
    uint32_t display_id_lo = static_cast<uint32_t>(display.id());
    if (event_mapping_.send_display_id)
      event_mapping_.send_display_id(resource_, display_id_hi, display_id_lo);

    constexpr int64_t DISPLAY_ID_PORT_MASK = 0xff;
    uint32_t port = static_cast<uint32_t>(display.id() & DISPLAY_ID_PORT_MASK);
    if (event_mapping_.send_port)
      event_mapping_.send_port(resource_, port);

    wl_array data;
    wl_array_init(&data);

    const auto& bytes =
        WMHelper::GetInstance()->GetDisplayIdentificationData(display.id());
    for (uint8_t byte : bytes) {
      uint8_t* ptr =
          static_cast<uint8_t*>(wl_array_add(&data, sizeof(uint8_t)));
      DCHECK(ptr);
      *ptr = byte;
    }
    event_mapping_.send_identification_data(resource_, &data);
    wl_array_release(&data);
  }

  float device_scale_factor = display.device_scale_factor();
  gfx::Size size_in_pixel = display.GetSizeInPixel();

  gfx::Insets insets_in_pixel = GetWorkAreaInsetsInPixel(
      display, device_scale_factor, size_in_pixel, display.work_area());
  event_mapping_.send_insets(resource_, insets_in_pixel.left(),
                             insets_in_pixel.top(), insets_in_pixel.right(),
                             insets_in_pixel.bottom());

  gfx::Insets stable_insets_in_pixel = GetWorkAreaInsetsInPixel(
      display, device_scale_factor, size_in_pixel, GetStableWorkArea(display));
  event_mapping_.send_stable_insets(
      resource_, stable_insets_in_pixel.left(), stable_insets_in_pixel.top(),
      stable_insets_in_pixel.right(), stable_insets_in_pixel.bottom());

  // Currently no client uses zcr_remote_output_v1 systemui_visibility.
  // Only systemui_behavior is sent here.
  if (wl_resource_get_version(resource_) >=
      event_mapping_.system_ui_behavior_since_version) {
    int systemui_behavior = SystemUiBehavior(display);
    event_mapping_.send_systemui_behavior(resource_, systemui_behavior);
  }

  return true;
}

void WaylandRemoteOutput::SendActiveDisplay() {}

void WaylandRemoteOutput::OnOutputDestroyed() {
  display_handler_->RemoveObserver(this);
  display_handler_ = nullptr;
}

WaylandRemoteSurfaceDelegate::WaylandRemoteSurfaceDelegate(
    base::WeakPtr<WaylandRemoteShell> shell,
    wl_resource* resource,
    WaylandRemoteShellEventMapping event_mapping)
    : shell_(std::move(shell)),
      resource_(resource),
      event_mapping_(event_mapping) {}

WaylandRemoteSurfaceDelegate::~WaylandRemoteSurfaceDelegate() {
  if (shell_)
    shell_->OnRemoteSurfaceDestroyed(resource_);
}

// ClientControlledShellSurfaceDelegate:
void WaylandRemoteSurfaceDelegate::OnGeometryChanged(
    const gfx::Rect& geometry) {
  if (shell_)
    shell_->OnRemoteSurfaceGeometryChanged(resource_, geometry);
}
void WaylandRemoteSurfaceDelegate::OnStateChanged(
    chromeos::WindowStateType old_state_type,
    chromeos::WindowStateType new_state_type) {
  shell_->OnRemoteSurfaceStateChanged(resource_, old_state_type,
                                      new_state_type);
}
void WaylandRemoteSurfaceDelegate::OnBoundsChanged(
    chromeos::WindowStateType current_state,
    chromeos::WindowStateType requested_state,
    int64_t display_id,
    const gfx::Rect& bounds_in_display,
    bool is_resize,
    int bounds_change) {
  if (shell_) {
    shell_->OnRemoteSurfaceBoundsChanged(
        resource_, current_state, requested_state, display_id,
        bounds_in_display, is_resize, bounds_change);
  }
}
void WaylandRemoteSurfaceDelegate::OnDragStarted(int component) {
  event_mapping_.send_drag_started(resource_, ResizeDirection(component));
  wl_client_flush(wl_resource_get_client(resource_));
}
void WaylandRemoteSurfaceDelegate::OnDragFinished(int x, int y, bool canceled) {
  event_mapping_.send_drag_finished(resource_, x, y, canceled ? 1 : 0);
  wl_client_flush(wl_resource_get_client(resource_));
}
void WaylandRemoteSurfaceDelegate::OnZoomLevelChanged(ZoomChange zoom_change) {
  if (wl_resource_get_version(resource_) >=
          event_mapping_.change_zoom_level_since_version &&
      shell_) {
    shell_->OnRemoteSurfaceChangeZoomLevel(resource_, zoom_change);
  }
}

WaylandRemoteOutput::WaylandRemoteOutput(
    wl_resource* resource,
    WaylandRemoteOutputEventMapping event_mapping,
    WaylandDisplayHandler* display_handler)
    : resource_(resource),
      event_mapping_(event_mapping),
      display_handler_(display_handler) {
  display_handler_->AddObserver(this);
}

WaylandRemoteOutput::~WaylandRemoteOutput() {
  if (display_handler_)
    display_handler_->RemoveObserver(this);
}

using OutputResourceProvider = base::RepeatingCallback<wl_resource*(int64_t)>;

WaylandRemoteShell::WaylandRemoteShell(
    Display* display,
    wl_resource* remote_shell_resource,
    OutputResourceProvider output_provider,
    WaylandRemoteShellEventMapping event_mapping,
    bool use_default_scale_cancellation_default)
    : event_mapping_(event_mapping),
      display_(display),
      remote_shell_resource_(remote_shell_resource),
      output_provider_(output_provider),
      use_default_scale_cancellation_(use_default_scale_cancellation_default),
      seat_(display->seat()) {
  WMHelper* helper = WMHelper::GetInstance();
  helper->AddTabletModeObserver(this);
  helper->AddFrameThrottlingObserver();
  helper->SetDefaultScaleCancellation(use_default_scale_cancellation_);

  layout_mode_ = helper->InTabletMode()
                     ? ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_TABLET
                     : ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_WINDOWED;

  if (wl_resource_get_version(remote_shell_resource_) >=
      event_mapping_.layout_mode_since_version) {
    event_mapping_.send_layout_mode(remote_shell_resource_, layout_mode_);
  }

  if (wl_resource_get_version(remote_shell_resource_) >=
      event_mapping_.default_device_scale_factor_since_version) {
    double scale_factor = GetDefaultDeviceScaleFactor();
    int32_t fixed_scale = To8_24Fixed(scale_factor);
    event_mapping_.send_default_device_scale_factor(remote_shell_resource_,
                                                    fixed_scale);
  }

  SendDisplayMetrics();

  // The activation event has been moved to aura_shell, but the
  // desktop_focus_state event is still in remote_shell, which needs to be
  // called before the activation event.
  display->seat()->AddObserver(this, kRemoteShellSeatObserverPriority);
}

WaylandRemoteShell::~WaylandRemoteShell() {
  WMHelper* helper = WMHelper::GetInstance();
  helper->RemoveTabletModeObserver(this);
  helper->RemoveFrameThrottlingObserver();
  if (seat_)
    seat_->RemoveObserver(this);
}

std::unique_ptr<ClientControlledShellSurface>
WaylandRemoteShell::CreateShellSurface(Surface* surface, int container) {
  return display_->CreateOrGetClientControlledShellSurface(
      surface, container, use_default_scale_cancellation_,
      /*supports_floated_state=*/event_mapping_.has_bounds_change_reason_float);
}

std::unique_ptr<ClientControlledShellSurface::Delegate>
WaylandRemoteShell::CreateShellSurfaceDelegate(wl_resource* resource) {
  return std::make_unique<WaylandRemoteSurfaceDelegate>(
      weak_ptr_factory_.GetWeakPtr(), resource, event_mapping_);
}

std::unique_ptr<NotificationSurface>
WaylandRemoteShell::CreateNotificationSurface(
    Surface* surface,
    const std::string& notification_key) {
  return display_->CreateNotificationSurface(surface, notification_key);
}

std::unique_ptr<InputMethodSurface>
WaylandRemoteShell::CreateInputMethodSurface(Surface* surface) {
  return display_->CreateInputMethodSurface(surface,
                                            use_default_scale_cancellation_);
}

std::unique_ptr<ToastSurface> WaylandRemoteShell::CreateToastSurface(
    Surface* surface) {
  return display_->CreateToastSurface(surface, use_default_scale_cancellation_);
}

void WaylandRemoteShell::SetUseDefaultScaleCancellation(
    bool use_default_scale) {
  use_default_scale_cancellation_ = use_default_scale;
  WMHelper::GetInstance()->SetDefaultScaleCancellation(use_default_scale);
}

void WaylandRemoteShell::OnRemoteSurfaceDestroyed(wl_resource* resource) {
  // Sometimes resource might be destroyed after bounds change is scheduled to
  // |pending_bounds_change_| but before that bounds change is emitted. Erase
  // it from |pending_bounds_changes_| to prevent crashes. See also
  // https://crbug.com/1163271.
  pending_bounds_changes_.erase(resource);
}

// Overridden from display::DisplayObserver:
void WaylandRemoteShell::OnWillProcessDisplayChanges() {
  in_display_update_ = true;
}

void WaylandRemoteShell::OnDidProcessDisplayChanges() {
  in_display_update_ = false;
}

void WaylandRemoteShell::OnDisplayAdded(const display::Display& new_display) {
  ScheduleSendDisplayMetrics(0);
}

void WaylandRemoteShell::OnDisplayRemoved(const display::Display& old_display) {
  ScheduleSendDisplayMetrics(0);
}

void WaylandRemoteShell::OnDisplayTabletStateChanged(
    display::TabletState state) {
  const bool layout_change_started =
      state == display::TabletState::kEnteringTabletMode ||
      state == display::TabletState::kExitingTabletMode;
  if (layout_change_started)
    ScheduleSendDisplayMetrics(kConfigureDelayAfterLayoutSwitchMs);
}

void WaylandRemoteShell::OnDisplayMetricsChanged(
    const display::Display& display,
    uint32_t changed_metrics) {
  // No need to update when a primary display has changed without bounds
  // change. See WaylandDisplayObserver::OnDisplayMetricsChanged
  // for more details.
  if (changed_metrics &
      (DISPLAY_METRIC_BOUNDS | DISPLAY_METRIC_DEVICE_SCALE_FACTOR |
       DISPLAY_METRIC_ROTATION | DISPLAY_METRIC_WORK_AREA)) {
    ScheduleSendDisplayMetrics(0);
  }
}

// Overridden from ash::TabletModeObserver:
void WaylandRemoteShell::OnTabletModeStarted() {
  layout_mode_ = ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_TABLET;
  if (wl_resource_get_version(remote_shell_resource_) >=
      event_mapping_.layout_mode_since_version)
    event_mapping_.send_layout_mode(remote_shell_resource_, layout_mode_);
}
void WaylandRemoteShell::OnTabletModeEnding() {
  layout_mode_ = ZCR_REMOTE_SHELL_V1_LAYOUT_MODE_WINDOWED;
  if (wl_resource_get_version(remote_shell_resource_) >=
      event_mapping_.layout_mode_since_version)
    event_mapping_.send_layout_mode(remote_shell_resource_, layout_mode_);
}
void WaylandRemoteShell::OnTabletModeEnded() {}

void WaylandRemoteShell::ScheduleSendDisplayMetrics(int delay_ms) {
  needs_send_display_metrics_ = true;
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&WaylandRemoteShell::SendDisplayMetrics,
                     weak_ptr_factory_.GetWeakPtr()),
      base::Milliseconds(delay_ms));
}

// Returns the transform that a display's output is currently adjusted for.
wl_output_transform WaylandRemoteShell::DisplayTransform(
    display::Display::Rotation rotation) {
  switch (rotation) {
    case display::Display::ROTATE_0:
      return WL_OUTPUT_TRANSFORM_NORMAL;
    case display::Display::ROTATE_90:
      return WL_OUTPUT_TRANSFORM_90;
    case display::Display::ROTATE_180:
      return WL_OUTPUT_TRANSFORM_180;
    case display::Display::ROTATE_270:
      return WL_OUTPUT_TRANSFORM_270;
  }
  NOTREACHED();
  return WL_OUTPUT_TRANSFORM_NORMAL;
}

void WaylandRemoteShell::SendDisplayMetrics() {
  if (!needs_send_display_metrics_)
    return;
  needs_send_display_metrics_ = false;

  const display::Screen* screen = display::Screen::GetScreen();
  double default_dsf = GetDefaultDeviceScaleFactor();

  for (const auto& display : screen->GetAllDisplays()) {
    double device_scale_factor = display.device_scale_factor();

    uint32_t display_id_hi = static_cast<uint32_t>(display.id() >> 32);
    uint32_t display_id_lo = static_cast<uint32_t>(display.id());
    gfx::Size size_in_pixel = display.GetSizeInPixel();

    wl_array data;
    wl_array_init(&data);

    const auto& bytes =
        WMHelper::GetInstance()->GetDisplayIdentificationData(display.id());
    for (uint8_t byte : bytes) {
      uint8_t* ptr =
          static_cast<uint8_t*>(wl_array_add(&data, sizeof(uint8_t)));
      DCHECK(ptr);
      *ptr = byte;
    }

    if (wl_resource_get_version(remote_shell_resource_) >=
        event_mapping_.send_workspace_info_since_version) {
      // Apply the scale factor used on the remote shell client (ARC).
      const gfx::Rect& bounds = display.bounds();

      // Note: The origin is used just to identify the workspace on the client
      // side, and does not account the actual pixel size of other workspace
      // on the client side.
      int x_px = base::ClampRound(bounds.x() * default_dsf);
      int y_px = base::ClampRound(bounds.y() * default_dsf);

      float server_to_client_pixel_scale = default_dsf / device_scale_factor;

      gfx::Size size_in_client_pixel =
          gfx::ScaleToRoundedSize(size_in_pixel, server_to_client_pixel_scale);

      gfx::Insets insets_in_client_pixel = GetWorkAreaInsetsInPixel(
          display, default_dsf, size_in_client_pixel, display.work_area());

      gfx::Insets stable_insets_in_client_pixel =
          GetWorkAreaInsetsInPixel(display, default_dsf, size_in_client_pixel,
                                   GetStableWorkArea(display));

      // TODO(b/148977363): Fix the issue and remove the hack.
      MaybeApplyCTSHack(layout_mode_, size_in_pixel, &insets_in_client_pixel,
                        &stable_insets_in_client_pixel);

      int systemui_visibility = SystemUiVisibility(display);
      if (event_mapping_.send_workspace_info)
        event_mapping_.send_workspace_info(
            remote_shell_resource_, display_id_hi, display_id_lo, x_px, y_px,
            size_in_client_pixel.width(), size_in_client_pixel.height(),
            insets_in_client_pixel.left(), insets_in_client_pixel.top(),
            insets_in_client_pixel.right(), insets_in_client_pixel.bottom(),
            stable_insets_in_client_pixel.left(),
            stable_insets_in_client_pixel.top(),
            stable_insets_in_client_pixel.right(),
            stable_insets_in_client_pixel.bottom(), systemui_visibility,
            DisplayTransform(display.rotation()), display.IsInternal(), &data);
    } else {
      NOTREACHED() << "The remote shell resource version being used ("
                   << wl_resource_get_version(remote_shell_resource_)
                   << ") is not supported.";
    }

    wl_array_release(&data);
  }
  if (event_mapping_.send_configure)
    event_mapping_.send_configure(remote_shell_resource_, layout_mode_);

  base::flat_set<wl_client*> clients;
  clients.insert(wl_resource_get_client(remote_shell_resource_));

  for (const auto& bounds_change : pending_bounds_changes_) {
    SendBoundsChanged(bounds_change.first, bounds_change.second.display_id,
                      bounds_change.second.bounds_in_display,
                      bounds_change.second.reason);
    clients.insert(wl_resource_get_client(bounds_change.first));
  }
  pending_bounds_changes_.clear();

  for (auto* client : clients)
    wl_client_flush(client);
}

void WaylandRemoteShell::OnSurfaceFocused(Surface* gained_focus,
                                          Surface* lost_focus,
                                          bool has_focused_client) {
  FocusedSurfaceChanged(gained_focus, lost_focus, has_focused_client);
}

void WaylandRemoteShell::FocusedSurfaceChanged(Surface* gained_active_surface,
                                               Surface* lost_active_surface,
                                               bool has_focused_client) {
  if (gained_active_surface == lost_active_surface &&
      last_has_focused_client_ == has_focused_client) {
    return;
  }
  last_has_focused_client_ = has_focused_client;

  wl_resource* gained_active_surface_resource =
      gained_active_surface ? GetSurfaceResource(gained_active_surface)
                            : nullptr;
  wl_resource* lost_active_surface_resource =
      lost_active_surface ? GetSurfaceResource(lost_active_surface) : nullptr;

  wl_client* client = wl_resource_get_client(remote_shell_resource_);

  // If surface that gained active is not owned by remote shell client then
  // set it to null.
  if (gained_active_surface_resource &&
      wl_resource_get_client(gained_active_surface_resource) != client) {
    gained_active_surface_resource = nullptr;
  }

  // If surface that lost active is not owned by remote shell client then
  // set it to null.
  if (lost_active_surface_resource &&
      wl_resource_get_client(lost_active_surface_resource) != client) {
    lost_active_surface_resource = nullptr;
  }

  if (wl_resource_get_version(remote_shell_resource_) >=
      event_mapping_.desktop_focus_state_changed_since_version) {
    uint32_t focus_state;
    if (gained_active_surface_resource) {
      focus_state = ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_CLIENT_FOCUSED;
    } else if (has_focused_client) {
      focus_state =
          ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_OTHER_CLIENT_FOCUSED;
    } else {
      focus_state = ZCR_REMOTE_SHELL_V1_DESKTOP_FOCUS_STATE_NO_FOCUS;
    }
    event_mapping_.send_desktop_focus_state_changed(remote_shell_resource_,
                                                    focus_state);
  }

  if (event_mapping_.send_activated) {
    event_mapping_.send_activated(remote_shell_resource_,
                                  gained_active_surface_resource,
                                  lost_active_surface_resource);
  }

  wl_client_flush(client);
}

void WaylandRemoteShell::OnRemoteSurfaceBoundsChanged(
    wl_resource* resource,
    WindowStateType current_state,
    WindowStateType requested_state,
    int64_t display_id,
    const gfx::Rect& bounds_in_display,
    bool resize,
    int bounds_change) {
  uint32_t reason =
      ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_RESIZE;
  if (!resize)
    reason = ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_MOVE;
  if (current_state == WindowStateType::kPip)
    reason = ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_PIP;
  if (bounds_change & ash::WindowResizer::kBoundsChange_Resizes) {
    reason = ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_DRAG_RESIZE;
  } else if (bounds_change & ash::WindowResizer::kBoundsChange_Repositions) {
    reason = ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_DRAG_MOVE;
  }
  // Override the reason only if the window enters snapped or floated mode. If
  // the window resizes by dragging in snapped or floated mode, we need to keep
  // the original reason.
  if (requested_state != current_state) {
    if (requested_state == WindowStateType::kPrimarySnapped) {
      reason = ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_SNAP_TO_LEFT;
    } else if (requested_state == WindowStateType::kSecondarySnapped) {
      reason = ZCR_REMOTE_SURFACE_V1_BOUNDS_CHANGE_REASON_SNAP_TO_RIGHT;
    } else if (requested_state == WindowStateType::kFloated &&
               event_mapping_.has_bounds_change_reason_float) {
      reason = ZCR_REMOTE_SURFACE_V2_BOUNDS_CHANGE_REASON_FLOAT;
    }
  }

  if (in_display_update_ || needs_send_display_metrics_) {
    // We store only the latest bounds for each |resource|.
    pending_bounds_changes_.insert_or_assign(
        std::move(resource),
        BoundsChangeData(display_id, bounds_in_display, reason));
    return;
  }
  SendBoundsChanged(resource, display_id, bounds_in_display, reason);

  wl_client_flush(wl_resource_get_client(resource));
}

void WaylandRemoteShell::SendBoundsChanged(
    wl_resource* resource,
    int64_t display_id,
    const gfx::Rect& bounds_in_display,
    uint32_t reason) {
  if (event_mapping_.send_bounds_changed)
    event_mapping_.send_bounds_changed(
        resource, static_cast<uint32_t>(display_id >> 32),
        static_cast<uint32_t>(display_id), bounds_in_display.x(),
        bounds_in_display.y(), bounds_in_display.width(),
        bounds_in_display.height(), reason);

  if (wl_resource_get_version(resource) >=
      event_mapping_.bounds_changed_in_output_since_version) {
    wl_resource* output = output_provider_.Run(display_id);
    if (output == nullptr) {
      LOG(WARNING) << "Failed to get wayland_output resource for display_id: "
                   << display_id;
      return;
    }
    event_mapping_.send_bounds_changed_in_output(
        resource, output, bounds_in_display.x(), bounds_in_display.y(),
        bounds_in_display.width(), bounds_in_display.height(), reason);
  }
}

void WaylandRemoteShell::OnRemoteSurfaceStateChanged(
    wl_resource* resource,
    WindowStateType old_state_type,
    WindowStateType new_state_type) {
  DCHECK_NE(old_state_type, new_state_type);
  LOG_IF(ERROR, pending_bounds_changes_.count(resource) > 0)
      << "Sending window state while there is a pending bounds change. This "
         "should not happen.";

  uint32_t state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_NORMAL;
  switch (new_state_type) {
    case WindowStateType::kMinimized:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_MINIMIZED;
      break;
    case WindowStateType::kMaximized:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_MAXIMIZED;
      break;
    case WindowStateType::kFullscreen:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_FULLSCREEN;
      break;
    case WindowStateType::kPinned:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_PINNED;
      break;
    case WindowStateType::kTrustedPinned:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_TRUSTED_PINNED;
      break;
    case WindowStateType::kPrimarySnapped:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_LEFT_SNAPPED;
      break;
    case WindowStateType::kSecondarySnapped:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_RIGHT_SNAPPED;
      break;
    case WindowStateType::kPip:
      state_type = ZCR_REMOTE_SHELL_V1_STATE_TYPE_PIP;
      break;
    default:
      break;
  }

  event_mapping_.send_state_type_changed(resource, state_type);
  wl_client_flush(wl_resource_get_client(resource));
}

void WaylandRemoteShell::OnRemoteSurfaceChangeZoomLevel(wl_resource* resource,
                                                        ZoomChange change) {
  int32_t value = 0;
  switch (change) {
    case ZoomChange::IN:
      value = ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_IN;
      break;
    case ZoomChange::OUT:
      value = ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_OUT;
      break;
    case ZoomChange::RESET:
      value = ZCR_REMOTE_SURFACE_V1_ZOOM_CHANGE_RESET;
      break;
  }
  event_mapping_.send_change_zoom_level(resource, value);
  wl_client_flush(wl_resource_get_client(resource));
}

void WaylandRemoteShell::OnRemoteSurfaceGeometryChanged(
    wl_resource* resource,
    const gfx::Rect& geometry) {
  LOG_IF(ERROR, pending_bounds_changes_.count(resource) > 0)
      << "Sending the new window geometry while there is a pending bounds "
         "change. This should not happen.";
  event_mapping_.send_window_geometry_changed(resource, geometry.x(),
                                              geometry.y(), geometry.width(),
                                              geometry.height());
  wl_client_flush(wl_resource_get_client(resource));
}

namespace switches {

// This flag can be used to emulate device scale factor for remote shell.
constexpr char kForceRemoteShellScale[] = "force-remote-shell-scale";

}  // namespace switches

using chromeos::WindowStateType;

namespace zcr_remote_shell {

// Returns the scale factor to be used by remote shell clients.
double GetDefaultDeviceScaleFactor() {
  // A flag used by VM to emulate a device scale for a particular board.
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kForceRemoteShellScale)) {
    std::string value =
        command_line->GetSwitchValueASCII(switches::kForceRemoteShellScale);
    double scale = 1.0;
    if (base::StringToDouble(value, &scale))
      return std::max(1.0, scale);
  }
  return ::exo::GetDefaultDeviceScaleFactor();
}

// Scale the |child_bounds| in such a way that if it should fill the
// |parent_size|'s width/height, it returns the |parent_size_in_pixel|'s
// width/height.
gfx::Rect ScaleBoundsToPixelSnappedToParent(
    const gfx::Size& parent_size_in_pixel,
    const gfx::Size& parent_size,
    float device_scale_factor,
    const gfx::Rect& child_bounds) {
  int right = child_bounds.right();
  int bottom = child_bounds.bottom();

  int new_x = base::ClampRound(child_bounds.x() * device_scale_factor);
  int new_y = base::ClampRound(child_bounds.y() * device_scale_factor);

  int new_right = right == parent_size.width()
                      ? parent_size_in_pixel.width()
                      : base::ClampRound(right * device_scale_factor);

  int new_bottom = bottom == parent_size.height()
                       ? parent_size_in_pixel.height()
                       : base::ClampRound(bottom * device_scale_factor);
  return gfx::Rect(new_x, new_y, new_right - new_x, new_bottom - new_y);
}

void ScaleSkRegion(const SkRegion& src, float scale, SkRegion* dst) {
  SkRegion::Iterator iter(src);
  for (; !iter.done(); iter.next()) {
    SkIRect r;
    r.fLeft = base::ClampFloor(iter.rect().fLeft * scale);
    r.fTop = base::ClampFloor(iter.rect().fTop * scale);
    r.fRight = base::ClampCeil(iter.rect().fRight * scale);
    r.fBottom = base::ClampCeil(iter.rect().fBottom * scale);
    dst->op(r, SkRegion::kUnion_Op);
  }
}

int Component(uint32_t direction) {
  switch (direction) {
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_NONE:
      return HTNOWHERE;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOP:
      return HTTOP;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPRIGHT:
      return HTTOPRIGHT;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_RIGHT:
      return HTRIGHT;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMRIGHT:
      return HTBOTTOMRIGHT;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOM:
      return HTBOTTOM;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_BOTTOMLEFT:
      return HTBOTTOMLEFT;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_LEFT:
      return HTLEFT;
    case ZCR_REMOTE_SURFACE_V1_RESIZE_DIRECTION_TOPLEFT:
      return HTTOPLEFT;
    default:
      VLOG(2) << "Unknown direction:" << direction;
      break;
  }
  return HTNOWHERE;
}

uint32_t CaptionButtonMask(uint32_t mask) {
  uint32_t caption_button_icon_mask = 0;
  if (mask & ZCR_REMOTE_SURFACE_V1_FRAME_BUTTON_TYPE_BACK)
    caption_button_icon_mask |= 1 << views::CAPTION_BUTTON_ICON_BACK;
  if (mask & ZCR_REMOTE_SURFACE_V1_FRAME_BUTTON_TYPE_MENU)
    caption_button_icon_mask |= 1 << views::CAPTION_BUTTON_ICON_MENU;
  if (mask & ZCR_REMOTE_SURFACE_V1_FRAME_BUTTON_TYPE_MINIMIZE)
    caption_button_icon_mask |= 1 << views::CAPTION_BUTTON_ICON_MINIMIZE;
  if (mask & ZCR_REMOTE_SURFACE_V1_FRAME_BUTTON_TYPE_MAXIMIZE_RESTORE)
    caption_button_icon_mask |= 1
                                << views::CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE;
  if (mask & ZCR_REMOTE_SURFACE_V1_FRAME_BUTTON_TYPE_CLOSE)
    caption_button_icon_mask |= 1 << views::CAPTION_BUTTON_ICON_CLOSE;
  if (mask & ZCR_REMOTE_SURFACE_V1_FRAME_BUTTON_TYPE_ZOOM)
    caption_button_icon_mask |= 1 << views::CAPTION_BUTTON_ICON_ZOOM;
  if (mask & ZCR_REMOTE_SURFACE_V1_FRAME_BUTTON_TYPE_CENTER)
    caption_button_icon_mask |= 1 << views::CAPTION_BUTTON_ICON_CENTER;
  if (mask & ZCR_REMOTE_SURFACE_V2_FRAME_BUTTON_TYPE_FLOAT) {
    caption_button_icon_mask |= 1 << views::CAPTION_BUTTON_ICON_FLOAT;
  }
  return caption_button_icon_mask;
}

////////////////////////////////////////////////////////////////////////////////
// remote_surface_interface:

SurfaceFrameType RemoteShellSurfaceFrameType(uint32_t frame_type) {
  switch (frame_type) {
    case ZCR_REMOTE_SURFACE_V1_FRAME_TYPE_NONE:
      return SurfaceFrameType::NONE;
    case ZCR_REMOTE_SURFACE_V1_FRAME_TYPE_NORMAL:
      return SurfaceFrameType::NORMAL;
    case ZCR_REMOTE_SURFACE_V1_FRAME_TYPE_SHADOW:
      return SurfaceFrameType::SHADOW;
    case ZCR_REMOTE_SURFACE_V1_FRAME_TYPE_AUTOHIDE:
      return SurfaceFrameType::AUTOHIDE;
    case ZCR_REMOTE_SURFACE_V1_FRAME_TYPE_OVERLAY:
      return SurfaceFrameType::OVERLAY;
    case ZCR_REMOTE_SURFACE_V2_FRAME_TYPE_OVERLAP:
      return SurfaceFrameType::OVERLAP;
    default:
      VLOG(2) << "Unknown remote-shell frame type: " << frame_type;
      return SurfaceFrameType::NONE;
  }
}

void remote_surface_set_app_id(wl_client* client,
                               wl_resource* resource,
                               const char* app_id) {
  GetUserDataAs<ShellSurfaceBase>(resource)->SetApplicationId(app_id);
}

void remote_surface_set_window_geometry(wl_client* client,
                                        wl_resource* resource,
                                        int32_t x,
                                        int32_t y,
                                        int32_t width,
                                        int32_t height) {
  // DEPRECATED - Use set_bounds to send bounds info with a display_id.
  GetUserDataAs<ShellSurfaceBase>(resource)->SetGeometry(
      gfx::Rect(x, y, width, height));
}

void remote_surface_set_orientation(wl_client* client,
                                    wl_resource* resource,
                                    int32_t orientation) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetOrientation(
      orientation == ZCR_REMOTE_SURFACE_V1_ORIENTATION_PORTRAIT
          ? Orientation::PORTRAIT
          : Orientation::LANDSCAPE);
}

void remote_surface_set_scale(wl_client* client,
                              wl_resource* resource,
                              wl_fixed_t scale) {
  // DEPRECATED (b/141715728) - The server updates the client's scale.
  NOTREACHED();
}

void remote_surface_set_rectangular_shadow_DEPRECATED(wl_client* client,
                                                      wl_resource* resource,
                                                      int32_t x,
                                                      int32_t y,
                                                      int32_t width,
                                                      int32_t height) {
  NOTREACHED();
}

void remote_surface_set_rectangular_shadow_background_opacity_DEPRECATED(
    wl_client* client,
    wl_resource* resource,
    wl_fixed_t opacity) {
  NOTREACHED();
}

void remote_surface_set_title(wl_client* client,
                              wl_resource* resource,
                              const char* title) {
  GetUserDataAs<ShellSurfaceBase>(resource)->SetTitle(
      std::u16string(base::UTF8ToUTF16(title)));
}

void remote_surface_set_top_inset(wl_client* client,
                                  wl_resource* resource,
                                  int32_t height) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetTopInset(height);
}

void remote_surface_activate(wl_client* client,
                             wl_resource* resource,
                             uint32_t serial) {
  ShellSurfaceBase* shell_surface = GetUserDataAs<ShellSurfaceBase>(resource);
  shell_surface->Activate();
}

void remote_surface_maximize(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetMaximized();
}

void remote_surface_minimize(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetMinimized();
}

void remote_surface_restore(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetRestored();
}

void remote_surface_fullscreen(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetFullscreen(
      true, display::kInvalidDisplayId);
}

void remote_surface_unfullscreen(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetFullscreen(
      false, display::kInvalidDisplayId);
}

void remote_surface_pin(wl_client* client,
                        wl_resource* resource,
                        int32_t trusted) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetPinned(
      trusted ? chromeos::WindowPinType::kTrustedPinned
              : chromeos::WindowPinType::kPinned);
}

void remote_surface_unpin(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetPinned(
      chromeos::WindowPinType::kNone);
}

void remote_surface_set_system_modal(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetSystemModal(true);
}

void remote_surface_unset_system_modal(wl_client* client,
                                       wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetSystemModal(false);
}

void remote_surface_set_rectangular_surface_shadow(wl_client* client,
                                                   wl_resource* resource,
                                                   int32_t x,
                                                   int32_t y,
                                                   int32_t width,
                                                   int32_t height) {
  // Shadow Bounds are set in pixels, and should not be scaled.
  ClientControlledShellSurface* shell_surface =
      GetUserDataAs<ClientControlledShellSurface>(resource);
  shell_surface->SetShadowBounds(gfx::Rect(x, y, width, height));
}

void remote_surface_set_systemui_visibility(wl_client* client,
                                            wl_resource* resource,
                                            uint32_t visibility) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetSystemUiVisibility(
      visibility != ZCR_REMOTE_SURFACE_V1_SYSTEMUI_VISIBILITY_STATE_VISIBLE);
}

void remote_surface_set_always_on_top(wl_client* client,
                                      wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetAlwaysOnTop(true);
}

void remote_surface_unset_always_on_top(wl_client* client,
                                        wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetAlwaysOnTop(false);
}

void remote_surface_ack_configure_DEPRECATED(wl_client* client,
                                             wl_resource* resource,
                                             uint32_t serial) {
  NOTREACHED();
}

void remote_surface_move_DEPRECATED(wl_client* client, wl_resource* resource) {
  NOTREACHED();
}

void remote_surface_set_window_type(wl_client* client,
                                    wl_resource* resource,
                                    uint32_t type) {
  auto* widget = GetUserDataAs<ShellSurfaceBase>(resource)->GetWidget();
  if (!widget)
    return;

  switch (type) {
    case ZCR_REMOTE_SURFACE_V1_WINDOW_TYPE_NORMAL:
      widget->GetNativeWindow()->SetProperty(ash::kHideInOverviewKey, false);
      break;
    case ZCR_REMOTE_SURFACE_V1_WINDOW_TYPE_SYSTEM_UI:
      // TODO(takise): Consider removing this as this window type was added for
      // the old assistant and is not longer used.
      widget->GetNativeWindow()->SetProperty(ash::kHideInOverviewKey, true);
      wm::SetWindowVisibilityAnimationType(
          widget->GetNativeWindow(), wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
      break;
    case ZCR_REMOTE_SURFACE_V1_WINDOW_TYPE_HIDDEN_IN_OVERVIEW:
      widget->GetNativeWindow()->SetProperty(ash::kHideInOverviewKey, true);
      break;
  }
}

void remote_surface_resize_DEPRECATED(wl_client* client,
                                      wl_resource* resource) {
  // DEPRECATED
  NOTREACHED();
}

void remote_surface_set_resize_outset_DEPRECATED(wl_client* client,
                                                 wl_resource* resource,
                                                 int32_t outset) {
  // DEPRECATED
  NOTREACHED();
}

void remote_surface_start_move(wl_client* client,
                               wl_resource* resource,
                               int32_t x,
                               int32_t y) {
  ClientControlledShellSurface* shell_surface =
      GetUserDataAs<ClientControlledShellSurface>(resource);
  float scale = shell_surface->GetClientToDpScale();
  gfx::PointF p(x, y);
  shell_surface->StartDrag(HTCAPTION, gfx::ScalePoint(p, scale));
}

void remote_surface_set_can_maximize(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetCanMaximize(true);
}

void remote_surface_unset_can_maximize(wl_client* client,
                                       wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetCanMaximize(false);
}

void remote_surface_set_min_size(wl_client* client,
                                 wl_resource* resource,
                                 int32_t width,
                                 int32_t height) {
  ClientControlledShellSurface* shell_surface =
      GetUserDataAs<ClientControlledShellSurface>(resource);
  float scale = shell_surface->GetClientToDpPendingScale();
  gfx::Size s(width, height);
  shell_surface->SetMinimumSize(gfx::ScaleToRoundedSize(s, scale));
}

void remote_surface_set_max_size(wl_client* client,
                                 wl_resource* resource,
                                 int32_t width,
                                 int32_t height) {
  ClientControlledShellSurface* shell_surface =
      GetUserDataAs<ClientControlledShellSurface>(resource);
  float scale = shell_surface->GetClientToDpPendingScale();
  gfx::Size s(width, height);
  shell_surface->SetMaximumSize(gfx::ScaleToRoundedSize(s, scale));
}

void remote_surface_set_aspect_ratio(wl_client* client,
                                     wl_resource* resource,
                                     int32_t aspect_ratio_width,
                                     int32_t aspect_ratio_height) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetAspectRatio(
      gfx::SizeF(aspect_ratio_width, aspect_ratio_height));
}

void remote_surface_set_snapped_to_left(wl_client* client,
                                        wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetSnapPrimary(
      chromeos::kDefaultSnapRatio);
}

void remote_surface_set_snapped_to_right(wl_client* client,
                                         wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetSnapSecondary(
      chromeos::kDefaultSnapRatio);
}

void remote_surface_start_resize(wl_client* client,
                                 wl_resource* resource,
                                 uint32_t direction,
                                 int32_t x,
                                 int32_t y) {
  ClientControlledShellSurface* shell_surface =
      GetUserDataAs<ClientControlledShellSurface>(resource);
  float scale = shell_surface->GetClientToDpScale();
  gfx::PointF p(x, y);
  shell_surface->StartDrag(Component(direction), gfx::ScalePoint(p, scale));
}

void remote_surface_set_frame(wl_client* client,
                              wl_resource* resource,
                              uint32_t type) {
  ClientControlledShellSurface* shell_surface =
      GetUserDataAs<ClientControlledShellSurface>(resource);
  shell_surface->root_surface()->SetFrame(RemoteShellSurfaceFrameType(type));
}

void remote_surface_set_frame_buttons(wl_client* client,
                                      wl_resource* resource,
                                      uint32_t visible_button_mask,
                                      uint32_t enabled_button_mask) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetFrameButtons(
      CaptionButtonMask(visible_button_mask),
      CaptionButtonMask(enabled_button_mask));
}

void remote_surface_set_extra_title(wl_client* client,
                                    wl_resource* resource,
                                    const char* extra_title) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetExtraTitle(
      std::u16string(base::UTF8ToUTF16(extra_title)));
}

chromeos::OrientationType OrientationLock(uint32_t orientation_lock) {
  switch (orientation_lock) {
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_NONE:
      return chromeos::OrientationType::kAny;
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_CURRENT:
      return chromeos::OrientationType::kCurrent;
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_PORTRAIT:
      return chromeos::OrientationType::kPortrait;
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_LANDSCAPE:
      return chromeos::OrientationType::kLandscape;
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_PORTRAIT_PRIMARY:
      return chromeos::OrientationType::kPortraitPrimary;
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_PORTRAIT_SECONDARY:
      return chromeos::OrientationType::kPortraitSecondary;
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_LANDSCAPE_PRIMARY:
      return chromeos::OrientationType::kLandscapePrimary;
    case ZCR_REMOTE_SURFACE_V1_ORIENTATION_LOCK_LANDSCAPE_SECONDARY:
      return chromeos::OrientationType::kLandscapeSecondary;
  }
  VLOG(2) << "Unexpected value of orientation_lock: " << orientation_lock;
  return chromeos::OrientationType::kAny;
}

void remote_surface_set_orientation_lock(wl_client* client,
                                         wl_resource* resource,
                                         uint32_t orientation_lock) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetOrientationLock(
      OrientationLock(orientation_lock));
}

void remote_surface_pip(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetPip();
}

void remote_surface_set_bounds(wl_client* client,
                               wl_resource* resource,
                               uint32_t display_id_hi,
                               uint32_t display_id_lo,
                               int32_t x,
                               int32_t y,
                               int32_t width,
                               int32_t height) {
  // Bounds are set in pixels, and should not be scaled.
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetBounds(
      static_cast<int64_t>(display_id_hi) << 32 | display_id_lo,
      gfx::Rect(x, y, width, height));
}

void remote_surface_block_ime(wl_client* client, wl_resource* resource) {
  NOTIMPLEMENTED();
}

void remote_surface_unblock_ime(wl_client* client, wl_resource* resource) {
  NOTIMPLEMENTED();
}

void remote_surface_set_accessibility_id_DEPRECATED(wl_client* client,
                                                    wl_resource* resource,
                                                    int32_t accessibility_id) {
  NOTREACHED();
}

void remote_surface_set_pip_original_window(wl_client* client,
                                            wl_resource* resource) {
  auto* widget = GetUserDataAs<ShellSurfaceBase>(resource)->GetWidget();
  if (!widget) {
    LOG(ERROR) << "no widget found for setting pip original window";
    return;
  }

  widget->GetNativeWindow()->SetProperty(ash::kPipOriginalWindowKey, true);
}

void remote_surface_unset_pip_original_window(wl_client* client,
                                              wl_resource* resource) {
  auto* widget = GetUserDataAs<ShellSurfaceBase>(resource)->GetWidget();
  if (!widget) {
    LOG(ERROR) << "no widget found for unsetting pip original window";
    return;
  }

  widget->GetNativeWindow()->SetProperty(ash::kPipOriginalWindowKey, false);
}

void remote_surface_set_system_gesture_exclusion(wl_client* client,
                                                 wl_resource* resource,
                                                 wl_resource* region_resource) {
  auto* shell_surface = GetUserDataAs<ClientControlledShellSurface>(resource);
  auto* widget = shell_surface->GetWidget();
  if (!widget) {
    LOG(ERROR) << "no widget found for setting system gesture exclusion";
    return;
  }

  if (region_resource) {
    SkRegion* dst = new SkRegion;
    ScaleSkRegion(*GetUserDataAs<SkRegion>(region_resource),
                  shell_surface->GetClientToDpScale(), dst);
    widget->GetNativeWindow()->SetProperty(ash::kSystemGestureExclusionKey,
                                           dst);
  } else {
    widget->GetNativeWindow()->ClearProperty(ash::kSystemGestureExclusionKey);
  }
}

void remote_surface_set_resize_lock(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetResizeLockType(
      ash::ArcResizeLockType::RESIZE_DISABLED_TOGGLABLE);
}

void remote_surface_unset_resize_lock(wl_client* client,
                                      wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetResizeLockType(
      ash::ArcResizeLockType::NONE);
}

void remote_surface_set_bounds_in_output(wl_client* client,
                                         wl_resource* resource,
                                         wl_resource* output_resource,
                                         int32_t x,
                                         int32_t y,
                                         int32_t width,
                                         int32_t height) {
  WaylandDisplayHandler* display_handler =
      GetUserDataAs<WaylandDisplayHandler>(output_resource);
  // Bounds are set in pixels, and should not be scaled.
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetBounds(
      display_handler->id(), gfx::Rect(x, y, width, height));
}

void remote_surface_set_resize_lock_type(wl_client* client,
                                         wl_resource* resource,
                                         uint32_t type) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetResizeLockType(
      static_cast<ash::ArcResizeLockType>(type));
}

void remote_surface_set_float(wl_client* client, wl_resource* resource) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetFloatToLocation(
      chromeos::FloatStartLocation::kBottomRight);
}

void remote_surface_set_scale_factor(wl_client* client,
                                     wl_resource* resource,
                                     uint scale_factor_as_uint) {
  static_assert(sizeof(uint32_t) == sizeof(float),
                "Sizes much match for reinterpret cast to be meaningful");
  float scale_factor = *reinterpret_cast<float*>(&scale_factor_as_uint);
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetScaleFactor(
      scale_factor);
}

void remote_surface_set_window_corner_radii(wl_client* client,
                                            wl_resource* resource,
                                            uint32_t upper_left_radius,
                                            uint32_t upper_right_radius,
                                            uint32_t lower_right_radius,
                                            uint32_t lower_left_radius) {
  GetUserDataAs<ClientControlledShellSurface>(resource)->SetWindowCornerRadii(
      gfx::RoundedCornersF(upper_left_radius, upper_right_radius,
                           lower_right_radius, lower_left_radius));
}

////////////////////////////////////////////////////////////////////////////////
// notification_surface_interface:

void notification_surface_set_app_id(wl_client* client,
                                     wl_resource* resource,
                                     const char* app_id) {
  GetUserDataAs<NotificationSurface>(resource)->SetApplicationId(app_id);
}

////////////////////////////////////////////////////////////////////////////////
// input_method_surface_interface:

void input_method_surface_set_bounds(wl_client* client,
                                     wl_resource* resource,
                                     uint32_t display_id_hi,
                                     uint32_t display_id_lo,
                                     int32_t x,
                                     int32_t y,
                                     int32_t width,
                                     int32_t height) {
  GetUserDataAs<InputMethodSurface>(resource)->SetBounds(
      static_cast<int64_t>(display_id_hi) << 32 | display_id_lo,
      gfx::Rect(x, y, width, height));
}

void input_method_surface_set_bounds_in_output(wl_client* client,
                                               wl_resource* resource,
                                               wl_resource* output_resource,
                                               int32_t x,
                                               int32_t y,
                                               int32_t width,
                                               int32_t height) {
  WaylandDisplayHandler* display_handler =
      GetUserDataAs<WaylandDisplayHandler>(output_resource);
  GetUserDataAs<InputMethodSurface>(resource)->SetBounds(
      display_handler->id(), gfx::Rect(x, y, width, height));
}

////////////////////////////////////////////////////////////////////////////////
// toast_surface_interface:

void toast_surface_set_position(wl_client* client,
                                wl_resource* resource,
                                uint32_t display_id_hi,
                                uint32_t display_id_lo,
                                int32_t x,
                                int32_t y) {
  const int64_t display_id =
      static_cast<int64_t>(display_id_hi) << 32 | display_id_lo;
  GetUserDataAs<ToastSurface>(resource)->SetBoundsOrigin(display_id,
                                                         gfx::Point(x, y));
}

void toast_surface_set_size(wl_client* client,
                            wl_resource* resource,
                            int32_t width,
                            int32_t height) {
  GetUserDataAs<ToastSurface>(resource)->SetBoundsSize(
      gfx::Size(width, height));
}

void toast_surface_set_bounds_in_output(wl_client* client,
                                        wl_resource* resource,
                                        wl_resource* output_resource,
                                        int32_t x,
                                        int32_t y,
                                        int32_t width,
                                        int32_t height) {
  WaylandDisplayHandler* display_handler =
      GetUserDataAs<WaylandDisplayHandler>(output_resource);
  GetUserDataAs<ToastSurface>(resource)->SetBounds(
      display_handler->id(), gfx::Rect(x, y, width, height));
}

void toast_surface_set_scale_factor(wl_client* client,
                                    wl_resource* resource,
                                    uint scale_factor_as_uint) {
  static_assert(sizeof(uint32_t) == sizeof(float),
                "Sizes must match for reinterpret cast to be meaningful");
  float scale_factor = *reinterpret_cast<float*>(&scale_factor_as_uint);
  GetUserDataAs<ToastSurface>(resource)->SetScaleFactor(scale_factor);
}

////////////////////////////////////////////////////////////////////////////////
// remote_shell_interface:

void remote_shell_set_use_default_scale_cancellation(
    wl_client*,
    wl_resource* resource,
    int32_t use_default_scale_cancellation) {
  auto* shell = GetUserDataAs<WaylandRemoteShell>(resource);
  if (wl_resource_get_version(resource) <
      shell->event_mapping_.set_use_default_scale_cancellation_since_version) {
    return;
  }
  shell->SetUseDefaultScaleCancellation(use_default_scale_cancellation != 0);
}

}  // namespace zcr_remote_shell

}  // namespace wayland
}  // namespace exo