Skip to content

Commit

Permalink
feat: add sar_dpi_scale
Browse files Browse the repository at this point in the history
  • Loading branch information
mlugg committed Jul 9, 2022
1 parent a5572d5 commit b748de1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Modules/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "EngineDemoPlayer.hpp"
#include "EngineDemoRecorder.hpp"
#include "Event.hpp"
#include "InputSystem.hpp"
#include "Features/Camera.hpp"
#include "Features/Cvars.hpp"
#include "Features/Demo/DemoParser.hpp"
Expand Down Expand Up @@ -57,6 +58,9 @@ Variable sar_cm_rightwarp("sar_cm_rightwarp", "0", "Fix CM wrongwarp.\n");
REDECL(Engine::Disconnect);
REDECL(Engine::SetSignonState);
REDECL(Engine::ChangeLevel);
#ifndef _WIN32
REDECL(Engine::GetMouseDelta);
#endif
REDECL(Engine::Frame);
REDECL(Engine::PurgeUnusedModels);
REDECL(Engine::OnGameOverlayActivated);
Expand Down Expand Up @@ -305,6 +309,14 @@ DETOUR(Engine::ChangeLevel, const char *s1, const char *s2) {
return Engine::ChangeLevel(thisptr, s1, s2);
}

#ifndef _WIN32
// CVEngineClient::GetMouseDelta
DETOUR_T(void, Engine::GetMouseDelta, int &x, int &y, bool ignore_next) {
Engine::GetMouseDelta(thisptr, x, y, ignore_next);
inputSystem->DPIScaleDeltas(x, y);
}
#endif

void Engine::GetTicks(int &host, int &server, int &client) {
auto &et = this->engineTool;
using _Fn = int(__rescall *)(void *thisptr);
Expand Down Expand Up @@ -773,7 +785,7 @@ DETOUR(Engine::DestroyDebugMesh, int vertCount, Vector *verts) {
}

bool Engine::Init() {
this->engineClient = Interface::Create(this->Name(), "VEngineClient015", false);
this->engineClient = Interface::Create(this->Name(), "VEngineClient015");
this->s_ServerPlugin = Interface::Create(this->Name(), "ISERVERPLUGINHELPERS001", false);

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

#ifndef _WIN32
this->engineClient->Hook(Engine::GetMouseDelta_Hook, Engine::GetMouseDelta, Offsets::GetMouseDelta);
#endif

Memory::Read<_Cbuf_AddText>((uintptr_t)this->ClientCmd + Offsets::Cbuf_AddText, &this->Cbuf_AddText);
#ifndef _WIN32
if (sar.game->Is(SourceGame_EIPRelPIC)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Modules/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class Engine : public Module {
// CVEngineServer::ChangeLevel
DECL_DETOUR(ChangeLevel, const char *s1, const char *s2);

#ifndef _WIN32
// CVEngineClient::GetMouseDelta
DECL_DETOUR_T(void, GetMouseDelta, int &x, int &y, bool ignore_next);
#endif

// CEngine::Frame
DECL_DETOUR(Frame);

Expand Down
32 changes: 32 additions & 0 deletions src/Modules/InputSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "Utils.hpp"

REDECL(InputSystem::SleepUntilInput);
#ifdef _WIN32
REDECL(InputSystem::GetRawMouseAccumulators);
#endif

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

Variable sar_dpi_scale("sar_dpi_scale", "1", 1, "Fraction to scale mouse DPI down by.\n");
void InputSystem::DPIScaleDeltas(int &x, int &y) {
static int saved_x = 0;
static int saved_y = 0;

int scale = sar_dpi_scale.GetInt();
if (scale < 1) scale = 1;

saved_x += x;
saved_y += y;

x = saved_x / scale;
y = saved_y / scale;

saved_x %= scale;
saved_y %= scale;
}

// CInputSystem::SleepUntilInput
DETOUR(InputSystem::SleepUntilInput, int nMaxSleepTimeMS) {
if (sar_disable_no_focus_sleep.GetBool()) {
Expand All @@ -35,12 +56,23 @@ DETOUR(InputSystem::SleepUntilInput, int nMaxSleepTimeMS) {
return InputSystem::SleepUntilInput(thisptr, nMaxSleepTimeMS);
}

#ifdef _WIN32
// CInputSystem::GetRawMouseAccumulators
DETOUR_T(void, InputSystem::GetRawMouseAccumulators, int &x, int &y) {
InputSystem::GetRawMouseAccumulators(thisptr, x, y);
inputSystem->DPIScaleDeltas(x, y);
}
#endif

bool InputSystem::Init() {
this->g_InputSystem = Interface::Create(this->Name(), "InputSystemVersion001");
if (this->g_InputSystem) {
this->StringToButtonCode = this->g_InputSystem->Original<_StringToButtonCode>(Offsets::StringToButtonCode);

this->g_InputSystem->Hook(InputSystem::SleepUntilInput_Hook, InputSystem::SleepUntilInput, Offsets::SleepUntilInput);
#ifdef _WIN32
this->g_InputSystem->Hook(InputSystem::GetRawMouseAccumulators_Hook, InputSystem::GetRawMouseAccumulators, Offsets::GetRawMouseAccumulators);
#endif
this->IsButtonDown = this->g_InputSystem->Original<_IsButtonDown>(Offsets::IsButtonDown);
this->GetCursorPosition = this->g_InputSystem->Original<_GetCursorPosition>(Offsets::GetCursorPosition);
this->SetCursorPosition = this->g_InputSystem->Original<_SetCursorPosition>(Offsets::SetCursorPosition);
Expand Down
7 changes: 7 additions & 0 deletions src/Modules/InputSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,16 @@ class InputSystem : public Module {
void GetCursorPos(int &x, int &y);
void SetCursorPos(int x, int y);

void DPIScaleDeltas(int &x, int &y);

// CInputSystem::SleepUntilInput
DECL_DETOUR(SleepUntilInput, int nMaxSleepTimeMS);

#ifdef _WIN32
// CInputSystem::GetRawMouseAccumulators
DECL_DETOUR_T(void, GetRawMouseAccumulators, int &x, int &y);
#endif

bool Init() override;
void Shutdown() override;
const char *Name() override { return MODULE("inputsystem"); }
Expand Down
2 changes: 2 additions & 0 deletions src/OffsetsData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ OFFSET_DEFAULT(GetSaveDirName, 124, 124)
OFFSET_DEFAULT(ExecuteClientCmd, 104, 104)
OFFSET_DEFAULT(GetActiveSplitScreenPlayerSlot, 127, 127)
OFFSET_DEFAULT(GetSteamAPIContext, 177, 178)
OFFSET_DEFAULT(GetMouseDelta, -1, 161) // This method only exists on Linux
OFFSET_DEFAULT(IsPaused, 86, 86)
OFFSET_DEFAULT(DebugDrawPhysCollide, 75, 75)
OFFSET_DEFAULT(Con_IsVisible, 11, 11)
Expand Down Expand Up @@ -75,6 +76,7 @@ OFFSET_DEFAULT(CreateNewTextureID, 41, 41)
// CInputSystem
OFFSET_DEFAULT(StringToButtonCode, 31, 31)
OFFSET_DEFAULT(SleepUntilInput, 33, 33)
OFFSET_DEFAULT(GetRawMouseAccumulators, 50, 52)
OFFSET_DEFAULT(IsButtonDown, 14, 14)
OFFSET_DEFAULT(GetCursorPosition, 45, 45)
OFFSET_DEFAULT(SetCursorPosition, 38, 38)
Expand Down

0 comments on commit b748de1

Please sign in to comment.