<commit_before>package net.hearthstats;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Observable;

import jna.extra.GDI32Extra;
import jna.extra.User32Extra;
import jna.extra.WinGDIExtra;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.GDI32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinGDI;
import com.sun.jna.platform.win32.Variant.VARIANT._VARIANT.__VARIANT;
import com.sun.jna.platform.win32.WinDef.HBITMAP;
import com.sun.jna.platform.win32.WinDef.HDC;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinGDI.BITMAPINFO;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.ptr.PointerByReference;

public class ProgramHelper extends Observable {

	protected String _programName;
	private String _processName;
	private HWND _windowHandle = null;
	private String _windowHandleId = null;
	private boolean _isFullscreen = false;
	private static boolean _isMinimized = false;
	
	public ProgramHelper(String programName, String processName) {
		_programName = programName;
		_processName = processName;
	}

	public BufferedImage getScreenCapture() {
		if (foundProgram()) {

			BufferedImage image;
			
			// only supports windows at the moment
			image = _getScreenCaptureWindows(_windowHandle);

			// TODO: implement OSX version
			
			return image;
		}
		return null;
	}
	
	private HWND _getWindowHandle() {
		_windowHandle = null;
		User32.INSTANCE.EnumWindows(new WNDENUMPROC() {
	        @Override
	        public boolean callback(HWND hWnd, Pointer arg1) {
	            
	            int titleLength = User32.INSTANCE.GetWindowTextLength(hWnd) + 1;
	            char[] title = new char[titleLength];
	            User32.INSTANCE.GetWindowText(hWnd, title, titleLength);
	            String wText = Native.toString(title);
	            
	            if (wText.isEmpty()) {
	               return true;
	            }
	
	            if(wText.equals(_programName)) {
	            	
		      	 	PointerByReference pointer = new PointerByReference();
		      	    User32DLL.GetWindowThreadProcessId(hWnd, pointer);
		      	    Pointer process = Kernel32.OpenProcess(Kernel32.PROCESS_QUERY_INFORMATION | Kernel32.PROCESS_VM_READ, false, pointer.getValue());
		      	    Psapi.GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
		      	    if(Native.toString(buffer).equals(_processName)) {
		      	    	_windowHandle = hWnd;
		      	    	if(_windowHandleId == null) {
		      	    		_windowHandleId = _windowHandle.toString();
		      	    		_notifyObserversOfChangeTo(_programName + " window found with process name " + _processName);
		      	    	}
		      	    }
	            }
	            return true;
         	}
		}, null);
		
		// notify of window lost
		if(_windowHandle == null && _windowHandleId != null) {
			_notifyObserversOfChangeTo(_programName + " window with process name " + _processName + " closed");
			_windowHandleId = null;
		}
		return _windowHandle;
	}
	
	/**
	 * Is the program found?
	 * 
	 * @return Whether or not the program is found
	 */<commit_msg>#66 Get window class<commit_after>package net.hearthstats;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Observable;

import jna.extra.GDI32Extra;
import jna.extra.User32Extra;
import jna.extra.WinGDIExtra;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.GDI32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinGDI;
import com.sun.jna.platform.win32.Variant.VARIANT._VARIANT.__VARIANT;
import com.sun.jna.platform.win32.WinDef.HBITMAP;
import com.sun.jna.platform.win32.WinDef.HDC;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinGDI.BITMAPINFO;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.ptr.PointerByReference;

public class ProgramHelper extends Observable {

	protected String _programName;
	private String _processName;
	private HWND _windowHandle = null;
	private String _windowHandleId = null;
	private boolean _isFullscreen = false;
	private static boolean _isMinimized = false;
	
	public ProgramHelper(String programName, String processName) {
		_programName = programName;
		_processName = processName;
	}

	public BufferedImage getScreenCapture() {
		if (foundProgram()) {

			BufferedImage image;
			
			// only supports windows at the moment
			image = _getScreenCaptureWindows(_windowHandle);

			// TODO: implement OSX version
			
			return image;
		}
		return null;
	}
	
	private HWND _getWindowHandle() {
		_windowHandle = null;
		User32.INSTANCE.EnumWindows(new WNDENUMPROC() {
	        @Override
	        public boolean callback(HWND hWnd, Pointer arg1) {
	            
	            int titleLength = User32.INSTANCE.GetWindowTextLength(hWnd) + 1;
	            char[] title = new char[titleLength];
	            User32.INSTANCE.GetWindowText(hWnd, title, titleLength);
	            String wText = Native.toString(title);
	            
	            if (wText.isEmpty()) {
	               return true;
	            }
	
	            if(wText.equals(_programName)) {
	            	
		      	 	PointerByReference pointer = new PointerByReference();
		      	    User32DLL.GetWindowThreadProcessId(hWnd, pointer);
		      	    Pointer process = Kernel32.OpenProcess(Kernel32.PROCESS_QUERY_INFORMATION | Kernel32.PROCESS_VM_READ, false, pointer.getValue());
		      	    Psapi.GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
		      	    
		      	    if(Native.toString(buffer).equals(_processName)) {
		      	    	_windowHandle = hWnd;
		      	    	
		      	    	char[] className = new char[512];
		      	    	User32.INSTANCE.GetClassName(_windowHandle, className, 512);
		      	    	String classNameStr = Native.toString(className);
		      	    	
		      	    	if(_windowHandleId == null) {
		      	    		_windowHandleId = _windowHandle.toString();
		      	    		_notifyObserversOfChangeTo(_programName + " window found with process name " + _processName);
		      	    	}
		      	    }
	            }
	            return true;
         	}
		}, null);
		
		// notify of window lost
		if(_windowHandle == null && _windowHandleId != null) {
			_notifyObserversOfChangeTo(_programName + " window with process name " + _processName + " closed");
			_windowHandleId = null;
		}
		return _windowHandle;
	}
	
	/**
	 * Is the program found?
	 * 
	 * @return Whether or not the program is found
	 */
{'fraction_non_alphanumeric': 0.09542314335060449, 'fraction_numerical': 0.01468048359240069, 'mean_word_length': 2.9149295774647888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1870954', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2160, 'n_words': 423}