Skip to content

Commit

Permalink
Reevaluating device filters when devices change for MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Feb 14, 2024
1 parent 42b9f97 commit 5250c94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/client/unix/StringTyperImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <codecvt>
#include <locale>

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

using MakeStringTyperImpl = std::unique_ptr<StringTyperImpl>();
MakeStringTyperImpl make_string_typer_wayland;
MakeStringTyperImpl make_string_typer_x11;
Expand Down
23 changes: 16 additions & 7 deletions src/server/unix/GrabbedDevicesMacOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ class GrabbedDevicesImpl {

// try to grab existing virtual device (Karabiner Elements)
// before the own virtual device was created
if (update_grabbed_devices(true))
if (update(true))
return true;

IOHIDManagerRegisterDeviceMatchingCallback(m_hid_manager, &devices_changed_callback, this);
IOHIDManagerRegisterDeviceRemovalCallback(m_hid_manager, &devices_changed_callback, this);
return update_grabbed_devices(false);
return update(false);
}

std::pair<bool, std::optional<Event>> read_input_event(
Expand All @@ -110,10 +110,6 @@ class GrabbedDevicesImpl {
m_event_queue.clear();
m_event_queue_pos = 0;

if (std::exchange(m_devices_changed, false))
if (!update_grabbed_devices(false))
return { false, std::nullopt };

// TODO: do not poll. see https://stackoverflow.com/questions/48434976/cfsocket-data-callbacks
auto poll_timeout = (timeout.has_value() ? timeout.value() : Duration::max());
if (interrupt_fd >=0) {
Expand All @@ -130,6 +126,15 @@ class GrabbedDevicesImpl {
}
}

bool update_devices() {
if (!m_devices_changed)
return false;

update(false);
m_devices_changed = false;
return true;
}

const std::vector<std::string>& grabbed_device_names() const {
return m_grabbed_device_names;
}
Expand Down Expand Up @@ -174,7 +179,7 @@ class GrabbedDevicesImpl {
m_event_queue.push_back({ 0, 0, code, value });
}

bool update_grabbed_devices(bool grab_virtual_device) {
bool update(bool grab_virtual_device) {
verbose("Updating device list");

// get devices
Expand Down Expand Up @@ -241,6 +246,10 @@ bool GrabbedDevices::grab(const char* virtual_device_name, bool grab_mice) {
return m_impl->initialize(virtual_device_name, grab_mice);
}

bool GrabbedDevices::update_devices() {
return m_impl->update_devices();
}

auto GrabbedDevices::read_input_event(std::optional<Duration> timeout, int interrupt_fd)
-> std::pair<bool, std::optional<Event>> {
return m_impl->read_input_event(timeout, interrupt_fd);
Expand Down

0 comments on commit 5250c94

Please sign in to comment.