File: events.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (1595 lines) | stat: -rw-r--r-- 54,753 bytes parent folder | download | duplicates (2)
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// Allow use of stuff in <time.h>
#define FORBIDDEN_SYMBOL_EXCEPTION_time_h

// Disable printf override in common/forbidden.h to avoid
// clashes with log.h from the Android SDK.
// That header file uses
//   __attribute__ ((format(printf, 3, 4)))
// which gets messed up by our override mechanism; this could
// be avoided by either changing the Android SDK to use the equally
// legal and valid
//   __attribute__ ((format(__printf__, 3, 4)))
// or by refining our printf override to use a varadic macro
// (which then wouldn't be portable, though).
// Anyway, for now we just disable the printf override globally
// for the Android port
#define FORBIDDEN_SYMBOL_EXCEPTION_printf

#include <android/input.h>

#include "backends/platform/android/android.h"
#include "backends/graphics/android/android-graphics.h"
#include "backends/platform/android/jni-android.h"

// floating point. use sparingly
template<class T>
static inline T scalef(T in, float numerator, float denominator) {
	return static_cast<float>(in) * numerator / denominator;
}

// analog joystick axis id (for internal use) - Should match the logic in ScummVMEvents.java
enum {
	// auxiliary movement axis bitflags
	JE_JOY_AXIS_X_bf        = 0x01, // (0x01 << 0)
	JE_JOY_AXIS_Y_bf        = 0x02, // (0x01 << 1)
	JE_JOY_AXIS_HAT_X_bf    = 0x04, // (0x01 << 2)
	JE_JOY_AXIS_HAT_Y_bf    = 0x08, // (0x01 << 3)
	JE_JOY_AXIS_Z_bf        = 0x10, // (0x01 << 4)
	JE_JOY_AXIS_RZ_bf       = 0x20, // (0x01 << 5)
	JE_JOY_AXIS_LTRIGGER_bf = 0x40, // (0x01 << 6)
	JE_JOY_AXIS_RTRIGGER_bf = 0x80  // (0x01 << 7)
};

// event type
enum {
	JE_SYS_KEY = 0,
	JE_KEY = 1,
	JE_DPAD = 2,
	JE_DOWN = 3,
	JE_SCROLL = 4,
	JE_TAP = 5,
	JE_DOUBLE_TAP = 6,
	JE_MULTI = 7,
	JE_BALL = 8,
	JE_LMB_DOWN = 9,
	JE_LMB_UP = 10,
	JE_RMB_DOWN = 11,
	JE_RMB_UP = 12,
	JE_MOUSE_MOVE = 13,
	JE_GAMEPAD = 14,
	JE_JOYSTICK = 15,
	JE_MMB_DOWN = 16,
	JE_MMB_UP = 17,
	JE_BMB_DOWN = 18,
	JE_BMB_UP = 19,
	JE_FMB_DOWN = 20,
	JE_FMB_UP = 21,
	JE_MOUSE_WHEEL_UP = 22,
	JE_MOUSE_WHEEL_DOWN = 23,
	JE_TV_REMOTE = 24,
	JE_QUIT = 0x1000,
	JE_MENU = 0x1001
};

// meta modifier
enum {
	AMETA_CTRL_MASK  = AMETA_CTRL_ON  | AMETA_CTRL_LEFT_ON  | AMETA_CTRL_RIGHT_ON,
	AMETA_META_MASK  = AMETA_META_ON  | AMETA_META_LEFT_ON  | AMETA_META_RIGHT_ON,
	AMETA_SHIFT_MASK = AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON,
	AMETA_ALT_MASK   = AMETA_ALT_ON   | AMETA_ALT_LEFT_ON   | AMETA_ALT_RIGHT_ON
};

// map android key codes to our kbd codes (common/keyboard.h)
static const Common::KeyCode jkeymap[] = {
	Common::KEYCODE_INVALID, // AKEYCODE_UNKNOWN
	Common::KEYCODE_LEFTSOFT, // AKEYCODE_SOFT_LEFT
	Common::KEYCODE_RIGHTSOFT, // AKEYCODE_SOFT_RIGHT
	Common::KEYCODE_AC_HOME, // AKEYCODE_HOME
	Common::KEYCODE_AC_BACK, // AKEYCODE_BACK
	Common::KEYCODE_CALL, // AKEYCODE_CALL
	Common::KEYCODE_HANGUP, // AKEYCODE_ENDCALL
	Common::KEYCODE_0, // AKEYCODE_0
	Common::KEYCODE_1, // AKEYCODE_1
	Common::KEYCODE_2, // AKEYCODE_2
	Common::KEYCODE_3, // AKEYCODE_3
	Common::KEYCODE_4, // AKEYCODE_4
	Common::KEYCODE_5, // AKEYCODE_5
	Common::KEYCODE_6, // AKEYCODE_6
	Common::KEYCODE_7, // AKEYCODE_7
	Common::KEYCODE_8, // AKEYCODE_8
	Common::KEYCODE_9, // AKEYCODE_9
	Common::KEYCODE_ASTERISK, // AKEYCODE_STAR
	Common::KEYCODE_HASH, // AKEYCODE_POUND
	Common::KEYCODE_UP, // AKEYCODE_DPAD_UP
	Common::KEYCODE_DOWN, // AKEYCODE_DPAD_DOWN
	Common::KEYCODE_LEFT, // AKEYCODE_DPAD_LEFT
	Common::KEYCODE_RIGHT, // AKEYCODE_DPAD_RIGHT
	Common::KEYCODE_SELECT, // AKEYCODE_DPAD_CENTER
	Common::KEYCODE_VOLUMEUP, // AKEYCODE_VOLUME_UP
	Common::KEYCODE_VOLUMEDOWN, // AKEYCODE_VOLUME_DOWN
	Common::KEYCODE_POWER, // AKEYCODE_POWER
	Common::KEYCODE_CAMERA, // AKEYCODE_CAMERA
	Common::KEYCODE_CLEAR, // AKEYCODE_CLEAR
	Common::KEYCODE_a, // AKEYCODE_A
	Common::KEYCODE_b, // AKEYCODE_B
	Common::KEYCODE_c, // AKEYCODE_C
	Common::KEYCODE_d, // AKEYCODE_D
	Common::KEYCODE_e, // AKEYCODE_E
	Common::KEYCODE_f, // AKEYCODE_F
	Common::KEYCODE_g, // AKEYCODE_G
	Common::KEYCODE_h, // AKEYCODE_H
	Common::KEYCODE_i, // AKEYCODE_I
	Common::KEYCODE_j, // AKEYCODE_J
	Common::KEYCODE_k, // AKEYCODE_K
	Common::KEYCODE_l, // AKEYCODE_L
	Common::KEYCODE_m, // AKEYCODE_M
	Common::KEYCODE_n, // AKEYCODE_N
	Common::KEYCODE_o, // AKEYCODE_O
	Common::KEYCODE_p, // AKEYCODE_P
	Common::KEYCODE_q, // AKEYCODE_Q
	Common::KEYCODE_r, // AKEYCODE_R
	Common::KEYCODE_s, // AKEYCODE_S
	Common::KEYCODE_t, // AKEYCODE_T
	Common::KEYCODE_u, // AKEYCODE_U
	Common::KEYCODE_v, // AKEYCODE_V
	Common::KEYCODE_w, // AKEYCODE_W
	Common::KEYCODE_x, // AKEYCODE_X
	Common::KEYCODE_y, // AKEYCODE_Y
	Common::KEYCODE_z, // AKEYCODE_Z
	Common::KEYCODE_COMMA, // AKEYCODE_COMMA
	Common::KEYCODE_PERIOD, // AKEYCODE_PERIOD
	Common::KEYCODE_LALT, // AKEYCODE_ALT_LEFT
	Common::KEYCODE_RALT, // AKEYCODE_ALT_RIGHT
	Common::KEYCODE_LSHIFT, // AKEYCODE_SHIFT_LEFT
	Common::KEYCODE_RSHIFT, // AKEYCODE_SHIFT_RIGHT
	Common::KEYCODE_TAB, // AKEYCODE_TAB
	Common::KEYCODE_SPACE, // AKEYCODE_SPACE
	Common::KEYCODE_LCTRL, // AKEYCODE_SYM
	Common::KEYCODE_WWW, // AKEYCODE_EXPLORER
	Common::KEYCODE_MAIL, // AKEYCODE_ENVELOPE
	Common::KEYCODE_RETURN, // AKEYCODE_ENTER
	Common::KEYCODE_BACKSPACE, // AKEYCODE_DEL
	Common::KEYCODE_BACKQUOTE, // AKEYCODE_GRAVE
	Common::KEYCODE_MINUS, // AKEYCODE_MINUS
	Common::KEYCODE_EQUALS, // AKEYCODE_EQUALS
	Common::KEYCODE_LEFTPAREN, // AKEYCODE_LEFT_BRACKET
	Common::KEYCODE_RIGHTPAREN, // AKEYCODE_RIGHT_BRACKET
	Common::KEYCODE_BACKSLASH, // AKEYCODE_BACKSLASH
	Common::KEYCODE_SEMICOLON, // AKEYCODE_SEMICOLON
	Common::KEYCODE_QUOTE, // AKEYCODE_APOSTROPHE
	Common::KEYCODE_SLASH, // AKEYCODE_SLASH
	Common::KEYCODE_AT, // AKEYCODE_AT
	Common::KEYCODE_INVALID, // AKEYCODE_NUM
	Common::KEYCODE_INVALID, // AKEYCODE_HEADSETHOOK
	Common::KEYCODE_INVALID, // AKEYCODE_FOCUS
	Common::KEYCODE_PLUS, // AKEYCODE_PLUS
	Common::KEYCODE_MENU, // AKEYCODE_MENU
	Common::KEYCODE_INVALID, // AKEYCODE_NOTIFICATION
	Common::KEYCODE_AC_SEARCH, // AKEYCODE_SEARCH
	Common::KEYCODE_AUDIOPLAYPAUSE, // AKEYCODE_MEDIA_PLAY_PAUSE
	Common::KEYCODE_AUDIOSTOP, // AKEYCODE_MEDIA_STOP
	Common::KEYCODE_AUDIONEXT, // AKEYCODE_MEDIA_NEXT
	Common::KEYCODE_AUDIOPREV, // AKEYCODE_MEDIA_PREVIOUS
	Common::KEYCODE_AUDIOREWIND, // AKEYCODE_MEDIA_REWIND
	Common::KEYCODE_AUDIOFASTFORWARD, // AKEYCODE_MEDIA_FAST_FORWARD
	Common::KEYCODE_MUTE, // AKEYCODE_MUTE
	Common::KEYCODE_PAGEUP, // AKEYCODE_PAGE_UP
	Common::KEYCODE_PAGEDOWN, // AKEYCODE_PAGE_DOWN
	Common::KEYCODE_INVALID, // AKEYCODE_PICTSYMBOLS
	Common::KEYCODE_INVALID, // AKEYCODE_SWITCH_CHARSET
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_A
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_B
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_C
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_X
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_Y
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_Z
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_L1
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_R1
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_L2
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_R2
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_THUMBL
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_THUMBR
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_START
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_SELECT
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_MODE
	Common::KEYCODE_ESCAPE, // AKEYCODE_ESCAPE
	Common::KEYCODE_DELETE, // AKEYCODE_FORWARD_DEL
	Common::KEYCODE_LCTRL, // AKEYCODE_CTRL_LEFT
	Common::KEYCODE_RCTRL, // AKEYCODE_CTRL_RIGHT
	Common::KEYCODE_CAPSLOCK, // AKEYCODE_CAPS_LOCK
	Common::KEYCODE_SCROLLOCK, // AKEYCODE_SCROLL_LOCK
	Common::KEYCODE_LSUPER, // AKEYCODE_META_LEFT
	Common::KEYCODE_RSUPER, // AKEYCODE_META_RIGHT
	Common::KEYCODE_INVALID, // AKEYCODE_FUNCTION
	Common::KEYCODE_SYSREQ, // AKEYCODE_SYSRQ
	Common::KEYCODE_BREAK, // AKEYCODE_BREAK
	Common::KEYCODE_HOME, // AKEYCODE_MOVE_HOME
	Common::KEYCODE_END, // AKEYCODE_MOVE_END
	Common::KEYCODE_INSERT, // AKEYCODE_INSERT
	Common::KEYCODE_AC_FORWARD, // AKEYCODE_FORWARD
	Common::KEYCODE_AUDIOPLAY, // AKEYCODE_MEDIA_PLAY
	Common::KEYCODE_AUDIOPAUSE, // AKEYCODE_MEDIA_PAUSE
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_CLOSE
	Common::KEYCODE_EJECT, // AKEYCODE_MEDIA_EJECT
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_RECORD
	Common::KEYCODE_F1, // AKEYCODE_F1
	Common::KEYCODE_F2, // AKEYCODE_F2
	Common::KEYCODE_F3, // AKEYCODE_F3
	Common::KEYCODE_F4, // AKEYCODE_F4
	Common::KEYCODE_F5, // AKEYCODE_F5
	Common::KEYCODE_F6, // AKEYCODE_F6
	Common::KEYCODE_F7, // AKEYCODE_F7
	Common::KEYCODE_F8, // AKEYCODE_F8
	Common::KEYCODE_F9, // AKEYCODE_F9
	Common::KEYCODE_F10, // AKEYCODE_F10
	Common::KEYCODE_F11, // AKEYCODE_F11
	Common::KEYCODE_F12, // AKEYCODE_F12
	Common::KEYCODE_NUMLOCK, // AKEYCODE_NUM_LOCK
	Common::KEYCODE_KP0, // AKEYCODE_NUMPAD_0
	Common::KEYCODE_KP1, // AKEYCODE_NUMPAD_1
	Common::KEYCODE_KP2, // AKEYCODE_NUMPAD_2
	Common::KEYCODE_KP3, // AKEYCODE_NUMPAD_3
	Common::KEYCODE_KP4, // AKEYCODE_NUMPAD_4
	Common::KEYCODE_KP5, // AKEYCODE_NUMPAD_5
	Common::KEYCODE_KP6, // AKEYCODE_NUMPAD_6
	Common::KEYCODE_KP7, // AKEYCODE_NUMPAD_7
	Common::KEYCODE_KP8, // AKEYCODE_NUMPAD_8
	Common::KEYCODE_KP9, // AKEYCODE_NUMPAD_9
	Common::KEYCODE_KP_DIVIDE, // AKEYCODE_NUMPAD_DIVIDE
	Common::KEYCODE_KP_MULTIPLY, // AKEYCODE_NUMPAD_MULTIPLY
	Common::KEYCODE_KP_MINUS, // AKEYCODE_NUMPAD_SUBTRACT
	Common::KEYCODE_KP_PLUS, // AKEYCODE_NUMPAD_ADD
	Common::KEYCODE_KP_PERIOD, // AKEYCODE_NUMPAD_DOT
	Common::KEYCODE_INVALID, // AKEYCODE_NUMPAD_COMMA
	Common::KEYCODE_KP_ENTER, // AKEYCODE_NUMPAD_ENTER
	Common::KEYCODE_KP_EQUALS, // AKEYCODE_NUMPAD_EQUALS
	Common::KEYCODE_INVALID, // AKEYCODE_NUMPAD_LEFT_PAREN
	Common::KEYCODE_INVALID, // AKEYCODE_NUMPAD_RIGHT_PAREN
	Common::KEYCODE_INVALID, // AKEYCODE_VOLUME_MUTE
	Common::KEYCODE_INVALID, // AKEYCODE_INFO
	Common::KEYCODE_INVALID, // AKEYCODE_CHANNEL_UP
	Common::KEYCODE_INVALID, // AKEYCODE_CHANNEL_DOWN
	Common::KEYCODE_INVALID, // AKEYCODE_ZOOM_IN
	Common::KEYCODE_INVALID, // AKEYCODE_ZOOM_OUT
	Common::KEYCODE_INVALID, // AKEYCODE_TV
	Common::KEYCODE_INVALID, // AKEYCODE_WINDOW
	Common::KEYCODE_INVALID, // AKEYCODE_GUIDE
	Common::KEYCODE_INVALID, // AKEYCODE_DVR
	Common::KEYCODE_AC_BOOKMARKS, // AKEYCODE_BOOKMARK
	Common::KEYCODE_INVALID, // AKEYCODE_CAPTIONS
	Common::KEYCODE_INVALID, // AKEYCODE_SETTINGS
	Common::KEYCODE_INVALID, // AKEYCODE_TV_POWER
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT
	Common::KEYCODE_INVALID, // AKEYCODE_STB_POWER
	Common::KEYCODE_INVALID, // AKEYCODE_STB_INPUT
	Common::KEYCODE_INVALID, // AKEYCODE_AVR_POWER
	Common::KEYCODE_INVALID, // AKEYCODE_AVR_INPUT
	Common::KEYCODE_INVALID, // AKEYCODE_PROG_RED
	Common::KEYCODE_INVALID, // AKEYCODE_PROG_GREEN
	Common::KEYCODE_INVALID, // AKEYCODE_PROG_YELLOW
	Common::KEYCODE_INVALID, // AKEYCODE_PROG_BLUE
	Common::KEYCODE_INVALID, // AKEYCODE_APP_SWITCH
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_1
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_2
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_3
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_4
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_5
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_6
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_7
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_8
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_9
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_10
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_11
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_12
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_13
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_14
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_15
	Common::KEYCODE_INVALID, // AKEYCODE_BUTTON_16
	Common::KEYCODE_INVALID, // AKEYCODE_LANGUAGE_SWITCH
	Common::KEYCODE_INVALID, // AKEYCODE_MANNER_MODE
	Common::KEYCODE_INVALID, // AKEYCODE_3D_MODE
	Common::KEYCODE_INVALID, // AKEYCODE_CONTACTS
	Common::KEYCODE_INVALID, // AKEYCODE_CALENDAR
	Common::KEYCODE_INVALID, // AKEYCODE_MUSIC
	Common::KEYCODE_CALCULATOR, // AKEYCODE_CALCULATOR
	Common::KEYCODE_INVALID, // AKEYCODE_ZENKAKU_HANKAKU
	Common::KEYCODE_INVALID, // AKEYCODE_EISU
	Common::KEYCODE_INVALID, // AKEYCODE_MUHENKAN
	Common::KEYCODE_INVALID, // AKEYCODE_HENKAN
	Common::KEYCODE_INVALID, // AKEYCODE_KATAKANA_HIRAGANA
	Common::KEYCODE_INVALID, // AKEYCODE_YEN
	Common::KEYCODE_INVALID, // AKEYCODE_RO
	Common::KEYCODE_INVALID, // AKEYCODE_KANA
	Common::KEYCODE_INVALID, // AKEYCODE_ASSIST
	Common::KEYCODE_INVALID, // AKEYCODE_BRIGHTNESS_DOWN
	Common::KEYCODE_INVALID, // AKEYCODE_BRIGHTNESS_UP
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_AUDIO_TRACK
	Common::KEYCODE_SLEEP, // AKEYCODE_SLEEP
	Common::KEYCODE_INVALID, // AKEYCODE_WAKEUP
	Common::KEYCODE_INVALID, // AKEYCODE_PAIRING
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_TOP_MENU
	Common::KEYCODE_INVALID, // AKEYCODE_11
	Common::KEYCODE_INVALID, // AKEYCODE_12
	Common::KEYCODE_INVALID, // AKEYCODE_LAST_CHANNEL
	Common::KEYCODE_INVALID, // AKEYCODE_TV_DATA_SERVICE
	Common::KEYCODE_INVALID, // AKEYCODE_VOICE_ASSIST
	Common::KEYCODE_INVALID, // AKEYCODE_TV_RADIO_SERVICE
	Common::KEYCODE_INVALID, // AKEYCODE_TV_TELETEXT
	Common::KEYCODE_INVALID, // AKEYCODE_TV_NUMBER_ENTRY
	Common::KEYCODE_INVALID, // AKEYCODE_TV_TERRESTRIAL_ANALOG
	Common::KEYCODE_INVALID, // AKEYCODE_TV_TERRESTRIAL_DIGITAL
	Common::KEYCODE_INVALID, // AKEYCODE_TV_SATELLITE
	Common::KEYCODE_INVALID, // AKEYCODE_TV_SATELLITE_BS
	Common::KEYCODE_INVALID, // AKEYCODE_TV_SATELLITE_CS
	Common::KEYCODE_INVALID, // AKEYCODE_TV_SATELLITE_SERVICE
	Common::KEYCODE_INVALID, // AKEYCODE_TV_NETWORK
	Common::KEYCODE_INVALID, // AKEYCODE_TV_ANTENNA_CABLE
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_HDMI_1
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_HDMI_2
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_HDMI_3
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_HDMI_4
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_COMPOSITE_1
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_COMPOSITE_2
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_COMPONENT_1
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_COMPONENT_2
	Common::KEYCODE_INVALID, // AKEYCODE_TV_INPUT_VGA_1
	Common::KEYCODE_INVALID, // AKEYCODE_TV_AUDIO_DESCRIPTION
	Common::KEYCODE_INVALID, // AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP
	Common::KEYCODE_INVALID, // AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN
	Common::KEYCODE_INVALID, // AKEYCODE_TV_ZOOM_MODE
	Common::KEYCODE_INVALID, // AKEYCODE_TV_CONTENTS_MENU
	Common::KEYCODE_INVALID, // AKEYCODE_TV_MEDIA_CONTEXT_MENU
	Common::KEYCODE_INVALID, // AKEYCODE_TV_TIMER_PROGRAMMING
	Common::KEYCODE_HELP, // AKEYCODE_HELP
	Common::KEYCODE_INVALID, // AKEYCODE_NAVIGATE_PREVIOUS
	Common::KEYCODE_INVALID, // AKEYCODE_NAVIGATE_NEXT
	Common::KEYCODE_INVALID, // AKEYCODE_NAVIGATE_IN
	Common::KEYCODE_INVALID, // AKEYCODE_NAVIGATE_OUT
	Common::KEYCODE_INVALID, // AKEYCODE_STEM_PRIMARY
	Common::KEYCODE_INVALID, // AKEYCODE_STEM_1
	Common::KEYCODE_INVALID, // AKEYCODE_STEM_2
	Common::KEYCODE_INVALID, // AKEYCODE_STEM_3
	Common::KEYCODE_INVALID, // AKEYCODE_DPAD_UP_LEFT
	Common::KEYCODE_INVALID, // AKEYCODE_DPAD_DOWN_LEFT
	Common::KEYCODE_INVALID, // AKEYCODE_DPAD_UP_RIGHT
	Common::KEYCODE_INVALID, // AKEYCODE_DPAD_DOWN_RIGHT
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_SKIP_FORWARD
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_SKIP_BACKWARD
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_STEP_FORWARD
	Common::KEYCODE_INVALID, // AKEYCODE_MEDIA_STEP_BACKWARD
	Common::KEYCODE_INVALID, // AKEYCODE_SOFT_SLEEP
	Common::KEYCODE_CUT, // AKEYCODE_CUT
	Common::KEYCODE_COPY, // AKEYCODE_COPY
	Common::KEYCODE_PASTE // AKEYCODE_PASTE
};

void OSystem_Android::pushEvent(int type, int arg1, int arg2, int arg3,
                                          int arg4, int arg5, int arg6) {
	Common::Event ev0;

	switch (type) {
	case JE_SYS_KEY:
		// fall through
	case JE_KEY:
		switch (arg1) {
		case AKEY_EVENT_ACTION_DOWN:
			ev0.type = Common::EVENT_KEYDOWN;
			break;

		case AKEY_EVENT_ACTION_UP:
			ev0.type = Common::EVENT_KEYUP;
			break;

		default:
			LOGE("unhandled jaction on key: %d", arg1);
			return;
		}

		if (arg2 < 1 || arg2 > ARRAYSIZE(jkeymap)) {
			if (arg3 < 1) {
				LOGE("received invalid keycode: %d (%d)", arg2, arg3);
				return;
			} else {
				// lets bet on the ascii code
				ev0.kbd.keycode = Common::KEYCODE_INVALID;
			}
		} else {
//			LOGD("Received JE_KEY keycode: %d", arg3);
			ev0.kbd.keycode = jkeymap[arg2];
		}

		if (arg5 > 0) {
			ev0.kbdRepeat = true;
		}

//		// HACK: Special case for when the arrow keys on the virtual keyboard are pressed and held down
//		// This emulates what happens when the arrow keys on a physical keyboard are pressed and held down
//		// In the case of the physical keyboard Android keeps sending successive EVENT_KEYDOWN, EVENT_KEYUP events, non-"repeated"
//		if (ev0.kbdRepeat
//		    && ev0.type == Common::EVENT_KEYDOWN
//		    && (ev0.kbd.keycode == Common::KEYCODE_UP
//		        || ev0.kbd.keycode == Common::KEYCODE_DOWN
//		        || ev0.kbd.keycode == Common::KEYCODE_LEFT
//		        || ev0.kbd.keycode == Common::KEYCODE_RIGHT)) {
//			switch (ev0.kbd.keycode) {
//			case Common::KEYCODE_UP:
//				ev0.kbd.ascii = Common::KEYCODE_UP;
//				break;
//
//			case Common::KEYCODE_DOWN:
//				ev0.kbd.ascii = Common::KEYCODE_DOWN;
//				break;
//
//			case Common::KEYCODE_RIGHT:
//				ev0.kbd.ascii = Common::KEYCODE_RIGHT;
//				break;
//
//			case Common::KEYCODE_LEFT:
//				ev0.kbd.ascii = Common::KEYCODE_LEFT;
//				break;
//			}
//			// TODO Maybe only handle 1 every X such events to lower the spam on the queue of these very fast up/down events?
//			ev0.kbdRepeat = false;
//			Common::Event ev1 = ev0;
//			ev1.type = Common::EVENT_KEYUP;
////			LOGD("JE_KEY pushing rep event type: %d kbdcode: %d ascii: %d flags: %d repeats: %d", ev1.type, ev1.kbd.keycode, ev1.kbd.ascii, ev1.kbd.flags, ev1.kbdRepeat? 1: 0);
////			LOGD("JE_KEY pushing rep event type: %d kbdcode: %d ascii: %d flags: %d repeats: %d", ev0.type, ev0.kbd.keycode, ev0.kbd.ascii, ev0.kbd.flags, ev0.kbdRepeat? 1: 0);
//			pushEvent(ev1, ev0);
//			return;
//		}

		// map special keys to 'our' ascii codes
		switch (ev0.kbd.keycode) {
		case Common::KEYCODE_BACKSPACE:
//			LOGD("received BACKSPACE");
			ev0.kbd.ascii = Common::ASCII_BACKSPACE;
			break;

		case Common::KEYCODE_TAB:
			ev0.kbd.ascii = Common::ASCII_TAB;
			break;

		case Common::KEYCODE_RETURN:
			ev0.kbd.ascii = Common::ASCII_RETURN;
			break;

		case Common::KEYCODE_ESCAPE:
			ev0.kbd.ascii = Common::ASCII_ESCAPE;
			break;

		case Common::KEYCODE_SPACE:
			ev0.kbd.ascii = Common::ASCII_SPACE;
			break;

		case Common::KEYCODE_F1:
			ev0.kbd.ascii = Common::ASCII_F1;
			break;

		case Common::KEYCODE_F2:
			ev0.kbd.ascii = Common::ASCII_F2;
			break;

		case Common::KEYCODE_F3:
			ev0.kbd.ascii = Common::ASCII_F3;
			break;

		case Common::KEYCODE_F4:
			ev0.kbd.ascii = Common::ASCII_F4;
			break;

		case Common::KEYCODE_F5:
			ev0.kbd.ascii = Common::ASCII_F5;
			break;

		case Common::KEYCODE_F6:
			ev0.kbd.ascii = Common::ASCII_F6;
			break;

		case Common::KEYCODE_F7:
			ev0.kbd.ascii = Common::ASCII_F7;
			break;

		case Common::KEYCODE_F8:
			ev0.kbd.ascii = Common::ASCII_F8;
			break;

		case Common::KEYCODE_F9:
			ev0.kbd.ascii = Common::ASCII_F9;
			break;

		case Common::KEYCODE_F10:
			ev0.kbd.ascii = Common::ASCII_F10;
			break;

		case Common::KEYCODE_F11:
			ev0.kbd.ascii = Common::ASCII_F11;
			break;

		case Common::KEYCODE_F12:
			ev0.kbd.ascii = Common::ASCII_F12;
			break;

		case Common::KEYCODE_UP:
			// Set to match what a physical keyboard sends as ascii code for the arrow keys
			ev0.kbd.ascii = Common::KEYCODE_UP;
			break;

		case Common::KEYCODE_DOWN:
			// Set to match what a physical keyboard sends as ascii code for the arrow keys
			ev0.kbd.ascii = Common::KEYCODE_DOWN;
			break;

		case Common::KEYCODE_RIGHT:
			// Set to match what a physical keyboard sends as ascii code for the arrow keys
			ev0.kbd.ascii = Common::KEYCODE_RIGHT;
			break;

		case Common::KEYCODE_LEFT:
			// Set to match what a physical keyboard sends as ascii code for the arrow keys
			ev0.kbd.ascii = Common::KEYCODE_LEFT;
			break;

		default:
			ev0.kbd.ascii = arg3;
			break;
		}

		// arg4 is the metastate of the key press event
		// check for "Shift" key modifier
		if (arg4 & AMETA_SHIFT_MASK) {
			ev0.kbd.flags |= Common::KBD_SHIFT;
		}

		// We revert the commit to disable the Alt modifier
		// TODO revisit this old comment from commit https://github.com/scummvm/scummvm/commit/bfecb37501b6fdc35f2802216db5fb2b0e54b8ee
		//      for possible issues with physical keyboards and Fn key combos.
		//      It still seems like a bad idea to disable Alt key combos over Fn key combos
		//      and our keymapper should be able to handle this issue anyway
		//      (since it explicitly shows what key combination results from pressing keys, when the user edits a key mapping)
		//      More info: Back then we were checking for JMETA_ALT (defined as 0x02) which is now NDK's AMETA_ALT_ON (0x02) (see https://developer.android.com/ndk/reference/group/input)
		//      JMETA_ALT was defined in our own enum. (see full old file: https://github.com/scummvm/scummvm/blob/bfecb37501b6fdc35f2802216db5fb2b0e54b8ee/backends/platform/android/events.cpp#L113)
		// Old comment:
		// JMETA_ALT (0x02) is Fn on physical keyboards!
		// when mapping this to ALT - as we know it from PC keyboards - all
		// Fn combos will be broken (like Fn+q, which needs to end as 1 and
		// not ALT+1). Do not want.
		//if (arg4 & JMETA_ALT)
		//	ev0.kbd.flags |= Common::KBD_ALT;
		// end of old comment --
		// check for "Alt" key modifier
		if (arg4 & (AMETA_ALT_MASK)) {
			ev0.kbd.flags |= Common::KBD_ALT;
		}

		// check for "Ctrl" key modifier (We set Sym key to also behave as Ctrl)
		if (arg4 & (AMETA_SYM_ON | AMETA_CTRL_MASK)) {
			ev0.kbd.flags |= Common::KBD_CTRL;
		}

		// check for "Meta" key modifier
		if (arg4 & (AMETA_META_MASK)) {
			ev0.kbd.flags |= Common::KBD_META;
		}

		//  check for CAPS key modifier
		if (arg4 & (AMETA_CAPS_LOCK_ON)) {
			ev0.kbd.flags |= Common::KBD_CAPS;
		}

		//  check for NUM Lock key modifier
		if (arg4 & (AMETA_NUM_LOCK_ON)) {
			ev0.kbd.flags |= Common::KBD_NUM;
		}

		//  check for Scroll Lock key modifier
		if (arg4 & (AMETA_SCROLL_LOCK_ON)) {
			ev0.kbd.flags |= Common::KBD_SCRL;
		}

//		LOGD("JE_KEY pushing event type: %d kbdcode: %d ascii: %d flags: %d repeats: %d", ev0.type, ev0.kbd.keycode, ev0.kbd.ascii, ev0.kbd.flags, ev0.kbdRepeat? 1: 0);
		pushEvent(ev0);
		break;

	case JE_DPAD:
		// For now, this behavior, emulating mouse movement and left mouse clicking here for DPAD button presses,
		// is no longer used.
		switch (arg2) {
		case AKEYCODE_DPAD_UP:
		// fall through
		case AKEYCODE_DPAD_DOWN:
		// fall through
		case AKEYCODE_DPAD_LEFT:
		// fall through
		case AKEYCODE_DPAD_RIGHT:
			// Treat as mouse movement
			if (arg1 != AKEY_EVENT_ACTION_DOWN)
				break;

			ev0.type = Common::EVENT_MOUSEMOVE;
			ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
			{
				int16 *c;
				int s;

				if (arg2 == AKEYCODE_DPAD_UP || arg2 == AKEYCODE_DPAD_DOWN) {
					c = &ev0.mouse.y;
					s = _eventScaleY;
				} else {
					c = &ev0.mouse.x;
					s = _eventScaleX;
				}

				// the longer the button held, the faster the pointer is
				// TODO put these values in some option dlg?
				int f = CLIP(arg5, 1, 8) * _dpad_scale * 100 / s;

				if (arg2 == AKEYCODE_DPAD_UP || arg2 == AKEYCODE_DPAD_LEFT) {
					*c -= f;
				} else {
					*c += f;
				}
			}
			pushEvent(ev0);
			break;

		case AKEYCODE_DPAD_CENTER:
			// Treat as mouse click (left click)
			switch (arg1) {
			case AKEY_EVENT_ACTION_DOWN:
				ev0.type = Common::EVENT_LBUTTONDOWN;
				break;

			case AKEY_EVENT_ACTION_UP:
				ev0.type = Common::EVENT_LBUTTONUP;
				break;

			default:
				LOGE("unhandled jaction on dpad key: %d", arg1);
				return;
			}

			ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
			pushEvent(ev0);
			break;
		}
		break;

	case JE_TV_REMOTE:
		switch (arg1) {
		case AKEY_EVENT_ACTION_DOWN:
			ev0.type = Common::EVENT_KEYDOWN;
			break;

		case AKEY_EVENT_ACTION_UP:
			ev0.type = Common::EVENT_KEYUP;
			break;

		default:
			LOGE("unhandled jaction on key: %d", arg1);
			return;
		}

		switch (arg2) {
		case AKEYCODE_MEDIA_FAST_FORWARD:
		// fall through
		case AKEYCODE_MEDIA_REWIND:
		// fall through
		case AKEYCODE_MEDIA_PLAY_PAUSE:
			// Treat as keyboard presses, since they have equivalent hardware keyboard keys
			ev0.kbd.keycode = jkeymap[arg2];
			if (arg5 > 0) {
				ev0.kbdRepeat = true;
			}
			break;

		 // Unfortunately CHANNEL_UP or CHANNEL_DOWN do not trigger for the Fire TV Stick remote (3rd gen)
		 // despite the documentation (https://developer.amazon.com/docs/fire-tv/remote-input.html)
		 // so there's no way as of yet to test for them.
		 // TODO Maybe enable them anyway? Should we create hardware input keys for them in the main code?
//		 case AKEYCODE_CHANNEL_UP:
//		 // fall through
//		 case AKEYCODE_CHANNEL_DOWN:
//			break;

		}

		pushEvent(ev0);
		break;

	case JE_DOWN:
//		LOGD("JE_DOWN");
		_touch_pt_down = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
		// If the cursor was outside the area (because the screen rotated) clip it
		// to not scroll several times to make the cursor appear in the area
		// Do not clamp the cursor position while rotating the screen because if the user rotated by mistake
		// rotating again will see the cursor position preserved
		if (_touch_pt_down.x > JNI::egl_surface_width) {
			_touch_pt_down.x = JNI::egl_surface_width;
		}
		if (_touch_pt_down.y > JNI::egl_surface_height) {
			_touch_pt_down.y = JNI::egl_surface_height;
		}
		_touch_pt_scroll.x = -1;
		_touch_pt_scroll.y = -1;
//		if (_touch_mode != TOUCH_MODE_TOUCHPAD) {
//			ev0.type = Common::EVENT_MOUSEMOVE;
//			ev0.mouse.x = arg1;
//			ev0.mouse.y = arg2;
//			pushEvent(ev0);
//		}
		break;

	case JE_SCROLL:
//		LOGD("JE_SCROLL");
		ev0.type = Common::EVENT_MOUSEMOVE;

		if (_touch_mode == TOUCH_MODE_TOUCHPAD) {
			if (_touch_pt_scroll.x == -1 && _touch_pt_scroll.y == -1) {
				_touch_pt_scroll.x = arg3;
				_touch_pt_scroll.y = arg4;
				return;
			}

			ev0.mouse.x = (arg3 - _touch_pt_scroll.x) * 100 / _touchpad_scale;
			ev0.mouse.y = (arg4 - _touch_pt_scroll.y) * 100 / _touchpad_scale;
			ev0.mouse += _touch_pt_down;
		} else {
			ev0.mouse.x = arg3;
			ev0.mouse.y = arg4;
		}
		ev0.relMouse.x = arg5;
		ev0.relMouse.y = arg6;

		pushEvent(ev0);
		break;

	case JE_TAP:
		// arg1 = mouse x
		// arg2 = mouse y
		// arg3 is (int)(e.getEventTime() - e.getDownTime())
//		LOGD("JE_TAP - arg3 %d", arg3);

		ev0.type = Common::EVENT_MOUSEMOVE;

		if (_touch_mode == TOUCH_MODE_TOUCHPAD) {
			ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
		} else {
			ev0.mouse.x = arg1;
			ev0.mouse.y = arg2;
		}

		{
			Common::EventType down, up;

			// Based on this check, we check how long the tap finger was held down
			// and distinguish cases for:
			//  > 1.0 seconds: Middle Mouse Button
			//  > 0.5 seconds: Right Mouse Button
			//  < 0.5 seconds: Left Mouse Button
			// Due to how these are detected we cannot have hold and move detection for any of them
			// They only act as clicks
			// Note: for hold one finger and move gesture, this is the "move cursor around" action.
			//       Thus, the simple tap and hold cannot be used for "hold left mouse button and drag" behavior.
			//       This is the reason we use "double tap and move" to emulate that specific behavior.
			// Note: This one-finger gesture cannot currently be used for "hold right mouse and drag" or "hold middle mouse and drag" either.
			//       The gesture for those uses more than one fingers (see JE_MULTI).
			// TODO This might be unwanted "alternate" behavior (better to have it as optional?)
			// TODO put these time (in milliseconds) values in some option dlg?
			if (arg3 > 1500) {
//				LOGD("JE_TAP - arg3 %d --> Middle Mouse Button", arg3);
				down = Common::EVENT_MBUTTONDOWN;
				up = Common::EVENT_MBUTTONUP;
			} else if (arg3 > 500) {
//				LOGD("JE_TAP - arg3 %d --> Right Mouse Button", arg3);
				down = Common::EVENT_RBUTTONDOWN;
				up = Common::EVENT_RBUTTONUP;
			} else {
//				LOGD("JE_TAP - arg3 %d --> Left Mouse Button", arg3);
				down = Common::EVENT_LBUTTONDOWN;
				up = Common::EVENT_LBUTTONUP;
			}

			pushDelayedTouchMouseBtnEvents();
			Common::Event ev1 = ev0;
			ev1.type = down; // mouse down
			_event_queue_lock->lock();
			uint32 originTimeForDelayedEventGrp = getMillis(true);
			if (_touch_mode != TOUCH_MODE_TOUCHPAD) {
				// In this case the mouse move is done in "direct mode"
				// ie. the cursor jumps to where the tap occurred
				// so we don't have relMouse coordinates to set for the event
				// However, in Direct Touch mode, some games seem to expect a delay 
				// between the move event and the subsequent mouse down event (eg. Curse of Monkey Island, Tony Tough)
				// Thus, this Mouse Button Down event is also sent with a delay
				_event_queue.push(ev0);
				_delayedMouseBtnDownEvent.mouse = ev1.mouse;
				_delayedMouseBtnDownEvent.type = down;
				_delayedMouseBtnDownEvent.referTimeMillis = originTimeForDelayedEventGrp;
				_delayedMouseBtnDownEvent.originTimeMillis = originTimeForDelayedEventGrp;
				_delayedMouseBtnDownEvent.delayMillis = kQueuedInputEventDelay;
				_delayedMouseBtnDownEvent.connectedType = ev0.type; // Common::EVENT_MOUSEMOVE
				_delayedMouseBtnDownEvent.connectedTypeExecuted = false;
			} else {
				_event_queue.push(ev1);
			}

			_delayedMouseBtnUpEvent.mouse = ev1.mouse;
			_delayedMouseBtnUpEvent.type = up;
			// Mouse-up event is handled with a small delay as some engines require such a delay for mouse up events (Gob, Toonstruck)
			// See bug ticket: https://bugs.scummvm.org/ticket/5942
//			LOGD("JE_TAP - VALID: ev0: mx %d my %d t: %d | ev1: mx %d my %d t: %d | ev2: mx %d my %d t: %d", ev0.mouse.x, ev0.mouse.y, ev0.type, ev1.mouse.x, ev1.mouse.y, ev1.type, ev2.mouse.x, ev2.mouse.y, ev2.type);
			_delayedMouseBtnUpEvent.referTimeMillis = originTimeForDelayedEventGrp;
			_delayedMouseBtnUpEvent.originTimeMillis = originTimeForDelayedEventGrp;
			_delayedMouseBtnUpEvent.delayMillis = kQueuedInputEventDelay;
			_delayedMouseBtnUpEvent.connectedType = ev1.type;  // down (EVENT_MBUTTONDOWN, EVENT_RBUTTONDOWN, EVENT_LBUTTONDOWN)
			_delayedMouseBtnUpEvent.connectedTypeExecuted = false;
			_event_queue_lock->unlock();
		}
		break;

	case JE_DOUBLE_TAP:
		// arg1 = mouse x
		// arg2 = mouse y
		// arg3 = AMOTION_EVENT_ACTION_DOWN, AMOTION_EVENT_ACTION_UP, AMOTION_EVENT_ACTION_MOVE
		// NOTE: Typically in a double tap event:
		//       Before the ACTION_DOWN event, we also have ALREADY pushed a JE_DOWN event (via ScummVMEvents's onDown())
		//       and then a JE_TAP event (action UP) via ScummVMEvents's onSingleTapUp().
		//       Before the ACTION_UP event, we also have ALREADY pushed a JE_DOWN event (via ScummVMEvents's onDown()).
//		LOGD("JE_DOUBLE_TAP - x: %d y: %d, arg3: %d", arg1, arg2, arg3);

		ev0.type = Common::EVENT_MOUSEMOVE;
		ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();

//		LOGD("JE_DOUBLE_TAP 2 - x: %d y: %d, type: %d, arg3: %d", ev0.mouse.x , ev0.mouse.y, ev0.type, arg3);

		{
			Common::Event ev1 = ev0;
			Common::EventType dptype = Common::EVENT_INVALID;

			switch (arg3) {
			case AMOTION_EVENT_ACTION_DOWN:
				dptype = Common::EVENT_LBUTTONDOWN;
				_touch_pt_dt.x = -1;
				_touch_pt_dt.y = -1;
				break;

			case AMOTION_EVENT_ACTION_UP:
				dptype = Common::EVENT_LBUTTONUP;
				break;

			case AMOTION_EVENT_ACTION_MOVE:
				// held and moved
				if (_touch_pt_dt.x == -1 && _touch_pt_dt.y == -1) {
//					LOGD("JE_DOUBLE_TAP MOVE UPDATE dtx = %d, dty = %d and return", arg1, arg2);
					_touch_pt_dt.x = arg1;
					_touch_pt_dt.y = arg2;
					return;
				}

				dptype = Common::EVENT_MOUSEMOVE;

				if (_touch_mode == TOUCH_MODE_TOUCHPAD) {
					ev1.mouse.x = (arg1 - _touch_pt_dt.x) * 100 / _touchpad_scale;
					ev1.mouse.y = (arg2 - _touch_pt_dt.y) * 100 / _touchpad_scale;
					ev1.mouse += _touch_pt_down;
//					LOGD("JE_DOUBLE_TAP MOVE EVENT updating coords and mouse- x: %d y: %d, arg3: %d", ev1.mouse.x , ev1.mouse.y, arg3);
				} else {
					ev1.mouse.x = arg1;
					ev1.mouse.y = arg2;
				}

				break;

			default:
				LOGE("JE_DOUBLE_TAP - unhandled jaction on double tap: %d", arg3);
				return;
			}

			ev1.type = dptype;
			if (ev1.type == Common::EVENT_LBUTTONDOWN) {
				pushDelayedTouchMouseBtnEvents();
			}
//			// Commented out: Don't do a "move mouse" pre-event on a double_tap
//			pushEvent(ev0, ev1);
//			LOGD("JE_DOUBLE_TAP - Pushing DOUBLE TAP event - x: %d y: %d, type: %d, arg3: %d", ev1.mouse.x , ev1.mouse.y, ev1.type, arg3);
			pushEvent(ev1);
		}
		break;

	case JE_MULTI:
		// Documentation: https://developer.android.com/training/gestures/multi
		// TODO also look into: https://developer.android.com/training/gestures/movement
		//      for possible tweaks to gesture detection
		// arg1 = fingers down
		// arg2 = AMOTION_EVENT_ACTION_POINTER_DOWN, AMOTION_EVENT_ACTION_POINTER_UP, CANCEL, OUTSIDE, MOVE
		//        we handle CANCEL and OUTSIDE externally
		//
		// TODO Other related events for Android multitouch gestures events are:
		// - ACTION_DOWN — For the first pointer that touches the screen. This starts the gesture. The pointer data for this pointer is always at index 0 in the MotionEvent.
		// - ACTION_MOVE — A change has happened during a press gesture
		// - ACTION_UP   — Sent when the last pointer leaves the screen.
		LOGD("JE_MULTI - fingersDown=%d arg2=%d", arg1, arg2);

		ev0.type = Common::EVENT_MOUSEMOVE;

		if (_touch_mode == TOUCH_MODE_TOUCHPAD) {
			ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
		} else {
			ev0.mouse.x = arg3;
			ev0.mouse.y = arg4;
		}

		{
			Common::Event ev1 = ev0;
			Common::EventType multitype = Common::EVENT_INVALID;

			switch (arg2 & AMOTION_EVENT_ACTION_MASK) {
			case AMOTION_EVENT_ACTION_POINTER_DOWN:
				// (From Android Developers documentation)
				// [This event is fired] For extra pointers that enter the screen beyond the first.
				// The pointer data for this pointer is at the index returned by getActionIndex()
//				LOGD("AMOTION_EVENT_ACTION_POINTER_DOWN fingersDown: %d", arg1);
//				if (arg1 > _fingersDown) {
//					LOGD("AMOTION_EVENT_ACTION_POINTER_DOWN changing  _fingersDown to arg1: %d", arg1);
//					_fingersDown = arg1;
//				}

				_touch_pt_multi.x = -1;
				_touch_pt_multi.y = -1;

				// TODO also handle Cancel event
				// TODO also handle going from 3 to 2 fingers
				// TODO also handle case of receiving a new ACTION_DOWN without first having received an ACTION_UP
				switch (arg1) {
				case 2:
//					LOGD("AMOTION_EVENT_ACTION_POINTER_DOWN arg1: %d --> Right Mouse Button", arg1);
					multitype = Common::EVENT_RBUTTONDOWN;
					break;

				case 3:
//					LOGD("AMOTION_EVENT_ACTION_POINTER_DOWN arg1: %d --> Middle Mouse Button", arg1);
					multitype = Common::EVENT_MBUTTONDOWN;
					break;

				default:
					LOGE("AMOTION_EVENT_ACTION_POINTER_DOWN - unmapped multi tap (arg1): %d", arg1);
					return;
				}
				break;

 			case AMOTION_EVENT_ACTION_CANCEL:
//				LOGD("AMOTION_EVENT_ACTION_CANCEL - (arg1): %d", arg1);
 				return;

  			case AMOTION_EVENT_ACTION_OUTSIDE:
//				LOGD("AMOTION_EVENT_ACTION_OUTSIDE - (arg1): %d", arg1);
 				return;

			case AMOTION_EVENT_ACTION_POINTER_UP:
				// (From Android Developers documentation)
				// Sent when a non-primary pointer goes up.
//				LOGD("AMOTION_EVENT_ACTION_POINTER_UP arg1: %d", arg1);
//				if (arg1 != _fingersDown) {
//					LOGD("returning as AMOTION_EVENT_ACTION_POINTER_UP arg1 != _fingersDown");
//					_fingersDown = 0;
//					return;
//				}

				switch (arg1) {
				case 2:
//					LOGD("AMOTION_EVENT_ACTION_POINTER_UP arg1: %d --> Right Mouse Button", arg1);
					multitype = Common::EVENT_RBUTTONUP;
					break;

				case 3:
//					LOGD("AMOTION_EVENT_ACTION_POINTER_UP arg1: %d --> Middle Mouse Button", arg1);
					multitype = Common::EVENT_MBUTTONUP;
					break;

				default:
					LOGE("AMOTION_EVENT_ACTION_POINTER_UP - unmapped multi tap (arg1): %d", arg1);
					return;
				}
				break;

			case AMOTION_EVENT_ACTION_MOVE:
				// TODO wrong event?
				// https://developer.android.com/training/gestures/movement
				// https://developer.android.com/training/gestures/multi
				//
//				LOGD("AMOTION_EVENT_ACTION_MOVE arg1: %d", arg1);
				// held and moved
				if (_touch_pt_multi.x == -1 && _touch_pt_multi.y == -1) {
					_touch_pt_multi.x = arg3;
					_touch_pt_multi.y = arg4;
					return;
				}

				multitype = Common::EVENT_MOUSEMOVE;

				if (_touch_mode == TOUCH_MODE_TOUCHPAD) {
					ev1.mouse.x = (arg3 - _touch_pt_multi.x) * 100 / _touchpad_scale;
					ev1.mouse.y = (arg4 - _touch_pt_multi.y) * 100 / _touchpad_scale;
					ev1.mouse += _touch_pt_down; // TODO maybe we need another reference point???
				} else {
					ev1.mouse.x = arg3;
					ev1.mouse.y = arg4;
				}
				break;

			default:
				LOGE("JE_MULTI - unhandled jaction on multi tap: %d", arg2);
				return;
			}

			ev1.type = multitype;
			if (ev1.type == Common::EVENT_RBUTTONDOWN || ev1.type == Common::EVENT_MBUTTONDOWN) {
				// INFO: Cases for delayed actions:
				// JE_MULTI: MV + DelayedDown + possible DelayedUp  (Direct Touch)
				// JE_TAP:   MV + DelayedDown + DelayedUp           (Direct Touch)
				// JE_TAP:   Down + DelayedUp                       (Touchpad)
				pushDelayedTouchMouseBtnEvents();
				uint32 originTimeForDelayedEventGrp = getMillis(true);
				if (_touch_mode != TOUCH_MODE_TOUCHPAD) {
					// Only send an early move event if:
					// - in direct touch mode
					// - and the multi touch event is a mouse button down (right or middle)
					_event_queue_lock->lock();
					_event_queue.push(ev0);
					_delayedMouseBtnDownEvent.mouse = ev1.mouse;
					_delayedMouseBtnDownEvent.type = ev1.type;
					_delayedMouseBtnDownEvent.referTimeMillis = originTimeForDelayedEventGrp;
					_delayedMouseBtnDownEvent.originTimeMillis = originTimeForDelayedEventGrp;
					_delayedMouseBtnDownEvent.delayMillis = kQueuedInputEventDelay;
					_delayedMouseBtnDownEvent.connectedType = ev0.type; // Common::EVENT_MOUSEMOVE
					_delayedMouseBtnDownEvent.connectedTypeExecuted = false;
					_event_queue_lock->unlock();
				} else {
					pushEvent(ev1);
				}
			} else  if (ev1.type == Common::EVENT_RBUTTONUP || ev1.type == Common::EVENT_MBUTTONUP) {
				if (_touch_mode != TOUCH_MODE_TOUCHPAD && _delayedMouseBtnDownEvent.delayMillis > 0) {
				// This is a case when the mouse up event for the multitouch gesture was sent too soon
				// after the last down event (which in direct touch mode is "scheduled" for delayed execution)
				// In that case, the mouse up event has to be delayed as well
					_event_queue_lock->lock();
					_delayedMouseBtnUpEvent.mouse = ev1.mouse;
					_delayedMouseBtnUpEvent.type = ev1.type;
					_delayedMouseBtnUpEvent.referTimeMillis = getMillis(true);
					_delayedMouseBtnUpEvent.originTimeMillis = _delayedMouseBtnDownEvent.originTimeMillis;
					_delayedMouseBtnUpEvent.delayMillis = kQueuedInputEventDelay;
					_delayedMouseBtnUpEvent.connectedType = _delayedMouseBtnDownEvent.type;
					_delayedMouseBtnUpEvent.connectedTypeExecuted = false;
					_event_queue_lock->unlock();
				} else {
					pushEvent(ev1);
				}
			} else if (ev1.type != Common::EVENT_INVALID) {
				pushEvent(ev1);
			}
		}
		break;

	case JE_BALL:
		ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();

		switch (arg1) {
		case AMOTION_EVENT_ACTION_DOWN:
			ev0.type = Common::EVENT_LBUTTONDOWN;
			break;

		case AMOTION_EVENT_ACTION_UP:
			ev0.type = Common::EVENT_LBUTTONUP;
			break;

		case AMOTION_EVENT_ACTION_MOVE:
			ev0.type = Common::EVENT_MOUSEMOVE;

			// already multiplied by 100
			ev0.mouse.x += arg2 * _trackball_scale / _eventScaleX;
			ev0.mouse.y += arg3 * _trackball_scale / _eventScaleY;

			break;

		default:
			LOGE("unhandled JE_BALL jaction on system key: %d", arg1);
			return;
		}
		pushEvent(ev0);
		break;

	case JE_MOUSE_MOVE:
		ev0.type = Common::EVENT_MOUSEMOVE;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_LMB_DOWN:
		ev0.type = Common::EVENT_LBUTTONDOWN;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_LMB_UP:
		ev0.type = Common::EVENT_LBUTTONUP;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_RMB_DOWN:
		ev0.type = Common::EVENT_RBUTTONDOWN;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_RMB_UP:
		ev0.type = Common::EVENT_RBUTTONUP;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_MMB_DOWN:
		ev0.type = Common::EVENT_MBUTTONDOWN;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_MMB_UP:
		ev0.type = Common::EVENT_MBUTTONUP;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_BMB_DOWN:
		ev0.type = Common::EVENT_X1BUTTONDOWN;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_BMB_UP:
		ev0.type = Common::EVENT_X1BUTTONUP;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_FMB_DOWN:
		ev0.type = Common::EVENT_X2BUTTONDOWN;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_FMB_UP:
		ev0.type = Common::EVENT_X2BUTTONUP;
		ev0.mouse.x = arg1;
		ev0.mouse.y = arg2;
		pushEvent(ev0);
		break;

	case JE_MOUSE_WHEEL_UP:
		// Rolling wheel upwards
		ev0.type = Common::EVENT_WHEELUP;
		if (arg3 == 1 && _touch_mode == TOUCH_MODE_TOUCHPAD) {
			// event comes from touch gesture
			if (_touch_pt_scroll.x == -1 && _touch_pt_scroll.y == -1) {
				_touch_pt_scroll.x = arg1;
				_touch_pt_scroll.y = arg2;
				return;
			}

			ev0.mouse.x = (arg1 - _touch_pt_scroll.x) * 100 / _touchpad_scale;
			ev0.mouse.y = (arg2 - _touch_pt_scroll.y) * 100 / _touchpad_scale;
			ev0.mouse += _touch_pt_down;
		} else {
			ev0.mouse.x = arg1;
			ev0.mouse.y = arg2;
		}
//		ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
		pushEvent(ev0);
		break;

	case JE_MOUSE_WHEEL_DOWN:
		// Rolling wheel downwards
		ev0.type = Common::EVENT_WHEELDOWN;
		if (arg3 == 1 && _touch_mode == TOUCH_MODE_TOUCHPAD) {
			// event comes from touch gesture
			if (_touch_pt_scroll.x == -1 && _touch_pt_scroll.y == -1) {
				_touch_pt_scroll.x = arg1;
				_touch_pt_scroll.y = arg2;
				return;
			}

			ev0.mouse.x = (arg1 - _touch_pt_scroll.x) * 100 / _touchpad_scale;
			ev0.mouse.y = (arg2 - _touch_pt_scroll.y) * 100 / _touchpad_scale;
			ev0.mouse += _touch_pt_down;
		} else {
			ev0.mouse.x = arg1;
			ev0.mouse.y = arg2;
		}
//		ev0.mouse = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
		pushEvent(ev0);
		break;

	case JE_GAMEPAD:
		switch (arg1) {
		case AKEY_EVENT_ACTION_DOWN:
			ev0.type = Common::EVENT_JOYBUTTON_DOWN;
			break;
		case AKEY_EVENT_ACTION_UP:
			ev0.type = Common::EVENT_JOYBUTTON_UP;
			break;
		default:
			LOGE("unhandled jaction on gamepad key: %d", arg1);
			return;
		}

		switch (arg2) {
		case AKEYCODE_BUTTON_START:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_START;
			break;

		case AKEYCODE_BUTTON_SELECT:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_BACK;
			break;

		case AKEYCODE_BUTTON_MODE:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_GUIDE;
			break;

		case AKEYCODE_BUTTON_A:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_A;
			break;

		case AKEYCODE_BUTTON_B:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_B;
			break;

		case AKEYCODE_BUTTON_X:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_X;
			break;

		case AKEYCODE_BUTTON_Y:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_Y;
			break;

		case AKEYCODE_BUTTON_L1:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_LEFT_SHOULDER;
			break;

		case AKEYCODE_BUTTON_R1:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_RIGHT_SHOULDER;
			break;
//		// NOTE As of yet JOYSTICK_BUTTON_LEFT_TRIGGER, JOYSTICK_BUTTON_RIGHT_TRIGGER are missing as "buttons" from the hardware-input source code
//		// There are controllers like PS5's DualSense that trigger these buttons presses, albeit for wrong buttons (Create and Menu gamepad buttons)
//		// which could be due to Android OS not fully supporting them.
//		// PS3's DS3 also triggers these button presses but also generates a movement event so perhaps we can capture them that way
//		// (as generic joystick movement, "JOYSTICK_AXIS_LEFT_TRIGGER", "JOYSTICK_AXIS_RIGHT_TRIGGER" hardware-input).
//		case AKEYCODE_BUTTON_L2:
//			ev0.joystick.button = Common::JOYSTICK_BUTTON_LEFT_TRIGGER;
//			break;
//
//		case AKEYCODE_BUTTON_R2:
//			ev0.joystick.button = Common::JOYSTICK_BUTTON_RIGHT_TRIGGER;
//			break;
//
		case AKEYCODE_BUTTON_THUMBL:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_LEFT_STICK;
			break;

		case AKEYCODE_BUTTON_THUMBR:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_RIGHT_STICK;
			break;

		case AKEYCODE_DPAD_UP:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_DPAD_UP;
			break;

		case AKEYCODE_DPAD_DOWN:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_DPAD_DOWN;
			break;

		case AKEYCODE_DPAD_LEFT:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_DPAD_LEFT;
			break;

		case AKEYCODE_DPAD_RIGHT:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_DPAD_RIGHT;
			break;

		case AKEYCODE_DPAD_CENTER:
			ev0.joystick.button = Common::JOYSTICK_BUTTON_DPAD_CENTER;
			break;

		default:
			LOGW("unmapped gamepad key: %d", arg2);
			return;
		}
		pushEvent(ev0);
		break;

	case JE_JOYSTICK:
		switch (arg1) {
		// AMOTION_EVENT_ACTION_MOVE is 2 in NDK (https://developer.android.com/ndk/reference/group/input)
		case AMOTION_EVENT_ACTION_MOVE:
			ev0.type = Common::EVENT_JOYAXIS_MOTION;

			switch (arg4) {
			case JE_JOY_AXIS_X_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_LEFT_STICK_X;
				ev0.joystick.position = CLIP<int32>(arg2, Common::JOYAXIS_MIN, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			case JE_JOY_AXIS_Y_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_LEFT_STICK_Y;
				ev0.joystick.position = CLIP<int32>(arg2, Common::JOYAXIS_MIN, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			case JE_JOY_AXIS_HAT_X_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_HAT_X;
				ev0.joystick.position = CLIP<int32>(arg2, Common::JOYAXIS_MIN, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			case JE_JOY_AXIS_HAT_Y_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_HAT_Y;
				ev0.joystick.position = CLIP<int32>(arg2, Common::JOYAXIS_MIN, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			case JE_JOY_AXIS_Z_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_RIGHT_STICK_X;
				ev0.joystick.position = CLIP<int32>(arg2, Common::JOYAXIS_MIN, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			case JE_JOY_AXIS_RZ_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_RIGHT_STICK_Y;
				ev0.joystick.position = CLIP<int32>(arg2, Common::JOYAXIS_MIN, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			case JE_JOY_AXIS_LTRIGGER_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_LEFT_TRIGGER;
				ev0.joystick.position = CLIP<int32>(arg2, 0, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			case JE_JOY_AXIS_RTRIGGER_bf:
				ev0.joystick.axis = Common::JOYSTICK_AXIS_RIGHT_TRIGGER;
				ev0.joystick.position = CLIP<int32>(arg2, 0, Common::JOYAXIS_MAX);
				pushEvent(ev0);
				break;

			default:
				// unsupported axis case
				break;
			}
			break;

		case AKEY_EVENT_ACTION_DOWN:
			ev0.type = Common::EVENT_JOYBUTTON_DOWN;
			break;

		case AKEY_EVENT_ACTION_UP:
			ev0.type = Common::EVENT_JOYBUTTON_UP;
			break;

		default:
			LOGE("unhandled jaction on joystick: %d", arg1);
			return;
		}

		if (arg1 != AMOTION_EVENT_ACTION_MOVE) {
			switch (arg2) {
			case AKEYCODE_BUTTON_1:
				ev0.joystick.button = Common::JOYSTICK_BUTTON_A;
				break;

			case AKEYCODE_BUTTON_2:
				ev0.joystick.button = Common::JOYSTICK_BUTTON_B;
				break;

			case AKEYCODE_BUTTON_3:
				ev0.joystick.button = Common::JOYSTICK_BUTTON_X;
				break;

			case AKEYCODE_BUTTON_4:
				ev0.joystick.button = Common::JOYSTICK_BUTTON_Y;
				break;

			default:
				LOGW("unmapped gamepad key: %d", arg2);
				return;
			}
			pushEvent(ev0);
		}
		break;

	case JE_QUIT:
		ev0.type = Common::EVENT_QUIT;
		pushEvent(ev0);
		break;

	case JE_MENU:
		ev0.type = Common::EVENT_MAINMENU;
		pushEvent(ev0);
		break;

	default:
		LOGE("unknown jevent type: %d", type);
		break;
	}
}

bool OSystem_Android::pollEvent(Common::Event &event) {
	//ENTER();

	if (pthread_self() == _main_thread) {
		if (_screen_changeid != JNI::surface_changeid) {
			_screen_changeid = JNI::surface_changeid;
			// If we lose the surface, don't deinit as we lose the EGL context and this may lead to crashes
			// Keep a dangling surface until we get a resize
			if (JNI::egl_surface_width > 0 && JNI::egl_surface_height > 0) {
				// surface changed
				dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->resizeSurface();

				event.type = Common::EVENT_SCREEN_CHANGED;

				return true;
			}
		}

		if (_virtkeybd_on != JNI::virt_keyboard_state) {
			_virtkeybd_on = JNI::virt_keyboard_state;
			dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->syncVirtkeyboardState(_virtkeybd_on);
		}

		if (JNI::pause) {
			LOGD("main thread going to sleep");
			sem_wait(&JNI::pause_sem);
			LOGD("main thread woke up");
		}
	}

	_event_queue_lock->lock();

	// We currently allow only one delayed event group at any time,
	// and it's always for events that come from a touch event/gesture.
	// "Group" here means that these events are connected,
	// and have to be executed one after the other in sequence,
	// respecting the set delay for each of them.
	// As of yet, the cases for delayed events are as follows:
	// -- Direct Touch Mode --
	// JE_MULTI: MOVE (instant) -> MOUSE_DOWN (DELAYED) -> MOUSE_UP (POSSIBLY DELAYED, BUT NOT ALWAYS)
	// JE_TAP:   MOVE (instant) -> MOUSE_DOWN (DELAYED) -> MOUSE_UP (DELAYED)
	//
	// -- Touchpad Emulation Mode --
	// JE_TAP:   DOWN (instant) -> MOUSE_UP (DELAYED)
	//
	// Handling multiple delayed event groups gets complicated and is practically unnecessary too
	if (_delayedMouseBtnDownEvent.delayMillis > 0
	    && _delayedMouseBtnDownEvent.connectedTypeExecuted
	    && (getMillis(true) - _delayedMouseBtnDownEvent.referTimeMillis > _delayedMouseBtnDownEvent.delayMillis)) {
		Common::Event evHP = _delayedMouseBtnDownEvent;
		event = evHP;
		if ((_delayedMouseBtnUpEvent.delayMillis > 0)
		    && (event.type == _delayedMouseBtnUpEvent.connectedType)) {
			if (_delayedMouseBtnDownEvent.originTimeMillis == _delayedMouseBtnUpEvent.originTimeMillis) {
				_delayedMouseBtnUpEvent.connectedTypeExecuted = true;
				_delayedMouseBtnUpEvent.referTimeMillis = getMillis(true);
			}
		}
		_delayedMouseBtnDownEvent.reset();
	} else if (_delayedMouseBtnUpEvent.delayMillis > 0
	    && _delayedMouseBtnUpEvent.connectedTypeExecuted
	    && (getMillis(true) - _delayedMouseBtnUpEvent.referTimeMillis > _delayedMouseBtnUpEvent.delayMillis)) {
		Common::Event evHP = _delayedMouseBtnUpEvent;
		event = evHP;
		_delayedMouseBtnUpEvent.reset();
	} else if (_event_queue.empty()) {
		_event_queue_lock->unlock();
		return false;

	} else {
		event = _event_queue.pop();
		if ((_delayedMouseBtnDownEvent.delayMillis > 0)
		    && (event.type == _delayedMouseBtnDownEvent.connectedType
		       && _delayedMouseBtnDownEvent.connectedTypeExecuted == false)) {
			_delayedMouseBtnDownEvent.connectedTypeExecuted = true;
			_delayedMouseBtnDownEvent.referTimeMillis = getMillis(true);
		} else if ((_delayedMouseBtnUpEvent.delayMillis > 0)
		    && (event.type == _delayedMouseBtnUpEvent.connectedType
		       && _delayedMouseBtnUpEvent.connectedTypeExecuted == false)) {
			_delayedMouseBtnUpEvent.connectedTypeExecuted = true;
			_delayedMouseBtnUpEvent.referTimeMillis = getMillis(true);
		}
	}
	_event_queue_lock->unlock();

	if (Common::isMouseEvent(event)) {
		if (_graphicsManager)
			return dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->notifyMousePosition(event.mouse);
	}

	return true;
}

void OSystem_Android::pushEvent(const Common::Event &event) {
	_event_queue_lock->lock();
	_event_queue.push(event);
	_event_queue_lock->unlock();
}

void OSystem_Android::pushEvent(const Common::Event &event1, const Common::Event &event2) {
	_event_queue_lock->lock();
	_event_queue.push(event1);
	_event_queue.push(event2);
	_event_queue_lock->unlock();
}

// This method force-feeds the pending delayed event to the queue
// and hopes for the best, while not ignoring the new delayed events that follow.
// This is done for responsiveness, albeit obviously it's not an ideal solution.
void OSystem_Android::pushDelayedTouchMouseBtnEvents() {
	_event_queue_lock->lock();
	Common::Event evHP1, evHP2;
	if (_delayedMouseBtnDownEvent.delayMillis > 0) {
		evHP1 = _delayedMouseBtnDownEvent;
		_event_queue.push(evHP1);
		_delayedMouseBtnDownEvent.reset();
	}
	if (_delayedMouseBtnUpEvent.delayMillis > 0) {
		evHP2 = _delayedMouseBtnUpEvent;
		_event_queue.push(evHP2);
		_delayedMouseBtnUpEvent.reset();
	}
	_event_queue_lock->unlock();
}

void OSystem_Android::setupTouchMode(int oldValue, int newValue) {
	_touch_mode = newValue;

	if (newValue == TOUCH_MODE_TOUCHPAD) {
		// Make sure we have a proper touch point if we switch to touchpad mode with finger down
		if (_graphicsManager) {
			_touch_pt_down = dynamic_cast<AndroidCommonGraphics *>(_graphicsManager)->getMousePosition();
		} else {
			_touch_pt_down.x = 0;
			_touch_pt_down.y = 0;
		}
		_touch_pt_scroll.x = -1;
		_touch_pt_scroll.y = -1;
	}
}