Bug 932698 - hold a wakelock when we receive key events. r=mwu
authorViral Wang <vwang@mozilla.com>
Fri, 17 Oct 2014 03:44:00 +0200
changeset 211187 6250e897cd5dc474167a52d80310800576bdd79c
parent 211186 27bc3dbcaff532c3f91eb7eea4e4ff38152e7edd
child 211188 d2cbf090111f66eb9f5265578ba0ab855ec4fff9
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmwu
bugs932698
milestone36.0a1
Bug 932698 - hold a wakelock when we receive key events. r=mwu
widget/gonk/nsAppShell.cpp
--- a/widget/gonk/nsAppShell.cpp
+++ b/widget/gonk/nsAppShell.cpp
@@ -17,16 +17,17 @@
 
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
 
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <hardware_legacy/power.h>
 #include <signal.h>
 #include <sys/epoll.h>
 #include <sys/ioctl.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <utils/BitSet.h>
@@ -95,16 +96,17 @@ static nsAppShell *gAppShell = nullptr;
 static int epollfd = 0;
 static int signalfds[2] = {0};
 static bool sDevInputAudioJack;
 static int32_t sHeadphoneState;
 static int32_t sMicrophoneState;
 
 // Amount of time in MS before an input is considered expired.
 static const uint64_t kInputExpirationThresholdMs = 1000;
+static const char kKey_WAKE_LOCK_ID[] = "GeckoKeyEvent";
 
 NS_IMPL_ISUPPORTS_INHERITED(nsAppShell, nsBaseAppShell, nsIObserver)
 
 static uint64_t
 nanosecsToMillisecs(nsecs_t nsecs)
 {
     return nsecs / 1000000;
 }
@@ -490,16 +492,17 @@ protected:
 
 class GeckoInputDispatcher : public InputDispatcherInterface {
 public:
     GeckoInputDispatcher(sp<EventHub> &aEventHub)
         : mQueueLock("GeckoInputDispatcher::mQueueMutex")
         , mEventHub(aEventHub)
         , mKeyDownCount(0)
         , mKeyEventsFiltered(false)
+        , mPowerWakelock(false)
     {
         mTouchDispatcher = new GeckoTouchDispatcher();
     }
 
     virtual void dump(String8& dump);
 
     virtual void monitor() {}
 
@@ -540,16 +543,17 @@ private:
     // popped and dispatched on the main thread.
     mozilla::Mutex mQueueLock;
     std::queue<UserInputData> mEventQueue;
     sp<EventHub> mEventHub;
     nsRefPtr<GeckoTouchDispatcher> mTouchDispatcher;
 
     int mKeyDownCount;
     bool mKeyEventsFiltered;
+    bool mPowerWakelock;
 };
 
 // GeckoInputReaderPolicy
 void
 GeckoInputReaderPolicy::setDisplayInfo()
 {
     static_assert(nsIScreen::ROTATION_0_DEG ==
                   DISPLAY_ORIENTATION_0,
@@ -631,16 +635,21 @@ GeckoInputDispatcher::dispatchOnce()
         }
 
         sp<KeyCharacterMap> kcm = mEventHub->getKeyCharacterMap(data.deviceId);
         KeyEventDispatcher dispatcher(data, kcm.get());
         dispatcher.Dispatch();
         break;
     }
     }
+    MutexAutoLock lock(mQueueLock);
+    if (mPowerWakelock && mEventQueue.empty()) {
+        release_wake_lock(kKey_WAKE_LOCK_ID);
+        mPowerWakelock = false;
+    }
 }
 
 void
 GeckoInputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs*)
 {
 }
 
 void
@@ -653,16 +662,20 @@ GeckoInputDispatcher::notifyKey(const No
     data.flags = args->flags;
     data.metaState = args->metaState;
     data.deviceId = args->deviceId;
     data.key.keyCode = args->keyCode;
     data.key.scanCode = args->scanCode;
     {
         MutexAutoLock lock(mQueueLock);
         mEventQueue.push(data);
+        if (!mPowerWakelock) {
+            mPowerWakelock =
+                acquire_wake_lock(PARTIAL_WAKE_LOCK, kKey_WAKE_LOCK_ID);
+        }
     }
     gAppShell->NotifyNativeEvent();
 }
 
 static void
 addMultiTouch(MultiTouchInput& aMultiTouch,
                                     const NotifyMotionArgs* args, int aIndex)
 {