summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2013-05-01 09:37:10 +1000
committerBenjamin Tissoires <benjamin.tissoires@redhat.com>2013-05-02 16:17:57 +0200
commit96907b5213751af3792f469275b4ba3ee7b00af6 (patch)
tree8b1fa841189a1f4ba1ed2ad732eae536e5489f64 /tools
parent061e9d7cbc0488b990285afe294e8ea2dac085b3 (diff)
If no device file is given, display the list
Code taken from evtest, but since I wrote that code I'm happy for the relicensing. License picked for that code is MIT so we won't have to worry about copying that code in the future. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile.am3
-rw-r--r--tools/evemu-describe.txt8
-rw-r--r--tools/evemu-record.c11
-rw-r--r--tools/find_event_devices.c85
-rw-r--r--tools/find_event_devices.h30
5 files changed, 132 insertions, 5 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am
index dd5bdd8..e471570 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -12,7 +12,8 @@ INCLUDES=-I$(top_srcdir)/include/
AM_LDFLAGS = $(top_builddir)/src/libevemu.la
-evemu_describe_SOURCES = evemu-record.c
+evemu_describe_SOURCES = evemu-record.c find_event_devices.c find_event_devices.h
+evemu_record_SOURCES = evemu-record.c find_event_devices.c find_event_devices.h
# man page generation
if HAVE_DOCTOOLS
diff --git a/tools/evemu-describe.txt b/tools/evemu-describe.txt
index 8544169..a0469d5 100644
--- a/tools/evemu-describe.txt
+++ b/tools/evemu-describe.txt
@@ -9,9 +9,9 @@ NAME
SYNOPSIS
--------
- evemu-describe /dev/input/eventX
+ evemu-describe [/dev/input/eventX]
- evemu-record /dev/input/eventX
+ evemu-record [/dev/input/eventX]
DESCRIPTION
-----------
@@ -26,6 +26,10 @@ device created with evemu-device(1) emit the exact same event sequence.
evemu-describe and evemu-record need to be able to read from the device; in
most cases this means they must be run as root.
+If an event node is provided, evemu-describe and evemu-record use that event
+node. Otherwise, the user must interactively choose from a list of detected
+devices.
+
DIAGNOSTICS
-----------
If evtest-record does not see any events even though the device is being
diff --git a/tools/evemu-record.c b/tools/evemu-record.c
index 38d49b9..8fe2a57 100644
--- a/tools/evemu-record.c
+++ b/tools/evemu-record.c
@@ -42,11 +42,14 @@
#define _GNU_SOURCE
#include "evemu.h"
#include <stdio.h>
+#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
+#include "find_event_devices.h"
+
#define INFINITE -1
FILE *output;
@@ -89,17 +92,20 @@ int main(int argc, char *argv[])
int fd;
struct sigaction act;
char *prgm_name = program_invocation_short_name;
+ char *device;
if (prgm_name && (strcmp(prgm_name, "evemu-describe") == 0 ||
/* when run directly from the sources (not installed) */
strcmp(prgm_name, "lt-evemu-describe") == 0))
mode = EVEMU_DESCRIBE;
- if (argc < 2) {
+ device = (argc < 2) ? find_event_devices() : strdup(argv[1]);
+
+ if (device == NULL) {
fprintf(stderr, "Usage: %s <device> [output file]\n", argv[0]);
return -1;
}
- fd = open(argv[1], O_RDONLY | O_NONBLOCK);
+ fd = open(device, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
fprintf(stderr, "error: could not open device\n");
return -1;
@@ -146,6 +152,7 @@ int main(int argc, char *argv[])
}
out:
+ free(device);
close(fd);
if (output != stdout) {
fclose(output);
diff --git a/tools/find_event_devices.c b/tools/find_event_devices.c
new file mode 100644
index 0000000..fd3dc3a
--- /dev/null
+++ b/tools/find_event_devices.c
@@ -0,0 +1,85 @@
+/*****************************************************************************
+ * Copyright (C) 2013 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.
+ *
+ ****************************************************************************/
+
+#define _GNU_SOURCE
+#include <linux/input.h>
+#include <dirent.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+
+#define DEV_INPUT_EVENT "/dev/input"
+#define EVENT_DEV_NAME "event"
+
+static int is_event_device(const struct dirent *dir) {
+ return strncmp(EVENT_DEV_NAME, dir->d_name, 5) == 0;
+}
+
+char* find_event_devices(void)
+{
+ struct dirent **namelist;
+ int i, ndev, devnum;
+ char *filename;
+
+ ndev = scandir(DEV_INPUT_EVENT, &namelist, is_event_device, alphasort);
+ if (ndev <= 0)
+ return NULL;
+
+ fprintf(stderr, "Available devices:\n");
+
+ for (i = 0; i < ndev; i++)
+ {
+ char fname[64];
+ int fd = -1;
+ char name[256] = "???";
+
+ snprintf(fname, sizeof(fname),
+ "%s/%s", DEV_INPUT_EVENT, namelist[i]->d_name);
+ fd = open(fname, O_RDONLY);
+ if (fd < 0)
+ continue;
+ ioctl(fd, EVIOCGNAME(sizeof(name)), name);
+
+ fprintf(stderr, "%s: %s\n", fname, name);
+ close(fd);
+ free(namelist[i]);
+ }
+
+ fprintf(stderr, "Select the device event number [0-%d]: ", ndev - 1);
+ scanf("%d", &devnum);
+
+ if (devnum >= ndev || devnum < 0)
+ return NULL;
+
+ asprintf(&filename, "%s/%s%d",
+ DEV_INPUT_EVENT, EVENT_DEV_NAME,
+ devnum);
+
+ return filename;
+}
diff --git a/tools/find_event_devices.h b/tools/find_event_devices.h
new file mode 100644
index 0000000..04b87c1
--- /dev/null
+++ b/tools/find_event_devices.h
@@ -0,0 +1,30 @@
+/*****************************************************************************
+ * Copyright (C) 2013 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 FIND_EVENT_DEVICES_H
+#define FIND_EVENT_DEVICES_H
+
+char* find_event_devices(void);
+
+#endif