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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
#include "GEventLoop.h"
#include "GEvent.h"
#include "GWindow.h"
#include <LibC/errno.h>
#include <LibC/fcntl.h>
#include <LibC/stdio.h>
#include <LibC/stdlib.h>
#include <LibC/string.h>
#include <LibC/sys/select.h>
#include <LibC/sys/socket.h>
#include <LibC/sys/time.h>
#include <LibC/time.h>
#include <LibC/unistd.h>
#include <LibCore/CNotifier.h>
#include <LibCore/CObject.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GDesktop.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GWidget.h>
#include <sys/uio.h>
//#define GEVENTLOOP_DEBUG
//#define COALESCING_DEBUG
int GEventLoop::s_windowserver_fd = -1;
int GEventLoop::s_my_client_id = -1;
pid_t GEventLoop::s_server_pid = -1;
void GEventLoop::connect_to_server()
{
ASSERT(s_windowserver_fd == -1);
s_windowserver_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (s_windowserver_fd < 0) {
perror("socket");
ASSERT_NOT_REACHED();
}
sockaddr_un address;
address.sun_family = AF_LOCAL;
strcpy(address.sun_path, "/tmp/wsportal");
int retries = 1000;
int rc = 0;
while (retries) {
rc = connect(s_windowserver_fd, (const sockaddr*)&address, sizeof(address));
if (rc == 0)
break;
#ifdef GEVENTLOOP_DEBUG
dbgprintf("connect failed: %d, %s\n", errno, strerror(errno));
#endif
sleep(1);
--retries;
}
if (rc < 0) {
ASSERT_NOT_REACHED();
}
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::Greeting;
request.greeting.client_pid = getpid();
auto response = sync_request(request, WSAPI_ServerMessage::Type::Greeting);
handle_greeting(response);
}
GEventLoop::GEventLoop()
{
static bool connected = false;
if (!connected) {
connect_to_server();
connected = true;
}
#ifdef GEVENTLOOP_DEBUG
dbgprintf("(%u) GEventLoop constructed :)\n", getpid());
#endif
}
GEventLoop::~GEventLoop()
{
}
void GEventLoop::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window, const ByteBuffer& extra_data)
{
#ifdef GEVENTLOOP_DEBUG
dbgprintf("WID=%x Paint [%d,%d %dx%d]\n", event.window_id, event.paint.rect.location.x, event.paint.rect.location.y, event.paint.rect.size.width, event.paint.rect.size.height);
#endif
Vector<Rect, 32> rects;
for (int i = 0; i < min(WSAPI_ServerMessage::max_inline_rect_count, event.rect_count); ++i)
rects.append(event.rects[i]);
if (event.extra_size) {
auto* extra_rects = reinterpret_cast<const WSAPI_Rect*>(extra_data.data());
for (int i = 0; i < event.rect_count - WSAPI_ServerMessage::max_inline_rect_count; ++i)
rects.append(extra_rects[i]);
}
post_event(window, make<GMultiPaintEvent>(rects, event.paint.window_size));
}
void GEventLoop::handle_resize_event(const WSAPI_ServerMessage& event, GWindow& window)
{
post_event(window, make<GResizeEvent>(event.window.old_rect.size, event.window.rect.size));
}
void GEventLoop::handle_window_activation_event(const WSAPI_ServerMessage& event, GWindow& window)
{
#ifdef GEVENTLOOP_DEBUG
dbgprintf("WID=%x WindowActivation\n", event.window_id);
#endif
post_event(window, make<GEvent>(event.type == WSAPI_ServerMessage::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
}
void GEventLoop::handle_window_close_request_event(const WSAPI_ServerMessage&, GWindow& window)
{
post_event(window, make<GEvent>(GEvent::WindowCloseRequest));
}
void GEventLoop::handle_window_entered_or_left_event(const WSAPI_ServerMessage& message, GWindow& window)
{
post_event(window, make<GEvent>(message.type == WSAPI_ServerMessage::Type::WindowEntered ? GEvent::WindowEntered : GEvent::WindowLeft));
}
void GEventLoop::handle_key_event(const WSAPI_ServerMessage& event, GWindow& window)
{
#ifdef GEVENTLOOP_DEBUG
dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
#endif
auto key_event = make<GKeyEvent>(event.type == WSAPI_ServerMessage::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key, event.key.modifiers);
if (event.key.character != '\0')
key_event->m_text = String(&event.key.character, 1);
if (event.type == WSAPI_ServerMessage::Type::KeyDown) {
if (auto* focused_widget = window.focused_widget()) {
if (auto* action = focused_widget->action_for_key_event(*key_event)) {
if (action->is_enabled()) {
action->activate();
return;
}
}
}
if (auto* action = GApplication::the().action_for_key_event(*key_event)) {
if (action->is_enabled()) {
action->activate();
return;
}
}
}
post_event(window, move(key_event));
}
void GEventLoop::handle_mouse_event(const WSAPI_ServerMessage& event, GWindow& window)
{
#ifdef GEVENTLOOP_DEBUG
dbgprintf("WID=%x MouseEvent %d,%d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y, event.mouse.wheel_delta);
#endif
GMouseEvent::Type type;
switch (event.type) {
case WSAPI_ServerMessage::Type::MouseMove:
type = GEvent::MouseMove;
break;
case WSAPI_ServerMessage::Type::MouseUp:
type = GEvent::MouseUp;
break;
case WSAPI_ServerMessage::Type::MouseDown:
type = GEvent::MouseDown;
break;
case WSAPI_ServerMessage::Type::MouseDoubleClick:
type = GEvent::MouseDoubleClick;
break;
case WSAPI_ServerMessage::Type::MouseWheel:
type = GEvent::MouseWheel;
break;
default:
ASSERT_NOT_REACHED();
break;
}
GMouseButton button { GMouseButton::None };
switch (event.mouse.button) {
case WSAPI_MouseButton::NoButton:
button = GMouseButton::None;
break;
case WSAPI_MouseButton::Left:
button = GMouseButton::Left;
break;
case WSAPI_MouseButton::Right:
button = GMouseButton::Right;
break;
case WSAPI_MouseButton::Middle:
button = GMouseButton::Middle;
break;
default:
ASSERT_NOT_REACHED();
break;
}
post_event(window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button, event.mouse.modifiers, event.mouse.wheel_delta));
}
void GEventLoop::handle_menu_event(const WSAPI_ServerMessage& event)
{
if (event.type == WSAPI_ServerMessage::Type::MenuItemActivated) {
auto* menu = GMenu::from_menu_id(event.menu.menu_id);
if (!menu) {
dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
return;
}
if (auto* action = menu->action_at(event.menu.identifier))
action->activate();
return;
}
ASSERT_NOT_REACHED();
}
void GEventLoop::handle_wm_event(const WSAPI_ServerMessage& event, GWindow& window)
{
#ifdef GEVENTLOOP_DEBUG
dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
#endif
if (event.type == WSAPI_ServerMessage::WM_WindowStateChanged)
return post_event(window, make<GWMWindowStateChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length), event.wm.rect, event.wm.is_active, (GWindowType)event.wm.window_type, event.wm.is_minimized));
if (event.type == WSAPI_ServerMessage::WM_WindowRectChanged)
return post_event(window, make<GWMWindowRectChangedEvent>(event.wm.client_id, event.wm.window_id, event.wm.rect));
if (event.type == WSAPI_ServerMessage::WM_WindowIconChanged)
return post_event(window, make<GWMWindowIconChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length)));
if (event.type == WSAPI_ServerMessage::WM_WindowRemoved)
return post_event(window, make<GWMWindowRemovedEvent>(event.wm.client_id, event.wm.window_id));
ASSERT_NOT_REACHED();
}
void GEventLoop::process_unprocessed_bundles()
{
int coalesced_paints = 0;
int coalesced_resizes = 0;
auto unprocessed_bundles = move(m_unprocessed_bundles);
HashMap<int, Size> latest_size_for_window_id;
for (auto& bundle : unprocessed_bundles) {
auto& event = bundle.message;
if (event.type == WSAPI_ServerMessage::Type::WindowResized) {
latest_size_for_window_id.set(event.window_id, event.window.rect.size);
}
}
int paint_count = 0;
HashMap<int, Size> latest_paint_size_for_window_id;
for (auto& bundle : unprocessed_bundles) {
auto& event = bundle.message;
if (event.type == WSAPI_ServerMessage::Type::Paint) {
++paint_count;
#ifdef COALESCING_DEBUG
dbgprintf(" %s (window: %s)\n", Rect(event.paint.rect).to_string().characters(), Size(event.paint.window_size).to_string().characters());
#endif
latest_paint_size_for_window_id.set(event.window_id, event.paint.window_size);
}
}
#ifdef COALESCING_DEBUG
dbgprintf("paint_count: %d\n", paint_count);
#endif
for (auto& bundle : unprocessed_bundles) {
auto& event = bundle.message;
if (event.type == WSAPI_ServerMessage::Type::Greeting) {
handle_greeting(event);
continue;
}
if (event.type == WSAPI_ServerMessage::Type::ScreenRectChanged) {
GDesktop::the().did_receive_screen_rect({}, event.screen.rect);
continue;
}
if (event.type == WSAPI_ServerMessage::Error) {
dbgprintf("GEventLoop got error message from server\n");
dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());
quit(1);
return;
}
switch (event.type) {
case WSAPI_ServerMessage::MenuItemActivated:
handle_menu_event(event);
continue;
default:
break;
}
auto* window = GWindow::from_window_id(event.window_id);
if (!window) {
dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
continue;
}
switch (event.type) {
case WSAPI_ServerMessage::Type::Paint:
if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id)) {
++coalesced_paints;
break;
}
handle_paint_event(event, *window, bundle.extra_data);
break;
case WSAPI_ServerMessage::Type::MouseDown:
case WSAPI_ServerMessage::Type::MouseDoubleClick:
case WSAPI_ServerMessage::Type::MouseUp:
case WSAPI_ServerMessage::Type::MouseMove:
case WSAPI_ServerMessage::Type::MouseWheel:
handle_mouse_event(event, *window);
break;
case WSAPI_ServerMessage::Type::WindowActivated:
case WSAPI_ServerMessage::Type::WindowDeactivated:
handle_window_activation_event(event, *window);
break;
case WSAPI_ServerMessage::Type::WindowCloseRequest:
handle_window_close_request_event(event, *window);
break;
case WSAPI_ServerMessage::Type::KeyDown:
case WSAPI_ServerMessage::Type::KeyUp:
handle_key_event(event, *window);
break;
case WSAPI_ServerMessage::Type::WindowEntered:
case WSAPI_ServerMessage::Type::WindowLeft:
handle_window_entered_or_left_event(event, *window);
break;
case WSAPI_ServerMessage::Type::WindowResized:
if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id)) {
++coalesced_resizes;
break;
}
handle_resize_event(event, *window);
break;
case WSAPI_ServerMessage::Type::WM_WindowRemoved:
case WSAPI_ServerMessage::Type::WM_WindowStateChanged:
case WSAPI_ServerMessage::Type::WM_WindowIconChanged:
handle_wm_event(event, *window);
break;
default:
break;
}
}
#ifdef COALESCING_DEBUG
if (coalesced_paints)
dbgprintf("Coalesced %d paints\n", coalesced_paints);
if (coalesced_resizes)
dbgprintf("Coalesced %d resizes\n", coalesced_resizes);
#endif
}
bool GEventLoop::drain_messages_from_server()
{
for (;;) {
WSAPI_ServerMessage message;
ssize_t nread = recv(s_windowserver_fd, &message, sizeof(WSAPI_ServerMessage), MSG_DONTWAIT);
if (nread < 0) {
if (errno == EAGAIN) {
return true;
}
perror("read");
quit(1);
return false;
}
if (nread == 0) {
fprintf(stderr, "EOF on WindowServer fd\n");
quit(1);
exit(-1);
return false;
}
ASSERT(nread == sizeof(message));
ByteBuffer extra_data;
if (message.extra_size) {
extra_data = ByteBuffer::create_uninitialized(message.extra_size);
int extra_nread = read(s_windowserver_fd, extra_data.data(), extra_data.size());
ASSERT(extra_nread == message.extra_size);
}
m_unprocessed_bundles.append({ move(message), move(extra_data) });
}
}
bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data)
{
if (!extra_data.is_empty())
const_cast<WSAPI_ClientMessage&>(message).extra_size = extra_data.size();
struct iovec iov[2];
int iov_count = 1;
iov[0].iov_base = (void*)&message;
iov[0].iov_len = sizeof(message);
if (!extra_data.is_empty()) {
iov[1].iov_base = (void*)extra_data.data();
iov[1].iov_len = extra_data.size();
++iov_count;
}
int nwritten = writev(s_windowserver_fd, iov, iov_count);
ASSERT(nwritten == sizeof(message) + extra_data.size());
return true;
}
bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_ServerMessage& event)
{
for (;;) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(s_windowserver_fd, &rfds);
int rc = select(s_windowserver_fd + 1, &rfds, nullptr, nullptr, nullptr);
if (rc < 0) {
perror("select");
}
ASSERT(rc > 0);
ASSERT(FD_ISSET(s_windowserver_fd, &rfds));
bool success = drain_messages_from_server();
if (!success)
return false;
for (ssize_t i = 0; i < m_unprocessed_bundles.size(); ++i) {
if (m_unprocessed_bundles[i].message.type == type) {
event = move(m_unprocessed_bundles[i].message);
m_unprocessed_bundles.remove(i);
return true;
}
}
}
}
WSAPI_ServerMessage GEventLoop::sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type)
{
bool success = post_message_to_server(request);
ASSERT(success);
WSAPI_ServerMessage response;
success = wait_for_specific_event(response_type, response);
ASSERT(success);
return response;
}
void GEventLoop::handle_greeting(WSAPI_ServerMessage& message)
{
s_server_pid = message.greeting.server_pid;
s_my_client_id = message.greeting.your_client_id;
GDesktop::the().did_receive_screen_rect({}, message.greeting.screen_rect);
}
|