diff --git a/GlosSITarget/SteamTarget.cpp b/GlosSITarget/SteamTarget.cpp index c275a9e..4d7d276 100644 --- a/GlosSITarget/SteamTarget.cpp +++ b/GlosSITarget/SteamTarget.cpp @@ -16,6 +16,9 @@ limitations under the License. #include "SteamTarget.h" SteamTarget::SteamTarget(int argc, char *argv[]) : window_([this] { run_ = false; }) +SteamTarget::SteamTarget(int argc, char *argv[]) + : window_([this] { run_ = false; }), + detector_([this](bool overlay_open) { onOverlayChanged(overlay_open); }), target_window_handle_(window_.getSystemHandle()) { } @@ -29,3 +32,42 @@ int SteamTarget::run() } return 1; } + +void SteamTarget::onOverlayChanged(bool overlay_open) +{ + window_.setClickThrough(!overlay_open); + focusWindow(target_window_handle_); +} + +void SteamTarget::focusWindow(WindowHandle hndl) +{ +#ifdef _WIN32 + + //MH_DisableHook(&GetForegroundWindow); // TODO: when GetForegroundWindow hooked, unhook! + // store last focused window for later restore + last_foreground_window_ = GetForegroundWindow(); + const DWORD fg_thread = GetWindowThreadProcessId(last_foreground_window_, nullptr); + //MH_EnableHook(&GetForegroundWindow); // TODO: when GetForegroundWindow hooked, re-hook! + + // lot's of ways actually bringing our window to foreground... + const DWORD current_thread = GetCurrentThreadId(); + AttachThreadInput(current_thread, fg_thread, TRUE); + + SetForegroundWindow(hndl); + SetCapture(hndl); + SetFocus(hndl); + SetActiveWindow(hndl); + EnableWindow(hndl, TRUE); + + AttachThreadInput(current_thread, fg_thread, FALSE); + + //try to forcefully set foreground window at least a few times + sf::Clock clock; + while (!SetForegroundWindow(hndl) && clock.getElapsedTime().asMilliseconds() < 20) + { + SetActiveWindow(hndl); + Sleep(1); + } + +#endif +} diff --git a/GlosSITarget/TargetWindow.cpp b/GlosSITarget/TargetWindow.cpp index b9fb3ac..ec12c51 100644 --- a/GlosSITarget/TargetWindow.cpp +++ b/GlosSITarget/TargetWindow.cpp @@ -25,7 +25,7 @@ limitations under the License. #include #endif -static const bool DEV_MODE = true; +static const bool DEV_MODE = false; TargetWindow::TargetWindow(std::function on_close) : on_close_(std::move(on_close)) @@ -97,3 +97,8 @@ void TargetWindow::close() window_.close(); on_close_(); } + +WindowHandle TargetWindow::getSystemHandle() const +{ + return window_.getSystemHandle(); +} diff --git a/GlosSITarget/TargetWindow.h b/GlosSITarget/TargetWindow.h index 2cb850c..f68ce6f 100644 --- a/GlosSITarget/TargetWindow.h +++ b/GlosSITarget/TargetWindow.h @@ -18,6 +18,14 @@ limitations under the License. #include +#ifdef _WIN32 +#include +using WindowHandle = HWND; +#else +using WindowHandle = int; // ??? +#endif + + class TargetWindow { public: explicit TargetWindow(std::function on_close = []() {}); @@ -27,6 +35,8 @@ class TargetWindow { void update(); void close(); + WindowHandle getSystemHandle() const; + private: const std::function on_close_; sf::RenderWindow window_;