2 * evdev_id - extracts miscellaneous information from evdev devices
4 * Copyright (C) 2014 Red Hat
6 * Carlos Garnacho <carlosg@gnome.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with keymap; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 #include <linux/input.h>
27 static inline int abs_size_mm(const struct input_absinfo *absinfo) {
28 /* Resolution is defined to be in units/mm for ABS_X/Y */
29 return (absinfo->maximum - absinfo->minimum) / absinfo->resolution;
32 static void extract_info(struct udev_device *dev, const char *devpath, bool test) {
33 char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)];
34 struct input_absinfo xabsinfo = {}, yabsinfo = {};
35 _cleanup_close_ int fd = -1;
37 fd = open(devpath, O_RDONLY|O_CLOEXEC);
41 if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
42 ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0)
45 if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
48 snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo));
49 snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo));
51 udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width);
52 udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height);
55 static int builtin_evdev_id(struct udev_device *dev, int argc, char *argv[], bool test) {
56 const char *subsystem;
59 subsystem = udev_device_get_subsystem(dev);
61 if (!subsystem || !streq(subsystem, "input"))
64 devnode = udev_device_get_devnode(dev);
65 /* not an evdev node */
69 extract_info(dev, devnode, test);
74 const struct udev_builtin udev_builtin_evdev_id = {
76 .cmd = builtin_evdev_id,
77 .help = "evdev devices information",