From 16755904a7a45aeea90ee62284a9a17346a6227b Mon Sep 17 00:00:00 2001 From: Brandon-T Date: Wed, 6 Sep 2023 16:12:48 -0400 Subject: [PATCH] Remove unnecessary mutex since InputThread doesn't actually modify the keys-held array, and input is synchronous. --- RemoteInput/Plugin/InputOutput.cxx | 26 +++++--------------------- RemoteInput/Plugin/InputOutput.hxx | 6 +++--- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/RemoteInput/Plugin/InputOutput.cxx b/RemoteInput/Plugin/InputOutput.cxx index 35834d5..80d15b1 100644 --- a/RemoteInput/Plugin/InputOutput.cxx +++ b/RemoteInput/Plugin/InputOutput.cxx @@ -207,9 +207,7 @@ void InputOutput::hold_key(std::int32_t code) noexcept if (std::find(std::begin(control_keys), std::end(control_keys), code) != std::end(control_keys)) { //Control Keys only generate a single held event.. - this->mutex.lock(); held_keys.push_back(code); - this->mutex.unlock(); //PostEvent java::Component receiver = control_center->reflect_canvas(); @@ -239,10 +237,8 @@ void InputOutput::hold_key(std::int32_t code) noexcept //Key already being pressed so just replace it if (currently_held_key != -1 && this->keyboard_speed >= 0 && this->keyboard_repeat_delay >= 0) { - this->mutex.lock(); currently_held_key = code; held_keys.push_back(code); - this->mutex.unlock(); //Post Event java::Component receiver = control_center->reflect_canvas(); @@ -282,10 +278,8 @@ void InputOutput::hold_key(std::int32_t code) noexcept } else { - this->mutex.lock(); currently_held_key = code; held_keys.push_back(code); - this->mutex.unlock(); //Post Event java::Component receiver = control_center->reflect_canvas(); @@ -394,9 +388,7 @@ void InputOutput::release_key(std::int32_t code) noexcept { if (std::find(std::begin(control_keys), std::end(control_keys), code) != std::end(control_keys)) { - this->mutex.lock(); held_keys.erase(it); //held_keys.erase(std::remove(held_keys.begin(), held_keys.end(), code), held_keys.end()); - this->mutex.unlock(); //Post Event java::Component receiver = control_center->reflect_canvas(); @@ -423,7 +415,6 @@ void InputOutput::release_key(std::int32_t code) noexcept else { //Remove the held key from the list.. - this->mutex.lock(); held_keys.erase(it); //Find the next non-control held-key.. @@ -433,7 +424,6 @@ void InputOutput::release_key(std::int32_t code) noexcept //Set the next currently held key to the first non-control key.. currently_held_key = jt != held_keys.crend() ? *jt : -1; - this->mutex.unlock(); //Post Event java::Component receiver = control_center->reflect_canvas(); @@ -460,22 +450,16 @@ void InputOutput::release_key(std::int32_t code) noexcept } } -bool InputOutput::is_key_held(std::int32_t code) noexcept +bool InputOutput::is_key_held(std::int32_t code) const noexcept { - this->mutex.lock(); - bool result = std::find(std::begin(held_keys), std::end(held_keys), code) != std::end(held_keys); - this->mutex.unlock(); - return result; + return std::find(std::begin(held_keys), std::end(held_keys), code) != std::end(held_keys); } -bool InputOutput::any_key_held(std::array&& keys) noexcept +bool InputOutput::any_key_held(std::array&& keys) const noexcept { - this->mutex.lock(); - bool result = std::any_of(std::cbegin(held_keys), std::cend(held_keys), [&](std::int32_t key){ + return std::any_of(std::cbegin(held_keys), std::cend(held_keys), [&](std::int32_t key){ return std::find(std::cbegin(keys), std::cend(keys), key) != std::cend(keys); }); - this->mutex.unlock(); - return result; } void InputOutput::send_string(std::string string, std::int32_t keywait, std::int32_t keymodwait) const noexcept @@ -1193,7 +1177,7 @@ std::int32_t InputOutput::GetKeyLocation(std::int32_t keycode) const noexcept return java::KeyEvent::KeyCodes::KEY_LOCATION_STANDARD; } -std::int32_t InputOutput::GetActiveKeyModifiers() noexcept +std::int32_t InputOutput::GetActiveKeyModifiers() const noexcept { std::int32_t modifiers = 0; diff --git a/RemoteInput/Plugin/InputOutput.hxx b/RemoteInput/Plugin/InputOutput.hxx index 1aa77da..3166f85 100644 --- a/RemoteInput/Plugin/InputOutput.hxx +++ b/RemoteInput/Plugin/InputOutput.hxx @@ -46,7 +46,7 @@ private: jchar NativeKeyCodeToChar(std::int32_t keycode, std::int32_t modifiers) const noexcept; std::int32_t GetJavaKeyCode(std::int32_t native_key_code) const noexcept; std::int32_t GetKeyLocation(std::int32_t keycode) const noexcept; - std::int32_t GetActiveKeyModifiers() noexcept; + std::int32_t GetActiveKeyModifiers() const noexcept; std::int32_t ModifiersForChar(char c) const noexcept; std::int32_t SimbaMouseButtonToJava(std::int32_t button) const noexcept; @@ -54,7 +54,7 @@ private: void gain_focus(java::Component* component) const noexcept; void lose_focus(java::Component* component) const noexcept; - bool any_key_held(std::array&& keys) noexcept; + bool any_key_held(std::array&& keys) const noexcept; void handle_resize(java::Component* component) noexcept; public: @@ -76,7 +76,7 @@ public: void hold_key(std::int32_t code) noexcept; void release_key(std::int32_t code) noexcept; - bool is_key_held(std::int32_t code) noexcept; + bool is_key_held(std::int32_t code) const noexcept; void send_string(std::string string, std::int32_t keywait, std::int32_t keymodwait) const noexcept; void get_mouse_position(std::int32_t* x, std::int32_t* y) noexcept;