From 39562af664f791c0ea153ae12cd2533adcfc6b65 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 13 Mar 2016 18:36:47 -0400 Subject: [PATCH] Better output for HandleRead(). --- apple-ir-control.cc | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/apple-ir-control.cc b/apple-ir-control.cc index 8a6f5df..b102895 100644 --- a/apple-ir-control.cc +++ b/apple-ir-control.cc @@ -27,15 +27,6 @@ class ScopedIOHIDManager { ScopedIOHIDManager(const ScopedIOHIDManager&) = delete; - bool Open() { - IOReturn ret = IOHIDManagerOpen(manager_, kIOHIDManagerOptionNone); - if (ret != kIOReturnSuccess) { - ERROR("Failed to IOHIDManagerOpen: 0x%x", ret); - return false; - } - return true; - } - IOHIDManagerRef get() { return manager_; } private: @@ -67,9 +58,6 @@ class ScopedCFTypeRef { bool IsIRAvailable() { ScopedIOHIDManager manager; IOHIDManagerSetDeviceMatching(manager.get(), nullptr); - if (!manager.Open()) { - //return false; - } ScopedCFTypeRef devices(IOHIDManagerCopyDevices(manager.get())); if (!devices) { ERROR("Failed to IOHIDManagerCopyDevices"); @@ -92,22 +80,37 @@ bool IsIRAvailable() { return false; } +const char* GetBooleanDescription(CFTypeRef boolean) { + if (CFGetTypeID(boolean) != CFBooleanGetTypeID()) { + ERROR("Unexpected non-boolean CFTypeRef"); + abort(); + } + return CFBooleanGetValue(static_cast(boolean)) ? "on" : "off"; +} + CFTypeRef GetUserPropertyValue() { return CFPreferencesCopyValue(kPrefEnabled, kPrefDomain, kCFPreferencesAnyUser, kCFPreferencesCurrentHost); } -int HandleRead() { - CFTypeRef user_prop = GetUserPropertyValue(); - CFShow(user_prop); - +io_iterator_t CreateIOServiceIterator() { CFMutableDictionaryRef matching_dict = IOServiceMatching("AppleIRController"); io_iterator_t iterator; kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matching_dict, &iterator); if (kr != KERN_SUCCESS) { ERROR("Failed to IOServiceGetMatchingServices: 0x%x", kr); - return EXIT_FAILURE; + return 0; } + return iterator; +} + +int HandleRead() { + CFTypeRef user_prop = GetUserPropertyValue(); + printf("Userspace property value: %s\n", GetBooleanDescription(user_prop)); + + io_iterator_t iterator = CreateIOServiceIterator(); + if (!iterator) + return EXIT_FAILURE; io_object_t service; bool did_find = false; @@ -125,7 +128,8 @@ int HandleRead() { ScopedCFTypeRef device_enabled( IORegistryEntryCreateCFProperty(service, kPrefEnabled, nullptr, 0)); - CFShow(device_enabled.get()); + printf("Kernel property value %s: %s\n", + name, GetBooleanDescription(device_enabled.get())); #if 0 ScopedCFTypeRef props; -- 2.43.5