use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFTypeRef 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);
}
}
use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFTypeRef 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