]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-keyboard.c
udev-builtin: propagate negative errno
[thirdparty/systemd.git] / src / udev / udev-builtin-keyboard.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/ioctl.h>
9 #include <linux/input.h>
10
11 #include "device-util.h"
12 #include "fd-util.h"
13 #include "parse-util.h"
14 #include "stdio-util.h"
15 #include "string-util.h"
16 #include "strxcpyx.h"
17 #include "udev-builtin.h"
18
19 static const struct key_name *keyboard_lookup_key(const char *str, GPERF_LEN_TYPE len);
20 #include "keyboard-keys-from-name.h"
21
22 static int install_force_release(sd_device *dev, const unsigned *release, unsigned release_count) {
23 sd_device *atkbd;
24 const char *cur;
25 char codes[4096];
26 char *s;
27 size_t l;
28 unsigned i;
29 int r;
30
31 assert(dev);
32 assert(release);
33
34 r = sd_device_get_parent_with_subsystem_devtype(dev, "serio", NULL, &atkbd);
35 if (r < 0)
36 return r;
37
38 r = sd_device_get_sysattr_value(atkbd, "force_release", &cur);
39 if (r < 0)
40 return r;
41
42 s = codes;
43 l = sizeof(codes);
44
45 /* copy current content */
46 l = strpcpy(&s, l, cur);
47
48 /* append new codes */
49 for (i = 0; i < release_count; i++)
50 l = strpcpyf(&s, l, ",%u", release[i]);
51
52 log_debug("keyboard: updating force-release list with '%s'", codes);
53 r = sd_device_set_sysattr_value(atkbd, "force_release", codes);
54 if (r < 0)
55 return log_error_errno(r, "Error writing force-release attribute: %m");
56
57 return 0;
58 }
59
60 static void map_keycode(int fd, const char *devnode, int scancode, const char *keycode) {
61 struct {
62 unsigned scan;
63 unsigned key;
64 } map;
65 char *endptr;
66 const struct key_name *k;
67 unsigned keycode_num;
68
69 /* translate identifier to key code */
70 k = keyboard_lookup_key(keycode, strlen(keycode));
71 if (k) {
72 keycode_num = k->id;
73 } else {
74 /* check if it's a numeric code already */
75 keycode_num = strtoul(keycode, &endptr, 0);
76 if (endptr[0] !='\0') {
77 log_error("Unknown key identifier '%s'", keycode);
78 return;
79 }
80 }
81
82 map.scan = scancode;
83 map.key = keycode_num;
84
85 log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
86 map.scan, map.scan, map.key, map.key);
87
88 if (ioctl(fd, EVIOCSKEYCODE, &map) < 0)
89 log_error_errno(errno, "Error calling EVIOCSKEYCODE on device node '%s' (scan code 0x%x, key code %d): %m", devnode, map.scan, map.key);
90 }
91
92 static inline char* parse_token(const char *current, int32_t *val_out) {
93 char *next;
94 int32_t val;
95
96 if (!current)
97 return NULL;
98
99 val = strtol(current, &next, 0);
100 if (*next && *next != ':')
101 return NULL;
102
103 if (next != current)
104 *val_out = val;
105
106 if (*next)
107 next++;
108
109 return next;
110 }
111
112 static void override_abs(int fd, const char *devnode,
113 unsigned evcode, const char *value) {
114 struct input_absinfo absinfo;
115 int rc;
116 char *next;
117
118 rc = ioctl(fd, EVIOCGABS(evcode), &absinfo);
119 if (rc < 0) {
120 log_error_errno(errno, "Unable to EVIOCGABS device \"%s\"", devnode);
121 return;
122 }
123
124 next = parse_token(value, &absinfo.minimum);
125 next = parse_token(next, &absinfo.maximum);
126 next = parse_token(next, &absinfo.resolution);
127 next = parse_token(next, &absinfo.fuzz);
128 next = parse_token(next, &absinfo.flat);
129 if (!next) {
130 log_error("Unable to parse EV_ABS override '%s' for '%s'", value, devnode);
131 return;
132 }
133
134 log_debug("keyboard: %x overridden with %"PRIi32"/%"PRIi32"/%"PRIi32"/%"PRIi32"/%"PRIi32" for \"%s\"",
135 evcode,
136 absinfo.minimum, absinfo.maximum, absinfo.resolution, absinfo.fuzz, absinfo.flat,
137 devnode);
138 rc = ioctl(fd, EVIOCSABS(evcode), &absinfo);
139 if (rc < 0)
140 log_error_errno(errno, "Unable to EVIOCSABS device \"%s\"", devnode);
141 }
142
143 static int set_trackpoint_sensitivity(sd_device *dev, const char *value) {
144 sd_device *pdev;
145 char val_s[DECIMAL_STR_MAX(int)];
146 const char *devnode;
147 int r, val_i;
148
149 assert(dev);
150 assert(value);
151
152 r = sd_device_get_devname(dev, &devnode);
153 if (r < 0)
154 return log_error_errno(r, "Failed to get devname: %m");
155
156 /* The sensitivity sysfs attr belongs to the serio parent device */
157 r = sd_device_get_parent_with_subsystem_devtype(dev, "serio", NULL, &pdev);
158 if (r < 0)
159 return log_warning_errno(r, "Failed to get serio parent for '%s': %m", devnode);
160
161 r = safe_atoi(value, &val_i);
162 if (r < 0)
163 return log_error_errno(r, "Unable to parse POINTINGSTICK_SENSITIVITY '%s' for '%s': %m", value, devnode);
164 else if (val_i < 0 || val_i > 255)
165 return log_error_errno(ERANGE, "POINTINGSTICK_SENSITIVITY %d outside range [0..255] for '%s'", val_i, devnode);
166
167 xsprintf(val_s, "%d", val_i);
168
169 r = sd_device_set_sysattr_value(pdev, "sensitivity", val_s);
170 if (r < 0)
171 return log_error_errno(r, "Failed to write 'sensitivity' attribute for '%s': %m", devnode);
172
173 return 0;
174 }
175
176 static int open_device(const char *devnode) {
177 int fd;
178
179 fd = open(devnode, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
180 if (fd < 0)
181 return log_error_errno(errno, "Error opening device \"%s\": %m", devnode);
182
183 return fd;
184 }
185
186 static int builtin_keyboard(sd_device *dev, int argc, char *argv[], bool test) {
187 unsigned release[1024];
188 unsigned release_count = 0;
189 _cleanup_close_ int fd = -1;
190 const char *node, *key, *value;
191 int has_abs = -1, r;
192
193 r = sd_device_get_devname(dev, &node);
194 if (r < 0) {
195 const char *s = NULL;
196
197 (void) sd_device_get_syspath(dev, &s);
198 return log_error_errno(r, "No device node for \"%s\": %m", strnull(s));
199 }
200
201 FOREACH_DEVICE_PROPERTY(dev, key, value) {
202 char *endptr;
203
204 if (startswith(key, "KEYBOARD_KEY_")) {
205 const char *keycode = value;
206 unsigned scancode;
207
208 /* KEYBOARD_KEY_<hex scan code>=<key identifier string> */
209 scancode = strtoul(key + 13, &endptr, 16);
210 if (endptr[0] != '\0') {
211 log_warning("Unable to parse scan code from \"%s\"", key);
212 continue;
213 }
214
215 /* a leading '!' needs a force-release entry */
216 if (keycode[0] == '!') {
217 keycode++;
218
219 release[release_count] = scancode;
220 if (release_count < ELEMENTSOF(release)-1)
221 release_count++;
222
223 if (keycode[0] == '\0')
224 continue;
225 }
226
227 if (fd == -1) {
228 fd = open_device(node);
229 if (fd < 0)
230 return fd;
231 }
232
233 map_keycode(fd, node, scancode, keycode);
234 } else if (startswith(key, "EVDEV_ABS_")) {
235 unsigned evcode;
236
237 /* EVDEV_ABS_<EV_ABS code>=<min>:<max>:<res>:<fuzz>:<flat> */
238 evcode = strtoul(key + 10, &endptr, 16);
239 if (endptr[0] != '\0') {
240 log_warning("Unable to parse EV_ABS code from \"%s\"", key);
241 continue;
242 }
243
244 if (fd == -1) {
245 fd = open_device(node);
246 if (fd < 0)
247 return fd;
248 }
249
250 if (has_abs == -1) {
251 unsigned long bits;
252 int rc;
253
254 rc = ioctl(fd, EVIOCGBIT(0, sizeof(bits)), &bits);
255 if (rc < 0)
256 return log_error_errno(errno, "Unable to EVIOCGBIT device \"%s\"", node);
257
258 has_abs = !!(bits & (1 << EV_ABS));
259 if (!has_abs)
260 log_warning("EVDEV_ABS override set but no EV_ABS present on device \"%s\"", node);
261 }
262
263 if (!has_abs)
264 continue;
265
266 override_abs(fd, node, evcode, value);
267 } else if (streq(key, "POINTINGSTICK_SENSITIVITY"))
268 set_trackpoint_sensitivity(dev, value);
269 }
270
271 /* install list of force-release codes */
272 if (release_count > 0)
273 install_force_release(dev, release, release_count);
274
275 return 0;
276 }
277
278 const struct udev_builtin udev_builtin_keyboard = {
279 .name = "keyboard",
280 .cmd = builtin_keyboard,
281 .help = "Keyboard scan code to key mapping",
282 };