1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
|
/**
* libcoopgamma -- Library for interfacing with cooperative gamma servers
* Copyright (C) 2016 Mattias Andrée (maandree@kth.se)
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "libcoopgamma.h"
#include <sys/socket.h>
#include <sys/wait.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(__GNUC__)
# define NAME_OF_THE_PROCESS (argv0)
char* argv0 __attribute__((weak)) = "libcoopgamma";
#else
# define NAME_OF_THE_PROCESS ("libcoopgamma")
#endif
#define SUBBUF \
(buf ? buf + off : NULL)
#define MARSHAL_PROLOGUE \
char* restrict buf = vbuf; \
size_t off = 0;
#define UNMARSHAL_PROLOGUE \
const char* restrict buf = vbuf; \
size_t off = 0;
#define MARSHAL_EPILOGUE \
return off
#define UNMARSHAL_EPILOGUE \
return *np = off, LIBCOOPGAMMA_SUCCESS
#define marshal_prim(datum, type) \
((buf != NULL ? *(type*)(buf + off) = (datum) : 0), off += sizeof(type))
#define unmarshal_prim(datum, type) \
((datum) = *(type*)(buf + off), off += sizeof(type))
#define marshal_version(version) \
marshal_prim(version, int)
#define unmarshal_version(version) \
do \
{ \
int version__; \
unmarshal_prim(version__, int); \
if (version__ < version) \
return LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE; \
if (version__ > version) \
return LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE; \
} \
while (0)
#define marshal_buffer(data, n) \
((buf != NULL ? (memcpy(buf + off, (data), (n)), 0) : 0), off += (n))
#define unmarshal_buffer(data, n) \
do \
{ \
(data) = malloc(n); \
if ((data) == NULL) \
return LIBCOOPGAMMA_ERRNO_SET; \
memcpy((data), buf + off, (n)); \
off += (n); \
} \
while (0)
#define marshal_string(datum) \
((datum) == NULL ? marshal_prim(1, char) : \
(marshal_prim(0, char), marshal_buffer(datum, strlen(datum) + 1)))
#define unmarshal_string(datum) \
do \
{ \
char nonnull__; \
unmarshal_prim(nonnull__, char); \
if (nonnull__) \
unmarshal_buffer((datum), strlen(buf + off) + 1); \
else \
(datum) = NULL; \
} \
while (0)
#define copy_errno(ctx) \
((errno == 0) ? NULL : \
((ctx)->error.number = (uint64_t)errno, \
(ctx)->error.custom = 0, \
(ctx)->error.server_side = 0, \
free((ctx)->error.description), \
(ctx)->error.description = NULL))
/**
* Initialise a `libcoopgamma_ramps8_t`, `libcoopgamma_ramps16_t`, `libcoopgamma_ramps32_t`,
* `libcoopgamma_ramps64_t`, `libcoopgamma_rampsf_t`, or `libcoopgamma_rampsd_t`
*
* `this->red_size`, `this->green_size`, and `this->blue_size` must already be set
*
* @param this The record to initialise
* @para width The `sizeof(*(this->red))`
* @return Zero on success, -1 on error
*/
int (libcoopgamma_ramps_initialise)(void* restrict this, size_t width)
{
libcoopgamma_ramps8_t* restrict this8 = (libcoopgamma_ramps8_t* restrict)this;
this8->red = this8->green = this8->blue = NULL;
this8->red = malloc((this8->red_size + this8->green_size + this8->blue_size) * width);
if (this8->red == NULL)
return -1;
this8->green = this8->red + this8->red_size * width;
this8->blue = this8->green + this8->green_size * width;
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_ramps8_t`, `libcoopgamma_ramps16_t`,
* `libcoopgamma_ramps32_t`, `libcoopgamma_ramps64_t`, `libcoopgamma_rampsf_t`, or
* `libcoopgamma_rampsd_t`, the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_ramps_initialise`
* or failed call to `libcoopgamma_ramps_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_ramps_destroy(void* restrict this)
{
libcoopgamma_ramps8_t* restrict this8 = (libcoopgamma_ramps8_t* restrict)this;
free(this8->red);
this8->red = this8->green = this8->blue = NULL;
}
/**
* Marshal a `libcoopgamma_ramps8_t`, `libcoopgamma_ramps16_t`, `libcoopgamma_ramps32_t`,
* `libcoopgamma_ramps64_t`, `libcoopgamma_rampsf_t`, or `libcoopgamma_rampsd_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @param width The `sizeof(*(this->red))`
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t (libcoopgamma_ramps_marshal)(const void* restrict this, void* restrict vbuf, size_t width)
{
libcoopgamma_ramps8_t* restrict this8 = (libcoopgamma_ramps8_t* restrict)this;
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_RAMPS_VERSION);
marshal_prim(this8->red_size, size_t);
marshal_prim(this8->green_size, size_t);
marshal_prim(this8->blue_size, size_t);
marshal_buffer(this8->red, (this8->red_size + this8->green_size + this8->blue_size) * width);
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_ramps8_t`, `libcoopgamma_ramps16_t`, `libcoopgamma_ramps32_t`,
* `libcoopgamma_ramps64_t`, `libcoopgamma_rampsf_t`, or `libcoopgamma_rampsd_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @param width The `sizeof(*(this->red))`
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int (libcoopgamma_ramps_unmarshal)(void* restrict this, const void* restrict vbuf,
size_t* restrict np, size_t width)
{
libcoopgamma_ramps8_t* restrict this8 = (libcoopgamma_ramps8_t* restrict)this;
UNMARSHAL_PROLOGUE;
unmarshal_version(LIBCOOPGAMMA_RAMPS_VERSION);
unmarshal_prim(this8->red_size, size_t);
unmarshal_prim(this8->green_size, size_t);
unmarshal_prim(this8->blue_size, size_t);
unmarshal_buffer(this8->red, (this8->red_size + this8->green_size + this8->blue_size) * width);
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_filter_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_filter_initialise(libcoopgamma_filter_t* restrict this)
{
memset(this, 0, sizeof(*this));
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_filter_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_filter_initialise`
* or failed call to `libcoopgamma_filter_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_filter_destroy(libcoopgamma_filter_t* restrict this)
{
free(this->crtc);
free(this->class);
free(this->ramps.u8.red);
memset(this, 0, sizeof(*this));
}
/**
* Marshal a `libcoopgamma_filter_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_filter_marshal(const libcoopgamma_filter_t* restrict this, void* restrict vbuf)
{
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_FILTER_VERSION);
marshal_version(LIBCOOPGAMMA_DEPTH_VERSION);
marshal_version(LIBCOOPGAMMA_LIFESPAN_VERSION);
marshal_prim(this->depth, libcoopgamma_depth_t);
marshal_prim(this->priority, int64_t);
marshal_string(this->crtc);
marshal_string(this->class);
marshal_prim(this->lifespan, libcoopgamma_lifespan_t);
switch (this->depth)
{
case LIBCOOPGAMMA_UINT8: off += libcoopgamma_ramps_marshal(&(this->ramps.u8), SUBBUF); break;
case LIBCOOPGAMMA_UINT16: off += libcoopgamma_ramps_marshal(&(this->ramps.u16), SUBBUF); break;
case LIBCOOPGAMMA_UINT32: off += libcoopgamma_ramps_marshal(&(this->ramps.u32), SUBBUF); break;
case LIBCOOPGAMMA_UINT64: off += libcoopgamma_ramps_marshal(&(this->ramps.u64), SUBBUF); break;
case LIBCOOPGAMMA_FLOAT: off += libcoopgamma_ramps_marshal(&(this->ramps.f), SUBBUF); break;
case LIBCOOPGAMMA_DOUBLE: off += libcoopgamma_ramps_marshal(&(this->ramps.d), SUBBUF); break;
default:
break;
}
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_filter_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_filter_unmarshal(libcoopgamma_filter_t* restrict this,
const void* restrict vbuf, size_t* restrict np)
{
int r = LIBCOOPGAMMA_SUCCESS;
size_t n = 0;
UNMARSHAL_PROLOGUE;
memset(this, 0, sizeof(*this));
unmarshal_version(LIBCOOPGAMMA_FILTER_VERSION);
unmarshal_version(LIBCOOPGAMMA_DEPTH_VERSION);
unmarshal_version(LIBCOOPGAMMA_LIFESPAN_VERSION);
unmarshal_prim(this->depth, libcoopgamma_depth_t);
unmarshal_prim(this->priority, int64_t);
unmarshal_string(this->crtc);
unmarshal_string(this->class);
unmarshal_prim(this->lifespan, libcoopgamma_lifespan_t);
switch (this->depth)
{
case LIBCOOPGAMMA_UINT8: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u8), SUBBUF, &n); break;
case LIBCOOPGAMMA_UINT16: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u16), SUBBUF, &n); break;
case LIBCOOPGAMMA_UINT32: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u32), SUBBUF, &n); break;
case LIBCOOPGAMMA_UINT64: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u64), SUBBUF, &n); break;
case LIBCOOPGAMMA_FLOAT: r = libcoopgamma_ramps_unmarshal(&(this->ramps.f), SUBBUF, &n); break;
case LIBCOOPGAMMA_DOUBLE: r = libcoopgamma_ramps_unmarshal(&(this->ramps.d), SUBBUF, &n); break;
default:
break;
}
if (r != LIBCOOPGAMMA_SUCCESS)
return r;
off += n;
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_crtc_info_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_crtc_info_initialise(libcoopgamma_crtc_info_t* restrict this)
{
memset(this, 0, sizeof(*this));
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_crtc_info_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_crtc_info_initialise`
* or failed call to `libcoopgamma_crtc_info_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_crtc_info_destroy(libcoopgamma_crtc_info_t* restrict this)
{
(void) this;
}
/**
* Marshal a `libcoopgamma_crtc_info_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_crtc_info_marshal(const libcoopgamma_crtc_info_t* restrict this, void* restrict vbuf)
{
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_CRTC_INFO_VERSION);
marshal_version(LIBCOOPGAMMA_DEPTH_VERSION);
marshal_version(LIBCOOPGAMMA_SUPPORT_VERSION);
marshal_prim(this->cooperative, int);
marshal_prim(this->depth, libcoopgamma_depth_t);
marshal_prim(this->red_size, size_t);
marshal_prim(this->green_size, size_t);
marshal_prim(this->blue_size, size_t);
marshal_prim(this->supported, libcoopgamma_support_t);
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_crtc_info_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_crtc_info_unmarshal(libcoopgamma_crtc_info_t* restrict this,
const void* restrict vbuf, size_t* restrict np)
{
UNMARSHAL_PROLOGUE;
unmarshal_version(LIBCOOPGAMMA_CRTC_INFO_VERSION);
unmarshal_version(LIBCOOPGAMMA_DEPTH_VERSION);
unmarshal_version(LIBCOOPGAMMA_SUPPORT_VERSION);
unmarshal_prim(this->cooperative, int);
unmarshal_prim(this->depth, libcoopgamma_depth_t);
unmarshal_prim(this->red_size, size_t);
unmarshal_prim(this->green_size, size_t);
unmarshal_prim(this->blue_size, size_t);
unmarshal_prim(this->supported, libcoopgamma_support_t);
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_filter_query_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_filter_query_initialise(libcoopgamma_filter_query_t* restrict this)
{
this->crtc = NULL;
this->coalesce = 0;
this->high_priority = INT64_MAX;
this->low_priority = INT64_MIN;
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_filter_query_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_filter_query_initialise`
* or failed call to `libcoopgamma_filter_query_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_filter_query_destroy(libcoopgamma_filter_query_t* restrict this)
{
free(this->crtc), this->crtc = NULL;
}
/**
* Marshal a `libcoopgamma_filter_query_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_filter_query_marshal(const libcoopgamma_filter_query_t* restrict this, void* restrict vbuf)
{
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_FILTER_QUERY_VERSION);
marshal_string(this->crtc);
marshal_prim(this->coalesce, int);
marshal_prim(this->high_priority, int64_t);
marshal_prim(this->low_priority, int64_t);
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_filter_query_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_filter_query_unmarshal(libcoopgamma_filter_query_t* restrict this,
const void* restrict vbuf, size_t* restrict np)
{
UNMARSHAL_PROLOGUE;
this->crtc = NULL;
unmarshal_version(LIBCOOPGAMMA_FILTER_QUERY_VERSION);
unmarshal_string(this->crtc);
unmarshal_prim(this->coalesce, int);
unmarshal_prim(this->high_priority, int64_t);
unmarshal_prim(this->low_priority, int64_t);
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_queried_filter_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_queried_filter_initialise(libcoopgamma_queried_filter_t* restrict this)
{
memset(this, 0, sizeof(*this));
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_queried_filter_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_queried_filter_initialise`
* or failed call to `libcoopgamma_queried_filter_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_queried_filter_destroy(libcoopgamma_queried_filter_t* restrict this)
{
free(this->class), this->class = NULL;
libcoopgamma_ramps_destroy(&(this->ramps.u8));
}
/**
* Marshal a `libcoopgamma_queried_filter_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @param depth The type used of ramp stops
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_queried_filter_marshal(const libcoopgamma_queried_filter_t* restrict this,
void* restrict vbuf, libcoopgamma_depth_t depth)
{
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_QUERIED_FILTER_VERSION);
marshal_prim(this->priority, int64_t);
marshal_string(this->class);
switch (depth)
{
case LIBCOOPGAMMA_UINT8: off += libcoopgamma_ramps_marshal(&(this->ramps.u8), SUBBUF); break;
case LIBCOOPGAMMA_UINT16: off += libcoopgamma_ramps_marshal(&(this->ramps.u16), SUBBUF); break;
case LIBCOOPGAMMA_UINT32: off += libcoopgamma_ramps_marshal(&(this->ramps.u32), SUBBUF); break;
case LIBCOOPGAMMA_UINT64: off += libcoopgamma_ramps_marshal(&(this->ramps.u64), SUBBUF); break;
case LIBCOOPGAMMA_FLOAT: off += libcoopgamma_ramps_marshal(&(this->ramps.f), SUBBUF); break;
case LIBCOOPGAMMA_DOUBLE: off += libcoopgamma_ramps_marshal(&(this->ramps.d), SUBBUF); break;
default:
break;
}
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_queried_filter_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @param depth The type used of ramp stops
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_queried_filter_unmarshal(libcoopgamma_queried_filter_t* restrict this,
const void* restrict vbuf, size_t* restrict np,
libcoopgamma_depth_t depth)
{
int r = LIBCOOPGAMMA_SUCCESS;
size_t n = 0;
UNMARSHAL_PROLOGUE;
memset(this, 0, sizeof(*this));
unmarshal_version(LIBCOOPGAMMA_QUERIED_FILTER_VERSION);
unmarshal_prim(this->priority, int64_t);
unmarshal_string(this->class);
switch (depth)
{
case LIBCOOPGAMMA_UINT8: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u8), SUBBUF, &n); break;
case LIBCOOPGAMMA_UINT16: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u16), SUBBUF, &n); break;
case LIBCOOPGAMMA_UINT32: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u32), SUBBUF, &n); break;
case LIBCOOPGAMMA_UINT64: r = libcoopgamma_ramps_unmarshal(&(this->ramps.u64), SUBBUF, &n); break;
case LIBCOOPGAMMA_FLOAT: r = libcoopgamma_ramps_unmarshal(&(this->ramps.f), SUBBUF, &n); break;
case LIBCOOPGAMMA_DOUBLE: r = libcoopgamma_ramps_unmarshal(&(this->ramps.d), SUBBUF, &n); break;
default:
break;
}
if (r != LIBCOOPGAMMA_SUCCESS)
return r;
off += n;
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_filter_table_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_filter_table_initialise(libcoopgamma_filter_table_t* restrict this)
{
memset(this, 0, sizeof(*this));
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_filter_table_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_filter_table_initialise`
* or failed call to `libcoopgamma_filter_table_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_filter_table_destroy(libcoopgamma_filter_table_t* restrict this)
{
while (this->filter_count)
libcoopgamma_queried_filter_destroy(this->filters + --(this->filter_count));
free(this->filters), this->filters = NULL;
}
/**
* Marshal a `libcoopgamma_filter_table_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_filter_table_marshal(const libcoopgamma_filter_table_t* restrict this, void* restrict vbuf)
{
size_t i;
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_FILTER_TABLE_VERSION);
marshal_version(LIBCOOPGAMMA_DEPTH_VERSION);
marshal_prim(this->depth, libcoopgamma_depth_t);
marshal_prim(this->red_size, size_t);
marshal_prim(this->green_size, size_t);
marshal_prim(this->blue_size, size_t);
marshal_prim(this->filter_count, size_t);
for (i = 0; i < this->filter_count; i++)
off += libcoopgamma_queried_filter_marshal(this->filters + i, SUBBUF, this->depth);
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_filter_table_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_filter_table_unmarshal(libcoopgamma_filter_table_t* restrict this,
const void* restrict vbuf, size_t* restrict np)
{
size_t i, n, fn;
int r;
UNMARSHAL_PROLOGUE;
this->filter_count = 0;
this->filters = NULL;
unmarshal_version(LIBCOOPGAMMA_FILTER_TABLE_VERSION);
unmarshal_version(LIBCOOPGAMMA_DEPTH_VERSION);
unmarshal_prim(this->depth, libcoopgamma_depth_t);
unmarshal_prim(this->red_size, size_t);
unmarshal_prim(this->green_size, size_t);
unmarshal_prim(this->blue_size, size_t);
unmarshal_prim(fn, size_t);
this->filters = malloc(fn * sizeof(*(this->filters)));
if (this->filters == NULL)
return LIBCOOPGAMMA_ERRNO_SET;
for (i = 0; i < fn; i++)
{
r = libcoopgamma_queried_filter_unmarshal(this->filters + i, SUBBUF, &n, this->depth);
if (r != LIBCOOPGAMMA_SUCCESS)
return r;
off += n;
this->filter_count += 1;
}
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_error_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_error_initialise(libcoopgamma_error_t* restrict this)
{
this->number = 0;
this->custom = 0;
this->description = NULL;
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_error_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_error_initialise`
* or failed call to `libcoopgamma_error_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_error_destroy(libcoopgamma_error_t* restrict this)
{
free(this->description), this->description = NULL;
}
/**
* Marshal a `libcoopgamma_error_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_error_marshal(const libcoopgamma_error_t* restrict this, void* restrict vbuf)
{
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_ERROR_VERSION);
marshal_prim(this->number, uint64_t);
marshal_prim(this->custom, int);
marshal_prim(this->server_side, int);
marshal_string(this->description);
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_error_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_error_unmarshal(libcoopgamma_error_t* restrict this,
const void* restrict vbuf, size_t* restrict np)
{
UNMARSHAL_PROLOGUE;
this->description = NULL;
unmarshal_version(LIBCOOPGAMMA_ERROR_VERSION);
unmarshal_prim(this->number, uint64_t);
unmarshal_prim(this->custom, int);
unmarshal_string(this->description);
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_context_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_context_initialise(libcoopgamma_context_t* restrict this)
{
this->fd = -1;
libcoopgamma_error_initialise(&(this->error));
this->message_id = 0;
this->outbound = NULL;
this->outbound_head = 0;
this->outbound_tail = 0;
this->outbound_size = 0;
this->inbound = NULL;
this->inbound_head = 0;
this->inbound_tail = 0;
this->inbound_size = 0;
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_context_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_context_initialise`
* or failed call to `libcoopgamma_context_unmarshal`
*
* @param this The record to destroy
* @param disconnect Disconnect from the server?
*/
void libcoopgamma_context_destroy(libcoopgamma_context_t* restrict this, int disconnect)
{
if (disconnect && (this->fd >= 0))
{
shutdown(this->fd, SHUT_RDWR);
close(this->fd);
}
this->fd = -1;
libcoopgamma_error_destroy(&(this->error));
free(this->outbound), this->outbound = NULL;
free(this->inbound), this->inbound = NULL;
}
/**
* Marshal a `libcoopgamma_context_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_context_marshal(const libcoopgamma_context_t* restrict this, void* restrict vbuf)
{
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_CONTEXT_VERSION);
marshal_prim(this->fd, int);
off += libcoopgamma_error_marshal(&(this->error), SUBBUF);
marshal_prim(this->message_id, uint32_t);
marshal_prim(this->outbound_head - this->outbound_tail, size_t);
marshal_buffer(this->outbound + this->outbound_head, this->outbound_head - this->outbound_tail);
marshal_prim(this->inbound_head - this->inbound_tail, size_t);
marshal_buffer(this->inbound + this->inbound_head, this->inbound_head - this->inbound_tail);
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_context_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_context_unmarshal(libcoopgamma_context_t* restrict this,
const void* restrict vbuf, size_t* restrict np)
{
size_t n;
int r;
UNMARSHAL_PROLOGUE;
memset(this, 0, sizeof(*this));
unmarshal_version(LIBCOOPGAMMA_CONTEXT_VERSION);
unmarshal_prim(this->fd, int);
r = libcoopgamma_error_unmarshal(&(this->error), SUBBUF, &n);
if (r != LIBCOOPGAMMA_SUCCESS)
return r;
off += n;
unmarshal_prim(this->message_id, uint32_t);
unmarshal_prim(this->inbound_head, size_t);
this->outbound_size = this->outbound_head;
unmarshal_buffer(this->outbound, this->outbound_head);
this->inbound_size = this->inbound_head;
unmarshal_buffer(this->inbound, this->inbound_head);
UNMARSHAL_EPILOGUE;
}
/**
* Initialise a `libcoopgamma_async_context_t`
*
* @param this The record to initialise
* @return Zero on success, -1 on error
*/
int libcoopgamma_async_context_initialise(libcoopgamma_async_context_t* restrict this)
{
this->message_id = 0;
return 0;
}
/**
* Release all resources allocated to a `libcoopgamma_async_context_t`,
* the allocation of the record itself is not freed
*
* Always call this function after failed call to `libcoopgamma_async_context_initialise`
* or failed call to `libcoopgamma_async_context_unmarshal`
*
* @param this The record to destroy
*/
void libcoopgamma_async_context_destroy(libcoopgamma_async_context_t* restrict this)
{
(void) this;
}
/**
* Marshal a `libcoopgamma_async_context_t` into a buffer
*
* @param this The record to marshal
* @param vbuf The output buffer, `NULL` to only measure
* how large this buffer has to be
* @return The number of marshalled bytes, or if `buf == NULL`,
* how many bytes would be marshalled if `buf != NULL`
*/
size_t libcoopgamma_async_context_marshal(const libcoopgamma_async_context_t* restrict this,
void* restrict vbuf)
{
MARSHAL_PROLOGUE;
marshal_version(LIBCOOPGAMMA_ASYNC_CONTEXT_VERSION);
marshal_prim(this->message_id, uint32_t);
MARSHAL_EPILOGUE;
}
/**
* Unmarshal a `libcoopgamma_async_context_t` from a buffer
*
* @param this The output paramater for unmarshalled record
* @param vbuf The buffer with the marshalled record
* @param np Output parameter for the number of unmarshalled bytes, undefined on failure
* @return `LIBCOOPGAMMA_SUCCESS` (0), `LIBCOOPGAMMA_INCOMPATIBLE_DOWNGRADE`,
* `LIBCOOPGAMMA_INCOMPATIBLE_UPGRADE`, or `LIBCOOPGAMMA_ERRNO_SET`
*/
int libcoopgamma_async_context_unmarshal(libcoopgamma_async_context_t* restrict this,
const void* restrict vbuf, size_t* restrict np)
{
UNMARSHAL_PROLOGUE;
unmarshal_version(LIBCOOPGAMMA_ASYNC_CONTEXT_VERSION);
unmarshal_prim(this->message_id, uint32_t);
UNMARSHAL_EPILOGUE;
}
/**
* List all recognised adjustment method
*
* SIGCHLD must not be ignored or blocked
*
* @return A `NULL`-terminated list of names. You should only free
* the outer pointer, inner pointers are subpointers of the
* outer pointer and cannot be freed. `NULL` on error.
*/
char** libcoopgamma_get_methods(void)
{
char num[5]; /* The size is base on the fact that we have limited `n` in the loop below */
char** methods = NULL;
char** rc;
char* buffer;
int n = 0, saved_errno;
size_t size;
methods = malloc(4 * sizeof(*methods));
if (methods == NULL)
goto fail;
for (n = 0; n < 10000 /* just to be safe */; n++)
{
char* method;
if ((n >= 4) && ((n & -n) == n))
{
void* new = realloc(methods, (n << 1) * sizeof(*methods));
if (new == NULL)
goto fail;
methods = new;
}
sprintf(num, "%i", n);
if (libcoopgamma_get_method_and_site(num, NULL, &method, NULL))
goto fail;
if (!strcmp(method, num))
{
free(method);
break;
}
methods[n] = method;
size += strlen(method) + 1;
}
rc = malloc((n + 1) * sizeof(char*) + size);
if (rc == NULL)
goto fail;
buffer = ((char*)rc) + (n + 1) * sizeof(char*);
rc[n] = NULL;
while (n--)
{
buffer = stpcpy(buffer, methods[n]) + 1;
free(methods[n]);
}
free(methods);
return rc;
fail:
saved_errno = errno;
while (n--)
free(methods[n]);
free(methods);
errno = saved_errno;
return NULL;
}
/**
* Run coopgammad with -q or -qq and return the response
*
* SIGCHLD must not be ignored or blocked
*
* @param method The adjustment method, `NULL` for automatic
* @param site The site, `NULL` for automatic
* @param arg "-q" or "-qq", which shall be passed to coopgammad?
* @return The output of coopgammad, `NULL` on error. This
* will be NUL-terminated and will not contain any
* other `NUL` bytes.
*/
static char* libcoopgamma_query(const char* restrict method, const char* restrict site, const char* restrict arg)
{
const char* (args[7]) = {"coopgammad", arg};
size_t i = 2, n = 0, size = 0;
int pipe_rw[2] = { -1, -1 };
pid_t pid;
int saved_errno, status;
char* msg = NULL;
ssize_t got;
if (method != NULL) args[i++] = "-m", args[i++] = method;
if (site != NULL) args[i++] = "-s", args[i++] = site;
args[i] = NULL;
if (pipe(pipe_rw) < 0)
goto fail;
switch ((pid = fork()))
{
case -1:
goto fail;
case 0:
/* Child */
close(pipe_rw[0]);
if (pipe_rw[1] != STDOUT_FILENO)
{
close(STDOUT_FILENO);
if (dup2(pipe_rw[1], STDOUT_FILENO) < 0)
goto fail_child;
close(pipe_rw[1]);
}
execvp("coopgammad", (char* const*)(args));
fail_child:
saved_errno = errno;
perror(NAME_OF_THE_PROCESS);
if (write(STDOUT_FILENO, &saved_errno, sizeof(int)) != sizeof(int))
perror(NAME_OF_THE_PROCESS);
exit(1);
default:
/* Parent */
close(pipe_rw[1]), pipe_rw[1] = -1;
for (;;)
{
if (n == size)
{
void* new = realloc(msg, size = (n ? (n << 1) : 256));
if (new == NULL)
goto fail;
msg = new;
}
got = read(pipe_rw[0], msg + n, size - n);
if (got < 0)
{
if (errno == EINTR)
continue;
goto fail;
}
else if (got == 0)
break;
n += (size_t)got;
}
close(pipe_rw[0]), pipe_rw[0] = -1;
if (waitpid(pid, &status, 0) < 0)
goto fail;
if (status)
{
errno = EPIPE;
if ((n == sizeof(int)) && (*(int*)msg != 0))
errno = *(int*)msg;
}
break;
}
if (n == size)
{
void* new = realloc(msg, n + 1);
if (new == NULL)
goto fail;
msg = new;
}
msg[n] = '\0';
if (strchr(msg, '\0') != msg + n)
{
errno = EBADMSG;
goto fail;
}
return msg;
fail:
saved_errno = errno;
if (pipe_rw[0] >= 0) close(pipe_rw[0]);
if (pipe_rw[1] >= 0) close(pipe_rw[1]);
free(msg);
errno = saved_errno;
return NULL;
}
/**
* Get the adjustment method and site
*
* SIGCHLD must not be ignored or blocked
*
* @param method The adjustment method, `NULL` for automatic
* @param site The site, `NULL` for automatic
* @param methodp Output pointer for the selected adjustment method,
* which cannot be `NULL`. It is safe to call
* this function with this parameter set to `NULL`.
* @param sitep Output pointer for the selected site, which will
* be `NULL` the method only supports one site or if
* `site == NULL` and no site can be selected
* automatically. It is safe to call this function
* with this parameter set to `NULL`.
* @return Zero on success, -1 on error
*/
int libcoopgamma_get_method_and_site(const char* restrict method, const char* restrict site,
char** restrict methodp, char** restrict sitep)
{
int saved_errno;
char* raw;
char* p;
char* q;
raw = libcoopgamma_query(method, site, "-q");
if (raw == NULL)
return -1;
if (methodp != NULL) *methodp = NULL;
if (sitep != NULL) *sitep = NULL;
p = strchr(raw, '\n');
if (p == NULL)
{
errno = EBADMSG;
goto fail;
}
*p++ = '\0';
if (methodp != NULL)
{
*methodp = malloc(strlen(raw) + 1);
if (*methodp == NULL)
goto fail;
strcpy(*methodp, raw);
}
if ((site != NULL) && *(q = strchr(p, '\0') - 1))
{
if (*q != '\n')
{
errno = EBADMSG;
goto fail;
}
*q = '\0';
*sitep = malloc(strlen(p) + 1);
if (*sitep == NULL)
goto fail;
strcpy(*sitep, p);
}
free(raw);
return 0;
fail:
saved_errno = errno;
if (methodp != NULL)
free(*methodp), *methodp = NULL;
free(raw);
errno = saved_errno;
return -1;
}
/**
* Get the PID file of the coopgamma server
*
* SIGCHLD must not be ignored or blocked
*
* @param method The adjustment method, `NULL` for automatic
* @param site The site, `NULL` for automatic
* @return The pathname of the server's PID file, `NULL` on error
* or if there server does not use PID files. The later
* case is detected by checking that `errno` is set to 0.
*/
char* libcoopgamma_get_pid_file(const char* restrict method, const char* restrict site)
{
char* path;
size_t n;
path = libcoopgamma_get_socket_file(method, site);
if (path == NULL)
return NULL;
n = strlen(path);
if (n < 7 || strcmp(path + n - 7, ".socket"))
{
free(path);
errno = EBADMSG;
return NULL;
}
strcpy(path + n - 7, ".pid");
return path;
}
/**
* Get the socket file of the coopgamma server
*
* SIGCHLD must not be ignored or blocked
*
* @param method The adjustment method, `NULL` for automatic
* @param site The site, `NULL` for automatic
* @return The pathname of the server's socket, `NULL` on error
* or if there server does have its own socket. The later
* case is detected by checking that `errno` is set to 0,
* and is the case when communicating with a server in a
* multi-server display server like mds.
*/
char* libcoopgamma_get_socket_file(const char* restrict method, const char* restrict site)
{
int saved_errno;
char* raw;
char* p;
raw = libcoopgamma_query(method, site, "-qq");
if (raw == NULL)
return NULL;
p = strchr(raw, '\0') - 1;
if (*p != '\n')
{
errno = EBADMSG;
goto fail;
}
*p = '\0';
if (*raw == '\0')
{
errno = EBADMSG;
goto fail;
}
return raw;
fail:
saved_errno = errno;
free(raw);
errno = saved_errno;
return NULL;
}
/**
* Connect to a coopgamma server, and start it if necessary
*
* Use `libcoopgamma_context_destroy` to disconnect
*
* SIGCHLD must not be ignored or blocked
*
* @param method The adjustment method, `NULL` for automatic
* @param site The site, `NULL` for automatic
* @param ctx The state of the library, must be initialised
* @return Zero on success, -1 on error. On error, `errno` is set
* to 0 if the server could not be initialised.
*/
int libcoopgamma_connect(const char* restrict method, const char* restrict site,
libcoopgamma_context_t* restrict ctx)
{
}
/**
* Send all pending outbound data
*
* If this function or another function that sends a request
* to the server fails with EINTR, call this function to
* complete the transfer. The `async` parameter will always
* be in a properly configured state if a function fails
* with EINTR.
*
* @param ctx The state of the library, must be initialised
* @return Zero on success, -1 on error
*/
int libcoopgamma_flush(libcoopgamma_context_t* restrict ctx)
{
ssize_t sent;
size_t chunksize = ctx->outbound_head - ctx->outbound_tail;
size_t sendsize;
while (ctx->outbound_tail < ctx->outbound_head)
{
sendsize = ctx->outbound_head - ctx->outbound_tail;
sendsize = sendsize < chunksize ? sendsize : chunksize;
sent = send(ctx->fd, ctx->outbound + ctx->outbound_tail, sendsize, 0);
if (sent < 0)
{
if (errno != EMSGSIZE)
return -1;
if ((chunksize >>= 1) == 0)
return -1;
continue;
}
ctx->outbound_tail += (size_t)sent;
}
return 0;
}
/**
* Send a message to the server and wait for response
*
* @param resp:char** Output parameter for the response,
* will be NUL-terminated
* @param ctx:libcoopgamma_context_t* The state of the library
* @param payload:void* Data to append to the end of the message
* @param payload_size:size_t Byte-size of `payload`
* @param format:string-literal Message formatting string
* @param ... Message formatting arguments
*
* On error, the macro goes to `fail`.
*/
#define SEND_MESSAGE(ctx, payload, payload_size, format, ...) \
do \
{ \
ssize_t n__; \
char* msg__; \
snprintf(NULL, 0, format "%zn", __VA_ARGS__, &n__); \
msg__ = malloc((size_t)n__ + (payload_size)); \
if (msg__ == NULL) \
goto fail; \
sprintf(msg__, format, __VA_ARGS__); \
if ((payload) != NULL) \
memcpy(msg__ + n__, (payload), (payload_size)); \
if (send_message((ctx), msg__, (size_t)n__ + (payload_size)) < 0) \
goto fail; \
free(msg__); \
} \
while (0)
/**
* Send a message to the server and wait for response
*
* @param ctx The state of the library
* @param msg The message to send
* @param n The length of `msg`
* @return Zero on success, -1 on error
*/
static int send_message(libcoopgamma_context_t* restrict ctx, char* msg, size_t n)
{
if (ctx->outbound_head == ctx->outbound_tail)
{
free(ctx->outbound);
ctx->outbound = msg;
ctx->outbound_tail = 0;
ctx->outbound_head = n;
ctx->outbound_size = n;
}
else
{
if (ctx->outbound_head + n > ctx->outbound_size)
{
memmove(ctx->outbound, ctx->outbound + ctx->outbound_tail, ctx->outbound_head -= ctx->outbound_tail);
ctx->outbound_tail = 0;
}
if (ctx->outbound_head + n > ctx->outbound_size)
{
void* new = realloc(ctx->outbound, ctx->outbound_head + n);
if (new == NULL)
{
int saved_errno = errno;
free(msg);
errno = saved_errno;
return -1;
}
ctx->outbound = new;
ctx->outbound_size = ctx->outbound_head + n;
}
memcpy(ctx->outbound + ctx->outbound_head, msg, n);
ctx->outbound_head += n;
ctx->message_id += 1;
free(msg);
}
return libcoopgamma_flush(ctx);
}
static char* next_line(libcoopgamma_context_t* restrict ctx)
{
}
/**
* List all available CRTC:s, send request part
*
* Cannot be used before connecting to the server
*
* @param ctx The state of the library, must be connected
* @param async Information about the request, that is needed to
* identify and parse the response, is stored here
* @return Zero on success, -1 on error
*/
int libcoopgamma_get_crtcs_send(libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
async->message_id = ctx->message_id;
SEND_MESSAGE(ctx, NULL, 0,
"Command: enumerate-crtcs\n"
"Message ID: %" PRIu32 "\n"
"\n",
ctx->message_id);
return 0;
fail:
copy_errno(ctx);
return -1;
}
/**
* List all available CRTC:s, receive response part
*
* @param ctx The state of the library, must be connected
* @param async Information about the request
* @return A `NULL`-terminated list of names. You should only free
* the outer pointer, inner pointers are subpointers of the
* outer pointer and cannot be freed. `NULL` on error, in
* which case `ctx->error` (rather than `errno`) is read
* for information about the error.
*/
char** libcoopgamma_get_crtcs_recv(libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
}
/**
* Retrieve information about a CRTC:s gamma ramps, send request part
*
* Cannot be used before connecting to the server
*
* @param crtc The name of the CRTC
* @param ctx The state of the library, must be connected
* @param async Information about the request, that is needed to
* identify and parse the response, is stored here
* @return Zero on success, -1 on error
*/
int libcoopgamma_get_gamma_info_send(const char* crtc, libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
if ((crtc == NULL) || strchr(crtc, '\n'))
{
errno = EINVAL;
goto fail;
}
async->message_id = ctx->message_id;
SEND_MESSAGE(ctx, NULL, 0,
"Command: get-gamma-info\n"
"Message ID: %" PRIu32 "\n"
"CRTC: %s\n"
"\n",
ctx->message_id, crtc);
return 0;
fail:
copy_errno(ctx);
return 0;
}
/**
* Retrieve information about a CRTC:s gamma ramps, receive response part
*
* @param info Output parameter for the information, must be initialised
* @param ctx The state of the library, must be connected
* @param async Information about the request
* @return Zero on success, -1 on error, in which case `ctx->error`
* (rather than `errno`) is read for information about the error
*/
int libcoopgamma_get_gamma_info_recv(libcoopgamma_crtc_info_t* restrict info,
libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
}
/**
* Retrieve the current gamma ramp adjustments, send request part
*
* Cannot be used before connecting to the server
*
* @param query The query to send
* @param ctx The state of the library, must be connected
* @param async Information about the request, that is needed to
* identify and parse the response, is stored here
* @return Zero on success, -1 on error
*/
int libcoopgamma_get_gamma_send(libcoopgamma_filter_query_t* restrict query,
libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
if ((query == NULL) || (query->crtc == NULL) || strchr(query->crtc, '\n'))
{
errno = EINVAL;
goto fail;
}
async->message_id = ctx->message_id;
SEND_MESSAGE(ctx, NULL, 0,
"Command: get-gamma\n"
"Message ID: %" PRIu32 "\n"
"CRTC: %s\n"
"Coalesce: %s\n"
"High priority: %" PRIi64 "\n"
"Low priority: %" PRIi64 "\n"
"\n",
ctx->message_id, query->crtc, query->coalesce ? "yes" : "no",
query->high_priority, query->low_priority);
return 0;
fail:
copy_errno(ctx);
return -1;
}
/**
* Retrieve the current gamma ramp adjustments, receive response part
*
* @param table Output for the response, must be initialised
* @param ctx The state of the library, must be connected
* @param async Information about the request, that is needed to
* identify and parse the response, is stored here
* @return Zero on success, -1 on error, in which case `ctx->error`
* (rather than `errno`) is read for information about the error
*/
int libcoopgamma_get_gamma_recv(libcoopgamma_filter_table_t* restrict table,
libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
}
/**
* Apply, update, or remove a gamma ramp adjustment, send request part
*
* Cannot be used before connecting to the server
*
* @param filter The filter to apply, update, or remove, gamma ramp meta-data must match the CRTC's
* @param depth The datatype for the stops in the gamma ramps, must match the CRTC's
* @param ctx The state of the library, must be connected
* @param async Information about the request, that is needed to
* identify and parse the response, is stored here
* @return Zero on success, -1 on error
*/
int libcoopgamma_set_gamma_send(libcoopgamma_filter_t* restrict filter, libcoopgamma_depth_t depth,
libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
const void* payload = NULL;
const char* lifespan;
char priority[sizeof("Priority: \n") + 3 * sizeof(int64_t)] = {'\0'};
char length [sizeof("Length: \n") + 3 * sizeof(size_t) ] = {'\0'};
size_t payload_size = 0, stopwidth = 0;
if ((filter == NULL) || (filter->crtc == NULL) || strchr(filter->crtc, '\n') ||
(filter->class == NULL) || strchr(filter->class, '\n'))
{
errno = EINVAL;
goto fail;
}
switch (filter->lifespan)
{
case LIBCOOPGAMMA_REMOVE: lifespan = "remove"; break;
case LIBCOOPGAMMA_UNTIL_DEATH: lifespan = "until-death"; break;
case LIBCOOPGAMMA_UNTIL_REMOVAL: lifespan = "until-removal"; break;
default:
errno = EINVAL;
goto fail;
}
switch (depth)
{
case LIBCOOPGAMMA_FLOAT: stopwidth = sizeof(float); break;
case LIBCOOPGAMMA_DOUBLE: stopwidth = sizeof(double); break;
default:
if ((depth <= 0) || ((depth & 7) != 0))
{
errno = EINVAL;
goto fail;
}
stopwidth = depth / 8;
break;
}
if (filter->lifespan != LIBCOOPGAMMA_REMOVE)
{
payload_size = filter->ramps.u8.red_size;
payload_size += filter->ramps.u8.green_size;
payload_size += filter->ramps.u8.blue_size;
payload_size *= stopwidth;
payload = filter->ramps.u8.red;
sprintf(priority, "Priority: %" PRIi64 "\n", filter->priority);
sprintf(length, "Length: %zu\n", payload_size);
}
async->message_id = ctx->message_id;
SEND_MESSAGE(ctx, payload, payload_size,
"Command: set-gamma\n"
"Message ID: %" PRIu32 "\n"
"CRTC: %s\n"
"Class: %s\n"
"Lifespan: %s\n"
"%s"
"%s"
"\n",
ctx->message_id, filter->crtc, filter->class, lifespan,
priority, length);
return 0;
fail:
copy_errno(ctx);
return -1;
}
/**
* Apply, update, or remove a gamma ramp adjustment, receive response part
*
* @param ctx The state of the library, must be connected
* @param async Information about the request, that is needed to
* identify and parse the response, is stored here
* @return Zero on success, -1 on error, in which case `ctx->error`
* (rather than `errno`) is read for information about the error
*/
int libcoopgamma_set_gamma_recv(libcoopgamma_context_t* restrict ctx,
libcoopgamma_async_context_t* restrict async)
{
}
|