tools: move opening the backend to the shared lib too
authorPeter Hutterer <peter.hutterer@who-t.net>
Thu, 18 Dec 2014 05:02:45 +0000 (15:02 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Tue, 23 Dec 2014 00:50:31 +0000 (10:50 +1000)
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
tools/event-debug.c
tools/shared.c
tools/shared.h

index 184c68fc60af4999ee3feefb59f9350a60d243ae..b8be215d85ff65ab77ca4e09391e6d37512e5074 100644 (file)
@@ -38,8 +38,6 @@
 
 #include "shared.h"
 
-static const char *device;
-static struct udev *udev;
 uint32_t start_time;
 static const uint32_t screen_width = 100;
 static const uint32_t screen_height = 100;
@@ -63,70 +61,6 @@ static const struct libinput_interface interface = {
        .close_restricted = close_restricted,
 };
 
-static void
-log_handler(struct libinput *li,
-           enum libinput_log_priority priority,
-           const char *format,
-           va_list args)
-{
-       vprintf(format, args);
-}
-
-static int
-open_udev(struct libinput **li)
-{
-       udev = udev_new();
-       if (!udev) {
-               fprintf(stderr, "Failed to initialize udev\n");
-               return 1;
-       }
-
-       *li = libinput_udev_create_context(&interface, NULL, udev);
-       if (!*li) {
-               fprintf(stderr, "Failed to initialize context from udev\n");
-               return 1;
-       }
-
-       if (options.verbose) {
-               libinput_log_set_handler(*li, log_handler);
-               libinput_log_set_priority(*li, LIBINPUT_LOG_PRIORITY_DEBUG);
-       }
-
-       if (libinput_udev_assign_seat(*li, options.seat)) {
-               fprintf(stderr, "Failed to set seat\n");
-               libinput_unref(*li);
-               return 1;
-       }
-
-       return 0;
-}
-
-static int
-open_device(struct libinput **li, const char *path)
-{
-       struct libinput_device *device;
-
-       *li = libinput_path_create_context(&interface, NULL);
-       if (!*li) {
-               fprintf(stderr, "Failed to initialize context from %s\n", path);
-               return 1;
-       }
-
-       if (options.verbose) {
-               libinput_log_set_handler(*li, log_handler);
-               libinput_log_set_priority(*li, LIBINPUT_LOG_PRIORITY_DEBUG);
-       }
-
-       device = libinput_path_add_device(*li, path);
-       if (!device) {
-               fprintf(stderr, "Failed to initialized device %s\n", path);
-               libinput_unref(*li);
-               return 1;
-       }
-
-       return 0;
-}
-
 static void
 print_event_header(struct libinput_event *ev)
 {
@@ -440,14 +374,9 @@ main(int argc, char **argv)
        if (tools_parse_args(argc, argv, &options))
                return 1;
 
-       if (options.backend == BACKEND_UDEV) {
-               if (open_udev(&li))
-                       return 1;
-       } else if (options.backend == BACKEND_DEVICE) {
-               if (open_device(&li, device))
-                       return 1;
-       } else
-               abort();
+       li = tools_open_backend(&options, &interface);
+       if (!li)
+               return 1;
 
        clock_gettime(CLOCK_MONOTONIC, &tp);
        start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
@@ -455,8 +384,6 @@ main(int argc, char **argv)
        mainloop(li);
 
        libinput_unref(li);
-       if (udev)
-               udev_unref(udev);
 
        return 0;
 }
index 17a967321ab50336c07a00ffd3bc4562639527ba..f8ceb341440db82e1f3a640041a047e6587185c8 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <libudev.h>
 
 #include "shared.h"
 
@@ -40,6 +41,15 @@ enum options {
        OPT_TAP_DISABLE,
 };
 
+static void
+log_handler(struct libinput *li,
+           enum libinput_log_priority priority,
+           const char *format,
+           va_list args)
+{
+       vprintf(format, args);
+}
+
 void
 tools_usage()
 {
@@ -131,3 +141,84 @@ tools_parse_args(int argc, char **argv, struct tools_options *options)
 
        return 0;
 }
+
+static struct libinput *
+open_udev(const struct libinput_interface *interface,
+         const char *seat,
+         int verbose)
+{
+       struct libinput *li;
+       struct udev *udev = udev_new();
+
+       if (!udev) {
+               fprintf(stderr, "Failed to initialize udev\n");
+               return NULL;
+       }
+
+       li = libinput_udev_create_context(interface, NULL, udev);
+       if (!li) {
+               fprintf(stderr, "Failed to initialize context from udev\n");
+               goto out;
+       }
+
+       if (verbose) {
+               libinput_log_set_handler(li, log_handler);
+               libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
+       }
+
+       if (libinput_udev_assign_seat(li, seat)) {
+               fprintf(stderr, "Failed to set seat\n");
+               libinput_unref(li);
+               li = NULL;
+               goto out;
+       }
+
+out:
+       udev_unref(udev);
+       return li;
+}
+
+static struct libinput *
+open_device(const struct libinput_interface *interface,
+           const char *path,
+           int verbose)
+{
+       struct libinput_device *device;
+       struct libinput *li;
+
+       li = libinput_path_create_context(interface, NULL);
+       if (!li) {
+               fprintf(stderr, "Failed to initialize context from %s\n", path);
+               return NULL;
+       }
+
+       if (verbose) {
+               libinput_log_set_handler(li, log_handler);
+               libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
+       }
+
+       device = libinput_path_add_device(li, path);
+       if (!device) {
+               fprintf(stderr, "Failed to initialized device %s\n", path);
+               libinput_unref(li);
+               li = NULL;
+       }
+
+       return li;
+}
+
+struct libinput *
+tools_open_backend(struct tools_options *options,
+                  const struct libinput_interface *interface)
+{
+       struct libinput *li = NULL;
+
+       if (options->backend == BACKEND_UDEV) {
+               li = open_udev(interface, options->seat, options->verbose);
+       } else if (options->backend == BACKEND_DEVICE) {
+               li = open_device(interface, options->device, options->verbose);
+       } else
+               abort();
+
+       return li;
+}
index fa7c5efe74a051d01faa3d635abf370285e126f1..04d1369752910dfcf3c1c9facab44ad3473828b7 100644 (file)
@@ -23,6 +23,8 @@
 #ifndef _SHARED_H_
 #define _SHARED_H_
 
+#include <libinput.h>
+
 enum tools_backend {
        BACKEND_DEVICE,
        BACKEND_UDEV
@@ -39,6 +41,8 @@ struct tools_options {
 
 void tools_init_options(struct tools_options *options);
 int tools_parse_args(int argc, char **argv, struct tools_options *options);
+struct libinput* tools_open_backend(struct tools_options *options,
+                                   const struct libinput_interface *interface);
 
 void tools_usage();