forked from fruxo/turbobadger
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathport_sdl2.cpp
532 lines (497 loc) · 15.7 KB
/
port_sdl2.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// -*- Mode: C++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
#include "port_sdl2.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
//#include <emscripten/bind.h>
#endif
#include "tb_skin.h"
#include "tb_system.h"
#include "tb_msg.h"
#include "renderers/tb_renderer_gl.h"
#include "tb_font_renderer.h"
#include "Application.h"
#ifdef TB_SYSTEM_MACOSX
#include <unistd.h>
#include <mach-o/dyld.h>
#endif
using namespace tb;
MODIFIER_KEYS GetModifierKeys()
{
MODIFIER_KEYS code = TB_MODIFIER_NONE;
SDL_Keymod mods = SDL_GetModState();
if (mods & KMOD_ALT) code |= TB_ALT;
if (mods & KMOD_CTRL) code |= TB_CTRL;
if (mods & KMOD_SHIFT) code |= TB_SHIFT;
if (mods & KMOD_GUI) code |= TB_SUPER; // no idea what SUPER means, but doesn't seem to be used
return code;
}
MODIFIER_KEYS GetModifierKeys(SDL_Keymod mods)
{
MODIFIER_KEYS code = TB_MODIFIER_NONE;
if (mods & KMOD_ALT) code |= TB_ALT;
if (mods & KMOD_CTRL) code |= TB_CTRL;
if (mods & KMOD_SHIFT) code |= TB_SHIFT;
if (mods & KMOD_GUI) code |= TB_SUPER; // no idea what SUPER means, but doesn't seem to be used
return code;
}
static bool ShouldEmulateTouchEvent()
{
// Used to emulate that mouse events are touch events when alt, ctrl and shift are pressed.
// This makes testing a lot easier when there is no touch screen around :)
return (GetModifierKeys() & (TB_ALT | TB_CTRL | TB_SHIFT)) ? true : false;
}
// @return Return the upper case of a ascii charcter. Only for shortcut handling.
static int toupr_ascii(int ascii)
{
if (ascii >= 'a' && ascii <= 'z')
return ascii + 'A' - 'a';
return ascii;
}
static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
{
#ifdef TB_SYSTEM_MACOSX
bool shortcut_key = (modifierkeys & TB_SUPER) ? true : false;
#else
bool shortcut_key = (modifierkeys & TB_CTRL) ? true : false;
#endif
if (!TBWidget::focused_widget || !down || !shortcut_key)
return false;
bool reverse_key = (modifierkeys & TB_SHIFT) ? true : false;
int upper_key = toupr_ascii(key);
TBID id;
if (upper_key == 'X')
id = TBIDC("cut");
else if (upper_key == 'C' || special_key == TB_KEY_INSERT)
id = TBIDC("copy");
else if (upper_key == 'V' || (special_key == TB_KEY_INSERT && reverse_key))
id = TBIDC("paste");
else if (upper_key == 'A')
id = TBIDC("selectall");
else if (upper_key == 'Z' || upper_key == 'Y')
{
bool undo = upper_key == 'Z';
if (reverse_key)
undo = !undo;
id = undo ? TBIDC("undo") : TBIDC("redo");
}
else if (upper_key == 'N')
id = TBIDC("new");
else if (upper_key == 'O')
id = TBIDC("open");
else if (upper_key == 'Q')
id = TBIDC("quit");
else if (upper_key == 'S')
id = TBIDC("save");
else if (upper_key == 'W')
id = TBIDC("close");
else if (special_key == TB_KEY_PAGE_UP)
id = TBIDC("prev_doc");
else if (special_key == TB_KEY_PAGE_DOWN)
id = TBIDC("next_doc");
else
return false;
TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
ev.modifierkeys = modifierkeys;
ev.ref_id = id;
return TBWidget::focused_widget->InvokeEvent(ev);
}
bool AppBackendSDL2::InvokeKey(unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
{
if (InvokeShortcut(key, special_key, modifierkeys, down))
return true;
return m_app->GetRoot()->InvokeKey(key, special_key, modifierkeys, down);
}
void AppBackendSDL2::QueueUserEvent(Sint32 code, void * data1, void * data2)
{
// queue a user event to cause the SDL event loop to run
SDL_Event event;
SDL_UserEvent userevent;
userevent.type = SDL_USEREVENT;
userevent.code = code;
userevent.data1 = data1;
userevent.data2 = data2;
event.type = SDL_USEREVENT;
event.user = userevent;
SDL_PushEvent(&event);
}
bool AppBackendSDL2::Init(App *app)
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
{
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#if defined(TB_RENDERER_GLES_2)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#elif defined(TB_RENDERER_GL3)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
#endif
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_SetHint("SDL_HINT_ORIENTATIONS", "Portrait LandscapeLeft LandscapeRight");
#if defined(TB_SYSTEM_ANDROID) || defined(TB_SYSTEM_IOS)
int width = 0;
int height = 0;
#else
int width = app->GetWidth() > 0 ? app->GetWidth() : 1920;
int height = app->GetHeight() > 0 ? app->GetHeight() : 1080;
#endif
mainWindow = SDL_CreateWindow(app->GetTitle(),
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width, height,
//SDL_WINDOW_HIDDEN |
SDL_WINDOW_SHOWN |
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE |
#if defined(TB_SYSTEM_ANDROID)
SDL_WINDOW_FULLSCREEN |
#endif
SDL_WINDOW_ALLOW_HIGHDPI
);
if (!mainWindow)
{
SDL_Log("Unable to create window: %s\n", SDL_GetError());
return 1;
}
glContext = SDL_GL_CreateContext(mainWindow);
SDL_GL_MakeCurrent(mainWindow, glContext);
SDL_ShowWindow(mainWindow);
glClearColor(0.3f, 0.3f, 0.3f, 1);
m_renderer = new TBRendererGL();
tb_core_init(m_renderer);
// Create the App object for our demo
m_app = app;
SDL_GetWindowSize(mainWindow, &width, &height);
int pix_width, pix_height;
SDL_GL_GetDrawableSize(mainWindow, &pix_width, &pix_height);
m_xscale = (float)pix_width / width;
m_yscale = (float)pix_height / height;
TBSystem::SetDPI(96 * m_xscale);
m_app->OnBackendAttached(this, pix_width, pix_height);
return true;
}
AppBackendSDL2::~AppBackendSDL2()
{
m_app->OnBackendDetached();
m_app = nullptr;
tb_core_shutdown();
// Close and destroy the window
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(mainWindow);
SDL_Quit();
delete m_renderer;
m_renderer = nullptr;
g_renderer = nullptr;
}
#ifdef __EMSCRIPTEN__
//using namespace emscripten;
static AppBackendSDL2 *backend;
void mainloop()
{
// Event loop
SDL_Event event;
if (SDL_PollEvent(&event))
if (!backend->HandleSDLEvent(event))
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "mainloop() unhandled SDL event: 0x%x\n", event.type);
// Decide when we should be called next
if (SDL_PollEvent(NULL))
emscripten_set_main_loop_timing(EM_TIMING_SETIMMEDIATE, 0);
else
emscripten_set_main_loop_timing(EM_TIMING_RAF, 1);
}
void AppBackendSDL2::EventLoop()
{
backend = this;
emscripten_set_main_loop(mainloop, 0, 1);
}
#else // ! __EMSCRIPTEN__
void AppBackendSDL2::EventLoop()
{
SDL_Event event;
do {
// handle events
SDL_WaitEvent(&event);
if (!HandleSDLEvent(event))
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Unhandled SDL event: 0x%x\n", event.type);
} while (!m_quit_requested);
}
#endif // __EMSCRIPTEN__
void AppBackendSDL2::OnAppEvent(const EVENT &ev)
{
switch (ev)
{
case EVENT_PAINT_REQUEST:
if (!m_has_pending_update)
{
m_has_pending_update = true;
// queue a user event to cause the event loop to run
QueueUserEvent(0);
}
break;
case EVENT_QUIT_REQUEST:
m_quit_requested = true;
// queue a user event to cause the event loop to run
QueueUserEvent(1);
break;
case EVENT_TITLE_CHANGED:
SDL_SetWindowTitle(mainWindow, m_app->GetTitle());
break;
default:
assert(!"Unhandled app event!");
}
}
// Attempt to convert an sdl event to a TB event, return true if handled
bool AppBackendSDL2::HandleSDLEvent(SDL_Event & event)
{
bool handled = true;
switch (event.type) {
case SDL_KEYUP:
case SDL_KEYDOWN: {
// SDL_KeyboardEvent
// Handle any key presses that wont also be SDL_TEXTINPUT here.
bool down = event.type == SDL_KEYDOWN;
MODIFIER_KEYS modifier = GetModifierKeys((SDL_Keymod)event.key.keysym.mod);
// handle keys
switch (event.key.keysym.sym)
{
case SDLK_F1: InvokeKey(0, TB_KEY_F1, modifier, down); break;
case SDLK_F2: InvokeKey(0, TB_KEY_F2, modifier, down); break;
case SDLK_F3: InvokeKey(0, TB_KEY_F3, modifier, down); break;
case SDLK_F4: InvokeKey(0, TB_KEY_F4, modifier, down); break;
case SDLK_F5: InvokeKey(0, TB_KEY_F5, modifier, down); break;
case SDLK_F6: InvokeKey(0, TB_KEY_F6, modifier, down); break;
case SDLK_F7: InvokeKey(0, TB_KEY_F7, modifier, down); break;
case SDLK_F8: InvokeKey(0, TB_KEY_F8, modifier, down); break;
case SDLK_F9: InvokeKey(0, TB_KEY_F9, modifier, down); break;
case SDLK_F10: InvokeKey(0, TB_KEY_F10, modifier, down); break;
case SDLK_F11: InvokeKey(0, TB_KEY_F11, modifier, down); break;
case SDLK_F12: InvokeKey(0, TB_KEY_F12, modifier, down); break;
case SDLK_LEFT: InvokeKey(0, TB_KEY_LEFT, modifier, down); break;
case SDLK_UP: InvokeKey(0, TB_KEY_UP, modifier, down); break;
case SDLK_RIGHT: InvokeKey(0, TB_KEY_RIGHT, modifier, down); break;
case SDLK_DOWN: InvokeKey(0, TB_KEY_DOWN, modifier, down); break;
case SDLK_PAGEUP: InvokeKey(0, TB_KEY_PAGE_UP, modifier, down); break;
case SDLK_PAGEDOWN: InvokeKey(0, TB_KEY_PAGE_DOWN, modifier, down); break;
case SDLK_HOME: InvokeKey(0, TB_KEY_HOME, modifier, down); break;
case SDLK_END: InvokeKey(0, TB_KEY_END, modifier, down); break;
case SDLK_INSERT: InvokeKey(0, TB_KEY_INSERT, modifier, down); break;
case SDLK_TAB: InvokeKey(0, TB_KEY_TAB, modifier, down); break;
case SDLK_DELETE: InvokeKey(0, TB_KEY_DELETE, modifier, down); break;
case SDLK_BACKSPACE: InvokeKey(0, TB_KEY_BACKSPACE, modifier, down); break;
case SDLK_RETURN:
case SDLK_KP_ENTER: InvokeKey(0, TB_KEY_ENTER, modifier, down); break;
case SDLK_ESCAPE: InvokeKey(0, TB_KEY_ESC, modifier, down); break;
case SDLK_MENU:
if (TBWidget::focused_widget && !down)
{
TBWidgetEvent ev(EVENT_TYPE_CONTEXT_MENU);
ev.modifierkeys = modifier;
TBWidget::focused_widget->InvokeEvent(ev);
}
break;
/* just ignore lone modifier key presses */
case SDLK_LCTRL:
case SDLK_RCTRL:
case SDLK_LALT:
case SDLK_RALT:
case SDLK_LSHIFT:
case SDLK_RSHIFT:
case SDLK_LGUI:
case SDLK_RGUI:
break;
/* also ignore everything else, since it should show up in SDL_TEXTINPUT */
case SDLK_PLUS:
case SDLK_MINUS:
default:
// skip key events that will appear as SDL_TEXTINPUT
if (modifier & (TB_ALT | TB_CTRL)) {
unsigned int character = event.key.keysym.sym;
if (character >= 'a' && character <= 'z' && (modifier & TB_SHIFT))
character += ('A' - 'a');
InvokeKey(character, TB_KEY_UNDEFINED, modifier, down);
}
handled = true;
break;
}
break;
}
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
//event.tfinger;
break;
case SDL_MOUSEMOTION:
if (m_app->GetRoot() && !(ShouldEmulateTouchEvent() && !TBWidget::captured_widget)) {
event.motion.x *= m_xscale;
event.motion.y *= m_yscale;
m_app->GetRoot()->InvokePointerMove(event.motion.x, event.motion.y,
GetModifierKeys(),
ShouldEmulateTouchEvent());
}
break;
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN: {
// Handle mouse clicks here.
MODIFIER_KEYS modifier = GetModifierKeys();
event.button.x *= m_xscale;
event.button.y *= m_yscale;
int x = event.button.x;
int y = event.button.y;
if (event.button.button == SDL_BUTTON_LEFT)
{
int counter = event.button.clicks;
if (event.type == SDL_MOUSEBUTTONDOWN)
{
m_app->GetRoot()->InvokePointerDown(x, y, counter, modifier, ShouldEmulateTouchEvent());
}
else
m_app->GetRoot()->InvokePointerUp(x, y, counter, modifier, ShouldEmulateTouchEvent());
}
else if (event.button.button == SDL_BUTTON_RIGHT && event.type == SDL_MOUSEBUTTONUP)
{
m_app->GetRoot()->InvokePointerMove(x, y, modifier, ShouldEmulateTouchEvent());
if (TBWidget::hovered_widget)
{
TBWidget::hovered_widget->ConvertFromRoot(x, y);
TBWidgetEvent ev(EVENT_TYPE_CONTEXT_MENU, x, y, false, modifier);
TBWidget::hovered_widget->InvokeEvent(ev);
}
}
}
break;
case SDL_MOUSEWHEEL: {
int mouse_x, mouse_y;
SDL_GetMouseState(&mouse_x, &mouse_y);
mouse_x *= m_xscale;
mouse_y *= m_yscale;
if (m_app->GetRoot())
m_app->GetRoot()->InvokeWheel(mouse_x, mouse_y,
(int)event.wheel.x, -(int)event.wheel.y,
GetModifierKeys());
break;
}
case SDL_MULTIGESTURE:
//event.mgesture;
break;
case SDL_SYSWMEVENT:
//event.syswm;
break;
case SDL_TEXTEDITING:
//event.edit;
break;
case SDL_TEXTINPUT: {
MODIFIER_KEYS modifier = GetModifierKeys();
for (int ii = 0; event.text.text[ii]; ii++) {
unsigned int character = event.text.text[ii];
InvokeKey(character, TB_KEY_UNDEFINED, modifier, true);
InvokeKey(character, TB_KEY_UNDEFINED, modifier, false);
}
break;
}
case SDL_WINDOWEVENT: {
switch (event.window.event) {
case SDL_WINDOWEVENT_SHOWN:
//SDL_Log("Window %d shown", event.window.windowID);
break;
case SDL_WINDOWEVENT_HIDDEN:
//SDL_Log("Window %d hidden", event.window.windowID);
break;
case SDL_WINDOWEVENT_EXPOSED:
//SDL_Log("Window %d exposed", event.window.windowID);
OnAppEvent(EVENT_PAINT_REQUEST);
break;
case SDL_WINDOWEVENT_MOVED:
//SDL_Log("Window %d moved to %d,%d",
// event.window.windowID, event.window.data1,
// event.window.data2);
break;
case SDL_WINDOWEVENT_RESIZED:
if (m_app)
m_app->OnResized(event.window.data1, event.window.data2);
//SDL_Log("Window %d resized to %dx%d",
// event.window.windowID, event.window.data1,
// event.window.data2);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
//SDL_Log("Window %d size changed to %dx%d",
// event.window.windowID, event.window.data1,
// event.window.data2);
break;
case SDL_WINDOWEVENT_MINIMIZED:
//SDL_Log("Window %d minimized", event.window.windowID);
break;
case SDL_WINDOWEVENT_MAXIMIZED:
//SDL_Log("Window %d maximized", event.window.windowID);
break;
case SDL_WINDOWEVENT_RESTORED:
//SDL_Log("Window %d restored", event.window.windowID);
break;
case SDL_WINDOWEVENT_ENTER:
//SDL_Log("Mouse entered window %d", event.window.windowID);
break;
case SDL_WINDOWEVENT_LEAVE:
//SDL_Log("Mouse left window %d", event.window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
//SDL_Log("Window %d gained keyboard focus", event.window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
//SDL_Log("Window %d lost keyboard focus", event.window.windowID);
break;
case SDL_WINDOWEVENT_CLOSE:
//SDL_Log("Window %d closed", event.window.windowID);
break;
default:
handled = false;
SDL_Log("Window %d got unknown event %d", event.window.windowID, event.window.event);
break;
}
break;
}
case SDL_USEREVENT:
if (event.user.code == 3) {
// user.code == 3 is sent by tb_sdl_timer_callback(Uint32 interval, void *param)
TBMessageHandler::ProcessMessages();
// If we still have things to do (because we didn't process all messages,
// or because there are new messages), we need to rescedule, so call RescheduleTimer.
double next_fire_time = TBMessageHandler::GetNextMessageFireTime();
if (next_fire_time != TB_NOT_SOON)
TBSystem::RescheduleTimer(next_fire_time);
}
// draw event
if (m_has_pending_update)
{
m_app->Process();
m_has_pending_update = false;
// Bail out if we get here with invalid dimensions.
// This may happen when minimizing windows (GLFW 3.0.4, Windows 8.1).
if (GetWidth() == 0 || GetHeight() == 0)
; // ignore
else
{
m_app->RenderFrame();
SDL_GL_SwapWindow(mainWindow);
}
}
break;
case SDL_QUIT:
m_quit_requested = true;
return true;
default:
handled = false;
}
return handled;
}