-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddcutil-service.c
2925 lines (2637 loc) · 125 KB
/
ddcutil-service.c
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
/* ----------------------------------------------------------------------------------------------------
* ddcutil-service.c
* -----------------
* A D-Bus server for libddcutil/ddcutil
*
*
* Copyright (C) 2023, Michael Hamilton
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Code structure based on https://github.com/bratsche/glib/blob/master/gio/tests/gdbus-example-server.c
*
* Info on D-Bus: https://dbus.freedesktop.org/doc/dbus-specification.html
* Info on GDbus: https://docs.gtk.org/glib/
* Info on lbddcutil: https://www.ddcutil.com/api_main/ and the inline documentation the header files at
* https://github.com/rockowitz/ddcutil/tree/2.0.2-dev/src/public
*/
/**
* Logging:
* Using glib logging which defaults to syslog if running under the dbus-daemon, or the stderr otherwise:
* g_info() and g_debug are classed the same, and don't show by default
* g_message() is higher and always shows.
* g_warning() is for non fatal errors.
* g_critical() is for serious errors (which, as a class, can optionally be set to terminate the program).
* g_error() is for programming errors and will automatically core dump - so don't use g_error().
* There is also a special domain "all" - not sure if we want to use that.
*/
#define PROGRAM_NAME "ddcutil-service"
#define G_LOG_USE_STRUCTURED
#define G_LOG_DOMAIN PROGRAM_NAME // set the log domain before including the glib headers.
#include <gio/gio.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <glob.h>
#include <unistd.h>
#include <spawn.h>
#include <ddcutil_c_api.h>
#include <ddcutil_status_codes.h>
#include <ddcutil_macros.h>
#define DDCUTIL_DBUS_INTERFACE_VERSION_STRING "1.0.13"
#define DDCUTIL_DBUS_DOMAIN "com.ddcutil.DdcutilService"
#if DDCUTIL_VMAJOR == 2 && DDCUTIL_VMINOR == 0 && DDCUTIL_VMICRO < 2
#define LIBDDCUTIL_HAS_OPTION_ARGUMENTS
#define LIBDDCUTIL_HAS_DDCA_GET_SLEEP_MULTIPLIER
#elif DDCUTIL_VMAJOR >= 2
#define LIBDDCUTIL_HAS_CHANGES_CALLBACK
#define LIBDDCUTIL_HAS_OPTION_ARGUMENTS
#define LIBDDCUTIL_HAS_INDIVIDUAL_SLEEP_MULTIPLIER
#define LIBDDCUTIL_HAS_DYNAMIC_SLEEP_BOOLEAN
#else
#define LIBDDCUTIL_HAS_DDCA_GET_DEFAULT_SLEEP_MULTIPLIER
#endif
#define BOOL_STR(value) ((value) ? "true" : "false")
#define MACRO_EXISTS(name) (#name [0] != G_STRINGIFY(name) [0])
/**
* Perform startup checks to ensure i2c is accessible - don't just fail or silently do nothing.
*/
#define VERIFY_I2C
#undef XML_FROM_INTROSPECTED_DATA
/* ----------------------------------------------------------------------------------------------------
* D-Bus interface definition in XML
*/
static GDBusNodeInfo* introspection_data = NULL;
/**
* Introspection data for the service we are exporting
* At some point this could possibly be moved to a file, but maybe is handy to embed it here.
*
* Uses GTK-Doc comment blocks, see https://dbus.freedesktop.org/doc/dbus-api-design.html#annotations
* The @my_parameter annotation formats properly. The #my_property:property and #my_sig::signal annotations
* don't format at all, so I'm not using them.
*
* Using the gcc C raw-string extension R"(my_string)" to embed the XML.
*/
static const gchar introspection_xml[] = R"(
<node>
<!--
com.ddcutil.DdcutilInterface:
@short_description: D-Bus service for libddcutil VESA DDC Monitor Virtual Control Panel
ddcutil-service is D-Bus service wrapper for libddcutil which implements the VESA
DDC Monitor Control Command Set. Most things that can be controlled using a monitor's
on-screen display can be controlled by this service.
When using this service, avoid excessively writing VCP values because each VDU's NVRAM
likely has a write-cycle limit/lifespan. The suggested guideline is to limit updates
to rates comparable to those observed when using the VDU's onboard controls. Avoid coding
that might rapidly or infinitely loop, including when recovering from errors and bugs.
Non-standard manufacturer specific features should only be experimented with caution,
some may have irreversible consequences, including bricking the hardware.
For many of the methods a VDU can be specified by either passing the DDC display_number
or DDC EDID. The EDID is the more stable identifier, it remains unchanged if the number
of connected or powered-up VDUs changes, whereas the DDCA display numbers may be reallocated.
When passing an EDID, pass -1 for display_number, otherwise both are tied with the display_number
having precedence.
Methods that accept an base-64 encoded EDID will accept a prefix of the EDID when passed
a flags value of 1. This is intended as a convenience for passing EDIDs on the command line.
-->
<interface name='com.ddcutil.DdcutilInterface'>
<!--
Restart:
@text_options: Text options to be passed to libddcutil ddca_init().
@syslog_level: The libddcutil syslog level.
@flags: For future use.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Restarts the service with the supplied parameters.
If the service is configuration-locked, an com.ddcutil.DdcutilService.Error.ConfigurationLocked
error is raised.
-->
<method name='Restart'>
<arg name='text_options' type='s' direction='in'/>
<arg name='syslog_level' type='u' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
Detect:
@flags: If set to 8 (DETECT_ALL), any invalid VDUs will be included in the results.
@number_of_displays: The number of VDUs detected (the length of @detected_displays).
@detected_displays: An array of structures describing the VDUs.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Issues a detect and returns the VDUs detected.
The array @detected_displays will be of length @number_of_displays.
Each element of @detected_displays array will contain the fields
specified by the AttributesReturnedByDetect property. The fields
will include the libddcutil display-number and a base64-encoded EDID.
-->
<method name='Detect'>
<arg name='flags' type='u' direction='in'/>
<arg name='number_of_displays' type='i' direction='out'/>
<arg name='detected_displays' type='a(iiisssqsu)' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
ListDetected:
@flags: For future use
@detected_displays: An array of structures describing the VDUs.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Returns the currently detected VDUs without performing using a new detect.
This method is particularly useful for libddcutil 2.2+ where detection
may occur in the background automatically.
The array @detected_displays will be of length @number_of_displays.
Each element of @detected_displays array will contain the fields
specified by the AttributesReturnedByDetect property. The fields
will include the libddcutil display-number and a base64-encoded EDID.
-->
<method name='ListDetected'>
<arg name='flags' type='u' direction='in'/>
<arg name='number_of_displays' type='i' direction='out'/>
<arg name='detected_displays' type='a(iiisssqsu)' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
GetVcp:
@display_number: The libddcutil/ddcutil display number to query
@edid_txt: The base-64 encoded EDID of the display
@vcp_code: The VPC-code to query, for example, 16 (0x10) is brightness.
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@vcp_current_value: The current numeric value as a unified 16 bit integer.
@vcp_max_value: The maximum possible value, to allow for easy calculation of current/max.
@vcp_formatted_value: A formatted version of the value including related info such as the max-value.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Retrieve the value for a VCP-code for the specified VDU.
For simplicity the @vcp_current_value returned will always be 16 bit integer. Most
VCP values are single byte 8-bit integers, very few are two-byte 16-bit.
The method's @flags parameter can be set to 2 (RETURN_RAW_VALUE),
see ddcutil-service.1 LIMITATIONS for an explanation.
The @vcp_formatted_value contains the current value along with any related info,
such as the maximum value, its similar to the output of the ddcutil getvcp shell-command.
-->
<method name='GetVcp'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='vcp_code' type='y' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='vcp_current_value' type='q' direction='out'/>
<arg name='vcp_max_value' type='q' direction='out'/>
<arg name='vcp_formatted_value' type='s' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
GetMultipleVcp:
@display_number: the libddcutil/ddcutil display number to query
@edid_txt: the base-64 encoded EDID of the display
@vcp_code: the VPC-code to query.
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@vcp_current_value: An array of VCP-codes and values.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Retrieves several different VCP values for the specified VDU. This is a convenience
method provided to more efficiently utilise D-Bus.
Each entry in @vcp_current_value array is a VCP-code along with its
current, maximum and formatted values (the same as those returned by GetVcp).
The method's @flags parameter can be set to 2 (RETURN_RAW_VALUES),
see ddcutil-service.1 LIMITATIONS for an explanation.
-->
<method name='GetMultipleVcp'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='vcp_code' type='ay' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='vcp_current_value' type='a(yqqs)' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
SetVcp:
@display_number: the libddcutil/ddcutil display number to alter
@edid_txt: the base-64 encoded EDID of the display
@vcp_code: the VPC-code to query.
@vcp_new_value: the numeric value as a 16 bit integer.
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Set the value for a VCP-code for the specified VDU.
For simplicity the @vcp_new_value is always passed as a 16 bit integer (most
VCP values are single byte 8-bit integers, very few are two-byte 16-bit).
The method's @flags parameter can be set to 4 (NO_VERIFY) to disable
libddcutil verify and retry. Verification and retry is the default.
-->
<method name='SetVcp'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='vcp_code' type='y' direction='in'/>
<arg name='vcp_new_value' type='q' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
SetVcpWithContext:
@display_number: the libddcutil/ddcutil display number to alter
@edid_txt: the base-64 encoded EDID of the display
@vcp_code: the VPC-code to query.
@vcp_new_value: the numeric value as a 16 bit integer.
@client_context: a client-context string that will be returned with the VcpValueChanged signal.
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Set the value for a VCP-code for the specified VDU.
For simplicity the @vcp_new_value is always passed as a 16 bit integer (most
VCP values are single byte 8-bit interers, very few are two-byte 16-bit).
The method's @flags parameter can be set to 4 (NO_VERIFY) to disable
libddcutil verify and retry. Verification and retry is the default.
-->
<method name='SetVcpWithContext'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='vcp_code' type='y' direction='in'/>
<arg name='vcp_new_value' type='q' direction='in'/>
<arg name='client_context' type='s' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
GetVcpMetadata:
@display_number: the libddcutil/ddcutil display number to query
@edid_txt: the base-64 encoded EDID of the display
@vcp_code: the VPC-code to query.
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@feature_name: the feature name for the VCP-code
@feature_description: the feature description, if any, of the VCP-code.
@is_read_only: True if the feature is read-only.
@is_write_only: True if the feature is write-only (for example, a code that turns the VDU off).
@is_rw: True if the feature is readable and writable.
@is_complex: True if the feature is complex (multi-byte).
@is_continuous: True in the feature is a continuous value (it is not an enumeration).
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Retrieve the metadata for a VCP-code for the specified VDU.
-->
<method name='GetVcpMetadata'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='vcp_code' type='y' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='feature_name' type='s' direction='out'/>
<arg name='feature_description' type='s' direction='out'/>
<arg name='is_read_only' type='b' direction='out'/>
<arg name='is_write_only' type='b' direction='out'/>
<arg name='is_rw' type='b' direction='out'/>
<arg name='is_complex' type='b' direction='out'/>
<arg name='is_continuous' type='b' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
GetCapabilitiesString:
@display_number: the libddcutil/ddcutil display number to query
@edid_txt: the base-64 encoded EDID of the display
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@capabilities_text: the capability string for the VDU.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Retrieve the capabilities metadata for a VDU in a format similar to that output by
the command ddcutil terse capabilities (similar enough for parsing by common code).
-->
<method name='GetCapabilitiesString'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='capabilities_text' type='s' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
GetCapabilitiesMetadata:
@display_number: the libddcutil/ddcutil display number to query
@edid_txt: the base-64 encoded EDID of the display
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@model_name: parsed model name string
@mccs_major: MCCS major version number byte.
@mccs_minor: MCCS minor version number byte.
@commands: supported commands as a dictionary indexed by command number.
@capabilities: supported VCP features as a dictionary indexed by VCP-code.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Retrieve the capabilities metadata for a VDU in a parsed dictionary structure
indexed by VCP code.
The @capabilities out parameter is an array of dictionary entries. Each entry consists
of a VCP-code along with a struct containing the feature-name, feature-description,
and an array of permitted-values. For features that have continuous values, the associated
permitted-value array will be empty. For non-continuous features, the permitted-value
array will contain a dictionary entry for each permitted value, each entry containing
a permitted-value and value-name.
-->
<method name='GetCapabilitiesMetadata'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='model_name' type='s' direction='out'/>
<arg name='mccs_major' type='y' direction='out'/>
<arg name='mccs_minor' type='y' direction='out'/>
<arg name='commands' type='a{ys}' direction='out'/>
<arg name='capabilities' type='a{y(ssa{ys})}' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
GetDisplayState:
@display_number: the libddcutil/ddcutil display number to query
@edid_txt: the base-64 encoded EDID of the display
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@status: A libddcutil display status.
@message: Text message for display status.
Retrieve the libddcutil display state.
Depending on the hardware and drivers, this method might return anything
useful.
For libddcutil prior to 2.1, the method will return a libddcutil
@error_status of DDCRC_UNIMPLEMENTED.
-->
<method name='GetDisplayState'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='status' type='i' direction='out'/>
<arg name='message' type='s' direction='out'/>
</method>
<!--
GetSleepMultiplier:
@display_number: the libddcutil/ddcutil display number to query
@edid_txt: the base-64 encoded EDID of the display
@vcp_code: the VPC-code to query, for example, 16 (0x10) is brightness.
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@current_multiplier: the sleep multiplier.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Get the current libddcutil sleep multiplier for the specified VDU.
In more recent versions of libddcutil this value is generally managed automatically.
-->
<method name='GetSleepMultiplier'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='current_multiplier' type='d' direction='out'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
SetSleepMultiplier:
@display_number: The libddcutil/ddcutil display number to query
@edid_txt: The base-64 encoded EDID of the display
@vcp_code: The VPC-code to query, for example, 16 (0x10) is brightness.
@flags: If 1 (EDID_PREFIX), the @edid_txt is matched as a unique prefix of the EDID.
@new_multiplier: The sleep multiplier.
@error_status: A libddcutil DDCRC error status. DDCRC_OK (zero) if no errors have occurred.
@error_message: Text message for error_status.
Set the libddcutil sleep multiplier for the specified VDU.
In more recent versions of libddcutil this is generally managed automatically,
but this method is provided should manual control be necessary (due to problem hardware).
Prior to taking manual control of the sleep-multiplier, the DdcutilDynamicSleep property
should be set to false to prevent the multiplier from being automatically returned.
If the service is configuration-locked, an com.ddcutil.DdcutilService.Error.ConfigurationLocked
error is raised.
-->
<method name='SetSleepMultiplier'>
<arg name='display_number' type='i' direction='in'/>
<arg name='edid_txt' type='s' direction='in'/>
<arg name='new_multiplier' type='d' direction='in'/>
<arg name='flags' type='u' direction='in'/>
<arg name='error_status' type='i' direction='out'/>
<arg name='error_message' type='s' direction='out'/>
</method>
<!--
ConnectedDisplaysChanged:
@edid_txt: The base-64 encoded EDID of the display.
@event_type: A value matching one of those from the DisplayEventTypes property.
@flags: For future use.
Where hardware and drivers support it, this signal will be raised if a displays
connection status changes due to cabling, power, or DPMS.
The hardware, cabling and drivers determines which of states listed by DisplayEventTypes property
that can actually be signaled (the possibilities cannot be determined programmatically).
Requires the ServiceEmitConnectivitySignals property to be set to true.
-->
<signal name='ConnectedDisplaysChanged'>
<arg type='s' name='edid_txt'/>
<arg type='i' name='event_type'/>
<arg type='u' name='flags'/>
</signal>
<!--
VcpValueChanged:
@display_number: the display number
@edid_txt: The base-64 encoded EDID of the display.
@vcp_code: The VCP code whose value changed.
@vcp_new_value: The new value.
@client_name: The D-Bus client-name that requested the change (eases filtering signals caused by self).
@client_context: The client-context passed to SetVcpWithContext (empty string if none).
@flags: no currently in use.
This signal will be raised if a SetVcp or SetVcpWithContext method call succeeds.
-->
<signal name='VcpValueChanged'>
<arg name='display_number' type='i'/>
<arg name='edid_txt' type='s'/>
<arg name='vcp_code' type='y'/>
<arg name='vcp_new_value' type='q'/>
<arg name='source_client_name' type='s'/>
<arg name='source_client_context' type='s'/>
<arg type='u' name='flags'/>
</signal>
<!--
ServiceInitialized:
@flags: For future use.
This signal is raised when the service is initialized. It provides
clients with a way to detect restarts and reinstate properties or
other settings.
-->
<signal name='ServiceInitialized'>
<arg type='u' name='flags'/>
</signal>
<!--
AttributesReturnedByDetect:
The text names of each of the fields in the array of structs returned by the Detect method.
-->
<property type='as' name='AttributesReturnedByDetect' access='read'/>
<!--
StatusValues:
The list of libddcutil status values and their text names that might be returned
in the @error_status out-parameter of most of the service methods.
-->
<property type='a{is}' name='StatusValues' access='read'/>
<!--
DdcutilVersion:
The ddcutil version number for the linked libddcutil.
-->
<property type='s' name='DdcutilVersion' access='read'/>
<!--
DdcutilDynamicSleep:
Enables/disables automatic adjustment of the sleep-multiplier. Before
using the SetSleepMultiplier method, this property should be set to false
to stop any automatic retuning of the multiplier.
Attempting to set this property when the service is configuration-locked
will result in an com.ddcutil.DdcutilService.Error.ConfigurationLocked error
being raised.
-->
<property type='b' name='DdcutilDynamicSleep' access='readwrite'/>
<!--
DdcutilOutputLevel:
Change the libddcutil diagnostic output-level. See the libddcutil/ddcutil
documentation for details.
Attempting to set this property when the service is configuration-locked
will result in an com.ddcutil.DdcutilService.Error.ConfigurationLocked error
being raised.
-->
<property type='u' name='DdcutilOutputLevel' access='readwrite'/>
<!--
DisplayEventTypes:
A list of the event types sent by the ConnectedDisplaysChanged signal along with
their text names. Events are included for display connection/disconnection
(hotplug), DPMS-sleep, and DPMS-wake. If the list is empty, the GPU, GPU-driver,
or libddcutil version does not support display event detection.
-->
<property type='a{is}' name='DisplayEventTypes' access='read'/>
<!--
ServiceInterfaceVersion:
The interface version of this service. Providing the major number remains
the same, the service remains backward compatibility with existing clients.
-->
<property type='s' name='ServiceInterfaceVersion' access='read'/>
<!--
ServiceInfoLogging:
Enables/disables info and debug level logging within the service executable.
(The service using glib logging.)
Attempting to set this property when the service is configuration-locked
will result in an com.ddcutil.DdcutilService.Error.ConfigurationLocked error
being raised.
-->
<property type='b' name='ServiceInfoLogging' access='readwrite'/>
<!--
ServiceEmitConnectivitySignals:
Because VDU connectivity change detection involves some polling, this
property can be used to disable it if it is unecessary. For example, where
the configuration of VDUs is fixed.
Attempting to set this property when the service is configuration-locked
will result in an com.ddcutil.DdcutilService.Error.ConfigurationLocked error
being raised.
-->
<property type='b' name='ServiceEmitConnectivitySignals' access='readwrite'/>
<!--
ServiceEmitSignals:
Deprecated, name was too generic, replaced by ServiceEmitConnectivitySignals
-->
<property type='b' name='ServiceEmitSignals' access='readwrite'/>
<!--
ServiceFlagOptions:
The list of available @flags values that can be passed to the service methods.
Not all options are applicable to all methods.
-->
<property type='a{is}' name='ServiceFlagOptions' access='read'/>
<!--
ServiceParametersLocked:
Indicates whether the lock command line argument has been used to prevent
configuration changes via method calls and property changes.
-->
<property type='b' name='ServiceParametersLocked' access='read'/>
<!--
ServicePollInterval:
Query or set the display change detection poll-interval performed by the service
(minimum 10 seconds, zero to disable polling).
If libddcutil supports change detection and it works for hardware, drivers and cabling
in use, internal polling by the service may be unecessary, in which case polling can be
turned off by setting the interval to zero.
Attempting to set this property when the service is configuration-locked
will result in an com.ddcutil.DdcutilService.Error.ConfigurationLocked error
being raised.
-->
<property type='u' name='ServicePollInterval' access='readwrite'/>
<!--
ServicePollCascadeInterval:
Query or set the display change detection poll-cascade-interval (minimum 0.1 seconds).
When dealing with a cascade of events, for example, when several VDUs are set to DPMS sleep,
polling occurs more frequently until the cascade is cleared.
Attempting to set this property when the service is configuration-locked
will result in an com.ddcutil.DdcutilService.Error.ConfigurationLocked error
being raised.
-->
<property type='d' name='ServicePollCascadeInterval' access='readwrite'/>
</interface>
</node>
)"
; // The newline+semicolon stops any linter warnings due to the above raw-string from propagating to the code below.
/* ----------------------------------------------------------------------------------------------------
* Our GDBus service connection - this will be set to the running connection when the bus is acquired.
*/
static GDBusConnection* dbus_connection = NULL;
/**
* If TRUE, clients cannot change any configuration data (set by a command line parameter).
*/
static gboolean lock_configuration = FALSE;
/**
* Global option (set by command line parameter) to always return full 16 bit raw VCP values.
*/
static gboolean return_raw_values = FALSE;
/**
* True if service info logging is currently enabled.
*/
static gboolean service_info_logging = FALSE;
/* ----------------------------------------------------------------------------------------------------
* Bit flags that can be passed in the service method flags argument.
*/
typedef enum {
EDID_PREFIX = 1, // Indicates the EDID passed to the service is a unique prefix (substr) of the actual EDID.
RETURN_RAW_VALUES = 2, // GetVcp GetMultipleVcp
NO_VERIFY = 4, // SetVcp
DETECT_ALL = 8, // Detect all VDUs, including those that are not powered up.
} Flags_Enum_Type;
/**
* Iterable definitions of Flags_Enum_Type values/names (for return from a service property).
*/
static const int flag_options[] = {EDID_PREFIX,RETURN_RAW_VALUES, NO_VERIFY, DETECT_ALL, };
static const char* flag_options_names[] = {G_STRINGIFY(EDID_PREFIX),
G_STRINGIFY(RETURN_RAW_VALUES),
G_STRINGIFY(NO_VERIFY),
G_STRINGIFY(DETECT_ALL),};
G_STATIC_ASSERT(G_N_ELEMENTS(flag_options) == G_N_ELEMENTS(flag_options_names)); // Boilerplate
/* ----------------------------------------------------------------------------------------------------
* Define the service's connectivity monitoring options for VDU hot-plug/DPMS events
*
* Detecting events is optional. Events can be detected byinternal event polling or by setting
* up libddcutil callbacks. However, libddcutil needs fully functioning DRM to trap events, which
* is often not the case, therefore default to internal polling.
*/
typedef enum {
MONITOR_BY_INTERNAL_POLLING,
MONITOR_BY_LIBDDCUTIL_EVENTS,
} Monitoring_Preference_Type;
static gboolean display_status_detection_enabled = FALSE;
static gboolean enable_connectivity_signals = TRUE;
static Monitoring_Preference_Type monitoring_preference = MONITOR_BY_INTERNAL_POLLING;
#define MIN_POLL_SECONDS 10
#define DEFAULT_POLL_SECONDS 30
/**
* If service is doing it's own polling for events, how often to poll:
*/
static long poll_interval_micros = DEFAULT_POLL_SECONDS * 1000000;
#define MIN_POLL_CASCADE_INTERVAL_SECONDS 0.1
#define DEFAULT_POLL_CASCADE_INTERVAL_SECONDS 0.5
/**
* Each event in an event-cascade will be at least this far apart:
*/
static long poll_cascade_interval_micros = (long) (DEFAULT_POLL_CASCADE_INTERVAL_SECONDS * 1000000);
#if defined(LIBDDCUTIL_HAS_CHANGES_CALLBACK)
/**
* Custom signal event data - used by the service's custom signal source
*/
typedef DDCA_Display_Status_Event Event_Data_Type;
#else
/**
* Define our own event type enum which mirrors the future one in libddcutil 2.1
*/
typedef enum {
DDCA_EVENT_DPMS_AWAKE,
DDCA_EVENT_DPMS_ASLEEP,
DDCA_EVENT_DISPLAY_CONNECTED,
DDCA_EVENT_DISPLAY_DISCONNECTED,
DDCA_EVENT_UNUSED1,
DDCA_EVENT_UNUSED2,
} DDCA_Display_Event_Type;
/**
* Define our own event data which mirrors the future one in libddcutil 2.1
*/
typedef struct {
DDCA_Display_Event_Type event_type;
DDCA_Display_Ref dref;
} Event_Data_Type;
#endif
/**
* List of DDCA events numbers and names that can be iterated over (for return from a service property).
*/
static const int event_types[] = {
DDCA_EVENT_DISPLAY_CONNECTED, DDCA_EVENT_DISPLAY_DISCONNECTED,
DDCA_EVENT_DPMS_AWAKE, DDCA_EVENT_DPMS_ASLEEP,
};
/**
* List of DDCA events names in the same order as the event_types array.
*/
static const char* event_type_names[] = {
G_STRINGIFY(DDCA_EVENT_DISPLAY_CONNECTED), G_STRINGIFY(DDCA_EVENT_DISPLAY_DISCONNECTED),
G_STRINGIFY(DDCA_EVENT_DPMS_AWAKE), G_STRINGIFY(DDCA_EVENT_DPMS_ASLEEP),
};
G_STATIC_ASSERT(G_N_ELEMENTS(event_types) == G_N_ELEMENTS(event_type_names)); // Boilerplate
/**
* \brief for a given event_type_num return its name
* \param event_type_num
* \return name or "unknown_event_type"
*/
static const char* get_event_type_name(int event_type_num) {
for (int i = 0; i < sizeof(event_types) / sizeof(int); i++) {
if (event_types[i] == event_type_num) {
return event_type_names[i];
}
}
return "unknown_event_type";
}
/**
* Global data value - held for dispatch - accessed/updated atomically.
*/
static Event_Data_Type* signal_event_data = NULL;
/**
* List of fields returned in by the Detect service method (for return from a service property).
*/
static const char* attributes_returned_from_detect[] = {
"display_number", "usb_bus", "usb_device",
"manufacturer_id", "model_name", "serial_number", "product_code",
"edid_txt", "binary_serial_number",
NULL
};
/* ----------------------------------------------------------------------------------------------------
* GLib error message definitions for use with the GQuark service_error_quark
*/
typedef enum {
DDCUTIL_SERVICE_CONFIGURATION_LOCKED,
DDCUTIL_SERVICE_INVALID_POLL_SECONDS,
DDCUTIL_SERVICE_INVALID_POLL_CASCADE_SECONDS,
DDCUTIL_SERVICE_I2C_DEV_NO_MODULE,
DDCUTIL_SERVICE_I2C_DEV_NO_PERMISSIONS,
DDCUTIL_SERVICE_OK, // Non error
DDCUTIL_SERVICE_N_ERRORS // Dummy placeholder for counting the number of entries
} DdcutilServiceStatus;
static DdcutilServiceStatus ddcutil_service_status = DDCUTIL_SERVICE_OK;
static const GDBusErrorEntry ddcutil_service_error_entries[] = {
{ DDCUTIL_SERVICE_CONFIGURATION_LOCKED, "com.ddcutil.DdcutilService.Error.ConfigurationLocked" },
{ DDCUTIL_SERVICE_INVALID_POLL_SECONDS, "com.ddcutil.DdcutilService.Error.InvalidPollSeconds" },
{ DDCUTIL_SERVICE_INVALID_POLL_CASCADE_SECONDS, "com.ddcutil.DdcutilService.Error.InvalidPollCascadeSeconds" },
{ DDCUTIL_SERVICE_I2C_DEV_NO_MODULE, "com.ddcutil.DdcutilService.Error.I2cDevNoModule" },
{ DDCUTIL_SERVICE_I2C_DEV_NO_PERMISSIONS, "com.ddcutil.DdcutilService.Error.I2cDevNoPermissions" },
{ DDCUTIL_SERVICE_OK, "com.ddcutil.DdcutilService.Error.OK" },
};
G_STATIC_ASSERT(G_N_ELEMENTS(ddcutil_service_error_entries) == DDCUTIL_SERVICE_N_ERRORS); // Boilerplate
static GQuark service_error_quark;
static void init_service_error_quark(void)
{
static gsize quark = 0;
g_dbus_error_register_error_domain("ddcutil_service_error_quark",
&quark,
ddcutil_service_error_entries,
G_N_ELEMENTS (ddcutil_service_error_entries));
service_error_quark = quark;
}
/* ----------------------------------------------------------------------------------------------------
*/
/**
* @brief Encode the EDID for easy/efficient unmarshalling on clients.
* @param edid binary EDID
* @return a relatively compact character string encoded edid
*/
static char* edid_encode(const uint8_t* edid) {
return g_base64_encode(edid, 128); // Shorter than hex but not too much like line noise.
}
static char* server_executable = PROGRAM_NAME;
/**
* \brief Binary serial number extraction (coded copied from ddcutil)
* \param edid_bytes binary edid bytes
* \return binary serial number
*/
static uint32_t edid_to_binary_serial_number(const uint8_t* edid_bytes) {
const uint32_t binary_serial =
edid_bytes[0x0c] |
edid_bytes[0x0d] << 8 |
edid_bytes[0x0e] << 16 |
edid_bytes[0x0f] << 24;
return binary_serial;
}
/**
* @brief Create a new string with invalid utf-8 edited out (replaced with ?)
* @param text suspect text
* @return g_malloced text with invalid utf-8 edited out
*/
static gchar* sanitize_utf8(const char* text) {
gchar* result = g_strdup(text);
const char *ptr = result, *end = ptr + strlen(result);
while (true) {
const char* ptr2;
g_utf8_validate(ptr, end - ptr, &ptr2);
if (ptr2 == end)
break;
result[ptr2 - result] = '?'; // Sanitize invalid utf-8
ptr = ptr2 + 1;
}
return result;
}
/**
* @brief Enable logging of info and debug level messages for this service.
* @param enable whether to log or not
* @param overwrite whether to overwrite any existing setting
* @return the new enabled state
*/
static bool enable_service_info_logging(bool enable, bool overwrite) {
service_info_logging = enable;
if (enable) {
// WARNING g_setenv/g_unsetenv stopped working, using setenv/unsetenv instead.
// Possible interaction with other logging options, maybe from libddcutil - weird
setenv("G_MESSAGES_DEBUG", G_LOG_DOMAIN, overwrite); // enable info+debug messages for our domain.
return TRUE;
}
unsetenv("G_MESSAGES_DEBUG"); // disable info+debug messages for our domain.
return FALSE;
}
/**
* @brief Return whether the service is set to log info and debug messages.
* @return enabled state
*/
static bool is_service_info_logging() {
const char* value = g_getenv("G_MESSAGES_DEBUG");
return value != NULL && strstr(value, G_LOG_DOMAIN) != NULL;
}
/**
* @brief Obtain a text message for a DDCA status.
* @param status the DDCA status
* @return a g_malloced message
*/
static char* get_status_message(const DDCA_Status status) {
const char* status_text = ddca_rc_name(status);
char* message_text = NULL;
if (status != 0) {
DDCA_Error_Detail* error_detail = ddca_get_error_detail();
if (error_detail != NULL && error_detail->detail != NULL) {
message_text = g_strdup_printf("%s: %s", status_text, error_detail->detail);
}
free(error_detail);
}
if (message_text == NULL) {
message_text = g_strdup(status_text);
}
return message_text;
}
/**
* Wrap ddca_get_display_info_list2 filter return status for success. Some versions of libddcutil return
* both DDCRC_OK and DDCRC_OTHER for success.
* @param include_invalid include invalid displays in the list (probably powered off)
* @param dlist_loc output dlist
* @param msg_prefix if not NULL warn with prefix if successful but status was not originally DDCRC_OK
* @return DDCRC_OK if successful
*/
static DDCA_Status get_display_info_list(bool include_invalid, DDCA_Display_Info_List** dlist_loc, char *msg_prefix) {
DDCA_Status detect_status = ddca_get_display_info_list2(include_invalid, dlist_loc);
// Pre libddcutil 2.1.5 ddca_get_display_info_list2 could return DDCRC_OTHER if some VDUs were invalid,
// For libddcutil 2.1.5+ ddca_get_display_info_list2 will return DDCRC_OK, but set error_detail if some VDUs are invalid.
DDCA_Error_Detail *error_detail = ddca_get_error_detail();
if (error_detail != NULL) { // libddcutil 2.1.5 and abouve
if (error_detail->status_code == DDCRC_OTHER) {
if (msg_prefix != NULL) {
char *detect_message_text = get_status_message(error_detail->status_code);
g_warning("%s: treating as OK ddca_get_display_info_list2 status=%d message=%s detail=%s",
msg_prefix, error_detail->status_code, detect_message_text, error_detail->detail);
free(detect_message_text);
}
detect_status = DDCRC_OK; // change to DDCRC_OK, just in case
}