// 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;