diff --git a/src/spice2x/cfg/api.cpp b/src/spice2x/cfg/api.cpp index ace41c4..461c9d1 100644 --- a/src/spice2x/cfg/api.cpp +++ b/src/spice2x/cfg/api.cpp @@ -1,5 +1,8 @@ #include "api.h" +#include + +#include "launcher/superexit.h" #include "rawinput/rawinput.h" #include "rawinput/piuio.h" #include "util/time.h" @@ -64,18 +67,29 @@ GameAPI::Buttons::State GameAPI::Buttons::getState(rawinput::RawInputManager *ma auto current_button = &_button; auto alternatives = check_alts ? ¤t_button->getAlternatives() : nullptr; unsigned int button_count = 0; + std::optional window_has_focus; while (true) { // naive behavior if (current_button->isNaive()) { + GameAPI::Buttons::State state; + auto vkey = current_button->getVKey(); + + // check for focus + if (vkey != INVALID_VKEY && rawinput::NAIVE_REQUIRE_FOCUS) { + if (!window_has_focus.has_value()) { + window_has_focus = superexit::has_focus(); + } + if (!window_has_focus.value()) { + vkey = INVALID_VKEY; + } + } // read - auto vkey = current_button->getVKey(); - GameAPI::Buttons::State state; - if (vkey == 0xFF) { + if (vkey == INVALID_VKEY) { state = BUTTON_NOT_PRESSED; } else { - state = (GetAsyncKeyState(current_button->getVKey()) & 0x8000) ? BUTTON_PRESSED : BUTTON_NOT_PRESSED; + state = (GetAsyncKeyState(vkey) & 0x8000) ? BUTTON_PRESSED : BUTTON_NOT_PRESSED; } // invert @@ -106,6 +120,16 @@ GameAPI::Buttons::State GameAPI::Buttons::getState(rawinput::RawInputManager *ma auto &devid = current_button->getDeviceIdentifier(); auto device = manager->devices_get(devid, false); // TODO: fix to update only + // check for focus + if (device && rawinput::RAWINPUT_REQUIRE_FOCUS) { + if (!window_has_focus.has_value()) { + window_has_focus = superexit::has_focus(); + } + if (!window_has_focus.value()) { + device = nullptr; + } + } + // get state if device was marked as updated GameAPI::Buttons::State state = current_button->getLastState(); double *last_up = nullptr; @@ -571,6 +595,12 @@ float GameAPI::Analogs::getState(rawinput::Device *device, Analog &analog) { switch (device->type) { case rawinput::MOUSE: { + // check for focus + if (rawinput::NAIVE_REQUIRE_FOCUS && !superexit::has_focus()) { + value = analog.getLastState(); + break; + } + // get mouse position auto mouse = device->mouseInfo; long pos; diff --git a/src/spice2x/cfg/button.cpp b/src/spice2x/cfg/button.cpp index 439f9b3..134439f 100644 --- a/src/spice2x/cfg/button.cpp +++ b/src/spice2x/cfg/button.cpp @@ -284,7 +284,7 @@ std::string Button::getDisplayString(rawinput::RawInputManager* manager) { std::string vKeyString = fmt::format("{:#x}", vKey); // device must be existing - if (this->device_identifier.empty() && vKey == 0xFF) { + if (this->device_identifier.empty() && vKey == INVALID_VKEY) { return ""; } diff --git a/src/spice2x/cfg/button.h b/src/spice2x/cfg/button.h index 4043047..b320b11 100644 --- a/src/spice2x/cfg/button.h +++ b/src/spice2x/cfg/button.h @@ -33,12 +33,14 @@ enum ButtonAnalogType { extern const char *ButtonAnalogTypeStr[]; +constexpr unsigned short INVALID_VKEY = UINT16_C(0xFF); + class Button { private: std::vector