Examples of NSAutoreleasePool


Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

     *
     * @param windowId The window ID of Hearthstone, as reported by a call to Quartz Window Services
     * @return An image of the window, or null if the window doesn't exist or is too small to be an active window.
     */
    private BufferedImage getWindowImage(int windowId) {
        final NSAutoreleasePool pool = NSAutoreleasePool.new_();
        try {
            // Create a CGRect with zero boundaries so that OS X automatically picks the correct size
            CoreGraphicsLibrary.CGRect bounds = new CoreGraphicsLibrary.CGRect.CGRectByValue();
            bounds.origin = new CoreGraphicsLibrary.CGPoint();
            bounds.origin.x = 0;
            bounds.origin.y = 0;
            bounds.size = new CoreGraphicsLibrary.CGSize();
            bounds.size.width = 0;
            bounds.size.height = 0;

            // Take a screenshot of the program window
            ID imageRef = CoreGraphicsLibrary.INSTANCE.CGWindowListCreateImage(bounds, CoreGraphicsLibrary.kCGWindowListOptionIncludingWindow | CoreGraphicsLibrary.kCGWindowListExcludeDesktopElements, windowId, CoreGraphicsLibrary.kCGWindowImageBoundsIgnoreFraming | CoreGraphicsLibrary.kCGWindowImageNominalResolution);

            // Convert the screenshot into a more useful ImageRep object, and retain the object so that it isn't lost before we extract the image data
            NSBitmapImageRep imageRep = NSBitmapImageRep.CLASS.alloc().initWithCGImage(imageRef).initWithCGImage(imageRef);
            imageRep.retain();

            int width = imageRep.pixelsWide();
            int height = imageRep.pixelsHigh();

            int windowTitleHeight = determineWindowTitleHeight(height, width);

            if (debugLog.isTraceEnabled()) {
                debugLog.trace("    Window height={} width={} titleHeight={}", new Object[] { height, width, windowTitleHeight});
            }

            int heightWithoutTitle = height - windowTitleHeight;

            Pointer bitmapPointer = imageRep.bitmapData();
            if (bitmapPointer == null || bitmapPointer == Pointer.NULL) {
                imageRep.release();
                return null;

            } else {
                int[] data = bitmapPointer.getIntArray(0, width * height);

                if (heightWithoutTitle > 512) {
                    BufferedImage image = new BufferedImage(width, heightWithoutTitle, BufferedImage.TYPE_INT_RGB);

                    // Start on row windowTitleHeight to exclude the window titlebar
                    int idx = windowTitleHeight * width;

                    // Manually write each pixel to the raster because OS X generates ARGB screenshots but BufferedImage expects RGB data.
                    WritableRaster raster = image.getRaster();
                    for (int y = 0; y < heightWithoutTitle; y++) {

                        for (int x = 0; x < width; x++) {
                            int pixel = data[idx++];
                            raster.setSample(x, y, 0, pixel >> 8 & 0xFF);   // Red is the second byte
                            raster.setSample(x, y, 1, pixel >> 16 & 0xFF)// Green is the third byte
                            raster.setSample(x, y, 2, pixel >> 24 & 0xFF)// Blue is the fourth byte
                        }
                    }

                    // Now that we have a copy of the image in a Java object it's safe to release the native pointers
                    Foundation.cfRelease(imageRef);
                    imageRep.release();

                    return image;

                } else {
                    // The window is too small to generate an image
                    return null;
                }
            }

        } finally {
            pool.drain();
        }
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

     *
     * @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();
        }
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

    /**
     * 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;
    }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

    }

  @Override
  public Rectangle getHSWindowBounds() {

    final NSAutoreleasePool pool = NSAutoreleasePool.new_();
    try {

      // CGWindowListCreateDescriptionFromArray would be more efficient than the loop below,\
      // but isn't working... commented-out until can be fixed.
//      final Pointer[] values = { new IntByReference(_windowId).getPointer() };
//      CFArrayRef windowArray = CoreFoundationLibrary.INSTANCE.CFArrayCreate(null, values, 1, null);
//      final CFArrayRef descriptionArray = CoreGraphicsLibrary.INSTANCE.CGWindowListCreateDescriptionFromArray(windowArray);
//      long count = CoreFoundationLibrary.INSTANCE.CFArrayGetCount(descriptionArray);


      // Instead, obtain a dictionary of all on-screen windows from Quartz Window Services, which will include all running applications.
      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 ID of this 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 thisWindowId = windowIdRef.getValue();

        if (thisWindowId == _windowId) {

          // Determine the bounds of this window
          NSString kCGWindowBounds = CoreGraphicsLibrary.kCGWindowBounds;
          Pointer boundPointer = CoreFoundationLibrary.INSTANCE.CFDictionaryGetValue(dictionaryRef, kCGWindowBounds.id());

          CoreGraphicsLibrary.CGRectRef rect = new CoreGraphicsLibrary.CGRectRef();
          boolean result = CoreGraphicsLibrary.INSTANCE.CGRectMakeWithDictionaryRepresentation(boundPointer, rect);

          int x = (int) rect.origin.x;
          int y = (int) rect.origin.y;
          int width = (int) rect.size.width;
          int height = (int) rect.size.height;

          // Determine height of the title bar, if present
          int titleHeight = determineWindowTitleHeight(height, width);

          debugLog.debug("Found Hearthstone window at x={} y={} width={} height={} title={}", x, y, width, height, titleHeight);

//          x = x + titleHeight;
//          height = height - titleHeight;

          return new Rectangle(x, y, width, height);
        }
      }

    } finally {
      pool.drain();
    }

    // Couldn't find the Hearthstone window so return null... this will break the calling code.
    debugLog.warn("Unable to find position of Hearthstone window.");
    return null;
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

  }


  @Override
  public boolean bringWindowToForeground() {
    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())) {
          boolean result = nsRunningApplication.activateWithOptions(0);
          debugLog.debug("nsRunningApplication.activateWithOptions returned {}", result);
          return result;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace(System.err);
      throw new RuntimeException("Unable to find program " + _bundleIdentifier + " due to exception", ex);
    } finally {
      pool.drain();
    }
    return false;
  }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

  @Override
  public void add(String header, String message, boolean allowFocus) {
    debugLog.debug("    Showing OS X notification \"{}\", \"{}\"", header, message);

    final NSAutoreleasePool pool = NSAutoreleasePool.new_();
    try {
      NSUserNotification nsUserNotification = NSUserNotification.CLASS.alloc();

      nsUserNotification.setTitle(header);
      nsUserNotification.setSubtitle(message);

      NSUserNotificationCenter defaultNotificationCenter = NSUserNotificationCenter.CLASS.defaultUserNotificationCenter();
      defaultNotificationCenter.setDelegate(defaultNotificationCenter);
      defaultNotificationCenter.deliverNotification(nsUserNotification);
    } finally {
      pool.drain();
    }

  }
View Full Code Here

Examples of org.rococoa.cocoa.foundation.NSAutoreleasePool

  @Override
  public void clearAllNotifications() {
    debugLog.debug("    Clearing all notifications");

    final NSAutoreleasePool pool = NSAutoreleasePool.new_();
    try {
      NSUserNotificationCenter defaultNotificationCenter = NSUserNotificationCenter.CLASS.defaultUserNotificationCenter();
      defaultNotificationCenter.removeAllDeliveredNotifications();
    } finally {
      pool.drain();
    }

  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.