summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2017-02-10 18:39:27 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2017-02-10 18:39:27 +0100
commit09e923d965a86a944dad7d77b336721386f6bf63 (patch)
tree1000abf99b16bb6d70d8d88f1cd26096b634b027
parent53fa1c76c27726670f2724793d867d817c029709 (diff)
Return a list of events form native window instead of one event at a time.
-rw-r--r--plugingui/eventhandler.cc21
-rw-r--r--plugingui/eventhandler.h3
-rw-r--r--plugingui/nativewindow_win32.cc80
-rw-r--r--plugingui/nativewindow_win32.h5
-rw-r--r--plugingui/nativewindow_x11.cc67
-rw-r--r--plugingui/nativewindow_x11.h6
6 files changed, 43 insertions, 139 deletions
diff --git a/plugingui/eventhandler.cc b/plugingui/eventhandler.cc
index a5249a6..9643d3f 100644
--- a/plugingui/eventhandler.cc
+++ b/plugingui/eventhandler.cc
@@ -40,23 +40,38 @@ EventHandler::EventHandler(NativeWindow& nativeWindow, Window& window)
bool EventHandler::hasEvent()
{
- return nativeWindow.hasEvent();
+ return !events.empty();
}
std::shared_ptr<Event> EventHandler::getNextEvent()
{
- return nativeWindow.getNextEvent();
+ if(events.empty())
+ {
+ return nullptr;
+ }
+
+ auto event = events.front();
+ events.pop();
+ return event;
}
std::shared_ptr<Event> EventHandler::peekNextEvent()
{
- return nativeWindow.peekNextEvent();
+ if(events.empty())
+ {
+ return nullptr;
+ }
+
+ auto event = events.front();
+ return event;
}
void EventHandler::processEvents()
{
Painter p(window); // Make sure we only redraw buffer one time.
+ events = nativeWindow.getEvents();
+
while(hasEvent())
{
auto event = getNextEvent();
diff --git a/plugingui/eventhandler.h b/plugingui/eventhandler.h
index 6269baf..31ab3e2 100644
--- a/plugingui/eventhandler.h
+++ b/plugingui/eventhandler.h
@@ -28,6 +28,7 @@
#include <notifier.h>
#include <memory>
+#include <queue>
#include "guievent.h"
#include "nativewindow.h"
@@ -62,6 +63,8 @@ private:
// Used to ignore mouse button release after a double click.
bool lastWasDoubleClick;
+
+ std::queue<std::shared_ptr<Event>> events;
};
} // GUI::
diff --git a/plugingui/nativewindow_win32.cc b/plugingui/nativewindow_win32.cc
index b81d121..3065b8c 100644
--- a/plugingui/nativewindow_win32.cc
+++ b/plugingui/nativewindow_win32.cc
@@ -30,7 +30,8 @@
#include "window.h"
-namespace GUI {
+namespace GUI
+{
LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
WPARAM wp, LPARAM lp)
@@ -417,79 +418,18 @@ void NativeWindowWin32::grabMouse(bool grab)
}
}
-bool NativeWindowWin32::hasEvent()
-{
- if(!event_queue.empty())
- {
- return true;
- }
-
- // Parented windows have their event loop somewhere else.
- if(parent_window == nullptr)
- {
- MSG msg;
- return PeekMessage(&msg, m_hwnd, 0, 0, PM_NOREMOVE) != 0;
- }
-
- return false;
-}
-
-std::shared_ptr<Event> NativeWindowWin32::getNextEvent()
+std::queue<std::shared_ptr<Event>> NativeWindowWin32::getEvents()
{
- if(!event_queue.empty())
- {
- auto event = event_queue.front();
- event_queue.pop();
- return event;
- }
-
- // Parented windows have their event loop somewhere else.
- if(parent_window == nullptr)
- {
- MSG msg;
- if(GetMessage(&msg, m_hwnd, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
-
- if(event_queue.empty())
- {
- return nullptr;
- }
-
- auto event = event_queue.front();
- event_queue.pop();
- return event;
-}
-
-std::shared_ptr<Event> NativeWindowWin32::peekNextEvent()
-{
- if(!event_queue.empty())
- {
- auto event = event_queue.front();
- return event;
- }
-
- // Parented windows have their event loop somewhere else.
- if(parent_window == nullptr)
- {
- MSG msg;
- if(PeekMessage(&msg, m_hwnd, 0, 0, PM_NOREMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
-
- if(event_queue.empty())
+ MSG msg;
+ while(PeekMessage(&msg, m_hwnd, 0, 0, PM_REMOVE) != 0)
{
- return nullptr;
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
}
- auto event = event_queue.front();
- return event;
+ std::queue<std::shared_ptr<Event>> events;
+ std::swap(events, event_queue);
+ return events;
}
} // GUI::
diff --git a/plugingui/nativewindow_win32.h b/plugingui/nativewindow_win32.h
index 8282c6f..1ed072c 100644
--- a/plugingui/nativewindow_win32.h
+++ b/plugingui/nativewindow_win32.h
@@ -55,10 +55,7 @@ public:
void handleBuffer() override;
void redraw() override;
void grabMouse(bool grab) override;
-
- bool hasEvent() override;
- std::shared_ptr<Event> getNextEvent() override;
- std::shared_ptr<Event> peekNextEvent() override;
+ std::queue<std::shared_ptr<Event>> getEvents() override;
private:
static LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
diff --git a/plugingui/nativewindow_x11.cc b/plugingui/nativewindow_x11.cc
index f50e01d..3fffcd5 100644
--- a/plugingui/nativewindow_x11.cc
+++ b/plugingui/nativewindow_x11.cc
@@ -264,52 +264,21 @@ void NativeWindowX11::grabMouse(bool grab)
// Don't need to do anything on this platform...
}
-bool NativeWindowX11::hasEvent()
+std::queue<std::shared_ptr<Event>> NativeWindowX11::getEvents()
{
- if(display == nullptr)
- {
- return false;
- }
-
- return XPending(display);
-}
-
-std::shared_ptr<Event> NativeWindowX11::getNextEvent()
-{
- if(display == nullptr)
- {
- return nullptr;
- }
-
- if(!XPending(display))
- {
- return nullptr;
- }
-
- XEvent xEvent;
- XNextEvent(display, &xEvent);
- return translateXMessage(xEvent);
-}
+ std::queue<std::shared_ptr<Event>> events;
-std::shared_ptr<Event> NativeWindowX11::peekNextEvent()
-{
- if(display == nullptr)
+ while(XPending(display))
{
- return nullptr;
+ XEvent xEvent;
+ XNextEvent(display, &xEvent);
+ events.push(translateXMessage(xEvent));
}
- if(!XPending(display))
- {
- return nullptr;
- }
-
- XEvent peekXEvent;
- XPeekEvent(display, &peekXEvent);
- return translateXMessage(peekXEvent, true);
+ return events;
}
-std::shared_ptr<Event> NativeWindowX11::translateXMessage(XEvent& xevent,
- bool peek)
+std::shared_ptr<Event> NativeWindowX11::translateXMessage(XEvent& xevent)
{
std::shared_ptr<Event> event = nullptr;
@@ -327,15 +296,6 @@ std::shared_ptr<Event> NativeWindowX11::translateXMessage(XEvent& xevent,
case Expose:
//DEBUG(x11, "Expose");
- //if(!peek)
- //{
- // XEvent peekXEvent;
- // while(XCheckWindowEvent(display, xwindow, Expose, &peekXEvent))
- // {
- // xevent.xexpose = peekXEvent.xexpose;
- // }
- //}
-
if(xevent.xexpose.count == 0)
{
auto repaintEvent = std::make_shared<RepaintEvent>();
@@ -351,15 +311,6 @@ std::shared_ptr<Event> NativeWindowX11::translateXMessage(XEvent& xevent,
case ConfigureNotify:
//DEBUG(x11, "ConfigureNotify");
{
- //if(!peek)
- //{
- // XEvent peekXEvent;
- // while(XCheckWindowEvent(display, xwindow, ConfigureNotify, &peekXEvent))
- // {
- // xevent.xconfigure = peekXEvent.xconfigure;
- // }
- //}
-
if((window.width() != (std::size_t)xevent.xconfigure.width) ||
(window.height() != (std::size_t)xevent.xconfigure.height))
{
@@ -435,7 +386,7 @@ std::shared_ptr<Event> NativeWindowX11::translateXMessage(XEvent& xevent,
(xevent.type == ButtonPress) &&
((xevent.xbutton.time - last_click) < 200);
- if(!peek && (xevent.type == ButtonPress))
+ if(xevent.type == ButtonPress)
{
last_click = xevent.xbutton.time;
}
diff --git a/plugingui/nativewindow_x11.h b/plugingui/nativewindow_x11.h
index 10c2026..abc8af0 100644
--- a/plugingui/nativewindow_x11.h
+++ b/plugingui/nativewindow_x11.h
@@ -55,12 +55,10 @@ public:
void handleBuffer() override;
void redraw() override;
void grabMouse(bool grab) override;
- bool hasEvent() override;
- std::shared_ptr<Event> getNextEvent() override;
- std::shared_ptr<Event> peekNextEvent() override;
+ std::queue<std::shared_ptr<Event>> getEvents() override;
private:
- std::shared_ptr<Event> translateXMessage(XEvent& xevent, bool peek = false);
+ std::shared_ptr<Event> translateXMessage(XEvent& xevent);
//! Allocate new shared memory buffer for the pixel buffer.
//! Frees the existing buffer if there is one.