evdev: Dynamically allocate slot array
authorJonas Ådahl <jadahl@gmail.com>
Tue, 22 Apr 2014 21:02:14 +0000 (23:02 +0200)
committerJonas Ådahl <jadahl@gmail.com>
Tue, 22 Apr 2014 21:44:24 +0000 (23:44 +0200)
Don't have a hard coded slot array size; instead allocate the array
needed according to the abs info reported by either libmtdev or libevdev.

Signed-off-by: Jonas Ådahl <jadahl@gmail.com>
src/evdev.c
src/evdev.h

index 34c6798f3e06ee9c1f57d4ffd4a8196e1b6f9ec5..8d11e7fcd67e6f2867f17b687fe6dcbe7bad860c 100644 (file)
@@ -547,6 +547,10 @@ evdev_configure_device(struct evdev_device *device)
        const struct input_absinfo *absinfo;
        int has_abs, has_rel, has_mt;
        int has_button, has_keyboard, has_touch;
+       struct mt_slot *slots;
+       int num_slots;
+       int active_slot;
+       int slot;
        unsigned int i;
 
        has_rel = 0;
@@ -588,10 +592,29 @@ evdev_configure_device(struct evdev_device *device)
                                device->mtdev = mtdev_new_open(device->fd);
                                if (!device->mtdev)
                                        return -1;
-                               device->mt.slot = device->mtdev->caps.slot.value;
+
+                               num_slots = device->mtdev->caps.slot.maximum;
+                               if (device->mtdev->caps.slot.minimum < 0 ||
+                                   num_slots <= 0)
+                                       return -1;
+                               active_slot = device->mtdev->caps.slot.value;
                        } else {
-                               device->mt.slot = libevdev_get_current_slot(device->evdev);
+                               num_slots = libevdev_get_num_slots(device->evdev);
+                               active_slot = libevdev_get_current_slot(evdev);
+                       }
+
+                       slots = calloc(num_slots, sizeof(struct mt_slot));
+                       if (!slots)
+                               return -1;
+
+                       for (slot = 0; slot < num_slots; ++slot) {
+                               slots[slot].seat_slot = -1;
+                               slots[slot].x = 0;
+                               slots[slot].y = 0;
                        }
+                       device->mt.slots = slots;
+                       device->mt.slots_len = num_slots;
+                       device->mt.slot = active_slot;
                }
        }
        if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
@@ -686,7 +709,6 @@ evdev_device_create(struct libinput_seat *seat,
        device->mtdev = NULL;
        device->devnode = strdup(devnode);
        device->sysname = strdup(sysname);
-       device->mt.slot = -1;
        device->rel.dx = 0;
        device->rel.dy = 0;
        device->dispatch = NULL;
@@ -802,6 +824,7 @@ evdev_device_destroy(struct evdev_device *device)
 
        libinput_seat_unref(device->base.seat);
        libevdev_free(device->evdev);
+       free(device->mt.slots);
        free(device->devnode);
        free(device->sysname);
        free(device);
index 0ab95720fe79e58be703a6457ab1f2b706a7e3c7..36daf3c792117aaeda8e70a33b3847fa4d9c4cd0 100644 (file)
@@ -31,8 +31,6 @@
 
 #include "libinput-private.h"
 
-#define MAX_SLOTS 16
-
 enum evdev_event_type {
        EVDEV_NONE,
        EVDEV_ABSOLUTE_TOUCH_DOWN,
@@ -50,6 +48,11 @@ enum evdev_device_seat_capability {
        EVDEV_DEVICE_TOUCH = (1 << 2)
 };
 
+struct mt_slot {
+       int32_t seat_slot;
+       int32_t x, y;
+};
+
 struct evdev_device {
        struct libinput_device base;
 
@@ -74,10 +77,8 @@ struct evdev_device {
 
        struct {
                int slot;
-               struct {
-                       int32_t seat_slot;
-                       int32_t x, y;
-               } slots[MAX_SLOTS];
+               struct mt_slot *slots;
+               size_t slots_len;
        } mt;
        struct mtdev *mtdev;