summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Drake <dsd@laptop.org>2011-07-16 23:06:54 +0100
committerPeter Hutterer <peter.hutterer@who-t.net>2011-07-18 10:06:27 +1000
commit6015b4c4cf0efeb845673c37d80f3049e86c04a1 (patch)
treefda19504ec8381921adc3f3aefefcf7622946977
parentbbd01de228e84213be051a1699acc2dd4f618bb8 (diff)
Factor out capture setup code into its own function
This is in preparation for a followup patch which adds a one-shot query mode. Signed-off-by: Daniel Drake <dsd@laptop.org> Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r--evtest.c68
1 files changed, 35 insertions, 33 deletions
diff --git a/evtest.c b/evtest.c
index 5cc8505..eb04a51 100644
--- a/evtest.c
+++ b/evtest.c
@@ -551,34 +551,6 @@ static void usage(void)
}
/**
- * Parse the commandline arguments.
- *
- * @return The filename of the device file to open, or NULL in case of
- * error. This string is allocated and must be freed by the caller.
- */
-static char* parse_args(int argc, char **argv)
-{
- char *filename;
-
- if (argc < 2) {
- fprintf(stderr, "No device specified, trying to scan all of %s/%s*\n",
- DEV_INPUT_EVENT, EVENT_DEV_NAME);
-
- if (getuid() != 0)
- fprintf(stderr, "Not running as root, no devices may be available.\n");
-
- filename = scan_devices();
- if (!filename) {
- usage();
- return NULL;
- }
- } else
- filename = strdup(argv[argc - 1]);
-
- return filename;
-}
-
-/**
* Print additional information for absolute axes (min/max, current value,
* etc.).
*
@@ -708,15 +680,35 @@ static int test_grab(int fd)
return rc;
}
-int main (int argc, char **argv)
+/**
+ * Enter capture mode. The requested event device will be monitored, and any
+ * captured events will be decoded and printed on the console.
+ *
+ * @param device The device to monitor, or NULL if the user should be prompted.
+ * @return 0 on success, non-zero on error.
+ */
+static int do_capture(const char *device)
{
int fd;
char *filename;
- filename = parse_args(argc, argv);
+ if (!device) {
+ fprintf(stderr, "No device specified, trying to scan all of %s/%s*\n",
+ DEV_INPUT_EVENT, EVENT_DEV_NAME);
+
+ if (getuid() != 0)
+ fprintf(stderr, "Not running as root, no devices may be available.\n");
+
+ filename = scan_devices();
+ if (!filename) {
+ usage();
+ return EXIT_FAILURE;
+ }
+ } else
+ filename = strdup(device);
if (!filename)
- return 1;
+ return EXIT_FAILURE;
if ((fd = open(filename, O_RDONLY)) < 0) {
perror("evtest");
@@ -724,7 +716,7 @@ int main (int argc, char **argv)
fprintf(stderr, "You do not have access to %s. Try "
"running as root instead.\n",
filename);
- return 1;
+ return EXIT_FAILURE;
}
free(filename);
@@ -733,7 +725,7 @@ int main (int argc, char **argv)
setbuf(stdout, NULL);
if (print_device_info(fd))
- return 1;
+ return EXIT_FAILURE;
printf("Testing ... (interrupt to exit)\n");
@@ -751,4 +743,14 @@ int main (int argc, char **argv)
return print_events(fd);
}
+int main (int argc, char **argv)
+{
+ const char *device = NULL;
+
+ if (argc >= 2)
+ device = argv[1];
+
+ return do_capture(device);
+}
+
/* vim: set noexpandtab tabstop=8 shiftwidth=8: */