-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
99 lines (82 loc) · 2.94 KB
/
Window.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Window.hpp"
#include <climits>
#include <cstring>
constexpr char WM_DELETE_WINDOW_NAME[] = "WM_DELETE_WINDOW";
constexpr char WM_PROTOCOLS_NAME[] = "WM_PROTOCOLS";
Window::Window(std::string_view window_name)
{
int screen_id;
_connection.reset(xcb_connect(nullptr, &screen_id));
const auto wm_delete_window_intern_cookie = xcb_intern_atom(_connection.get(), false, strlen(WM_DELETE_WINDOW_NAME), WM_DELETE_WINDOW_NAME);
const auto wm_protocols_intern_cookie = xcb_intern_atom(_connection.get(), false, strlen(WM_PROTOCOLS_NAME), WM_PROTOCOLS_NAME);
const auto setup = xcb_get_setup(_connection.get());
auto screen = xcb_setup_roots_iterator(setup);
for (int i = 0; i < screen_id; ++i)
{
xcb_screen_next(&screen);
}
_window = xcb_generate_id(_connection.get());
xcb_create_window(
_connection.get(),
screen.data->root_depth,
_window,
screen.data->root,
0, 0, 1600, 900, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen.data->root_visual,
0, nullptr
);
xcb_change_property(_connection.get(),
XCB_PROP_MODE_REPLACE, _window, XCB_ATOM_WM_NAME,
XCB_ATOM_STRING, CHAR_BIT,
window_name.size(), window_name.data());
std::unique_ptr<xcb_intern_atom_reply_t, FreeDeleter> wm_delete_window_intern_reply{
xcb_intern_atom_reply(_connection.get(), wm_delete_window_intern_cookie, nullptr)
};
_wm_delete_window_atom = wm_delete_window_intern_reply->atom;
std::unique_ptr<xcb_intern_atom_reply_t, FreeDeleter> wm_protocols_intern_reply{
xcb_intern_atom_reply(_connection.get(), wm_protocols_intern_cookie, nullptr)
};
_wm_protocols_atom = wm_protocols_intern_reply->atom;
xcb_change_property(_connection.get(),
XCB_PROP_MODE_REPLACE, _window, _wm_protocols_atom,
XCB_ATOM_ATOM, sizeof(xcb_atom_t) * CHAR_BIT,
1, &_wm_delete_window_atom);
xcb_map_window(_connection.get(), _window);
}
xcb_connection_t *Window::connection() const noexcept
{
return _connection.get();
}
std::unique_ptr<Event> Window::poll_event()
{
std::unique_ptr<xcb_generic_event_t, FreeDeleter> event{ xcb_wait_for_event(_connection.get()) };
switch (event->response_type & ~0x80)
{
case XCB_CLIENT_MESSAGE: {
const auto clientMessage = reinterpret_cast<const xcb_client_message_event_t*>(event.get());
if (_window == clientMessage->window
&& _wm_protocols_atom == clientMessage->type
&& _wm_delete_window_atom == clientMessage->data.data32[0])
{
return std::unique_ptr<Event>(new QuitEvent);
}
break;
}
default:
break;
}
return nullptr;
}
xcb_window_t Window::window() const noexcept
{
return _window;
}
void Window::ConnectionDeleter::operator()(xcb_connection_t *c) const noexcept
{
xcb_disconnect(c);
}
void Window::FreeDeleter::operator()(void *ptr) const noexcept
{
free(ptr);
}