Fix documentation for libinput_log_set_handler
[platform/upstream/libinput.git] / src / libinput.c
index 9155777..5780a92 100644 (file)
@@ -457,7 +457,6 @@ libinput_add_fd(struct libinput *libinput,
        ep.data.ptr = source;
 
        if (epoll_ctl(libinput->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) {
-               close(source->fd);
                free(source);
                return NULL;
        }
@@ -496,6 +495,7 @@ libinput_init(struct libinput *libinput,
        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);
 
@@ -524,15 +524,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);
 
@@ -556,6 +568,8 @@ libinput_destroy(struct libinput *libinput)
        libinput_drop_destroyed_sources(libinput);
        close(libinput->epoll_fd);
        free(libinput);
+
+       return NULL;
 }
 
 LIBINPUT_EXPORT void
@@ -601,10 +615,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
@@ -616,13 +631,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
@@ -657,10 +676,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
@@ -669,13 +689,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
@@ -1027,7 +1051,6 @@ touch_notify_frame(struct libinput_device *device,
                          &touch_event->base);
 }
 
-
 static void
 libinput_post_event(struct libinput *libinput,
                    struct libinput_event *event)
@@ -1137,6 +1160,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);
@@ -1212,3 +1253,251 @@ 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);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_calibration_has_matrix(struct libinput_device *device)
+{
+       return device->config.calibration ?
+               device->config.calibration->has_matrix(device) : 0;
+}
+
+LIBINPUT_EXPORT enum libinput_config_status
+libinput_device_config_calibration_set_matrix(struct libinput_device *device,
+                                             const float matrix[6])
+{
+       if (!libinput_device_config_calibration_has_matrix(device))
+               return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
+
+       return device->config.calibration->set_matrix(device, matrix);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_calibration_get_matrix(struct libinput_device *device,
+                                             float matrix[6])
+{
+       if (!libinput_device_config_calibration_has_matrix(device))
+               return 0;
+
+       return device->config.calibration->get_matrix(device, matrix);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_calibration_get_default_matrix(struct libinput_device *device,
+                                                     float matrix[6])
+{
+       if (!libinput_device_config_calibration_has_matrix(device))
+               return 0;
+
+       return device->config.calibration->get_default_matrix(device, matrix);
+}
+
+LIBINPUT_EXPORT uint32_t
+libinput_device_config_send_events_get_modes(struct libinput_device *device)
+{
+       uint32_t modes = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
+
+       if (device->config.sendevents)
+               modes |= device->config.sendevents->get_modes(device);
+
+       return modes;
+}
+
+LIBINPUT_EXPORT enum libinput_config_status
+libinput_device_config_send_events_set_mode(struct libinput_device *device,
+                                           enum libinput_config_send_events_mode mode)
+{
+       if ((libinput_device_config_send_events_get_modes(device) & mode) == 0)
+               return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
+
+       if (device->config.sendevents)
+               return device->config.sendevents->set_mode(device, mode);
+       else /* mode must be _ENABLED to get here */
+               return LIBINPUT_CONFIG_STATUS_SUCCESS;
+}
+
+LIBINPUT_EXPORT enum libinput_config_send_events_mode
+libinput_device_config_send_events_get_mode(struct libinput_device *device)
+{
+       if (device->config.sendevents)
+               return device->config.sendevents->get_mode(device);
+       else
+               return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
+}
+
+LIBINPUT_EXPORT enum libinput_config_send_events_mode
+libinput_device_config_send_events_get_default_mode(struct libinput_device *device)
+{
+       return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
+}
+
+
+LIBINPUT_EXPORT int
+libinput_device_config_accel_is_available(struct libinput_device *device)
+{
+       return device->config.accel ?
+               device->config.accel->available(device) : 0;
+}
+
+LIBINPUT_EXPORT enum libinput_config_status
+libinput_device_config_accel_set_speed(struct libinput_device *device,
+                                      double speed)
+{
+       if (!libinput_device_config_accel_is_available(device))
+               return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
+
+       if (speed < -1.0 || speed > 1.0)
+               return LIBINPUT_CONFIG_STATUS_INVALID;
+
+       return device->config.accel->set_speed(device, speed);
+}
+
+LIBINPUT_EXPORT double
+libinput_device_config_accel_get_speed(struct libinput_device *device)
+{
+       if (!libinput_device_config_accel_is_available(device))
+               return 0;
+
+       return device->config.accel->get_speed(device);
+}
+
+LIBINPUT_EXPORT double
+libinput_device_config_accel_get_default_speed(struct libinput_device *device)
+{
+       if (!libinput_device_config_accel_is_available(device))
+               return 0;
+
+       return device->config.accel->get_default_speed(device);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_scroll_has_natural_scroll(struct libinput_device *device)
+{
+       if (!device->config.natural_scroll)
+               return 0;
+
+       return device->config.natural_scroll->has(device);
+}
+
+LIBINPUT_EXPORT enum libinput_config_status
+libinput_device_config_scroll_set_natural_scroll_enabled(struct libinput_device *device,
+                                                        int enabled)
+{
+       if (!libinput_device_config_scroll_has_natural_scroll(device))
+               return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
+
+       return device->config.natural_scroll->set_enabled(device, enabled);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_scroll_get_natural_scroll_enabled(struct libinput_device *device)
+{
+       if (!device->config.natural_scroll)
+               return 0;
+
+       return device->config.natural_scroll->get_enabled(device);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_scroll_get_default_natural_scroll_enabled(struct libinput_device *device)
+{
+       if (!device->config.natural_scroll)
+               return 0;
+
+       return device->config.natural_scroll->get_default_enabled(device);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_buttons_has_left_handed(struct libinput_device *device)
+{
+       if (!device->config.left_handed)
+               return 0;
+
+       return device->config.left_handed->has(device);
+}
+
+LIBINPUT_EXPORT enum libinput_config_status
+libinput_device_config_buttons_set_left_handed(struct libinput_device *device,
+                                              int left_handed)
+{
+       if (!libinput_device_config_buttons_has_left_handed(device))
+               return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
+
+       return device->config.left_handed->set(device, left_handed);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_buttons_get_left_handed(struct libinput_device *device)
+{
+       if (!libinput_device_config_buttons_has_left_handed(device))
+               return 0;
+
+       return device->config.left_handed->get(device);
+}
+
+LIBINPUT_EXPORT int
+libinput_device_config_buttons_get_default_left_handed(struct libinput_device *device)
+{
+       if (!libinput_device_config_buttons_has_left_handed(device))
+               return 0;
+
+       return device->config.left_handed->get_default(device);
+}