forked from coolstar/crosecservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardBacklight.cpp
151 lines (123 loc) · 4.53 KB
/
KeyboardBacklight.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
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
#include <Windows.h>
#include <cstdio>
#include <thread>
#include "croskbhid.h"
#include "KeyboardBacklight.h"
KeyboardBacklight::KeyboardBacklight(pcroskbhid_client client) {
this->client = client;
this->HasKeyboardBacklight = croskblight_connect(client);
if (this->HasKeyboardBacklight) {
this->task = std::thread(&KeyboardBacklight::FadeBrightnessStep, this);
}
this->InstantlySet(this->InternalBrightness);
}
KeyboardBacklight::~KeyboardBacklight() {
if (this->HasKeyboardBacklight) {
this->HasKeyboardBacklight = FALSE;
this->task.join();
croskblight_disconnect(client);
}
}
BOOL KeyboardBacklight::GetBacklightEnabled() {
return BacklightEnabled;
}
void KeyboardBacklight::SetBacklightEnabled(BOOL enabled) {
this->BacklightEnabled = enabled;
this->InstantlySet(this->InternalBrightness);
}
INT KeyboardBacklight::GetBrightness() {
return this->CurrentBrightness;
}
void KeyboardBacklight::FadeSetInternal(INT brightness) {
this->BrightnessFadeStep = abs(this->InternalBrightness - brightness) / 6; //do over 6 steps[300 ms]
this->BrightnessFadeTarget = brightness;
}
void KeyboardBacklight::FadeSet(INT brightness) {
this->CurrentBrightness = brightness;
this->BacklightEnabled = TRUE;
this->FadeSetInternal(brightness);
}
void KeyboardBacklight::BrightnessInc() {
INT DesiredBrightness = this->BrightnessFadeTarget;
DesiredBrightness += 10;
if (DesiredBrightness > 100) {
DesiredBrightness = 100;
}
this->FadeSet(DesiredBrightness);
}
void KeyboardBacklight::BrightnessDec() {
INT DesiredBrightness = this->BrightnessFadeTarget;
DesiredBrightness -= 10;
if (DesiredBrightness < 0) {
DesiredBrightness = 0;
}
this->FadeSet(DesiredBrightness);
}
void KeyboardBacklight::FadeBrightnessStep() {
INT TimerAdjustment = 0;
while (this->HasKeyboardBacklight) {
if ((50 - TimerAdjustment) > 0) {
Sleep(50 - TimerAdjustment);
}
ULONGLONG Start = GetTickCount64();
if (this->InternalBrightness == this->BrightnessFadeTarget &&
this->IsLaptopMode == CheckIsLaptopMode()) {
LASTINPUTINFO lastInput;
lastInput.cbSize = sizeof(lastInput);
GetLastInputInfo(&lastInput);
DWORD currentTick = GetTickCount();
if (currentTick - lastInput.dwTime > this->IdleTimeout || lastInput.dwTime > currentTick) {
if (this->BacklightEnabled)
this->FadeSetInternal(0);
}
else if (this->CurrentBrightness != 0 && this->InternalBrightness == 0) {
if (this->BacklightEnabled)
this->FadeSetInternal(this->CurrentBrightness);
}
TimerAdjustment = (INT)(GetTickCount64() - Start);
continue;
}
INT DesiredBrightness = this->InternalBrightness;
INT BrightnessFadeTarget = this->BrightnessFadeTarget;
INT BrightnessFadeStep = this->BrightnessFadeStep;
if ((DesiredBrightness < BrightnessFadeTarget) && (DesiredBrightness + BrightnessFadeStep) > BrightnessFadeTarget)
{
DesiredBrightness = BrightnessFadeTarget;
}
else if ((DesiredBrightness > BrightnessFadeTarget) && (DesiredBrightness - BrightnessFadeStep) < BrightnessFadeTarget)
{
DesiredBrightness = BrightnessFadeTarget;
}
else if (DesiredBrightness < BrightnessFadeTarget) {
DesiredBrightness = DesiredBrightness + BrightnessFadeStep;
}
else if (DesiredBrightness > BrightnessFadeTarget) {
DesiredBrightness = DesiredBrightness - BrightnessFadeStep;
}
this->InstantlySet(DesiredBrightness);
TimerAdjustment = (INT)(GetTickCount64() - Start);
}
}
extern BOOL isTabletConvertible;
BOOL KeyboardBacklight::CheckIsLaptopMode() {
if (!isTabletConvertible) {
return TRUE;
}
return (GetSystemMetrics(SM_CONVERTIBLESLATEMODE) != 0);
}
void KeyboardBacklight::InstantlySet(INT brightness) {
if (brightness > 100)
brightness = 100;
else if (brightness < 0)
brightness = 0;
this->InternalBrightness = brightness;
CrosKBLightSettingsReport report = { 0 };
report.ReportID = REPORTID_KBLIGHT;
report.SetBrightness = 1;
report.Brightness = this->BacklightEnabled ? brightness : 0;
this->IsLaptopMode = CheckIsLaptopMode();
if (!this->IsLaptopMode) {
report.Brightness = 0;
}
croskblight_write_message(client, &report);
}