aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/main.cpp
blob: 00666689673c66a686f04ac5a86e954533f5260a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
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
/*
    Copyright 2016-2020 Arisotura

    This file is part of melonDS.

    melonDS is free software: you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free
    Software Foundation, either version 3 of the License, or (at your option)
    any later version.

    melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with melonDS. If not, see http://www.gnu.org/licenses/.
*/

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>

#ifndef __WIN32__
#include <glib.h>
#endif

#include <SDL2/SDL.h>
#include "libui/ui.h"

#include "../OpenGLSupport.h"
#include "main_shaders.h"

#include "../types.h"
#include "../version.h"
#include "PlatformConfig.h"

#include "DlgEmuSettings.h"
#include "DlgInputConfig.h"
#include "DlgVideoSettings.h"
#include "DlgAudioSettings.h"
#include "DlgWifiSettings.h"

#include "../NDS.h"
#include "../GBACart.h"
#include "../GPU.h"
#include "../SPU.h"
#include "../Wifi.h"
#include "../Platform.h"
#include "../Config.h"
#include "../ARMJIT.h"

#include "../Savestate.h"

#include "OSD.h"

#ifdef MELONCAP
#include "MelonCap.h"
#endif // MELONCAP


// savestate slot mapping
// 1-8: regular slots (quick access)
// '9': load/save arbitrary file
const int kSavestateNum[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};

const int kScreenSize[4] = {1, 2, 3, 4};
const int kScreenRot[4] = {0, 1, 2, 3};
const int kScreenGap[6] = {0, 1, 8, 64, 90, 128};
const int kScreenLayout[3] = {0, 1, 2};
const int kScreenSizing[4] = {0, 1, 2, 3};


char* EmuDirectory;


uiWindow* MainWindow;
uiArea* MainDrawArea;
uiAreaHandler MainDrawAreaHandler;

const u32 kGLVersions[] = {uiGLVersion(3,2), uiGLVersion(3,1), 0};
uiGLContext* GLContext;

int WindowWidth, WindowHeight;

uiMenuItem* MenuItem_SaveState;
uiMenuItem* MenuItem_LoadState;
uiMenuItem* MenuItem_UndoStateLoad;

uiMenuItem* MenuItem_SaveStateSlot[9];
uiMenuItem* MenuItem_LoadStateSlot[9];

uiMenuItem* MenuItem_Pause;
uiMenuItem* MenuItem_Reset;
uiMenuItem* MenuItem_Stop;

uiMenuItem* MenuItem_SavestateSRAMReloc;

uiMenuItem* MenuItem_ScreenRot[4];
uiMenuItem* MenuItem_ScreenGap[6];
uiMenuItem* MenuItem_ScreenLayout[3];
uiMenuItem* MenuItem_ScreenSizing[4];

uiMenuItem* MenuItem_ScreenFilter;
uiMenuItem* MenuItem_LimitFPS;
uiMenuItem* MenuItem_AudioSync;
uiMenuItem* MenuItem_ShowOSD;

SDL_Thread* EmuThread;
int EmuRunning;
volatile int EmuStatus;

bool RunningSomething;
char ROMPath[2][1024];
char SRAMPath[2][1024];
char PrevSRAMPath[2][1024]; // for savestate 'undo load'

bool SavestateLoaded;

bool Screen_UseGL;

bool ScreenDrawInited = false;
uiDrawBitmap* ScreenBitmap[2] = {NULL,NULL};

GLuint GL_ScreenShader[3];
GLuint GL_ScreenShaderAccel[3];
GLuint GL_ScreenShaderOSD[3];
struct
{
    float uScreenSize[2];
    u32 u3DScale;
    u32 uFilterMode;

} GL_ShaderConfig;
GLuint GL_ShaderConfigUBO;
GLuint GL_ScreenVertexArrayID, GL_ScreenVertexBufferID;
float GL_ScreenVertices[2 * 3*2 * 4]; // position/texcoord
GLuint GL_ScreenTexture;
bool GL_ScreenSizeDirty;

int GL_3DScale;

bool GL_VSyncStatus;

int ScreenGap = 0;
int ScreenLayout = 0;
int ScreenSizing = 0;
int ScreenRotation = 0;

int MainScreenPos[3];
int AutoScreenSizing;

uiRect TopScreenRect;
uiRect BottomScreenRect;
uiDrawMatrix TopScreenTrans;
uiDrawMatrix BottomScreenTrans;

bool Touching = false;

u32 KeyInputMask, JoyInputMask;
u32 KeyHotkeyMask, JoyHotkeyMask;
u32 HotkeyMask, LastHotkeyMask;
u32 HotkeyPress, HotkeyRelease;

#define HotkeyDown(hk)     (HotkeyMask & (1<<(hk)))
#define HotkeyPressed(hk)  (HotkeyPress & (1<<(hk)))
#define HotkeyReleased(hk) (HotkeyRelease & (1<<(hk)))

bool LidStatus;

int JoystickID;
SDL_Joystick* Joystick;

int AudioFreq;
float AudioSampleFrac;
SDL_AudioDeviceID AudioDevice, MicDevice;

SDL_cond* AudioSync;
SDL_mutex* AudioSyncLock;

u32 MicBufferLength = 2048;
s16 MicBuffer[2048];
u32 MicBufferReadPos, MicBufferWritePos;

u32 MicWavLength;
s16* MicWavBuffer;

void SetupScreenRects(int width, int height);

void TogglePause(void* blarg);
void Reset(void* blarg);

void SetupSRAMPath(int slot);

void SaveState(int slot);
void LoadState(int slot);
void UndoStateLoad();
void GetSavestateName(int slot, char* filename, int len);

void CreateMainWindow(bool opengl);
void DestroyMainWindow();
void RecreateMainWindow(bool opengl);



bool GLScreen_InitShader(GLuint* shader, const char* fs)
{
    if (!OpenGL_BuildShaderProgram(kScreenVS, fs, shader, "ScreenShader"))
        return false;

    glBindAttribLocation(shader[2], 0, "vPosition");
    glBindAttribLocation(shader[2], 1, "vTexcoord");
    glBindFragDataLocation(shader[2], 0, "oColor");

    if (!OpenGL_LinkShaderProgram(shader))
        return false;

    GLuint uni_id;

    uni_id = glGetUniformBlockIndex(shader[2], "uConfig");
    glUniformBlockBinding(shader[2], uni_id, 16);

    glUseProgram(shader[2]);
    uni_id = glGetUniformLocation(shader[2], "ScreenTex");
    glUniform1i(uni_id, 0);
    uni_id = glGetUniformLocation(shader[2], "_3DTex");
    glUniform1i(uni_id, 1);

    return true;
}

bool GLScreen_InitOSDShader(GLuint* shader)
{
    if (!OpenGL_BuildShaderProgram(kScreenVS_OSD, kScreenFS_OSD, shader, "ScreenShaderOSD"))
        return false;

    glBindAttribLocation(shader[2], 0, "vPosition");
    glBindFragDataLocation(shader[2], 0, "oColor");

    if (!OpenGL_LinkShaderProgram(shader))
        return false;

    GLuint uni_id;

    uni_id = glGetUniformBlockIndex(shader[2], "uConfig");
    glUniformBlockBinding(shader[2], uni_id, 16);

    glUseProgram(shader[2]);
    uni_id = glGetUniformLocation(shader[2], "OSDTex");
    glUniform1i(uni_id, 0);

    return true;
}

bool GLScreen_Init()
{
    GL_VSyncStatus = Config::ScreenVSync;

    // TODO: consider using epoxy?
    if (!OpenGL_Init())
        return false;

    const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString(GL_VERSION); // version as a string
    printf("OpenGL: renderer: %s\n", renderer);
    printf("OpenGL: version: %s\n", version);

    if (!GLScreen_InitShader(GL_ScreenShader, kScreenFS))
        return false;
    if (!GLScreen_InitShader(GL_ScreenShaderAccel, kScreenFS_Accel))
        return false;
    if (!GLScreen_InitOSDShader(GL_ScreenShaderOSD))
        return false;

    memset(&GL_ShaderConfig, 0, sizeof(GL_ShaderConfig));

    glGenBuffers(1, &GL_ShaderConfigUBO);
    glBindBuffer(GL_UNIFORM_BUFFER, GL_ShaderConfigUBO);
    glBufferData(GL_UNIFORM_BUFFER, sizeof(GL_ShaderConfig), &GL_ShaderConfig, GL_STATIC_DRAW);
    glBindBufferBase(GL_UNIFORM_BUFFER, 16, GL_ShaderConfigUBO);

    glGenBuffers(1, &GL_ScreenVertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, GL_ScreenVertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GL_ScreenVertices), NULL, GL_STATIC_DRAW);

    glGenVertexArrays(1, &GL_ScreenVertexArrayID);
    glBindVertexArray(GL_ScreenVertexArrayID);
    glEnableVertexAttribArray(0); // position
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*4, (void*)(0));
    glEnableVertexAttribArray(1); // texcoord
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*4, (void*)(2*4));

    glGenTextures(1, &GL_ScreenTexture);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, GL_ScreenTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, 256*3 + 1, 192*2, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, NULL);

    GL_ScreenSizeDirty = true;

    return true;
}

void GLScreen_DeInit()
{
    glDeleteTextures(1, &GL_ScreenTexture);

    glDeleteVertexArrays(1, &GL_ScreenVertexArrayID);
    glDeleteBuffers(1, &GL_ScreenVertexBufferID);

    OpenGL_DeleteShaderProgram(GL_ScreenShader);
    OpenGL_DeleteShaderProgram(GL_ScreenShaderAccel);
    OpenGL_DeleteShaderProgram(GL_ScreenShaderOSD);
}

void GLScreen_DrawScreen()
{
    bool vsync = Config::ScreenVSync && !HotkeyDown(HK_FastForward);
    if (vsync != GL_VSyncStatus)
    {
        GL_VSyncStatus = vsync;
        uiGLSetVSync(vsync);
    }

    float scale = uiGLGetFramebufferScale(GLContext);

    glBindFramebuffer(GL_FRAMEBUFFER, uiGLGetFramebuffer(GLContext));

    if (GL_ScreenSizeDirty)
    {
        GL_ScreenSizeDirty = false;

        GL_ShaderConfig.uScreenSize[0] = WindowWidth;
        GL_ShaderConfig.uScreenSize[1] = WindowHeight;
        GL_ShaderConfig.u3DScale = GL_3DScale;

        glBindBuffer(GL_UNIFORM_BUFFER, GL_ShaderConfigUBO);
        void* unibuf = glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY);
        if (unibuf) memcpy(unibuf, &GL_ShaderConfig, sizeof(GL_ShaderConfig));
        glUnmapBuffer(GL_UNIFORM_BUFFER);

        float scwidth, scheight;

        float x0, y0, x1, y1;
        float s0, s1, s2, s3;
        float t0, t1, t2, t3;

#define SETVERTEX(i, x, y, s, t) \
    GL_ScreenVertices[4*(i) + 0] = x; \
    GL_ScreenVertices[4*(i) + 1] = y; \
    GL_ScreenVertices[4*(i) + 2] = s; \
    GL_ScreenVertices[4*(i) + 3] = t;

        x0 = TopScreenRect.X;
        y0 = TopScreenRect.Y;
        x1 = TopScreenRect.X + TopScreenRect.Width;
        y1 = TopScreenRect.Y + TopScreenRect.Height;

        scwidth = 256;
        scheight = 192;

        switch (ScreenRotation)
        {
        case 0:
            s0 = 0; t0 = 0;
            s1 = scwidth; t1 = 0;
            s2 = 0; t2 = scheight;
            s3 = scwidth; t3 = scheight;
            break;

        case 1:
            s0 = 0; t0 = scheight;
            s1 = 0; t1 = 0;
            s2 = scwidth; t2 = scheight;
            s3 = scwidth; t3 = 0;
            break;

        case 2:
            s0 = scwidth; t0 = scheight;
            s1 = 0; t1 = scheight;
            s2 = scwidth; t2 = 0;
            s3 = 0; t3 = 0;
            break;

        case 3:
            s0 = scwidth; t0 = 0;
            s1 = scwidth; t1 = scheight;
            s2 = 0; t2 = 0;
            s3 = 0; t3 = scheight;
            break;
        }

        SETVERTEX(0, x0, y0, s0, t0);
        SETVERTEX(1, x1, y1, s3, t3);
        SETVERTEX(2, x1, y0, s1, t1);
        SETVERTEX(3, x0, y0, s0, t0);
        SETVERTEX(4, x0, y1, s2, t2);
        SETVERTEX(5, x1, y1, s3, t3);

        x0 = BottomScreenRect.X;
        y0 = BottomScreenRect.Y;
        x1 = BottomScreenRect.X + BottomScreenRect.Width;
        y1 = BottomScreenRect.Y + BottomScreenRect.Height;

        scwidth = 256;
        scheight = 192;

        switch (ScreenRotation)
        {
        case 0:
            s0 = 0; t0 = 192;
            s1 = scwidth; t1 = 192;
            s2 = 0; t2 = 192+scheight;
            s3 = scwidth; t3 = 192+scheight;
            break;

        case 1:
            s0 = 0; t0 = 192+scheight;
            s1 = 0; t1 = 192;
            s2 = scwidth; t2 = 192+scheight;
            s3 = scwidth; t3 = 192;
            break;

        case 2:
            s0 = scwidth; t0 = 192+scheight;
            s1 = 0; t1 = 192+scheight;
            s2 = scwidth; t2 = 192;
            s3 = 0; t3 = 192;
            break;

        case 3:
            s0 = scwidth; t0 = 192;
            s1 = scwidth; t1 = 192+scheight;
            s2 = 0; t2 = 192;
            s3 = 0; t3 = 192+scheight;
            break;
        }

        SETVERTEX(6, x0, y0, s0, t0);
        SETVERTEX(7, x1, y1, s3, t3);
        SETVERTEX(8, x1, y0, s1, t1);
        SETVERTEX(9, x0, y0, s0, t0);
        SETVERTEX(10, x0, y1, s2, t2);
        SETVERTEX(11, x1, y1, s3, t3);

#undef SETVERTEX

        glBindBuffer(GL_ARRAY_BUFFER, GL_ScreenVertexBufferID);
        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GL_ScreenVertices), GL_ScreenVertices);
    }

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_STENCIL_TEST);
    glDisable(GL_BLEND);
    glColorMaski(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

    glViewport(0, 0, WindowWidth*scale, WindowHeight*scale);

    if (GPU3D::Renderer == 0)
        OpenGL_UseShaderProgram(GL_ScreenShader);
    else
        OpenGL_UseShaderProgram(GL_ScreenShaderAccel);

    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    if (RunningSomething)
    {
        int frontbuf = GPU::FrontBuffer;
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, GL_ScreenTexture);

        if (GPU::Framebuffer[frontbuf][0] && GPU::Framebuffer[frontbuf][1])
        {
            if (GPU3D::Renderer == 0)
            {
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 192, GL_RGBA_INTEGER,
                                GL_UNSIGNED_BYTE, GPU::Framebuffer[frontbuf][0]);
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 192, 256, 192, GL_RGBA_INTEGER,
                                GL_UNSIGNED_BYTE, GPU::Framebuffer[frontbuf][1]);
            }
            else
            {
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256*3 + 1, 192, GL_RGBA_INTEGER,
                                GL_UNSIGNED_BYTE, GPU::Framebuffer[frontbuf][0]);
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 192, 256*3 + 1, 192, GL_RGBA_INTEGER,
                                GL_UNSIGNED_BYTE, GPU::Framebuffer[frontbuf][1]);
            }
        }

        glActiveTexture(GL_TEXTURE1);
        if (GPU3D::Renderer != 0)
            GPU3D::GLRenderer::SetupAccelFrame();

        glBindBuffer(GL_ARRAY_BUFFER, GL_ScreenVertexBufferID);
        glBindVertexArray(GL_ScreenVertexArrayID);
        glDrawArrays(GL_TRIANGLES, 0, 4*3);
    }

    OpenGL_UseShaderProgram(GL_ScreenShaderOSD);
    OSD::Update(true, NULL);

    glFlush();
    uiGLSwapBuffers(GLContext);
}

void MicLoadWav(char* name)
{
    SDL_AudioSpec format;
    memset(&format, 0, sizeof(SDL_AudioSpec));

    if (MicWavBuffer) delete[] MicWavBuffer;
    MicWavBuffer = NULL;
    MicWavLength = 0;

    u8* buf;
    u32 len;
    if (!SDL_LoadWAV(name, &format, &buf, &len))
        return;

    const u64 dstfreq = 44100;

    if (format.format == AUDIO_S16 || format.format == AUDIO_U16)
    {
        int srcinc = format.channels;
        len /= (2 * srcinc);

        MicWavLength = (len * dstfreq) / format.freq;
        if (MicWavLength < 735) MicWavLength = 735;
        MicWavBuffer = new s16[MicWavLength];

        float res_incr = len / (float)MicWavLength;
        float res_timer = 0;
        int res_pos = 0;

        for (int i = 0; i < MicWavLength; i++)
        {
            u16 val = ((u16*)buf)[res_pos];
            if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000;

            MicWavBuffer[i] = val;

            res_timer += res_incr;
            while (res_timer >= 1.0)
            {
                res_timer -= 1.0;
                res_pos += srcinc;
            }
        }
    }
    else if (format.format == AUDIO_S8 || format.format == AUDIO_U8)
    {
        int srcinc = format.channels;
        len /= srcinc;

        MicWavLength = (len * dstfreq) / format.freq;
        if (MicWavLength < 735) MicWavLength = 735;
        MicWavBuffer = new s16[MicWavLength];

        float res_incr = len / (float)MicWavLength;
        float res_timer = 0;
        int res_pos = 0;

        for (int i = 0; i < MicWavLength; i++)
        {
            u16 val = buf[res_pos] << 8;
            if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000;

            MicWavBuffer[i] = val;

            res_timer += res_incr;
            while (res_timer >= 1.0)
            {
                res_timer -= 1.0;
                res_pos += srcinc;
            }
        }
    }
    else
        printf("bad WAV format %08X\n", format.format);

    SDL_FreeWAV(buf);
}

void AudioCallback(void* data, Uint8* stream, int len)
{
    len /= (sizeof(s16) * 2);

    // resample incoming audio to match the output sample rate

    float f_len_in = (len * 32823.6328125) / (float)AudioFreq;
    f_len_in += AudioSampleFrac;
    int len_in = (int)floor(f_len_in);
    AudioSampleFrac = f_len_in - len_in;

    s16 buf_in[1024*2];
    s16* buf_out = (s16*)stream;

    int num_in;
    int num_out = len;

    SDL_LockMutex(AudioSyncLock);
    num_in = SPU::ReadOutput(buf_in, len_in);
    SDL_CondSignal(AudioSync);
    SDL_UnlockMutex(AudioSyncLock);

    if (num_in < 1)
    {
        memset(stream, 0, len*sizeof(s16)*2);
        return;
    }

    int margin = 6;
    if (num_in < len_in-margin)
    {
        int last = num_in-1;
        if (last < 0) last = 0;

        for (int i = num_in; i < len_in-margin; i++)
            ((u32*)buf_in)[i] = ((u32*)buf_in)[last];

        num_in = len_in-margin;
    }

    float res_incr = num_in / (float)num_out;
    float res_timer = 0;
    int res_pos = 0;

    int volume = Config::AudioVolume;

    for (int i = 0; i < len; i++)
    {
        buf_out[i*2  ] = (buf_in[res_pos*2  ] * volume) >> 8;
        buf_out[i*2+1] = (buf_in[res_pos*2+1] * volume) >> 8;

        /*s16 s_l = buf_in[res_pos*2  ];
        s16 s_r = buf_in[res_pos*2+1];

        float a = res_timer;
        float b = 1.0 - a;
        s_l = (s_l * a) + (buf_in[(res_pos-1)*2  ] * b);
        s_r = (s_r * a) + (buf_in[(res_pos-1)*2+1] * b);

        buf_out[i*2  ] = (s_l * volume) >> 8;
        buf_out[i*2+1] = (s_r * volume) >> 8;*/

        res_timer += res_incr;
        while (res_timer >= 1.0)
        {
            res_timer -= 1.0;
            res_pos++;
        }
    }
}

void MicCallback(void* data, Uint8* stream, int len)
{
    if (Config::MicInputType != 1) return;

    s16* input = (s16*)stream;
    len /= sizeof(s16);

    if ((MicBufferWritePos + len) > MicBufferLength)
    {
        u32 len1 = MicBufferLength - MicBufferWritePos;
        memcpy(&MicBuffer[MicBufferWritePos], &input[0], len1*sizeof(s16));
        memcpy(&MicBuffer[0], &input[len1], (len - len1)*sizeof(s16));
        MicBufferWritePos = len - len1;
    }
    else
    {
        memcpy(&MicBuffer[MicBufferWritePos], input, len*sizeof(s16));
        MicBufferWritePos += len;
    }
}

void FeedMicInput()
{
    int type = Config::MicInputType;
    bool cmd = HotkeyDown(HK_Mic);

    if ((type != 1 && !cmd) ||
        (type == 1 && MicBufferLength == 0) ||
        (type == 3 && MicWavBuffer == NULL))
    {
        type = 0;
        MicBufferReadPos = 0;
    }

    switch (type)
    {
    case 0: // no mic
        NDS::MicInputFrame(NULL, 0);
        break;

    case 1: // host mic
        if ((MicBufferReadPos + 735) > MicBufferLength)
        {
            s16 tmp[735];
            u32 len1 = MicBufferLength - MicBufferReadPos;
            memcpy(&tmp[0], &MicBuffer[MicBufferReadPos], len1*sizeof(s16));
            memcpy(&tmp[len1], &MicBuffer[0], (735 - len1)*sizeof(s16));

            NDS::MicInputFrame(tmp, 735);
            MicBufferReadPos = 735 - len1;
        }
        else
        {
            NDS::MicInputFrame(&MicBuffer[MicBufferReadPos], 735);
            MicBufferReadPos += 735;
        }
        break;

    case 2: // white noise
        {
            s16 tmp[735];
            for (int i = 0; i < 735; i++) tmp[i] = rand() & 0xFFFF;
            NDS::MicInputFrame(tmp, 735);
        }
        break;

    case 3: // WAV
        if ((MicBufferReadPos + 735) > MicWavLength)
        {
            s16 tmp[735];
            u32 len1 = MicWavLength - MicBufferReadPos;
            memcpy(&tmp[0], &MicWavBuffer[MicBufferReadPos], len1*sizeof(s16));
            memcpy(&tmp[len1], &MicWavBuffer[0], (735 - len1)*sizeof(s16));

            NDS::MicInputFrame(tmp, 735);
            MicBufferReadPos = 735 - len1;
        }
        else
        {
            NDS::MicInputFrame(&MicWavBuffer[MicBufferReadPos], 735);
            MicBufferReadPos += 735;
        }
        break;
    }
}

void OpenJoystick()
{
    if (Joystick) SDL_JoystickClose(Joystick);

    int num = SDL_NumJoysticks();
    if (num < 1)
    {
        Joystick = NULL;
        return;
    }

    if (JoystickID >= num)
        JoystickID = 0;

    Joystick = SDL_JoystickOpen(JoystickID);
}

bool JoystickButtonDown(int val)
{
    if (val == -1) return false;

    bool hasbtn = ((val & 0xFFFF) != 0xFFFF);

    if (hasbtn)
    {
        if (val & 0x100)
        {
            int hatnum = (val >> 4) & 0xF;
            int hatdir = val & 0xF;
            Uint8 hatval = SDL_JoystickGetHat(Joystick, hatnum);

            bool pressed = false;
            if      (hatdir == 0x1) pressed = (hatval & SDL_HAT_UP);
            else if (hatdir == 0x4) pressed = (hatval & SDL_HAT_DOWN);
            else if (hatdir == 0x2) pressed = (hatval & SDL_HAT_RIGHT);
            else if (hatdir == 0x8) pressed = (hatval & SDL_HAT_LEFT);

            if (pressed) return true;
        }
        else
        {
            int btnnum = val & 0xFFFF;
            Uint8 btnval = SDL_JoystickGetButton(Joystick, btnnum);

            if (btnval) return true;
        }
    }

    if (val & 0x10000)
    {
        int axisnum = (val >> 24) & 0xF;
        int axisdir = (val >> 20) & 0xF;
        Sint16 axisval = SDL_JoystickGetAxis(Joystick, axisnum);

        switch (axisdir)
        {
        case 0: // positive
            if (axisval > 16384) return true;
            break;

        case 1: // negative
            if (axisval < -16384) return true;
            break;

        case 2: // trigger
            if (axisval > 0) return true;
            break;
        }
    }

    return false;
}

void ProcessInput()
{
    SDL_JoystickUpdate();

    if (Joystick)
    {
        if (!SDL_JoystickGetAttached(Joystick))
        {
            SDL_JoystickClose(Joystick);
            Joystick = NULL;
        }
    }
    if (!Joystick && (SDL_NumJoysticks() > 0))
    {
        JoystickID = Config::JoystickID;
        OpenJoystick();
    }

    JoyInputMask = 0xFFF;
    for (int i = 0; i < 12; i++)
        if (JoystickButtonDown(Config::JoyMapping[i]))
            JoyInputMask &= ~(1<<i);

    JoyHotkeyMask = 0;
    for (int i = 0; i < HK_MAX; i++)
        if (JoystickButtonDown(Config::HKJoyMapping[i]))
            JoyHotkeyMask |= (1<<i);

    HotkeyMask = KeyHotkeyMask | JoyHotkeyMask;
    HotkeyPress = HotkeyMask & ~LastHotkeyMask;
    HotkeyRelease = LastHotkeyMask & ~HotkeyMask;
    LastHotkeyMask = HotkeyMask;
}

bool JoyButtonPressed(int btnid, int njoybuttons, Uint8* joybuttons, Uint32 hat)
{
    if (btnid < 0) return false;

    hat &= ~(hat >> 4);

    bool pressed = false;
    if (btnid == 0x101) // up
        pressed = (hat & SDL_HAT_UP);
    else if (btnid == 0x104) // down
        pressed = (hat & SDL_HAT_DOWN);
    else if (btnid == 0x102) // right
        pressed = (hat & SDL_HAT_RIGHT);
    else if (btnid == 0x108) // left
        pressed = (hat & SDL_HAT_LEFT);
    else if (btnid < njoybuttons)
        pressed = (joybuttons[btnid] & ~(joybuttons[btnid] >> 1)) & 0x01;

    return pressed;
}

bool JoyButtonHeld(int btnid, int njoybuttons, Uint8* joybuttons, Uint32 hat)
{
    if (btnid < 0) return false;

    bool pressed = false;
    if (btnid == 0x101) // up
        pressed = (hat & SDL_HAT_UP);
    else if (btnid == 0x104) // down
        pressed = (hat & SDL_HAT_DOWN);
    else if (btnid == 0x102) // right
        pressed = (hat & SDL_HAT_RIGHT);
    else if (btnid == 0x108) // left
        pressed = (hat & SDL_HAT_LEFT);
    else if (btnid < njoybuttons)
        pressed = joybuttons[btnid] & 0x01;

    return pressed;
}

void UpdateWindowTitle(void* data)
{
    if (EmuStatus == 0) return;
    void** dataarray = (void**)data;
    SDL_LockMutex((SDL_mutex*)dataarray[1]);
    uiWindowSetTitle(MainWindow, (const char*)dataarray[0]);
    SDL_UnlockMutex((SDL_mutex*)dataarray[1]);
}

void UpdateFPSLimit(void* data)
{
    uiMenuItemSetChecked(MenuItem_LimitFPS, Config::LimitFPS==1);
}

int EmuThreadFunc(void* burp)
{
    NDS::Init();

    MainScreenPos[0] = 0;
    MainScreenPos[1] = 0;
    MainScreenPos[2] = 0;
    AutoScreenSizing = 0;

    if (Screen_UseGL)
    {
        uiGLMakeContextCurrent(GLContext);
        GPU3D::InitRenderer(true);
        uiGLMakeContextCurrent(NULL);
    }
    else
    {
        GPU3D::InitRenderer(false);
    }

    Touching = false;
    KeyInputMask = 0xFFF;
    JoyInputMask = 0xFFF;
    KeyHotkeyMask = 0;
    JoyHotkeyMask = 0;
    HotkeyMask = 0;
    LastHotkeyMask = 0;
    LidStatus = false;

    u32 nframes = 0;
    u32 starttick = SDL_GetTicks();
    u32 lasttick = starttick;
    u32 lastmeasuretick = lasttick;
    u32 fpslimitcount = 0;
    u64 perfcount = SDL_GetPerformanceCounter();
    u64 perffreq = SDL_GetPerformanceFrequency();
    float samplesleft = 0;
    u32 nsamples = 0;

    char melontitle[100];
    SDL_mutex* titlemutex = SDL_CreateMutex();
    void* titledata[2] = {melontitle, titlemutex};

    while (EmuRunning != 0)
    {
        ProcessInput();

        if (HotkeyPressed(HK_FastForwardToggle))
        {
            Config::LimitFPS = !Config::LimitFPS;
            uiQueueMain(UpdateFPSLimit, NULL);
        }
        // TODO: similar hotkeys for video/audio sync?

        if (HotkeyPressed(HK_Pause)) uiQueueMain(TogglePause, NULL);
        if (HotkeyPressed(HK_Reset)) uiQueueMain(Reset, NULL);

        if (GBACart::CartInserted && GBACart::HasSolarSensor)
        {
            if (HotkeyPressed(HK_SolarSensorDecrease))
            {
                if (GBACart_SolarSensor::LightLevel > 0) GBACart_SolarSensor::LightLevel--;
                char msg[64];
                sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
                OSD::AddMessage(0, msg);
            }
            if (HotkeyPressed(HK_SolarSensorIncrease))
            {
                if (GBACart_SolarSensor::LightLevel < 10) GBACart_SolarSensor::LightLevel++;
                char msg[64];
                sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
                OSD::AddMessage(0, msg);
            }
        }

        if (EmuRunning == 1)
        {
            EmuStatus = 1;

            // process input and hotkeys
            NDS::SetKeyMask(KeyInputMask & JoyInputMask);

            if (HotkeyPressed(HK_Lid))
            {
                LidStatus = !LidStatus;
                NDS::SetLidClosed(LidStatus);
                OSD::AddMessage(0, LidStatus ? "Lid closed" : "Lid opened");
            }

            // microphone input
            FeedMicInput();

            if (Screen_UseGL)
            {
                uiGLBegin(GLContext);
                uiGLMakeContextCurrent(GLContext);
            }

            // auto screen layout
            {
                MainScreenPos[2] = MainScreenPos[1];
                MainScreenPos[1] = MainScreenPos[0];
                MainScreenPos[0] = NDS::PowerControl9 >> 15;

                int guess;
                if (MainScreenPos[0] == MainScreenPos[2] &&
                    MainScreenPos[0] != MainScreenPos[1])
                {
                    // constant flickering, likely displaying 3D on both screens
                    // TODO: when both screens are used for 2D only...???
                    guess = 0;
                }
                else
                {
                    if (MainScreenPos[0] == 1)
                        guess = 1;
                    else
                        guess = 2;
                }

                if (guess != AutoScreenSizing)
                {
                    AutoScreenSizing = guess;
                    SetupScreenRects(WindowWidth, WindowHeight);
                }
            }

            // emulate
            u32 nlines = NDS::RunFrame();

#ifdef MELONCAP
            MelonCap::Update();
#endif // MELONCAP

            if (EmuRunning == 0) break;

            if (Screen_UseGL)
            {
                GLScreen_DrawScreen();
                uiGLEnd(GLContext);
            }
            uiAreaQueueRedrawAll(MainDrawArea);

            bool fastforward = HotkeyDown(HK_FastForward);

            if (Config::AudioSync && !fastforward)
            {
                SDL_LockMutex(AudioSyncLock);
                while (SPU::GetOutputSize() > 1024)
                {
                    int ret = SDL_CondWaitTimeout(AudioSync, AudioSyncLock, 500);
                    if (ret == SDL_MUTEX_TIMEDOUT) break;
                }
                SDL_UnlockMutex(AudioSyncLock);
            }
            else
            {
                // ensure the audio FIFO doesn't overflow
                //SPU::TrimOutput();
            }

            float framerate = (1000.0f * nlines) / (60.0f * 263.0f);

            {
                u32 curtick = SDL_GetTicks();
                u32 delay = curtick - lasttick;

                bool limitfps = Config::LimitFPS && !fastforward;
                if (limitfps)
                {
                    float wantedtickF = starttick + (framerate * (fpslimitcount+1));
                    u32 wantedtick = (u32)ceil(wantedtickF);
                    if (curtick < wantedtick) SDL_Delay(wantedtick - curtick);

                    lasttick = SDL_GetTicks();
                    fpslimitcount++;
                    if ((abs(wantedtickF - (float)wantedtick) < 0.001312) || (fpslimitcount > 60))
                    {
                        fpslimitcount = 0;
                        nsamples = 0;
                        starttick = lasttick;
                    }
                }
                else
                {
                    if (delay < 1) SDL_Delay(1);
                    lasttick = SDL_GetTicks();
                }
            }

            nframes++;
            if (nframes >= 30)
            {
                u32 tick = SDL_GetTicks();
                u32 diff = tick - lastmeasuretick;
                lastmeasuretick = tick;

                u32 fps;
                if (diff < 1) fps = 77777;
                else fps = (nframes * 1000) / diff;
                nframes = 0;

                float fpstarget;
                if (framerate < 1) fpstarget = 999;
                else fpstarget = 1000.0f/framerate;

                SDL_LockMutex(titlemutex);
                sprintf(melontitle, "[%d/%.0f] melonDS " MELONDS_VERSION, fps, fpstarget);
                SDL_UnlockMutex(titlemutex);
                uiQueueMain(UpdateWindowTitle, titledata);
            }
        }
        else
        {
            // paused
            nframes = 0;
            lasttick = SDL_GetTicks();
            starttick = lasttick;
            lastmeasuretick = lasttick;
            fpslimitcount = 0;

            if (EmuRunning == 2)
            {
                if (Screen_UseGL)
                {
                    uiGLBegin(GLContext);
                    uiGLMakeContextCurrent(GLContext);
                    GLScreen_DrawScreen();
                    uiGLEnd(GLContext);
                }
                uiAreaQueueRedrawAll(MainDrawArea);
            }

            if (Screen_UseGL) uiGLMakeContextCurrent(NULL);

            EmuStatus = EmuRunning;

            SDL_Delay(100);
        }
    }

    EmuStatus = 0;

    SDL_DestroyMutex(titlemutex);

    if (Screen_UseGL) uiGLMakeContextCurrent(GLContext);

    NDS::DeInit();
    Platform::LAN_DeInit();

    if (Screen_UseGL)
    {
        OSD::DeInit(true);
        GLScreen_DeInit();
    }
    else
        OSD::DeInit(false);

    if (Screen_UseGL) uiGLMakeContextCurrent(NULL);

    return 44203;
}

void StopEmuThread()
{
    EmuRunning = 0;
    SDL_WaitThread(EmuThread, NULL);
}


void OnAreaDraw(uiAreaHandler* handler, uiArea* area, uiAreaDrawParams* params)
{
    if (!ScreenDrawInited)
    {
        if (ScreenBitmap[0]) uiDrawFreeBitmap(ScreenBitmap[0]);
        if (ScreenBitmap[1]) uiDrawFreeBitmap(ScreenBitmap[1]);

        ScreenDrawInited = true;
        ScreenBitmap[0] = uiDrawNewBitmap(params->Context, 256, 192, 0);
        ScreenBitmap[1] = uiDrawNewBitmap(params->Context, 256, 192, 0);
    }

    int frontbuf = GPU::FrontBuffer;
    if (!ScreenBitmap[0] || !ScreenBitmap[1]) return;
    if (!GPU::Framebuffer[frontbuf][0] || !GPU::Framebuffer[frontbuf][1]) return;

    uiRect top = {0, 0, 256, 192};
    uiRect bot = {0, 0, 256, 192};

    uiDrawBitmapUpdate(ScreenBitmap[0], GPU::Framebuffer[frontbuf][0]);
    uiDrawBitmapUpdate(ScreenBitmap[1], GPU::Framebuffer[frontbuf][1]);

    uiDrawSave(params->Context);
    uiDrawTransform(params->Context, &TopScreenTrans);
    uiDrawBitmapDraw(params->Context, ScreenBitmap[0], &top, &TopScreenRect, Config::ScreenFilter==1);
    uiDrawRestore(params->Context);

    uiDrawSave(params->Context);
    uiDrawTransform(params->Context, &BottomScreenTrans);
    uiDrawBitmapDraw(params->Context, ScreenBitmap[1], &bot, &BottomScreenRect, Config::ScreenFilter==1);
    uiDrawRestore(params->Context);

    OSD::Update(false, params);
}

void OnAreaMouseEvent(uiAreaHandler* handler, uiArea* area, uiAreaMouseEvent* evt)
{
    int x = (int)evt->X;
    int y = (int)evt->Y;

    if (Touching && (evt->Up == 1))
    {
        Touching = false;
        NDS::ReleaseKey(16+6);
        NDS::ReleaseScreen();
    }
    else if (!Touching && (evt->Down == 1) &&
             (x >= BottomScreenRect.X) && (y >= BottomScreenRect.Y) &&
             (x < (BottomScreenRect.X+BottomScreenRect.Width)) && (y < (BottomScreenRect.Y+BottomScreenRect.Height)))
    {
        Touching = true;
        NDS::PressKey(16+6);
    }

    if (Touching)
    {
        x -= BottomScreenRect.X;
        y -= BottomScreenRect.Y;

        if (ScreenRotation == 0 || ScreenRotation == 2)
        {
            if (BottomScreenRect.Width != 256)
                x = (x * 256) / BottomScreenRect.Width;
            if (BottomScreenRect.Height != 192)
                y = (y * 192) / BottomScreenRect.Height;

            if (ScreenRotation == 2)
            {
                x = 255 - x;
                y = 191 - y;
            }
        }
        else
        {
            if (BottomScreenRect.Width != 192)
                x = (x * 192) / BottomScreenRect.Width;
            if (BottomScreenRect.Height != 256)
                y = (y * 256) / BottomScreenRect.Height;

            if (ScreenRotation == 1)
            {
                int tmp = x;
                x = y;
                y = 191 - tmp;
            }
            else
            {
                int tmp = x;
                x = 255 - y;
                y = tmp;
            }
        }

        // clamp
        if (x < 0) x = 0;
        else if (x > 255) x = 255;
        if (y < 0) y = 0;
        else if (y > 191) y = 191;

        // TODO: take advantage of possible extra precision when possible? (scaled window for example)
        NDS::TouchScreen(x, y);
    }
}

void OnAreaMouseCrossed(uiAreaHandler* handler, uiArea* area, int left)
{
}

void OnAreaDragBroken(uiAreaHandler* handler, uiArea* area)
{
}

bool EventMatchesKey(uiAreaKeyEvent* evt, int val, bool checkmod)
{
    if (val == -1) return false;

    int key = val & 0xFFFF;
    int mod = val >> 16;
    return evt->Scancode == key && (!checkmod || evt->Modifiers == mod);
}

int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
{
    // TODO: release all keys if the window loses focus? or somehow global key input?
    if (evt->Scancode == 0x38) // ALT
        return 0;
    if (evt->Modifiers == 0x2) // ALT+key
        return 0;

    if (evt->Up)
    {
        for (int i = 0; i < 12; i++)
            if (EventMatchesKey(evt, Config::KeyMapping[i], false))
                KeyInputMask |= (1<<i);

        for (int i = 0; i < HK_MAX; i++)
            if (EventMatchesKey(evt, Config::HKKeyMapping[i], true))
                KeyHotkeyMask &= ~(1<<i);
    }
    else if (!evt->Repeat)
    {
        // TODO, eventually: make savestate keys configurable?
        // F keys: 3B-44, 57-58 | SHIFT: mod. 0x4
        if (evt->Scancode >= 0x3B && evt->Scancode <= 0x42) // F1-F8, quick savestate
        {
            if      (evt->Modifiers == 0x4) SaveState(1 + (evt->Scancode - 0x3B));
            else if (evt->Modifiers == 0x0) LoadState(1 + (evt->Scancode - 0x3B));
        }
        else if (evt->Scancode == 0x43) // F9, savestate from/to file
        {
            if      (evt->Modifiers == 0x4) SaveState(0);
            else if (evt->Modifiers == 0x0) LoadState(0);
        }
        else if (evt->Scancode == 0x58) // F12, undo savestate
        {
            if (evt->Modifiers == 0x0) UndoStateLoad();
        }

        for (int i = 0; i < 12; i++)
            if (EventMatchesKey(evt, Config::KeyMapping[i], false))
                KeyInputMask &= ~(1<<i);

        for (int i = 0; i < HK_MAX; i++)
            if (EventMatchesKey(evt, Config::HKKeyMapping[i], true))
                KeyHotkeyMask |= (1<<i);

        // REMOVE ME
        //if (evt->Scancode == 0x57) // F11
        //    NDS::debug(0);
    }

    return 1;
}

void SetupScreenRects(int width, int height)
{
    bool horizontal = false;
    bool sideways = false;

    if (ScreenRotation == 1 || ScreenRotation == 3)
        sideways = true;

    if (ScreenLayout == 2) horizontal = true;
    else if (ScreenLayout == 0)
    {
        if (sideways)
            horizontal = true;
    }

    int sizemode;
    if (ScreenSizing == 3)
        sizemode = AutoScreenSizing;
    else
        sizemode = ScreenSizing;

    int screenW, screenH, gap;
    if (sideways)
    {
        screenW = 192;
        screenH = 256;
    }
    else
    {
        screenW = 256;
        screenH = 192;
    }

    gap = ScreenGap;

    uiRect *topscreen, *bottomscreen;
    if (ScreenRotation == 1 || ScreenRotation == 2)
    {
        topscreen = &BottomScreenRect;
        bottomscreen = &TopScreenRect;
    }
    else
    {
        topscreen = &TopScreenRect;
        bottomscreen = &BottomScreenRect;
    }

    if (horizontal)
    {
        // side-by-side

        int heightreq;
        int startX = 0;

        width -= gap;

        if (sizemode == 0) // even
        {
            heightreq = (width * screenH) / (screenW*2);
            if (heightreq > height)
            {
                int newwidth = (height * width) / heightreq;
                startX = (width - newwidth) / 2;
                heightreq = height;
                width = newwidth;
            }
        }
        else // emph. top/bottom
        {
            heightreq = ((width - screenW) * screenH) / screenW;
            if (heightreq > height)
            {
                int newwidth = ((height * (width - screenW)) / heightreq) + screenW;
                startX = (width - newwidth) / 2;
                heightreq = height;
                width = newwidth;
            }
        }

        if (sizemode == 2)
        {
            topscreen->Width = screenW;
            topscreen->Height = screenH;
        }
        else
        {
            topscreen->Width = (sizemode==0) ? (width / 2) : (width - screenW);
            topscreen->Height = heightreq;
        }
        topscreen->X = startX;
        topscreen->Y = ((height - heightreq) / 2) + (heightreq - topscreen->Height);

        bottomscreen->X = topscreen->X + topscreen->Width + gap;

        if (sizemode == 1)
        {
            bottomscreen->Width = screenW;
            bottomscreen->Height = screenH;
        }
        else
        {
            bottomscreen->Width = width - topscreen->Width;
            bottomscreen->Height = heightreq;
        }
        bottomscreen->Y = ((height - heightreq) / 2) + (heightreq - bottomscreen->Height);
    }
    else
    {
        // top then bottom

        int widthreq;
        int startY = 0;

        height -= gap;

        if (sizemode == 0) // even
        {
            widthreq = (height * screenW) / (screenH*2);
            if (widthreq > width)
            {
                int newheight = (width * height) / widthreq;
                startY = (height - newheight) / 2;
                widthreq = width;
                height = newheight;
            }
        }
        else // emph. top/bottom
        {
            widthreq = ((height - screenH) * screenW) / screenH;
            if (widthreq > width)
            {
                int newheight = ((width * (height - screenH)) / widthreq) + screenH;
                startY = (height - newheight) / 2;
                widthreq = width;
                height = newheight;
            }
        }

        if (sizemode == 2)
        {
            topscreen->Width = screenW;
            topscreen->Height = screenH;
        }
        else
        {
            topscreen->Width = widthreq;
            topscreen->Height = (sizemode==0) ? (height / 2) : (height - screenH);
        }
        topscreen->Y = startY;
        topscreen->X = (width - topscreen->Width) / 2;

        bottomscreen->Y = topscreen->Y + topscreen->Height + gap;

        if (sizemode == 1)
        {
            bottomscreen->Width = screenW;
            bottomscreen->Height = screenH;
        }
        else
        {
            bottomscreen->Width = widthreq;
            bottomscreen->Height = height - topscreen->Height;
        }
        bottomscreen->X = (width - bottomscreen->Width) / 2;
    }

    // setup matrices for potential rotation

    uiDrawMatrixSetIdentity(&TopScreenTrans);
    uiDrawMatrixSetIdentity(&BottomScreenTrans);

    switch (ScreenRotation)
    {
    case 1: // 90°
        {
            uiDrawMatrixTranslate(&TopScreenTrans, -TopScreenRect.X, -TopScreenRect.Y);
            uiDrawMatrixRotate(&TopScreenTrans, 0, 0, M_PI/2.0f);
            uiDrawMatrixScale(&TopScreenTrans, 0, 0,
                              TopScreenRect.Width/(double)TopScreenRect.Height,
                              TopScreenRect.Height/(double)TopScreenRect.Width);
            uiDrawMatrixTranslate(&TopScreenTrans, TopScreenRect.X+TopScreenRect.Width, TopScreenRect.Y);

            uiDrawMatrixTranslate(&BottomScreenTrans, -BottomScreenRect.X, -BottomScreenRect.Y);
            uiDrawMatrixRotate(&BottomScreenTrans, 0, 0, M_PI/2.0f);
            uiDrawMatrixScale(&BottomScreenTrans, 0, 0,
                              BottomScreenRect.Width/(double)BottomScreenRect.Height,
                              BottomScreenRect.Height/(double)BottomScreenRect.Width);
            uiDrawMatrixTranslate(&BottomScreenTrans, BottomScreenRect.X+BottomScreenRect.Width, BottomScreenRect.Y);
        }
        break;

    case 2: // 180°
        {
            uiDrawMatrixTranslate(&TopScreenTrans, -TopScreenRect.X, -TopScreenRect.Y);
            uiDrawMatrixRotate(&TopScreenTrans, 0, 0, M_PI);
            uiDrawMatrixTranslate(&TopScreenTrans, TopScreenRect.X+TopScreenRect.Width, TopScreenRect.Y+TopScreenRect.Height);

            uiDrawMatrixTranslate(&BottomScreenTrans, -BottomScreenRect.X, -BottomScreenRect.Y);
            uiDrawMatrixRotate(&BottomScreenTrans, 0, 0, M_PI);
            uiDrawMatrixTranslate(&BottomScreenTrans, BottomScreenRect.X+BottomScreenRect.Width, BottomScreenRect.Y+BottomScreenRect.Height);
        }
        break;

    case 3: // 270°
        {
            uiDrawMatrixTranslate(&TopScreenTrans, -TopScreenRect.X, -TopScreenRect.Y);
            uiDrawMatrixRotate(&TopScreenTrans, 0, 0, -M_PI/2.0f);
            uiDrawMatrixScale(&TopScreenTrans, 0, 0,
                              TopScreenRect.Width/(double)TopScreenRect.Height,
                              TopScreenRect.Height/(double)TopScreenRect.Width);
            uiDrawMatrixTranslate(&TopScreenTrans, TopScreenRect.X, TopScreenRect.Y+TopScreenRect.Height);

            uiDrawMatrixTranslate(&BottomScreenTrans, -BottomScreenRect.X, -BottomScreenRect.Y);
            uiDrawMatrixRotate(&BottomScreenTrans, 0, 0, -M_PI/2.0f);
            uiDrawMatrixScale(&BottomScreenTrans, 0, 0,
                              BottomScreenRect.Width/(double)BottomScreenRect.Height,
                              BottomScreenRect.Height/(double)BottomScreenRect.Width);
            uiDrawMatrixTranslate(&BottomScreenTrans, BottomScreenRect.X, BottomScreenRect.Y+BottomScreenRect.Height);
        }
        break;
    }

    GL_ScreenSizeDirty = true;
}

void SetMinSize(int w, int h)
{
    int cw, ch;
    uiWindowContentSize(MainWindow, &cw, &ch);

    uiControlSetMinSize(uiControl(MainDrawArea), w, h);
    if ((cw < w) || (ch < h))
    {
        if (cw < w) cw = w;
        if (ch < h) ch = h;
        uiWindowSetContentSize(MainWindow, cw, ch);
    }
}

void OnAreaResize(uiAreaHandler* handler, uiArea* area, int width, int height)
{
    SetupScreenRects(width, height);

    // TODO:
    // should those be the size of the uiArea, or the size of the window client area?
    // for now the uiArea fills the whole window anyway
    // but... we never know, I guess
    WindowWidth = width;
    WindowHeight = height;

    int ismax = uiWindowMaximized(MainWindow);
    int ismin = uiWindowMinimized(MainWindow);

    Config::WindowMaximized = ismax;
    if (!ismax && !ismin)
    {
        Config::WindowWidth = width;
        Config::WindowHeight = height;
    }

    OSD::WindowResized(Screen_UseGL);
}


void Run()
{
    EmuRunning = 1;
    RunningSomething = true;

    SPU::InitOutput();
    AudioSampleFrac = 0;
    SDL_PauseAudioDevice(AudioDevice, 0);
    SDL_PauseAudioDevice(MicDevice, 0);

    uiMenuItemEnable(MenuItem_SaveState);
    uiMenuItemEnable(MenuItem_LoadState);

    if (SavestateLoaded)
        uiMenuItemEnable(MenuItem_UndoStateLoad);
    else
        uiMenuItemDisable(MenuItem_UndoStateLoad);

    for (int i = 0; i < 8; i++)
    {
        char ssfile[1024];
        GetSavestateName(i+1, ssfile, 1024);
        if (Platform::FileExists(ssfile)) uiMenuItemEnable(MenuItem_LoadStateSlot[i]);
        else                              uiMenuItemDisable(MenuItem_LoadStateSlot[i]);
    }

    for (int i = 0; i < 9; i++) uiMenuItemEnable(MenuItem_SaveStateSlot[i]);
    uiMenuItemEnable(MenuItem_LoadStateSlot[8]);

    uiMenuItemEnable(MenuItem_Pause);
    uiMenuItemEnable(MenuItem_Reset);
    uiMenuItemEnable(MenuItem_Stop);
    uiMenuItemSetChecked(MenuItem_Pause, 0);
}

void TogglePause(void* blarg)
{
    if (!RunningSomething) return;

    if (EmuRunning == 1)
    {
        // enable pause
        EmuRunning = 2;
        uiMenuItemSetChecked(MenuItem_Pause, 1);

        SPU::DrainOutput();
        SDL_PauseAudioDevice(AudioDevice, 1);
        SDL_PauseAudioDevice(MicDevice, 1);

        OSD::AddMessage(0, "Paused");
    }
    else
    {
        // disable pause
        EmuRunning = 1;
        uiMenuItemSetChecked(MenuItem_Pause, 0);

        SPU::InitOutput();
        AudioSampleFrac = 0;
        SDL_PauseAudioDevice(AudioDevice, 0);
        SDL_PauseAudioDevice(MicDevice, 0);

        OSD::AddMessage(0, "Resumed");
    }
}

void Reset(void* blarg)
{
    if (!RunningSomething) return;

    EmuRunning = 2;
    while (EmuStatus != 2);

    SavestateLoaded = false;
    uiMenuItemDisable(MenuItem_UndoStateLoad);

    if (ROMPath[0][0] == '\0')
        NDS::LoadBIOS();
    else
    {
        SetupSRAMPath(0);
        NDS::LoadROM(ROMPath[0], SRAMPath[0], Config::DirectBoot);
    }

    if (ROMPath[1][0] != '\0')
    {
        SetupSRAMPath(1);
        NDS::LoadGBAROM(ROMPath[1], SRAMPath[1]);
    }

    Run();

    OSD::AddMessage(0, "Reset");
}

void Stop(bool internal)
{
    EmuRunning = 2;
    if (!internal) // if shutting down from the UI thread, wait till the emu thread has stopped
        while (EmuStatus != 2);
    RunningSomething = false;

    // eject any inserted GBA cartridge
    GBACart::Eject();
    ROMPath[1][0] = '\0';

    uiWindowSetTitle(MainWindow, "melonDS " MELONDS_VERSION);

    for (int i = 0; i < 9; i++) uiMenuItemDisable(MenuItem_SaveStateSlot[i]);
    for (int i = 0; i < 9; i++) uiMenuItemDisable(MenuItem_LoadStateSlot[i]);
    uiMenuItemDisable(MenuItem_UndoStateLoad);

    uiMenuItemDisable(MenuItem_Pause);
    uiMenuItemDisable(MenuItem_Reset);
    uiMenuItemDisable(MenuItem_Stop);
    uiMenuItemSetChecked(MenuItem_Pause, 0);

    uiAreaQueueRedrawAll(MainDrawArea);

    SPU::DrainOutput();
    SDL_PauseAudioDevice(AudioDevice, 1);
    SDL_PauseAudioDevice(MicDevice, 1);

    OSD::AddMessage(0xFFC040, "Shutdown");
}

void SetupSRAMPath(int slot)
{
    strncpy(SRAMPath[slot], ROMPath[slot], 1023);
    SRAMPath[slot][1023] = '\0';
    strncpy(SRAMPath[slot] + strlen(ROMPath[slot]) - 3, "sav", 3);
}

void TryLoadROM(char* file, int slot, int prevstatus)
{
    char oldpath[1024];
    char oldsram[1024];
    strncpy(oldpath, ROMPath[slot], 1024);
    strncpy(oldsram, SRAMPath[slot], 1024);

    strncpy(ROMPath[slot], file, 1023);
    ROMPath[slot][1023] = '\0';

    SetupSRAMPath(0);
    SetupSRAMPath(1);

    if (slot == 0 && NDS::LoadROM(ROMPath[slot], SRAMPath[slot], Config::DirectBoot))
    {
        SavestateLoaded = false;
        uiMenuItemDisable(MenuItem_UndoStateLoad);

        // Reload the inserted GBA cartridge (if any)
        if (ROMPath[1][0] != '\0') NDS::LoadGBAROM(ROMPath[1], SRAMPath[1]);

        strncpy(PrevSRAMPath[slot], SRAMPath[slot], 1024); // safety
        Run();
    }
    else if (slot == 1 && NDS::LoadGBAROM(ROMPath[slot], SRAMPath[slot]))
    {
        SavestateLoaded = false;
        uiMenuItemDisable(MenuItem_UndoStateLoad);

        strncpy(PrevSRAMPath[slot], SRAMPath[slot], 1024); // safety
        if (RunningSomething) Run(); // do not start just from a GBA cart
    }
    else
    {
        uiMsgBoxError(MainWindow,
                      "Failed to load the ROM",
                      "Make sure the file can be accessed and isn't opened in another application.");

        strncpy(ROMPath[slot], oldpath, 1024);
        strncpy(SRAMPath[slot], oldsram, 1024);
        EmuRunning = prevstatus;
    }
}


// SAVESTATE TODO
// * configurable paths. not everyone wants their ROM directory to be polluted, I guess.

void GetSavestateName(int slot, char* filename, int len)
{
    int pos;

    if (ROMPath[0][0] == '\0') // running firmware, no ROM
    {
        strcpy(filename, "firmware");
        pos = 8;
    }
    else
    {
        int l = strlen(ROMPath[0]);
        pos = l;
        while (ROMPath[0][pos] != '.' && pos > 0) pos--;
        if (pos == 0) pos = l;

        // avoid buffer overflow. shoddy
        if (pos > len-5) pos = len-5;

        strncpy(&filename[0], ROMPath[0], pos);
    }
    strcpy(&filename[pos], ".ml");
    filename[pos+3] = '0'+slot;
    filename[pos+4] = '\0';
}

void LoadState(int slot)
{
    int prevstatus = EmuRunning;
    EmuRunning = 2;
    while (EmuStatus != 2);

    char filename[1024];

    if (slot > 0)
    {
        GetSavestateName(slot, filename, 1024);
    }
    else
    {
        char* file = uiOpenFile(MainWindow, "melonDS savestate (any)|*.ml1;*.ml2;*.ml3;*.ml4;*.ml5;*.ml6;*.ml7;*.ml8;*.mln", Config::LastROMFolder);
        if (!file)
        {
            EmuRunning = prevstatus;
            return;
        }

        strncpy(filename, file, 1023);
        filename[1023] = '\0';
        uiFreeText(file);
    }

    if (!Platform::FileExists(filename))
    {
        char msg[64];
        if (slot > 0) sprintf(msg, "State slot %d is empty", slot);
        else          sprintf(msg, "State file does not exist");
        OSD::AddMessage(0xFFA0A0, msg);

        EmuRunning = prevstatus;
        return;
    }

    u32 oldGBACartCRC = GBACart::CartCRC;

    // backup
    Savestate* backup = new Savestate("timewarp.mln", true);
    NDS::DoSavestate(backup);
    delete backup;

    bool failed = false;

    Savestate* state = new Savestate(filename, false);
    if (state->Error)
    {
        delete state;

        uiMsgBoxError(MainWindow, "Error", "Could not load savestate file.");

        // current state might be crapoed, so restore from sane backup
        state = new Savestate("timewarp.mln", false);
        failed = true;
    }

    NDS::DoSavestate(state);
    delete state;

    if (!failed)
    {
        if (Config::SavestateRelocSRAM && ROMPath[0][0]!='\0')
        {
            strncpy(PrevSRAMPath[0], SRAMPath[0], 1024);

            strncpy(SRAMPath[0], filename, 1019);
            int len = strlen(SRAMPath[0]);
            strcpy(&SRAMPath[0][len], ".sav");
            SRAMPath[0][len+4] = '\0';

            NDS::RelocateSave(SRAMPath[0], false);
        }

        bool loadedPartialGBAROM = false;

        // in case we have a GBA cart inserted, and the GBA ROM changes
        // due to having loaded a save state, we do not want to reload
        // the previous cartridge on reset, or commit writes to any
        // loaded save file. therefore, their paths are "nulled".
        if (GBACart::CartInserted && GBACart::CartCRC != oldGBACartCRC)
        {
            ROMPath[1][0] = '\0';
            SRAMPath[1][0] = '\0';
            loadedPartialGBAROM = true;
        }

        char msg[64];
        if (slot > 0) sprintf(msg, "State loaded from slot %d%s",
                        slot, loadedPartialGBAROM ? " (GBA ROM header only)" : "");
        else          sprintf(msg, "State loaded from file%s",
                        loadedPartialGBAROM ? " (GBA ROM header only)" : "");
        OSD::AddMessage(0, msg);

        SavestateLoaded = true;
        uiMenuItemEnable(MenuItem_UndoStateLoad);
    }

    EmuRunning = prevstatus;
}

void SaveState(int slot)
{
    int prevstatus = EmuRunning;
    EmuRunning = 2;
    while (EmuStatus != 2);

    char filename[1024];

    if (slot > 0)
    {
        GetSavestateName(slot, filename, 1024);
    }
    else
    {
        char* file = uiSaveFile(MainWindow, "melonDS savestate (*.mln)|*.mln", Config::LastROMFolder);
        if (!file)
        {
            EmuRunning = prevstatus;
            return;
        }

        strncpy(filename, file, 1023);
        filename[1023] = '\0';
        uiFreeText(file);
    }

    Savestate* state = new Savestate(filename, true);
    if (state->Error)
    {
        delete state;

        uiMsgBoxError(MainWindow, "Error", "Could not save state.");
    }
    else
    {
        NDS::DoSavestate(state);
        delete state;

        if (slot > 0)
            uiMenuItemEnable(MenuItem_LoadStateSlot[slot-1]);

        if (Config::SavestateRelocSRAM && ROMPath[0][0]!='\0')
        {
            strncpy(SRAMPath[0], filename, 1019);
            int len = strlen(SRAMPath[0]);
            strcpy(&SRAMPath[0][len], ".sav");
            SRAMPath[0][len+4] = '\0';

            NDS::RelocateSave(SRAMPath[0], true);
        }
    }

    char msg[64];
    if (slot > 0) sprintf(msg, "State saved to slot %d", slot);
    else          sprintf(msg, "State saved to file");
    OSD::AddMessage(0, msg);

    EmuRunning = prevstatus;
}

void UndoStateLoad()
{
    if (!SavestateLoaded) return;

    int prevstatus = EmuRunning;
    EmuRunning = 2;
    while (EmuStatus != 2);

    // pray that this works
    // what do we do if it doesn't???
    // but it should work.
    Savestate* backup = new Savestate("timewarp.mln", false);
    NDS::DoSavestate(backup);
    delete backup;

    if (ROMPath[0][0]!='\0')
    {
        strncpy(SRAMPath[0], PrevSRAMPath[0], 1024);
        NDS::RelocateSave(SRAMPath[0], false);
    }

    OSD::AddMessage(0, "State load undone");

    EmuRunning = prevstatus;
}


void CloseAllDialogs()
{
    DlgAudioSettings::Close();
    DlgEmuSettings::Close();
    DlgInputConfig::Close(0);
    DlgInputConfig::Close(1);
    DlgVideoSettings::Close();
    DlgWifiSettings::Close();
}


int OnCloseWindow(uiWindow* window, void* blarg)
{
    EmuRunning = 3;
    while (EmuStatus != 3);

    CloseAllDialogs();
    StopEmuThread();
    uiQuit();
    return 1;
}

void OnDropFile(uiWindow* window, char* file, void* blarg)
{
    char* ext = &file[strlen(file)-3];
    int prevstatus = EmuRunning;

    if (!strcasecmp(ext, "nds") || !strcasecmp(ext, "srl"))
    {
        if (RunningSomething)
        {
            EmuRunning = 2;
            while (EmuStatus != 2);
        }

        TryLoadROM(file, 0, prevstatus);
    }
    else if (!strcasecmp(ext, "gba"))
    {
        TryLoadROM(file, 1, prevstatus);
    }
}

void OnGetFocus(uiWindow* window, void* blarg)
{
    uiControlSetFocus(uiControl(MainDrawArea));
}

void OnLoseFocus(uiWindow* window, void* blarg)
{
    // TODO: shit here?
}

void OnCloseByMenu(uiMenuItem* item, uiWindow* window, void* blarg)
{
    EmuRunning = 3;
    while (EmuStatus != 3);

    CloseAllDialogs();
    StopEmuThread();
    DestroyMainWindow();
    uiQuit();
}

void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg)
{
    int prevstatus = EmuRunning;
    EmuRunning = 2;
    while (EmuStatus != 2);

    char* file = uiOpenFile(window, "DS ROM (*.nds)|*.nds;*.srl|GBA ROM (*.gba)|*.gba|Any file|*.*", Config::LastROMFolder);
    if (!file)
    {
        EmuRunning = prevstatus;
        return;
    }

    int pos = strlen(file)-1;
    while (file[pos] != '/' && file[pos] != '\\' && pos > 0) pos--;
    strncpy(Config::LastROMFolder, file, pos);
    Config::LastROMFolder[pos] = '\0';
    char* ext = &file[strlen(file)-3];

    if (!strcasecmp(ext, "gba"))
    {
        TryLoadROM(file, 1, prevstatus);
    }
    else
    {
        TryLoadROM(file, 0, prevstatus);
    }

    uiFreeText(file);
}

void OnSaveState(uiMenuItem* item, uiWindow* window, void* param)
{
    int slot = *(int*)param;
    SaveState(slot);
}

void OnLoadState(uiMenuItem* item, uiWindow* window, void* param)
{
    int slot = *(int*)param;
    LoadState(slot);
}

void OnUndoStateLoad(uiMenuItem* item, uiWindow* window, void* param)
{
    UndoStateLoad();
}

void OnRun(uiMenuItem* item, uiWindow* window, void* blarg)
{
    if (!RunningSomething)
    {
        ROMPath[0][0] = '\0';
        NDS::LoadBIOS();

        if (ROMPath[1][0] != '\0')
        {
            SetupSRAMPath(1);
            NDS::LoadGBAROM(ROMPath[1], SRAMPath[1]);
        }
    }

    Run();
}

void OnPause(uiMenuItem* item, uiWindow* window, void* blarg)
{
    TogglePause(NULL);
}

void OnReset(uiMenuItem* item, uiWindow* window, void* blarg)
{
    Reset(NULL);
}

void OnStop(uiMenuItem* item, uiWindow* window, void* blarg)
{
    if (!RunningSomething) return;

    Stop(false);
}

void OnOpenEmuSettings(uiMenuItem* item, uiWindow* window, void* blarg)
{
    DlgEmuSettings::Open();
}

void OnOpenInputConfig(uiMenuItem* item, uiWindow* window, void* blarg)
{
    DlgInputConfig::Open(0);
}

void OnOpenHotkeyConfig(uiMenuItem* item, uiWindow* window, void* blarg)
{
    DlgInputConfig::Open(1);
}

void OnOpenVideoSettings(uiMenuItem* item, uiWindow* window, void* blarg)
{
    DlgVideoSettings::Open();
}

void OnOpenAudioSettings(uiMenuItem* item, uiWindow* window, void* blarg)
{
    DlgAudioSettings::Open();
}

void OnOpenWifiSettings(uiMenuItem* item, uiWindow* window, void* blarg)
{
    DlgWifiSettings::Open();
}


void OnSetSavestateSRAMReloc(uiMenuItem* item, uiWindow* window, void* param)
{
    Config::SavestateRelocSRAM = uiMenuItemChecked(item) ? 1:0;
}


void EnsureProperMinSize()
{
    bool isHori = (ScreenRotation == 1 || ScreenRotation == 3);

    int w0 = 256;
    int h0 = 192;
    int w1 = 256;
    int h1 = 192;

    if (ScreenLayout == 0) // natural
    {
        if (isHori)
            SetMinSize(h0+ScreenGap+h1, std::max(w0,w1));
        else
            SetMinSize(std::max(w0,w1), h0+ScreenGap+h1);
    }
    else if (ScreenLayout == 1) // vertical
    {
        if (isHori)
            SetMinSize(std::max(h0,h1), w0+ScreenGap+w1);
        else
            SetMinSize(std::max(w0,w1), h0+ScreenGap+h1);
    }
    else // horizontal
    {
        if (isHori)
            SetMinSize(h0+ScreenGap+h1, std::max(w0,w1));
        else
            SetMinSize(w0+ScreenGap+w1, std::max(h0,h1));
    }
}

void OnSetScreenSize(uiMenuItem* item, uiWindow* window, void* param)
{
    int factor = *(int*)param;
    bool isHori = (ScreenRotation == 1 || ScreenRotation == 3);

    int w = 256*factor;
    int h = 192*factor;

    // FIXME

    if (ScreenLayout == 0) // natural
    {
        if (isHori)
            uiWindowSetContentSize(window, (h*2)+ScreenGap, w);
        else
            uiWindowSetContentSize(window, w, (h*2)+ScreenGap);
    }
    else if (ScreenLayout == 1) // vertical
    {
        if (isHori)
            uiWindowSetContentSize(window, h, (w*2)+ScreenGap);
        else
            uiWindowSetContentSize(window, w, (h*2)+ScreenGap);
    }
    else // horizontal
    {
        if (isHori)
            uiWindowSetContentSize(window, (h*2)+ScreenGap, w);
        else
            uiWindowSetContentSize(window, (w*2)+ScreenGap, h);
    }
}

void OnSetScreenRotation(uiMenuItem* item, uiWindow* window, void* param)
{
    int rot = *(int*)param;

    int oldrot = ScreenRotation;
    ScreenRotation = rot;

    int w, h;
    uiWindowContentSize(window, &w, &h);

    bool isHori = (rot == 1 || rot == 3);
    bool wasHori = (oldrot == 1 || oldrot == 3);

    EnsureProperMinSize();

    if (ScreenLayout == 0) // natural
    {
        if (isHori ^ wasHori)
        {
            int blarg = h;
            h = w;
            w = blarg;

            uiWindowSetContentSize(window, w, h);
        }
    }

    SetupScreenRects(w, h);

    for (int i = 0; i < 4; i++)
        uiMenuItemSetChecked(MenuItem_ScreenRot[i], i==ScreenRotation);
}

void OnSetScreenGap(uiMenuItem* item, uiWindow* window, void* param)
{
    int gap = *(int*)param;

    //int oldgap = ScreenGap;
    ScreenGap = gap;

    EnsureProperMinSize();
    SetupScreenRects(WindowWidth, WindowHeight);

    for (int i = 0; i < 6; i++)
        uiMenuItemSetChecked(MenuItem_ScreenGap[i], kScreenGap[i]==ScreenGap);
}

void OnSetScreenLayout(uiMenuItem* item, uiWindow* window, void* param)
{
    int layout = *(int*)param;
    ScreenLayout = layout;

    EnsureProperMinSize();
    SetupScreenRects(WindowWidth, WindowHeight);

    for (int i = 0; i < 3; i++)
        uiMenuItemSetChecked(MenuItem_ScreenLayout[i], i==ScreenLayout);
}

void OnSetScreenSizing(uiMenuItem* item, uiWindow* window, void* param)
{
    int sizing = *(int*)param;
    ScreenSizing = sizing;

    SetupScreenRects(WindowWidth, WindowHeight);

    for (int i = 0; i < 4; i++)
        uiMenuItemSetChecked(MenuItem_ScreenSizing[i], i==ScreenSizing);
}

void OnSetScreenFiltering(uiMenuItem* item, uiWindow* window, void* blarg)
{
    int chk = uiMenuItemChecked(item);
    if (chk != 0) Config::ScreenFilter = 1;
    else          Config::ScreenFilter = 0;
}

void OnSetLimitFPS(uiMenuItem* item, uiWindow* window, void* blarg)
{
    int chk = uiMenuItemChecked(item);
    if (chk != 0) Config::LimitFPS = true;
    else          Config::LimitFPS = false;
}

void OnSetAudioSync(uiMenuItem* item, uiWindow* window, void* blarg)
{
    int chk = uiMenuItemChecked(item);
    if (chk != 0) Config::AudioSync = true;
    else          Config::AudioSync = false;
}

void OnSetShowOSD(uiMenuItem* item, uiWindow* window, void* blarg)
{
    int chk = uiMenuItemChecked(item);
    if (chk != 0) Config::ShowOSD = true;
    else          Config::ShowOSD = false;
}

void ApplyNewSettings(int type)
{
#ifdef JIT_ENABLED
    if (type == 4)
    {
        Reset(NULL);
        return;
    }
#endif

    if (!RunningSomething)
    {
        if (type == 1) return;
    }

    int prevstatus = EmuRunning;
    EmuRunning = 3;
    while (EmuStatus != 3);

    if (type == 0) // 3D renderer settings
    {
        if (Screen_UseGL) uiGLMakeContextCurrent(GLContext);
        GPU3D::UpdateRendererConfig();
        if (Screen_UseGL) uiGLMakeContextCurrent(NULL);

        GL_3DScale = Config::GL_ScaleFactor; // dorp
        GL_ScreenSizeDirty = true;
    }
    else if (type == 1) // wifi settings
    {
        if (Wifi::MPInited)
        {
            Platform::MP_DeInit();
            Platform::MP_Init();
        }

        Platform::LAN_DeInit();
        Platform::LAN_Init();
    }
    else if (type == 2) // video output method
    {
        bool usegl = Config::ScreenUseGL || (Config::_3DRenderer != 0);
        if (usegl != Screen_UseGL)
        {
            if (Screen_UseGL) uiGLMakeContextCurrent(GLContext);
            GPU3D::DeInitRenderer();
            OSD::DeInit(Screen_UseGL);
            if (Screen_UseGL) uiGLMakeContextCurrent(NULL);

            Screen_UseGL = usegl;
            RecreateMainWindow(usegl);

            if (Screen_UseGL) uiGLMakeContextCurrent(GLContext);
            GPU3D::InitRenderer(Screen_UseGL);
            if (Screen_UseGL) uiGLMakeContextCurrent(NULL);
        }
    }
    else if (type == 3) // 3D renderer
    {
        if (Screen_UseGL) uiGLMakeContextCurrent(GLContext);
        GPU3D::DeInitRenderer();
        GPU3D::InitRenderer(Screen_UseGL);
        if (Screen_UseGL) uiGLMakeContextCurrent(NULL);
    }
    EmuRunning = prevstatus;
}


void CreateMainWindowMenu()
{
    uiMenu* menu;
    uiMenuItem* menuitem;

    menu = uiNewMenu("File");
    menuitem = uiMenuAppendItem(menu, "Open ROM...");
    uiMenuItemOnClicked(menuitem, OnOpenFile, NULL);
    uiMenuAppendSeparator(menu);
    {
        uiMenu* submenu = uiNewMenu("Save state");

        for (int i = 0; i < 9; i++)
        {
            char name[32];
            if (i < 8)
                sprintf(name, "%d\tShift+F%d", kSavestateNum[i], kSavestateNum[i]);
            else
                strcpy(name, "File...\tShift+F9");

            uiMenuItem* ssitem = uiMenuAppendItem(submenu, name);
            uiMenuItemOnClicked(ssitem, OnSaveState, (void*)&kSavestateNum[i]);

            MenuItem_SaveStateSlot[i] = ssitem;
        }

        MenuItem_SaveState = uiMenuAppendSubmenu(menu, submenu);
    }
    {
        uiMenu* submenu = uiNewMenu("Load state");

        for (int i = 0; i < 9; i++)
        {
            char name[32];
            if (i < 8)
                sprintf(name, "%d\tF%d", kSavestateNum[i], kSavestateNum[i]);
            else
                strcpy(name, "File...\tF9");

            uiMenuItem* ssitem = uiMenuAppendItem(submenu, name);
            uiMenuItemOnClicked(ssitem, OnLoadState, (void*)&kSavestateNum[i]);

            MenuItem_LoadStateSlot[i] = ssitem;
        }

        MenuItem_LoadState = uiMenuAppendSubmenu(menu, submenu);
    }
    menuitem = uiMenuAppendItem(menu, "Undo state load\tF12");
    uiMenuItemOnClicked(menuitem, OnUndoStateLoad, NULL);
    MenuItem_UndoStateLoad = menuitem;
    uiMenuAppendSeparator(menu);
    menuitem = uiMenuAppendItem(menu, "Quit");
    uiMenuItemOnClicked(menuitem, OnCloseByMenu, NULL);

    menu = uiNewMenu("System");
    menuitem = uiMenuAppendItem(menu, "Run");
    uiMenuItemOnClicked(menuitem, OnRun, NULL);
    menuitem = uiMenuAppendCheckItem(menu, "Pause");
    uiMenuItemOnClicked(menuitem, OnPause, NULL);
    MenuItem_Pause = menuitem;
    uiMenuAppendSeparator(menu);
    menuitem = uiMenuAppendItem(menu, "Reset");
    uiMenuItemOnClicked(menuitem, OnReset, NULL);
    MenuItem_Reset = menuitem;
    menuitem = uiMenuAppendItem(menu, "Stop");
    uiMenuItemOnClicked(menuitem, OnStop, NULL);
    MenuItem_Stop = menuitem;

    menu = uiNewMenu("Config");
    {
        menuitem = uiMenuAppendItem(menu, "Emu settings");
        uiMenuItemOnClicked(menuitem, OnOpenEmuSettings, NULL);
        menuitem = uiMenuAppendItem(menu, "Input config");
        uiMenuItemOnClicked(menuitem, OnOpenInputConfig, NULL);
        menuitem = uiMenuAppendItem(menu, "Hotkey config");
        uiMenuItemOnClicked(menuitem, OnOpenHotkeyConfig, NULL);
        menuitem = uiMenuAppendItem(menu, "Video settings");
        uiMenuItemOnClicked(menuitem, OnOpenVideoSettings, NULL);
        menuitem = uiMenuAppendItem(menu, "Audio settings");
        uiMenuItemOnClicked(menuitem, OnOpenAudioSettings, NULL);
        menuitem = uiMenuAppendItem(menu, "Wifi settings");
        uiMenuItemOnClicked(menuitem, OnOpenWifiSettings, NULL);
    }
    uiMenuAppendSeparator(menu);
    {
        uiMenu* submenu = uiNewMenu("Savestate settings");

        MenuItem_SavestateSRAMReloc = uiMenuAppendCheckItem(submenu, "Separate savefiles");
        uiMenuItemOnClicked(MenuItem_SavestateSRAMReloc, OnSetSavestateSRAMReloc, NULL);

        uiMenuAppendSubmenu(menu, submenu);
    }
    uiMenuAppendSeparator(menu);
    {
        uiMenu* submenu = uiNewMenu("Screen size");

        for (int i = 0; i < 4; i++)
        {
            char name[32];
            sprintf(name, "%dx", kScreenSize[i]);
            uiMenuItem* item = uiMenuAppendItem(submenu, name);
            uiMenuItemOnClicked(item, OnSetScreenSize, (void*)&kScreenSize[i]);
        }

        uiMenuAppendSubmenu(menu, submenu);
    }
    {
        uiMenu* submenu = uiNewMenu("Screen rotation");

        for (int i = 0; i < 4; i++)
        {
            char name[32];
            sprintf(name, "%d", kScreenRot[i]*90);
            MenuItem_ScreenRot[i] = uiMenuAppendCheckItem(submenu, name);
            uiMenuItemOnClicked(MenuItem_ScreenRot[i], OnSetScreenRotation, (void*)&kScreenRot[i]);
        }

        uiMenuAppendSubmenu(menu, submenu);
    }
    {
        uiMenu* submenu = uiNewMenu("Mid-screen gap");

        //for (int i = 0; kScreenGap[i] != -1; i++)
        for (int i = 0; i < 6; i++)
        {
            char name[32];
            sprintf(name, "%d pixels", kScreenGap[i]);
            MenuItem_ScreenGap[i] = uiMenuAppendCheckItem(submenu, name);
            uiMenuItemOnClicked(MenuItem_ScreenGap[i], OnSetScreenGap, (void*)&kScreenGap[i]);
        }

        uiMenuAppendSubmenu(menu, submenu);
    }
    {
        uiMenu* submenu = uiNewMenu("Screen layout");

        MenuItem_ScreenLayout[0] = uiMenuAppendCheckItem(submenu, "Natural");
        uiMenuItemOnClicked(MenuItem_ScreenLayout[0], OnSetScreenLayout, (void*)&kScreenLayout[0]);
        MenuItem_ScreenLayout[1] = uiMenuAppendCheckItem(submenu, "Vertical");
        uiMenuItemOnClicked(MenuItem_ScreenLayout[1], OnSetScreenLayout, (void*)&kScreenLayout[1]);
        MenuItem_ScreenLayout[2] = uiMenuAppendCheckItem(submenu, "Horizontal");
        uiMenuItemOnClicked(MenuItem_ScreenLayout[2], OnSetScreenLayout, (void*)&kScreenLayout[2]);

        uiMenuAppendSubmenu(menu, submenu);
    }
    {
        uiMenu* submenu = uiNewMenu("Screen sizing");

        MenuItem_ScreenSizing[0] = uiMenuAppendCheckItem(submenu, "Even");
        uiMenuItemOnClicked(MenuItem_ScreenSizing[0], OnSetScreenSizing, (void*)&kScreenSizing[0]);
        MenuItem_ScreenSizing[1] = uiMenuAppendCheckItem(submenu, "Emphasize top");
        uiMenuItemOnClicked(MenuItem_ScreenSizing[1], OnSetScreenSizing, (void*)&kScreenSizing[1]);
        MenuItem_ScreenSizing[2] = uiMenuAppendCheckItem(submenu, "Emphasize bottom");
        uiMenuItemOnClicked(MenuItem_ScreenSizing[2], OnSetScreenSizing, (void*)&kScreenSizing[2]);
        MenuItem_ScreenSizing[3] = uiMenuAppendCheckItem(submenu, "Auto");
        uiMenuItemOnClicked(MenuItem_ScreenSizing[3], OnSetScreenSizing, (void*)&kScreenSizing[3]);

        uiMenuAppendSubmenu(menu, submenu);
    }

    MenuItem_ScreenFilter = uiMenuAppendCheckItem(menu, "Screen filtering");
    uiMenuItemOnClicked(MenuItem_ScreenFilter, OnSetScreenFiltering, NULL);

    MenuItem_ShowOSD = uiMenuAppendCheckItem(menu, "Show OSD");
    uiMenuItemOnClicked(MenuItem_ShowOSD, OnSetShowOSD, NULL);

    uiMenuAppendSeparator(menu);

    MenuItem_LimitFPS = uiMenuAppendCheckItem(menu, "Limit framerate");
    uiMenuItemOnClicked(MenuItem_LimitFPS, OnSetLimitFPS, NULL);

    MenuItem_AudioSync = uiMenuAppendCheckItem(menu, "Audio sync");
    uiMenuItemOnClicked(MenuItem_AudioSync, OnSetAudioSync, NULL);
}

void CreateMainWindow(bool opengl)
{
    MainWindow = uiNewWindow("melonDS " MELONDS_VERSION,
                             WindowWidth, WindowHeight,
                             Config::WindowMaximized, 1, 1);
    uiWindowOnClosing(MainWindow, OnCloseWindow, NULL);

    uiWindowSetDropTarget(MainWindow, 1);
    uiWindowOnDropFile(MainWindow, OnDropFile, NULL);

    uiWindowOnGetFocus(MainWindow, OnGetFocus, NULL);
    uiWindowOnLoseFocus(MainWindow, OnLoseFocus, NULL);

    ScreenDrawInited = false;
    bool opengl_good = opengl;

    if (!opengl) MainDrawArea = uiNewArea(&MainDrawAreaHandler);
    else         MainDrawArea = uiNewGLArea(&MainDrawAreaHandler, kGLVersions);

    uiWindowSetChild(MainWindow, uiControl(MainDrawArea));
    uiControlSetMinSize(uiControl(MainDrawArea), 256, 384);
    uiAreaSetBackgroundColor(MainDrawArea, 0, 0, 0);

    uiControlShow(uiControl(MainWindow));
    uiControlSetFocus(uiControl(MainDrawArea));

    if (opengl_good)
    {
        GLContext = uiAreaGetGLContext(MainDrawArea);
        if (!GLContext) opengl_good = false;
    }
    if (opengl_good)
    {
        uiGLMakeContextCurrent(GLContext);
        uiGLSetVSync(Config::ScreenVSync);
        if (!GLScreen_Init()) opengl_good = false;
        if (opengl_good)
        {
            OpenGL_UseShaderProgram(GL_ScreenShaderOSD);
            OSD::Init(true);
        }
        uiGLMakeContextCurrent(NULL);
    }

    if (opengl && !opengl_good)
    {
        printf("OpenGL: initialization failed\n");
        RecreateMainWindow(false);
        Screen_UseGL = false;
    }

    if (!opengl) OSD::Init(false);
}

void DestroyMainWindow()
{
    uiControlDestroy(uiControl(MainWindow));

    if (ScreenBitmap[0]) uiDrawFreeBitmap(ScreenBitmap[0]);
    if (ScreenBitmap[1]) uiDrawFreeBitmap(ScreenBitmap[1]);

    ScreenBitmap[0] = NULL;
    ScreenBitmap[1] = NULL;
}

void RecreateMainWindow(bool opengl)
{
    int winX, winY, maxi;
    uiWindowPosition(MainWindow, &winX, &winY);
    maxi = uiWindowMaximized(MainWindow);
    DestroyMainWindow();
    CreateMainWindow(opengl);
    uiWindowSetPosition(MainWindow, winX, winY);
    uiWindowSetMaximized(MainWindow, maxi);
}


int main(int argc, char** argv)
{
    srand(time(NULL));

    printf("melonDS " MELONDS_VERSION "\n");
    printf(MELONDS_URL "\n");

#if defined(__WIN32__) || defined(UNIX_PORTABLE)
    if (argc > 0 && strlen(argv[0]) > 0)
    {
        int len = strlen(argv[0]);
        while (len > 0)
        {
            if (argv[0][len] == '/') break;
            if (argv[0][len] == '\\') break;
            len--;
        }
        if (len > 0)
        {
            EmuDirectory = new char[len+1];
            strncpy(EmuDirectory, argv[0], len);
            EmuDirectory[len] = '\0';
        }
        else
        {
            EmuDirectory = new char[2];
            strcpy(EmuDirectory, ".");
        }
    }
    else
    {
        EmuDirectory = new char[2];
        strcpy(EmuDirectory, ".");
    }
#else
	const char* confdir = g_get_user_config_dir();
	const char* confname = "/melonDS";
	EmuDirectory = new char[strlen(confdir) + strlen(confname) + 1];
	strcat(EmuDirectory, confdir);
	strcat(EmuDirectory, confname);
#endif

    // http://stackoverflow.com/questions/14543333/joystick-wont-work-using-sdl
    SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");

    if (SDL_Init(SDL_INIT_HAPTIC) < 0)
    {
        printf("SDL couldn't init rumble\n");
    }
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0)
    {
        printf("SDL shat itself :(\n");
        return 1;
    }

    SDL_JoystickEventState(SDL_ENABLE);

    uiInitOptions ui_opt;
    memset(&ui_opt, 0, sizeof(uiInitOptions));
    const char* ui_err = uiInit(&ui_opt);
    if (ui_err != NULL)
    {
        printf("libui shat itself :( %s\n", ui_err);
        uiFreeInitError(ui_err);
        return 1;
    }

    Config::Load();

    if      (Config::AudioVolume < 0)   Config::AudioVolume = 0;
    else if (Config::AudioVolume > 256) Config::AudioVolume = 256;

    if (!Platform::LocalFileExists("bios7.bin") ||
        !Platform::LocalFileExists("bios9.bin") ||
        !Platform::LocalFileExists("firmware.bin"))
    {
#if defined(__WIN32__) || defined(UNIX_PORTABLE)
		const char* locationName = "the directory you run melonDS from";
#else
		char* locationName = EmuDirectory;
#endif
		char msgboxtext[512];
		sprintf(msgboxtext,
            "One or more of the following required files don't exist or couldn't be accessed:\n\n"
            "bios7.bin -- ARM7 BIOS\n"
            "bios9.bin -- ARM9 BIOS\n"
            "firmware.bin -- firmware image\n\n"
            "Dump the files from your DS and place them in %s.\n"
            "Make sure that the files can be accessed.",
			locationName
		);

        uiMsgBoxError(NULL, "BIOS/Firmware not found", msgboxtext);

        uiUninit();
        SDL_Quit();
        return 0;
    }
    if (!Platform::LocalFileExists("firmware.bin.bak"))
    {
        // verify the firmware
        //
        // there are dumps of an old hacked firmware floating around on the internet
        // and those are problematic
        // the hack predates WFC, and, due to this, any game that alters the WFC
        // access point data will brick that firmware due to it having critical
        // data in the same area. it has the same problem on hardware.
        //
        // but this should help stop users from reporting that issue over and over
        // again, when the issue is not from melonDS but from their firmware dump.
        //
        // I don't know about all the firmware hacks in existence, but the one I
        // looked at has 0x180 bytes from the header repeated at 0x3FC80, but
        // bytes 0x0C-0x14 are different.

        FILE* f = Platform::OpenLocalFile("firmware.bin", "rb");
        u8 chk1[0x180], chk2[0x180];

        fseek(f, 0, SEEK_SET);
        fread(chk1, 1, 0x180, f);
        fseek(f, -0x380, SEEK_END);
        fread(chk2, 1, 0x180, f);

        memset(&chk1[0x0C], 0, 8);
        memset(&chk2[0x0C], 0, 8);

        fclose(f);

        if (!memcmp(chk1, chk2, 0x180))
        {
            uiMsgBoxError(NULL,
                          "Problematic firmware dump",
                          "You are using an old hacked firmware dump.\n"
                          "Firmware boot will stop working if you run any game that alters WFC settings.\n\n"
                          "Note that the issue is not from melonDS, it would also happen on an actual DS.");
        }
    }
    {
        const char* romlist_missing = "Save memory type detection will not work correctly.\n\n"
            "You should use the latest version of romlist.bin (provided in melonDS release packages).";
#if !defined(UNIX_PORTABLE) && !defined(__WIN32__)
        std::string missingstr = std::string(romlist_missing) +
            "\n\nThe ROM list should be placed in " + g_get_user_data_dir() + "/melonds/, otherwise "
            "melonDS will search for it in the current working directory.";
        const char* romlist_missing_text = missingstr.c_str();
#else
        const char* romlist_missing_text = romlist_missing;
#endif

        FILE* f = Platform::OpenDataFile("romlist.bin");
        if (f)
        {
            u32 data;
            fread(&data, 4, 1, f);
            fclose(f);

            if ((data >> 24) == 0) // old CRC-based list
            {
                uiMsgBoxError(NULL, "Your version of romlist.bin is outdated.", romlist_missing_text);
            }
        }
        else
        {
        	uiMsgBoxError(NULL, "romlist.bin not found.", romlist_missing_text);
        }
    }

    CreateMainWindowMenu();

    MainDrawAreaHandler.Draw = OnAreaDraw;
    MainDrawAreaHandler.MouseEvent = OnAreaMouseEvent;
    MainDrawAreaHandler.MouseCrossed = OnAreaMouseCrossed;
    MainDrawAreaHandler.DragBroken = OnAreaDragBroken;
    MainDrawAreaHandler.KeyEvent = OnAreaKeyEvent;
    MainDrawAreaHandler.Resize = OnAreaResize;

    WindowWidth = Config::WindowWidth;
    WindowHeight = Config::WindowHeight;

    Screen_UseGL = Config::ScreenUseGL || (Config::_3DRenderer != 0);

    GL_3DScale = Config::GL_ScaleFactor;
    if      (GL_3DScale < 1) GL_3DScale = 1;
    else if (GL_3DScale > 8) GL_3DScale = 8;

    CreateMainWindow(Screen_UseGL);

    ScreenRotation = Config::ScreenRotation;
    ScreenGap = Config::ScreenGap;
    ScreenLayout = Config::ScreenLayout;
    ScreenSizing = Config::ScreenSizing;

#define SANITIZE(var, min, max)  if ((var < min) || (var > max)) var = 0;
    SANITIZE(ScreenRotation, 0, 3);
    SANITIZE(ScreenLayout, 0, 2);
    SANITIZE(ScreenSizing, 0, 3);
#undef SANITIZE

    for (int i = 0; i < 9; i++) uiMenuItemDisable(MenuItem_SaveStateSlot[i]);
    for (int i = 0; i < 9; i++) uiMenuItemDisable(MenuItem_LoadStateSlot[i]);
    uiMenuItemDisable(MenuItem_UndoStateLoad);

    uiMenuItemDisable(MenuItem_Pause);
    uiMenuItemDisable(MenuItem_Reset);
    uiMenuItemDisable(MenuItem_Stop);

    uiMenuItemSetChecked(MenuItem_SavestateSRAMReloc, Config::SavestateRelocSRAM?1:0);

    uiMenuItemSetChecked(MenuItem_ScreenRot[ScreenRotation], 1);
    uiMenuItemSetChecked(MenuItem_ScreenLayout[ScreenLayout], 1);
    uiMenuItemSetChecked(MenuItem_ScreenSizing[ScreenSizing], 1);

    for (int i = 0; i < 6; i++)
    {
        if (ScreenGap == kScreenGap[i])
            uiMenuItemSetChecked(MenuItem_ScreenGap[i], 1);
    }

    OnSetScreenRotation(MenuItem_ScreenRot[ScreenRotation], MainWindow, (void*)&kScreenRot[ScreenRotation]);

    uiMenuItemSetChecked(MenuItem_ScreenFilter, Config::ScreenFilter==1);
    uiMenuItemSetChecked(MenuItem_LimitFPS, Config::LimitFPS==1);
    uiMenuItemSetChecked(MenuItem_AudioSync, Config::AudioSync==1);
    uiMenuItemSetChecked(MenuItem_ShowOSD, Config::ShowOSD==1);

#ifdef MELONCAP
    MelonCap::Init();
#endif // MELONCAP

    AudioSync = SDL_CreateCond();
    AudioSyncLock = SDL_CreateMutex();

    AudioFreq = 48000; // TODO: make configurable?
    SDL_AudioSpec whatIwant, whatIget;
    memset(&whatIwant, 0, sizeof(SDL_AudioSpec));
    whatIwant.freq = AudioFreq;
    whatIwant.format = AUDIO_S16LSB;
    whatIwant.channels = 2;
    whatIwant.samples = 1024;
    whatIwant.callback = AudioCallback;
    AudioDevice = SDL_OpenAudioDevice(NULL, 0, &whatIwant, &whatIget, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
    if (!AudioDevice)
    {
        printf("Audio init failed: %s\n", SDL_GetError());
    }
    else
    {
        AudioFreq = whatIget.freq;
        printf("Audio output frequency: %d Hz\n", AudioFreq);
        SDL_PauseAudioDevice(AudioDevice, 1);
    }

    memset(&whatIwant, 0, sizeof(SDL_AudioSpec));
    whatIwant.freq = 44100;
    whatIwant.format = AUDIO_S16LSB;
    whatIwant.channels = 1;
    whatIwant.samples = 1024;
    whatIwant.callback = MicCallback;
    MicDevice = SDL_OpenAudioDevice(NULL, 1, &whatIwant, &whatIget, 0);
    if (!MicDevice)
    {
        printf("Mic init failed: %s\n", SDL_GetError());
        MicBufferLength = 0;
    }
    else
    {
        SDL_PauseAudioDevice(MicDevice, 1);
    }

    memset(MicBuffer, 0, sizeof(MicBuffer));
    MicBufferReadPos = 0;
    MicBufferWritePos = 0;

    MicWavBuffer = NULL;
    if (Config::MicInputType == 3) MicLoadWav(Config::MicWavPath);

    JoystickID = Config::JoystickID;
    Joystick = NULL;
    OpenJoystick();

    EmuRunning = 2;
    RunningSomething = false;
    EmuThread = SDL_CreateThread(EmuThreadFunc, "melonDS magic", NULL);

    if (argc > 1)
    {
        char* file = argv[1];
        char* ext = &file[strlen(file)-3];

        if (!strcasecmp(ext, "nds") || !strcasecmp(ext, "srl"))
        {
            strncpy(ROMPath[0], file, 1023);
            ROMPath[0][1023] = '\0';

            SetupSRAMPath(0);

            if (NDS::LoadROM(ROMPath[0], SRAMPath[0], Config::DirectBoot))
                Run();
        }

        if (argc > 2)
        {
            file = argv[2];
            ext = &file[strlen(file)-3];

            if (!strcasecmp(ext, "gba"))
            {
                strncpy(ROMPath[1], file, 1023);
                ROMPath[1][1023] = '\0';

                SetupSRAMPath(1);

                NDS::LoadGBAROM(ROMPath[1], SRAMPath[1]);
            }
        }
    }

    uiMain();

    if (Joystick) SDL_JoystickClose(Joystick);
    if (AudioDevice) SDL_CloseAudioDevice(AudioDevice);
    if (MicDevice)   SDL_CloseAudioDevice(MicDevice);

    SDL_DestroyCond(AudioSync);
    SDL_DestroyMutex(AudioSyncLock);

    if (MicWavBuffer) delete[] MicWavBuffer;

#ifdef MELONCAP
    MelonCap::DeInit();
#endif // MELONCAP

    if (ScreenBitmap[0]) uiDrawFreeBitmap(ScreenBitmap[0]);
    if (ScreenBitmap[1]) uiDrawFreeBitmap(ScreenBitmap[1]);

    Config::ScreenRotation = ScreenRotation;
    Config::ScreenGap = ScreenGap;
    Config::ScreenLayout = ScreenLayout;
    Config::ScreenSizing = ScreenSizing;

    Config::Save();

    uiUninit();
    SDL_Quit();
    delete[] EmuDirectory;
    return 0;
}

#ifdef __WIN32__

#include <windows.h>

int CALLBACK WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int cmdshow)
{
    int argc = 0;
    wchar_t** argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
    char* nullarg = "";

    char** argv = new char*[argc];
    for (int i = 0; i < argc; i++)
    {
        int len = WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1, NULL, 0, NULL, NULL);
        if (len < 1) return NULL;
        argv[i] = new char[len];
        int res = WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1, argv[i], len, NULL, NULL);
        if (res != len) { delete[] argv[i]; argv[i] = nullarg; }
    }

    if (AttachConsole(ATTACH_PARENT_PROCESS))
    {
        freopen("CONOUT$", "w", stdout);
        freopen("CONOUT$", "w", stderr);
        printf("\n");
    }

    int ret = main(argc, argv);

    printf("\n\n>");

    for (int i = 0; i < argc; i++) if (argv[i] != nullarg) delete[] argv[i];
    delete[] argv;

    return ret;
}

#endif