vnc: Remove EventQueue

Put the VNCFlinger in the screenData so that it can be accessed
by clients. This removes the need for the message queue. The
buffer ready event was removed in a previous commit.
diff --git a/Android.mk b/Android.mk
index b6b8b26..e101f3b 100644
--- a/Android.mk
+++ b/Android.mk
@@ -4,7 +4,6 @@
 
 LOCAL_SRC_FILES := \
     src/EglWindow.cpp \
-    src/EventQueue.cpp \
     src/InputDevice.cpp \
     src/Program.cpp \
     src/VirtualDisplay.cpp \
diff --git a/src/EventQueue.cpp b/src/EventQueue.cpp
deleted file mode 100644
index a107f30..0000000
--- a/src/EventQueue.cpp
+++ /dev/null
@@ -1,59 +0,0 @@
-#define LOG_TAG "VNC-EventQueue"
-#include <utils/Log.h>
-
-#include "EventQueue.h"
-
-using namespace android;
-
-void EventQueue::enqueue(const Event& event) {
-    mQueue.push(event);
-    ALOGV("enqueue: mId=%d mData=%p qlen=%zu", event.mId, event.mData, mQueue.size());
-
-    Mutex::Autolock _l(mMutex);
-    mCondition.broadcast();
-}
-
-void EventQueue::await() {
-    Mutex::Autolock _l(mMutex);
-
-    while (mRunning) {
-        ALOGV("begin wait");
-        mCondition.wait(mMutex);
-
-        ALOGV("queue active");
-        while (!mQueue.empty()) {
-            Event event = mQueue.front();
-            mQueue.pop();
-
-            mMutex.unlock();
-            for (std::vector<EventListener *>::iterator it = mListeners.begin();
-                    it != mListeners.end(); ++it) {
-                ALOGV("call listener: %p", *it);
-                (*it)->onEvent(event);
-            }
-            mMutex.lock();
-
-        }
-    }
-}
-
-void EventQueue::shutdown() {
-    Mutex::Autolock _l(mMutex);
-    flush();
-    mRunning = false;
-    mCondition.broadcast();
-}
-
-void EventQueue::flush() {
-    Mutex::Autolock _l(mMutex);
-    mQueue = {};
-}
-
-void EventQueue::addListener(EventListener *listener) {
-    mListeners.push_back(listener);
-    ALOGV("addListener: %p", listener);
-}
-
-void EventQueue::removeListener(EventListener *listener) {
-    mListeners.erase(std::remove(mListeners.begin(), mListeners.end(), listener), mListeners.end());
-}
diff --git a/src/EventQueue.h b/src/EventQueue.h
deleted file mode 100644
index e926831..0000000
--- a/src/EventQueue.h
+++ /dev/null
@@ -1,67 +0,0 @@
-#ifndef MQ_H
-#define MQ_H
-
-#include <queue>
-#include <vector>
-
-#include <utils/RefBase.h>
-#include <utils/Thread.h>
-
-#define EVENT_CLIENT_CONNECT 0
-#define EVENT_CLIENT_GONE 1
-#define EVENT_BUFFER_READY 2
-
-namespace android {
-
-class Event {
-public:
-    Event(unsigned int id, void* data = 0):
-        mId(id),
-        mData(data) {}
-
-    Event(const Event& ev):
-        mId(ev.mId),
-        mData(ev.mData) {}
-
-    unsigned int mId;
-    void* mData;
-};
-
-class EventListener {
-public:
-    virtual ~EventListener() {}
-
-    virtual void onEvent(const Event& event) = 0;
-};
-
-class EventQueue : public RefBase {
-public:
-    EventQueue():
-        mRunning(true) {}
-
-    virtual void await();
-
-    virtual void shutdown();
-
-    virtual void enqueue(const Event& event);
-
-    virtual void flush();
-
-    virtual void addListener(EventListener *listener);
-
-    virtual void removeListener(EventListener *listener);
-
-    virtual ~EventQueue() { }
-
-private:
-    Mutex mMutex;
-    Condition mCondition;
-
-    std::queue<const Event> mQueue;
-    std::vector<EventListener *> mListeners;
-
-    bool mRunning;
-};
-
-};
-#endif
diff --git a/src/VNCFlinger.cpp b/src/VNCFlinger.cpp
index dfb1a38..70f741a 100644
--- a/src/VNCFlinger.cpp
+++ b/src/VNCFlinger.cpp
@@ -14,8 +14,6 @@
 
 using namespace android;
 
-EventQueue *VNCFlinger::sQueue = new EventQueue();
-
 status_t VNCFlinger::start() {
     Mutex::Autolock _l(mMutex);
 
@@ -28,7 +26,8 @@
     ALOGD("VNCFlinger is running!");
 
     rfbRunEventLoop(mVNCScreen, -1, true);
-    sQueue->await();
+
+    mCondition.wait(mMutex);
 
     release_l();
     return NO_ERROR;
@@ -56,8 +55,6 @@
 
     ALOGD("Display dimensions: %dx%d rotated=%d", mWidth, mHeight, rotated);
 
-    sQueue->addListener(this);
-
     rfbLog = VNCFlinger::rfbLogger;
     rfbErr = VNCFlinger::rfbLogger;
 
@@ -81,6 +78,7 @@
     mVNCScreen->serverFormat.bitsPerPixel = 32;
     mVNCScreen->handleEventsEagerly = true;
     mVNCScreen->deferUpdateTime = 16;
+    mVNCScreen->screenData = this;
 
     rfbInitServer(mVNCScreen);
 
@@ -94,7 +92,6 @@
 }
 
 void VNCFlinger::release_l() {
-    sQueue->removeListener(this);
     mVirtualDisplay.clear();
 
     ALOGD("VNCFlinger released");
@@ -102,70 +99,51 @@
 
 status_t VNCFlinger::stop() {
     Mutex::Autolock _l(mMutex);
-    sQueue->shutdown();
+    mCondition.signal();
 
     return NO_ERROR;
 }
 
-void VNCFlinger::onEvent(const Event& event) {
-
-    ALOGV("onEvent: mId=%d mData=%p", event.mId, event.mData);
-
-    switch(event.mId) {
-        case EVENT_CLIENT_CONNECT:
-            if (mClientCount == 0) {
-                InputDevice::start(mWidth, mHeight);
-                mVirtualDisplay->start(mMainDpyInfo, sQueue);
-            }
-            mClientCount++;
-
-            ALOGI("Client connected (%zu)", mClientCount);
-            break;
-
-        case EVENT_CLIENT_GONE:
-            if (mClientCount > 0) {
-                mClientCount--;
-                if (mClientCount == 0) {
-                    mVirtualDisplay->stop();
-                    InputDevice::stop();
-                }
-            }
-
-            ALOGI("Client disconnected (%zu)", mClientCount);
-            break;
-
-        /*
-        case EVENT_BUFFER_READY:
-            int64_t startWhenNsec, endWhenNsec;
-            startWhenNsec = systemTime(CLOCK_MONOTONIC);
-            if (event.mData == NULL) {
-                break;
-            }
-            //memcpy(mVNCBuf, (uint8_t *) event.mData, mWidth * mHeight * 4);
-            //mVNCScreen->frameBuffer = (char *) event.mData;
-            rfbMarkRectAsModified(mVNCScreen, 0, 0, mWidth, mHeight);
-            endWhenNsec = systemTime(CLOCK_MONOTONIC);
-            ALOGV("got pixels (mark=%.3fms)",
-                    (endWhenNsec - startWhenNsec) / 1000000.0);
-            break;
-        */
-
-        default:
-            ALOGE("Unhandled event: %d", event.mId);
-            break;
+size_t VNCFlinger::addClient() {
+    Mutex::Autolock _l(mMutex);
+    if (mClientCount == 0) {
+        InputDevice::start(mWidth, mHeight);
+        mVirtualDisplay->start(mMainDpyInfo);
     }
+    mClientCount++;
+
+    ALOGI("Client connected (%zu)", mClientCount);
+
+    return mClientCount;
 }
 
-ClientGoneHookPtr VNCFlinger::onClientGone(rfbClientPtr /* cl */) {
+size_t VNCFlinger::removeClient() {
+    Mutex::Autolock _l(mMutex);
+    if (mClientCount > 0) {
+        mClientCount--;
+        if (mClientCount == 0) {
+            mVirtualDisplay->stop();
+            InputDevice::stop();
+        }
+    }
+
+    ALOGI("Client disconnected (%zu)", mClientCount);
+
+    return mClientCount;
+}
+
+ClientGoneHookPtr VNCFlinger::onClientGone(rfbClientPtr cl) {
     ALOGV("onClientGone");
-    sQueue->enqueue(Event(EVENT_CLIENT_GONE));
+    VNCFlinger *vf = (VNCFlinger *)cl->screen->screenData;
+    vf->removeClient();
     return 0;
 }
 
 enum rfbNewClientAction VNCFlinger::onNewClient(rfbClientPtr cl) {
     ALOGV("onNewClient");
     cl->clientGoneHook = (ClientGoneHookPtr) VNCFlinger::onClientGone;
-    sQueue->enqueue(Event(EVENT_CLIENT_CONNECT));
+    VNCFlinger *vf = (VNCFlinger *)cl->screen->screenData;
+    vf->addClient();
     return RFB_CLIENT_ACCEPT;
 }
 
diff --git a/src/VNCFlinger.h b/src/VNCFlinger.h
index 45057b5..c1e38ba 100644
--- a/src/VNCFlinger.h
+++ b/src/VNCFlinger.h
@@ -1,7 +1,6 @@
 #ifndef VNCFLINGER_H
 #define VNCFLINGER_H
 
-#include "EventQueue.h"
 #include "VirtualDisplay.h"
 
 #include <ui/DisplayInfo.h>
@@ -12,7 +11,7 @@
 
 namespace android {
 
-class VNCFlinger : public EventListener {
+class VNCFlinger {
 public:
     VNCFlinger(int argc, char **argv) :
             mArgc(argc),
@@ -20,21 +19,25 @@
             mClientCount(0) {
     }
 
-    virtual void onEvent(const Event& event);
+    virtual ~VNCFlinger() {}
 
     virtual status_t start();
     virtual status_t stop();
 
-    static EventQueue *sQueue;
+    virtual size_t addClient();
+    virtual size_t removeClient();
 
 private:
+
     virtual status_t setup_l();
     virtual void release_l();
 
     static ClientGoneHookPtr onClientGone(rfbClientPtr cl);
     static enum rfbNewClientAction onNewClient(rfbClientPtr cl);
     static void rfbLogger(const char *format, ...);
-        
+
+    Condition mCondition;
+
     rfbScreenInfoPtr mVNCScreen;
     uint8_t *mVNCBuf;
 
diff --git a/src/VirtualDisplay.cpp b/src/VirtualDisplay.cpp
index 7a95042..b44c64c 100644
--- a/src/VirtualDisplay.cpp
+++ b/src/VirtualDisplay.cpp
@@ -119,12 +119,10 @@
     return NO_ERROR;
 }
 
-status_t VirtualDisplay::start(const DisplayInfo& mainDpyInfo, EventQueue *queue) {
+status_t VirtualDisplay::start(const DisplayInfo& mainDpyInfo) {
 
     Mutex::Autolock _l(mMutex);
 
-    mQueue = queue;
-
     mRotate = isDeviceRotated(mainDpyInfo.orientation);
     mWidth = mRotate ? mainDpyInfo.h : mainDpyInfo.w;
     mHeight = mRotate ? mainDpyInfo.w : mainDpyInfo.h;
@@ -185,10 +183,7 @@
     while (mState == RUNNING) {
         mEventCond.wait(mMutex);
         ALOGD("Awake, frame available");
-        void* ptr = processFrame_l();
-
-        //const Event ev(EVENT_BUFFER_READY, ptr);
-        //mQueue->enqueue(ev);
+        processFrame_l();
     }
 
     ALOGV("VDS thread stopping");
diff --git a/src/VirtualDisplay.h b/src/VirtualDisplay.h
index c215395..90f1138 100644
--- a/src/VirtualDisplay.h
+++ b/src/VirtualDisplay.h
@@ -17,7 +17,6 @@
 #ifndef VDS_H
 #define VDS_H
 
-#include "EventQueue.h"
 #include "Program.h"
 #include "EglWindow.h"
 
@@ -48,7 +47,7 @@
     // Create an "input surface", similar in purpose to a MediaCodec input
     // surface, that the virtual display can send buffers to.  Also configures
     // EGL with a pbuffer surface on the current thread.
-    virtual status_t start(const DisplayInfo& mainDpyInfo, EventQueue *queue);
+    virtual status_t start(const DisplayInfo& mainDpyInfo);
 
     virtual status_t stop();
 
@@ -86,8 +85,6 @@
     uint32_t mHeight, mWidth;
     bool mRotate;
 
-    EventQueue *mQueue;
-
     // Used to wait for the FrameAvailableListener callback.
     Mutex mMutex;
 
@@ -96,6 +93,7 @@
 
     // Thread status, mostly useful during startup.
     status_t mThreadResult;
+
     // Overlay thread state.  States advance from left to right; object may
     // not be restarted.
     enum { UNINITIALIZED, INIT, RUNNING, STOPPING, STOPPED } mState;
@@ -123,7 +121,6 @@
 
     // Pixel data buffers.
     size_t mBufSize;
-    uint8_t* mPixelBuf;
     GLuint* mPBO;
     unsigned int mIndex;