]>
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> | |
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 | static const struct key *keyboard_lookup_key(const char *str, unsigned int len); | |
32 | #include "keyboard-keys-from-name.h" | |
33 | #include "keyboard-keys-to-name.h" | |
34 | ||
35 | static int install_force_release(struct udev_device *dev, const unsigned int *release, unsigned int release_count) { | |
36 | struct udev_device *atkbd; | |
37 | const char *cur; | |
38 | char codes[4096]; | |
39 | char *s; | |
40 | size_t l; | |
41 | unsigned int i; | |
42 | int ret; | |
43 | ||
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++) | |
60 | l = strpcpyf(&s, l, ",%d", release[i]); | |
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 | ||
69 | static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], bool test) { | |
70 | struct udev_list_entry *entry; | |
71 | struct { | |
72 | unsigned int scan; | |
73 | unsigned int key; | |
74 | } map[1024]; | |
75 | unsigned int map_count = 0; | |
76 | unsigned int release[1024]; | |
77 | unsigned int release_count = 0; | |
78 | ||
79 | udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) { | |
80 | const char *key; | |
7925709c | 81 | unsigned int scancode, keycode_num; |
9d7d42bc KS |
82 | char *endptr; |
83 | const char *keycode; | |
84 | const struct key *k; | |
85 | ||
86 | key = udev_list_entry_get_name(entry); | |
87 | if (!startswith(key, "KEYBOARD_KEY_")) | |
88 | continue; | |
89 | ||
90 | /* KEYBOARD_KEY_<hex scan code>=<key identifier string> */ | |
b151ca99 | 91 | scancode = strtoul(key + 13, &endptr, 16); |
9d7d42bc | 92 | if (endptr[0] != '\0') { |
9f6445e3 | 93 | log_error("Error, unable to parse scan code from '%s'", key); |
9d7d42bc KS |
94 | continue; |
95 | } | |
96 | ||
97 | keycode = udev_list_entry_get_value(entry); | |
98 | ||
99 | /* a leading '!' needs a force-release entry */ | |
100 | if (keycode[0] == '!') { | |
101 | keycode++; | |
102 | ||
103 | release[release_count] = scancode; | |
104 | if (release_count < ELEMENTSOF(release)-1) | |
105 | release_count++; | |
106 | ||
107 | if (keycode[0] == '\0') | |
108 | continue; | |
109 | } | |
110 | ||
111 | /* translate identifier to key code */ | |
112 | k = keyboard_lookup_key(keycode, strlen(keycode)); | |
7925709c MP |
113 | if (k) { |
114 | keycode_num = k->id; | |
115 | } else { | |
116 | /* check if it's a numeric code already */ | |
117 | keycode_num = strtoul(keycode, &endptr, 0); | |
118 | if (endptr[0] !='\0') { | |
119 | log_error("Error, unknown key identifier '%s'", keycode); | |
120 | continue; | |
121 | } | |
9d7d42bc KS |
122 | } |
123 | ||
124 | map[map_count].scan = scancode; | |
7925709c | 125 | map[map_count].key = keycode_num; |
9d7d42bc KS |
126 | if (map_count < ELEMENTSOF(map)-1) |
127 | map_count++; | |
128 | } | |
129 | ||
130 | if (map_count > 0 || release_count > 0) { | |
131 | const char *node; | |
132 | int fd; | |
133 | unsigned int i; | |
134 | ||
135 | node = udev_device_get_devnode(dev); | |
136 | if (!node) { | |
9f6445e3 | 137 | log_error("Error, no device node for '%s'", udev_device_get_syspath(dev)); |
9d7d42bc KS |
138 | return EXIT_FAILURE; |
139 | } | |
140 | ||
141 | fd = open(udev_device_get_devnode(dev), O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
142 | if (fd < 0) { | |
56f64d95 | 143 | log_error_errno(errno, "Error, opening device '%s': %m", node); |
9d7d42bc KS |
144 | return EXIT_FAILURE; |
145 | } | |
146 | ||
147 | /* install list of map codes */ | |
148 | for (i = 0; i < map_count; i++) { | |
9f6445e3 | 149 | log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)", |
9d7d42bc KS |
150 | map[i].scan, map[i].scan, map[i].key, map[i].key); |
151 | if (ioctl(fd, EVIOCSKEYCODE, &map[i]) < 0) | |
56f64d95 | 152 | 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); |
9d7d42bc KS |
153 | } |
154 | ||
155 | /* install list of force-release codes */ | |
156 | if (release_count > 0) | |
157 | install_force_release(dev, release, release_count); | |
158 | ||
159 | close(fd); | |
160 | } | |
161 | ||
162 | return EXIT_SUCCESS; | |
163 | } | |
164 | ||
165 | const struct udev_builtin udev_builtin_keyboard = { | |
166 | .name = "keyboard", | |
167 | .cmd = builtin_keyboard, | |
168 | .help = "keyboard scan code to key mapping", | |
169 | }; |