-
Notifications
You must be signed in to change notification settings - Fork 2
/
Laptop.cpp
333 lines (260 loc) · 8.74 KB
/
Laptop.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//
// Created by alex on 09/01/2022.
//
#include "Laptop.h"
namespace librazerblade {
Laptop::Laptop(LaptopDescription description, UsbHandle usbHandle, UsbDevice device) :
description(description),
usbHandle(usbHandle),
device(device),
state{0}
{
}
UsbDevice Laptop::getDevice() const
{
return device;
}
UsbHandle Laptop::getUsbHandle() const
{
return usbHandle;
}
void Laptop::setUsbHandle(UsbHandle v)
{
LockGuard autoLockq(queryMutex);
LockGuard lock(laptopMutex);
this->usbHandle = v;
}
uint8_t Laptop::clampFan(int32_t v) const
{
if (v <= 0)
return 0;
v = std::max(std::min(v, description.fan.maxFanSpeed), description.fan.minFanSpeed);
return v / 100;
}
BladeCapabilities Laptop::getCapabilities() const
{
return description.capabilities;
}
LaptopDescription Laptop::getDescription()
{
LockGuard autoLockq(queryMutex);
LockGuard autoLock(laptopMutex);
return description;
}
LaptopState Laptop::getState()
{
LockGuard autoLockq(queryMutex);
LockGuard autoLock(laptopMutex);
return state;
}
LaptopState* Laptop::getStatePtrUnsafe()
{
LockGuard autoLockq(queryMutex);
LockGuard autoLock(laptopMutex);
return &state;
}
UsbPacketResult Laptop::queryFanSpeed(int numRetries)
{
LockGuard autoLock(laptopMutex);
auto pkt = PacketFactory::fan(0, 1, Get);
auto output = PacketFactory::empty();
auto result = sendPacketWithRetry(&pkt, &output, numRetries);
if (result.isSuccess) {
state.fanSpeed = PacketUtil::getFanValueRaw(&output);
}
return result;
}
UsbPacketResult Laptop::queryBoost(int numRetries)
{
LockGuard autoLock(laptopMutex);
auto pkt = PacketFactory::boost(BoostCpu, 0, Get);
auto output = PacketFactory::empty();
auto result = sendPacketWithRetry(&pkt, &output, numRetries);
if (result.isSuccess) {
state.cpuBoost = PacketUtil::getBoostValue(&output);
}else{
return result;
}
pkt = PacketFactory::boost(BoostGpu, 0, Get);
output = PacketFactory::empty();
result = sendPacketWithRetry(&pkt, &output, numRetries);
if (result.isSuccess) {
state.gpuBoost = PacketUtil::getBoostValue(&output);
}
return result;
}
UsbPacketResult Laptop::queryPowerMode(int numRetries)
{
LockGuard autoLock(laptopMutex);
auto pkt = PacketFactory::power(0, false, Get);
auto output = PacketFactory::empty();
auto result = sendPacketWithRetry(&pkt, &output, numRetries);
if (result.isSuccess) {
state.powerMode = PacketUtil::getPowerMode(&output);
state.manualFanSpeed = PacketUtil::getManualFanSpeed(&output);
}
return result;
}
UsbPacketResult Laptop::queryBrightness(int numRetries)
{
LockGuard autoLock(laptopMutex);
auto pkt = PacketFactory::brightness(0, Get);
auto output = PacketFactory::empty();
auto result = sendPacketWithRetry(&pkt, &output, numRetries);
if (result.isSuccess) {
state.keyboardInfo.brightness = PacketUtil::getBrightness(&output);
}
return result;
}
UsbPacketResult Laptop::queryChromaRow(int rowId, int numRetries)
{
LockGuard autoLock(laptopMutex);
KeyboardRow row;
row.rowid = rowId;
auto pkt = PacketFactory::row(row, Get);
auto output = PacketFactory::empty();
auto result = sendPacketWithRetry(&pkt, &output, numRetries);
if (result.isSuccess) {
row = PacketUtil::getRow(&output);
if (row.rowid != rowId) {
result.isSuccess = 0;
result.sendResult = RazerResponseDoesntMatch;
return result;
}
state.keyboardInfo.rows[rowId] = row;
}
return result;
}
LaptopQueryResult Laptop::query(BladeQuery query, int numRetries)
{
LockGuard autoLock(queryMutex);
LaptopQueryResult result{0};
if (query & BladeQuery::QueryBrightness) {
result.results[getArrayIndex(BladeQuery::QueryBrightness)] = queryBrightness(numRetries);
}
if (query & BladeQuery::QueryBoost) {
result.results[getArrayIndex(BladeQuery::QueryBoost)] = queryBoost(numRetries);
}
if (query & BladeQuery::QueryFanSpeed) {
result.results[getArrayIndex(BladeQuery::QueryFanSpeed)] = queryFanSpeed(numRetries);
}
if (query & BladeQuery::QueryPowerMode) {
result.results[getArrayIndex(BladeQuery::QueryPowerMode)] = queryPowerMode(numRetries);
}
return result;
}
LaptopQueryResult Laptop::queryRows(BladeQueryRows rows, int numRetries)
{
LockGuard autoLock(queryMutex);
LaptopQueryResult result{0};
for (int i = 0; i < BladeQueryRows::QueryRowCount; i++) {
auto check = (BladeQueryRows) (1 << i);
if ((rows & check) == 0)
continue;
result.results[i] = queryChromaRow(i, numRetries);
}
return result;
}
UsbPacketResult Laptop::setFanSpeed(int32_t speed, int numRetries, int fanId, bool clampSpeed)
{
if (speed < 0)
speed = 0;
auto clamped = this->clampFan(speed);
if (!clampSpeed)
clamped = speed / 100;
auto pkt = PacketFactory::fan(clamped, fanId, Set);
auto output = PacketFactory::empty();
auto r = sendPacketWithRetry(&pkt, &output, numRetries);
if (r.isSuccess) {
state.fanSpeed = clamped;
}
return r;
}
UsbPacketResult Laptop::setBoost(BladeBoostId boostId, uint8_t value, int numRetries)
{
auto pkt = PacketFactory::boost(boostId, value, Set);
auto output = PacketFactory::empty();
auto r = sendPacketWithRetry(&pkt, &output, numRetries);
if (r.isSuccess) {
if(boostId == BoostCpu)
state.cpuBoost = value;
else if(boostId == BoostGpu)
state.gpuBoost = value;
}
return r;
}
UsbPacketResult Laptop::setPowerMode(uint8_t powerMode, bool autoFanSpeed, int numRetries)
{
if (powerMode > 4)
powerMode = 0;
auto pkt = PacketFactory::power(powerMode, autoFanSpeed);
auto output = PacketFactory::empty();
auto r = sendPacketWithRetry(&pkt, &output, numRetries);
if (r.isSuccess) {
state.powerMode = powerMode;
state.manualFanSpeed = autoFanSpeed ? 0 : 1;
}
return r;
}
UsbPacketResult Laptop::setBrightness(uint8_t brightness, int numRetries)
{
auto pkt = PacketFactory::brightness(brightness);
auto output = PacketFactory::empty();
auto r = sendPacketWithRetry(&pkt, &output, numRetries);
if (r.isSuccess) {
state.keyboardInfo.brightness = brightness;
}
return r;
}
UsbPacketResult Laptop::applyChroma(int numRetries)
{
auto pkt = PacketFactory::applyChroma();
auto output = PacketFactory::empty();
auto r = sendPacketWithRetry(&pkt, &output, numRetries);
return r;
}
UsbPacketResult Laptop::sendKeyboardRow(KeyboardRow row, int numRetries)
{
if (row.rowid > 5)
row.rowid = 0;
auto pkt = PacketFactory::row(row);
auto output = PacketFactory::empty();
auto r = sendPacketWithRetry(&pkt, &output, numRetries);
if (r.isSuccess) {
state.keyboardInfo.rows[row.rowid] = row;
}
return r;
}
int Laptop::getArrayIndex(int32_t q)
{
int idx = 0;
while ((q & 0x01) == 0) {
idx++;
q >>= 1;
}
return idx;
}
UsbPacketResult
Laptop::sendPacketWithRetry(RazerPacket* packet, RazerPacket* output, int numRetries, int retryIntervalMs)
{
UsbPacketResult result{};
do {
auto pktResult = gUsb.sendPayload(usbHandle.handle, packet, 1000, &result, device.aux);
if (output != nullptr)
*output = pktResult;
if (result.isSuccess > 0) {
break;
}
numRetries--;
Utils::usleep(retryIntervalMs * 1000);
} while (numRetries > 0);
return result;
}
Laptop::~Laptop()
{
if (usbHandle.autoRelease) {
usbHandle.autoRelease = false;
gUsb.closeHandle(usbHandle.handle, &device);
}
}
}