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
use core::ffi::c_int as int;
use core::ffi::c_ulong as ulong;

use linux_io::fd::ioctl::{
    ioctl_no_arg, ioctl_write, ioctl_writeread, IoDevice, IoctlReqNoArgs, IoctlReqWrite,
    IoctlReqWriteRead,
};

pub struct DrmCardDevice;

impl IoDevice for DrmCardDevice {}

const DRM_IOCTL_BASE: u64 = 100;

#[allow(non_snake_case)]
const fn _IO(nr: ulong) -> ulong {
    linux_io::fd::ioctl::_IO(DRM_IOCTL_BASE, nr)
}

#[allow(non_snake_case)]
const fn _IOW<T>(nr: ulong) -> ulong {
    linux_io::fd::ioctl::_IOW(DRM_IOCTL_BASE, nr, core::mem::size_of::<T>() as u64)
}

#[allow(non_snake_case)]
const fn _IOR<T>(nr: ulong) -> ulong {
    linux_io::fd::ioctl::_IOR(DRM_IOCTL_BASE, nr, core::mem::size_of::<T>() as u64)
}

#[allow(non_snake_case)]
const fn _IOWR<T>(nr: ulong) -> ulong {
    linux_io::fd::ioctl::_IOWR(DRM_IOCTL_BASE, nr, core::mem::size_of::<T>() as u64)
}

/// Fixed-point unsigned 16.16-bit number type, represented as [`u32`].
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct fixedu16_16(u32);

impl fixedu16_16 {
    #[inline(always)]
    pub fn from_u16(v: u16) -> Self {
        Self((v as u32) << 16)
    }

    #[inline(always)]
    pub fn from_u16_frac(w: u16, f: u16) -> Self {
        Self(((w as u32) << 16) | (f as u32))
    }

    #[inline(always)]
    pub fn as_raw_u32(self) -> u32 {
        self.0
    }
}

impl From<u16> for fixedu16_16 {
    #[inline(always)]
    fn from(value: u16) -> Self {
        Self::from_u16(value)
    }
}

impl From<u8> for fixedu16_16 {
    #[inline(always)]
    fn from(value: u8) -> Self {
        Self::from_u16(value as u16)
    }
}

macro_rules! impl_zeroed {
    ($t:ty) => {
        impl $t {
            #[inline(always)]
            pub const fn zeroed() -> Self {
                // Safety: All of the field types in $t must
                // treat all-zeroes as a valid bit pattern.
                unsafe { ::core::mem::zeroed() }
            }
        }

        /// The default value is the result of [`Self::zeroed`].
        impl ::core::default::Default for $t {
            #[inline(always)]
            fn default() -> Self {
                Self::zeroed()
            }
        }
    };
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrmVersion {
    pub version_major: int,
    pub version_minor: int,
    pub version_patchlevel: int,
    pub name_len: usize,
    pub name: *mut i8,
    pub date_len: usize,
    pub date: *mut i8,
    pub desc_len: usize,
    pub desc: *mut i8,
}

impl_zeroed!(DrmVersion);

pub const DRM_IOCTL_VERSION: IoctlReqWriteRead<DrmCardDevice, DrmVersion, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmVersion>(0x00)) };

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrmSetVersion {
    pub drm_di_major: int,
    pub drm_di_minor: int,
    pub drm_dd_major: int,
    pub drm_dd_minor: int,
}

impl_zeroed!(DrmSetVersion);

pub const DRM_IOCTL_SET_VERSION: IoctlReqWriteRead<DrmCardDevice, DrmSetVersion, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmSetVersion>(0x07)) };

pub const DRM_IOCTL_SET_MASTER: IoctlReqNoArgs<DrmCardDevice, int> =
    unsafe { ioctl_no_arg(_IO(0x1e)) };

pub const DRM_IOCTL_DROP_MASTER: IoctlReqNoArgs<DrmCardDevice, int> =
    unsafe { ioctl_no_arg(_IO(0x1f)) };

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrmGetCap {
    pub capability: DrmCap,
    pub value: u64,
}

impl_zeroed!(DrmGetCap);

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct DrmCap(pub u64);

pub const DRM_IOCTL_GET_CAP: IoctlReqWriteRead<DrmCardDevice, DrmGetCap, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmGetCap>(0x0c)) };

/// If set to 1, the driver supports creating "dumb buffers" via [`DRM_IOCTL_MODE_CREATE_DUMB`].
pub const DRM_CAP_DUMB_BUFFER: DrmCap = DrmCap(0x1);
/// If set to 1, the kernel supports specifying a CRTC index
/// in the high bits of [`DrmWaitVblankRequest::type`].
pub const DRM_CAP_VBLANK_HIGH_CRTC: DrmCap = DrmCap(0x2);
/// The preferred bit depth for "dumb buffers".
///
/// The bit depth is the number of bits used to indicate the color of a single
/// pixel excluding any padding. This is different from the number of bits per
/// pixel. For instance, XRGB8888 has a bit depth of 24 but has 32 bits per
/// pixel.
///
/// This preference only applies to dumb buffers, and is irrelevant for
/// other types of buffers.
pub const DRM_CAP_DUMB_PREFERRED_DEPTH: DrmCap = DrmCap(0x3);
/// If set to 1, the driver prefers userspace to render to a shadow buffer
/// instead of directly rendering to a dumb buffer. For best speed, userspace
/// should do streaming ordered memory copies into the dumb buffer and never
/// read from it.
///
/// This preference only applies to dumb buffers, and is irrelevant for
/// other types of buffers.
pub const DRM_CAP_DUMB_PREFER_SHADOW: DrmCap = DrmCap(0x4);
/// Bitfield of supported PRIME sharing capabilities. See [`DRM_PRIME_CAP_IMPORT`]
/// and [`DRM_PRIME_CAP_EXPORT`].
///
/// Starting from kernel version 6.6, both `DRM_PRIME_CAP_IMPORT` and
/// `DRM_PRIME_CAP_EXPORT` are always advertised.
///
/// PRIME buffers are exposed as dma-buf file descriptors.
pub const DRM_CAP_PRIME: DrmCap = DrmCap(0x5);
/// If this bit is set in [`DRM_CAP_PRIME`], the driver supports importing PRIME
/// buffers via [`DRM_IOCTL_PRIME_FD_TO_HANDLE`].
pub const DRM_PRIME_CAP_IMPORT: DrmCap = DrmCap(0x1);
/// If this bit is set in [`DRM_CAP_PRIME`], the driver supports exporting PRIME
/// buffers via [`DRM_IOCTL_PRIME_HANDLE_TO_FD`].
pub const DRM_PRIME_CAP_EXPORT: DrmCap = DrmCap(0x2);
/// If set to 0, the kernel will report timestamps with `CLOCK_REALTIME` in
/// [`crate::event::raw::DrmEventVblank`]. If set to 1, the kernel will report
/// timestamps with `CLOCK_MONOTONIC`.
///
/// Starting from kernel version 2.6.39, the default value for this capability
/// is 1. Starting kernel version 4.15, this capability is always set to 1.
pub const DRM_CAP_TIMESTAMP_MONOTONIC: DrmCap = DrmCap(0x6);
/// If set to 1, the driver supports [`DRM_MODE_PAGE_FLIP_ASYNC`] for legacy
/// page-flips.
pub const DRM_CAP_ASYNC_PAGE_FLIP: DrmCap = DrmCap(0x7);
/// A plane width that is valid to use for a cursor plane.
pub const DRM_CAP_CURSOR_WIDTH: DrmCap = DrmCap(0x8);
/// A plane height that is valid to use for a cursor plane.
pub const DRM_CAP_CURSOR_HEIGHT: DrmCap = DrmCap(0x9);
/// If set to 1, the driver supports supplying modifiers in [`DRM_IOCTL_MODE_ADDFB2`].
pub const DRM_CAP_ADDFB2_MODIFIERS: DrmCap = DrmCap(0x10);
pub const DRM_CAP_PAGE_FLIP_TARGET: DrmCap = DrmCap(0x11);
/// If set to 1, the kernel supports reporting the CRTC ID in
/// [`crate::event::raw::DrmEventVblank::crtc_id`] for
/// [`crate::event::raw::DRM_EVENT_VBLANK`] and
/// [`crate::event::raw::DRM_EVENT_FLIP_COMPLETE`] events.
///
/// Starting kernel version 4.12, this capability is always set to 1.
pub const DRM_CAP_CRTC_IN_VBLANK_EVENT: DrmCap = DrmCap(0x12);
/// If set to 1, the driver supports sync objects.
pub const DRM_CAP_SYNCOBJ: DrmCap = DrmCap(0x13);
/// If set to 1, the driver supports timeline operations on sync objects.
pub const DRM_CAP_SYNCOBJ_TIMELINE: DrmCap = DrmCap(0x14);
/// If set to 1, the driver supports [`DRM_MODE_PAGE_FLIP_ASYNC`] for atomic commits.
pub const DRM_CAP_ATOMIC_ASYNC_PAGE_FLIP: DrmCap = DrmCap(0x15);

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrmSetClientCap {
    pub capability: DrmClientCap,
    pub value: u64,
}

impl_zeroed!(DrmSetClientCap);

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct DrmClientCap(pub u64);

pub const DRM_IOCTL_SET_CLIENT_CAP: IoctlReqWrite<DrmCardDevice, DrmSetClientCap, int> =
    unsafe { ioctl_write(_IOW::<DrmSetClientCap>(0x0d)) };

/// If set to 1, the DRM core will expose the stereo 3D capabilities of the
/// monitor by advertising the supported 3D layouts in the flags of struct
/// drm_mode_modeinfo.
pub const DRM_CLIENT_CAP_STEREO_3D: DrmClientCap = DrmClientCap(1);

/// If set to 1, the DRM core will expose all planes (overlay, primary, and
/// cursor) to userspace.
pub const DRM_CLIENT_CAP_UNIVERSAL_PLANES: DrmClientCap = DrmClientCap(2);

/// If set to 1, the DRM core will expose atomic properties to userspace.
pub const DRM_CLIENT_CAP_ATOMIC: DrmClientCap = DrmClientCap(3);

/// If set to 1, the DRM core will provide aspect ratio information in modes.
pub const DRM_CLIENT_CAP_ASPECT_RATIO: DrmClientCap = DrmClientCap(4);

/// If set to 1, the DRM core will expose special connectors to be used for
/// writing back to memory the scene setup in the commit. Depends on client
/// also supporting DRM_CLIENT_CAP_ATOMIC
pub const DRM_CLIENT_CAP_WRITEBACK_CONNECTORS: DrmClientCap = DrmClientCap(5);

/// Drivers for para-virtualized hardware (e.g. `vmwgfx`, `qxl`, `virtio` and
/// `virtualbox`) have additional restrictions for cursor planes (thus
/// making cursor planes on those drivers not truly universal,) e.g.
/// they need cursor planes to act like one would expect from a mouse
/// cursor and have correctly set hotspot properties.
/// If this client cap is not set the DRM core will hide cursor plane on
/// those virtualized drivers because not setting it implies that the
/// client is not capable of dealing with those extra restictions.
/// Clients which do set cursor hotspot and treat the cursor plane
/// like a mouse cursor should set this property.
/// The client must enable [`DRM_CLIENT_CAP_ATOMIC`] first.
///
/// Setting this property on drivers which do not special case
/// cursor planes (i.e. non-virtualized drivers) will return
/// [`linux_io::result::EOPNOTSUPP`], which can be used by userspace
/// to gauge requirements of the hardware/drivers they're running on.
///
/// This capability is always supported for atomic-capable virtualized
/// drivers starting from kernel version 6.6.
pub const DRM_CLIENT_CAP_CURSOR_PLANE_HOTSPOT: DrmClientCap = DrmClientCap(6);

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeCardRes {
    pub fb_id_ptr: u64,
    pub crtc_id_ptr: u64,
    pub connector_id_ptr: u64,
    pub encoder_id_ptr: u64,
    pub count_fbs: u32,
    pub count_crtcs: u32,
    pub count_connectors: u32,
    pub count_encoders: u32,
    pub min_width: u32,
    pub max_width: u32,
    pub min_height: u32,
    pub max_height: u32,
}

impl_zeroed!(DrmModeCardRes);

pub const DRM_IOCTL_MODE_GETRESOURCES: IoctlReqWriteRead<DrmCardDevice, DrmModeCardRes, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeCardRes>(0xa0)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeInfo {
    pub clock: u32,
    pub hdisplay: u16,
    pub hsync_start: u16,
    pub hsync_end: u16,
    pub htotal: u16,
    pub hskew: u16,
    pub vdisplay: u16,
    pub vsync_start: u16,
    pub vsync_end: u16,
    pub vtotal: u16,
    pub vscan: u16,
    pub vrefresh: u32,
    pub flags: u32,
    pub typ: u32,
    pub name: [core::ffi::c_char; 32],
}

pub const DRM_MODE_TYPE_PREFERRED: u32 = 1 << 3;
pub const DRM_MODE_TYPE_USERDEF: u32 = 1 << 5;
pub const DRM_MODE_TYPE_DRIVER: u32 = 1 << 6;

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeGetConnector {
    pub encoders_ptr: u64,
    pub modes_ptr: u64,
    pub props_ptr: u64,
    pub prop_values_ptr: u64,
    pub count_modes: u32,
    pub count_props: u32,
    pub count_encoders: u32,
    pub encoder_id: u32,
    pub connector_id: u32,
    pub connector_type: u32,
    pub connector_type_id: u32,
    pub connection: u32,
    pub mm_width: u32,
    pub mm_height: u32,
    pub subpixel: u32,
    #[doc(hidden)]
    pub _pad: u32,
}

impl_zeroed!(DrmModeGetConnector);

pub const DRM_IOCTL_MODE_GETCONNECTOR: IoctlReqWriteRead<DrmCardDevice, DrmModeGetConnector, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeGetConnector>(0xa7)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeGetEncoder {
    pub encoder_id: u32,
    pub encoder_type: u32,
    pub crtc_id: u32,
    pub possible_crtcs: u32,
    pub possible_clones: u32,
}

impl_zeroed!(DrmModeGetEncoder);

pub const DRM_IOCTL_MODE_GETENCODER: IoctlReqWriteRead<DrmCardDevice, DrmModeGetEncoder, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeGetEncoder>(0xa6)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeCrtc {
    pub set_connectors_ptr: u64,
    pub count_connectors: u32,
    pub crtc_id: u32,
    pub fb_id: u32,
    pub x: u32,
    pub y: u32,
    pub gamma_size: u32,
    pub mode_valid: u32,
    pub mode: DrmModeInfo,
}

impl_zeroed!(DrmModeCrtc);

pub const DRM_IOCTL_MODE_GETCRTC: IoctlReqWriteRead<DrmCardDevice, DrmModeCrtc, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeCrtc>(0xa1)) };

pub const DRM_IOCTL_MODE_SETCRTC: IoctlReqWriteRead<DrmCardDevice, DrmModeCrtc, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeCrtc>(0xa2)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeCreateDumb {
    pub height: u32,
    pub width: u32,
    pub bpp: u32,
    pub flags: u32,
    pub handle: u32,
    pub pitch: u32,
    pub size: u64,
}

impl_zeroed!(DrmModeCreateDumb);

pub const DRM_IOCTL_MODE_CREATE_DUMB: IoctlReqWriteRead<DrmCardDevice, DrmModeCreateDumb, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeCreateDumb>(0xb2)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeMapDumb {
    pub handle: u32,
    pub pad: u32,
    pub offset: u64,
}

impl_zeroed!(DrmModeMapDumb);

pub const DRM_IOCTL_MODE_MAP_DUMB: IoctlReqWriteRead<DrmCardDevice, DrmModeMapDumb, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeMapDumb>(0xb3)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeDestroyDumb {
    pub handle: u32,
}

impl_zeroed!(DrmModeDestroyDumb);

pub const DRM_IOCTL_MODE_DESTROY_DUMB: IoctlReqWriteRead<DrmCardDevice, DrmModeDestroyDumb, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeDestroyDumb>(0xb4)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeFbCmd {
    pub fb_id: u32,
    pub width: u32,
    pub height: u32,
    pub pitch: u32,
    pub bpp: u32,
    pub depth: u32,
    pub handle: u32,
}

impl_zeroed!(DrmModeFbCmd);

pub const DRM_IOCTL_MODE_GETFB: IoctlReqWriteRead<DrmCardDevice, DrmModeFbCmd, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeFbCmd>(0xad)) };

pub const DRM_IOCTL_MODE_ADDFB: IoctlReqWriteRead<DrmCardDevice, DrmModeFbCmd, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeFbCmd>(0xae)) };

pub const DRM_IOCTL_MODE_RMFB: IoctlReqWriteRead<DrmCardDevice, linux_unsafe::uint, int> =
    unsafe { ioctl_writeread(_IOWR::<linux_unsafe::uint>(0xaf)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeFbDirtyCmd {
    pub fb_id: u32,
    pub flags: u32,
    pub color: u32,
    pub num_clips: u32,
    pub clips_ptr: u64,
}

impl_zeroed!(DrmModeFbDirtyCmd);

///
/// Mark a region of a framebuffer as dirty.
///
/// Some hardware does not automatically update display contents
/// as a hardware or software draw to a framebuffer. This ioctl
/// allows userspace to tell the kernel and the hardware what
/// regions of the framebuffer have changed.
///
/// The kernel or hardware is free to update more then just the
/// region specified by the clip rects. The kernel or hardware
/// may also delay and/or coalesce several calls to dirty into a
/// single update.
///
/// Userspace may annotate the updates, the annotates are a
/// promise made by the caller that the change is either a copy
/// of pixels or a fill of a single color in the region specified.
///
/// If the [`DRM_MODE_FB_DIRTY_ANNOTATE_COPY`] flag is given then
/// the number of updated regions are half of num_clips given,
/// where the clip rects are paired in src and dst. The width and
/// height of each one of the pairs must match.
///
/// If the [`DRM_MODE_FB_DIRTY_ANNOTATE_FILL`] flag is given the caller
/// promises that the region specified of the clip rects is filled
/// completely with a single color as given in the color argument.
///
pub const DRM_IOCTL_MODE_DIRTYFB: IoctlReqWriteRead<DrmCardDevice, DrmModeFbDirtyCmd, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeFbDirtyCmd>(0xb1)) };

pub const DRM_MODE_FB_DIRTY_ANNOTATE_COPY: u32 = 0x01;
pub const DRM_MODE_FB_DIRTY_ANNOTATE_FILL: u32 = 0x02;
pub const DRM_MODE_FB_DIRTY_FLAGS: u32 = 0x03;
pub const DRM_MODE_FB_DIRTY_MAX_CLIPS: u32 = 256;

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeCrtcPageFlip {
    pub crtc_id: u32,
    pub fb_id: u32,
    pub flags: u32,
    /// Must always be set to zero.
    pub reserved: u32,
    pub user_data: u64,
}

impl_zeroed!(DrmModeCrtcPageFlip);

/// Request a page flip on the specified crtc.
///
/// This ioctl will ask KMS to schedule a page flip for the specified
/// crtc.  Once any pending rendering targeting the specified fb (as of
/// ioctl time) has completed, the crtc will be reprogrammed to display
/// that fb after the next vertical refresh.  The ioctl returns
/// immediately, but subsequent rendering to the current fb will block
/// in the execbuffer ioctl until the page flip happens.  If a page
/// flip is already pending as the ioctl is called, EBUSY will be
/// returned.
///
/// Flag [`DRM_MODE_PAGE_FLIP_EVENT`] requests that drm sends back a vblank
/// event (see drm.h: struct drm_event_vblank) when the page flip is
/// done.  The user_data field passed in with this ioctl will be
/// returned as the user_data field in the vblank event struct.
///
/// Flag [`DRM_MODE_PAGE_FLIP_ASYNC`] requests that the flip happen
/// 'as soon as possible', meaning that it not delay waiting for vblank.
/// This may cause tearing on the screen.
///
/// The reserved field must be zero.
pub const DRM_IOCTL_MODE_PAGE_FLIP: IoctlReqWriteRead<DrmCardDevice, DrmModeCrtcPageFlip, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeCrtcPageFlip>(0xb0)) };

/// Request that the kernel sends back a vblank event (see
/// struct drm_event_vblank) with the [`crate::event::raw::DRM_EVENT_FLIP_COMPLETE`]
/// type when the page-flip is done.
pub const DRM_MODE_PAGE_FLIP_EVENT: u32 = 0x01;
/// Request that the page-flip is performed as soon as possible, ie. with no
/// delay due to waiting for vblank. This may cause tearing to be visible on
/// the screen.
///
/// When used with atomic uAPI, the driver will return an error if the hardware
/// doesn't support performing an asynchronous page-flip for this update.
/// User-space should handle this, e.g. by falling back to a regular page-flip.
///
/// Note, some hardware might need to perform one last synchronous page-flip
/// before being able to switch to asynchronous page-flips. As an exception,
/// the driver will return success even though that first page-flip is not
/// asynchronous.
pub const DRM_MODE_PAGE_FLIP_ASYNC: u32 = 0x02;
pub const DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE: u32 = 0x4;
pub const DRM_MODE_PAGE_FLIP_TARGET_RELATIVE: u32 = 0x8;
pub const DRM_MODE_PAGE_FLIP_TARGET: u32 =
    DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE | DRM_MODE_PAGE_FLIP_TARGET_RELATIVE;
/// Bitmask of flags suitable for [`DrmModeCrtcPageFlip::flags`].
pub const DRM_MODE_PAGE_FLIP_FLAGS: u32 =
    DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_PAGE_FLIP_ASYNC | DRM_MODE_PAGE_FLIP_TARGET;

pub const DRM_MODE_CURSOR_BO: u32 = 0x01;
pub const DRM_MODE_CURSOR_MOVE: u32 = 0x02;
pub const DRM_MODE_CURSOR_FLAGS: u32 = 0x03;

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeAtomic {
    pub flags: u32,
    pub count_objs: u32,
    pub objs_ptr: u64,
    pub count_props_ptr: u64,
    pub props_ptr: u64,
    pub prop_values_ptr: u64,
    pub reserved: u64,
    pub user_data: u64,
}

impl_zeroed!(DrmModeAtomic);

pub const DRM_IOCTL_MODE_ATOMIC: IoctlReqWriteRead<DrmCardDevice, DrmModeAtomic, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeAtomic>(0xbc)) };

/// Do not apply the atomic commit, and instead check whether the hardware supports
/// this configuration.
pub const DRM_MODE_ATOMIC_TEST_ONLY: u32 = 0x0100;

/// Do not block while applying the atomic commit. The [`DRM_IOCTL_MODE_ATOMIC`]
/// request returns immediately instead of waiting for the changes to be applied
/// in hardware. Note, the driver will still check whether the update can be
/// applied before retuning.
pub const DRM_MODE_ATOMIC_NONBLOCK: u32 = 0x0200;

/// Allow the update to result in temporary or transient visible artifacts while
/// the update is being applied. Applying the update may also take significantly
/// more time than a page flip. All visual artifacts will disappear by the time
/// the update is completed, as signalled through the vblank event's timestamp.
///
/// This flag must be set when the KMS update might cause visible artifacts.
/// Without this flag such KMS update will return an `EINVAL` error. What kind of
/// update may cause visible artifacts depends on the driver and the hardware.
/// User-space that needs to know beforehand if an update might cause visible
/// artifacts can use [`DRM_MODE_ATOMIC_TEST_ONLY`] without
/// [`DRM_MODE_ATOMIC_ALLOW_MODESET`] to see if it fails.
///
/// To the best of the driver's knowledge, visual artifacts are guaranteed to
/// not appear when this flag is not set. Some sinks might display visual
/// artifacts outside of the driver's control.
pub const DRM_MODE_ATOMIC_ALLOW_MODESET: u32 = 0x0400;

/// Bitfield of flags accepted by [`DRM_IOCTL_MODE_ATOMIC`] in
/// [`DrmModeAtomic::flags`].
pub const DRM_MODE_ATOMIC_FLAGS: u32 = DRM_MODE_PAGE_FLIP_EVENT
    | DRM_MODE_PAGE_FLIP_ASYNC
    | DRM_MODE_ATOMIC_TEST_ONLY
    | DRM_MODE_ATOMIC_NONBLOCK
    | DRM_MODE_ATOMIC_ALLOW_MODESET;

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeObjGetProperties {
    pub props_ptr: u64,
    pub prop_values_ptr: u64,
    pub count_props: u32,
    pub obj_id: u32,
    pub obj_type: u32,
}

impl_zeroed!(DrmModeObjGetProperties);

pub const DRM_IOCTL_MODE_OBJ_GETPROPERTIES: IoctlReqWriteRead<
    DrmCardDevice,
    DrmModeObjGetProperties,
    int,
> = unsafe { ioctl_writeread(_IOWR::<DrmModeObjGetProperties>(0xb9)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeObjSetProperty {
    pub value: u64,
    pub prop_id: u32,
    pub obj_id: u32,
    pub obj_type: u32,
}

impl_zeroed!(DrmModeObjSetProperty);

pub const DRM_IOCTL_MODE_OBJ_SETPROPERTY: IoctlReqWriteRead<
    DrmCardDevice,
    DrmModeObjSetProperty,
    int,
> = unsafe { ioctl_writeread(_IOWR::<DrmModeObjSetProperty>(0xba)) };

pub const DRM_MODE_OBJECT_CRTC: u32 = 0xcccccccc;
pub const DRM_MODE_OBJECT_CONNECTOR: u32 = 0xc0c0c0c0;
pub const DRM_MODE_OBJECT_ENCODER: u32 = 0xe0e0e0e0;
pub const DRM_MODE_OBJECT_MODE: u32 = 0xdededede;
pub const DRM_MODE_OBJECT_PROPERTY: u32 = 0xb0b0b0b0;
pub const DRM_MODE_OBJECT_FB: u32 = 0xfbfbfbfb;
pub const DRM_MODE_OBJECT_BLOB: u32 = 0xbbbbbbbb;
pub const DRM_MODE_OBJECT_PLANE: u32 = 0xeeeeeeee;
pub const DRM_MODE_OBJECT_ANY: u32 = 0;

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeGetPlaneRes {
    pub plane_id_ptr: u64,
    pub count_planes: u32,
}

impl_zeroed!(DrmModeGetPlaneRes);

pub const DRM_IOCTL_MODE_GETPLANERESOURCES: IoctlReqWriteRead<
    DrmCardDevice,
    DrmModeGetPlaneRes,
    int,
> = unsafe { ioctl_writeread(_IOWR::<DrmModeGetPlaneRes>(0xb5)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeGetPlane {
    pub plane_id: u32,
    pub crtc_id: u32,
    pub fb_id: u32,
    pub possible_crtcs: u32,
    pub gamma_size: u32,
    pub count_format_types: u32,
    pub format_type_ptr: u32,
}

impl_zeroed!(DrmModeGetPlane);

pub const DRM_IOCTL_MODE_GETPLANE: IoctlReqWriteRead<DrmCardDevice, DrmModeGetPlane, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeGetPlane>(0xb6)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeSetPlane {
    pub plane_id: u32,
    pub crtc_id: u32,
    pub fb_id: u32, // fb object contains surface format type
    pub flags: u32, // DRM_MODE_PRESENT_ flags

    pub crtc_x: i32,
    pub crtc_y: i32,
    pub crtc_w: u32,
    pub crtc_h: u32,

    pub src_x: fixedu16_16,
    pub src_y: fixedu16_16,
    pub src_h: fixedu16_16,
    pub src_w: fixedu16_16,
}

impl_zeroed!(DrmModeSetPlane);

pub const DRM_IOCTL_MODE_SETPLANE: IoctlReqWriteRead<DrmCardDevice, DrmModeSetPlane, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeSetPlane>(0xb7)) };

pub const DRM_MODE_PRESENT_TOP_FIELD: u32 = 1 << 0;
pub const DRM_MODE_PRESENT_BOTTOM_FIELD: u32 = 1 << 1;

#[repr(C)]
#[derive(Debug, Clone)]
pub struct DrmModeGetProperty {
    pub values_ptr: u64,
    pub enum_blob_ptr: u64,
    pub prop_id: u32,
    pub flags: u32,
    pub name: [u8; DRM_PROP_NAME_LEN],
    pub count_values: u32,
    pub count_enum_blobs: u32,
}

impl_zeroed!(DrmModeGetProperty);

#[repr(C)]
#[derive(Debug, Clone)]
pub struct DrmModePropertyEnum {
    pub value: u64,
    pub name: [u8; DRM_PROP_NAME_LEN],
}

impl_zeroed!(DrmModePropertyEnum);

/// User-space can perform a `GETPROPERTY` request to retrieve information about a
/// property. The same property may be attached to multiple objects.
///
/// The meaning of [`DrmModeGetProperty::values_ptr`] changes depending on the
/// property type.
///
/// [`DrmModeGetProperty::enum_blob_ptr`] and [`DrmModeGetProperty::count_enum_blobs`]
/// are only meaningful when the property has the type [`DRM_MODE_PROP_ENUM`] or
/// [`DRM_MODE_PROP_BITMASK`]. For backwards compatibility, the kernel will always set
/// [`DrmModeGetProperty::count_enum_blobs`] to
/// zero when the property has the type [`DRM_MODE_PROP_BLOB`]. User-space must
/// ignore these two fields if the property has a different type.
///
/// Userspace is expected to retrieve values and enums by performing this request
/// at least twice: the first time to retrieve the number of elements, the
/// second time to retrieve the elements themselves.
///
/// To retrieve the number of elements, set [`DrmModeGetProperty::count_values`]
/// and [`DrmModeGetProperty::count_enum_blobs`] to zero. [`DrmModeGetProperty::count_values`]
/// will be updated with the number of elements. If the property has the type
/// [`DRM_MODE_PROP_ENUM`] or [`DRM_MODE_PROP_BITMASK`], [`DrmModeGetProperty::count_enum_blobs`]
/// will be updated as well.
///
/// To retrieve the elements themselves, allocate an array for
/// [`DrmModeGetProperty::values_ptr`] and set [`DrmModeGetProperty::count_values`] to
/// its capacity. If the property has the type [`DRM_MODE_PROP_ENUM`] or [`DRM_MODE_PROP_BITMASK`],
/// allocate an array for [`DrmModeGetProperty::enum_blob_ptr`] and set
/// [`DrmModeGetProperty::count_enum_blobs`] to its capacity. Sending the request
/// again will then fill the arrays.
pub const DRM_IOCTL_MODE_GETPROPERTY: IoctlReqWriteRead<DrmCardDevice, DrmModeGetProperty, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeGetProperty>(0xaa)) };

pub const DRM_PROP_NAME_LEN: usize = 32;

pub const DRM_MODE_PROP_PENDING: u32 = 1 << 0;
pub const DRM_MODE_PROP_RANGE: u32 = 1 << 1;
pub const DRM_MODE_PROP_IMMUTABLE: u32 = 1 << 2;
pub const DRM_MODE_PROP_ENUM: u32 = 1 << 3;
pub const DRM_MODE_PROP_BLOB: u32 = 1 << 4;
pub const DRM_MODE_PROP_BITMASK: u32 = 1 << 5;
pub const DRM_MODE_PROP_LEGACY_TYPE: u32 =
    DRM_MODE_PROP_RANGE | DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BLOB | DRM_MODE_PROP_BITMASK;
pub const DRM_MODE_PROP_EXTENDED_TYPE: u32 = 0x0000ffc0;
pub const DRM_MODE_PROP_OBJECT: u32 = DRM_MODE_PROP_TYPE(1);
pub const DRM_MODE_PROP_SIGNED_RANGE: u32 = DRM_MODE_PROP_TYPE(2);

#[allow(non_snake_case)]
#[inline(always)]
pub const fn DRM_MODE_PROP_TYPE(n: u32) -> u32 {
    n << 6
}

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeCreateBlob {
    pub data: u64,
    pub length: u32,
    pub blob_id: u32,
}

impl_zeroed!(DrmModeCreateBlob);

pub const DRM_IOCTL_MODE_CREATEPROPBLOB: IoctlReqWriteRead<DrmCardDevice, DrmModeCreateBlob, int> =
    unsafe { ioctl_writeread(_IOWR::<DrmModeCreateBlob>(0xbd)) };

#[repr(C)]
#[derive(Debug)]
pub struct DrmModeDestroyBlob {
    pub blob_id: u32,
}

impl_zeroed!(DrmModeDestroyBlob);

pub const DRM_IOCTL_MODE_DESTROYPROPBLOB: IoctlReqWriteRead<
    DrmCardDevice,
    DrmModeDestroyBlob,
    int,
> = unsafe { ioctl_writeread(_IOWR::<DrmModeDestroyBlob>(0xbe)) };