udev-builtin-input_id.c revision 1a3439ef68d02e8ca4da02a7c1816d596e70de8b
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * expose input properties via udev
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen *
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * Copyright (C) 2014 Carlos Garnacho <carlosg@gnome.org>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen *
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * This program is free software: you can redistribute it and/or modify
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * it under the terms of the GNU General Public License as published by
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * the Free Software Foundation, either version 2 of the License, or
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * (at your option) any later version.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen *
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * This program is distributed in the hope that it will be useful,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * GNU General Public License for more details.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen *
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * You should have received a copy of the GNU General Public License
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * along with this program. If not, see <http://www.gnu.org/licenses/>.
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <errno.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <stdarg.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <stdio.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <stdlib.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <string.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <unistd.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <linux/limits.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include <linux/input.h>
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
920b52e4909d9dc812817fd8b82f83ca23a11c91Thomas Hindoe Paaboel Andersen#include "fd-util.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "string-util.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "udev.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#include "util.h"
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/* we must use this kernel-compatible implementation */
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering#define BITS_PER_LONG (sizeof(unsigned long) * 8)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
f0213e3796b4dd66e546e2de4d677db319f9171bTom Gundersen#define OFF(x) ((x)%BITS_PER_LONG)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#define BIT(x) (1UL<<OFF(x))
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering#define LONG(x) ((x)/BITS_PER_LONG)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic inline int abs_size_mm(const struct input_absinfo *absinfo) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* Resolution is defined to be in units/mm for ABS_X/Y */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return (absinfo->maximum - absinfo->minimum) / absinfo->resolution;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen}
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void extract_info(struct udev_device *dev, const char *devpath, bool test) {
91b5f997316ddc77d26f9a7a5e24c335484586bdTom Gundersen char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)];
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen struct input_absinfo xabsinfo = {}, yabsinfo = {};
ed9e361a8a798f9fee353b5c7e0e23308e0d329fTom Gundersen _cleanup_close_ int fd = -1;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering fd = open(devpath, O_RDONLY|O_CLOEXEC);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (fd < 0)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0)
86655331bc28887def7998d321b14ef8fccbeaf9Tom Gundersen return;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
91b5f997316ddc77d26f9a7a5e24c335484586bdTom Gundersen if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return;
8012cd391932d58b44332df106d426a360faf0a6Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen}
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * Read a capability attribute and return bitmask.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * @param dev udev_device
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * @param attr sysfs attribute name (e. g. "capabilities/key")
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen * @param bitmask: Output array which has a sizeof of bitmask_size
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen */
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poetteringstatic void get_cap_mask(struct udev_device *dev,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen struct udev_device *pdev, const char* attr,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned long *bitmask, size_t bitmask_size,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool test) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const char *v;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen char text[4096];
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned i;
8012cd391932d58b44332df106d426a360faf0a6Tom Gundersen char* word;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned long val;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen v = udev_device_get_sysattr_value(pdev, attr);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (!v)
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen v = "";
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(text, sizeof(text), "%s", v);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_debug("%s raw kernel attribute: %s", attr, text);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen memzero(bitmask, bitmask_size);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen i = 0;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen while ((word = strrchr(text, ' ')) != NULL) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen val = strtoul (word+1, NULL, 16);
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering if (i < bitmask_size/sizeof(unsigned long))
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bitmask[i] = val;
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen else
200a0868fcdf7b95f3d8d1fda3aa2aef48d84fddTom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen *word = '\0';
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen ++i;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen }
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen val = strtoul (text, NULL, 16);
8012cd391932d58b44332df106d426a360faf0a6Tom Gundersen if (i < bitmask_size / sizeof(unsigned long))
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bitmask[i] = val;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen else
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (test) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* printf pattern with the right unsigned long number of hex chars */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(text, sizeof(text), " bit %%4u: %%0%zulX\n", 2 * sizeof(unsigned long));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_debug("%s decoded bit map:", attr);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen val = bitmask_size / sizeof (unsigned long);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* skip over leading zeros */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen while (bitmask[val-1] == 0 && val > 0)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen --val;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen for (i = 0; i < val; ++i) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen DISABLE_WARNING_FORMAT_NONLITERAL;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_debug(text, i * BITS_PER_LONG, bitmask[i]);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen REENABLE_WARNING;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen }
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen }
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen}
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/* pointer devices */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic bool test_pointers(struct udev_device *dev,
f0213e3796b4dd66e546e2de4d677db319f9171bTom Gundersen const unsigned long* bitmask_ev,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_abs,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_key,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_rel,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_props,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool test) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool has_abs_coordinates = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool has_rel_coordinates = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool has_mt_coordinates = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool has_joystick_axes_or_buttons = false;
6666907869fb3bc7fe6a6025540db5b887c7a78bTom Gundersen bool is_direct = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool has_touch = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool has_3d_coordinates = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool has_keys = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool stylus_or_pen = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool finger_but_no_pen = false;
ed9e361a8a798f9fee353b5c7e0e23308e0d329fTom Gundersen bool has_mouse_button = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool is_mouse = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool is_touchpad = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool is_touchscreen = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool is_tablet = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool is_joystick = false;
ed9e361a8a798f9fee353b5c7e0e23308e0d329fTom Gundersen bool is_accelerometer = false;
86655331bc28887def7998d321b14ef8fccbeaf9Tom Gundersen bool is_pointing_stick= false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_keys = test_bit(EV_KEY, bitmask_ev);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_abs_coordinates = test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_3d_coordinates = has_abs_coordinates && test_bit(ABS_Z, bitmask_abs);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_accelerometer = test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (!has_keys && has_3d_coordinates)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_accelerometer = true;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (is_accelerometer) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return true;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen }
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_pointing_stick = test_bit(INPUT_PROP_POINTING_STICK, bitmask_props);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen stylus_or_pen = test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen finger_but_no_pen = test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_mouse_button = test_bit(BTN_LEFT, bitmask_key);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_rel_coordinates = test_bit(EV_REL, bitmask_ev) && test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_mt_coordinates = test_bit(ABS_MT_POSITION_X, bitmask_abs) && test_bit(ABS_MT_POSITION_Y, bitmask_abs);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* unset has_mt_coordinates if devices claims to have all abs axis */
be19c5b5e0c0f78b8429b126936fa15856550a23David Herrmann if(has_mt_coordinates && test_bit(ABS_MT_SLOT, bitmask_abs) && test_bit(ABS_MT_SLOT - 1, bitmask_abs))
be19c5b5e0c0f78b8429b126936fa15856550a23David Herrmann has_mt_coordinates = false;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_direct = test_bit(INPUT_PROP_DIRECT, bitmask_props);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_touch = test_bit(BTN_TOUCH, bitmask_key);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* joysticks don't necessarily have buttons; e. g.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * rudders/pedals are joystick-like, but buttonless; they have
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * other fancy axes */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen has_joystick_axes_or_buttons = test_bit(BTN_TRIGGER, bitmask_key) ||
be19c5b5e0c0f78b8429b126936fa15856550a23David Herrmann test_bit(BTN_A, bitmask_key) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(BTN_1, bitmask_key) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(ABS_RX, bitmask_abs) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(ABS_RY, bitmask_abs) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(ABS_RZ, bitmask_abs) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(ABS_THROTTLE, bitmask_abs) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(ABS_RUDDER, bitmask_abs) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(ABS_WHEEL, bitmask_abs) ||
b45e4eb679ad0c9a77c4fe6e404c8842d4097fdbTom Gundersen test_bit(ABS_GAS, bitmask_abs) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_bit(ABS_BRAKE, bitmask_abs);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (has_abs_coordinates) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (stylus_or_pen)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_tablet = true;
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt else if (finger_but_no_pen && !is_direct)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_touchpad = true;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen else if (has_mouse_button)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* This path is taken by VMware's USB mouse, which has
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * absolute axes, but no touch/pressure button. */
e0ee46f29028e291eb67f435aff1b6202d75d9d6Lennart Poettering is_mouse = true;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen else if (has_touch || is_direct)
0bc70f1d9c5453ba614ec0ed041dc30b9cd52071Tom Gundersen is_touchscreen = true;
0bc70f1d9c5453ba614ec0ed041dc30b9cd52071Tom Gundersen else if (has_joystick_axes_or_buttons)
0bc70f1d9c5453ba614ec0ed041dc30b9cd52071Tom Gundersen is_joystick = true;
0bc70f1d9c5453ba614ec0ed041dc30b9cd52071Tom Gundersen }
0bc70f1d9c5453ba614ec0ed041dc30b9cd52071Tom Gundersen if (has_mt_coordinates && (is_direct || has_touch))
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_touchscreen = true;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt if (has_rel_coordinates && has_mouse_button)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_mouse = true;
e3dca0089b7b50e2ec21409d1292727921d06102Tom Gundersen
e3dca0089b7b50e2ec21409d1292727921d06102Tom Gundersen if (is_pointing_stick)
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (is_mouse)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (is_touchpad)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (is_touchscreen)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (is_joystick)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (is_tablet)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return is_tablet || is_mouse || is_touchpad || is_touchscreen || is_joystick || is_pointing_stick;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen}
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/* key like devices */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic bool test_key(struct udev_device *dev,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_ev,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_key,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool test) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned i;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned long found;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned long mask;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool ret = false;
/* do we have any KEY_* capability? */
if (!test_bit(EV_KEY, bitmask_ev)) {
log_debug("test_key: no EV_KEY capability");
return false;
}
/* only consider KEY_* here, not BTN_* */
found = 0;
for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) {
found |= bitmask_key[i];
log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0);
}
/* If there are no keys in the lower block, check the higher block */
if (!found) {
for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
if (test_bit(i, bitmask_key)) {
log_debug("test_key: Found key %x in high block", i);
found = 1;
break;
}
}
}
if (found > 0) {
udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1");
ret = true;
}
/* the first 32 bits are ESC, numbers, and Q to D; if we have all of
* those, consider it a full keyboard; do not test KEY_RESERVED, though */
mask = 0xFFFFFFFE;
if ((bitmask_key[0] & mask) == mask) {
udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1");
ret = true;
}
return ret;
}
static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) {
struct udev_device *pdev;
unsigned long bitmask_ev[NBITS(EV_MAX)];
unsigned long bitmask_abs[NBITS(ABS_MAX)];
unsigned long bitmask_key[NBITS(KEY_MAX)];
unsigned long bitmask_rel[NBITS(REL_MAX)];
unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)];
const char *sysname, *devnode;
bool is_pointer;
bool is_key;
assert(dev);
/* walk up the parental chain until we find the real input device; the
* argument is very likely a subdevice of this, like eventN */
pdev = dev;
while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL)
pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL);
if (pdev) {
/* Use this as a flag that input devices were detected, so that this
* program doesn't need to be called more than once per device */
udev_builtin_add_property(dev, test, "ID_INPUT", "1");
get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test);
get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test);
get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test);
get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test);
get_cap_mask(dev, pdev, "properties", bitmask_props, sizeof(bitmask_props), test);
is_pointer = test_pointers(dev, bitmask_ev, bitmask_abs,
bitmask_key, bitmask_rel,
bitmask_props, test);
is_key = test_key(dev, bitmask_ev, bitmask_key, test);
/* Some evdev nodes have only a scrollwheel */
if (!is_pointer && !is_key && test_bit(EV_REL, bitmask_ev) &&
(test_bit(REL_WHEEL, bitmask_rel) || test_bit(REL_HWHEEL, bitmask_rel)))
udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1");
}
devnode = udev_device_get_devnode(dev);
sysname = udev_device_get_sysname(dev);
if (devnode && sysname && startswith(sysname, "event"))
extract_info(dev, devnode, test);
return EXIT_SUCCESS;
}
const struct udev_builtin udev_builtin_input_id = {
.name = "input_id",
.cmd = builtin_input_id,
.help = "Input device properties",
};