]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-keyboard.c
Remove 'inline' attributes from static functions in .c files (#11426)
[thirdparty/systemd.git] / src / udev / udev-builtin-keyboard.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
9d7d42bc 2
07a26e42
YW
3#include <errno.h>
4#include <fcntl.h>
9d7d42bc 5#include <stdio.h>
9d7d42bc 6#include <stdlib.h>
07630cea 7#include <string.h>
9d7d42bc 8#include <sys/ioctl.h>
9d7d42bc
KS
9#include <linux/input.h>
10
786cc852 11#include "device-util.h"
3ffd4af2 12#include "fd-util.h"
6bedfcbb 13#include "parse-util.h"
15a5e950 14#include "stdio-util.h"
07630cea 15#include "string-util.h"
07a26e42
YW
16#include "strxcpyx.h"
17#include "udev-builtin.h"
9d7d42bc 18
65fbd939 19static const struct key_name *keyboard_lookup_key(const char *str, GPERF_LEN_TYPE len);
9d7d42bc 20#include "keyboard-keys-from-name.h"
9d7d42bc 21
786cc852
YW
22static int install_force_release(sd_device *dev, const unsigned *release, unsigned release_count) {
23 sd_device *atkbd;
9d7d42bc
KS
24 const char *cur;
25 char codes[4096];
26 char *s;
27 size_t l;
1fa2f38f 28 unsigned i;
786cc852 29 int r;
9d7d42bc 30
3b64e4d4
TG
31 assert(dev);
32 assert(release);
33
786cc852
YW
34 r = sd_device_get_parent_with_subsystem_devtype(dev, "serio", NULL, &atkbd);
35 if (r < 0)
2dccfe4c 36 return log_device_error_errno(dev, r, "Failed to get serio parent: %m");
9d7d42bc 37
786cc852
YW
38 r = sd_device_get_sysattr_value(atkbd, "force_release", &cur);
39 if (r < 0)
2dccfe4c 40 return log_device_error_errno(atkbd, r, "Failed to get force-release attribute: %m");
9d7d42bc
KS
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++)
1fa2f38f 50 l = strpcpyf(&s, l, ",%u", release[i]);
9d7d42bc 51
2dccfe4c 52 log_device_debug(atkbd, "keyboard: updating force-release list with '%s'", codes);
786cc852
YW
53 r = sd_device_set_sysattr_value(atkbd, "force_release", codes);
54 if (r < 0)
2dccfe4c 55 return log_device_error_errno(atkbd, r, "Failed to set force-release attribute: %m");
786cc852
YW
56
57 return 0;
9d7d42bc
KS
58}
59
2dccfe4c 60static int map_keycode(sd_device *dev, int fd, int scancode, const char *keycode) {
9d7d42bc 61 struct {
1fa2f38f
ZJS
62 unsigned scan;
63 unsigned key;
cfba2656 64 } map;
c9a8e340 65 char *endptr;
65fbd939 66 const struct key_name *k;
c9a8e340
PH
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);
2dccfe4c 76 if (endptr[0] !='\0')
a1193286 77 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Failed to parse key identifier '%s'", keycode);
c9a8e340
PH
78 }
79
80 map.scan = scancode;
81 map.key = keycode_num;
82
2dccfe4c
YW
83 log_device_debug(dev, "keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
84 map.scan, map.scan, map.key, map.key);
c9a8e340
PH
85
86 if (ioctl(fd, EVIOCSKEYCODE, &map) < 0)
2dccfe4c
YW
87 return log_device_error_errno(dev, errno, "Failed to call EVIOCSKEYCODE with scan code 0x%x, and key code %d: %m", map.scan, map.key);
88
89 return 0;
c9a8e340
PH
90}
91
a1e92eee 92static char* parse_token(const char *current, int32_t *val_out) {
51c0c286
PH
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
2dccfe4c 112static int override_abs(sd_device *dev, int fd, unsigned evcode, const char *value) {
51c0c286 113 struct input_absinfo absinfo;
51c0c286 114 char *next;
2dccfe4c 115 int r;
51c0c286 116
2dccfe4c
YW
117 r = ioctl(fd, EVIOCGABS(evcode), &absinfo);
118 if (r < 0)
119 return log_device_error_errno(dev, errno, "Failed to call EVIOCGABS");
51c0c286
PH
120
121 next = parse_token(value, &absinfo.minimum);
122 next = parse_token(next, &absinfo.maximum);
123 next = parse_token(next, &absinfo.resolution);
124 next = parse_token(next, &absinfo.fuzz);
125 next = parse_token(next, &absinfo.flat);
2dccfe4c
YW
126 if (!next)
127 return log_device_error(dev, "Failed to parse EV_ABS override '%s'", value);
128
129 log_device_debug(dev, "keyboard: %x overridden with %"PRIi32"/%"PRIi32"/%"PRIi32"/%"PRIi32"/%"PRIi32,
130 evcode, absinfo.minimum, absinfo.maximum, absinfo.resolution, absinfo.fuzz, absinfo.flat);
131 r = ioctl(fd, EVIOCSABS(evcode), &absinfo);
132 if (r < 0)
133 return log_device_error_errno(dev, errno, "Failed to call EVIOCSABS");
51c0c286 134
2dccfe4c 135 return 0;
51c0c286
PH
136}
137
786cc852
YW
138static int set_trackpoint_sensitivity(sd_device *dev, const char *value) {
139 sd_device *pdev;
5defbb5f
HG
140 char val_s[DECIMAL_STR_MAX(int)];
141 int r, val_i;
142
3b64e4d4
TG
143 assert(dev);
144 assert(value);
145
5defbb5f 146 /* The sensitivity sysfs attr belongs to the serio parent device */
786cc852
YW
147 r = sd_device_get_parent_with_subsystem_devtype(dev, "serio", NULL, &pdev);
148 if (r < 0)
2dccfe4c 149 return log_device_error_errno(dev, r, "Failed to get serio parent: %m");
5defbb5f
HG
150
151 r = safe_atoi(value, &val_i);
786cc852 152 if (r < 0)
2dccfe4c 153 return log_device_error_errno(dev, r, "Failed to parse POINTINGSTICK_SENSITIVITY '%s': %m", value);
786cc852 154 else if (val_i < 0 || val_i > 255)
a1193286 155 return log_device_error_errno(dev, SYNTHETIC_ERRNO(ERANGE), "POINTINGSTICK_SENSITIVITY %d outside range [0..255]", val_i);
5defbb5f
HG
156
157 xsprintf(val_s, "%d", val_i);
158
786cc852 159 r = sd_device_set_sysattr_value(pdev, "sensitivity", val_s);
5defbb5f 160 if (r < 0)
2dccfe4c 161 return log_device_error_errno(dev, r, "Failed to write 'sensitivity' attribute: %m");
786cc852
YW
162
163 return 0;
5defbb5f
HG
164}
165
3fc2e9a2 166static int builtin_keyboard(sd_device *dev, int argc, char *argv[], bool test) {
1fa2f38f
ZJS
167 unsigned release[1024];
168 unsigned release_count = 0;
cfba2656 169 _cleanup_close_ int fd = -1;
786cc852
YW
170 const char *node, *key, *value;
171 int has_abs = -1, r;
786cc852
YW
172
173 r = sd_device_get_devname(dev, &node);
2dccfe4c
YW
174 if (r < 0)
175 return log_device_error_errno(dev, r, "Failed to get device name: %m");
9d7d42bc 176
786cc852 177 FOREACH_DEVICE_PROPERTY(dev, key, value) {
9d7d42bc 178 char *endptr;
9d7d42bc 179
8a0fd83c 180 if (startswith(key, "KEYBOARD_KEY_")) {
786cc852 181 const char *keycode = value;
8a0fd83c
PH
182 unsigned scancode;
183
184 /* KEYBOARD_KEY_<hex scan code>=<key identifier string> */
185 scancode = strtoul(key + 13, &endptr, 16);
186 if (endptr[0] != '\0') {
2dccfe4c 187 log_device_warning(dev, "Failed to parse scan code from \"%s\", ignoring", key);
8a0fd83c
PH
188 continue;
189 }
9d7d42bc 190
8a0fd83c
PH
191 /* a leading '!' needs a force-release entry */
192 if (keycode[0] == '!') {
193 keycode++;
9d7d42bc 194
8a0fd83c 195 release[release_count] = scancode;
49c603bd 196 if (release_count < ELEMENTSOF(release)-1)
8a0fd83c 197 release_count++;
9d7d42bc 198
8a0fd83c
PH
199 if (keycode[0] == '\0')
200 continue;
201 }
9d7d42bc 202
2dccfe4c
YW
203 if (fd < 0) {
204 fd = open(node, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
51c0c286 205 if (fd < 0)
2dccfe4c 206 return log_device_error_errno(dev, errno, "Failed to open device '%s': %m", node);
cfba2656 207 }
9d7d42bc 208
2dccfe4c 209 (void) map_keycode(dev, fd, scancode, keycode);
51c0c286
PH
210 } else if (startswith(key, "EVDEV_ABS_")) {
211 unsigned evcode;
212
213 /* EVDEV_ABS_<EV_ABS code>=<min>:<max>:<res>:<fuzz>:<flat> */
214 evcode = strtoul(key + 10, &endptr, 16);
215 if (endptr[0] != '\0') {
2dccfe4c 216 log_device_warning(dev, "Failed to parse EV_ABS code from \"%s\", ignoring", key);
51c0c286
PH
217 continue;
218 }
219
2dccfe4c
YW
220 if (fd < 0) {
221 fd = open(node, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
51c0c286 222 if (fd < 0)
2dccfe4c 223 return log_device_error_errno(dev, errno, "Failed to open device '%s': %m", node);
51c0c286
PH
224 }
225
855cf359
PH
226 if (has_abs == -1) {
227 unsigned long bits;
228 int rc;
229
230 rc = ioctl(fd, EVIOCGBIT(0, sizeof(bits)), &bits);
d354690e 231 if (rc < 0)
2dccfe4c 232 return log_device_error_errno(dev, errno, "Failed to set EVIOCGBIT");
855cf359
PH
233
234 has_abs = !!(bits & (1 << EV_ABS));
235 if (!has_abs)
2dccfe4c 236 log_device_warning(dev, "EVDEV_ABS override set but no EV_ABS present on device");
855cf359
PH
237 }
238
239 if (!has_abs)
240 continue;
241
2dccfe4c 242 (void) override_abs(dev, fd, evcode, value);
1f6b4113 243 } else if (streq(key, "POINTINGSTICK_SENSITIVITY"))
2dccfe4c 244 (void) set_trackpoint_sensitivity(dev, value);
9d7d42bc
KS
245 }
246
cfba2656
PH
247 /* install list of force-release codes */
248 if (release_count > 0)
2dccfe4c 249 (void) install_force_release(dev, release, release_count);
cfba2656 250
d354690e 251 return 0;
9d7d42bc
KS
252}
253
254const struct udev_builtin udev_builtin_keyboard = {
255 .name = "keyboard",
256 .cmd = builtin_keyboard,
5ac0162c 257 .help = "Keyboard scan code to key mapping",
9d7d42bc 258};