From b472cf36a65c3a7eeb17c3860ecead85e67e56cd Mon Sep 17 00:00:00 2001 From: Joseph Hwang Date: Fri, 23 Jan 2015 16:22:16 +0800 Subject: [PATCH] add grab flag This patch grabs the device when --grab command line option is present. In this way, no other processes are able to access the device. TEST=Check that the cursor is frozen when --grab option is specified. $ evtest --grab --- evtest.c | 38 +++++++++++++++++++++++++++++++++++--- evtest.txt | 6 +++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/evtest.c b/evtest.c index 74230ea..6a2b7a1 100644 --- a/evtest.c +++ b/evtest.c @@ -55,6 +55,10 @@ #include #include #include +#include +#include +#include +#include #define BITS_PER_LONG (sizeof(long) * 8) #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) @@ -93,6 +97,17 @@ static const struct query_mode { { "EV_SW", EV_SW, SW_MAX, EVIOCGSW(SW_MAX) }, }; +static int grab_flag = 0; +static volatile int stop = 0; + +/** + * An interrupt handler to ungrab the device. + */ +static void interrupt_handler(void) +{ + stop = 1; +} + /** * Look up an entry in the query_modes table by its textual name. * @@ -675,8 +690,9 @@ static int version(void) static int usage(void) { printf("USAGE:\n"); - printf(" Grab mode:\n"); - printf(" %s /dev/input/eventX\n", program_invocation_short_name); + printf(" Capture mode:\n"); + printf(" %s [--grab] /dev/input/eventX\n", program_invocation_short_name); + printf(" --grab grab the device for exclusive access\n"); printf("\n"); printf(" Query mode: (check exit code)\n"); printf(" %s --query /dev/input/eventX \n", @@ -768,8 +784,15 @@ static int print_events(int fd) { struct input_event ev[64]; int i, rd; + fd_set rdfs; + + FD_ZERO(&rdfs); + FD_SET(fd, &rdfs); while (1) { + select(fd + 1, &rdfs, NULL, NULL, NULL); + if (stop) + break; rd = read(fd, ev, sizeof(struct input_event) * 64); if (rd < (int) sizeof(struct input_event)) { @@ -800,6 +823,9 @@ static int print_events(int fd) } } + + ioctl(fd, EVIOCGRAB, (void*)0); + return EXIT_SUCCESS; } /** @@ -814,7 +840,7 @@ static int test_grab(int fd) rc = ioctl(fd, EVIOCGRAB, (void*)1); - if (!rc) + if (!rc && !grab_flag) ioctl(fd, EVIOCGRAB, (void*)0); return rc; @@ -878,6 +904,9 @@ static int do_capture(const char *device) printf("***********************************************\n"); } + signal(SIGINT, interrupt_handler); + signal(SIGTERM, interrupt_handler); + return print_events(fd); } @@ -954,6 +983,7 @@ static int do_query(const char *device, const char *event_type, const char *keyn } static const struct option long_options[] = { + { "grab", no_argument, &grab_flag, 1 }, { "query", no_argument, NULL, MODE_QUERY }, { "version", no_argument, NULL, MODE_VERSION }, { 0, }, @@ -972,6 +1002,8 @@ int main (int argc, char **argv) if (c == -1) break; switch (c) { + case 0: + break; case MODE_QUERY: mode = c; break; diff --git a/evtest.txt b/evtest.txt index b614482..0a8d3cb 100644 --- a/evtest.txt +++ b/evtest.txt @@ -8,7 +8,7 @@ NAME SYNOPSIS -------- - evtest /dev/input/eventX + evtest [--grab] /dev/input/eventX evtest --query /dev/input/eventX @@ -19,6 +19,10 @@ display information about the specified input device, including all the events supported by the device. It then monitors the device and displays all the events layer events generated. +If the grab flag is specified in the capture mode, evtest would grab +the device for exclusive access during its execution period. No other +processes are allowed to access the device simultaneously in this case. + In the second invocation type ("query mode"), evtest performs a one-shot query of the state of a specific key *value* of an event *type*. -- 2.2.0.rc0.207.ga3a616c