Search in sources :

Example 1 with CFMutableDictionaryRef

use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef in project UniversalMediaServer by UniversalMediaServer.

the class CoreFoundationTest method testCFDictionary.

/**
 * Tests {@link CFDictionaryRef} and {@link CFMutableDictionaryRef}.
 *
 * @throws Throwable if an error occurs during the test.
 */
@Test
public void testCFDictionary() throws Throwable {
    CFStringRef cfStringRef = CFStringRef.toCFStringRef("Test");
    CFNumberRef cfNumberRef = CFNumberRef.toCFNumberRef(4.3);
    CFMutableDictionaryRef mutableDictionaryRef = IOKit.INSTANCE.IOServiceMatching("IOHIDSystem");
    assertTrue(CF.CFDictionaryGetCount(mutableDictionaryRef) > 0);
    long l = CF.CFDictionaryGetCount(mutableDictionaryRef);
    CF.CFDictionaryAddValue(mutableDictionaryRef, cfNumberRef, cfStringRef);
    assertEquals(l + 1, CF.CFDictionaryGetCount(mutableDictionaryRef));
    assertTrue(CF.CFDictionaryContainsKey(mutableDictionaryRef, cfNumberRef));
    assertTrue(CF.CFDictionaryContainsValue(mutableDictionaryRef, cfStringRef));
    CFDictionaryRef dictionaryRef = CF.CFDictionaryCreateCopy(CoreFoundation.ALLOCATOR, mutableDictionaryRef);
    CFTypeArrayRef keys = new CFTypeArrayRef(CF.CFDictionaryGetCount(dictionaryRef));
    CFTypeArrayRef values = new CFTypeArrayRef(CF.CFDictionaryGetCount(dictionaryRef));
    CF.CFDictionaryGetKeysAndValues(dictionaryRef, keys, values);
    assertTrue(Arrays.asList(keys.getArray()).contains(cfNumberRef));
    assertTrue(Arrays.asList(values.getArray()).contains(cfStringRef));
    CF.CFDictionaryRemoveValue(mutableDictionaryRef, cfNumberRef);
    assertEquals(l, CF.CFDictionaryGetCount(mutableDictionaryRef));
    PointerByReference pointerRef = new PointerByReference();
    assertTrue(CF.CFDictionaryGetValueIfPresent(dictionaryRef, cfNumberRef, pointerRef));
    CFStringRef cfStringRef2 = new CFStringRef(pointerRef.getValue());
    assertTrue(cfStringRef.compareTo(cfStringRef2) == CFComparisonResult.kCFCompareEqualTo);
    assertEquals(cfStringRef.toString(), cfStringRef2.toString());
    assertEquals(cfStringRef, cfStringRef2);
    // Don't release cfStringRef2 as it's actually the same CFStringRef instance as cfStringRef
    CF.CFRelease(dictionaryRef);
    CF.CFRelease(mutableDictionaryRef);
    CF.CFRelease(cfNumberRef);
    CF.CFRelease(cfStringRef);
}
Also used : CFDictionaryRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFDictionaryRef) CFMutableDictionaryRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef) CFStringRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef) PointerByReference(com.sun.jna.ptr.PointerByReference) CFNumberRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef) Test(org.junit.Test)

Example 2 with CFMutableDictionaryRef

use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef 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

CFMutableDictionaryRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef)2 CFNumberRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef)2 LongByReference (com.sun.jna.ptr.LongByReference)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1 CFDictionaryRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFDictionaryRef)1 CFMutableDictionaryRefByReference (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRefByReference)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 IOIteratorTRef (net.pms.util.jna.macos.types.IOIteratorTRef)1 IORegistryEntryT (net.pms.util.jna.macos.types.IORegistryEntryT)1 Test (org.junit.Test)1