Search in sources :

Example 6 with NSAutoreleasePool

use of org.rococoa.cocoa.foundation.NSAutoreleasePool in project HearthStats.net-Uploader by HearthStats.

the class ProgramHelperOsx method findProgramPid.

/**
 * Looks for the program specified by {@link #_bundleIdentifier}, and if it finds it sets the {@link #_pid} to the process ID.
 * Resets the {@link #_pid} if the program could not be found (ie it's not running).
 */
private int findProgramPid(String bundleIdentifier) {
    final NSAutoreleasePool pool;
    try {
        pool = NSAutoreleasePool.new_();
    } catch (Throwable ex) {
        ex.printStackTrace(System.err);
        throw new RuntimeException("Unable to find program " + bundleIdentifier + " due to exception", ex);
    }
    try {
        final NSArray nsArray = NSRunningApplication.CLASS.runningApplicationsWithBundleIdentifier(bundleIdentifier);
        final int size = nsArray.count();
        for (int i = 0; i < size; i++) {
            final NSRunningApplication nsRunningApplication = Rococoa.cast(nsArray.objectAtIndex(i), NSRunningApplication.class);
            // This double-check of the bundle identifier is probably unnecessary...
            if (bundleIdentifier.equals(nsRunningApplication.bundleIdentifier())) {
                // We've found the application, so we can skip the rest of the loop
                return nsRunningApplication.processIdentifier();
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        throw new RuntimeException("Unable to find program " + bundleIdentifier + " due to exception", ex);
    } finally {
        pool.drain();
    }
    return 0;
}
Also used : NSAutoreleasePool(org.rococoa.cocoa.foundation.NSAutoreleasePool) NSArray(org.rococoa.cocoa.foundation.NSArray)

Example 7 with NSAutoreleasePool

use of org.rococoa.cocoa.foundation.NSAutoreleasePool in project HearthStats.net-Uploader by HearthStats.

the class ProgramHelperOsx method findWindow.

/**
 * <p>Finds the main Hearthstone window ID for the given process ID.</p>
 * <p>Will only return the window that matches expected characteristics of the main Hearthstone window, namely:</p>
 * <ul>
 *     <li>kCGWindowIsOnscreen = 1</li>
 *     <li>kCGWindowLayer = 0</li>
 *     <li>kCGWindowOwnerPID = [pid]</li>
 * </ul>
 *
 * @param pid The process ID of Hearthstone.
 * @return the window ID if found, or zero if no suitable window was found. It is normal for the window ID to be zero briefly during startup of Hearthstone.
 */
private int findWindow(int pid) {
    final NSAutoreleasePool pool = NSAutoreleasePool.new_();
    try {
        // Obtain a dictionary of all on-screen windows from Quartz Window Services, which will include all running applications.
        // Hearthstone typically has five or six windows, but only one or two are 'on screen' and it is those that we are interested in.
        final CFArrayRef originalArray = CoreGraphicsLibrary.INSTANCE.CGWindowListCopyWindowInfo(CGWindow.kCGWindowListExcludeDesktopElements | CGWindow.kCGWindowListOptionOnScreenOnly, 0);
        long count = CoreFoundationLibrary.INSTANCE.CFArrayGetCount(originalArray);
        for (long i = 0; i < count; i++) {
            // Obtain a CFDictionary containing this window's information dictionary
            Pointer pointer = CoreFoundationLibrary.INSTANCE.CFArrayGetValueAtIndex(originalArray, i);
            CFDictionaryRef dictionaryRef = new CFDictionaryRef(pointer);
            // Determine the process ID of this window
            NSString kCGWindowOwnerPID = CoreGraphicsLibrary.kCGWindowOwnerPID;
            Pointer pidPointer = CoreFoundationLibrary.INSTANCE.CFDictionaryGetValue(dictionaryRef, kCGWindowOwnerPID.id());
            NativeLongByReference longByReference = new NativeLongByReference();
            CoreFoundationLibrary.INSTANCE.CFNumberGetValue(pidPointer, CoreFoundationLibrary.CFNumberType.kCFNumberLongType, longByReference.getPointer());
            long pidLong = longByReference.getValue().longValue();
            if (pidLong == pid) {
                // This window is a Hearthstone window
                // When running in full-screen mode, Hearthstone has two windows: one for the game and one that appears to be a temporary desktop or space for the game to run in.
                // The game window always has a kCGWindowLayer of zero, whereas the desktop has a non-zero kCGWindowLayer.
                NSString kCGWindowLayer = CoreGraphicsLibrary.kCGWindowLayer;
                Pointer windowLayerPointer = CoreFoundationLibrary.INSTANCE.CFDictionaryGetValue(dictionaryRef, kCGWindowLayer.id());
                IntByReference windowLayerRef = new IntByReference();
                CoreFoundationLibrary.INSTANCE.CFNumberGetValue(windowLayerPointer, CoreFoundationLibrary.CFNumberType.kCFNumberFloatType, windowLayerRef.getPointer());
                int windowLayer = windowLayerRef.getValue();
                if (windowLayer == 0) {
                    // This window has a zero kCGWindowLayer so it must be the main Hearthstone window
                    NSString kCGWindowNumber = CoreGraphicsLibrary.kCGWindowNumber;
                    Pointer windowNumberPointer = CoreFoundationLibrary.INSTANCE.CFDictionaryGetValue(dictionaryRef, kCGWindowNumber.id());
                    IntByReference windowIdRef = new IntByReference();
                    CoreFoundationLibrary.INSTANCE.CFNumberGetValue(windowNumberPointer, CoreFoundationLibrary.CFNumberType.kCFNumberIntType, windowIdRef.getPointer());
                    int windowId = windowIdRef.getValue();
                    return windowId;
                }
            }
        }
        // No Hearthstone window was found
        return 0;
    } finally {
        pool.drain();
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) NSAutoreleasePool(org.rococoa.cocoa.foundation.NSAutoreleasePool) Pointer(com.sun.jna.Pointer) NSString(org.rococoa.cocoa.foundation.NSString) NativeLongByReference(com.sun.jna.ptr.NativeLongByReference)

Aggregations

NSAutoreleasePool (org.rococoa.cocoa.foundation.NSAutoreleasePool)7 Pointer (com.sun.jna.Pointer)2 IntByReference (com.sun.jna.ptr.IntByReference)2 NSUserNotificationCenter (net.hearthstats.osx.jna.NSUserNotificationCenter)2 NSArray (org.rococoa.cocoa.foundation.NSArray)2 NSString (org.rococoa.cocoa.foundation.NSString)2 NativeLongByReference (com.sun.jna.ptr.NativeLongByReference)1 BufferedImage (java.awt.image.BufferedImage)1 NSUserNotification (net.hearthstats.osx.jna.NSUserNotification)1