From 790ed4e5f956c22c0e829ba0a8388258d02dd767 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 13 Mar 2016 18:53:56 -0400 Subject: [PATCH] Handle setting the property value. --- apple-ir-control.cc | 47 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/apple-ir-control.cc b/apple-ir-control.cc index b102895..fcb3f63 100644 --- a/apple-ir-control.cc +++ b/apple-ir-control.cc @@ -88,6 +88,15 @@ const char* GetBooleanDescription(CFTypeRef boolean) { return CFBooleanGetValue(static_cast(boolean)) ? "on" : "off"; } +bool SynchronizePrefs() { + bool rv = CFPreferencesSynchronize(kPrefDomain, kCFPreferencesAnyUser, + kCFPreferencesCurrentHost); + if (!rv) { + ERROR("Failed to CFPreferencesSynchronize"); + } + return rv; +} + CFTypeRef GetUserPropertyValue() { return CFPreferencesCopyValue(kPrefEnabled, kPrefDomain, kCFPreferencesAnyUser, kCFPreferencesCurrentHost); @@ -96,7 +105,8 @@ CFTypeRef GetUserPropertyValue() { io_iterator_t CreateIOServiceIterator() { CFMutableDictionaryRef matching_dict = IOServiceMatching("AppleIRController"); io_iterator_t iterator; - kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matching_dict, &iterator); + kern_return_t kr = IOServiceGetMatchingServices( + kIOMasterPortDefault, matching_dict, &iterator); if (kr != KERN_SUCCESS) { ERROR("Failed to IOServiceGetMatchingServices: 0x%x", kr); return 0; @@ -105,6 +115,9 @@ io_iterator_t CreateIOServiceIterator() { } int HandleRead() { + if (!SynchronizePrefs()) + return EXIT_FAILURE; + CFTypeRef user_prop = GetUserPropertyValue(); printf("Userspace property value: %s\n", GetBooleanDescription(user_prop)); @@ -151,7 +164,33 @@ int HandleRead() { } int HandleWrite(bool enable) { - return EXIT_SUCCESS; + const CFBooleanRef enabled_value = enable ? kCFBooleanTrue : kCFBooleanFalse; + + CFPreferencesSetValue(kPrefEnabled, enabled_value, kPrefDomain, + kCFPreferencesAnyUser, kCFPreferencesCurrentHost); + if (!SynchronizePrefs()) + return EXIT_FAILURE; + + io_iterator_t iterator = CreateIOServiceIterator(); + io_object_t service; + while ((service = IOIteratorNext(iterator))) { + io_name_t name; + kern_return_t kr = IORegistryEntryGetName(service, name); + if (kr != KERN_SUCCESS) { + ERROR("Failed to IORegistryEntryGetName: 0x%x", kr); + continue; + } + + LOG("Setting property for %s to %d", name, enable); + + kr = IORegistryEntrySetCFProperty(service, kPrefEnabled, enabled_value); + if (kr != KERN_SUCCESS) { + ERROR("Failed to IORegistryEntrySetCFProperty: 0x%x", kr); + continue; + } + } + + return HandleRead(); } int main(int argc, char* argv[]) { @@ -163,9 +202,9 @@ int main(int argc, char* argv[]) { if (argc == 1) { return HandleRead(); } else if (argc == 2) { - if (strcmp("on", argv[1])) { + if (strcmp("on", argv[1]) == 0) { return HandleWrite(true); - } else if (strcmp("off", argv[1])) { + } else if (strcmp("off", argv[1]) == 0) { return HandleWrite(false); } } -- 2.43.5