-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
244 lines (185 loc) · 6.39 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <array>
#include <cstdlib>
#include <csignal>
#include <csetjmp>
#include <fluke.hpp>
std::jmp_buf exit_jump; // This is used to jump back to main after a signal is handled.
int status = EXIT_SUCCESS; // Exit status for signal handler.
// Keyboard interrupt.
inline void sigint(int) {
FLUKE_DEBUG_WARN("SIGINT")
std::longjmp(exit_jump, EXIT_FAILURE);
}
// Terminate.
inline void sigterm(int) {
FLUKE_DEBUG_WARN("SIGTERM")
std::longjmp(exit_jump, EXIT_FAILURE);
}
// Kill.
[[noreturn]] inline void sigkill(int) {
FLUKE_DEBUG_ERROR("SIGKILL")
std::exit(EXIT_FAILURE);
}
int main() {
// Setup signals handlers.
FLUKE_DEBUG_SUCCESS("setting up signal handlers.")
std::signal(SIGINT, sigint);
std::signal(SIGTERM, sigterm);
std::signal(SIGKILL, sigkill);
// Connect to X.
FLUKE_DEBUG_SUCCESS("connecting to X server.")
fluke::Connection conn;
// Setup the randr extension to allow us to recieve display change events.
FLUKE_DEBUG_SUCCESS("setting up randr extension.")
const auto randr_ext = xcb_get_extension_data(conn, &xcb_randr_id);
const auto randr_base = randr_ext->first_event;
fluke::randr_select_input(conn, conn.root(), fluke::XCB_RANDR_EVENTS);
// Register to receive window manager events. Only one window manager can be active at one time.
FLUKE_DEBUG_SUCCESS("registering as a window manager.")
fluke::change_window_attributes(conn, conn.root(), XCB_CW_EVENT_MASK, fluke::XCB_WINDOWMANAGER_EVENTS);
// Gain control of windows which were already open before the window manager was started
// so that we can receive events for them.
FLUKE_DEBUG_SUCCESS("adopting orphaned windows.")
// For every mapped window, tell it what events we wish to receive from it
// and also set the border colour and width of the window.
for (const xcb_window_t win: fluke::get_mapped_windows(conn)) {
fluke::change_window_attributes(conn, win, XCB_CW_EVENT_MASK, fluke::XCB_WINDOW_EVENTS);
fluke::configure_window(conn, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, fluke::config::BORDER_SIZE);
fluke::change_window_attributes(conn, win, XCB_CW_BORDER_PIXEL, fluke::config::BORDER_COLOUR_INACTIVE);
}
// Get the window which currently has keyboard focus
// (if no window is focused an error will be generated but we just ignore it)
const xcb_window_t focused = fluke::get_focused_window(conn);
if (fluke::is_valid_window(conn, focused)) {
// Set the stacking mode, border width and border colour for the focused window.
fluke::configure_window(
conn, focused,
XCB_CONFIG_WINDOW_BORDER_WIDTH | XCB_CONFIG_WINDOW_STACK_MODE,
fluke::config::BORDER_SIZE, XCB_STACK_MODE_ABOVE
);
fluke::set_input_focus(conn, XCB_NONE, XCB_NONE);
fluke::set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, focused);
}
// Register keybindings defined in the `keys` structure of the config.
if constexpr(fluke::config::keybindings.size() > 0) {
FLUKE_DEBUG_SUCCESS("registering keybindings.")
fluke::register_keybindings(conn, fluke::config::keybindings);
}
// Set jump point, when a signal handler gets activated, it will jump here.
if (status = setjmp(exit_jump); status) {
FLUKE_DEBUG_SUCCESS("signal caught.")
goto exit;
}
FLUKE_DEBUG_SUCCESS("starting main event loop.")
fluke::on_launch(conn);
while (true) {
conn.flush();
// Get the next event and its type.
auto event = fluke::get_next_event(conn);
auto ev_type = fluke::get_event_type(event);
auto randr_ev_type = randr_base - ev_type;
// Check for errors.
if (xcb_connection_has_error(conn) != 0) {
tinge::errorln("cannot connect to the X server!");
break;
}
if (not event) {
tinge::errorln("event returned nullptr, this shouldnt happen!");
continue;
}
// Handle all events.
switch (ev_type) {
case XCB_MOTION_NOTIFY:
fluke::event_motion_notify(conn,
fluke::event_cast<fluke::MotionNotifyEvent>(std::move(event))
);
continue;
case XCB_CONFIGURE_REQUEST:
fluke::event_configure_request(conn,
fluke::event_cast<fluke::ConfigureRequestEvent>(std::move(event))
);
continue;
case XCB_KEY_PRESS:
fluke::event_keypress(conn,
fluke::event_cast<fluke::KeyPressEvent>(std::move(event))
);
continue;
case 0:
fluke::event_error(conn,
fluke::event_cast<fluke::Error>(std::move(event))
);
continue;
case XCB_ENTER_NOTIFY:
fluke::event_enter_notify(conn,
fluke::event_cast<fluke::EnterNotifyEvent>(std::move(event))
);
continue;
case XCB_LEAVE_NOTIFY:
fluke::event_leave_notify(conn,
fluke::event_cast<fluke::LeaveNotifyEvent>(std::move(event))
);
continue;
case XCB_FOCUS_IN:
fluke::event_focus_in(conn,
fluke::event_cast<fluke::FocusInEvent>(std::move(event))
);
continue;
case XCB_FOCUS_OUT:
fluke::event_focus_out(conn,
fluke::event_cast<fluke::FocusOutEvent>(std::move(event))
);
continue;
case XCB_CREATE_NOTIFY:
fluke::event_create_notify(conn,
fluke::event_cast<fluke::CreateNotifyEvent>(std::move(event))
);
continue;
case XCB_DESTROY_NOTIFY:
fluke::event_destroy_notify(conn,
fluke::event_cast<fluke::DestroyNotifyEvent>(std::move(event))
);
continue;
case XCB_MAP_REQUEST:
fluke::event_map_request(conn,
fluke::event_cast<fluke::MapRequestEvent>(std::move(event))
);
continue;
case XCB_UNMAP_NOTIFY:
fluke::event_unmap_notify(conn,
fluke::event_cast<fluke::UnmapNotifyEvent>(std::move(event))
);
continue;
case XCB_PROPERTY_NOTIFY:
fluke::event_property_notify(conn,
fluke::event_cast<fluke::PropertyNotifyEvent>(std::move(event))
);
continue;
case XCB_CLIENT_MESSAGE:
fluke::event_client_message(conn,
fluke::event_cast<fluke::ClientMessageEvent>(std::move(event))
);
continue;
}
// Handle randr events, these checks are exhaustive so we
// do not need to check for unhandled randr events.
switch (randr_ev_type) {
case XCB_RANDR_SCREEN_CHANGE_NOTIFY:
fluke::event_randr_screen_change_notify(conn,
fluke::event_cast<fluke::RandrScreenChangeNotifyEvent>(std::move(event))
);
continue;
case XCB_RANDR_NOTIFY:
fluke::event_randr_notify(conn,
fluke::event_cast<fluke::RandrNotifyEvent>(std::move(event))
);
continue;
}
// Warn about unhandled events.
FLUKE_DEBUG_WARN("unhandled event '", fluke::event_str[ev_type], "'!")
}
exit:
FLUKE_DEBUG_SUCCESS("exiting.")
fluke::on_exit(conn);
return status;
}