Swap conditions for ARRAY_FOR_EACH()
[platform/upstream/libinput.git] / src / evdev.c
index 31ca620..b09bb98 100644 (file)
@@ -47,6 +47,76 @@ enum evdev_key_type {
        EVDEV_KEY_TYPE_BUTTON,
 };
 
+static void
+set_key_down(struct evdev_device *device, int code, int pressed)
+{
+       long_set_bit_state(device->key_mask, code, pressed);
+}
+
+static int
+is_key_down(struct evdev_device *device, int code)
+{
+       return long_bit_is_set(device->key_mask, code);
+}
+
+static int
+get_key_down_count(struct evdev_device *device, int code)
+{
+       return device->key_count[code];
+}
+
+static int
+update_key_down_count(struct evdev_device *device, int code, int pressed)
+{
+       int key_count;
+       assert(code >= 0 && code < KEY_CNT);
+
+       if (pressed) {
+               key_count = ++device->key_count[code];
+       } else {
+               assert(device->key_count[code] > 0);
+               key_count = --device->key_count[code];
+       }
+
+       if (key_count > 32) {
+               log_bug_libinput(device->base.seat->libinput,
+                                "Key count for %s reached abnormal values\n",
+                                libevdev_event_code_get_name(EV_KEY, code));
+       }
+
+       return key_count;
+}
+
+void
+evdev_keyboard_notify_key(struct evdev_device *device,
+                         uint32_t time,
+                         int key,
+                         enum libinput_key_state state)
+{
+       int down_count;
+
+       down_count = update_key_down_count(device, key, state);
+
+       if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
+           (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
+               keyboard_notify_key(&device->base, time, key, state);
+}
+
+void
+evdev_pointer_notify_button(struct evdev_device *device,
+                           uint32_t time,
+                           int button,
+                           enum libinput_button_state state)
+{
+       int down_count;
+
+       down_count = update_key_down_count(device, button, state);
+
+       if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
+           (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0))
+               pointer_notify_button(&device->base, time, button, state);
+}
+
 void
 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
 {
@@ -294,6 +364,8 @@ static inline void
 evdev_process_key(struct evdev_device *device,
                  struct input_event *e, uint64_t time)
 {
+       enum evdev_key_type type;
+
        /* ignore kernel key repeat */
        if (e->value == 2)
                return;
@@ -306,20 +378,37 @@ evdev_process_key(struct evdev_device *device,
 
        evdev_flush_pending_event(device, time);
 
-       switch (get_key_type(e->code)) {
+       type = get_key_type(e->code);
+
+       /* Ignore key release events from the kernel for keys that libinput
+        * never got a pressed event for. */
+       if (e->value == 0) {
+               switch (type) {
+               case EVDEV_KEY_TYPE_NONE:
+                       break;
+               case EVDEV_KEY_TYPE_KEY:
+               case EVDEV_KEY_TYPE_BUTTON:
+                       if (!is_key_down(device, e->code))
+                               return;
+               }
+       }
+
+       set_key_down(device, e->code, e->value);
+
+       switch (type) {
        case EVDEV_KEY_TYPE_NONE:
                break;
        case EVDEV_KEY_TYPE_KEY:
-               keyboard_notify_key(
-                       &device->base,
+               evdev_keyboard_notify_key(
+                       device,
                        time,
                        e->code,
                        e->value ? LIBINPUT_KEY_STATE_PRESSED :
                                   LIBINPUT_KEY_STATE_RELEASED);
                break;
        case EVDEV_KEY_TYPE_BUTTON:
-               pointer_notify_button(
-                       &device->base,
+               evdev_pointer_notify_button(
+                       device,
                        time,
                        e->code,
                        e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
@@ -796,10 +885,11 @@ evdev_device_create(struct libinput_seat *seat,
                return NULL;
 
        libinput_device_init(&device->base, seat);
+       libinput_seat_ref(seat);
 
        rc = libevdev_new_from_fd(fd, &device->evdev);
        if (rc != 0)
-               return NULL;
+               goto err;
 
        libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
 
@@ -816,8 +906,6 @@ evdev_device_create(struct libinput_seat *seat,
        device->pending_event = EVDEV_NONE;
        device->devname = libevdev_get_name(device->evdev);
 
-       libinput_seat_ref(seat);
-
        if (evdev_configure_device(device) == -1)
                goto err;
 
@@ -853,12 +941,8 @@ err:
 int
 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
 {
-       int len;
-
        memset(keys, 0, size);
-       len = ioctl(device->fd, EVIOCGKEY(size), keys);
-
-       return (len == -1) ? -errno : len;
+       return 0;
 }
 
 const char *
@@ -934,6 +1018,45 @@ evdev_device_get_size(struct evdev_device *device,
        return 0;
 }
 
+static void
+release_pressed_keys(struct evdev_device *device)
+{
+       struct libinput *libinput = device->base.seat->libinput;
+       struct timespec ts;
+       uint64_t time;
+       int code;
+
+       if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
+               log_bug_libinput(libinput, "clock_gettime: %s\n", strerror(errno));
+               return;
+       }
+
+       time = ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000;
+
+       for (code = 0; code < KEY_CNT; code++) {
+               if (get_key_down_count(device, code) > 0) {
+                       switch (get_key_type(code)) {
+                       case EVDEV_KEY_TYPE_NONE:
+                               break;
+                       case EVDEV_KEY_TYPE_KEY:
+                               keyboard_notify_key(
+                                       &device->base,
+                                       time,
+                                       code,
+                                       LIBINPUT_KEY_STATE_RELEASED);
+                               break;
+                       case EVDEV_KEY_TYPE_BUTTON:
+                               pointer_notify_button(
+                                       &device->base,
+                                       time,
+                                       code,
+                                       LIBINPUT_BUTTON_STATE_RELEASED);
+                               break;
+                       }
+               }
+       }
+}
+
 void
 evdev_device_remove(struct evdev_device *device)
 {
@@ -941,6 +1064,8 @@ evdev_device_remove(struct evdev_device *device)
                libinput_remove_source(device->base.seat->libinput,
                                       device->source);
 
+       release_pressed_keys(device);
+
        if (device->mtdev)
                mtdev_close_delete(device->mtdev);
        close_restricted(device->base.seat->libinput, device->fd);