Skip to content

Commit 7240b4c

Browse files
committed
:(#22) Working retrival of window name in Linux
1 parent b193e7a commit 7240b4c

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/linux/window_manager.cc

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <X11/Xlib.h>
2+
#include <X11/Xutil.h>
23
#include "../window_manager.h"
34

45
Display* connectToX() {
@@ -27,32 +28,30 @@ std::vector<WindowHandle> getWindows() {
2728

2829
std::string getWindowTitle(const WindowHandle windowHandle) {
2930
Display* xServer = connectToX();
31+
std::string windowName = "";
3032
if (xServer != nullptr) {
31-
char* windowName = NULL;
32-
if (XFetchName(xServer, windowHandle, &windowName)) {
33-
std::string wndName = std::string(windowName);
34-
XFree(windowName);
35-
disconnectFromX(xServer);
36-
return wndName;
33+
XTextProperty windowTextProperty;
34+
Status getWMNameResult = XGetWMName(xServer, windowHandle, &windowTextProperty);
35+
if (getWMNameResult > 0) {
36+
windowName = std::string(reinterpret_cast<const char*>(windowTextProperty.value));
3737
}
3838
disconnectFromX(xServer);
39-
return "";
4039
}
41-
return "";
40+
return windowName;
4241
}
4342

4443
MMRect getWindowRect(const WindowHandle windowHandle) {
4544
Display* xServer = connectToX();
45+
MMRect windowRect = MMRectMake(0, 0, 0, 0);
4646
if (xServer != nullptr) {
4747
Window rootWindow;
4848
int32_t x, y;
4949
uint32_t width, height, border_width, border_height;
50-
if (XGetGeometry(xServer, windowHandle, &rootWindow, &x, &y, &width, &height, &border_width, &border_height)) {
51-
disconnectFromX(xServer);
52-
return MMRectMake(x, y, width, height);
50+
Status getXGeometryResult = XGetGeometry(xServer, windowHandle, &rootWindow, &x, &y, &width, &height, &border_width, &border_height);
51+
if (getXGeometryResult > 0) {
52+
windowRect = MMRectMake(x - border_width, y - border_height, width + border_width, height + border_height);
5353
}
5454
disconnectFromX(xServer);
55-
return MMRectMake(0, 0, 0, 0);
5655
}
57-
return MMRectMake(0, 0, 0, 0);
56+
return windowRect;
5857
}

0 commit comments

Comments
 (0)