diff --git a/window/goWindow.h b/window/goWindow.h index 25dbfc2..2bf1d9e 100644 --- a/window/goWindow.h +++ b/window/goWindow.h @@ -57,16 +57,46 @@ void set_active(const MData win){ SetActive(win); } +#if defined(IS_WINDOWS) + typedef struct{ + HWND hWnd; + DWORD dwPid; + }WNDINFO; + + BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam){ + WNDINFO* pInfo = (WNDINFO*)lParam; + DWORD dwProcessId = 0; + GetWindowThreadProcessId(hWnd, &dwProcessId); + + if(dwProcessId == pInfo->dwPid){ + pInfo->hWnd = hWnd; + return FALSE; + } + return TRUE; + } + + HWND GetHwndByPId(DWORD dwProcessId){ + WNDINFO info = {0}; + info.hWnd = NULL; + info.dwPid = dwProcessId; + EnumWindows(EnumWindowsProc, (LPARAM)&info); + // printf("%d\n", info.hWnd); + return info.hWnd; + } +#endif + void active_PID(uintptr pid){ MData win; #if defined(IS_MACOSX) // Handle to a AXUIElementRef win.AxID = AXUIElementCreateApplication(pid); #elif defined(USE_X11) - win.XWin = (Window) pid; // Handle to an X11 window + win.XWin = (Window)pid; // Handle to an X11 window #elif defined(IS_WINDOWS) - win.HWnd = (HWND) pid; // Handle to a window HWND + // win.HWnd = (HWND)pid; // Handle to a window HWND + win.HWnd = GetHwndByPId(pid); #endif + SetActive(win); } diff --git a/window/window.h b/window/window.h index a6ca217..5c25009 100644 --- a/window/window.h +++ b/window/window.h @@ -504,8 +504,8 @@ bool IsTopMost(void){ #elif defined(IS_WINDOWS) - return (GetWindowLongPtr(mData.HWnd, - GWL_EXSTYLE) & WS_EX_TOPMOST) != 0; + return (GetWindowLongPtr(mData.HWnd, GWL_EXSTYLE) + & WS_EX_TOPMOST) != 0; #endif } @@ -537,8 +537,8 @@ bool IsMinimized(void){ #elif defined(IS_WINDOWS) - return (GetWindowLongPtr(mData.HWnd, - GWL_STYLE) & WS_MINIMIZE) != 0; + return (GetWindowLongPtr(mData.HWnd, GWL_STYLE) + & WS_MINIMIZE) != 0; #endif } @@ -560,8 +560,8 @@ bool IsMaximized(void){ #elif defined(IS_WINDOWS) - return (GetWindowLongPtr (mData.HWnd, - GWL_STYLE) & WS_MAXIMIZE) != 0; + return (GetWindowLongPtr(mData.HWnd, GWL_STYLE) + & WS_MAXIMIZE) != 0; #endif } @@ -676,8 +676,8 @@ MData GetActive(void){ CGWindowID win = 0; // Use undocumented API to get WID - if (_AXUIElementGetWindow (element, - &win) == kAXErrorSuccess && win){ + if (_AXUIElementGetWindow (element, &win) + == kAXErrorSuccess && win){ // Manually set internals result.CgID = win; result.AxID = element; @@ -697,8 +697,7 @@ MData GetActive(void){ MData result; Display *rDisplay = XOpenDisplay(NULL); // Check X-Window display - if (WM_ACTIVE == None || - rDisplay == NULL){ + if (WM_ACTIVE == None || rDisplay == NULL){ return result; } @@ -725,8 +724,7 @@ MData GetActive(void){ // Use input focus instead Window window = None; int revert = RevertToNone; - XGetInputFocus(rDisplay, - &window, &revert); + XGetInputFocus(rDisplay, &window, &revert); // Return foreground window result.XWin = window; @@ -770,9 +768,9 @@ void SetTopMost(bool state){ #elif defined(IS_WINDOWS) - SetWindowPos (mData.HWnd, state ? - HWND_TOPMOST : HWND_NOTOPMOST, 0, - 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); + SetWindowPos(mData.HWnd, + state ? HWND_TOPMOST : HWND_NOTOPMOST, + 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); #endif }