forked from nut-tree/libnut-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_manager.cc
105 lines (93 loc) · 3.36 KB
/
window_manager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "../window_manager.h"
extern "C" {
#include "../xdisplay.h"
}
WindowHandle getActiveWindow() {
Display* xServer = XGetMainDisplay();
Window window;
if (xServer != NULL) {
int32_t revertToWindow;
XGetInputFocus(xServer, &window, &revertToWindow);
return window;
}
return -1;
}
std::vector<WindowHandle> getWindows() {
Display* xServer = XGetMainDisplay();
std::vector<WindowHandle> windowHandles;
if (xServer != NULL) {
Window defaultRootWindow = DefaultRootWindow(xServer);
Window rootWindow;
Window parentWindow;
Window* windowList;
uint32_t windowCount;
Status queryTreeResult = XQueryTree(xServer, defaultRootWindow, &rootWindow, &parentWindow, &windowList, &windowCount);
if (queryTreeResult > 0) {
for (size_t idx = 0; idx < windowCount; ++idx) {
windowHandles.push_back(windowList[idx]);
}
}
}
return windowHandles;
}
std::string getWindowTitle(const WindowHandle windowHandle) {
Display* xServer = XGetMainDisplay();
std::string windowName = "";
if (xServer != NULL && windowHandle >= 0) {
/*
* While there's also `XFetchName` to retrieve a window's `WM_NAME` property, in my tests `XFetchName` always failed and return 0
* `XGetWMName` on the other hand just worked as expected.
* Keep that in mind in case you're considering changing this implementation
*/
XTextProperty windowTextProperty;
Status getWMNameResult = XGetWMName(xServer, windowHandle, &windowTextProperty);
if (getWMNameResult > 0) {
windowName = std::string(reinterpret_cast<const char*>(windowTextProperty.value));
}
}
return windowName;
}
MMRect getWindowRect(const WindowHandle windowHandle) {
Display* xServer = XGetMainDisplay();
MMRect windowRect = MMRectMake(0, 0, 0, 0);
if (xServer != NULL && windowHandle >= 0) {
Window rootWindow;
int32_t x, y;
uint32_t width, height, border_width, border_height;
Status getXGeometryResult = XGetGeometry(xServer, windowHandle, &rootWindow, &x, &y, &width, &height, &border_width, &border_height);
if (getXGeometryResult > 0) {
windowRect = MMRectMake(x, y, width, height);
}
}
return windowRect;
}
bool focusWindow(const WindowHandle windowHandle) {
Display* display = XGetMainDisplay();
if (display != NULL && windowHandle >= 0) {
// Try to set the window to the foreground
XSetInputFocus(display, windowHandle, RevertToParent, CurrentTime);
XRaiseWindow(display, windowHandle);
XFlush(display);
return true;
}
return false;
}
bool resizeWindow(const WindowHandle windowHandle, const MMRect& rect) {
Display* display = XGetMainDisplay();
if (display != NULL && windowHandle >= 0) {
XWindowChanges changes;
//size
changes.width = rect.size.width;
changes.height = rect.size.height;
//origin
changes.x = rect.origin.x;
changes.y = rect.origin.y;
// Resize and move the window
XConfigureWindow(display, windowHandle, CWX | CWY | CWWidth | CWHeight, &changes);
XFlush(display);
return true;
}
return false;
}