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