-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrightnessControl.cpp
103 lines (87 loc) · 3.07 KB
/
brightnessControl.cpp
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
#include "brightnessControl.h"
#include <algorithm>
#include <Windows.h>
#include <ntddvdeo.h>
#include <qdebug.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <powersetting.h>
#include <PowrProf.h>
#include "powermonitor.h"
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "powrprof.lib")
BrightnessControl::BrightnessControl(){
}
bool BrightnessControl::setBrightness(int level) {
if (level<0){
level = 0;
}
else if (level >100){
level = 100;
}
HANDLE hDevice = CreateFile(L"\\\\.\\LCD",
GENERIC_READ | GENERIC_WRITE,
0,NULL,
OPEN_EXISTING,
0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
qDebug() << "Brightness set: Invalid handle";
qDebug() << "Brightness Set:" << GetLastError() << "\n";
return false;
}
DWORD bytesReturned;
DISPLAY_BRIGHTNESS displayBrightness = DISPLAY_BRIGHTNESS{ DISPLAYPOLICY_BOTH , (UCHAR)level, (UCHAR)level};
bool result = DeviceIoControl(hDevice, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, &displayBrightness, sizeof(displayBrightness), NULL, 0, &bytesReturned, NULL);
if (result) {
current = level;
}
else{
qDebug() << "Brightness Set:" << GetLastError() << "\n";
return false;
}
//update power scheme. (windows sometimes will auto update, sometimes won't, bug?)
GUID *pActivePolicyGuid;
DWORD success = PowerGetActiveScheme(NULL, &pActivePolicyGuid);
if (success!=ERROR_SUCCESS) qDebug() << "SetBrightness: error get active scheme";
if(PowerMonitor::isOnAC()){
success = PowerWriteACValueIndex(NULL,pActivePolicyGuid,&GUID_VIDEO_SUBGROUP,&guidDisplayBrightness, level);
}
else{
success = PowerWriteDCValueIndex(NULL,pActivePolicyGuid,&GUID_VIDEO_SUBGROUP,&guidDisplayBrightness, level);
}
if (success!=ERROR_SUCCESS) qDebug() << "SetBrightness: error set brightness in power scheme";
LocalFree(pActivePolicyGuid);
qDebug()<<"Brightness set to "<<level;
CloseHandle(hDevice);
return result;
}
/*
* Return -1 if failed to get current brightness
*/
int BrightnessControl::getCurrentBrightness(bool refresh){
if(!refresh)
return current;
//get active scheme
GUID *pActivePolicyGuid;
DWORD result = PowerGetActiveScheme(NULL, &pActivePolicyGuid);
if (result!=ERROR_SUCCESS) {
qDebug() << "getBrightness: error get active scheme";
return -1;
}
//read brightness in power scheme
DWORD currentBrightness;
if (PowerMonitor::isOnAC()){
result = PowerReadACValueIndex(NULL,pActivePolicyGuid,&GUID_VIDEO_SUBGROUP,&guidDisplayBrightness,¤tBrightness);
}
else{
result = PowerReadDCValueIndex(NULL,pActivePolicyGuid,&GUID_VIDEO_SUBGROUP,&guidDisplayBrightness,¤tBrightness);
}
if(result!=ERROR_SUCCESS) {
qDebug()<<"Error get brightness";
return -1;
}
LocalFree(pActivePolicyGuid);
return currentBrightness;
}
BrightnessControl::~BrightnessControl(){
}