udev-builtin-input_id.c revision b914418a153098c5112f1e4487e01e5a14cc60dc
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * expose input properties via udev
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * Copyright (C) 2014 Carlos Garnacho <carlosg@gnome.org>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * This program is free software: you can redistribute it and/or modify
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * it under the terms of the GNU General Public License as published by
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * the Free Software Foundation, either version 2 of the License, or
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * (at your option) any later version.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * This program is distributed in the hope that it will be useful,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * GNU General Public License for more details.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * You should have received a copy of the GNU General Public License
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * along with this program. If not, see <http://www.gnu.org/licenses/>.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/* we must use this kernel-compatible implementation */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen#define BITS_PER_LONG (sizeof(unsigned long) * 8)
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic inline int abs_size_mm(const struct input_absinfo *absinfo) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* Resolution is defined to be in units/mm for ABS_X/Y */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return (absinfo->maximum - absinfo->minimum) / absinfo->resolution;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersenstatic void extract_info(struct udev_device *dev, const char *devpath, bool test) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)];
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen struct input_absinfo xabsinfo = {}, yabsinfo = {};
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height);
977085794d2996320e345433403de75f662b0622Tom Gundersen * Read a capability attribute and return bitmask.
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen * @param dev udev_device
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen * @param attr sysfs attribute name (e. g. "capabilities/key")
9dc670ea766c711741f462b29572f2e5f8f3f6bcTom Gundersen * @param bitmask: Output array which has a sizeof of bitmask_size
9dc670ea766c711741f462b29572f2e5f8f3f6bcTom Gundersenstatic void get_cap_mask(struct udev_device *dev,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const char *v;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long val;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen v = udev_device_get_sysattr_value(pdev, attr);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_debug("%s raw kernel attribute: %s", attr, text);
977085794d2996320e345433403de75f662b0622Tom Gundersen if (i < bitmask_size/sizeof(unsigned long))
977085794d2996320e345433403de75f662b0622Tom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
977085794d2996320e345433403de75f662b0622Tom Gundersen if (i < bitmask_size / sizeof(unsigned long))
977085794d2996320e345433403de75f662b0622Tom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
977085794d2996320e345433403de75f662b0622Tom Gundersen /* printf pattern with the right unsigned long number of hex chars */
977085794d2996320e345433403de75f662b0622Tom Gundersen snprintf(text, sizeof(text), " bit %%4u: %%0%zulX\n", 2 * sizeof(unsigned long));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* skip over leading zeros */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen for (i = 0; i < val; ++i) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_debug(text, i * BITS_PER_LONG, bitmask[i]);
d2df0d0ed3a88e491405b403e6022e6619750130Tom Gundersen/* pointer devices */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic void test_pointers(struct udev_device *dev,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const unsigned long* bitmask_ev,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const unsigned long* bitmask_abs,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const unsigned long* bitmask_key,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const unsigned long* bitmask_rel,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const unsigned long* bitmask_props,
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen if (test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props)) {
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
2fd069b18e525860514a70d3ea08410ca122d3e2Zbigniew Jędrzejewski-Szmek if (test_bit(EV_ABS, bitmask_ev) &&
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key))
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1");
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* This path is taken by VMware's USB mouse, which has
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * absolute axes, but no touch/pressure button. */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* joysticks don't necessarily have to have buttons; e. g.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * rudders/pedals are joystick-like, but buttonless; they have
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * other fancy axes */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen else if (test_bit(BTN_TRIGGER, bitmask_key) ||
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (test_bit(INPUT_PROP_POINTING_STICK, bitmask_props))
98a375f6d5cac24eb80d6d4e00699851324afdecTom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) &&
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1");
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen/* key like devices */
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen const unsigned long* bitmask_ev,
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen const unsigned long* bitmask_key,
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering unsigned long found;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering unsigned long mask;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering /* do we have any KEY_* capability? */
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen /* only consider KEY_* here, not BTN_* */
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* If there are no keys in the lower block, check the higher block */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen log_debug("test_key: Found key %x in high block", i);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * those, consider it a full keyboard; do not test KEY_RESERVED, though */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)];
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* walk up the parental chain until we find the real input device; the
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * argument is very likely a subdevice of this, like eventN */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL);
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen /* Use this as a flag that input devices were detected, so that this
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen * program doesn't need to be called more than once per device */
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT", "1");
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test);
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test);
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen get_cap_mask(dev, pdev, "properties", bitmask_props, sizeof(bitmask_props), test);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen test_pointers(dev, bitmask_ev, bitmask_abs, bitmask_key,
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen if (devnode && sysname && startswith(sysname, "event"))