11#include < X11/Xlib.h>
22#include < X11/Xutil.h>
33#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"
175
186WindowHandle getActiveWindow () {
19- Display* xServer = connectToX ();
7+ Display* xServer = XGetMainDisplay ();
208 Window window;
219 if (xServer != nullptr ) {
2210 int32_t revertToWindow;
2311 XGetInputFocus (xServer, &window, &revertToWindow);
24- disconnectFromX (xServer );
12+ XCloseMainDisplay ( );
2513 return window;
2614 }
2715 return NULL ;
2816}
2917
3018std::vector<WindowHandle> getWindows () {
31- Display* xServer = connectToX ();
19+ Display* xServer = XGetMainDisplay ();
3220 std::vector<WindowHandle> windowHandles;
3321 if (xServer != nullptr ) {
3422 Window defaultRootWindow = DefaultRootWindow (xServer);
@@ -43,27 +31,27 @@ std::vector<WindowHandle> getWindows() {
4331 windowHandles.push_back (windowList[idx]);
4432 }
4533 }
46- disconnectFromX (xServer );
34+ XCloseMainDisplay ( );
4735 }
4836 return windowHandles;
4937}
5038
5139std::string getWindowTitle (const WindowHandle windowHandle) {
52- Display* xServer = connectToX ();
40+ Display* xServer = XGetMainDisplay ();
5341 std::string windowName = " " ;
5442 if (xServer != nullptr ) {
5543 XTextProperty windowTextProperty;
5644 Status getWMNameResult = XGetWMName (xServer, windowHandle, &windowTextProperty);
5745 if (getWMNameResult > 0 ) {
5846 windowName = std::string (reinterpret_cast <const char *>(windowTextProperty.value ));
5947 }
60- disconnectFromX (xServer );
48+ XCloseMainDisplay ( );
6149 }
6250 return windowName;
6351}
6452
6553MMRect getWindowRect (const WindowHandle windowHandle) {
66- Display* xServer = connectToX ();
54+ Display* xServer = XGetMainDisplay ();
6755 MMRect windowRect = MMRectMake (0 , 0 , 0 , 0 );
6856 if (xServer != nullptr ) {
6957 Window rootWindow;
@@ -73,7 +61,7 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
7361 if (getXGeometryResult > 0 ) {
7462 windowRect = MMRectMake (x, y, width, height);
7563 }
76- disconnectFromX (xServer );
64+ XCloseMainDisplay ( );
7765 }
7866 return windowRect;
7967}
0 commit comments