-
Notifications
You must be signed in to change notification settings - Fork 2
/
k400_plus_conf.c
143 lines (129 loc) · 4.33 KB
/
k400_plus_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
/*
* Logitech K400 Plus https://github.com/lenisko/k400_plus by @lenisko
*
* based on Mario Scholz <mario@expires-2013.mail.trial-n-error.net> k810_conf.c
* see also http://trial-n-error.de/posts/2012/12/31/logitech-k810-keyboard-configurator
*
* which was based on pairing_tool.c from Benjamin Tissoires <benjamin.tissoires@gmail.com>
* 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
#define HID_DEVICE_ID_K400_PLUS (__s16)0x404d
const char k400_plus_seq_fkeys_on[] = { 0x10, 0x01, 0x09, 0x19, 0x00, 0x00, 0x00 };
const char k400_plus_seq_fkeys_off[] = { 0x10, 0x01, 0x09, 0x18, 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 {
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 K400 Plus Keyboard Configurator\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"
"-f <on|off>\n"
" To enable direct access to F-keys.\n\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();
}
}
/* Open the Device with non-blocking reads. */
fd = open(dev, O_RDWR | O_NONBLOCK);
if (fd < 0) {
perror("Unable to open device");
return 1;
}
/* Get Raw Info */
res = ioctl(fd, HIDIOCGRAWINFO, &info);
if (res < 0) {
perror("error while getting info from device");
}
else {
if (info.vendor != HID_VENDOR_ID_LOGITECH || info.product != HID_DEVICE_ID_K400_PLUS) {
errno = EPERM;
perror("The given device is not a supported Logitech keyboard");
return 1;
}
}
if (flag_fkeys) {
send(fd, k400_plus_seq_fkeys_on, sizeof(k400_plus_seq_fkeys_on));
}
else {
send(fd, k400_plus_seq_fkeys_off, sizeof(k400_plus_seq_fkeys_off));
}
close(fd);
return 0;
}