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
// MIT/Apache2 License

#![allow(clippy::similar_names)]

pub use crate::{
    auto::{
        xproto::{
            Atom, BackingStore, ChangePropertyRequest, ChangeSaveSetRequest,
            ChangeWindowAttributesRequest, Circulate, CirculateWindowRequest, ClearAreaRequest,
            Colormap, ConfigWindow, ConfigureWindowRequest, ConvertSelectionRequest, Cursor,
            DeletePropertyRequest, DestroySubwindowsRequest, DestroyWindowRequest, EventMask,
            Gcontext, GetGeometryRequest, GetWindowAttributesReply, GetWindowAttributesRequest,
            Gravity, MapState, MapWindowRequest, PropMode, SetMode, StackMode, Timestamp, Visualid,
            Window, WindowClass, ATOM_WM_NAME,
        },
        AsByteSequence,
    },
    display::{Connection, Display, RequestCookie, WindowParameters},
    drawable::{self, Geometry as DrawableGeomtry},
    xid::XidType,
};
use alloc::{string::ToString, vec::Vec};
use core::{iter, mem};

// macro for retrieving an atom that might be cached in the display
macro_rules! retrieve_atom {
    ($dpy: expr, $dfield: ident, $name: expr) => {{
        match $dpy.$dfield {
            Some(wpa) => Atom::const_from_xid(wpa.get()),
            None => {
                let wpa = $dpy.intern_atom_immediate(($name).to_string(), false)?;
                if wpa.xid() == 0 {
                    log::error!("Unable to intern {} atom", $name);
                    return Ok(());
                } else {
                    $dpy.$dfield = core::num::NonZeroU32::new(wpa.xid());
                    wpa
                }
            }
        }
    }};
}

#[cfg(feature = "async")]
macro_rules! retrieve_atom_async {
    ($dpy: expr, $dfield: ident, $name: expr) => {{
        match $dpy.$dfield {
            Some(wpa) => Atom::const_from_xid(wpa.get()),
            None => {
                let wpa = $dpy
                    .intern_atom_immediate_async(($name).to_string(), false)
                    .await?;
                if wpa.xid() == 0 {
                    log::error!("Unable to intern {} atom", $name);
                    return Ok(());
                } else {
                    $dpy.$dfield = core::num::NonZeroU32::new(wpa.xid());
                    wpa
                }
            }
        }
    }};
}

crate::create_paramaterizer! {
    pub struct ConfigureWindowParameters : (ConfigWindow, ConfigureWindowRequest) {
        x            (set_x,            x)            : i32,
        y            (set_y,            y)            : i32,
        width        (set_width,        width)        : u32,
        height       (set_height,       height)       : u32,
        border_width (set_border_width, border_width) : u32,
        sibling      (set_sibling,      sibling)       : Window,
        stack_mode   (set_stack_mode,   stack_mode)   : StackMode
    }
}

/// The return type of `Window::window_attributes_immediate`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct WindowAttributes {
    pub backing_store: BackingStore,
    pub visual: Visualid,
    pub class: WindowClass,
    pub bit_gravity: Gravity,
    pub win_gravity: Gravity,
    pub backing_planes: u32,
    pub backing_pixel: u32,
    pub save_under: bool,
    pub map_is_installed: bool,
    pub map_state: MapState,
    pub override_redirect: bool,
    pub colormap: Colormap,
    pub all_event_masks: EventMask,
    pub your_event_mask: EventMask,
    pub do_not_propagate_mask: EventMask,
}

impl From<GetWindowAttributesReply> for WindowAttributes {
    #[inline]
    fn from(gwar: GetWindowAttributesReply) -> Self {
        convert_get_window_attributes_reply(gwar)
    }
}

/// The return type of `Window::geometry_immediate`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct WindowGeometry {
    pub depth: u8,
    pub root: Window,
    pub x: i16,
    pub y: i16,
    pub width: u16,
    pub height: u16,
    pub border_width: u16,
}

impl Window {
    /// Map this window to the screen.
    #[inline]
    pub fn map<Conn: Connection>(self, dpy: &mut Display<Conn>) -> crate::Result {
        let mw = MapWindowRequest {
            window: self,
            ..Default::default()
        };

        log::debug!("Sending MapWindowRequest to server");
        let tok = dpy.send_request(mw)?;
        log::debug!("Sent MapWindowRequest to server");
        dpy.resolve_request(tok)
    }

    /// Map this window to the screen, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn map_async<Conn: Connection>(self, dpy: &mut Display<Conn>) -> crate::Result {
        let mw = MapWindowRequest {
            window: self,
            ..Default::default()
        };
        let tok = dpy.send_request_async(mw).await?;
        dpy.resolve_request_async(tok).await
    }

    /// Request struct to change the property of a window.
    #[inline]
    fn change_property_request<T: AsByteSequence>(
        self,
        property: Atom,
        property_type: PropertyType,
        format: PropertyFormat,
        mode: PropMode,
        data: &[T],
    ) -> ChangePropertyRequest {
        // convert to a u8 collection
        let mut data_bytes: Vec<u8> = iter::repeat(0)
            .take(mem::size_of::<T>() * data.len())
            .collect();
        let len = data.iter().fold(0, |index, item| {
            item.as_bytes(&mut data_bytes[index..]) + index
        });
        data_bytes.truncate(len);

        let format = format as u8;

        ChangePropertyRequest {
            mode,
            window: self,
            property,
            ty: Atom::const_from_xid(property_type as u32),
            format,
            data_len: data.len() as u32,
            data: data_bytes,
            ..Default::default()
        }
    }

    /// Change a property of the window, given an atom that identifies that property.
    #[inline]
    pub fn change_property<Conn: Connection, T: AsByteSequence>(
        self,
        dpy: &mut Display<Conn>,
        property: Atom,
        property_type: PropertyType,
        format: PropertyFormat,
        mode: PropMode,
        data: &[T],
    ) -> crate::Result<()> {
        log::debug!("Sending ChangePropertyRequest to server");
        let tok = dpy.send_request(self.change_property_request(
            property,
            property_type,
            format,
            mode,
            data,
        ))?;
        log::debug!("Sent ChangePropertyRequest to server");
        dpy.resolve_request(tok)
    }

    /// Change a property of the window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn change_property_async<Conn: Connection, T: AsByteSequence>(
        self,
        dpy: &mut Display<Conn>,
        property: Atom,
        property_type: PropertyType,
        format: PropertyFormat,
        mode: PropMode,
        data: &[T],
    ) -> crate::Result<()> {
        log::debug!("Sending ChangePropertyRequest to server");
        let tok = dpy
            .send_request_async(self.change_property_request(
                property,
                property_type,
                format,
                mode,
                data,
            ))
            .await?;
        log::debug!("Sent ChangePropertyRequest to server");
        dpy.resolve_request_async(tok).await
    }

    /// Delete a property of this window.
    #[inline]
    pub fn delete_property<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        property: Atom,
    ) -> crate::Result {
        let dpr = DeletePropertyRequest {
            window: self,
            property,
            ..Default::default()
        };
        log::debug!("Sending DeletePropertyRequest to server.");
        let tok = dpy.send_request(dpr)?;
        log::debug!("Sent DeletePropertyRequest to server.");
        dpy.resolve_request(tok)
    }

    /// Delete a property of this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn delete_property_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        property: Atom,
    ) -> crate::Result {
        let dpr = DeletePropertyRequest {
            window: self,
            property,
            ..Default::default()
        };
        log::debug!("Sending DeletePropertyRequest to server.");
        let tok = dpy.send_request_async(dpr).await?;
        log::debug!("Sent DeletePropertyRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Set the protocols for the WM in regards to this window.
    #[inline]
    pub fn set_wm_protocols<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        protocols: &[Atom],
    ) -> crate::Result<()> {
        let wm_protocols_atom = retrieve_atom!(dpy, wm_protocols_atom, "WM_PROTOCOLS");

        self.change_property(
            dpy,
            wm_protocols_atom,
            PropertyType::Atom,
            PropertyFormat::ThirtyTwo,
            PropMode::Replace,
            protocols,
        )
    }

    /// Set the WM protocols for this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn set_wm_protocols_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        protocols: &[Atom],
    ) -> crate::Result<()> {
        let wm_protocols_atom = retrieve_atom_async!(dpy, wm_protocols_atom, "WM_PROTOCOLS");

        self.change_property_async(
            dpy,
            wm_protocols_atom,
            PropertyType::Atom,
            PropertyFormat::ThirtyTwo,
            PropMode::Replace,
            protocols,
        )
        .await
    }

    /// Set the title for this window.
    #[inline]
    pub fn set_title<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        title: &str,
    ) -> crate::Result<()> {
        self.change_property(
            dpy,
            ATOM_WM_NAME,
            PropertyType::String,
            PropertyFormat::Eight,
            PropMode::Replace,
            title.as_bytes(),
        )
    }

    /// Set the title for this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn set_title_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        title: &str,
    ) -> crate::Result<()> {
        self.change_property_async(
            dpy,
            ATOM_WM_NAME,
            PropertyType::String,
            PropertyFormat::Eight,
            PropMode::Replace,
            title.as_bytes(),
        )
        .await
    }

    /// `GetWindowAttributesRequest`
    #[inline]
    fn get_window_attributes_request(self) -> GetWindowAttributesRequest {
        GetWindowAttributesRequest {
            window: self,
            ..Default::default()
        }
    }

    /// Get the current set of window attributes for this window.
    #[inline]
    pub fn window_attributes<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<RequestCookie<GetWindowAttributesRequest>> {
        let gwar = self.get_window_attributes_request();
        log::debug!("Sending GetWindowAttributesRequest to server.");
        let tok = dpy.send_request(gwar)?;
        log::debug!("Sent GetWindowAttributesRequest to server.");
        Ok(tok)
    }

    /// Get the current set of window attributes for this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn window_attributes_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<RequestCookie<GetWindowAttributesRequest>> {
        let gwar = self.get_window_attributes_request();
        log::debug!("Sending GetWindowAttributesRequest to server.");
        let tok = dpy.send_request_async(gwar).await?;
        log::debug!("Sent GetWindowAttributesRequest to server.");
        Ok(tok)
    }

    /// Immediately get the current set of window attributes for this window.
    #[inline]
    pub fn window_attributes_immediate<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<WindowAttributes> {
        let tok = self.window_attributes(dpy)?;
        Ok(convert_get_window_attributes_reply(
            dpy.resolve_request(tok)?,
        ))
    }

    /// Immediately get the current set of window attributes for this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn window_attributes_immediate_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<WindowAttributes> {
        let tok = self.window_attributes_async(dpy).await?;
        Ok(convert_get_window_attributes_reply(
            dpy.resolve_request_async(tok).await?,
        ))
    }

    /// Get the geometry of this window.
    #[inline]
    pub fn geometry<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<RequestCookie<GetGeometryRequest>> {
        drawable::get_geometry(dpy, self)
    }

    /// Get the geometry of this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn geometry_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<RequestCookie<GetGeometryRequest>> {
        drawable::get_geometry_async(dpy, self).await
    }

    /// Immediately get the geometry of this window.
    #[inline]
    pub fn geometry_immediate<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<DrawableGeomtry> {
        drawable::get_geometry_immediate(dpy, self)
    }

    /// Immediately get the geometry of this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn geometry_immediate_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result<DrawableGeomtry> {
        drawable::get_geometry_immediate_async(dpy, self).await
    }

    /// Request to change this window's parameters.
    #[inline]
    fn change_window_attrs_request(self, props: WindowParameters) -> ChangeWindowAttributesRequest {
        let mut cwar = ChangeWindowAttributesRequest {
            window: self,
            ..Default::default()
        };

        let c = props.mask_to_change_attrs_request(&mut cwar);
        cwar.value_mask = c;
        cwar
    }

    /// Change the properties of this window.
    #[inline]
    pub fn change_attributes<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        props: WindowParameters,
    ) -> crate::Result {
        let cwar = self.change_window_attrs_request(props);
        log::debug!("Sending ChangeWindowAttributesRequest to server.");
        let tok = dpy.send_request(cwar)?;
        log::debug!("Sent ChangeWindowAttributesRequest to server.");
        dpy.resolve_request(tok)
    }

    /// Change the properties of this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn change_attributes_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        props: WindowParameters,
    ) -> crate::Result {
        let cwar = self.change_window_attrs_request(props);
        log::debug!("Sending ChangeWindowAttributesRequest to server.");
        let tok = dpy.send_request_async(cwar).await?;
        log::debug!("Sent ChangeWindowAttributesRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Set this window's background color.
    #[inline]
    pub fn set_background_color<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        clr: u32,
    ) -> crate::Result<()> {
        let props = WindowParameters {
            background_pixel: Some(clr),
            ..Default::default()
        };
        self.change_attributes(dpy, props)
    }

    /// Set this window's background color, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn set_background_color_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        clr: u32,
    ) -> crate::Result<()> {
        let mut props: WindowParameters = Default::default();
        props.background_pixel = Some(clr);
        self.change_attributes_async(dpy, props).await
    }

    /// Request to configure window.
    #[inline]
    fn configure_window_request(self, props: ConfigureWindowParameters) -> ConfigureWindowRequest {
        let mut cwr = ConfigureWindowRequest {
            window: self,
            ..Default::default()
        };
        let c = props.convert_to_flags(&mut cwr);
        cwr.value_mask = c;
        cwr
    }

    /// Configure the window's physical properties.
    #[inline]
    pub fn configure<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        props: ConfigureWindowParameters,
    ) -> crate::Result {
        let cwr = self.configure_window_request(props);
        log::debug!("Sending ConfigureWindowRequest to server.");
        let tok = dpy.send_request(cwr)?;
        log::debug!("Sent ConfigureWindowRequest to server.");
        dpy.resolve_request(tok)
    }

    /// Configure the window's physical properties, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn configure_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        props: ConfigureWindowParameters,
    ) -> crate::Result {
        let cwr = self.configure_window_request(props);
        log::debug!("Sending ConfigureWindowRequest to server.");
        let tok = dpy.send_request_async(cwr).await?;
        log::debug!("Sent ConfigureWindowRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Set the border of this window.
    #[inline]
    pub fn set_border_width<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        width: u32,
    ) -> crate::Result {
        let props = ConfigureWindowParameters {
            border_width: Some(width),
            ..Default::default()
        };
        self.configure(dpy, props)
    }

    /// Set the border of this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn set_border_width_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        width: u32,
    ) -> crate::Result {
        let props = ConfigureWindowParameters {
            border_width: Some(width),
            ..Default::default()
        };
        self.configure_async(dpy, props).await
    }

    /// Change the colormap associated with this window.
    #[inline]
    pub fn set_colormap<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        colormap: Colormap,
    ) -> crate::Result {
        let props = WindowParameters {
            colormap: Some(colormap),
            ..Default::default()
        };
        self.change_attributes(dpy, props)
    }

    /// Change the colormap associated with this window.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn set_colormap_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        colormap: Colormap,
    ) -> crate::Result {
        let mut props: WindowParameters = Default::default();
        props.colormap = Some(colormap);
        self.change_attributes_async(dpy, props).await
    }

    #[inline]
    fn change_save_set_request(self, mode: SetMode) -> ChangeSaveSetRequest {
        ChangeSaveSetRequest {
            mode,
            window: self,
            ..Default::default()
        }
    }

    #[inline]
    pub fn change_save_set<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        mode: SetMode,
    ) -> crate::Result {
        let cssr = self.change_save_set_request(mode);
        log::debug!("Sending ChangeSaveSetRequest to server.");
        let tok = dpy.send_request(cssr)?;
        log::debug!("Sent ChangeSaveSetRequest to server.");
        dpy.resolve_request(tok)
    }

    #[cfg(feature = "async")]
    #[inline]
    pub async fn change_save_set_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        mode: SetMode,
    ) -> crate::Result {
        let cssr = self.change_save_set_request(mode);
        log::debug!("Sending ChangeSaveSetRequest to server.");
        let tok = dpy.send_request_async(cssr).await?;
        log::debug!("Sent ChangeSaveSetRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Resize the window.
    #[inline]
    pub fn resize<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        width: u32,
        height: u32,
    ) -> crate::Result {
        let props = ConfigureWindowParameters {
            width: Some(width),
            height: Some(height),
            ..Default::default()
        };
        self.configure(dpy, props)
    }

    /// Resize the window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn resize_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        width: u32,
        height: u32,
    ) -> crate::Result {
        let mut props: ConfigureWindowParameters = Default::default();
        props.width = Some(width);
        props.height = Some(height);
        self.configure_async(dpy, props).await
    }

    /// Move and resize the window.
    #[inline]
    pub fn move_resize<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> crate::Result {
        let props = ConfigureWindowParameters {
            x: Some(x),
            y: Some(y),
            width: Some(width),
            height: Some(height),
            ..Default::default()
        };
        self.configure(dpy, props)
    }

    /// Move and resize the window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn move_resize_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> crate::Result {
        let props = ConfigureWindowParameters {
            x: Some(x),
            y: Some(y),
            width: Some(width),
            height: Some(height),
            ..Default::default()
        };
        self.configure_async(dpy, props).await
    }

    #[inline]
    fn circulate_window_request(self, direction: Circulate) -> CirculateWindowRequest {
        CirculateWindowRequest {
            window: self,
            direction,
            ..Default::default()
        }
    }

    #[inline]
    pub fn circulate<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        direction: Circulate,
    ) -> crate::Result {
        let cwr = self.circulate_window_request(direction);
        log::debug!("Sending CirculateWindowRequest to server.");
        let tok = dpy.send_request(cwr)?;
        log::debug!("Sent CirculateWindowRequest to server.");
        dpy.resolve_request(tok)
    }

    #[cfg(feature = "async")]
    #[inline]
    pub async fn circulate_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        direction: Circulate,
    ) -> crate::Result {
        let cwr = self.circulate_window_request(direction);
        log::debug!("Sending CirculateWindowRequest to server.");
        let tok = dpy.send_request_async(cwr).await?;
        log::debug!("Sent CirculateWindowRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Clear Window Request
    #[inline]
    fn clear_area_request(
        self,
        x: i16,
        y: i16,
        width: u16,
        height: u16,
        exposures: bool,
    ) -> ClearAreaRequest {
        ClearAreaRequest {
            exposures,
            x,
            y,
            width,
            height,
            window: self,
            ..Default::default()
        }
    }

    /// Clear an area of the window.
    #[inline]
    pub fn clear_area<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        x: i16,
        y: i16,
        width: u16,
        height: u16,
        exposures: bool,
    ) -> crate::Result {
        let car = self.clear_area_request(x, y, width, height, exposures);
        log::debug!("Sending ClearAreaRequest to server.");
        let tok = dpy.send_request(car)?;
        log::debug!("Sent ClearAreaRequest to server.");
        dpy.resolve_request(tok)
    }

    /// Clear an area of the window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn clear_area_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        x: i16,
        y: i16,
        width: u16,
        height: u16,
        exposures: bool,
    ) -> crate::Result {
        let car = self.clear_area_request(x, y, width, height, exposures);
        log::debug!("Sending ClearAreaRequest to server.");
        let tok = dpy.send_request_async(car).await?;
        log::debug!("Sent ClearAreaRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Clear the entire window.
    #[inline]
    pub fn clear<Conn: Connection>(self, dpy: &mut Display<Conn>) -> crate::Result {
        self.clear_area(dpy, 0, 0, 0, 0, false) // means: clear the whole dang thing
    }

    /// Clear the entire window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn clear_async<Conn: Connection>(self, dpy: &mut Display<Conn>) -> crate::Result {
        self.clear_area_async(dpy, 0, 0, 0, 0, false).await
    }

    #[inline]
    fn convert_selection_request(
        self,
        selection: Atom,
        target: Atom,
        property: Atom,
        time: Timestamp,
    ) -> ConvertSelectionRequest {
        ConvertSelectionRequest {
            requestor: self,
            selection,
            target,
            property,
            time,
            ..Default::default()
        }
    }

    #[inline]
    pub fn convert_selection<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        selection: Atom,
        target: Atom,
        property: Atom,
        time: Timestamp,
    ) -> crate::Result {
        let csr = self.convert_selection_request(selection, target, property, time);
        log::debug!("Sending ConvertSelectionRequest to server.");
        let tok = dpy.send_request(csr)?;
        log::debug!("Sent ConvertSelectionRequest to server.");
        dpy.resolve_request(tok)
    }

    #[cfg(feature = "async")]
    #[inline]
    pub async fn convert_selection_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        selection: Atom,
        target: Atom,
        property: Atom,
        time: Timestamp,
    ) -> crate::Result {
        let csr = self.convert_selection_request(selection, target, property, time);
        log::debug!("Sending ConvertSelectionRequest to server.");
        let tok = dpy.send_request_async(csr).await?;
        log::debug!("Sent ConvertSelectionRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Set the cursor used by this window.
    #[inline]
    pub fn set_cursor<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        cursor: Cursor,
    ) -> crate::Result {
        let props = WindowParameters {
            cursor: Some(cursor),
            ..Default::default()
        };
        self.change_attributes(dpy, props)
    }

    /// Set the cursor used by this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn set_cursor_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
        cursor: Cursor,
    ) -> crate::Result {
        let props = WindowParameters {
            cursor: Some(cursor),
            ..Default::default()
        };
        self.change_attributes_async(dpy, props).await
    }

    /// Destroy this window's subwindows.
    #[inline]
    pub fn destroy_subwindows<Conn: Connection>(self, dpy: &mut Display<Conn>) -> crate::Result {
        let dsr = DestroySubwindowsRequest {
            window: self,
            ..Default::default()
        };
        log::debug!("Sending DestroySubwindowsRequest to server.");
        let tok = dpy.send_request(dsr)?;
        log::debug!("Sent DestroySubwindowsRequest to server.");
        dpy.resolve_request(tok)
    }

    /// Destroy this window's subwindows, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn destroy_subwindows_async<Conn: Connection>(
        self,
        dpy: &mut Display<Conn>,
    ) -> crate::Result {
        let dsr = DestroySubwindowsRequest {
            window: self,
            ..Default::default()
        };
        log::debug!("Sending DestroySubwindowsRequest to server.");
        let tok = dpy.send_request_async(dsr).await?;
        log::debug!("Sent DestroySubwindowsRequest to server.");
        dpy.resolve_request_async(tok).await
    }

    /// Free this window.
    #[inline]
    pub fn free<Conn: Connection>(self, dpy: &mut Display<Conn>) -> crate::Result {
        let dwr = DestroyWindowRequest {
            window: self,
            ..Default::default()
        };
        log::debug!("Sending DestroyWindowRequest to server.");
        let tok = dpy.send_request(dwr)?;
        log::debug!("Sent DestroyWindowRequest to server.");
        dpy.resolve_request(tok)
    }

    /// Free this window, async redox.
    #[cfg(feature = "async")]
    #[inline]
    pub async fn free_async<Conn: Connection>(self, dpy: &mut Display<Conn>) -> crate::Result {
        let dwr = DestroyWindowRequest {
            window: self,
            ..Default::default()
        };
        log::debug!("Sending DestroyWindowRequest to server.");
        let tok = dpy.send_request_async(dwr).await?;
        log::debug!("Sent DestroyWindowRequest to server.");
        dpy.resolve_request_async(tok).await
    }
}

/// Convert a `GetWindowAttributesReply` to a `WindowAttributes` struct.
#[inline]
fn convert_get_window_attributes_reply(reply: GetWindowAttributesReply) -> WindowAttributes {
    WindowAttributes {
        backing_store: reply.backing_store,
        visual: reply.visual,
        class: reply.class,
        bit_gravity: reply.bit_gravity,
        win_gravity: reply.win_gravity,
        backing_planes: reply.backing_planes,
        backing_pixel: reply.backing_pixel,
        save_under: reply.save_under,
        map_is_installed: reply.map_is_installed,
        map_state: reply.map_state,
        override_redirect: reply.override_redirect,
        colormap: reply.colormap,
        all_event_masks: reply.all_event_masks,
        your_event_mask: reply.your_event_mask,
        do_not_propagate_mask: reply.do_not_propagate_mask,
    }
}

/// The type of the property being changed.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u32)]
pub enum PropertyType {
    Atom = 4,
    String = 31,
}

/// The format of the property being changed.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum PropertyFormat {
    Eight = 8,
    Sixteen = 16,
    ThirtyTwo = 32,
}