jay-compositor 1.13.0

The Jay compositor
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
use {
    crate::{
        backend::{BackendColorSpace, BackendEotfs},
        cli::{
            GlobalArgs,
            json::{
                JsonConnector, JsonDrmDevice, JsonMode, JsonOutput, JsonPrimaries, JsonRandrData,
                JsonTearingMode, JsonVrrMode, jsonl,
            },
        },
        cmm::cmm_primaries::Primaries,
        format::{Format, XRGB8888},
        ifs::wl_output::BlendSpace,
        scale::Scale,
        tools::tool_client::{Handle, ToolClient, with_tool_client},
        tree::Transform,
        utils::{errorfmt::ErrorFmt, ordered_float::F64, static_text::StaticText},
        wire::{JayRandrId, jay_compositor, jay_randr},
    },
    clap::{
        Args, Subcommand, ValueEnum,
        builder::{PossibleValue, PossibleValuesParser},
    },
    isnt::std_1::vec::IsntVecExt,
    jay_config::video::{TearingMode, VrrMode},
    linearize::LinearizeExt,
    std::{
        cell::RefCell,
        fmt::{self, Display, Formatter},
        rc::Rc,
        str::FromStr,
        time::Duration,
    },
    thiserror::Error,
};

#[derive(Args, Debug)]
pub struct RandrArgs {
    #[clap(subcommand)]
    pub command: Option<RandrCmd>,
}

#[derive(Subcommand, Debug)]
pub enum RandrCmd {
    /// Show the current settings.
    Show(ShowArgs),
    /// Modify the settings of a graphics card.
    Card(CardArgs),
    /// Modify the settings of an output.
    Output(OutputArgs),
    /// Modify virtual outputs.
    VirtualOutput(VirtualOutputArgs),
}

impl Default for RandrCmd {
    fn default() -> Self {
        Self::Show(Default::default())
    }
}

#[derive(Args, Debug, Default)]
pub struct ShowArgs {
    /// Show all available modes.
    #[arg(long)]
    pub modes: bool,
    /// Show all available formats.
    #[arg(long)]
    pub formats: bool,
}

#[derive(Args, Debug)]
pub struct CardArgs {
    /// The card to modify, e.g. card0.
    pub card: String,
    #[clap(subcommand)]
    pub command: CardCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum CardCommand {
    /// Make this device the primary device.
    Primary,
    /// Modify the graphics API used by the card.
    Api(ApiArgs),
    /// Modify the direct scanout setting of the card.
    DirectScanout(DirectScanoutArgs),
    /// Modify timing settings of the card.
    Timing(TimingArgs),
}

#[derive(Args, Debug, Clone)]
pub struct TimingArgs {
    #[clap(subcommand)]
    pub cmd: TimingCmd,
}

#[derive(Subcommand, Debug, Clone)]
pub enum TimingCmd {
    /// Sets the margin to use for page flips.
    ///
    /// This is duration between the compositor initiating a page flip and the output's
    /// vblank event. This determines the minimum input latency. The default is 1.5 ms.
    ///
    /// Note that if the margin is too small, the compositor will dynamically increase it.
    SetFlipMargin(SetFlipMarginArgs),
}

#[derive(Args, Debug, Clone)]
pub struct SetFlipMarginArgs {
    /// The margin in milliseconds.
    pub margin_ms: f64,
}

#[derive(Args, Debug, Clone)]
pub struct ApiArgs {
    #[clap(subcommand)]
    pub cmd: ApiCmd,
}

#[derive(Subcommand, Debug, Clone)]
pub enum ApiCmd {
    /// Use OpenGL for rendering in this card.
    #[clap(name = "opengl")]
    OpenGl,
    /// Use Vulkan for rendering in this card.
    #[clap(name = "vulkan")]
    Vulkan,
}

#[derive(Args, Debug, Clone)]
pub struct DirectScanoutArgs {
    #[clap(subcommand)]
    pub cmd: DirectScanoutCmd,
}

#[derive(Subcommand, Debug, Clone)]
pub enum DirectScanoutCmd {
    /// Enable direct scanout.
    Enable,
    /// Disable direct scanout.
    Disable,
}

#[derive(Args, Debug)]
pub struct OutputArgs {
    /// The output to modify, e.g. DP-1.
    pub output: String,
    #[clap(subcommand)]
    pub command: OutputCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum OutputCommand {
    /// Modify the transform of the output.
    Transform(TransformArgs),
    /// Modify the scale of the output.
    Scale(ScaleArgs),
    /// Modify the mode of the output.
    Mode(ModeArgs),
    /// Modify the position of the output.
    Position(PositionArgs),
    /// Enable the output.
    Enable,
    /// Disable the output.
    Disable,
    /// Override the display's non-desktop setting.
    NonDesktop(NonDesktopArgs),
    /// Change VRR settings.
    Vrr(VrrArgs),
    /// Change tearing settings.
    Tearing(TearingArgs),
    /// Change format settings.
    Format(FormatSettings),
    /// Change color settings.
    Colors(ColorsSettings),
    /// Change the output brightness.
    Brightness(BrightnessArgs),
    /// Change the blend space.
    BlendSpace(BlendSpaceArgs),
    /// Change whether the display primaries are used.
    UseNativeGamut(UseNativeGamutArgs),
}

#[derive(Args, Debug, Clone)]
pub struct UseNativeGamutArgs {
    /// Configures whether the display primaries are used.
    ///
    /// By default, Jay pretends that the display uses sRGB primaries. This is also how
    /// most other systems behave. In reality, most displays use a much larger gamut. For
    /// example, they advertise that they support 95% of the DCI-P3 gamut. If the display
    /// is interpreting colors in their native gamut, then colors will appear more
    /// saturated than their specification.
    ///
    /// If this is set to `true`, Jay assumes that the display uses the primaries
    /// advertised in its EDID. This might produce more accurate colors while also
    /// allowing color-managed applications to use the full gamut of the display.
    ///
    /// This setting has no effect when the display is explicitly operating in a wide
    /// color space.
    ///
    /// The default is `false`.
    #[arg(action = clap::ArgAction::Set)]
    pub use_native_gamut: bool,
}

#[derive(ValueEnum, Debug, Clone)]
pub enum NonDesktopType {
    Default,
    False,
    True,
}

#[derive(Args, Debug, Clone)]
pub struct NonDesktopArgs {
    /// Whether this output is a non-desktop output.
    pub setting: NonDesktopType,
}

#[derive(Args, Debug, Clone)]
pub struct VrrArgs {
    #[clap(subcommand)]
    pub command: VrrCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum VrrCommand {
    /// Sets the mode that determines when VRR is enabled.
    SetMode(SetVrrModeArgs),
    /// Sets the maximum refresh rate of the cursor.
    SetCursorHz(CursorHzArgs),
}

#[derive(Args, Debug, Clone)]
pub struct SetVrrModeArgs {
    #[clap(value_enum)]
    pub mode: VrrModeArg,
}

#[derive(ValueEnum, Debug, Copy, Clone, Hash, PartialEq)]
pub enum VrrModeArg {
    /// VRR is never enabled.
    Never,
    /// VRR is always enabled.
    Always,
    /// VRR is enabled when one or more applications are displayed fullscreen.
    Variant1,
    /// VRR is enabled when a single application is displayed fullscreen.
    Variant2,
    /// VRR is enabled when a single game or video is displayed fullscreen.
    Variant3,
}

#[derive(Args, Debug, Clone)]
pub struct CursorHzArgs {
    /// The rate at which the cursor will be updated on screen.
    pub rate: String,
}

#[derive(Args, Debug, Clone)]
pub struct FormatSettings {
    #[clap(subcommand)]
    pub command: FormatCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum FormatCommand {
    /// Sets the format of the framebuffer.
    Set {
        #[clap(value_enum)]
        format: &'static Format,
    },
}

#[derive(Args, Debug, Clone)]
pub struct TearingArgs {
    #[clap(subcommand)]
    pub command: TearingCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum TearingCommand {
    /// Sets the mode that determines when tearing is enabled.
    SetMode(SetTearingModeArgs),
}

#[derive(Args, Debug, Clone)]
pub struct SetTearingModeArgs {
    #[clap(value_enum)]
    pub mode: TearingModeArg,
}

#[derive(ValueEnum, Debug, Copy, Clone, Hash, PartialEq)]
pub enum TearingModeArg {
    /// Tearing is never enabled.
    Never,
    /// Tearing is always enabled.
    Always,
    /// Tearing is enabled when one or more applications are displayed fullscreen.
    Variant1,
    /// Tearing is enabled when a single application is displayed fullscreen.
    Variant2,
    /// Tearing is enabled when a single application is displayed fullscreen and the
    /// application has requested tearing.
    ///
    /// This is the default.
    Variant3,
}

#[derive(Args, Debug, Clone)]
pub struct PositionArgs {
    /// The top-left x coordinate.
    pub x: i32,
    /// The top-left y coordinate.
    pub y: i32,
}

#[derive(Args, Debug, Clone)]
pub struct ModeArgs {
    /// The width.
    pub width: i32,
    /// The height.
    pub height: i32,
    /// The refresh rate.
    pub refresh_rate: f64,
}

#[derive(Args, Debug, Clone)]
pub struct ScaleArgs {
    /// Rounds the scale to a value that can be stored in a floating-point number. This
    /// might be useful because some applications store scales as floating-point numbers
    /// and can become blurry if the scale cannot be represented exactly.
    #[arg(long)]
    pub round_to_float: bool,
    /// The new scale.
    pub scale: f64,
}

#[derive(Args, Debug, Clone)]
pub struct TransformArgs {
    #[clap(subcommand)]
    pub command: TransformCmd,
}

#[derive(Subcommand, Debug, Clone)]
pub enum TransformCmd {
    /// Apply no transformation.
    None,
    /// Rotate the content 90 degrees counter-clockwise.
    #[clap(name = "rotate-90")]
    Rotate90,
    /// Rotate the content 180 degrees counter-clockwise.
    #[clap(name = "rotate-180")]
    Rotate180,
    /// Rotate the content 270 degrees counter-clockwise.
    #[clap(name = "rotate-270")]
    Rotate270,
    /// Flip the content around the vertical axis.
    Flip,
    /// Flip the content around the vertical axis, then rotate 90 degrees counter-clockwise.
    #[clap(name = "flip-rotate-90")]
    FlipRotate90,
    /// Flip the content around the vertical axis, then rotate 180 degrees counter-clockwise.
    #[clap(name = "flip-rotate-180")]
    FlipRotate180,
    /// Flip the content around the vertical axis, then rotate 270 degrees counter-clockwise.
    #[clap(name = "flip-rotate-270")]
    FlipRotate270,
}

#[derive(Args, Debug, Clone)]
pub struct ColorsSettings {
    #[clap(subcommand)]
    pub command: ColorsCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum ColorsCommand {
    /// Sets the color space and EOTF of the output.
    Set {
        /// The name of the color space.
        #[clap(value_parser = PossibleValuesParser::new(color_space_possible_values()))]
        color_space: String,
        /// The name of the EOTF.
        #[clap(value_parser = PossibleValuesParser::new(eotf_possible_values()))]
        eotf: String,
    },
}

fn color_space_possible_values() -> Vec<PossibleValue> {
    let mut res = vec![];
    for cs in BackendColorSpace::variants() {
        use BackendColorSpace::*;
        let help = match cs {
            Default => "The default color space (usually sRGB)",
            Bt2020 => "The BT.2020 color space",
        };
        res.push(PossibleValue::new(cs.name()).help(help));
    }
    res
}

fn eotf_possible_values() -> Vec<PossibleValue> {
    let mut res = vec![];
    for cs in BackendEotfs::variants() {
        use BackendEotfs::*;
        let help = match cs {
            Default => "The default EOTF (usually gamma22)",
            Pq => "The PQ EOTF",
        };
        res.push(PossibleValue::new(cs.name()).help(help));
    }
    res
}

#[derive(Args, Debug, Clone)]
pub struct BrightnessArgs {
    /// The brightness of standard white in cd/m^2 or `default` to use the default
    /// brightness.
    ///
    /// The default brightness depends on the EOTF:
    ///
    /// - default: the maximum display brightness
    /// - PQ: 203 cd/m^2.
    ///
    /// When using the default EOTF, you likely want to set this to `default`
    /// and adjust the display hardware brightness setting instead.
    ///
    /// When used with the default transfer function, the default brightness is anchored
    /// at 80 cd/m^2. That is, setting this to 40 cd/m^2 makes everything appear half as
    /// bright as normal and creates 50% HDR headroom.
    ///
    /// This has no effect unless the vulkan renderer is used.
    #[clap(verbatim_doc_comment, value_parser = parse_brightness)]
    brightness: Brightness,
}

#[derive(Debug, Clone)]
pub enum Brightness {
    Default,
    Lux(f64),
}

#[derive(Debug, Error)]
#[error("Value is neither `default` nor a floating point value")]
struct ParseBrightnessError;

fn parse_brightness(s: &str) -> Result<Brightness, ParseBrightnessError> {
    if s == "default" {
        return Ok(Brightness::Default);
    }
    f64::from_str(s)
        .map(Brightness::Lux)
        .map_err(|_| ParseBrightnessError)
}

#[derive(Args, Debug, Clone)]
pub struct BlendSpaceArgs {
    /// The space to blend translucent surfaces in.
    #[clap(value_parser = PossibleValuesParser::new(blend_space_possible_values()))]
    blend_space: String,
}

fn blend_space_possible_values() -> Vec<PossibleValue> {
    let mut res = vec![];
    for bs in BlendSpace::variants() {
        use BlendSpace::*;
        let help = match bs {
            Linear => "Linear space, more accurate but brighter",
            Srgb => "sRGB space, the classic desktop blend space",
        };
        res.push(PossibleValue::new(bs.name()).help(help));
    }
    res
}

#[derive(Args, Debug)]
pub struct VirtualOutputArgs {
    #[clap(subcommand)]
    pub command: VirtualOutputCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum VirtualOutputCommand {
    /// Create a virtual output.
    Create(CreateVirtualOutputArgs),
    /// Remove a virtual output.
    Remove(RemoveVirtualOutputArgs),
}

#[derive(Args, Debug, Clone)]
pub struct CreateVirtualOutputArgs {
    /// The name of the virtual output.
    pub name: String,
}

#[derive(Args, Debug, Clone)]
pub struct RemoveVirtualOutputArgs {
    /// The name of the virtual output.
    pub name: String,
}

pub fn main(global: GlobalArgs, args: RandrArgs) {
    with_tool_client(global.log_level, |tc| async move {
        let idle = Rc::new(Randr { tc: tc.clone() });
        idle.run(&global, args).await;
    });
}

#[derive(Clone, Debug)]
struct Device {
    pub id: u64,
    pub syspath: String,
    pub devnode: String,
    pub vendor: u32,
    pub vendor_name: String,
    pub model: u32,
    pub model_name: String,
    pub gfx_api: String,
    pub render_device: bool,
}

#[derive(Clone, Debug)]
struct Connector {
    pub _id: u64,
    pub drm_device: Option<u64>,
    pub name: String,
    pub enabled: bool,
    pub output: Option<Output>,
}

#[derive(Clone, Debug, Default)]
struct Output {
    pub scale: f64,
    pub width: i32,
    pub height: i32,
    pub x: i32,
    pub y: i32,
    pub transform: Transform,
    pub manufacturer: String,
    pub product: String,
    pub serial_number: String,
    pub width_mm: i32,
    pub height_mm: i32,
    pub current_mode: Option<Mode>,
    pub modes: Vec<Mode>,
    pub non_desktop: bool,
    pub vrr_capable: bool,
    pub vrr_enabled: bool,
    pub vrr_mode: VrrMode,
    pub vrr_cursor_hz: Option<f64>,
    pub tearing_mode: TearingMode,
    pub formats: Vec<String>,
    pub format: Option<String>,
    pub flip_margin_ns: Option<u64>,
    pub supported_color_spaces: Vec<String>,
    pub current_color_space: Option<String>,
    pub supported_eotfs: Vec<String>,
    pub current_eotf: Option<String>,
    pub brightness_range: Option<(f64, f64)>,
    pub brightness: Option<f64>,
    pub blend_space: Option<String>,
    pub native_gamut: Option<Primaries>,
    pub use_native_gamut: bool,
    pub arbitrary_modes: bool,
}

#[derive(Copy, Clone, Debug)]
struct Mode {
    pub width: i32,
    pub height: i32,
    pub refresh_rate_millihz: u32,
    pub current: bool,
}

impl Mode {
    fn refresh_rate(&self) -> f64 {
        (self.refresh_rate_millihz as f64) / 1000.0
    }
}

impl Display for Mode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} x {} @ {}",
            self.width,
            self.height,
            self.refresh_rate(),
        )
    }
}

#[derive(Clone, Debug, Default)]
struct Data {
    default_api: String,
    drm_devices: Vec<Device>,
    connectors: Vec<Connector>,
}

struct Randr {
    tc: Rc<ToolClient>,
}

impl Randr {
    async fn run(self: &Rc<Self>, global: &GlobalArgs, args: RandrArgs) {
        let tc = &self.tc;
        let comp = tc.jay_compositor().await;
        let randr = tc.id();
        tc.send(jay_compositor::GetRandr {
            self_id: comp,
            id: randr,
        });
        match args.command.unwrap_or_default() {
            RandrCmd::Show(args) => self.show(global, randr, args).await,
            RandrCmd::Card(args) => self.card(randr, args).await,
            RandrCmd::Output(args) => self.output(randr, args).await,
            RandrCmd::VirtualOutput(args) => self.virtual_output(randr, args).await,
        }
    }

    fn handle_error<F: Fn(&str) + 'static>(&self, randr: JayRandrId, f: F) {
        jay_randr::Error::handle(&self.tc, randr, (), move |_, msg| {
            f(msg.msg);
            std::process::exit(1);
        });
    }

    async fn output(self: &Rc<Self>, randr: JayRandrId, args: OutputArgs) {
        let tc = &self.tc;
        match args.command {
            OutputCommand::Transform(t) => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not modify the transform: {}", msg);
                });
                let transform = match t.command {
                    TransformCmd::None => Transform::None,
                    TransformCmd::Rotate90 => Transform::Rotate90,
                    TransformCmd::Rotate180 => Transform::Rotate180,
                    TransformCmd::Rotate270 => Transform::Rotate270,
                    TransformCmd::Flip => Transform::Flip,
                    TransformCmd::FlipRotate90 => Transform::FlipRotate90,
                    TransformCmd::FlipRotate180 => Transform::FlipRotate180,
                    TransformCmd::FlipRotate270 => Transform::FlipRotate270,
                };
                tc.send(jay_randr::SetTransform {
                    self_id: randr,
                    output: &args.output,
                    transform: transform.to_wl(),
                });
            }
            OutputCommand::Scale(t) => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not modify the scale: {}", msg);
                });
                let scale = match t.round_to_float {
                    true => Scale::from_f64_as_float(t.scale),
                    false => Scale::from_f64(t.scale),
                };
                tc.send(jay_randr::SetScale {
                    self_id: randr,
                    output: &args.output,
                    scale: scale.to_wl(),
                });
            }
            OutputCommand::Mode(t) => {
                let name = args.output.to_ascii_lowercase();
                let data = self.get(randr).await;
                let Some(connector) = data
                    .connectors
                    .iter()
                    .find(|c| c.name.to_ascii_lowercase() == name)
                else {
                    log::error!("Connector with name `{}` does not exist", args.output);
                    return;
                };
                let Some(output) = &connector.output else {
                    log::error!("Connector {} is not connected", connector.name);
                    return;
                };
                let mode = 'mode: {
                    if let Some(mode) = output.modes.iter().find(|m| {
                        m.width == t.width
                            && m.height == t.height
                            && m.refresh_rate() == t.refresh_rate
                    }) {
                        break 'mode *mode;
                    }
                    if output.arbitrary_modes {
                        break 'mode Mode {
                            width: t.width,
                            height: t.height,
                            refresh_rate_millihz: (t.refresh_rate * 1_000.0).round() as u32,
                            current: false,
                        };
                    }
                    log::error!(
                        "Output {} does not support this refresh rate",
                        connector.name
                    );
                    return;
                };
                self.handle_error(randr, |msg| {
                    eprintln!("Could not modify the mode: {}", msg);
                });
                tc.send(jay_randr::SetMode {
                    self_id: randr,
                    output: &args.output,
                    width: mode.width,
                    height: mode.height,
                    refresh_rate_millihz: mode.refresh_rate_millihz,
                });
            }
            OutputCommand::Position(t) => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not modify the position: {}", msg);
                });
                tc.send(jay_randr::SetPosition {
                    self_id: randr,
                    output: &args.output,
                    x: t.x,
                    y: t.y,
                });
            }
            OutputCommand::Enable | OutputCommand::Disable => {
                let (enable, name) = match args.command {
                    OutputCommand::Enable => (true, "enable"),
                    _ => (false, "disable"),
                };
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not {} the output: {}", name, msg);
                });
                tc.send(jay_randr::SetEnabled {
                    self_id: randr,
                    output: &args.output,
                    enabled: enable as _,
                });
            }
            OutputCommand::NonDesktop(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not change the non-desktop setting: {}", msg);
                });
                tc.send(jay_randr::SetNonDesktop {
                    self_id: randr,
                    output: &args.output,
                    non_desktop: a.setting as _,
                });
            }
            OutputCommand::Vrr(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not change the VRR setting: {}", msg);
                });
                let parse_rate = |rate: &str| {
                    if rate.eq_ignore_ascii_case("none") {
                        f64::INFINITY
                    } else {
                        match f64::from_str(rate) {
                            Ok(v) => v,
                            Err(e) => {
                                fatal!("Could not parse rate: {}", ErrorFmt(e));
                            }
                        }
                    }
                };
                match a.command {
                    VrrCommand::SetMode(a) => {
                        let mode = match a.mode {
                            VrrModeArg::Never => VrrMode::NEVER,
                            VrrModeArg::Always => VrrMode::ALWAYS,
                            VrrModeArg::Variant1 => VrrMode::VARIANT_1,
                            VrrModeArg::Variant2 => VrrMode::VARIANT_2,
                            VrrModeArg::Variant3 => VrrMode::VARIANT_3,
                        };
                        tc.send(jay_randr::SetVrrMode {
                            self_id: randr,
                            output: &args.output,
                            mode: mode.0,
                        });
                    }
                    VrrCommand::SetCursorHz(r) => {
                        let hz = parse_rate(&r.rate);
                        tc.send(jay_randr::SetVrrCursorHz {
                            self_id: randr,
                            output: &args.output,
                            hz,
                        });
                    }
                }
            }
            OutputCommand::Tearing(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not change the tearing setting: {}", msg);
                });
                match a.command {
                    TearingCommand::SetMode(a) => {
                        let mode = match a.mode {
                            TearingModeArg::Never => TearingMode::NEVER,
                            TearingModeArg::Always => TearingMode::ALWAYS,
                            TearingModeArg::Variant1 => TearingMode::VARIANT_1,
                            TearingModeArg::Variant2 => TearingMode::VARIANT_2,
                            TearingModeArg::Variant3 => TearingMode::VARIANT_3,
                        };
                        tc.send(jay_randr::SetTearingMode {
                            self_id: randr,
                            output: &args.output,
                            mode: mode.0,
                        });
                    }
                }
            }
            OutputCommand::Format(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not change the framebuffer format: {}", msg);
                });
                match a.command {
                    FormatCommand::Set { format } => {
                        tc.send(jay_randr::SetFbFormat {
                            self_id: randr,
                            output: &args.output,
                            format: format.name,
                        });
                    }
                }
            }
            OutputCommand::Colors(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not change the colors: {}", msg);
                });
                match a.command {
                    ColorsCommand::Set { color_space, eotf } => {
                        tc.send(jay_randr::SetColors {
                            self_id: randr,
                            output: &args.output,
                            color_space: &color_space,
                            eotf: &eotf,
                        });
                    }
                }
            }
            OutputCommand::Brightness(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not change the brightness: {}", msg);
                });
                match a.brightness {
                    Brightness::Default => {
                        tc.send(jay_randr::UnsetBrightness {
                            self_id: randr,
                            output: &args.output,
                        });
                    }
                    Brightness::Lux(lux) => {
                        tc.send(jay_randr::SetBrightness {
                            self_id: randr,
                            output: &args.output,
                            lux,
                        });
                    }
                }
            }
            OutputCommand::BlendSpace(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!("Could not set the blend space: {}", msg);
                });
                tc.send(jay_randr::SetBlendSpace {
                    self_id: randr,
                    output: &args.output,
                    blend_space: &a.blend_space,
                });
            }
            OutputCommand::UseNativeGamut(a) => {
                self.handle_error(randr, move |msg| {
                    eprintln!(
                        "Could not change whether the compositor uses the native gamut: {}",
                        msg,
                    );
                });
                tc.send(jay_randr::SetUseNativeGamut {
                    self_id: randr,
                    output: &args.output,
                    use_native_gamut: a.use_native_gamut as _,
                });
            }
        }
        tc.round_trip().await;
    }

    async fn virtual_output(self: &Rc<Self>, randr: JayRandrId, args: VirtualOutputArgs) {
        let tc = &self.tc;
        match args.command {
            VirtualOutputCommand::Create(t) => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not create a virtual output: {}", msg);
                });
                tc.send(jay_randr::CreateVirtualOutput {
                    self_id: randr,
                    name: &t.name,
                });
            }
            VirtualOutputCommand::Remove(t) => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not remove a virtual output: {}", msg);
                });
                tc.send(jay_randr::RemoveVirtualOutput {
                    self_id: randr,
                    name: &t.name,
                });
            }
        }
        tc.round_trip().await;
    }

    async fn card(self: &Rc<Self>, randr: JayRandrId, args: CardArgs) {
        let tc = &self.tc;
        match args.command {
            CardCommand::Primary => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not set the primary device: {}", msg);
                });
                tc.send(jay_randr::MakeRenderDevice {
                    self_id: randr,
                    dev: &args.card,
                });
            }
            CardCommand::Api(api) => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not set the API: {}", msg);
                });
                let api = match &api.cmd {
                    ApiCmd::OpenGl => "opengl",
                    ApiCmd::Vulkan => "vulkan",
                };
                tc.send(jay_randr::SetApi {
                    self_id: randr,
                    dev: &args.card,
                    api,
                });
            }
            CardCommand::DirectScanout(ds) => {
                self.handle_error(randr, |msg| {
                    eprintln!("Could not modify direct-scanout behavior: {}", msg);
                });
                tc.send(jay_randr::SetDirectScanout {
                    self_id: randr,
                    dev: &args.card,
                    enabled: match ds.cmd {
                        DirectScanoutCmd::Enable => 1,
                        DirectScanoutCmd::Disable => 0,
                    },
                });
            }
            CardCommand::Timing(ts) => match ts.cmd {
                TimingCmd::SetFlipMargin(sfm) => {
                    self.handle_error(randr, |msg| {
                        eprintln!("Could not modify the flip margin: {}", msg);
                    });
                    tc.send(jay_randr::SetFlipMargin {
                        self_id: randr,
                        dev: &args.card,
                        margin_ns: (sfm.margin_ms * 1_000_000.0) as u64,
                    });
                }
            },
        }
        tc.round_trip().await;
    }

    async fn show(self: &Rc<Self>, global: &GlobalArgs, randr: JayRandrId, args: ShowArgs) {
        let mut data = self.get(randr).await;
        data.drm_devices.sort_by(|l, r| l.devnode.cmp(&r.devnode));
        if global.json {
            self.show_json(&data);
        } else {
            self.show_text(&data, &args);
        }
    }

    fn show_json(&self, data: &Data) {
        let mut drm_devices = Vec::new();
        for dev in &data.drm_devices {
            let mut connectors: Vec<_> = data
                .connectors
                .iter()
                .filter(|c| c.drm_device == Some(dev.id))
                .collect();
            connectors.sort_by_key(|c| &c.name);
            drm_devices.push(JsonDrmDevice {
                devnode: &dev.devnode,
                syspath: &dev.syspath,
                vendor: dev.vendor,
                vendor_name: &dev.vendor_name,
                model: dev.model,
                model_name: &dev.model_name,
                gfx_api: &dev.gfx_api,
                render_device: dev.render_device,
                connectors: connectors.into_iter().map(make_json_connector).collect(),
            });
        }
        let mut unbound: Vec<_> = data
            .connectors
            .iter()
            .filter(|c| c.drm_device.is_none())
            .collect();
        unbound.sort_by_key(|c| &c.name);
        let json = JsonRandrData {
            drm_devices,
            unbound_connectors: unbound.into_iter().map(make_json_connector).collect(),
        };
        jsonl(&json);
    }

    fn show_text(&self, data: &Data, args: &ShowArgs) {
        if data.drm_devices.is_not_empty() {
            println!("drm devices:");
        }
        for dev in &data.drm_devices {
            self.print_drm_device(dev);
            println!("    connectors:");
            let mut connectors: Vec<_> = data
                .connectors
                .iter()
                .filter(|c| c.drm_device == Some(dev.id))
                .collect();
            connectors.sort_by_key(|c| &c.name);
            for c in connectors {
                self.print_connector(c, args.modes, args.formats);
            }
        }
        {
            let mut connectors: Vec<_> = data
                .connectors
                .iter()
                .filter(|c| c.drm_device.is_none())
                .collect();
            if connectors.is_not_empty() {
                connectors.sort_by_key(|c| &c.name);
                println!("unbound connectors:");
                for c in connectors {
                    self.print_connector(c, args.modes, args.formats);
                }
            }
        }
    }

    fn print_drm_device(&self, dev: &Device) {
        println!("  {}:", dev.devnode);
        println!("    model: {} {}", dev.vendor_name, dev.model_name);
        println!("    pci-id: {:x}:{:x}", dev.vendor, dev.model);
        println!("    syspath: {}", dev.syspath);
        println!("    api: {}", dev.gfx_api);
        if dev.render_device {
            println!("    primary device");
        }
    }

    fn print_connector(&self, connector: &Connector, modes: bool, formats: bool) {
        println!("      {}:", connector.name);
        if !connector.enabled {
            println!("        disabled");
        }
        let Some(o) = &connector.output else {
            if connector.enabled {
                println!("        disconnected");
            }
            return;
        };
        println!("        product: {}", o.product);
        println!("        manufacturer: {}", o.manufacturer);
        println!("        serial number: {}", o.serial_number);
        println!(
            "        physical size: {}mm x {}mm",
            o.width_mm, o.height_mm
        );
        if o.non_desktop {
            if connector.enabled {
                println!("        non-desktop");
            }
            return;
        }
        println!("        VRR capable: {}", o.vrr_capable);
        if o.vrr_capable {
            println!("        VRR enabled: {}", o.vrr_enabled);
            let mode_str;
            let mode = match o.vrr_mode {
                VrrMode::NEVER => "never",
                VrrMode::ALWAYS => "always",
                VrrMode::VARIANT_1 => "variant1",
                VrrMode::VARIANT_2 => "variant2",
                VrrMode::VARIANT_3 => "variant3",
                _ => {
                    mode_str = format!("unknown ({})", o.vrr_mode.0);
                    &mode_str
                }
            };
            println!("        VRR mode: {}", mode);
            if let Some(hz) = o.vrr_cursor_hz {
                println!("        VRR cursor hz: {}", hz);
            }
        }
        {
            let mode_str;
            let mode = match o.tearing_mode {
                TearingMode::NEVER => "never",
                TearingMode::ALWAYS => "always",
                TearingMode::VARIANT_1 => "variant1",
                TearingMode::VARIANT_2 => "variant2",
                TearingMode::VARIANT_3 => "variant3",
                _ => {
                    mode_str = format!("unknown ({})", o.tearing_mode.0);
                    &mode_str
                }
            };
            println!("        Tearing mode: {}", mode);
        }
        println!("        position: {} x {}", o.x, o.y);
        println!("        logical size: {} x {}", o.width, o.height);
        if let Some(mode) = &o.current_mode {
            print!("        mode: ");
            self.print_mode(mode, false);
        }
        if let Some(format) = &o.format {
            if format != XRGB8888.name {
                println!("        format: {format}");
            }
        }
        if o.scale != 1.0 {
            println!("        scale: {}", o.scale);
        }
        if o.transform != Transform::None {
            println!("        transform: {}", o.transform.text());
        }
        if let Some(flip_margin_ns) = o.flip_margin_ns {
            println!(
                "        flip margin: {:?}",
                Duration::from_nanos(flip_margin_ns)
            );
        }
        if o.supported_color_spaces.is_not_empty() {
            println!("        color spaces:");
            let handle_cs = |cs: &str| {
                let current = match Some(cs) == o.current_color_space.as_deref() {
                    false => "",
                    true => " (current)",
                };
                println!("          {cs}{current}");
            };
            handle_cs("default");
            o.supported_color_spaces.iter().for_each(|cs| handle_cs(cs));
        }
        if o.supported_eotfs.is_not_empty() {
            println!("        eotfs:");
            let handle_tf = |tf: &str| {
                let current = match Some(tf) == o.current_eotf.as_deref() {
                    false => "",
                    true => " (current)",
                };
                println!("          {tf}{current}");
            };
            handle_tf("default");
            o.supported_eotfs.iter().for_each(|tf| handle_tf(tf));
        }
        if let Some((min, max)) = o.brightness_range {
            println!("        min brightness: {:>10.4} cd/m^2", min);
            println!("        max brightness: {:>10.4} cd/m^2", max);
        } else {
            println!("        max brightness: {:>10.4} cd/m^2 (implied)", 80.0);
        }
        if let Some(lux) = o.brightness {
            println!("        brightness:     {:>10.4} cd/m^2", lux);
        }
        if let Some(bs) = &o.blend_space {
            println!("        blend space: {bs}");
        }
        if let Some(p) = &o.native_gamut {
            println!(
                "        native gamut:{}",
                fmt::from_fn(|f| {
                    if o.use_native_gamut {
                        f.write_str(" (used for default color space)")?;
                    }
                    Ok(())
                }),
            );
            println!(
                "          red:  {:.6} {:.6} green: {:.6} {:.6}",
                p.r.0.0, p.r.1.0, p.g.0.0, p.g.1.0
            );
            println!(
                "          blue: {:.6} {:.6} white: {:.6} {:.6}",
                p.b.0.0, p.b.1.0, p.wp.0.0, p.wp.1.0
            );
        }
        if o.arbitrary_modes {
            println!("        supports arbitrary modes");
        }
        if o.modes.is_not_empty() && modes {
            println!("        modes:");
            for mode in &o.modes {
                print!("          ");
                self.print_mode(mode, true);
            }
        }
        if o.formats.is_not_empty() && formats {
            println!("        formats:");
            for format in &o.formats {
                println!("          {format}");
            }
        }
    }

    fn print_mode(&self, m: &Mode, print_current: bool) {
        print!("{}", m);
        if print_current && m.current {
            print!(" (current)");
        }
        println!();
    }

    async fn get(self: &Rc<Self>, randr: JayRandrId) -> Data {
        let tc = &self.tc;
        tc.send(jay_randr::Get { self_id: randr });
        let data = Rc::new(RefCell::new(Data::default()));
        jay_randr::Global::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            data.default_api = msg.default_gfx_api.to_string();
        });
        jay_randr::DrmDevice::handle(tc, randr, data.clone(), |data, msg| {
            data.borrow_mut().drm_devices.push(Device {
                id: msg.id,
                syspath: msg.syspath.to_string(),
                devnode: msg.devnode.to_string(),
                vendor: msg.vendor,
                vendor_name: msg.vendor_name.to_string(),
                model: msg.model,
                model_name: msg.model_name.to_string(),
                gfx_api: msg.gfx_api.to_string(),
                render_device: msg.render_device != 0,
            });
        });
        jay_randr::Connector::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            data.connectors.push(Connector {
                _id: msg.id,
                drm_device: (msg.drm_device != 0).then_some(msg.drm_device),
                name: msg.name.to_string(),
                enabled: msg.enabled != 0,
                output: None,
            });
        });
        jay_randr::Output::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            c.output = Some(Output {
                scale: Scale::from_wl(msg.scale).to_f64(),
                width: msg.width,
                height: msg.height,
                x: msg.x,
                y: msg.y,
                transform: Transform::from_wl(msg.transform).unwrap(),
                manufacturer: msg.manufacturer.to_string(),
                product: msg.product.to_string(),
                serial_number: msg.serial_number.to_string(),
                width_mm: msg.width_mm,
                height_mm: msg.height_mm,
                ..Default::default()
            });
        });
        jay_randr::NonDesktopOutput::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            c.output = Some(Output {
                scale: 1.0,
                manufacturer: msg.manufacturer.to_string(),
                product: msg.product.to_string(),
                serial_number: msg.serial_number.to_string(),
                width_mm: msg.width_mm,
                height_mm: msg.height_mm,
                non_desktop: true,
                ..Default::default()
            });
        });
        jay_randr::VrrState::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.vrr_capable = msg.capable != 0;
            output.vrr_enabled = msg.enabled != 0;
            output.vrr_mode = VrrMode(msg.mode);
        });
        jay_randr::VrrCursorHz::handle(tc, randr, data.clone(), move |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.vrr_cursor_hz = Some(msg.hz);
        });
        jay_randr::TearingState::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.tearing_mode = TearingMode(msg.mode);
        });
        jay_randr::FbFormat::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.formats.push(msg.name.to_string());
            if msg.current != 0 {
                output.format = Some(msg.name.to_string());
            }
        });
        jay_randr::FlipMargin::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.flip_margin_ns = Some(msg.margin_ns);
        });
        jay_randr::Mode::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let o = c.output.as_mut().unwrap();
            let mode = Mode {
                width: msg.width,
                height: msg.height,
                refresh_rate_millihz: msg.refresh_rate_millihz,
                current: msg.current != 0,
            };
            if mode.current {
                o.current_mode = Some(mode);
            }
            o.modes.push(mode);
        });
        jay_randr::SupportedColorSpace::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output
                .supported_color_spaces
                .push(msg.color_space.to_string());
        });
        jay_randr::CurrentColorSpace::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.current_color_space = Some(msg.color_space.to_string());
        });
        jay_randr::SupportedEotf::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.supported_eotfs.push(msg.eotf.to_string());
        });
        jay_randr::CurrentEotf::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.current_eotf = Some(msg.eotf.to_string());
        });
        jay_randr::BrightnessRange::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.brightness_range = Some((msg.min, msg.max));
        });
        jay_randr::Brightness::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.brightness = Some(msg.lux);
        });
        jay_randr::BlendSpace::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.blend_space = Some(msg.blend_space.to_string());
        });
        jay_randr::NativeGamut::handle(tc, randr, data.clone(), |data, msg| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            let primaries = Primaries {
                r: (F64(msg.r_x), F64(msg.r_y)),
                g: (F64(msg.g_x), F64(msg.g_y)),
                b: (F64(msg.b_x), F64(msg.b_y)),
                wp: (F64(msg.w_x), F64(msg.w_y)),
            };
            output.native_gamut = Some(primaries);
        });
        jay_randr::UseNativeGamut::handle(tc, randr, data.clone(), |data, _| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.use_native_gamut = true;
        });
        jay_randr::ArbitraryModes::handle(tc, randr, data.clone(), |data, _| {
            let mut data = data.borrow_mut();
            let c = data.connectors.last_mut().unwrap();
            let output = c.output.as_mut().unwrap();
            output.arbitrary_modes = true;
        });
        tc.round_trip().await;
        data.borrow_mut().clone()
    }
}

fn make_json_connector(c: &Connector) -> JsonConnector<'_> {
    let output = c.output.as_ref().map(|o| {
        let modes = o
            .modes
            .iter()
            .map(|m| JsonMode {
                width: m.width,
                height: m.height,
                refresh_rate_millihz: m.refresh_rate_millihz,
                current: m.current,
            })
            .collect();
        let formats = o.formats.iter().map(|f| f.as_str()).collect();
        JsonOutput {
            product: &o.product,
            manufacturer: &o.manufacturer,
            serial_number: &o.serial_number,
            width_mm: o.width_mm,
            height_mm: o.height_mm,
            non_desktop: o.non_desktop,
            scale: o.scale,
            x: o.x,
            y: o.y,
            width: o.width,
            height: o.height,
            transform: o.transform.text(),
            mode: o.current_mode.map(|m| JsonMode {
                width: m.width,
                height: m.height,
                refresh_rate_millihz: m.refresh_rate_millihz,
                current: m.current,
            }),
            format: o.format.as_deref(),
            vrr_capable: o.vrr_capable,
            vrr_enabled: o.vrr_enabled,
            vrr_mode: JsonVrrMode(o.vrr_mode),
            vrr_cursor_hz: o.vrr_cursor_hz,
            tearing_mode: JsonTearingMode(o.tearing_mode),
            flip_margin_ns: o.flip_margin_ns,
            supported_color_spaces: o
                .supported_color_spaces
                .iter()
                .map(|s| s.as_str())
                .collect(),
            current_color_space: o.current_color_space.as_deref(),
            supported_eotfs: o.supported_eotfs.iter().map(|s| s.as_str()).collect(),
            current_eotf: o.current_eotf.as_deref(),
            min_brightness: o.brightness_range.map(|(min, _)| min),
            max_brightness: o.brightness_range.map(|(_, max)| max),
            brightness: o.brightness,
            blend_space: o.blend_space.as_deref(),
            native_gamut: o.native_gamut.as_ref().map(|p| JsonPrimaries {
                r_x: p.r.0.0,
                r_y: p.r.1.0,
                g_x: p.g.0.0,
                g_y: p.g.1.0,
                b_x: p.b.0.0,
                b_y: p.b.1.0,
                w_x: p.wp.0.0,
                w_y: p.wp.1.0,
            }),
            use_native_gamut: o.use_native_gamut,
            arbitrary_modes: o.arbitrary_modes,
            modes,
            formats,
        }
    });
    JsonConnector {
        name: &c.name,
        enabled: c.enabled,
        output,
    }
}