Store the abs value after handling mt events
authorPeter Hutterer <peter.hutterer@who-t.net>
Thu, 15 Aug 2013 01:10:17 +0000 (11:10 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Sun, 25 Aug 2013 23:52:33 +0000 (09:52 +1000)
This way any ABS_MT_ event value that comes in will also be stored in abs_info.
That always corresponds to "current slot", so if a user calls
libevdev_set_event_value() or libevdev_get_event_value() they're actually
modifying the current slot value.

When the current slot changes, sync the state back into the absinfo values.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
libevdev/libevdev.c
libevdev/libevdev.h
test/test-libevdev-events.c

index ba5b17fab680891945954a14762d28b3ee3914f1..e59d795812ddcca042004442062d407bf2376c72 100644 (file)
@@ -507,7 +507,14 @@ static int
 update_mt_state(struct libevdev *dev, const struct input_event *e)
 {
        if (e->code == ABS_MT_SLOT) {
+               int i;
                dev->current_slot = e->value;
+               /* sync abs_info with the current slot values */
+               for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
+                       if (libevdev_has_event_code(dev, EV_ABS, i))
+                               dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
+               }
+
                return 0;
        } else if (dev->current_slot == -1)
                return 1;
@@ -527,7 +534,7 @@ update_abs_state(struct libevdev *dev, const struct input_event *e)
                return 1;
 
        if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
-               return update_mt_state(dev, e);
+               update_mt_state(dev, e);
 
        dev->abs_info[e->code].value = e->value;
 
index db6a2ec3d22aa3021a3a3d291a4de8116ef69917..61553cc1a91d4b67645eda80cb7f8bc09ddd5360 100644 (file)
@@ -799,6 +799,10 @@ const struct input_absinfo* libevdev_get_abs_info(const struct libevdev *dev, un
  * Behaviour of this function is undefined if the device does not provide
  * the event.
  *
+ * If the device supports ABS_MT_SLOT, the value returned for any ABS_MT_*
+ * event code is the value of the currently active slot. You should use
+ * libevdev_get_slot_value() instead.
+ *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
  * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
  * @param code The event code to query for, one of ABS_X, REL_X, etc.
@@ -823,6 +827,10 @@ int libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsi
  * this device. A future call to libevdev_get_event_value() will return this
  * value, unless the value was overwritten by an event.
  *
+ * If the device supports ABS_MT_SLOT, the value set for any ABS_MT_*
+ * event code is the value of the currently active slot. You should use
+ * libevdev_set_slot_value() instead.
+ *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
  * @param type The event type for the code to query (EV_SYN, EV_REL, etc.)
  * @param code The event code to set the value for, one of ABS_X, LED_NUML, etc.
index afa426dabe7775dbe34117b1509f9248737663bf..669de1266fd74ab7b07e4b9eba34127f20e75224 100644 (file)
@@ -1033,6 +1033,59 @@ START_TEST(test_event_mt_value_setters_invalid)
 }
 END_TEST
 
+START_TEST(test_event_mt_value_setters_current_slot)
+{
+       struct uinput_device* uidev;
+       struct libevdev *dev;
+       int rc;
+       struct input_absinfo abs[5];
+
+       memset(abs, 0, sizeof(abs));
+       abs[0].value = ABS_X;
+       abs[0].maximum = 1000;
+       abs[1].value = ABS_MT_POSITION_X;
+       abs[1].maximum = 1000;
+
+       abs[2].value = ABS_Y;
+       abs[2].maximum = 1000;
+       abs[3].value = ABS_MT_POSITION_Y;
+       abs[3].maximum = 1000;
+
+       abs[4].value = ABS_MT_SLOT;
+       abs[4].maximum = 2;
+
+       rc = test_create_abs_device(&uidev, &dev,
+                                   5, abs,
+                                   EV_SYN, SYN_REPORT,
+                                   -1);
+       ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
+
+       /* set_event_value/get_event_value works on the current slot */
+
+       ck_assert_int_eq(libevdev_get_current_slot(dev), 0);
+       ck_assert_int_eq(libevdev_set_event_value(dev, EV_ABS, ABS_MT_POSITION_X, 1), 0);
+       ck_assert_int_eq(libevdev_get_event_value(dev, EV_ABS, ABS_MT_POSITION_X), 1);
+       ck_assert_int_eq(libevdev_get_slot_value(dev, 0, ABS_MT_POSITION_X), 1);
+
+       ck_assert_int_eq(libevdev_set_event_value(dev, EV_ABS, ABS_MT_SLOT, 1), 0);
+       ck_assert_int_eq(libevdev_get_current_slot(dev), 1);
+       ck_assert_int_eq(libevdev_set_event_value(dev, EV_ABS, ABS_MT_POSITION_X, 2), 0);
+       ck_assert_int_eq(libevdev_get_event_value(dev, EV_ABS, ABS_MT_POSITION_X), 2);
+       ck_assert_int_eq(libevdev_get_slot_value(dev, 1, ABS_MT_POSITION_X), 2);
+
+       /* set slot 0, but current is still slot 1 */
+       ck_assert_int_eq(libevdev_set_slot_value(dev, 0, ABS_MT_POSITION_X, 3), 0);
+       ck_assert_int_eq(libevdev_get_event_value(dev, EV_ABS, ABS_MT_POSITION_X), 2);
+
+       ck_assert_int_eq(libevdev_set_event_value(dev, EV_ABS, ABS_MT_SLOT, 0), 0);
+       ck_assert_int_eq(libevdev_get_current_slot(dev), 0);
+       ck_assert_int_eq(libevdev_get_event_value(dev, EV_ABS, ABS_MT_POSITION_X), 3);
+
+       uinput_device_free(uidev);
+       libevdev_free(dev);
+}
+END_TEST
+
 Suite *
 libevdev_events(void)
 {
@@ -1071,6 +1124,7 @@ libevdev_events(void)
        tcase_add_test(tc, test_event_value_setters_invalid);
        tcase_add_test(tc, test_event_mt_value_setters);
        tcase_add_test(tc, test_event_mt_value_setters_invalid);
+       tcase_add_test(tc, test_event_mt_value_setters_current_slot);
        suite_add_tcase(s, tc);
 
        return s;