Swap conditions for ARRAY_FOR_EACH()
[platform/upstream/libinput.git] / src / udev-seat.c
index 9b807be..8d19894 100644 (file)
@@ -80,10 +80,10 @@ device_added(struct udev_device *udev_device, struct udev_input *input)
        libinput_seat_unref(&seat->base);
 
        if (device == EVDEV_UNHANDLED_DEVICE) {
-               log_info("not using input device '%s'.\n", devnode);
+               log_info(&input->base, "not using input device '%s'.\n", devnode);
                return 0;
        } else if (device == NULL) {
-               log_info("failed to create input device '%s'.\n", devnode);
+               log_info(&input->base, "failed to create input device '%s'.\n", devnode);
                return 0;
        }
 
@@ -100,7 +100,8 @@ device_added(struct udev_device *udev_device, struct udev_input *input)
                                         &device->abs.calibration[4],
                                         &device->abs.calibration[5]) == 6) {
                device->abs.apply_calibration = 1;
-               log_info("Applying calibration: %f %f %f %f %f %f\n",
+               log_info(&input->base,
+                        "Applying calibration: %f %f %f %f %f %f\n",
                         device->abs.calibration[0],
                         device->abs.calibration[1],
                         device->abs.calibration[2],
@@ -116,6 +117,28 @@ device_added(struct udev_device *udev_device, struct udev_input *input)
        return 0;
 }
 
+static void
+device_removed(struct udev_device *udev_device, struct udev_input *input)
+{
+       const char *devnode;
+       struct evdev_device *device, *next;
+       struct udev_seat *seat;
+
+       devnode = udev_device_get_devnode(udev_device);
+       list_for_each(seat, &input->base.seat_list, base.link) {
+               list_for_each_safe(device, next,
+                                  &seat->base.devices_list, base.link) {
+                       if (!strcmp(device->devnode, devnode)) {
+                               log_info(&input->base,
+                                        "input device %s, %s removed\n",
+                                        device->devname, device->devnode);
+                               evdev_device_remove(device);
+                               break;
+                       }
+               }
+       }
+}
+
 static int
 udev_input_add_devices(struct udev_input *input, struct udev *udev)
 {
@@ -155,10 +178,7 @@ evdev_udev_handler(void *data)
 {
        struct udev_input *input = data;
        struct udev_device *udev_device;
-       struct evdev_device *device, *next;
        const char *action;
-       const char *devnode;
-       struct udev_seat *seat;
 
        udev_device = udev_monitor_receive_device(input->udev_monitor);
        if (!udev_device)
@@ -171,22 +191,10 @@ evdev_udev_handler(void *data)
        if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
                goto out;
 
-       if (!strcmp(action, "add")) {
+       if (!strcmp(action, "add"))
                device_added(udev_device, input);
-       }
-       else if (!strcmp(action, "remove")) {
-               devnode = udev_device_get_devnode(udev_device);
-               list_for_each(seat, &input->base.seat_list, base.link) {
-                       list_for_each_safe(device, next,
-                                          &seat->base.devices_list, base.link)
-                               if (!strcmp(device->devnode, devnode)) {
-                                       log_info("input device %s, %s removed\n",
-                                                device->devname, device->devnode);
-                                       evdev_device_remove(device);
-                                       break;
-                               }
-               }
-       }
+       else if (!strcmp(action, "remove"))
+               device_removed(udev_device, input);
 
 out:
        udev_device_unref(udev_device);
@@ -203,20 +211,11 @@ udev_input_remove_devices(struct udev_input *input)
                list_for_each_safe(device, next,
                                   &seat->base.devices_list, base.link) {
                        evdev_device_remove(device);
-                       if (list_empty(&seat->base.devices_list)) {
-                               /* if the seat may be referenced by the
-                                  client, so make sure it's dropped from
-                                  the seat list now, to be freed whenever
-                                * the device is removed */
-                               list_remove(&seat->base.link);
-                               list_init(&seat->base.link);
-                       }
                }
                libinput_seat_unref(&seat->base);
        }
 }
 
-
 static void
 udev_input_disable(struct libinput *libinput)
 {
@@ -245,7 +244,8 @@ udev_input_enable(struct libinput *libinput)
 
        input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
        if (!input->udev_monitor) {
-               log_info("udev: failed to create the udev monitor\n");
+               log_info(libinput,
+                        "udev: failed to create the udev monitor\n");
                return -1;
        }
 
@@ -253,7 +253,7 @@ udev_input_enable(struct libinput *libinput)
                        "input", NULL);
 
        if (udev_monitor_enable_receiving(input->udev_monitor)) {
-               log_info("udev: failed to bind the udev monitor\n");
+               log_info(libinput, "udev: failed to bind the udev monitor\n");
                udev_monitor_unref(input->udev_monitor);
                input->udev_monitor = NULL;
                return -1;
@@ -311,7 +311,6 @@ udev_seat_create(struct udev_input *input,
        libinput_seat_init(&seat->base, &input->base,
                           device_seat, seat_name,
                           udev_seat_destroy);
-       list_insert(&input->base.seat_list, &seat->base.link);
 
        return seat;
 }
@@ -336,14 +335,13 @@ static const struct libinput_interface_backend interface_backend = {
 };
 
 LIBINPUT_EXPORT struct libinput *
-libinput_udev_create_for_seat(const struct libinput_interface *interface,
-                             void *user_data,
-                             struct udev *udev,
-                             const char *seat_id)
+libinput_udev_create_context(const struct libinput_interface *interface,
+                            void *user_data,
+                            struct udev *udev)
 {
        struct udev_input *input;
 
-       if (!interface || !udev || !seat_id)
+       if (!interface || !udev)
                return NULL;
 
        input = zalloc(sizeof *input);
@@ -352,19 +350,36 @@ libinput_udev_create_for_seat(const struct libinput_interface *interface,
 
        if (libinput_init(&input->base, interface,
                          &interface_backend, user_data) != 0) {
+               libinput_unref(&input->base);
                free(input);
                return NULL;
        }
 
        input->udev = udev_ref(udev);
-       input->seat_id = strdup(seat_id);
 
-       if (udev_input_enable(&input->base) < 0) {
-               udev_unref(udev);
-               libinput_destroy(&input->base);
-               free(input);
-               return NULL;
+       return &input->base;
+}
+
+LIBINPUT_EXPORT int
+libinput_udev_assign_seat(struct libinput *libinput,
+                         const char *seat_id)
+{
+       struct udev_input *input = (struct udev_input*)libinput;
+
+       if (!seat_id)
+               return -1;
+       if (input->seat_id != NULL)
+               return -1;
+
+       if (libinput->interface_backend != &interface_backend) {
+               log_bug_client(libinput, "Mismatching backends.\n");
+               return -1;
        }
 
-       return &input->base;
+       input->seat_id = strdup(seat_id);
+
+       if (udev_input_enable(&input->base) < 0)
+               return -1;
+
+       return 0;
 }