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
//! Utilities for handling shell surfaces with the `xdg_shell` protocol
//!
//! This module provides automatic handling of shell surfaces objects, by being registered
//! as a global handler for `xdg_shell`.
//!
//! ## Why use this implementation
//!
//! This implementation can track for you the various shell surfaces defined by the
//! clients by handling the `xdg_shell` protocol. It also contains a compatibility
//! layer handling its precursor, the unstable `zxdg_shell_v6` protocol, which is
//! mostly identical.
//!
//! It allows you to easily access a list of all shell surfaces defined by your clients
//! access their associated metadata and underlying `wl_surface`s.
//!
//! This handler only handles the protocol exchanges with the client to present you the
//! information in a coherent and relatively easy to use manner. All the actual drawing
//! and positioning logic of windows is out of its scope.
//!
//! ## How to use it
//!
//! ### Initialization
//!
//! To initialize this handler, simple use the [`xdg_shell_init`] function provided in this module.
//! You need to provide a closure that will be invoked whenever some action is required from you,
//! are represented by the [`XdgRequest`] enum.
//!
//! ```no_run
//! # extern crate wayland_server;
//! #
//! use smithay::wayland::shell::xdg::{xdg_shell_init, XdgRequest};
//!
//! # let mut display = wayland_server::Display::new();
//! let (shell_state, _, _) = xdg_shell_init(
//!     &mut display,
//!     // your implementation
//!     |event: XdgRequest, dispatch_data| { /* handle the shell requests here */ },
//!     None  // put a logger if you want
//! );
//!
//! // You're now ready to go!
//! ```
//!
//! ### Access to shell surface and clients data
//!
//! There are mainly 3 kind of objects that you'll manipulate from this implementation:
//!
//! - [`ShellClient`]:
//!   This is a handle representing an instantiation of a shell global
//!   you can associate client-wise metadata to it through an [`UserDataMap`].
//! - [`ToplevelSurface`]:
//!   This is a handle representing a toplevel surface, you can
//!   retrieve a list of all currently alive toplevel surface from the
//!   [`ShellState`].
//! - [`PopupSurface`]:
//!   This is a handle representing a popup/tooltip surface. Similarly,
//!   you can get a list of all currently alive popup surface from the
//!   [`ShellState`].
//!
//! You'll obtain these objects though two means: either via the callback methods of
//! the subhandler you provided, or via methods on the [`ShellState`]
//! that you are given (in an `Arc<Mutex<_>>`) as return value of the `init` function.

use crate::utils::DeadResource;
use crate::utils::{Logical, Point, Rectangle, Size};
use crate::wayland::compositor;
use crate::wayland::compositor::Cacheable;
use crate::wayland::{Serial, SERIAL_COUNTER};
use std::fmt::Debug;
use std::{
    cell::RefCell,
    rc::Rc,
    sync::{Arc, Mutex},
};
use wayland_protocols::unstable::xdg_shell::v6::server::zxdg_surface_v6;
use wayland_protocols::xdg_shell::server::xdg_surface;
use wayland_protocols::{
    unstable::xdg_shell::v6::server::{zxdg_popup_v6, zxdg_shell_v6, zxdg_toplevel_v6},
    xdg_shell::server::{xdg_popup, xdg_positioner, xdg_toplevel, xdg_wm_base},
};
use wayland_server::DispatchData;
use wayland_server::{
    protocol::{wl_output, wl_seat, wl_surface},
    Display, Filter, Global, UserDataMap,
};

use super::PingError;

// handlers for the xdg_shell protocol
mod xdg_handlers;
// compatibility handlers for the zxdg_shell_v6 protocol, its earlier version
mod zxdgv6_handlers;

macro_rules! xdg_role {
    ($configure:ty,
     $(#[$attr:meta])* $element:ident {$($(#[$field_attr:meta])* $vis:vis$field:ident:$type:ty),*},
     $role_ack_configure:expr) => {

        $(#[$attr])*
        pub struct $element {
                /// Defines if the surface has received at least one
                /// xdg_surface.ack_configure from the client
                pub configured: bool,

                /// The serial of the last acked configure
                pub configure_serial: Option<Serial>,

                /// Holds the state if the surface has sent the initial
                /// configure event to the client. It is expected that
                /// during the first commit a initial
                /// configure event is sent to the client
                pub initial_configure_sent: bool,

                /// Holds the configures the server has sent out
                /// to the client waiting to be acknowledged by
                /// the client. All pending configures that are older
                /// than the acknowledged one will be discarded during
                /// processing xdg_surface.ack_configure.
                pending_configures: Vec<$configure>,

                $(
                    $(#[$field_attr])*
                    $vis $field: $type,
                )*
        }

        impl $element {
            fn ack_configure(&mut self, serial: Serial) -> Option<Configure> {
                let configure = match self
                    .pending_configures
                    .iter()
                    .find(|configure| configure.serial == serial)
                {
                    Some(configure) => (*configure).clone(),
                    None => {
                        return None;
                    }
                };

                // Role specific ack_configure
                let role_ack_configure: &dyn Fn(&mut Self, $configure) = &$role_ack_configure;
                role_ack_configure(self, configure.clone());

                // Set the xdg_surface to configured
                self.configured = true;

                // Save the last configure serial as a reference
                self.configure_serial = Some(Serial::from(serial));

                // Clean old configures
                self.pending_configures.retain(|c| c.serial > serial);

                Some(configure.into())
            }
        }

        impl Default for $element {
            fn default() -> Self {
                Self {
                    configured: false,
                    configure_serial: None,
                    pending_configures: Vec::new(),
                    initial_configure_sent: false,

                    $(
                        $field: Default::default(),
                    )*
                }
            }
        }
    };
}

xdg_role!(
    ToplevelConfigure,
    /// Role specific attributes for xdg_toplevel
    ///
    /// This interface defines an xdg_surface role which allows a surface to,
    /// among other things, set window-like properties such as maximize,
    /// fullscreen, and minimize, set application-specific metadata like title and
    /// id, and well as trigger user interactive operations such as interactive
    /// resize and move.
    ///
    /// Unmapping an xdg_toplevel means that the surface cannot be shown
    /// by the compositor until it is explicitly mapped again.
    /// All active operations (e.g., move, resize) are cancelled and all
    /// attributes (e.g. title, state, stacking, ...) are discarded for
    /// an xdg_toplevel surface when it is unmapped. The xdg_toplevel returns to
    /// the state it had right after xdg_surface.get_toplevel. The client
    /// can re-map the toplevel by performing a commit without any buffer
    /// attached, waiting for a configure event and handling it as usual (see
    /// xdg_surface description).
    ///
    /// Attaching a null buffer to a toplevel unmaps the surface.
    #[derive(Debug)]
    XdgToplevelSurfaceRoleAttributes {
        /// The parent field of a toplevel should be used
        /// by the compositor to determine which toplevel
        /// should be brought to front. If the parent is focused
        /// all of it's child should be brought to front.
        pub parent: Option<wl_surface::WlSurface>,
        /// Holds the optional title the client has set for
        /// this toplevel. For example a web-browser will most likely
        /// set this to include the current uri.
        ///
        /// This string may be used to identify the surface in a task bar,
        /// window list, or other user interface elements provided by the
        /// compositor.
        pub title: Option<String>,
        /// Holds the optional app ID the client has set for
        /// this toplevel.
        ///
        /// The app ID identifies the general class of applications to which
        /// the surface belongs. The compositor can use this to group multiple
        /// surfaces together, or to determine how to launch a new application.
        ///
        /// For D-Bus activatable applications, the app ID is used as the D-Bus
        /// service name.
        pub app_id: Option<String>,
        /// Minimum size requested for this surface
        ///
        /// A value of 0 on an axis means this axis is not constrained
        pub min_size: Size<i32, Logical>,
        /// Maximum size requested for this surface
        ///
        /// A value of 0 on an axis means this axis is not constrained
        pub max_size: Size<i32, Logical>,
        /// Holds the pending state as set by the server.
        pub server_pending: Option<ToplevelState>,
        /// Holds the last server_pending state that has been acknowledged
        /// by the client. This state should be cloned to the current
        /// during a commit.
        pub last_acked: Option<ToplevelState>,
        /// Holds the current state of the toplevel after a successful
        /// commit.
        pub current: ToplevelState
    },
    |attributes, configure| {
        attributes.last_acked = Some(configure.state);
    }
);

xdg_role!(
    PopupConfigure,
    /// Role specific attributes for xdg_popup
    ///
    /// A popup surface is a short-lived, temporary surface. It can be used to
    /// implement for example menus, popovers, tooltips and other similar user
    /// interface concepts.
    ///
    /// A popup can be made to take an explicit grab. See xdg_popup.grab for
    /// details.
    ///
    /// When the popup is dismissed, a popup_done event will be sent out, and at
    /// the same time the surface will be unmapped. See the xdg_popup.popup_done
    /// event for details.
    ///
    /// Explicitly destroying the xdg_popup object will also dismiss the popup and
    /// unmap the surface. Clients that want to dismiss the popup when another
    /// surface of their own is clicked should dismiss the popup using the destroy
    /// request.
    ///
    /// A newly created xdg_popup will be stacked on top of all previously created
    /// xdg_popup surfaces associated with the same xdg_toplevel.
    ///
    /// The parent of an xdg_popup must be mapped (see the xdg_surface
    /// description) before the xdg_popup itself.
    ///
    /// The client must call wl_surface.commit on the corresponding wl_surface
    /// for the xdg_popup state to take effect.
    #[derive(Debug)]
    XdgPopupSurfaceRoleAttributes {
        /// Holds the parent for the xdg_popup.
        ///
        /// The parent is allowed to remain unset as long
        /// as no commit has been requested for the underlying
        /// wl_surface. The parent can either be directly
        /// specified during xdg_surface.get_popup or using
        /// another protocol extension, for example xdg_layer_shell.
        ///
        /// It is a protocol error to call commit on a wl_surface with
        /// the xdg_popup role when no parent is set.
        pub parent: Option<wl_surface::WlSurface>,
        /// The positioner state can be used by the compositor
        /// to calculate the best placement for the popup.
        ///
        /// For example the compositor should prevent that a popup
        /// is placed outside the visible rectangle of a output.
        pub positioner: PositionerState,
        /// Holds the last server_pending state that has been acknowledged
        /// by the client. This state should be cloned to the current
        /// during a commit.
        pub last_acked: Option<PopupState>,
        /// Holds the current state of the popup after a successful
        /// commit.
        pub current: PopupState,
        /// Holds the pending state as set by the server.
        pub server_pending: Option<PopupState>,
        popup_handle: Option<PopupKind>
    },
    |attributes,configure| {
        attributes.last_acked = Some(configure.state);
    }
);

/// Represents the state of the popup
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PopupState {
    /// Holds the geometry of the popup as defined by the positioner.
    ///
    /// `Rectangle::width` and `Rectangle::height` holds the size of the
    /// of the popup in surface-local coordinates and corresponds to the
    /// window geometry
    ///
    /// `Rectangle::x` and `Rectangle::y` holds the position of the popup
    /// The position is relative to the window geometry as defined by
    /// xdg_surface.set_window_geometry of the parent surface.
    pub geometry: Rectangle<i32, Logical>,
}

impl Default for PopupState {
    fn default() -> Self {
        Self {
            geometry: Default::default(),
        }
    }
}

#[derive(Clone, Debug)]
/// The state of a positioner, as set by the client
pub struct PositionerState {
    /// Size of the rectangle that needs to be positioned
    pub rect_size: Size<i32, Logical>,
    /// Anchor rectangle in the parent surface coordinates
    /// relative to which the surface must be positioned
    pub anchor_rect: Rectangle<i32, Logical>,
    /// Edges defining the anchor point
    pub anchor_edges: xdg_positioner::Anchor,
    /// Gravity direction for positioning the child surface
    /// relative to its anchor point
    pub gravity: xdg_positioner::Gravity,
    /// Adjustments to do if previous criteria constrain the
    /// surface
    pub constraint_adjustment: xdg_positioner::ConstraintAdjustment,
    /// Offset placement relative to the anchor point
    pub offset: Point<i32, Logical>,
}

impl Default for PositionerState {
    fn default() -> Self {
        PositionerState {
            anchor_edges: xdg_positioner::Anchor::None,
            anchor_rect: Default::default(),
            constraint_adjustment: xdg_positioner::ConstraintAdjustment::empty(),
            gravity: xdg_positioner::Gravity::None,
            offset: Default::default(),
            rect_size: Default::default(),
        }
    }
}

impl PositionerState {
    pub(crate) fn anchor_has_edge(&self, edge: xdg_positioner::Anchor) -> bool {
        match edge {
            xdg_positioner::Anchor::Top => {
                self.anchor_edges == xdg_positioner::Anchor::Top
                    || self.anchor_edges == xdg_positioner::Anchor::TopLeft
                    || self.anchor_edges == xdg_positioner::Anchor::TopRight
            }
            xdg_positioner::Anchor::Bottom => {
                self.anchor_edges == xdg_positioner::Anchor::Bottom
                    || self.anchor_edges == xdg_positioner::Anchor::BottomLeft
                    || self.anchor_edges == xdg_positioner::Anchor::BottomRight
            }
            xdg_positioner::Anchor::Left => {
                self.anchor_edges == xdg_positioner::Anchor::Left
                    || self.anchor_edges == xdg_positioner::Anchor::TopLeft
                    || self.anchor_edges == xdg_positioner::Anchor::BottomLeft
            }
            xdg_positioner::Anchor::Right => {
                self.anchor_edges == xdg_positioner::Anchor::Right
                    || self.anchor_edges == xdg_positioner::Anchor::TopRight
                    || self.anchor_edges == xdg_positioner::Anchor::BottomRight
            }
            _ => unreachable!(),
        }
    }

    pub(crate) fn gravity_has_edge(&self, edge: xdg_positioner::Gravity) -> bool {
        match edge {
            xdg_positioner::Gravity::Top => {
                self.gravity == xdg_positioner::Gravity::Top
                    || self.gravity == xdg_positioner::Gravity::TopLeft
                    || self.gravity == xdg_positioner::Gravity::TopRight
            }
            xdg_positioner::Gravity::Bottom => {
                self.gravity == xdg_positioner::Gravity::Bottom
                    || self.gravity == xdg_positioner::Gravity::BottomLeft
                    || self.gravity == xdg_positioner::Gravity::BottomRight
            }
            xdg_positioner::Gravity::Left => {
                self.gravity == xdg_positioner::Gravity::Left
                    || self.gravity == xdg_positioner::Gravity::TopLeft
                    || self.gravity == xdg_positioner::Gravity::BottomLeft
            }
            xdg_positioner::Gravity::Right => {
                self.gravity == xdg_positioner::Gravity::Right
                    || self.gravity == xdg_positioner::Gravity::TopRight
                    || self.gravity == xdg_positioner::Gravity::BottomRight
            }
            _ => unreachable!(),
        }
    }

    /// Get the geometry for a popup as defined by this positioner.
    ///
    /// `Rectangle::width` and `Rectangle::height` corresponds to the
    /// size set by `xdg_positioner.set_size`.
    ///
    /// `Rectangle::x` and `Rectangle::y` define the position of the
    /// popup relative to it's parent surface `window_geometry`.
    /// The position is calculated according to the rules defined
    /// in the `xdg_shell` protocol.
    /// The `constraint_adjustment` will not be considered by this
    /// implementation and the position and size should be re-calculated
    /// in the compositor if the compositor implements `constraint_adjustment`
    pub(crate) fn get_geometry(&self) -> Rectangle<i32, Logical> {
        // From the `xdg_shell` prococol specification:
        //
        // set_offset:
        //
        //  Specify the surface position offset relative to the position of the
        //  anchor on the anchor rectangle and the anchor on the surface. For
        //  example if the anchor of the anchor rectangle is at (x, y), the surface
        //  has the gravity bottom|right, and the offset is (ox, oy), the calculated
        //  surface position will be (x + ox, y + oy)
        let mut geometry = Rectangle {
            loc: self.offset,
            size: self.rect_size,
        };

        // Defines the anchor point for the anchor rectangle. The specified anchor
        // is used derive an anchor point that the child surface will be
        // positioned relative to. If a corner anchor is set (e.g. 'top_left' or
        // 'bottom_right'), the anchor point will be at the specified corner;
        // otherwise, the derived anchor point will be centered on the specified
        // edge, or in the center of the anchor rectangle if no edge is specified.
        if self.anchor_has_edge(xdg_positioner::Anchor::Top) {
            geometry.loc.y += self.anchor_rect.loc.y;
        } else if self.anchor_has_edge(xdg_positioner::Anchor::Bottom) {
            geometry.loc.y += self.anchor_rect.loc.y + self.anchor_rect.size.h;
        } else {
            geometry.loc.y += self.anchor_rect.loc.y + self.anchor_rect.size.h / 2;
        }

        if self.anchor_has_edge(xdg_positioner::Anchor::Left) {
            geometry.loc.x += self.anchor_rect.loc.x;
        } else if self.anchor_has_edge(xdg_positioner::Anchor::Right) {
            geometry.loc.x += self.anchor_rect.loc.x + self.anchor_rect.size.w;
        } else {
            geometry.loc.x += self.anchor_rect.loc.x + self.anchor_rect.size.w / 2;
        }

        // Defines in what direction a surface should be positioned, relative to
        // the anchor point of the parent surface. If a corner gravity is
        // specified (e.g. 'bottom_right' or 'top_left'), then the child surface
        // will be placed towards the specified gravity; otherwise, the child
        // surface will be centered over the anchor point on any axis that had no
        // gravity specified.
        if self.gravity_has_edge(xdg_positioner::Gravity::Top) {
            geometry.loc.y -= geometry.size.h;
        } else if !self.gravity_has_edge(xdg_positioner::Gravity::Bottom) {
            geometry.loc.y -= geometry.size.h / 2;
        }

        if self.gravity_has_edge(xdg_positioner::Gravity::Left) {
            geometry.loc.x -= geometry.size.w;
        } else if !self.gravity_has_edge(xdg_positioner::Gravity::Right) {
            geometry.loc.x -= geometry.size.w / 2;
        }

        geometry
    }
}

/// State of a regular toplevel surface
#[derive(Debug, PartialEq)]
pub struct ToplevelState {
    /// The suggested size of the surface
    pub size: Option<Size<i32, Logical>>,

    /// The states for this surface
    pub states: ToplevelStateSet,

    /// The output for a fullscreen display
    pub fullscreen_output: Option<wl_output::WlOutput>,
}

impl Default for ToplevelState {
    fn default() -> Self {
        ToplevelState {
            fullscreen_output: None,
            states: Default::default(),
            size: None,
        }
    }
}

impl Clone for ToplevelState {
    fn clone(&self) -> ToplevelState {
        ToplevelState {
            fullscreen_output: self.fullscreen_output.clone(),
            states: self.states.clone(),
            size: self.size,
        }
    }
}

/// Container holding the states for a `XdgToplevel`
///
/// This container will prevent the `XdgToplevel` from
/// having the same `xdg_toplevel::State` multiple times
/// and simplifies setting and un-setting a particularly
/// `xdg_toplevel::State`
#[derive(Debug, Clone, PartialEq)]
pub struct ToplevelStateSet {
    states: Vec<xdg_toplevel::State>,
}

impl ToplevelStateSet {
    /// Returns `true` if the states contains a state.
    pub fn contains(&self, state: xdg_toplevel::State) -> bool {
        self.states.iter().any(|s| *s == state)
    }

    /// Adds a state to the states.
    ///
    /// If the states did not have this state present, `true` is returned.
    ///
    /// If the states did have this state present, `false` is returned.
    pub fn set(&mut self, state: xdg_toplevel::State) -> bool {
        if self.contains(state) {
            false
        } else {
            self.states.push(state);
            true
        }
    }

    /// Removes a state from the states. Returns whether the state was
    /// present in the states.
    pub fn unset(&mut self, state: xdg_toplevel::State) -> bool {
        if !self.contains(state) {
            false
        } else {
            self.states.retain(|s| *s != state);
            true
        }
    }
}

impl Default for ToplevelStateSet {
    fn default() -> Self {
        Self { states: Vec::new() }
    }
}

impl IntoIterator for ToplevelStateSet {
    type Item = xdg_toplevel::State;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.states.into_iter()
    }
}

impl From<ToplevelStateSet> for Vec<xdg_toplevel::State> {
    fn from(states: ToplevelStateSet) -> Self {
        states.states
    }
}

/// Represents the client pending state
#[derive(Debug, Clone, Copy)]
pub struct SurfaceCachedState {
    /// Holds the double-buffered geometry that may be specified
    /// by xdg_surface.set_window_geometry.
    pub geometry: Option<Rectangle<i32, Logical>>,
    /// Minimum size requested for this surface
    ///
    /// A value of 0 on an axis means this axis is not constrained
    ///
    /// This is only relevant for xdg_toplevel, and will always be
    /// `(0, 0)` for xdg_popup.
    pub min_size: Size<i32, Logical>,
    /// Maximum size requested for this surface
    ///
    /// A value of 0 on an axis means this axis is not constrained
    ///
    /// This is only relevant for xdg_toplevel, and will always be
    /// `(0, 0)` for xdg_popup.
    pub max_size: Size<i32, Logical>,
}

impl Default for SurfaceCachedState {
    fn default() -> Self {
        Self {
            geometry: None,
            min_size: Default::default(),
            max_size: Default::default(),
        }
    }
}

impl Cacheable for SurfaceCachedState {
    fn commit(&mut self) -> Self {
        *self
    }
    fn merge_into(self, into: &mut Self) {
        *into = self;
    }
}

pub(crate) struct ShellData {
    log: ::slog::Logger,
    user_impl: Rc<RefCell<dyn FnMut(XdgRequest, DispatchData<'_>)>>,
    shell_state: Arc<Mutex<ShellState>>,
}

impl Clone for ShellData {
    fn clone(&self) -> Self {
        ShellData {
            log: self.log.clone(),
            user_impl: self.user_impl.clone(),
            shell_state: self.shell_state.clone(),
        }
    }
}

/// Create a new `xdg_shell` globals
pub fn xdg_shell_init<L, Impl>(
    display: &mut Display,
    implementation: Impl,
    logger: L,
) -> (
    Arc<Mutex<ShellState>>,
    Global<xdg_wm_base::XdgWmBase>,
    Global<zxdg_shell_v6::ZxdgShellV6>,
)
where
    L: Into<Option<::slog::Logger>>,
    Impl: FnMut(XdgRequest, DispatchData<'_>) + 'static,
{
    let log = crate::slog_or_fallback(logger);
    let shell_state = Arc::new(Mutex::new(ShellState {
        known_toplevels: Vec::new(),
        known_popups: Vec::new(),
    }));

    let shell_data = ShellData {
        log: log.new(slog::o!("smithay_module" => "xdg_shell_handler")),
        user_impl: Rc::new(RefCell::new(implementation)),
        shell_state: shell_state.clone(),
    };

    let shell_data_z = shell_data.clone();

    let xdg_shell_global = display.create_global(
        1,
        Filter::new(move |(shell, _version), _, dispatch_data| {
            self::xdg_handlers::implement_wm_base(shell, &shell_data, dispatch_data);
        }),
    );

    let zxdgv6_shell_global = display.create_global(
        1,
        Filter::new(move |(shell, _version), _, dispatch_data| {
            self::zxdgv6_handlers::implement_shell(shell, &shell_data_z, dispatch_data);
        }),
    );

    (shell_state, xdg_shell_global, zxdgv6_shell_global)
}

/// Shell global state
///
/// This state allows you to retrieve a list of surfaces
/// currently known to the shell global.
#[derive(Debug)]
pub struct ShellState {
    known_toplevels: Vec<ToplevelSurface>,
    known_popups: Vec<PopupSurface>,
}

impl ShellState {
    /// Access all the shell surfaces known by this handler
    pub fn toplevel_surfaces(&self) -> &[ToplevelSurface] {
        &self.known_toplevels[..]
    }

    /// Access all the popup surfaces known by this handler
    pub fn popup_surfaces(&self) -> &[PopupSurface] {
        &self.known_popups[..]
    }
}

/*
 * User interaction
 */

#[derive(Debug)]
enum ShellClientKind {
    Xdg(xdg_wm_base::XdgWmBase),
    ZxdgV6(zxdg_shell_v6::ZxdgShellV6),
}

pub(crate) struct ShellClientData {
    pending_ping: Option<Serial>,
    data: UserDataMap,
}

fn make_shell_client_data() -> ShellClientData {
    ShellClientData {
        pending_ping: None,
        data: UserDataMap::new(),
    }
}

/// A shell client
///
/// This represents an instantiation of a shell
/// global (be it `wl_shell` or `xdg_shell`).
///
/// Most of the time, you can consider that a
/// Wayland client will be a single shell client.
///
/// You can use this handle to access a storage for any
/// client-specific data you wish to associate with it.
#[derive(Debug)]
pub struct ShellClient {
    kind: ShellClientKind,
}

impl std::cmp::PartialEq for ShellClient {
    fn eq(&self, other: &Self) -> bool {
        match (&self.kind, &other.kind) {
            (&ShellClientKind::Xdg(ref s1), &ShellClientKind::Xdg(ref s2)) => s1 == s2,
            (&ShellClientKind::ZxdgV6(ref s1), &ShellClientKind::ZxdgV6(ref s2)) => s1 == s2,
            _ => false,
        }
    }
}

impl ShellClient {
    /// Is the shell client represented by this handle still connected?
    pub fn alive(&self) -> bool {
        match self.kind {
            ShellClientKind::Xdg(ref s) => s.as_ref().is_alive(),
            ShellClientKind::ZxdgV6(ref s) => s.as_ref().is_alive(),
        }
    }

    /// Send a ping request to this shell client
    ///
    /// You'll receive the reply as a [`XdgRequest::ClientPong`] request.
    ///
    /// A typical use is to start a timer at the same time you send this ping
    /// request, and cancel it when you receive the pong. If the timer runs
    /// down to 0 before a pong is received, mark the client as unresponsive.
    ///
    /// Fails if this shell client already has a pending ping or is already dead.
    pub fn send_ping(&self, serial: Serial) -> Result<(), PingError> {
        if !self.alive() {
            return Err(PingError::DeadSurface);
        }
        match self.kind {
            ShellClientKind::Xdg(ref shell) => {
                let user_data = shell
                    .as_ref()
                    .user_data()
                    .get::<self::xdg_handlers::ShellUserData>()
                    .unwrap();
                let mut guard = user_data.client_data.lock().unwrap();
                if let Some(pending_ping) = guard.pending_ping {
                    return Err(PingError::PingAlreadyPending(pending_ping));
                }
                guard.pending_ping = Some(serial);
                shell.ping(serial.into());
            }
            ShellClientKind::ZxdgV6(ref shell) => {
                let user_data = shell
                    .as_ref()
                    .user_data()
                    .get::<self::zxdgv6_handlers::ShellUserData>()
                    .unwrap();
                let mut guard = user_data.client_data.lock().unwrap();
                if let Some(pending_ping) = guard.pending_ping {
                    return Err(PingError::PingAlreadyPending(pending_ping));
                }
                guard.pending_ping = Some(serial);
                shell.ping(serial.into());
            }
        }
        Ok(())
    }

    /// Access the user data associated with this shell client
    pub fn with_data<F, T>(&self, f: F) -> Result<T, crate::utils::DeadResource>
    where
        F: FnOnce(&mut UserDataMap) -> T,
    {
        if !self.alive() {
            return Err(crate::utils::DeadResource);
        }
        match self.kind {
            ShellClientKind::Xdg(ref shell) => {
                let data = shell
                    .as_ref()
                    .user_data()
                    .get::<self::xdg_handlers::ShellUserData>()
                    .unwrap();
                let mut guard = data.client_data.lock().unwrap();
                Ok(f(&mut guard.data))
            }
            ShellClientKind::ZxdgV6(ref shell) => {
                let data = shell
                    .as_ref()
                    .user_data()
                    .get::<self::zxdgv6_handlers::ShellUserData>()
                    .unwrap();
                let mut guard = data.client_data.lock().unwrap();
                Ok(f(&mut guard.data))
            }
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) enum ToplevelKind {
    Xdg(xdg_toplevel::XdgToplevel),
    ZxdgV6(zxdg_toplevel_v6::ZxdgToplevelV6),
}

/// A handle to a toplevel surface
#[derive(Debug, Clone)]
pub struct ToplevelSurface {
    wl_surface: wl_surface::WlSurface,
    shell_surface: ToplevelKind,
}

impl std::cmp::PartialEq for ToplevelSurface {
    fn eq(&self, other: &Self) -> bool {
        self.alive() && other.alive() && self.wl_surface == other.wl_surface
    }
}

impl ToplevelSurface {
    /// Is the toplevel surface referred by this handle still alive?
    pub fn alive(&self) -> bool {
        let shell_alive = match self.shell_surface {
            ToplevelKind::Xdg(ref s) => s.as_ref().is_alive(),
            ToplevelKind::ZxdgV6(ref s) => s.as_ref().is_alive(),
        };
        shell_alive && self.wl_surface.as_ref().is_alive()
    }

    /// Retrieve the shell client owning this toplevel surface
    ///
    /// Returns `None` if the surface does actually no longer exist.
    pub fn client(&self) -> Option<ShellClient> {
        if !self.alive() {
            return None;
        }

        let shell = match self.shell_surface {
            ToplevelKind::Xdg(ref s) => {
                let data = s
                    .as_ref()
                    .user_data()
                    .get::<self::xdg_handlers::ShellSurfaceUserData>()
                    .unwrap();
                ShellClientKind::Xdg(data.wm_base.clone())
            }
            ToplevelKind::ZxdgV6(ref s) => {
                let data = s
                    .as_ref()
                    .user_data()
                    .get::<self::zxdgv6_handlers::ShellSurfaceUserData>()
                    .unwrap();
                ShellClientKind::ZxdgV6(data.shell.clone())
            }
        };

        Some(ShellClient { kind: shell })
    }

    /// Gets the current pending state for a configure
    ///
    /// Returns `Some` if either no initial configure has been sent or
    /// the `server_pending` is `Some` and different from the last pending
    /// configure or `last_acked` if there is no pending
    ///
    /// Returns `None` if either no `server_pending` or the pending
    /// has already been sent to the client or the pending is equal
    /// to the `last_acked`
    fn get_pending_state(&self, attributes: &mut XdgToplevelSurfaceRoleAttributes) -> Option<ToplevelState> {
        if !attributes.initial_configure_sent {
            return Some(attributes.server_pending.take().unwrap_or_default());
        }

        let server_pending = match attributes.server_pending.take() {
            Some(state) => state,
            None => {
                return None;
            }
        };

        let last_state = attributes
            .pending_configures
            .last()
            .map(|c| &c.state)
            .or_else(|| attributes.last_acked.as_ref());

        if let Some(state) = last_state {
            if state == &server_pending {
                return None;
            }
        }

        Some(server_pending)
    }

    /// Send a configure event to this toplevel surface to suggest it a new configuration
    ///
    /// The serial of this configure will be tracked waiting for the client to ACK it.
    ///
    /// You can manipulate the state that will be sent to the client with the [`with_pending_state`](#method.with_pending_state)
    /// method.
    pub fn send_configure(&self) {
        if let Some(surface) = self.get_surface() {
            let configure = compositor::with_states(surface, |states| {
                let mut attributes = states
                    .data_map
                    .get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
                    .unwrap()
                    .lock()
                    .unwrap();
                if let Some(pending) = self.get_pending_state(&mut *attributes) {
                    let configure = ToplevelConfigure {
                        serial: SERIAL_COUNTER.next_serial(),
                        state: pending,
                    };

                    attributes.pending_configures.push(configure.clone());
                    attributes.initial_configure_sent = true;

                    Some(configure)
                } else {
                    None
                }
            })
            .unwrap_or(None);
            if let Some(configure) = configure {
                match self.shell_surface {
                    ToplevelKind::Xdg(ref s) => self::xdg_handlers::send_toplevel_configure(s, configure),
                    ToplevelKind::ZxdgV6(ref s) => {
                        self::zxdgv6_handlers::send_toplevel_configure(s, configure)
                    }
                }
            }
        }
    }

    /// Handles the role specific commit logic
    ///
    /// This should be called when the underlying WlSurface
    /// handles a wl_surface.commit request.
    pub(crate) fn commit_hook(surface: &wl_surface::WlSurface) {
        compositor::with_states(surface, |states| {
            let mut guard = states
                .data_map
                .get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
                .unwrap()
                .lock()
                .unwrap();
            if let Some(state) = guard.last_acked.clone() {
                guard.current = state;
            }
        })
        .unwrap();
    }

    /// Make sure this surface was configured
    ///
    /// Returns `true` if it was, if not, returns `false` and raise
    /// a protocol error to the associated client. Also returns `false`
    /// if the surface is already destroyed.
    ///
    /// `xdg_shell` mandates that a client acks a configure before committing
    /// anything.
    pub fn ensure_configured(&self) -> bool {
        if !self.alive() {
            return false;
        }
        let configured = compositor::with_states(&self.wl_surface, |states| {
            states
                .data_map
                .get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
                .unwrap()
                .lock()
                .unwrap()
                .configured
        })
        .unwrap();
        if !configured {
            match self.shell_surface {
                ToplevelKind::Xdg(ref s) => {
                    let data = s
                        .as_ref()
                        .user_data()
                        .get::<self::xdg_handlers::ShellSurfaceUserData>()
                        .unwrap();
                    data.xdg_surface.as_ref().post_error(
                        xdg_surface::Error::NotConstructed as u32,
                        "Surface has not been configured yet.".into(),
                    );
                }
                ToplevelKind::ZxdgV6(ref s) => {
                    let data = s
                        .as_ref()
                        .user_data()
                        .get::<self::zxdgv6_handlers::ShellSurfaceUserData>()
                        .unwrap();
                    data.xdg_surface.as_ref().post_error(
                        zxdg_surface_v6::Error::NotConstructed as u32,
                        "Surface has not been configured yet.".into(),
                    );
                }
            }
        }
        configured
    }

    /// Send a "close" event to the client
    pub fn send_close(&self) {
        match self.shell_surface {
            ToplevelKind::Xdg(ref s) => s.close(),
            ToplevelKind::ZxdgV6(ref s) => s.close(),
        }
    }

    /// Access the underlying `wl_surface` of this toplevel surface
    ///
    /// Returns `None` if the toplevel surface actually no longer exists.
    pub fn get_surface(&self) -> Option<&wl_surface::WlSurface> {
        if self.alive() {
            Some(&self.wl_surface)
        } else {
            None
        }
    }

    /// Allows the pending state of this toplevel to
    /// be manipulated.
    ///
    /// This should be used to inform the client about size and state changes,
    /// for example after a resize request from the client.
    ///
    /// The state will be sent to the client when calling [`send_configure`](#method.send_configure).
    pub fn with_pending_state<F, T>(&self, f: F) -> Result<T, DeadResource>
    where
        F: FnOnce(&mut ToplevelState) -> T,
    {
        if !self.alive() {
            return Err(DeadResource);
        }

        Ok(compositor::with_states(&self.wl_surface, |states| {
            let mut attributes = states
                .data_map
                .get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
                .unwrap()
                .lock()
                .unwrap();
            if attributes.server_pending.is_none() {
                attributes.server_pending = Some(attributes.current.clone());
            }

            let server_pending = attributes.server_pending.as_mut().unwrap();
            f(server_pending)
        })
        .unwrap())
    }

    /// Gets a copy of the current state of this toplevel
    ///
    /// Returns `None` if the underlying surface has been
    /// destroyed
    pub fn current_state(&self) -> Option<ToplevelState> {
        if !self.alive() {
            return None;
        }

        Some(
            compositor::with_states(&self.wl_surface, |states| {
                let attributes = states
                    .data_map
                    .get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
                    .unwrap()
                    .lock()
                    .unwrap();

                attributes.current.clone()
            })
            .unwrap(),
        )
    }
}

#[derive(Debug, Clone)]
pub(crate) enum PopupKind {
    Xdg(xdg_popup::XdgPopup),
    ZxdgV6(zxdg_popup_v6::ZxdgPopupV6),
}

/// A handle to a popup surface
///
/// This is an unified abstraction over the popup surfaces
/// of both `wl_shell` and `xdg_shell`.
#[derive(Debug, Clone)]
pub struct PopupSurface {
    wl_surface: wl_surface::WlSurface,
    shell_surface: PopupKind,
}

impl std::cmp::PartialEq for PopupSurface {
    fn eq(&self, other: &Self) -> bool {
        self.alive() && other.alive() && self.wl_surface == other.wl_surface
    }
}

impl PopupSurface {
    /// Is the popup surface referred by this handle still alive?
    pub fn alive(&self) -> bool {
        let shell_alive = match self.shell_surface {
            PopupKind::Xdg(ref p) => p.as_ref().is_alive(),
            PopupKind::ZxdgV6(ref p) => p.as_ref().is_alive(),
        };
        shell_alive && self.wl_surface.as_ref().is_alive()
    }

    /// Gets a reference of the parent WlSurface of
    /// this popup.
    pub fn get_parent_surface(&self) -> Option<wl_surface::WlSurface> {
        if !self.alive() {
            None
        } else {
            compositor::with_states(&self.wl_surface, |states| {
                states
                    .data_map
                    .get::<Mutex<XdgPopupSurfaceRoleAttributes>>()
                    .unwrap()
                    .lock()
                    .unwrap()
                    .parent
                    .clone()
            })
            .unwrap()
        }
    }

    /// Retrieve the shell client owning this popup surface
    ///
    /// Returns `None` if the surface does actually no longer exist.
    pub fn client(&self) -> Option<ShellClient> {
        if !self.alive() {
            return None;
        }

        let shell = match self.shell_surface {
            PopupKind::Xdg(ref p) => {
                let data = p
                    .as_ref()
                    .user_data()
                    .get::<self::xdg_handlers::ShellSurfaceUserData>()
                    .unwrap();
                ShellClientKind::Xdg(data.wm_base.clone())
            }
            PopupKind::ZxdgV6(ref p) => {
                let data = p
                    .as_ref()
                    .user_data()
                    .get::<self::zxdgv6_handlers::ShellSurfaceUserData>()
                    .unwrap();
                ShellClientKind::ZxdgV6(data.shell.clone())
            }
        };

        Some(ShellClient { kind: shell })
    }

    /// Send a configure event to this popup surface to suggest it a new configuration
    ///
    /// The serial of this configure will be tracked waiting for the client to ACK it.
    ///
    /// You can manipulate the state that will be sent to the client with the [`with_pending_state`](#method.with_pending_state)
    /// method.
    pub fn send_configure(&self) {
        if let Some(surface) = self.get_surface() {
            let next_configure = compositor::with_states(surface, |states| {
                let mut attributes = states
                    .data_map
                    .get::<Mutex<XdgPopupSurfaceRoleAttributes>>()
                    .unwrap()
                    .lock()
                    .unwrap();
                if !attributes.initial_configure_sent || attributes.server_pending.is_some() {
                    let pending = attributes.server_pending.take().unwrap_or(attributes.current);

                    let configure = PopupConfigure {
                        state: pending,
                        serial: SERIAL_COUNTER.next_serial(),
                    };

                    attributes.pending_configures.push(configure);
                    attributes.initial_configure_sent = true;

                    Some(configure)
                } else {
                    None
                }
            })
            .unwrap_or(None);
            if let Some(configure) = next_configure {
                match self.shell_surface {
                    PopupKind::Xdg(ref p) => {
                        self::xdg_handlers::send_popup_configure(p, configure);
                    }
                    PopupKind::ZxdgV6(ref p) => {
                        self::zxdgv6_handlers::send_popup_configure(p, configure);
                    }
                }
            }
        }
    }

    /// Handles the role specific commit logic
    ///
    /// This should be called when the underlying WlSurface
    /// handles a wl_surface.commit request.
    pub(crate) fn commit_hook(surface: &wl_surface::WlSurface) {
        let send_error_to = compositor::with_states(surface, |states| {
            let attributes = states
                .data_map
                .get::<Mutex<XdgPopupSurfaceRoleAttributes>>()
                .unwrap()
                .lock()
                .unwrap();
            if attributes.parent.is_none() {
                attributes.popup_handle.clone()
            } else {
                None
            }
        })
        .unwrap_or(None);
        if let Some(handle) = send_error_to {
            match handle {
                PopupKind::Xdg(ref s) => {
                    let data = s
                        .as_ref()
                        .user_data()
                        .get::<self::xdg_handlers::ShellSurfaceUserData>()
                        .unwrap();
                    data.xdg_surface.as_ref().post_error(
                        xdg_surface::Error::NotConstructed as u32,
                        "Surface has not been configured yet.".into(),
                    );
                }
                PopupKind::ZxdgV6(ref s) => {
                    let data = s
                        .as_ref()
                        .user_data()
                        .get::<self::zxdgv6_handlers::ShellSurfaceUserData>()
                        .unwrap();
                    data.xdg_surface.as_ref().post_error(
                        zxdg_surface_v6::Error::NotConstructed as u32,
                        "Surface has not been configured yet.".into(),
                    );
                }
            }
            return;
        }

        compositor::with_states(surface, |states| {
            let mut attributes = states
                .data_map
                .get::<Mutex<XdgPopupSurfaceRoleAttributes>>()
                .unwrap()
                .lock()
                .unwrap();
            if attributes.initial_configure_sent {
                if let Some(state) = attributes.last_acked {
                    if state != attributes.current {
                        attributes.current = state;
                    }
                }
            }
            !attributes.initial_configure_sent
        })
        .unwrap();
    }

    /// Make sure this surface was configured
    ///
    /// Returns `true` if it was, if not, returns `false` and raise
    /// a protocol error to the associated client. Also returns `false`
    /// if the surface is already destroyed.
    ///
    /// xdg_shell mandates that a client acks a configure before committing
    /// anything.
    pub fn ensure_configured(&self) -> bool {
        if !self.alive() {
            return false;
        }
        let configured = compositor::with_states(&self.wl_surface, |states| {
            states
                .data_map
                .get::<Mutex<XdgPopupSurfaceRoleAttributes>>()
                .unwrap()
                .lock()
                .unwrap()
                .configured
        })
        .unwrap();
        if !configured {
            match self.shell_surface {
                PopupKind::Xdg(ref s) => {
                    let data = s
                        .as_ref()
                        .user_data()
                        .get::<self::xdg_handlers::ShellSurfaceUserData>()
                        .unwrap();
                    data.xdg_surface.as_ref().post_error(
                        xdg_surface::Error::NotConstructed as u32,
                        "Surface has not been configured yet.".into(),
                    );
                }
                PopupKind::ZxdgV6(ref s) => {
                    let data = s
                        .as_ref()
                        .user_data()
                        .get::<self::zxdgv6_handlers::ShellSurfaceUserData>()
                        .unwrap();
                    data.xdg_surface.as_ref().post_error(
                        zxdg_surface_v6::Error::NotConstructed as u32,
                        "Surface has not been configured yet.".into(),
                    );
                }
            }
        }
        configured
    }

    /// Send a `popup_done` event to the popup surface
    ///
    /// It means that the use has dismissed the popup surface, or that
    /// the pointer has left the area of popup grab if there was a grab.
    pub fn send_popup_done(&self) {
        match self.shell_surface {
            PopupKind::Xdg(ref p) => p.popup_done(),
            PopupKind::ZxdgV6(ref p) => p.popup_done(),
        }
    }

    /// Access the underlying `wl_surface` of this toplevel surface
    ///
    /// Returns `None` if the popup surface actually no longer exists.
    pub fn get_surface(&self) -> Option<&wl_surface::WlSurface> {
        if self.alive() {
            Some(&self.wl_surface)
        } else {
            None
        }
    }

    /// Allows the pending state of this popup to
    /// be manipulated.
    ///
    /// This should be used to inform the client about size and position changes,
    /// for example after a move of the parent toplevel.
    ///
    /// The state will be sent to the client when calling [`send_configure`](#method.send_configure).
    pub fn with_pending_state<F, T>(&self, f: F) -> Result<T, DeadResource>
    where
        F: FnOnce(&mut PopupState) -> T,
    {
        if !self.alive() {
            return Err(DeadResource);
        }

        Ok(compositor::with_states(&self.wl_surface, |states| {
            let mut attributes = states
                .data_map
                .get::<Mutex<XdgPopupSurfaceRoleAttributes>>()
                .unwrap()
                .lock()
                .unwrap();
            if attributes.server_pending.is_none() {
                attributes.server_pending = Some(attributes.current);
            }

            let server_pending = attributes.server_pending.as_mut().unwrap();
            f(server_pending)
        })
        .unwrap())
    }
}

/// A configure message for toplevel surfaces
#[derive(Debug, Clone)]
pub struct ToplevelConfigure {
    /// The state associated with this configure
    pub state: ToplevelState,

    /// A serial number to track ACK from the client
    ///
    /// This should be an ever increasing number, as the ACK-ing
    /// from a client for a serial will validate all pending lower
    /// serials.
    pub serial: Serial,
}

/// A configure message for popup surface
#[derive(Debug, Clone, Copy)]
pub struct PopupConfigure {
    /// The state associated with this configure,
    pub state: PopupState,
    /// A serial number to track ACK from the client
    ///
    /// This should be an ever increasing number, as the ACK-ing
    /// from a client for a serial will validate all pending lower
    /// serials.
    pub serial: Serial,
}

/// Defines the possible configure variants
/// for a XdgSurface that will be issued in
/// the user_impl for notifying about a ack_configure
#[derive(Debug)]
pub enum Configure {
    /// A xdg_surface with a role of xdg_toplevel
    /// has processed an ack_configure request
    Toplevel(ToplevelConfigure),
    /// A xdg_surface with a role of xdg_popup
    /// has processed an ack_configure request
    Popup(PopupConfigure),
}

impl From<ToplevelConfigure> for Configure {
    fn from(configure: ToplevelConfigure) -> Self {
        Configure::Toplevel(configure)
    }
}

impl From<PopupConfigure> for Configure {
    fn from(configure: PopupConfigure) -> Self {
        Configure::Popup(configure)
    }
}

/// Events generated by xdg shell surfaces
///
/// These are events that the provided implementation cannot process
/// for you directly.
///
/// Depending on what you want to do, you might ignore some of them
#[derive(Debug)]
pub enum XdgRequest {
    /// A new shell client was instantiated
    NewClient {
        /// the client
        client: ShellClient,
    },
    /// The pong for a pending ping of this shell client was received
    ///
    /// The `ShellHandler` already checked for you that the serial matches the one
    /// from the pending ping.
    ClientPong {
        /// the client
        client: ShellClient,
    },
    /// A new toplevel surface was created
    ///
    /// You likely need to send a [`ToplevelConfigure`] to the surface, to hint the
    /// client as to how its window should be sized.
    NewToplevel {
        /// the surface
        surface: ToplevelSurface,
    },
    /// A new popup surface was created
    ///
    /// You likely need to send a [`PopupConfigure`] to the surface, to hint the
    /// client as to how its popup should be sized.
    NewPopup {
        /// the surface
        surface: PopupSurface,
    },
    /// The client requested the start of an interactive move for this surface
    Move {
        /// the surface
        surface: ToplevelSurface,
        /// the seat associated to this move
        seat: wl_seat::WlSeat,
        /// the grab serial
        serial: Serial,
    },
    /// The client requested the start of an interactive resize for this surface
    Resize {
        /// The surface
        surface: ToplevelSurface,
        /// The seat associated with this resize
        seat: wl_seat::WlSeat,
        /// The grab serial
        serial: Serial,
        /// Specification of which part of the window's border is being dragged
        edges: xdg_toplevel::ResizeEdge,
    },
    /// This popup requests a grab of the pointer
    ///
    /// This means it requests to be sent a `popup_done` event when the pointer leaves
    /// the grab area.
    Grab {
        /// The surface
        surface: PopupSurface,
        /// The seat to grab
        seat: wl_seat::WlSeat,
        /// The grab serial
        serial: Serial,
    },
    /// A toplevel surface requested to be maximized
    Maximize {
        /// The surface
        surface: ToplevelSurface,
    },
    /// A toplevel surface requested to stop being maximized
    UnMaximize {
        /// The surface
        surface: ToplevelSurface,
    },
    /// A toplevel surface requested to be set fullscreen
    Fullscreen {
        /// The surface
        surface: ToplevelSurface,
        /// The output (if any) on which the fullscreen is requested
        output: Option<wl_output::WlOutput>,
    },
    /// A toplevel surface request to stop being fullscreen
    UnFullscreen {
        /// The surface
        surface: ToplevelSurface,
    },
    /// A toplevel surface requested to be minimized
    Minimize {
        /// The surface
        surface: ToplevelSurface,
    },
    /// The client requests the window menu to be displayed on this surface at this location
    ///
    /// This menu belongs to the compositor. It is typically expected to contain options for
    /// control of the window (maximize/minimize/close/move/etc...).
    ShowWindowMenu {
        /// The surface
        surface: ToplevelSurface,
        /// The seat associated with this input grab
        seat: wl_seat::WlSeat,
        /// the grab serial
        serial: Serial,
        /// location of the menu request relative to the surface geometry
        location: Point<i32, Logical>,
    },
    /// A surface has acknowledged a configure serial.
    AckConfigure {
        /// The surface.
        surface: wl_surface::WlSurface,
        /// The configure serial.
        configure: Configure,
    },
}