1
+ #include < X11/Xlib.h>
2
+ #include " ../window_manager.h"
3
+
4
+ Display* connectToX () {
5
+ Display* xDisplay = XOpenDisplay (NULL );
6
+
7
+ if (xDisplay == NULL ) {
8
+ return nullptr ;
9
+ }
10
+ return xDisplay;
11
+ }
12
+
13
+ int32_t disconnectFromX (Display* connection) {
14
+ return XCloseDisplay (connection);
15
+ }
16
+
17
+ std::vector<WindowHandle> getWindows () {
18
+ Display* xServer = connectToX ();
19
+ Window window;
20
+ int32_t revertToWindow;
21
+ if (XGetInputFocus (xServer, &window, &revertToWindow)) {
22
+ disconnectFromX (xServer);
23
+ return std::vector<WindowHandle>{static_cast <WindowHandle>(window)};
24
+ }
25
+ return std::vector<WindowHandle>();
26
+ }
27
+
28
+ std::string getWindowTitle (const WindowHandle windowHandle) {
29
+ Display* xServer = connectToX ();
30
+ 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;
37
+ }
38
+ disconnectFromX (xServer);
39
+ return " " ;
40
+ }
41
+ return " " ;
42
+ }
43
+
44
+ MMRect getWindowRect (const WindowHandle windowHandle) {
45
+ Display* xServer = connectToX ();
46
+ 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);
53
+ }
54
+ disconnectFromX (xServer);
55
+ return MMRectMake (0 , 0 , 0 , 0 );
56
+ }
57
+ return MMRectMake (0 , 0 , 0 , 0 );
58
+ }
0 commit comments