-
Notifications
You must be signed in to change notification settings - Fork 4
/
EventController.cpp
443 lines (381 loc) · 12.9 KB
/
EventController.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
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
440
441
442
//
// Created by Cry on 2018-12-20.
//
#include "EventController.h"
void EventController::init() {
queue = new EventQueue();
queue->init();
mutex = SDL_CreateMutex();
if (!mutex) {
printf("EventQueue SDL_CreateMutex error\n");
// return SDL_FALSE;
}
cond = SDL_CreateCond();
if (!cond) {
printf("EventQueue SDL_CreateCond error\n");
// return SDL_FALSE;
}
}
int event_run(void *data) {
EventController *controller = static_cast<EventController *>(data);
controller->stop = SDL_FALSE;
controller->event_handle();
return 0;
}
void EventController::async_start() {
event_tid = SDL_CreateThread(event_run, "event_controller", this);
}
void EventController::destroy() {
stop_loop();
queue->destroy();
// SDL_CondSignal(cond);
SDL_DestroyCond(cond);
SDL_DestroyMutex(mutex);
}
bool mouseDown = false;
void EventController::handleButtonEvent(SDL_Screen *screen, SDL_MouseButtonEvent *event) {
int width = screen->screen_w;
int height = screen->screen_h;
int x = event->x;
int y = event->y;
//是否超过来边界
bool outside_device_screen = x < 0 || x >= width ||
y < 0 || y >= height;
printf("outside_device_screen =%d\n", outside_device_screen);
if (outside_device_screen) {
// ignore
return;
}
char buf[6];
memset(buf, 0, sizeof(buf));
printf("event x =%d\n", event->x);
printf("event y =%d\n", event->y);
printf("event char size =%zu\n", sizeof(char));
buf[0] = 0;
if (event->type == SDL_MOUSEBUTTONDOWN) {
//发送down 事件
buf[1] = EVENT_DOWN;
mouseDown = true;
} else {
// 发送UP事件
buf[1] = EVENT_UP;
mouseDown = false;
}
//高8位
buf[2] = event->x >> 8;
//低8位
buf[3] = event->x & 0xff;
//高8位
buf[4] = event->y >> 8;
//低8位
buf[5] = event->y & 0xff;
int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 6);;
printf("send result = %d\n", result);
}
// Convert window coordinates (as provided by SDL_GetMouseState() to renderer coordinates (as provided in SDL mouse events)
//
// See my question:
// <https://stackoverflow.com/questions/49111054/how-to-get-mouse-position-on-mouse-wheel-event>
void EventController::handleScrollEvent(SDL_Screen *sc, SDL_MouseWheelEvent *event) {
//处理滑动事件
int x_c;
int y_c;
int *x = &x_c;
int *y = &y_c;
SDL_GetMouseState(x, y);
SDL_Rect viewport;
float scale_x, scale_y;
SDL_RenderGetViewport(sc->sdl_renderer, &viewport);
SDL_RenderGetScale(sc->sdl_renderer, &scale_x, &scale_y);
*x = (int) (*x / scale_x) - viewport.x;
*y = (int) (*y / scale_y) - viewport.y;
int width = sc->screen_w;
int height = sc->screen_h;
//是否超过来边界
bool outside_device_screen = x_c < 0 || x_c >= width ||
y_c < 0 || y_c >= height;
printf("outside_device_screen =%d\n", outside_device_screen);
if (outside_device_screen) {
// ignore
return;
}
SDL_assert_release(x_c >= 0 && x_c < 0x10000 && y_c >= 0 && y_c < 0x10000);
//使用这个来记录滑动的方向
// SDL behavior seems inconsistent between horizontal and vertical scrolling
// so reverse the horizontal
// <https://wiki.libsdl.org/SDL_MouseWheelEvent#Remarks>
// SDL 的滑动情况,两个方向不一致
int mul = event->direction == SDL_MOUSEWHEEL_NORMAL ? 1 : -1;
int hs = -mul * event->x;
int vs = mul * event->y;
char buf[14];
memset(buf, 0, sizeof(buf));
printf(" x_c =%d\n", x_c);
printf(" y_c =%d\n", y_c);
printf(" hs =%d\n", hs);
printf(" vs =%d\n", vs);
buf[0] = 0;
//滚动事件
buf[1] = 2;
//高8位
buf[2] = x_c >> 8;
//低8位
buf[3] = x_c & 0xff;
//高8位
buf[4] = y_c >> 8;
//低8位
buf[5] = y_c & 0xff;
//继续滚动距离
buf[6] = hs >> 24;
//低8位
buf[7] = hs >> 16;
buf[8] = hs >> 8;
buf[9] = hs;
//高8位
buf[10] = vs >> 24;
//低8位
buf[11] = vs >> 16;
buf[12] = vs >> 8;
buf[13] = vs;
int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 14);
printf("send result = %d\n", result);
}
void EventController::handleSDLKeyEvent(SDL_Screen *sc, SDL_KeyboardEvent *event) {
//分别对应 mac 上的 control option command
int ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);
int alt = event->keysym.mod & (KMOD_LALT | KMOD_RALT);
int meta = event->keysym.mod & (KMOD_LGUI | KMOD_RGUI);
printf("ctrl = %d,", ctrl);
printf("meta = %d,", meta);
printf("alt = %d,\n", alt);
//期望control+ H = home键 control+b = back键
//再去取keycode
SDL_Keycode keycode = event->keysym.sym;
printf("keycode = %d, action type = %d\n", keycode, event->type);
printf("b = %d, action type = %d\n", SDLK_b, event->type);
if (event->type == SDL_KEYDOWN && ctrl != 0) {
//这个时候发送的是按下的状态
if (keycode == SDLK_h) {
char buf[4];
memset(buf, 0, sizeof(buf));
buf[0] = 0;
//自定义的案件事件
buf[1] = 3;
//1 是 down
buf[2] = 1;
//key code home 键对应的是 3
buf[3] = 3;
int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 4);
printf("send result = %d\n", result);
} else if (keycode == SDLK_b) {
char buf[4];
memset(buf, 0, sizeof(buf));
buf[0] = 0;
//自定义的案件事件
buf[1] = 3;
//1 是 down
buf[2] = 1;
//key code back 键对应的是 4
buf[3] = 4;
int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 4);
printf("send result = %d\n", result);
}
}
if (event->type == SDL_KEYUP && keycode != 0) {
if (keycode == SDLK_h) {
char buf[4];
memset(buf, 0, sizeof(buf));
buf[0] = 0;
//自定义的案件事件
buf[1] = 3;
//1 是 up
buf[2] = 0;
//key code home 键对应的是 3
buf[3] = 3;
int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 4);
printf("send result = %d\n", result);
} else if (keycode == SDLK_b) {
char buf[4];
memset(buf, 0, sizeof(buf));
buf[0] = 0;
//自定义的案件事件
buf[1] = 3;
//1 是 up
buf[2] = 0;
//key code back 键对应的是 4
buf[3] = 4;
int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 4);
printf("send result = %d\n", result);
}
}
}
void EventController::handleMsg(SDL_Screen *sc, SDL_TextInputEvent *event) {
printf("event inputting \n");
char buf[4];
memset(buf, 0, sizeof(buf));
buf[0] = 1;
//自定义的案件事件
buf[1] = EVENT_CUSTOM;
buf[2] = EVENT_UP;
//key code back 键对应的是 4
buf[3] = 4;
// int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 4);
int result = connection->send_to_(reinterpret_cast<u_int8_t *>(event->text), 3);
printf("send result = %d\n", result);
}
void EventController::handleMotionEvent(SDL_Screen *sc, SDL_MouseMotionEvent *event) {
//处理滑动事件
int x_c = event->x;
int y_c = event->y;
// int *x = &x_c;
// int *y = &y_c;
// SDL_GetMouseState(x, y);
// SDL_Rect viewport;
// float scale_x, scale_y;
// SDL_RenderGetViewport(sc->sdl_renderer, &viewport);
// SDL_RenderGetScale(sc->sdl_renderer, &scale_x, &scale_y);
// *x = (int) (*x / scale_x) - viewport.x;
// *y = (int) (*y / scale_y) - viewport.y;
int width = sc->screen_w;
int height = sc->screen_h;
//是否超过来边界
bool outside_device_screen = x_c < 0 || x_c >= width ||
y_c < 0 || y_c >= height;
if (outside_device_screen) {
printf("outside_device_screen");
// ignore
return;
}
SDL_assert_release(x_c >= 0 && x_c < 0x10000 && y_c >= 0 && y_c < 0x10000);
// int mul = event->which == SDL_MOUSEWHEEL_NORMAL ? 1 : -1;
int mul = -1;
int hs = -mul * event->xrel;
int vs = mul * event->yrel;
//记录鼠标移动时的坐标
int mouse_x = event->x;
int mouse_y = event->y;
if (mouseDown) {
//此处处理的是鼠标拖动情况
// printf("event move \n");
// printf("event move x=%d, y =%d, xrel=%d, yrel=%d \n", mouse_x, mouse_y, event->xrel, event->yrel);
char buf[14];
memset(buf, 0, sizeof(buf));
printf(" x_c =%d, y_c =%d, hs =%d, vs =%d\n", x_c, y_c, hs, vs);
buf[0] = 0;
//滚动事件
buf[1] = EVENT_SCROLL;
//高8位
buf[2] = x_c >> 8;
//低8位
buf[3] = x_c & 0xff;
//高8位
buf[4] = y_c >> 8;
//低8位
buf[5] = y_c & 0xff;
//继续滚动距离
// 24 -> 32
buf[6] = hs >> 24;
// 16 -> 23
buf[7] = hs >> 16;
// 8 -> 15
buf[8] = hs >> 8;
// 0 -> 7
buf[9] = hs;
// 24 -> 32
buf[10] = vs >> 24;
// 16 -> 23
buf[11] = vs >> 16;
// 8 -> 15
buf[12] = vs >> 8;
// 0 -> 7
buf[13] = vs;
int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 14);
printf("event move result = %d\n", result);
} else {
//此时处理的是鼠标有可能放到滑动按钮上的情况
// printf("event move x=%d, y =%d, xrel=%d, yrel=%d \n", mouse_x, mouse_y, event->xrel, event->yrel);
}
}
/** for resize test */
void EventController::handleResize(SDL_Screen *sc, SDL_Event event) {
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
int nWidth = event.window.data1;
int nHeight = event.window.data2;
printf("wtf resize event type w=%d h=%d\n", nWidth, nHeight);
Size size = {
nWidth & ~7,
nHeight & ~7,
};
char buf[6];
memset(buf, 0, sizeof(buf));
buf[0] = 0;
buf[1] = 4;
buf[2] = size.width >> 8;
buf[3] = size.width & 0xFF;
buf[4] = size.height >> 8;
buf[5] = size.height & 0xFF;
// int result = connection->send_to_(reinterpret_cast<uint8_t *>(buf), 4);
int result = connection->send_to_(reinterpret_cast<u_int8_t *>(buf), 6);
printf("send resize result = %d, w=%d, h=%d\n", result, size.width, size.height);
// decoder->stop();
// decoder->destroy();
// decoder->latestSize = { size };
// decoder->updateContext(size);
// printf("updated ? resize result = %d, w=%d, h=%d\n", result, decoder->latestSize.width, decoder->latestSize.height);
SDL_Delay(1000);
// decoder->async_start();
}
}
void EventController::event_handle() {
printf("event_handle");
for (;;) {
if (stop) {
break;
}
SDL_Event event;
SDL_LockMutex(mutex);
if (queue->is_empty()) {
// printf("is empty\n");
// return SDL_FALSE;
SDL_CondWait(cond, mutex);
}
SDL_bool sdl_bool = queue->take_event(&event);
SDL_UnlockMutex(mutex);
if (event.type == SDL_MOUSEBUTTONDOWN) {
handleButtonEvent(screen, &event.button);
} else if (event.type == SDL_MOUSEBUTTONUP) {
handleButtonEvent(screen, &event.button);
} else if (event.type == SDL_KEYDOWN) {
handleSDLKeyEvent(screen, &event.key);
} else if (event.type == SDL_KEYUP) {
handleSDLKeyEvent(screen, &event.key);
} else if (event.type == SDL_MOUSEWHEEL) {
//处理滑动事件
handleScrollEvent(screen, &event.wheel);
} else if (event.type == SDL_TEXTINPUT) {
handleMsg(screen, &event.text);
} else if (event.type == SDL_MOUSEMOTION) {
handleMotionEvent(screen, &event.motion);
} else if (event.type == SDL_WINDOWEVENT) {
handleResize(screen, event);
}
}
}
void EventController::stop_loop() {
stop = SDL_TRUE;
SDL_DetachThread(event_tid);
}
EventController::EventController(SDL_Screen *screen, SocketConnection *connection) : screen(screen),
connection(connection) {}
void EventController::push_event(SDL_Event event) {
SDL_LockMutex(mutex);
//先判断当前是否为空。因为如果为空的话,会锁住。
int empty = queue->is_empty();
queue->push_event(event);
//如果是空的话,就需要通知他继续取
if (empty) {
// printf("signal");
SDL_CondSignal(cond);
}
SDL_UnlockMutex(mutex);
}