-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_BrightnessAndVolumeSetter.ahk
239 lines (208 loc) · 10.1 KB
/
class_BrightnessAndVolumeSetter.ahk
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
class BrightnessAndVolumeSetter {
; qwerty12 - 27/05/17
static _WM_POWERBROADCAST := 0x218, _osdHwnd := 0, hPowrprofMod := DllCall("LoadLibrary", "Str", "powrprof.dll", "Ptr")
__New() {
if (BrightnessAndVolumeSetter.IsOnAc(AC))
this._AC := AC
if ((this.pwrAcNotifyHandle := DllCall("RegisterPowerSettingNotification", "Ptr", A_ScriptHwnd, "Ptr", BrightnessAndVolumeSetter._GUID_ACDC_POWER_SOURCE(), "UInt", DEVICE_NOTIFY_WINDOW_HANDLE := 0x00000000, "Ptr"))) ; Sadly the callback passed to *PowerSettingRegister*Notification runs on a new threadl
OnMessage(this._WM_POWERBROADCAST, ((this.pwrBroadcastFunc := ObjBindMethod(this, "_On_WM_POWERBROADCAST"))))
}
__Delete() {
if (this.pwrAcNotifyHandle) {
OnMessage(BrightnessAndVolumeSetter._WM_POWERBROADCAST, this.pwrBroadcastFunc, 0)
,DllCall("UnregisterPowerSettingNotification", "Ptr", this.pwrAcNotifyHandle)
,this.pwrAcNotifyHandle := 0
,this.pwrBroadcastFunc := ""
}
}
SetBrightness(increment, jump := False, showOSD := True, autoDcOrAc := -1, ptrAnotherScheme := 0)
{
static PowerGetActiveScheme := DllCall("GetProcAddress", "Ptr", BrightnessAndVolumeSetter.hPowrprofMod, "AStr", "PowerGetActiveScheme", "Ptr")
,PowerSetActiveScheme := DllCall("GetProcAddress", "Ptr", BrightnessAndVolumeSetter.hPowrprofMod, "AStr", "PowerSetActiveScheme", "Ptr")
,PowerWriteACValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessAndVolumeSetter.hPowrprofMod, "AStr", "PowerWriteACValueIndex", "Ptr")
,PowerWriteDCValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessAndVolumeSetter.hPowrprofMod, "AStr", "PowerWriteDCValueIndex", "Ptr")
,PowerApplySettingChanges := DllCall("GetProcAddress", "Ptr", BrightnessAndVolumeSetter.hPowrprofMod, "AStr", "PowerApplySettingChanges", "Ptr")
if (increment == 0 && !jump) {
if (showOSD)
BrightnessAndVolumeSetter._ShowBrightnessOSD()
return
}
if (!ptrAnotherScheme ? DllCall(PowerGetActiveScheme, "Ptr", 0, "Ptr*", currSchemeGuid, "UInt") == 0 : DllCall("powrprof\PowerDuplicateScheme", "Ptr", 0, "Ptr", ptrAnotherScheme, "Ptr*", currSchemeGuid, "UInt") == 0) {
if (autoDcOrAc == -1) {
if (this != BrightnessAndVolumeSetter) {
AC := this._AC
} else {
if (!BrightnessAndVolumeSetter.IsOnAc(AC))
return
}
} else {
AC := !!autoDcOrAc
}
if (jump || BrightnessAndVolumeSetter._GetCurrentBrightness(currSchemeGuid, AC, currBrightness := 0)) {
maxBrightness := BrightnessAndVolumeSetter.GetMaxBrightness()
,minBrightness := BrightnessAndVolumeSetter.GetMinBrightness()
if (jump || !((currBrightness == maxBrightness && increment > 0) || (currBrightness == minBrightness && increment < minBrightness))) {
if (currBrightness + increment > maxBrightness)
increment := maxBrightness
else if (currBrightness + increment < minBrightness)
increment := minBrightness
else
increment += currBrightness
if (DllCall(AC ? PowerWriteACValueIndex : PowerWriteDCValueIndex, "Ptr", 0, "Ptr", currSchemeGuid, "Ptr", BrightnessAndVolumeSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessAndVolumeSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt", increment, "UInt") == 0) {
; PowerApplySettingChanges is undocumented and exists only in Windows 8+. Since both the Power control panel and the brightness slider use this, we'll do the same, but fallback to PowerSetActiveScheme if on Windows 7 or something
if (!PowerApplySettingChanges || DllCall(PowerApplySettingChanges, "Ptr", BrightnessAndVolumeSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessAndVolumeSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt") != 0)
DllCall(PowerSetActiveScheme, "Ptr", 0, "Ptr", currSchemeGuid, "UInt")
}
}
if (showOSD)
BrightnessAndVolumeSetter._ShowBrightnessOSD()
}
DllCall("LocalFree", "Ptr", currSchemeGuid, "Ptr")
}
}
SetVolume(increment, jump := False, showOSD := True)
{
if (increment == 0 && !jump) {
if (showOSD)
BrightnessAndVolumeSetter._ShowVolumeOSD()
return
}
if (increment > 0 && !jump )
{
increment := "+" . increment
}
SoundSet increment
soundSet 0,,mute
soundGet vol
if (showOSD)
BrightnessAndVolumeSetter._ShowVolumeOSD()
}
IsOnAc(ByRef acStatus)
{
static SystemPowerStatus
if (!VarSetCapacity(SystemPowerStatus))
VarSetCapacity(SystemPowerStatus, 12)
if (DllCall("GetSystemPowerStatus", "Ptr", &SystemPowerStatus)) {
acStatus := NumGet(SystemPowerStatus, 0, "UChar") == 1
return True
}
return False
}
GetDefaultBrightnessIncrement()
{
static ret := 10
DllCall("powrprof\PowerReadValueIncrement", "Ptr", BrightnessAndVolumeSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessAndVolumeSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", ret, "UInt")
return ret
}
GetMinBrightness()
{
static ret := -1
if (ret == -1)
if (DllCall("powrprof\PowerReadValueMin", "Ptr", BrightnessAndVolumeSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessAndVolumeSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", ret, "UInt"))
ret := 0
return ret
}
GetMaxBrightness()
{
static ret := -1
if (ret == -1)
if (DllCall("powrprof\PowerReadValueMax", "Ptr", BrightnessAndVolumeSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessAndVolumeSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", ret, "UInt"))
ret := 100
return ret
}
_GetCurrentBrightness(schemeGuid, AC, ByRef currBrightness)
{
static PowerReadACValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessAndVolumeSetter.hPowrprofMod, "AStr", "PowerReadACValueIndex", "Ptr")
,PowerReadDCValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessAndVolumeSetter.hPowrprofMod, "AStr", "PowerReadDCValueIndex", "Ptr")
return DllCall(AC ? PowerReadACValueIndex : PowerReadDCValueIndex, "Ptr", 0, "Ptr", schemeGuid, "Ptr", BrightnessAndVolumeSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessAndVolumeSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", currBrightness, "UInt") == 0
}
_ShowBrightnessOSD()
{
; Thanks to YashMaster @ https://github.com/YashMaster/Tweaky/blob/master/Tweaky/BrightnessHandler.h for realising this could be done:
BrightnessAndVolumeSetter._ShowOSD(0x37, 0)
}
_ShowVolumeOSD()
{
; Thanks to YashMaster @ https://github.com/YashMaster/Tweaky/blob/master/Tweaky/VolumeHandler.h for realising this could be done:
BrightnessAndVolumeSetter._ShowOSD(0x0C, 0xA0000)
}
_ShowOSD(message1, message2)
{
static PostMessagePtr := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", A_IsUnicode ? "PostMessageW" : "PostMessageA", "Ptr")
,WM_SHELLHOOK := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK", "UInt")
if A_OSVersion in WIN_VISTA,WIN_7
return
BrightnessAndVolumeSetter._RealiseOSDWindowIfNeeded()
if (BrightnessAndVolumeSetter._osdHwnd)
DllCall(PostMessagePtr, "Ptr", BrightnessAndVolumeSetter._osdHwnd, "UInt", WM_SHELLHOOK, "Ptr", message1, "Ptr", message2)
}
_RealiseOSDWindowIfNeeded()
{
static IsWindow := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", "IsWindow", "Ptr")
if (!DllCall(IsWindow, "Ptr", BrightnessAndVolumeSetter._osdHwnd) && !BrightnessAndVolumeSetter._FindAndSetOSDWindow()) {
BrightnessAndVolumeSetter._osdHwnd := 0
try if ((shellProvider := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{00000000-0000-0000-C000-000000000046}"))) {
try if ((flyoutDisp := ComObjQuery(shellProvider, "{41f9d2fb-7834-4ab6-8b1b-73e74064b465}", "{41f9d2fb-7834-4ab6-8b1b-73e74064b465}"))) {
DllCall(NumGet(NumGet(flyoutDisp+0)+3*A_PtrSize), "Ptr", flyoutDisp, "Int", 0, "UInt", 0)
,ObjRelease(flyoutDisp)
}
ObjRelease(shellProvider)
if (BrightnessAndVolumeSetter._FindAndSetOSDWindow())
return
}
; who knows if the SID & IID above will work for future versions of Windows 10 (or Windows 8). Fall back to this if needs must
Loop 2 {
SendEvent {Volume_Mute 2}
if (BrightnessAndVolumeSetter._FindAndSetOSDWindow())
return
Sleep 100
}
}
}
_FindAndSetOSDWindow()
{
static FindWindow := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", A_IsUnicode ? "FindWindowW" : "FindWindowA", "Ptr")
return !!((BrightnessAndVolumeSetter._osdHwnd := DllCall(FindWindow, "Str", "NativeHWNDHost", "Str", "", "Ptr")))
}
_On_WM_POWERBROADCAST(wParam, lParam)
{
;OutputDebug % &this
if (wParam == 0x8013 && lParam && NumGet(lParam+0, 0, "UInt") == NumGet(BrightnessAndVolumeSetter._GUID_ACDC_POWER_SOURCE()+0, 0, "UInt")) { ; PBT_POWERSETTINGCHANGE and a lazy comparison
this._AC := NumGet(lParam+0, 20, "UChar") == 0
return True
}
}
_GUID_VIDEO_SUBGROUP()
{
static GUID_VIDEO_SUBGROUP__
if (!VarSetCapacity(GUID_VIDEO_SUBGROUP__)) {
VarSetCapacity(GUID_VIDEO_SUBGROUP__, 16)
,NumPut(0x7516B95F, GUID_VIDEO_SUBGROUP__, 0, "UInt"), NumPut(0x4464F776, GUID_VIDEO_SUBGROUP__, 4, "UInt")
,NumPut(0x1606538C, GUID_VIDEO_SUBGROUP__, 8, "UInt"), NumPut(0x99CC407F, GUID_VIDEO_SUBGROUP__, 12, "UInt")
}
return &GUID_VIDEO_SUBGROUP__
}
_GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS()
{
static GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__
if (!VarSetCapacity(GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__)) {
VarSetCapacity(GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 16)
,NumPut(0xADED5E82, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 0, "UInt"), NumPut(0x4619B909, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 4, "UInt")
,NumPut(0xD7F54999, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 8, "UInt"), NumPut(0xCB0BAC1D, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 12, "UInt")
}
return &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__
}
_GUID_ACDC_POWER_SOURCE()
{
static GUID_ACDC_POWER_SOURCE_
if (!VarSetCapacity(GUID_ACDC_POWER_SOURCE_)) {
VarSetCapacity(GUID_ACDC_POWER_SOURCE_, 16)
,NumPut(0x5D3E9A59, GUID_ACDC_POWER_SOURCE_, 0, "UInt"), NumPut(0x4B00E9D5, GUID_ACDC_POWER_SOURCE_, 4, "UInt")
,NumPut(0x34FFBDA6, GUID_ACDC_POWER_SOURCE_, 8, "UInt"), NumPut(0x486551FF, GUID_ACDC_POWER_SOURCE_, 12, "UInt")
}
return &GUID_ACDC_POWER_SOURCE_
}
}
BrightnessAndVolumeSetter_new() {
return new BrightnessAndVolumeSetter()
}