-
Notifications
You must be signed in to change notification settings - Fork 1
/
k380.c
151 lines (138 loc) · 4.67 KB
/
k380.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
/*
* based on
* k480_conf.c by Emir Bucalovic <[email protected]> www.betoneful.com
* https://betoneful.com/tech/logitech-k480-on-ubuntu-and-fn-buttons-default-behaviour/
* k810_conf.c by Mario Scholz <[email protected]>
* http://www.trial-n-error.de/posts/2012/12/31/logitech-k810-keyboard-configurator/
* 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 <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "hidapi.h"
#define HID_VENDOR_ID_LOGITECH 0x46d
#define HID_DEVICE_ID_K380 0xb342
const unsigned char k380_seq_fkeys_on[] = {0x10, 0xff, 0x0b, 0x1e, 0x00, 0x00, 0x00};
const unsigned char k380_seq_fkeys_off[] = {0x10, 0xff, 0x0b, 0x1e, 0x01, 0x00, 0x00};
const char opt_on[] = "on";
const char opt_off[] = "off";
int main(int argc, char **argv)
{
int flag_fkeys = 1;
int c,res;
hid_device *handle;
struct hid_device_info *devs, *cur_dev;
if (argc < 2)
{
printf("Logitech K380 Keyboard Configurator (by faust93)\n\n");
printf("Usage: %s -f {on|off}:\n\n", argv[0]);
printf("-f <on|off>\n"
" To enable direct access to F-keys.\n");
printf("-l\n"
" To list enabled HID devices.\n");
printf("\n");
exit(0);
}
while ((c = getopt (argc, argv, "lf:")) != -1)
{
switch (c)
{
case 'l':
printf("list\n");
devs = hid_enumerate(0x46d, 0x0);
cur_dev = devs;
while (cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf(" Release: %hx\n", cur_dev->release_number);
printf(" Interface: %d\n", cur_dev->interface_number);
printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
printf("\n");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
exit(0);
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 ();
}
}
if (hid_init()) {
perror("Unable to init HID subsystem");
return -1;
}
handle = hid_open(HID_VENDOR_ID_LOGITECH, HID_DEVICE_ID_K380, NULL);
if (!handle) {
printf("unable to open device\n");
return 1;
}
if (flag_fkeys)
{
res = hid_write(handle, k380_seq_fkeys_on, 7);
if (res != 7)
fprintf(stderr, "Unable to switch F-n keys\n");
else
printf("F-n keys: ON\n");
}
else
{
res = hid_write(handle, k380_seq_fkeys_off, 7);
if (res != 7)
fprintf(stderr, "Unable to switch F-n keys\n");
else
printf("F-n keys: OFF\n");
}
hid_close(handle);
hid_exit();
return 0;
}