[PATCH evtest 1/2] Factor out capture setup code into its own function

Daniel Drake dsd at laptop.org
Sat Jul 16 15:06:54 PDT 2011


This is in preparation for a followup patch which adds a one-shot
query mode.

Signed-off-by: Daniel Drake <dsd at laptop.org>
---
 evtest.c |   68 +++++++++++++++++++++++++++++++------------------------------
 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: */
-- 
1.7.6



More information about the xorg-devel mailing list