From af670a7af310d8ffd2c1e87415aa140828eea213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 2 Mar 2016 18:48:12 +0100 Subject: [PATCH] Why not just do it the hard way? --- include/luares.h | 1 + include/lupi.h | 5 +++ src/c/event.c | 8 +++- src/c/modules.c | 1 + src/c/run.c | 4 +- src/c/winapigpu.c | 89 ++++++++++++++++++++++++++++++++++++++ src/lua/core/gpudetect.lua | 9 ++++ src/lua/core/winapigpu.lua | 11 +++++ 8 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/c/winapigpu.c create mode 100644 src/lua/core/winapigpu.lua diff --git a/include/luares.h b/include/luares.h index cf85f09..5f3d109 100644 --- a/include/luares.h +++ b/include/luares.h @@ -16,4 +16,5 @@ extern char lua_textgpu[]; extern char lua_util_buffer[]; extern char lua_util_color[]; extern char lua_util_random[]; +extern char lua_winapigpu[]; #endif diff --git a/include/lupi.h b/include/lupi.h index 7461cd7..06410fb 100644 --- a/include/lupi.h +++ b/include/lupi.h @@ -30,4 +30,9 @@ void internet_start(lua_State *L); void event_prepare(); int event_pull(int timeout); +#ifdef _WIN32 +void winapigpu_init(lua_State* L); +int winapigpu_events(); +#endif + #endif diff --git a/src/c/event.c b/src/c/event.c index 76eafed..59849c9 100644 --- a/src/c/event.c +++ b/src/c/event.c @@ -58,6 +58,12 @@ static void add_events(struct timeval* timeout) { int event_pull(int _timeout) { + int n = 0; +#ifdef _WIN32 + n = winapigpu_events(); + if(n > 0) return n; +#endif + if(_timeout > 0) { /* wait max this much time for event */ struct timeval timeout = {_timeout / 1000, (_timeout % 1000) * 1000}; add_events(&timeout); @@ -70,7 +76,7 @@ int event_pull(int _timeout) { event_base_loop(base, EVLOOP_ONCE); } - int n = nevt; + n = nevt; nevt = 0; return n; } diff --git a/src/c/modules.c b/src/c/modules.c index 82613e6..7ca75ea 100644 --- a/src/c/modules.c +++ b/src/c/modules.c @@ -22,6 +22,7 @@ void setup_modules(lua_State *L) { pushstuple(L, "sandbox", lua_sandbox); pushstuple(L, "textgpu", lua_textgpu); pushstuple(L, "fbgpu", lua_fbgpu); + pushstuple(L, "winapigpu", lua_winapigpu); pushstuple(L, "color", lua_util_color); pushstuple(L, "random", lua_util_random); pushstuple(L, "buffer", lua_util_buffer); diff --git a/src/c/run.c b/src/c/run.c index 0510b17..4b91ee2 100644 --- a/src/c/run.c +++ b/src/c/run.c @@ -8,7 +8,6 @@ #include #include -//#include #include #include #include @@ -27,6 +26,9 @@ void run_init(int argc, char **argv) { setup_modules (L); luanative_start (L); internet_start (L); +#ifdef _WIN32 + winapigpu_init (L); +#endif fb_start (L); termutils_start (L); event_prepare(); diff --git a/src/c/winapigpu.c b/src/c/winapigpu.c new file mode 100644 index 0000000..b599cd5 --- /dev/null +++ b/src/c/winapigpu.c @@ -0,0 +1,89 @@ +#ifdef _WIN32 +#include "res.h" +#include "lupi.h" +#include +#include +#include +#include + +#define WINDOW_CLASS "LuPi2 GPU" +#define WINDOW_TITLE "LuPi2" + +LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { + switch(msg) { + case WM_CLOSE: + DestroyWindow(hwnd); + exit(0); /* TODO: Push exit event? Or just remove gpu component?? */ + break; + case WM_DESTROY: + PostQuitMessage(0); + break; + default: + return DefWindowProc(hwnd, msg, wParam, lParam); + } + return 0; +} + +static int l_open(lua_State *L) { + WNDCLASSEX wc; + HWND hwnd; + + wc.cbSize = sizeof(WNDCLASSEX); + wc.style = 0; + wc.lpfnWndProc = WndProc; + wc.cbClsExtra = 0; + wc.cbWndExtra = 0; + wc.hInstance = GetModuleHandle(NULL); + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); + wc.lpszMenuName = NULL; + wc.lpszClassName = WINDOW_CLASS; + wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); + + if(!RegisterClassEx(&wc)) { + lua_pushboolean(L, 0); + lua_pushstring(L, "Window registration failed"); + return 2; + } + + hwnd = CreateWindowEx( + WS_EX_CLIENTEDGE, + WINDOW_CLASS, + WINDOW_TITLE, + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, + NULL, NULL, GetModuleHandle(NULL), NULL); + + if(hwnd == NULL) { + lua_pushboolean(L, 0); + lua_pushstring(L, "Window creation failed"); + return 2; + } + + ShowWindow(hwnd, SW_SHOW); + UpdateWindow(hwnd); + + lua_pushboolean(L, 1); + return 1; +} + +void winapigpu_init(lua_State* L) { + struct luaL_Reg winlib[] = { + {"open", l_open}, + {NULL, NULL} + }; + + luaL_openlib(L, "winapigpu", winlib, 0); +} + +int winapigpu_events() { + MSG Msg; + while(GetMessage(&Msg, NULL, 0, 0) > 0) { + TranslateMessage(&Msg); + DispatchMessage(&Msg); + } + return 0; +} + +#endif diff --git a/src/lua/core/gpudetect.lua b/src/lua/core/gpudetect.lua index 7136973..870323a 100644 --- a/src/lua/core/gpudetect.lua +++ b/src/lua/core/gpudetect.lua @@ -19,6 +19,11 @@ local function tryFb() return false end +local function tryWindows() + loadModule("winapigpu") + return modules.winapigpu.start() and true or false +end + function gpudetect.run() local s = false if hasOpt("-t", "--text") then @@ -32,6 +37,10 @@ function gpudetect.run() lprint("Falling back to text gpu") s = tryText() end + if not s and winapigpu then + lprint("Falling back to windows gpu") + s = tryWindows() + end end return gpudetect diff --git a/src/lua/core/winapigpu.lua b/src/lua/core/winapigpu.lua new file mode 100644 index 0000000..b3651b1 --- /dev/null +++ b/src/lua/core/winapigpu.lua @@ -0,0 +1,11 @@ +local wingpu = {} + +function wingpu.start() + local s, reason = winapigpu.open() + if not s then + lprint("Couldn't open window: " .. tostring(reason)) + end + return s +end + +return wingpu