Skip to content

Commit

Permalink
dummy callbacks for keyboard (dis)connection
Browse files Browse the repository at this point in the history
  • Loading branch information
thoelze1 committed Jul 13, 2020
1 parent ac75c22 commit 4cc27fc
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions c_src/mac/keyio_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ static int fd[2];
* It passes the relevant information into a pipe that will be read
* from with wait_key.
*/
void callback(void *context, IOReturn result, void *sender, IOHIDValueRef value) {
void input_callback(void *context, IOReturn result, void *sender, IOHIDValueRef value) {
std::cout << "input: " << CFRunLoopGetCurrent() << std::endl;
struct KeyEvent e;
CFIndex integer_value = IOHIDValueGetIntegerValue(value);
IOHIDElementRef element = IOHIDValueGetElement(value);
Expand All @@ -59,6 +60,24 @@ void callback(void *context, IOReturn result, void *sender, IOHIDValueRef value)
write(fd[1], &e, sizeof(struct KeyEvent));
}

/*
* We'll register this callback to run whenever an IOHIDDevice
* (representing a keyboard) is connected to the OS
*
*/
void matched_callback(void *context, io_iterator_t iter) {
std::cout << "matched: " << CFRunLoopGetCurrent() << std::endl;
}

/*
* We'll register this callback to run whenever an IOHIDDevice
* (representing a keyboard) is disconnected from the OS
*
*/
void terminated_callback(void *context, io_iterator_t iter) {
std::cout << "terminated: " << CFRunLoopGetCurrent() << std::endl;
}

/*
* This gets us some code reuse (see the send_key overload below)
*/
Expand Down Expand Up @@ -119,6 +138,7 @@ void monitor_kb(char *product) {
CFDictionarySetValue(matching_dictionary,CFSTR(kIOHIDDeviceUsageKey),cfValue);
CFRelease(cfValue);
io_iterator_t iter = IO_OBJECT_NULL;
CFRetain(matching_dictionary);
kr = IOServiceGetMatchingServices(kIOMasterPortDefault,
matching_dictionary,
&iter);
Expand All @@ -141,7 +161,7 @@ void monitor_kb(char *product) {
}
IOHIDDeviceRef dev = IOHIDDeviceCreate(kCFAllocatorDefault, curr);
source_device.push_back(dev);
IOHIDDeviceRegisterInputValueCallback(dev, callback, NULL);
IOHIDDeviceRegisterInputValueCallback(dev, input_callback, NULL);
kr = IOHIDDeviceOpen(dev, kIOHIDOptionsTypeSeizeDevice);
if(kr != kIOReturnSuccess) {
std::cerr << "IOHIDDeviceOpen error: " << kr << std::endl;
Expand All @@ -152,6 +172,40 @@ void monitor_kb(char *product) {
}
}
listener_loop = CFRunLoopGetCurrent();
IONotificationPortRef notification_port = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopSourceRef notification_source = IONotificationPortGetRunLoopSource(notification_port);
CFRunLoopAddSource(listener_loop, notification_source, kCFRunLoopDefaultMode);
CFRetain(matching_dictionary);
kr = IOServiceAddMatchingNotification(notification_port,
kIOMatchedNotification,
matching_dictionary,
matched_callback,
NULL,
&iter);
if(kr != KERN_SUCCESS) {
std::cerr << "IOServiceAddMatchingNotification error: " << kr << std::endl;
return;
}
for(mach_port_t curr = IOIteratorNext(iter); curr; curr = IOIteratorNext(iter)) {
CFStringRef port_name = (CFStringRef)IORegistryEntryCreateCFProperty(curr, CFSTR(kIOHIDProductKey), kCFAllocatorDefault, kIOHIDOptionsTypeNone);
CFShow(port_name);
}
std::cout << "slkjfaslk" << std::endl;
kr = IOServiceAddMatchingNotification(notification_port,
kIOTerminatedNotification,
matching_dictionary,
terminated_callback,
NULL,
&iter);
for(mach_port_t curr = IOIteratorNext(iter); curr; curr = IOIteratorNext(iter)) {
CFStringRef port_name = (CFStringRef)IORegistryEntryCreateCFProperty(curr, CFSTR(kIOHIDProductKey), kCFAllocatorDefault, kIOHIDOptionsTypeNone);
CFShow(port_name);
}
if(kr != KERN_SUCCESS) {
std::cerr << "IOServiceAddMatchingNotification error: " << kr << std::endl;
return;
}

for(IOHIDDeviceRef d : source_device) {
IOHIDDeviceScheduleWithRunLoop(d, listener_loop, kCFRunLoopDefaultMode);
}
Expand Down

0 comments on commit 4cc27fc

Please sign in to comment.