tools: move opening the backend to the shared lib too

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2014-12-18 15:02:45 +10:00
parent e205615c98
commit 3628f7016e
3 changed files with 98 additions and 76 deletions

View 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;
}

View 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;
}

View 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();