Add support for mouse hover and scroll wheel.

Dispatch ACTION_HOVER_MOVE and ACTION_SCROLL through the View
hierarchy as onGenericTouchEvent.  Pointer events dispatched
this way are delivered to the view under the pointer.  Non-pointer
events continue to be delivered to the focused view.

Added scroll wheel support to AbsListView, ScrollView,
HorizontalScrollView and WebView.  Shift+VSCROLL is translated
to HSCROLL as appropriate.

Added logging of new pointer events in PointerLocationView.

Fixed a problem in EventHub when a USB device is removed that
resulted in a long stream of ENODEV errors being logged until INotify
noticed the device was gone.

Note that the new events are not supported by wallpapers at this time
because the wallpaper engine only delivers touch events.

Make all mouse buttons behave identically.  (Effectively we only
support one button.)

Change-Id: I9ab445ffb63c813fcb07db6693987b02475f3756
diff --git a/services/input/EventHub.cpp b/services/input/EventHub.cpp
index b31381a..25db25e 100644
--- a/services/input/EventHub.cpp
+++ b/services/input/EventHub.cpp
@@ -508,6 +508,7 @@
         }
 
         // Grab the next input event.
+        bool deviceWasRemoved = false;
         for (;;) {
             // Consume buffered input events, if any.
             if (mInputBufferIndex < mInputBufferCount) {
@@ -558,6 +559,10 @@
                 int32_t readSize = read(pfd.fd, mInputBufferData,
                         sizeof(struct input_event) * INPUT_BUFFER_SIZE);
                 if (readSize < 0) {
+                    if (errno == ENODEV) {
+                        deviceWasRemoved = true;
+                        break;
+                    }
                     if (errno != EAGAIN && errno != EINTR) {
                         LOGW("could not get event (errno=%d)", errno);
                     }
@@ -570,6 +575,13 @@
             }
         }
 
+        // Handle the case where a device has been removed but INotify has not yet noticed.
+        if (deviceWasRemoved) {
+            AutoMutex _l(mLock);
+            closeDeviceAtIndexLocked(mInputFdIndex);
+            continue; // report added or removed devices immediately
+        }
+
 #if HAVE_INOTIFY
         // readNotify() will modify mFDs and mFDCount, so this must be done after
         // processing all other events.
@@ -580,8 +592,6 @@
         }
 #endif
 
-        mInputFdIndex = 0;
-
         // Poll for events.  Mind the wake lock dance!
         // We hold a wake lock at all times except during poll().  This works due to some
         // subtle choreography.  When a device driver has pending (unread) events, it acquires
@@ -602,6 +612,9 @@
                 usleep(100000);
             }
         }
+
+        // Prepare to process all of the FDs we just polled.
+        mInputFdIndex = 0;
     }
 }
 
@@ -1008,37 +1021,42 @@
     for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
         Device* device = mDevices[i];
         if (device->path == devicePath) {
-            LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
-                 device->path.string(), device->identifier.name.string(), device->id,
-                 device->fd, device->classes);
-
-            for (int j=0; j<EV_SW; j++) {
-                if (mSwitches[j] == device->id) {
-                    mSwitches[j] = 0;
-                }
-            }
-
-            if (device->id == mBuiltInKeyboardId) {
-                LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
-                        device->path.string(), mBuiltInKeyboardId);
-                mBuiltInKeyboardId = -1;
-                clearKeyboardProperties(device, true);
-            }
-            clearKeyboardProperties(device, false);
-
-            mFds.removeAt(i);
-            mDevices.removeAt(i);
-            device->close();
-
-            device->next = mClosingDevices;
-            mClosingDevices = device;
-            return 0;
+            return closeDeviceAtIndexLocked(i);
         }
     }
-    LOGE("remove device: %s not found\n", devicePath);
+    LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
     return -1;
 }
 
+int EventHub::closeDeviceAtIndexLocked(int index) {
+    Device* device = mDevices[index];
+    LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
+         device->path.string(), device->identifier.name.string(), device->id,
+         device->fd, device->classes);
+
+    for (int j=0; j<EV_SW; j++) {
+        if (mSwitches[j] == device->id) {
+            mSwitches[j] = 0;
+        }
+    }
+
+    if (device->id == mBuiltInKeyboardId) {
+        LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
+                device->path.string(), mBuiltInKeyboardId);
+        mBuiltInKeyboardId = -1;
+        clearKeyboardProperties(device, true);
+    }
+    clearKeyboardProperties(device, false);
+
+    mFds.removeAt(index);
+    mDevices.removeAt(index);
+    device->close();
+
+    device->next = mClosingDevices;
+    mClosingDevices = device;
+    return 0;
+}
+
 int EventHub::readNotify(int nfd) {
 #ifdef HAVE_INOTIFY
     int res;