forked from unknownzerx/lguf-brightness
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.cpp
317 lines (282 loc) · 8.93 KB
/
main.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
/*
busb_get_device_descriptor* libusb example program to list devices on the bus
* Copyright © 2007 Daniel Drake <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <iostream>
// #include <stdio.h>
#include <vector>
#include <libusb.h>
#include <curses.h>
// From the HID spec:
static const int HID_GET_REPORT = 0x01;
static const int HID_SET_REPORT = 0x09;
static const int HID_REPORT_TYPE_INPUT = 0x01;
static const int HID_REPORT_TYPE_OUTPUT = 0x02;
static const int HID_REPORT_TYPE_FEATURE = 0x03;
using std::vector;
const uint16_t vendor_id = 0x43e;
const uint16_t product_id = 0x9a40;
const uint16_t max_brightness = 0xd2f0;
const uint16_t min_brightness = 0x0000;
const std::vector<uint16_t> small_steps = {
0x0000,
0x0190, 0x01af, 0x01d2, 0x01f7,
0x021f, 0x024a, 0x0279, 0x02ac,
0x02e2, 0x031d, 0x035c, 0x03a1,
0x03eb, 0x043b, 0x0491, 0x04ee,
0x0553, 0x05c0, 0x0635, 0x06b3,
0x073c, 0x07d0, 0x086f, 0x091b,
0x09d5, 0x0a9d, 0x0b76, 0x0c60,
0x0d5c, 0x0e6c, 0x0f93, 0x10d0,
0x1227, 0x1399, 0x1529, 0x16d9,
0x18aa, 0x1aa2, 0x1cc1, 0x1f0b,
0x2184, 0x2430, 0x2712, 0x2a2e,
0x2d8b, 0x312b, 0x3516, 0x3951,
0x3de2, 0x42cf, 0x4822, 0x4de1,
0x5415, 0x5ac8, 0x6203, 0x69d2,
0x7240, 0x7b5a, 0x852d, 0x8fc9,
0x9b3d, 0xa79b, 0xb4f5, 0xc35f,
0xd2f0};
const std::vector<uint16_t> big_steps = {
0x0000,
0x0190, 0x021f, 0x02e2, 0x03eb,
0x0553, 0x073c, 0x09d5, 0x0d5c,
0x1227, 0x18aa, 0x2184, 0x2d8b,
0x3de2, 0x5415, 0x7240, 0x9b3d,
0xd2f0};
static libusb_device *get_lg_ultrafine_usb_device(libusb_device **devs)
{
libusb_device *dev, *lgdev = NULL;
int i = 0, j = 0;
uint8_t path[8];
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0)
{
printw("failed to get device descriptor");
return NULL;
}
if (desc.idVendor == vendor_id && desc.idProduct == product_id)
{
lgdev = dev;
}
// r = libusb_get_port_numbers(dev, path, sizeof(path));
// if (r > 0)
// {
// printw(" path: %d", path[0]);
// for (j = 1; j < r; j++)
// printw(".%d", path[j]);
// }
// printw("\n");
}
return lgdev;
}
uint16_t next_step(uint16_t val, const vector<uint16_t> &steps)
{
auto start = 0;
auto end = steps.size() - 1;
while (start + 1 < end)
{
auto mid = start + (end - start) / 2;
if (steps[mid] > val)
{
end = mid;
}
else
{
start = mid;
}
}
return steps[end];
}
uint16_t prev_step(uint16_t val, const vector<uint16_t> &steps)
{
auto start = 0;
auto end = steps.size() - 1;
while (start + 1 < end)
{
auto mid = start + (end - start) / 2;
if (steps[mid] >= val)
{
end = mid;
}
else
{
start = mid;
}
}
return steps[start];
}
uint16_t get_brightness(libusb_device_handle *handle)
{
u_char data[8] = {0x00};
// int res = hid_get_feature_report(handle, data, sizeof(data));
int res = libusb_control_transfer(handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 1, data, sizeof(data), 0);
if (res < 0)
{
printw("Unable to get brightness.\n");
printw("libusb_control_transfer error: %s (%d)\n", libusb_error_name(res), res);
}
else {
// for (int i = 0; i < sizeof(data); i++) {
// printw("0x%x ", data[i]);
// }
// printw("\n");
}
uint16_t val = data[0] + (data[1] << 8);
// printw("val=%d (0x%x 0x%x 0x%x)\n", val, data[0], data[1], data[2]);
return val;
}
void set_brightness(libusb_device_handle *handle, uint16_t val)
{
u_char data[6] = {
u_char(val & 0x00ff),
u_char((val >> 8) & 0x00ff), 0x00, 0x00, 0x00, 0x00 };
// int res = hid_send_feature_report(handle, data, sizeof(data));
int res = libusb_control_transfer(handle,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
HID_SET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 1, data, sizeof(data), 0);
if (res < 0)
{
printw("Unable to set brightness.\n");
printw("libusb_control_transfer error: %s\n", libusb_error_name(res));
}
// else {
// get_brightness(handle);
// }
}
void adjust_brighness(libusb_device_handle *handle)
{
auto brightness = get_brightness(handle);
printw("Press '-' or '=' to adjust brightness.\n");
printw("Press '[' or: ']' to fine tune.\n");
printw("Press 'p' to use the minimum brightness\n");
printw("Press '\\' to use the maximum brightness\n");
printw("Press 'q' to quit.\n");
bool stop = false;
while (not stop)
{
printw("Current brightness = %d%4s\r", int((float(brightness) / 54000) * 100.0), " ");
int c = getch();
switch (c)
{
case '+':
case '=':
brightness = next_step(brightness, big_steps);
set_brightness(handle, brightness);
break;
case '-':
case '_':
brightness = prev_step(brightness, big_steps);
set_brightness(handle, brightness);
break;
case ']':
brightness = next_step(brightness, small_steps);
set_brightness(handle, brightness);
break;
case '[':
brightness = prev_step(brightness, small_steps);
set_brightness(handle, brightness);
break;
case '\\':
brightness = max_brightness;
set_brightness(handle, brightness);
break;
case 'p':
brightness = min_brightness;
set_brightness(handle, brightness);
break;
case 'q':
case '\n':
printw("You pressed q.\n");
stop = true;
break;
default:
break;
}
}
}
int main(void)
{
libusb_device **devs, *lgdev;
int r, openCode, iface = 1;
ssize_t cnt;
libusb_device_handle *handle;
initscr();
noecho();
cbreak();
r = libusb_init(NULL);
#if LIBUSB_API_VERSION >= 0x01000106
libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_WARNING);
#else
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_WARNING); // LIBUSB_LOG_LEVEL_DEBUG
#endif
if (r < 0)
{
printw("Unable to initialize libusb.\n");
endwin();
return r;
}
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
{
printw("Unable to get USB device list (%ld).\n", cnt);
endwin();
return (int)cnt;
}
lgdev = get_lg_ultrafine_usb_device(devs);
if (lgdev == NULL)
{
printw("Failed to get LG screen device.\n");
endwin();
return -1;
}
openCode = libusb_open(lgdev, &handle);
if (openCode == 0)
{
libusb_set_auto_detach_kernel_driver(handle, 1);
// r = libusb_detach_kernel_driver(handle, iface);
// if (r == LIBUSB_SUCCESS) {
r = libusb_claim_interface(handle, iface);
if (r == LIBUSB_SUCCESS) {
adjust_brighness(handle);
libusb_release_interface(handle, iface);
libusb_attach_kernel_driver(handle, iface);
} else {
printw("Failed to claim interface %d. Error: %d\n", iface, r);
printw("Error: %s\n", libusb_error_name(r));
}
// } else {
// printw("Failed to detach interface %d. Error: %d\n", iface, r);
// printw("Error: %s\n", libusb_error_name(r));
// }
libusb_close(handle);
}
else
{
printw("libusb_open failed and returned %d\n", openCode);
}
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
getch();
endwin();
return 0;
}