From 4effe6b1b9245c694cd3166db3c4bd1a0ef1c823 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Mon, 28 Aug 2023 09:06:49 +1000 Subject: [PATCH] evdev: add a helper macro for the absinfo range The range is (max - min + 1) because the kernel range is inclusive min and max. Let's fix that once and for all with a helper function. Signed-off-by: Peter Hutterer --- src/evdev-mt-touchpad.c | 3 ++- src/evdev-tablet-pad.c | 3 ++- src/evdev-tablet.c | 15 ++++++--------- src/evdev.c | 23 +++++++++-------------- src/util-input-event.h | 6 ++++++ test/test-tablet.c | 15 ++++++++------- test/test-totem.c | 5 +++-- tools/libinput-debug-tablet.c | 3 ++- 8 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/evdev-mt-touchpad.c b/src/evdev-mt-touchpad.c index 5b040ce5..d0f9a506 100644 --- a/src/evdev-mt-touchpad.c +++ b/src/evdev-mt-touchpad.c @@ -34,6 +34,7 @@ #include "quirks.h" #include "evdev-mt-touchpad.h" +#include "util-input-event.h" #define DEFAULT_TRACKPOINT_ACTIVITY_TIMEOUT ms2us(300) #define DEFAULT_TRACKPOINT_EVENT_TIMEOUT ms2us(40) @@ -3629,7 +3630,7 @@ tp_init_pressure(struct tp_dispatch *tp, goto out; } } else { - unsigned int range = abs->maximum - abs->minimum; + double range = absinfo_range(abs); /* Approximately the synaptics defaults */ hi = abs->minimum + 0.12 * range; diff --git a/src/evdev-tablet-pad.c b/src/evdev-tablet-pad.c index 991c2851..de3d7cbe 100644 --- a/src/evdev-tablet-pad.c +++ b/src/evdev-tablet-pad.c @@ -23,6 +23,7 @@ #include "config.h" #include "evdev-tablet-pad.h" +#include "util-input-event.h" #include #include @@ -151,7 +152,7 @@ normalize_ring(const struct input_absinfo *absinfo) current logical rotation, increasing clockwise to 1. Wacom has 0 on the left-most wheel position. */ - double range = absinfo->maximum - absinfo->minimum + 1; + double range = absinfo_range(absinfo); double value = (absinfo->value - absinfo->minimum) / range - 0.25; if (value < 0.0) diff --git a/src/evdev-tablet.c b/src/evdev-tablet.c index ff33823f..5625e23f 100644 --- a/src/evdev-tablet.c +++ b/src/evdev-tablet.c @@ -23,6 +23,7 @@ */ #include "config.h" #include "evdev-tablet.h" +#include "util-input-event.h" #include #include @@ -334,8 +335,7 @@ tablet_update_tool(struct tablet_dispatch *tablet, static inline double normalize_slider(const struct input_absinfo *absinfo) { - double range = absinfo->maximum - absinfo->minimum; - double value = (absinfo->value - absinfo->minimum) / range; + double value = (absinfo->value - absinfo->minimum) / absinfo_range(absinfo); return value * 2 - 1; } @@ -343,8 +343,7 @@ normalize_slider(const struct input_absinfo *absinfo) static inline double normalize_distance(const struct input_absinfo *absinfo) { - double range = absinfo->maximum - absinfo->minimum; - double value = (absinfo->value - absinfo->minimum) / range; + double value = (absinfo->value - absinfo->minimum) / absinfo_range(absinfo); return value; } @@ -374,8 +373,7 @@ normalize_pressure(const struct input_absinfo *absinfo, static inline double adjust_tilt(const struct input_absinfo *absinfo) { - double range = absinfo->maximum - absinfo->minimum + 1; - double value = (absinfo->value - absinfo->minimum) / range; + double value = (absinfo->value - absinfo->minimum) / absinfo_range(absinfo); const int WACOM_MAX_DEGREES = 64; /* If resolution is nonzero, it's in units/radian. But require @@ -433,8 +431,7 @@ static double convert_to_degrees(const struct input_absinfo *absinfo, double offset) { /* range is [0, 360[, i.e. range + 1 */ - double range = absinfo->maximum - absinfo->minimum + 1; - double value = (absinfo->value - absinfo->minimum) / range; + double value = (absinfo->value - absinfo->minimum) / absinfo_range(absinfo); return fmod(value * 360.0 + offset, 360.0); } @@ -1077,7 +1074,7 @@ tool_set_bits(const struct tablet_dispatch *tablet, static inline int axis_range_percentage(const struct input_absinfo *a, double percent) { - return (a->maximum - a->minimum) * percent/100.0 + a->minimum; + return absinfo_range(a) * percent/100.0 + a->minimum; } static inline void diff --git a/src/evdev.c b/src/evdev.c index 33b3f514..73b394b0 100644 --- a/src/evdev.c +++ b/src/evdev.c @@ -378,8 +378,7 @@ evdev_transform_relative(struct evdev_device *device, static inline double scale_axis(const struct input_absinfo *absinfo, double val, double to_range) { - return (val - absinfo->minimum) * to_range / - (absinfo->maximum - absinfo->minimum + 1); + return (val - absinfo->minimum) * to_range / absinfo_range(absinfo); } double @@ -1673,8 +1672,8 @@ evdev_fix_abs_resolution(struct evdev_device *device, */ if (!evdev_read_attr_res_prop(device, &xres, &yres) && evdev_read_attr_size_prop(device, &widthmm, &heightmm)) { - xres = (absx->maximum - absx->minimum)/widthmm; - yres = (absy->maximum - absy->minimum)/heightmm; + xres = absinfo_range(absx)/widthmm; + yres = absinfo_range(absy)/heightmm; } /* libevdev_set_abs_resolution() changes the absinfo we already @@ -1843,10 +1842,8 @@ evdev_extract_abs_axes(struct evdev_device *device, device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_X); device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_Y); - device->abs.dimensions.x = abs(device->abs.absinfo_x->maximum - - device->abs.absinfo_x->minimum); - device->abs.dimensions.y = abs(device->abs.absinfo_y->maximum - - device->abs.absinfo_y->minimum); + device->abs.dimensions.x = abs((int)absinfo_range(device->abs.absinfo_x)); + device->abs.dimensions.y = abs((int)absinfo_range(device->abs.absinfo_y)); if (evdev_is_fake_mt_device(device) || !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) || @@ -1865,10 +1862,8 @@ evdev_extract_abs_axes(struct evdev_device *device, device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X); device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y); - device->abs.dimensions.x = abs(device->abs.absinfo_x->maximum - - device->abs.absinfo_x->minimum); - device->abs.dimensions.y = abs(device->abs.absinfo_y->maximum - - device->abs.absinfo_y->minimum); + device->abs.dimensions.x = abs((int)absinfo_range(device->abs.absinfo_x)); + device->abs.dimensions.y = abs((int)absinfo_range(device->abs.absinfo_y)); device->is_mt = 1; } @@ -2562,8 +2557,8 @@ evdev_device_calibrate(struct evdev_device *device, return; } - sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1; - sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1; + sx = absinfo_range(device->abs.absinfo_x); + sy = absinfo_range(device->abs.absinfo_y); /* The transformation matrix is in the form: * [ a b c ] diff --git a/src/util-input-event.h b/src/util-input-event.h index 8a81aec5..9d8d6a2a 100644 --- a/src/util-input-event.h +++ b/src/util-input-event.h @@ -66,3 +66,9 @@ input_event_set_time(struct input_event *e, e->input_event_sec = tval.tv_sec; e->input_event_usec = tval.tv_usec; } + +static inline double +absinfo_range(const struct input_absinfo *abs) +{ + return (double)(abs->maximum - abs->minimum + 1); +} diff --git a/test/test-tablet.c b/test/test-tablet.c index 81eae936..5e0711a7 100644 --- a/test/test-tablet.c +++ b/test/test-tablet.c @@ -35,6 +35,7 @@ #include "libinput-util.h" #include "evdev-tablet.h" #include "litest.h" +#include "util-input-event.h" static inline unsigned int pick_stylus_or_btn0(struct litest_device *dev) @@ -1998,11 +1999,11 @@ rotate_event(struct litest_device *dev, int angle_degrees) abs = libevdev_get_abs_info(dev->evdev, ABS_TILT_X); ck_assert_notnull(abs); - tilt_center_x = (abs->maximum - abs->minimum + 1) / 2; + tilt_center_x = absinfo_range(abs) / 2; abs = libevdev_get_abs_info(dev->evdev, ABS_TILT_Y); ck_assert_notnull(abs); - tilt_center_y = (abs->maximum - abs->minimum + 1) / 2; + tilt_center_y = absinfo_range(abs) / 2; x = cos(a) * 20 + tilt_center_x; y = sin(a) * 20 + tilt_center_y; @@ -2098,7 +2099,7 @@ START_TEST(left_handed_artpen_rotation) abs = libevdev_get_abs_info(dev->evdev, ABS_Z); ck_assert_notnull(abs); - scale = (abs->maximum - abs->minimum + 1)/360.0; + scale = absinfo_range(abs)/360.0; litest_event(dev, EV_KEY, BTN_TOOL_BRUSH, 1); litest_event(dev, EV_ABS, ABS_MISC, 0x804); /* Art Pen */ @@ -3343,9 +3344,9 @@ START_TEST(mouse_wheel) for (i = 2; i < 5; i++) { /* send x/y events to make sure we reset the wheel */ abs = libevdev_get_abs_info(dev->evdev, ABS_X); - litest_event(dev, EV_ABS, ABS_X, (abs->maximum - abs->minimum)/i); + litest_event(dev, EV_ABS, ABS_X, absinfo_range(abs)/i); abs = libevdev_get_abs_info(dev->evdev, ABS_Y); - litest_event(dev, EV_ABS, ABS_Y, (abs->maximum - abs->minimum)/i); + litest_event(dev, EV_ABS, ABS_Y, absinfo_range(abs)/i); litest_event(dev, EV_SYN, SYN_REPORT, 0); libinput_dispatch(li); @@ -3436,7 +3437,7 @@ START_TEST(airbrush_slider) litest_drain_events(li); - scale = abs->maximum - abs->minimum; + scale = absinfo_range(abs); for (v = abs->minimum; v < abs->maximum; v += 8) { litest_event(dev, EV_ABS, ABS_WHEEL, v); litest_event(dev, EV_SYN, SYN_REPORT, 0); @@ -3510,7 +3511,7 @@ START_TEST(artpen_rotation) abs = libevdev_get_abs_info(dev->evdev, ABS_Z); ck_assert_notnull(abs); - scale = (abs->maximum - abs->minimum + 1)/360.0; + scale = absinfo_range(abs)/360.0; litest_event(dev, EV_KEY, BTN_TOOL_BRUSH, 1); litest_event(dev, EV_ABS, ABS_MISC, 0x804); /* Art Pen */ diff --git a/test/test-totem.c b/test/test-totem.c index 917c0fc8..efe726cd 100644 --- a/test/test-totem.c +++ b/test/test-totem.c @@ -34,6 +34,7 @@ #include "libinput-util.h" #include "evdev-tablet.h" #include "litest.h" +#include "util-input-event.h" START_TEST(totem_type) { @@ -143,9 +144,9 @@ START_TEST(totem_proximity_in_on_init) const struct input_absinfo *abs; abs = libevdev_get_abs_info(dev->evdev, ABS_MT_POSITION_X); - w = (abs->maximum - abs->minimum + 1)/abs->resolution; + w = absinfo_range(abs)/abs->resolution; abs = libevdev_get_abs_info(dev->evdev, ABS_MT_POSITION_Y); - h = (abs->maximum - abs->minimum + 1)/abs->resolution; + h = absinfo_range(abs)/abs->resolution; litest_tablet_proximity_in(dev, 50, 50, NULL); diff --git a/tools/libinput-debug-tablet.c b/tools/libinput-debug-tablet.c index 05d3b9a1..6666530b 100644 --- a/tools/libinput-debug-tablet.c +++ b/tools/libinput-debug-tablet.c @@ -39,6 +39,7 @@ #include "shared.h" #include "util-macros.h" +#include "util-input-event.h" static volatile sig_atomic_t stop = 0; static struct tools_options options; @@ -149,7 +150,7 @@ normalize(struct libevdev *evdev, int code, int value) if (!abs) return 0.0; - return 1.0 * (value - abs->minimum)/(abs->maximum - abs->minimum + 1); + return 1.0 * (value - abs->minimum)/absinfo_range(abs); } static int