Skip to content

Commit

Permalink
Update nemirtingas overlay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr_Goldberg committed Aug 13, 2022
1 parent b72b4da commit 743a810
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 79 deletions.
20 changes: 10 additions & 10 deletions overlay_experimental/Renderer_Detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Renderer_Detector
bool vulkan_hooked;

Base_Hook detection_hooks;
Renderer_Hook* renderer_hook;
ingame_overlay::Renderer_Hook* renderer_hook;
DX12_Hook* dx12_hook;
DX11_Hook* dx11_hook;
DX10_Hook* dx10_hook;
Expand Down Expand Up @@ -238,7 +238,7 @@ class Renderer_Detector
void HookDetected(T*& detected_renderer)
{
detection_hooks.UnhookAll();
renderer_hook = static_cast<Renderer_Hook*>(detected_renderer);
renderer_hook = static_cast<ingame_overlay::Renderer_Hook*>(detected_renderer);
detected_renderer = nullptr;
detection_done = true;
DestroyHWND();
Expand Down Expand Up @@ -1032,7 +1032,7 @@ class Renderer_Detector
}

public:
Renderer_Hook* detect_renderer(std::chrono::milliseconds timeout)
ingame_overlay::Renderer_Hook* detect_renderer(std::chrono::milliseconds timeout)
{
std::unique_lock<std::timed_mutex> detection_lock(detector_mutex, std::defer_lock);

Expand Down Expand Up @@ -1173,7 +1173,7 @@ class Renderer_Detector
bool openglx_hooked;
//bool vulkan_hooked;

Renderer_Hook* renderer_hook;
ingame_overlay::Renderer_Hook* renderer_hook;
OpenGLX_Hook* openglx_hook;

bool detection_done;
Expand All @@ -1191,7 +1191,7 @@ class Renderer_Detector
if (gladLoaderLoadGL() >= GLAD_MAKE_VERSION(3, 1))
{
inst->detection_hooks.UnhookAll();
inst->renderer_hook = static_cast<Renderer_Hook*>(Inst()->openglx_hook);
inst->renderer_hook = static_cast<ingame_overlay::Renderer_Hook*>(Inst()->openglx_hook);
inst->openglx_hook = nullptr;
inst->detection_done = true;
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ class Renderer_Detector
}

public:
Renderer_Hook* detect_renderer(std::chrono::milliseconds timeout)
ingame_overlay::Renderer_Hook* detect_renderer(std::chrono::milliseconds timeout)
{
std::pair<const char*, void(Renderer_Detector::*)(std::string const&)> libraries[]{
std::pair<const char*, void(Renderer_Detector::*)(std::string const&)>{OpenGLX_Hook::DLL_NAME, &Renderer_Detector::hook_openglx},
Expand Down Expand Up @@ -1352,7 +1352,7 @@ class Renderer_Detector

bool opengl_hooked;

Renderer_Hook* renderer_hook;
ingame_overlay::Renderer_Hook* renderer_hook;
OpenGL_Hook* opengl_hook;

bool detection_done;
Expand All @@ -1368,7 +1368,7 @@ class Renderer_Detector
if (gladLoaderLoadGL() >= GLAD_MAKE_VERSION(2, 0))
{
inst->detection_hooks.UnhookAll();
inst->renderer_hook = static_cast<Renderer_Hook*>(Inst()->opengl_hook);
inst->renderer_hook = static_cast<ingame_overlay::Renderer_Hook*>(Inst()->opengl_hook);
inst->opengl_hook = nullptr;
inst->detection_done = true;
}
Expand Down Expand Up @@ -1417,7 +1417,7 @@ class Renderer_Detector
}

public:
Renderer_Hook* detect_renderer(std::chrono::milliseconds timeout)
ingame_overlay::Renderer_Hook* detect_renderer(std::chrono::milliseconds timeout)
{
std::pair<const char*, void(Renderer_Detector::*)(std::string const&)> libraries[]{
std::pair<const char*, void(Renderer_Detector::*)(std::string const&)>{OpenGL_Hook::DLL_NAME, &Renderer_Detector::hook_opengl}
Expand Down Expand Up @@ -1495,7 +1495,7 @@ Renderer_Detector* Renderer_Detector::instance = nullptr;

namespace ingame_overlay {

std::future<Renderer_Hook*> DetectRenderer(std::chrono::milliseconds timeout)
std::future<ingame_overlay::Renderer_Hook*> DetectRenderer(std::chrono::milliseconds timeout)
{
return std::async(std::launch::async, &Renderer_Detector::detect_renderer, Renderer_Detector::Inst(), timeout);
}
Expand Down
14 changes: 13 additions & 1 deletion overlay_experimental/Renderer_Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
#include <string>
#include <memory>
#include <cstdint>
#include <set>

namespace ingame_overlay {

enum class ToggleKey
{
SHIFT, CTRL, ALT,
TAB,
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
};

class Renderer_Hook
{
Expand All @@ -37,11 +47,13 @@ class Renderer_Hook
std::function<void()> OverlayProc;
std::function<void(bool)> OverlayHookReady;

virtual bool StartHook(std::function<bool(bool)> key_combination_callback) = 0;
virtual bool StartHook(std::function<bool(bool)> key_combination_callback, std::set<ToggleKey> toggle_keys) = 0;
virtual bool IsStarted() = 0;
// Returns a Handle to the renderer image ressource or nullptr if it failed to create the resource, the handle can be used in ImGui's Image calls, image_buffer must be RGBA ordered
virtual std::weak_ptr<uint64_t> CreateImageResource(const void* image_data, uint32_t width, uint32_t height) = 0;
virtual void ReleaseImageResource(std::weak_ptr<uint64_t> resource) = 0;

virtual std::string GetLibraryName() const = 0;
};

}
4 changes: 2 additions & 2 deletions overlay_experimental/linux/OpenGLX_Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ OpenGLX_Hook* OpenGLX_Hook::_inst = nullptr;

constexpr decltype(OpenGLX_Hook::DLL_NAME) OpenGLX_Hook::DLL_NAME;

bool OpenGLX_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
bool OpenGLX_Hook::StartHook(std::function<bool(bool)> key_combination_callback, std::set<ingame_overlay::ToggleKey> toggle_keys)
{
if (!_Hooked)
{
Expand All @@ -39,7 +39,7 @@ bool OpenGLX_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
return false;
}

if (!X11_Hook::Inst()->StartHook(key_combination_callback))
if (!X11_Hook::Inst()->StartHook(key_combination_callback, toggle_keys))
return false;

_X11Hooked = true;
Expand Down
4 changes: 2 additions & 2 deletions overlay_experimental/linux/OpenGLX_Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <GL/glx.h>

class OpenGLX_Hook :
public Renderer_Hook,
public ingame_overlay::Renderer_Hook,
public Base_Hook
{
public:
Expand Down Expand Up @@ -59,7 +59,7 @@ class OpenGLX_Hook :

virtual ~OpenGLX_Hook();

virtual bool StartHook(std::function<bool(bool)> key_combination_callback);
virtual bool StartHook(std::function<bool(bool)> key_combination_callback, std::set<ingame_overlay::ToggleKey> toggle_keys);
virtual bool IsStarted();
static OpenGLX_Hook* Inst();
virtual std::string GetLibraryName() const;
Expand Down
106 changes: 92 additions & 14 deletions overlay_experimental/linux/X11_Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,62 @@ constexpr decltype(X11_Hook::DLL_NAME) X11_Hook::DLL_NAME;

X11_Hook* X11_Hook::_inst = nullptr;

bool X11_Hook::StartHook(std::function<bool(bool)>& _key_combination_callback)
uint32_t ToggleKeyToNativeKey(ingame_overlay::ToggleKey k)
{
struct {
ingame_overlay::ToggleKey lib_key;
uint32_t native_key;
} mapping[] = {
{ ingame_overlay::ToggleKey::ALT , XK_Alt_L },
{ ingame_overlay::ToggleKey::CTRL , XK_Control_L },
{ ingame_overlay::ToggleKey::SHIFT, XK_Shift_L },
{ ingame_overlay::ToggleKey::TAB , XK_Tab },
{ ingame_overlay::ToggleKey::F1 , XK_F1 },
{ ingame_overlay::ToggleKey::F2 , XK_F2 },
{ ingame_overlay::ToggleKey::F3 , XK_F3 },
{ ingame_overlay::ToggleKey::F4 , XK_F4 },
{ ingame_overlay::ToggleKey::F5 , XK_F5 },
{ ingame_overlay::ToggleKey::F6 , XK_F6 },
{ ingame_overlay::ToggleKey::F7 , XK_F7 },
{ ingame_overlay::ToggleKey::F8 , XK_F8 },
{ ingame_overlay::ToggleKey::F9 , XK_F9 },
{ ingame_overlay::ToggleKey::F10 , XK_F10 },
{ ingame_overlay::ToggleKey::F11 , XK_F11 },
{ ingame_overlay::ToggleKey::F12 , XK_F12 },
};

for (auto const& item : mapping)
{
if (item.lib_key == k)
return item.native_key;
}

return 0;
}

bool GetKeyState(Display* d, KeySym keySym, char szKey[32])
{
int iKeyCodeToFind = XKeysymToKeycode(d, keySym);

return szKey[iKeyCodeToFind / 8] & (1 << (iKeyCodeToFind % 8));
}

bool X11_Hook::StartHook(std::function<bool(bool)>& _key_combination_callback, std::set<ingame_overlay::ToggleKey> const& toggle_keys)
{
if (!_Hooked)
{
if (!_key_combination_callback)
{
SPDLOG_ERROR("Failed to hook X11: No key combination callback.");
return false;
}

if (toggle_keys.empty())
{
SPDLOG_ERROR("Failed to hook X11: No key combination.");
return false;
}

void* hX11 = System::Library::GetLibraryHandle(DLL_NAME);
if (hX11 == nullptr)
{
Expand Down Expand Up @@ -60,6 +112,16 @@ bool X11_Hook::StartHook(std::function<bool(bool)>& _key_combination_callback)
SPDLOG_INFO("Hooked X11");

_KeyCombinationCallback = std::move(_key_combination_callback);

for (auto& key : toggle_keys)
{
uint32_t k = ToggleKeyToNativeKey(key);
if (k != 0)
{
_NativeKeyCombination.insert(k);
}
}

_Hooked = true;

UnhookAll();
Expand Down Expand Up @@ -140,33 +202,47 @@ int X11_Hook::_CheckForOverlay(Display *d, int num_events)
static Time prev_time = {};
X11_Hook* inst = Inst();

if( inst->_Initialized )
char szKey[32];

if( _Initialized )
{
XEvent event;
while(num_events)
{
bool skip_input = inst->_KeyCombinationCallback(false);
bool skip_input = _KeyCombinationCallback(false);

XPeekEvent(d, &event);
ImGui_ImplX11_EventHandler(event);

// Is the event is a key press
if (event.type == KeyPress)
if (event.type == KeyPress || event.type == KeyRelease)
{
// Tab is pressed and was not pressed before
if (event.xkey.keycode == XKeysymToKeycode(d, XK_Tab) && event.xkey.state & ShiftMask)
XQueryKeymap(d, szKey);
int key_count = 0;
for (auto const& key : inst->_NativeKeyCombination)
{
// if key TAB is held, don't make the overlay flicker :p
if (event.xkey.time != prev_time)
if (GetKeyState(d, key, szKey))
++key_count;
}

if (key_count == inst->_NativeKeyCombination.size())
{// All shortcut keys are pressed
if (!inst->_KeyCombinationPushed)
{
skip_input = true;
inst->_KeyCombinationCallback(true);
if (inst->_KeyCombinationCallback(true))
{
skip_input = true;
// Save the last known cursor pos when opening the overlay
// so we can spoof the GetCursorPos return value.
//inst->GetCursorPos(&inst->_SavedCursorPos);
}
inst->_KeyCombinationPushed = true;
}
}
}
else if(event.type == KeyRelease && event.xkey.keycode == XKeysymToKeycode(d, XK_Tab))
{
prev_time = event.xkey.time;
else
{
inst->_KeyCombinationPushed = false;
}
}

if (!skip_input || !IgnoreEvent(event))
Expand Down Expand Up @@ -215,6 +291,7 @@ X11_Hook::X11_Hook() :
_Initialized(false),
_Hooked(false),
_GameWnd(0),
_KeyCombinationPushed(false),
XEventsQueued(nullptr),
XPending(nullptr)
{
Expand All @@ -241,3 +318,4 @@ std::string X11_Hook::GetLibraryName() const
{
return LibraryName;
}

10 changes: 7 additions & 3 deletions overlay_experimental/linux/X11_Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class X11_Hook :
bool _Initialized;
Window _GameWnd;

// In (bool): Is toggle wanted
// Out(bool): Is the overlay visible, if true, inputs will be disabled
std::function<bool(bool)> _KeyCombinationCallback;
std::set<uint32_t> _NativeKeyCombination;
bool _KeyCombinationPushed;

// Functions
X11_Hook();
int _CheckForOverlay(Display *d, int num_events);
Expand All @@ -52,8 +58,6 @@ class X11_Hook :
static int MyXEventsQueued(Display * display, int mode);
static int MyXPending(Display* display);

std::function<bool(bool)> _KeyCombinationCallback;

public:
std::string LibraryName;

Expand All @@ -65,7 +69,7 @@ class X11_Hook :

Window GetGameWnd() const{ return _GameWnd; }

bool StartHook(std::function<bool(bool)>& key_combination_callback);
bool StartHook(std::function<bool(bool)>& key_combination_callback, std::set<ingame_overlay::ToggleKey> const& toggle_keys);
static X11_Hook* Inst();
virtual std::string GetLibraryName() const;
};
3 changes: 2 additions & 1 deletion overlay_experimental/steam_overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,8 @@ void Steam_Overlay::RunCallbacks()
_renderer->OverlayProc = std::bind(&Steam_Overlay::OverlayProc, this);
auto callback = std::bind(&Steam_Overlay::OpenOverlayHook, this, std::placeholders::_1);
PRINT_DEBUG("start renderer\n", _renderer);
bool started = _renderer->StartHook(callback);
std::set<ingame_overlay::ToggleKey> keys = {ingame_overlay::ToggleKey::SHIFT, ingame_overlay::ToggleKey::TAB};
bool started = _renderer->StartHook(callback, keys);
PRINT_DEBUG("tried to start renderer %u\n", started);
}

Expand Down
4 changes: 2 additions & 2 deletions overlay_experimental/steam_overlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class Steam_Overlay

std::recursive_mutex overlay_mutex;
std::atomic<bool> i_have_lobby;
std::future<Renderer_Hook*> future_renderer;
Renderer_Hook* _renderer;
std::future<ingame_overlay::Renderer_Hook*> future_renderer;
ingame_overlay::Renderer_Hook* _renderer;

Steam_Overlay(Steam_Overlay const&) = delete;
Steam_Overlay(Steam_Overlay&&) = delete;
Expand Down
4 changes: 2 additions & 2 deletions overlay_experimental/windows/DX10_Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ inline void SafeRelease(T*& pUnk)
}
}

bool DX10_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
bool DX10_Hook::StartHook(std::function<bool(bool)> key_combination_callback, std::set<ingame_overlay::ToggleKey> toggle_keys)
{
if (!_Hooked)
{
Expand All @@ -45,7 +45,7 @@ bool DX10_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
return false;
}

if (!Windows_Hook::Inst()->StartHook(key_combination_callback))
if (!Windows_Hook::Inst()->StartHook(key_combination_callback, toggle_keys))
return false;

_WindowsHooked = true;
Expand Down
Loading

0 comments on commit 743a810

Please sign in to comment.