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);
}
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());
}
}
}
Aggregations