Skip to content

Commit

Permalink
tools: add a 'libinput' tool
Browse files Browse the repository at this point in the history
This tool will eventually replace the different libinput tools we ship atm
with the various functionalities being commands to the single tool, rather
than multiple tools.

Right now, we still build both tools separately.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
  • Loading branch information
whot committed May 17, 2017
1 parent d2c7f73 commit ed72e2b
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 18 deletions.
10 changes: 10 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,18 @@ executable('libinput-list-devices',
libinput_list_devices_sources,
dependencies : [ dep_libinput ],
include_directories : include_directories('src'),
c_args : [ '-DTOOLS_BUILD_STANDALONE=1' ],
install : true,
)

libinput_sources = [ 'tools/libinput-tool.c' ] + libinput_list_devices_sources
executable('libinput',
libinput_sources,
dependencies : dep_libinput,
include_directories : include_directories ('src'),
install : true
)
install_man('tools/libinput.1')

ptraccel_debug_sources = [ 'tools/ptraccel-debug.c' ]
executable('ptraccel-debug',
Expand Down
14 changes: 12 additions & 2 deletions tools/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
noinst_PROGRAMS = event-debug ptraccel-debug
bin_PROGRAMS = libinput-list-devices libinput-debug-events
bin_PROGRAMS = libinput-list-devices libinput-debug-events libinput

AM_CPPFLAGS = -I$(top_srcdir)/include \
-I$(top_srcdir)/src \
Expand All @@ -22,14 +22,24 @@ ptraccel_debug_LDFLAGS = -no-install

libinput_list_devices_SOURCES = libinput-list-devices.c $(shared_sources)
libinput_list_devices_LDADD = ../src/libinput.la $(LIBUDEV_LIBS) $(LIBEVDEV_LIBS)
libinput_list_devices_CFLAGS = $(AM_CFLAGS) $(LIBUDEV_CFLAGS) $(LIBEVDEV_CFLAGS)
libinput_list_devices_CFLAGS = $(AM_CFLAGS) $(LIBUDEV_CFLAGS) $(LIBEVDEV_CFLAGS) \
-DTOOLS_BUILD_STANDALONE=1
dist_man1_MANS = libinput-list-devices.1

libinput_debug_events_SOURCES = $(event_debug_SOURCES)
libinput_debug_events_LDADD = $(event_debug_LDADD)
libinput_debug_events_CFLAGS = $(AM_CFLAGS) $(event_debug_CFLAGS)
dist_man1_MANS += libinput-debug-events.1

libinput_SOURCES = \
libinput-tool.c \
libinput-tool.h \
libinput-list-devices.c \
$(shared_sources)
libinput_LDADD = ../src/libinput.la $(LIBUDEV_LIBS) $(LIBEVDEV_LIBS)
libinput_CFLAGS = $(AM_CFLAGS) $(LIBUDEV_CFLAGS) $(LIBEVDEV_CFLAGS)
dist_man1_MANS += libinput.1

if BUILD_EVENTGUI
noinst_PROGRAMS += event-gui

Expand Down
56 changes: 40 additions & 16 deletions tools/libinput-list-devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <libinput-util.h>
#include <libinput-version.h>

#include "libinput-tool.h"
#include "shared.h"

static const char *
Expand Down Expand Up @@ -354,39 +355,38 @@ print_device_notify(struct libinput_event *ev)
static inline void
usage(void)
{
printf("Usage: %s [--help|--version]\n"
"\n"
#if TOOLS_BUILD_STANDALONE
printf("Usage: %s [--help|--version]\n", program_invocation_short_name);
#else
printf("Usage: libinput list-devices [--help]\n");
#endif
printf("\n"
"This tool creates a libinput context on the default seat \"seat0\"\n"
"and lists all devices recognized by libinput and the configuration options.\n"
"Where multiple options are possible, the default is prefixed with \"*\".\n"
"\n"
"Options:\n"
"--help ...... show this help\n"
#if TOOLS_BUILD_STANDALONE
"--version ... show version information\n"
#endif
"\n"
"This tool requires access to the /dev/input/eventX nodes.\n",
program_invocation_short_name);
"This tool requires access to the /dev/input/eventX nodes.\n");
}

int
main(int argc, char **argv)
libinput_list_devices(struct global_options *opts, int argc, char **argv)
{
struct libinput *li;
struct tools_context context;
struct libinput_event *ev;

#if !TOOLS_BUILD_STANDALONE
if (argc > 1) {
if (streq(argv[1], "--help")) {
usage();
return 0;
} else if (streq(argv[1], "--version")) {
printf("%s\n", LIBINPUT_VERSION);
return 0;
} else {
usage();
return 1;
}
usage();
return streq(argv[1], "--help") ? EXIT_SUCCESS : EXIT_FAILURE;
}
#endif

tools_init_context(&context);

Expand All @@ -406,5 +406,29 @@ main(int argc, char **argv)

libinput_unref(li);

return 0;
return EXIT_SUCCESS;
}

#if TOOLS_BUILD_STANDALONE
int
main(int argc, char **argv)
{
struct global_options options = {0};

if (argc > 1) {
if (streq(argv[1], "--help")) {
usage();
return 0;
} else if (streq(argv[1], "--version")) {
printf("%s\n", LIBINPUT_VERSION);
return 0;
} else {
usage();
return 1;
}
}

return libinput_list_devices(&options, argc - optind, &argv[optind]);
}

#endif /* TOOLS_BUILD_STANDALONE */
153 changes: 153 additions & 0 deletions tools/libinput-tool.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright © 2017 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#include "config.h"

#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libudev.h>

#include <libinput.h>
#include <libinput-util.h>
#include <libinput-version.h>

#include "libinput-tool.h"

void
libinput_tool_usage(void)
{
printf("Usage: libinput [GLOBAL OPTIONS] [COMMAND] [ARGS]\n"
"\n"
"This tool creates a libinput context and interacts with that context.\n"
"For detailed information about the options below, see the"
"libinput(1) man page.\n"
"\n"
"This tool usually requires access to the /dev/input/eventX nodes.\n"
"\n"
"Global options:\n"
" --help ...... show this help\n"
" --version ... show version information\n"
" --verbose ... enable verbose output for debugging\n"
" --quiet ..... reduce output (may be used with --verbose)\n"
"\n"
"Commands:\n"
" list-devices\n"
" List all devices with their default configuration options\n"
"\n");
}

enum command {
COMMAND_NONE,
COMMAND_LIST_DEVICES,
};

enum global_opts {
GOPT_HELP = 1,
GOPT_VERSION,
GOPT_QUIET,
GOPT_VERBOSE,
};

static bool
parse_args_cmd(enum command cmd,
struct global_options *global_options,
int argc, char *argv[])
{
optind = 0;

switch (cmd) {
case COMMAND_NONE:
break;
case COMMAND_LIST_DEVICES:
return libinput_list_devices(global_options, argc, argv);
}
return true;
}

int
main(int argc, char **argv)
{
enum command cmd = COMMAND_NONE;
const char *command;
int option_index = 0;
struct global_options global_options = {0};

if (argc == 1) {
libinput_tool_usage();
return false;
}

while (1) {
int c;
static struct option opts[] = {
{ "help", no_argument, 0, GOPT_HELP },
{ "version", no_argument, 0, GOPT_VERSION },
{ "quiet", no_argument, 0, GOPT_QUIET },
{ "verbose", no_argument, 0, GOPT_VERBOSE },
{ 0, 0, 0, 0}
};

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

switch(c) {
case 'h':
case GOPT_HELP:
libinput_tool_usage();
exit(0);
case GOPT_VERSION:
printf("%s\n", LIBINPUT_VERSION);
exit(0);
case GOPT_VERBOSE:
global_options.verbose = true;
break;
case GOPT_QUIET:
global_options.quiet = true;
break;
default:
libinput_tool_usage();
return false;
}
}

if (optind > argc) {
libinput_tool_usage();
return false;
}

command = argv[optind];

if (streq(command, "list-devices")) {
cmd = COMMAND_LIST_DEVICES;
} else {
fprintf(stderr, "Invalid command '%s'\n", command);
return EXIT_FAILURE;
}

return parse_args_cmd(cmd, &global_options, argc - optind, &argv[optind]);
}
39 changes: 39 additions & 0 deletions tools/libinput-tool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright © 2017 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#ifndef LIBINPUT_TOOL_H
#define LIBINPUT_TOOL_H 1

#include "config.h"

#include <stdbool.h>

struct global_options {
bool verbose;
bool quiet;
};

void libinput_tool_usage(void);
int libinput_list_devices(struct global_options *opts, int argc, char **argv);

#endif

0 comments on commit ed72e2b

Please sign in to comment.