-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericBrightness.cpp
More file actions
156 lines (125 loc) · 4.53 KB
/
GenericBrightness.cpp
File metadata and controls
156 lines (125 loc) · 4.53 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
#include "GenericBrightness.h"
#define super IOService
OSDefineMetaClassAndStructors(GenericBrightness, IOService)
IOService* GenericBrightness::probe( IOService *provider, SInt32 *score )
{
if( !super::probe( provider, score )) return( 0 );
return (this);
}
bool GenericBrightness::start( IOService * provider )
{
if(!provider || !super::start(provider))
return false;
BrightnessMethods = (IOACPIPlatformDevice *) provider;
provider->joinPMtree(this);
this->registerService(0);
if (GetBrightnessLevels()==0) return false;
IOLog("Acpi brightness levels:%d, lowest brightness:%d, highest brightness:%d \n ", (int)brightnessIndex-2, (int)GetValueFromArray(brightnessTable,2), (int)GetValueFromArray(brightnessTable, brightnessIndex-1) );
BTWorkLoop = getWorkLoop();
BTPollTimer = IOTimerEventSource::timerEventSource(this, OSMemberFunctionCast(IOTimerEventSource::Action, this, &GenericBrightness::BrightnessCheck));
if (!BTWorkLoop || !BTPollTimer || (kIOReturnSuccess != BTWorkLoop->addEventSource(BTPollTimer)))
{
IOLog("Timer not working\n"); return false;
}
BrightnessCheck();
return true;
}
OSDictionary * getDisplayParams(IODisplay * display){
OSDictionary * params = NULL;
if(display && (display->getProperty("IODisplayGUID") != 0))
{
params = OSDynamicCast(OSDictionary, display->getProperty("IODisplayParameters"));
}
return params;
}
IODisplay * GenericBrightness::getDisplay(void){
IODisplay * displayRes;
OSIterator * displayList;
displayList = getMatchingServices(serviceMatching("IOBacklightDisplay"));
if (displayList)
{
IOService *obj = NULL;
while( (obj = (IOService *) displayList->getNextObject()) )
{
displayRes = OSDynamicCast( IOBacklightDisplay, obj);
}
displayList->release();
displayList = NULL;
}
return displayRes;
}
void GenericBrightness::BrightnessCheck(void)
{
if (!displayParams || !display || display != getDisplay() || getDisplayParams(display)!=displayParams || !IODisplay::getIntegerRange(displayParams, gIODisplayBrightnessKey,
&fCurrentBrightness, &fMinBrightness, &fMaxBrightness))
{
IOLog("We still don't have brightness entry in ioreg... waiting...\n");
display = getDisplay();
if(display){
displayParams = getDisplayParams(display);
}
BTPollTimer->setTimeoutMS(10000);
}
else {
BTPollTimer->setTimeoutMS(1000);
}
if (fLastBrightness != fCurrentBrightness)
{
fLastBrightness = fCurrentBrightness;
// IOLog("brightnessmin %d, brightnessmax %d, currentbrightness %d\n", (int)fMinBrightness, (int)fMaxBrightness, (int)fCurrentBrightness);
// SInt32 fHardBrightness=fCurrentBrightness/(fMaxBrightness/(brightnessIndex-2))+2;
// SInt32 fHardBrightness=((fCurrentBrightness*(brightnessIndex-3))/fMaxBrightness)+2;
// if (fHardBrightness>brightnessIndex-1) fHardBrightness=brightnessIndex-1;
// IOLog("Index of brightness table:%d , brightness to call:%d \n", (int)fHardBrightness, (int)GetValueFromArray(brightnessTable,fHardBrightness));
OSObject * param = OSNumber::withNumber(GetValueFromArray(brightnessTable,((fCurrentBrightness*(brightnessIndex-3))/fMaxBrightness)+2),8);
SetBrightness("_BCM", param);
}
}
IOReturn GenericBrightness::SetBrightness(const char * method, OSObject * param)
{
OSObject * acpi;
if (BrightnessMethods->evaluateObject(method, &acpi, ¶m, 1) != kIOReturnSuccess)
{
IOLog("%s: No object of method %s\n", this->getName(), method);
return 0;
}
return 1;
}
IOReturn GenericBrightness::GetBrightnessLevels(void)
{
OSObject * brightnessLevels;
if (kIOReturnSuccess == BrightnessMethods->evaluateObject("_BCL", &brightnessLevels)) {
brightnessTable = OSDynamicCast(OSArray, brightnessLevels);
brightnessIndex = brightnessTable->getCount();
return brightnessTable->getCount();
} else {
return 0;
}
}
UInt32 GenericBrightness::GetValueFromArray(OSArray * array, UInt8 index) {
OSObject * object = array->getObject(index);
if (object && (OSTypeIDInst(object) == OSTypeID(OSNumber))) {
OSNumber * number = OSDynamicCast(OSNumber, object);
if (number) return number->unsigned32BitValue();
}
return -1;
}
bool GenericBrightness::init(OSDictionary *properties)
{
return super::init(properties);
}
void GenericBrightness::stop( IOService * provider )
{
if( BTPollTimer ) {
BTWorkLoop->removeEventSource(BTPollTimer);
BTPollTimer->release();
}
if( BTWorkLoop ) {
BTWorkLoop->release();
}
super::stop( provider );
}
void GenericBrightness::free ()
{
super::free ();
}