Skip to content

Commit cd6bae9

Browse files
committed
feat(win32): implement cursor mode and raw input
- implement PL_CURSOR_MODE_HIDDEN and PL_CURSOR_DISABLED in pl_set_cursor_mode - implement pl_set_raw_mouse_input using win32 RAWINPUTDEVICE - add WM_INPUT handler for raw mouse delta evvents - update pl_get_window_capabilities to advertise new supported modes - add set_mouse_pos to plWindowsI struct and pl.h declerations
1 parent 8933ea0 commit cd6bae9

4 files changed

Lines changed: 79 additions & 10 deletions

File tree

src/pl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,7 @@ pl__load_core_apis(void)
16671667
tWindowApi.set_cursor_mode = pl_set_cursor_mode;
16681668
tWindowApi.get_cursor_mode = pl_get_cursor_mode;
16691669
tWindowApi.set_raw_mouse_input = pl_set_raw_mouse_input;
1670+
tWindowApi.set_mouse_pos = pl_set_mouse_pos;
16701671
tWindowApi.set_fullscreen = pl_set_fullscreen;
16711672
tWindowApi.get_capabilities = pl_get_window_capabilities;
16721673

src/pl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ PL_API bool pl_window_get_attribute (plWindow*, plWindowAttribute, plWindowAttri
197197
PL_API bool pl_window_set_cursor_mode (plWindow*, plCursorMode);
198198
PL_API plCursorMode pl_window_get_cursor_mode (plWindow*);
199199
PL_API bool pl_window_set_raw_mouse_input (plWindow*, bool);
200+
PL_API bool pl_window_set_mouse_pos (plWindow*, plVec2);
200201

201202
// full screen modes
202203
PL_API bool pl_window_set_fullscreen(plWindow*, const plFullScreenDesc*);
@@ -328,6 +329,7 @@ typedef struct _plWindowI
328329
bool (*set_cursor_mode) (plWindow*, plCursorMode);
329330
plCursorMode (*get_cursor_mode) (plWindow*);
330331
bool (*set_raw_mouse_input) (plWindow*, bool);
332+
bool (*set_mouse_pos) (plWindow*, plVec2);
331333

332334
// full screen modes
333335
bool (*set_fullscreen)(plWindow*, const plFullScreenDesc*);

src/pl_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ bool pl_get_window_attribute(plWindow*, plWindowAttribute
7171
bool pl_set_cursor_mode(plWindow*, plCursorMode);
7272
plCursorMode pl_get_cursor_mode(plWindow*);
7373
bool pl_set_raw_mouse_input(plWindow*, bool);
74+
bool pl_set_mouse_pos(plWindow*, plVec2);
7475
bool pl_set_fullscreen(plWindow*, const plFullScreenDesc*);
7576
const plWindowCapabilities* pl_get_window_capabilities(void);
7677

src/pl_main_win32.c

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,29 @@ pl__windows_procedure(HWND tHwnd, UINT tMsg, WPARAM tWParam, LPARAM tLParam)
428428
break;
429429
}
430430

431+
case WM_INPUT:
432+
{
433+
UINT uSize = 0;
434+
GetRawInputData((HRAWINPUT)tLParam, RID_INPUT, NULL, &uSize, sizeof(RAWINPUTHEADER));
435+
if(uSize > 0)
436+
{
437+
RAWINPUT* ptRaw = (RAWINPUT*)malloc(uSize);
438+
if(GetRawInputData((HRAWINPUT)tLParam, RID_INPUT, ptRaw, &uSize, sizeof(RAWINPUTHEADER)) == uSize)
439+
{
440+
if(ptRaw->header.dwType == RIM_TYPEMOUSE)
441+
{
442+
// get current cursor pos in client space and feed as mouse event
443+
POINT tCurrent;
444+
GetCursorPos(&tCurrent);
445+
ScreenToClient(tHwnd, &tCurrent);
446+
gptIOI->add_mouse_pos_event((float)tCurrent.x, (float)tCurrent.y);
447+
}
448+
}
449+
free(ptRaw);
450+
}
451+
break;
452+
}
453+
431454
case WM_MOUSEMOVE:
432455
{
433456
tMouseHandle = tHwnd;
@@ -844,7 +867,31 @@ pl_get_window_attribute(plWindow* ptWindow, plWindowAttribute tAttribute, plWind
844867
bool
845868
pl_set_cursor_mode(plWindow* ptWindow, plCursorMode tMode)
846869
{
847-
return tMode == PL_CURSOR_MODE_NORMAL;
870+
HWND tHwnd = (HWND)ptWindow->_pBackendData;
871+
872+
if(tMode == PL_CURSOR_MODE_NORMAL)
873+
{
874+
ShowCursor(true);
875+
ClipCursor(NULL);
876+
return true;
877+
}
878+
else if(tMode == PL_CURSOR_MODE_HIDDEN)
879+
{
880+
ShowCursor(false);
881+
ClipCursor(NULL);
882+
return true;
883+
}
884+
else if(tMode == PL_CURSOR_MODE_DISABLED)
885+
{
886+
ShowCursor(false);
887+
RECT tRect;
888+
GetClientRect(tHwnd, &tRect);
889+
MapWindowPoints(tHwnd, NULL, (POINT*)&tRect, 2);
890+
ClipCursor(&tRect);
891+
return true;
892+
}
893+
894+
return false;
848895
}
849896

850897
plCursorMode
@@ -856,7 +903,23 @@ pl_get_cursor_mode(plWindow* ptWindow)
856903
bool
857904
pl_set_raw_mouse_input(plWindow* ptWindow, bool bValue)
858905
{
859-
return !bValue;
906+
HWND tHwnd = (HWND)ptWindow->_pBackendData;
907+
RAWINPUTDEVICE tRid = {
908+
.usUsagePage = 0x01,
909+
.usUsage = 0x02,
910+
.dwFlags = bValue ? 0 : RIDEV_REMOVE,
911+
.hwndTarget = bValue ? tHwnd : NULL
912+
};
913+
return RegisterRawInputDevices(&tRid, 1, sizeof(tRid));
914+
}
915+
916+
bool
917+
pl_set_mouse_pos(plWindow* ptWindow, plVec2 tPos)
918+
{
919+
HWND tHwnd = (HWND)ptWindow->_pBackendData;
920+
POINT tPoint = {(LONG)tPos.x, (LONG)tPos.y};
921+
ClientToScreen(tHwnd, &tPoint);
922+
return SetCursorPos(tPoint.x, tPoint.y);
860923
}
861924

862925
bool
@@ -868,29 +931,31 @@ pl_set_fullscreen(plWindow* ptWindow, const plFullScreenDesc* tDesc)
868931
const plWindowCapabilities*
869932
pl_get_window_capabilities(void)
870933
{
871-
static plWindowCapabilities tCapabilities = {};
934+
static plWindowCapabilities tCapabilities = {0};
872935

873-
tCapabilities.uCursorModeCount = 1;
874-
tCapabilities.uAttributeCount = 1;
936+
tCapabilities.uCursorModeCount = 3;
937+
tCapabilities.uAttributeCount = 1;
875938
tCapabilities.uFullScreenModeCount = 2;
876939

877940
static const plWindowAttribute atSupportedAttributes[] = {
878941
-1
879942
};
880943

881944
static const plCursorMode atSupportedCursorModes[] = {
882-
PL_CURSOR_MODE_NORMAL
945+
PL_CURSOR_MODE_NORMAL,
946+
PL_CURSOR_MODE_HIDDEN,
947+
PL_CURSOR_MODE_DISABLED
883948
};
884949

885950
static const plFullScreenMode atSupportedScreenModes[] = {
886951
PL_FULLSCREEN_MODE_NONE,
887952
PL_FULLSCREEN_MODE_EXCLUSIVE
888953
};
889954

890-
tCapabilities.atCursorModes = atSupportedCursorModes;
891-
tCapabilities.atFullScreenModes = atSupportedScreenModes;
892-
tCapabilities.atWindowAttributes = atSupportedAttributes;
893-
tCapabilities.tFlags = PL_WINDOW_CAPABILITY_FLAGS_NONE;
955+
tCapabilities.atCursorModes = atSupportedCursorModes;
956+
tCapabilities.atFullScreenModes = atSupportedScreenModes;
957+
tCapabilities.atWindowAttributes = atSupportedAttributes;
958+
tCapabilities.tFlags = PL_WINDOW_CAPABILITY_FLAGS_RAW_MOUSE_INPUT;
894959

895960
return &tCapabilities;
896961
}

0 commit comments

Comments
 (0)