File: randr.go

package info (click to toggle)
golang-github-linuxdeepin-go-x11-client 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,784 kB
  • sloc: python: 944; sh: 38; makefile: 17
file content (1449 lines) | stat: -rw-r--r-- 29,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

package randr

import (
	"errors"
	"math"

	"github.com/linuxdeepin/go-x11-client/ext/render"

	"github.com/linuxdeepin/go-x11-client"
)

const (
	StatusSuccess           = 0
	StatusInvalidConfigTime = 1
	StatusInvalidTime       = 2
	StatusFailed            = 3
)

// #WREQ
func encodeQueryVersion(clientMajorVersion, clientMinorVersion uint32) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(clientMajorVersion).
		Write4b(clientMinorVersion).
		End()
	return
}

type QueryVersionReply struct {
	ServerMajorVersion uint32
	ServerMinorVersion uint32
}

func readQueryVersionReply(r *x.Reader, v *QueryVersionReply) error {
	if !r.RemainAtLeast4b(4) {
		return x.ErrDataLenShort
	}
	r.ReadPad(8)

	v.ServerMajorVersion = r.Read4b()

	v.ServerMinorVersion = r.Read4b() // 4

	return nil
}

// #WREQ
func encodeSetScreenConfig(window x.Window, timestamp x.Timestamp, configTimestamp x.Timestamp,
	sizeID uint16, rotation uint16, rate uint16) (b x.RequestBody) {
	b.AddBlock(5).
		Write4b(uint32(window)).
		Write4b(uint32(timestamp)).
		Write4b(uint32(configTimestamp)). // 3
		Write2b(uint16(sizeID)).
		Write2b(rotation).
		Write2b(rate).
		WritePad(2). // 5
		End()
	return
}

type SetScreenConfigReply struct {
	Status          uint8
	NewTimestamp    x.Timestamp
	ConfigTimestamp x.Timestamp
	Root            x.Window
	SubpixelOrder   uint16
}

func readSetScreenConfigReply(r *x.Reader, v *SetScreenConfigReply) error {
	if !r.RemainAtLeast4b(6) {
		return x.ErrDataLenShort
	}
	v.Status, _ = r.ReadReplyHeader()

	v.NewTimestamp = x.Timestamp(r.Read4b())

	v.ConfigTimestamp = x.Timestamp(r.Read4b()) // 4

	v.Root = x.Window(r.Read4b())

	v.SubpixelOrder = r.Read2b() // 6
	return nil
}

// #WREQ
func encodeGetScreenInfo(window x.Window) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(window))
	return
}

type GetScreenInfoReply struct {
	Rotations       uint8
	Root            x.Window
	Timestamp       x.Timestamp
	ConfigTimestamp x.Timestamp
	NSizes          uint16
	SizeID          uint16
	Rotation        uint16
	Rate            uint16
	NInfo           uint16 // useless
	Sizes           []ScreenSize
	Rates           []RefreshRates
}

func readGetScreenInfoReply(r *x.Reader, v *GetScreenInfoReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	v.Rotations, _ = r.ReadReplyHeader()

	v.Root = x.Window(r.Read4b())

	v.Timestamp = x.Timestamp(r.Read4b())

	v.ConfigTimestamp = x.Timestamp(r.Read4b()) // 5

	v.NSizes = r.Read2b()
	v.SizeID = r.Read2b()

	v.Rotation = r.Read2b()
	v.Rate = r.Read2b()

	v.NInfo = r.Read2b()
	r.ReadPad(2) // 8

	if v.NSizes > 0 {
		if !r.RemainAtLeast4b(2 * int(v.NSizes)) {
			return x.ErrDataLenShort
		}
		v.Sizes = make([]ScreenSize, v.NSizes)
		for i := 0; i < int(v.NSizes); i++ {
			v.Sizes[i] = readScreenSize(r)
		}
	}

	nRefreshRates := int(v.NSizes)
	if nRefreshRates > 0 {
		v.Rates = make([]RefreshRates, nRefreshRates)
		for i := 0; i < nRefreshRates; i++ {
			err := readRefreshRates(r, &v.Rates[i])
			if err != nil {
				return err
			}
		}
	}

	return nil
}

// size: 2 * 4b
type ScreenSize struct {
	Width  uint16 // pixels
	Height uint16

	MWidth  uint16 // millimeters
	MHeight uint16
}

func readScreenSize(r *x.Reader) (v ScreenSize) {
	v.Width = r.Read2b()
	v.Height = r.Read2b()

	v.MWidth = r.Read2b()
	v.MHeight = r.Read2b()
	return
}

// size: ?
type RefreshRates struct {
	NRates uint16
	Rates  []uint16
}

func readRefreshRates(r *x.Reader, v *RefreshRates) error {
	if !r.RemainAtLeast(2) {
		return x.ErrDataLenShort
	}
	v.NRates = r.Read2b()
	if v.NRates > 0 {
		if !r.RemainAtLeast(2 * int(v.NRates)) {
			return x.ErrDataLenShort
		}

		v.Rates = make([]uint16, v.NRates)
		for i := 0; i < int(v.NRates); i++ {
			v.Rates[i] = r.Read2b()
		}
	}
	return nil
}

// #WREQ
func encodeGetScreenSizeRange(window x.Window) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(window)).
		End()
	return
}

type GetScreenSizeRangeReply struct {
	MinWidth  uint16
	MinHeight uint16
	MaxWidth  uint16
	MaxHeight uint16
}

func readGetScreenSizeRangeReply(r *x.Reader, v *GetScreenSizeRangeReply) error {
	if !r.RemainAtLeast4b(4) {
		return x.ErrDataLenShort
	}
	r.ReadReplyHeader()

	v.MinWidth = r.Read2b()
	v.MinHeight = r.Read2b()

	v.MaxWidth = r.Read2b()
	v.MaxHeight = r.Read2b() // 4

	return nil
}

// #WREQ
func encodeSetScreenSize(window x.Window, width, height uint16,
	mmWidth, mmHeight uint32) (b x.RequestBody) {
	b.AddBlock(4).
		Write4b(uint32(window)).
		Write2b(width).
		Write2b(height).
		Write4b(mmWidth).
		Write4b(mmHeight).
		End()
	return
}

// #WREQ
func encodeGetCrtcInfo(crtc Crtc, configTimestamp x.Timestamp) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(crtc)).
		Write4b(uint32(configTimestamp)).
		End()
	return
}

type GetCrtcInfoReply struct {
	Status          uint8
	Timestamp       x.Timestamp
	X               int16
	Y               int16
	Width           uint16
	Height          uint16
	Mode            Mode
	Rotation        uint16
	Rotations       uint16
	Outputs         []Output
	PossibleOutputs []Output
}

func readGetCrtcInfoReply(r *x.Reader, v *GetCrtcInfoReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	v.Status, _ = r.ReadReplyHeader()

	v.Timestamp = x.Timestamp(r.Read4b())

	v.X = int16(r.Read2b())
	v.Y = int16(r.Read2b())

	v.Width = r.Read2b()
	v.Height = r.Read2b() // 5

	v.Mode = Mode(r.Read4b())

	v.Rotation = r.Read2b()
	v.Rotations = r.Read2b()

	outputsLen := int(r.Read2b())
	possibleOutputsLen := int(r.Read2b()) // 8

	if outputsLen > 0 {
		if !r.RemainAtLeast4b(outputsLen) {
			return x.ErrDataLenShort
		}
		v.Outputs = make([]Output, outputsLen)
		for i := 0; i < outputsLen; i++ {
			v.Outputs[i] = Output(r.Read4b())
		}
	}

	if possibleOutputsLen > 0 {
		if !r.RemainAtLeast4b(possibleOutputsLen) {
			return x.ErrDataLenShort
		}
		v.PossibleOutputs = make([]Output, possibleOutputsLen)
		for i := 0; i < possibleOutputsLen; i++ {
			v.PossibleOutputs[i] = Output(r.Read4b())
		}
	}

	return nil
}

// #WREQ
func encodeGetOutputInfo(output Output, configTimestamp x.Timestamp) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(output)).
		Write4b(uint32(configTimestamp)).
		End()
	return
}

type GetOutputInfoReply struct {
	Status        uint8
	Timestamp     x.Timestamp
	Crtc          Crtc
	MmWidth       uint32
	MmHeight      uint32
	Connection    uint8
	SubPixelOrder uint8
	NumPreferred  uint16
	Crtcs         []Crtc
	Modes         []Mode
	Clones        []Output
	Name          string
}

func readGetOutputInfoReply(r *x.Reader, v *GetOutputInfoReply) error {
	if !r.RemainAtLeast4b(9) {
		return x.ErrDataLenShort
	}
	v.Status, _ = r.ReadReplyHeader()

	v.Timestamp = x.Timestamp(r.Read4b())

	v.Crtc = Crtc(r.Read4b())

	v.MmWidth = r.Read4b() // 5

	v.MmHeight = r.Read4b()

	v.Connection = r.Read1b()
	v.SubPixelOrder = r.Read1b()
	crtcsLen := int(r.Read2b())

	modesLen := int(r.Read2b())
	v.NumPreferred = r.Read2b() // 8

	if int(v.NumPreferred) > modesLen {
		return errors.New("numPreferred > modesLen")
	}

	clonesLen := int(r.Read2b())
	nameLen := int(r.Read2b()) // 9

	if crtcsLen > 0 {
		if !r.RemainAtLeast4b(crtcsLen) {
			return x.ErrDataLenShort
		}
		v.Crtcs = make([]Crtc, crtcsLen)
		for i := 0; i < crtcsLen; i++ {
			v.Crtcs[i] = Crtc(r.Read4b())
		}
	}

	if modesLen > 0 {
		if !r.RemainAtLeast4b(modesLen) {
			return x.ErrDataLenShort
		}
		v.Modes = make([]Mode, modesLen)
		for i := 0; i < modesLen; i++ {
			v.Modes[i] = Mode(r.Read4b())
		}
	}

	if clonesLen > 0 {
		if !r.RemainAtLeast4b(clonesLen) {
			return x.ErrDataLenShort
		}
		v.Clones = make([]Output, clonesLen)
		for i := 0; i < clonesLen; i++ {
			v.Clones[i] = Output(r.Read4b())
		}
	}

	var err error
	v.Name, err = r.ReadString(nameLen)
	return err
}

func (r *GetOutputInfoReply) GetPreferredMode() Mode {
	if r.NumPreferred == 0 || len(r.Modes) == 0 {
		return 0
	}
	return r.Modes[r.NumPreferred-1]
}

// #WREQ
func encodeGetScreenResources(window x.Window) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(window)).
		End()
	return
}

type GetScreenResourcesReply struct {
	Timestamp       x.Timestamp
	ConfigTimestamp x.Timestamp
	Crtcs           []Crtc
	Outputs         []Output // size: xgb.Pad((int(NumOutputs) * 4))
	Modes           []ModeInfo
}

// size: 8 * 4b
type ModeInfo struct {
	Id         uint32
	Width      uint16
	Height     uint16
	DotClock   uint32
	HSyncStart uint16
	HSyncEnd   uint16
	HTotal     uint16
	HSkew      uint16
	VSyncStart uint16
	VSyncEnd   uint16
	VTotal     uint16
	nameLen    uint16
	Name       string
	ModeFlags  uint32
}

func readModeInfo(r *x.Reader, v *ModeInfo) {
	v.Id = r.Read4b()

	v.Width = r.Read2b()
	v.Height = r.Read2b()

	v.DotClock = r.Read4b()

	v.HSyncStart = r.Read2b()
	v.HSyncEnd = r.Read2b()

	v.HTotal = r.Read2b()
	v.HSkew = r.Read2b() // 5

	v.VSyncStart = r.Read2b()
	v.VSyncEnd = r.Read2b()

	v.VTotal = r.Read2b()
	v.nameLen = r.Read2b()

	v.ModeFlags = r.Read4b() // 8
}

func writeModeInfo(b *x.FixedSizeBuf, v *ModeInfo) {
	b.Write4b(v.Id).
		Write2b(v.Width).
		Write2b(v.Height).
		Write4b(v.DotClock).
		Write2b(v.HSyncStart).
		Write2b(v.HSyncEnd).
		Write2b(v.HTotal).
		Write2b(v.HSkew).
		Write2b(v.VSyncStart).
		Write2b(v.VSyncEnd).
		Write2b(v.VTotal).
		Write2b(v.nameLen).
		Write4b(v.ModeFlags)
}

func readGetScreenResourcesReply(r *x.Reader, v *GetScreenResourcesReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	r.ReadPad(8)

	v.Timestamp = x.Timestamp(r.Read4b())

	v.ConfigTimestamp = x.Timestamp(r.Read4b())

	crtcsLen := int(r.Read2b())
	outputsLen := int(r.Read2b()) // 5

	modesLen := int(r.Read2b())
	// namesLen
	modeNamesLen := int(r.Read2b())

	// unused
	r.ReadPad(8) // 8

	if crtcsLen > 0 {
		if !r.RemainAtLeast4b(crtcsLen) {
			return x.ErrDataLenShort
		}
		v.Crtcs = make([]Crtc, crtcsLen)
		for i := 0; i < crtcsLen; i++ {
			v.Crtcs[i] = Crtc(r.Read4b())
		}
	}

	if outputsLen > 0 {
		if !r.RemainAtLeast4b(outputsLen) {
			return x.ErrDataLenShort
		}
		v.Outputs = make([]Output, outputsLen)
		for i := 0; i < outputsLen; i++ {
			v.Outputs[i] = Output(r.Read4b())
		}
	}

	if modesLen > 0 {
		if !r.RemainAtLeast4b(8 * modesLen) {
			return x.ErrDataLenShort
		}
		v.Modes = make([]ModeInfo, modesLen)
		for i := 0; i < modesLen; i++ {
			readModeInfo(r, &v.Modes[i])
		}
	}

	var b int
	for i := 0; i < modesLen; i++ {
		modeNameLen := int(v.Modes[i].nameLen)
		b += modeNameLen
		var err error
		v.Modes[i].Name, err = r.ReadString(modeNameLen)
		if err != nil {
			return err
		}
	}

	if b != modeNamesLen {
		return errors.New("mode names len not equal")
	}

	return nil
}

// #WREQ
func encodeGetScreenResourcesCurrent(window x.Window) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(window)).
		End()
	return
}

type GetScreenResourcesCurrentReply GetScreenResourcesReply

func readGetScreenResourcesCurrentReply(r *x.Reader, v *GetScreenResourcesCurrentReply) error {
	return readGetScreenResourcesReply(r, (*GetScreenResourcesReply)(v))
}

// #WREQ
func encodeSetCrtcTransform(crtc Crtc, transform *render.Transform, filter string,
	filterParams []render.Fixed) (b x.RequestBody) {

	filter = x.TruncateStr(filter, math.MaxUint16)

	buf := b.AddBlock(11 + x.SizeIn4bWithPad(len(filter)) + len(filterParams)).
		Write4b(uint32(crtc))

	render.WriteTransform(buf, transform) // 10

	buf.Write2b(uint16(len(filter))).
		WritePad(2). // 11
		WriteString(filter).
		WritePad(x.Pad(len(filter)))
	for _, value := range filterParams {
		buf.Write4b(uint32(value)) // +len(filterParams)
	}
	buf.End()
	return
}

// #WREQ
func encodeGetCrtcTransform(crtc Crtc) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(crtc)).
		End()
	return
}

type GetCrtcTransformReply struct {
	PendingTransform render.Transform
	HasTransform     bool
	CurrentTransform render.Transform

	PendingLen     uint16
	PendingNParams uint16

	CurrentLen     uint16
	CurrentNParams uint16

	PendingFilter string
	PendingParams []render.Fixed

	CurrentFilter string
	CurrentParams []render.Fixed
}

func readGetCrtcTransformReply(r *x.Reader, v *GetCrtcTransformReply) error {
	if !r.RemainAtLeast4b(24) {
		return x.ErrDataLenShort
	}
	r.ReadReplyHeader()
	render.ReadTransform(r, &v.PendingTransform) // 11

	v.HasTransform = r.ReadBool()
	r.ReadPad(3) // 12

	render.ReadTransform(r, &v.CurrentTransform) // 21

	r.ReadPad(4) // 22

	v.PendingLen = r.Read2b()
	v.PendingNParams = r.Read2b() // 23

	v.CurrentLen = r.Read2b()
	v.CurrentNParams = r.Read2b() // 24

	var err error
	v.PendingFilter, err = r.ReadStrWithPad(int(v.PendingLen))
	if err != nil {
		return err
	}

	if v.PendingNParams > 0 {
		if !r.RemainAtLeast4b(int(v.PendingNParams)) {
			return x.ErrDataLenShort
		}
		v.PendingParams = make([]render.Fixed, v.PendingNParams)
		for i := 0; i < int(v.PendingNParams); i++ {
			v.PendingParams[i] = render.Fixed(r.Read4b())
		}
	}

	v.CurrentFilter, err = r.ReadStrWithPad(int(v.CurrentLen))
	if err != nil {
		return err
	}

	if v.CurrentNParams > 0 {
		if !r.RemainAtLeast4b(int(v.CurrentNParams)) {
			return x.ErrDataLenShort
		}
		v.CurrentParams = make([]render.Fixed, v.CurrentNParams)
		for i := 0; i < int(v.CurrentNParams); i++ {
			v.CurrentParams[i] = render.Fixed(r.Read4b())
		}
	}
	return nil
}

// size: 6 * 4b
type Panning struct {
	Left, Top, Width, Height                         uint16
	TrackLeft, TrackTop, TrackWidth, TrackHeight     uint16
	BorderLeft, BorderTop, BorderRight, BorderBottom int16
}

func readPanning(r *x.Reader, v *Panning) {
	v.Left = r.Read2b()
	v.Top = r.Read2b()

	v.Width = r.Read2b()
	v.Height = r.Read2b()

	v.TrackLeft = r.Read2b()
	v.TrackTop = r.Read2b()

	v.TrackWidth = r.Read2b()
	v.TrackHeight = r.Read2b()

	v.BorderLeft = int16(r.Read2b())
	v.BorderTop = int16(r.Read2b())
	v.BorderRight = int16(r.Read2b())
	v.BorderBottom = int16(r.Read2b())
}

func writePanning(b *x.FixedSizeBuf, v *Panning) {
	b.Write2b(v.Left)
	b.Write2b(v.Top)

	b.Write2b(v.Width)
	b.Write2b(v.Height)

	b.Write2b(v.TrackLeft)
	b.Write2b(v.TrackTop)

	b.Write2b(v.TrackWidth)
	b.Write2b(v.TrackHeight)

	b.Write2b(uint16(v.BorderLeft))
	b.Write2b(uint16(v.TrackTop))

	b.Write2b(uint16(v.BorderRight))
	b.Write2b(uint16(v.BorderBottom))
}

// #WREQ
func encodeGetPanning(crtc Crtc) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(crtc)).
		End()
	return
}

type GetPanningReply struct {
	Status    uint8
	Timestamp x.Timestamp
	Panning
}

func readGetPanningReply(r *x.Reader, v *GetPanningReply) error {
	if !r.RemainAtLeast4b(9) {
		return x.ErrDataLenShort
	}
	v.Status, _ = r.ReadReplyHeader()

	v.Timestamp = x.Timestamp(r.Read4b()) // 3

	readPanning(r, &v.Panning) // 9
	return nil
}

// #WREQ
func encodeSetPanning(crtc Crtc, timestamp x.Timestamp, panning *Panning) (b x.RequestBody) {
	buf := b.AddBlock(8).
		Write4b(uint32(crtc)).
		Write4b(uint32(timestamp))
	writePanning(buf, panning)
	return
}

type SetPanningReply struct {
	Status    uint8
	Timestamp x.Timestamp
}

func readSetPanningReply(r *x.Reader, v *SetPanningReply) error {
	if !r.RemainAtLeast4b(3) {
		return x.ErrDataLenShort
	}
	v.Status, _ = r.ReadReplyHeader()
	v.Timestamp = x.Timestamp(r.Read4b()) // 3
	return nil
}

// #WREQ
func encodeSetOutputPrimary(window x.Window, output Output) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(window)).
		Write4b(uint32(output)).
		End()
	return
}

// #WREQ
func encodeGetOutputPrimary(window x.Window) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(window)).
		End()
	return
}

type GetOutputPrimaryReply struct {
	Output Output
}

func readGetOutputPrimaryReply(r *x.Reader, v *GetOutputPrimaryReply) error {
	if !r.RemainAtLeast4b(3) {
		return x.ErrDataLenShort
	}
	r.ReadPad(8)

	v.Output = Output(r.Read4b()) // 3

	return nil
}

// #WREQ
func encodeListOutputProperties(output Output) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(output)).
		End()
	return
}

type ListOutputPropertiesReply struct {
	NumAtoms uint16
	Atoms    []x.Atom
}

func readListOutputPropertiesReply(r *x.Reader, v *ListOutputPropertiesReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	r.ReadReplyHeader()

	v.NumAtoms = r.Read2b()
	r.ReadPad(22) // 8

	if v.NumAtoms > 0 {
		if !r.RemainAtLeast4b(int(v.NumAtoms)) {
			return x.ErrDataLenShort
		}
		v.Atoms = make([]x.Atom, v.NumAtoms)
		for i := 0; i < int(v.NumAtoms); i++ {
			v.Atoms[i] = x.Atom(r.Read4b())
		}
	}
	return nil
}

// #WREQ
func encodeQueryOutputProperty(output Output, property x.Atom) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(output)).
		Write4b(uint32(property)).
		End()
	return
}

type QueryOutputPropertyReply struct {
	Pending     bool
	Range       bool
	Immutable   bool
	ValidValues []int32
}

func readQueryOutputPropertyReply(r *x.Reader, v *QueryOutputPropertyReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	_, length := r.ReadReplyHeader()

	v.Pending = r.ReadBool()
	v.Range = r.ReadBool()
	v.Immutable = r.ReadBool()
	r.ReadPad(21) // 8

	if length > 0 {
		if !r.RemainAtLeast4b(int(length)) {
			return x.ErrDataLenShort
		}
		v.ValidValues = make([]int32, length)
		for i := 0; i < int(length); i++ {
			v.ValidValues[i] = int32(r.Read4b())
		}
	}
	return nil
}

// #WREQ
func encodeConfigureOutputProperty(output Output, property x.Atom, pending, range0 bool,
	values []int32) (b x.RequestBody) {
	buf := b.AddBlock(3 + len(values)).
		Write4b(uint32(output)).
		Write4b(uint32(property)).
		WriteBool(pending).
		WriteBool(range0).
		WritePad(2) // 3
	for _, value := range values {
		buf.Write4b(uint32(value))
	}
	return
}

// #WREQ
func encodeChangeOutputProperty(output Output, property x.Atom, type0 x.Atom,
	format, mode uint8, data []byte) (b x.RequestBody) {
	numUnits := uint32(len(data) / (int(format) / 8))
	b.AddBlock(5).
		Write4b(uint32(output)).
		Write4b(uint32(property)).
		Write4b(uint32(type0)). // 3
		Write1b(format).
		Write1b(mode).
		WritePad(2).
		Write4b(numUnits). // 5
		End()
	b.AddBytes(data)
	return
}

// #WREQ
func encodeDeleteOutputProperty(output Output, property x.Atom) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(output)).
		Write4b(uint32(property)).
		End()
	return
}

// #WREQ
func encodeGetOutputProperty(output Output, property, Type x.Atom,
	longOffset, longLength uint32, delete, pending bool) (b x.RequestBody) {
	b.AddBlock(6).
		Write4b(uint32(output)).
		Write4b(uint32(property)).
		Write4b(uint32(Type)).
		Write4b(longOffset).
		Write4b(longLength). // 5
		WriteBool(delete).
		WriteBool(pending).
		WritePad(2). // 6
		End()
	return
}

type GetOutputPropertyReply struct {
	Format     byte
	Type       x.Atom
	BytesAfter uint32
	ValueLen   uint32
	Value      []byte
}

func readGetOutputPropertyReply(r *x.Reader, v *GetOutputPropertyReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	v.Format, _ = r.ReadReplyHeader()

	v.Type = x.Atom(r.Read4b())

	v.BytesAfter = r.Read4b()

	v.ValueLen = r.Read4b() // 5

	// unused
	r.ReadPad(12) // 8

	var err error
	n := int(v.ValueLen) * int(v.Format/8)
	v.Value, err = r.ReadBytes(n)
	return err
}

// #WREQ
func encodeCreateMode(window x.Window, modeInfo *ModeInfo) (b x.RequestBody) {
	modeInfo.Name = x.TruncateStr(modeInfo.Name, math.MaxUint16)
	modeInfo.nameLen = uint16(len(modeInfo.Name))

	buf := b.AddBlock(9).
		Write4b(uint32(window))
	writeModeInfo(buf, modeInfo)
	buf.End()
	b.AddBytes([]byte(modeInfo.Name))
	return
}

type CreateModeReply struct {
	Mode Mode
}

func readCreateModeReply(r *x.Reader, v *CreateModeReply) error {
	if !r.RemainAtLeast4b(3) {
		return x.ErrDataLenShort
	}
	r.ReadReplyHeader()
	v.Mode = Mode(r.Read4b())
	return nil
}

// #WREQ
func encodeDestroyMode(mode Mode) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(mode)).
		End()
	return
}

// #WREQ
func encodeAddOutputMode(output Output, mode Mode) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(output)).
		Write4b(uint32(mode)).
		End()
	return
}

// #WREQ
func encodeDeleteOutputMode(output Output, mode Mode) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(output)).
		Write4b(uint32(mode)).
		End()
	return
}

// #WREQ
func encodeSetCrtcConfig(crtc Crtc, timestamp, configTimestamp x.Timestamp,
	X, y int16, mode Mode, rotation uint16, outputs []Output) (b x.RequestBody) {
	b0 := b.AddBlock(6 + len(outputs)).
		Write4b(uint32(crtc)).
		Write4b(uint32(timestamp)).
		Write4b(uint32(configTimestamp)).
		Write2b(uint16(X)).
		Write2b(uint16(y)).
		Write4b(uint32(mode)).
		Write2b(rotation).
		WritePad(2)

	for _, output := range outputs {
		b0.Write4b(uint32(output))
	}
	b0.End()
	return
}

type SetCrtcConfigReply struct {
	Status    uint8
	Timestamp x.Timestamp
}

func readSetCrtcConfigReply(r *x.Reader, v *SetCrtcConfigReply) error {
	if !r.RemainAtLeast4b(3) {
		return x.ErrDataLenShort
	}
	v.Status, _ = r.ReadReplyHeader()

	v.Timestamp = x.Timestamp(r.Read4b()) // 3

	return nil
}

// #WREQ
func encodeSelectInput(window x.Window, enable uint16) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(window)).
		Write2b(enable).
		WritePad(2).
		End()
	return
}

// #WREQ
func encodeGetCrtcGammaSize(crtc Crtc) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(crtc)).
		End()
	return
}

type GetCrtcGammaSizeReply struct {
	Size uint16
}

func readGetCrtcGammaSizeReply(r *x.Reader, v *GetCrtcGammaSizeReply) error {
	r.Read1b()

	r.Read1b()

	// seq
	r.Read2b()

	// length
	r.Read4b()

	v.Size = r.Read2b()

	r.ReadPad(22)

	return nil
}

// #WREQ
func encodeGetCrtcGamma(crtc Crtc) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(crtc)).
		End()
	return
}

type GetCrtcGammaReply struct {
	Size  uint16
	Red   []uint16
	Green []uint16
	Blue  []uint16
}

func readGetCrtcGammaReply(r *x.Reader, v *GetCrtcGammaReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	r.ReadReplyHeader()

	v.Size = r.Read2b()
	r.ReadPad(22) // 8

	if v.Size > 0 {
		if !r.RemainAtLeast(3 * (2*int(v.Size) + x.Pad(2*int(v.Size)))) {
			return x.ErrDataLenShort
		}
		v.Red = make([]uint16, v.Size)
		for i := 0; i < int(v.Size); i++ {
			v.Red[i] = r.Read2b()
		}
		r.ReadPad(x.Pad(int(v.Size) * 2))

		v.Green = make([]uint16, v.Size)
		for i := 0; i < int(v.Size); i++ {
			v.Green[i] = r.Read2b()
		}
		r.ReadPad(x.Pad(int(v.Size) * 2))

		v.Blue = make([]uint16, v.Size)
		for i := 0; i < int(v.Size); i++ {
			v.Blue[i] = r.Read2b()
		}
	}

	return nil
}

// #WREQ
func encodeSetCrtcGamma(crtc Crtc, red, green, blue []uint16) (b x.RequestBody) {
	size := len(red)
	if len(green) != size {
		panic("assert len(green) != size failed")
	}
	if len(blue) != size {
		panic("assert len(blue) != size failed")
	}

	b0 := b.AddBlock(2 + x.SizeIn4bWithPad(6*size)).
		Write4b(uint32(crtc)).
		Write2b(uint16(size)).
		WritePad(2)

	for _, value := range red {
		b0.Write2b(value)
	}

	for _, value := range green {
		b0.Write2b(value)
	}

	for _, value := range blue {
		b0.Write2b(value)
	}
	b0.WritePad(x.Pad(6 * size))
	return
}

// #WREQ
func encodeGetProviders(window x.Window) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(window)).
		End()
	return
}

type GetProvidersReply struct {
	Timestamp    x.Timestamp
	NumProviders uint16
	Providers    []Provider
}

func readGetProvidersReply(r *x.Reader, v *GetProvidersReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}

	r.ReadReplyHeader()

	v.Timestamp = x.Timestamp(r.Read4b()) // 3

	v.NumProviders = r.Read2b()
	r.ReadPad(18) // 8

	if v.NumProviders > 0 {
		if !r.RemainAtLeast4b(int(v.NumProviders)) {
			return x.ErrDataLenShort
		}
		v.Providers = make([]Provider, v.NumProviders)
		for i := 0; i < int(v.NumProviders); i++ {
			v.Providers[i] = Provider(r.Read4b())
		}
	}

	return nil
}

// #WREQ
func encodeGetProviderInfo(provider Provider, configTimestamp x.Timestamp) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(provider)).
		Write4b(uint32(configTimestamp)).
		End()
	return
}

type GetProviderInfoReply struct {
	Status                 uint8
	Timestamp              x.Timestamp
	Capabilities           uint32
	NumCrtcs               uint16
	NumOutputs             uint16
	NumAssociatedProviders uint16
	NameLen                uint16
	Crtcs                  []Crtc
	Outputs                []Output
	AssociatedProviders    []Provider
	AssociatedCapability   []uint32
	Name                   string
}

func readGetProviderInfoReply(r *x.Reader, v *GetProviderInfoReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	v.Status, _ = r.ReadReplyHeader()

	v.Timestamp = x.Timestamp(r.Read4b())

	v.Capabilities = r.Read4b()

	v.NumCrtcs = r.Read2b()
	v.NumOutputs = r.Read2b()

	v.NumAssociatedProviders = r.Read2b()
	v.NameLen = r.Read2b() // 6

	r.ReadPad(8) // 8

	if v.NumCrtcs > 0 {
		if !r.RemainAtLeast4b(int(v.NumCrtcs)) {
			return x.ErrDataLenShort
		}
		v.Crtcs = make([]Crtc, v.NumCrtcs)
		for i := 0; i < int(v.NumCrtcs); i++ {
			v.Crtcs[i] = Crtc(r.Read4b())
		}
	}

	if v.NumOutputs > 0 {
		if !r.RemainAtLeast4b(int(v.NumOutputs)) {
			return x.ErrDataLenShort
		}
		v.Outputs = make([]Output, v.NumOutputs)
		for i := 0; i < int(v.NumOutputs); i++ {
			v.Outputs[i] = Output(r.Read4b())
		}
	}

	if v.NumAssociatedProviders > 0 {
		if !r.RemainAtLeast4b(2 * int(v.NumAssociatedProviders)) {
			return x.ErrDataLenShort
		}
		v.AssociatedProviders = make([]Provider, v.NumAssociatedProviders)
		for i := 0; i < int(v.NumAssociatedProviders); i++ {
			v.AssociatedProviders[i] = Provider(r.Read4b())
		}

		v.AssociatedCapability = make([]uint32, v.NumAssociatedProviders)
		for i := 0; i < int(v.NumAssociatedProviders); i++ {
			v.AssociatedCapability[i] = r.Read4b()
		}
	}

	if v.NameLen > 0 {
		var err error
		v.Name, err = r.ReadString(int(v.NameLen))
		if err != nil {
			return err
		}
	}

	return nil
}

// #WREQ
func encodeSetProviderOffloadSink(provider, sinkProvider Provider,
	configTimestamp x.Timestamp) (b x.RequestBody) {
	b.AddBlock(3).
		Write4b(uint32(provider)).
		Write4b(uint32(sinkProvider)).
		Write4b(uint32(configTimestamp)).
		End()
	return
}

// #WREQ
func encodeSetProviderOutputSource(provider, sourceProvider Provider,
	configTimestamp x.Timestamp) (b x.RequestBody) {
	b.AddBlock(3).
		Write4b(uint32(provider)).
		Write4b(uint32(sourceProvider)).
		Write4b(uint32(configTimestamp)).
		End()
	return
}

// #WREQ
func encodeListProviderProperties(provider Provider) (b x.RequestBody) {
	b.AddBlock(1).
		Write4b(uint32(provider)).
		End()
	return
}

type ListProviderPropertiesReply struct {
	NumAtoms uint16
	Atoms    []x.Atom
}

func readListProviderPropertiesReply(r *x.Reader, v *ListProviderPropertiesReply) error {
	if !r.RemainAtLeast4b(8) {
		return x.ErrDataLenShort
	}
	r.ReadReplyHeader()

	v.NumAtoms = r.Read2b()
	r.ReadPad(22) // 8

	if v.NumAtoms > 0 {
		if !r.RemainAtLeast4b(int(v.NumAtoms)) {
			return x.ErrDataLenShort
		}
		v.Atoms = make([]x.Atom, v.NumAtoms)
		for i := 0; i < int(v.NumAtoms); i++ {
			v.Atoms[i] = x.Atom(r.Read4b())
		}
	}
	return nil
}

// #WREQ
func encodeGetMonitors(window x.Window, getActive bool) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(window)).
		WriteBool(getActive).
		WritePad(3).
		End()
	return
}

type GetMonitorsReply struct {
	Timestamp x.Timestamp
	NMonitors uint32
	NOutputs  uint32
	Monitors  []MonitorInfo
}

// size: (6 + NOutputs) * 4b
type MonitorInfo struct {
	Name              x.Atom
	Primary           bool
	Automatic         bool
	NOutputs          uint16
	X, Y              int16
	Width, Height     uint16 // pixels
	MmWidth, MmHeight uint32 // in millimeters
	Outputs           []Output
}

func readMonitorInfo(r *x.Reader, v *MonitorInfo) error {
	if !r.RemainAtLeast4b(6) {
		return x.ErrDataLenShort
	}
	v.Name = x.Atom(r.Read4b())

	v.Primary = r.ReadBool()
	v.Automatic = r.ReadBool()
	v.NOutputs = r.Read2b()

	v.X = int16(r.Read2b())
	v.Y = int16(r.Read2b())

	v.Width = r.Read2b()
	v.Height = r.Read2b()

	v.MmWidth = r.Read4b()

	v.MmHeight = r.Read4b() // 6

	if v.NOutputs > 0 {
		v.Outputs = make([]Output, v.NOutputs)
		for i := 0; i < int(v.NOutputs); i++ {
			v.Outputs[i] = Output(r.Read4b())
		}
	}
	return nil
}

func writeMonitorInfo(b *x.FixedSizeBuf, v *MonitorInfo) {
	v.NOutputs = uint16(len(v.Outputs))
	b.Write4b(uint32(v.Name)).
		WriteBool(v.Primary).
		WriteBool(v.Automatic).
		Write2b(v.NOutputs).
		Write2b(uint16(v.X)).
		Write2b(uint16(v.Y)).
		Write2b(v.Width).
		Write2b(v.Height).
		Write4b(v.MmWidth).
		Write4b(v.MmHeight) // 6
	for _, o := range v.Outputs {
		b.Write4b(uint32(o))
	}
}

func readGetMonitorsReply(r *x.Reader, v *GetMonitorsReply) error {
	r.ReadReplyHeader()

	v.Timestamp = x.Timestamp(r.Read4b())

	v.NMonitors = r.Read4b()

	v.NOutputs = r.Read4b()

	r.ReadPad(12)

	if v.NMonitors > 0 {
		v.Monitors = make([]MonitorInfo, v.NMonitors)
		for i := 0; i < int(v.NMonitors); i++ {
			err := readMonitorInfo(r, &v.Monitors[i])
			if err != nil {
				return err
			}
		}
	}
	return nil
}

// #WREQ
func encodeSetMonitor(window x.Window, monitorInfo *MonitorInfo) (b x.RequestBody) {
	sizeIn4b := 7 + len(monitorInfo.Outputs)
	buf := b.AddBlock(sizeIn4b).
		Write4b(uint32(window))
	writeMonitorInfo(buf, monitorInfo)
	buf.End()
	return
}

// #WREQ
func encodeDeleteMonitor(window x.Window, name x.Atom) (b x.RequestBody) {
	b.AddBlock(2).
		Write4b(uint32(window)).
		Write4b(uint32(name)).
		End()
	return
}