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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
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.Profiling;

////TODO: now that we can bind to controls by display name, we need to re-resolve controls when those change (e.g. when the keyboard layout changes)

////TODO: remove direct references to InputManager

////TODO: make sure controls in per-action and per-map control arrays are unique (the internal arrays are probably okay to have duplicates)

////REVIEW: should the default interaction be an *explicit* interaction?

////REVIEW: should "pass-through" be an interaction instead of a setting on actions?

////REVIEW: allow setup where state monitor is enabled but action is disabled?

namespace UnityEngine.InputSystem
{
    using InputActionListener = Action<InputAction.CallbackContext>;

    /// <summary>
    /// Dynamic execution state of one or more <see cref="InputActionMap">action maps</see> and
    /// all the actions they contain.
    /// </summary>
    /// <remarks>
    /// The aim of this class is to both put all the dynamic execution state into one place as well
    /// as to organize state in tight, GC-optimized arrays. Also, by moving state out of individual
    /// <see cref="InputActionMap">action maps</see>, we can combine the state of several maps
    /// into one single object with a single set of arrays. Ideally, if you have a single action
    /// asset in the game, you get a single InputActionState that contains the entire dynamic
    /// execution state for your game's actions.
    ///
    /// Note that this class allocates unmanaged memory. It has to be disposed of or it will leak
    /// memory!
    ///
    /// An instance of this class is also used for singleton actions by means of the hidden action
    /// map we create for those actions. In that case, there will be both a hidden map instance
    /// as well as an action state for every separate singleton action. This makes singleton actions
    /// relatively expensive.
    /// </remarks>
    internal unsafe class InputActionState : IInputStateChangeMonitor, ICloneable, IDisposable
    {
        public const int kInvalidIndex = -1;

        /// <summary>
        /// Array of all maps added to the state.
        /// </summary>
        public InputActionMap[] maps;

        /// <summary>
        /// List of all resolved controls.
        /// </summary>
        /// <remarks>
        /// As we don't know in advance how many controls a binding may match (if any), we bump the size of
        /// this array in increments during resolution. This means it may be end up being larger than the total
        /// number of used controls and have empty entries at the end. Use <see cref="UnmanagedMemory.controlCount"/> and not
        /// <c>.Length</c> to find the actual number of controls.
        ///
        /// All bound controls are included in the array regardless of whether only a partial set of actions
        /// is currently enabled. What ultimately decides whether controls get triggered or not is whether we
        /// have installed state monitors for them or not.
        /// </remarks>
        public InputControl[] controls;

        /// <summary>
        /// Array of instantiated interaction objects.
        /// </summary>
        /// <remarks>
        /// Every binding that has interactions corresponds to a slice of this array.
        ///
        /// Indices match between this and interaction states in <see cref="memory"/>.
        /// </remarks>
        public IInputInteraction[] interactions;

        /// <summary>
        /// Processor objects instantiated for the bindings in the state.
        /// </summary>
        public InputProcessor[] processors;

        /// <summary>
        /// Array of instantiated composite objects.
        /// </summary>
        public InputBindingComposite[] composites;

        public int totalProcessorCount;
        public int totalCompositeCount => memory.compositeCount;
        public int totalMapCount => memory.mapCount;
        public int totalActionCount => memory.actionCount;
        public int totalBindingCount => memory.bindingCount;
        public int totalInteractionCount => memory.interactionCount;
        public int totalControlCount => memory.controlCount;

        /// <summary>
        /// Block of unmanaged memory that holds the dynamic execution state of the actions and their controls.
        /// </summary>
        /// <remarks>
        /// We keep several arrays of structured data in a single block of unmanaged memory.
        /// </remarks>
        public UnmanagedMemory memory;

        public ActionMapIndices* mapIndices => memory.mapIndices;
        public TriggerState* actionStates => memory.actionStates;
        public BindingState* bindingStates => memory.bindingStates;
        public InteractionState* interactionStates => memory.interactionStates;
        public int* controlIndexToBindingIndex => memory.controlIndexToBindingIndex;
        public ushort* controlGroupingAndComplexity => memory.controlGroupingAndComplexity;
        public float* controlMagnitudes => memory.controlMagnitudes;
        public uint* enabledControls => (uint*)memory.enabledControls;

        public bool isProcessingControlStateChange => m_InProcessControlStateChange;

        private bool m_OnBeforeUpdateHooked;
        private bool m_OnAfterUpdateHooked;
        private bool m_InProcessControlStateChange;
        private InputEventPtr m_CurrentlyProcessingThisEvent;
        private Action m_OnBeforeUpdateDelegate;
        private Action m_OnAfterUpdateDelegate;

        /// <summary>
        /// Initialize execution state with given resolved binding information.
        /// </summary>
        /// <param name="resolver"></param>
        public void Initialize(InputBindingResolver resolver)
        {
            ClaimDataFrom(resolver);
            AddToGlobalList();
        }

        private void ComputeControlGroupingIfNecessary()
        {
            if (memory.controlGroupingInitialized)
                return;

            // If shortcut support is disabled, we simply put put all bindings at complexity=1 and
            // in their own group.
            var disableControlGrouping = !InputSystem.settings.shortcutKeysConsumeInput;

            var currentGroup = 1u;
            for (var i = 0; i < totalControlCount; ++i)
            {
                var control = controls[i];
                var bindingIndex = controlIndexToBindingIndex[i];
                ref var binding = ref bindingStates[bindingIndex];

                ////REVIEW: take processors and interactions into account??

                // Compute complexity.
                var complexity = 1;
                if (binding.isPartOfComposite && !disableControlGrouping)
                {
                    var compositeBindingIndex = binding.compositeOrCompositeBindingIndex;

                    for (var n = compositeBindingIndex + 1; n < totalBindingCount; ++n)
                    {
                        ref var partBinding = ref bindingStates[n];
                        if (!partBinding.isPartOfComposite || partBinding.compositeOrCompositeBindingIndex != compositeBindingIndex)
                            break;
                        ++complexity;
                    }
                }
                controlGroupingAndComplexity[i * 2 + 1] = (ushort)complexity;

                // Compute grouping. If already set, skip.
                if (controlGroupingAndComplexity[i * 2] == 0)
                {
                    if (!disableControlGrouping)
                    {
                        for (var n = 0; n < totalControlCount; ++n)
                        {
                            // NOTE: We could compute group numbers based on device index + control offsets
                            //       and thus make them work globally in a stable way. But we'd need a mechanism
                            //       to then determine ordering of actions globally such that it is clear which
                            //       action gets a first shot at an input.

                            var otherControl = controls[n];
                            if (control != otherControl)
                                continue;

                            controlGroupingAndComplexity[n * 2] = (ushort)currentGroup;
                        }
                    }

                    controlGroupingAndComplexity[i * 2] = (ushort)currentGroup;

                    ++currentGroup;
                }
            }

            memory.controlGroupingInitialized = true;
        }

        public void ClaimDataFrom(InputBindingResolver resolver)
        {
            totalProcessorCount = resolver.totalProcessorCount;

            maps = resolver.maps;
            interactions = resolver.interactions;
            processors = resolver.processors;
            composites = resolver.composites;
            controls = resolver.controls;

            memory = resolver.memory;
            resolver.memory = new UnmanagedMemory();

            ComputeControlGroupingIfNecessary();
        }

        ~InputActionState()
        {
            Destroy(isFinalizing: true);
        }

        public void Dispose()
        {
            Destroy();
        }

        private void Destroy(bool isFinalizing = false)
        {
            Debug.Assert(!isProcessingControlStateChange, "Must not destroy InputActionState while executing an action callback within it");

            if (!isFinalizing)
            {
                for (var i = 0; i < totalMapCount; ++i)
                {
                    var map = maps[i];

                    // Remove state change monitors.
                    if (map.enabled)
                        DisableControls(i, mapIndices[i].controlStartIndex, mapIndices[i].controlCount);

                    if (map.m_Asset != null)
                        map.m_Asset.m_SharedStateForAllMaps = null;

                    map.m_State = null;
                    map.m_MapIndexInState = kInvalidIndex;
                    map.m_EnabledActionsCount = 0;

                    // Reset action indices on the map's actions.
                    var actions = map.m_Actions;
                    if (actions != null)
                    {
                        for (var n = 0; n < actions.Length; ++n)
                            actions[n].m_ActionIndexInState = kInvalidIndex;
                    }
                }

                RemoveMapFromGlobalList();
            }
            memory.Dispose();
        }

        /// <summary>
        /// Create a copy of the state.
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// The copy is non-functional in so far as it cannot be used to keep track of changes made to
        /// any associated actions. However, it can be used to freeze the binding resolution state of
        /// a particular set of enabled actions. This is used by <see cref="InputActionTrace"/>.
        /// </remarks>
        public InputActionState Clone()
        {
            return new InputActionState
            {
                maps = ArrayHelpers.Copy(maps),
                controls = ArrayHelpers.Copy(controls),
                interactions = ArrayHelpers.Copy(interactions),
                processors = ArrayHelpers.Copy(processors),
                composites = ArrayHelpers.Copy(composites),
                totalProcessorCount = totalProcessorCount,
                memory = memory.Clone(),
            };
        }

        object ICloneable.Clone()
        {
            return Clone();
        }

        /// <summary>
        /// Check if the state is currently using a control from the given device.
        /// </summary>
        /// <param name="device">Any input device.</param>
        /// <returns>True if any of the maps in the state has the device in its <see cref="InputActionMap.devices"/>
        /// list or if any of the device's controls are contained in <see cref="controls"/>.</returns>
        private bool IsUsingDevice(InputDevice device)
        {
            Debug.Assert(device != null, "Device is null");

            // If all maps have device restrictions, the device must be in it
            // or we're not using it.
            var haveMapsWithoutDeviceRestrictions = false;
            for (var i = 0; i < totalMapCount; ++i)
            {
                var map = maps[i];
                var devicesForMap = map.devices;

                if (devicesForMap == null)
                    haveMapsWithoutDeviceRestrictions = true;
                else if (devicesForMap.Value.Contains(device))
                    return true;
            }

            if (!haveMapsWithoutDeviceRestrictions)
                return false;

            // Check all our controls one by one.
            for (var i = 0; i < totalControlCount; ++i)
                if (controls[i].device == device)
                    return true;

            return false;
        }

        // Check if the state would use a control from the given device.
        private bool CanUseDevice(InputDevice device)
        {
            Debug.Assert(device != null, "Device is null");

            // If all maps have device restrictions and the device isn't in them, we can't use
            // the device.
            var haveMapWithoutDeviceRestrictions = false;
            for (var i = 0; i < totalMapCount; ++i)
            {
                var map = maps[i];
                var devicesForMap = map.devices;

                if (devicesForMap == null)
                    haveMapWithoutDeviceRestrictions = true;
                else if (devicesForMap.Value.Contains(device))
                    return true;
            }

            if (!haveMapWithoutDeviceRestrictions)
                return false;

            for (var i = 0; i < totalMapCount; ++i)
            {
                var map = maps[i];
                var bindings = map.m_Bindings;
                if (bindings == null)
                    continue;

                var bindingCount = bindings.Length;
                for (var n = 0; n < bindingCount; ++n)
                {
                    if (InputControlPath.TryFindControl(device, bindings[n].effectivePath) != null)
                        return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Check whether the state has any actions that are currently enabled.
        /// </summary>
        /// <returns></returns>
        public bool HasEnabledActions()
        {
            for (var i = 0; i < totalMapCount; ++i)
            {
                var map = maps[i];
                if (map.enabled)
                    return true;
            }

            return false;
        }

        private void FinishBindingCompositeSetups()
        {
            for (var i = 0; i < totalBindingCount; ++i)
            {
                ref var binding = ref bindingStates[i];
                if (!binding.isComposite || binding.compositeOrCompositeBindingIndex == -1)
                    continue;

                var composite = composites[binding.compositeOrCompositeBindingIndex];
                var context = new InputBindingCompositeContext { m_State = this, m_BindingIndex = i };
                composite.CallFinishSetup(ref context);
            }
        }

        internal void PrepareForBindingReResolution(bool needFullResolve,
            ref InputControlList<InputControl> activeControls, ref bool hasEnabledActions)
        {
            // Let listeners know we're about to modify bindings.
            var needToCloneActiveControls = false;
            for (var i = 0; i < totalMapCount; ++i)
            {
                var map = maps[i];

                if (map.enabled)
                {
                    hasEnabledActions = true;

                    if (needFullResolve)
                    {
                        // For a full-resolve, we temporarily disable all actions and then re-enable
                        // all that were enabled after bindings have been resolved (plus we also flip on
                        // initial state checks for those actions to make sure they react right away
                        // to whatever state controls are in).
                        DisableAllActions(map);
                    }
                    else
                    {
                        // Cancel any action that is driven from a control we will lose when we re-resolve.
                        // For any other on-going action, save active controls.
                        foreach (var action in map.actions)
                        {
                            if (!action.phase.IsInProgress())
                                continue;

                            // Skip action's that are in progress but whose active control is not affected
                            // by the changes that lead to re-resolution.
                            if (action.ActiveControlIsValid(action.activeControl))
                            {
                                // As part of re-resolving, we're losing m_State.controls. So, while we retain
                                // the current execution state of the method including the index of the currently
                                // active control, we lose the actual references to the control.
                                // Thus, we retain an explicit list of active controls into which we *only* copy
                                // those few controls that are currently active. Also, this list is kept in unmanaged
                                // memory so we don't add an additional GC allocation here.
                                if (needToCloneActiveControls == false)
                                {
                                    activeControls = new InputControlList<InputControl>(Allocator.Temp);
                                    activeControls.Resize(totalControlCount);
                                    needToCloneActiveControls = true;
                                }

                                ref var actionState = ref actionStates[action.m_ActionIndexInState];
                                var activeControlIndex = actionState.controlIndex;
                                activeControls[activeControlIndex] = controls[activeControlIndex];

                                // Also save active controls for other ongoing interactions.
                                var bindingState = bindingStates[actionState.bindingIndex];
                                for (var n = 0; n < bindingState.interactionCount; ++n)
                                {
                                    var interactionIndex = bindingState.interactionStartIndex + n;
                                    if (!interactionStates[interactionIndex].phase.IsInProgress())
                                        continue;

                                    activeControlIndex = interactionStates[interactionIndex]
                                        .triggerControlIndex;
                                    if (action.ActiveControlIsValid(controls[activeControlIndex]))
                                        activeControls[activeControlIndex] = controls[activeControlIndex];
                                    else
                                        ResetInteractionState(interactionIndex);
                                }
                            }
                            else
                            {
                                ResetActionState(action.m_ActionIndexInState);
                            }
                        }

                        // NOTE: Removing state monitors here also means we're terminating any pending
                        //       timeouts. However, we have information in the action state about how much
                        //       is time is remaining on each of them so we can resume them later.

                        DisableControls(map);
                    }
                }

                map.ClearCachedActionData(onlyControls: !needFullResolve);
            }

            NotifyListenersOfActionChange(InputActionChange.BoundControlsAboutToChange);
        }

        public void FinishBindingResolution(bool hasEnabledActions, UnmanagedMemory oldMemory, InputControlList<InputControl> activeControls, bool isFullResolve)
        {
            // Fire InputBindingComposite.FinishSetup() calls.
            FinishBindingCompositeSetups();

            // Sync action states between the old and the new state. This also ensures
            // that any action that was already in progress just keeps going -- except
            // if we actually lost the control that was driving it.
            if (hasEnabledActions)
                RestoreActionStatesAfterReResolvingBindings(oldMemory, activeControls, isFullResolve);
            else
                NotifyListenersOfActionChange(InputActionChange.BoundControlsChanged);
        }

        /// <summary>
        /// Synchronize the current action states based on what they were before.
        /// </summary>
        /// <param name="oldState"></param>
        /// <remarks>
        /// We do this when we have to temporarily disable actions in order to re-resolve bindings.
        ///
        /// Note that we do NOT restore action states perfectly. I.e. will we will not preserve trigger
        /// and interaction states exactly to what they were before. Given that the bound controls may change,
        /// it would be non-trivial to reliably correlate the old and the new state. Instead, we simply
        /// reenable all the actions and controls that were enabled before and then let the next update
        /// take it from there.
        /// </remarks>
        private void RestoreActionStatesAfterReResolvingBindings(UnmanagedMemory oldState, InputControlList<InputControl> activeControls, bool isFullResolve)
        {
            Debug.Assert(oldState.isAllocated, "Old state contains no memory");

            // No maps and/or actions must have been added, replaced, or removed.
            //
            // IF
            //  isFullResolve==true:
            //     - No bindings must have been added, replaced, or removed or touched in any other way.
            //     - The only thing that is allowed to have changed is the list of controls used by the actions.
            //     - Binding masks must not have changed.
            //
            //  isFullResolve==false:
            //     - Bindings may have been added, replaced, modified, and/or removed.
            //     - Also, the list of controls may have changed.
            //     - Binding masks may have changed.
            //
            // This means that when we compare UnmanagedMemory from before and after:
            //  - Map indices are identical.
            //  - Action indices are identical.
            //  - Binding indices may have changed arbitrarily.
            //  - Control indices may have changed arbitrarily (controls[] before and after need not relate at all).
            //  - Processor indices may have changed arbitrarily.
            //  - Interaction indices may have changed arbitrarily.
            //
            // HOWEVER, if isFullResolve==false, then ONLY control indices may have changed. All other
            // indices must have remained unchanged.
            Debug.Assert(oldState.actionCount == memory.actionCount, "Action count in old and new state must be the same");
            Debug.Assert(oldState.mapCount == memory.mapCount, "Map count in old and new state must be the same");
            if (!isFullResolve)
            {
                Debug.Assert(oldState.bindingCount == memory.bindingCount, "Binding count in old and new state must be the same");
                Debug.Assert(oldState.interactionCount == memory.interactionCount, "Interaction count in old and new state must be the same");
                Debug.Assert(oldState.compositeCount == memory.compositeCount, "Composite count in old and new state must be the same");
            }

            // Restore action states.
            for (var actionIndex = 0; actionIndex < totalActionCount; ++actionIndex)
            {
                ref var oldActionState = ref oldState.actionStates[actionIndex];
                ref var newActionState = ref actionStates[actionIndex];

                newActionState.lastCanceledInUpdate = oldActionState.lastCanceledInUpdate;
                newActionState.lastPerformedInUpdate = oldActionState.lastPerformedInUpdate;
                newActionState.pressedInUpdate = oldActionState.pressedInUpdate;
                newActionState.releasedInUpdate = oldActionState.releasedInUpdate;
                newActionState.startTime = oldActionState.startTime;

                if (oldActionState.phase != InputActionPhase.Disabled)
                {
                    // In this step, we only put enabled actions into Waiting phase.
                    // When isFullResolve==false, we will restore the actual phase from
                    // before when we look at bindings further down in the code.
                    newActionState.phase = InputActionPhase.Waiting;

                    // In a full resolve, we actually disable any action we find enabled.
                    // So count any action we reenable here.
                    if (isFullResolve)
                        ++maps[newActionState.mapIndex].m_EnabledActionsCount;
                }
            }

            // Restore binding (and interaction) states.
            for (var bindingIndex = 0; bindingIndex < totalBindingCount; ++bindingIndex)
            {
                ref var newBindingState = ref memory.bindingStates[bindingIndex];
                if (newBindingState.isPartOfComposite)
                {
                    // Bindings that are part of composites get enabled through the composite itself.
                    continue;
                }

                // For composites, bring magnitudes along.
                if (newBindingState.isComposite)
                {
                    var compositeIndex = newBindingState.compositeOrCompositeBindingIndex;
                    memory.compositeMagnitudes[compositeIndex] = oldState.compositeMagnitudes[compositeIndex];
                }

                var actionIndex = newBindingState.actionIndex;
                if (actionIndex == kInvalidIndex)
                {
                    // Binding is not targeting an action.
                    continue;
                }

                // Skip if action is disabled.
                ref var newActionState = ref actionStates[actionIndex];
                if (newActionState.isDisabled)
                    continue;

                // For all bindings to actions that are enabled, we flip on initial state checks to make sure
                // we're checking the action's current state against the most up-to-date actuation state of controls.
                // NOTE: We're only restore execution state for currently active controls. So, if there were multiple
                //       concurrent actuations on an action that was in progress, we let initial state checks restore
                //       relevant state.
                newBindingState.initialStateCheckPending = newBindingState.wantsInitialStateCheck;

                // Enable all controls on the binding.
                EnableControls(newBindingState.mapIndex, newBindingState.controlStartIndex,
                    newBindingState.controlCount);

                // For the remainder of what we do, we need binding indices to be stable.
                if (isFullResolve)
                    continue;

                ref var oldBindingState = ref memory.bindingStates[bindingIndex];
                newBindingState.triggerEventIdForComposite = oldBindingState.triggerEventIdForComposite;

                // If we only re-resolved controls and the action was in progress from the binding we're currently
                // looking at and we still have the control that was driving the action, we can simply keep the
                // action going from its previous state. However, control indices may have shifted (devices may have been added
                // or removed) so we need to be careful to update those. Other indices (bindings, actions, maps, etc.)
                // are guaranteed to still match.
                ref var oldActionState = ref oldState.actionStates[actionIndex];
                if (bindingIndex == oldActionState.bindingIndex && oldActionState.phase.IsInProgress() &&
                    activeControls.Count > 0 && activeControls[oldActionState.controlIndex] != null)
                {
                    var control = activeControls[oldActionState.controlIndex];

                    // Find the new control index. Binding index is guaranteed to be the same,
                    // so we can simply look on the binding for where the control is now.
                    var newControlIndex = FindControlIndexOnBinding(bindingIndex, control);

                    Debug.Assert(newControlIndex != kInvalidIndex, "Could not find active control after binding resolution");
                    if (newControlIndex != kInvalidIndex)
                    {
                        newActionState.phase = oldActionState.phase;
                        newActionState.controlIndex = newControlIndex;
                        newActionState.magnitude = oldActionState.magnitude;
                        newActionState.interactionIndex = oldActionState.interactionIndex;

                        memory.controlMagnitudes[newControlIndex] = oldActionState.magnitude;
                    }

                    // Also bring over interaction states.
                    Debug.Assert(newBindingState.interactionCount == oldBindingState.interactionCount,
                        "Interaction count on binding must not have changed when doing a control-only resolve");
                    for (var n = 0; n < newBindingState.interactionCount; ++n)
                    {
                        ref var oldInteractionState = ref oldState.interactionStates[oldBindingState.interactionStartIndex + n];
                        if (!oldInteractionState.phase.IsInProgress())
                            continue;

                        control = activeControls[oldInteractionState.triggerControlIndex];
                        if (control == null)
                            continue;

                        newControlIndex = FindControlIndexOnBinding(bindingIndex, control);
                        Debug.Assert(newControlIndex != kInvalidIndex, "Could not find active control on interaction after binding resolution");

                        ref var newInteractionState = ref interactionStates[newBindingState.interactionStartIndex + n];
                        newInteractionState.phase = oldInteractionState.phase;
                        newInteractionState.performedTime = oldInteractionState.performedTime;
                        newInteractionState.startTime = oldInteractionState.startTime;
                        newInteractionState.triggerControlIndex = newControlIndex;

                        // If there was a running timeout on the interaction, resume it now.
                        if (oldInteractionState.isTimerRunning)
                        {
                            var trigger = new TriggerState
                            {
                                mapIndex = newBindingState.mapIndex,
                                controlIndex = newControlIndex,
                                bindingIndex = bindingIndex,
                                time = oldInteractionState.timerStartTime,
                                interactionIndex = newBindingState.interactionStartIndex + n
                            };
                            StartTimeout(oldInteractionState.timerDuration, ref trigger);

                            newInteractionState.totalTimeoutCompletionDone = oldInteractionState.totalTimeoutCompletionDone;
                            newInteractionState.totalTimeoutCompletionTimeRemaining = oldInteractionState.totalTimeoutCompletionTimeRemaining;
                        }
                    }
                }
            }

            // Make sure we get an initial state check.
            HookOnBeforeUpdate();

            // Let listeners know we have changed controls.
            NotifyListenersOfActionChange(InputActionChange.BoundControlsChanged);

            // For a full resolve, we will have temporarily disabled actions and reenabled them now.
            // Let listeners now.
            if (isFullResolve && s_GlobalState.onActionChange.length > 0)
            {
                for (var i = 0; i < totalMapCount; ++i)
                {
                    var map = maps[i];
                    if (map.m_SingletonAction == null && map.m_EnabledActionsCount == map.m_Actions.LengthSafe())
                    {
                        NotifyListenersOfActionChange(InputActionChange.ActionMapEnabled, map);
                    }
                    else
                    {
                        var actions = map.actions;
                        foreach (var action in actions)
                            if (action.enabled)
                                NotifyListenersOfActionChange(InputActionChange.ActionEnabled, action);
                    }
                }
            }
        }

        // Return true if the action that bindingIndex is bound to is currently driven from the given control
        // -OR- if any of the interactions on the binding are currently driven from the control.
        private bool IsActiveControl(int bindingIndex, int controlIndex)
        {
            ref var bindingState = ref bindingStates[bindingIndex];
            var actionIndex = bindingState.actionIndex;
            if (actionIndex == kInvalidIndex)
                return false;
            if (actionStates[actionIndex].controlIndex == controlIndex)
                return true;
            for (var i = 0; i < bindingState.interactionCount; ++i)
                if (interactionStates[bindingStates->interactionStartIndex + i].triggerControlIndex == controlIndex)
                    return true;
            return false;
        }

        private int FindControlIndexOnBinding(int bindingIndex, InputControl control)
        {
            var controlStartIndex = bindingStates[bindingIndex].controlStartIndex;
            var controlCount = bindingStates[bindingIndex].controlCount;

            for (var n = 0; n < controlCount; ++n)
            {
                if (control == controls[controlStartIndex + n])
                    return controlStartIndex + n;
            }

            return kInvalidIndex;
        }

        private void ResetActionStatesDrivenBy(InputDevice device)
        {
            using (InputActionRebindingExtensions.DeferBindingResolution())
            {
                for (var actionIndex = 0; actionIndex < totalActionCount; ++actionIndex)
                {
                    var actionState = &actionStates[actionIndex];

                    // Skip actions that aren't in progress.
                    if (actionState->phase == InputActionPhase.Waiting || actionState->phase == InputActionPhase.Disabled)
                        continue;

                    // Skip actions not driven from this device.
                    if (actionState->isPassThrough)
                    {
                        // Pass-through actions are not driven from specific controls yet still benefit
                        // from being able to observe resets. So for these, we need to check all bound controls,
                        // not just the one that happen to trigger last.
                        if (!IsActionBoundToControlFromDevice(device, actionIndex))
                            continue;
                    }
                    else
                    {
                        // For button and value actions, we go by whatever is currently driving the action.

                        var controlIndex = actionState->controlIndex;
                        if (controlIndex == -1)
                            continue;
                        var control = controls[controlIndex];
                        if (control.device != device)
                            continue;
                    }

                    // Reset.
                    ResetActionState(actionIndex);
                }
            }
        }

        private bool IsActionBoundToControlFromDevice(InputDevice device, int actionIndex)
        {
            var usesControlFromDevice = false;
            var bindingStartIndex = GetActionBindingStartIndexAndCount(actionIndex, out var bindingCount);
            for (var i = 0; i < bindingCount; ++i)
            {
                var bindingIndex = memory.actionBindingIndices[bindingStartIndex + i];
                var controlCount = bindingStates[bindingIndex].controlCount;
                var controlStartIndex = bindingStates[bindingIndex].controlStartIndex;

                for (var n = 0; n < controlCount; ++n)
                {
                    var control = controls[controlStartIndex + n];
                    if (control.device == device)
                    {
                        usesControlFromDevice = true;
                        break;
                    }
                }
            }

            return usesControlFromDevice;
        }

        /// <summary>
        /// Reset the trigger state of the given action such that the action has no record of being triggered.
        /// </summary>
        /// <param name="actionIndex">Action whose state to reset.</param>
        /// <param name="toPhase">Phase to reset the action to. Must be either <see cref="InputActionPhase.Waiting"/>
        /// or <see cref="InputActionPhase.Disabled"/>. Other phases cannot be transitioned to through resets.</param>
        /// <param name="hardReset">If true, also wipe state such as for <see cref="InputAction.WasPressedThisFrame"/> which normally
        /// persists even if an action is disabled.</param>
        public void ResetActionState(int actionIndex, InputActionPhase toPhase = InputActionPhase.Waiting, bool hardReset = false)
        {
            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount, "Action index out of range when resetting action");
            Debug.Assert(toPhase == InputActionPhase.Waiting || toPhase == InputActionPhase.Disabled,
                "Phase must be Waiting or Disabled");

            // If the action in started or performed phase, cancel it first.
            var actionState = &actionStates[actionIndex];
            if (actionState->phase != InputActionPhase.Waiting && actionState->phase != InputActionPhase.Disabled)
            {
                // Cancellation calls should receive current time.
                actionState->time = InputState.currentTime;

                // If the action got triggered from an interaction, go and reset all interactions on the binding
                // that got triggered.
                if (actionState->interactionIndex != kInvalidIndex)
                {
                    var bindingIndex = actionState->bindingIndex;
                    if (bindingIndex != kInvalidIndex)
                    {
                        var mapIndex = actionState->mapIndex;
                        var interactionCount = bindingStates[bindingIndex].interactionCount;
                        var interactionStartIndex = bindingStates[bindingIndex].interactionStartIndex;

                        for (var i = 0; i < interactionCount; ++i)
                        {
                            var interactionIndex = interactionStartIndex + i;
                            ResetInteractionStateAndCancelIfNecessary(mapIndex, bindingIndex, interactionIndex);
                        }
                    }
                }
                else
                {
                    // No interactions. Cancel the action directly.

                    Debug.Assert(actionState->bindingIndex != kInvalidIndex, "Binding index on trigger state is invalid");
                    Debug.Assert(bindingStates[actionState->bindingIndex].interactionCount == 0,
                        "Action has been triggered but apparently not from an interaction yet there's interactions on the binding that got triggered?!?");

                    if (actionState->phase != InputActionPhase.Canceled)
                        ChangePhaseOfAction(InputActionPhase.Canceled, ref actionStates[actionIndex]);
                }
            }

            // Wipe state.
            actionState->phase = toPhase;
            actionState->controlIndex = kInvalidIndex;
            actionState->bindingIndex = memory.actionBindingIndices[memory.actionBindingIndicesAndCounts[actionIndex]];
            actionState->interactionIndex = kInvalidIndex;
            actionState->startTime = 0;
            actionState->time = 0;
            actionState->hasMultipleConcurrentActuations = false;
            actionState->inProcessing = false;
            actionState->isPressed = false;

            // For "hard resets", wipe state we don't normally wipe. This resets things such as WasPressedThisFrame().
            if (hardReset)
            {
                actionState->lastCanceledInUpdate = default;
                actionState->lastPerformedInUpdate = default;
                actionState->pressedInUpdate = default;
                actionState->releasedInUpdate = default;
            }

            Debug.Assert(!actionState->isStarted, "Cannot reset an action to started phase");
            Debug.Assert(!actionState->isPerformed, "Cannot reset an action to performed phase");
            Debug.Assert(!actionState->isCanceled, "Cannot reset an action to canceled phase");
        }

        public ref TriggerState FetchActionState(InputAction action)
        {
            Debug.Assert(action != null, "Action must not be null");
            Debug.Assert(action.m_ActionMap != null, "Action must have an action map");
            Debug.Assert(action.m_ActionMap.m_MapIndexInState != kInvalidIndex, "Action must have index set");
            Debug.Assert(maps.Contains(action.m_ActionMap), "Action map must be contained in state");
            Debug.Assert(action.m_ActionIndexInState >= 0 && action.m_ActionIndexInState < totalActionCount, "Action index is out of range");

            return ref actionStates[action.m_ActionIndexInState];
        }

        public ActionMapIndices FetchMapIndices(InputActionMap map)
        {
            Debug.Assert(map != null, "Must must not be null");
            Debug.Assert(maps.Contains(map), "Map must be contained in state");
            return mapIndices[map.m_MapIndexInState];
        }

        public void EnableAllActions(InputActionMap map)
        {
            Debug.Assert(map != null, "Map must not be null");
            Debug.Assert(map.m_Actions != null, "Map must have actions");
            Debug.Assert(maps.Contains(map), "Map must be contained in state");

            // Enable all controls in map that aren't already enabled.
            EnableControls(map);

            // Put all actions that aren't already enabled into waiting state.
            var mapIndex = map.m_MapIndexInState;
            Debug.Assert(mapIndex >= 0 && mapIndex < totalMapCount, "Map index on InputActionMap is out of range");
            var actionCount = mapIndices[mapIndex].actionCount;
            var actionStartIndex = mapIndices[mapIndex].actionStartIndex;
            for (var i = 0; i < actionCount; ++i)
            {
                var actionIndex = actionStartIndex + i;
                var actionState = &actionStates[actionIndex];
                if (actionState->isDisabled)
                    actionState->phase = InputActionPhase.Waiting;
                actionState->inProcessing = false;
            }
            map.m_EnabledActionsCount = actionCount;

            HookOnBeforeUpdate();

            // Make sure that if we happen to get here with one of the hidden action maps we create for singleton
            // action, we notify on the action, not the hidden map.
            if (map.m_SingletonAction != null)
                NotifyListenersOfActionChange(InputActionChange.ActionEnabled, map.m_SingletonAction);
            else
                NotifyListenersOfActionChange(InputActionChange.ActionMapEnabled, map);
        }

        private void EnableControls(InputActionMap map)
        {
            Debug.Assert(map != null, "Map must not be null");
            Debug.Assert(map.m_Actions != null, "Map must have actions");
            Debug.Assert(maps.Contains(map), "Map must be contained in state");

            var mapIndex = map.m_MapIndexInState;
            Debug.Assert(mapIndex >= 0 && mapIndex < totalMapCount, "Map index on InputActionMap is out of range");

            // Install state monitors for all controls.
            var controlCount = mapIndices[mapIndex].controlCount;
            var controlStartIndex = mapIndices[mapIndex].controlStartIndex;
            if (controlCount > 0)
                EnableControls(mapIndex, controlStartIndex, controlCount);
        }

        public void EnableSingleAction(InputAction action)
        {
            Debug.Assert(action != null, "Action must not be null");
            Debug.Assert(action.m_ActionMap != null, "Action must have action map");
            Debug.Assert(maps.Contains(action.m_ActionMap), "Action map must be contained in state");

            EnableControls(action);

            // Put action into waiting state.
            var actionIndex = action.m_ActionIndexInState;
            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount,
                "Action index out of range when enabling single action");
            actionStates[actionIndex].phase = InputActionPhase.Waiting;
            ++action.m_ActionMap.m_EnabledActionsCount;

            HookOnBeforeUpdate();
            NotifyListenersOfActionChange(InputActionChange.ActionEnabled, action);
        }

        private void EnableControls(InputAction action)
        {
            Debug.Assert(action != null, "Action must not be null");
            Debug.Assert(action.m_ActionMap != null, "Action must have action map");
            Debug.Assert(maps.Contains(action.m_ActionMap), "Map must be contained in state");

            var actionIndex = action.m_ActionIndexInState;
            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount,
                "Action index out of range when enabling controls");

            var map = action.m_ActionMap;
            var mapIndex = map.m_MapIndexInState;
            Debug.Assert(mapIndex >= 0 && mapIndex < totalMapCount, "Map index out of range in EnableControls");

            // Go through all bindings in the map and for all that belong to the given action,
            // enable the associated controls.
            var bindingStartIndex = mapIndices[mapIndex].bindingStartIndex;
            var bindingCount = mapIndices[mapIndex].bindingCount;
            var bindingStatesPtr = memory.bindingStates;
            for (var i = 0; i < bindingCount; ++i)
            {
                var bindingIndex = bindingStartIndex + i;
                var bindingState = &bindingStatesPtr[bindingIndex];
                if (bindingState->actionIndex != actionIndex)
                    continue;

                // Composites enable en-bloc through the composite binding itself.
                if (bindingState->isPartOfComposite)
                    continue;

                var controlCount = bindingState->controlCount;
                if (controlCount == 0)
                    continue;

                EnableControls(mapIndex, bindingState->controlStartIndex, controlCount);
            }
        }

        public void DisableAllActions(InputActionMap map)
        {
            Debug.Assert(map != null, "Map must not be null");
            Debug.Assert(map.m_Actions != null, "Map must have actions");
            Debug.Assert(maps.Contains(map), "Map must be contained in state");

            DisableControls(map);

            // Mark all actions as disabled.
            var mapIndex = map.m_MapIndexInState;
            Debug.Assert(mapIndex >= 0 && mapIndex < totalMapCount, "Map index out of range in DisableAllActions");
            var actionStartIndex = mapIndices[mapIndex].actionStartIndex;
            var actionCount = mapIndices[mapIndex].actionCount;
            var allActionsEnabled = map.m_EnabledActionsCount == actionCount;
            for (var i = 0; i < actionCount; ++i)
            {
                var actionIndex = actionStartIndex + i;
                if (actionStates[actionIndex].phase != InputActionPhase.Disabled)
                {
                    ResetActionState(actionIndex, toPhase: InputActionPhase.Disabled);
                    if (!allActionsEnabled)
                        NotifyListenersOfActionChange(InputActionChange.ActionDisabled, map.m_Actions[i]);
                }
            }
            map.m_EnabledActionsCount = 0;

            // Make sure that if we happen to get here with one of the hidden action maps we create for singleton
            // action, we notify on the action, not the hidden map.
            if (map.m_SingletonAction != null)
                NotifyListenersOfActionChange(InputActionChange.ActionDisabled, map.m_SingletonAction);
            else if (allActionsEnabled)
                NotifyListenersOfActionChange(InputActionChange.ActionMapDisabled, map);
        }

        public void DisableControls(InputActionMap map)
        {
            Debug.Assert(map != null, "Map must not be null");
            Debug.Assert(map.m_Actions != null, "Map must have actions");
            Debug.Assert(maps.Contains(map), "Map must be contained in state");

            var mapIndex = map.m_MapIndexInState;
            Debug.Assert(mapIndex >= 0 && mapIndex < totalMapCount, "Map index out of range in DisableControls(InputActionMap)");

            // Remove state monitors from all controls.
            var controlCount = mapIndices[mapIndex].controlCount;
            var controlStartIndex = mapIndices[mapIndex].controlStartIndex;
            if (controlCount > 0)
                DisableControls(mapIndex, controlStartIndex, controlCount);
        }

        public void DisableSingleAction(InputAction action)
        {
            Debug.Assert(action != null, "Action must not be null");
            Debug.Assert(action.m_ActionMap != null, "Action must have action map");
            Debug.Assert(maps.Contains(action.m_ActionMap), "Action map must be contained in state");

            DisableControls(action);
            ResetActionState(action.m_ActionIndexInState, toPhase: InputActionPhase.Disabled);
            --action.m_ActionMap.m_EnabledActionsCount;

            NotifyListenersOfActionChange(InputActionChange.ActionDisabled, action);
        }

        private void DisableControls(InputAction action)
        {
            Debug.Assert(action != null, "Action must not be null");
            Debug.Assert(action.m_ActionMap != null, "Action must have action map");
            Debug.Assert(maps.Contains(action.m_ActionMap), "Action map must be contained in state");

            var actionIndex = action.m_ActionIndexInState;
            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount,
                "Action index out of range when disabling controls");

            var map = action.m_ActionMap;
            var mapIndex = map.m_MapIndexInState;
            Debug.Assert(mapIndex >= 0 && mapIndex < totalMapCount, "Map index out of range in DisableControls(InputAction)");

            // Go through all bindings in the map and for all that belong to the given action,
            // disable the associated controls.
            var bindingStartIndex = mapIndices[mapIndex].bindingStartIndex;
            var bindingCount = mapIndices[mapIndex].bindingCount;
            var bindingStatesPtr = memory.bindingStates;
            for (var i = 0; i < bindingCount; ++i)
            {
                var bindingIndex = bindingStartIndex + i;
                var bindingState = &bindingStatesPtr[bindingIndex];
                if (bindingState->actionIndex != actionIndex)
                    continue;

                // Composites enable en-bloc through the composite binding itself.
                if (bindingState->isPartOfComposite)
                    continue;

                var controlCount = bindingState->controlCount;
                if (controlCount == 0)
                    continue;

                DisableControls(mapIndex, bindingState->controlStartIndex, controlCount);
            }
        }

        ////REVIEW: can we have a method on InputManager doing this in bulk?

        ////NOTE: This must not enable only a partial set of controls on a binding (currently we have no setup that would lead to that)
        private void EnableControls(int mapIndex, int controlStartIndex, int numControls)
        {
            Debug.Assert(controls != null, "State must have controls");
            Debug.Assert(controlStartIndex >= 0 && (controlStartIndex < totalControlCount || numControls == 0),
                "Control start index out of range");
            Debug.Assert(controlStartIndex + numControls <= totalControlCount, "Control range out of bounds");

            var manager = InputSystem.s_Manager;
            for (var i = 0; i < numControls; ++i)
            {
                var controlIndex = controlStartIndex + i;

                // We don't want to add multiple state monitors for the same control. This can happen if enabling
                // single actions is mixed with enabling actions maps containing them.
                if (IsControlEnabled(controlIndex))
                    continue;

                var bindingIndex = controlIndexToBindingIndex[controlIndex];
                var mapControlAndBindingIndex = ToCombinedMapAndControlAndBindingIndex(mapIndex, controlIndex, bindingIndex);

                var bindingStatePtr = &bindingStates[bindingIndex];
                if (bindingStatePtr->wantsInitialStateCheck)
                    SetInitialStateCheckPending(bindingStatePtr, true);
                manager.AddStateChangeMonitor(controls[controlIndex], this, mapControlAndBindingIndex, controlGroupingAndComplexity[controlIndex * 2]);

                SetControlEnabled(controlIndex, true);
            }
        }

        private void DisableControls(int mapIndex, int controlStartIndex, int numControls)
        {
            Debug.Assert(controls != null, "State must have controls");
            Debug.Assert(controlStartIndex >= 0 && (controlStartIndex < totalControlCount || numControls == 0),
                "Control start index out of range");
            Debug.Assert(controlStartIndex + numControls <= totalControlCount, "Control range out of bounds");

            var manager = InputSystem.s_Manager;
            for (var i = 0; i < numControls; ++i)
            {
                var controlIndex = controlStartIndex + i;
                ////TODO: This can be done much more efficiently by at least going byte by byte in the mask instead of just bit by bit
                if (!IsControlEnabled(controlIndex))
                    continue;

                var bindingIndex = controlIndexToBindingIndex[controlIndex];
                var mapControlAndBindingIndex = ToCombinedMapAndControlAndBindingIndex(mapIndex, controlIndex, bindingIndex);
                var bindingStatePtr = &bindingStates[bindingIndex];
                if (bindingStatePtr->wantsInitialStateCheck)
                    SetInitialStateCheckPending(bindingStatePtr, false);
                manager.RemoveStateChangeMonitor(controls[controlIndex], this, mapControlAndBindingIndex);

                SetControlEnabled(controlIndex, false);
            }
        }

        public void SetInitialStateCheckPending(int actionIndex, bool value = true)
        {
            var mapIndex = actionStates[actionIndex].mapIndex;
            var bindingStartIndex = mapIndices[mapIndex].bindingStartIndex;
            var bindingCount = mapIndices[mapIndex].bindingCount;
            for (var i = 0; i < bindingCount; ++i)
            {
                ref var bindingState = ref bindingStates[bindingStartIndex + i];
                if (bindingState.actionIndex == actionIndex && !bindingState.isPartOfComposite)
                    bindingState.initialStateCheckPending = value;
            }
        }

        private void SetInitialStateCheckPending(BindingState* bindingStatePtr, bool value)
        {
            if (bindingStatePtr->isPartOfComposite)
            {
                // For composites, we always flag the composite itself as wanting an initial state check. This
                // way, we don't have to worry about triggering the composite multiple times when several of its
                // controls are actuated.
                var compositeIndex = bindingStatePtr->compositeOrCompositeBindingIndex;
                bindingStates[compositeIndex].initialStateCheckPending = value;
            }
            else
            {
                bindingStatePtr->initialStateCheckPending = value;
            }
        }

        private bool IsControlEnabled(int controlIndex)
        {
            var intIndex = controlIndex / 32;
            var mask = 1U << (controlIndex % 32);
            return (enabledControls[intIndex] & mask) != 0;
        }

        private void SetControlEnabled(int controlIndex, bool state)
        {
            var intIndex = controlIndex / 32;
            var mask = 1U << (controlIndex % 32);

            if (state)
                enabledControls[intIndex] |= mask;
            else
                enabledControls[intIndex] &= ~mask;
        }

        private void HookOnBeforeUpdate()
        {
            if (m_OnBeforeUpdateHooked)
                return;

            if (m_OnBeforeUpdateDelegate == null)
                m_OnBeforeUpdateDelegate = OnBeforeInitialUpdate;
            InputSystem.s_Manager.onBeforeUpdate += m_OnBeforeUpdateDelegate;
            m_OnBeforeUpdateHooked = true;
        }

        private void UnhookOnBeforeUpdate()
        {
            if (!m_OnBeforeUpdateHooked)
                return;

            InputSystem.s_Manager.onBeforeUpdate -= m_OnBeforeUpdateDelegate;
            m_OnBeforeUpdateHooked = false;
        }

        // We hook this into InputManager.onBeforeUpdate every time actions are enabled and then take it off
        // the list after the first call. Inside here we check whether any actions we enabled already have
        // non-default state on bound controls.
        //
        // NOTE: We do this as a callback from onBeforeUpdate rather than directly when the action is enabled
        //       to ensure that the callbacks happen during input processing and not randomly from wherever
        //       an action happens to be enabled.
        private void OnBeforeInitialUpdate()
        {
            if (InputState.currentUpdateType == InputUpdateType.BeforeRender
                #if UNITY_EDITOR
                || InputState.currentUpdateType == InputUpdateType.Editor
                #endif
            )
                return;

            // Remove us from the callback as the processing we're doing here is a one-time thing.
            UnhookOnBeforeUpdate();

            Profiler.BeginSample("InitialActionStateCheck");

            // Use current time as time of control state change.
            var time = InputState.currentTime;

            ////REVIEW: should we store this data in a separate place rather than go through all bindingStates?

            // Go through all binding states and for every binding that needs an initial state check,
            // go through all bound controls and for each one that isn't in its default state, pretend
            // that the control just got actuated.
            var manager = InputSystem.s_Manager;
            for (var bindingIndex = 0; bindingIndex < totalBindingCount; ++bindingIndex)
            {
                ref var bindingState = ref bindingStates[bindingIndex];
                if (!bindingState.initialStateCheckPending)
                    continue;

                Debug.Assert(!bindingState.isPartOfComposite, "Initial state check flag must be set on composite, not on its parts");
                bindingState.initialStateCheckPending = false;

                var controlStartIndex = bindingState.controlStartIndex;
                var controlCount = bindingState.controlCount;

                var isComposite = bindingState.isComposite;
                var didFindControlToSignal = false;
                for (var n = 0; n < controlCount; ++n)
                {
                    var controlIndex = controlStartIndex + n;
                    var control = controls[controlIndex];

                    // Leave any control alone that is already driving an interaction and/or action.
                    if (IsActiveControl(bindingIndex, controlIndex))
                        continue;

                    if (!control.CheckStateIsAtDefault())
                    {
                        // Update press times.
                        if (control.IsValueConsideredPressed(control.magnitude))
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            if (bindingState.pressTime == default || bindingState.pressTime > time)
                                bindingState.pressTime = time;
                        }

                        // For composites, any one actuated control will lead to the composite being
                        // processed as a whole so we can stop here. This also ensures that we are
                        // not triggering the composite repeatedly if there are multiple actuated
                        // controls bound to its parts.
                        if (isComposite && didFindControlToSignal)
                            continue;

                        manager.SignalStateChangeMonitor(control, this);
                        didFindControlToSignal = true;
                    }
                }
            }
            manager.FireStateChangeNotifications();

            Profiler.EndSample();
        }

        // Called from InputManager when one of our state change monitors has fired.
        // Tells us the time of the change *according to the state events coming in*.
        // Also tells us which control of the controls we are binding to triggered the
        // change and relays the binding index we gave it when we called AddChangeMonitor.
        void IInputStateChangeMonitor.NotifyControlStateChanged(InputControl control, double time,
            InputEventPtr eventPtr, long mapControlAndBindingIndex)
        {
            #if UNITY_EDITOR
            if (InputState.currentUpdateType == InputUpdateType.Editor)
                return;
            #endif

            SplitUpMapAndControlAndBindingIndex(mapControlAndBindingIndex, out var mapIndex, out var controlIndex, out var bindingIndex);
            ProcessControlStateChange(mapIndex, controlIndex, bindingIndex, time, eventPtr);
        }

        void IInputStateChangeMonitor.NotifyTimerExpired(InputControl control, double time,
            long mapControlAndBindingIndex, int interactionIndex)
        {
            SplitUpMapAndControlAndBindingIndex(mapControlAndBindingIndex, out var mapIndex, out var controlIndex, out var bindingIndex);
            ProcessTimeout(time, mapIndex, controlIndex, bindingIndex, interactionIndex);
        }

        /// <summary>
        /// Bit pack the mapIndex, controlIndex, bindingIndex and complexity components into a single long monitor index value.
        /// </summary>
        /// <param name="mapIndex">The mapIndex value to pack.</param>
        /// <param name="controlIndex">The controlIndex value to pack.</param>
        /// <param name="bindingIndex">The bindingIndex value to pack..</param>
        /// <remarks>
        /// We mangle the various indices we use into a single long for association with state change
        /// monitors. While we could look up map and binding indices from control indices, keeping
        /// all the information together avoids having to unnecessarily jump around in memory to grab
        /// the various pieces of data.
        /// The complexity component is implicitly derived and does not need to be passed as an argument.
        /// </remarks>
        private long ToCombinedMapAndControlAndBindingIndex(int mapIndex, int controlIndex, int bindingIndex)
        {
            // We have limits on the numbers of maps, controls, and bindings we allow in any single
            // action state (see TriggerState.kMaxNumXXX).
            var complexity = controlGroupingAndComplexity[controlIndex * 2 + 1];
            var result = (long)controlIndex;
            result |= (long)bindingIndex << 24;
            result |= (long)mapIndex << 40;
            result |= (long)complexity << 48;
            return result;
        }

        /// <summary>
        /// Extract the mapIndex, controlIndex and bindingIndex components from the provided bit packed argument (monitor index).
        /// </summary>
        /// <param name="mapControlAndBindingIndex">Represents a monitor index, which is a bit packed field containing multiple components.</param>
        /// <param name="mapIndex">Will hold the extracted mapIndex value after the function completes.</param>
        /// <param name="controlIndex">Will hold the extracted controlIndex value after the function completes.</param>
        /// <param name="bindingIndex">Will hold the extracted bindingIndex value after the function completes.</param>
        private void SplitUpMapAndControlAndBindingIndex(long mapControlAndBindingIndex, out int mapIndex,
            out int controlIndex, out int bindingIndex)
        {
            controlIndex = (int)(mapControlAndBindingIndex & 0x00ffffff);
            bindingIndex = (int)((mapControlAndBindingIndex >> 24) & 0xffff);
            mapIndex = (int)((mapControlAndBindingIndex >> 40) & 0xff);
        }

        /// <summary>
        /// Extract the 'complexity' component from the provided bit packed argument (monitor index).
        /// </summary>
        /// <param name="mapControlAndBindingIndex">Represents a monitor index, which is a bit packed field containing multiple components.</param>
        internal static int GetComplexityFromMonitorIndex(long mapControlAndBindingIndex)
        {
            return (int)((mapControlAndBindingIndex >> 48) & 0xff);
        }

        /// <summary>
        /// Process a state change that has happened in one of the controls attached
        /// to this action map state.
        /// </summary>
        /// <param name="mapIndex">Index of the action map to which the binding belongs.</param>
        /// <param name="controlIndex">Index of the control that changed state.</param>
        /// <param name="bindingIndex">Index of the binding associated with the given control.</param>
        /// <param name="time">The timestamp associated with the state change (comes from the state change event).</param>
        /// <param name="eventPtr">Event (if any) that triggered the state change.</param>
        /// <remarks>
        /// This is where we end up if one of the state monitors we've put in the system has triggered.
        /// From here we go back to the associated binding and then let it figure out what the state change
        /// means for it.
        ///
        /// Note that we get called for any change in state even if the change in state does not actually
        /// result in a change of value on the respective control.
        /// </remarks>
        private void ProcessControlStateChange(int mapIndex, int controlIndex, int bindingIndex, double time, InputEventPtr eventPtr)
        {
            Debug.Assert(mapIndex >= 0 && mapIndex < totalMapCount, "Map index out of range in ProcessControlStateChange");
            Debug.Assert(controlIndex >= 0 && controlIndex < totalControlCount, "Control index out of range");
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");

            using (InputActionRebindingExtensions.DeferBindingResolution())
            {
                // Callbacks can do pretty much anything and thus trigger arbitrary state/configuration
                // changes in the system. We have to ensure that while we're executing callbacks, our
                // current InputActionState is not getting changed from under us. We dictate that while
                // m_InProcessControlStateChange is true, no binding resolution can be triggered on the state and
                // it cannot be destroyed.
                //
                // This is also why we defer binding resolution above. If there is a configuration change
                // triggered by an action callback, the state will be marked dirty and re-resolved after
                // we have completed the callback.
                m_InProcessControlStateChange = true;
                m_CurrentlyProcessingThisEvent = eventPtr;

                try
                {
                    var bindingStatePtr = &bindingStates[bindingIndex];
                    var actionIndex = bindingStatePtr->actionIndex;

                    var trigger = new TriggerState
                    {
                        mapIndex = mapIndex,
                        controlIndex = controlIndex,
                        bindingIndex = bindingIndex,
                        interactionIndex = kInvalidIndex,
                        time = time,
                        startTime = time,
                        isPassThrough = actionIndex != kInvalidIndex && actionStates[actionIndex].isPassThrough,
                        isButton = actionIndex != kInvalidIndex && actionStates[actionIndex].isButton,
                    };

                    // If we have pending initial state checks that will run in the next update,
                    // force-reset the flag on the control that just triggered. This ensures that we're
                    // not triggering an action twice from the same state change in case the initial state
                    // check happens later (see Actions_ValueActionsEnabledInOnEvent_DoNotReactToCurrentStateOfControlTwice).
                    if (m_OnBeforeUpdateHooked)
                        bindingStatePtr->initialStateCheckPending = false;

                    // Store magnitude. We do this once and then only read it from here.
                    var control = controls[controlIndex];
                    trigger.magnitude = control.CheckStateIsAtDefault() ? 0f : control.magnitude;
                    controlMagnitudes[controlIndex] = trigger.magnitude;

                    // Update press times.
                    if (control.IsValueConsideredPressed(trigger.magnitude))
                    {
                        // ReSharper disable once CompareOfFloatsByEqualityOperator
                        if (bindingStatePtr->pressTime == default || bindingStatePtr->pressTime > trigger.time)
                            bindingStatePtr->pressTime = trigger.time;
                    }

                    // If the binding is part of a composite, check for interactions on the composite
                    // itself and give them a first shot at processing the value change.
                    var haveInteractionsOnComposite = false;
                    if (bindingStatePtr->isPartOfComposite)
                    {
                        var compositeBindingIndex = bindingStatePtr->compositeOrCompositeBindingIndex;
                        var compositeBindingPtr = &bindingStates[compositeBindingIndex];

                        // If the composite has already been triggered from the very same event, ignore it.
                        // Example: KeyboardState change that includes both A and W key state changes and we're looking
                        //          at a WASD composite binding. There's a state change monitor on both the A and the W
                        //          key and thus the manager will notify us individually of both changes. However, we
                        //          want to perform the action only once.
                        if (ShouldIgnoreInputOnCompositeBinding(compositeBindingPtr, eventPtr))
                            return;

                        // Update magnitude for composite.
                        var compositeIndex = bindingStates[compositeBindingIndex].compositeOrCompositeBindingIndex;
                        var compositeContext = new InputBindingCompositeContext
                        {
                            m_State = this,
                            m_BindingIndex = compositeBindingIndex
                        };
                        trigger.magnitude = composites[compositeIndex].EvaluateMagnitude(ref compositeContext);
                        memory.compositeMagnitudes[compositeIndex] = trigger.magnitude;

                        // Run through interactions on composite.
                        var interactionCountOnComposite = compositeBindingPtr->interactionCount;
                        if (interactionCountOnComposite > 0)
                        {
                            haveInteractionsOnComposite = true;
                            ProcessInteractions(ref trigger,
                                compositeBindingPtr->interactionStartIndex,
                                interactionCountOnComposite);
                        }
                    }

                    // Check if we have multiple concurrent actuations on the same action. This may lead us
                    // to ignore certain inputs (e.g. when we get an input of lesser magnitude while already having
                    // one of higher magnitude) or may even lead us to switch to processing a different binding
                    // (e.g. when an input of previously greater magnitude has now fallen below the level of another
                    // ongoing input with now higher magnitude).
                    var isConflictingInput = IsConflictingInput(ref trigger, actionIndex);
                    bindingStatePtr = &bindingStates[trigger.bindingIndex]; // IsConflictingInput may switch us to a different binding.

                    // Process button presses/releases.
                    if (!isConflictingInput)
                        ProcessButtonState(ref trigger, actionIndex, bindingStatePtr);

                    // If we have interactions, let them do all the processing. The presence of an interaction
                    // essentially bypasses the default phase progression logic of an action.
                    var interactionCount = bindingStatePtr->interactionCount;
                    if (interactionCount > 0 && !bindingStatePtr->isPartOfComposite)
                    {
                        ProcessInteractions(ref trigger, bindingStatePtr->interactionStartIndex, interactionCount);
                    }
                    else if (!haveInteractionsOnComposite && !isConflictingInput)
                    {
                        ProcessDefaultInteraction(ref trigger, actionIndex);
                    }
                }
                finally
                {
                    m_InProcessControlStateChange = default;
                    m_CurrentlyProcessingThisEvent = default;
                }
            }
        }

        private void ProcessButtonState(ref TriggerState trigger, int actionIndex, BindingState* bindingStatePtr)
        {
            var control = controls[trigger.controlIndex];
            var pressPoint = control.isButton
                ? ((ButtonControl)control).pressPointOrDefault
                : ButtonControl.s_GlobalDefaultButtonPressPoint;

            // NOTE: This method relies on conflict resolution happening *first*. Otherwise, we may inadvertently
            //       detect a "release" from a control that is not actually driving the action.

            // Record release time on the binding.
            // NOTE: Explicitly look up control magnitude here instead of using trigger.magnitude
            //       as for part bindings, the trigger will have the magnitude of the whole composite.
            var controlActuation = controlMagnitudes[trigger.controlIndex];
            if (controlActuation <= pressPoint * ButtonControl.s_GlobalDefaultButtonReleaseThreshold)
                bindingStatePtr->pressTime = 0d;

            var actuation = trigger.magnitude;
            var actionState = &actionStates[actionIndex];
            if (!actionState->isPressed && actuation >= pressPoint)
            {
                actionState->pressedInUpdate = InputUpdate.s_UpdateStepCount;
                actionState->isPressed = true;
            }
            else if (actionState->isPressed)
            {
                var releasePoint = pressPoint * ButtonControl.s_GlobalDefaultButtonReleaseThreshold;
                if (actuation <= releasePoint)
                {
                    actionState->releasedInUpdate = InputUpdate.s_UpdateStepCount;
                    actionState->isPressed = false;
                }
            }
        }

        /// <summary>
        /// Whether the given state change on a composite binding should be ignored.
        /// </summary>
        /// <param name="binding"></param>
        /// <param name="eventPtr"></param>
        /// <returns></returns>
        /// <remarks>
        /// Each state event may change the state of arbitrary many controls on a device and thus may trigger
        /// several bindings at once that are part of the same composite binding. We still want to trigger the
        /// composite binding only once for the event.
        ///
        /// To do so, we store the ID of the event on the binding and ignore events if they have the same
        /// ID as the one we've already recorded.
        /// </remarks>
        private static bool ShouldIgnoreInputOnCompositeBinding(BindingState* binding, InputEvent* eventPtr)
        {
            if (eventPtr == null)
                return false;

            var eventId = eventPtr->eventId;
            if (eventId != 0 && binding->triggerEventIdForComposite == eventId)
                return true;

            binding->triggerEventIdForComposite = eventId;
            return false;
        }

        /// <summary>
        /// Whether the given control state should be ignored.
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="actionIndex"></param>
        /// <returns></returns>
        /// <remarks>
        /// If an action has multiple controls bound to it, control state changes on the action may conflict with each other.
        /// If that happens, we resolve the conflict by always sticking to the most actuated control.
        ///
        /// Pass-through actions (<see cref="InputAction.passThrough"/>) will always bypass conflict resolution and respond
        /// to every value change.
        ///
        /// Actions that are resolved to only a single control will early out of conflict resolution.
        ///
        /// Actions that are bound to multiple controls but have only one control actuated will early out of conflict
        /// resolution as well.
        ///
        /// Note that conflict resolution here is entirely tied to magnitude. This ignores other qualities that the value
        /// of a control may have. For example, one 2D vector may have a similar magnitude to another yet point in an
        /// entirely different direction.
        ///
        /// There are other conflict resolution mechanisms that could be used. For example, we could average the values
        /// from all controls. However, it would not necessarily result in more useful conflict resolution and would
        /// at the same time be much more expensive.
        /// </remarks>
        private bool IsConflictingInput(ref TriggerState trigger, int actionIndex)
        {
            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount,
                "Action index out of range when checking for conflicting control input");

            // The goal of this method is to provide conflict resolution but do so ONLY if it is
            // really needed. In the vast majority of cases, this method should do almost nothing and
            // simply return straight away.

            // If conflict resolution is disabled on the action, early out. This is the case for pass-through
            // actions and for actions that cannot get into an ambiguous state based on the controls they
            // are bound to.
            var actionState = &actionStates[actionIndex];
            if (!actionState->mayNeedConflictResolution)
                return false;

            // Anything past here happens only for actions that may have conflicts.
            // Anything below here we want to avoid executing whenever we can.
            Debug.Assert(actionState->mayNeedConflictResolution);

            Profiler.BeginSample("InputActionResolveConflict");

            // We take a local copy of this value, so we can change it to use the starting control of composites
            // for simpler conflict resolution (so composites always use the same value), but still report the actually
            // actuated control to the user.
            var triggerControlIndex = trigger.controlIndex;
            if (bindingStates[trigger.bindingIndex].isPartOfComposite)
            {
                // For actions that need conflict resolution, we force TriggerState.controlIndex to the
                // first control in a composite. Otherwise it becomes much harder to tell if the we have
                // multiple concurrent actuations or not.
                // Since composites always evaluate as a whole instead of as single controls, having
                // triggerControlIndex differ from the state monitor that fired should be fine.
                var compositeBindingIndex = bindingStates[trigger.bindingIndex].compositeOrCompositeBindingIndex;
                triggerControlIndex = bindingStates[compositeBindingIndex].controlStartIndex;
                Debug.Assert(triggerControlIndex >= 0 && triggerControlIndex < totalControlCount,
                    "Control start index on composite binding out of range");
            }

            // Determine which control to consider the one currently associated with the action.
            // We do the same thing as for the triggered control and in the case of a composite,
            // switch to the first control of the composite.
            var actionStateControlIndex = actionState->controlIndex;
            if (bindingStates[actionState->bindingIndex].isPartOfComposite)
            {
                var compositeBindingIndex = bindingStates[actionState->bindingIndex].compositeOrCompositeBindingIndex;
                actionStateControlIndex = bindingStates[compositeBindingIndex].controlStartIndex;
            }

            // Never ignore state changes for actions that aren't currently driven by
            // anything.
            if (actionStateControlIndex == kInvalidIndex)
            {
                actionState->magnitude = trigger.magnitude;
                Profiler.EndSample();
                return false;
            }

            // Find out if we get triggered from the control that is actively driving the action.
            var isControlCurrentlyDrivingTheAction = triggerControlIndex == actionStateControlIndex ||
                controls[triggerControlIndex] == controls[actionStateControlIndex];                                      // Same control, different binding.

            // If the control is actuated *more* than the current level of actuation we recorded for the
            // action, we process the state change normally. If this isn't the control that is already
            // driving the action, it will become the one now.
            //
            // NOTE: For composites, we're looking at the combined actuation of the entire binding here,
            //       not just at the actuation level of the individual control. ComputeMagnitude()
            //       automatically takes care of that for us.
            if (trigger.magnitude > actionState->magnitude)
            {
                // If this is not the control that is currently driving the action, we know
                // there are multiple controls that are concurrently actuated on the action.
                // Remember that so that when the controls are released again, we can more
                // efficiently determine whether we need to take multiple bound controls into
                // account or not.
                // NOTE: For composites, we have forced triggerControlIndex to the first control
                //       in the composite. See above.
                if (trigger.magnitude > 0 && !isControlCurrentlyDrivingTheAction && actionState->magnitude > 0)
                    actionState->hasMultipleConcurrentActuations = true;

                // Keep recorded magnitude in action state up to date.
                actionState->magnitude = trigger.magnitude;
                Profiler.EndSample();
                return false;
            }

            // If the control is actuated *less* then the current level of actuation we
            // recorded for the action *and* the control that changed is the one that is currently
            // driving the action, we have to check whether there is another actuation
            // that is now *higher* than what we're getting from the current control.
            if (trigger.magnitude < actionState->magnitude)
            {
                // If we're not currently driving the action, it's simple. Doesn't matter that we lowered
                // actuation as we didn't have the highest actuation anyway.
                if (!isControlCurrentlyDrivingTheAction)
                {
                    Profiler.EndSample();
                    ////REVIEW: should we *count* actuations instead? (problem is that then we have to reliably determine when a control
                    ////        first actuates; the current solution will occasionally run conflict resolution when it doesn't have to
                    ////        but won't require the extra bookkeeping)
                    // Do NOT let this control state change affect the action.
                    if (trigger.magnitude > 0)
                        actionState->hasMultipleConcurrentActuations = true;
                    return true;
                }

                // If we don't have multiple controls that are currently actuated, it's simple.
                if (!actionState->hasMultipleConcurrentActuations)
                {
                    // Keep recorded magnitude in action state up to date.
                    actionState->magnitude = trigger.magnitude;
                    Profiler.EndSample();
                    return false;
                }

                ////REVIEW: is there a simpler way we can do this???

                // So, now we know we are actually looking at a potential conflict. Multiple
                // controls bound to the action are actuated but we don't yet know whether
                // any of them is actuated *more* than the control that had just changed value.
                // Go through the bindings for the action and see what we've got.
                var bindingStartIndex = GetActionBindingStartIndexAndCount(actionIndex, out var bindingCount);
                var highestActuationLevel = trigger.magnitude;
                var controlWithHighestActuation = kInvalidIndex;
                var bindingWithHighestActuation = kInvalidIndex;
                var numActuations = 0;
                for (var i = 0; i < bindingCount; ++i)
                {
                    var bindingIndex = memory.actionBindingIndices[bindingStartIndex + i];
                    var binding = &memory.bindingStates[bindingIndex];

                    if (binding->isComposite)
                    {
                        // Composite bindings result in a single actuation value regardless of how
                        // many controls are bound through the parts of the composite.

                        var firstControlIndex = binding->controlStartIndex;
                        var compositeIndex = binding->compositeOrCompositeBindingIndex;

                        Debug.Assert(compositeIndex >= 0 && compositeIndex < totalCompositeCount,
                            "Composite index out of range on composite");

                        var magnitude = memory.compositeMagnitudes[compositeIndex];
                        if (magnitude > 0)
                            ++numActuations;
                        if (magnitude > highestActuationLevel)
                        {
                            Debug.Assert(firstControlIndex >= 0 && firstControlIndex < totalControlCount,
                                "Control start index out of range on composite");

                            controlWithHighestActuation = firstControlIndex;
                            bindingWithHighestActuation = controlIndexToBindingIndex[firstControlIndex];
                            highestActuationLevel = magnitude;
                        }
                    }
                    else if (!binding->isPartOfComposite)
                    {
                        // Check actuation of each control on the binding.
                        for (var n = 0; n < binding->controlCount; ++n)
                        {
                            var controlIndex = binding->controlStartIndex + n;
                            var magnitude = memory.controlMagnitudes[controlIndex];

                            if (magnitude > 0)
                                ++numActuations;

                            if (magnitude > highestActuationLevel)
                            {
                                controlWithHighestActuation = controlIndex;
                                bindingWithHighestActuation = bindingIndex;
                                highestActuationLevel = magnitude;
                            }
                        }
                    }
                }

                // Update our record of whether there are multiple concurrent actuations.
                if (numActuations <= 1)
                    actionState->hasMultipleConcurrentActuations = false;

                // If we didn't find a control with a higher actuation level, then go and process
                // the control value change.
                if (controlWithHighestActuation != kInvalidIndex)
                {
                    // We do have a control with a higher actuation level. Switch from our current
                    // control to processing the control with the now highest actuation level.
                    //
                    // NOTE: We are processing an artificial control state change here. Information
                    //       such as the timestamp will not correspond to when the control actually
                    //       changed value. However, if we skip processing this as a separate control
                    //       change here, interactions may not behave properly as they would not be
                    //       seeing that we just lowered the actuation level on the action.
                    trigger.controlIndex = controlWithHighestActuation;
                    trigger.bindingIndex = bindingWithHighestActuation;
                    trigger.magnitude = highestActuationLevel;

                    // If we're switching to a different binding, we may also have to switch to a
                    // different stack of interactions.
                    if (actionState->bindingIndex != bindingWithHighestActuation)
                    {
                        // If there's an interaction currently driving the action, reset it.
                        // NOTE: This will also cancel an ongoing timer. So, say we're currently 0.5 seconds into
                        //       a 1 second "Hold" when the user shifts to a different control, then this code here
                        //       will *cancel* the current "Hold" and restart from scratch.
                        if (actionState->interactionIndex != kInvalidIndex)
                            ResetInteractionState(actionState->interactionIndex);

                        // If there's an interaction in progress on the new binding, let
                        // it drive the action.
                        var bindingState = &bindingStates[bindingWithHighestActuation];
                        var interactionCount = bindingState->interactionCount;
                        var interactionStartIndex = bindingState->interactionStartIndex;
                        for (var i = 0; i < interactionCount; ++i)
                        {
                            if (!interactionStates[interactionStartIndex + i].phase.IsInProgress())
                                continue;

                            actionState->interactionIndex = interactionStartIndex + i;
                            trigger.interactionIndex = interactionStartIndex + i;
                            break;
                        }
                    }

                    // We're switching the action to a different control so regardless of whether
                    // the processing of the control state change results in a call to ChangePhaseOfAction,
                    // we need to record this or the disambiguation code may start ignoring valid input.
                    actionState->controlIndex = controlWithHighestActuation;
                    actionState->bindingIndex = bindingWithHighestActuation;
                    actionState->magnitude = highestActuationLevel;

                    Profiler.EndSample();
                    return false;
                }
            }

            Profiler.EndSample();

            // If we're not really effecting any change on the action, ignore the control state change.
            // NOTE: We may be looking at a control here that points in a completely direction, for example, even
            //       though it has the same magnitude. However, we require a control to *increase* absolute actuation
            //       before we let it drive the action.
            if (!isControlCurrentlyDrivingTheAction && Mathf.Approximately(trigger.magnitude, actionState->magnitude))
            {
                // If we do have an actuation on a control that isn't currently driving the action, flag the action has
                // having multiple concurrent inputs ATM.
                if (trigger.magnitude > 0)
                    actionState->hasMultipleConcurrentActuations = true;
                return true;
            }

            return false;
        }

        private ushort GetActionBindingStartIndexAndCount(int actionIndex, out ushort bindingCount)
        {
            bindingCount = memory.actionBindingIndicesAndCounts[actionIndex * 2 + 1];
            return memory.actionBindingIndicesAndCounts[actionIndex * 2];
        }

        /// <summary>
        /// When there is no interaction on an action, this method perform the default interaction logic that we
        /// run when a bound control changes value.
        /// </summary>
        /// <param name="trigger">Control trigger state.</param>
        /// <param name="actionIndex"></param>
        /// <remarks>
        /// The default interaction does not have its own <see cref="InteractionState"/>. Whatever we do in here,
        /// we store directly on the action state.
        ///
        /// The default interaction is basically a sort of optimization where we don't require having an explicit
        /// interaction object. Conceptually, it can be thought of, however, as putting this interaction on any
        /// binding that doesn't have any other interaction on it.
        /// </remarks>
        private void ProcessDefaultInteraction(ref TriggerState trigger, int actionIndex)
        {
            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount,
                "Action index out of range when processing default interaction");

            var actionState = &actionStates[actionIndex];
            switch (actionState->phase)
            {
                case InputActionPhase.Waiting:
                {
                    // Pass-through actions we perform on every value change and then go back
                    // to waiting.
                    if (trigger.isPassThrough)
                    {
                        ChangePhaseOfAction(InputActionPhase.Performed, ref trigger,
                            phaseAfterPerformedOrCanceled: InputActionPhase.Waiting);
                        break;
                    }
                    // Button actions need to cross the button-press threshold.
                    if (trigger.isButton)
                    {
                        var actuation = trigger.magnitude;
                        if (actuation > 0)
                            ChangePhaseOfAction(InputActionPhase.Started, ref trigger);
                        var threshold = controls[trigger.controlIndex] is ButtonControl button ? button.pressPointOrDefault : ButtonControl.s_GlobalDefaultButtonPressPoint;
                        if (actuation >= threshold)
                        {
                            ChangePhaseOfAction(InputActionPhase.Performed, ref trigger,
                                phaseAfterPerformedOrCanceled: InputActionPhase.Performed);
                        }
                    }
                    else
                    {
                        // Value-type action.
                        // Ignore if the control has not crossed its actuation threshold.
                        if (IsActuated(ref trigger))
                        {
                            ////REVIEW: Why is it we don't stay in performed but rather go back to started all the time?

                            // Go into started, then perform and then go back to started.
                            ChangePhaseOfAction(InputActionPhase.Started, ref trigger);
                            ChangePhaseOfAction(InputActionPhase.Performed, ref trigger,
                                phaseAfterPerformedOrCanceled: InputActionPhase.Started);
                        }
                    }

                    break;
                }

                case InputActionPhase.Started:
                {
                    if (actionState->isButton)
                    {
                        var actuation = trigger.magnitude;
                        var threshold = controls[trigger.controlIndex] is ButtonControl button ? button.pressPointOrDefault : ButtonControl.s_GlobalDefaultButtonPressPoint;
                        if (actuation >= threshold)
                        {
                            // Button crossed press threshold. Perform.
                            ChangePhaseOfAction(InputActionPhase.Performed, ref trigger,
                                phaseAfterPerformedOrCanceled: InputActionPhase.Performed);
                        }
                        else if (Mathf.Approximately(actuation, 0))
                        {
                            // Button is no longer actuated. Never reached threshold to perform.
                            // Cancel.
                            ChangePhaseOfAction(InputActionPhase.Canceled, ref trigger);
                        }
                    }
                    else
                    {
                        if (!IsActuated(ref trigger))
                        {
                            // Control went back to below actuation threshold. Cancel interaction.
                            ChangePhaseOfAction(InputActionPhase.Canceled, ref trigger);
                        }
                        else
                        {
                            // Control changed value above magnitude threshold. Perform and remain started.
                            ChangePhaseOfAction(InputActionPhase.Performed, ref trigger,
                                phaseAfterPerformedOrCanceled: InputActionPhase.Started);
                        }
                    }
                    break;
                }

                case InputActionPhase.Performed:
                {
                    if (actionState->isButton)
                    {
                        var actuation = trigger.magnitude;
                        var pressPoint = controls[trigger.controlIndex] is ButtonControl button ? button.pressPointOrDefault : ButtonControl.s_GlobalDefaultButtonPressPoint;
                        if (Mathf.Approximately(0f, actuation))
                        {
                            ChangePhaseOfAction(InputActionPhase.Canceled, ref trigger);
                        }
                        else
                        {
                            var threshold = pressPoint * ButtonControl.s_GlobalDefaultButtonReleaseThreshold;
                            if (actuation <= threshold)
                            {
                                // Button released to below threshold but not fully released.
                                ChangePhaseOfAction(InputActionPhase.Started, ref trigger);
                            }
                        }
                    }
                    else if (actionState->isPassThrough)
                    {
                        ////REVIEW: even for pass-through actions, shouldn't we cancel when seeing a default value?
                        ChangePhaseOfAction(InputActionPhase.Performed, ref trigger,
                            phaseAfterPerformedOrCanceled: InputActionPhase.Performed);
                    }
                    break;
                }

                default:
                    Debug.Assert(false, "Should not get here");
                    break;
            }
        }

        private void ProcessInteractions(ref TriggerState trigger, int interactionStartIndex, int interactionCount)
        {
            var context = new InputInteractionContext
            {
                m_State = this,
                m_TriggerState = trigger
            };

            for (var i = 0; i < interactionCount; ++i)
            {
                var index = interactionStartIndex + i;
                var state = interactionStates[index];
                var interaction = interactions[index];

                context.m_TriggerState.phase = state.phase;
                context.m_TriggerState.startTime = state.startTime;
                context.m_TriggerState.interactionIndex = index;

                interaction.Process(ref context);
            }
        }

        private void ProcessTimeout(double time, int mapIndex, int controlIndex, int bindingIndex, int interactionIndex)
        {
            Debug.Assert(controlIndex >= 0 && controlIndex < totalControlCount, "Control index out of range");
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            Debug.Assert(interactionIndex >= 0 && interactionIndex < totalInteractionCount, "Interaction index out of range");

            ref var currentState = ref interactionStates[interactionIndex];

            var context = new InputInteractionContext
            {
                m_State = this,
                m_TriggerState =
                    new TriggerState
                {
                    phase = currentState.phase,
                    time = time,
                    mapIndex = mapIndex,
                    controlIndex = controlIndex,
                    bindingIndex = bindingIndex,
                    interactionIndex = interactionIndex,
                    startTime = currentState.startTime
                },
                timerHasExpired = true,
            };

            currentState.isTimerRunning = false;
            currentState.totalTimeoutCompletionTimeRemaining =
                Mathf.Max(currentState.totalTimeoutCompletionTimeRemaining - currentState.timerDuration, 0);
            currentState.timerDuration = default;

            // Let interaction handle timer expiration.
            interactions[interactionIndex].Process(ref context);
        }

        internal void SetTotalTimeoutCompletionTime(float seconds, ref TriggerState trigger)
        {
            Debug.Assert(trigger.interactionIndex >= 0 && trigger.interactionIndex < totalInteractionCount, "Interaction index out of range");

            ref var interactionState = ref interactionStates[trigger.interactionIndex];
            interactionState.totalTimeoutCompletionDone = 0;
            interactionState.totalTimeoutCompletionTimeRemaining = seconds;
        }

        internal void StartTimeout(float seconds, ref TriggerState trigger)
        {
            Debug.Assert(trigger.mapIndex >= 0 && trigger.mapIndex < totalMapCount, "Map index out of range");
            Debug.Assert(trigger.controlIndex >= 0 && trigger.controlIndex < totalControlCount, "Control index out of range");
            Debug.Assert(trigger.interactionIndex >= 0 && trigger.interactionIndex < totalInteractionCount, "Interaction index out of range");

            var manager = InputSystem.s_Manager;
            var currentTime = trigger.time;
            var control = controls[trigger.controlIndex];
            var interactionIndex = trigger.interactionIndex;
            var monitorIndex =
                ToCombinedMapAndControlAndBindingIndex(trigger.mapIndex, trigger.controlIndex, trigger.bindingIndex);

            // If there's already a timeout running, cancel it first.
            ref var interactionState = ref interactionStates[interactionIndex];
            if (interactionState.isTimerRunning)
                StopTimeout(interactionIndex);

            // Add new timeout.
            manager.AddStateChangeMonitorTimeout(control, this, currentTime + seconds, monitorIndex,
                interactionIndex);

            // Update state.
            interactionState.isTimerRunning = true;
            interactionState.timerStartTime = currentTime;
            interactionState.timerDuration = seconds;
            interactionState.timerMonitorIndex = monitorIndex;
        }

        private void StopTimeout(int interactionIndex)
        {
            Debug.Assert(interactionIndex >= 0 && interactionIndex < totalInteractionCount, "Interaction index out of range");

            ref var interactionState = ref interactionStates[interactionIndex];

            var manager = InputSystem.s_Manager;
            manager.RemoveStateChangeMonitorTimeout(this, interactionState.timerMonitorIndex, interactionIndex);

            // Update state.
            interactionState.isTimerRunning = false;
            interactionState.totalTimeoutCompletionDone += interactionState.timerDuration;
            interactionState.totalTimeoutCompletionTimeRemaining =
                Mathf.Max(interactionState.totalTimeoutCompletionTimeRemaining - interactionState.timerDuration, 0);
            interactionState.timerDuration = default;
            interactionState.timerStartTime = default;
            interactionState.timerMonitorIndex = default;
        }

        /// <summary>
        /// Perform a phase change on the given interaction. Only visible to observers
        /// if it happens to change the phase of the action, too.
        /// </summary>
        /// <param name="newPhase">New phase to transition the interaction to.</param>
        /// <param name="trigger">Information about the binding and control that triggered the phase change.</param>
        /// <param name="phaseAfterPerformed">If <paramref name="newPhase"/> is <see cref="InputActionPhase.Performed"/>,
        /// this determines which phase to transition to after the action has been performed. This would usually be
        /// <see cref="InputActionPhase.Waiting"/> (default), <see cref="InputActionPhase.Started"/> (if the action is supposed
        /// to be oscillate between started and performed), or <see cref="InputActionPhase.Performed"/> (if the action is
        /// supposed to perform over and over again until canceled).</param>
        /// <param name="processNextInteractionOnCancel">Indicates if the system should try and change the phase of other
        /// interactions on the same action that are already started or performed after cancelling this interaction. This should be
        /// false when resetting interactions.</param>
        /// <remarks>
        /// Multiple interactions on the same binding can be started concurrently but the
        /// first interaction that starts will get to drive an action until it either cancels
        /// or performs the action.
        ///
        /// If an interaction driving an action performs it, all interactions will reset and
        /// go back waiting.
        ///
        /// If an interaction driving an action cancels it, the next interaction in the list which
        /// has already started will get to drive the action (example: a TapInteraction and a
        /// SlowTapInteraction both start and the TapInteraction gets to drive the action because
        /// it comes first; then the TapInteraction cancels because the button is held for too
        /// long and the SlowTapInteraction will get to drive the action next).
        /// </remarks>
        internal void ChangePhaseOfInteraction(InputActionPhase newPhase, ref TriggerState trigger,
            InputActionPhase phaseAfterPerformed = InputActionPhase.Waiting, bool processNextInteractionOnCancel = true)
        {
            var interactionIndex = trigger.interactionIndex;
            var bindingIndex = trigger.bindingIndex;

            Debug.Assert(interactionIndex >= 0 && interactionIndex < totalInteractionCount, "Interaction index out of range");
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");

            ////TODO: need to make sure that performed and canceled phase changes happen on the *same* binding&control
            ////      as the start of the phase

            var phaseAfterPerformedOrCanceled = InputActionPhase.Waiting;
            if (newPhase == InputActionPhase.Performed)
                phaseAfterPerformedOrCanceled = phaseAfterPerformed;

            // Any time an interaction changes phase, we cancel all pending timeouts.
            ref var interactionState = ref interactionStates[interactionIndex];
            if (interactionState.isTimerRunning)
                StopTimeout(trigger.interactionIndex);

            // Update interaction state.
            interactionState.phase = newPhase;
            interactionState.triggerControlIndex = trigger.controlIndex;
            interactionState.startTime = trigger.startTime;
            if (newPhase == InputActionPhase.Performed)
                interactionState.performedTime = trigger.time;

            // See if it affects the phase of an associated action.
            var actionIndex = bindingStates[bindingIndex].actionIndex; // We already had to tap this array and entry in ProcessControlStateChange.
            if (actionIndex != -1)
            {
                if (actionStates[actionIndex].phase == InputActionPhase.Waiting)
                {
                    // We're the first interaction to go to the start phase.
                    if (!ChangePhaseOfAction(newPhase, ref trigger,
                        phaseAfterPerformedOrCanceled: phaseAfterPerformedOrCanceled))
                        return;
                }
                else if (newPhase == InputActionPhase.Canceled && actionStates[actionIndex].interactionIndex == trigger.interactionIndex)
                {
                    // We're canceling but maybe there's another interaction ready
                    // to go into start phase. *Or* there's an interaction that has
                    // already performed.

                    if (!ChangePhaseOfAction(newPhase, ref trigger))
                        return;

                    if (processNextInteractionOnCancel == false)
                        return;

                    var interactionStartIndex = bindingStates[bindingIndex].interactionStartIndex;
                    var numInteractions = bindingStates[bindingIndex].interactionCount;
                    for (var i = 0; i < numInteractions; ++i)
                    {
                        var index = interactionStartIndex + i;
                        if (index != trigger.interactionIndex && (interactionStates[index].phase == InputActionPhase.Started ||
                                                                  interactionStates[index].phase == InputActionPhase.Performed))
                        {
                            // Trigger start.
                            var startTime = interactionStates[index].startTime;
                            var triggerForInteraction = new TriggerState
                            {
                                phase = InputActionPhase.Started,
                                controlIndex = interactionStates[index].triggerControlIndex,
                                bindingIndex = trigger.bindingIndex,
                                interactionIndex = index,
                                mapIndex = trigger.mapIndex,
                                time = startTime,
                                startTime = startTime,
                            };
                            if (!ChangePhaseOfAction(InputActionPhase.Started, ref triggerForInteraction))
                                return;

                            // If the interaction has already performed, trigger it now.
                            if (interactionStates[index].phase == InputActionPhase.Performed)
                            {
                                triggerForInteraction = new TriggerState
                                {
                                    phase = InputActionPhase.Performed,
                                    controlIndex = interactionStates[index].triggerControlIndex,
                                    bindingIndex = trigger.bindingIndex,
                                    interactionIndex = index,
                                    mapIndex = trigger.mapIndex,
                                    time = interactionStates[index].performedTime, // Time when the interaction performed.
                                    startTime = startTime,
                                };
                                if (!ChangePhaseOfAction(InputActionPhase.Performed, ref triggerForInteraction))
                                    return;
                            }
                            break;
                        }
                    }
                }
                else if (actionStates[actionIndex].interactionIndex == trigger.interactionIndex)
                {
                    // Any other phase change goes to action if we're the interaction driving
                    // the current phase.
                    if (!ChangePhaseOfAction(newPhase, ref trigger, phaseAfterPerformedOrCanceled))
                        return;

                    // We're the interaction driving the action and we performed the action,
                    // so reset any other interaction to waiting state.
                    if (newPhase == InputActionPhase.Performed)
                    {
                        var interactionStartIndex = bindingStates[bindingIndex].interactionStartIndex;
                        var numInteractions = bindingStates[bindingIndex].interactionCount;
                        for (var i = 0; i < numInteractions; ++i)
                        {
                            var index = interactionStartIndex + i;
                            if (index != trigger.interactionIndex)
                                ResetInteractionState(index);
                        }
                    }
                }
            }

            // If the interaction performed or canceled, go back to waiting.
            // Exception: if it was performed and we're to remain in started state, set the interaction
            //            to started. Note that for that phase transition, there are no callbacks being
            //            triggered (i.e. we don't call 'started' every time after 'performed').
            if (newPhase == InputActionPhase.Performed && actionStates[actionIndex].interactionIndex != trigger.interactionIndex)
            {
                // We performed but we're not the interaction driving the action. We want to stay performed to make
                // sure that if the interaction that is currently driving the action cancels, we get to perform
                // the action. If we go back to waiting here, then the system can't tell that there's another interaction
                // ready to perform (in fact, that has already performed).
            }
            else if (newPhase == InputActionPhase.Performed && phaseAfterPerformed != InputActionPhase.Waiting)
            {
                interactionState.phase = phaseAfterPerformed;
            }
            else if (newPhase == InputActionPhase.Performed || newPhase == InputActionPhase.Canceled)
            {
                ResetInteractionState(trigger.interactionIndex);
            }
        }

        /// <summary>
        /// Change the current phase of the action referenced by <paramref name="trigger"/> to <paramref name="newPhase"/>.
        /// </summary>
        /// <param name="newPhase">New phase to transition to.</param>
        /// <param name="trigger">Trigger that caused the change in phase.</param>
        /// <param name="phaseAfterPerformedOrCanceled"></param>
        /// <remarks>
        /// The change in phase is visible to observers, i.e. on the various callbacks and notifications.
        ///
        /// If <paramref name="newPhase"/> is <see cref="InputActionPhase.Performed"/> or <see cref="InputActionPhase.Canceled"/>,
        /// the action will subsequently immediately transition to <paramref name="phaseAfterPerformedOrCanceled"/>
        /// (<see cref="InputActionPhase.Waiting"/> by default). This change is not visible to observers, i.e. there won't
        /// be another run through callbacks.
        /// </remarks>
        private bool ChangePhaseOfAction(InputActionPhase newPhase, ref TriggerState trigger,
            InputActionPhase phaseAfterPerformedOrCanceled = InputActionPhase.Waiting)
        {
            Debug.Assert(newPhase != InputActionPhase.Disabled, "Should not disable an action using this method");
            Debug.Assert(trigger.mapIndex >= 0 && trigger.mapIndex < totalMapCount, "Map index out of range");
            Debug.Assert(trigger.controlIndex >= 0 && trigger.controlIndex < totalControlCount, "Control index out of range");
            Debug.Assert(trigger.bindingIndex >= 0 && trigger.bindingIndex < totalBindingCount, "Binding index out of range");

            var actionIndex = bindingStates[trigger.bindingIndex].actionIndex;
            if (actionIndex == kInvalidIndex)
                return true; // No action associated with binding.

            // Ignore if action is disabled.
            var actionState = &actionStates[actionIndex];
            if (actionState->isDisabled)
                return true;

            // We mark the action as in-processing while we execute its phase transitions and perform
            // callbacks. The callbacks may alter system state such that the action may get disabled
            // (and potentially re-enabled) while the callback is in progress. We need to make sure that
            // if that happens, we don't go and then do more processing on the action.
            actionState->inProcessing = true;
            try
            {
                // Enforce transition constraints.
                if (actionState->isPassThrough && trigger.interactionIndex == kInvalidIndex)
                {
                    // No constraints on pass-through actions except if there are interactions driving the action.
                    ChangePhaseOfActionInternal(actionIndex, actionState, newPhase, ref trigger);
                    if (!actionState->inProcessing)
                        return false;
                }
                else if (newPhase == InputActionPhase.Performed && actionState->phase == InputActionPhase.Waiting)
                {
                    // Going from waiting to performed, we make a detour via started.
                    ChangePhaseOfActionInternal(actionIndex, actionState, InputActionPhase.Started, ref trigger);
                    if (!actionState->inProcessing)
                        return false;

                    // Then we perform.
                    ChangePhaseOfActionInternal(actionIndex, actionState, newPhase, ref trigger);
                    if (!actionState->inProcessing)
                        return false;

                    // And finally, if we're going back to waiting, we make a detour via canceled.
                    if (phaseAfterPerformedOrCanceled == InputActionPhase.Waiting)
                        ChangePhaseOfActionInternal(actionIndex, actionState, InputActionPhase.Canceled, ref trigger);
                    if (!actionState->inProcessing)
                        return false;

                    actionState->phase = phaseAfterPerformedOrCanceled;
                }
                else if (actionState->phase != newPhase || newPhase == InputActionPhase.Performed) // We allow Performed to trigger repeatedly.
                {
                    ChangePhaseOfActionInternal(actionIndex, actionState, newPhase, ref trigger);
                    if (!actionState->inProcessing)
                        return false;

                    if (newPhase == InputActionPhase.Performed || newPhase == InputActionPhase.Canceled)
                        actionState->phase = phaseAfterPerformedOrCanceled;
                }
            }
            finally
            {
                actionState->inProcessing = false;
            }

            // If we're now waiting, reset control state. This is important for the disambiguation code
            // to not consider whatever control actuation happened on the action last.
            if (actionState->phase == InputActionPhase.Waiting)
            {
                actionState->controlIndex = kInvalidIndex;
                actionState->flags &= ~TriggerState.Flags.HaveMagnitude;
            }

            return true;
        }

        private void ChangePhaseOfActionInternal(int actionIndex, TriggerState* actionState, InputActionPhase newPhase, ref TriggerState trigger)
        {
            Debug.Assert(trigger.mapIndex == actionState->mapIndex,
                "Map index on trigger does not correspond to map index of trigger state");

            // Update action state.
            var newState = trigger;

            // We need to make sure here that any HaveMagnitude flag we may be carrying over from actionState
            // is handled correctly (case 1239551).
            newState.flags = actionState->flags; // Preserve flags.
            if (newPhase != InputActionPhase.Canceled)
                newState.magnitude = trigger.magnitude;
            else
                newState.magnitude = 0;

            newState.phase = newPhase;
            if (newPhase == InputActionPhase.Performed)
            {
                newState.lastPerformedInUpdate = InputUpdate.s_UpdateStepCount;
                newState.lastCanceledInUpdate = actionState->lastCanceledInUpdate;

                // When we perform an action, we mark the event handled such that FireStateChangeNotifications()
                // can then reset state monitors in the same group.
                // NOTE: We don't consume for controls at binding complexity 1. Those we fire in unison.
                if (controlGroupingAndComplexity[trigger.controlIndex * 2 + 1] > 1 &&
                    // we can end up switching to performed state from an interaction with a timeout, at which point
                    // the original event will probably have been removed from memory, so make sure to check
                    // we still have one
                    m_CurrentlyProcessingThisEvent.valid)
                    m_CurrentlyProcessingThisEvent.handled = true;
            }
            else if (newPhase == InputActionPhase.Canceled)
            {
                newState.lastCanceledInUpdate = InputUpdate.s_UpdateStepCount;
                newState.lastPerformedInUpdate = actionState->lastPerformedInUpdate;
            }
            else
            {
                newState.lastPerformedInUpdate = actionState->lastPerformedInUpdate;
                newState.lastCanceledInUpdate = actionState->lastCanceledInUpdate;
            }
            newState.pressedInUpdate = actionState->pressedInUpdate;
            newState.releasedInUpdate = actionState->releasedInUpdate;
            if (newPhase == InputActionPhase.Started)
                newState.startTime = newState.time;
            *actionState = newState;

            // Let listeners know.
            var map = maps[trigger.mapIndex];
            Debug.Assert(actionIndex >= mapIndices[trigger.mapIndex].actionStartIndex,
                "actionIndex is below actionStartIndex for map that the action belongs to");
            var action = map.m_Actions[actionIndex - mapIndices[trigger.mapIndex].actionStartIndex];
            trigger.phase = newPhase;
            switch (newPhase)
            {
                case InputActionPhase.Started:
                {
                    Debug.Assert(trigger.controlIndex != -1, "Must have control to start an action");
                    CallActionListeners(actionIndex, map, newPhase, ref action.m_OnStarted, "started");
                    break;
                }

                case InputActionPhase.Performed:
                {
                    Debug.Assert(trigger.controlIndex != -1, "Must have control to perform an action");
                    CallActionListeners(actionIndex, map, newPhase, ref action.m_OnPerformed, "performed");
                    break;
                }

                case InputActionPhase.Canceled:
                {
                    Debug.Assert(trigger.controlIndex != -1, "When canceling, must have control that started action");
                    CallActionListeners(actionIndex, map, newPhase, ref action.m_OnCanceled, "canceled");
                    break;
                }
            }
        }

        private void CallActionListeners(int actionIndex, InputActionMap actionMap, InputActionPhase phase, ref CallbackArray<InputActionListener> listeners, string callbackName)
        {
            // If there's no listeners, don't bother with anything else.
            var callbacksOnMap = actionMap.m_ActionCallbacks;
            if (listeners.length == 0 && callbacksOnMap.length == 0 && s_GlobalState.onActionChange.length == 0)
                return;

            var context = new InputAction.CallbackContext
            {
                m_State = this,
                m_ActionIndex = actionIndex,
            };

            Profiler.BeginSample("InputActionCallback");

            // Global callback goes first.
            var action = context.action;
            if (s_GlobalState.onActionChange.length > 0)
            {
                InputActionChange change;
                switch (phase)
                {
                    case InputActionPhase.Started:
                        change = InputActionChange.ActionStarted;
                        break;
                    case InputActionPhase.Performed:
                        change = InputActionChange.ActionPerformed;
                        break;
                    case InputActionPhase.Canceled:
                        change = InputActionChange.ActionCanceled;
                        break;
                    default:
                        Debug.Assert(false, "Should not reach here");
                        return;
                }

                DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionChange, action, change, "InputSystem.onActionChange");
            }

            // Run callbacks (if any) directly on action.
            DelegateHelpers.InvokeCallbacksSafe(ref listeners, context, callbackName, action);

            // Run callbacks (if any) on action map.
            DelegateHelpers.InvokeCallbacksSafe(ref callbacksOnMap, context, callbackName, actionMap);

            Profiler.EndSample();
        }

        private object GetActionOrNoneString(ref TriggerState trigger)
        {
            var action = GetActionOrNull(ref trigger);
            if (action == null)
                return "<none>";
            return action;
        }

        internal InputAction GetActionOrNull(int bindingIndex)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");

            var actionIndex = bindingStates[bindingIndex].actionIndex;
            if (actionIndex == kInvalidIndex)
                return null;

            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount,
                "Action index out of range when getting action");
            var mapIndex = bindingStates[bindingIndex].mapIndex;
            var actionStartIndex = mapIndices[mapIndex].actionStartIndex;
            return maps[mapIndex].m_Actions[actionIndex - actionStartIndex];
        }

        internal InputAction GetActionOrNull(ref TriggerState trigger)
        {
            Debug.Assert(trigger.mapIndex >= 0 && trigger.mapIndex < totalMapCount, "Map index out of range");
            Debug.Assert(trigger.bindingIndex >= 0 && trigger.bindingIndex < totalBindingCount, "Binding index out of range");

            var actionIndex = bindingStates[trigger.bindingIndex].actionIndex;
            if (actionIndex == kInvalidIndex)
                return null;

            Debug.Assert(actionIndex >= 0 && actionIndex < totalActionCount, "Action index out of range");
            var actionStartIndex = mapIndices[trigger.mapIndex].actionStartIndex;
            return maps[trigger.mapIndex].m_Actions[actionIndex - actionStartIndex];
        }

        internal InputControl GetControl(ref TriggerState trigger)
        {
            Debug.Assert(trigger.controlIndex != kInvalidIndex, "Control index is invalid");
            Debug.Assert(trigger.controlIndex >= 0 && trigger.controlIndex < totalControlCount, "Control index out of range");
            return controls[trigger.controlIndex];
        }

        private IInputInteraction GetInteractionOrNull(ref TriggerState trigger)
        {
            if (trigger.interactionIndex == kInvalidIndex)
                return null;

            Debug.Assert(trigger.interactionIndex >= 0 && trigger.interactionIndex < totalInteractionCount, "Interaction index out of range");
            return interactions[trigger.interactionIndex];
        }

        internal int GetBindingIndexInMap(int bindingIndex)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            var mapIndex = bindingStates[bindingIndex].mapIndex;
            var bindingStartIndex = mapIndices[mapIndex].bindingStartIndex;
            return bindingIndex - bindingStartIndex;
        }

        internal int GetBindingIndexInState(int mapIndex, int bindingIndexInMap)
        {
            var bindingStartIndex = mapIndices[mapIndex].bindingStartIndex;
            return bindingStartIndex + bindingIndexInMap;
        }

        // Iterators may not use unsafe code so do the detour here.
        internal ref BindingState GetBindingState(int bindingIndex)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            return ref bindingStates[bindingIndex];
        }

        internal ref InputBinding GetBinding(int bindingIndex)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            var mapIndex = bindingStates[bindingIndex].mapIndex;
            var bindingStartIndex = mapIndices[mapIndex].bindingStartIndex;
            return ref maps[mapIndex].m_Bindings[bindingIndex - bindingStartIndex];
        }

        internal InputActionMap GetActionMap(int bindingIndex)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            var mapIndex = bindingStates[bindingIndex].mapIndex;
            return maps[mapIndex];
        }

        private void ResetInteractionStateAndCancelIfNecessary(int mapIndex, int bindingIndex, int interactionIndex)
        {
            Debug.Assert(interactionIndex >= 0 && interactionIndex < totalInteractionCount, "Interaction index out of range");
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");

            // If interaction is currently driving an action and it has been started or performed,
            // cancel it.
            //
            // NOTE: We could just blindly call ChangePhaseOfInteraction() and it would handle the case of
            //       when the interaction is currently driving the action automatically. However, doing so
            //       would give other interactions a chance to take over which is something we don't want to
            //       happen when resetting actions.
            var actionIndex = bindingStates[bindingIndex].actionIndex;
            if (actionStates[actionIndex].interactionIndex == interactionIndex)
            {
                switch (interactionStates[interactionIndex].phase)
                {
                    case InputActionPhase.Started:
                    case InputActionPhase.Performed:
                        ChangePhaseOfInteraction(InputActionPhase.Canceled, ref actionStates[actionIndex], processNextInteractionOnCancel: false);
                        break;
                }

                actionStates[actionIndex].interactionIndex = kInvalidIndex;
            }

            ResetInteractionState(interactionIndex);
        }

        private void ResetInteractionState(int interactionIndex)
        {
            Debug.Assert(interactionIndex >= 0 && interactionIndex < totalInteractionCount, "Interaction index out of range");

            // Clean up internal state that the interaction may keep.
            interactions[interactionIndex].Reset();

            // Clean up timer.
            if (interactionStates[interactionIndex].isTimerRunning)
                StopTimeout(interactionIndex);

            // Reset state record.
            interactionStates[interactionIndex] =
                new InteractionState
            {
                // We never set interactions to disabled. This way we don't have to go through them
                // when we disable/enable actions.
                phase = InputActionPhase.Waiting,
                triggerControlIndex = kInvalidIndex
            };
        }

        internal int GetValueSizeInBytes(int bindingIndex, int controlIndex)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            Debug.Assert(controlIndex >= 0 && controlIndex < totalControlCount, "Control index out of range");

            if (bindingStates[bindingIndex].isPartOfComposite) ////TODO: instead, just have compositeOrCompositeBindingIndex be invalid
            {
                var compositeBindingIndex = bindingStates[bindingIndex].compositeOrCompositeBindingIndex;
                var compositeIndex = bindingStates[compositeBindingIndex].compositeOrCompositeBindingIndex;
                var compositeObject = composites[compositeIndex];
                Debug.Assert(compositeObject != null, "Composite object on composite state is null");

                return compositeObject.valueSizeInBytes;
            }

            var control = controls[controlIndex];
            Debug.Assert(control != null, "Control at given index is null");
            return control.valueSizeInBytes;
        }

        internal Type GetValueType(int bindingIndex, int controlIndex)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            Debug.Assert(controlIndex >= 0 && controlIndex < totalControlCount, "Control index out of range");

            if (bindingStates[bindingIndex].isPartOfComposite) ////TODO: instead, just have compositeOrCompositeBindingIndex be invalid
            {
                var compositeBindingIndex = bindingStates[bindingIndex].compositeOrCompositeBindingIndex;
                var compositeIndex = bindingStates[compositeBindingIndex].compositeOrCompositeBindingIndex;
                var compositeObject = composites[compositeIndex];
                Debug.Assert(compositeObject != null, "Composite object is null");

                return compositeObject.valueType;
            }

            var control = controls[controlIndex];
            Debug.Assert(control != null, "Control is null");
            return control.valueType;
        }

        internal static bool IsActuated(ref TriggerState trigger, float threshold = 0)
        {
            var magnitude = trigger.magnitude;
            if (magnitude < 0)
                return true;
            if (Mathf.Approximately(threshold, 0))
                return magnitude > 0;
            return magnitude >= threshold;
        }

        ////REVIEW: we can unify the reading paths once we have blittable type constraints

        internal void ReadValue(int bindingIndex, int controlIndex, void* buffer, int bufferSize, bool ignoreComposites = false)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index out of range");
            Debug.Assert(controlIndex >= 0 && controlIndex < totalControlCount, "Control index out of range");

            InputControl control = null;

            // If the binding that triggered the action is part of a composite, let
            // the composite determine the value we return.
            if (!ignoreComposites && bindingStates[bindingIndex].isPartOfComposite)
            {
                var compositeBindingIndex = bindingStates[bindingIndex].compositeOrCompositeBindingIndex;
                var compositeIndex = bindingStates[compositeBindingIndex].compositeOrCompositeBindingIndex;
                var compositeObject = composites[compositeIndex];
                Debug.Assert(compositeObject != null, "Composite object is null");

                var context = new InputBindingCompositeContext
                {
                    m_State = this,
                    m_BindingIndex = compositeBindingIndex
                };

                compositeObject.ReadValue(ref context, buffer, bufferSize);

                // Switch bindingIndex to that of composite so that we use the right processors.
                bindingIndex = compositeBindingIndex;
            }
            else
            {
                control = controls[controlIndex];
                Debug.Assert(control != null, "Control is null");
                control.ReadValueIntoBuffer(buffer, bufferSize);
            }

            // Run value through processors, if any.
            var processorCount = bindingStates[bindingIndex].processorCount;
            if (processorCount > 0)
            {
                var processorStartIndex = bindingStates[bindingIndex].processorStartIndex;
                for (var i = 0; i < processorCount; ++i)
                    processors[processorStartIndex + i].Process(buffer, bufferSize, control);
            }
        }

        internal TValue ReadValue<TValue>(int bindingIndex, int controlIndex, bool ignoreComposites = false)
            where TValue : struct
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index is out of range");

            var value = default(TValue);

            // In the case of a composite, this will be null.
            InputControl<TValue> controlOfType = null;

            // If the binding that triggered the action is part of a composite, let
            // the composite determine the value we return.
            if (!ignoreComposites && bindingStates[bindingIndex].isPartOfComposite)
            {
                var compositeBindingIndex = bindingStates[bindingIndex].compositeOrCompositeBindingIndex;
                Debug.Assert(compositeBindingIndex >= 0 && compositeBindingIndex < totalBindingCount, "Composite binding index is out of range");
                var compositeIndex = bindingStates[compositeBindingIndex].compositeOrCompositeBindingIndex;
                var compositeObject = composites[compositeIndex];
                Debug.Assert(compositeObject != null, "Composite object is null");

                var context = new InputBindingCompositeContext
                {
                    m_State = this,
                    m_BindingIndex = compositeBindingIndex
                };

                var compositeOfType = compositeObject as InputBindingComposite<TValue>;
                if (compositeOfType == null)
                {
                    // Composite is not derived from InputBindingComposite<TValue>. Do an explicit value
                    // type check here. Might be a composite like OneModifierComposite that dynamically
                    // determines its value type based on what its parts are bound to.
                    var valueType = compositeObject.valueType;
                    if (!valueType.IsAssignableFrom(typeof(TValue)))
                        throw new InvalidOperationException(
                            $"Cannot read value of type '{typeof(TValue).Name}' from composite '{compositeObject}' bound to action '{GetActionOrNull(bindingIndex)}' (composite is a '{compositeIndex.GetType().Name}' with value type '{TypeHelpers.GetNiceTypeName(valueType)}')");

                    compositeObject.ReadValue(ref context, UnsafeUtility.AddressOf(ref value), UnsafeUtility.SizeOf<TValue>());
                }
                else
                {
                    value = compositeOfType.ReadValue(ref context);
                }

                // Switch bindingIndex to that of composite so that we use the right processors.
                bindingIndex = compositeBindingIndex;
            }
            else
            {
                if (controlIndex != kInvalidIndex)
                {
                    var control = controls[controlIndex];
                    Debug.Assert(control != null, "Control is null");

                    controlOfType = control as InputControl<TValue>;
                    if (controlOfType == null)
                        throw new InvalidOperationException(
                            $"Cannot read value of type '{TypeHelpers.GetNiceTypeName(typeof(TValue))}' from control '{control.path}' bound to action '{GetActionOrNull(bindingIndex)}' (control is a '{control.GetType().Name}' with value type '{TypeHelpers.GetNiceTypeName(control.valueType)}')");

                    value = controlOfType.value;
                }
            }

            // Run value through processors, if any.
            return ApplyProcessors(bindingIndex, value, controlOfType);
        }

        internal TValue ApplyProcessors<TValue>(int bindingIndex, TValue value, InputControl<TValue> controlOfType = null)
            where TValue : struct
        {
            var processorCount = bindingStates[bindingIndex].processorCount;
            if (processorCount > 0)
            {
                var processorStartIndex = bindingStates[bindingIndex].processorStartIndex;
                for (var i = 0; i < processorCount; ++i)
                {
                    if (processors[processorStartIndex + i] is InputProcessor<TValue> processor)
                        value = processor.Process(value, controlOfType);
                }
            }

            return value;
        }

        public float EvaluateCompositePartMagnitude(int bindingIndex, int partNumber)
        {
            var firstChildBindingIndex = bindingIndex + 1;
            var currentMagnitude = float.MinValue;
            for (var index = firstChildBindingIndex; index < totalBindingCount && bindingStates[index].isPartOfComposite; ++index)
            {
                if (bindingStates[index].partIndex != partNumber)
                    continue;

                var controlCount = bindingStates[index].controlCount;
                var controlStartIndex = bindingStates[index].controlStartIndex;
                for (var i = 0; i < controlCount; ++i)
                {
                    var control = controls[controlStartIndex + i];

                    // NOTE: We do *NOT* go to controlMagnitudes here. The reason is we may not yet have received the ProcessControlStateChange
                    //       call for a specific control that is part of the composite and thus controlMagnitudes may not yet have been updated
                    //       for a specific control.
                    currentMagnitude = Mathf.Max(control.magnitude, currentMagnitude);
                }
            }

            return currentMagnitude;
        }

        internal double GetCompositePartPressTime(int bindingIndex, int partNumber)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index is out of range");
            Debug.Assert(bindingStates[bindingIndex].isComposite, "Binding must be a composite");

            var firstChildBindingIndex = bindingIndex + 1;
            var pressTime = double.MaxValue;
            for (var index = firstChildBindingIndex; index < totalBindingCount && bindingStates[index].isPartOfComposite; ++index)
            {
                ref var bindingState = ref bindingStates[index];

                if (bindingState.partIndex != partNumber)
                    continue;

                // ReSharper disable once CompareOfFloatsByEqualityOperator
                if (bindingState.pressTime != default && bindingState.pressTime < pressTime)
                    pressTime = bindingState.pressTime;
            }

            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (pressTime == double.MaxValue)
                return -1d;

            return pressTime;
        }

        /// <summary>
        /// Read the value of the given part of a composite binding.
        /// </summary>
        /// <param name="bindingIndex">Index of the composite binding in <see cref="bindingStates"/>.</param>
        /// <param name="partNumber">Index of the part. Note that part indices start at 1!</param>
        /// <typeparam name="TValue">Value type to read. Must correspond to the value of bound controls or an exception will
        /// be thrown.</typeparam>
        /// <returns>Greatest value from among the bound controls for the given part.</returns>
        /// <remarks>
        /// Composites are composed of "parts". Each part has an associated name (e.g. "negative" or "positive") which is
        /// referenced by <see cref="InputBinding.name"/> of bindings that are part of the composite. However, multiple
        /// bindings may reference the same part (e.g. there could be a binding for "W" and another binding for "UpArrow"
        /// and both would reference the "Up" part).
        ///
        /// However, a given composite will only be interested in a single value for any given part. What we do is give
        /// a composite an integer key for every part. When it asks for a value for the given part, we go through all
        /// bindings that reference the given part and return the greatest value from among the controls of all those
        /// bindings.
        ///
        /// <example>
        /// <code>
        /// // Read a float value from the second part of the composite binding at index 3.
        /// ReadCompositePartValue&lt;float&gt;(3, 2);
        /// </code>
        /// </example>
        /// </remarks>
        internal TValue ReadCompositePartValue<TValue, TComparer>(int bindingIndex, int partNumber,
            bool* buttonValuePtr, out int controlIndex, TComparer comparer = default)
            where TValue : struct
            where TComparer : IComparer<TValue>
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index is out of range");
            Debug.Assert(bindingStates[bindingIndex].isComposite, "Binding must be a composite");

            var result = default(TValue);
            var firstChildBindingIndex = bindingIndex + 1;
            var isFirstValue = true;

            controlIndex = kInvalidIndex;

            // Find the binding in the composite that has both the given part number and
            // the greatest value.
            //
            // NOTE: It is tempting to go by control magnitudes instead as those are readily available to us (controlMagnitudes)
            //       and avoids us reading values that we're not going to use. Unfortunately, we can't do that as several controls
            //       used by a composite may all have been updated with a single event (e.g. WASD on a keyboard will usually see
            //       just one update that refreshes the entire state of the keyboard). In that case, one of the controls will
            //       see its state monitor trigger first and in turn trigger processing of the action and composite. Thus only
            //       that one single control would have its value refreshed in controlMagnitudes whereas the other control magnitudes
            //       would be stale.
            for (var index = firstChildBindingIndex; index < totalBindingCount && bindingStates[index].isPartOfComposite; ++index)
            {
                if (bindingStates[index].partIndex != partNumber)
                    continue;

                var controlCount = bindingStates[index].controlCount;
                var controlStartIndex = bindingStates[index].controlStartIndex;
                for (var i = 0; i < controlCount; ++i)
                {
                    var thisControlIndex = controlStartIndex + i;
                    var value = ReadValue<TValue>(index, thisControlIndex, ignoreComposites: true);

                    if (isFirstValue)
                    {
                        result = value;
                        controlIndex = thisControlIndex;
                        isFirstValue = false;
                    }
                    else if (comparer.Compare(value, result) > 0)
                    {
                        result = value;
                        controlIndex = thisControlIndex;
                    }

                    if (buttonValuePtr != null && controlIndex == thisControlIndex)
                    {
                        var control = controls[thisControlIndex];
                        if (control is ButtonControl button)
                        {
                            *buttonValuePtr = button.isPressed;
                        }
                        else if (control is InputControl<float>)
                        {
                            var valuePtr = UnsafeUtility.AddressOf(ref value);
                            *buttonValuePtr = *(float*)valuePtr >= ButtonControl.s_GlobalDefaultButtonPressPoint;
                        }

                        ////REVIEW: Early out here as soon as *any* button is pressed? Technically, the comparer
                        ////        could still select a different control, though...
                    }
                }
            }

            return result;
        }

        internal bool ReadCompositePartValue(int bindingIndex, int partNumber, void* buffer, int bufferSize)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index is out of range");
            Debug.Assert(bindingStates[bindingIndex].isComposite, "Binding must be a composite");

            var firstChildBindingIndex = bindingIndex + 1;

            // Find the binding in the composite that has both the given part number and
            // the greatest amount of actuation.
            var currentMagnitude = float.MinValue;
            for (var index = firstChildBindingIndex; index < totalBindingCount && bindingStates[index].isPartOfComposite; ++index)
            {
                if (bindingStates[index].partIndex != partNumber)
                    continue;

                var controlCount = bindingStates[index].controlCount;
                var controlStartIndex = bindingStates[index].controlStartIndex;
                for (var i = 0; i < controlCount; ++i)
                {
                    var thisControlIndex = controlStartIndex + i;

                    // Check if the control has greater actuation than the most actuated control
                    // we've found so far.
                    //
                    // NOTE: We cannot rely on controlMagnitudes here as several controls used by a composite may all have been updated
                    //       with a single event (e.g. WASD on a keyboard will usually see just one update that refreshes the entire state
                    //       of the keyboard). In that case, one of the controls will see its state monitor trigger first and in turn
                    //       trigger processing of the action and composite. Thus only that one single control would have its value
                    //       refreshed in controlMagnitudes whereas the other control magnitudes would be stale.
                    var control = controls[thisControlIndex];
                    var magnitude = control.magnitude;
                    if (magnitude < currentMagnitude)
                        continue;

                    // If so, read the value.
                    ReadValue(index, thisControlIndex, buffer, bufferSize, ignoreComposites: true);
                    currentMagnitude = magnitude;
                }
            }

            return currentMagnitude > float.MinValue;
        }

        internal object ReadCompositePartValueAsObject(int bindingIndex, int partNumber)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index is out of range");
            Debug.Assert(bindingStates[bindingIndex].isComposite, "Binding must be a composite");

            var firstChildBindingIndex = bindingIndex + 1;

            // Find the binding in the composite that both has the given part number and
            // the greatest amount of actuation.
            var currentMagnitude = float.MinValue;
            object currentValue = null;
            for (var index = firstChildBindingIndex; index < totalBindingCount && bindingStates[index].isPartOfComposite; ++index)
            {
                if (bindingStates[index].partIndex != partNumber)
                    continue;

                var controlCount = bindingStates[index].controlCount;
                var controlStartIndex = bindingStates[index].controlStartIndex;
                for (var i = 0; i < controlCount; ++i)
                {
                    var thisControlIndex = controlStartIndex + i;

                    // Check if the control has greater actuation than the most actuated control
                    // we've found so far.
                    //
                    // NOTE: We cannot rely on controlMagnitudes here as several controls used by a composite may all have been updated
                    //       with a single event (e.g. WASD on a keyboard will usually see just one update that refreshes the entire state
                    //       of the keyboard). In that case, one of the controls will see its state monitor trigger first and in turn
                    //       trigger processing of the action and composite. Thus only that one single control would have its value
                    //       refreshed in controlMagnitudes whereas the other control magnitudes would be stale.
                    var control = controls[thisControlIndex];
                    var magnitude = control.magnitude;
                    if (magnitude < currentMagnitude)
                        continue;

                    // If so, read the value.
                    currentValue = ReadValueAsObject(index, thisControlIndex, ignoreComposites: true);
                    currentMagnitude = magnitude;
                }
            }

            return currentValue;
        }

        internal object ReadValueAsObject(int bindingIndex, int controlIndex, bool ignoreComposites = false)
        {
            Debug.Assert(bindingIndex >= 0 && bindingIndex < totalBindingCount, "Binding index is out of range");

            InputControl control = null;
            object value = null;

            // If the binding that triggered the action is part of a composite, let
            // the composite determine the value we return.
            if (!ignoreComposites && bindingStates[bindingIndex].isPartOfComposite) ////TODO: instead, just have compositeOrCompositeBindingIndex be invalid
            {
                var compositeBindingIndex = bindingStates[bindingIndex].compositeOrCompositeBindingIndex;
                Debug.Assert(compositeBindingIndex >= 0 && compositeBindingIndex < totalBindingCount, "Binding index is out of range");
                var compositeIndex = bindingStates[compositeBindingIndex].compositeOrCompositeBindingIndex;
                var compositeObject = composites[compositeIndex];
                Debug.Assert(compositeObject != null, "Composite object is null");

                var context = new InputBindingCompositeContext
                {
                    m_State = this,
                    m_BindingIndex = compositeBindingIndex
                };

                value = compositeObject.ReadValueAsObject(ref context);

                // Switch bindingIndex to that of composite so that we use the right processors.
                bindingIndex = compositeBindingIndex;
            }
            else
            {
                if (controlIndex != kInvalidIndex)
                {
                    control = controls[controlIndex];
                    Debug.Assert(control != null, "Control is null");
                    value = control.ReadValueAsObject();
                }
            }

            if (value != null)
            {
                // Run value through processors, if any.
                var processorCount = bindingStates[bindingIndex].processorCount;
                if (processorCount > 0)
                {
                    var processorStartIndex = bindingStates[bindingIndex].processorStartIndex;
                    for (var i = 0; i < processorCount; ++i)
                        value = processors[processorStartIndex + i].ProcessAsObject(value, control);
                }
            }

            return value;
        }

        internal bool ReadValueAsButton(int bindingIndex, int controlIndex)
        {
            var buttonControl = default(ButtonControl);
            if (!bindingStates[bindingIndex].isPartOfComposite)
                buttonControl = controls[controlIndex] as ButtonControl;

            // Read float value.
            var floatValue = ReadValue<float>(bindingIndex, controlIndex);

            // Compare to press point.
            if (buttonControl != null)
                return floatValue >= buttonControl.pressPointOrDefault;
            return floatValue >= ButtonControl.s_GlobalDefaultButtonPressPoint;
        }

        /// <summary>
        /// Records the current state of a single interaction attached to a binding.
        /// Each interaction keeps track of its own trigger control and phase progression.
        /// </summary>
        [StructLayout(LayoutKind.Explicit, Size = 48)]
        internal struct InteractionState
        {
            [FieldOffset(0)] private ushort m_TriggerControlIndex;
            [FieldOffset(2)] private byte m_Phase;
            [FieldOffset(3)] private byte m_Flags;
            [FieldOffset(4)] private float m_TimerDuration;
            [FieldOffset(8)] private double m_StartTime;
            [FieldOffset(16)] private double m_TimerStartTime;
            [FieldOffset(24)] private double m_PerformedTime;
            [FieldOffset(32)] private float m_TotalTimeoutCompletionTimeDone;
            [FieldOffset(36)] private float m_TotalTimeoutCompletionTimeRemaining;
            [FieldOffset(40)] private long m_TimerMonitorIndex;

            public int triggerControlIndex
            {
                get
                {
                    if (m_TriggerControlIndex == ushort.MaxValue)
                        return kInvalidIndex;
                    return m_TriggerControlIndex;
                }
                set
                {
                    if (value == kInvalidIndex)
                        m_TriggerControlIndex = ushort.MaxValue;
                    else
                    {
                        if (value < 0 || value >= ushort.MaxValue)
                            throw new NotSupportedException("More than ushort.MaxValue-1 controls in a single InputActionState");
                        m_TriggerControlIndex = (ushort)value;
                    }
                }
            }

            public double startTime
            {
                get => m_StartTime;
                set => m_StartTime = value;
            }

            public double performedTime
            {
                get => m_PerformedTime;
                set => m_PerformedTime = value;
            }

            public double timerStartTime
            {
                get => m_TimerStartTime;
                set => m_TimerStartTime = value;
            }

            public float timerDuration
            {
                get => m_TimerDuration;
                set => m_TimerDuration = value;
            }

            public float totalTimeoutCompletionDone
            {
                get => m_TotalTimeoutCompletionTimeDone;
                set => m_TotalTimeoutCompletionTimeDone = value;
            }

            public float totalTimeoutCompletionTimeRemaining
            {
                get => m_TotalTimeoutCompletionTimeRemaining;
                set => m_TotalTimeoutCompletionTimeRemaining = value;
            }

            public long timerMonitorIndex
            {
                get => m_TimerMonitorIndex;
                set => m_TimerMonitorIndex = value;
            }

            public bool isTimerRunning
            {
                get => ((Flags)m_Flags & Flags.TimerRunning) == Flags.TimerRunning;
                set
                {
                    if (value)
                        m_Flags |= (byte)Flags.TimerRunning;
                    else
                    {
                        var mask = ~Flags.TimerRunning;
                        m_Flags &= (byte)mask;
                    }
                }
            }

            public InputActionPhase phase
            {
                get => (InputActionPhase)m_Phase;
                set => m_Phase = (byte)value;
            }

            [Flags]
            private enum Flags
            {
                TimerRunning = 1 << 0,
            }
        }

        /// <summary>
        /// Runtime state for a single binding.
        /// </summary>
        /// <remarks>
        /// Correlated to the <see cref="InputBinding"/> it corresponds to by the index in the binding
        /// array.
        /// </remarks>
        [StructLayout(LayoutKind.Explicit, Size = 32)]
        internal struct BindingState
        {
            [FieldOffset(0)] private byte m_ControlCount;
            [FieldOffset(1)] private byte m_InteractionCount;
            [FieldOffset(2)] private byte m_ProcessorCount;
            [FieldOffset(3)] private byte m_MapIndex;
            [FieldOffset(4)] private byte m_Flags;
            [FieldOffset(5)] private byte m_PartIndex;
            [FieldOffset(6)] private ushort m_ActionIndex;
            [FieldOffset(8)] private ushort m_CompositeOrCompositeBindingIndex;
            [FieldOffset(10)] private ushort m_ProcessorStartIndex;
            [FieldOffset(12)] private ushort m_InteractionStartIndex;
            [FieldOffset(14)] private ushort m_ControlStartIndex;
            [FieldOffset(16)] private double m_PressTime;
            [FieldOffset(24)] private int m_TriggerEventIdForComposite;
            [FieldOffset(28)] private int __padding; // m_PressTime double must be aligned

            [Flags]
            public enum Flags
            {
                ChainsWithNext = 1 << 0,
                EndOfChain = 1 << 1,
                Composite = 1 << 2,
                PartOfComposite = 1 << 3,
                InitialStateCheckPending = 1 << 4,
                WantsInitialStateCheck = 1 << 5,
            }

            /// <summary>
            /// Index into <see cref="controls"/> of first control associated with the binding.
            /// </summary>
            /// <remarks>
            /// For composites, this is the index of the first control that is bound by any of the parts in the composite.
            /// </remarks>
            public int controlStartIndex
            {
                get => m_ControlStartIndex;
                set
                {
                    Debug.Assert(value != kInvalidIndex, "Control state index is invalid");
                    if (value >= ushort.MaxValue)
                        throw new NotSupportedException("Total control count in state cannot exceed byte.MaxValue=" + ushort.MaxValue);
                    m_ControlStartIndex = (ushort)value;
                }
            }

            /// <summary>
            /// Number of controls associated with this binding.
            /// </summary>
            /// <remarks>
            /// For composites, this is the total number of controls bound by all parts of the composite combined.
            /// </remarks>
            public int controlCount
            {
                get => m_ControlCount;
                set
                {
                    if (value >= byte.MaxValue)
                        throw new NotSupportedException("Control count per binding cannot exceed byte.MaxValue=" + byte.MaxValue);
                    m_ControlCount = (byte)value;
                }
            }

            /// <summary>
            /// Index into <see cref="InputActionState.interactionStates"/> of first interaction associated with the binding.
            /// </summary>
            public int interactionStartIndex
            {
                get
                {
                    if (m_InteractionStartIndex == ushort.MaxValue)
                        return kInvalidIndex;
                    return m_InteractionStartIndex;
                }
                set
                {
                    if (value == kInvalidIndex)
                        m_InteractionStartIndex = ushort.MaxValue;
                    else
                    {
                        if (value >= ushort.MaxValue)
                            throw new NotSupportedException("Interaction count cannot exceed ushort.MaxValue=" + ushort.MaxValue);
                        m_InteractionStartIndex = (ushort)value;
                    }
                }
            }

            /// <summary>
            /// Number of interactions associated with this binding.
            /// </summary>
            public int interactionCount
            {
                get => m_InteractionCount;
                set
                {
                    if (value >= byte.MaxValue)
                        throw new NotSupportedException("Interaction count per binding cannot exceed byte.MaxValue=" + byte.MaxValue);
                    m_InteractionCount = (byte)value;
                }
            }

            public int processorStartIndex
            {
                get
                {
                    if (m_ProcessorStartIndex == ushort.MaxValue)
                        return kInvalidIndex;
                    return m_ProcessorStartIndex;
                }
                set
                {
                    if (value == kInvalidIndex)
                        m_ProcessorStartIndex = ushort.MaxValue;
                    else
                    {
                        if (value >= ushort.MaxValue)
                            throw new NotSupportedException("Processor count cannot exceed ushort.MaxValue=" + ushort.MaxValue);
                        m_ProcessorStartIndex = (ushort)value;
                    }
                }
            }

            public int processorCount
            {
                get => m_ProcessorCount;
                set
                {
                    if (value >= byte.MaxValue)
                        throw new NotSupportedException("Processor count per binding cannot exceed byte.MaxValue=" + byte.MaxValue);
                    m_ProcessorCount = (byte)value;
                }
            }

            /// <summary>
            /// Index of the action being triggered by the binding (if any).
            /// </summary>
            /// <remarks>
            /// For bindings that don't trigger actions, this is <see cref="kInvalidIndex"/>.
            ///
            /// For bindings that are part of a composite, we force this to be the action set on the composite itself.
            /// </remarks>
            public int actionIndex
            {
                get
                {
                    if (m_ActionIndex == ushort.MaxValue)
                        return kInvalidIndex;
                    return m_ActionIndex;
                }
                set
                {
                    if (value == kInvalidIndex)
                        m_ActionIndex = ushort.MaxValue;
                    else
                    {
                        if (value >= ushort.MaxValue)
                            throw new NotSupportedException("Action count cannot exceed ushort.MaxValue=" + ushort.MaxValue);
                        m_ActionIndex = (ushort)value;
                    }
                }
            }

            public int mapIndex
            {
                get => m_MapIndex;
                set
                {
                    Debug.Assert(value != kInvalidIndex, "Map index is invalid");
                    if (value >= byte.MaxValue)
                        throw new NotSupportedException("Map count cannot exceed byte.MaxValue=" + byte.MaxValue);
                    m_MapIndex = (byte)value;
                }
            }

            /// <summary>
            /// If this is a composite binding, this is the index of the composite in <see cref="composites"/>.
            /// If the binding is part of a composite, this is the index of the binding that is the composite.
            /// If the binding is neither a composite nor part of a composite, this is <see cref="kInvalidIndex"/>.
            /// </summary>
            public int compositeOrCompositeBindingIndex
            {
                get
                {
                    if (m_CompositeOrCompositeBindingIndex == ushort.MaxValue)
                        return kInvalidIndex;
                    return m_CompositeOrCompositeBindingIndex;
                }
                set
                {
                    if (value == kInvalidIndex)
                        m_CompositeOrCompositeBindingIndex = ushort.MaxValue;
                    else
                    {
                        if (value >= ushort.MaxValue)
                            throw new NotSupportedException("Composite count cannot exceed ushort.MaxValue=" + ushort.MaxValue);
                        m_CompositeOrCompositeBindingIndex = (ushort)value;
                    }
                }
            }

            /// <summary>
            /// <see cref="InputEvent.eventId">ID</see> of the event that last triggered the binding.
            /// </summary>
            /// <remarks>
            /// We only store this for composites ATM.
            /// </remarks>
            public int triggerEventIdForComposite
            {
                get => m_TriggerEventIdForComposite;
                set => m_TriggerEventIdForComposite = value;
            }

            // For now, we only record this for part bindings!
            public double pressTime
            {
                get => m_PressTime;
                set => m_PressTime = value;
            }

            public Flags flags
            {
                get => (Flags)m_Flags;
                set => m_Flags = (byte)value;
            }

            public bool chainsWithNext
            {
                get => (flags & Flags.ChainsWithNext) == Flags.ChainsWithNext;
                set
                {
                    if (value)
                        flags |= Flags.ChainsWithNext;
                    else
                        flags &= ~Flags.ChainsWithNext;
                }
            }

            public bool isEndOfChain
            {
                get => (flags & Flags.EndOfChain) == Flags.EndOfChain;
                set
                {
                    if (value)
                        flags |= Flags.EndOfChain;
                    else
                        flags &= ~Flags.EndOfChain;
                }
            }

            public bool isPartOfChain => chainsWithNext || isEndOfChain;

            public bool isComposite
            {
                get => (flags & Flags.Composite) == Flags.Composite;
                set
                {
                    if (value)
                        flags |= Flags.Composite;
                    else
                        flags &= ~Flags.Composite;
                }
            }

            public bool isPartOfComposite
            {
                get => (flags & Flags.PartOfComposite) == Flags.PartOfComposite;
                set
                {
                    if (value)
                        flags |= Flags.PartOfComposite;
                    else
                        flags &= ~Flags.PartOfComposite;
                }
            }

            public bool initialStateCheckPending
            {
                get => (flags & Flags.InitialStateCheckPending) != 0;
                set
                {
                    if (value)
                        flags |= Flags.InitialStateCheckPending;
                    else
                        flags &= ~Flags.InitialStateCheckPending;
                }
            }

            public bool wantsInitialStateCheck
            {
                get => (flags & Flags.WantsInitialStateCheck) != 0;
                set
                {
                    if (value)
                        flags |= Flags.WantsInitialStateCheck;
                    else
                        flags &= ~Flags.WantsInitialStateCheck;
                }
            }

            public int partIndex
            {
                get => m_PartIndex;
                set
                {
                    if (partIndex < 0)
                        throw new ArgumentOutOfRangeException(nameof(value), "Part index must not be negative");
                    if (partIndex > byte.MaxValue)
                        throw new InvalidOperationException("Part count must not exceed byte.MaxValue=" + byte.MaxValue);
                    m_PartIndex = (byte)value;
                }
            }
        }

        /// <summary>
        /// Record of an input control change and its related data.
        /// </summary>
        /// <remarks>
        /// This serves a dual purpose. One is, trigger states represent control actuations while we process them. The
        /// other is to represent the current actuation state of an action as a whole. The latter is stored in <see cref="actionStates"/>
        /// while the former is passed around as temporary instances on the stack.
        /// </remarks>
        [StructLayout(LayoutKind.Explicit, Size = 48)]
        public struct TriggerState
        {
            public const int kMaxNumMaps = byte.MaxValue;
            public const int kMaxNumControls = ushort.MaxValue;
            public const int kMaxNumBindings = ushort.MaxValue;

            [FieldOffset(0)] private byte m_Phase;
            [FieldOffset(1)] private byte m_Flags;
            [FieldOffset(2)] private byte m_MapIndex;
            // One byte available here.
            [FieldOffset(4)] private ushort m_ControlIndex;
            // Two bytes available here.
            ////REVIEW: can we condense these to floats? would save us a whopping 8 bytes
            [FieldOffset(8)] private double m_Time;
            [FieldOffset(16)] private double m_StartTime;
            [FieldOffset(24)] private ushort m_BindingIndex;
            [FieldOffset(26)] private ushort m_InteractionIndex;
            [FieldOffset(28)] private float m_Magnitude;
            [FieldOffset(32)] private uint m_LastPerformedInUpdate;
            [FieldOffset(36)] private uint m_LastCanceledInUpdate;
            [FieldOffset(40)] private uint m_PressedInUpdate;
            [FieldOffset(44)] private uint m_ReleasedInUpdate;

            /// <summary>
            /// Phase being triggered by the control value change.
            /// </summary>
            public InputActionPhase phase
            {
                get => (InputActionPhase)m_Phase;
                set => m_Phase = (byte)value;
            }

            public bool isDisabled => phase == InputActionPhase.Disabled;
            public bool isWaiting => phase == InputActionPhase.Waiting;
            public bool isStarted => phase == InputActionPhase.Started;
            public bool isPerformed => phase == InputActionPhase.Performed;
            public bool isCanceled => phase == InputActionPhase.Canceled;

            /// <summary>
            /// The time the binding got triggered.
            /// </summary>
            public double time
            {
                get => m_Time;
                set => m_Time = value;
            }

            /// <summary>
            /// The time when the binding moved into <see cref="InputActionPhase.Started"/>.
            /// </summary>
            public double startTime
            {
                get => m_StartTime;
                set => m_StartTime = value;
            }

            /// <summary>
            /// Amount of actuation on the control.
            /// </summary>
            /// <remarks>
            /// This is only valid if <see cref="haveMagnitude"/> is true.
            ///
            /// Note that this may differ from the actuation stored for <see cref="controlIndex"/> in <see
            /// cref="UnmanagedMemory.controlMagnitudes"/> if the binding is a composite.
            /// </remarks>
            public float magnitude
            {
                get => m_Magnitude;
                set
                {
                    flags |= Flags.HaveMagnitude;
                    m_Magnitude = value;
                }
            }

            /// <summary>
            /// Whether <see cref="magnitude"/> has been set.
            /// </summary>
            /// <remarks>
            /// Magnitude computation is expensive so we only want to do it once. Also, we sometimes need to compare
            /// a current magnitude to a magnitude value from a previous frame and the magnitude of the control
            /// may have already changed.
            /// </remarks>
            public bool haveMagnitude => (flags & Flags.HaveMagnitude) != 0;

            /// <summary>
            /// Index of the action map in <see cref="maps"/> that contains the binding that triggered.
            /// </summary>
            public int mapIndex
            {
                get => m_MapIndex;
                set
                {
                    if (value < 0 || value > kMaxNumMaps)
                        throw new NotSupportedException("More than byte.MaxValue InputActionMaps in a single InputActionState");
                    m_MapIndex = (byte)value;
                }
            }

            /// <summary>
            /// Index of the control currently driving the action or <see cref="kInvalidIndex"/> if none.
            /// </summary>
            public int controlIndex
            {
                get
                {
                    if (m_ControlIndex == kMaxNumControls)
                        return kInvalidIndex;
                    return m_ControlIndex;
                }
                set
                {
                    if (value == kInvalidIndex)
                        m_ControlIndex = ushort.MaxValue;
                    else
                    {
                        if (value < 0 || value >= kMaxNumControls)
                            throw new NotSupportedException("More than ushort.MaxValue-1 controls in a single InputActionState");
                        m_ControlIndex = (ushort)value;
                    }
                }
            }

            /// <summary>
            /// Index into <see cref="bindingStates"/> for the binding that triggered.
            /// </summary>
            /// <remarks>
            /// This corresponds 1:1 to an <see cref="InputBinding"/>.
            /// </remarks>
            public int bindingIndex
            {
                get => m_BindingIndex;
                set
                {
                    if (value < 0 || value > kMaxNumBindings)
                        throw new NotSupportedException("More than ushort.MaxValue bindings in a single InputActionState");
                    m_BindingIndex = (ushort)value;
                }
            }

            /// <summary>
            /// Index into <see cref="InputActionState.interactionStates"/> for the interaction that triggered.
            /// </summary>
            /// <remarks>
            /// Is <see cref="InputActionState.kInvalidIndex"/> if there is no interaction present on the binding.
            /// </remarks>
            public int interactionIndex
            {
                get
                {
                    if (m_InteractionIndex == ushort.MaxValue)
                        return kInvalidIndex;
                    return m_InteractionIndex;
                }
                set
                {
                    if (value == kInvalidIndex)
                        m_InteractionIndex = ushort.MaxValue;
                    else
                    {
                        if (value < 0 || value >= ushort.MaxValue)
                            throw new NotSupportedException("More than ushort.MaxValue-1 interactions in a single InputActionState");
                        m_InteractionIndex = (ushort)value;
                    }
                }
            }

            /// <summary>
            /// Update step count (<see cref="InputUpdate.s_UpdateStepCount"/>) in which action triggered/performed last.
            /// Zero if the action did not trigger yet. Also reset to zero when the action is disabled.
            /// </summary>
            public uint lastPerformedInUpdate
            {
                get => m_LastPerformedInUpdate;
                set => m_LastPerformedInUpdate = value;
            }

            public uint lastCanceledInUpdate
            {
                get => m_LastCanceledInUpdate;
                set => m_LastCanceledInUpdate = value;
            }

            public uint pressedInUpdate
            {
                get => m_PressedInUpdate;
                set => m_PressedInUpdate = value;
            }

            public uint releasedInUpdate
            {
                get => m_ReleasedInUpdate;
                set => m_ReleasedInUpdate = value;
            }

            /// <summary>
            /// Whether the action associated with the trigger state is marked as pass-through.
            /// </summary>
            /// <seealso cref="InputActionType.PassThrough"/>
            public bool isPassThrough
            {
                get => (flags & Flags.PassThrough) != 0;
                set
                {
                    if (value)
                        flags |= Flags.PassThrough;
                    else
                        flags &= ~Flags.PassThrough;
                }
            }

            /// <summary>
            /// Whether the action associated with the trigger state is a button-type action.
            /// </summary>
            /// <seealso cref="InputActionType.Button"/>
            public bool isButton
            {
                get => (flags & Flags.Button) != 0;
                set
                {
                    if (value)
                        flags |= Flags.Button;
                    else
                        flags &= ~Flags.Button;
                }
            }

            public bool isPressed
            {
                get => (flags & Flags.Pressed) != 0;
                set
                {
                    if (value)
                        flags |= Flags.Pressed;
                    else
                        flags &= ~Flags.Pressed;
                }
            }

            /// <summary>
            /// Whether the action may potentially see multiple concurrent actuations from its bindings
            /// and wants them resolved automatically.
            /// </summary>
            /// <remarks>
            /// We use this to gate some of the more expensive checks that are pointless to
            /// perform if we don't have to disambiguate input from concurrent sources.
            ///
            /// Always disabled if <see cref="isPassThrough"/> is true.
            /// </remarks>
            public bool mayNeedConflictResolution
            {
                get => (flags & Flags.MayNeedConflictResolution) != 0;
                set
                {
                    if (value)
                        flags |= Flags.MayNeedConflictResolution;
                    else
                        flags &= ~Flags.MayNeedConflictResolution;
                }
            }

            /// <summary>
            /// Whether the action currently has several concurrent actuations from its bindings.
            /// </summary>
            /// <remarks>
            /// This is only used when automatic conflict resolution is enabled (<see cref="mayNeedConflictResolution"/>).
            /// </remarks>
            public bool hasMultipleConcurrentActuations
            {
                get => (flags & Flags.HasMultipleConcurrentActuations) != 0;
                set
                {
                    if (value)
                        flags |= Flags.HasMultipleConcurrentActuations;
                    else
                        flags &= ~Flags.HasMultipleConcurrentActuations;
                }
            }

            public bool inProcessing
            {
                get => (flags & Flags.InProcessing) != 0;
                set
                {
                    if (value)
                        flags |= Flags.InProcessing;
                    else
                        flags &= ~Flags.InProcessing;
                }
            }

            public Flags flags
            {
                get => (Flags)m_Flags;
                set => m_Flags = (byte)value;
            }

            [Flags]
            public enum Flags
            {
                /// <summary>
                /// Whether <see cref="magnitude"/> has been set.
                /// </summary>
                HaveMagnitude = 1 << 0,

                /// <summary>
                /// Whether the action associated with the trigger state is marked as pass-through.
                /// </summary>
                /// <seealso cref="InputActionType.PassThrough"/>
                PassThrough = 1 << 1,

                /// <summary>
                /// Whether the action has more than one control bound to it.
                /// </summary>
                /// <remarks>
                /// An action may have arbitrary many bindings yet may still resolve only to a single control
                /// at runtime. In that case, this flag is NOT set. We only set it if binding resolution for
                /// an action indeed ended up with multiple controls able to trigger the same action.
                /// </remarks>
                MayNeedConflictResolution = 1 << 2,

                /// <summary>
                /// Whether there are currently multiple bound controls that are actuated.
                /// </summary>
                /// <remarks>
                /// This is only used if <see cref="TriggerState.mayNeedConflictResolution"/> is true.
                /// </remarks>
                HasMultipleConcurrentActuations = 1 << 3,

                InProcessing = 1 << 4,

                /// <summary>
                /// Whether the action associated with the trigger state is a button-type action.
                /// </summary>
                /// <seealso cref="InputActionType.Button"/>
                Button = 1 << 5,

                Pressed = 1 << 6,
            }
        }

        /// <summary>
        /// Tells us where the data for a single action map is found in the
        /// various arrays.
        /// </summary>
        public struct ActionMapIndices
        {
            public int actionStartIndex;
            public int actionCount;
            public int controlStartIndex;
            public int controlCount;
            public int bindingStartIndex;
            public int bindingCount;
            public int interactionStartIndex;
            public int interactionCount;
            public int processorStartIndex;
            public int processorCount;
            public int compositeStartIndex;
            public int compositeCount;
        }

        /// <summary>
        /// Unmanaged memory kept for action maps.
        /// </summary>
        /// <remarks>
        /// Most of the dynamic execution state for actions we keep in a single block of unmanaged memory.
        /// Essentially, only the C# heap objects (like IInputInteraction and such) we keep in managed arrays.
        /// Aside from being able to condense the data into a single block of memory and not having to have
        /// it spread out on the GC heap, we gain the advantage of being able to freely allocate and re-allocate
        /// these blocks without creating garbage on the GC heap.
        ///
        /// The data here is set up by <see cref="InputBindingResolver"/>.
        /// </remarks>
        public struct UnmanagedMemory : IDisposable
        {
            public bool isAllocated => basePtr != null;

            public void* basePtr;

            /// <summary>
            /// Number of action maps and entries in <see cref="mapIndices"/> and <see cref="maps"/>.
            /// </summary>
            public int mapCount;

            /// <summary>
            /// Total number of actions (i.e. from all maps combined) and entries in <see cref="actionStates"/>.
            /// </summary>
            public int actionCount;

            /// <summary>
            /// Total number of interactions and entries in <see cref="interactionStates"/> and <see cref="interactions"/>.
            /// </summary>
            public int interactionCount;

            /// <summary>
            /// Total number of bindings and entries in <see cref="bindingStates"/>.
            /// </summary>
            public int bindingCount;

            /// <summary>
            /// Total number of bound controls and entries in <see cref="controls"/>.
            /// </summary>
            public int controlCount;

            /// <summary>
            /// Total number of composite bindings and entries in <see cref="composites"/>.
            /// </summary>
            public int compositeCount;

            /// <summary>
            /// Total size of allocated unmanaged memory.
            /// </summary>
            public int sizeInBytes =>
                mapCount * sizeof(ActionMapIndices) + // mapIndices
                actionCount * sizeof(TriggerState) + // actionStates
                bindingCount * sizeof(BindingState) + // bindingStates
                interactionCount * sizeof(InteractionState) + // interactionStates
                controlCount * sizeof(float) + // controlMagnitudes
                compositeCount * sizeof(float) + // compositeMagnitudes
                controlCount * sizeof(int) + // controlIndexToBindingIndex
                controlCount * sizeof(ushort) * 2 + // controlGrouping
                actionCount * sizeof(ushort) * 2 + // actionBindingIndicesAndCounts
                bindingCount * sizeof(ushort) + // actionBindingIndices
                (controlCount + 31) / 32 * sizeof(int); // enabledControlsArray

            /// <summary>
            /// Trigger state of all actions added to the state.
            /// </summary>
            /// <remarks>
            /// This array also tells which actions are enabled or disabled. Any action with phase
            /// <see cref="InputActionPhase.Disabled"/> is disabled.
            /// </remarks>
            public TriggerState* actionStates;

            /// <summary>
            /// State of all bindings added to the state.
            /// </summary>
            /// <remarks>
            /// For the most part, this is read-only information set up during resolution.
            /// </remarks>
            public BindingState* bindingStates;

            /// <summary>
            /// State of all interactions on bindings in the action map.
            /// </summary>
            /// <remarks>
            /// Any interaction mentioned on any of the bindings gets its own execution state record
            /// in here. The interactions for any one binding are grouped together.
            /// </remarks>
            public InteractionState* interactionStates;

            /// <summary>
            /// Current remembered level of actuation of each of the controls in <see cref="controls"/>.
            /// </summary>
            /// <remarks>
            /// This array is NOT kept strictly up to date. In fact, we only use it for conflict resolution
            /// between multiple bound controls at the moment. Meaning that in the majority of cases, the magnitude
            /// stored for a control here will NOT be up to date.
            ///
            /// Also note that for controls that are part of composites, this will NOT be the magnitude of the
            /// control but rather be the magnitude of the entire compound.
            /// </remarks>
            public float* controlMagnitudes;

            public float* compositeMagnitudes;

            public int* enabledControls;

            /// <summary>
            /// Array of pair of ints, one pair for each action (same index as <see cref="actionStates"/>). First int
            /// is the index into <see cref="actionBindingIndices"/> where bindings of action are found and second int
            /// is the count of bindings on action.
            /// </summary>
            public ushort* actionBindingIndicesAndCounts;

            /// <summary>
            /// Array of indices into <see cref="bindingStates"/>. The indices for every action are laid out sequentially.
            /// The array slice corresponding to each action can be determined by looking it up in <see cref="actionBindingIndicesAndCounts"/>.
            /// </summary>
            public ushort* actionBindingIndices;

            ////REVIEW: make this an array of shorts rather than ints?
            public int* controlIndexToBindingIndex;

            // Two shorts per control. First one is group number. Second one is complexity count.
            public ushort* controlGroupingAndComplexity;
            public bool controlGroupingInitialized;

            public ActionMapIndices* mapIndices;

            public void Allocate(int mapCount, int actionCount, int bindingCount, int controlCount, int interactionCount, int compositeCount)
            {
                Debug.Assert(basePtr == null, "Memory already allocated! Free first!");
                Debug.Assert(mapCount >= 1, "Map count out of range");
                Debug.Assert(actionCount >= 0, "Action count out of range");
                Debug.Assert(bindingCount >= 0, "Binding count out of range");
                Debug.Assert(interactionCount >= 0, "Interaction count out of range");
                Debug.Assert(compositeCount >= 0, "Composite count out of range");

                this.mapCount = mapCount;
                this.actionCount = actionCount;
                this.interactionCount = interactionCount;
                this.bindingCount = bindingCount;
                this.controlCount = controlCount;
                this.compositeCount = compositeCount;

                var numBytes = sizeInBytes;
                var ptr = (byte*)UnsafeUtility.Malloc(numBytes, 8, Allocator.Persistent);
                UnsafeUtility.MemClear(ptr, numBytes);

                basePtr = ptr;

                // NOTE: This depends on the individual structs being sufficiently aligned in order to not
                //       cause any misalignment here. TriggerState, InteractionState, and BindingState all
                //       contain doubles so put them first in memory to make sure they get proper alignment.
                actionStates = (TriggerState*)ptr; ptr += actionCount * sizeof(TriggerState);
                interactionStates = (InteractionState*)ptr; ptr += interactionCount * sizeof(InteractionState);
                bindingStates = (BindingState*)ptr; ptr += bindingCount * sizeof(BindingState);
                mapIndices = (ActionMapIndices*)ptr; ptr += mapCount * sizeof(ActionMapIndices);
                controlMagnitudes = (float*)ptr; ptr += controlCount * sizeof(float);
                compositeMagnitudes = (float*)ptr; ptr += compositeCount * sizeof(float);
                controlIndexToBindingIndex = (int*)ptr; ptr += controlCount * sizeof(int);
                controlGroupingAndComplexity = (ushort*)ptr; ptr += controlCount * sizeof(ushort) * 2;
                actionBindingIndicesAndCounts = (ushort*)ptr; ptr += actionCount * sizeof(ushort) * 2;
                actionBindingIndices = (ushort*)ptr; ptr += bindingCount * sizeof(ushort);
                enabledControls = (int*)ptr; ptr += (controlCount + 31) / 32 * sizeof(int);
            }

            public void Dispose()
            {
                if (basePtr == null)
                    return;

                UnsafeUtility.Free(basePtr, Allocator.Persistent);

                basePtr = null;
                actionStates = null;
                interactionStates = null;
                bindingStates = null;
                mapIndices = null;
                controlMagnitudes = null;
                compositeMagnitudes = null;
                controlIndexToBindingIndex = null;
                controlGroupingAndComplexity = null;
                actionBindingIndices = null;
                actionBindingIndicesAndCounts = null;

                mapCount = 0;
                actionCount = 0;
                bindingCount = 0;
                controlCount = 0;
                interactionCount = 0;
                compositeCount = 0;
            }

            public void CopyDataFrom(UnmanagedMemory memory)
            {
                Debug.Assert(memory.basePtr != null, "Given struct has no allocated data");

                // Even if a certain array is empty (e.g. we have no controls), we set the pointer
                // in Allocate() to something other than null.

                UnsafeUtility.MemCpy(mapIndices, memory.mapIndices, memory.mapCount * sizeof(ActionMapIndices));
                UnsafeUtility.MemCpy(actionStates, memory.actionStates, memory.actionCount * sizeof(TriggerState));
                UnsafeUtility.MemCpy(bindingStates, memory.bindingStates, memory.bindingCount * sizeof(BindingState));
                UnsafeUtility.MemCpy(interactionStates, memory.interactionStates, memory.interactionCount * sizeof(InteractionState));
                UnsafeUtility.MemCpy(controlMagnitudes, memory.controlMagnitudes, memory.controlCount * sizeof(float));
                UnsafeUtility.MemCpy(compositeMagnitudes, memory.compositeMagnitudes, memory.compositeCount * sizeof(float));
                UnsafeUtility.MemCpy(controlIndexToBindingIndex, memory.controlIndexToBindingIndex, memory.controlCount * sizeof(int));
                UnsafeUtility.MemCpy(controlGroupingAndComplexity, memory.controlGroupingAndComplexity, memory.controlCount * sizeof(ushort) * 2);
                UnsafeUtility.MemCpy(actionBindingIndicesAndCounts, memory.actionBindingIndicesAndCounts, memory.actionCount * sizeof(ushort) * 2);
                UnsafeUtility.MemCpy(actionBindingIndices, memory.actionBindingIndices, memory.bindingCount * sizeof(ushort));
                UnsafeUtility.MemCpy(enabledControls, memory.enabledControls, (memory.controlCount + 31) / 32 * sizeof(int));
            }

            public UnmanagedMemory Clone()
            {
                if (!isAllocated)
                    return new UnmanagedMemory();

                var clone = new UnmanagedMemory();
                clone.Allocate(
                    mapCount: mapCount,
                    actionCount: actionCount,
                    controlCount: controlCount,
                    bindingCount: bindingCount,
                    interactionCount: interactionCount,
                    compositeCount: compositeCount);
                clone.CopyDataFrom(this);

                return clone;
            }
        }

        #region Global State

        /// <summary>
        /// Global state containing a list of weak references to all action map states currently in the system.
        /// </summary>
        /// <remarks>
        /// When the control setup in the system changes, we need a way for control resolution that
        /// has already been done to be invalidated and redone. We also want a way to find all
        /// currently enabled actions in the system.
        ///
        /// Both of these needs are served by this global list.
        /// </remarks>
        internal struct GlobalState
        {
            internal InlinedArray<GCHandle> globalList;
            internal CallbackArray<Action<object, InputActionChange>> onActionChange;
            internal CallbackArray<Action<object>> onActionControlsChanged;
        }

        internal static GlobalState s_GlobalState;

        internal static ISavedState SaveAndResetState()
        {
            // Save current state
            var savedState = new SavedStructState<GlobalState>(
                ref s_GlobalState,
                (ref GlobalState state) => s_GlobalState = state, // restore
                () => ResetGlobals()); // static dispose

            // Reset global state
            s_GlobalState = default;

            return savedState;
        }

        private void AddToGlobalList()
        {
            CompactGlobalList();
            var handle = GCHandle.Alloc(this, GCHandleType.Weak);
            s_GlobalState.globalList.AppendWithCapacity(handle);
        }

        private void RemoveMapFromGlobalList()
        {
            var count = s_GlobalState.globalList.length;
            for (var i = 0; i < count; ++i)
                if (s_GlobalState.globalList[i].Target == this)
                {
                    s_GlobalState.globalList[i].Free();
                    s_GlobalState.globalList.RemoveAtByMovingTailWithCapacity(i);
                    break;
                }
        }

        /// <summary>
        /// Remove any entries for states that have been reclaimed by GC.
        /// </summary>
        private static void CompactGlobalList()
        {
            var length = s_GlobalState.globalList.length;
            var head = 0;
            for (var i = 0; i < length; ++i)
            {
                var handle = s_GlobalState.globalList[i];
                if (handle.IsAllocated && handle.Target != null)
                {
                    if (head != i)
                        s_GlobalState.globalList[head] = handle;
                    ++head;
                }
                else
                {
                    if (handle.IsAllocated)
                        s_GlobalState.globalList[i].Free();
                    s_GlobalState.globalList[i] = default;
                }
            }
            s_GlobalState.globalList.length = head;
        }

        internal void NotifyListenersOfActionChange(InputActionChange change)
        {
            for (var i = 0; i < totalMapCount; ++i)
            {
                var map = maps[i];
                if (map.m_SingletonAction != null)
                {
                    NotifyListenersOfActionChange(change, map.m_SingletonAction);
                }
                else if (map.m_Asset == null)
                {
                    NotifyListenersOfActionChange(change, map);
                }
                else
                {
                    NotifyListenersOfActionChange(change, map.m_Asset);
                    return;
                }
            }
        }

        internal static void NotifyListenersOfActionChange(InputActionChange change, object actionOrMapOrAsset)
        {
            Debug.Assert(actionOrMapOrAsset != null, "Should have action or action map or asset object to notify about");
            Debug.Assert(actionOrMapOrAsset is InputAction || (actionOrMapOrAsset as InputActionMap)?.m_SingletonAction == null,
                "Must not send notifications for changes made to hidden action maps of singleton actions");

            DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionChange, actionOrMapOrAsset, change, "onActionChange");
            if (change == InputActionChange.BoundControlsChanged)
                DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionControlsChanged, actionOrMapOrAsset, "onActionControlsChange");
        }

        /// <summary>
        /// Nuke global state we have to keep track of action map states.
        /// </summary>
        private static void ResetGlobals()
        {
            DestroyAllActionMapStates();
            for (var i = 0; i < s_GlobalState.globalList.length; ++i)
                if (s_GlobalState.globalList[i].IsAllocated)
                    s_GlobalState.globalList[i].Free();
            s_GlobalState.globalList.length = 0;
            s_GlobalState.onActionChange.Clear();
            s_GlobalState.onActionControlsChanged.Clear();
        }

        // Walk all maps with enabled actions and add all enabled actions to the given list.
        internal static int FindAllEnabledActions(List<InputAction> result)
        {
            var numFound = 0;
            var stateCount = s_GlobalState.globalList.length;
            for (var i = 0; i < stateCount; ++i)
            {
                var handle = s_GlobalState.globalList[i];
                if (!handle.IsAllocated)
                    continue;
                var state = (InputActionState)handle.Target;
                if (state == null)
                    continue;

                var mapCount = state.totalMapCount;
                var maps = state.maps;
                for (var n = 0; n < mapCount; ++n)
                {
                    var map = maps[n];
                    if (!map.enabled)
                        continue;

                    var actions = map.m_Actions;
                    var actionCount = actions.Length;
                    if (map.m_EnabledActionsCount == actionCount)
                    {
                        result.AddRange(actions);
                        numFound += actionCount;
                    }
                    else
                    {
                        var actionStartIndex = state.mapIndices[map.m_MapIndexInState].actionStartIndex;
                        for (var k = 0; k < actionCount; ++k)
                        {
                            if (state.actionStates[actionStartIndex + k].phase != InputActionPhase.Disabled)
                            {
                                result.Add(actions[k]);
                                ++numFound;
                            }
                        }
                    }
                }
            }

            return numFound;
        }

        ////TODO: when re-resolving, we need to preserve InteractionStates and not just reset them

        /// <summary>
        /// Deal with the fact that the control setup in the system may change at any time and can affect
        /// actions that had their controls already resolved.
        /// </summary>
        /// <remarks>
        /// Note that this method can NOT deal with changes other than the control setup in the system
        /// changing. Specifically, it will NOT handle configuration changes in action maps (e.g. bindings
        /// being altered) correctly.
        ///
        /// We get called from <see cref="InputManager"/> directly rather than hooking into <see cref="InputSystem.onDeviceChange"/>
        /// so that we're not adding needless calls for device changes that are not of interest to us.
        /// </remarks>
        internal static void OnDeviceChange(InputDevice device, InputDeviceChange change)
        {
            Debug.Assert(device != null, "Device is null");
            ////REVIEW: should we ignore disconnected devices in InputBindingResolver?
            Debug.Assert(
                change == InputDeviceChange.Added || change == InputDeviceChange.Removed ||
                change == InputDeviceChange.UsageChanged || change == InputDeviceChange.ConfigurationChanged ||
                change == InputDeviceChange.SoftReset || change == InputDeviceChange.HardReset,
                "Should only be called for relevant changes");

            for (var i = 0; i < s_GlobalState.globalList.length; ++i)
            {
                var handle = s_GlobalState.globalList[i];
                if (!handle.IsAllocated || handle.Target == null)
                {
                    // Stale entry in the list. State has already been reclaimed by GC. Remove it.
                    if (handle.IsAllocated)
                        s_GlobalState.globalList[i].Free();
                    s_GlobalState.globalList.RemoveAtWithCapacity(i);
                    --i;
                    continue;
                }
                var state = (InputActionState)handle.Target;

                // If this state is not affected by the change, skip.
                var needsFullResolve = true;
                switch (change)
                {
                    case InputDeviceChange.Added:
                        if (!state.CanUseDevice(device))
                            continue;
                        needsFullResolve = false;
                        break;

                    case InputDeviceChange.Removed:
                        if (!state.IsUsingDevice(device))
                            continue;

                        // If the device is listed in a device mask (on either a map or an asset) in the
                        // state, remove it (see Actions_WhenDeviceIsRemoved_DeviceIsRemovedFromDeviceMask).
                        for (var n = 0; n < state.totalMapCount; ++n)
                        {
                            var map = state.maps[n];
                            map.m_Devices.Remove(device);
                            map.asset?.m_Devices.Remove(device);
                        }

                        needsFullResolve = false;
                        break;

                    // NOTE: ConfigurationChanges can affect display names of controls which may make a device usable that
                    //       we didn't find anything usable on before.
                    case InputDeviceChange.ConfigurationChanged:
                    case InputDeviceChange.UsageChanged:
                        if (!state.IsUsingDevice(device) && !state.CanUseDevice(device))
                            continue;
                        // Full resolve necessary!
                        break;

                    // On reset, cancel all actions currently in progress from the device that got reset.
                    // If we simply let change monitors trigger, we will respond to things like button releases
                    // that are in fact just resets of buttons to their default state.
                    case InputDeviceChange.SoftReset:
                    case InputDeviceChange.HardReset:
                        if (!state.IsUsingDevice(device))
                            continue;
                        state.ResetActionStatesDrivenBy(device);
                        continue; // No re-resolving necessary.
                }

                // Trigger a lazy-resolve on all action maps in the state.
                for (var n = 0; n < state.totalMapCount; ++n)
                {
                    if (state.maps[n].LazyResolveBindings(fullResolve: needsFullResolve))
                    {
                        // Map has chosen to resolve right away. This will resolve bindings for *all*
                        // maps in the state, so we're done here.
                        break;
                    }
                }
            }
        }

        internal static void DeferredResolutionOfBindings()
        {
            ++InputActionMap.s_DeferBindingResolution;
            try
            {
                for (var i = 0; i < s_GlobalState.globalList.length; ++i)
                {
                    var handle = s_GlobalState.globalList[i];
                    if (!handle.IsAllocated || handle.Target == null)
                    {
                        // Stale entry in the list. State has already been reclaimed by GC. Remove it.
                        if (handle.IsAllocated)
                            s_GlobalState.globalList[i].Free();
                        s_GlobalState.globalList.RemoveAtWithCapacity(i);
                        --i;
                        continue;
                    }

                    var state = (InputActionState)handle.Target;
                    for (var n = 0; n < state.totalMapCount; ++n)
                        state.maps[n].ResolveBindingsIfNecessary();
                }
            }
            finally
            {
                --InputActionMap.s_DeferBindingResolution;
            }
        }

        internal static void DisableAllActions()
        {
            for (var i = 0; i < s_GlobalState.globalList.length; ++i)
            {
                var handle = s_GlobalState.globalList[i];
                if (!handle.IsAllocated || handle.Target == null)
                    continue;
                var state = (InputActionState)handle.Target;

                var mapCount = state.totalMapCount;
                var maps = state.maps;
                for (var n = 0; n < mapCount; ++n)
                {
                    maps[n].Disable();
                    Debug.Assert(!maps[n].enabled, "Map is still enabled after calling Disable");
                }
            }
        }

        /// <summary>
        /// Forcibly destroy all states currently on the global list.
        /// </summary>
        /// <remarks>
        /// We do this when exiting play mode in the editor to make sure we are cleaning up our
        /// unmanaged memory allocations.
        /// </remarks>
        internal static void DestroyAllActionMapStates()
        {
            while (s_GlobalState.globalList.length > 0)
            {
                var index = s_GlobalState.globalList.length - 1;
                var handle = s_GlobalState.globalList[index];
                if (!handle.IsAllocated || handle.Target == null)
                {
                    // Already destroyed.
                    if (handle.IsAllocated)
                        s_GlobalState.globalList[index].Free();
                    s_GlobalState.globalList.RemoveAtWithCapacity(index);
                    continue;
                }

                var state = (InputActionState)handle.Target;
                state.Destroy();
            }
        }

        #endregion
    }
}