source: trunk/LocationDo/brightness.m@ 581

Last change on this file since 581 was 548, checked in by Nicholas Riley, 15 years ago

brightness.m: Port command-line argument syntax from brightness.c.

File size: 3.5 KB
Line 
1/* gcc -std=c99 -o brightness -framework Cocoa -framework DisplayServices -F/System/Library/PrivateFrameworks brightness.m */
2
3#import <Foundation/Foundation.h>
4#include <crt_externs.h>
5
6@interface O3Manager : NSObject
7+ (void)initialize;
8+ (id)engineOfClass:(NSString *)cls forDisplayID:(CGDirectDisplayID)fp12;
9@end
10
11@protocol O3EngineWireProtocol
12@end
13
14@protocol BrightnessEngineWireProtocol <O3EngineWireProtocol>
15- (float)brightness;
16- (BOOL)setBrightness:(float)fp8;
17- (void)bumpBrightnessUp;
18- (void)bumpBrightnessDown;
19@end
20
21const int kMaxDisplays = 16;
22
23#define APP_NAME *(_NSGetProgname())
24
25void errexit(const char *fmt, ...) {
26 va_list ap;
27 va_start(ap, fmt);
28 fprintf(stderr, "%s: ", APP_NAME);
29 vfprintf(stderr, fmt, ap);
30 fprintf(stderr, "\n");
31 exit(1);
32}
33
34void usage() {
35 fprintf(stderr, "usage: %s [-m|-d display] [-v] <brightness>\n", APP_NAME);
36 fprintf(stderr, " or: %s -l [-v]\n", APP_NAME);
37 exit(1);
38}
39
40int main(int argc, char * const argv[]) {
41 if (argc == 1)
42 usage();
43
44 BOOL verbose = NO;
45 unsigned long displayToSet = 0;
46 enum { ACTION_LIST, ACTION_SET_ALL, ACTION_SET_ONE } action = ACTION_SET_ALL;
47 extern char *optarg;
48 extern int optind;
49 int ch;
50
51 while ( (ch = getopt(argc, argv, "lmvd:")) != -1) {
52 switch (ch) {
53 case 'l':
54 if (action == ACTION_SET_ONE) usage();
55 action = ACTION_LIST;
56 break;
57 case 'v':
58 verbose = YES;
59 break;
60 case 'm':
61 if (action != ACTION_SET_ALL) usage();
62 action = ACTION_SET_ONE;
63 displayToSet = (unsigned long)CGMainDisplayID();
64 break;
65 case 'd':
66 if (action != ACTION_SET_ALL) usage();
67 action = ACTION_SET_ONE;
68 errno = 0;
69 displayToSet = strtoul(optarg, NULL, 0);
70 if (errno == EINVAL || errno == ERANGE)
71 errexit("display must be an integer index (0) or a hexadecimal ID (0x4270a80)");
72 break;
73 default: usage();
74 }
75 }
76
77 argc -= optind;
78 argv += optind;
79
80 float brightness;
81 if (action == ACTION_LIST) {
82 if (argc > 0) usage();
83 } else {
84 if (argc != 1) usage();
85
86 errno = 0;
87 brightness = strtof(argv[0], NULL);
88 if (errno == ERANGE)
89 usage();
90 if (brightness < 0 || brightness > 1)
91 errexit("brightness must be between 0 and 1");
92 }
93
94 [[NSAutoreleasePool alloc] init];
95 [O3Manager initialize];
96
97 CGDirectDisplayID display[kMaxDisplays];
98 CGDisplayCount numDisplays;
99 CGDisplayErr err;
100 err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);
101 if (err != CGDisplayNoErr)
102 errexit("cannot get list of displays (error %d)\n", err);
103
104 for (CGDisplayCount i = 0; i < numDisplays; ++i) {
105 CGDirectDisplayID dspy = display[i];
106 CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
107 if (originalMode == NULL)
108 continue;
109
110 if (action == ACTION_LIST) {
111 printf("display %d: ", i);
112 if (CGMainDisplayID() == dspy)
113 printf("main display, ");
114 printf("ID 0x%x\n", (unsigned int)dspy);
115 if (verbose) {
116 puts([[(NSDictionary *)originalMode description] UTF8String]);
117 }
118 }
119
120 if ([[(NSDictionary *)originalMode objectForKey: @"RefreshRate"] intValue] != 0)
121 continue;
122
123 id<BrightnessEngineWireProtocol> engine =
124 [O3Manager engineOfClass: @"BrightnessEngine" forDisplayID: dspy];
125
126 switch (action) {
127 case ACTION_SET_ONE:
128 if ((CGDirectDisplayID)displayToSet != dspy && displayToSet != i)
129 continue;
130 case ACTION_SET_ALL:
131 [engine setBrightness: brightness];
132 if (!verbose) continue;
133 case ACTION_LIST:
134 printf("display %d: brightness %f\n", i, [engine brightness]);
135 }
136 }
137 return 0;
138}
Note: See TracBrowser for help on using the repository browser.