Search in sources :

Example 1 with KernReturnT

use of net.pms.util.jna.macos.kernreturn.KernReturnT 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");
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) CFStringRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef) KernReturnT(net.pms.util.jna.macos.kernreturn.KernReturnT) DefaultKernReturnT(net.pms.util.jna.macos.kernreturn.DefaultKernReturnT)

Example 2 with KernReturnT

use of net.pms.util.jna.macos.kernreturn.KernReturnT in project UniversalMediaServer by UniversalMediaServer.

the class IOKitUtils method resetIdleTimer.

/**
 * Creates an assertion preventing sleep with a timeout value equal to the
 * idle sleep time. This has the same effect as if a user activity occurred
 * so that the idle timer was reset.
 *
 * @param assertionName the name of the assertion to create.
 * @param assertionId the assertion id if you want to extend the time of an
 *            existing assertion or {@code 0} to create a new assertion.
 * @return The assertionId of the created assertion. This may or may not be
 *         the same as {@code assertionId}.
 * @throws IOKitException If an error occurs during the operation.
 */
public static int resetIdleTimer(String assertionName, int assertionId) throws IOKitException {
    if (isMacOsVersionEqualOrGreater(7, 3)) {
        IntByReference assertionIdRef = new IntByReference(assertionId > 0 ? assertionId : 0);
        CFStringRef name = CFStringRef.toCFStringRef(assertionName);
        KernReturnT ioReturn = ioKit.IOPMAssertionDeclareUserActivity(name, IOPMUserActiveType.kIOPMUserActiveLocal, assertionIdRef);
        if (ioReturn == DefaultKernReturnT.KERN_SUCCESS) {
            return assertionIdRef.getValue();
        }
        throw new IOKitException("IOPMAssertionDeclareUserActivity failed with error code: " + ioReturn.toStandardString());
    }
    LOGGER.warn("Unable to reset sleep timer; not supported by maxOS version {}", System.getProperty("os.version"));
    return -1;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) CFStringRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef) KernReturnT(net.pms.util.jna.macos.kernreturn.KernReturnT) DefaultKernReturnT(net.pms.util.jna.macos.kernreturn.DefaultKernReturnT)

Example 3 with KernReturnT

use of net.pms.util.jna.macos.kernreturn.KernReturnT 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

DefaultKernReturnT (net.pms.util.jna.macos.kernreturn.DefaultKernReturnT)3 KernReturnT (net.pms.util.jna.macos.kernreturn.KernReturnT)3 IntByReference (com.sun.jna.ptr.IntByReference)2 CFStringRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef)2 LongByReference (com.sun.jna.ptr.LongByReference)1 CFMutableDictionaryRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRef)1 CFMutableDictionaryRefByReference (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableDictionaryRefByReference)1 CFNumberRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFNumberRef)1 CFTypeRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFTypeRef)1 IOIteratorTRef (net.pms.util.jna.macos.types.IOIteratorTRef)1 IORegistryEntryT (net.pms.util.jna.macos.types.IORegistryEntryT)1