use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef in project UniversalMediaServer by UniversalMediaServer.
the class IOKitUtils method disableGoToSleep.
/**
* Tells the OS to prevent system sleep until further notice by creating an
* assertion.
*
* @param assertionName the name of the assertion to create.
* @param assertionDetails the details of the assertion to create.
* @return The assertion id if an assertion is created. This value is needed
* when calling {@link #enableGoToSleep} to cancel the assertion.
* @throws IOKitException If an error occurs during the operation.
*/
public static int disableGoToSleep(String assertionName, String assertionDetails) throws IOKitException {
IntByReference assertionIdRef = new IntByReference();
CFStringRef assertionType = CFStringRef.toCFStringRef(IOKit.kIOPMAssertPreventUserIdleSystemSleep);
CFStringRef name = CFStringRef.toCFStringRef(assertionName);
CFStringRef details = CFStringRef.toCFStringRef(assertionDetails);
if (isMacOsVersionEqualOrGreater(7, 0)) {
KernReturnT ioReturn = ioKit.IOPMAssertionCreateWithDescription(assertionType, name, details, null, null, 0, null, assertionIdRef);
if (ioReturn == DefaultKernReturnT.KERN_SUCCESS) {
return assertionIdRef.getValue();
}
throw new IOKitException("IOPMAssertionCreateWithDescription failed with error code: " + ioReturn.toStandardString());
} else if (isMacOsVersionEqualOrGreater(6, 0)) {
KernReturnT ioReturn = ioKit.IOPMAssertionCreateWithName(assertionType, IOKit.kIOPMAssertionLevelOn, name, assertionIdRef);
if (ioReturn == DefaultKernReturnT.KERN_SUCCESS) {
return assertionIdRef.getValue();
}
throw new IOKitException("IOPMAssertionCreateWithName failed with error code: " + ioReturn.toStandardString());
} else if (isMacOsVersionEqualOrGreater(5, 0)) {
@SuppressWarnings("deprecation") KernReturnT ioReturn = ioKit.IOPMAssertionCreate(assertionType, IOKit.kIOPMAssertionLevelOn, assertionIdRef);
if (ioReturn == DefaultKernReturnT.KERN_SUCCESS) {
return assertionIdRef.getValue();
}
throw new IOKitException("IOPMAssertionCreate failed with error code: " + ioReturn.toStandardString());
}
throw new IOKitException("Unable to disable sleep mode; maxOS " + System.getProperty("os.version") + "doesn't support sleep prevention");
}
use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef in project UniversalMediaServer by UniversalMediaServer.
the class CoreFoundationTest method testCFString.
/**
* Tests {@link CFStringRef} and {@link CFMutableStringRef}.
*
* @throws Throwable if an error occurs during the test.
*/
@Test
public void testCFString() throws Throwable {
assertEquals("FooBar", CFStringRef.toCFStringRef("FooBar").toString());
assertEquals("", CFStringRef.toCFStringRef("").toString());
assertNull(CFStringRef.toCFStringRef(null));
assertEquals(IOKit.kIOPMAssertionTypePreventUserIdleDisplaySleep, CFStringRef.toCFStringRef(IOKit.kIOPMAssertionTypePreventUserIdleDisplaySleep).toString());
CFStringRef cfStringRef = new CFStringRef(Pointer.NULL);
CFStringRef cfStringRef2 = null;
assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
cfStringRef2 = new CFStringRef(Pointer.NULL);
assertEquals(CFComparisonResult.kCFCompareEqualTo, cfStringRef.compareTo(cfStringRef2));
cfStringRef = CFStringRef.toCFStringRef("a");
assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
cfStringRef2 = CFStringRef.toCFStringRef("b");
assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
CF.CFRelease(cfStringRef);
CF.CFRelease(cfStringRef2);
cfStringRef = CFStringRef.toCFStringRef("Foo-BAR");
cfStringRef2 = CFStringRef.toCFStringRef("foo-bar");
assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
assertEquals(CFComparisonResult.kCFCompareEqualTo, cfStringRef.compareTo(cfStringRef2, CFStringCompareFlags.kCFCompareCaseInsensitive));
assertEquals(CFComparisonResult.kCFCompareEqualTo, cfStringRef.compareTo(cfStringRef2, CFStringCompareFlags.kCFCompareCaseInsensitive, CFStringCompareFlags.kCFCompareCaseInsensitive));
assertEquals(new NativeLong(4), CF.CFStringConvertEncodingToNSStringEncoding(CFStringBuiltInEncodings.kCFStringEncodingUTF8.getValue()));
CF.CFRelease(cfStringRef);
cfStringRef = CF.CFStringCreateWithCString(null, "test string", CFStringBuiltInEncodings.kCFStringEncodingUTF8.getValue());
assertEquals("test string", cfStringRef.toString());
byte[] stringBytes = "Test byte array string".getBytes(StandardCharsets.UTF_16BE);
CF.CFRelease(cfStringRef2);
cfStringRef2 = CF.CFStringCreateWithBytes(null, stringBytes, stringBytes.length, CFStringBuiltInEncodings.kCFStringEncodingUTF16BE.getValue(), false);
assertEquals("Test byte array string", cfStringRef2.toString());
StringByReference refString = new StringByReference(20);
assertTrue(CF.CFStringGetCString(cfStringRef, refString, refString.getAllocatedSize(), CFStringBuiltInEncodings.kCFStringEncodingASCII.getValue()));
assertEquals("test string", refString.getValue());
refString = CF.CFStringGetCStringPtr(cfStringRef2, CFStringBuiltInEncodings.kCFStringEncodingMacRoman.getValue());
assertEquals("Test byte array string", refString.toString());
refString = new StringByReference(CF.CFStringGetMaximumSizeOfFileSystemRepresentation(cfStringRef2));
assertTrue(CF.CFStringGetFileSystemRepresentation(cfStringRef2, refString, refString.getAllocatedSize()));
CF.CFRelease(cfStringRef);
cfStringRef = CF.CFStringCreateWithFileSystemRepresentation(null, refString);
assertTrue(cfStringRef.compareTo(cfStringRef2) == CFComparisonResult.kCFCompareEqualTo);
CFMutableStringRef mutableStringRef = CF.CFStringCreateMutableCopy(null, 0, cfStringRef2);
assertTrue(mutableStringRef.compareTo(cfStringRef) == CFComparisonResult.kCFCompareEqualTo);
CF.CFRelease(cfStringRef);
CF.CFRelease(cfStringRef2);
CF.CFRelease(mutableStringRef);
}
use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef 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.CFStringRef 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.CFStringRef 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());
}
Aggregations