Skip to content

Instantly share code, notes, and snippets.

@vtkacenko
Created March 8, 2019 13:28
Show Gist options
  • Save vtkacenko/5698ea55badcfcd7658a72501027e272 to your computer and use it in GitHub Desktop.
Save vtkacenko/5698ea55badcfcd7658a72501027e272 to your computer and use it in GitHub Desktop.
Example that works for detecting the Wooting keyboard on macOS and Linux.
hid_device *WrapperImpl::getHandle(const unsigned short &vid_, const unsigned short &pid_) {
hid_info_ = hid_enumerate(vid_, pid_);
if (hid_info_ == nullptr) {
std::cout << "hid_enumerate returned nullptr \n";
return nullptr;
} else {
struct hid_device_info *walker = hid_info_;
int interface_number = 0;
while (walker) {
if (walker->interface_number > interface_number) {
interface_number = walker->interface_number;
}
walker = walker->next;
}
for (struct hid_device_info *tmp = hid_info_; tmp != nullptr; tmp = tmp->next) {
/* 65535 is the analog interface,
* 4919 is the configuration one used for RGB
*/
#ifdef __APPLE__
if(tmp->usage_page == static_cast<unsigned short>(65535)){
#else
if (tmp->interface_number == interface_number) {
#endif
std::cout << "Usage page " << tmp->usage_page << ". \n";
std::wcout << "Product " << tmp->product_string << ". \n";
std::wcout << "Manufacturer " << tmp->manufacturer_string << ". \n";
std::wcout << "Serial number " << tmp->serial_number << ". \n";
std::wcout << "Path " << tmp->path << " \n";
if (handle_ == nullptr) {
handle_ = hid_open_path(tmp->path);
// raise event keyboard found
if (!keyboard_observer_.expired() && handle_ != nullptr) {
notification_queue_->dispatch(std::bind(&WrapperImpl::notifyKeyboardConnected,
keyboard_observer_));
}
}
}
}
hid_free_enumeration(hid_info_);
}
return handle_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment