static int setup_led(void) { static int initialized = 0; if(initialized) return kIOReturnSuccess; initialized = 1; IOReturn ioReturnValue = kIOReturnError; if (!(hidService = find_a_keyboard())) { fprintf(stderr, "No keyboard found.\n"); return ioReturnValue; } hidDevice = (io_object_t)hidService; create_hid_interface(hidDevice, &hidDeviceInterface); find_led_cookies((IOHIDDeviceInterface122 **)hidDeviceInterface); ioReturnValue = IOObjectRelease(hidDevice); if (ioReturnValue != kIOReturnSuccess) { goto out; } ioReturnValue = kIOReturnError; if (hidDeviceInterface == NULL) { fprintf(stderr, "Failed to create HID device interface.\n"); return ioReturnValue; } ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, 0); if (ioReturnValue != kIOReturnSuccess) { fprintf(stderr, "Failed to open HID device interface.\n"); goto out; } out: if(ioReturnValue != kIOReturnSuccess) (void)(*hidDeviceInterface)->Release(hidDeviceInterface); return ioReturnValue; }
int manipulate_led(UInt32 whichLED, UInt32 value) { io_service_t hidService = (io_service_t)0; io_object_t hidDevice = (io_object_t)0; IOHIDDeviceInterface **hidDeviceInterface = NULL; IOReturn ioReturnValue = kIOReturnError; IOHIDElementCookie theCookie = (IOHIDElementCookie)0; IOHIDEventStruct theEvent; if (!(hidService = find_a_keyboard())) { fprintf(stderr, "No keyboard found.\n"); return ioReturnValue; } hidDevice = (io_object_t)hidService; create_hid_interface(hidDevice, &hidDeviceInterface); find_led_cookies((IOHIDDeviceInterface122 **)hidDeviceInterface); ioReturnValue = IOObjectRelease(hidDevice); if (ioReturnValue != kIOReturnSuccess) { goto out; } ioReturnValue = kIOReturnError; if (hidDeviceInterface == NULL) { fprintf(stderr, "Failed to create HID device interface.\n"); return ioReturnValue; } if (whichLED == kHIDUsage_LED_NumLock) { theCookie = numlock_cookie; } else if (whichLED == kHIDUsage_LED_CapsLock) { theCookie = capslock_cookie; } if (theCookie == 0) { fprintf(stderr, "Bad or missing LED cookie.\n"); goto out; } ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, 0); if (ioReturnValue != kIOReturnSuccess) { fprintf(stderr, "Failed to open HID device interface.\n"); goto out; } ioReturnValue = (*hidDeviceInterface)->getElementValue(hidDeviceInterface, theCookie, &theEvent); if (ioReturnValue != kIOReturnSuccess) { (void)(*hidDeviceInterface)->close(hidDeviceInterface); goto out; } fprintf(stdout, "%s\n", (theEvent.value) ? "on" : "off"); if (value != -1) { if (theEvent.value != value) { theEvent.value = value; ioReturnValue = (*hidDeviceInterface)->setElementValue( hidDeviceInterface, theCookie, &theEvent, 0, 0, 0, 0); if (ioReturnValue == kIOReturnSuccess) { fprintf(stdout, "%s\n", (theEvent.value) ? "on" : "off"); } } } ioReturnValue = (*hidDeviceInterface)->close(hidDeviceInterface); out: (void)(*hidDeviceInterface)->Release(hidDeviceInterface); return ioReturnValue; }