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
//! A wrapper around the NDK's `AInputEvent`.
//!
//! Most of these operations directly wrap functions in the NDK.  Documentation for all NDK
//! functions in this module can be found
//! [here](https://developer.android.com/ndk/reference/group/input).  See also the javadocs for
//! [`android.view.InputEvent`](https://developer.android.com/reference/android/view/InputEvent.html),
//! [`android.view.MotionEvent`](https://developer.android.com/reference/android/view/MotionEvent.html),
//! and [`android.view.KeyEvent`](https://developer.android.com/reference/android/view/KeyEvent).

use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::convert::TryInto;
use std::ptr::NonNull;

/// A [`*const AInputEvent`](ffi::AInputEvent)
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#ainputevent)
#[derive(Debug)]
pub enum InputEvent {
    MotionEvent(MotionEvent),
    KeyEvent(KeyEvent),
}

/// An enum representing the source of an [`InputEvent`].
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-36)
#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Source {
    Unknown = ffi::AINPUT_SOURCE_UNKNOWN,
    Keyboard = ffi::AINPUT_SOURCE_KEYBOARD,
    Dpad = ffi::AINPUT_SOURCE_DPAD,
    Gamepad = ffi::AINPUT_SOURCE_GAMEPAD,
    Touchscreen = ffi::AINPUT_SOURCE_TOUCHSCREEN,
    Mouse = ffi::AINPUT_SOURCE_MOUSE,
    Stylus = ffi::AINPUT_SOURCE_STYLUS,
    BluetoothStylus = ffi::AINPUT_SOURCE_BLUETOOTH_STYLUS,
    Trackball = ffi::AINPUT_SOURCE_TRACKBALL,
    MouseRelative = ffi::AINPUT_SOURCE_MOUSE_RELATIVE,
    Touchpad = ffi::AINPUT_SOURCE_TOUCHPAD,
    TouchNavigation = ffi::AINPUT_SOURCE_TOUCH_NAVIGATION,
    Joystick = ffi::AINPUT_SOURCE_JOYSTICK,
    RotaryEncoder = ffi::AINPUT_SOURCE_ROTARY_ENCODER,
    Any = ffi::AINPUT_SOURCE_ANY,
}

/// An enum representing the class of an [`InputEvent`] source
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-35)
#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
enum Class {
    None = ffi::AINPUT_SOURCE_CLASS_NONE,
    Button = ffi::AINPUT_SOURCE_CLASS_BUTTON,
    Pointer = ffi::AINPUT_SOURCE_CLASS_POINTER,
    Navigation = ffi::AINPUT_SOURCE_CLASS_NAVIGATION,
    Position = ffi::AINPUT_SOURCE_CLASS_POSITION,
    Joystick = ffi::AINPUT_SOURCE_CLASS_JOYSTICK,
}

impl InputEvent {
    /// Initialize an [`InputEvent`] from a pointer
    ///
    /// # Safety
    /// By calling this function, you assert that the pointer is a valid pointer to a
    /// native [`AInputEvent`](ffi::AInputEvent).
    #[inline]
    pub unsafe fn from_ptr(ptr: NonNull<ffi::AInputEvent>) -> Self {
        match ffi::AInputEvent_getType(ptr.as_ptr()) as u32 {
            ffi::AINPUT_EVENT_TYPE_KEY => InputEvent::KeyEvent(KeyEvent::from_ptr(ptr)),
            ffi::AINPUT_EVENT_TYPE_MOTION => InputEvent::MotionEvent(MotionEvent::from_ptr(ptr)),
            x => panic!("Bad event type received: {}", x),
        }
    }

    /// Returns a pointer to the native [`AInputEvent`](ffi::AInputEvent).
    #[inline]
    pub fn ptr(&self) -> NonNull<ffi::AInputEvent> {
        match self {
            InputEvent::MotionEvent(MotionEvent { ptr }) => *ptr,
            InputEvent::KeyEvent(KeyEvent { ptr }) => *ptr,
        }
    }

    /// Get the source of the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource)
    #[inline]
    pub fn source(&self) -> Source {
        let source = unsafe { ffi::AInputEvent_getSource(self.ptr().as_ptr()) as u32 };
        source.try_into().unwrap_or(Source::Unknown)
    }

    /// Get the device id associated with the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid)
    #[inline]
    pub fn device_id(&self) -> i32 {
        unsafe { ffi::AInputEvent_getDeviceId(self.ptr().as_ptr()) }
    }
}

/// A bitfield representing the state of modifier keys during an event.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-25)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct MetaState(pub u32);

impl MetaState {
    #[inline]
    pub fn alt_on(self) -> bool {
        self.0 & ffi::AMETA_ALT_ON != 0
    }
    #[inline]
    pub fn alt_left_on(self) -> bool {
        self.0 & ffi::AMETA_ALT_LEFT_ON != 0
    }
    #[inline]
    pub fn alt_right_on(self) -> bool {
        self.0 & ffi::AMETA_ALT_RIGHT_ON != 0
    }
    #[inline]
    pub fn shift_on(self) -> bool {
        self.0 & ffi::AMETA_SHIFT_ON != 0
    }
    #[inline]
    pub fn shift_left_on(self) -> bool {
        self.0 & ffi::AMETA_SHIFT_LEFT_ON != 0
    }
    #[inline]
    pub fn shift_right_on(self) -> bool {
        self.0 & ffi::AMETA_SHIFT_RIGHT_ON != 0
    }
    #[inline]
    pub fn sym_on(self) -> bool {
        self.0 & ffi::AMETA_SYM_ON != 0
    }
    #[inline]
    pub fn function_on(self) -> bool {
        self.0 & ffi::AMETA_FUNCTION_ON != 0
    }
    #[inline]
    pub fn ctrl_on(self) -> bool {
        self.0 & ffi::AMETA_CTRL_ON != 0
    }
    #[inline]
    pub fn ctrl_left_on(self) -> bool {
        self.0 & ffi::AMETA_CTRL_LEFT_ON != 0
    }
    #[inline]
    pub fn ctrl_right_on(self) -> bool {
        self.0 & ffi::AMETA_CTRL_RIGHT_ON != 0
    }
    #[inline]
    pub fn meta_on(self) -> bool {
        self.0 & ffi::AMETA_META_ON != 0
    }
    #[inline]
    pub fn meta_left_on(self) -> bool {
        self.0 & ffi::AMETA_META_LEFT_ON != 0
    }
    #[inline]
    pub fn meta_right_on(self) -> bool {
        self.0 & ffi::AMETA_META_RIGHT_ON != 0
    }
    #[inline]
    pub fn caps_lock_on(self) -> bool {
        self.0 & ffi::AMETA_CAPS_LOCK_ON != 0
    }
    #[inline]
    pub fn num_lock_on(self) -> bool {
        self.0 & ffi::AMETA_NUM_LOCK_ON != 0
    }
    #[inline]
    pub fn scroll_lock_on(self) -> bool {
        self.0 & ffi::AMETA_SCROLL_LOCK_ON != 0
    }
}

/// A motion event.
///
/// For general discussion of motion events in Android, see [the relevant
/// javadoc](https://developer.android.com/reference/android/view/MotionEvent).
#[derive(Clone, Debug)]
pub struct MotionEvent {
    ptr: NonNull<ffi::AInputEvent>,
}

// TODO: thread safety?

/// A motion action.
///
/// See [the NDK
/// docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-29)
#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum MotionAction {
    Down = ffi::AMOTION_EVENT_ACTION_DOWN,
    Up = ffi::AMOTION_EVENT_ACTION_UP,
    Move = ffi::AMOTION_EVENT_ACTION_MOVE,
    Cancel = ffi::AMOTION_EVENT_ACTION_CANCEL,
    Outside = ffi::AMOTION_EVENT_ACTION_OUTSIDE,
    PointerDown = ffi::AMOTION_EVENT_ACTION_POINTER_DOWN,
    PointerUp = ffi::AMOTION_EVENT_ACTION_POINTER_UP,
    HoverMove = ffi::AMOTION_EVENT_ACTION_HOVER_MOVE,
    Scroll = ffi::AMOTION_EVENT_ACTION_SCROLL,
    HoverEnter = ffi::AMOTION_EVENT_ACTION_HOVER_ENTER,
    HoverExit = ffi::AMOTION_EVENT_ACTION_HOVER_EXIT,
    ButtonPress = ffi::AMOTION_EVENT_ACTION_BUTTON_PRESS,
    ButtonRelease = ffi::AMOTION_EVENT_ACTION_BUTTON_RELEASE,
}

/// An axis of a motion event.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-32)
#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Axis {
    X = ffi::AMOTION_EVENT_AXIS_X,
    Y = ffi::AMOTION_EVENT_AXIS_Y,
    Pressure = ffi::AMOTION_EVENT_AXIS_PRESSURE,
    Size = ffi::AMOTION_EVENT_AXIS_SIZE,
    TouchMajor = ffi::AMOTION_EVENT_AXIS_TOUCH_MAJOR,
    TouchMinor = ffi::AMOTION_EVENT_AXIS_TOUCH_MINOR,
    ToolMajor = ffi::AMOTION_EVENT_AXIS_TOOL_MAJOR,
    ToolMinor = ffi::AMOTION_EVENT_AXIS_TOOL_MINOR,
    Orientation = ffi::AMOTION_EVENT_AXIS_ORIENTATION,
    Vscroll = ffi::AMOTION_EVENT_AXIS_VSCROLL,
    Hscroll = ffi::AMOTION_EVENT_AXIS_HSCROLL,
    Z = ffi::AMOTION_EVENT_AXIS_Z,
    Rx = ffi::AMOTION_EVENT_AXIS_RX,
    Ry = ffi::AMOTION_EVENT_AXIS_RY,
    Rz = ffi::AMOTION_EVENT_AXIS_RZ,
    HatX = ffi::AMOTION_EVENT_AXIS_HAT_X,
    HatY = ffi::AMOTION_EVENT_AXIS_HAT_Y,
    Ltrigger = ffi::AMOTION_EVENT_AXIS_LTRIGGER,
    Rtrigger = ffi::AMOTION_EVENT_AXIS_RTRIGGER,
    Throttle = ffi::AMOTION_EVENT_AXIS_THROTTLE,
    Rudder = ffi::AMOTION_EVENT_AXIS_RUDDER,
    Wheel = ffi::AMOTION_EVENT_AXIS_WHEEL,
    Gas = ffi::AMOTION_EVENT_AXIS_GAS,
    Brake = ffi::AMOTION_EVENT_AXIS_BRAKE,
    Distance = ffi::AMOTION_EVENT_AXIS_DISTANCE,
    Tilt = ffi::AMOTION_EVENT_AXIS_TILT,
    Scroll = ffi::AMOTION_EVENT_AXIS_SCROLL,
    RelativeX = ffi::AMOTION_EVENT_AXIS_RELATIVE_X,
    RelativeY = ffi::AMOTION_EVENT_AXIS_RELATIVE_Y,
    Generic1 = ffi::AMOTION_EVENT_AXIS_GENERIC_1,
    Generic2 = ffi::AMOTION_EVENT_AXIS_GENERIC_2,
    Generic3 = ffi::AMOTION_EVENT_AXIS_GENERIC_3,
    Generic4 = ffi::AMOTION_EVENT_AXIS_GENERIC_4,
    Generic5 = ffi::AMOTION_EVENT_AXIS_GENERIC_5,
    Generic6 = ffi::AMOTION_EVENT_AXIS_GENERIC_6,
    Generic7 = ffi::AMOTION_EVENT_AXIS_GENERIC_7,
    Generic8 = ffi::AMOTION_EVENT_AXIS_GENERIC_8,
    Generic9 = ffi::AMOTION_EVENT_AXIS_GENERIC_9,
    Generic10 = ffi::AMOTION_EVENT_AXIS_GENERIC_10,
    Generic11 = ffi::AMOTION_EVENT_AXIS_GENERIC_11,
    Generic12 = ffi::AMOTION_EVENT_AXIS_GENERIC_12,
    Generic13 = ffi::AMOTION_EVENT_AXIS_GENERIC_13,
    Generic14 = ffi::AMOTION_EVENT_AXIS_GENERIC_14,
    Generic15 = ffi::AMOTION_EVENT_AXIS_GENERIC_15,
    Generic16 = ffi::AMOTION_EVENT_AXIS_GENERIC_16,
}

/// A bitfield representing the state of buttons during a motion event.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-33)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct ButtonState(pub u32);

impl ButtonState {
    #[inline]
    pub fn primary(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_BUTTON_PRIMARY != 0
    }
    #[inline]
    pub fn secondary(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_BUTTON_SECONDARY != 0
    }
    #[inline]
    pub fn teriary(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_BUTTON_TERTIARY != 0
    }
    #[inline]
    pub fn back(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_BUTTON_BACK != 0
    }
    #[inline]
    pub fn forward(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_BUTTON_FORWARD != 0
    }
    #[inline]
    pub fn stylus_primary(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_BUTTON_STYLUS_PRIMARY != 0
    }
    #[inline]
    pub fn stylus_secondary(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_BUTTON_STYLUS_SECONDARY != 0
    }
}

/// A bitfield representing which edges were touched by a motion event.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-31)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct EdgeFlags(pub u32);

impl EdgeFlags {
    #[inline]
    pub fn top(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_TOP != 0
    }
    #[inline]
    pub fn bottom(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_BOTTOM != 0
    }
    #[inline]
    pub fn left(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_LEFT != 0
    }
    #[inline]
    pub fn right(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_RIGHT != 0
    }
}

/// Flags associated with this [`MotionEvent`].
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-30)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct MotionEventFlags(pub u32);

impl MotionEventFlags {
    #[inline]
    pub fn window_is_obscured(self) -> bool {
        self.0 & ffi::AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED != 0
    }
}

impl MotionEvent {
    /// Constructs a MotionEvent from a pointer to a native [`AInputEvent`](ffi::AInputEvent)
    ///
    /// # Safety
    /// By calling this method, you assert that the pointer is a valid, non-null pointer to a
    /// native [`AInputEvent`](ffi::AInputEvent) and that that [`AInputEvent`](ffi::AInputEvent)
    /// is an `AMotionEvent`.
    #[inline]
    pub unsafe fn from_ptr(ptr: NonNull<ffi::AInputEvent>) -> Self {
        Self { ptr }
    }

    /// Returns a pointer to the native [`AInputEvent`](ffi::AInputEvent)
    #[inline]
    pub fn ptr(&self) -> NonNull<ffi::AInputEvent> {
        self.ptr
    }

    /// Get the source of the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource)
    #[inline]
    pub fn source(&self) -> Source {
        let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) as u32 };
        source.try_into().unwrap_or(Source::Unknown)
    }

    /// Get the device id associated with the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid)
    #[inline]
    pub fn device_id(&self) -> i32 {
        unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) }
    }

    /// Returns the motion action associated with the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getaction)
    #[inline]
    pub fn action(&self) -> MotionAction {
        let action = unsafe {
            ffi::AMotionEvent_getAction(self.ptr.as_ptr()) as u32 & ffi::AMOTION_EVENT_ACTION_MASK
        };
        action.try_into().unwrap()
    }

    /// Returns the pointer index of an `Up` or `Down` event.
    ///
    /// Pointer indices can change per motion event.  For an identifier that stays the same, see
    /// [`Pointer::pointer_id()`].
    ///
    /// This only has a meaning when the [action](Self::action) is one of [`Up`](MotionAction::Up),
    /// [`Down`](MotionAction::Down), [`PointerUp`](MotionAction::PointerUp),
    /// or [`PointerDown`](MotionAction::PointerDown).
    #[inline]
    pub fn pointer_index(&self) -> usize {
        let action = unsafe { ffi::AMotionEvent_getAction(self.ptr.as_ptr()) as u32 };
        let index = (action & ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
            >> ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
        index as usize
    }

    /*
    /// Returns the pointer id associated with the given pointer index.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getpointerid)
    // TODO: look at output with out-of-range pointer index
    // Probably -1 though
    pub fn pointer_id_for(&self, pointer_index: usize) -> i32 {
        unsafe { ffi::AMotionEvent_getPointerId(self.ptr.as_ptr(), pointer_index) }
    }
    */

    /// Returns the number of pointers in this event
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getpointercount)
    #[inline]
    pub fn pointer_count(&self) -> usize {
        unsafe { ffi::AMotionEvent_getPointerCount(self.ptr.as_ptr()) as usize }
    }

    /// An iterator over the pointers in this motion event
    #[inline]
    pub fn pointers(&self) -> PointersIter<'_> {
        PointersIter {
            event: self.ptr,
            next_index: 0,
            count: self.pointer_count(),
            _marker: std::marker::PhantomData,
        }
    }

    /// The pointer at a given pointer index. Panics if the pointer index is out of bounds.
    ///
    /// If you need to loop over all the pointers, prefer the [`pointers()`](Self::pointers) method.
    #[inline]
    pub fn pointer_at_index(&self, index: usize) -> Pointer<'_> {
        if index >= self.pointer_count() {
            panic!("Pointer index {} is out of bounds", index);
        }
        Pointer {
            event: self.ptr,
            index,
            _marker: std::marker::PhantomData,
        }
    }

    /// Returns the size of the history contained in this event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_gethistorysize)
    #[inline]
    pub fn history_size(&self) -> usize {
        unsafe { ffi::AMotionEvent_getHistorySize(self.ptr.as_ptr()) as usize }
    }

    /// An iterator over the historical events contained in this event.
    #[inline]
    pub fn history(&self) -> HistoricalMotionEventsIter<'_> {
        HistoricalMotionEventsIter {
            event: self.ptr,
            next_history_index: 0,
            history_size: self.history_size(),
            _marker: std::marker::PhantomData,
        }
    }

    /// Returns the state of any modifier keys that were pressed during the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getmetastate)
    #[inline]
    pub fn meta_state(&self) -> MetaState {
        unsafe { MetaState(ffi::AMotionEvent_getMetaState(self.ptr.as_ptr()) as u32) }
    }

    /// Returns the button state during this event, as a bitfield.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getbuttonstate)
    #[inline]
    pub fn button_state(&self) -> ButtonState {
        unsafe { ButtonState(ffi::AMotionEvent_getButtonState(self.ptr.as_ptr()) as u32) }
    }

    /// Returns the time of the start of this gesture, in the `java.lang.System.nanoTime()` time
    /// base
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getdowntime)
    #[inline]
    pub fn down_time(&self) -> i64 {
        unsafe { ffi::AMotionEvent_getDownTime(self.ptr.as_ptr()) }
    }

    /// Returns a bitfield indicating which edges were touched by this event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getedgeflags)
    #[inline]
    pub fn edge_flags(&self) -> EdgeFlags {
        unsafe { EdgeFlags(ffi::AMotionEvent_getEdgeFlags(self.ptr.as_ptr()) as u32) }
    }

    /// Returns the time of this event, in the `java.lang.System.nanoTime()` time base
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_geteventtime)
    #[inline]
    pub fn event_time(&self) -> i64 {
        unsafe { ffi::AMotionEvent_getEventTime(self.ptr.as_ptr()) }
    }

    /// The flags associated with a motion event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getflags)
    #[inline]
    pub fn flags(&self) -> MotionEventFlags {
        unsafe { MotionEventFlags(ffi::AMotionEvent_getFlags(self.ptr.as_ptr()) as u32) }
    }

    /// Returns the offset in the x direction between the coordinates and the raw coordinates
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getxoffset)
    #[inline]
    pub fn x_offset(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getXOffset(self.ptr.as_ptr()) }
    }

    /// Returns the offset in the y direction between the coordinates and the raw coordinates
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getyoffset)
    #[inline]
    pub fn y_offset(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getYOffset(self.ptr.as_ptr()) }
    }

    /// Returns the precision of the x value of the coordinates
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getxprecision)
    #[inline]
    pub fn x_precision(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getXPrecision(self.ptr.as_ptr()) }
    }

    /// Returns the precision of the y value of the coordinates
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getyprecision)
    #[inline]
    pub fn y_precision(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getYPrecision(self.ptr.as_ptr()) }
    }
}

/// A view into the data of a specific pointer in a motion event.
#[derive(Debug)]
pub struct Pointer<'a> {
    event: NonNull<ffi::AInputEvent>,
    index: usize,
    _marker: std::marker::PhantomData<&'a MotionEvent>,
}

// TODO: thread safety?

impl<'a> Pointer<'a> {
    #[inline]
    pub fn pointer_index(&self) -> usize {
        self.index
    }

    #[inline]
    pub fn pointer_id(&self) -> i32 {
        unsafe { ffi::AMotionEvent_getPointerId(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn axis_value(&self, axis: Axis) -> f32 {
        unsafe {
            ffi::AMotionEvent_getAxisValue(
                self.event.as_ptr(),
                axis as i32,
                self.index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn orientation(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getOrientation(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn pressure(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getPressure(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn raw_x(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getRawX(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn raw_y(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getRawY(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn x(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getX(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn y(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getY(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn size(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getSize(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn tool_major(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getToolMajor(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn tool_minor(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getToolMinor(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn touch_major(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getTouchMajor(self.event.as_ptr(), self.index as ffi::size_t) }
    }

    #[inline]
    pub fn touch_minor(&self) -> f32 {
        unsafe { ffi::AMotionEvent_getTouchMinor(self.event.as_ptr(), self.index as ffi::size_t) }
    }
}

/// An iterator over the pointers in a [`MotionEvent`].
#[derive(Debug)]
pub struct PointersIter<'a> {
    event: NonNull<ffi::AInputEvent>,
    next_index: usize,
    count: usize,
    _marker: std::marker::PhantomData<&'a MotionEvent>,
}

// TODO: thread safety?

impl<'a> Iterator for PointersIter<'a> {
    type Item = Pointer<'a>;
    fn next(&mut self) -> Option<Pointer<'a>> {
        if self.next_index < self.count {
            let ptr = Pointer {
                event: self.event,
                index: self.next_index,
                _marker: std::marker::PhantomData,
            };
            self.next_index += 1;
            Some(ptr)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.count - self.next_index;
        (size, Some(size))
    }
}
impl<'a> ExactSizeIterator for PointersIter<'a> {
    fn len(&self) -> usize {
        self.count - self.next_index
    }
}

/// Represents a view into a past moment of a motion event
#[derive(Debug)]
pub struct HistoricalMotionEvent<'a> {
    event: NonNull<ffi::AInputEvent>,
    history_index: usize,
    _marker: std::marker::PhantomData<&'a MotionEvent>,
}

// TODO: thread safety?

impl<'a> HistoricalMotionEvent<'a> {
    /// Returns the "history index" associated with this historical event.  Older events have smaller indices.
    #[inline]
    pub fn history_index(&self) -> usize {
        self.history_index
    }

    /// Returns the time of the historical event, in the `java.lang.System.nanoTime()` time base
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_gethistoricaleventtime)
    #[inline]
    pub fn event_time(&self) -> i64 {
        unsafe {
            ffi::AMotionEvent_getHistoricalEventTime(
                self.event.as_ptr(),
                self.history_index as ffi::size_t,
            )
        }
    }

    /// An iterator over the pointers of this historical motion event
    #[inline]
    pub fn pointers(&self) -> HistoricalPointersIter<'a> {
        HistoricalPointersIter {
            event: self.event,
            history_index: self.history_index,
            next_pointer_index: 0,
            pointer_count: unsafe {
                ffi::AMotionEvent_getPointerCount(self.event.as_ptr()) as usize
            },
            _marker: std::marker::PhantomData,
        }
    }
}

/// An iterator over all the historical moments in a [`MotionEvent`].
///
/// It iterates from oldest to newest.
#[derive(Debug)]
pub struct HistoricalMotionEventsIter<'a> {
    event: NonNull<ffi::AInputEvent>,
    next_history_index: usize,
    history_size: usize,
    _marker: std::marker::PhantomData<&'a MotionEvent>,
}

// TODO: thread safety?

impl<'a> Iterator for HistoricalMotionEventsIter<'a> {
    type Item = HistoricalMotionEvent<'a>;

    fn next(&mut self) -> Option<HistoricalMotionEvent<'a>> {
        if self.next_history_index < self.history_size {
            let res = HistoricalMotionEvent {
                event: self.event,
                history_index: self.next_history_index,
                _marker: std::marker::PhantomData,
            };
            self.next_history_index += 1;
            Some(res)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.history_size - self.next_history_index;
        (size, Some(size))
    }
}
impl ExactSizeIterator for HistoricalMotionEventsIter<'_> {
    fn len(&self) -> usize {
        self.history_size - self.next_history_index
    }
}
impl<'a> DoubleEndedIterator for HistoricalMotionEventsIter<'a> {
    fn next_back(&mut self) -> Option<HistoricalMotionEvent<'a>> {
        if self.next_history_index < self.history_size {
            self.history_size -= 1;
            Some(HistoricalMotionEvent {
                event: self.event,
                history_index: self.history_size,
                _marker: std::marker::PhantomData,
            })
        } else {
            None
        }
    }
}

/// A view into a pointer at a historical moment
#[derive(Debug)]
pub struct HistoricalPointer<'a> {
    event: NonNull<ffi::AInputEvent>,
    pointer_index: usize,
    history_index: usize,
    _marker: std::marker::PhantomData<&'a MotionEvent>,
}

// TODO: thread safety?

impl<'a> HistoricalPointer<'a> {
    #[inline]
    pub fn pointer_index(&self) -> usize {
        self.pointer_index
    }

    #[inline]
    pub fn pointer_id(&self) -> i32 {
        unsafe {
            ffi::AMotionEvent_getPointerId(self.event.as_ptr(), self.pointer_index as ffi::size_t)
        }
    }

    #[inline]
    pub fn history_index(&self) -> usize {
        self.history_index
    }

    #[inline]
    pub fn axis_value(&self, axis: Axis) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalAxisValue(
                self.event.as_ptr(),
                axis as i32,
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn orientation(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalOrientation(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn pressure(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalPressure(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn raw_x(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalRawX(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn raw_y(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalRawY(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn x(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalX(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn y(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalY(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn size(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalSize(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn tool_major(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalToolMajor(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn tool_minor(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalToolMinor(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn touch_major(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalTouchMajor(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }

    #[inline]
    pub fn touch_minor(&self) -> f32 {
        unsafe {
            ffi::AMotionEvent_getHistoricalTouchMinor(
                self.event.as_ptr(),
                self.pointer_index as ffi::size_t,
                self.history_index as ffi::size_t,
            )
        }
    }
}

/// An iterator over the pointers in a historical motion event
#[derive(Debug)]
pub struct HistoricalPointersIter<'a> {
    event: NonNull<ffi::AInputEvent>,
    history_index: usize,
    next_pointer_index: usize,
    pointer_count: usize,
    _marker: std::marker::PhantomData<&'a MotionEvent>,
}

// TODO: thread safety?

impl<'a> Iterator for HistoricalPointersIter<'a> {
    type Item = HistoricalPointer<'a>;

    fn next(&mut self) -> Option<HistoricalPointer<'a>> {
        if self.next_pointer_index < self.pointer_count {
            let ptr = HistoricalPointer {
                event: self.event,
                history_index: self.history_index,
                pointer_index: self.next_pointer_index,
                _marker: std::marker::PhantomData,
            };
            self.next_pointer_index += 1;
            Some(ptr)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.pointer_count - self.next_pointer_index;
        (size, Some(size))
    }
}
impl ExactSizeIterator for HistoricalPointersIter<'_> {
    fn len(&self) -> usize {
        self.pointer_count - self.next_pointer_index
    }
}

/// A key event.
///
/// For general discussion of key events in Android, see [the relevant
/// javadoc](https://developer.android.com/reference/android/view/KeyEvent).
#[derive(Debug)]
pub struct KeyEvent {
    ptr: NonNull<ffi::AInputEvent>,
}

// TODO: thread safety?

/// Key actions.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-27)
#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum KeyAction {
    Down = ffi::AKEY_EVENT_ACTION_DOWN,
    Up = ffi::AKEY_EVENT_ACTION_UP,
    Multiple = ffi::AKEY_EVENT_ACTION_MULTIPLE,
}

/// Key codes.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-39)
#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Keycode {
    Unknown = ffi::AKEYCODE_UNKNOWN,
    SoftLeft = ffi::AKEYCODE_SOFT_LEFT,
    SoftRight = ffi::AKEYCODE_SOFT_RIGHT,
    Home = ffi::AKEYCODE_HOME,
    Back = ffi::AKEYCODE_BACK,
    Call = ffi::AKEYCODE_CALL,
    Endcall = ffi::AKEYCODE_ENDCALL,
    Keycode0 = ffi::AKEYCODE_0,
    Keycode1 = ffi::AKEYCODE_1,
    Keycode2 = ffi::AKEYCODE_2,
    Keycode3 = ffi::AKEYCODE_3,
    Keycode4 = ffi::AKEYCODE_4,
    Keycode5 = ffi::AKEYCODE_5,
    Keycode6 = ffi::AKEYCODE_6,
    Keycode7 = ffi::AKEYCODE_7,
    Keycode8 = ffi::AKEYCODE_8,
    Keycode9 = ffi::AKEYCODE_9,
    Star = ffi::AKEYCODE_STAR,
    Pound = ffi::AKEYCODE_POUND,
    DpadUp = ffi::AKEYCODE_DPAD_UP,
    DpadDown = ffi::AKEYCODE_DPAD_DOWN,
    DpadLeft = ffi::AKEYCODE_DPAD_LEFT,
    DpadRight = ffi::AKEYCODE_DPAD_RIGHT,
    DpadCenter = ffi::AKEYCODE_DPAD_CENTER,
    VolumeUp = ffi::AKEYCODE_VOLUME_UP,
    VolumeDown = ffi::AKEYCODE_VOLUME_DOWN,
    Power = ffi::AKEYCODE_POWER,
    Camera = ffi::AKEYCODE_CAMERA,
    Clear = ffi::AKEYCODE_CLEAR,
    A = ffi::AKEYCODE_A,
    B = ffi::AKEYCODE_B,
    C = ffi::AKEYCODE_C,
    D = ffi::AKEYCODE_D,
    E = ffi::AKEYCODE_E,
    F = ffi::AKEYCODE_F,
    G = ffi::AKEYCODE_G,
    H = ffi::AKEYCODE_H,
    I = ffi::AKEYCODE_I,
    J = ffi::AKEYCODE_J,
    K = ffi::AKEYCODE_K,
    L = ffi::AKEYCODE_L,
    M = ffi::AKEYCODE_M,
    N = ffi::AKEYCODE_N,
    O = ffi::AKEYCODE_O,
    P = ffi::AKEYCODE_P,
    Q = ffi::AKEYCODE_Q,
    R = ffi::AKEYCODE_R,
    S = ffi::AKEYCODE_S,
    T = ffi::AKEYCODE_T,
    U = ffi::AKEYCODE_U,
    V = ffi::AKEYCODE_V,
    W = ffi::AKEYCODE_W,
    X = ffi::AKEYCODE_X,
    Y = ffi::AKEYCODE_Y,
    Z = ffi::AKEYCODE_Z,
    Comma = ffi::AKEYCODE_COMMA,
    Period = ffi::AKEYCODE_PERIOD,
    AltLeft = ffi::AKEYCODE_ALT_LEFT,
    AltRight = ffi::AKEYCODE_ALT_RIGHT,
    ShiftLeft = ffi::AKEYCODE_SHIFT_LEFT,
    ShiftRight = ffi::AKEYCODE_SHIFT_RIGHT,
    Tab = ffi::AKEYCODE_TAB,
    Space = ffi::AKEYCODE_SPACE,
    Sym = ffi::AKEYCODE_SYM,
    Explorer = ffi::AKEYCODE_EXPLORER,
    Envelope = ffi::AKEYCODE_ENVELOPE,
    Enter = ffi::AKEYCODE_ENTER,
    Del = ffi::AKEYCODE_DEL,
    Grave = ffi::AKEYCODE_GRAVE,
    Minus = ffi::AKEYCODE_MINUS,
    Equals = ffi::AKEYCODE_EQUALS,
    LeftBracket = ffi::AKEYCODE_LEFT_BRACKET,
    RightBracket = ffi::AKEYCODE_RIGHT_BRACKET,
    Backslash = ffi::AKEYCODE_BACKSLASH,
    Semicolon = ffi::AKEYCODE_SEMICOLON,
    Apostrophe = ffi::AKEYCODE_APOSTROPHE,
    Slash = ffi::AKEYCODE_SLASH,
    At = ffi::AKEYCODE_AT,
    Num = ffi::AKEYCODE_NUM,
    Headsethook = ffi::AKEYCODE_HEADSETHOOK,
    Focus = ffi::AKEYCODE_FOCUS,
    Plus = ffi::AKEYCODE_PLUS,
    Menu = ffi::AKEYCODE_MENU,
    Notification = ffi::AKEYCODE_NOTIFICATION,
    Search = ffi::AKEYCODE_SEARCH,
    MediaPlayPause = ffi::AKEYCODE_MEDIA_PLAY_PAUSE,
    MediaStop = ffi::AKEYCODE_MEDIA_STOP,
    MediaNext = ffi::AKEYCODE_MEDIA_NEXT,
    MediaPrevious = ffi::AKEYCODE_MEDIA_PREVIOUS,
    MediaRewind = ffi::AKEYCODE_MEDIA_REWIND,
    MediaFastForward = ffi::AKEYCODE_MEDIA_FAST_FORWARD,
    Mute = ffi::AKEYCODE_MUTE,
    PageUp = ffi::AKEYCODE_PAGE_UP,
    PageDown = ffi::AKEYCODE_PAGE_DOWN,
    Pictsymbols = ffi::AKEYCODE_PICTSYMBOLS,
    SwitchCharset = ffi::AKEYCODE_SWITCH_CHARSET,
    ButtonA = ffi::AKEYCODE_BUTTON_A,
    ButtonB = ffi::AKEYCODE_BUTTON_B,
    ButtonC = ffi::AKEYCODE_BUTTON_C,
    ButtonX = ffi::AKEYCODE_BUTTON_X,
    ButtonY = ffi::AKEYCODE_BUTTON_Y,
    ButtonZ = ffi::AKEYCODE_BUTTON_Z,
    ButtonL1 = ffi::AKEYCODE_BUTTON_L1,
    ButtonR1 = ffi::AKEYCODE_BUTTON_R1,
    ButtonL2 = ffi::AKEYCODE_BUTTON_L2,
    ButtonR2 = ffi::AKEYCODE_BUTTON_R2,
    ButtonThumbl = ffi::AKEYCODE_BUTTON_THUMBL,
    ButtonThumbr = ffi::AKEYCODE_BUTTON_THUMBR,
    ButtonStart = ffi::AKEYCODE_BUTTON_START,
    ButtonSelect = ffi::AKEYCODE_BUTTON_SELECT,
    ButtonMode = ffi::AKEYCODE_BUTTON_MODE,
    Escape = ffi::AKEYCODE_ESCAPE,
    ForwardDel = ffi::AKEYCODE_FORWARD_DEL,
    CtrlLeft = ffi::AKEYCODE_CTRL_LEFT,
    CtrlRight = ffi::AKEYCODE_CTRL_RIGHT,
    CapsLock = ffi::AKEYCODE_CAPS_LOCK,
    ScrollLock = ffi::AKEYCODE_SCROLL_LOCK,
    MetaLeft = ffi::AKEYCODE_META_LEFT,
    MetaRight = ffi::AKEYCODE_META_RIGHT,
    Function = ffi::AKEYCODE_FUNCTION,
    Sysrq = ffi::AKEYCODE_SYSRQ,
    Break = ffi::AKEYCODE_BREAK,
    MoveHome = ffi::AKEYCODE_MOVE_HOME,
    MoveEnd = ffi::AKEYCODE_MOVE_END,
    Insert = ffi::AKEYCODE_INSERT,
    Forward = ffi::AKEYCODE_FORWARD,
    MediaPlay = ffi::AKEYCODE_MEDIA_PLAY,
    MediaPause = ffi::AKEYCODE_MEDIA_PAUSE,
    MediaClose = ffi::AKEYCODE_MEDIA_CLOSE,
    MediaEject = ffi::AKEYCODE_MEDIA_EJECT,
    MediaRecord = ffi::AKEYCODE_MEDIA_RECORD,
    F1 = ffi::AKEYCODE_F1,
    F2 = ffi::AKEYCODE_F2,
    F3 = ffi::AKEYCODE_F3,
    F4 = ffi::AKEYCODE_F4,
    F5 = ffi::AKEYCODE_F5,
    F6 = ffi::AKEYCODE_F6,
    F7 = ffi::AKEYCODE_F7,
    F8 = ffi::AKEYCODE_F8,
    F9 = ffi::AKEYCODE_F9,
    F10 = ffi::AKEYCODE_F10,
    F11 = ffi::AKEYCODE_F11,
    F12 = ffi::AKEYCODE_F12,
    NumLock = ffi::AKEYCODE_NUM_LOCK,
    Numpad0 = ffi::AKEYCODE_NUMPAD_0,
    Numpad1 = ffi::AKEYCODE_NUMPAD_1,
    Numpad2 = ffi::AKEYCODE_NUMPAD_2,
    Numpad3 = ffi::AKEYCODE_NUMPAD_3,
    Numpad4 = ffi::AKEYCODE_NUMPAD_4,
    Numpad5 = ffi::AKEYCODE_NUMPAD_5,
    Numpad6 = ffi::AKEYCODE_NUMPAD_6,
    Numpad7 = ffi::AKEYCODE_NUMPAD_7,
    Numpad8 = ffi::AKEYCODE_NUMPAD_8,
    Numpad9 = ffi::AKEYCODE_NUMPAD_9,
    NumpadDivide = ffi::AKEYCODE_NUMPAD_DIVIDE,
    NumpadMultiply = ffi::AKEYCODE_NUMPAD_MULTIPLY,
    NumpadSubtract = ffi::AKEYCODE_NUMPAD_SUBTRACT,
    NumpadAdd = ffi::AKEYCODE_NUMPAD_ADD,
    NumpadDot = ffi::AKEYCODE_NUMPAD_DOT,
    NumpadComma = ffi::AKEYCODE_NUMPAD_COMMA,
    NumpadEnter = ffi::AKEYCODE_NUMPAD_ENTER,
    NumpadEquals = ffi::AKEYCODE_NUMPAD_EQUALS,
    NumpadLeftParen = ffi::AKEYCODE_NUMPAD_LEFT_PAREN,
    NumpadRightParen = ffi::AKEYCODE_NUMPAD_RIGHT_PAREN,
    VolumeMute = ffi::AKEYCODE_VOLUME_MUTE,
    Info = ffi::AKEYCODE_INFO,
    ChannelUp = ffi::AKEYCODE_CHANNEL_UP,
    ChannelDown = ffi::AKEYCODE_CHANNEL_DOWN,
    ZoomIn = ffi::AKEYCODE_ZOOM_IN,
    ZoomOut = ffi::AKEYCODE_ZOOM_OUT,
    Tv = ffi::AKEYCODE_TV,
    Window = ffi::AKEYCODE_WINDOW,
    Guide = ffi::AKEYCODE_GUIDE,
    Dvr = ffi::AKEYCODE_DVR,
    Bookmark = ffi::AKEYCODE_BOOKMARK,
    Captions = ffi::AKEYCODE_CAPTIONS,
    Settings = ffi::AKEYCODE_SETTINGS,
    TvPower = ffi::AKEYCODE_TV_POWER,
    TvInput = ffi::AKEYCODE_TV_INPUT,
    StbPower = ffi::AKEYCODE_STB_POWER,
    StbInput = ffi::AKEYCODE_STB_INPUT,
    AvrPower = ffi::AKEYCODE_AVR_POWER,
    AvrInput = ffi::AKEYCODE_AVR_INPUT,
    ProgRed = ffi::AKEYCODE_PROG_RED,
    ProgGreen = ffi::AKEYCODE_PROG_GREEN,
    ProgYellow = ffi::AKEYCODE_PROG_YELLOW,
    ProgBlue = ffi::AKEYCODE_PROG_BLUE,
    AppSwitch = ffi::AKEYCODE_APP_SWITCH,
    Button1 = ffi::AKEYCODE_BUTTON_1,
    Button2 = ffi::AKEYCODE_BUTTON_2,
    Button3 = ffi::AKEYCODE_BUTTON_3,
    Button4 = ffi::AKEYCODE_BUTTON_4,
    Button5 = ffi::AKEYCODE_BUTTON_5,
    Button6 = ffi::AKEYCODE_BUTTON_6,
    Button7 = ffi::AKEYCODE_BUTTON_7,
    Button8 = ffi::AKEYCODE_BUTTON_8,
    Button9 = ffi::AKEYCODE_BUTTON_9,
    Button10 = ffi::AKEYCODE_BUTTON_10,
    Button11 = ffi::AKEYCODE_BUTTON_11,
    Button12 = ffi::AKEYCODE_BUTTON_12,
    Button13 = ffi::AKEYCODE_BUTTON_13,
    Button14 = ffi::AKEYCODE_BUTTON_14,
    Button15 = ffi::AKEYCODE_BUTTON_15,
    Button16 = ffi::AKEYCODE_BUTTON_16,
    LanguageSwitch = ffi::AKEYCODE_LANGUAGE_SWITCH,
    MannerMode = ffi::AKEYCODE_MANNER_MODE,
    Keycode3dMode = ffi::AKEYCODE_3D_MODE,
    Contacts = ffi::AKEYCODE_CONTACTS,
    Calendar = ffi::AKEYCODE_CALENDAR,
    Music = ffi::AKEYCODE_MUSIC,
    Calculator = ffi::AKEYCODE_CALCULATOR,
    ZenkakuHankaku = ffi::AKEYCODE_ZENKAKU_HANKAKU,
    Eisu = ffi::AKEYCODE_EISU,
    Muhenkan = ffi::AKEYCODE_MUHENKAN,
    Henkan = ffi::AKEYCODE_HENKAN,
    KatakanaHiragana = ffi::AKEYCODE_KATAKANA_HIRAGANA,
    Yen = ffi::AKEYCODE_YEN,
    Ro = ffi::AKEYCODE_RO,
    Kana = ffi::AKEYCODE_KANA,
    Assist = ffi::AKEYCODE_ASSIST,
    BrightnessDown = ffi::AKEYCODE_BRIGHTNESS_DOWN,
    BrightnessUp = ffi::AKEYCODE_BRIGHTNESS_UP,
    MediaAudioTrack = ffi::AKEYCODE_MEDIA_AUDIO_TRACK,
    Sleep = ffi::AKEYCODE_SLEEP,
    Wakeup = ffi::AKEYCODE_WAKEUP,
    Pairing = ffi::AKEYCODE_PAIRING,
    MediaTopMenu = ffi::AKEYCODE_MEDIA_TOP_MENU,
    Keycode11 = ffi::AKEYCODE_11,
    Keycode12 = ffi::AKEYCODE_12,
    LastChannel = ffi::AKEYCODE_LAST_CHANNEL,
    TvDataService = ffi::AKEYCODE_TV_DATA_SERVICE,
    VoiceAssist = ffi::AKEYCODE_VOICE_ASSIST,
    TvRadioService = ffi::AKEYCODE_TV_RADIO_SERVICE,
    TvTeletext = ffi::AKEYCODE_TV_TELETEXT,
    TvNumberEntry = ffi::AKEYCODE_TV_NUMBER_ENTRY,
    TvTerrestrialAnalog = ffi::AKEYCODE_TV_TERRESTRIAL_ANALOG,
    TvTerrestrialDigital = ffi::AKEYCODE_TV_TERRESTRIAL_DIGITAL,
    TvSatellite = ffi::AKEYCODE_TV_SATELLITE,
    TvSatelliteBs = ffi::AKEYCODE_TV_SATELLITE_BS,
    TvSatelliteCs = ffi::AKEYCODE_TV_SATELLITE_CS,
    TvSatelliteService = ffi::AKEYCODE_TV_SATELLITE_SERVICE,
    TvNetwork = ffi::AKEYCODE_TV_NETWORK,
    TvAntennaCable = ffi::AKEYCODE_TV_ANTENNA_CABLE,
    TvInputHdmi1 = ffi::AKEYCODE_TV_INPUT_HDMI_1,
    TvInputHdmi2 = ffi::AKEYCODE_TV_INPUT_HDMI_2,
    TvInputHdmi3 = ffi::AKEYCODE_TV_INPUT_HDMI_3,
    TvInputHdmi4 = ffi::AKEYCODE_TV_INPUT_HDMI_4,
    TvInputComposite1 = ffi::AKEYCODE_TV_INPUT_COMPOSITE_1,
    TvInputComposite2 = ffi::AKEYCODE_TV_INPUT_COMPOSITE_2,
    TvInputComponent1 = ffi::AKEYCODE_TV_INPUT_COMPONENT_1,
    TvInputComponent2 = ffi::AKEYCODE_TV_INPUT_COMPONENT_2,
    TvInputVga1 = ffi::AKEYCODE_TV_INPUT_VGA_1,
    TvAudioDescription = ffi::AKEYCODE_TV_AUDIO_DESCRIPTION,
    TvAudioDescriptionMixUp = ffi::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP,
    TvAudioDescriptionMixDown = ffi::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN,
    TvZoomMode = ffi::AKEYCODE_TV_ZOOM_MODE,
    TvContentsMenu = ffi::AKEYCODE_TV_CONTENTS_MENU,
    TvMediaContextMenu = ffi::AKEYCODE_TV_MEDIA_CONTEXT_MENU,
    TvTimerProgramming = ffi::AKEYCODE_TV_TIMER_PROGRAMMING,
    Help = ffi::AKEYCODE_HELP,
    NavigatePrevious = ffi::AKEYCODE_NAVIGATE_PREVIOUS,
    NavigateNext = ffi::AKEYCODE_NAVIGATE_NEXT,
    NavigateIn = ffi::AKEYCODE_NAVIGATE_IN,
    NavigateOut = ffi::AKEYCODE_NAVIGATE_OUT,
    StemPrimary = ffi::AKEYCODE_STEM_PRIMARY,
    Stem1 = ffi::AKEYCODE_STEM_1,
    Stem2 = ffi::AKEYCODE_STEM_2,
    Stem3 = ffi::AKEYCODE_STEM_3,
    DpadUpLeft = ffi::AKEYCODE_DPAD_UP_LEFT,
    DpadDownLeft = ffi::AKEYCODE_DPAD_DOWN_LEFT,
    DpadUpRight = ffi::AKEYCODE_DPAD_UP_RIGHT,
    DpadDownRight = ffi::AKEYCODE_DPAD_DOWN_RIGHT,
    MediaSkipForward = ffi::AKEYCODE_MEDIA_SKIP_FORWARD,
    MediaSkipBackward = ffi::AKEYCODE_MEDIA_SKIP_BACKWARD,
    MediaStepForward = ffi::AKEYCODE_MEDIA_STEP_FORWARD,
    MediaStepBackward = ffi::AKEYCODE_MEDIA_STEP_BACKWARD,
    SoftSleep = ffi::AKEYCODE_SOFT_SLEEP,
    Cut = ffi::AKEYCODE_CUT,
    Copy = ffi::AKEYCODE_COPY,
    Paste = ffi::AKEYCODE_PASTE,
    SystemNavigationUp = ffi::AKEYCODE_SYSTEM_NAVIGATION_UP,
    SystemNavigationDown = ffi::AKEYCODE_SYSTEM_NAVIGATION_DOWN,
    SystemNavigationLeft = ffi::AKEYCODE_SYSTEM_NAVIGATION_LEFT,
    SystemNavigationRight = ffi::AKEYCODE_SYSTEM_NAVIGATION_RIGHT,
    AllApps = ffi::AKEYCODE_ALL_APPS,
    Refresh = ffi::AKEYCODE_REFRESH,
    ThumbsUp = ffi::AKEYCODE_THUMBS_UP,
    ThumbsDown = ffi::AKEYCODE_THUMBS_DOWN,
    ProfileSwitch = ffi::AKEYCODE_PROFILE_SWITCH,
}

impl KeyEvent {
    /// Constructs a KeyEvent from a pointer to a native [`AInputEvent`](ffi::AInputEvent)
    ///
    /// # Safety
    /// By calling this method, you assert that the pointer is a valid, non-null pointer to an
    /// [`AInputEvent`](ffi::AInputEvent), and that that [`AInputEvent`](ffi::AInputEvent) is an `AKeyEvent`.
    #[inline]
    pub unsafe fn from_ptr(ptr: NonNull<ffi::AInputEvent>) -> Self {
        Self { ptr }
    }

    /// Returns a pointer to the native [`AInputEvent`](ffi::AInputEvent)
    #[inline]
    pub fn ptr(&self) -> NonNull<ffi::AInputEvent> {
        self.ptr
    }

    /// Returns the key action represented by this event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getaction)
    #[inline]
    pub fn action(&self) -> KeyAction {
        let action = unsafe { ffi::AKeyEvent_getAction(self.ptr.as_ptr()) as u32 };
        action.try_into().unwrap()
    }

    /// Get the source of the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource)
    #[inline]
    pub fn source(&self) -> Source {
        let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) as u32 };
        source.try_into().unwrap_or(Source::Unknown)
    }

    /// Get the device id associated with the event.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid)
    #[inline]
    pub fn device_id(&self) -> i32 {
        unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) }
    }

    /// Returns the last time the key was pressed.  This is on the scale of
    /// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getdowntime)
    #[inline]
    pub fn down_time(&self) -> i64 {
        unsafe { ffi::AKeyEvent_getDownTime(self.ptr.as_ptr()) }
    }

    /// Returns the time this event occured.  This is on the scale of
    /// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_geteventtime)
    #[inline]
    pub fn event_time(&self) -> i64 {
        unsafe { ffi::AKeyEvent_getEventTime(self.ptr.as_ptr()) }
    }

    /// Returns the keycode associated with this key event
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getkeycode)
    #[inline]
    pub fn key_code(&self) -> Keycode {
        let keycode = unsafe { ffi::AKeyEvent_getKeyCode(self.ptr.as_ptr()) as u32 };
        keycode.try_into().unwrap_or(Keycode::Unknown)
    }

    /// Returns the number of repeats of a key.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getrepeatcount)
    #[inline]
    pub fn repeat_count(&self) -> i32 {
        unsafe { ffi::AKeyEvent_getRepeatCount(self.ptr.as_ptr()) }
    }

    /// Returns the hardware keycode of a key.  This varies from device to device.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getscancode)
    #[inline]
    pub fn scan_code(&self) -> i32 {
        unsafe { ffi::AKeyEvent_getScanCode(self.ptr.as_ptr()) }
    }
}

/// Flags associated with [`KeyEvent`].
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-28)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct KeyEventFlags(pub u32);

impl KeyEventFlags {
    #[inline]
    pub fn cancelled(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_CANCELED != 0
    }
    #[inline]
    pub fn cancelled_long_press(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_CANCELED_LONG_PRESS != 0
    }
    #[inline]
    pub fn editor_action(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_EDITOR_ACTION != 0
    }
    #[inline]
    pub fn fallback(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_FALLBACK != 0
    }
    #[inline]
    pub fn from_system(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_FROM_SYSTEM != 0
    }
    #[inline]
    pub fn keep_touch_mode(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_KEEP_TOUCH_MODE != 0
    }
    #[inline]
    pub fn long_press(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_LONG_PRESS != 0
    }
    #[inline]
    pub fn soft_keyboard(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_SOFT_KEYBOARD != 0
    }
    #[inline]
    pub fn tracking(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_TRACKING != 0
    }
    #[inline]
    pub fn virtual_hard_key(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY != 0
    }
    #[inline]
    pub fn woke_here(&self) -> bool {
        self.0 & ffi::AKEY_EVENT_FLAG_WOKE_HERE != 0
    }
}

impl KeyEvent {
    /// Flags associated with this [`KeyEvent`].
    ///
    /// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getflags)
    #[inline]
    pub fn flags(&self) -> KeyEventFlags {
        unsafe { KeyEventFlags(ffi::AKeyEvent_getFlags(self.ptr.as_ptr()) as u32) }
    }

    /// Returns the state of the modifiers during this key event, represented by a bitmask.
    ///
    /// See [the NDK
    /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getmetastate)
    #[inline]
    pub fn meta_state(&self) -> MetaState {
        unsafe { MetaState(ffi::AKeyEvent_getMetaState(self.ptr.as_ptr()) as u32) }
    }
}