-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathX11Display.cpp
More file actions
38 lines (30 loc) · 1.08 KB
/
Copy pathX11Display.cpp
File metadata and controls
38 lines (30 loc) · 1.08 KB
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
#include "X11Display.h"
#include <utility>
namespace robot::x11 {
std::expected<X11Connection, Error> X11Connection::open() {
Display* display = XOpenDisplay(nullptr);
if (display == nullptr) {
return std::unexpected(Error::backendUnavailable(
"cannot open an X display (no X server, or a Wayland session without "
"Xwayland); set DISPLAY or use the uinput backend"
));
}
return X11Connection{display};
}
X11Connection::X11Connection(Display* display)
: display_(display), root_(DefaultRootWindow(display)) {}
X11Connection::~X11Connection() {
if (display_ != nullptr) XCloseDisplay(display_);
}
X11Connection::X11Connection(X11Connection&& other) noexcept
: display_(std::exchange(other.display_, nullptr)),
root_(std::exchange(other.root_, 0)) {}
X11Connection& X11Connection::operator=(X11Connection&& other) noexcept {
if (this != &other) {
if (display_ != nullptr) XCloseDisplay(display_);
display_ = std::exchange(other.display_, nullptr);
root_ = std::exchange(other.root_, 0);
}
return *this;
}
} // namespace robot::x11