Repository navigation
Expand file tree
/
Copy pathacdcontrol.cpp
More file actions
870 lines (757 loc) · 29.4 KB
/
Copy pathacdcontrol.cpp
File metadata and controls
870 lines (757 loc) · 29.4 KB
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
/*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define HID_MAX_USAGES 1024
#define HID_MAX_APPLICATIONS 16
#define VERSION "0.5"
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <sys/signal.h>
#include <getopt.h>
#include <linux/hiddev.h>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
using namespace std;
enum Mode {
MODE_GET,
MODE_SET,
MODE_DETECT,
MODE_SETREL
};
enum AutoBrightnessMode {
AUTO_BRIGHTNESS_NONE,
AUTO_BRIGHTNESS_STATUS,
AUTO_BRIGHTNESS_SET_ON,
AUTO_BRIGHTNESS_SET_OFF
};
// Supported vendors
const int APPLE = 0x05ac;
const int SAMSUNG = 0x0419;
const int BRIGHTNESS_REPORT_ID = 16;
const int BRIGHTNESS_FIELD_INDEX = 0;
const int BRIGHTNESS_USAGE_INDEX = 0;
const int MIN_BRIGHTNESS = 0;
const int MAX_BRIGHTNESS = 1023;
const int USAGE_CODE = 0x820010;
const int AUTO_BRIGHTNESS_REPORT_ID = 102;
const int AUTO_BRIGHTNESS_FIELD_INDEX = 0;
const int AUTO_BRIGHTNESS_USAGE_INDEX = 0;
const int AUTO_BRIGHTNESS_USAGE_CODE = 0x820066;
const int AUTO_BRIGHTNESS_VALUE_OFF = 1;
const int AUTO_BRIGHTNESS_VALUE_ON = 2;
const int STUDIO_DISPLAY_15 = 0x9215;
const int STUDIO_DISPLAY_17 = 0x9217;
const int CINEMA_DISPLAY_23_OLD = 0x9218;
const int CINEMA_DISPLAY_20_ALT = 0x921b;
const int CINEMA_DISPLAY_23_NEW = 0x921c;
const int CINEMA_DISPLAY_20_OLD = 0x9219;
const int CINEMA_DISPLAY_20_NEW = 0x9222;
const int CINEMA_DISPLAY_24 = 0x921e;
const int CINEMA_DISPLAY_27 = 0x9226;
const int CINEMA_DISPLAY_27_2013 = 0x9227;
const int CINEMA_DISPLAY_30 = 0x9232;
const int CINEMA_DISPLAY_30_NEW = 0x9221;
const int CINEMA_LED_DISPLAY_24 = 0x9236;
const int S1 = 0x8002;
// Forward Declarations
void init_device_database();
void dump_supported();
// Helpful declarations
typedef unsigned Vendor;
typedef unsigned Product;
struct DeviceId {
Product product;
Vendor vendor;
string description;
DeviceId ( Vendor vendor_, Product product_, string description_ )
: product( product_ )
, vendor( vendor_ )
, description( description_ ) { }
bool operator < ( const DeviceId& other ) const {
return (vendor < other.vendor) ||
(vendor == other.vendor && product < other.product );
}
};
typedef set< DeviceId > SupportedDevices;
SupportedDevices supportedDevices;
typedef map< Vendor, string > SupportedVendors;
SupportedVendors supportedVendors;
typedef pair< Vendor, string> VendorDesc;
/** @return true if the string is a valid integer brightness argument */
bool parse_int_arg( const char* str, int& value, bool& relative ) {
char* end = NULL;
long parsed = 0;
if ( !str || !*str )
return false;
errno = 0;
parsed = strtol( str, &end, 10 );
if ( end == str || *end != '\0' || errno == ERANGE ||
parsed < INT_MIN || parsed > INT_MAX ) {
return false;
}
relative = ( str[0] == '+' || str[0] == '-' );
value = static_cast<int>( parsed );
return true;
}
/** @return true if the device is in our database */
bool is_supported ( const hiddev_devinfo& device_info ) {
Product product = device_info.product & 0xFFFF;
Vendor vendor = device_info.vendor & 0xFFFF;
return supportedDevices.find( DeviceId( vendor, product, "" )) !=
supportedDevices.end();
}
/**
* @param v query vendor
* @param p query product
* @return description of the device with given vendor and product
*/
string description ( Vendor v, Product p ) {
SupportedDevices::iterator it =
supportedDevices.find( DeviceId( v, p, "" ));
if ( it != supportedDevices.end() )
return it->description;
return "";
}
/** @param v vendor to query
* @return true if the venodr is in the database
*/
bool known_vendor ( Vendor v ) {
v &= 0xFFFF;
return supportedVendors.find( v ) != supportedVendors.end();
}
/** Checks whether the HID device is a usb monitor
* @param device_info HID device info
* @param fd file to read applications from
*/
bool is_usb_monitor ( const hiddev_devinfo& device_info, int fd ) {
/* Now that we have the number of applications, we can retrieve them */
/* using the HIDIOCAPPLICATION ioctl() call */
/* applications are indexed from 0..{num_applications-1} */
for ( __u32 appl_num = 0; appl_num < device_info.num_applications;
++appl_num ) {
int application = ioctl( fd, HIDIOCAPPLICATION, appl_num );
/* The magic values come from various usage table specs */
if ( ((application >> 16) & 0xFF) == 0x80 ) {
return true;
}
}
return false;
}
/** @return true if auto-brightness mapping is known for this device. */
bool supports_auto_brightness( const hiddev_devinfo& device_info ) {
Product product = device_info.product & 0xFFFF;
Vendor vendor = device_info.vendor & 0xFFFF;
return vendor == APPLE && product == CINEMA_DISPLAY_27;
}
/** Report a fatal device I/O error, close descriptor, and terminate. */
static void fatal_perror_and_close( int fd, const char* operation, int code ) {
perror( operation );
if ( fd >= 0 )
close( fd );
exit( code );
}
/** Wrapper for HIDIOCSUSAGE. */
static int set_usage( int fd, hiddev_usage_ref& usage_ref ) {
return ioctl( fd, HIDIOCSUSAGE, &usage_ref );
}
/** Wrapper for HIDIOCSREPORT. */
static int set_report( int fd, hiddev_report_info& rep_info ) {
return ioctl( fd, HIDIOCSREPORT, &rep_info );
}
/** Wrapper for HIDIOCGUSAGE. */
static int get_usage( int fd, hiddev_usage_ref& usage_ref ) {
return ioctl( fd, HIDIOCGUSAGE, &usage_ref );
}
/** Wrapper for HIDIOCGREPORT. */
static int get_report( int fd, hiddev_report_info& rep_info ) {
return ioctl( fd, HIDIOCGREPORT, &rep_info );
}
/** Initialize a feature usage reference. */
static void init_feature_usage( hiddev_usage_ref& usage_ref,
int report_id,
int field_index,
int usage_index,
int usage_code,
int value ) {
memset( &usage_ref, 0, sizeof( usage_ref ) );
usage_ref.report_type = HID_REPORT_TYPE_FEATURE;
usage_ref.report_id = report_id;
usage_ref.field_index = field_index;
usage_ref.usage_index = usage_index;
usage_ref.usage_code = usage_code;
usage_ref.value = value;
}
/** Initialize a feature report descriptor. */
static void init_feature_report( hiddev_report_info& rep_info,
int report_id ) {
memset( &rep_info, 0, sizeof( rep_info ) );
rep_info.report_type = HID_REPORT_TYPE_FEATURE;
rep_info.report_id = report_id;
rep_info.num_fields = 1;
}
/** Ensure a usage code matches the expected value. */
static void validate_usage_code( int fd,
const hiddev_usage_ref& usage_ref,
unsigned int expected_usage_code ) {
if ( expected_usage_code != 0 && usage_ref.usage_code != expected_usage_code ) {
fprintf( stderr,
"Unexpected usage code. Expected 0x%08x, got 0x%08x\n",
expected_usage_code,
usage_ref.usage_code );
close( fd );
exit( 4 );
}
}
/** Write HID feature usage/report state to the device and verify it. */
static void write_feature_report( int fd,
hiddev_usage_ref& usage_ref,
hiddev_report_info& rep_info,
const char* usage_error,
const char* report_error,
const char* verify_usage_error,
const char* verify_report_error,
unsigned int expected_usage_code ) {
if ( set_usage( fd, usage_ref ) < 0 ) {
fatal_perror_and_close( fd, usage_error, 2 );
}
if ( set_report( fd, rep_info ) < 0 ) {
fatal_perror_and_close( fd, report_error, 3 );
}
if ( get_report( fd, rep_info ) < 0 ) {
fatal_perror_and_close( fd, verify_report_error, 3 );
}
if ( get_usage( fd, usage_ref ) < 0 ) {
fatal_perror_and_close( fd, verify_usage_error, 2 );
}
validate_usage_code( fd, usage_ref, expected_usage_code );
}
/** Read HID feature usage/report state from the device. */
static void read_feature_report( int fd,
hiddev_usage_ref& usage_ref,
hiddev_report_info& rep_info,
const char* usage_error,
const char* report_error,
unsigned int expected_usage_code ) {
if ( get_usage( fd, usage_ref ) < 0 ) {
fatal_perror_and_close( fd, usage_error, 2 );
}
if ( get_report( fd, rep_info ) < 0 ) {
fatal_perror_and_close( fd, report_error, 3 );
}
validate_usage_code( fd, usage_ref, expected_usage_code );
}
/** Write brightness-related HID usage/report state to the device. */
static void write_brightness_report( int fd,
hiddev_usage_ref& usage_ref,
hiddev_report_info& rep_info ) {
write_feature_report( fd,
usage_ref,
rep_info,
"HIDIOCSUSAGE brightness failed",
"HIDIOCSREPORT brightness failed",
"HIDIOCGUSAGE brightness verify failed",
"HIDIOCGREPORT brightness verify failed",
USAGE_CODE );
}
/** Read brightness-related HID usage/report state from the device. */
static void read_brightness_report( int fd,
hiddev_usage_ref& usage_ref,
hiddev_report_info& rep_info ) {
read_feature_report( fd,
usage_ref,
rep_info,
"HIDIOCGUSAGE brightness failed",
"HIDIOCGREPORT brightness failed",
USAGE_CODE );
}
/** Write auto-brightness-related HID usage/report state to the device. */
static void write_auto_brightness_report( int fd,
hiddev_usage_ref& usage_ref,
hiddev_report_info& rep_info ) {
write_feature_report( fd,
usage_ref,
rep_info,
"HIDIOCSUSAGE auto-brightness failed",
"HIDIOCSREPORT auto-brightness failed",
"HIDIOCGUSAGE auto-brightness verify failed",
"HIDIOCGREPORT auto-brightness verify failed",
AUTO_BRIGHTNESS_USAGE_CODE );
}
/** Read auto-brightness-related HID usage/report state from the device. */
static void read_auto_brightness_report( int fd,
hiddev_usage_ref& usage_ref,
hiddev_report_info& rep_info ) {
read_feature_report( fd,
usage_ref,
rep_info,
"HIDIOCGUSAGE auto-brightness failed",
"HIDIOCGREPORT auto-brightness failed",
AUTO_BRIGHTNESS_USAGE_CODE );
}
/** Convert a raw auto-brightness value into a printable string. */
static const char* auto_brightness_status( int value ) {
switch ( value ) {
case AUTO_BRIGHTNESS_VALUE_OFF:
return "off";
case AUTO_BRIGHTNESS_VALUE_ON:
return "on";
default:
return "unknown";
}
}
/** Pretty-prints the given device information
* @param o output stream to print to
* @param device_info HID device info
*/
void format_device( ostream& o, const hiddev_devinfo& device_info ) {
Vendor v = device_info.vendor & 0xFFFF;
Product p = device_info.product & 0xFFFF;
o << "Vendor=" << showbase << setw( 6 ) << hex << v;
if ( known_vendor( v ) )
o << " (" << supportedVendors[ v ] << ")";
o << ", Product=" << showbase << setw( 6 ) << hex << p ;
if ( is_supported( device_info ))
o << "[" << description( v, p ) << "]";
o << endl;
}
/** Debug routine for dumping device usage
* @param usage_ref usage description
*/
void dump_usage ( hiddev_usage_ref& usage_ref ) {
printf (" report_type =%d\n", usage_ref.report_type );
printf (" report_id =%d\n", usage_ref.report_id );
printf (" field_index =%d\n", usage_ref.field_index );
printf (" usage_index =%d\n", usage_ref.usage_index );
printf (" usage_code =%d\n", usage_ref.usage_code );
}
/** Prints help for the program.
* @param programName this program name
*/
void help( const char *programName ) {
printf( "acdcontrol " VERSION "\n");
printf( "USAGE: %s [--silent|-s] [--brief|-b] [--help|-h] [--about|-a] [--force|-f] "
"[--detect|-d] [--list-all |-l] [--auto-brightness <on|off|status>] <hid device(s)> [<brightness>]\n\n"
"Parameters:\n"
" --silent,-s\n"
" Suppress non-functional program output\n"
" --brief,-b\n"
" Print brightness value only when in query mode,\n"
" otherwise ignored. For --auto-brightness status,\n"
" print only 'on', 'off' or 'unknown'.\n"
" --force,-f\n"
" Continue even if the detected device is unsupported.\n"
" --detect, -d\n"
" Perform detection only\n"
" --list-all, -l\n"
" List supported devices and exit\n"
" --auto-brightness <on|off|status>\n"
" Control or query auto-brightness on displays with a known\n"
" auto-brightness mapping.\n"
" --help,-h\n"
" Show short help message and quit.\n"
" --about,-a\n"
" Show information about the program, some credits and thanks.\n"
" hid device\n"
" device that represents your Apple Cinema display.\n"
" It shoud normally be one of /dev/usb/hiddevX. or /dev/hiddevX\n"
" Note\n"
" You must have write permissions to this device.\n"
" Note\n"
" It should be safe to run the program on other device than\n"
" Apple Cinema/Studio Display as the program checks whether\n"
" the device is Apple display and warns about it.\n"
" brightness\n"
" When this option is specified, the operation is to set brightness,\n"
" otherwise, current brightness is retreived. If brightness starts\n"
" with '+' or '-', the current brightness is increased or decreased\n"
" by that value.\n"
" Note\n"
" Preceed brightness decrement by '--'.\n"
" See also: --brief option.\n"
"\n\nEXAMPLES:\n\n"
"In the following examples I assume that your HID device is /dev/hiddev0. You may \n"
"have it as /dev/hiddevX or /dev/usb/hiddevX.\n"
"\n"
" acdcontrol\n"
" acdcontrol --help\n"
" Show long help message.\n"
"\n"
" acdcontrol --detect /dev/hiddev*\n"
" Perform detection, which HID device is actually your display to be controlled.\n"
"\n"
" acdcontrol /dev/hiddev0\n"
" Read current brightness parameter\n"
"\n"
" acdcontrol /dev/hiddev0 160\n"
" Set brightness to 160. Note, that brightness setting depends on your model. \n"
" Valid brightness ranges depend on the display model.\n"
" On the maintainer's tested Apple LED Cinema Display 27\" (A1316),\n"
" the effective range is 0..1023.\n"
"\n"
" acdcontrol /dev/hiddev0 +10\n"
" Increment current brightness by 10.\n"
"\n"
" acdcontrol /dev/hiddev0 -- -10\n"
" Decrement current brightness by 10. Please,note '--'!\n"
"\n"
" acdcontrol /dev/hiddev0 --auto-brightness status\n"
" Query current auto-brightness state on a supported display.\n"
"\n"
" acdcontrol /dev/hiddev0 --auto-brightness on\n"
" Enable auto-brightness on a supported display.\n"
"\n"
" acdcontrol /dev/hiddev0 --auto-brightness off\n"
" Disable auto-brightness on a supported display.\n"
,
programName );
}
/** Prints brief notice about the program */
void notice() {
printf( "Apple Cinema and Studio Display Control Program. Please, use --about switch to learn more\n" );
}
/** Prints detailed info about this program */
void about() {
printf( "acdcontrol " VERSION "\n");
printf( "Written by Pavel Gurevich.\n\n"
"This program is a free software and is distributed under GPL2\n\n"
"CREDITS\n\n"
"This program would never possibly come out without valuable help of Andre Beckedorf."
" Andre wrote a similar software for Windows. Please, visit http://metaexception.de/."
" Andre would like to thank to people that helped him to write his software and I'd "
"like to join him as they helped me that way :)\n\n"
" * Dmitri Kitaynik (20\" Display)\n"
" * Mark Wagner (15\" and 17\" Displays)\n"
" * Veit Wahlich (30\" Display)\n"
" * Charles Lepple (24\" Display)\n"
" * Arne Zellentin (relative brightness change)\n\n"
" Please, visit http://acdcontrol.sf.net for updates\n"
"NOTE: You can suppress a startup message with --silent (-s) option\n"
);
}
/******************************************************************************
** _
** (_)
** _ __ ___ __ _ _ _ __
** | '_ ` _ \ / _` | | | | '_ \
** | | | | | | | (_| | | | | | | |
** |_| |_| |_| \__,_| |_| |_| |_|
**
*******************************************************************************/
int main (int argc, char **argv) {
int fd = -1;
struct hiddev_devinfo device_info;
struct hiddev_report_info rep_info;
struct hiddev_usage_ref usage_ref;
int version;
int brightness = 0;
int amount = 0;
Mode mode = MODE_GET;
AutoBrightnessMode auto_brightness_mode = AUTO_BRIGHTNESS_NONE;
int open_mode = O_RDONLY;
/* Behavior options */
bool brief = false;
bool silent = false;
bool force = false;
bool first_device = true;
int c;
init_device_database();
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"about", 0, 0, 'a'},
{"brief", 0, 0, 'b'},
{"help", 0, 0, 'h'},
{"silent", 0, 0, 's'},
{"force", 0, 0, 'f'},
{"detect", 0, 0, 'd'},
{"list-all", 0, 0, 'l'},
{"auto-brightness", 1, 0, 'A'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "abhsdlfA:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'a':
about();
return 0;
case 'b':
brief = true;
break;
case 'h':
help( argv[0] );
return 0;
case 's':
silent = true;
break;
case 'f':
force = true;
break;
case 'd':
mode = MODE_DETECT;
break;
case 'l':
dump_supported();
return 0;
case 'A':
if ( strcmp( optarg, "status" ) == 0 ) {
auto_brightness_mode = AUTO_BRIGHTNESS_STATUS;
} else if ( strcmp( optarg, "on" ) == 0 ) {
auto_brightness_mode = AUTO_BRIGHTNESS_SET_ON;
} else if ( strcmp( optarg, "off" ) == 0 ) {
auto_brightness_mode = AUTO_BRIGHTNESS_SET_OFF;
} else {
fprintf( stderr,
"Invalid auto-brightness mode '%s'. Valid values are: on, off, status.\n",
optarg );
return 2;
}
break;
default:
fprintf (stderr,"Unknown option '%c'\n", c);
help( argv[0] );
return 2;
}
}
typedef list< const char* > FileList;
FileList files;
for ( int param = optind; param < argc; ++param ) {
bool relative = false;
int value = 0;
bool numeric_candidate = argv[ param ] &&
( ((argv[ param ][0] >= '0') && (argv[ param ][0] <= '9')) ||
argv[ param ][0] == '+' || argv[ param ][0] == '-' );
if ( mode != MODE_DETECT && auto_brightness_mode == AUTO_BRIGHTNESS_NONE && numeric_candidate ) {
if ( !parse_int_arg( argv[ param ], value, relative ) ) {
fprintf( stderr,
"Invalid brightness value '%s'. Valid forms: 123, +10, or -- -10.\n",
argv[ param ] );
return 2;
}
if ( relative ) {
mode = MODE_SETREL;
amount = value;
} else {
mode = MODE_SET;
brightness = value;
}
continue;
}
files.push_back( argv[ param ] );
}
if ( auto_brightness_mode != AUTO_BRIGHTNESS_NONE &&
( mode == MODE_SET || mode == MODE_SETREL ) ) {
fprintf( stderr,
"Brightness arguments cannot be combined with --auto-brightness.\n" );
return 2;
}
if ( files.empty() ) {
help( argv[0] );
return 1;
}
if ( mode == MODE_SET || mode == MODE_SETREL ||
auto_brightness_mode == AUTO_BRIGHTNESS_SET_ON ||
auto_brightness_mode == AUTO_BRIGHTNESS_SET_OFF ) {
open_mode = O_RDWR;
}
if ( !silent )
notice();
for ( FileList::iterator it = files.begin(); it != files.end(); ++it ) {
if (( fd = open( *it, open_mode )) < 0) {
perror(*it);
continue;
}
/* ioctl() accesses the underlying driver */
if ( ioctl( fd, HIDIOCGVERSION, &version ) < 0 ) {
perror( "HIDIOCGVERSION" );
} else {
/* the HIDIOCGVERSION ioctl() returns a packed 32 field (aka integer) */
/* so we unpack it and display it */
if ( first_device ) {
if ( !silent ) {
printf("hiddev driver version is %d.%d.%d\n",
version >> 16, (version >> 8) & 0xff, version & 0xff);
}
first_device = false;
}
}
/* suck out some device information */
if ( ioctl( fd, HIDIOCGDEVINFO, &device_info ) < 0 ) {
perror( "HIDIOCGDEVINFO" );
close( fd );
continue;
}
if ( mode == MODE_DETECT ) {
if ( is_usb_monitor( device_info, fd ) ) {
cout << *it << ": USB Monitor - "
<< (is_supported( device_info ) ? "SUPPORTED": "UNSUPPORTED")
<< ".\t";
format_device( cout, device_info );
}
close(fd);
continue;
}
if ( !is_supported ( device_info ) ) {
cerr << "Device unsupported:";
format_device(cerr, device_info);
if ( !force ) {
close(fd);
return 2;
}
}
if (! is_usb_monitor( device_info, fd )) {
cerr << *it << ": This device is NOT USB monitor!" << endl;
close(fd);
continue;
}
/* Initialise the internal report structures */
if (ioctl(fd, HIDIOCINITREPORT,0) < 0) {
cerr << "FATAL: Failed to initialize internal report structures"
<< endl;
close(fd);
return 1;
}
if ( auto_brightness_mode != AUTO_BRIGHTNESS_NONE ) {
struct hiddev_usage_ref auto_usage_ref;
struct hiddev_report_info auto_rep_info;
if ( !supports_auto_brightness( device_info ) ) {
cerr << *it
<< ": Auto-brightness control is not mapped for this display model yet."
<< endl;
close( fd );
return 2;
}
init_feature_usage( auto_usage_ref,
AUTO_BRIGHTNESS_REPORT_ID,
AUTO_BRIGHTNESS_FIELD_INDEX,
AUTO_BRIGHTNESS_USAGE_INDEX,
AUTO_BRIGHTNESS_USAGE_CODE,
AUTO_BRIGHTNESS_VALUE_OFF );
init_feature_report( auto_rep_info, AUTO_BRIGHTNESS_REPORT_ID );
if ( auto_brightness_mode == AUTO_BRIGHTNESS_SET_ON ) {
auto_usage_ref.value = AUTO_BRIGHTNESS_VALUE_ON;
write_auto_brightness_report( fd, auto_usage_ref, auto_rep_info );
} else if ( auto_brightness_mode == AUTO_BRIGHTNESS_SET_OFF ) {
auto_usage_ref.value = AUTO_BRIGHTNESS_VALUE_OFF;
write_auto_brightness_report( fd, auto_usage_ref, auto_rep_info );
}
read_auto_brightness_report( fd, auto_usage_ref, auto_rep_info );
init_feature_usage( usage_ref,
BRIGHTNESS_REPORT_ID,
BRIGHTNESS_FIELD_INDEX,
BRIGHTNESS_USAGE_INDEX,
USAGE_CODE,
0 );
init_feature_report( rep_info, BRIGHTNESS_REPORT_ID );
read_brightness_report( fd, usage_ref, rep_info );
if ( !brief )
cout << *it << ": AUTO_BRIGHTNESS=";
cout << auto_brightness_status( auto_usage_ref.value );
if ( !brief )
cout << " BRIGHTNESS=" << usage_ref.value;
cout << endl;
close( fd );
continue;
}
init_feature_usage( usage_ref,
BRIGHTNESS_REPORT_ID,
BRIGHTNESS_FIELD_INDEX,
BRIGHTNESS_USAGE_INDEX,
USAGE_CODE,
brightness );
init_feature_report( rep_info, BRIGHTNESS_REPORT_ID );
// dump_usage ( usage_ref );
if ( mode == MODE_SET ) {
if ( brightness < MIN_BRIGHTNESS )
brightness = MIN_BRIGHTNESS;
if ( brightness > MAX_BRIGHTNESS )
brightness = MAX_BRIGHTNESS;
usage_ref.value = brightness;
write_brightness_report( fd, usage_ref, rep_info );
read_brightness_report( fd, usage_ref, rep_info );
} else {
read_brightness_report( fd, usage_ref, rep_info );
if ( mode == MODE_SETREL ) {
brightness = usage_ref.value + amount;
if ( brightness < MIN_BRIGHTNESS )
brightness = MIN_BRIGHTNESS;
if ( brightness > MAX_BRIGHTNESS )
brightness = MAX_BRIGHTNESS;
usage_ref.value = brightness;
/* set calculated brightness */
write_brightness_report( fd, usage_ref, rep_info );
/* read brightness back from device */
read_brightness_report( fd, usage_ref, rep_info );
}
}
if ( !brief )
cout << *it << ": BRIGHTNESS=";
cout << usage_ref.value << endl;
close(fd);
}
}
/* Initializes our knowledge database about Apple Cinema Displays */
void init_device_database() {
supportedVendors.insert( VendorDesc( SAMSUNG, "Samsung Electronics" ) );
supportedVendors.insert( VendorDesc( APPLE, "Apple" ) );
supportedDevices.insert( DeviceId( APPLE, STUDIO_DISPLAY_15,
"Apple Studio Display 15\"" ));
supportedDevices.insert( DeviceId( APPLE, STUDIO_DISPLAY_17,
"Apple Studio Display 17\"" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_20_NEW,
"Apple Cinema Display 20\" (new)" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_20_OLD,
"Apple Cinema Display 20\" (old)" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_23_NEW,
"Apple Cinema Display 23\" (new)" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_20_ALT,
"Apple Cinema Display 20\" (alt)" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_23_OLD,
"Apple Cinema Display 23\" (old)" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_24,
"Apple Cinema Display 24\"" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_30_NEW,
"Apple Cinema Display 30\"" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_27,
"Apple Cinema HD Display 27\"" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_27_2013,
"Apple Cinema HD Display 27\"" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_DISPLAY_30,
"Apple Cinema HD Display 30\"" ));
supportedDevices.insert( DeviceId( APPLE, CINEMA_LED_DISPLAY_24,
"Apple LED Cinema Display 24\"" ));
supportedDevices.insert( DeviceId( SAMSUNG, S1,
"Samsung SyncMaster 757NF" ));
}
void dump_supported () {
for ( SupportedDevices::iterator it = supportedDevices.begin();
it != supportedDevices.end(); ++it )
cout << "Vendor=" << setw( 6 ) << hex << showbase << it->vendor
<< " (" << supportedVendors[ it->vendor ] << "), "
<< "Product=" << it->product << " ["
<< it->description << "]" << endl;
}