Skip to content
Snippets Groups Projects
Commit 3628f701 authored by Peter Hutterer's avatar Peter Hutterer
Browse files

tools: move opening the backend to the shared lib too


Signed-off-by: default avatarPeter Hutterer <peter.hutterer@who-t.net>
parent e205615c
No related branches found
No related tags found
No related merge requests found
...@@ -38,8 +38,6 @@ ...@@ -38,8 +38,6 @@
#include "shared.h" #include "shared.h"
static const char *device;
static struct udev *udev;
uint32_t start_time; uint32_t start_time;
static const uint32_t screen_width = 100; static const uint32_t screen_width = 100;
static const uint32_t screen_height = 100; static const uint32_t screen_height = 100;
...@@ -63,70 +61,6 @@ static const struct libinput_interface interface = { ...@@ -63,70 +61,6 @@ static const struct libinput_interface interface = {
.close_restricted = close_restricted, .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 static void
print_event_header(struct libinput_event *ev) print_event_header(struct libinput_event *ev)
{ {
...@@ -440,14 +374,9 @@ main(int argc, char **argv) ...@@ -440,14 +374,9 @@ main(int argc, char **argv)
if (tools_parse_args(argc, argv, &options)) if (tools_parse_args(argc, argv, &options))
return 1; return 1;
if (options.backend == BACKEND_UDEV) { li = tools_open_backend(&options, &interface);
if (open_udev(&li)) if (!li)
return 1; return 1;
} else if (options.backend == BACKEND_DEVICE) {
if (open_device(&li, device))
return 1;
} else
abort();
clock_gettime(CLOCK_MONOTONIC, &tp); clock_gettime(CLOCK_MONOTONIC, &tp);
start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
...@@ -455,8 +384,6 @@ main(int argc, char **argv) ...@@ -455,8 +384,6 @@ main(int argc, char **argv)
mainloop(li); mainloop(li);
libinput_unref(li); libinput_unref(li);
if (udev)
udev_unref(udev);
return 0; return 0;
} }
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <libudev.h>
#include "shared.h" #include "shared.h"
...@@ -40,6 +41,15 @@ enum options { ...@@ -40,6 +41,15 @@ enum options {
OPT_TAP_DISABLE, 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 void
tools_usage() tools_usage()
{ {
...@@ -131,3 +141,84 @@ tools_parse_args(int argc, char **argv, struct tools_options *options) ...@@ -131,3 +141,84 @@ tools_parse_args(int argc, char **argv, struct tools_options *options)
return 0; 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;
}
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#ifndef _SHARED_H_ #ifndef _SHARED_H_
#define _SHARED_H_ #define _SHARED_H_
#include <libinput.h>
enum tools_backend { enum tools_backend {
BACKEND_DEVICE, BACKEND_DEVICE,
BACKEND_UDEV BACKEND_UDEV
...@@ -39,6 +41,8 @@ struct tools_options { ...@@ -39,6 +41,8 @@ struct tools_options {
void tools_init_options(struct tools_options *options); void tools_init_options(struct tools_options *options);
int tools_parse_args(int argc, char **argv, 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(); void tools_usage();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment