Skip to content

Commit

Permalink
tools: split the configuration option parsing out
Browse files Browse the repository at this point in the history
We had one shared parsing function for all config options so tools parse
options that don't actually make sense (e.g. --quiet or --show-keycodes for
libinput-list-devices).

This patch splits the actual libinput device configuration out and reshuffles
everything to make use of that. One large patch, because splitting this up is
more confusing than dumping it all.

This means the actual option parsing is partially duplicated between debug-gui
and debug-events but hey, not everything in life is perfect.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
  • Loading branch information
whot committed Jun 26, 2017
1 parent be7da7f commit d1b7b62
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 292 deletions.
8 changes: 4 additions & 4 deletions tools/libinput-debug-events.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SH NAME
libinput\-debug\-events \- debug helper for libinput
.SH SYNOPSIS
libinput debug\-events [\-\-help] [\-\-show\-keycodes] [\-\-udev [<seat>]|\-\-device /dev/input/event0] [configuration options]
libinput debug\-events [\-\-help] [\-\-show\-keycodes] [\-\-udev <seat>|\-\-device /dev/input/event0] [configuration options]
.SH DESCRIPTION
.PP
The
Expand Down Expand Up @@ -36,9 +36,9 @@ and other sensitive information showing up in the output. Use the
.B \-\-show\-keycodes
argument to make all keycodes visible.
.TP 8
.B \-\-udev [<seat>]
Use the udev backend to listen for device notifications. If a seat is given,
use that seat, otherwise default to "seat0".
.B \-\-udev <seat>
Use the udev backend to listen for device notifications on the given seat.
The default behavior is equivalent to \-\-udev "seat0".
.TP 8
.B \-\-verbose
Use verbose output
Expand Down
101 changes: 86 additions & 15 deletions tools/libinput-debug-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <errno.h>
#include <inttypes.h>
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <stdio.h>
#include <signal.h>
Expand All @@ -41,10 +42,11 @@

#include "shared.h"

uint32_t start_time;
static uint32_t start_time;
static const uint32_t screen_width = 100;
static const uint32_t screen_height = 100;
struct tools_context context;
static struct tools_options options;
static bool show_keycodes;
static unsigned int stop = 0;
static bool be_quiet = false;

Expand Down Expand Up @@ -282,21 +284,15 @@ static void
print_key_event(struct libinput *li, struct libinput_event *ev)
{
struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
struct tools_context *context;
struct tools_options *options;
enum libinput_key_state state;
uint32_t key;
const char *keyname;

context = libinput_get_user_data(li);
options = &context->options;

print_event_time(libinput_event_keyboard_get_time(k));
state = libinput_event_keyboard_get_key_state(k);

key = libinput_event_keyboard_get_key(k);
if (!options->show_keycodes &&
(key >= KEY_ESC && key < KEY_ZENKAKUHANKAKU)) {
if (!show_keycodes && (key >= KEY_ESC && key < KEY_ZENKAKUHANKAKU)) {
keyname = "***";
key = -1;
} else {
Expand Down Expand Up @@ -777,7 +773,7 @@ handle_and_print_events(struct libinput *li)
case LIBINPUT_EVENT_DEVICE_REMOVED:
print_device_notify(ev);
tools_device_apply_config(libinput_event_get_device(ev),
&context.options);
&options);
break;
case LIBINPUT_EVENT_KEYBOARD_KEY:
print_key_event(li, ev);
Expand Down Expand Up @@ -895,23 +891,98 @@ mainloop(struct libinput *li)
handle_and_print_events(li);
}

static void
usage(void) {
printf("Usage: libinput debug-events [options] [--udev <seat>|--device /dev/input/event0]\n");
}

int
main(int argc, char **argv)
{
struct libinput *li;
struct timespec tp;
enum tools_backend backend = BACKEND_UDEV;
const char *seat_or_device = "seat0";
bool grab = false;
bool verbose = false;

clock_gettime(CLOCK_MONOTONIC, &tp);
start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;

tools_init_context(&context);
tools_init_options(&options);

while (1) {
int c;
int option_index = 0;
enum {
OPT_DEVICE = 1,
OPT_UDEV,
OPT_GRAB,
OPT_VERBOSE,
OPT_SHOW_KEYCODES,
OPT_QUIET,
};
static struct option opts[] = {
CONFIGURATION_OPTIONS,
{ "help", no_argument, 0, 'h' },
{ "show-keycodes", no_argument, 0, OPT_SHOW_KEYCODES },
{ "device", required_argument, 0, OPT_DEVICE },
{ "udev", required_argument, 0, OPT_UDEV },
{ "grab", no_argument, 0, OPT_GRAB },
{ "verbose", no_argument, 0, OPT_VERBOSE },
{ "quiet", no_argument, 0, OPT_QUIET },
{ 0, 0, 0, 0}
};

c = getopt_long(argc, argv, "h", opts, &option_index);
if (c == -1)
break;

if (tools_parse_args("debug-events", argc, argv, &context))
return 1;
switch(c) {
case '?':
exit(1);
break;
case 'h':
usage();
exit(0);
break;
case OPT_SHOW_KEYCODES:
show_keycodes = true;
break;
case OPT_QUIET:
be_quiet = true;
break;
case OPT_DEVICE:
backend = BACKEND_DEVICE;
seat_or_device = optarg;
break;
case OPT_UDEV:
backend = BACKEND_UDEV;
seat_or_device = optarg;
break;
case OPT_GRAB:
grab = true;
break;
case OPT_VERBOSE:
verbose = true;
break;
default:
printf(".. %c\n", c);
if (tools_parse_option(c, optarg, &options) != 0) {
usage();
return 1;
}
break;
}

}

be_quiet = context.options.quiet;
if (optind < argc) {
usage();
return 1;
}

li = tools_open_backend(&context);
li = tools_open_backend(backend, seat_or_device, verbose, grab);
if (!li)
return 1;

Expand Down
12 changes: 4 additions & 8 deletions tools/libinput-debug-gui.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SH NAME
libinput\-debug\-gui \- visual debug helper for libinput
.SH SYNOPSIS
.B libinput debug\-gui [\-\-help] [\-\-udev [<seat>]|\-\-device /dev/input/event0] [configuration options]
.B libinput debug\-gui [\-\-help] [\-\-udev <seat>|\-\-device /dev/input/event0] [configuration options]
.SH DESCRIPTION
.PP
The
Expand Down Expand Up @@ -30,13 +30,9 @@ delivered to the host system.
.B \-\-help
Print help
.TP 8
.B \-\-quiet
Only print libinput messages, don't print anything from this tool. This is
useful in combination with --verbose for internal state debugging.
.TP 8
.B \-\-udev [<seat>]
Use the udev backend to listen for device notifications. If a seat is given,
use that seat, otherwise default to "seat0".
.B \-\-udev <seat>
Use the udev backend to listen for device notifications on the given seat.
The default behavior is equivalent to \-\-udev "seat0".
.TP 8
.B \-\-verbose
Use verbose output
Expand Down
111 changes: 78 additions & 33 deletions tools/libinput-debug-gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <cairo.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -43,8 +44,6 @@

#define clip(val_, min_, max_) min((max_), max((min_), (val_)))

struct tools_context context;

struct touch {
int active;
int x, y;
Expand All @@ -55,6 +54,8 @@ struct point {
};

struct window {
struct tools_options options;

GtkWidget *win;
GtkWidget *area;
int width, height; /* of window */
Expand Down Expand Up @@ -107,21 +108,7 @@ struct window {
};

LIBINPUT_ATTRIBUTE_PRINTF(1, 2)
static int
error(const char *fmt, ...)
{
va_list args;
fprintf(stderr, "error: ");

va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);

return EXIT_FAILURE;
}

LIBINPUT_ATTRIBUTE_PRINTF(1, 2)
static void
static inline void
msg(const char *fmt, ...)
{
va_list args;
Expand Down Expand Up @@ -492,7 +479,6 @@ change_ptraccel(struct window *w, double amount)
static void
handle_event_device_notify(struct libinput_event *ev)
{
struct tools_context *context;
struct libinput_device *dev = libinput_event_get_device(ev);
struct libinput *li;
struct window *w;
Expand All @@ -510,11 +496,10 @@ handle_event_device_notify(struct libinput_event *ev)
type);

li = libinput_event_get_context(ev);
context = libinput_get_user_data(li);
w = context->user_data;
w = libinput_get_user_data(li);

tools_device_apply_config(libinput_event_get_device(ev),
&context->options);
&w->options);

if (libinput_event_get_type(ev) == LIBINPUT_EVENT_DEVICE_ADDED) {
for (i = 0; i < ARRAY_LENGTH(w->devices); i++) {
Expand Down Expand Up @@ -796,8 +781,7 @@ static gboolean
handle_event_libinput(GIOChannel *source, GIOCondition condition, gpointer data)
{
struct libinput *li = data;
struct tools_context *context = libinput_get_user_data(li);
struct window *w = context->user_data;
struct window *w = libinput_get_user_data(li);
struct libinput_event *ev;

libinput_dispatch(li);
Expand Down Expand Up @@ -878,29 +862,91 @@ sockets_init(struct libinput *li)
g_io_add_watch(c, G_IO_IN, handle_event_libinput, li);
}

static void
usage(void) {
printf("Usage: libinput debug-gui [options] [--udev <seat>|--device /dev/input/event0]\n");
}

int
main(int argc, char **argv)
{
struct window w;
struct libinput *li;
struct udev *udev;
enum tools_backend backend = BACKEND_UDEV;
const char *seat_or_device = "seat0";
bool grab = false;
bool verbose = false;

gtk_init(&argc, &argv);

tools_init_context(&context);
tools_init_options(&w.options);

while (1) {
int c;
int option_index = 0;
enum {
OPT_DEVICE = 1,
OPT_UDEV,
OPT_GRAB,
OPT_VERBOSE,
};
static struct option opts[] = {
CONFIGURATION_OPTIONS,
{ "help", no_argument, 0, 'h' },
{ "device", required_argument, 0, OPT_DEVICE },
{ "udev", required_argument, 0, OPT_UDEV },
{ "grab", no_argument, 0, OPT_GRAB },
{ "verbose", no_argument, 0, OPT_VERBOSE },
{ 0, 0, 0, 0}
};

c = getopt_long(argc, argv, "h", opts, &option_index);
if (c == -1)
break;

if (tools_parse_args("debug-gui", argc, argv, &context) != 0)
return 1;
switch(c) {
case '?':
exit(1);
break;
case 'h':
usage();
exit(0);
break;
case OPT_DEVICE:
backend = BACKEND_DEVICE;
seat_or_device = optarg;
break;
case OPT_UDEV:
backend = BACKEND_UDEV;
seat_or_device = optarg;
break;
case OPT_GRAB:
grab = true;
break;
case OPT_VERBOSE:
verbose = true;
break;
default:
if (tools_parse_option(c, optarg, &w.options) != 0) {
usage();
return 1;
}
break;
}

}

udev = udev_new();
if (!udev)
error("Failed to initialize udev\n");
if (optind < argc) {
usage();
return 1;
}

context.user_data = &w;
li = tools_open_backend(&context);
li = tools_open_backend(backend, seat_or_device, verbose, grab);
if (!li)
return 1;

libinput_set_user_data(li, &w);

window_init(&w);
sockets_init(li);
handle_event_libinput(NULL, 0, li);
Expand All @@ -909,7 +955,6 @@ main(int argc, char **argv)

window_cleanup(&w);
libinput_unref(li);
udev_unref(udev);

return 0;
}

0 comments on commit d1b7b62

Please sign in to comment.