udev-builtin-input_id.c revision 1a3439ef68d02e8ca4da02a7c1816d596e70de8b
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * expose input properties via udev
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 * 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 * 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 * 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/>.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/* we must use this kernel-compatible implementation */
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering#define BITS_PER_LONG (sizeof(unsigned long) * 8)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
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 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 = {};
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
91b5f997316ddc77d26f9a7a5e24c335484586bdTom Gundersen if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo));
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 * 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
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poetteringstatic void get_cap_mask(struct udev_device *dev,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const char *v;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned long val;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen v = udev_device_get_sysattr_value(pdev, attr);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_debug("%s raw kernel attribute: %s", attr, text);
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering if (i < bitmask_size/sizeof(unsigned long))
200a0868fcdf7b95f3d8d1fda3aa2aef48d84fddTom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
8012cd391932d58b44332df106d426a360faf0a6Tom Gundersen if (i < bitmask_size / sizeof(unsigned long))
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
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 /* skip over leading zeros */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen for (i = 0; i < val; ++i) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen log_debug(text, i * BITS_PER_LONG, bitmask[i]);
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 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 udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
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 /* 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))
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen is_direct = test_bit(INPUT_PROP_DIRECT, bitmask_props);
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) ||
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* This path is taken by VMware's USB mouse, which has
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen * absolute axes, but no touch/pressure button. */
0bc70f1d9c5453ba614ec0ed041dc30b9cd52071Tom Gundersen if (has_mt_coordinates && (is_direct || has_touch))
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1");
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen return is_tablet || is_mouse || is_touchpad || is_touchscreen || is_joystick || is_pointing_stick;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/* key like devices */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_ev,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const unsigned long* bitmask_key,
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned long found;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen unsigned long mask;
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen bool ret = false;
found = 0;
log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0);
if (!found) {
if (found > 0) {
ret = true;
ret = true;
return ret;
bool is_pointer;
bool is_key;
if (pdev) {
return EXIT_SUCCESS;