From 1bdbf68ca9100acf0faecbc81a4d45bf84b15be7 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Mon, 26 Apr 2021 07:14:24 +1000 Subject: [PATCH] [windows] Main thread dispatcher. Small refactor. --- v2/internal/ffenestri/ffenestri_windows.c | 130 +++++++++++++++++++--- v2/internal/ffenestri/ffenestri_windows.h | 2 + 2 files changed, 114 insertions(+), 18 deletions(-) diff --git a/v2/internal/ffenestri/ffenestri_windows.c b/v2/internal/ffenestri/ffenestri_windows.c index 9c2903afe..8c727a07b 100644 --- a/v2/internal/ffenestri/ffenestri_windows.c +++ b/v2/internal/ffenestri/ffenestri_windows.c @@ -2,11 +2,33 @@ #include "ffenestri_windows.h" int debug = 0; +DWORD mainThread; + +typedef void(*dispatchMethod)(void); +typedef void(*dispatchMethod1)(void*); +typedef void(*dispatchMethod2)(void*,void*); + +typedef struct { + void* func; + void* args[5]; + int argc; +} dispatchFunction; + +dispatchFunction* NewDispatchFunction(void *func) { + dispatchFunction *result = malloc(sizeof(dispatchFunction)); + result->func = func; + result->argc = 0; + return result; +} + +// dispatch will execute the given `func` pointer +void dispatch(dispatchFunction *func) { + PostThreadMessage(mainThread, WM_APP, 0, (LPARAM) func); +} struct Application{ // Window specific HWND window; - DWORD mainThread; // Application const char *title; @@ -52,7 +74,7 @@ struct Application *NewApplication(const char *title, int width, int height, int result->frame = 1; // Capture Main Thread - result->mainThread = GetCurrentThreadId(); + mainThread = GetCurrentThreadId(); return result; } @@ -128,16 +150,16 @@ void Run(struct Application* app, int argc, char **argv) { // Process window resizable DWORD windowStyle = WS_OVERLAPPEDWINDOW; + if (app->resizable == 0) { + windowStyle &= ~WS_MAXIMIZEBOX; + windowStyle &= ~WS_THICKFRAME; + } if ( app->frame == 0 ) { windowStyle = WS_POPUP; } - DWORD dwStyle = windowStyle; - if (app->resizable == 0) { - dwStyle = windowStyle | WS_MINIMIZEBOX | WS_SYSMENU; - } RegisterClassEx(&wc); - app->window = CreateWindow("ffenestri", "", dwStyle, CW_USEDEFAULT, + app->window = CreateWindow("ffenestri", "", windowStyle, CW_USEDEFAULT, CW_USEDEFAULT, app->width, app->height, NULL, NULL, GetModuleHandle(NULL), NULL); // Set Title @@ -151,7 +173,8 @@ void Run(struct Application* app, int argc, char **argv) { if ( app->startHidden == 1 ) { startVisibility = SW_HIDE; } - Center(app); + // private center() as we are on main thread + center(app); ShowWindow(app->window, startVisibility); UpdateWindow(app->window); SetFocus(app->window); @@ -168,14 +191,21 @@ void Run(struct Application* app, int argc, char **argv) { continue; } if (msg.message == WM_APP) { + dispatchFunction *m = (dispatchFunction*) msg.lParam; + void *method = m->func; + if (m->argc == 1) { + ((dispatchMethod1)method)(m->args[0]); + } + if (m->argc == 2) { + ((dispatchMethod2)method)(m->args[0], m->args[1]); + } + free(m); } else if (msg.message == WM_QUIT) { return; } } } - - void DestroyApplication(struct Application* app) { PostQuitMessage(0); } @@ -189,15 +219,29 @@ void SetBindings(struct Application* app, const char *bindings) { void ExecJS(struct Application* app, const char *script) { } -void Hide(struct Application* app) { +void hide(struct Application* app) { ShowWindow(app->window, SW_HIDE); } -void Show(struct Application* app) { +void Hide(struct Application* app) { + dispatchFunction *f = NewDispatchFunction(hide); + f->args[0] = app; + f->argc = 1; + dispatch(f); +} + +void show(struct Application* app) { ShowWindow(app->window, SW_SHOW); } -void Center(struct Application* app) { +void Show(struct Application* app) { + dispatchFunction *f = NewDispatchFunction(show); + f->args[0] = app; + f->argc = 1; + dispatch(f); +} + +void center(struct Application* app) { HMONITOR currentMonitor = MonitorFromWindow(app->window, MONITOR_DEFAULTTONEAREST); MONITORINFO info = {0}; @@ -221,6 +265,13 @@ void Center(struct Application* app) { SetWindowPos(app->window, HWND_TOP, windowX, windowY, winWidth, winHeight, SWP_NOSIZE); } +void Center(struct Application* app) { + dispatchFunction *f = NewDispatchFunction(center); + f->args[0] = app; + f->argc = 1; + dispatch(f); +} + UINT getWindowPlacement(struct Application* app) { WINDOWPLACEMENT lpwndpl; lpwndpl.length = sizeof(WINDOWPLACEMENT); @@ -236,14 +287,29 @@ int isMaximised(struct Application* app) { return getWindowPlacement(app) == SW_SHOWMAXIMIZED; } -void Maximise(struct Application* app) { +void maximise(struct Application* app) { ShowWindow(app->window, SW_MAXIMIZE); } -void Unmaximise(struct Application* app) { +void Maximise(struct Application* app) { + dispatchFunction *f = NewDispatchFunction(maximise); + f->args[0] = app; + f->argc = 1; + dispatch(f); +} + +void unmaximise(struct Application* app) { ShowWindow(app->window, SW_RESTORE); } +void Unmaximise(struct Application* app) { + dispatchFunction *f = NewDispatchFunction(unmaximise); + f->args[0] = app; + f->argc = 1; + dispatch(f); +} + + void ToggleMaximise(struct Application* app) { if(isMaximised(app)) { return Unmaximise(app); @@ -255,14 +321,28 @@ int isMinimised(struct Application* app) { return getWindowPlacement(app) == SW_SHOWMINIMIZED; } -void Minimise(struct Application* app) { +void minimise(struct Application* app) { ShowWindow(app->window, SW_MINIMIZE); } -void Unminimise(struct Application* app) { +void Minimise(struct Application* app) { + dispatchFunction *f = NewDispatchFunction(minimise); + f->args[0] = app; + f->argc = 1; + dispatch(f); +} + +void unminimise(struct Application* app) { ShowWindow(app->window, SW_RESTORE); } +void Unminimise(struct Application* app) { + dispatchFunction *f = NewDispatchFunction(unminimise); + f->args[0] = app; + f->argc = 1; + dispatch(f); +} + void ToggleMinimise(struct Application* app) { if(isMinimised(app)) { return Unminimise(app); @@ -271,20 +351,34 @@ void ToggleMinimise(struct Application* app) { } void SetColour(struct Application* app, int red, int green, int blue, int alpha) { + } + void SetSize(struct Application* app, int width, int height) { } void SetPosition(struct Application* app, int x, int y) { } void Quit(struct Application* app) { } -void SetTitle(struct Application* app, const char *title) { + +void setTitle(struct Application* app, const char *title) { SetWindowText(app->window, title); } + +void SetTitle(struct Application* app, const char *title) { + dispatchFunction *f = NewDispatchFunction(setTitle); + f->args[0] = app; + f->args[1] = (void*)title; + f->argc = 2; + dispatch(f); +} + void Fullscreen(struct Application* app) { } + void UnFullscreen(struct Application* app) { } + void ToggleFullscreen(struct Application* app) { } diff --git a/v2/internal/ffenestri/ffenestri_windows.h b/v2/internal/ffenestri/ffenestri_windows.h index 294ab9220..dbe507e17 100644 --- a/v2/internal/ffenestri/ffenestri_windows.h +++ b/v2/internal/ffenestri/ffenestri_windows.h @@ -6,4 +6,6 @@ #include #include +void center(struct Application*); + #endif \ No newline at end of file