use of org.rococoa.cocoa.foundation.NSAutoreleasePool in project HearthStats.net-Uploader by HearthStats.
the class OsxNotificationQueue method add.
@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();
}
}
use of org.rococoa.cocoa.foundation.NSAutoreleasePool in project HearthStats.net-Uploader by HearthStats.
the class OsxNotificationQueue method clearAllNotifications.
@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();
}
}
use of org.rococoa.cocoa.foundation.NSAutoreleasePool in project HearthStats.net-Uploader by HearthStats.
the class ProgramHelperOsx method getHSWindowBounds.
@Override
public Rectangle getHSWindowBounds() {
final NSAutoreleasePool pool = NSAutoreleasePool.new_();
try {
if (_pid == 0) {
_pid = findProgramPid(_bundleIdentifier);
}
if (_windowId == 0) {
_windowId = findWindow(_pid);
}
// 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);
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;
}
use of org.rococoa.cocoa.foundation.NSAutoreleasePool in project HearthStats.net-Uploader by HearthStats.
the class ProgramHelperOsx method bringWindowToForeground.
@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;
}
use of org.rococoa.cocoa.foundation.NSAutoreleasePool in project HearthStats.net-Uploader by HearthStats.
the class ProgramHelperOsx method getScreenCapture.
@Override
public BufferedImage getScreenCapture() {
final NSAutoreleasePool pool = NSAutoreleasePool.new_();
try {
if (_windowId > 0) {
// We already know the ID of the program window.
BufferedImage image = getWindowImage(_windowId);
if (image == null) {
// We seem to have lost the window?
debugLog.debug(" Window not found, resetting _windowId and _pid to 0");
_windowId = 0;
_pid = 0;
return null;
} else {
return image;
}
} else {
// We don't know the ID of the program window, so look for it now.
_windowId = findWindow(_pid);
if (_windowId == 0) {
// The window couldn't be found, so maybe the program has been closed. Reset the pid to force it to start from the start
debugLog.debug(" Window not found, resetting _pid to 0");
_pid = 0;
} else {
// The program was found, so take an image
debugLog.debug(" Window found, setting _windowId to {}", _windowId);
BufferedImage image = getWindowImage(_windowId);
return image;
}
}
} catch (Throwable ex) {
ex.printStackTrace(System.err);
throw new RuntimeException("Unable to create screen capture for " + _bundleIdentifier + " due to exception", ex);
} finally {
pool.drain();
}
return null;
}
Aggregations