Skip to content

Commit

Permalink
Remove first_value_arrived_,grabbed_,grabbed_time_stamp_,ungrabbed_ti…
Browse files Browse the repository at this point in the history
…me_stamp_ from device_grabber_details::entry
  • Loading branch information
tekezo committed Apr 29, 2024
1 parent 2a14210 commit c283656
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 105 deletions.
32 changes: 9 additions & 23 deletions src/core/grabber/include/grabber/device_grabber.hpp
Expand Up @@ -279,9 +279,16 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {
it->second->seized() ? "grabbed" : "observed");
logger_unique_filter_.reset();

post_device_grabbed_event(it->second->get_device_properties());
if (it->second->seized()) {
if (auto probable_stuck_events_manager = find_probable_stuck_events_manager(device_id)) {
logger::get_logger()->info("{0} probable_stuck_events_manager->clear()",
it->second->get_device_name());

it->second->set_grabbed(true);
probable_stuck_events_manager->clear();
}
}

post_device_grabbed_event(it->second->get_device_properties());

update_caps_lock_led();

Expand All @@ -296,8 +303,6 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {
it->second->get_device_name());
logger_unique_filter_.reset();

it->second->set_grabbed(false);

post_device_ungrabbed_event(device_id);

update_virtual_hid_pointing();
Expand Down Expand Up @@ -696,16 +701,6 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {
// Manipulate events

if (auto probable_stuck_events_manager = find_probable_stuck_events_manager(entry->get_device_id())) {
if (!entry->get_first_value_arrived()) {
// First grabbed event is arrived.

entry->set_first_value_arrived(true);

if (entry->seized()) {
probable_stuck_events_manager->clear();
}
}

bool needs_regrab = false;
bool notify = false;

Expand Down Expand Up @@ -936,15 +931,6 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {
}
}

bool grabbed(device_id device_id,
absolute_time_point time_stamp) const {
auto it = entries_.find(device_id);
if (it != std::end(entries_)) {
return it->second->grabbed(time_stamp);
}
return false;
}

bool needs_prepare_virtual_hid_pointing_device(void) const {
//
// Check if there is a pointing device to grab
Expand Down
96 changes: 14 additions & 82 deletions src/core/grabber/include/grabber/device_grabber_details/entry.hpp
Expand Up @@ -21,20 +21,25 @@ class entry final : public pqrs::dispatcher::extra::dispatcher_client {
std::weak_ptr<const core_configuration::core_configuration> core_configuration) : dispatcher_client(),
device_id_(device_id),
core_configuration_(core_configuration),
first_value_arrived_(false),
grabbed_(false),
disabled_(false),
event_origin_(event_origin::none),
grabbed_time_stamp_(0),
ungrabbed_time_stamp_(0) {
event_origin_(event_origin::none) {
device_properties_ = device_properties(device_id,
device);

pressed_keys_manager_ = std::make_shared<pressed_keys_manager>();

caps_lock_led_state_manager_ = std::make_shared<krbn::hid_keyboard_caps_lock_led_state_manager>(device);

hid_queue_value_monitor_ = std::make_shared<pqrs::osx::iokit_hid_queue_value_monitor>(pqrs::dispatcher::extra::get_shared_dispatcher(),
pqrs::cf::run_loop_thread::extra::get_shared_run_loop_thread(),
device);
caps_lock_led_state_manager_ = std::make_shared<krbn::hid_keyboard_caps_lock_led_state_manager>(device);
hid_queue_value_monitor_->started.connect([this] {
control_caps_lock_led_state_manager();
});
hid_queue_value_monitor_->stopped.connect([this] {
control_caps_lock_led_state_manager();
});

device_name_ = iokit_utility::make_device_name_for_log(device_id,
device);
device_short_name_ = iokit_utility::make_device_name(device);
Expand Down Expand Up @@ -81,14 +86,6 @@ class entry final : public pqrs::dispatcher::extra::dispatcher_client {
return hid_queue_value_monitor_;
}

bool get_first_value_arrived(void) const {
return first_value_arrived_;
}

void set_first_value_arrived(bool value) {
first_value_arrived_ = value;
}

void set_caps_lock_led_state(std::optional<led_state> state) {
caps_lock_led_state_manager_->set_state(state);
}
Expand All @@ -101,22 +98,6 @@ class entry final : public pqrs::dispatcher::extra::dispatcher_client {
return device_short_name_;
}

bool get_grabbed(void) const {
return grabbed_;
}

void set_grabbed(bool value) {
grabbed_ = value;

if (grabbed_) {
grabbed_time_stamp_ = pqrs::osx::chrono::mach_absolute_time_point();
} else {
ungrabbed_time_stamp_ = pqrs::osx::chrono::mach_absolute_time_point();
}

control_caps_lock_led_state_manager();
}

bool get_disabled(void) const {
return disabled_;
}
Expand Down Expand Up @@ -187,61 +168,20 @@ class entry final : public pqrs::dispatcher::extra::dispatcher_client {
}
}

// Skip the process if the same options have already been processed.
if (hid_queue_value_monitor_async_start_options_ == options) {
return;
}

//
// Stop if already started with different options.
//

if (options != hid_queue_value_monitor_async_start_options_) {
async_stop_queue_value_monitor();
}

//
// Start
//

first_value_arrived_ = false;
hid_queue_value_monitor_async_start_options_ = options;

hid_queue_value_monitor_->async_start(options,
std::chrono::milliseconds(1000));
}

void async_stop_queue_value_monitor(void) {
hid_queue_value_monitor_->async_stop();

hid_queue_value_monitor_async_start_options_ = std::nullopt;
}

bool grabbed(absolute_time_point time_stamp) const {
if (grabbed_) {
if (grabbed_time_stamp_ <= time_stamp) {
return true;
}
} else {
//
// (grabbed_time_stamp_ <= ungrabbed_time_stamp_) when (grabbed_ == false)
//

if (grabbed_time_stamp_ <= time_stamp &&
time_stamp <= ungrabbed_time_stamp_) {
return true;
}
}

return false;
}

bool seized(void) const {
if (auto options = hid_queue_value_monitor_async_start_options_) {
return *options & kIOHIDOptionsTypeSeizeDevice;
}

return false;
return hid_queue_value_monitor_->seized();
}

bool is_karabiner_virtual_hid_device(void) const {
Expand Down Expand Up @@ -299,7 +239,7 @@ class entry final : public pqrs::dispatcher::extra::dispatcher_client {
if (caps_lock_led_state_manager_) {
if (auto c = core_configuration_.lock()) {
if (c->get_selected_profile().get_device_manipulate_caps_lock_led(device_properties_.get_device_identifiers())) {
if (grabbed_ && event_origin_ == event_origin::grabbed_device) {
if (seized()) {
caps_lock_led_state_manager_->async_start();
return;
}
Expand All @@ -314,21 +254,13 @@ class entry final : public pqrs::dispatcher::extra::dispatcher_client {
std::weak_ptr<const core_configuration::core_configuration> core_configuration_;
device_properties device_properties_;
std::shared_ptr<pressed_keys_manager> pressed_keys_manager_;

std::shared_ptr<pqrs::osx::iokit_hid_queue_value_monitor> hid_queue_value_monitor_;
std::optional<IOOptionBits> hid_queue_value_monitor_async_start_options_;
bool first_value_arrived_;

std::shared_ptr<hid_keyboard_caps_lock_led_state_manager> caps_lock_led_state_manager_;
std::shared_ptr<pqrs::osx::iokit_hid_queue_value_monitor> hid_queue_value_monitor_;
std::string device_name_;
std::string device_short_name_;

bool grabbed_;
bool disabled_;
event_origin event_origin_;

absolute_time_point grabbed_time_stamp_;
absolute_time_point ungrabbed_time_stamp_;
};
} // namespace device_grabber_details
} // namespace grabber
Expand Down

0 comments on commit c283656

Please sign in to comment.