feat(console): hardware detection et cetera

This commit is contained in:
phaneron 2025-04-12 04:35:49 -04:00
parent 97bbe2ea66
commit 31f215ea14
118 changed files with 4059 additions and 1931 deletions

58
src/os/CMakeLists.txt Normal file
View file

@ -0,0 +1,58 @@
file(GLOB PRIVATE_SOURCES
"*.cpp"
"internal/*.cpp"
)
if(WHOA_SYSTEM_WIN)
file(GLOB WIN_SOURCES
"win/*.cpp")
list(APPEND PRIVATE_SOURCES ${WIN_SOURCES})
endif()
if(WHOA_SYSTEM_MAC)
file(GLOB MAC_SOURCES
"mac/*.mm")
set_source_files_properties(${MAC_SOURCES}
PROPERTIES COMPILE_FLAGS "-x objective-c++"
)
list(APPEND PRIVATE_SOURCES ${MAC_SOURCES})
endif()
if(WHOA_SYSTEM_LINUX)
file(GLOB LINUX_SOURCES
"linux/*.cpp")
list(APPEND PRIVATE_SOURCES ${LINUX_SOURCES})
endif()
if(WHOA_BUILD_GLSDL)
file(GLOB SDL_SOURCES
"sdl/*.cpp")
list(APPEND PRIVATE_SOURCES ${SDL_SOURCES})
endif()
add_library(os STATIC
${PRIVATE_SOURCES}
)
target_include_directories(os
PRIVATE
${CMAKE_SOURCE_DIR}/src
)
target_link_libraries(os
PUBLIC
storm
common
)
if(WHOA_BUILD_GLSDL)
target_link_libraries(os
PRIVATE
SDL3::SDL3-static
)
endif()

10
src/os/Clipboard.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef OS_CLIPBOARD_HPP
#define OS_CLIPBOARD_HPP
#include <cstdint>
char* OsClipboardGetString();
int32_t OsClipboardPutString(const char*);
#endif

View file

@ -2,7 +2,7 @@
#define OS_COMPAT_HPP
#if defined(WHOA_SYSTEM_MAC)
#include "os/compat/Mac.hpp"
#include "os/mac/Compat.hpp"
#endif
#endif

6
src/os/Debug.hpp Normal file
View file

@ -0,0 +1,6 @@
#ifndef OS_DEBUG_HPP
#define OS_DEBUG_HPP
void OsOutputDebugString(const char* format, ...);
#endif

18
src/os/Gui.hpp Normal file
View file

@ -0,0 +1,18 @@
#ifndef OS_GUI_HPP
#define OS_GUI_HPP
#include <cstdint>
void* OsGuiGetWindow(int32_t type);
bool OsGuiIsModifierKeyDown(int32_t key);
int32_t OsGuiProcessMessage(void* message);
void OsGuiSetGxWindow(void* window);
int32_t OsGuiMessageBox(void* parentWindow, int32_t style, const char* message, const char* title);
void OsGuiSetWindowTitle(void* window, const char* title);
#endif

23
src/os/Input.hpp Normal file
View file

@ -0,0 +1,23 @@
#ifndef OS_INPUT_HPP
#define OS_INPUT_HPP
#include "os/Types.hpp"
#include <cstdint>
int32_t OsInputGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3);
void OsInputSetWindowResizeLock(int32_t resizeLock);
void OsInputInitialize();
bool OsInputIsUsingCocoaEventLoop();
void OsInputPostEvent(OSINPUT id, int32_t param0, int32_t param1, int32_t param2, int32_t param3);
void OsInputSetMouseMode(OS_MOUSE_MODE mode);
void OsInputGetMousePosition(int32_t* x, int32_t* y);
int32_t OsWindowProc(void* window, uint32_t message, uintptr_t wparam, intptr_t lparam);
#endif

66
src/os/Queue.cpp Normal file
View file

@ -0,0 +1,66 @@
#include "os/Queue.hpp"
#include "os/internal/Queue.hpp"
int32_t OsQueueGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3) {
if (s_queueTail == s_queueHead) {
return 0;
}
OSEVENT event = s_queue[s_queueTail];
*id = event.id;
*param0 = event.param[0];
*param1 = event.param[1];
*param2 = event.param[2];
*param3 = event.param[3];
if (s_queueTail == (OS_QUEUE_SIZE - 1)) {
s_queueTail = 0;
} else {
++s_queueTail;
}
return 1;
}
void OsQueuePut(OSINPUT id, int32_t param0, int32_t param1, int32_t param2, int32_t param3) {
int32_t nextTail = 0;
int32_t nextHead = 0;
if (s_queueHead != OS_QUEUE_SIZE - 1) {
nextHead = s_queueHead + 1;
}
if (nextHead == s_queueTail) {
if (nextHead != OS_QUEUE_SIZE - 1) {
nextTail = nextHead + 1;
}
s_queueTail = nextTail;
}
auto event = &s_queue[s_queueHead];
event->id = id;
event->param[0] = param0;
event->param[1] = param1;
event->param[2] = param2;
event->param[3] = param3;
s_queueHead = nextHead;
}
void OsQueueSetParam(int32_t index, int32_t param) {
int32_t pos = s_queueTail;
while (pos != s_queueHead) {
auto event = &s_queue[pos];
event->param[index] = param;
if (pos == OS_QUEUE_SIZE - 1) {
pos = 0;
} else {
++pos;
}
}
}

12
src/os/Queue.hpp Normal file
View file

@ -0,0 +1,12 @@
#ifndef OS_QUEUE_HPP
#define OS_QUEUE_HPP
#include "os/Types.hpp"
int32_t OsQueueGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3);
void OsQueuePut(OSINPUT id, int32_t param0, int32_t param1, int32_t param2, int32_t param3);
void OsQueueSetParam(int32_t index, int32_t param);
#endif

40
src/os/Types.hpp Normal file
View file

@ -0,0 +1,40 @@
#ifndef OS_TYPES_HPP
#define OS_TYPES_HPP
#include <cstdint>
enum OSINPUT {
OS_INPUT_CAPTURE_CHANGED = 0,
OS_INPUT_CHAR = 1,
OS_INPUT_STRING = 2,
OS_INPUT_IME = 3,
OS_INPUT_SIZE = 4,
OS_INPUT_CLOSE = 5,
OS_INPUT_FOCUS = 6,
OS_INPUT_KEY_DOWN = 7,
OS_INPUT_KEY_UP = 8,
OS_INPUT_MOUSE_DOWN = 9,
OS_INPUT_MOUSE_MOVE = 10,
OS_INPUT_MOUSE_WHEEL = 11,
OS_INPUT_MOUSE_MOVE_RELATIVE = 12,
OS_INPUT_MOUSE_UP = 13,
OS_INPUT_14 = 14,
OS_INPUT_15 = 15,
OS_INPUT_16 = 16,
OS_INPUT_17 = 17,
OS_INPUT_18 = 18,
OS_INPUT_SHUTDOWN = 19
};
enum OS_MOUSE_MODE {
OS_MOUSE_MODE_NORMAL = 0,
OS_MOUSE_MODE_RELATIVE = 1,
OS_MOUSE_MODES = 2,
};
struct OSEVENT {
OSINPUT id;
int32_t param[4];
};
#endif

23
src/os/internal/Input.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "os/internal/Input.hpp"
int32_t s_numlockState;
uint32_t s_osButtonState;
OS_MOUSE_MODE s_osMouseMode;
int32_t s_windowFocused;
int32_t s_WindowResizeLock;
#if defined(WHOA_SYSTEM_WIN)
int32_t s_savedMouseSpeed;
#endif
#if defined(WHOA_SYSTEM_MAC)
double s_savedMouseSpeed;
#endif

28
src/os/internal/Input.hpp Normal file
View file

@ -0,0 +1,28 @@
#ifndef OS_INTERNAL_INPUT_HPP
#define OS_INTERNAL_INPUT_HPP
#include "os/Types.hpp"
extern int32_t s_numlockState;
extern uint32_t s_osButtonState;
extern OS_MOUSE_MODE s_osMouseMode;
extern int32_t s_windowFocused;
extern int32_t s_WindowResizeLock;
#if defined(WHOA_SYSTEM_WIN)
extern int32_t s_savedMouseSpeed;
#endif
#if defined(WHOA_SYSTEM_MAC)
extern double s_savedMouseSpeed;
#endif
#endif

View file

@ -0,0 +1,6 @@
#include "os/internal/Queue.hpp"
OSEVENT s_queue[32];
int32_t s_queueHead;
int32_t s_queueTail;

13
src/os/internal/Queue.hpp Normal file
View file

@ -0,0 +1,13 @@
#ifndef OS_INTERNAL_QUEUE_HPP
#define OS_INTERNAL_QUEUE_HPP
#define OS_QUEUE_SIZE 32
#include "os/Types.hpp"
extern OSEVENT s_queue[OS_QUEUE_SIZE];
extern int32_t s_queueHead;
extern int32_t s_queueTail;
#endif

View file

@ -0,0 +1,25 @@
#include "os/Clipboard.hpp"
#if defined(WHOA_BUILD_GLSDL)
#include "os/sdl/Clipboard.hpp"
char* OsClipboardGetString() {
return OsSDLClipboardGetString();
}
int32_t OsClipboardPutString(const char* str) {
return OsSDLClipboardPutString(str);
}
#else
char* OsClipboardGetString() {
return nullptr;
}
int32_t OsClipboardPutString(const char* str) {
return 0;
}
#endif

11
src/os/linux/Debug.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "os/Debug.hpp"
#include <cstdio>
#include <cstdarg>
void OsOutputDebugString(const char* format, ...) {
// TODO
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
return;
}

51
src/os/linux/Gui.cpp Normal file
View file

@ -0,0 +1,51 @@
#include "os/Gui.hpp"
#if defined(WHOA_BUILD_GLSDL)
#include "os/sdl/Gui.hpp"
#endif
static void* s_GxDevWindow = nullptr;
void* OsGuiGetWindow(int32_t type) {
switch (type) {
case 0:
case 1:
case 2:
return s_GxDevWindow;
default:
return nullptr;
}
}
bool OsGuiIsModifierKeyDown(int32_t key) {
// TODO
return false;
}
int32_t OsGuiProcessMessage(void* message) {
return 0;
}
void OsGuiSetGxWindow(void* window) {
s_GxDevWindow = window;
}
#if defined(WHOA_BUILD_GLSDL)
int32_t OsGuiMessageBox(void* parentWindow, int32_t style, const char* message, const char* title) {
return OsSDLGuiMessageBox(parentWindow, style, message, title);
}
void OsGuiSetWindowTitle(void* window, const char* title) {
return OsSDLGuiSetWindowTitle(window, title);
}
#else
int32_t OsGuiMessageBox(void* parentWindow, int32_t style, const char* message, const char* title) {
return 1;
}
void OsGuiSetWindowTitle(void* window, const char* title) {
}
#endif

66
src/os/linux/Input.cpp Normal file
View file

@ -0,0 +1,66 @@
#include <common/Time.hpp>
#include "os/Input.hpp"
#include "os/Queue.hpp"
#include "os/internal/Queue.hpp"
#if defined(WHOA_BUILD_GLSDL)
#include "os/sdl/Input.hpp"
#endif
int32_t OsInputGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3) {
#if defined(WHOA_BUILD_GLSDL)
if (OsSDLInputActive()) {
return OsSDLInputGet(id, param0, param1, param2, param3);
}
#endif
if (s_queueTail == s_queueHead) {
return 0;
}
OsQueueSetParam(3, OsGetAsyncTimeMs());
return OsQueueGet(id, param0, param1, param2, param3);
}
void OsInputSetWindowResizeLock(int32_t resizeLock) {
#if defined(WHOA_BUILD_GLSDL)
OsSDLInputSetWindowResizeLock(resizeLock);
#endif
}
void OsInputInitialize() {
}
bool OsInputIsUsingCocoaEventLoop() {
return false;
}
void OsInputPostEvent(OSINPUT id, int32_t param0, int32_t param1, int32_t param2, int32_t param3) {
// TODO
}
void OsInputSetMouseMode(OS_MOUSE_MODE mode) {
// TODO
}
void OsInputGetMousePosition(int32_t* x, int32_t* y) {
#if defined(WHOA_BUILD_GLSDL)
if (OsSDLInputActive()) {
OsSDLInputGetMousePosition(x, y);
return;
}
#endif
if (x) {
*x = 0;
}
if (y) {
*y = 0;
}
}
int32_t OsWindowProc(void* window, uint32_t message, uintptr_t wparam, intptr_t lparam) {
return 0;
}

11
src/os/mac/Clipboard.mm Normal file
View file

@ -0,0 +1,11 @@
#include "os/Clipboard.hpp"
char* OsClipboardGetString() {
// TODO
return nullptr;
}
int32_t OsClipboardPutString(const char* str) {
// TODO
return 0;
}

View file

@ -1,5 +1,5 @@
#ifndef OS_COMPAT_MAC_HPP
#define OS_COMPAT_MAC_HPP
#ifndef OS_MAC_COMPAT_HPP
#define OS_MAC_COMPAT_HPP
#ifdef __OBJC__
#include <AppKit/AppKit.h>

11
src/os/mac/Debug.mm Normal file
View file

@ -0,0 +1,11 @@
#include "os/Debug.hpp"
#include <cstdio>
#include <cstdarg>
void OsOutputDebugString(const char* format, ...) {
// TODO
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
return;
}

27
src/os/mac/Gui.mm Normal file
View file

@ -0,0 +1,27 @@
#include "os/Gui.hpp"
void* OsGuiGetWindow(int32_t type) {
return nullptr;
}
bool OsGuiIsModifierKeyDown(int32_t key) {
// TODO
return false;
}
int32_t OsGuiProcessMessage(void* message) {
return 0;
}
void OsGuiSetGxWindow(void* window) {
// TODO
}
int32_t OsGuiMessageBox(void* parentWindow, int32_t style, const char* message, const char* title) {
// TODO
return 1;
}
void OsGuiSetWindowTitle(void* window, const char* title) {
// TODO
}

74
src/os/mac/Input.mm Normal file
View file

@ -0,0 +1,74 @@
#include "os/Input.hpp"
#include <common/Time.hpp>
#include <tempest/Vector.hpp>
double Input::s_savedMouseSpeed;
static C2iVector s_mousePos;
int32_t OsInputGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3) {
// TODO
// Unknown logic
if (!OsInputIsUsingCocoaEventLoop()) {
// Legacy Carbon input handling
return 0;
}
// TODO
// Steelseries WoW Mouse logic
if (Input::s_queueTail == Input::s_queueHead) {
return 0;
}
OsQueueSetParam(3, OsGetAsyncTimeMs());
auto queue_result = OsQueueGet(id, param0, param1, param2, param3);
if (queue_result) {
if (*id == OS_INPUT_MOUSE_MOVE) {
s_mousePos.x = *param1;
s_mousePos.y = *param2;
}
}
return queue_result;
}
void OsInputSetWindowResizeLock(int32_t resizeLock) {
s_WindowResizeLock = resizeLock;
}
void OsInputInitialize() {
// Legacy Carbon input handling
// if (!byte_143EFE0) {
// Carbon_OsInputRegisterHICommandHandler(0x71756974, sub_A4F230);
// }
MacClient::SetMouseCoalescingEnabled(true);
Input::s_savedMouseSpeed = MacClient::GetMouseSpeed();
}
bool OsInputIsUsingCocoaEventLoop() {
// TODO
return true;
}
void OsInputSetMouseMode(OS_MOUSE_MODE mode) {
// TODO
}
void OsInputGetMousePosition(int32_t* x, int32_t* y) {
if (x) {
*x = s_mousePos.x;
}
if (y) {
*y = s_mousePos.y;
}
}
int32_t OsWindowProc(void* window, uint32_t message, uintptr_t wparam, intptr_t lparam) {
return 0;
}

15
src/os/sdl/Clipboard.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "os/sdl/Clipboard.hpp"
#include <storm/String.hpp>
#include <SDL3/SDL_clipboard.h>
char* OsSDLClipboardGetString() {
auto sdlText = SDL_GetClipboardText();
auto text = SStrDupA(sdlText, __FILE__, __LINE__);
SDL_free(sdlText);
return text;
}
int32_t OsSDLClipboardPutString(const char* str) {
return SDL_SetClipboardText(str);
}

10
src/os/sdl/Clipboard.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef OS_SDL_CLIPBOARD_HPP
#define OS_SDL_CLIPBOARD_HPP
#include <cstdint>
char* OsSDLClipboardGetString();
int32_t OsSDLClipboardPutString(const char* str);
#endif

62
src/os/sdl/Gui.cpp Normal file
View file

@ -0,0 +1,62 @@
#include "os/sdl/Gui.hpp"
#include <SDL3/SDL.h>
static SDL_MessageBoxButtonData s_ok_button = { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "OK" };
static SDL_MessageBoxButtonData s_ok_cancel_buttons[2] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "OK" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" }
};
static SDL_MessageBoxButtonData s_yes_no_buttons[2] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Yes" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 1, "No" }
};
static SDL_MessageBoxButtonData s_yes_no_cancel_buttons[3] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Yes" },
{ 0, 1, "No" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" }
};
int32_t OsSDLGuiMessageBox(void* parentWindow, int32_t style, const char* message, const char* title) {
SDL_MessageBoxData box;
box.flags = 0;
box.window = static_cast<SDL_Window*>(parentWindow);
box.title = title;
box.message = message;
box.colorScheme = nullptr;
switch (style) {
case 0:
// type = MB_OK;
box.numbuttons = 1;
box.buttons = &s_ok_button;
break;
case 1:
// type = MB_OKCANCEL;
box.numbuttons = 2;
box.buttons = s_ok_cancel_buttons;
break;
case 2:
// type = MB_YESNO;
box.numbuttons = 2;
box.buttons = s_yes_no_buttons;
break;
case 3:
// type = MB_YESNOCANCEL;
box.numbuttons = 3;
box.buttons = s_yes_no_cancel_buttons;
break;
}
int buttonid;
SDL_ShowMessageBox(&box, &buttonid);
return static_cast<int32_t>(buttonid);
}
void OsSDLGuiSetWindowTitle(void* window, const char* title) {
if (window) {
SDL_SetWindowTitle(static_cast<SDL_Window*>(window), title);
}
}

10
src/os/sdl/Gui.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef OS_SDL_GUI_HPP
#define OS_SDL_GUI_HPP
#include <cstdint>
int32_t OsSDLGuiMessageBox(void* parentWindow, int32_t style, const char* message, const char* title);
void OsSDLGuiSetWindowTitle(void* window, const char* title);
#endif

317
src/os/sdl/Input.cpp Normal file
View file

@ -0,0 +1,317 @@
#include <unordered_map>
#include "event/Types.hpp"
#include "os/internal/Input.hpp"
#include "os/internal/Queue.hpp"
#include "os/sdl/Input.hpp"
#include "os/Input.hpp"
#include "os/Queue.hpp"
#include "os/Gui.hpp"
#include "gx/Device.hpp"
#include "gx/Window.hpp"
#include <storm/Unicode.hpp>
#include <SDL3/SDL.h>
static const std::unordered_map<SDL_Scancode, KEY> s_keyConversion = {
{ SDL_SCANCODE_LSHIFT, KEY_LSHIFT },
{ SDL_SCANCODE_RSHIFT, KEY_RSHIFT },
{ SDL_SCANCODE_LCTRL, KEY_LCONTROL },
{ SDL_SCANCODE_RCTRL, KEY_RCONTROL },
{ SDL_SCANCODE_LALT, KEY_LALT },
{ SDL_SCANCODE_RALT, KEY_RALT },
{ SDL_SCANCODE_SPACE, KEY_SPACE },
{ SDL_SCANCODE_0, KEY_0 },
{ SDL_SCANCODE_1, KEY_1 },
{ SDL_SCANCODE_2, KEY_2 },
{ SDL_SCANCODE_3, KEY_3 },
{ SDL_SCANCODE_4, KEY_4 },
{ SDL_SCANCODE_5, KEY_5 },
{ SDL_SCANCODE_6, KEY_6 },
{ SDL_SCANCODE_7, KEY_7 },
{ SDL_SCANCODE_8, KEY_8 },
{ SDL_SCANCODE_9, KEY_9 },
{ SDL_SCANCODE_A, KEY_A },
{ SDL_SCANCODE_B, KEY_B },
{ SDL_SCANCODE_C, KEY_C },
{ SDL_SCANCODE_D, KEY_D },
{ SDL_SCANCODE_E, KEY_E },
{ SDL_SCANCODE_F, KEY_F },
{ SDL_SCANCODE_G, KEY_G },
{ SDL_SCANCODE_H, KEY_H },
{ SDL_SCANCODE_I, KEY_I },
{ SDL_SCANCODE_J, KEY_J },
{ SDL_SCANCODE_K, KEY_K },
{ SDL_SCANCODE_L, KEY_L },
{ SDL_SCANCODE_M, KEY_M },
{ SDL_SCANCODE_N, KEY_N },
{ SDL_SCANCODE_O, KEY_O },
{ SDL_SCANCODE_P, KEY_P },
{ SDL_SCANCODE_Q, KEY_Q },
{ SDL_SCANCODE_R, KEY_R },
{ SDL_SCANCODE_S, KEY_S },
{ SDL_SCANCODE_T, KEY_T },
{ SDL_SCANCODE_U, KEY_U },
{ SDL_SCANCODE_V, KEY_V },
{ SDL_SCANCODE_W, KEY_W },
{ SDL_SCANCODE_X, KEY_X },
{ SDL_SCANCODE_Y, KEY_Y },
{ SDL_SCANCODE_Z, KEY_Z },
{ SDL_SCANCODE_GRAVE, KEY_TILDE },
{ SDL_SCANCODE_KP_0, KEY_NUMPAD0 },
{ SDL_SCANCODE_KP_1, KEY_NUMPAD1 },
{ SDL_SCANCODE_KP_2, KEY_NUMPAD2 },
{ SDL_SCANCODE_KP_3, KEY_NUMPAD3 },
{ SDL_SCANCODE_KP_4, KEY_NUMPAD4 },
{ SDL_SCANCODE_KP_5, KEY_NUMPAD5 },
{ SDL_SCANCODE_KP_6, KEY_NUMPAD6 },
{ SDL_SCANCODE_KP_7, KEY_NUMPAD7 },
{ SDL_SCANCODE_KP_8, KEY_NUMPAD8 },
{ SDL_SCANCODE_KP_9, KEY_NUMPAD9 },
{ SDL_SCANCODE_KP_PLUS, KEY_NUMPAD_PLUS },
{ SDL_SCANCODE_KP_MINUS, KEY_NUMPAD_MINUS },
{ SDL_SCANCODE_KP_MULTIPLY, KEY_NUMPAD_MULTIPLY },
{ SDL_SCANCODE_KP_DIVIDE, KEY_NUMPAD_DIVIDE },
{ SDL_SCANCODE_KP_DECIMAL, KEY_NUMPAD_DECIMAL },
{ SDL_SCANCODE_KP_EQUALS, KEY_NUMPAD_EQUALS },
{ SDL_SCANCODE_EQUALS, KEY_PLUS },
{ SDL_SCANCODE_MINUS, KEY_MINUS },
{ SDL_SCANCODE_LEFTBRACKET, KEY_BRACKET_OPEN },
{ SDL_SCANCODE_RIGHTBRACKET, KEY_BRACKET_CLOSE },
{ SDL_SCANCODE_SLASH, KEY_SLASH },
{ SDL_SCANCODE_BACKSLASH, KEY_BACKSLASH },
{ SDL_SCANCODE_SEMICOLON, KEY_SEMICOLON },
{ SDL_SCANCODE_APOSTROPHE, KEY_APOSTROPHE },
{ SDL_SCANCODE_COMMA, KEY_COMMA },
{ SDL_SCANCODE_PERIOD, KEY_PERIOD },
{ SDL_SCANCODE_ESCAPE, KEY_ESCAPE },
{ SDL_SCANCODE_RETURN, KEY_ENTER },
{ SDL_SCANCODE_BACKSPACE, KEY_BACKSPACE },
{ SDL_SCANCODE_TAB, KEY_TAB },
{ SDL_SCANCODE_LEFT, KEY_LEFT },
{ SDL_SCANCODE_UP, KEY_UP },
{ SDL_SCANCODE_RIGHT, KEY_RIGHT },
{ SDL_SCANCODE_DOWN, KEY_DOWN },
{ SDL_SCANCODE_INSERT, KEY_INSERT },
{ SDL_SCANCODE_DELETE, KEY_DELETE },
{ SDL_SCANCODE_HOME, KEY_HOME },
{ SDL_SCANCODE_END, KEY_END },
{ SDL_SCANCODE_PAGEUP, KEY_PAGEUP },
{ SDL_SCANCODE_PAGEDOWN, KEY_PAGEDOWN },
{ SDL_SCANCODE_CAPSLOCK, KEY_CAPSLOCK },
{ SDL_SCANCODE_NUMLOCKCLEAR, KEY_NUMLOCK },
{ SDL_SCANCODE_SCROLLLOCK, KEY_SCROLLLOCK },
{ SDL_SCANCODE_PAUSE, KEY_PAUSE },
{ SDL_SCANCODE_PRINTSCREEN, KEY_PRINTSCREEN },
{ SDL_SCANCODE_F1, KEY_F1 },
{ SDL_SCANCODE_F2, KEY_F2 },
{ SDL_SCANCODE_F3, KEY_F3 },
{ SDL_SCANCODE_F4, KEY_F4 },
{ SDL_SCANCODE_F5, KEY_F5 },
{ SDL_SCANCODE_F6, KEY_F6 },
{ SDL_SCANCODE_F7, KEY_F7 },
{ SDL_SCANCODE_F8, KEY_F8 },
{ SDL_SCANCODE_F9, KEY_F9 },
{ SDL_SCANCODE_F10, KEY_F10 },
{ SDL_SCANCODE_F11, KEY_F11 },
{ SDL_SCANCODE_F12, KEY_F12 },
{ SDL_SCANCODE_F13, KEY_F13 },
{ SDL_SCANCODE_F14, KEY_F14 },
{ SDL_SCANCODE_F15, KEY_F15 },
{ SDL_SCANCODE_F16, KEY_F16 },
{ SDL_SCANCODE_F17, KEY_F17 },
{ SDL_SCANCODE_F18, KEY_F18 },
{ SDL_SCANCODE_F19, KEY_F19 }
};
static MOUSEBUTTON s_buttonConversion[16] = {
MOUSE_BUTTON_NONE,
MOUSE_BUTTON_LEFT,
MOUSE_BUTTON_MIDDLE,
MOUSE_BUTTON_RIGHT,
MOUSE_BUTTON_XBUTTON1,
MOUSE_BUTTON_XBUTTON2,
MOUSE_BUTTON_XBUTTON3,
MOUSE_BUTTON_XBUTTON4,
MOUSE_BUTTON_XBUTTON5,
MOUSE_BUTTON_XBUTTON6,
MOUSE_BUTTON_XBUTTON7,
MOUSE_BUTTON_XBUTTON8,
MOUSE_BUTTON_XBUTTON9,
MOUSE_BUTTON_XBUTTON10,
MOUSE_BUTTON_XBUTTON11,
MOUSE_BUTTON_XBUTTON12
};
bool OsSDLInputActive() {
return OsGuiGetWindow(0) != nullptr && GxDevApi() == GxApi_GLSDL;
}
void OsSDLInputSetWindowResizeLock(int32_t resizeLock) {
s_WindowResizeLock = resizeLock;
auto window = static_cast<SDL_Window*>(OsGuiGetWindow(0));
if (window) {
SDL_SetWindowResizable(window, !resizeLock);
}
}
void OsSDLInputGetMousePosition(int32_t* x, int32_t *y) {
float mouseX;
float mouseY;
if (s_osMouseMode == OS_MOUSE_MODE_RELATIVE) {
SDL_GetMouseState(&mouseX, &mouseY);
} else {
SDL_GetGlobalMouseState(&mouseX, &mouseY);
}
if (x) {
*x = static_cast<int32_t>(mouseX);
}
if (y) {
*y = static_cast<int32_t>(mouseY);
}
return;
}
bool ConvertScancode(SDL_Scancode scancode, KEY& key) {
// What key does this SDL scancode correspond to?
auto lookup = s_keyConversion.find(scancode);
if (lookup != s_keyConversion.end()) {
// Scancode was found
key = lookup->second;
return true;
}
return false;
}
int32_t OsSDLInputGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3) {
*id = static_cast<OSINPUT>(-1);
if (s_queueTail != s_queueHead) {
OsQueueGet(id, param0, param1, param2, param3);
return 1;
}
auto gxWindow = OsGuiGetWindow(0);
auto sdlWindow = static_cast<SDL_Window*>(gxWindow);
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP: {
KEY key;
if (ConvertScancode(event.key.scancode, key)) {
*id = event.type == SDL_EVENT_KEY_UP ? OS_INPUT_KEY_UP : OS_INPUT_KEY_DOWN;
*param0 = key;
return 1;
}
break;
}
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP: {
// Is this an up or down mouse click?
*id = event.type == SDL_EVENT_MOUSE_BUTTON_UP ? OS_INPUT_MOUSE_UP : OS_INPUT_MOUSE_DOWN;
// XY click coordinates
auto x = static_cast<int32_t>(event.button.x);
auto y = static_cast<int32_t>(event.button.y);
// Convert SDL button index into internal MOUSEBUTTON ID
auto buttonIndex = event.button.button;
if (buttonIndex > 15) {
break;
}
auto button = s_buttonConversion[buttonIndex];
*param0 = button;
*param1 = x;
*param2 = y;
*param3 = 0;
return 1;
}
case SDL_EVENT_MOUSE_MOTION: {
auto x = static_cast<int32_t>(event.motion.x);
auto y = static_cast<int32_t>(event.motion.y);
*id = OS_INPUT_MOUSE_MOVE;
*param0 = 0;
*param1 = x;
*param2 = y;
*param3 = 0;
return 1;
}
case SDL_EVENT_TEXT_INPUT: {
// text input string holding one or more UTF-8 characters
auto text = reinterpret_cast<const uint8_t*>(event.text.text);
// Because SDL_TextInputEvent can hold multiple UTF-8 characters
// split into UTF-8 characters
while (*text != '\0') {
// byte size of current UTF-8 character
int32_t charactersize = 0;
// Read UTF-8 character
auto character = static_cast<int32_t>(SUniSGetUTF8(text, &charactersize));
if (character < 0) {
// Cancel in case of invalid input
break;
}
// Push character to input queue
OsQueuePut(OS_INPUT_CHAR, character, 1, 0, 0);
// Advance text pointer
text += charactersize;
}
// deque first character if any
if (s_queueTail != s_queueHead) {
OsQueueGet(id, param0, param1, param2, param3);
return 1;
}
// continue loop if there was not a single character in this event
break;
}
case SDL_EVENT_WINDOW_RESIZED: {
auto width = static_cast<int32_t>(event.window.data1);
auto height = static_cast<int32_t>(event.window.data2);
// static_cast<CGxDeviceGLSDL*>(g_theGxDevicePtr)->Resize(width, height);
*id = OS_INPUT_SIZE;
*param0 = width;
*param1 = height;
auto bounds = GetSavedWindowBounds();
Rect newBounds = {
bounds->top,
bounds->left,
static_cast<int16_t>(bounds->top + height),
static_cast<int16_t>(bounds->left + width)
};
SetSavedWindowBounds(newBounds);
break;
}
case SDL_EVENT_QUIT: {
*id = OS_INPUT_CLOSE;
return 1;
}
default:
break;
}
}
return 0;
}

14
src/os/sdl/Input.hpp Normal file
View file

@ -0,0 +1,14 @@
#ifndef OS_SDL_INPUT_HPP
#define OS_SDL_INPUT_HPP
#include "os/Input.hpp"
bool OsSDLInputActive();
void OsSDLInputSetWindowResizeLock(int32_t resizeLock);
int32_t OsSDLInputGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3);
void OsSDLInputGetMousePosition(int32_t* x, int32_t *y);
#endif

73
src/os/win/Clipboard.cpp Normal file
View file

@ -0,0 +1,73 @@
#include "os/Clipboard.hpp"
#include "gx/Device.hpp"
#include "gx/Types.hpp"
#include <storm/String.hpp>
#include <windows.h>
char* IOsClipboardGetString(HWND window) {
if (!OpenClipboard(window)) {
return nullptr;
}
auto handle = GetClipboardData(CF_UNICODETEXT);
auto widestr = reinterpret_cast<LPWSTR>(GlobalLock(handle));
if (!widestr) {
return nullptr;
}
// Get length of UTF-8 text
auto length = WideCharToMultiByte(CP_UTF8, 0, widestr, -1, nullptr, 0, nullptr, nullptr);
auto str = reinterpret_cast<char*>(SMemAlloc(length, __FILE__, __LINE__, 0x0));
// Convert UTF-16 into UTF-8
WideCharToMultiByte(CP_UTF8, 0, widestr, -1, str, length, nullptr, nullptr);
str[length - 1] = '\0';
GlobalUnlock(handle);
CloseClipboard();
return str;
}
int32_t IOsClipboardPutString(const char* str, HWND window) {
// Allocate twice as large UTF-16 string
auto length = SStrLen(str) + 1;
auto memory = GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE | GMEM_ZEROINIT, length * 2);
if (!memory) {
return 0;
}
// Get access to global memory
auto widestr = reinterpret_cast<LPWSTR>(GlobalLock(memory));
if (!widestr) {
GlobalFree(memory);
return 0;
}
// Convert UTF-8 string into UTF-16 string
MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, str, length - 1, widestr, length * 2);
// Finish writing to memory
GlobalUnlock(memory);
// Copy UTF-16 text into clipboard
if (!OpenClipboard(nullptr)) {
GlobalFree(memory);
return 0;
}
if (!EmptyClipboard()) {
CloseClipboard();
GlobalFree(memory);
return 0;
}
SetClipboardData(CF_UNICODETEXT, memory);
CloseClipboard();
return 1;
}
int32_t OsClipboardPutString(const char* str) {
return IOsClipboardPutString(str, GxDevApi() == GxApi_GLSDL ? static_cast<HWND>(nullptr) : static_cast<HWND>(GxDevWindow()));
}
char* OsClipboardGetString() {
return IOsClipboardGetString(
GxDevApi() == GxApi_GLSDL ?
static_cast<HWND>(nullptr) :
static_cast<HWND>(GxDevWindow()));
}

15
src/os/win/Debug.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "os/Debug.hpp"
#include <cstdarg>
#include <cstdio>
#include <windows.h>
void OsOutputDebugString(const char* format, ...) {
char buffer[256];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
OutputDebugString(buffer);
return;
}

82
src/os/win/Gui.cpp Normal file
View file

@ -0,0 +1,82 @@
#include "os/Gui.hpp"
#include <storm/String.hpp>
#include <storm/Unicode.hpp>
#include <windows.h>
#include <malloc.h>
static void* s_GxDevWindow;
void* OsGuiGetWindow(int32_t type) {
switch (type) {
case 0:
return s_GxDevWindow;
case 1:
return GetActiveWindow();
case 2:
return GetForegroundWindow();
default:
return nullptr;
}
}
bool OsGuiIsModifierKeyDown(int32_t key) {
// TODO
return false;
}
int32_t OsGuiProcessMessage(void* message) {
// TODO
return 0;
}
void OsGuiSetGxWindow(void* window) {
s_GxDevWindow = window;
}
int32_t OsGuiMessageBox(void* parentWindow, int32_t style, const char* message, const char* title) {
auto messagelength = SUniConvertUTF8to16Len(reinterpret_cast<const uint8_t*>(message), STORM_MAX_STR, nullptr);
auto messagebuffer = reinterpret_cast<uint16_t*>(alloca(2 * messagelength));
SUniConvertUTF8to16(messagebuffer, messagelength, reinterpret_cast<const uint8_t*>(message), STORM_MAX_STR, nullptr, nullptr);
title = title ? title : "";
auto titlelength = SUniConvertUTF8to16Len(reinterpret_cast<const uint8_t*>(title), STORM_MAX_STR, nullptr);
auto titlebuffer = reinterpret_cast<uint16_t*>(alloca(2 * titlelength));
SUniConvertUTF8to16(titlebuffer, titlelength, reinterpret_cast<const uint8_t*>(title), STORM_MAX_STR, nullptr, nullptr);
UINT type = 0;
switch (style) {
case 0:
type = 0;
break;
case 1:
type = MB_OKCANCEL;
break;
case 2:
type = MB_YESNO;
break;
case 3:
type = MB_YESNOCANCEL;
break;
}
auto result = MessageBoxW(
static_cast<HWND>(parentWindow),
reinterpret_cast<LPCWSTR>(messagebuffer),
reinterpret_cast<LPCWSTR>(titlebuffer),
type);
if (result == IDOK || result == IDYES) {
return 0;
}
if (result == IDNO) {
return 1;
}
return 2;
}
void OsGuiSetWindowTitle(void* window, const char* title) {
auto widetitlelength = SUniConvertUTF8to16Len(reinterpret_cast<const uint8_t*>(title), STORM_MAX_STR, nullptr);
auto widetitle = reinterpret_cast<uint16_t*>(alloca(2 * widetitlelength));
SUniConvertUTF8to16(widetitle, widetitlelength, reinterpret_cast<const uint8_t*>(title), STORM_MAX_STR, nullptr, nullptr);
SetWindowTextW(static_cast<HWND>(window), reinterpret_cast<LPCWSTR>(widetitle));
}

786
src/os/win/Input.cpp Normal file
View file

@ -0,0 +1,786 @@
#include "os/Input.hpp"
#include "os/Queue.hpp"
#include "os/Gui.hpp"
#include "os/internal/Input.hpp"
#include "os/internal/Queue.hpp"
#include "event/Types.hpp"
#include <storm/Error.hpp>
#include <tempest/Vector.hpp>
#include <windows.h>
#if defined(WHOA_BUILD_GLSDL)
#include "os/sdl/Input.hpp"
#endif
static const uint32_t s_latin5lookup[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D,
0x7E, 0x7F, 0x20AC, 0x20, 0x201A, 0x192, 0x201E, 0x2026, 0x2020,
0x2021, 0x2C6, 0x2030, 0x160, 0x2039, 0x152, 0x20, 0x20, 0x20,
0x20, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x2DC,
0x2122, 0x161, 0x203A, 0x153, 0x20, 0x20, 0x178, 0x0A0, 0x0A1,
0x0A2, 0x0A3, 0x0A4, 0x0A5, 0x0A6, 0x0A7, 0x0A8, 0x0A9, 0x0AA,
0x0AB, 0x0AC, 0x0AD, 0x0AE, 0x0AF, 0x0B0, 0x0B1, 0x0B2, 0x0B3,
0x0B4, 0x0B5, 0x0B6, 0x0B7, 0x0B8, 0x0B9, 0x0BA, 0x0BB, 0x0BC,
0x0BD, 0x0BE, 0x0BF, 0x0C0, 0x0C1, 0x0C2, 0x0C3, 0x0C4, 0x0C5,
0x0C6, 0x0C7, 0x0C8, 0x0C9, 0x0CA, 0x0CB, 0x0CC, 0x0CD, 0x0CE,
0x0CF, 0x11E, 0x0D1, 0x0D2, 0x0D3, 0x0D4, 0x0D5, 0x0D6, 0x0D7,
0x0D8, 0x0D9, 0x0DA, 0x0DB, 0x0DC, 0x130, 0x15E, 0x0DF, 0x0E0,
0x0E1, 0x0E2, 0x0E3, 0x0E4, 0x0E5, 0x0E6, 0x0E7, 0x0E8, 0x0E9,
0x0EA, 0x0EB, 0x0EC, 0x0ED, 0x0EE, 0x0EF, 0x11F, 0x0F1, 0x0F2,
0x0F3, 0x0F4, 0x0F5, 0x0F6, 0x0F7, 0x0F8, 0x0F9, 0x0FA, 0x0FB,
0x0FC, 0x131, 0x15F, 0x0FF
};
static const uint32_t s_latin1lookup[32] = {
0x0FFFE, 0x0FFFE, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021,
0x2C6, 0x2030, 0x160, 0x2039, 0x152, 0x0FFFE, 0x0FFFE, 0x0FFFE,
0x0FFFE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x2DC, 0x2122, 0x161, 0x203A, 0x153, 0x0FFFE, 0x0FFFE, 0x178
};
static const uint32_t s_cyrilliclookup[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D,
0x7E, 0x7F, 0x402, 0x403, 0x201A, 0x453, 0x201E, 0x2026, 0x2020,
0x2021, 0x20AC, 0x2030, 0x409, 0x2039, 0x40A, 0x40C, 0x40B, 0x40F,
0x452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x20,
0x2122, 0x459, 0x203A, 0x45A, 0x45C, 0x45B, 0x45F, 0x0A0, 0x40E,
0x45E, 0x408, 0x0A4, 0x490, 0x0A6, 0x0A7, 0x401, 0x0A9, 0x404,
0x0AB, 0x0AC, 0x0AD, 0x0AE, 0x407, 0x0B0, 0x0B1, 0x406, 0x456,
0x491, 0x0B5, 0x0B6, 0x0B7, 0x451, 0x2116, 0x454, 0x0BB, 0x458,
0x405, 0x455, 0x457, 0x410, 0x411, 0x412, 0x413, 0x414, 0x415,
0x416, 0x417, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E,
0x41F, 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427,
0x428, 0x429, 0x42A, 0x42B, 0x42C, 0x42D, 0x42E, 0x42F, 0x430,
0x431, 0x432, 0x433, 0x434, 0x435, 0x436, 0x437, 0x438, 0x439,
0x43A, 0x43B, 0x43C, 0x43D, 0x43E, 0x43F, 0x440, 0x441, 0x442,
0x443, 0x444, 0x445, 0x446, 0x447, 0x448, 0x449, 0x44A, 0x44B,
0x44C, 0x44D, 0x44E, 0x44F
};
static const uint32_t s_latin2lookup[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D,
0x7E, 0x7F, 0x20AC, 0x0FFFE, 0x201A, 0x0FFFE, 0x201E, 0x2026, 0x2020,
0x2021, 0x0FFFE, 0x2030, 0x160, 0x2039, 0x15A, 0x164, 0x17D, 0x179,
0x0FFFE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0FFFE,
0x2122, 0x161, 0x203A, 0x15B, 0x165, 0x17E, 0x17A, 0x0A0, 0x2C7,
0x2D8, 0x141, 0x0A4, 0x104, 0x0A6, 0x0A7, 0x0A8, 0x0A9, 0x15E,
0x0AB, 0x0AC, 0x0AD, 0x0AE, 0x17B, 0x0B0, 0x0B1, 0x2DB, 0x142,
0x0B4, 0x0B5, 0x0B6, 0x0B7, 0x0B8, 0x105, 0x15F, 0x0BB, 0x13D,
0x2DD, 0x13E, 0x17C, 0x154, 0x0C1, 0x0C2, 0x102, 0x0C4, 0x139,
0x106, 0x0C7, 0x10C, 0x0C9, 0x118, 0x0CB, 0x11A, 0x0CD, 0x0CE,
0x10E, 0x110, 0x143, 0x147, 0x0D3, 0x0D4, 0x150, 0x0D6, 0x0D7,
0x158, 0x16E, 0x0DA, 0x170, 0x0DC, 0x0DD, 0x162, 0x0DF, 0x155,
0x0E1, 0x0E2, 0x103, 0x0E4, 0x13A, 0x107, 0x0E7, 0x10D, 0x0E9,
0x119, 0x0EB, 0x11B, 0x0ED, 0x0EE, 0x10F, 0x111, 0x144, 0x148,
0x0F3, 0x0F4, 0x151, 0x0F6, 0x0F7, 0x159, 0x16F, 0x0FA, 0x171,
0x0FC, 0x0FD, 0x163, 0x2D9
};
static const uint32_t s_thailookup[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D,
0x7E, 0x7F, 0x20AC, 0x20, 0x20, 0x20, 0x20, 0x2026, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0A0, 0x0E01,
0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09, 0x0E0A,
0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, 0x0E10, 0x0E11, 0x0E12, 0x0E13,
0x0E14, 0x0E15, 0x0E16, 0x0E17, 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C,
0x0E1D, 0x0E1E, 0x0E1F, 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25,
0x0E26, 0x0E27, 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E,
0x0E2F, 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37,
0x0E38, 0x0E39, 0x0E3A, 0x20, 0x20, 0x20, 0x20, 0x0E3F, 0x0E40,
0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, 0x0E48, 0x0E49,
0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, 0x0E50, 0x0E51, 0x0E52,
0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, 0x0E58, 0x0E59, 0x0E5A, 0x0E5B,
0x20, 0x20, 0x20, 0x20
};
static RECT s_defaultWindowRect;
static int32_t s_savedResize;
static HWND s_mouseWnd;
static C2iVector s_mousePos;
void CenterMouse() {
// TODO
}
void RestoreMouse() {
// TODO
}
void SaveMouse(HWND window, const POINT& pt) {
s_mouseWnd = window;
s_mousePos.x = static_cast<int32_t>(pt.x);
s_mousePos.y = static_cast<int32_t>(pt.y);
}
int32_t ConvertButton(uint32_t message, uintptr_t wparam, MOUSEBUTTON* button) {
switch (message) {
case WM_NCLBUTTONDOWN:
case WM_NCLBUTTONUP:
case WM_NCLBUTTONDBLCLK:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP: {
*button = MOUSE_BUTTON_LEFT;
return 1;
}
case WM_NCRBUTTONDOWN:
case WM_NCRBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP: {
*button = MOUSE_BUTTON_RIGHT;
return 1;
}
case WM_NCMBUTTONDOWN:
case WM_NCMBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP: {
*button = MOUSE_BUTTON_MIDDLE;
return 1;
}
case WM_NCXBUTTONDOWN:
case WM_NCXBUTTONUP:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP: {
switch (GET_XBUTTON_WPARAM(wparam)) {
case XBUTTON1: {
*button = MOUSE_BUTTON_XBUTTON1;
return 1;
}
case XBUTTON2: {
*button = MOUSE_BUTTON_XBUTTON2;
return 1;
}
default: {
*button = MOUSE_BUTTON_NONE;
return 0;
}
}
}
default: {
*button = MOUSE_BUTTON_NONE;
return 0;
}
}
}
int32_t ConvertKeyCode(uint32_t vkey, KEY* key) {
if (vkey >= VK_F1 && vkey <= VK_F12) {
*key = static_cast<KEY>(KEY_F1 + (vkey - VK_F1));
return 1;
}
if (vkey >= 0x30 && vkey <= 0x39) {
*key = static_cast<KEY>(KEY_0 + (vkey - 0x30));
return 1;
}
switch (vkey) {
case VK_BACK: {
*key = KEY_BACKSPACE;
return 1;
}
case VK_TAB: {
*key = KEY_TAB;
return 1;
}
case VK_RETURN: {
*key = KEY_ENTER;
return 1;
}
case VK_PAUSE: {
*key = KEY_PAUSE;
return 1;
}
case VK_CAPITAL: {
*key = KEY_CAPSLOCK;
return 1;
}
case VK_ESCAPE: {
*key = KEY_ESCAPE;
return 1;
}
case VK_SPACE: {
*key = KEY_SPACE;
return 1;
}
case VK_PRIOR: {
*key = KEY_PAGEUP;
return 1;
}
case VK_NEXT: {
*key = KEY_PAGEDOWN;
return 1;
}
case VK_END: {
*key = KEY_END;
return 1;
}
case VK_HOME: {
*key = KEY_HOME;
return 1;
}
case VK_LEFT: {
*key = KEY_LEFT;
return 1;
}
case VK_UP: {
*key = KEY_UP;
return 1;
}
case VK_RIGHT: {
*key = KEY_RIGHT;
return 1;
}
case VK_DOWN: {
*key = KEY_DOWN;
return 1;
}
case VK_SNAPSHOT: {
*key = KEY_PRINTSCREEN;
return 1;
}
case VK_INSERT: {
*key = KEY_INSERT;
return 1;
}
case VK_DELETE: {
*key = KEY_DELETE;
return 1;
}
case VK_NUMPAD0:
case VK_NUMPAD1:
case VK_NUMPAD2:
case VK_NUMPAD3:
case VK_NUMPAD4:
case VK_NUMPAD5:
case VK_NUMPAD6:
case VK_NUMPAD7:
case VK_NUMPAD8:
case VK_NUMPAD9: {
*key = static_cast<KEY>(KEY_NUMPAD0 + (vkey - VK_NUMPAD0));
return 1;
}
case VK_MULTIPLY: {
*key = KEY_NUMPAD_MULTIPLY;
return 1;
}
case VK_ADD: {
*key = KEY_NUMPAD_PLUS;
return 1;
}
case VK_SUBTRACT: {
*key = KEY_NUMPAD_MINUS;
return 1;
}
case VK_DECIMAL: {
*key = KEY_NUMPAD_DECIMAL;
return 1;
}
case VK_DIVIDE: {
*key = KEY_NUMPAD_DIVIDE;
return 1;
}
case VK_NUMLOCK: {
*key = KEY_NUMLOCK;
return 1;
}
case VK_SCROLL: {
*key = KEY_SCROLLLOCK;
return 1;
}
case VK_LSHIFT: {
*key = KEY_LSHIFT;
return 1;
}
case VK_RSHIFT: {
*key = KEY_RSHIFT;
return 1;
}
case VK_LCONTROL: {
*key = KEY_LCONTROL;
return 1;
}
case VK_RCONTROL: {
*key = KEY_RCONTROL;
return 1;
}
case VK_LMENU: {
*key = KEY_LALT;
return 1;
}
case VK_RMENU: {
*key = KEY_RALT;
return 1;
}
default: {
auto character = MapVirtualKey(vkey, MAPVK_VK_TO_CHAR);
*key = static_cast<KEY>(character);
if (character && character <= 0xFF) {
return 1;
} else {
return 0;
}
}
}
}
bool ProcessMouseEvent(MOUSEBUTTON button, uint32_t message, HWND hwnd, OSINPUT id) {
POINT mousePos;
if (s_osMouseMode == OS_MOUSE_MODE_RELATIVE) {
// TODO
} else {
GetCursorPos(&mousePos);
ScreenToClient(hwnd, &mousePos);
}
OsQueuePut(id, button, mousePos.x, mousePos.y, 0);
return message == WM_XBUTTONDOWN
|| message == WM_XBUTTONUP
|| message == WM_NCXBUTTONDOWN
|| message == WM_NCXBUTTONUP;
}
int32_t HandleMouseDown(uint32_t message, uintptr_t wparam, bool* xbutton, HWND hwnd) {
MOUSEBUTTON button;
if (!ConvertButton(message, wparam, &button)) {
return 0;
}
if (s_osButtonState == 0) {
SetCapture(hwnd);
}
s_osButtonState |= button;
auto xb = ProcessMouseEvent(button, message, hwnd, OS_INPUT_MOUSE_DOWN);
if (xbutton) {
*xbutton = xb;
}
return 1;
}
int32_t HandleMouseUp(uint32_t message, uintptr_t wparam, bool* xbutton, HWND hwnd) {
MOUSEBUTTON button;
if (!ConvertButton(message, wparam, &button)) {
return 0;
}
s_osButtonState &= ~button;
if (s_osButtonState == 0) {
// TODO
ReleaseCapture();
// TODO
}
auto xb = ProcessMouseEvent(button, message, hwnd, OS_INPUT_MOUSE_UP);
if (xbutton) {
*xbutton = xb;
}
return 1;
}
void OsInputSetWindowResizeLock(int32_t resizeLock) {
#if defined(WHOA_BUILD_GLSDL)
if (OsSDLInputActive()) {
OsSDLInputSetWindowResizeLock(resizeLock);
}
#endif
s_WindowResizeLock = resizeLock;
}
void OsInputInitialize() {
s_numlockState = GetAsyncKeyState(144);
int32_t mouseSpeed = 10;
SystemParametersInfoA(SPI_GETMOUSESPEED, 0, &mouseSpeed, 0);
s_savedMouseSpeed = mouseSpeed;
}
int32_t OsInputGet(OSINPUT* id, int32_t* param0, int32_t* param1, int32_t* param2, int32_t* param3) {
#if defined(WHOA_BUILD_GLSDL)
if (OsSDLInputActive()) {
// SDL handles input events for us
return OsSDLInputGet(id, param0, param1, param2, param3);
}
#endif
*id = static_cast<OSINPUT>(-1);
if (s_savedResize) {
auto hwnd = static_cast<HWND>(OsGuiGetWindow(0));
if (!IsIconic(hwnd)) {
s_savedResize = 0;
RECT windowRect;
GetWindowRect(hwnd, &windowRect);
auto style = GetWindowLong(hwnd, GWL_STYLE);
RECT clientArea = { 0, 0, 0, 0 };
AdjustWindowRectEx(&clientArea, style, false, 0);
auto width = windowRect.right - clientArea.right - (windowRect.left - clientArea.left);
auto height = windowRect.bottom - clientArea.bottom - (windowRect.top - clientArea.top);
if (s_defaultWindowRect.right != width || s_defaultWindowRect.bottom != height) {
s_defaultWindowRect.left = 0;
s_defaultWindowRect.top = 0;
s_defaultWindowRect.right = width;
s_defaultWindowRect.bottom = height;
*id = OS_INPUT_SIZE;
*param0 = width;
*param1 = height;
*param2 = 0;
*param3 = 0;
return 1;
}
}
}
if (s_queueTail != s_queueHead) {
OsQueueGet(id, param0, param1, param2, param3);
return 1;
}
// TODO Sub8714B0(dwordB1C220);
while (true) {
MSG msg;
auto peekResult = PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE);
if (s_queueTail != s_queueHead) {
break;
}
if (!peekResult) {
return 0;
}
if (!GetMessage(&msg, nullptr, 0, 0)) {
*id = OS_INPUT_SHUTDOWN;
break;
}
if (OsGuiProcessMessage(&msg)) {
break;
}
if (s_queueTail != s_queueHead) {
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
if (s_queueTail != s_queueHead) {
break;
}
}
OsQueueGet(id, param0, param1, param2, param3);
return 1;
}
void OsInputSetMouseMode(OS_MOUSE_MODE mode) {
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(mode < OS_MOUSE_MODES);
STORM_VALIDATE_END_VOID;
if (s_osMouseMode == mode) {
return;
}
if (mode == OS_MOUSE_MODE_NORMAL) {
s_osMouseMode = mode;
RestoreMouse();
} else if (mode == OS_MOUSE_MODE_RELATIVE) {
s_osMouseMode = mode;
CenterMouse();
}
}
void OsInputGetMousePosition(int32_t* x, int32_t* y) {
#if defined(WHOA_BUILD_GLSDL)
if (OsSDLInputActive()) {
SDLInputGetMousePosition(x, y);
return;
}
#endif
// Get HWND created by CGxDevice
auto window = static_cast<HWND>(OsGuiGetWindow(0));
POINT pt;
GetCursorPos(&pt);
ScreenToClient(window, &pt);
if (s_osMouseMode != OS_MOUSE_MODE_RELATIVE) {
SaveMouse(window, pt);
}
// Provide mouse position to caller
if (x) {
*x = static_cast<int32_t>(pt.x);
}
if (y) {
*y = static_cast<int32_t>(pt.y);
}
}
uint32_t OsInputGetCodePage() {
return ::GetACP();
}
int32_t OsWindowProc(void* window, uint32_t message, uintptr_t wparam, intptr_t lparam) {
static uint32_t s_codepage = 0;
auto hwnd = static_cast<HWND>(window);
// TODO
switch (message) {
// TODO handle remaining message types
case WM_SIZE:
case WM_DISPLAYCHANGE: {
s_savedResize = lparam;
break;
}
case WM_ACTIVATE: {
auto isMinimized = IsIconic(hwnd);
auto isActive = wparam != WA_INACTIVE;
s_windowFocused = isActive && !isMinimized;
// TODO capture
// TODO mouse speed
OsQueuePut(OS_INPUT_FOCUS, s_windowFocused != 0, 0, 0, 0);
break;
}
case WM_CLOSE: {
OsQueuePut(OS_INPUT_CLOSE, 0, 0, 0, 0);
return 0;
}
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP: {
auto keyDown = message == WM_KEYDOWN || message == WM_SYSKEYDOWN;
if (wparam == VK_SHIFT) {
// TODO
} else if (wparam == VK_CONTROL) {
// TODO
} else if (wparam == VK_MENU) {
// TODO
}
KEY key;
if (ConvertKeyCode(wparam, &key)) {
OsQueuePut(keyDown ? OS_INPUT_KEY_DOWN : OS_INPUT_KEY_UP, key, LOWORD(lparam), 0, 0);
// Alt + F4
if (key == KEY_F4 && OsGuiIsModifierKeyDown(2)) {
break;
}
return 0;
}
break;
}
case WM_CHAR: {
if (wparam < 32) {
break;
}
uint32_t character = wparam;
if (wparam >= 128) {
if (s_codepage == 0) {
s_codepage = OsInputGetCodePage();
}
if (s_codepage == 1254) {
// ANSI Turkish; Turkish (Windows)
character = s_latin5lookup[wparam % 256];
} else if (s_codepage == 1252 && wparam < 0xA0) {
// ANSI Latin 1; Western European (Windows)
character = s_latin1lookup[wparam % 32];
} else if (s_codepage == 1251) {
// ANSI Cyrillic; Cyrillic (Windows)
character = s_cyrilliclookup[wparam % 256];
} else if (s_codepage == 1250) {
// ANSI Central European; Central European (Windows)
character = s_latin2lookup[wparam % 256];
} else if (s_codepage == 874) {
// Thai (Windows)
character = s_thailookup[wparam % 256];
}
}
OsQueuePut(OS_INPUT_CHAR, character, LOWORD(lparam), 0, 0);
return 0;
}
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN: {
bool xbutton;
if (HandleMouseDown(message, wparam, &xbutton, hwnd)) {
// Normally, a processed button down message should return 0
// In the case of xbuttons, a processed button down message should return 1
// See: https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-xbuttondown
return xbutton ? 1 : 0;
}
break;
}
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP: {
if (message == WM_LBUTTONUP) {
// TODO
}
bool xbutton;
if (HandleMouseUp(message, wparam, &xbutton, hwnd)) {
// Normally, a processed button down message should return 0
// In the case of xbuttons, a processed button down message should return 1
// See: https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-xbuttondown
return xbutton ? 1 : 0;
}
break;
}
case WM_MOUSEMOVE: {
// TODO
if (s_osMouseMode == OS_MOUSE_MODE_RELATIVE) {
// TODO
} else {
POINT mousePos;
GetCursorPos(&mousePos);
ScreenToClient(hwnd, &mousePos);
OsQueuePut(OS_INPUT_MOUSE_MOVE, 0, mousePos.x, mousePos.y, 0);
SaveMouse(hwnd, mousePos);
}
break;
}
default:
break;
}
// TODO
return DefWindowProc(static_cast<HWND>(window), message, wparam, lparam);
}