evdev: Use helper for separating buttons from keys
[platform/upstream/libinput.git] / src / libinput.c
index f384f43..90b6a13 100644 (file)
@@ -56,7 +56,7 @@ struct libinput_event_keyboard {
        uint32_t time;
        uint32_t key;
        uint32_t seat_key_count;
-       enum libinput_keyboard_key_state state;
+       enum libinput_key_state state;
 };
 
 struct libinput_event_pointer {
@@ -81,8 +81,8 @@ struct libinput_event_touch {
 };
 
 static void
-libinput_default_log_func(enum libinput_log_priority priority,
-                         void *data,
+libinput_default_log_func(struct libinput *libinput,
+                         enum libinput_log_priority priority,
                          const char *format, va_list args)
 {
        const char *prefix;
@@ -98,55 +98,47 @@ libinput_default_log_func(enum libinput_log_priority priority,
        vfprintf(stderr, format, args);
 }
 
-struct log_data {
-       enum libinput_log_priority priority;
-       libinput_log_handler handler;
-       void *user_data;
-};
-
-static struct log_data log_data = {
-       .priority = LIBINPUT_LOG_PRIORITY_ERROR,
-       .handler = libinput_default_log_func,
-       .user_data = NULL,
-};
-
 void
-log_msg_va(enum libinput_log_priority priority,
+log_msg_va(struct libinput *libinput,
+          enum libinput_log_priority priority,
           const char *format,
           va_list args)
 {
-       if (log_data.handler && log_data.priority <= priority)
-               log_data.handler(priority, log_data.user_data, format, args);
+       if (libinput->log_handler &&
+           libinput->log_priority <= priority)
+               libinput->log_handler(libinput, priority, format, args);
 }
 
 void
-log_msg(enum libinput_log_priority priority, const char *format, ...)
+log_msg(struct libinput *libinput,
+       enum libinput_log_priority priority,
+       const char *format, ...)
 {
        va_list args;
 
        va_start(args, format);
-       log_msg_va(priority, format, args);
+       log_msg_va(libinput, priority, format, args);
        va_end(args);
 }
 
 LIBINPUT_EXPORT void
-libinput_log_set_priority(enum libinput_log_priority priority)
+libinput_log_set_priority(struct libinput *libinput,
+                         enum libinput_log_priority priority)
 {
-       log_data.priority = priority;
+       libinput->log_priority = priority;
 }
 
 LIBINPUT_EXPORT enum libinput_log_priority
-libinput_log_get_priority(void)
+libinput_log_get_priority(const struct libinput *libinput)
 {
-       return log_data.priority;
+       return libinput->log_priority;
 }
 
 LIBINPUT_EXPORT void
-libinput_log_set_handler(libinput_log_handler log_handler,
-                        void *user_data)
+libinput_log_set_handler(struct libinput *libinput,
+                        libinput_log_handler log_handler)
 {
-       log_data.handler = log_handler;
-       log_data.user_data = user_data;
+       libinput->log_handler = log_handler;
 }
 
 static void
@@ -285,7 +277,7 @@ libinput_event_keyboard_get_key(struct libinput_event_keyboard *event)
        return event->key;
 }
 
-LIBINPUT_EXPORT enum libinput_keyboard_key_state
+LIBINPUT_EXPORT enum libinput_key_state
 libinput_event_keyboard_get_key_state(struct libinput_event_keyboard *event)
 {
        return event->state;
@@ -499,9 +491,12 @@ libinput_init(struct libinput *libinput,
                return -1;
        }
 
+       libinput->log_handler = libinput_default_log_func;
+       libinput->log_priority = LIBINPUT_LOG_PRIORITY_ERROR;
        libinput->interface = interface;
        libinput->interface_backend = interface_backend;
        libinput->user_data = user_data;
+       libinput->refcount = 1;
        list_init(&libinput->source_destroy_list);
        list_init(&libinput->seat_list);
 
@@ -530,15 +525,27 @@ libinput_drop_destroyed_sources(struct libinput *libinput)
        list_init(&libinput->source_destroy_list);
 }
 
-LIBINPUT_EXPORT void
-libinput_destroy(struct libinput *libinput)
+LIBINPUT_EXPORT struct libinput *
+libinput_ref(struct libinput *libinput)
+{
+       libinput->refcount++;
+       return libinput;
+}
+
+LIBINPUT_EXPORT struct libinput *
+libinput_unref(struct libinput *libinput)
 {
        struct libinput_event *event;
        struct libinput_device *device, *next_device;
        struct libinput_seat *seat, *next_seat;
 
        if (libinput == NULL)
-               return;
+               return NULL;
+
+       assert(libinput->refcount > 0);
+       libinput->refcount--;
+       if (libinput->refcount > 0)
+               return libinput;
 
        libinput_suspend(libinput);
 
@@ -562,6 +569,8 @@ libinput_destroy(struct libinput *libinput)
        libinput_drop_destroyed_sources(libinput);
        close(libinput->epoll_fd);
        free(libinput);
+
+       return NULL;
 }
 
 LIBINPUT_EXPORT void
@@ -607,10 +616,11 @@ libinput_seat_init(struct libinput_seat *seat,
        list_insert(&libinput->seat_list, &seat->link);
 }
 
-LIBINPUT_EXPORT void
+LIBINPUT_EXPORT struct libinput_seat *
 libinput_seat_ref(struct libinput_seat *seat)
 {
        seat->refcount++;
+       return seat;
 }
 
 static void
@@ -622,13 +632,17 @@ libinput_seat_destroy(struct libinput_seat *seat)
        seat->destroy(seat);
 }
 
-LIBINPUT_EXPORT void
+LIBINPUT_EXPORT struct libinput_seat *
 libinput_seat_unref(struct libinput_seat *seat)
 {
        assert(seat->refcount > 0);
        seat->refcount--;
-       if (seat->refcount == 0)
+       if (seat->refcount == 0) {
                libinput_seat_destroy(seat);
+               return NULL;
+       } else {
+               return seat;
+       }
 }
 
 LIBINPUT_EXPORT void
@@ -663,10 +677,11 @@ libinput_device_init(struct libinput_device *device,
        device->refcount = 1;
 }
 
-LIBINPUT_EXPORT void
+LIBINPUT_EXPORT struct libinput_device *
 libinput_device_ref(struct libinput_device *device)
 {
        device->refcount++;
+       return device;
 }
 
 static void
@@ -675,13 +690,17 @@ libinput_device_destroy(struct libinput_device *device)
        evdev_device_destroy((struct evdev_device *) device);
 }
 
-LIBINPUT_EXPORT void
+LIBINPUT_EXPORT struct libinput_device *
 libinput_device_unref(struct libinput_device *device)
 {
        assert(device->refcount > 0);
        device->refcount--;
-       if (device->refcount == 0)
+       if (device->refcount == 0) {
                libinput_device_destroy(device);
+               return NULL;
+       } else {
+               return device;
+       }
 }
 
 LIBINPUT_EXPORT int
@@ -717,14 +736,14 @@ libinput_dispatch(struct libinput *libinput)
 static uint32_t
 update_seat_key_count(struct libinput_seat *seat,
                      int32_t key,
-                     enum libinput_keyboard_key_state state)
+                     enum libinput_key_state state)
 {
        assert(key >= 0 && key <= KEY_MAX);
 
        switch (state) {
-       case LIBINPUT_KEYBOARD_KEY_STATE_PRESSED:
+       case LIBINPUT_KEY_STATE_PRESSED:
                return ++seat->button_count[key];
-       case LIBINPUT_KEYBOARD_KEY_STATE_RELEASED:
+       case LIBINPUT_KEY_STATE_RELEASED:
                /* We might not have received the first PRESSED event. */
                if (seat->button_count[key] == 0)
                        return 0;
@@ -816,7 +835,7 @@ void
 keyboard_notify_key(struct libinput_device *device,
                    uint32_t time,
                    uint32_t key,
-                   enum libinput_keyboard_key_state state)
+                   enum libinput_key_state state)
 {
        struct libinput_event_keyboard *key_event;
        uint32_t seat_key_count;
@@ -1033,7 +1052,6 @@ touch_notify_frame(struct libinput_device *device,
                          &touch_event->base);
 }
 
-
 static void
 libinput_post_event(struct libinput *libinput,
                    struct libinput_event *event)
@@ -1143,6 +1161,24 @@ libinput_device_get_sysname(struct libinput_device *device)
 }
 
 LIBINPUT_EXPORT const char *
+libinput_device_get_name(struct libinput_device *device)
+{
+       return evdev_device_get_name((struct evdev_device *) device);
+}
+
+LIBINPUT_EXPORT unsigned int
+libinput_device_get_id_product(struct libinput_device *device)
+{
+       return evdev_device_get_id_product((struct evdev_device *) device);
+}
+
+LIBINPUT_EXPORT unsigned int
+libinput_device_get_id_vendor(struct libinput_device *device)
+{
+       return evdev_device_get_id_vendor((struct evdev_device *) device);
+}
+
+LIBINPUT_EXPORT const char *
 libinput_device_get_output_name(struct libinput_device *device)
 {
        return evdev_device_get_output((struct evdev_device *) device);
@@ -1185,6 +1221,16 @@ libinput_device_has_capability(struct libinput_device *device,
                                           capability);
 }
 
+LIBINPUT_EXPORT int
+libinput_device_get_size(struct libinput_device *device,
+                        double *width,
+                        double *height)
+{
+       return evdev_device_get_size((struct evdev_device *)device,
+                                    width,
+                                    height);
+}
+
 LIBINPUT_EXPORT struct libinput_event *
 libinput_event_device_notify_get_base_event(struct libinput_event_device_notify *event)
 {
@@ -1208,3 +1254,62 @@ libinput_event_touch_get_base_event(struct libinput_event_touch *event)
 {
        return &event->base;
 }
+
+LIBINPUT_EXPORT const char *
+libinput_config_status_to_str(enum libinput_config_status status)
+{
+       const char *str = NULL;
+
+       switch(status) {
+       case LIBINPUT_CONFIG_STATUS_SUCCESS:
+               str = "Success";
+               break;
+       case LIBINPUT_CONFIG_STATUS_UNSUPPORTED:
+               str = "Unsupported configuration option";
+               break;
+       case LIBINPUT_CONFIG_STATUS_INVALID:
+               str = "Invalid argument range";
+               break;
+       }
+
+       return str;
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_tap_get_finger_count(struct libinput_device *device)
+{
+       return device->config.tap ? device->config.tap->count(device) : 0;
+}
+
+LIBINPUT_EXPORT enum libinput_config_status
+libinput_device_config_tap_set_enabled(struct libinput_device *device,
+                                      enum libinput_config_tap_state enable)
+{
+       if (enable != LIBINPUT_CONFIG_TAP_ENABLED &&
+           enable != LIBINPUT_CONFIG_TAP_DISABLED)
+               return LIBINPUT_CONFIG_STATUS_INVALID;
+
+       if (enable &&
+           libinput_device_config_tap_get_finger_count(device) == 0)
+               return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
+
+       return device->config.tap->set_enabled(device, enable);
+}
+
+LIBINPUT_EXPORT enum libinput_config_tap_state
+libinput_device_config_tap_get_enabled(struct libinput_device *device)
+{
+       if (libinput_device_config_tap_get_finger_count(device) == 0)
+               return LIBINPUT_CONFIG_TAP_DISABLED;
+
+       return device->config.tap->get_enabled(device);
+}
+
+LIBINPUT_EXPORT enum libinput_config_tap_state
+libinput_device_config_tap_get_default_enabled(struct libinput_device *device)
+{
+       if (libinput_device_config_tap_get_finger_count(device) == 0)
+               return LIBINPUT_CONFIG_TAP_DISABLED;
+
+       return device->config.tap->get_default(device);
+}