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