Search in sources :

Example 1 with NativeLongByReference

use of com.sun.jna.ptr.NativeLongByReference in project jna by java-native-access.

the class ByReferenceArgumentsTest method testNativeLongByReference.

public void testNativeLongByReference() {
    NativeLongByReference iref = new NativeLongByReference();
    lib.incrementNativeLongByReference(iref);
    assertEquals("Native long argument not modified", new NativeLong(1), iref.getValue());
}
Also used : NativeLongByReference(com.sun.jna.ptr.NativeLongByReference)

Example 2 with NativeLongByReference

use of com.sun.jna.ptr.NativeLongByReference 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

NativeLongByReference (com.sun.jna.ptr.NativeLongByReference)2 Pointer (com.sun.jna.Pointer)1 IntByReference (com.sun.jna.ptr.IntByReference)1 NSAutoreleasePool (org.rococoa.cocoa.foundation.NSAutoreleasePool)1 NSString (org.rococoa.cocoa.foundation.NSString)1