Support EV_REP values through libevdev_get_event_value
[platform/upstream/libevdev.git] / libevdev / libevdev.c
index e1bb6d0..60bbbfc 100644 (file)
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <stdbool.h>
 
 #include "libevdev.h"
 #include "libevdev-int.h"
 
 #define MAXEVENTS 64
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_SYNC = LIBEVDEV_READ_FLAG_SYNC;
-LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_NORMAL = LIBEVDEV_READ_FLAG_NORMAL;
-LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_FORCE_SYNC = LIBEVDEV_READ_FLAG_FORCE_SYNC;
-LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_BLOCKING = LIBEVDEV_READ_FLAG_BLOCKING;
-
 static int sync_mt_state(struct libevdev *dev, int create_events);
 
 static int
@@ -68,6 +63,7 @@ libevdev_dflt_log_func(enum libevdev_log_priority priority,
                                        prefix = "libevdev debug";
                                        break;
                default:
+                                       prefix = "libevdev INVALID LOG PRIORITY";
                                        break;
        }
        /* default logging format:
@@ -108,19 +104,29 @@ log_msg(enum libevdev_log_priority priority,
        va_end(args);
 }
 
+static void
+libevdev_reset(struct libevdev *dev)
+{
+       memset(dev, 0, sizeof(*dev));
+       dev->fd = -1;
+       dev->initialized = false;
+       dev->num_slots = -1;
+       dev->current_slot = -1;
+       dev->grabbed = LIBEVDEV_UNGRAB;
+       dev->sync_state = SYNC_NONE;
+       libevdev_enable_event_type(dev, EV_SYN);
+}
+
 LIBEVDEV_EXPORT struct libevdev*
 libevdev_new(void)
 {
        struct libevdev *dev;
 
-       dev = calloc(1, sizeof(*dev));
+       dev = malloc(sizeof(*dev));
        if (!dev)
                return NULL;
-       dev->fd = -1;
-       dev->num_slots = -1;
-       dev->current_slot = -1;
-       dev->grabbed = LIBEVDEV_UNGRAB;
-       dev->sync_state = SYNC_NONE;
+
+       libevdev_reset(dev);
 
        return dev;
 }
@@ -156,14 +162,6 @@ libevdev_free(struct libevdev *dev)
        free(dev);
 }
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT void
-libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
-{
-       /* Can't be backwards compatible to this yet, so don't even try */
-       fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
-}
-
 LIBEVDEV_EXPORT void
 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
 {
@@ -188,7 +186,7 @@ libevdev_get_log_priority(void)
 LIBEVDEV_EXPORT int
 libevdev_change_fd(struct libevdev *dev, int fd)
 {
-       if (dev->fd == -1) {
+       if (!dev->initialized) {
                log_bug("device not initialized. call libevdev_set_fd() first\n");
                return -1;
        }
@@ -203,10 +201,13 @@ libevdev_set_fd(struct libevdev* dev, int fd)
        int i;
        char buf[256];
 
-       if (dev->fd != -1) {
+       if (dev->initialized) {
                log_bug("device already initialized.\n");
                return -EBADF;
-       }
+       } else if (fd < 0)
+               return -EBADF;
+
+       libevdev_reset(dev);
 
        rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
        if (rc < 0)
@@ -263,8 +264,12 @@ libevdev_set_fd(struct libevdev* dev, int fd)
        if (rc < 0)
                goto out;
 
+       /* Built on a kernel with props, running against a kernel without property
+          support. This should not be a fatal case, we'll be missing properties but other
+          than that everything is as expected.
+        */
        rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
-       if (rc < 0)
+       if (rc < 0 && errno != EINVAL)
                goto out;
 
        rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
@@ -350,7 +355,10 @@ libevdev_set_fd(struct libevdev* dev, int fd)
         * Same with the valuators, really, but they may not change.
         */
 
+       dev->initialized = true;
 out:
+       if (rc)
+               libevdev_reset(dev);
        return rc ? -errno : 0;
 }
 
@@ -489,11 +497,14 @@ sync_mt_state(struct libevdev *dev, int create_events)
 {
        int rc;
        int i;
+       int ioctl_success = 0;
        struct mt_state {
                int code;
                int val[MAX_SLOTS];
        } mt_state[ABS_MT_CNT];
 
+       memset(&mt_state, 0, sizeof(mt_state));
+
        for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
                int idx;
                if (i == ABS_MT_SLOT)
@@ -505,8 +516,15 @@ sync_mt_state(struct libevdev *dev, int create_events)
                idx = i - ABS_MT_MIN;
                mt_state[idx].code = i;
                rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
-               if (rc < 0)
-                       goto out;
+               if (rc < 0) {
+                       /* if the first ioctl fails with -EINVAL, chances are the kernel
+                          doesn't support the ioctl. Simply continue */
+                       if (errno == -EINVAL && !ioctl_success) {
+                               rc = 0;
+                       } else /* if the second, ... ioctl fails, really fail */
+                               goto out;
+               } else if (ioctl_success == 0)
+                       ioctl_success = 1;
        }
 
        for (i = 0; i < dev->num_slots; i++) {
@@ -730,10 +748,11 @@ libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event
 {
        int rc = LIBEVDEV_READ_STATUS_SUCCESS;
 
-       if (dev->fd < 0) {
+       if (!dev->initialized) {
                log_bug("device not initialized. call libevdev_set_fd() first\n");
                return -EBADF;
-       }
+       } else if (dev->fd < 0)
+               return -EBADF;
 
        if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
                log_bug("invalid flags %#x\n.\n", flags);
@@ -820,10 +839,11 @@ libevdev_has_event_pending(struct libevdev *dev)
        struct pollfd fds = { dev->fd, POLLIN, 0 };
        int rc;
 
-       if (dev->fd < 0) {
+       if (!dev->initialized) {
                log_bug("device not initialized. call libevdev_set_fd() first\n");
                return -EBADF;
-       }
+       } else if (dev->fd < 0)
+               return -EBADF;
 
        if (queue_num_elements(dev) != 0)
                return 1;
@@ -859,9 +879,9 @@ LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *fiel
        dev->field = strdup(field); \
 }
 
-STRING_SETTER(name);
-STRING_SETTER(phys);
-STRING_SETTER(uniq);
+STRING_SETTER(name)
+STRING_SETTER(phys)
+STRING_SETTER(uniq)
 
 
 #define PRODUCT_GETTER(name) \
@@ -870,10 +890,10 @@ LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
        return dev->ids.name; \
 }
 
-PRODUCT_GETTER(product);
-PRODUCT_GETTER(vendor);
-PRODUCT_GETTER(bustype);
-PRODUCT_GETTER(version);
+PRODUCT_GETTER(product)
+PRODUCT_GETTER(vendor)
+PRODUCT_GETTER(bustype)
+PRODUCT_GETTER(version)
 
 #define PRODUCT_SETTER(field) \
 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
@@ -881,10 +901,10 @@ LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
        dev->ids.field = field;\
 }
 
-PRODUCT_SETTER(product);
-PRODUCT_SETTER(vendor);
-PRODUCT_SETTER(bustype);
-PRODUCT_SETTER(version);
+PRODUCT_SETTER(product)
+PRODUCT_SETTER(vendor)
+PRODUCT_SETTER(bustype)
+PRODUCT_SETTER(version)
 
 LIBEVDEV_EXPORT int
 libevdev_get_driver_version(const struct libevdev *dev)
@@ -911,7 +931,7 @@ libevdev_enable_property(struct libevdev *dev, unsigned int prop)
 LIBEVDEV_EXPORT int
 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
 {
-       return (type <= EV_MAX) && bit_is_set(dev->bits, type);
+       return type == EV_SYN ||(type <= EV_MAX && bit_is_set(dev->bits, type));
 }
 
 LIBEVDEV_EXPORT int
@@ -947,6 +967,19 @@ libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned
                case EV_KEY: value = bit_is_set(dev->key_values, code); break;
                case EV_LED: value = bit_is_set(dev->led_values, code); break;
                case EV_SW: value = bit_is_set(dev->sw_values, code); break;
+               case EV_REP:
+                           switch(code) {
+                                   case REP_DELAY:
+                                           libevdev_get_repeat(dev, &value, NULL);
+                                           break;
+                                   case REP_PERIOD:
+                                           libevdev_get_repeat(dev, NULL, &value);
+                                           break;
+                                   default:
+                                           value = 0;
+                                           break;
+                           }
+                           break;
                default:
                        value = 0;
                        break;
@@ -1073,11 +1106,11 @@ LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned
        return absinfo ? absinfo->name : 0; \
 }
 
-ABS_GETTER(maximum);
-ABS_GETTER(minimum);
-ABS_GETTER(fuzz);
-ABS_GETTER(flat);
-ABS_GETTER(resolution);
+ABS_GETTER(maximum)
+ABS_GETTER(minimum)
+ABS_GETTER(fuzz)
+ABS_GETTER(flat)
+ABS_GETTER(resolution)
 
 #define ABS_SETTER(field) \
 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
@@ -1105,12 +1138,18 @@ libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct inpu
 LIBEVDEV_EXPORT int
 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
 {
+       int max;
+
        if (type > EV_MAX)
                return -1;
 
        if (libevdev_has_event_type(dev, type))
                return 0;
 
+       max = libevdev_event_type_get_max(type);
+       if (max == -1)
+               return -1;
+
        set_bit(dev->bits, type);
 
        if (type == EV_REP) {
@@ -1124,9 +1163,15 @@ libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
 LIBEVDEV_EXPORT int
 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
 {
+       int max;
+
        if (type > EV_MAX || type == EV_SYN)
                return -1;
 
+       max = libevdev_event_type_get_max(type);
+       if (max == -1)
+               return -1;
+
        clear_bit(dev->bits, type);
 
        return 0;
@@ -1158,7 +1203,7 @@ libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
 
        max = type_to_mask(dev, type, &mask);
 
-       if (code > max)
+       if (code > max || (int)max == -1)
                return -1;
 
        set_bit(mask, code);
@@ -1180,12 +1225,12 @@ libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned in
        unsigned int max;
        unsigned long *mask = NULL;
 
-       if (type > EV_MAX)
+       if (type > EV_MAX || type == EV_SYN)
                return -1;
 
        max = type_to_mask(dev, type, &mask);
 
-       if (code > max)
+       if (code > max || (int)max == -1)
                return -1;
 
        clear_bit(mask, code);
@@ -1194,20 +1239,15 @@ libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned in
 }
 
 LIBEVDEV_EXPORT int
-libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
-{
-       return libevdev_kernel_set_abs_info(dev, code, abs);
-}
-
-LIBEVDEV_EXPORT int
 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
 {
        int rc;
 
-       if (dev->fd < 0) {
+       if (!dev->initialized) {
                log_bug("device not initialized. call libevdev_set_fd() first\n");
                return -EBADF;
-       }
+       } else if (dev->fd < 0)
+               return -EBADF;
 
        if (code > ABS_MAX)
                return -EINVAL;
@@ -1226,10 +1266,11 @@ libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
 {
        int rc = 0;
 
-       if (dev->fd < 0) {
+       if (!dev->initialized) {
                log_bug("device not initialized. call libevdev_set_fd() first\n");
                return -EBADF;
-       }
+       } else if (dev->fd < 0)
+               return -EBADF;
 
        if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
                log_bug("invalid grab parameter %#x\n", grab);
@@ -1250,22 +1291,12 @@ libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
        return rc < 0 ? -errno : 0;
 }
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT int
-libevdev_is_event_type(const struct input_event *ev, unsigned int type)
-ALIAS(libevdev_event_is_type);
-
 LIBEVDEV_EXPORT int
 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
 {
        return type < EV_CNT && ev->type == type;
 }
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT int
-libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
-ALIAS(libevdev_event_is_code);
-
 LIBEVDEV_EXPORT int
 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
 {
@@ -1278,11 +1309,6 @@ libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned
        return (max > -1 && code <= (unsigned int)max && ev->code == code);
 }
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT const char*
-libevdev_get_event_type_name(unsigned int type)
-ALIAS(libevdev_event_type_get_name);
-
 LIBEVDEV_EXPORT const char*
 libevdev_event_type_get_name(unsigned int type)
 {
@@ -1292,11 +1318,6 @@ libevdev_event_type_get_name(unsigned int type)
        return ev_map[type];
 }
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT const char*
-libevdev_get_event_code_name(unsigned int type, unsigned int code)
-ALIAS(libevdev_event_code_get_name);
-
 LIBEVDEV_EXPORT const char*
 libevdev_event_code_get_name(unsigned int type, unsigned int code)
 {
@@ -1308,16 +1329,6 @@ libevdev_event_code_get_name(unsigned int type, unsigned int code)
        return event_type_map[type][code];
 }
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT const char*
-libevdev_get_input_prop_name(unsigned int prop)
-ALIAS(libevdev_property_get_name);
-
-/* DEPRECATED */
-LIBEVDEV_EXPORT const char*
-libevdev_get_property_name(unsigned int prop)
-ALIAS(libevdev_property_get_name);
-
 LIBEVDEV_EXPORT const char*
 libevdev_property_get_name(unsigned int prop)
 {
@@ -1327,11 +1338,6 @@ libevdev_property_get_name(unsigned int prop)
        return input_prop_map[prop];
 }
 
-/* DEPRECATED */
-LIBEVDEV_EXPORT int
-libevdev_get_event_type_max(unsigned int type)
-ALIAS(libevdev_event_type_get_max);
-
 LIBEVDEV_EXPORT int
 libevdev_event_type_get_max(unsigned int type)
 {
@@ -1342,7 +1348,7 @@ libevdev_event_type_get_max(unsigned int type)
 }
 
 LIBEVDEV_EXPORT int
-libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
+libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
 {
        if (!libevdev_has_event_type(dev, EV_REP))
                return -1;
@@ -1371,10 +1377,11 @@ libevdev_kernel_set_led_values(struct libevdev *dev, ...)
        int rc = 0;
        size_t nleds = 0;
 
-       if (dev->fd < 0) {
+       if (!dev->initialized) {
                log_bug("device not initialized. call libevdev_set_fd() first\n");
                return -EBADF;
-       }
+       } else if (dev->fd < 0)
+               return -EBADF;
 
        memset(ev, 0, sizeof(ev));
 
@@ -1426,10 +1433,11 @@ libevdev_kernel_set_led_values(struct libevdev *dev, ...)
 LIBEVDEV_EXPORT int
 libevdev_set_clock_id(struct libevdev *dev, int clockid)
 {
-       if (dev->fd < 0) {
+       if (!dev->initialized) {
                log_bug("device not initialized. call libevdev_set_fd() first\n");
                return -EBADF;
-       }
+       } else if (dev->fd < 0)
+               return -EBADF;
 
        return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;
 }