1
1
#include < X11/Xlib.h>
2
2
#include < X11/Xutil.h>
3
3
#include " ../window_manager.h"
4
-
5
- Display* connectToX () {
6
- Display* xDisplay = XOpenDisplay (NULL );
7
-
8
- if (xDisplay == NULL ) {
9
- return nullptr ;
10
- }
11
- return xDisplay;
12
- }
13
-
14
- void disconnectFromX (Display* connection) {
15
- XCloseDisplay (connection);
16
- }
4
+ #include " ../xdisplay.h"
17
5
18
6
WindowHandle getActiveWindow () {
19
- Display* xServer = connectToX ();
7
+ Display* xServer = XGetMainDisplay ();
20
8
Window window;
21
9
if (xServer != nullptr ) {
22
10
int32_t revertToWindow;
23
11
XGetInputFocus (xServer, &window, &revertToWindow);
24
- disconnectFromX (xServer );
12
+ XCloseMainDisplay ( );
25
13
return window;
26
14
}
27
15
return NULL ;
28
16
}
29
17
30
18
std::vector<WindowHandle> getWindows () {
31
- Display* xServer = connectToX ();
19
+ Display* xServer = XGetMainDisplay ();
32
20
std::vector<WindowHandle> windowHandles;
33
21
if (xServer != nullptr ) {
34
22
Window defaultRootWindow = DefaultRootWindow (xServer);
@@ -43,27 +31,27 @@ std::vector<WindowHandle> getWindows() {
43
31
windowHandles.push_back (windowList[idx]);
44
32
}
45
33
}
46
- disconnectFromX (xServer );
34
+ XCloseMainDisplay ( );
47
35
}
48
36
return windowHandles;
49
37
}
50
38
51
39
std::string getWindowTitle (const WindowHandle windowHandle) {
52
- Display* xServer = connectToX ();
40
+ Display* xServer = XGetMainDisplay ();
53
41
std::string windowName = " " ;
54
42
if (xServer != nullptr ) {
55
43
XTextProperty windowTextProperty;
56
44
Status getWMNameResult = XGetWMName (xServer, windowHandle, &windowTextProperty);
57
45
if (getWMNameResult > 0 ) {
58
46
windowName = std::string (reinterpret_cast <const char *>(windowTextProperty.value ));
59
47
}
60
- disconnectFromX (xServer );
48
+ XCloseMainDisplay ( );
61
49
}
62
50
return windowName;
63
51
}
64
52
65
53
MMRect getWindowRect (const WindowHandle windowHandle) {
66
- Display* xServer = connectToX ();
54
+ Display* xServer = XGetMainDisplay ();
67
55
MMRect windowRect = MMRectMake (0 , 0 , 0 , 0 );
68
56
if (xServer != nullptr ) {
69
57
Window rootWindow;
@@ -73,7 +61,7 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
73
61
if (getXGeometryResult > 0 ) {
74
62
windowRect = MMRectMake (x, y, width, height);
75
63
}
76
- disconnectFromX (xServer );
64
+ XCloseMainDisplay ( );
77
65
}
78
66
return windowRect;
79
67
}
0 commit comments