Skip to content

Commit c650f00

Browse files
author
Simon Hofmann
committed
(#17) Added checks for valid windows
1 parent 204ffdf commit c650f00

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/linux/window_manager.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ std::vector<WindowHandle> getWindows() {
3939
std::string getWindowTitle(const WindowHandle windowHandle) {
4040
Display* xServer = XGetMainDisplay();
4141
std::string windowName = "";
42-
if (xServer != NULL) {
42+
if (xServer != NULL && windowHandle >= 0) {
4343
/*
4444
* While there's also `XFetchName` to retrieve a window's `WM_NAME` property, in my tests `XFetchName` always failed and return 0
4545
* `XGetWMName` on the other hand just worked as expected.
@@ -57,7 +57,7 @@ std::string getWindowTitle(const WindowHandle windowHandle) {
5757
MMRect getWindowRect(const WindowHandle windowHandle) {
5858
Display* xServer = XGetMainDisplay();
5959
MMRect windowRect = MMRectMake(0, 0, 0, 0);
60-
if (xServer != NULL) {
60+
if (xServer != NULL && windowHandle >= 0) {
6161
Window rootWindow;
6262
int32_t x, y;
6363
uint32_t width, height, border_width, border_height;

src/macos/window_manager.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ WindowHandle getActiveWindow() {
7575

7676
MMRect getWindowRect(const WindowHandle windowHandle) {
7777
auto windowInfo = getWindowInfo(windowHandle);
78-
if (windowInfo != nullptr) {
78+
if (windowInfo != nullptr && windowHandle >= 0) {
7979
CGRect windowRect;
8080
if (CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)windowInfo[(id)kCGWindowBounds], &windowRect)) {
8181
return MMRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.width, windowRect.size.height);
@@ -86,7 +86,7 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
8686

8787
std::string getWindowTitle(const WindowHandle windowHandle) {
8888
auto windowInfo = getWindowInfo(windowHandle);
89-
if (windowInfo != nullptr) {
89+
if (windowInfo != nullptr && windowHandle >= 0) {
9090
NSString *windowName = windowInfo[(id)kCGWindowName];
9191
return [windowName UTF8String];
9292
}

src/win32/window_manager.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ std::vector<WindowHandle> getWindows() {
2222
}
2323

2424
WindowHandle getActiveWindow() {
25-
HWND foregroundWindow = GetForegroundWindow();
26-
if (foregroundWindow != NULL) {
25+
if (IsWindow(GetForegroundWindow())) {
2726
return reinterpret_cast<WindowHandle>(foregroundWindow);
2827
}
2928
return -1;
@@ -32,19 +31,21 @@ WindowHandle getActiveWindow() {
3231
MMRect getWindowRect(const WindowHandle windowHandle) {
3332
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
3433
RECT windowRect;
35-
if (GetWindowRect(hWnd, &windowRect)) {
34+
if (IsWindow(hWnd) && GetWindowRect(hWnd, &windowRect)) {
3635
return MMRectMake(windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top);
3736
}
3837
return MMRectMake(0, 0, 0, 0);
3938
}
4039

4140
std::string getWindowTitle(const WindowHandle windowHandle) {
4241
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
43-
auto BUFFER_SIZE = GetWindowTextLength(hWnd) + 1;
44-
if (BUFFER_SIZE) {
45-
LPSTR windowTitle = new CHAR[BUFFER_SIZE];
46-
if (GetWindowText(hWnd, windowTitle, BUFFER_SIZE)) {
47-
return std::string(windowTitle);
42+
if (IsWindow(hWnd)) {
43+
auto BUFFER_SIZE = GetWindowTextLength(hWnd) + 1;
44+
if (BUFFER_SIZE) {
45+
LPSTR windowTitle = new CHAR[BUFFER_SIZE];
46+
if (GetWindowText(hWnd, windowTitle, BUFFER_SIZE)) {
47+
return std::string(windowTitle);
48+
}
4849
}
4950
}
5051
return "";

0 commit comments

Comments
 (0)