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