]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-keyboard.c
Merge pull request #5164 from Werkov/ordering-for-_netdev-devices
[thirdparty/systemd.git] / src / udev / udev-builtin-keyboard.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Kay Sievers <kay@vrfy.org>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <linux/input.h>
25
26 #include "fd-util.h"
27 #include "parse-util.h"
28 #include "stdio-util.h"
29 #include "string-util.h"
30 #include "udev.h"
31
32 static const struct key_name *keyboard_lookup_key(const char *str, GPERF_LEN_TYPE len);
33 #include "keyboard-keys-from-name.h"
34
35 static int install_force_release(struct udev_device *dev, const unsigned *release, unsigned release_count) {
36 struct udev_device *atkbd;
37 const char *cur;
38 char codes[4096];
39 char *s;
40 size_t l;
41 unsigned i;
42 int ret;
43
44 assert(dev);
45 assert(release);
46
47 atkbd = udev_device_get_parent_with_subsystem_devtype(dev, "serio", NULL);
48 if (!atkbd)
49 return -ENODEV;
50
51 cur = udev_device_get_sysattr_value(atkbd, "force_release");
52 if (!cur)
53 return -ENODEV;
54
55 s = codes;
56 l = sizeof(codes);
57
58 /* copy current content */
59 l = strpcpy(&s, l, cur);
60
61 /* append new codes */
62 for (i = 0; i < release_count; i++)
63 l = strpcpyf(&s, l, ",%u", release[i]);
64
65 log_debug("keyboard: updating force-release list with '%s'", codes);
66 ret = udev_device_set_sysattr_value(atkbd, "force_release", codes);
67 if (ret < 0)
68 log_error_errno(ret, "Error writing force-release attribute: %m");
69 return ret;
70 }
71
72 static void map_keycode(int fd, const char *devnode, int scancode, const char *keycode)
73 {
74 struct {
75 unsigned scan;
76 unsigned key;
77 } map;
78 char *endptr;
79 const struct key_name *k;
80 unsigned keycode_num;
81
82 /* translate identifier to key code */
83 k = keyboard_lookup_key(keycode, strlen(keycode));
84 if (k) {
85 keycode_num = k->id;
86 } else {
87 /* check if it's a numeric code already */
88 keycode_num = strtoul(keycode, &endptr, 0);
89 if (endptr[0] !='\0') {
90 log_error("Unknown key identifier '%s'", keycode);
91 return;
92 }
93 }
94
95 map.scan = scancode;
96 map.key = keycode_num;
97
98 log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
99 map.scan, map.scan, map.key, map.key);
100
101 if (ioctl(fd, EVIOCSKEYCODE, &map) < 0)
102 log_error_errno(errno, "Error calling EVIOCSKEYCODE on device node '%s' (scan code 0x%x, key code %d): %m", devnode, map.scan, map.key);
103 }
104
105 static inline char* parse_token(const char *current, int32_t *val_out) {
106 char *next;
107 int32_t val;
108
109 if (!current)
110 return NULL;
111
112 val = strtol(current, &next, 0);
113 if (*next && *next != ':')
114 return NULL;
115
116 if (next != current)
117 *val_out = val;
118
119 if (*next)
120 next++;
121
122 return next;
123 }
124
125 static void override_abs(int fd, const char *devnode,
126 unsigned evcode, const char *value) {
127 struct input_absinfo absinfo;
128 int rc;
129 char *next;
130
131 rc = ioctl(fd, EVIOCGABS(evcode), &absinfo);
132 if (rc < 0) {
133 log_error_errno(errno, "Unable to EVIOCGABS device \"%s\"", devnode);
134 return;
135 }
136
137 next = parse_token(value, &absinfo.minimum);
138 next = parse_token(next, &absinfo.maximum);
139 next = parse_token(next, &absinfo.resolution);
140 next = parse_token(next, &absinfo.fuzz);
141 next = parse_token(next, &absinfo.flat);
142 if (!next) {
143 log_error("Unable to parse EV_ABS override '%s' for '%s'", value, devnode);
144 return;
145 }
146
147 log_debug("keyboard: %x overridden with %"PRIi32"/%"PRIi32"/%"PRIi32"/%"PRIi32"/%"PRIi32" for \"%s\"",
148 evcode,
149 absinfo.minimum, absinfo.maximum, absinfo.resolution, absinfo.fuzz, absinfo.flat,
150 devnode);
151 rc = ioctl(fd, EVIOCSABS(evcode), &absinfo);
152 if (rc < 0)
153 log_error_errno(errno, "Unable to EVIOCSABS device \"%s\"", devnode);
154 }
155
156 static void set_trackpoint_sensitivity(struct udev_device *dev, const char *value)
157 {
158 struct udev_device *pdev;
159 char val_s[DECIMAL_STR_MAX(int)];
160 int r, val_i;
161
162 assert(dev);
163 assert(value);
164
165 /* The sensitivity sysfs attr belongs to the serio parent device */
166 pdev = udev_device_get_parent_with_subsystem_devtype(dev, "serio", NULL);
167 if (!pdev) {
168 log_warning("Failed to get serio parent for '%s'", udev_device_get_devnode(dev));
169 return;
170 }
171
172 r = safe_atoi(value, &val_i);
173 if (r < 0) {
174 log_error("Unable to parse POINTINGSTICK_SENSITIVITY '%s' for '%s'", value, udev_device_get_devnode(dev));
175 return;
176 }
177
178 xsprintf(val_s, "%d", val_i);
179
180 r = udev_device_set_sysattr_value(pdev, "sensitivity", val_s);
181 if (r < 0)
182 log_error_errno(r, "Failed to write 'sensitivity' attribute for '%s': %m", udev_device_get_devnode(pdev));
183 }
184
185 static int open_device(const char *devnode) {
186 int fd;
187
188 fd = open(devnode, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
189 if (fd < 0)
190 return log_error_errno(errno, "Error opening device \"%s\": %m", devnode);
191
192 return fd;
193 }
194
195 static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], bool test) {
196 struct udev_list_entry *entry;
197 unsigned release[1024];
198 unsigned release_count = 0;
199 _cleanup_close_ int fd = -1;
200 const char *node;
201
202 node = udev_device_get_devnode(dev);
203 if (!node) {
204 log_error("No device node for \"%s\"", udev_device_get_syspath(dev));
205 return EXIT_FAILURE;
206 }
207
208 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) {
209 const char *key;
210 char *endptr;
211
212 key = udev_list_entry_get_name(entry);
213 if (startswith(key, "KEYBOARD_KEY_")) {
214 const char *keycode;
215 unsigned scancode;
216
217 /* KEYBOARD_KEY_<hex scan code>=<key identifier string> */
218 scancode = strtoul(key + 13, &endptr, 16);
219 if (endptr[0] != '\0') {
220 log_warning("Unable to parse scan code from \"%s\"", key);
221 continue;
222 }
223
224 keycode = udev_list_entry_get_value(entry);
225
226 /* a leading '!' needs a force-release entry */
227 if (keycode[0] == '!') {
228 keycode++;
229
230 release[release_count] = scancode;
231 if (release_count < ELEMENTSOF(release)-1)
232 release_count++;
233
234 if (keycode[0] == '\0')
235 continue;
236 }
237
238 if (fd == -1) {
239 fd = open_device(node);
240 if (fd < 0)
241 return EXIT_FAILURE;
242 }
243
244 map_keycode(fd, node, scancode, keycode);
245 } else if (startswith(key, "EVDEV_ABS_")) {
246 unsigned evcode;
247
248 /* EVDEV_ABS_<EV_ABS code>=<min>:<max>:<res>:<fuzz>:<flat> */
249 evcode = strtoul(key + 10, &endptr, 16);
250 if (endptr[0] != '\0') {
251 log_warning("Unable to parse EV_ABS code from \"%s\"", key);
252 continue;
253 }
254
255 if (fd == -1) {
256 fd = open_device(node);
257 if (fd < 0)
258 return EXIT_FAILURE;
259 }
260
261 override_abs(fd, node, evcode, udev_list_entry_get_value(entry));
262 } else if (streq(key, "POINTINGSTICK_SENSITIVITY"))
263 set_trackpoint_sensitivity(dev, udev_list_entry_get_value(entry));
264 }
265
266 /* install list of force-release codes */
267 if (release_count > 0)
268 install_force_release(dev, release, release_count);
269
270 return EXIT_SUCCESS;
271 }
272
273 const struct udev_builtin udev_builtin_keyboard = {
274 .name = "keyboard",
275 .cmd = builtin_keyboard,
276 .help = "Keyboard scan code to key mapping",
277 };