Filter kernel key-repeat events

These events can cause problems on some devices, like combination
touch/keyboard devices. In the device we have (Logitech TK820), we get
repeated BTN events that are really just supposed to indicate the
finger count, but end up sending events into the gesture recognizer
without new finger positions, triggering bugs.

Additionally, these events are unneeded, because X does key repeat for
us.

Since these events have no benefit and cause issues, filter them.

BUG=chromium:389178
TEST=TK820 keyboard touchpad doesn't exhibit jittery behavior. Also,
key repeat still works.

Change-Id: Ib8bcdc7db9df8863b996222c12afefa91539c47d
Reviewed-on: https://chromium-review.googlesource.com/205775
Reviewed-by: Dennis Kempin <denniskempin@chromium.org>
Commit-Queue: Andrew de los Reyes <adlr@chromium.org>
Tested-by: Andrew de los Reyes <adlr@chromium.org>
diff --git a/include/libevdev/libevdev.h b/include/libevdev/libevdev.h
index ffce027..3240e7f 100644
--- a/include/libevdev/libevdev.h
+++ b/include/libevdev/libevdev.h
@@ -56,6 +56,7 @@
 struct Evdev_ {
   syn_report_callback syn_report;
   void* syn_report_udata;
+  int got_valid_event;
 
   log_callback log;
   void* log_udata;
diff --git a/src/libevdev_event.c b/src/libevdev_event.c
index cd69ba8..694ed5a 100755
--- a/src/libevdev_event.c
+++ b/src/libevdev_event.c
@@ -53,6 +53,8 @@
 
 static void Event_Get_Time(struct timeval*, bool);
 
+static int Event_Is_Valid(struct input_event*);
+
 const char*
 Evdev_Get_Version() {
     return VCSID;
@@ -465,6 +467,12 @@
 Event_Process(EvdevPtr device, struct input_event* ev)
 {
     Event_Print(device, ev);
+    if (Event_Is_Valid(ev)) {
+        if (!(ev->type == EV_SYN && ev->code == SYN_REPORT))
+            device->got_valid_event = 1;
+    } else {
+        return false;
+    }
 
     switch (ev->type) {
     case EV_SYN:
@@ -587,11 +595,13 @@
 Event_Syn_Report(EvdevPtr device, struct input_event* ev)
 {
     EventStatePtr evstate = device->evstate;
-    device->syn_report(device->syn_report_udata, evstate, &ev->time);
+    if (device->got_valid_event)
+        device->syn_report(device->syn_report_udata, evstate, &ev->time);
 
     MT_Print_Slots(device);
 
     Event_Clear_Ev_Rel_State(device);
+    device->got_valid_event = 0;
 }
 
 static void
@@ -686,3 +696,12 @@
         break;
     }
 }
+
+static int Event_Is_Valid(struct input_event* ev)
+{
+    /* Key repeats are invalid. They're handled by X anyway */
+    if (ev->type == EV_KEY &&
+        ev->value == 2)
+        return 0;
+    return 1;
+}