tools: split the configuration option parsing out
authorPeter Hutterer <peter.hutterer@who-t.net>
Mon, 19 Jun 2017 08:38:33 +0000 (18:38 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Mon, 26 Jun 2017 08:43:59 +0000 (18:43 +1000)
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>
tools/libinput-debug-events.1
tools/libinput-debug-events.c
tools/libinput-debug-gui.1
tools/libinput-debug-gui.c
tools/libinput-list-devices.c
tools/shared.c
tools/shared.h

index f29ae95e661745bb77f6be02bb79bec28423eb62..6030388881c5c0fd519d8d168859b854bba46912 100644 (file)
@@ -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
@@ -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
index 25894b1df304e65703eb9ef4453f223d6e53ec50..a54ff55de8bb296666b24cb512195be37ba31f28 100644 (file)
@@ -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>
 
 #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;
 
@@ -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 {
@@ -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);
@@ -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;
 
index 59a796ab30c24e78ce204d9d6d7895a90f538829..00263ac376966f84534f131a3f68840368169409 100644 (file)
@@ -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
@@ -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
index ae1d2950cafa4f841232a89b358ada4e896db7d4..98d706b66bac29920dbab867cb6996518a8870d9 100644 (file)
@@ -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>
@@ -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;
@@ -55,6 +54,8 @@ struct point {
 };
 
 struct window {
+       struct tools_options options;
+
        GtkWidget *win;
        GtkWidget *area;
        int width, height; /* of window */
@@ -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;
@@ -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;
@@ -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++) {
@@ -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);
@@ -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);
@@ -909,7 +955,6 @@ main(int argc, char **argv)
 
        window_cleanup(&w);
        libinput_unref(li);
-       udev_unref(udev);
 
        return 0;
 }
index b98bb78a8e48765f843331b03a015e5284a48c77..376a3c96fba8f44d35a437cea820dab86033e949 100755 (executable)
@@ -365,7 +365,6 @@ int
 main(int argc, char **argv)
 {
        struct libinput *li;
-       struct tools_context context;
        struct libinput_event *ev;
 
        /* This is kept for backwards-compatibility with the old
@@ -383,9 +382,7 @@ main(int argc, char **argv)
                }
        }
 
-       tools_init_context(&context);
-
-       li = tools_open_backend(&context);
+       li = tools_open_backend(BACKEND_UDEV, "seat0", false, false);
        if (!li)
                return 1;
 
index 7df9f6839d80d05725f3c3e022864bdb3ab3ca20..db6e70ce7d42e9c4a7640c24faed50a85f2460ca 100644 (file)
 
 #include "shared.h"
 
-enum options {
-       OPT_DEVICE,
-       OPT_UDEV,
-       OPT_GRAB,
-       OPT_HELP,
-       OPT_VERBOSE,
-       OPT_TAP_ENABLE,
-       OPT_TAP_DISABLE,
-       OPT_TAP_MAP,
-       OPT_DRAG_ENABLE,
-       OPT_DRAG_DISABLE,
-       OPT_DRAG_LOCK_ENABLE,
-       OPT_DRAG_LOCK_DISABLE,
-       OPT_NATURAL_SCROLL_ENABLE,
-       OPT_NATURAL_SCROLL_DISABLE,
-       OPT_LEFT_HANDED_ENABLE,
-       OPT_LEFT_HANDED_DISABLE,
-       OPT_MIDDLEBUTTON_ENABLE,
-       OPT_MIDDLEBUTTON_DISABLE,
-       OPT_DWT_ENABLE,
-       OPT_DWT_DISABLE,
-       OPT_CLICK_METHOD,
-       OPT_SCROLL_METHOD,
-       OPT_SCROLL_BUTTON,
-       OPT_SPEED,
-       OPT_PROFILE,
-       OPT_SHOW_KEYCODES,
-       OPT_QUIET,
-};
-
 LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
 static void
 log_handler(struct libinput *li,
@@ -93,19 +63,8 @@ log_handler(struct libinput *li,
 }
 
 void
-tools_usage(const char *command)
-{
-       printf("Usage: libinput %s [options] [--udev [<seat>]|--device /dev/input/event0]\n",
-              command);
-}
-
-void
-tools_init_context(struct tools_context *context)
+tools_init_options(struct tools_options *options)
 {
-       struct tools_options *options = &context->options;
-
-       context->user_data = NULL;
-
        memset(options, 0, sizeof(*options));
        options->tapping = -1;
        options->tap_map = -1;
@@ -118,83 +77,16 @@ tools_init_context(struct tools_context *context)
        options->click_method = -1;
        options->scroll_method = -1;
        options->scroll_button = -1;
-       options->backend = BACKEND_UDEV;
-       options->seat = "seat0";
        options->speed = 0.0;
        options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
-       options->show_keycodes = false;
 }
 
 int
-tools_parse_args(const char *command,
-                int argc,
-                char **argv,
-                struct tools_context *context)
+tools_parse_option(int option,
+                  const char *optarg,
+                  struct tools_options *options)
 {
-       struct tools_options *options = &context->options;
-
-       while (1) {
-               int c;
-               int option_index = 0;
-               static struct option opts[] = {
-                       { "device",                    required_argument, 0, OPT_DEVICE },
-                       { "udev",                      no_argument,       0, OPT_UDEV },
-                       { "grab",                      no_argument,       0, OPT_GRAB },
-                       { "help",                      no_argument,       0, OPT_HELP },
-                       { "verbose",                   no_argument,       0, OPT_VERBOSE },
-                       { "quiet",                     no_argument,       0, OPT_QUIET },
-                       { "enable-tap",                no_argument,       0, OPT_TAP_ENABLE },
-                       { "disable-tap",               no_argument,       0, OPT_TAP_DISABLE },
-                       { "enable-drag",               no_argument,       0, OPT_DRAG_ENABLE },
-                       { "disable-drag",              no_argument,       0, OPT_DRAG_DISABLE },
-                       { "enable-drag-lock",          no_argument,       0, OPT_DRAG_LOCK_ENABLE },
-                       { "disable-drag-lock",         no_argument,       0, OPT_DRAG_LOCK_DISABLE },
-                       { "enable-natural-scrolling",  no_argument,       0, OPT_NATURAL_SCROLL_ENABLE },
-                       { "disable-natural-scrolling", no_argument,       0, OPT_NATURAL_SCROLL_DISABLE },
-                       { "enable-left-handed",        no_argument,       0, OPT_LEFT_HANDED_ENABLE },
-                       { "disable-left-handed",       no_argument,       0, OPT_LEFT_HANDED_DISABLE },
-                       { "enable-middlebutton",       no_argument,       0, OPT_MIDDLEBUTTON_ENABLE },
-                       { "disable-middlebutton",      no_argument,       0, OPT_MIDDLEBUTTON_DISABLE },
-                       { "enable-dwt",                no_argument,       0, OPT_DWT_ENABLE },
-                       { "disable-dwt",               no_argument,       0, OPT_DWT_DISABLE },
-                       { "set-click-method",          required_argument, 0, OPT_CLICK_METHOD },
-                       { "set-scroll-method",         required_argument, 0, OPT_SCROLL_METHOD },
-                       { "set-scroll-button",         required_argument, 0, OPT_SCROLL_BUTTON },
-                       { "set-profile",               required_argument, 0, OPT_PROFILE },
-                       { "set-tap-map",               required_argument, 0, OPT_TAP_MAP },
-                       { "set-speed",                 required_argument, 0, OPT_SPEED },
-                       { "show-keycodes",             no_argument,       0, OPT_SHOW_KEYCODES },
-                       { 0, 0, 0, 0}
-               };
-
-               c = getopt_long(argc, argv, "h", opts, &option_index);
-               if (c == -1)
-                       break;
-
-               switch(c) {
-               case 'h':
-               case OPT_HELP:
-                       tools_usage(command);
-                       exit(0);
-               case OPT_DEVICE:
-                       options->backend = BACKEND_DEVICE;
-                       if (!optarg) {
-                               tools_usage(command);
-                               return 1;
-                       }
-                       options->device = optarg;
-                       break;
-               case OPT_UDEV:
-                       options->backend = BACKEND_UDEV;
-                       if (optarg)
-                               options->seat = optarg;
-                       break;
-               case OPT_GRAB:
-                       options->grab = 1;
-                       break;
-               case OPT_VERBOSE:
-                       options->verbose = 1;
-                       break;
+       switch(option) {
                case OPT_TAP_ENABLE:
                        options->tapping = 1;
                        break;
@@ -202,16 +94,14 @@ tools_parse_args(const char *command,
                        options->tapping = 0;
                        break;
                case OPT_TAP_MAP:
-                       if (!optarg) {
-                               tools_usage(command);
+                       if (!optarg)
                                return 1;
-                       }
+
                        if (streq(optarg, "lrm")) {
                                options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LRM;
                        } else if (streq(optarg, "lmr")) {
                                options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LMR;
                        } else {
-                               tools_usage(command);
                                return 1;
                        }
                        break;
@@ -252,10 +142,9 @@ tools_parse_args(const char *command,
                        options->dwt = LIBINPUT_CONFIG_DWT_DISABLED;
                        break;
                case OPT_CLICK_METHOD:
-                       if (!optarg) {
-                               tools_usage(command);
+                       if (!optarg)
                                return 1;
-                       }
+
                        if (streq(optarg, "none")) {
                                options->click_method =
                                LIBINPUT_CONFIG_CLICK_METHOD_NONE;
@@ -266,15 +155,13 @@ tools_parse_args(const char *command,
                                options->click_method =
                                LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;
                        } else {
-                               tools_usage(command);
                                return 1;
                        }
                        break;
                case OPT_SCROLL_METHOD:
-                       if (!optarg) {
-                               tools_usage(command);
+                       if (!optarg)
                                return 1;
-                       }
+
                        if (streq(optarg, "none")) {
                                options->scroll_method =
                                LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
@@ -288,13 +175,11 @@ tools_parse_args(const char *command,
                                options->scroll_method =
                                LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
                        } else {
-                               tools_usage(command);
                                return 1;
                        }
                        break;
                case OPT_SCROLL_BUTTON:
                        if (!optarg) {
-                               tools_usage(command);
                                return 1;
                        }
                        options->scroll_button =
@@ -308,52 +193,55 @@ tools_parse_args(const char *command,
                        }
                        break;
                case OPT_SPEED:
-                       if (!optarg) {
-                               tools_usage(command);
+                       if (!optarg)
                                return 1;
-                       }
                        options->speed = atof(optarg);
                        break;
                case OPT_PROFILE:
-                       if (!optarg) {
-                               tools_usage(command);
+                       if (!optarg)
                                return 1;
-                       }
-                       if (streq(optarg, "adaptive")) {
+
+                       if (streq(optarg, "adaptive"))
                                options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE;
-                       } else if (streq(optarg, "flat")) {
-                               options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT;
-                       } else {
-                               tools_usage(command);
-                               return 1;
-                       }
-                       break;
-               case OPT_SHOW_KEYCODES:
-                       options->show_keycodes = true;
+                       else if (streq(optarg, "flat"))
+                             options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT;
+                       else
+                             return 1;
                        break;
-               case OPT_QUIET:
-                       options->quiet = true;
-                       break;
-               default:
-                       tools_usage(command);
-                       return 1;
-               }
-
-       }
-
-       if (optind < argc) {
-               tools_usage(command);
-               return 1;
        }
 
        return 0;
 }
 
+static int
+open_restricted(const char *path, int flags, void *user_data)
+{
+       bool *grab = user_data;
+       int fd = open(path, flags);
+
+       if (fd < 0)
+               fprintf(stderr, "Failed to open %s (%s)\n",
+                       path, strerror(errno));
+       else if (*grab && ioctl(fd, EVIOCGRAB, (void*)1) == -1)
+               fprintf(stderr, "Grab requested, but failed for %s (%s)\n",
+                       path, strerror(errno));
+
+       return fd < 0 ? -errno : fd;
+}
+
+static void
+close_restricted(int fd, void *user_data)
+{
+       close(fd);
+}
+
+static const struct libinput_interface interface = {
+       .open_restricted = open_restricted,
+       .close_restricted = close_restricted,
+};
+
 static struct libinput *
-open_udev(const struct libinput_interface *interface,
-         void *userdata,
-         const char *seat,
-         int verbose)
+tools_open_udev(const char *seat, bool verbose, bool grab)
 {
        struct libinput *li;
        struct udev *udev = udev_new();
@@ -363,7 +251,7 @@ open_udev(const struct libinput_interface *interface,
                return NULL;
        }
 
-       li = libinput_udev_create_context(interface, userdata, udev);
+       li = libinput_udev_create_context(&interface, &grab, udev);
        if (!li) {
                fprintf(stderr, "Failed to initialize context from udev\n");
                goto out;
@@ -387,15 +275,12 @@ out:
 }
 
 static struct libinput *
-open_device(const struct libinput_interface *interface,
-           void *userdata,
-           const char *path,
-           int verbose)
+tools_open_device(const char *path, bool verbose, bool grab)
 {
        struct libinput_device *device;
        struct libinput *li;
 
-       li = libinput_path_create_context(interface, userdata);
+       li = libinput_path_create_context(&interface, &grab);
        if (!li) {
                fprintf(stderr, "Failed to initialize context from %s\n", path);
                return NULL;
@@ -416,51 +301,22 @@ open_device(const struct libinput_interface *interface,
        return li;
 }
 
-static int
-open_restricted(const char *path, int flags, void *user_data)
-{
-       const struct tools_context *context = user_data;
-       int fd = open(path, flags);
-
-       if (fd < 0)
-               fprintf(stderr, "Failed to open %s (%s)\n",
-                       path, strerror(errno));
-       else if (context->options.grab &&
-                ioctl(fd, EVIOCGRAB, (void*)1) == -1)
-               fprintf(stderr, "Grab requested, but failed for %s (%s)\n",
-                       path, strerror(errno));
-
-       return fd < 0 ? -errno : fd;
-}
-
-static void
-close_restricted(int fd, void *user_data)
-{
-       close(fd);
-}
-
-static const struct libinput_interface interface = {
-       .open_restricted = open_restricted,
-       .close_restricted = close_restricted,
-};
-
 struct libinput *
-tools_open_backend(struct tools_context *context)
+tools_open_backend(enum tools_backend which,
+                  const char *seat_or_device,
+                  bool verbose,
+                  bool grab)
 {
-       struct libinput *li = NULL;
-       struct tools_options *options = &context->options;
-
-       if (options->backend == BACKEND_UDEV) {
-               li = open_udev(&interface,
-                              context,
-                              options->seat,
-                              options->verbose);
-       } else if (options->backend == BACKEND_DEVICE) {
-               li = open_device(&interface,
-                                context,
-                                options->device,
-                                options->verbose);
-       } else {
+       struct libinput *li;
+
+       switch (which) {
+       case BACKEND_UDEV:
+               li = tools_open_udev(seat_or_device, verbose, grab);
+               break;
+       case BACKEND_DEVICE:
+               li = tools_open_device(seat_or_device, verbose, grab);
+               break;
+       default:
                abort();
        }
 
index 0a39484ef3e5ce642c8d6ba51e4c1af17089c02a..ceaa08a92fb9929fd8dd6596cffd3972a5c646e3 100644 (file)
 
 #include <libinput.h>
 
+enum configuration_options {
+       OPT_TAP_ENABLE = 256,
+       OPT_TAP_DISABLE,
+       OPT_TAP_MAP,
+       OPT_DRAG_ENABLE,
+       OPT_DRAG_DISABLE,
+       OPT_DRAG_LOCK_ENABLE,
+       OPT_DRAG_LOCK_DISABLE,
+       OPT_NATURAL_SCROLL_ENABLE,
+       OPT_NATURAL_SCROLL_DISABLE,
+       OPT_LEFT_HANDED_ENABLE,
+       OPT_LEFT_HANDED_DISABLE,
+       OPT_MIDDLEBUTTON_ENABLE,
+       OPT_MIDDLEBUTTON_DISABLE,
+       OPT_DWT_ENABLE,
+       OPT_DWT_DISABLE,
+       OPT_CLICK_METHOD,
+       OPT_SCROLL_METHOD,
+       OPT_SCROLL_BUTTON,
+       OPT_SPEED,
+       OPT_PROFILE,
+};
+
+#define CONFIGURATION_OPTIONS \
+       { "enable-tap",                no_argument,       0, OPT_TAP_ENABLE }, \
+       { "disable-tap",               no_argument,       0, OPT_TAP_DISABLE }, \
+       { "enable-drag",               no_argument,       0, OPT_DRAG_ENABLE }, \
+       { "disable-drag",              no_argument,       0, OPT_DRAG_DISABLE }, \
+       { "enable-drag-lock",          no_argument,       0, OPT_DRAG_LOCK_ENABLE }, \
+       { "disable-drag-lock",         no_argument,       0, OPT_DRAG_LOCK_DISABLE }, \
+       { "enable-natural-scrolling",  no_argument,       0, OPT_NATURAL_SCROLL_ENABLE }, \
+       { "disable-natural-scrolling", no_argument,       0, OPT_NATURAL_SCROLL_DISABLE }, \
+       { "enable-left-handed",        no_argument,       0, OPT_LEFT_HANDED_ENABLE }, \
+       { "disable-left-handed",       no_argument,       0, OPT_LEFT_HANDED_DISABLE }, \
+       { "enable-middlebutton",       no_argument,       0, OPT_MIDDLEBUTTON_ENABLE }, \
+       { "disable-middlebutton",      no_argument,       0, OPT_MIDDLEBUTTON_DISABLE }, \
+       { "enable-dwt",                no_argument,       0, OPT_DWT_ENABLE }, \
+       { "disable-dwt",               no_argument,       0, OPT_DWT_DISABLE }, \
+       { "set-click-method",          required_argument, 0, OPT_CLICK_METHOD }, \
+       { "set-scroll-method",         required_argument, 0, OPT_SCROLL_METHOD }, \
+       { "set-scroll-button",         required_argument, 0, OPT_SCROLL_BUTTON }, \
+       { "set-profile",               required_argument, 0, OPT_PROFILE }, \
+       { "set-tap-map",               required_argument, 0, OPT_TAP_MAP }, \
+       { "set-speed",                 required_argument, 0, OPT_SPEED }
+
 enum tools_backend {
        BACKEND_DEVICE,
        BACKEND_UDEV
 };
 
 struct tools_options {
-       bool verbose;
-       bool quiet;
-       enum tools_backend backend;
-       const char *device; /* if backend is BACKEND_DEVICE */
-       const char *seat; /* if backend is BACKEND_UDEV */
-       int grab; /* EVIOCGRAB */
-       bool show_keycodes; /* show keycodes */
-
        int tapping;
        int drag;
        int drag_lock;
@@ -57,20 +94,16 @@ struct tools_options {
        enum libinput_config_accel_profile profile;
 };
 
-struct tools_context {
-       struct tools_options options;
-       void *user_data;
-};
-
-void tools_init_context(struct tools_context *context);
-int tools_parse_args(const char *command,
-                    int argc,
-                    char **argv,
-                    struct tools_context *context);
-struct libinput* tools_open_backend(struct tools_context *context);
+void tools_init_options(struct tools_options *options);
+int tools_parse_option(int option,
+                      const char *optarg,
+                      struct tools_options *options);
+struct libinput* tools_open_backend(enum tools_backend which,
+                                   const char *seat_or_device,
+                                   bool verbose,
+                                   bool grab);
 void tools_device_apply_config(struct libinput_device *device,
                               struct tools_options *options);
-void tools_usage(const char *command);
 int tools_exec_command(const char *prefix, int argc, char **argv);
 
 bool find_touchpad_device(char *path, size_t path_len);