-
Notifications
You must be signed in to change notification settings - Fork 28
/
k480_conf.c
205 lines (185 loc) · 5.33 KB
/
k480_conf.c
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
/*
* Copyright 2015 Emir Bucalovic <[email protected]> www.betoneful.com
* based on:
* k810_conf.c by Mario Scholz <[email protected]>
* and pairing_tool.c from Benjamin Tissoires <[email protected]>
* see also https://lkml.org/lkml/2011/9/22/367
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/input.h>
#include <linux/hidraw.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#define HID_VENDOR_ID_LOGITECH (__u32)0x046d
//If using k810 #define HID_DEVICE_ID_K810 (__s16)0xb319
#define HID_DEVICE_ID_K480 (__s16)0xb330
#define HID_DEVICE_ID_K480_ALT (__s16)0xb33c
// The folowing HID_DEVICE_ID_K480_ALT2 uses the k380 key sequences!
#define HID_DEVICE_ID_K480_ALT2 (__s16)0xb33d
const char k480_seq_fkeys_on[] = {0x10, 0xff, 0x08, 0x1c, 0x00, 0x00, 0x00};
const char k480_seq_fkeys_off[] = {0x10, 0xff, 0x08, 0x1c, 0x01, 0x00, 0x00};
// from: https://github.com/jergusg/k380-function-keys-conf/blob/master/k380_conf.c
const char k380_seq_fkeys_on[] = {0x10, 0xff, 0x0b, 0x1e, 0x00, 0x00, 0x00};
const char k380_seq_fkeys_off[] = {0x10, 0xff, 0x0b, 0x1e, 0x01, 0x00, 0x00};
//const char k810_seq_fkeys_on[] = {0x10, 0xff, 0x06, 0x15, 0x00, 0x00, 0x00};
//const char k810_seq_fkeys_off[] = {0x10, 0xff, 0x06, 0x15, 0x01, 0x00, 0x00};
const char opt_on[] = "on";
const char opt_off[] = "off";
void send(const int fd, const char * buf, const int len)
{
int res;
/* Send sequence to the Device */
res = write(fd, buf, len);
if (res < 0)
{
printf("Error: %d\n", errno);
perror("write");
}
else if (res == len)
{
printf("Configuration sent.\n");
}
else
{
errno = ENOMEM;
printf("write: %d were written instead of %d.\n", res, len);
}
}
int main(int argc, char **argv)
{
int fd;
int res;
struct hidraw_devinfo info;
const char * seq;
char *dev = NULL;
int flag_fkeys = 1;
int c;
if (argc < 5)
{
printf("Logitech K810 Keyboard Configurator (by trial-n-error)\n\n");
printf("Usage: %s -d /dev/hidraw{0,1,...} -f {on|off}:\n\n", argv[0]);
printf("-d /dev/hidrawX\n"
" Path to hidraw device. Determine by e.g.:\n"
" ls /sys/class/hidraw/hidraw*/device/uevent\n"
" and/or\n"
" cat /sys/class/hidraw/hidraw*/device/uevent\n");
printf("-f <on|off>\n"
" To enable direct access to F-keys.\n");
printf("\n");
}
while ((c = getopt (argc, argv, "d:f:")) != -1)
{
switch (c)
{
case 'd':
dev = optarg;
break;
case 'f':
if (strcmp(opt_on, optarg) == 0)
{
flag_fkeys = 1;
}
else if (strcmp(opt_off, optarg) == 0)
{
flag_fkeys = 0;
}
else
{
fprintf (stderr, "Option -%c requires argument '%s' or '%s'.\n", optopt, opt_on, opt_off);
return 1;
}
break;
case '?':
if (optopt == 'f')
{
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
}
else if (isprint (optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
}
else
{
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
}
return 1;
default:
abort ();
}
}
char devPath [30];
/* TODO: get real device count? */
for (int i = 0; i < 10; i++) {
printf("\n Trying device %d\n\n", i);
snprintf(devPath, 30, "/dev/hidraw%d", i);
dev = devPath;
/* Open the Device with non-blocking reads. */
fd = open(dev, O_RDWR|O_NONBLOCK);
if (fd < 0)
{
perror("Unable to open device");
continue;
}
/* Get Raw Info */
res = ioctl(fd, HIDIOCGRAWINFO, &info);
if (res < 0)
{
perror("error while getting info from device");
continue;
}
else
{
if (info.bustype != BUS_BLUETOOTH ||
info.vendor != HID_VENDOR_ID_LOGITECH ||
(info.product != HID_DEVICE_ID_K480 &&
info.product != HID_DEVICE_ID_K480_ALT &&
info.product != HID_DEVICE_ID_K480_ALT2))
{
errno = EPERM;
perror("The given device is not a supported "
"Logitech keyboard");
printf("Product : %x\n", info.product);
continue;
}
}
if (flag_fkeys)
{
printf("Sending ON: \n");
if (info.product == HID_DEVICE_ID_K480_ALT2)
send(fd, k380_seq_fkeys_on, sizeof(k380_seq_fkeys_on));
else
send(fd, k480_seq_fkeys_on, sizeof(k480_seq_fkeys_on));
}
else
{
printf("Sending OFF: \n");
if (info.product == HID_DEVICE_ID_K480_ALT2)
send(fd, k380_seq_fkeys_off, sizeof(k380_seq_fkeys_off));
else
send(fd, k480_seq_fkeys_off, sizeof(k480_seq_fkeys_off));
}
close(fd);
return 0;
}
return 1;
}