Skip to content

Commit b748de1

Browse files
committed
feat: add sar_dpi_scale
1 parent a5572d5 commit b748de1

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

src/Modules/Engine.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "EngineDemoPlayer.hpp"
66
#include "EngineDemoRecorder.hpp"
77
#include "Event.hpp"
8+
#include "InputSystem.hpp"
89
#include "Features/Camera.hpp"
910
#include "Features/Cvars.hpp"
1011
#include "Features/Demo/DemoParser.hpp"
@@ -57,6 +58,9 @@ Variable sar_cm_rightwarp("sar_cm_rightwarp", "0", "Fix CM wrongwarp.\n");
5758
REDECL(Engine::Disconnect);
5859
REDECL(Engine::SetSignonState);
5960
REDECL(Engine::ChangeLevel);
61+
#ifndef _WIN32
62+
REDECL(Engine::GetMouseDelta);
63+
#endif
6064
REDECL(Engine::Frame);
6165
REDECL(Engine::PurgeUnusedModels);
6266
REDECL(Engine::OnGameOverlayActivated);
@@ -305,6 +309,14 @@ DETOUR(Engine::ChangeLevel, const char *s1, const char *s2) {
305309
return Engine::ChangeLevel(thisptr, s1, s2);
306310
}
307311

312+
#ifndef _WIN32
313+
// CVEngineClient::GetMouseDelta
314+
DETOUR_T(void, Engine::GetMouseDelta, int &x, int &y, bool ignore_next) {
315+
Engine::GetMouseDelta(thisptr, x, y, ignore_next);
316+
inputSystem->DPIScaleDeltas(x, y);
317+
}
318+
#endif
319+
308320
void Engine::GetTicks(int &host, int &server, int &client) {
309321
auto &et = this->engineTool;
310322
using _Fn = int(__rescall *)(void *thisptr);
@@ -773,7 +785,7 @@ DETOUR(Engine::DestroyDebugMesh, int vertCount, Vector *verts) {
773785
}
774786

775787
bool Engine::Init() {
776-
this->engineClient = Interface::Create(this->Name(), "VEngineClient015", false);
788+
this->engineClient = Interface::Create(this->Name(), "VEngineClient015");
777789
this->s_ServerPlugin = Interface::Create(this->Name(), "ISERVERPLUGINHELPERS001", false);
778790

779791
if (this->engineClient) {
@@ -792,6 +804,10 @@ bool Engine::Init() {
792804
this->GetLevelNameShort = this->engineClient->Original<_GetLevelNameShort>(Offsets::GetLevelNameShort);
793805
this->GetLightForPoint = this->engineClient->Original<_GetLightForPoint>(Offsets::GetLightForPoint);
794806

807+
#ifndef _WIN32
808+
this->engineClient->Hook(Engine::GetMouseDelta_Hook, Engine::GetMouseDelta, Offsets::GetMouseDelta);
809+
#endif
810+
795811
Memory::Read<_Cbuf_AddText>((uintptr_t)this->ClientCmd + Offsets::Cbuf_AddText, &this->Cbuf_AddText);
796812
#ifndef _WIN32
797813
if (sar.game->Is(SourceGame_EIPRelPIC)) {

src/Modules/Engine.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ class Engine : public Module {
161161
// CVEngineServer::ChangeLevel
162162
DECL_DETOUR(ChangeLevel, const char *s1, const char *s2);
163163

164+
#ifndef _WIN32
165+
// CVEngineClient::GetMouseDelta
166+
DECL_DETOUR_T(void, GetMouseDelta, int &x, int &y, bool ignore_next);
167+
#endif
168+
164169
// CEngine::Frame
165170
DECL_DETOUR(Frame);
166171

src/Modules/InputSystem.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "Utils.hpp"
1010

1111
REDECL(InputSystem::SleepUntilInput);
12+
#ifdef _WIN32
13+
REDECL(InputSystem::GetRawMouseAccumulators);
14+
#endif
1215

1316
int InputSystem::GetButton(const char *pString) {
1417
return this->StringToButtonCode(this->g_InputSystem->ThisPtr(), pString);
@@ -26,6 +29,24 @@ void InputSystem::SetCursorPos(int x, int y) {
2629
return this->SetCursorPosition(this->g_InputSystem->ThisPtr(), x, y);
2730
}
2831

32+
Variable sar_dpi_scale("sar_dpi_scale", "1", 1, "Fraction to scale mouse DPI down by.\n");
33+
void InputSystem::DPIScaleDeltas(int &x, int &y) {
34+
static int saved_x = 0;
35+
static int saved_y = 0;
36+
37+
int scale = sar_dpi_scale.GetInt();
38+
if (scale < 1) scale = 1;
39+
40+
saved_x += x;
41+
saved_y += y;
42+
43+
x = saved_x / scale;
44+
y = saved_y / scale;
45+
46+
saved_x %= scale;
47+
saved_y %= scale;
48+
}
49+
2950
// CInputSystem::SleepUntilInput
3051
DETOUR(InputSystem::SleepUntilInput, int nMaxSleepTimeMS) {
3152
if (sar_disable_no_focus_sleep.GetBool()) {
@@ -35,12 +56,23 @@ DETOUR(InputSystem::SleepUntilInput, int nMaxSleepTimeMS) {
3556
return InputSystem::SleepUntilInput(thisptr, nMaxSleepTimeMS);
3657
}
3758

59+
#ifdef _WIN32
60+
// CInputSystem::GetRawMouseAccumulators
61+
DETOUR_T(void, InputSystem::GetRawMouseAccumulators, int &x, int &y) {
62+
InputSystem::GetRawMouseAccumulators(thisptr, x, y);
63+
inputSystem->DPIScaleDeltas(x, y);
64+
}
65+
#endif
66+
3867
bool InputSystem::Init() {
3968
this->g_InputSystem = Interface::Create(this->Name(), "InputSystemVersion001");
4069
if (this->g_InputSystem) {
4170
this->StringToButtonCode = this->g_InputSystem->Original<_StringToButtonCode>(Offsets::StringToButtonCode);
4271

4372
this->g_InputSystem->Hook(InputSystem::SleepUntilInput_Hook, InputSystem::SleepUntilInput, Offsets::SleepUntilInput);
73+
#ifdef _WIN32
74+
this->g_InputSystem->Hook(InputSystem::GetRawMouseAccumulators_Hook, InputSystem::GetRawMouseAccumulators, Offsets::GetRawMouseAccumulators);
75+
#endif
4476
this->IsButtonDown = this->g_InputSystem->Original<_IsButtonDown>(Offsets::IsButtonDown);
4577
this->GetCursorPosition = this->g_InputSystem->Original<_GetCursorPosition>(Offsets::GetCursorPosition);
4678
this->SetCursorPosition = this->g_InputSystem->Original<_SetCursorPosition>(Offsets::SetCursorPosition);

src/Modules/InputSystem.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,16 @@ class InputSystem : public Module {
235235
void GetCursorPos(int &x, int &y);
236236
void SetCursorPos(int x, int y);
237237

238+
void DPIScaleDeltas(int &x, int &y);
239+
238240
// CInputSystem::SleepUntilInput
239241
DECL_DETOUR(SleepUntilInput, int nMaxSleepTimeMS);
240242

243+
#ifdef _WIN32
244+
// CInputSystem::GetRawMouseAccumulators
245+
DECL_DETOUR_T(void, GetRawMouseAccumulators, int &x, int &y);
246+
#endif
247+
241248
bool Init() override;
242249
void Shutdown() override;
243250
const char *Name() override { return MODULE("inputsystem"); }

src/OffsetsData.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ OFFSET_DEFAULT(GetSaveDirName, 124, 124)
2424
OFFSET_DEFAULT(ExecuteClientCmd, 104, 104)
2525
OFFSET_DEFAULT(GetActiveSplitScreenPlayerSlot, 127, 127)
2626
OFFSET_DEFAULT(GetSteamAPIContext, 177, 178)
27+
OFFSET_DEFAULT(GetMouseDelta, -1, 161) // This method only exists on Linux
2728
OFFSET_DEFAULT(IsPaused, 86, 86)
2829
OFFSET_DEFAULT(DebugDrawPhysCollide, 75, 75)
2930
OFFSET_DEFAULT(Con_IsVisible, 11, 11)
@@ -75,6 +76,7 @@ OFFSET_DEFAULT(CreateNewTextureID, 41, 41)
7576
// CInputSystem
7677
OFFSET_DEFAULT(StringToButtonCode, 31, 31)
7778
OFFSET_DEFAULT(SleepUntilInput, 33, 33)
79+
OFFSET_DEFAULT(GetRawMouseAccumulators, 50, 52)
7880
OFFSET_DEFAULT(IsButtonDown, 14, 14)
7981
OFFSET_DEFAULT(GetCursorPosition, 45, 45)
8082
OFFSET_DEFAULT(SetCursorPosition, 38, 38)

0 commit comments

Comments
 (0)