Skip to content

Commit

Permalink
Remove unnecessary mutex since InputThread doesn't actually modify th…
Browse files Browse the repository at this point in the history
…e keys-held array, and input is synchronous.
  • Loading branch information
Brandon-T committed Sep 6, 2023
1 parent 745bb49 commit 1675590
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
26 changes: 5 additions & 21 deletions RemoteInput/Plugin/InputOutput.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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..
Expand All @@ -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();
Expand All @@ -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<std::int32_t, 4>&& keys) noexcept
bool InputOutput::any_key_held(std::array<std::int32_t, 4>&& 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
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions RemoteInput/Plugin/InputOutput.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ 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;

bool has_focus(java::Component* component) const noexcept;
void gain_focus(java::Component* component) const noexcept;
void lose_focus(java::Component* component) const noexcept;

bool any_key_held(std::array<std::int32_t, 4>&& keys) noexcept;
bool any_key_held(std::array<std::int32_t, 4>&& keys) const noexcept;
void handle_resize(java::Component* component) noexcept;

public:
Expand All @@ -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;
Expand Down

0 comments on commit 1675590

Please sign in to comment.