tools/demo-server: add option parsing

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2020-08-25 09:20:31 +10:00
parent 55d92c8f61
commit 901235af5c

View file

@ -25,6 +25,7 @@
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <poll.h>
#include <stdio.h>
#include <signal.h>
@ -147,24 +148,78 @@ handle_key(struct eis_server *server, uint32_t keycode, bool is_press)
keysym_name);
}
static void
usage(FILE *fp, const char *argv0)
{
fprintf(fp,
"Usage: %s [--verbose] [--socketpath=/path/to/socket]\n"
"\n"
"Start an EIS demo server. The server accepts all client connections\n"
"and devices and prints any events from the client to stdout.\n"
"\n"
"Options:\n"
" --socketpath Use the given socket path. Default: $XDG_RUNTIME/eis-0\n"
" --verbose Enable debugging output\n"
"",
argv0);
}
int main(int argc, char **argv)
{
bool verbose = false;
_cleanup_unlink_free_ char *socketpath = NULL;
const char *xdg = getenv("XDG_RUNTIME_DIR");
if (xdg)
socketpath = xaprintf("%s/eis-0", xdg);
while (true) {
enum {
OPT_VERBOSE,
OPT_SOCKETPATH,
};
static struct option long_opts[] = {
{"socketpath", required_argument, 0, OPT_SOCKETPATH},
{"verbose", no_argument,0, OPT_VERBOSE},
{"help", no_argument,0, 'h'},
{NULL},
};
int optind = 0;
int c = getopt_long(argc, argv, "h", long_opts, &optind);
if (c == -1)
break;
switch(c) {
case 'h':
usage(stdout, argv[0]);
return EXIT_SUCCESS;
case OPT_SOCKETPATH:
free(socketpath);
socketpath = xstrdup(optarg);
break;
case OPT_VERBOSE:
verbose = true;
break;
default:
usage(stderr, argv[0]);
return EXIT_FAILURE;
}
}
if (socketpath == NULL) {
fprintf(stderr, "No socketpath given and $XDG_RUNTIME_DIR is not set\n");
return EXIT_FAILURE;
}
struct eis_server server = {0};
_cleanup_(eis_unrefp) struct eis *eis = eis_new(NULL);
assert(eis);
eis_log_set_priority(eis, EIS_LOG_PRIORITY_DEBUG);
_cleanup_unlink_free_ char *socketpath = NULL;
if (argc < 2) {
const char SOCKETNAME[] = "eis-0";
const char *xdgdir = getenv("XDG_RUNTIME_DIR");
assert(xdgdir != NULL);
socketpath = xaprintf("%s/%s", xdgdir, SOCKETNAME);
} else {
socketpath = xstrdup(argv[1]);
}
if (verbose)
eis_log_set_priority(eis, EIS_LOG_PRIORITY_DEBUG);
signal(SIGINT, sighandler);