-
Notifications
You must be signed in to change notification settings - Fork 4
/
clem_host_app.cpp
645 lines (572 loc) · 23.6 KB
/
clem_host_app.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include "spdlog/common.h"
#include <cinttypes>
#ifdef _WIN32
#include <combaseapi.h>
#endif
#include "clem_host.hpp"
#include "clem_imgui.hpp"
#include <array>
#include <clocale>
#include <cmath>
#include <cstdio>
#include <filesystem>
#include "clem_host_platform.h"
#include "clem_assets.hpp"
#include "clem_configuration.hpp"
#include "clem_front.hpp"
#include "clem_startup_view.hpp"
#include "sokol/sokol_app.h"
#include "sokol/sokol_audio.h"
#include "sokol/sokol_gfx.h"
#include "sokol/sokol_glue.h"
#include "sokol/sokol_imgui.h"
#include "sokol/sokol_time.h"
#include "spdlog/spdlog.h"
#include "fonts/font_bloada1024.h"
#include "fonts/font_printchar21.h"
#include "fonts/font_prnumber3.h"
struct SharedAppData {
SharedAppData(int /*argc*/, char *[] /* *argv[] */) : config(findConfiguration()) {}
ImVector<ImWchar> imguiUnicodeRanges;
ClemensConfiguration config;
};
static uint64_t g_lastTime = 0;
static ClemensHostView *g_Host = nullptr;
static sg_pass_action g_sgPassAction;
static unsigned g_ADBKeyToggleMask = 0;
static cinek::ByteBuffer g_systemFontLoBuffer;
static cinek::ByteBuffer g_systemFontHiBuffer;
static const unsigned kClipboardTextLimit = 8192;
static ClemensHostInterop g_interop{};
#define CLEMENS_APP_ICONS_ARE_RESIDENT
#if defined(CLEMENS_APP_ICONS_ARE_RESIDENT)
#include "images/app_icons_png.h"
void loadAppIcon(sapp_image_desc *desc, const uint8_t *src, const unsigned srcLen) {
desc->pixels.ptr =
ClemensHostAssets::loadBitmapFromPNG(src, srcLen, &desc->width, &desc->height);
if (!desc->pixels.ptr) {
return;
}
desc->pixels.size = desc->width * desc->height * 4;
}
#endif
// Keyboard customization
// Typically the OS specific "super" key is used to augment key combinations that
// may otherwise be intercepted by the OS. This usage really depends on the target
// platform. See each platform's implementation below for exceptional cases.
#if defined(CLEMENS_PLATFORM_LINUX)
// utility for platforms that require special mapping of function keys
static int xlatToFnKey(const sapp_event *evt) {
int fnKey = -1;
if (evt->key_code >= SAPP_KEYCODE_0 && evt->key_code <= SAPP_KEYCODE_9) {
fnKey = evt->key_code - SAPP_KEYCODE_0;
if (fnKey == 0)
fnKey = 10;
} else if (evt->key_code == SAPP_KEYCODE_MINUS) {
fnKey = 11;
} else if (evt->key_code == SAPP_KEYCODE_EQUAL) {
fnKey = 12;
}
return fnKey;
}
#endif
#if defined(CLEMENS_PLATFORM_WINDOWS)
// The SUPER key maps to the Windows Key, which is pretty much off limits
// for key-mapping. Windows also traps the CTRL+ESC key combination.
// The logic below ensures that the ESC key can be used in the emulated
// machine in combination with either CTRL or ALT.
//
// CTRL + F1 = CTRL-ESC
// ALT + F1 = APPLE or OPTION - ESC
//
static bool g_escapeKeyDown = false;
static sapp_keycode onKeyDown(const sapp_event *evt) {
if (evt->modifiers & (SAPP_MODIFIER_CTRL + SAPP_MODIFIER_ALT)) {
if (evt->key_code == SAPP_KEYCODE_F1 && !g_escapeKeyDown) {
g_escapeKeyDown = true;
return SAPP_KEYCODE_ESCAPE;
}
}
if (evt->key_code == SAPP_KEYCODE_ESCAPE) {
if (g_escapeKeyDown)
return SAPP_KEYCODE_INVALID;
}
return evt->key_code;
}
static sapp_keycode onKeyUp(const sapp_event *evt, bool *doDownEvent) {
if (g_escapeKeyDown) {
if (evt->key_code == SAPP_KEYCODE_F1) {
g_escapeKeyDown = false;
return SAPP_KEYCODE_ESCAPE;
} else if (evt->key_code == SAPP_KEYCODE_ESCAPE) {
return SAPP_KEYCODE_INVALID;
}
}
*doDownEvent = false;
return evt->key_code;
}
#elif defined(CLEMENS_PLATFORM_LINUX)
// Linux, Alt and function keys makes emulation tricky
// Solution: When either Ctrl or ALt are pressed, number keys are translated
// to function keys and remain so until the number key is released
// (g_fnKeys[n] = 0 for up, 1 = down)
//
//
static bool g_leftAltKeyDown = false;
static bool g_leftCtrlKeyDown = false;
static bool g_rightAltKeyDown = false;
static bool g_rightCtrlKeyDown = false;
static bool g_escapeKeyDown = false;
static bool g_fnKeys[12];
static sapp_keycode onKeyDown(const sapp_event *evt) {
sapp_keycode outKeyCode = evt->key_code;
if (!g_leftAltKeyDown && evt->key_code == SAPP_KEYCODE_LEFT_ALT)
g_leftAltKeyDown = true;
if (!g_rightAltKeyDown && evt->key_code == SAPP_KEYCODE_RIGHT_ALT)
g_rightAltKeyDown = true;
if (!g_leftCtrlKeyDown && evt->key_code == SAPP_KEYCODE_LEFT_CONTROL)
g_leftCtrlKeyDown = true;
if (!g_rightCtrlKeyDown && evt->key_code == SAPP_KEYCODE_RIGHT_CONTROL)
g_rightCtrlKeyDown = true;
int fnKey = xlatToFnKey(evt);
if (g_leftAltKeyDown || g_leftCtrlKeyDown || g_rightAltKeyDown || g_rightCtrlKeyDown) {
if (fnKey > 0) {
g_fnKeys[fnKey - 1] = true;
outKeyCode = static_cast<sapp_keycode>(static_cast<int>(SAPP_KEYCODE_F1) + (fnKey - 1));
}
}
if (evt->modifiers & (SAPP_MODIFIER_CTRL + SAPP_MODIFIER_ALT)) {
if (g_fnKeys[0] && !g_escapeKeyDown) {
g_escapeKeyDown = true;
outKeyCode = SAPP_KEYCODE_ESCAPE;
}
}
if (evt->key_code == SAPP_KEYCODE_ESCAPE) {
if (g_escapeKeyDown)
outKeyCode = SAPP_KEYCODE_INVALID;
}
return outKeyCode;
}
static sapp_keycode onKeyUp(const sapp_event *evt, bool *doDownEvent) {
sapp_keycode outKeyCode = evt->key_code;
if (g_leftAltKeyDown && evt->key_code == SAPP_KEYCODE_LEFT_ALT)
g_leftAltKeyDown = false;
else if (g_rightAltKeyDown && evt->key_code == SAPP_KEYCODE_RIGHT_ALT)
g_rightAltKeyDown = false;
if (g_leftCtrlKeyDown && evt->key_code == SAPP_KEYCODE_LEFT_CONTROL)
g_leftCtrlKeyDown = false;
else if (g_rightAltKeyDown && evt->key_code == SAPP_KEYCODE_RIGHT_CONTROL)
g_rightCtrlKeyDown = false;
int fnKey = xlatToFnKey(evt);
if (fnKey > 0 && g_fnKeys[fnKey - 1]) {
g_fnKeys[fnKey - 1] = false;
outKeyCode = static_cast<sapp_keycode>(static_cast<int>(SAPP_KEYCODE_F1) + (fnKey - 1));
}
if (g_escapeKeyDown) {
if (fnKey == 1) {
g_escapeKeyDown = false;
outKeyCode = SAPP_KEYCODE_ESCAPE;
} else if (evt->key_code == SAPP_KEYCODE_ESCAPE) {
outKeyCode = SAPP_KEYCODE_INVALID;
}
}
*doDownEvent = false;
return outKeyCode;
}
#elif defined(CLEMENS_PLATFORM_MACOS)
// Option -> Alt (Option/Closed apple)
// Command -> Super (Open Apple)
// Both Option and Command do not have a 'right' equivalent exposed by NSEvent
// Function keys often require pressing the Fn key on macOS (unless this feature
// was turned off by the user via macOS preferences.)
// edge case where CTRL + ESC does not report the ESCAPE down event
static bool g_escape_down = false;
static sapp_keycode onKeyDown(const sapp_event *evt) {
sapp_keycode outKeyCode = evt->key_code;
if (evt->key_code == SAPP_KEYCODE_LEFT_SUPER) {
outKeyCode = SAPP_KEYCODE_RIGHT_ALT;
}
if (evt->modifiers & (SAPP_MODIFIER_CTRL + SAPP_MODIFIER_ALT)) {
if (evt->key_code == SAPP_KEYCODE_F1 && !g_escape_down) {
g_escape_down = true;
return SAPP_KEYCODE_ESCAPE;
}
}
if (evt->key_code == SAPP_KEYCODE_ESCAPE) {
if (g_escape_down)
return SAPP_KEYCODE_INVALID;
g_escape_down = true;
}
return outKeyCode;
}
static sapp_keycode onKeyUp(const sapp_event *evt, bool *doDownEvent) {
sapp_keycode outKeyCode = evt->key_code;
*doDownEvent = false;
if (evt->key_code == SAPP_KEYCODE_LEFT_SUPER) {
outKeyCode = SAPP_KEYCODE_RIGHT_ALT;
}
if (evt->key_code == SAPP_KEYCODE_ESCAPE) {
if (!g_escape_down)
*doDownEvent = true;
g_escape_down = false;
} else if (g_escape_down) {
if (evt->key_code == SAPP_KEYCODE_F1 || evt->key_code == SAPP_KEYCODE_ESCAPE) {
g_escape_down = false;
outKeyCode = SAPP_KEYCODE_ESCAPE;
}
}
return outKeyCode;
}
#else
static sapp_keycode onKeyDown(const sapp_event *evt) { return evt->key_code; }
static sapp_keycode onKeyUp(const sapp_event *evt, bool *doDownEvent) {
*doDownEvent = false;
return evt->key_code;
}
#endif
std::array<int16_t, 512> g_sokolToADBKey;
void sokolLogger(const char *tag, // e.g. 'sg'
uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
uint32_t, // SG_LOGITEM_*
const char *message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_gfx.h
const char *filename_or_null, // source filename, may be nullptr in release mode
void *) {
static spdlog::level::level_enum levels[] = {spdlog::level::critical, spdlog::level::err,
spdlog::level::warn, spdlog::level::info};
if (!message_or_null)
return;
spdlog::log(levels[log_level], "[{}] {}({}) {}", tag, filename_or_null, line_nr,
message_or_null);
}
static cinek::ByteBuffer loadFont(const char *pathname) {
cinek::ByteBuffer buffer;
if (!strcasecmp(pathname, "fonts/PrintChar21.ttf")) {
buffer = cinek::ByteBuffer(PrintChar21_ttf, PrintChar21_ttf_len, PrintChar21_ttf_len);
} else if (!strcasecmp(pathname, "fonts/PRNumber3.ttf")) {
buffer = cinek::ByteBuffer(PRNumber3_ttf, PRNumber3_ttf_len, PRNumber3_ttf_len);
}
return buffer;
}
static void onInit(void *userdata) {
auto *appdata = reinterpret_cast<SharedAppData *>(userdata);
g_interop.debuggerOn = appdata->config.hybridInterfaceEnabled;
clemens_host_init(&g_interop);
stm_setup();
#if defined(CLEMENS_PLATFORM_WINDOWS)
CoInitializeEx(NULL, COINIT_MULTITHREADED);
#endif
sg_desc desc = {};
desc.context = sapp_sgcontext();
desc.logger.func = sokolLogger;
sg_setup(desc);
g_sgPassAction.colors[0].action = SG_ACTION_CLEAR;
g_sgPassAction.colors[0].value = {0.0f, 0.5f, 0.75f, 1.0f};
simgui_desc_t simguiDesc = {};
simguiDesc.no_default_font = true;
simgui_setup(simguiDesc);
g_sokolToADBKey.fill(-1);
g_sokolToADBKey[SAPP_KEYCODE_SPACE] = CLEM_ADB_KEY_SPACE;
g_sokolToADBKey[SAPP_KEYCODE_APOSTROPHE] = CLEM_ADB_KEY_APOSTRAPHE;
g_sokolToADBKey[SAPP_KEYCODE_COMMA] = CLEM_ADB_KEY_COMMA;
g_sokolToADBKey[SAPP_KEYCODE_MINUS] = CLEM_ADB_KEY_MINUS;
g_sokolToADBKey[SAPP_KEYCODE_PERIOD] = CLEM_ADB_KEY_PERIOD;
g_sokolToADBKey[SAPP_KEYCODE_SLASH] = CLEM_ADB_KEY_FWDSLASH;
g_sokolToADBKey[SAPP_KEYCODE_0] = CLEM_ADB_KEY_0;
g_sokolToADBKey[SAPP_KEYCODE_1] = CLEM_ADB_KEY_1;
g_sokolToADBKey[SAPP_KEYCODE_2] = CLEM_ADB_KEY_2;
g_sokolToADBKey[SAPP_KEYCODE_3] = CLEM_ADB_KEY_3;
g_sokolToADBKey[SAPP_KEYCODE_4] = CLEM_ADB_KEY_4;
g_sokolToADBKey[SAPP_KEYCODE_5] = CLEM_ADB_KEY_5;
g_sokolToADBKey[SAPP_KEYCODE_6] = CLEM_ADB_KEY_6;
g_sokolToADBKey[SAPP_KEYCODE_7] = CLEM_ADB_KEY_7;
g_sokolToADBKey[SAPP_KEYCODE_8] = CLEM_ADB_KEY_8;
g_sokolToADBKey[SAPP_KEYCODE_9] = CLEM_ADB_KEY_9;
g_sokolToADBKey[SAPP_KEYCODE_SEMICOLON] = CLEM_ADB_KEY_SEMICOLON;
g_sokolToADBKey[SAPP_KEYCODE_EQUAL] = CLEM_ADB_KEY_EQUALS;
g_sokolToADBKey[SAPP_KEYCODE_A] = CLEM_ADB_KEY_A;
g_sokolToADBKey[SAPP_KEYCODE_B] = CLEM_ADB_KEY_B;
g_sokolToADBKey[SAPP_KEYCODE_C] = CLEM_ADB_KEY_C;
g_sokolToADBKey[SAPP_KEYCODE_D] = CLEM_ADB_KEY_D;
g_sokolToADBKey[SAPP_KEYCODE_E] = CLEM_ADB_KEY_E;
g_sokolToADBKey[SAPP_KEYCODE_F] = CLEM_ADB_KEY_F;
g_sokolToADBKey[SAPP_KEYCODE_G] = CLEM_ADB_KEY_G;
g_sokolToADBKey[SAPP_KEYCODE_H] = CLEM_ADB_KEY_H;
g_sokolToADBKey[SAPP_KEYCODE_I] = CLEM_ADB_KEY_I;
g_sokolToADBKey[SAPP_KEYCODE_J] = CLEM_ADB_KEY_J;
g_sokolToADBKey[SAPP_KEYCODE_K] = CLEM_ADB_KEY_K;
g_sokolToADBKey[SAPP_KEYCODE_L] = CLEM_ADB_KEY_L;
g_sokolToADBKey[SAPP_KEYCODE_M] = CLEM_ADB_KEY_M;
g_sokolToADBKey[SAPP_KEYCODE_N] = CLEM_ADB_KEY_N;
g_sokolToADBKey[SAPP_KEYCODE_O] = CLEM_ADB_KEY_O;
g_sokolToADBKey[SAPP_KEYCODE_P] = CLEM_ADB_KEY_P;
g_sokolToADBKey[SAPP_KEYCODE_Q] = CLEM_ADB_KEY_Q;
g_sokolToADBKey[SAPP_KEYCODE_R] = CLEM_ADB_KEY_R;
g_sokolToADBKey[SAPP_KEYCODE_S] = CLEM_ADB_KEY_S;
g_sokolToADBKey[SAPP_KEYCODE_T] = CLEM_ADB_KEY_T;
g_sokolToADBKey[SAPP_KEYCODE_U] = CLEM_ADB_KEY_U;
g_sokolToADBKey[SAPP_KEYCODE_V] = CLEM_ADB_KEY_V;
g_sokolToADBKey[SAPP_KEYCODE_W] = CLEM_ADB_KEY_W;
g_sokolToADBKey[SAPP_KEYCODE_X] = CLEM_ADB_KEY_X;
g_sokolToADBKey[SAPP_KEYCODE_Y] = CLEM_ADB_KEY_Y;
g_sokolToADBKey[SAPP_KEYCODE_Z] = CLEM_ADB_KEY_Z;
g_sokolToADBKey[SAPP_KEYCODE_LEFT_BRACKET] = CLEM_ADB_KEY_LBRACKET;
g_sokolToADBKey[SAPP_KEYCODE_BACKSLASH] = CLEM_ADB_KEY_LBRACKET;
g_sokolToADBKey[SAPP_KEYCODE_RIGHT_BRACKET] = CLEM_ADB_KEY_LBRACKET;
g_sokolToADBKey[SAPP_KEYCODE_GRAVE_ACCENT] = CLEM_ADB_KEY_BACKQUOTE;
g_sokolToADBKey[SAPP_KEYCODE_ESCAPE] = CLEM_ADB_KEY_ESCAPE;
g_sokolToADBKey[SAPP_KEYCODE_ENTER] = CLEM_ADB_KEY_RETURN;
g_sokolToADBKey[SAPP_KEYCODE_TAB] = CLEM_ADB_KEY_TAB;
g_sokolToADBKey[SAPP_KEYCODE_BACKSPACE] = CLEM_ADB_KEY_DELETE;
g_sokolToADBKey[SAPP_KEYCODE_INSERT] = CLEM_ADB_KEY_HELP_INSERT;
g_sokolToADBKey[SAPP_KEYCODE_DELETE] = CLEM_ADB_KEY_DELETE;
g_sokolToADBKey[SAPP_KEYCODE_RIGHT] = CLEM_ADB_KEY_RIGHT;
g_sokolToADBKey[SAPP_KEYCODE_LEFT] = CLEM_ADB_KEY_LEFT;
g_sokolToADBKey[SAPP_KEYCODE_DOWN] = CLEM_ADB_KEY_DOWN;
g_sokolToADBKey[SAPP_KEYCODE_UP] = CLEM_ADB_KEY_UP;
g_sokolToADBKey[SAPP_KEYCODE_PAGE_UP] = CLEM_ADB_KEY_PAGEUP;
g_sokolToADBKey[SAPP_KEYCODE_PAGE_DOWN] = CLEM_ADB_KEY_PAGEDOWN;
g_sokolToADBKey[SAPP_KEYCODE_HOME] = CLEM_ADB_KEY_HOME;
g_sokolToADBKey[SAPP_KEYCODE_END] = CLEM_ADB_KEY_END;
g_sokolToADBKey[SAPP_KEYCODE_CAPS_LOCK] = CLEM_ADB_KEY_CAPSLOCK;
g_sokolToADBKey[SAPP_KEYCODE_NUM_LOCK] = CLEM_ADB_KEY_PAD_CLEAR_NUMLOCK;
g_sokolToADBKey[SAPP_KEYCODE_F1] = CLEM_ADB_KEY_F1;
g_sokolToADBKey[SAPP_KEYCODE_F2] = CLEM_ADB_KEY_F2;
g_sokolToADBKey[SAPP_KEYCODE_F3] = CLEM_ADB_KEY_F3;
g_sokolToADBKey[SAPP_KEYCODE_F4] = CLEM_ADB_KEY_F4;
g_sokolToADBKey[SAPP_KEYCODE_F5] = CLEM_ADB_KEY_F5;
g_sokolToADBKey[SAPP_KEYCODE_F6] = CLEM_ADB_KEY_F6;
g_sokolToADBKey[SAPP_KEYCODE_F7] = CLEM_ADB_KEY_F7;
g_sokolToADBKey[SAPP_KEYCODE_F8] = CLEM_ADB_KEY_F8;
g_sokolToADBKey[SAPP_KEYCODE_F9] = CLEM_ADB_KEY_F9;
g_sokolToADBKey[SAPP_KEYCODE_F10] = CLEM_ADB_KEY_F10;
g_sokolToADBKey[SAPP_KEYCODE_F11] = CLEM_ADB_KEY_F11;
g_sokolToADBKey[SAPP_KEYCODE_F12] = CLEM_ADB_KEY_RESET;
g_sokolToADBKey[SAPP_KEYCODE_F13] = CLEM_ADB_KEY_F13;
g_sokolToADBKey[SAPP_KEYCODE_F14] = CLEM_ADB_KEY_F14;
g_sokolToADBKey[SAPP_KEYCODE_F15] = CLEM_ADB_KEY_F15;
g_sokolToADBKey[SAPP_KEYCODE_KP_0] = CLEM_ADB_KEY_PAD_0;
g_sokolToADBKey[SAPP_KEYCODE_KP_1] = CLEM_ADB_KEY_PAD_1;
g_sokolToADBKey[SAPP_KEYCODE_KP_2] = CLEM_ADB_KEY_PAD_2;
g_sokolToADBKey[SAPP_KEYCODE_KP_3] = CLEM_ADB_KEY_PAD_3;
g_sokolToADBKey[SAPP_KEYCODE_KP_4] = CLEM_ADB_KEY_PAD_4;
g_sokolToADBKey[SAPP_KEYCODE_KP_5] = CLEM_ADB_KEY_PAD_5;
g_sokolToADBKey[SAPP_KEYCODE_KP_6] = CLEM_ADB_KEY_PAD_6;
g_sokolToADBKey[SAPP_KEYCODE_KP_7] = CLEM_ADB_KEY_PAD_7;
g_sokolToADBKey[SAPP_KEYCODE_KP_8] = CLEM_ADB_KEY_PAD_8;
g_sokolToADBKey[SAPP_KEYCODE_KP_9] = CLEM_ADB_KEY_PAD_9;
g_sokolToADBKey[SAPP_KEYCODE_KP_DECIMAL] = CLEM_ADB_KEY_PAD_DECIMAL;
g_sokolToADBKey[SAPP_KEYCODE_KP_DIVIDE] = CLEM_ADB_KEY_PAD_DIVIDE;
g_sokolToADBKey[SAPP_KEYCODE_KP_MULTIPLY] = CLEM_ADB_KEY_PAD_MULTIPLY;
g_sokolToADBKey[SAPP_KEYCODE_KP_SUBTRACT] = CLEM_ADB_KEY_PAD_MINUS;
g_sokolToADBKey[SAPP_KEYCODE_KP_ADD] = CLEM_ADB_KEY_PAD_PLUS;
g_sokolToADBKey[SAPP_KEYCODE_KP_ENTER] = CLEM_ADB_KEY_PAD_ENTER;
g_sokolToADBKey[SAPP_KEYCODE_KP_EQUAL] = CLEM_ADB_KEY_PAD_EQUALS;
g_sokolToADBKey[SAPP_KEYCODE_LEFT_SHIFT] = CLEM_ADB_KEY_LSHIFT;
g_sokolToADBKey[SAPP_KEYCODE_LEFT_CONTROL] = CLEM_ADB_KEY_LCTRL;
g_sokolToADBKey[SAPP_KEYCODE_LEFT_ALT] = CLEM_ADB_KEY_OPTION;
g_sokolToADBKey[SAPP_KEYCODE_RIGHT_SHIFT] = CLEM_ADB_KEY_RSHIFT;
g_sokolToADBKey[SAPP_KEYCODE_RIGHT_CONTROL] = CLEM_ADB_KEY_RCTRL;
g_sokolToADBKey[SAPP_KEYCODE_RIGHT_ALT] = CLEM_ADB_KEY_COMMAND_OPEN_APPLE;
g_systemFontLoBuffer = loadFont("fonts/PrintChar21.ttf");
g_systemFontHiBuffer = loadFont("fonts/PRNumber3.ttf");
ClemensHostImGui::FontSetup(appdata->imguiUnicodeRanges, g_systemFontLoBuffer,
g_systemFontHiBuffer);
ClemensHostAssets::initialize();
g_Host = new ClemensStartupView(appdata->config);
}
static void onFrame(void *userdata) {
auto *appdata = reinterpret_cast<SharedAppData *>(userdata);
const int frameWidth = sapp_width();
const int frameHeight = sapp_height();
uint64_t deltaTicks = stm_laptime(&g_lastTime);
double deltaTime = stm_sec(deltaTicks);
bool exitApp = g_interop.exitApp;
simgui_frame_desc_t imguiFrameDesc = {};
imguiFrameDesc.delta_time = deltaTime;
imguiFrameDesc.dpi_scale = 1.0f;
imguiFrameDesc.width = frameWidth;
imguiFrameDesc.height = frameHeight;
simgui_new_frame(imguiFrameDesc);
if (g_Host) {
g_interop.mouseLock = sapp_mouse_locked();
g_interop.mouseShow = sapp_mouse_shown();
g_interop.poweredOn = false;
auto nextViewType = g_Host->frame(frameWidth, frameHeight, deltaTime, g_interop);
sapp_lock_mouse(g_interop.mouseLock);
if (g_interop.mouseShow != sapp_mouse_shown()) {
sapp_show_mouse(g_interop.mouseShow);
}
if (g_interop.action == ClemensHostInterop::PasteFromClipboard) {
// this is separate from ImGui's clipboard support so that the emulator controls when
// it receives clipboard data
g_Host->pasteText(sapp_get_clipboard_string(), kClipboardTextLimit);
}
g_interop.action = ClemensHostInterop::None;
exitApp = g_interop.exitApp;
clemens_host_update();
if (nextViewType != g_Host->getViewType()) {
auto *oldHost = g_Host;
switch (nextViewType) {
case ClemensHostView::ViewType::Startup:
g_Host = new ClemensStartupView(appdata->config);
break;
case ClemensHostView::ViewType::Main: {
g_Host = new ClemensFrontend(appdata->config, g_systemFontLoBuffer,
g_systemFontHiBuffer);
break;
}
default:
g_Host = nullptr;
CK_ASSERT(false);
exitApp = true;
break;
}
if (oldHost) {
delete oldHost;
}
}
if (g_interop.viewWidth && g_interop.viewHeight) {
// TODO: set window size
g_interop.viewWidth = 0;
g_interop.viewHeight = 0;
}
}
sg_begin_default_pass(&g_sgPassAction, frameWidth, frameHeight);
simgui_render();
sg_end_pass();
sg_commit();
if (exitApp) {
sapp_request_quit();
}
}
static void doHostInputEvent(struct ClemensInputEvent &clemInput, uint32_t modifiers) {
if (clemInput.type == kClemensInputType_None)
return;
if (modifiers & SAPP_MODIFIER_CAPS) {
g_ADBKeyToggleMask |= CLEM_ADB_KEYB_TOGGLE_CAPS_LOCK;
} else {
g_ADBKeyToggleMask &= ~CLEM_ADB_KEYB_TOGGLE_CAPS_LOCK;
}
clemInput.adb_key_toggle_mask = g_ADBKeyToggleMask;
if (g_Host)
g_Host->input(clemInput);
}
static void onEvent(const sapp_event *evt, void *) {
struct ClemensInputEvent clemInput {};
simgui_handle_event(evt);
sapp_keycode keycode;
bool doDownEventOnKeyUp = false;
switch (evt->type) {
case SAPP_EVENTTYPE_UNFOCUSED:
if (g_Host)
g_Host->lostFocus();
break;
case SAPP_EVENTTYPE_FOCUSED:
if (g_Host)
g_Host->gainFocus();
break;
case SAPP_EVENTTYPE_KEY_DOWN:
keycode = onKeyDown(evt);
if (keycode != SAPP_KEYCODE_INVALID) {
clemInput.value_a = g_sokolToADBKey[keycode];
clemInput.type = kClemensInputType_KeyDown;
}
break;
case SAPP_EVENTTYPE_KEY_UP:
keycode = onKeyUp(evt, &doDownEventOnKeyUp);
if (keycode != SAPP_KEYCODE_INVALID) {
clemInput.value_a = g_sokolToADBKey[keycode];
}
if (doDownEventOnKeyUp) {
printf("Do ESCAPE DOWN\n");
// on lost key down events, emulate a key tap.
if (keycode != SAPP_KEYCODE_INVALID) {
clemInput.type = kClemensInputType_KeyDown;
}
doHostInputEvent(clemInput, evt->modifiers);
}
if (keycode != SAPP_KEYCODE_INVALID) {
clemInput.type = kClemensInputType_KeyUp;
}
break;
case SAPP_EVENTTYPE_MOUSE_DOWN:
clemInput.type = kClemensInputType_MouseButtonDown;
if (evt->mouse_button == SAPP_MOUSEBUTTON_LEFT) {
clemInput.value_a |= 0x01;
clemInput.value_b |= 0x01;
}
break;
case SAPP_EVENTTYPE_MOUSE_UP:
clemInput.type = kClemensInputType_MouseButtonUp;
if (evt->mouse_button == SAPP_MOUSEBUTTON_LEFT) {
clemInput.value_a |= 0x01;
clemInput.value_b |= 0x01;
}
break;
case SAPP_EVENTTYPE_MOUSE_MOVE:
clemInput.type = kClemensInputType_MouseMove;
clemInput.value_a = (int16_t)(std::floor(evt->mouse_dx));
clemInput.value_b = (int16_t)(std::floor(evt->mouse_dy));
break;
default:
clemInput.type = kClemensInputType_None;
break;
}
doHostInputEvent(clemInput, evt->modifiers);
}
static void onCleanup(void *userdata) {
auto *appdata = reinterpret_cast<SharedAppData *>(userdata);
if (g_Host) {
delete g_Host;
g_Host = nullptr;
}
#if defined(CLEMENS_PLATFORM_WINDOWS)
CoUninitialize();
#endif
delete appdata;
ClemensHostAssets::terminate();
spdlog::shutdown();
simgui_shutdown();
sg_shutdown();
clemens_host_terminate();
}
sapp_desc sokol_main(int argc, char *argv[]) {
sapp_desc sapp = {};
// TODO: investigate other locales based on localization features
if (const char *loc = std::setlocale(LC_ALL, "en_US.UTF-8")) {
fprintf(stdout, "locale: %s\n", loc);
}
spdlog::set_level(spdlog::level::info);
spdlog::flush_on(spdlog::level::err);
spdlog::info("Setting up host frameworks");
#if defined(CLEMENS_APP_ICONS_ARE_RESIDENT)
#endif
SharedAppData *appdata = new SharedAppData(argc, argv);
if (appdata->config.viewMode == ClemensConfiguration::ViewMode::Fullscreen) {
sapp.fullscreen = true;
} else {
sapp.fullscreen = false;
}
sapp.user_data = appdata;
sapp.height = 900;
sapp.width = 1440;
sapp.init_userdata_cb = &onInit;
sapp.frame_userdata_cb = &onFrame;
sapp.cleanup_userdata_cb = &onCleanup;
sapp.event_userdata_cb = &onEvent;
sapp.window_title = "Clemens IIGS";
sapp.win32_console_create = true;
sapp.win32_console_attach = true;
sapp.logger.func = sokolLogger;
sapp.clipboard_size = kClipboardTextLimit;
loadAppIcon(&sapp.icon.images[0], app_icon_cinekine_Clemens_16_png,
app_icon_cinekine_Clemens_16_png_len);
loadAppIcon(&sapp.icon.images[1], app_icon_cinekine_Clemens_32_png,
app_icon_cinekine_Clemens_32_png_len);
loadAppIcon(&sapp.icon.images[2], app_icon_cinekine_Clemens_64_png,
app_icon_cinekine_Clemens_64_png_len);
loadAppIcon(&sapp.icon.images[3], app_icon_cinekine_Clemens_128_png,
app_icon_cinekine_Clemens_128_png_len);
return sapp;
}