udev-builtin-input_id.c revision b914418a153098c5112f1e4487e01e5a14cc60dc
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/*
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen * expose input properties via udev
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen *
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 *
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 *
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 *
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/>.
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen */
2a73e0d39a9bec82c3800071e375d27164727e71Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include <stdio.h>
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#include <stdlib.h>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen#include <stdarg.h>
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#include <unistd.h>
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen#include <string.h>
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen#include <errno.h>
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include <linux/limits.h>
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include <linux/input.h>
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen#include "udev.h"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen#include "util.h"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/* we must use this kernel-compatible implementation */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen#define BITS_PER_LONG (sizeof(unsigned long) * 8)
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#define OFF(x) ((x)%BITS_PER_LONG)
3aeb37bc4f32b5edc334f2ac7c5d3c7b0a121328Tom Gundersen#define BIT(x) (1UL<<OFF(x))
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen#define LONG(x) ((x)/BITS_PER_LONG)
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
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;
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen}
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen
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 = {};
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen _cleanup_close_ int fd = -1;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen fd = open(devpath, O_RDONLY|O_CLOEXEC);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (fd < 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return;
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return;
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo));
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
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);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen/*
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 Gundersen */
9dc670ea766c711741f462b29572f2e5f8f3f6bcTom Gundersenstatic void get_cap_mask(struct udev_device *dev,
c50e28a38ccbdd1e1d1f082dd67bb3e74be4d459Dave Reisner struct udev_device *pdev, const char* attr,
c50e28a38ccbdd1e1d1f082dd67bb3e74be4d459Dave Reisner unsigned long *bitmask, size_t bitmask_size,
c50e28a38ccbdd1e1d1f082dd67bb3e74be4d459Dave Reisner bool test) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const char *v;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen char text[4096];
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned i;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen char* word;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long val;
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen v = udev_device_get_sysattr_value(pdev, attr);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!v)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen v = "";
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen snprintf(text, sizeof(text), "%s", v);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_debug("%s raw kernel attribute: %s", attr, text);
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen memzero(bitmask, bitmask_size);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen i = 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen while ((word = strrchr(text, ' ')) != NULL) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen val = strtoul (word+1, NULL, 16);
977085794d2996320e345433403de75f662b0622Tom Gundersen if (i < bitmask_size/sizeof(unsigned long))
977085794d2996320e345433403de75f662b0622Tom Gundersen bitmask[i] = val;
977085794d2996320e345433403de75f662b0622Tom Gundersen else
977085794d2996320e345433403de75f662b0622Tom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
977085794d2996320e345433403de75f662b0622Tom Gundersen *word = '\0';
977085794d2996320e345433403de75f662b0622Tom Gundersen ++i;
977085794d2996320e345433403de75f662b0622Tom Gundersen }
977085794d2996320e345433403de75f662b0622Tom Gundersen val = strtoul (text, NULL, 16);
977085794d2996320e345433403de75f662b0622Tom Gundersen if (i < bitmask_size / sizeof(unsigned long))
977085794d2996320e345433403de75f662b0622Tom Gundersen bitmask[i] = val;
977085794d2996320e345433403de75f662b0622Tom Gundersen else
977085794d2996320e345433403de75f662b0622Tom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
977085794d2996320e345433403de75f662b0622Tom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen if (test) {
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));
977085794d2996320e345433403de75f662b0622Tom Gundersen log_debug("%s decoded bit map:", attr);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen val = bitmask_size / sizeof (unsigned long);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* skip over leading zeros */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen while (bitmask[val-1] == 0 && val > 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen --val;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen for (i = 0; i < val; ++i) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen DISABLE_WARNING_FORMAT_NONLITERAL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_debug(text, i * BITS_PER_LONG, bitmask[i]);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen REENABLE_WARNING;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
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,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen bool test) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen int is_mouse = 0;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen int is_touchpad = 0;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen if (test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props)) {
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen return;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!test_bit(EV_KEY, bitmask_ev)) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (test_bit(EV_ABS, bitmask_ev) &&
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_X, bitmask_abs) &&
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_Y, bitmask_abs) &&
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_Z, bitmask_abs))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
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))
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen is_touchpad = 1;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen else if (test_bit(BTN_MOUSE, 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 is_mouse = 1;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen else if (test_bit(BTN_TOUCH, bitmask_key))
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 test_bit(BTN_A, bitmask_key) ||
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(BTN_1, bitmask_key) ||
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_RX, bitmask_abs) ||
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_RY, bitmask_abs) ||
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test_bit(ABS_RZ, bitmask_abs) ||
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test_bit(ABS_THROTTLE, bitmask_abs) ||
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test_bit(ABS_RUDDER, bitmask_abs) ||
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen test_bit(ABS_WHEEL, bitmask_abs) ||
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_GAS, bitmask_abs) ||
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(ABS_BRAKE, bitmask_abs))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1");
489124365d1d391864898b9869dd668eea5b2e28Dave Reisner }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
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
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (test_bit(EV_REL, bitmask_ev) &&
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) &&
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_bit(BTN_MOUSE, bitmask_key))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen is_mouse = 1;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (is_mouse)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (is_touchpad)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen/* key like devices */
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersenstatic void test_key(struct udev_device *dev,
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen const unsigned long* bitmask_ev,
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen const unsigned long* bitmask_key,
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen bool test) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen unsigned i;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering unsigned long found;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering unsigned long mask;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering /* do we have any KEY_* capability? */
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering if (!test_bit(EV_KEY, bitmask_ev)) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen log_debug("test_key: no EV_KEY capability");
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return;
ff83aac3647e21f31ac5e2b575ec1285dc585f6bTom Gundersen }
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen /* only consider KEY_* here, not BTN_* */
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen found = 0;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen found |= bitmask_key[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 }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen /* If there are no keys in the lower block, check the higher block */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!found) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (test_bit(i, bitmask_key)) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen log_debug("test_key: Found key %x in high block", i);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen found = 1;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen break;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen }
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen }
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen }
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (found > 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
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 mask = 0xFFFFFFFE;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if ((bitmask_key[0] & mask) == mask)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct udev_device *pdev;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long bitmask_ev[NBITS(EV_MAX)];
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long bitmask_abs[NBITS(ABS_MAX)];
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long bitmask_key[NBITS(KEY_MAX)];
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long bitmask_rel[NBITS(REL_MAX)];
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)];
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen const char *sysname, *devnode;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
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 pdev = dev;
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
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen if (pdev) {
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,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen bitmask_rel, bitmask_props, test);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen test_key(dev, bitmask_ev, bitmask_key, test);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen devnode = udev_device_get_devnode(dev);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen sysname = udev_device_get_sysname(dev);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen if (devnode && sysname && startswith(sysname, "event"))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen extract_info(dev, devnode, test);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return EXIT_SUCCESS;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen}
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersenconst struct udev_builtin udev_builtin_input_id = {
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen .name = "input_id",
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen .cmd = builtin_input_id,
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen .help = "Input device properties",
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen};
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen