From b78b0276e04b949a66cffbed9239184d73badf2a Mon Sep 17 00:00:00 2001 From: Joseph Hwang Date: Tue, 13 Jan 2015 16:49:51 +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. BUG=chromium:443539 TEST=Check that the cursor is frozen when --grab option is specified. $ evtest --grab --- evtest.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/evtest.c b/evtest.c index 74230ea..6dbe9b5 100644 --- a/evtest.c +++ b/evtest.c @@ -55,6 +55,7 @@ #include #include #include +#include #define BITS_PER_LONG (sizeof(long) * 8) #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) @@ -93,6 +94,19 @@ static const struct query_mode { { "EV_SW", EV_SW, SW_MAX, EVIOCGSW(SW_MAX) }, }; +static int fd; +static int grab_flag = 0; + +/** + * An interrupt handler to ungrab the device. + */ +static void ungrab(void) +{ + if (grab_flag) + ioctl(fd, EVIOCGRAB, (void*)0); + exit(EXIT_SUCCESS); +} + /** * Look up an entry in the query_modes table by its textual name. * @@ -675,9 +689,12 @@ static int version(void) static int usage(void) { printf("USAGE:\n"); - printf(" Grab mode:\n"); + printf(" Capture mode:\n"); printf(" %s /dev/input/eventX\n", program_invocation_short_name); printf("\n"); + printf(" Capture mode with the device grabbed:\n"); + printf(" %s --grab /dev/input/eventX\n", program_invocation_short_name); + printf("\n"); printf(" Query mode: (check exit code)\n"); printf(" %s --query /dev/input/eventX \n", program_invocation_short_name); @@ -814,7 +831,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; @@ -829,7 +846,6 @@ static int test_grab(int fd) */ static int do_capture(const char *device) { - int fd; char *filename; if (!device) { @@ -954,6 +970,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, }, @@ -966,12 +983,17 @@ int main (int argc, char **argv) const char *event_type; enum evtest_mode mode = MODE_CAPTURE; + signal(SIGINT, ungrab); + signal(SIGTERM, ungrab); + while (1) { int option_index = 0; int c = getopt_long(argc, argv, "", long_options, &option_index); if (c == -1) break; switch (c) { + case 0: + break; case MODE_QUERY: mode = c; break; -- 2.2.0.rc0.207.ga3a616c