Search in sources :

Example 1 with CFNumberRef

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

the class CoreFoundationTest method testCFArray.

/**
 * Tests {@link CFArrayRef} and {@link CFMutableArrayRef}.
 *
 * @throws Throwable if an error occurs during the test.
 */
@Test
public void testCFArray() {
    CFTypeRef[] cfTypeRefs = { CFStringRef.toCFStringRef("First array element"), CFStringRef.toCFStringRef("Second array element"), CFStringRef.toCFStringRef("Third array element"), CFStringRef.toCFStringRef("Fourth array element"), CFStringRef.toCFStringRef("Fifth array element") };
    CFTypeArrayRef typeArrayRef = new CFTypeArrayRef(cfTypeRefs);
    CFArrayRef cfArrayRef = CF.CFArrayCreate(null, typeArrayRef, typeArrayRef.getSize(), CoreFoundation.kCFTypeArrayCallBacks);
    assertEquals(5, CF.CFArrayGetCount(cfArrayRef));
    CFMutableArrayRef cfMutableArrayRef = CF.CFArrayCreateMutableCopy(null, 10, cfArrayRef);
    assertEquals(5, CF.CFArrayGetCount(cfMutableArrayRef));
    CFNumberRef cfNumberRef = CFNumberRef.toCFNumberRef(50);
    CF.CFArrayAppendValue(cfMutableArrayRef, cfNumberRef);
    assertEquals(6, CF.CFArrayGetCount(cfMutableArrayRef));
    assertEquals("Fourth array element", new CFStringRef(CF.CFArrayGetValueAtIndex(cfMutableArrayRef, 3)).toString());
    assertEquals("50", new CFNumberRef(CF.CFArrayGetValueAtIndex(cfMutableArrayRef, 5)).toString());
    CF.CFArrayInsertValueAtIndex(cfMutableArrayRef, 0, cfNumberRef);
    assertEquals(7, CF.CFArrayGetCount(cfMutableArrayRef));
    assertEquals("50", new CFNumberRef(CF.CFArrayGetValueAtIndex(cfMutableArrayRef, 0)).toString());
    CF.CFArrayExchangeValuesAtIndices(cfMutableArrayRef, 0, 2);
    assertEquals("50", new CFNumberRef(CF.CFArrayGetValueAtIndex(cfMutableArrayRef, 2)).toString());
    CF.CFArrayRemoveAllValues(cfMutableArrayRef);
    assertEquals(0, CF.CFArrayGetCount(cfMutableArrayRef));
    CF.CFRelease(cfNumberRef);
    CF.CFRelease(cfMutableArrayRef);
    CF.CFRelease(cfArrayRef);
    for (CFTypeRef ref : cfTypeRefs) {
        CF.CFRelease(ref);
    }
}
Also used : CFArrayRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFArrayRef) CFStringRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef) CFMutableArrayRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableArrayRef) CFTypeRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFTypeRef) CFNumberRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef) Test(org.junit.Test)

Example 2 with CFNumberRef

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

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

the class CoreFoundationTest method testCFNumber.

/**
 * Tests {@link CFNumberRef}.
 *
 * @throws Throwable if an error occurs during the test.
 */
@Test
public void testCFNumber() throws Throwable {
    IntByReference intRef = new IntByReference();
    LongByReference longRef = new LongByReference();
    DoubleByReference doubleRef = new DoubleByReference();
    CFNumberRef cfNumberRef = CFNumberRef.toCFNumberRef((byte) 254);
    assertEquals((byte) 254, cfNumberRef.getValue());
    assertEquals(CFNumberType.kCFNumberSInt8Type, CF.CFNumberGetType(cfNumberRef));
    assertFalse(CF.CFNumberIsFloatType(cfNumberRef));
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef((short) 31400);
    assertEquals((short) 31400, cfNumberRef.getValue());
    assertEquals(CFNumberType.kCFNumberSInt16Type, CF.CFNumberGetType(cfNumberRef));
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef('a');
    assertEquals('a', cfNumberRef.getValue().intValue());
    assertEquals(CFNumberType.kCFNumberSInt8Type, CF.CFNumberGetType(cfNumberRef));
    assertFalse(CF.CFNumberIsFloatType(cfNumberRef));
    assertTrue(CF.CFNumberGetValue(cfNumberRef, CFNumberType.kCFNumberSInt32Type, intRef));
    assertEquals('a', intRef.getValue());
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef(11.43d);
    assertEquals(11.43d, cfNumberRef.getValue().doubleValue(), 0.0d);
    assertEquals(CFNumberType.kCFNumberFloat64Type, CF.CFNumberGetType(cfNumberRef));
    assertTrue(CF.CFNumberIsFloatType(cfNumberRef));
    assertFalse(CF.CFNumberGetValue(cfNumberRef, CFNumberType.kCFNumberSInt32Type, intRef));
    assertTrue(CF.CFNumberGetValue(cfNumberRef, CFNumberType.kCFNumberFloat64Type, doubleRef));
    assertEquals(11, intRef.getValue());
    assertEquals(11.43d, doubleRef.getValue(), 0.0d);
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef(-43.7f);
    assertEquals(-43.7f, cfNumberRef.getValue().floatValue(), 0.0f);
    assertEquals(CFNumberType.kCFNumberFloat32Type, CF.CFNumberGetType(cfNumberRef));
    assertTrue(CF.CFNumberIsFloatType(cfNumberRef));
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef(Integer.MAX_VALUE);
    assertEquals(Integer.MAX_VALUE, cfNumberRef.getValue());
    assertEquals(CFNumberType.kCFNumberSInt32Type, CF.CFNumberGetType(cfNumberRef));
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef(Integer.MIN_VALUE);
    assertEquals(Integer.MIN_VALUE, cfNumberRef.getValue());
    assertEquals(CFNumberType.kCFNumberSInt32Type, CF.CFNumberGetType(cfNumberRef));
    assertFalse(CF.CFNumberIsFloatType(cfNumberRef));
    assertTrue(CF.CFNumberGetValue(cfNumberRef, CFNumberType.kCFNumberSInt32Type, intRef));
    assertEquals(Integer.MIN_VALUE, intRef.getValue());
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef(0);
    assertEquals(0, cfNumberRef.getValue());
    assertEquals(CFNumberType.kCFNumberSInt32Type, CF.CFNumberGetType(cfNumberRef));
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef(Long.MAX_VALUE);
    assertEquals(Long.MAX_VALUE, cfNumberRef.getValue());
    assertEquals(CFNumberType.kCFNumberSInt64Type, CF.CFNumberGetType(cfNumberRef));
    assertFalse(CF.CFNumberIsFloatType(cfNumberRef));
    assertTrue(CF.CFNumberGetValue(cfNumberRef, CFNumberType.kCFNumberSInt64Type, longRef));
    assertEquals(Long.MAX_VALUE, longRef.getValue());
    CF.CFRelease(cfNumberRef);
    cfNumberRef = CFNumberRef.toCFNumberRef(Long.MIN_VALUE);
    assertEquals(Long.MIN_VALUE, cfNumberRef.getValue());
    assertEquals(CFNumberType.kCFNumberSInt64Type, CF.CFNumberGetType(cfNumberRef));
    CF.CFRelease(cfNumberRef);
}
Also used : DoubleByReference(com.sun.jna.ptr.DoubleByReference) IntByReference(com.sun.jna.ptr.IntByReference) LongByReference(com.sun.jna.ptr.LongByReference) CFNumberRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef) Test(org.junit.Test)

Example 4 with CFNumberRef

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

CFNumberRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef)4 Test (org.junit.Test)3 LongByReference (com.sun.jna.ptr.LongByReference)2 CFMutableDictionaryRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef)2 CFStringRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef)2 CFTypeRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFTypeRef)2 DoubleByReference (com.sun.jna.ptr.DoubleByReference)1 IntByReference (com.sun.jna.ptr.IntByReference)1 PointerByReference (com.sun.jna.ptr.PointerByReference)1 CFArrayRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFArrayRef)1 CFDictionaryRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFDictionaryRef)1 CFMutableArrayRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableArrayRef)1 CFMutableDictionaryRefByReference (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRefByReference)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