Created
May 21, 2026 04:31
-
-
Save nullset2/26bb4184d5547bd25c29eb1aaa9d1bcb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/linux/evdev_manager.cc b/linux/evdev_manager.cc | |
| index b472c49..e9189cf 100644 | |
| --- a/linux/evdev_manager.cc | |
| +++ b/linux/evdev_manager.cc | |
| @@ -366,7 +366,7 @@ void EvdevManager::AddDevice(const char* path) { | |
| return G_SOURCE_REMOVE; | |
| } | |
| - self->OnInput(it->second); | |
| + self->OnInput(it->second, wd->path); | |
| return G_SOURCE_CONTINUE; | |
| })), | |
| wd, | |
| @@ -432,7 +432,7 @@ void EvdevManager::RemoveDevice(const char* path) { | |
| // Event reading (runs on worker thread, mutex held by caller) | |
| // --------------------------------------------------------------------------- | |
| -void EvdevManager::OnInput(DeviceInfo& info) { | |
| +void EvdevManager::OnInput(DeviceInfo& info, const std::string& path) { | |
| struct input_event ev; | |
| int rc; | |
| @@ -583,6 +583,31 @@ void EvdevManager::OnInput(DeviceInfo& info) { | |
| // Drain sync events. | |
| } | |
| } | |
| + | |
| + // If the kernel signals device removal via ENODEV or EIO, trigger a clean | |
| + // disconnect without waiting for G_IO_HUP (which may arrive late or not | |
| + // at all on some kernels/udev configurations). | |
| + if (rc == -ENODEV || rc == -EIO) { | |
| + auto* path_copy = new std::string(path); | |
| + GSource* idle = g_idle_source_new(); | |
| + g_source_set_callback( | |
| + idle, | |
| + [](gpointer data) -> gboolean { | |
| + // NOTE: idle sources created inside OnInput run on the worker | |
| + // context because OnInput is only ever called from the worker | |
| + // IO callback. We therefore capture the manager pointer via a | |
| + // separate channel. Use a small helper struct. | |
| + auto* args = static_cast<std::pair<EvdevManager*, std::string*>*>(data); | |
| + args->first->RemoveDevice(args->second->c_str()); | |
| + delete args->second; | |
| + delete args; | |
| + return G_SOURCE_REMOVE; | |
| + }, | |
| + new std::pair<EvdevManager*, std::string*>(this, path_copy), | |
| + nullptr); | |
| + g_source_attach(idle, worker_context_); | |
| + g_source_unref(idle); | |
| + } | |
| } | |
| void EvdevManager::OnDirectoryChanged(GFileMonitor* monitor, GFile* file, | |
| @@ -597,8 +622,46 @@ void EvdevManager::OnDirectoryChanged(GFileMonitor* monitor, GFile* file, | |
| g_autofree gchar* basename = g_file_get_basename(file); | |
| if (!basename || strncmp(basename, "event", 5) != 0) return; | |
| - if (event_type == G_FILE_MONITOR_EVENT_CREATED) { | |
| - self->AddDevice(path); | |
| + if (event_type == G_FILE_MONITOR_EVENT_CREATED || | |
| + event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { | |
| + // The kernel creates the node before the fd is readable; retry a few | |
| + // times if open() fails so that a freshly-plugged device is not missed. | |
| + struct RetryData { | |
| + EvdevManager* self; | |
| + std::string path; | |
| + int attempts_left; | |
| + }; | |
| + auto* rd = new RetryData{self, std::string(path), 5}; | |
| + GSource* retry = g_timeout_source_new(50); | |
| + g_source_set_callback( | |
| + retry, | |
| + [](gpointer data) -> gboolean { | |
| + auto* rd = static_cast<RetryData*>(data); | |
| + // If already tracked, we're done. | |
| + { | |
| + std::lock_guard<std::mutex> lk(rd->self->mutex_); | |
| + if (rd->self->devices_.count(rd->path)) { | |
| + delete rd; | |
| + return G_SOURCE_REMOVE; | |
| + } | |
| + } | |
| + int fd = open(rd->path.c_str(), O_RDONLY | O_NONBLOCK); | |
| + if (fd >= 0) { | |
| + close(fd); | |
| + rd->self->AddDevice(rd->path.c_str()); | |
| + delete rd; | |
| + return G_SOURCE_REMOVE; | |
| + } | |
| + if (--rd->attempts_left <= 0) { | |
| + delete rd; | |
| + return G_SOURCE_REMOVE; | |
| + } | |
| + return G_SOURCE_CONTINUE; // try again after another 50 ms | |
| + }, | |
| + rd, | |
| + nullptr); | |
| + g_source_attach(retry, self->worker_context_); | |
| + g_source_unref(retry); | |
| } else if (event_type == G_FILE_MONITOR_EVENT_DELETED) { | |
| self->RemoveDevice(path); | |
| } | |
| diff --git a/linux/evdev_manager.h b/linux/evdev_manager.h | |
| index 031640c..4153f2e 100644 | |
| --- a/linux/evdev_manager.h | |
| +++ b/linux/evdev_manager.h | |
| @@ -59,7 +59,7 @@ class EvdevManager { | |
| void ScanDevices(); | |
| void AddDevice(const char* path); | |
| void RemoveDevice(const char* path); | |
| - void OnInput(DeviceInfo& info); | |
| + void OnInput(DeviceInfo& info, const std::string& path); | |
| /// Queue an event for delivery on the next timer tick. | |
| void ForwardEvent(FlValue* event); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment