From 3628f7016e3c457b0384549c3f4fe9299002f6a0 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 18 Dec 2014 15:02:45 +1000 Subject: [PATCH] tools: move opening the backend to the shared lib too Signed-off-by: Peter Hutterer --- tools/event-debug.c | 79 ++------------------------------------- tools/shared.c | 91 +++++++++++++++++++++++++++++++++++++++++++++ tools/shared.h | 4 ++ 3 files changed, 98 insertions(+), 76 deletions(-) diff --git a/tools/event-debug.c b/tools/event-debug.c index 184c68fc..b8be215d 100644 --- a/tools/event-debug.c +++ b/tools/event-debug.c @@ -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; } diff --git a/tools/shared.c b/tools/shared.c index 17a96732..f8ceb341 100644 --- a/tools/shared.c +++ b/tools/shared.c @@ -28,6 +28,7 @@ #include #include #include +#include #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; +} diff --git a/tools/shared.h b/tools/shared.h index fa7c5efe..04d13697 100644 --- a/tools/shared.h +++ b/tools/shared.h @@ -23,6 +23,8 @@ #ifndef _SHARED_H_ #define _SHARED_H_ +#include + 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();