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