From b9773f6eecea38e6a5aa28745cc6fe79aa2d441f Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 12 Mar 2016 10:54:52 +0100 Subject: Add parent widget pointer for contained plugin windows. Remove threaded event handling and make all event handling depend on processEcvents being called with a regular (~50ms) interval. --- plugingui/dgwindow.cc | 8 ++++--- plugingui/dgwindow.h | 2 +- plugingui/nativewindow_pugl.cc | 24 +++++++++++---------- plugingui/nativewindow_pugl.h | 5 +++-- plugingui/nativewindow_win32.cc | 23 ++++++-------------- plugingui/nativewindow_win32.h | 2 +- plugingui/nativewindow_x11.cc | 20 ++++++++++-------- plugingui/nativewindow_x11.h | 2 +- plugingui/plugingui.cc | 47 ++++++----------------------------------- plugingui/plugingui.h | 14 ++++++------ plugingui/testmain.cc | 5 +++-- plugingui/window.cc | 8 +++---- plugingui/window.h | 2 +- 13 files changed, 63 insertions(+), 99 deletions(-) diff --git a/plugingui/dgwindow.cc b/plugingui/dgwindow.cc index cc4aa8a..8d9e789 100644 --- a/plugingui/dgwindow.cc +++ b/plugingui/dgwindow.cc @@ -119,14 +119,16 @@ public: Knob falloffKnob{&falloff}; }; -DGWindow::DGWindow(MessageHandler& messageHandler, Config& config) - : messageHandler(messageHandler) +DGWindow::DGWindow(void* native_window, MessageHandler& messageHandler, + Config& config) + : Window(native_window) + , messageHandler(messageHandler) , config(config) { int vlineSpacing = 16; - setFixedSize(370, 330); + resize(370, 330); setCaption("DrumGizmo v" VERSION); layout.setResizeChildren(false); diff --git a/plugingui/dgwindow.h b/plugingui/dgwindow.h index 4df93cf..07e23df 100644 --- a/plugingui/dgwindow.h +++ b/plugingui/dgwindow.h @@ -47,7 +47,7 @@ class File; class DGWindow : public Window { public: - DGWindow(MessageHandler& messageHandler, Config& config); + DGWindow(void* native_window, MessageHandler& messageHandler, Config& config); Header* header; diff --git a/plugingui/nativewindow_pugl.cc b/plugingui/nativewindow_pugl.cc index 48cc3f9..bf9fa3b 100644 --- a/plugingui/nativewindow_pugl.cc +++ b/plugingui/nativewindow_pugl.cc @@ -47,7 +47,7 @@ namespace GUI { void NativeWindowPugl::onDisplay(PuglView* view) { NativeWindowPugl* native = (NativeWindowPugl*)puglGetHandle(view); - Window* windowptr = native->window; + Window& window = native->window; glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT); @@ -64,17 +64,17 @@ void NativeWindowPugl::onDisplay(PuglView* view) glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_REPLACE); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowptr->wpixbuf.width, - windowptr->wpixbuf.height, 0, GL_RGB, GL_UNSIGNED_BYTE, - windowptr->wpixbuf.buf); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, window.wpixbuf.width, + window.wpixbuf.height, 0, GL_RGB, GL_UNSIGNED_BYTE, + window.wpixbuf.buf); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2d(0.0, 0.0); glVertex2f(0.0, 0.0); - glTexCoord2d(0.0, 1.0); glVertex2f(0.0, windowptr->wpixbuf.height); - glTexCoord2d(1.0, 1.0); glVertex2f(windowptr->wpixbuf.width, windowptr->wpixbuf.height); - glTexCoord2d(1.0, 0.0); glVertex2f(windowptr->wpixbuf.width, 0.0); + glTexCoord2d(0.0, 1.0); glVertex2f(0.0, window.wpixbuf.height); + glTexCoord2d(1.0, 1.0); glVertex2f(window.wpixbuf.width, window.wpixbuf.height); + glTexCoord2d(1.0, 0.0); glVertex2f(window.wpixbuf.width, 0.0); glEnd(); glDeleteTextures(1, &image); @@ -145,8 +145,9 @@ void NativeWindowPugl::onKeyboard(PuglView* view, bool press, uint32_t key) native->eventq.push_back(e); } -NativeWindowPugl::NativeWindowPugl(Window *window) +NativeWindowPugl::NativeWindowPugl(void* native_window, Window& window) : window(window) + , native_window(native_window) { INFO(nativewindow, "Running with PuGL native window\n"); init(); @@ -157,15 +158,16 @@ NativeWindowPugl::~NativeWindowPugl() puglDestroy(view); } -void NativeWindowPugl::init() { +void NativeWindowPugl::init() +{ PuglView* oldView = view; if(view) { oldView = view; } -// view = puglCreate(0, "DrumgGizmo", window->x(), window->y(), false, true); - view = puglCreate(0, "DrumgGizmo", 370, 330, false, true); +// view = puglCreate(0, "DrumgGizmo", window.x(), window.y(), false, true); + view = puglCreate((PuglNativeWindow)native_window, "DrumgGizmo", 370, 330, false, true); puglSetHandle(view, (PuglHandle)this); puglSetDisplayFunc(view, onDisplay); puglSetMouseFunc(view, onMouse); diff --git a/plugingui/nativewindow_pugl.h b/plugingui/nativewindow_pugl.h index 2c9fbc7..13ca1e7 100644 --- a/plugingui/nativewindow_pugl.h +++ b/plugingui/nativewindow_pugl.h @@ -38,7 +38,7 @@ class Window; class NativeWindowPugl : public NativeWindow { public: - NativeWindowPugl(Window *window); + NativeWindowPugl(void* native_window, Window& window); ~NativeWindowPugl(); void init(); @@ -57,7 +57,8 @@ public: Event *peekNextEvent(); private: - Window* window{nullptr}; + Window& window; + void* native_window{nullptr}; PuglView* view{nullptr}; std::list eventq; diff --git a/plugingui/nativewindow_win32.cc b/plugingui/nativewindow_win32.cc index e3103b1..5acb43b 100644 --- a/plugingui/nativewindow_win32.cc +++ b/plugingui/nativewindow_win32.cc @@ -283,11 +283,10 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, return DefWindowProc(hwnd, msg, wp, lp); } -NativeWindowWin32::NativeWindowWin32(Window& window) +NativeWindowWin32::NativeWindowWin32(void* native_window, Window& window) : window(window) { WNDCLASSEX wcex; - WNDID wndId; std::memset(&wcex, 0, sizeof(wcex)); @@ -316,22 +315,12 @@ NativeWindowWin32::NativeWindowWin32(Window& window) RegisterClassEx(&wcex); - /* - if(parent) { - style = style | WS_CHILD; - wndId = parent->getWndId(); - } else { - */ - //style = style | WS_OVERLAPPEDWINDOW; - wndId = 0; - // } - m_hwnd = CreateWindowEx(0/*ex_style*/, m_className, "DGBasisWidget", - (WS_OVERLAPPEDWINDOW | WS_VISIBLE), + (native_window?WS_CHILD:WS_OVERLAPPEDWINDOW) | WS_VISIBLE, window.x(), window.y(), window.width(), window.height(), - wndId, nullptr, + (HWND)native_window, nullptr, GetModuleHandle(nullptr), nullptr); SetWindowLongPtr(m_hwnd, GWLP_USERDATA, (LONG_PTR)this); @@ -407,7 +396,7 @@ void NativeWindowWin32::grabMouse(bool grab) bool NativeWindowWin32::hasEvent() { MSG msg; - return PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE) != 0; + return PeekMessage(&msg, m_hwnd, 0, 0, PM_NOREMOVE) != 0; } Event* NativeWindowWin32::getNextEvent() @@ -415,7 +404,7 @@ Event* NativeWindowWin32::getNextEvent() Event* event = nullptr; MSG msg; - if(GetMessage(&msg, nullptr, 0, 0)) + if(GetMessage(&msg, m_hwnd, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); @@ -432,7 +421,7 @@ Event* NativeWindowWin32::peekNextEvent() Event* event = nullptr; MSG msg; - if(PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)) + if(PeekMessage(&msg, m_hwnd, 0, 0, PM_NOREMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); diff --git a/plugingui/nativewindow_win32.h b/plugingui/nativewindow_win32.h index e9e9804..2c8507d 100644 --- a/plugingui/nativewindow_win32.h +++ b/plugingui/nativewindow_win32.h @@ -39,7 +39,7 @@ class Event; class NativeWindowWin32 : public NativeWindow { public: - NativeWindowWin32(Window& window); + NativeWindowWin32(void* native_window, Window& window); ~NativeWindowWin32(); void setFixedSize(int width, int height) override; diff --git a/plugingui/nativewindow_x11.cc b/plugingui/nativewindow_x11.cc index ae1907c..8088d93 100644 --- a/plugingui/nativewindow_x11.cc +++ b/plugingui/nativewindow_x11.cc @@ -35,16 +35,10 @@ namespace GUI { -NativeWindowX11::NativeWindowX11(Window& window) +NativeWindowX11::NativeWindowX11(void* native_window, Window& window) : buffer(nullptr) , window(window) { - auto status = XInitThreads(); - if(status) - { - ERR(X11, "Could not initialise threaded Xlib calls (XInitThreads)"); - } - display = XOpenDisplay(nullptr); if(display == nullptr) { @@ -57,12 +51,20 @@ NativeWindowX11::NativeWindowX11(Window& window) // Get some colors int blackColor = BlackPixel(display, screen); - ::Window rootWindow = DefaultRootWindow(display); + ::Window parentWindow; + if(native_window) + { + parentWindow = (::Window)native_window; + } + else + { + parentWindow = DefaultRootWindow(display); + } // Create the window unsigned long border = 0; xwindow = XCreateSimpleWindow(display, - rootWindow, + parentWindow, window.x(), window.y(), window.width(), window.height(), border, diff --git a/plugingui/nativewindow_x11.h b/plugingui/nativewindow_x11.h index b0dcd83..1487e07 100644 --- a/plugingui/nativewindow_x11.h +++ b/plugingui/nativewindow_x11.h @@ -35,7 +35,7 @@ namespace GUI { class Window; class NativeWindowX11 : public NativeWindow { public: - NativeWindowX11(Window& window); + NativeWindowX11(void* native_window, Window& window); ~NativeWindowX11(); // From NativeWindow: diff --git a/plugingui/plugingui.cc b/plugingui/plugingui.cc index 85e628c..3375817 100644 --- a/plugingui/plugingui.cc +++ b/plugingui/plugingui.cc @@ -33,21 +33,15 @@ namespace GUI { -PluginGUI::PluginGUI() +PluginGUI::PluginGUI(void* native_window) : MessageReceiver(MSGRCV_UI) + , native_window(native_window) { -#ifdef USE_THREAD - run(); -#else init(); -#endif/*USE_THREAD*/ - - sem.wait(); } PluginGUI::~PluginGUI() { - stopThread(); } void PluginGUI::handleMessage(Message *msg) @@ -114,27 +108,6 @@ void PluginGUI::handleMessage(Message *msg) } } -void PluginGUI::thread_main() -{ - init(); - - { // Request all engine settings - EngineSettingsMessage *msg = new EngineSettingsMessage(); - msghandler.sendMessage(MSGRCV_ENGINE, msg); - } - - while(processEvents()) - { -#ifdef WIN32 - SleepEx(50, FALSE); -#else - usleep(50000); -#endif/*WIN32*/ - } - - deinit(); -} - bool PluginGUI::processEvents() { if(!initialised) @@ -155,15 +128,6 @@ bool PluginGUI::processEvents() return running; } -void PluginGUI::stopThread() -{ - if(running) - { - running = false; - wait_stop(); - } -} - void PluginGUI::init() { DEBUG(gui, "init"); @@ -171,14 +135,17 @@ void PluginGUI::init() config = new Config(); config->load(); - window = new DGWindow(msghandler, *config); + window = new DGWindow(native_window, msghandler, *config); auto eventHandler = window->eventHandler(); CONNECT(eventHandler, closeNotifier, this, &PluginGUI::closeEventHandler); window->show(); - sem.post(); + { // Request all engine settings + EngineSettingsMessage *msg = new EngineSettingsMessage(); + msghandler.sendMessage(MSGRCV_ENGINE, msg); + } initialised = true; } diff --git a/plugingui/plugingui.h b/plugingui/plugingui.h index d639a16..af05478 100644 --- a/plugingui/plugingui.h +++ b/plugingui/plugingui.h @@ -40,19 +40,18 @@ namespace GUI { -class PluginGUI : public Thread, public MessageReceiver, public Listener { +class PluginGUI + : public MessageReceiver + , public Listener +{ public: - PluginGUI(); + PluginGUI(void* native_window = nullptr); virtual ~PluginGUI(); - void thread_main(); - //! Process all events and messages in queue //! \return true if not closing, returns false if closing. bool processEvents(); - void stopThread(); - void init(); void deinit(); @@ -65,7 +64,6 @@ public: DGWindow* window{nullptr}; EventHandler* eventhandler{nullptr}; - Config* config{nullptr}; Notifier<> closeNotifier; @@ -76,6 +74,8 @@ public: private: void closeEventHandler(); + void* native_window{nullptr}; + volatile bool running{true}; volatile bool closing{false}; volatile bool initialised{false}; diff --git a/plugingui/testmain.cc b/plugingui/testmain.cc index 08c0ad7..e1a1256 100644 --- a/plugingui/testmain.cc +++ b/plugingui/testmain.cc @@ -52,10 +52,11 @@ public: while(running) { #ifdef WIN32 - SleepEx(1000, FALSE); + SleepEx(50, FALSE); #else - sleep(1); + usleep(50000); #endif + gui.processEvents(); } } diff --git a/plugingui/window.cc b/plugingui/window.cc index ab51008..e61b004 100644 --- a/plugingui/window.cc +++ b/plugingui/window.cc @@ -42,7 +42,7 @@ namespace GUI { -Window::Window() +Window::Window(void* native_window) : Widget(nullptr) , wpixbuf(100, 100) { @@ -52,13 +52,13 @@ Window::Window() #ifndef PUGL #ifdef X11 - native = new NativeWindowX11(*this); + native = new NativeWindowX11(native_window, *this); #endif/*X11*/ #ifdef WIN32 - native = new NativeWindowWin32(*this); + native = new NativeWindowWin32(native_window, *this); #endif/*WIN32*/ #else/*Use pugl*/ - native = new NativeWindowPugl(this); + native = new NativeWindowPugl(native_window, *this); #endif eventhandler = new EventHandler(*native, *this); diff --git a/plugingui/window.h b/plugingui/window.h index f898105..c101458 100644 --- a/plugingui/window.h +++ b/plugingui/window.h @@ -37,7 +37,7 @@ namespace GUI { class Window : public Widget { public: - Window(); + Window(void* native_window); ~Window(); void setFixedSize(int width, int height); -- cgit v1.2.3