chiark / gitweb /
build-sys: add one more Makefile symlink
[elogind.git] / src / udev / udev-builtin-keyboard.c
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 <string.h>
22 #include <stdlib.h>
23 #include <sys/ioctl.h>
24 #include <linux/input.h>
25
26 #include "udev.h"
27
28 static const struct key *keyboard_lookup_key(const char *str, unsigned len);
29 #include "keyboard-keys-from-name.h"
30
31 static int install_force_release(struct udev_device *dev, const unsigned *release, unsigned release_count) {
32         struct udev_device *atkbd;
33         const char *cur;
34         char codes[4096];
35         char *s;
36         size_t l;
37         unsigned i;
38         int ret;
39
40         atkbd = udev_device_get_parent_with_subsystem_devtype(dev, "serio", NULL);
41         if (!atkbd)
42                 return -ENODEV;
43
44         cur = udev_device_get_sysattr_value(atkbd, "force_release");
45         if (!cur)
46                 return -ENODEV;
47
48         s = codes;
49         l = sizeof(codes);
50
51         /* copy current content */
52         l = strpcpy(&s, l, cur);
53
54         /* append new codes */
55         for (i = 0; i < release_count; i++)
56                 l = strpcpyf(&s, l, ",%u", release[i]);
57
58         log_debug("keyboard: updating force-release list with '%s'", codes);
59         ret = udev_device_set_sysattr_value(atkbd, "force_release", codes);
60         if (ret < 0)
61                 log_error_errno(ret, "Error writing force-release attribute: %m");
62         return ret;
63 }
64
65 static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], bool test) {
66         struct udev_list_entry *entry;
67         struct {
68                 unsigned scan;
69                 unsigned key;
70         } map[1024];
71         unsigned map_count = 0;
72         unsigned release[1024];
73         unsigned release_count = 0;
74
75         udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) {
76                 const char *key;
77                 unsigned scancode, keycode_num;
78                 char *endptr;
79                 const char *keycode;
80                 const struct key *k;
81
82                 key = udev_list_entry_get_name(entry);
83                 if (!startswith(key, "KEYBOARD_KEY_"))
84                         continue;
85
86                 /* KEYBOARD_KEY_<hex scan code>=<key identifier string> */
87                 scancode = strtoul(key + 13, &endptr, 16);
88                 if (endptr[0] != '\0') {
89                         log_error("Error, unable to parse scan code from '%s'", key);
90                         continue;
91                 }
92
93                 keycode = udev_list_entry_get_value(entry);
94
95                 /* a leading '!' needs a force-release entry */
96                 if (keycode[0] == '!') {
97                         keycode++;
98
99                         release[release_count] = scancode;
100                         if (release_count <  ELEMENTSOF(release)-1)
101                                 release_count++;
102
103                         if (keycode[0] == '\0')
104                                 continue;
105                 }
106
107                 /* translate identifier to key code */
108                 k = keyboard_lookup_key(keycode, strlen(keycode));
109                 if (k) {
110                         keycode_num = k->id;
111                 } else {
112                         /* check if it's a numeric code already */
113                         keycode_num = strtoul(keycode, &endptr, 0);
114                         if (endptr[0] !='\0') {
115                                 log_error("Error, unknown key identifier '%s'", keycode);
116                                 continue;
117                         }
118                 }
119
120                 map[map_count].scan = scancode;
121                 map[map_count].key = keycode_num;
122                 if (map_count < ELEMENTSOF(map)-1)
123                         map_count++;
124         }
125
126         if (map_count > 0 || release_count > 0) {
127                 const char *node;
128                 int fd;
129                 unsigned i;
130
131                 node = udev_device_get_devnode(dev);
132                 if (!node) {
133                         log_error("Error, no device node for '%s'", udev_device_get_syspath(dev));
134                         return EXIT_FAILURE;
135                 }
136
137                 fd = open(udev_device_get_devnode(dev), O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
138                 if (fd < 0) {
139                         log_error_errno(errno, "Error, opening device '%s': %m", node);
140                         return EXIT_FAILURE;
141                 }
142
143                 /* install list of map codes */
144                 for (i = 0; i < map_count; i++) {
145                         log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
146                                   map[i].scan, map[i].scan, map[i].key, map[i].key);
147                         if (ioctl(fd, EVIOCSKEYCODE, &map[i]) < 0)
148                                 log_error_errno(errno, "Error calling EVIOCSKEYCODE on device node '%s' (scan code 0x%x, key code %d): %m", node, map[i].scan, map[i].key);
149                 }
150
151                 /* install list of force-release codes */
152                 if (release_count > 0)
153                         install_force_release(dev, release, release_count);
154
155                 close(fd);
156         }
157
158         return EXIT_SUCCESS;
159 }
160
161 const struct udev_builtin udev_builtin_keyboard = {
162         .name = "keyboard",
163         .cmd = builtin_keyboard,
164         .help = "Keyboard scan code to key mapping",
165 };