Debian Codegraph @

src/udev/udev-builtin-keyboard.c

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