evdev: implement support for the MOUSE_WHEEL_CLICK_COUNT property
authorPeter Hutterer <peter.hutterer@who-t.net>
Fri, 28 Oct 2016 05:08:32 +0000 (15:08 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Fri, 4 Nov 2016 02:10:52 +0000 (12:10 +1000)
Not all mice have a click angle with integer degrees. The new
MOUSE_WHEEL_CLICK_COUNT property specifies how many clicks per full rotation,
the angle can be calculated from that.

See https://github.com/systemd/systemd/pull/4440 for more information

CLICK_COUNT overrides CLICK_ANGLE, so we check for the former first and then
fall back to the angle if need be. No changes to the user-facing API.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
src/evdev.c
src/libinput-private.h
src/libinput-util.c
src/libinput-util.h
test/Makefile.am
test/litest-device-mouse-wheel-click-count.c [new file with mode: 0644]
test/litest.c
test/litest.h
test/misc.c
test/pointer.c

index 2412751e2a59da5aa6d4a7f4356aa2d8f6610679..d04636f33c6dcf0f35267366139801856aa4aa52 100644 (file)
@@ -2008,7 +2008,7 @@ evdev_device_init_pointer_acceleration(struct evdev_device *device,
 static inline bool
 evdev_read_wheel_click_prop(struct evdev_device *device,
                            const char *prop,
-                           int *angle)
+                           double *angle)
 {
        int val;
 
@@ -2032,18 +2032,53 @@ evdev_read_wheel_click_prop(struct evdev_device *device,
        return false;
 }
 
+static inline bool
+evdev_read_wheel_click_count_prop(struct evdev_device *device,
+                                 const char *prop,
+                                 double *angle)
+{
+       int val;
+
+       prop = udev_device_get_property_value(device->udev_device, prop);
+       if (!prop)
+               return false;
+
+       val = parse_mouse_wheel_click_angle_property(prop);
+       if (val) {
+               *angle = 360.0/val;
+               return true;
+       }
+
+       log_error(evdev_libinput_context(device),
+                 "Mouse wheel click count '%s' is present but invalid, "
+                 "using %d degrees for angle instead instead\n",
+                 device->devname,
+                 DEFAULT_WHEEL_CLICK_ANGLE);
+       *angle = DEFAULT_WHEEL_CLICK_ANGLE;
+
+       return false;
+}
+
 static inline struct wheel_angle
 evdev_read_wheel_click_props(struct evdev_device *device)
 {
        struct wheel_angle angles;
 
-       evdev_read_wheel_click_prop(device,
-                                   "MOUSE_WHEEL_CLICK_ANGLE",
-                                   &angles.x);
-       if (!evdev_read_wheel_click_prop(device,
-                                        "MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL",
-                                        &angles.y))
-               angles.y = angles.x;
+       /* CLICK_COUNT overrides CLICK_ANGLE */
+       if (!evdev_read_wheel_click_count_prop(device,
+                                             "MOUSE_WHEEL_CLICK_COUNT",
+                                             &angles.x))
+               evdev_read_wheel_click_prop(device,
+                                           "MOUSE_WHEEL_CLICK_ANGLE",
+                                           &angles.x);
+       if (!evdev_read_wheel_click_count_prop(device,
+                                             "MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL",
+                                             &angles.y)) {
+               if (!evdev_read_wheel_click_prop(device,
+                                                "MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL",
+                                                &angles.y))
+                       angles.y = angles.x;
+       }
 
        return angles;
 }
index 5b46da79cc8023ca304d73b4263c16299af40a3e..52f129aafc09d3ebf10f4d998bfbe2eb8effa911 100644 (file)
@@ -73,7 +73,7 @@ struct normalized_range_coords {
 
 /* A pair of angles in degrees */
 struct wheel_angle {
-       int x, y;
+       double x, y;
 };
 
 /* A pair of angles in degrees */
index 4b90fbb06115224f8b770d5cf2748888bd254712..6c051c38af7d10e2eadc00b4ecab868d2fa673b3 100644 (file)
@@ -176,6 +176,38 @@ parse_mouse_dpi_property(const char *prop)
 }
 
 /**
+ * Helper function to parse the MOUSE_WHEEL_CLICK_COUNT property from udev.
+ * Property is of the form:
+ * MOUSE_WHEEL_CLICK_COUNT=<integer>
+ * Where the number indicates the number of wheel clicks per 360 deg
+ * rotation.
+ *
+ * We skip preceding whitespaces and parse the first number seen. If
+ * multiple numbers are specified, we ignore those.
+ *
+ * @param prop The value of the udev property (without the MOUSE_WHEEL_CLICK_COUNT=)
+ * @return The click count of the wheel (may be negative) or 0 on error.
+ */
+int
+parse_mouse_wheel_click_count_property(const char *prop)
+{
+       int count = 0,
+           nread = 0;
+
+       while(*prop != 0 && *prop == ' ')
+               prop++;
+
+       sscanf(prop, "%d%n", &count, &nread);
+       if (nread == 0 || count == 0 || abs(count) > 360)
+               return 0;
+       if (prop[nread] != ' ' && prop[nread] != '\0')
+               return 0;
+
+        return count;
+}
+
+/**
+ *
  * Helper function to parse the MOUSE_WHEEL_CLICK_ANGLE property from udev.
  * Property is of the form:
  * MOUSE_WHEEL_CLICK_ANGLE=<integer>
index e31860dd9ee4121f833706733637b03633477695..b7bef80c6930018f7d8785393ae41aa8eed166f8 100644 (file)
@@ -370,6 +370,7 @@ enum ratelimit_state ratelimit_test(struct ratelimit *r);
 
 int parse_mouse_dpi_property(const char *prop);
 int parse_mouse_wheel_click_angle_property(const char *prop);
+int parse_mouse_wheel_click_count_property(const char *prop);
 double parse_trackpoint_accel_property(const char *prop);
 bool parse_dimension_property(const char *prop, size_t *width, size_t *height);
 
index 7ff12e437cb97a0b95a7be3f7180752e6cae7b3e..f4a9252bc8ebe46f39f7905ac2c6bb6f13cd57c9 100644 (file)
@@ -35,6 +35,7 @@ liblitest_la_SOURCES = \
        litest-device-mouse-roccat.c \
        litest-device-mouse-low-dpi.c \
        litest-device-mouse-wheel-click-angle.c \
+       litest-device-mouse-wheel-click-count.c \
        litest-device-ms-surface-cover.c \
        litest-device-protocol-a-touch-screen.c \
        litest-device-qemu-usb-tablet.c \
diff --git a/test/litest-device-mouse-wheel-click-count.c b/test/litest-device-mouse-wheel-click-count.c
new file mode 100644 (file)
index 0000000..419b702
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright © 2016 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "litest.h"
+#include "litest-int.h"
+
+static void litest_mouse_setup(void)
+{
+       struct litest_device *d = litest_create_device(LITEST_MOUSE_WHEEL_CLICK_COUNT);
+       litest_set_current_device(d);
+}
+
+static struct input_id input_id = {
+       .bustype = 0x3,
+       .vendor = 0x1234,
+       .product = 0x5678,
+};
+
+static int events[] = {
+       EV_KEY, BTN_LEFT,
+       EV_KEY, BTN_RIGHT,
+       EV_KEY, BTN_MIDDLE,
+       EV_REL, REL_X,
+       EV_REL, REL_Y,
+       EV_REL, REL_WHEEL,
+       -1 , -1,
+};
+
+static const char udev_rule[] =
+"ACTION==\"remove\", GOTO=\"wheel_click_count_end\"\n"
+"KERNEL!=\"event*\", GOTO=\"wheel_click_count_end\"\n"
+"\n"
+"ATTRS{name}==\"litest Wheel Click Count Mouse*\",\\\n"
+"    ENV{MOUSE_WHEEL_CLICK_ANGLE}=\"-15\",\n"
+"    ENV{MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL}=\"13\",\n\\"
+"    ENV{MOUSE_WHEEL_CLICK_COUNT}=\"-14\",\n"
+"    ENV{MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL}=\"27\"\\\n"
+"\n"
+"LABEL=\"wheel_click_count_end\"";
+
+struct litest_test_device litest_mouse_wheel_click_count_device = {
+       .type = LITEST_MOUSE_WHEEL_CLICK_COUNT,
+       .features = LITEST_RELATIVE | LITEST_BUTTON | LITEST_WHEEL,
+       .shortname = "mouse-wheelclickcount",
+       .setup = litest_mouse_setup,
+       .interface = NULL,
+
+       .name = "Wheel Click Count Mouse",
+       .id = &input_id,
+       .absinfo = NULL,
+       .events = events,
+       .udev_rule = udev_rule,
+};
index fd62d2140a2e71faf1b415ad4740ed5ed8f36738..515eb18d98cdd88586d3d20ee4c0b79cf5eae7c5 100644 (file)
@@ -402,6 +402,7 @@ extern struct litest_test_device litest_wacom_cintiq_13hdt_finger_device;
 extern struct litest_test_device litest_wacom_cintiq_13hdt_pen_device;
 extern struct litest_test_device litest_wacom_cintiq_13hdt_pad_device;
 extern struct litest_test_device litest_wacom_hid4800_tablet_device;
+extern struct litest_test_device litest_mouse_wheel_click_count_device;
 
 struct litest_test_device* devices[] = {
        &litest_synaptics_clickpad_device,
@@ -458,6 +459,7 @@ struct litest_test_device* devices[] = {
        &litest_wacom_cintiq_13hdt_pen_device,
        &litest_wacom_cintiq_13hdt_pad_device,
        &litest_wacom_hid4800_tablet_device,
+       &litest_mouse_wheel_click_count_device,
        NULL,
 };
 
index 460235549d905db675046d7bd1afd71692287999..d52ebd2fca5f3d35ff99ac9cb5c0dcd352795e5c 100644 (file)
@@ -224,6 +224,7 @@ enum litest_device_type {
        LITEST_WACOM_CINTIQ_13HDT_PAD,
        LITEST_WACOM_CINTIQ_13HDT_FINGER,
        LITEST_WACOM_HID4800_PEN,
+       LITEST_MOUSE_WHEEL_CLICK_COUNT,
 };
 
 enum litest_device_feature {
index 582d4fcfc5c95cf6af2a8a93e719acc931f30f1d..4afd4d003175fb3f124a7f16e2f9b0f2202d5868 100644 (file)
@@ -750,6 +750,35 @@ START_TEST(wheel_click_parser)
 }
 END_TEST
 
+START_TEST(wheel_click_count_parser)
+{
+       struct parser_test tests[] = {
+               { "1", 1 },
+               { "10", 10 },
+               { "-12", -12 },
+               { "360", 360 },
+               { "66 ", 66 },
+               { "   100 ", 100 },
+
+               { "0", 0 },
+               { "-0", 0 },
+               { "a", 0 },
+               { "10a", 0 },
+               { "10-", 0 },
+               { "sadfasfd", 0 },
+               { "361", 0 },
+               { NULL, 0 }
+       };
+
+       int i, angle;
+
+       for (i = 0; tests[i].tag != NULL; i++) {
+               angle = parse_mouse_wheel_click_count_property(tests[i].tag);
+               ck_assert_int_eq(angle, tests[i].expected_value);
+       }
+}
+END_TEST
+
 struct parser_test_float {
        char *tag;
        double expected_value;
@@ -956,6 +985,7 @@ litest_setup_tests_misc(void)
        litest_add_no_device("misc:ratelimit", ratelimit_helpers);
        litest_add_no_device("misc:parser", dpi_parser);
        litest_add_no_device("misc:parser", wheel_click_parser);
+       litest_add_no_device("misc:parser", wheel_click_count_parser);
        litest_add_no_device("misc:parser", trackpoint_accel_parser);
        litest_add_no_device("misc:parser", dimension_prop_parser);
        litest_add_no_device("misc:time", time_conversion);
index 175cb3b19ce900e74a2382afbf90376b3f376e76..4f33de5e243683d69a263d075df1775e1563268c 100644 (file)
@@ -473,14 +473,45 @@ START_TEST(pointer_button_auto_release)
 }
 END_TEST
 
-static inline int
+static inline double
+wheel_click_count(struct litest_device *dev, int which)
+{
+       struct udev_device *d;
+       const char *prop = NULL;
+       int count;
+       double angle = 0.0;
+
+       d = libinput_device_get_udev_device(dev->libinput_device);
+       litest_assert_ptr_notnull(d);
+
+       if (which == REL_HWHEEL)
+               prop = udev_device_get_property_value(d, "MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL");
+       if(!prop)
+               prop = udev_device_get_property_value(d, "MOUSE_WHEEL_CLICK_COUNT");
+       if (!prop)
+               goto out;
+
+       count = parse_mouse_wheel_click_count_property(prop);
+       angle = 360.0/count;
+
+out:
+       udev_device_unref(d);
+       return angle;
+}
+
+static inline double
 wheel_click_angle(struct litest_device *dev, int which)
 {
        struct udev_device *d;
        const char *prop = NULL;
        const int default_angle = 15;
-       int angle = default_angle;
+       double angle;
+
+       angle = wheel_click_count(dev, which);
+       if (angle != 0.0)
+               return angle;
 
+       angle = default_angle;
        d = libinput_device_get_udev_device(dev->libinput_device);
        litest_assert_ptr_notnull(d);
 
@@ -492,7 +523,7 @@ wheel_click_angle(struct litest_device *dev, int which)
                goto out;
 
        angle = parse_mouse_wheel_click_angle_property(prop);
-       if (angle == 0)
+       if (angle == 0.0)
                angle = default_angle;
 
 out:
@@ -508,7 +539,7 @@ test_wheel_event(struct litest_device *dev, int which, int amount)
        struct libinput_event_pointer *ptrev;
        enum libinput_pointer_axis axis;
 
-       int scroll_step, expected, discrete;;
+       double scroll_step, expected, discrete;
 
        scroll_step = wheel_click_angle(dev, which);
        expected = amount * scroll_step;
@@ -535,10 +566,12 @@ test_wheel_event(struct litest_device *dev, int which, int amount)
                                     axis,
                                     LIBINPUT_POINTER_AXIS_SOURCE_WHEEL);
 
-       litest_assert_int_eq(libinput_event_pointer_get_axis_value(ptrev, axis),
-                        expected);
-       litest_assert_int_eq(libinput_event_pointer_get_axis_value_discrete(ptrev, axis),
-                            discrete);
+       litest_assert_double_eq(
+                       libinput_event_pointer_get_axis_value(ptrev, axis),
+                       expected);
+       litest_assert_double_eq(
+                       libinput_event_pointer_get_axis_value_discrete(ptrev, axis),
+                       discrete);
        libinput_event_destroy(event);
 }