/
DisplayBrightnessAction.m
172 lines (127 loc) · 4.27 KB
/
DisplayBrightnessAction.m
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
//
// DisplayBrightnessAction.m
// ControlPlane
//
// Created by David Jennes on 02/09/11.
// Modifiedy by Dustin Rue on 19/11/11.
// inspired by http://dev.sabi.net/trac/dev/browser/trunk/LocationDo/brightness.m
//
// Copyright 2011. All rights reserved.
//
#import "DisplayBrightnessAction.h"
#import "DSLogger.h"
#import <IOKit/graphics/IOGraphicsLib.h>
#pragma mark Magic Bits!
@interface O3Manager : NSObject
+ (void)initialize;
+ (id)engineOfClass:(NSString *)cls forDisplayID:(CGDirectDisplayID)fp12;
@end
@protocol O3EngineWireProtocol
@end
@protocol BrightnessEngineWireProtocol <O3EngineWireProtocol>
- (float)brightness;
- (BOOL)setBrightness:(float)fp8;
- (void)bumpBrightnessUp;
- (void)bumpBrightnessDown;
@end
@interface DisplayBrightnessAction (Private)
+ (void) setBrightness: (float) brightness;
@end
@implementation DisplayBrightnessAction
- (id) init {
self = [super init];
if (!self)
return nil;
brightnessText = [[NSString alloc] init];
brightness = 100;
return self;
}
- (id) initWithDictionary: (NSDictionary *) dict {
self = [super initWithDictionary: dict];
if (!self)
return nil;
brightnessText = [[dict valueForKey: @"parameter"] copy];
brightness = (unsigned int) [brightnessText intValue];
// must be between 0 and 100
brightness = (brightness > 100) ? 100 : brightness;
brightnessText = [[NSString stringWithFormat: @"%d", brightness] copy];
return self;
}
- (void) dealloc {
[brightnessText release];
[super dealloc];
}
- (NSMutableDictionary *) dictionary {
NSMutableDictionary *dict = [super dictionary];
[dict setObject: [[brightnessText copy] autorelease] forKey: @"parameter"];
return dict;
}
- (NSString *) description {
return [NSString stringWithFormat: NSLocalizedString(@"Set brightness to %@%%.", @""), brightnessText];
}
- (BOOL) execute: (NSString **) errorString {
// get system version
SInt32 major = 0, minor = 0;
Gestalt(gestaltSystemVersionMajor, &major);
Gestalt(gestaltSystemVersionMinor, &minor);
if (major == 10 && minor > 7) {
*errorString = NSLocalizedString(@"DislplayBrightness is currently not supported on this version of OS X", @"");
return NO;
}
[O3Manager initialize];
const int kMaxDisplays = 16;
//const CFStringRef kDisplayBrightness = CFSTR(kIODisplayBrightnessKey);
BOOL errorOccurred = NO;
CGDirectDisplayID display[kMaxDisplays];
CGDisplayCount numDisplays;
CGDisplayErr err;
// get list of displays
err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);
if (err != CGDisplayNoErr) {
DSLog(@"Cannot get list of displays (error %d)", err);
errorOccurred = YES;
}
else {
#if DEBUG_MODE
DSLog(@"There are %d display[s] connected", numDisplays);
#endif
}
// loop through displays
for (CGDisplayCount i = 0; i < numDisplays; ++i) {
CGDirectDisplayID dspy = display[i];
//io_service_t service = CGDisplayIOServicePort(dspy);
// set brightness
id<BrightnessEngineWireProtocol> engine =
[O3Manager engineOfClass: @"BrightnessEngine" forDisplayID: dspy];
//err = IODisplaySetFloatParameter(service, kNilOptions, kDisplayBrightness, (brightness / 100.0));
[engine setBrightness: brightness / 100.0];
/*
if (err == kIOReturnUnsupported) {
DSLog(@"Failed to set brightness of display 0x%x because the system reported it wasn't a supported operation", (unsigned int)dspy);
}
else if (err != kIOReturnSuccess) {
DSLog(@"Failed to set brightness of display 0x%x for an unknown reason, error was: 0x%x", (unsigned int)dspy, err);
errorOccurred = YES;
continue;
}
*/
}
if (errorOccurred) {
*errorString = [NSString stringWithFormat: NSLocalizedString(@"Failed setting brightness to %@%%.", @""), brightnessText];
return NO;
} else
return YES;
}
+ (NSString *) helpText {
return NSLocalizedString(@"The parameter for the Display Brightness action is the brightness value as a percent between 0 and 100.", @"");
}
+ (NSString *) creationHelpText {
return NSLocalizedString(@"Set display brightness to (percent):", @"");
}
+ (NSString *) friendlyName {
return NSLocalizedString(@"Display Brightness", @"");
}
+ (NSString *)menuCategory {
return NSLocalizedString(@"System Preferences", @"");
}
@end