-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleWindowX11.cpp
59 lines (51 loc) · 1.56 KB
/
simpleWindowX11.cpp
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
// STL
#include <optional>
#include <cstdlib>
#include <iostream>
// X11
#include <X11/Xlib.h>
static auto parseProgramOptions(int argc, char** argv)
-> std::optional<std::pair<uint32_t, uint32_t>> {
if(argc != 3) {
return std::nullopt;
}
return std::make_pair(
std::stoi(argv[1]),
std::stoi(argv[2])
);
}
auto main(int argc, char *argv[]) -> int {
auto pDisplay = XOpenDisplay(nullptr);
if(pDisplay == nullptr) {
fprintf(stderr, "Cannot open display\n");
return EXIT_FAILURE;
}
auto width = 640U;
auto height = 480U;
if(const auto args = parseProgramOptions(argc, argv);
args) {
constexpr auto primaryDisplay = 0;
auto screen = ScreenOfDisplay(pDisplay, primaryDisplay);
width = std::min(args.value().first, static_cast<uint32_t>(screen->width));
height = std::min(args.value().second, static_cast<uint32_t>(screen->height));
}
auto screen = DefaultScreen(pDisplay);
auto window = XCreateSimpleWindow(pDisplay,
RootWindow(pDisplay, screen),
0, 0, width, height, 1,
WhitePixel(pDisplay, screen),
BlackPixel(pDisplay, screen));
XSelectInput(pDisplay, window, ExposureMask | KeyPressMask);
XMapWindow(pDisplay, window);
std::cout << "Press any key to close the window!\n";
auto bRunning = true;
XEvent event;
while(bRunning) {
XNextEvent(pDisplay, &event);
if (event.type == KeyPress) {
bRunning = false;
}
}
XCloseDisplay(pDisplay);
return EXIT_SUCCESS;
}