Search in sources :

Example 1 with IOIteratorTRef

use of net.pms.util.jna.macos.types.IOIteratorTRef in project UniversalMediaServer by UniversalMediaServer.

the class IOKitTest method testIOKit.

/**
 * Tests some {@link IOKit} mappings.
 *
 * @throws Throwable if an error occurs during the test.
 */
@Test
public void testIOKit() throws Throwable {
    CFDictionaryRef dictionaryRef = IO.IOServiceMatching("IOHIDSystem");
    IOIteratorTRef iteratorRef = new IOIteratorTRef(true);
    assertEquals(DefaultKernReturnT.SUCCESS, IO.IOServiceGetMatchingServices(IOKit.kIOMasterPortDefault, dictionaryRef, iteratorRef));
    IOIteratorT iterator = iteratorRef.getValue();
    IONameT name = new IONameT(true);
    assertEquals(DefaultKernReturnT.SUCCESS, IO.IOObjectGetClass(iterator, name));
    assertEquals("IOUserIterator", name.getString(StandardCharsets.UTF_8));
    IOObjectT object = IO.IOIteratorNext(iterator);
    assertEquals(DefaultKernReturnT.SUCCESS, IO.IOObjectGetClass(object, name));
    assertEquals("IOHIDSystem", name.getString(StandardCharsets.UTF_8));
    IORegistryEntryT registryEntry = IORegistryEntryT.toIORegistryT(object);
    CFMutableDictionaryRefByReference dictionaryRefRef = new CFMutableDictionaryRefByReference();
    assertEquals(DefaultKernReturnT.SUCCESS, IO.IORegistryEntryCreateCFProperties(registryEntry, dictionaryRefRef, CoreFoundation.ALLOCATOR, 0));
    CFStringRef key = CFStringRef.toCFStringRef("IOClass");
    assertTrue(CF.CFDictionaryContainsKey(dictionaryRefRef.getCFMutableDictionaryRef(), key));
    CFStringRef value = new CFStringRef(CF.CFDictionaryGetValue(dictionaryRefRef.getCFMutableDictionaryRef(), key));
    assertEquals("IOHIDSystem", value.toString());
    CF.CFRelease(key);
    key = CFStringRef.toCFStringRef("IOProviderClass");
    value = new CFStringRef(CF.CFDictionaryGetValue(dictionaryRefRef.getCFMutableDictionaryRef(), key));
    assertEquals("IOResources", value.toString());
    CF.CFRelease(key);
    IO.IOObjectRelease(object);
    object = IO.IOIteratorNext(iterator);
    assertEquals(0, object.intValue());
    assertTrue(IO.IOIteratorIsValid(iterator));
    IO.IOObjectRelease(iterator);
    CF.CFRelease(dictionaryRefRef.getCFMutableDictionaryRef());
}
Also used : CFDictionaryRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFDictionaryRef) IOObjectT(net.pms.util.jna.macos.types.IOObjectT) IOIteratorTRef(net.pms.util.jna.macos.types.IOIteratorTRef) CFStringRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef) IOIteratorT(net.pms.util.jna.macos.types.IOIteratorT) IORegistryEntryT(net.pms.util.jna.macos.types.IORegistryEntryT) CFMutableDictionaryRefByReference(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRefByReference) IONameT(net.pms.util.jna.macos.types.IONameT) Test(org.junit.Test)

Example 2 with IOIteratorTRef

use of net.pms.util.jna.macos.types.IOIteratorTRef in project UniversalMediaServer by UniversalMediaServer.

the class IOKitUtils method getSystemIdleTimeMS.

/**
 * Queries the OS for the current value of the idle timer and returns the
 * results.
 *
 * @return The number of milliseconds since the last user activity.
 * @throws IOKitException If an error occurred while querying the OS.
 */
@SuppressWarnings("null")
public static long getSystemIdleTimeMS() throws IOKitException {
    IOIteratorTRef iteratorRef = new IOIteratorTRef(true);
    KernReturnT ioReturn = ioKit.IOServiceGetMatchingServices(null, ioKit.IOServiceMatching("IOHIDSystem"), iteratorRef);
    try {
        if (ioReturn == DefaultKernReturnT.KERN_SUCCESS) {
            IORegistryEntryT entry = IORegistryEntryT.toIORegistryT(ioKit.IOIteratorNext(iteratorRef.getValue()));
            if (entry != null) {
                try {
                    CFMutableDictionaryRefByReference dictionaryRef = new CFMutableDictionaryRefByReference();
                    ioReturn = ioKit.IORegistryEntryCreateCFProperties(entry, dictionaryRef, CoreFoundation.ALLOCATOR, 0);
                    if (ioReturn == DefaultKernReturnT.KERN_SUCCESS) {
                        CFMutableDictionaryRef dictionary = dictionaryRef.getCFMutableDictionaryRef();
                        try {
                            CFTypeRef cfType = cf.CFDictionaryGetValue(dictionaryRef.getCFMutableDictionaryRef(), CFStringRef.toCFStringRef("HIDIdleTime"));
                            if (cfType != null) {
                                CFNumberRef cfNumber = new CFNumberRef(cfType);
                                LongByReference nanoSeconds = new LongByReference();
                                if (cf.CFNumberGetValue(cfNumber, CFNumberType.kCFNumberSInt64Type, nanoSeconds)) {
                                    return nanoSeconds.getValue() >> 20;
                                }
                                throw new IOKitException("HIDIdleTime out of range");
                            }
                            throw new IOKitException("HIDIdleTime not found");
                        } finally {
                            cf.CFRelease(dictionary);
                        }
                    }
                    throw new IOKitException("IORegistryEntryCreateCFProperties failed with error code: " + ioReturn.toStandardString());
                } finally {
                    ioKit.IOObjectRelease(entry);
                }
            }
            throw new IOKitException("IOHIDSystem not found");
        }
        throw new IOKitException("IOServiceGetMatchingServices failed with error code: " + ioReturn.toStandardString());
    } finally {
        // Even though Java doesn't understand it, this can be null because IOServiceGetMatchingServices() can return null.
        if (iteratorRef != null) {
            ioKit.IOObjectRelease(iteratorRef.getValue());
        }
    }
}
Also used : CFMutableDictionaryRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef) IOIteratorTRef(net.pms.util.jna.macos.types.IOIteratorTRef) LongByReference(com.sun.jna.ptr.LongByReference) KernReturnT(net.pms.util.jna.macos.kernreturn.KernReturnT) DefaultKernReturnT(net.pms.util.jna.macos.kernreturn.DefaultKernReturnT) IORegistryEntryT(net.pms.util.jna.macos.types.IORegistryEntryT) CFMutableDictionaryRefByReference(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRefByReference) CFTypeRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFTypeRef) CFNumberRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef)

Aggregations

CFMutableDictionaryRefByReference (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRefByReference)2 IOIteratorTRef (net.pms.util.jna.macos.types.IOIteratorTRef)2 IORegistryEntryT (net.pms.util.jna.macos.types.IORegistryEntryT)2 LongByReference (com.sun.jna.ptr.LongByReference)1 CFDictionaryRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFDictionaryRef)1 CFMutableDictionaryRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef)1 CFNumberRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef)1 CFStringRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef)1 CFTypeRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFTypeRef)1 DefaultKernReturnT (net.pms.util.jna.macos.kernreturn.DefaultKernReturnT)1 KernReturnT (net.pms.util.jna.macos.kernreturn.KernReturnT)1 IOIteratorT (net.pms.util.jna.macos.types.IOIteratorT)1 IONameT (net.pms.util.jna.macos.types.IONameT)1 IOObjectT (net.pms.util.jna.macos.types.IOObjectT)1 Test (org.junit.Test)1