Skip to content

Commit 2cc06ac

Browse files
committed
(#22) Working retrival of window name in Linux
1 parent b193e7a commit 2cc06ac

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Diff for: src/linux/window_manager.cc

+17-16
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,32 @@ 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+
int32_t 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) {
47-
Window rootWindow;
48-
int32_t x, y;
49-
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);
47+
XWindowAttributes windowAttributes;
48+
int32_t getXGeometryResult = XGetWindowAttributes(xServer, windowHandle, &windowAttributes);
49+
printf("Border width: %d", windowAttributes.border_width);
50+
if (getXGeometryResult > 0) {
51+
windowRect = MMRectMake(windowAttributes.x - windowAttributes.border_width, \
52+
windowAttributes.y - windowAttributes.border_width, \
53+
windowAttributes.width + windowAttributes.border_width, \
54+
windowAttributes.height + windowAttributes.border_width);
5355
}
5456
disconnectFromX(xServer);
55-
return MMRectMake(0, 0, 0, 0);
5657
}
57-
return MMRectMake(0, 0, 0, 0);
58+
return windowRect;
5859
}

0 commit comments

Comments
 (0)