2 This file is part of systemd.
4 Copyright 2013 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
23 #include <sys/ioctl.h>
24 #include <linux/input.h>
26 #include "string-util.h"
29 static const struct key
*keyboard_lookup_key(const char *str
, unsigned len
);
30 #include "keyboard-keys-from-name.h"
32 static int install_force_release(struct udev_device
*dev
, const unsigned *release
, unsigned release_count
) {
33 struct udev_device
*atkbd
;
44 atkbd
= udev_device_get_parent_with_subsystem_devtype(dev
, "serio", NULL
);
48 cur
= udev_device_get_sysattr_value(atkbd
, "force_release");
55 /* copy current content */
56 l
= strpcpy(&s
, l
, cur
);
58 /* append new codes */
59 for (i
= 0; i
< release_count
; i
++)
60 l
= strpcpyf(&s
, l
, ",%u", release
[i
]);
62 log_debug("keyboard: updating force-release list with '%s'", codes
);
63 ret
= udev_device_set_sysattr_value(atkbd
, "force_release", codes
);
65 log_error_errno(ret
, "Error writing force-release attribute: %m");
69 static void map_keycode(int fd
, const char *devnode
, int scancode
, const char *keycode
)
79 /* translate identifier to key code */
80 k
= keyboard_lookup_key(keycode
, strlen(keycode
));
84 /* check if it's a numeric code already */
85 keycode_num
= strtoul(keycode
, &endptr
, 0);
86 if (endptr
[0] !='\0') {
87 log_error("Unknown key identifier '%s'", keycode
);
93 map
.key
= keycode_num
;
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
);
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
);
102 static inline char* parse_token(const char *current
, int32_t *val_out
) {
109 val
= strtol(current
, &next
, 0);
110 if (*next
&& *next
!= ':')
122 static void override_abs(int fd
, const char *devnode
,
123 unsigned evcode
, const char *value
) {
124 struct input_absinfo absinfo
;
128 rc
= ioctl(fd
, EVIOCGABS(evcode
), &absinfo
);
130 log_error_errno(errno
, "Unable to EVIOCGABS device \"%s\"", devnode
);
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
);
140 log_error("Unable to parse EV_ABS override '%s' for '%s'", value
, devnode
);
144 log_debug("keyboard: %x overridden with %"PRIi32
"/%"PRIi32
"/%"PRIi32
"/%"PRIi32
"/%"PRIi32
" for \"%s\"",
146 absinfo
.minimum
, absinfo
.maximum
, absinfo
.resolution
, absinfo
.fuzz
, absinfo
.flat
,
148 rc
= ioctl(fd
, EVIOCSABS(evcode
), &absinfo
);
150 log_error_errno(errno
, "Unable to EVIOCSABS device \"%s\"", devnode
);
153 static void set_trackpoint_sensitivity(struct udev_device
*dev
, const char *value
)
155 struct udev_device
*pdev
;
156 char val_s
[DECIMAL_STR_MAX(int)];
162 /* The sensitivity sysfs attr belongs to the serio parent device */
163 pdev
= udev_device_get_parent_with_subsystem_devtype(dev
, "serio", NULL
);
165 log_warning("Failed to get serio parent for '%s'", udev_device_get_devnode(dev
));
169 r
= safe_atoi(value
, &val_i
);
171 log_error("Unable to parse POINTINGSTICK_SENSITIVITY '%s' for '%s'", value
, udev_device_get_devnode(dev
));
175 xsprintf(val_s
, "%d", val_i
);
177 r
= udev_device_set_sysattr_value(pdev
, "sensitivity", val_s
);
179 log_error_errno(r
, "Failed to write 'sensitivity' attribute for '%s': %m", udev_device_get_devnode(pdev
));
182 static int open_device(const char *devnode
) {
185 fd
= open(devnode
, O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
187 return log_error_errno(errno
, "Error opening device \"%s\": %m", devnode
);
192 static int builtin_keyboard(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
193 struct udev_list_entry
*entry
;
194 unsigned release
[1024];
195 unsigned release_count
= 0;
196 _cleanup_close_
int fd
= -1;
199 node
= udev_device_get_devnode(dev
);
201 log_error("No device node for \"%s\"", udev_device_get_syspath(dev
));
205 udev_list_entry_foreach(entry
, udev_device_get_properties_list_entry(dev
)) {
209 key
= udev_list_entry_get_name(entry
);
210 if (startswith(key
, "KEYBOARD_KEY_")) {
214 /* KEYBOARD_KEY_<hex scan code>=<key identifier string> */
215 scancode
= strtoul(key
+ 13, &endptr
, 16);
216 if (endptr
[0] != '\0') {
217 log_warning("Unable to parse scan code from \"%s\"", key
);
221 keycode
= udev_list_entry_get_value(entry
);
223 /* a leading '!' needs a force-release entry */
224 if (keycode
[0] == '!') {
227 release
[release_count
] = scancode
;
228 if (release_count
< ELEMENTSOF(release
)-1)
231 if (keycode
[0] == '\0')
236 fd
= open_device(node
);
241 map_keycode(fd
, node
, scancode
, keycode
);
242 } else if (startswith(key
, "EVDEV_ABS_")) {
245 /* EVDEV_ABS_<EV_ABS code>=<min>:<max>:<res>:<fuzz>:<flat> */
246 evcode
= strtoul(key
+ 10, &endptr
, 16);
247 if (endptr
[0] != '\0') {
248 log_warning("Unable to parse EV_ABS code from \"%s\"", key
);
253 fd
= open_device(node
);
258 override_abs(fd
, node
, evcode
, udev_list_entry_get_value(entry
));
259 } else if (streq(key
, "POINTINGSTICK_SENSITIVITY"))
260 set_trackpoint_sensitivity(dev
, udev_list_entry_get_value(entry
));
263 /* install list of force-release codes */
264 if (release_count
> 0)
265 install_force_release(dev
, release
, release_count
);
270 const struct udev_builtin udev_builtin_keyboard
= {
272 .cmd
= builtin_keyboard
,
273 .help
= "Keyboard scan code to key mapping",