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
|
/**
* cg-tools -- Cooperative gamma-enabled tools
* Copyright (C) 2016 Mattias Andrée (maandree@kth.se)
*
* This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#include "arg.h"
#include <libcoopgamma.h>
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* The libcoopgamma context
*/
static libcoopgamma_context_t cg;
/**
* Filter query
*/
static libcoopgamma_filter_query_t query;
/**
* The class of the filter to print
*/
static char* class = NULL;
/**
* Print usage information and exit
*/
void usage(void)
{
fprintf(stderr,
"Usage: %s [-M method] [-S site] [-h high] [-l low] [-f class] -c crtc\n",
argv0);
exit(1);
}
/**
* Initialise the process, specifically
* reset the signal mask and signal handlers
*
* @return Zero on success, -1 on error
*/
static int initialise_proc(void)
{
sigset_t sigmask;
int sig;
for (sig = 1; sig < _NSIG; sig++)
if (signal(sig, SIG_DFL) == SIG_ERR)
if (sig == SIGCHLD)
return -1;
if (sigemptyset(&sigmask) < 0)
return -1;
if (sigprocmask(SIG_SETMASK, &sigmask, NULL) < 0)
return -1;
return 0;
}
/**
* Print, to stdout, a list of all
* recognised adjustment methods
*
* @return Zero on success, -1 on error
*/
static int list_methods(void)
{
char** list;
size_t i;
list = libcoopgamma_get_methods();
if (list == NULL)
return -1;
for (i = 0; list[i]; i++)
printf("%s\n", list[i]);
free(list);
if (fflush(stdout) < 0)
return -1;
return 0;
}
/**
* Print, to stdout, a list of all CRTC:s
*
* A connection to the coopgamma server
* must have been made
*
* @return Zero on success, -1 on error, -2
* on libcoopgamma error
*/
static int list_crtcs(void)
{
char** list;
size_t i;
list = libcoopgamma_get_crtcs_sync(&cg);
if (list == NULL)
return -2;
for (i = 0; list[i]; i++)
printf("%s\n", list[i]);
free(list);
if (fflush(stdout) < 0)
return -1;
return 0;
}
/**
* Print, to stdout, information about
* the selected CRTC
*
* @return Zero on success, -1 on error, -2
* on libcoopgamma error, -3 on error
* with error message already printed
*/
static int print_info(void)
{
libcoopgamma_crtc_info_t info;
libcoopgamma_filter_table_t table;
char* str;
int saved_errno, ret = 0;
size_t i;
if (libcoopgamma_crtc_info_initialise(&info) < 0)
return -1;
if (libcoopgamma_filter_table_initialise(&table) < 0)
{
saved_errno = errno;
libcoopgamma_crtc_info_destroy(&info);
errno = saved_errno;
return -1;
}
if (libcoopgamma_get_gamma_info_sync(query.crtc, &info, &cg) < 0)
goto cg_fail;
printf("Cooperative gamma server running: %s\n",
info.cooperative ? "yes" : "no");
printf("Gamma adjustments supported: %s\n",
info.supported == LIBCOOPGAMMA_MAYBE ? "maybe" : info.supported ? "yes" : "no");
printf("Gamma ramps stops (red green blue): %zu %zu %zu\n",
info.red_size, info.green_size, info.blue_size);
switch (info.depth)
{
case LIBCOOPGAMMA_DOUBLE: str = "double-precision floating-point"; break;
case LIBCOOPGAMMA_FLOAT: str = "single-precision floating-point"; break;
case LIBCOOPGAMMA_UINT8: str = "unsigned 8-bit integer"; break;
case LIBCOOPGAMMA_UINT16: str = "unsigned 16-bit integer"; break;
case LIBCOOPGAMMA_UINT32: str = "unsigned 32-bit integer"; break;
case LIBCOOPGAMMA_UINT64: str = "unsigned 64-bit integer"; break;
default:
errno = EPROTO;
goto fail;
}
printf("Gamma ramps stops value type: %s\n", str);
if (info.colourspace != LIBCOOPGAMMA_UNKNOWN)
{
switch (info.colourspace)
{
case LIBCOOPGAMMA_SRGB: str = "sRGB"; break;
case LIBCOOPGAMMA_RGB: str = "non-standard RGB"; break;
case LIBCOOPGAMMA_NON_RGB: str = "non-RGB multicolour"; break;
case LIBCOOPGAMMA_GREY: str = "monochrome or singlecolour scale"; break;
default:
errno = EPROTO;
goto fail;
}
printf("Monitor's colourspace: %s\n", str);
}
if (info.have_gamut)
{
printf("Monitor's red colour (x y): %lf, %lf\n",
info.red_x / (double)1024, info.red_y / (double)1024);
printf("Monitor's green colour (x y): %lf, %lf\n",
info.green_x / (double)1024, info.green_y / (double)1024);
printf("Monitor's blue colour (x y): %lf, %lf\n",
info.blue_x / (double)1024, info.blue_y / (double)1024);
}
if (libcoopgamma_get_gamma_sync(&query, &table, &cg) < 0)
goto cg_fail;
if ((table.red_size != info.red_size) || (table.green_size != info.green_size) ||
(table.blue_size != info.blue_size) || (table.depth != info.depth))
{
fprintf(stderr, "%s: gamma ramp structure changed between queries\n", argv0);
goto custom_fail;
}
printf("Filters: %zu\n", table.filter_count);
for (i = 0; i < table.filter_count; i++)
{
printf(" Filter %zu:\n", i);
printf(" Priority: %" PRIi64 "\n", table.filters[i].priority);
printf(" Class: %s\n", table.filters[i].class);
}
done:
saved_errno = errno;
libcoopgamma_crtc_info_destroy(&info);
libcoopgamma_filter_table_destroy(&table);
errno = saved_errno;
return ret;
fail:
ret = -1;
goto done;
cg_fail:
ret = -2;
goto done;
custom_fail:
ret = -3;
goto done;
}
/**
* Print, to stdout, the ramps of the select
* filter on the select CRTC
*
* @return Zero on success, -1 on error, -2
* on libcoopgamma error, -3 on error
* with error message already printed
*/
static int print_filter(void)
{
libcoopgamma_filter_table_t table;
libcoopgamma_ramps_t* restrict ramps;
int saved_errno, ret = 0;
size_t i, n;
if (libcoopgamma_filter_table_initialise(&table) < 0)
return -1;
if (libcoopgamma_get_gamma_sync(&query, &table, &cg) < 0)
goto cg_fail;
if (query.coalesce)
i = 0;
else
for (i = 0; i < table.filter_count; i++)
if (!strcmp(table.filters[i].class, class))
break;
if (i == table.filter_count)
{
fprintf(stderr, "%s: selected filter does not exist on selected CRTC\n", argv0);
goto custom_fail;
}
ramps = &(table.filters[i].ramps);
n = table.red_size;
if (n < table.green_size)
n = table.green_size;
if (n < table.blue_size)
n = table.blue_size;
switch (table.depth)
{
#define X(CONST, MEMBER, TYPE, FORMAT, DASH) \
case CONST: \
for (i = 0; i < n; i++) \
{ \
if (i < ramps->MEMBER.red_size) \
printf("%" FORMAT " ", (TYPE)(ramps->MEMBER.red[i])); \
else \
printf(DASH " "); \
if (i < ramps->MEMBER.green_size) \
printf("%" FORMAT " ", (TYPE)(ramps->MEMBER.green[i])); \
else \
printf(DASH " "); \
if (i < ramps->MEMBER.blue_size) \
printf("%" FORMAT "\n", (TYPE)(ramps->MEMBER.blue[i])); \
else \
printf(DASH "\n"); \
} \
break
X(LIBCOOPGAMMA_DOUBLE, d, double, "lf", "----");
X(LIBCOOPGAMMA_FLOAT, f, double, "lf", "----");
X(LIBCOOPGAMMA_UINT8, u8, uint8_t, "02" PRIx8, "--");
X(LIBCOOPGAMMA_UINT16, u16, uint16_t, "04" PRIx16, "----");
X(LIBCOOPGAMMA_UINT32, u32, uint32_t, "08" PRIx32, "--------");
X(LIBCOOPGAMMA_UINT64, u64, uint64_t, "016" PRIx64, "----------------");
#undef X
default:
errno = EPROTO;
goto fail;
}
done:
saved_errno = errno;
libcoopgamma_filter_table_destroy(&table);
errno = saved_errno;
return ret;
fail:
ret = -1;
goto done;
cg_fail:
ret = -2;
goto done;
custom_fail:
ret = -3;
goto done;
}
/**
* -M METHOD
* Select adjustment method. If METHOD is "?",
* available methods will be printed to stdout.
*
* -S SITE
* Select site (display server instance).
*
* -c CRTC
* Select CRT controller. If CRTC is "?", CRTC:s
* will be printed to stdout.
*
* -h HIGH
* Suppress filter with higher priority than HIGH.
*
* -l LOW
* Suppress filter with lower priority than LOW.
*
* -f CLASS
* Print gamma ramps of the filter with class CLASS
* on the selected CRTC. If CLASS is "*" all filters
* with a priority in [LOW, HIGH] are coalesced.
*
* @param argc The number of command line arguments
* @param argv The command line arguments
* @return 0 on success, 1 on error
*/
int main(int argc, char* argv[])
{
int stage = 0, haveh = 0, havel = 0;
int rc = 0;
char* method = NULL;
char* site = NULL;
query.high_priority = INT64_MAX;
query.low_priority = INT64_MIN;
query.crtc = NULL;
query.coalesce = 0;
ARGBEGIN
{
case 'M':
if (method != NULL)
usage();
method = EARGF(usage());
break;
case 'S':
if (site != NULL)
usage();
site = EARGF(usage());
break;
case 'c':
if (query.crtc != NULL)
usage();
query.crtc = EARGF(usage());
break;
case 'h':
if (haveh++)
usage();
query.high_priority = (int64_t)atoll(EARGF(usage()));
break;
case 'l':
if (havel++)
usage();
query.low_priority = (int64_t)atoll(EARGF(usage()));
break;
case 'f':
if (class != NULL)
usage();
class = EARGF(usage());
if ((class[0] == '*') && (class[1] == '\0'))
query.coalesce = 1;
break;
}
ARGEND;
if (initialise_proc() < 0)
goto fail;
if ((method != NULL) && !strcmp(method, "?"))
{
if ((site != NULL) || (query.crtc != NULL))
usage();
if (list_methods() < 0)
goto fail;
return 0;
}
if (libcoopgamma_context_initialise(&cg) < 0)
goto fail;
stage++;
if (libcoopgamma_connect(method, site, &cg) < 0)
{
fprintf(stderr, "%s: server failed to initialise\n", argv0);
goto custom_fail;
}
stage++;
if (!(query.crtc))
usage();
if (!strcmp(query.crtc, "?"))
switch (list_crtcs())
{
case 0:
goto done;
case -1:
goto fail;
default:
goto cg_fail;
}
switch (class ? print_filter() : print_info())
{
case 0:
goto done;
case -1:
goto fail;
case -2:
goto cg_fail;
default:
goto custom_fail;
}
fflush(stdout);
if (ferror(stdout))
goto fail;
if (fclose(stdout) < 0)
goto fail;
done:
if (stage >= 1)
libcoopgamma_context_destroy(&cg, stage >= 2);
return rc;
custom_fail:
rc = 1;
goto done;
fail:
rc = 1;
perror(argv0);
goto done;
cg_fail:
rc = 1;
{
const char* side = cg.error.server_side ? "server" : "client";
if (cg.error.custom)
{
if ((cg.error.number != 0) || (cg.error.description != NULL))
fprintf(stderr, "%s: %s-side error number %" PRIu64 ": %s\n",
argv0, side, cg.error.number, cg.error.description);
else if (cg.error.number != 0)
fprintf(stderr, "%s: %s-side error number %" PRIu64 "\n", argv0, side, cg.error.number);
else if (cg.error.description != NULL)
fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description);
}
else if (cg.error.description != NULL)
fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, cg.error.description);
else
fprintf(stderr, "%s: %s-side error: %s\n", argv0, side, strerror(cg.error.number));
}
goto done;
}
|