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