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
|
/* Copyright (C) 2006-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#ifdef _WIN32
#include <process.h>
#endif
#include "libslirp.h"
#include "sockets.h"
#include "android/android.h"
#include "qemu-common.h"
#include "sysemu.h"
#include "console.h"
#include <SDL.h>
#include <SDL_syswm.h>
#include "math.h"
#include "android/charmap.h"
#include "modem_driver.h"
#include "shaper.h"
#include "proxy_http.h"
#include "android/utils/debug.h"
#include "android/resource.h"
#include "android/config.h"
#include "android/config/config.h"
#include "android/skin/image.h"
#include "android/skin/trackball.h"
#include "android/skin/keyboard.h"
#include "android/skin/file.h"
#include "android/skin/window.h"
#include "android/skin/keyset.h"
#include "android/gps.h"
#include "android/qemud.h"
#include "android/hw-kmsg.h"
#include "android/hw-control.h"
#include "android/user-config.h"
#include "android/utils/bufprint.h"
#include "android/utils/dirscanner.h"
#include "android/utils/path.h"
#include "android/utils/timezone.h"
#include "android/utils/display.h"
#include "android/cmdline-option.h"
#include "android/help.h"
#include "hw/goldfish_nand.h"
#include "android/globals.h"
#include "tcpdump.h"
/* in vl.c */
extern void qemu_help(int code);
#include "framebuffer.h"
AndroidRotation android_framebuffer_rotation;
#define STRINGIFY(x) _STRINGIFY(x)
#define _STRINGIFY(x) #x
#define VERSION_STRING STRINGIFY(ANDROID_VERSION_MAJOR)"."STRINGIFY(ANDROID_VERSION_MINOR)
#define KEYSET_FILE "default.keyset"
SkinKeyset* android_keyset;
#define D(...) do { if (VERBOSE_CHECK(init)) dprint(__VA_ARGS__); } while (0)
extern int control_console_start( int port ); /* in control.c */
extern int qemu_milli_needed;
/* the default device DPI if none is specified by the skin
*/
#define DEFAULT_DEVICE_DPI 165
static const AKeyCharmap* android_charmap;
int android_base_port;
#if 0
static int opts->flashkeys; /* forward */
#endif
static void handle_key_command( void* opaque, SkinKeyCommand command, int param );
#ifdef CONFIG_TRACE
extern void start_tracing(void);
extern void stop_tracing(void);
#endif
unsigned long android_verbose;
int qemu_cpu_delay = 0;
int qemu_cpu_delay_count;
/***********************************************************************/
/***********************************************************************/
/***** *****/
/***** U T I L I T Y R O U T I N E S *****/
/***** *****/
/***********************************************************************/
/***********************************************************************/
/*** APPLICATION DIRECTORY
*** Where are we ?
***/
const char* get_app_dir(void)
{
char buffer[1024];
char* p = buffer;
char* end = p + sizeof(buffer);
p = bufprint_app_dir(p, end);
if (p >= end)
return NULL;
return strdup(buffer);
}
/*** CONFIGURATION
***/
static AUserConfig* userConfig;
void
emulator_config_init( void )
{
userConfig = auserConfig_new( android_avdInfo );
}
/* only call this function on normal exits, so that ^C doesn't save the configuration */
void
emulator_config_done( void )
{
int win_x, win_y;
if (!userConfig) {
D("no user configuration?");
return;
}
SDL_WM_GetPos( &win_x, &win_y );
auserConfig_setWindowPos(userConfig, win_x, win_y);
auserConfig_save(userConfig);
}
void *loadpng(const char *fn, unsigned *_width, unsigned *_height);
void *readpng(const unsigned char* base, size_t size, unsigned *_width, unsigned *_height);
#ifdef CONFIG_DARWIN
# define ANDROID_ICON_PNG "android_icon_256.png"
#else
# define ANDROID_ICON_PNG "android_icon_16.png"
#endif
static void
sdl_set_window_icon( void )
{
static int window_icon_set;
if (!window_icon_set)
{
#ifdef _WIN32
HANDLE handle = GetModuleHandle( NULL );
HICON icon = LoadIcon( handle, MAKEINTRESOURCE(1) );
SDL_SysWMinfo wminfo;
SDL_GetWMInfo(&wminfo);
SetClassLong( wminfo.window, GCL_HICON, (LONG)icon );
#else /* !_WIN32 */
unsigned icon_w, icon_h;
size_t icon_bytes;
const unsigned char* icon_data;
void* icon_pixels;
window_icon_set = 1;
icon_data = android_icon_find( ANDROID_ICON_PNG, &icon_bytes );
if ( !icon_data )
return;
icon_pixels = readpng( icon_data, icon_bytes, &icon_w, &icon_h );
if ( !icon_pixels )
return;
/* the data is loaded into memory as RGBA bytes by libpng. we want to manage
* the values as 32-bit ARGB pixels, so swap the bytes accordingly depending
* on our CPU endianess
*/
{
unsigned* d = icon_pixels;
unsigned* d_end = d + icon_w*icon_h;
for ( ; d < d_end; d++ ) {
unsigned pix = d[0];
#if WORDS_BIGENDIAN
/* R,G,B,A read as RGBA => ARGB */
pix = ((pix >> 8) & 0xffffff) | (pix << 24);
#else
/* R,G,B,A read as ABGR => ARGB */
pix = (pix & 0xff00ff00) | ((pix >> 16) & 0xff) | ((pix & 0xff) << 16);
#endif
d[0] = pix;
}
}
SDL_Surface* icon = sdl_surface_from_argb32( icon_pixels, icon_w, icon_h );
if (icon != NULL) {
SDL_WM_SetIcon(icon, NULL);
SDL_FreeSurface(icon);
free( icon_pixels );
}
#endif /* !_WIN32 */
}
}
/***********************************************************************/
/***********************************************************************/
/***** *****/
/***** S K I N I M A G E S *****/
/***** *****/
/***********************************************************************/
/***********************************************************************/
void send_key_event(unsigned code, unsigned down)
{
if(code == 0) {
return;
}
if (VERBOSE_CHECK(keys))
printf(">> KEY [0x%03x,%s]\n", (code & 0x1ff), down ? "down" : " up " );
kbd_put_keycode((code & 0x1ff) | (down ? 0x200 : 0));
}
typedef struct {
AConfig* aconfig;
SkinFile* layout_file;
SkinLayout* layout;
SkinKeyboard* keyboard;
SkinWindow* window;
int win_x;
int win_y;
int show_trackball;
SkinTrackBall* trackball;
int lcd_brightness;
SkinImage* onion;
SkinRotation onion_rotation;
int onion_alpha;
AndroidOptions opts[1]; /* copy of options */
} QEmulator;
static QEmulator qemulator[1];
static void
qemulator_done( QEmulator* emulator )
{
if (emulator->window) {
skin_window_free(emulator->window);
emulator->window = NULL;
}
if (emulator->trackball) {
skin_trackball_destroy(emulator->trackball);
emulator->trackball = NULL;
}
if (emulator->keyboard) {
skin_keyboard_free(emulator->keyboard);
emulator->keyboard = NULL;
}
emulator->layout = NULL;
if (emulator->layout_file) {
skin_file_free(emulator->layout_file);
emulator->layout_file = NULL;
}
}
static void
qemulator_setup( QEmulator* emulator );
static void
qemulator_fb_update( void* _emulator, int x, int y, int w, int h )
{
QEmulator* emulator = _emulator;
if (emulator->window)
skin_window_update_display( emulator->window, x, y, w, h );
}
static void
qemulator_fb_rotate( void* _emulator, int rotation )
{
QEmulator* emulator = _emulator;
qemulator_setup( emulator );
}
static int
qemulator_init( QEmulator* emulator,
AConfig* aconfig,
const char* basepath,
int x,
int y,
AndroidOptions* opts )
{
emulator->aconfig = aconfig;
emulator->layout_file = skin_file_create_from_aconfig(aconfig, basepath);
emulator->layout = emulator->layout_file->layouts;
emulator->keyboard = skin_keyboard_create_from_aconfig(aconfig, opts->raw_keys);
emulator->window = NULL;
emulator->win_x = x;
emulator->win_y = y;
emulator->opts[0] = opts[0];
/* register as a framebuffer clients for all displays defined in the skin file */
SKIN_FILE_LOOP_PARTS( emulator->layout_file, part )
SkinDisplay* disp = part->display;
if (disp->valid) {
qframebuffer_add_client( disp->qfbuff,
emulator,
qemulator_fb_update,
qemulator_fb_rotate,
NULL );
}
SKIN_FILE_LOOP_END_PARTS
return 0;
}
static AndroidKeyCode
qemulator_rotate_keycode( QEmulator* emulator,
AndroidKeyCode sym )
{
switch (skin_layout_get_dpad_rotation(emulator->layout)) {
case SKIN_ROTATION_90:
switch (sym) {
case kKeyCodeDpadLeft: sym = kKeyCodeDpadDown; break;
case kKeyCodeDpadRight: sym = kKeyCodeDpadUp; break;
case kKeyCodeDpadUp: sym = kKeyCodeDpadLeft; break;
case kKeyCodeDpadDown: sym = kKeyCodeDpadRight; break;
default: ;
}
break;
case SKIN_ROTATION_180:
switch (sym) {
case kKeyCodeDpadLeft: sym = kKeyCodeDpadRight; break;
case kKeyCodeDpadRight: sym = kKeyCodeDpadLeft; break;
case kKeyCodeDpadUp: sym = kKeyCodeDpadDown; break;
case kKeyCodeDpadDown: sym = kKeyCodeDpadUp; break;
default: ;
}
break;
case SKIN_ROTATION_270:
switch (sym) {
case kKeyCodeDpadLeft: sym = kKeyCodeDpadUp; break;
case kKeyCodeDpadRight: sym = kKeyCodeDpadDown; break;
case kKeyCodeDpadUp: sym = kKeyCodeDpadRight; break;
case kKeyCodeDpadDown: sym = kKeyCodeDpadLeft; break;
default: ;
}
break;
default: ;
}
return sym;
}
static int
get_device_dpi( AndroidOptions* opts )
{
int dpi_device = DEFAULT_DEVICE_DPI;
if (opts->dpi_device != NULL) {
char* end;
dpi_device = strtol( opts->dpi_device, &end, 0 );
if (end == NULL || *end != 0 || dpi_device <= 0) {
fprintf(stderr, "argument for -dpi-device must be a positive integer. Aborting\n" );
exit(1);
}
}
return dpi_device;
}
static double
get_default_scale( AndroidOptions* opts )
{
int dpi_device = get_device_dpi( opts );
int dpi_monitor = -1;
double scale = 0.0;
/* possible values for the 'scale' option are
* 'auto' : try to determine the scale automatically
* '<number>dpi' : indicates the host monitor dpi, compute scale accordingly
* '<fraction>' : use direct scale coefficient
*/
if (opts->scale) {
if (!strcmp(opts->scale, "auto"))
{
/* we need to get the host dpi resolution ? */
int xdpi, ydpi;
if ( get_monitor_resolution( &xdpi, &ydpi ) < 0 ) {
fprintf(stderr, "could not get monitor DPI resolution from system. please use -dpi-monitor to specify one\n" );
exit(1);
}
D( "system reported monitor resolutions: xdpi=%d ydpi=%d\n", xdpi, ydpi);
dpi_monitor = (xdpi + ydpi+1)/2;
}
else
{
char* end;
scale = strtod( opts->scale, &end );
if (end && end[0] == 'd' && end[1] == 'p' && end[2] == 'i' && end[3] == 0) {
if ( scale < 20 || scale > 1000 ) {
fprintf(stderr, "emulator: ignoring bad -scale argument '%s': %s\n", opts->scale,
"host dpi number must be between 20 and 1000" );
exit(1);
}
dpi_monitor = scale;
scale = 0.0;
}
else if (end == NULL || *end != 0) {
fprintf(stderr, "emulator: ignoring bad -scale argument '%s': %s\n", opts->scale,
"not a number or the 'auto' keyword" );
exit(1);
}
else if ( scale < 0.1 || scale > 3. ) {
fprintf(stderr, "emulator: ignoring bad -window-scale argument '%s': %s\n", opts->scale,
"must be between 0.1 and 3.0" );
exit(1);
}
}
}
if (scale == 0.0 && dpi_monitor > 0)
scale = dpi_monitor*1.0/dpi_device;
if (scale == 0.0)
scale = 1.0;
return scale;
}
void
android_emulator_set_window_scale( double scale, int is_dpi )
{
QEmulator* emulator = qemulator;
if (is_dpi)
scale /= get_device_dpi( emulator->opts );
if (emulator->window)
skin_window_set_scale( emulator->window, scale );
}
static void
qemulator_set_title( QEmulator* emulator )
{
char temp[64];
if (emulator->window == NULL)
return;
snprintf( temp, sizeof(temp), "Android Emulator (%s:%d)",
avdInfo_getName( android_avdInfo ),
android_base_port );
skin_window_set_title( emulator->window, temp );
}
/* called by the emulated framebuffer device each time the content of the
* framebuffer has changed. the rectangle is the bounding box of all changes
*/
static void
sdl_update(DisplayState *ds, int x, int y, int w, int h)
{
/* this function is being called from the console code that is currently inactive
** simple totally ignore it...
*/
(void)ds;
(void)x;
(void)y;
(void)w;
(void)h;
}
static void
qemulator_light_brightness( void* opaque, const char* light, int value )
{
QEmulator* emulator = opaque;
VERBOSE_PRINT(hw_control,"%s: light='%s' value=%d window=%p", __FUNCTION__, light, value, emulator->window);
if ( !strcmp(light, "lcd_backlight") ) {
emulator->lcd_brightness = value;
if (emulator->window)
skin_window_set_lcd_brightness( emulator->window, value );
return;
}
}
static void
qemulator_setup( QEmulator* emulator )
{
AndroidOptions* opts = emulator->opts;
if ( !emulator->window && !opts->no_window ) {
SkinLayout* layout = emulator->layout;
double scale = get_default_scale(emulator->opts);
emulator->window = skin_window_create( layout, emulator->win_x, emulator->win_y, scale, 0);
if (emulator->window == NULL)
return;
{
SkinTrackBall* ball;
SkinTrackBallParameters params;
params.diameter = 30;
params.ring = 2;
params.ball_color = 0xffe0e0e0;
params.dot_color = 0xff202020;
params.ring_color = 0xff000000;
ball = skin_trackball_create( ¶ms );
emulator->trackball = ball;
skin_window_set_trackball( emulator->window, ball );
emulator->lcd_brightness = 128; /* 50% */
skin_window_set_lcd_brightness( emulator->window, emulator->lcd_brightness );
}
if ( emulator->onion != NULL )
skin_window_set_onion( emulator->window,
emulator->onion,
emulator->onion_rotation,
emulator->onion_alpha );
qemulator_set_title( emulator );
skin_window_enable_touch ( emulator->window, android_hw->hw_touchScreen != 0 );
skin_window_enable_dpad ( emulator->window, android_hw->hw_dPad != 0 );
skin_window_enable_qwerty( emulator->window, android_hw->hw_keyboard != 0 );
skin_window_enable_trackball( emulator->window, android_hw->hw_trackBall != 0 );
}
/* initialize hardware control support */
{
AndroidHwControlFuncs funcs;
funcs.light_brightness = qemulator_light_brightness;
android_hw_control_init( emulator, &funcs );
}
}
/* called by the emulated framebuffer device each time the framebuffer
* is resized or rotated */
static void
sdl_resize(DisplayState *ds, int w, int h)
{
fprintf(stderr, "weird, sdl_resize being called with framebuffer interface\n");
exit(1);
}
static void sdl_refresh(DisplayState *ds)
{
QEmulator* emulator = ds->opaque;
SDL_Event ev;
SkinWindow* window = emulator->window;
SkinKeyboard* keyboard = emulator->keyboard;
/* this will eventually call sdl_update if the content of the VGA framebuffer
* has changed */
qframebuffer_check_updates();
if (window == NULL)
return;
while(SDL_PollEvent(&ev)){
switch(ev.type){
case SDL_VIDEOEXPOSE:
skin_window_redraw( window, NULL );
break;
case SDL_KEYDOWN:
#ifdef _WIN32
/* special code to deal with Alt-F4 properly */
if (ev.key.keysym.sym == SDLK_F4 &&
ev.key.keysym.mod & KMOD_ALT) {
goto CleanExit;
}
#endif
#ifdef __APPLE__
/* special code to deal with Command-Q properly */
if (ev.key.keysym.sym == SDLK_q &&
ev.key.keysym.mod & KMOD_META) {
goto CleanExit;
}
#endif
skin_keyboard_process_event( keyboard, &ev, 1 );
break;
case SDL_KEYUP:
skin_keyboard_process_event( keyboard, &ev, 0 );
break;
case SDL_MOUSEMOTION:
skin_window_process_event( window, &ev );
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
int down = (ev.type == SDL_MOUSEBUTTONDOWN);
if (ev.button.button == 4)
{
/* scroll-wheel simulates DPad up */
AndroidKeyCode kcode;
kcode = qemulator_rotate_keycode(emulator, kKeyCodeDpadUp);
send_key_event( kcode, down );
}
else if (ev.button.button == 5)
{
/* scroll-wheel simulates DPad down */
AndroidKeyCode kcode;
kcode = qemulator_rotate_keycode(emulator, kKeyCodeDpadDown);
send_key_event( kcode, down );
}
else if (ev.button.button == SDL_BUTTON_LEFT) {
skin_window_process_event( window, &ev );
}
#if 0
else {
fprintf(stderr, "... mouse button %s: button=%d state=%04x x=%d y=%d\n",
down ? "down" : "up ",
ev.button.button, ev.button.state, ev.button.x, ev.button.y);
}
#endif
}
break;
case SDL_QUIT:
#if defined _WIN32 || defined __APPLE__
CleanExit:
#endif
/* only save emulator config through clean exit */
qemulator_done( emulator );
qemu_system_shutdown_request();
return;
}
}
skin_keyboard_flush( keyboard );
}
/* used to respond to a given keyboard command shortcut
*/
static void
handle_key_command( void* opaque, SkinKeyCommand command, int down )
{
static const struct { SkinKeyCommand cmd; AndroidKeyCode kcode; } keycodes[] =
{
{ SKIN_KEY_COMMAND_BUTTON_CALL, kKeyCodeCall },
{ SKIN_KEY_COMMAND_BUTTON_HOME, kKeyCodeHome },
{ SKIN_KEY_COMMAND_BUTTON_BACK, kKeyCodeBack },
{ SKIN_KEY_COMMAND_BUTTON_HANGUP, kKeyCodeEndCall },
{ SKIN_KEY_COMMAND_BUTTON_POWER, kKeyCodePower },
{ SKIN_KEY_COMMAND_BUTTON_SEARCH, kKeyCodeSearch },
{ SKIN_KEY_COMMAND_BUTTON_MENU, kKeyCodeMenu },
{ SKIN_KEY_COMMAND_BUTTON_DPAD_UP, kKeyCodeDpadUp },
{ SKIN_KEY_COMMAND_BUTTON_DPAD_LEFT, kKeyCodeDpadLeft },
{ SKIN_KEY_COMMAND_BUTTON_DPAD_RIGHT, kKeyCodeDpadRight },
{ SKIN_KEY_COMMAND_BUTTON_DPAD_DOWN, kKeyCodeDpadDown },
{ SKIN_KEY_COMMAND_BUTTON_DPAD_CENTER, kKeyCodeDpadCenter },
{ SKIN_KEY_COMMAND_BUTTON_VOLUME_UP, kKeyCodeVolumeUp },
{ SKIN_KEY_COMMAND_BUTTON_VOLUME_DOWN, kKeyCodeVolumeDown },
{ SKIN_KEY_COMMAND_BUTTON_CAMERA, kKeyCodeCamera },
{ SKIN_KEY_COMMAND_NONE, 0 }
};
int nn;
#ifdef CONFIG_TRACE
static int tracing = 0;
#endif
QEmulator* emulator = opaque;
for (nn = 0; keycodes[nn].kcode != 0; nn++) {
if (command == keycodes[nn].cmd) {
unsigned code = keycodes[nn].kcode;
if (down)
code |= 0x200;
kbd_put_keycode( code );
return;
}
}
// for the show-trackball command, handle down events to enable, and
// up events to disable
if (command == SKIN_KEY_COMMAND_SHOW_TRACKBALL) {
emulator->show_trackball = (down != 0);
skin_window_show_trackball( emulator->window, emulator->show_trackball );
//qemulator_set_title( emulator );
return;
}
// only handle down events for the rest
if (down == 0)
return;
switch (command)
{
case SKIN_KEY_COMMAND_TOGGLE_NETWORK:
{
qemu_net_disable = !qemu_net_disable;
if (android_modem) {
amodem_set_data_registration(
android_modem,
qemu_net_disable ? A_REGISTRATION_UNREGISTERED
: A_REGISTRATION_HOME);
}
D( "network is now %s", qemu_net_disable ? "disconnected" : "connected" );
}
break;
case SKIN_KEY_COMMAND_TOGGLE_FULLSCREEN:
if (emulator->window) {
skin_window_toggle_fullscreen(emulator->window);
}
break;
case SKIN_KEY_COMMAND_TOGGLE_TRACING:
{
#ifdef CONFIG_TRACE
tracing = !tracing;
if (tracing)
start_tracing();
else
stop_tracing();
#endif
}
break;
case SKIN_KEY_COMMAND_TOGGLE_TRACKBALL:
emulator->show_trackball = !emulator->show_trackball;
skin_window_show_trackball( emulator->window, emulator->show_trackball );
break;
case SKIN_KEY_COMMAND_ONION_ALPHA_UP:
case SKIN_KEY_COMMAND_ONION_ALPHA_DOWN:
if (emulator->onion)
{
int alpha = emulator->onion_alpha;
if (command == SKIN_KEY_COMMAND_ONION_ALPHA_UP)
alpha += 16;
else
alpha -= 16;
if (alpha > 256)
alpha = 256;
else if (alpha < 0)
alpha = 0;
emulator->onion_alpha = alpha;
skin_window_set_onion( emulator->window, emulator->onion, emulator->onion_rotation, alpha );
skin_window_redraw( emulator->window, NULL );
//dprint( "onion alpha set to %d (%.f %%)", alpha, alpha/2.56 );
}
break;
case SKIN_KEY_COMMAND_CHANGE_LAYOUT_PREV:
case SKIN_KEY_COMMAND_CHANGE_LAYOUT_NEXT:
{
SkinLayout* layout = NULL;
if (command == SKIN_KEY_COMMAND_CHANGE_LAYOUT_NEXT) {
layout = emulator->layout->next;
if (layout == NULL)
layout = emulator->layout_file->layouts;
}
else if (command == SKIN_KEY_COMMAND_CHANGE_LAYOUT_PREV) {
layout = emulator->layout_file->layouts;
while (layout->next && layout->next != emulator->layout)
layout = layout->next;
}
if (layout != NULL) {
SkinRotation rotation;
emulator->layout = layout;
skin_window_reset( emulator->window, layout );
rotation = skin_layout_get_dpad_rotation( layout );
if (emulator->keyboard)
skin_keyboard_set_rotation( emulator->keyboard, rotation );
if (emulator->trackball) {
skin_trackball_set_rotation( emulator->trackball, rotation );
skin_window_set_trackball( emulator->window, emulator->trackball );
skin_window_show_trackball( emulator->window, emulator->show_trackball );
}
skin_window_set_lcd_brightness( emulator->window, emulator->lcd_brightness );
qframebuffer_invalidate_all();
qframebuffer_check_updates();
}
}
break;
default:
/* XXX: TODO ? */
;
}
}
static void sdl_at_exit(void)
{
emulator_config_done();
qemulator_done( qemulator );
SDL_Quit();
}
void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
{
QEmulator* emulator = qemulator;
SkinDisplay* disp = skin_layout_get_display(emulator->layout);
// fprintf(stderr,"*** sdl_display_init ***\n");
ds->opaque = emulator;
if (disp->rotation & 1) {
ds->width = disp->rect.size.h;
ds->height = disp->rect.size.w;
} else {
ds->width = disp->rect.size.w;
ds->height = disp->rect.size.h;
}
ds->dpy_update = sdl_update;
ds->dpy_resize = sdl_resize;
ds->dpy_refresh = sdl_refresh;
skin_keyboard_enable( emulator->keyboard, 1 );
skin_keyboard_on_command( emulator->keyboard, handle_key_command, emulator );
}
extern SkinKeyboard* android_emulator_get_keyboard(void)
{
return qemulator->keyboard;
}
static const char* skin_network_speed = NULL;
static const char* skin_network_delay = NULL;
/* list of skin aliases */
static const struct {
const char* name;
const char* alias;
} skin_aliases[] = {
{ "QVGA-L", "320x240" },
{ "QVGA-P", "240x320" },
{ "HVGA-L", "480x320" },
{ "HVGA-P", "320x480" },
{ "QVGA", "320x240" },
{ "HVGA", "320x480" },
{ NULL, NULL }
};
/* this is used by hw/events_device.c to send the charmap name to the system */
const char* android_skin_keycharmap = NULL;
void init_skinned_ui(const char *path, const char *name, AndroidOptions* opts)
{
char tmp[1024];
AConfig* root;
AConfig* n;
int win_x, win_y, flags;
signal(SIGINT, SIG_DFL);
#ifndef _WIN32
signal(SIGQUIT, SIG_DFL);
#endif
/* we're not a game, so allow the screensaver to run */
putenv("SDL_VIDEO_ALLOW_SCREENSAVER=1");
flags = SDL_INIT_NOPARACHUTE;
if (!opts->no_window)
flags |= SDL_INIT_VIDEO;
if(SDL_Init(flags)){
fprintf(stderr, "SDL init failure, reason is: %s\n", SDL_GetError() );
exit(1);
}
if (!opts->no_window) {
SDL_EnableUNICODE(!opts->raw_keys);
SDL_EnableKeyRepeat(0,0);
sdl_set_window_icon();
}
else
{
#ifndef _WIN32
/* prevent SIGTTIN and SIGTTOUT from stopping us. this is necessary to be
* able to run the emulator in the background (e.g. "emulator &").
* despite the fact that the emulator should not grab input or try to
* write to the output in normal cases, we're stopped on some systems
* (e.g. OS X)
*/
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
#endif
}
atexit(sdl_at_exit);
root = aconfig_node("", "");
if(name) {
/* Support skin aliases like QVGA-H QVGA-P, etc...
But first we check if it's a directory that exist before applying
the alias */
int checkAlias = 1;
if (path != NULL) {
bufprint(tmp, tmp+sizeof(tmp), "%s/%s", path, name);
if (path_exists(tmp)) {
checkAlias = 0;
} else {
D("there is no '%s' skin in '%s'", name, path);
}
}
if (checkAlias) {
int nn;
for (nn = 0; ; nn++ ) {
const char* skin_name = skin_aliases[nn].name;
const char* skin_alias = skin_aliases[nn].alias;
if ( !skin_name )
break;
if ( !strcasecmp( skin_name, name ) ) {
D("skin name '%s' aliased to '%s'", name, skin_alias);
name = skin_alias;
break;
}
}
}
/* Magically support skins like "320x240" */
if(isdigit(name[0])) {
char *x = strchr(name, 'x');
if(x && isdigit(x[1])) {
int width = atoi(name);
int height = atoi(x + 1);
sprintf(tmp,"display {\n width %d\n height %d\n}\n",
width, height);
aconfig_load(root, strdup(tmp));
path = ":";
goto found_a_skin;
}
}
if (path == NULL) {
derror("unknown skin name '%s'", name);
exit(1);
}
sprintf(tmp, "%s/%s/layout", path, name);
D("trying to load skin file '%s'", tmp);
if(aconfig_load_file(root, tmp) >= 0) {
sprintf(tmp, "%s/%s/", path, name);
path = tmp;
goto found_a_skin;
} else {
dwarning("could not load skin file '%s', using built-in one\n",
tmp);
}
}
{
const unsigned char* layout_base;
size_t layout_size;
name = "<builtin>";
layout_base = android_resource_find( "layout", &layout_size );
if (layout_base != NULL) {
char* base = malloc( layout_size+1 );
memcpy( base, layout_base, layout_size );
base[layout_size] = 0;
D("parsing built-in skin layout file (size=%d)", (int)layout_size);
aconfig_load(root, base);
path = ":";
} else {
fprintf(stderr, "Couldn't load builtin skin\n");
exit(1);
}
}
found_a_skin:
{
win_x = 10;
win_y = 10;
if (userConfig)
auserConfig_getWindowPos(userConfig, &win_x, &win_y);
}
if ( qemulator_init( qemulator, root, path, win_x, win_y, opts ) < 0 ) {
fprintf(stderr, "### Error: could not load emulator skin '%s'\n", name);
exit(1);
}
android_skin_keycharmap = skin_keyboard_charmap_name(qemulator->keyboard);
/* the default network speed and latency can now be specified by the device skin */
n = aconfig_find(root, "network");
if (n != NULL) {
skin_network_speed = aconfig_str(n, "speed", 0);
skin_network_delay = aconfig_str(n, "delay", 0);
}
#if 0
/* create a trackball if needed */
n = aconfig_find(root, "trackball");
if (n != NULL) {
SkinTrackBallParameters params;
params.x = aconfig_unsigned(n, "x", 0);
params.y = aconfig_unsigned(n, "y", 0);
params.diameter = aconfig_unsigned(n, "diameter", 20);
params.ring = aconfig_unsigned(n, "ring", 1);
params.ball_color = aconfig_unsigned(n, "ball-color", 0xffe0e0e0);
params.dot_color = aconfig_unsigned(n, "dot-color", 0xff202020 );
params.ring_color = aconfig_unsigned(n, "ring-color", 0xff000000 );
qemu_disp->trackball = skin_trackball_create( ¶ms );
skin_trackball_refresh( qemu_disp->trackball );
}
#endif
/* add an onion overlay image if needed */
if (opts->onion) {
SkinImage* onion = skin_image_find_simple( opts->onion );
int alpha, rotate;
if ( opts->onion_alpha && 1 == sscanf( opts->onion_alpha, "%d", &alpha ) ) {
alpha = (256*alpha)/100;
} else
alpha = 128;
if ( opts->onion_rotation && 1 == sscanf( opts->onion_rotation, "%d", &rotate ) ) {
rotate &= 3;
} else
rotate = SKIN_ROTATION_0;
qemulator->onion = onion;
qemulator->onion_alpha = alpha;
qemulator->onion_rotation = rotate;
}
}
int qemu_main(int argc, char **argv);
/* this function dumps the QEMU help */
extern void help( void );
extern void emulator_help( void );
#define VERBOSE_OPT(str,var) { str, &var }
#define _VERBOSE_TAG(x,y) { #x, VERBOSE_##x, y },
static const struct { const char* name; int flag; const char* text; }
verbose_options[] = {
VERBOSE_TAG_LIST
{ 0, 0, 0 }
};
int
android_parse_network_speed(const char* speed)
{
int n;
char* end;
double sp;
if (speed == NULL || speed[0] == 0) {
speed = DEFAULT_NETSPEED;
}
for (n = 0; android_netspeeds[n].name != NULL; n++) {
if (!strcmp(android_netspeeds[n].name, speed)) {
qemu_net_download_speed = android_netspeeds[n].download;
qemu_net_upload_speed = android_netspeeds[n].upload;
return 0;
}
}
/* is this a number ? */
sp = strtod(speed, &end);
if (end == speed) {
return -1;
}
qemu_net_download_speed = qemu_net_upload_speed = sp*1000.;
if (*end == ':') {
speed = end+1;
sp = strtod(speed, &end);
if (end > speed) {
qemu_net_download_speed = sp*1000.;
}
}
if (android_modem)
amodem_set_data_network_type( android_modem,
android_parse_network_type(speed) );
return 0;
}
int
android_parse_network_latency(const char* delay)
{
int n;
char* end;
double sp;
if (delay == NULL || delay[0] == 0)
delay = DEFAULT_NETDELAY;
for (n = 0; android_netdelays[n].name != NULL; n++) {
if ( !strcmp( android_netdelays[n].name, delay ) ) {
qemu_net_min_latency = android_netdelays[n].min_ms;
qemu_net_max_latency = android_netdelays[n].max_ms;
return 0;
}
}
/* is this a number ? */
sp = strtod(delay, &end);
if (end == delay) {
return -1;
}
qemu_net_min_latency = qemu_net_max_latency = (int)sp;
if (*end == ':') {
delay = (const char*)end+1;
sp = strtod(delay, &end);
if (end > delay) {
qemu_net_max_latency = (int)sp;
}
}
return 0;
}
static int
load_keyset(const char* path)
{
if (path_can_read(path)) {
AConfig* root = aconfig_node("","");
if (!aconfig_load_file(root, path)) {
android_keyset = skin_keyset_new(root);
if (android_keyset != NULL) {
D( "keyset loaded from: %s", path);
return 0;
}
}
}
return -1;
}
static void
parse_keyset(const char* keyset, AndroidOptions* opts)
{
char kname[MAX_PATH];
char temp[MAX_PATH];
char* p;
char* end;
/* append .keyset suffix if needed */
if (strchr(keyset, '.') == NULL) {
p = kname;
end = p + sizeof(kname);
p = bufprint(p, end, "%s.keyset", keyset);
if (p >= end) {
derror( "keyset name too long: '%s'\n", keyset);
exit(1);
}
keyset = kname;
}
/* look for a the keyset file */
p = temp;
end = p + sizeof(temp);
p = bufprint_config_file(p, end, keyset);
if (p < end && load_keyset(temp) == 0)
return;
p = temp;
p = bufprint(p, end, "%s" PATH_SEP "keysets" PATH_SEP "%s", opts->sysdir, keyset);
if (p < end && load_keyset(temp) == 0)
return;
p = temp;
p = bufprint_app_dir(p, end);
p = bufprint(p, end, PATH_SEP "keysets" PATH_SEP "%s", keyset);
if (p < end && load_keyset(temp) == 0)
return;
return;
}
static void
write_default_keyset( void )
{
char path[MAX_PATH];
bufprint_config_file( path, path+sizeof(path), KEYSET_FILE );
/* only write if there is no file here */
if ( !path_exists(path) ) {
int fd = open( path, O_WRONLY | O_CREAT, 0666 );
int ret;
const char* ks = skin_keyset_get_default();
D( "writing default keyset file to %s", path );
if (fd < 0) {
D( "%s: could not create file: %s", __FUNCTION__, strerror(errno) );
return;
}
CHECKED(ret, write(fd, ks, strlen(ks)));
close(fd);
}
}
#ifdef CONFIG_NAND_LIMITS
static uint64_t
parse_nand_rw_limit( const char* value )
{
char* end;
uint64_t val = strtoul( value, &end, 0 );
if (end == value) {
derror( "bad parameter value '%s': expecting unsigned integer", value );
exit(1);
}
switch (end[0]) {
case 'K': val <<= 10; break;
case 'M': val <<= 20; break;
case 'G': val <<= 30; break;
case 0: break;
default:
derror( "bad read/write limit suffix: use K, M or G" );
exit(1);
}
return val;
}
static void
parse_nand_limits(char* limits)
{
int pid = -1, signal = -1;
int64_t reads = 0, writes = 0;
char* item = limits;
/* parse over comma-separated items */
while (item && *item) {
char* next = strchr(item, ',');
char* end;
if (next == NULL) {
next = item + strlen(item);
} else {
*next++ = 0;
}
if ( !memcmp(item, "pid=", 4) ) {
pid = strtol(item+4, &end, 10);
if (end == NULL || *end) {
derror( "bad parameter, expecting pid=<number>, got '%s'",
item );
exit(1);
}
if (pid <= 0) {
derror( "bad parameter: process identifier must be > 0" );
exit(1);
}
}
else if ( !memcmp(item, "signal=", 7) ) {
signal = strtol(item+7,&end, 10);
if (end == NULL || *end) {
derror( "bad parameter: expecting signal=<number>, got '%s'",
item );
exit(1);
}
if (signal <= 0) {
derror( "bad parameter: signal number must be > 0" );
exit(1);
}
}
else if ( !memcmp(item, "reads=", 6) ) {
reads = parse_nand_rw_limit(item+6);
}
else if ( !memcmp(item, "writes=", 7) ) {
writes = parse_nand_rw_limit(item+7);
}
else {
derror( "bad parameter '%s' (see -help-nand-limits)", item );
exit(1);
}
item = next;
}
if (pid < 0) {
derror( "bad paramater: missing pid=<number>" );
exit(1);
}
else if (signal < 0) {
derror( "bad parameter: missing signal=<number>" );
exit(1);
}
else if (reads == 0 && writes == 0) {
dwarning( "no read or write limit specified. ignoring -nand-limits" );
} else {
nand_threshold* t;
t = &android_nand_read_threshold;
t->pid = pid;
t->signal = signal;
t->counter = 0;
t->limit = reads;
t = &android_nand_write_threshold;
t->pid = pid;
t->signal = signal;
t->counter = 0;
t->limit = writes;
}
}
#endif /* CONFIG_NAND_LIMITS */
void emulator_help( void )
{
STRALLOC_DEFINE(out);
android_help_main(out);
printf( "%.*s", out->n, out->s );
stralloc_reset(out);
exit(1);
}
static int
add_dns_server( const char* server_name )
{
SockAddress addr;
if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
fprintf(stderr,
"### WARNING: can't resolve DNS server name '%s'\n",
server_name );
return -1;
}
D( "DNS server name '%s' resolved to %s", server_name, sock_address_to_string(&addr) );
if ( slirp_add_dns_server( &addr ) < 0 ) {
fprintf(stderr,
"### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
return -1;
}
return 0;
}
enum {
REPORT_CONSOLE_SERVER = (1 << 0),
REPORT_CONSOLE_MAX = (1 << 1)
};
static int
get_report_console_options( char* end, int *maxtries )
{
int flags = 0;
if (end == NULL || *end == 0)
return 0;
if (end[0] != ',') {
derror( "socket port/path can be followed by [,<option>]+ only\n");
exit(3);
}
end += 1;
while (*end) {
char* p = strchr(end, ',');
if (p == NULL)
p = end + strlen(end);
if (memcmp( end, "server", p-end ) == 0)
flags |= REPORT_CONSOLE_SERVER;
else if (memcmp( end, "max=", 4) == 0) {
end += 4;
*maxtries = strtol( end, NULL, 10 );
flags |= REPORT_CONSOLE_MAX;
} else {
derror( "socket port/path can be followed by [,server][,max=<count>] only\n");
exit(3);
}
end = p;
if (*end)
end += 1;
}
return flags;
}
static void
report_console( const char* proto_port, int console_port )
{
int s = -1, s2;
int maxtries = 10;
int flags = 0;
signal_state_t sigstate;
disable_sigalrm( &sigstate );
if ( !strncmp( proto_port, "tcp:", 4) ) {
char* end;
long port = strtol(proto_port + 4, &end, 10);
flags = get_report_console_options( end, &maxtries );
if (flags & REPORT_CONSOLE_SERVER) {
s = socket_loopback_server( port, SOCKET_STREAM );
if (s < 0) {
fprintf(stderr, "could not create server socket on TCP:%ld: %s\n",
port, errno_str);
exit(3);
}
} else {
for ( ; maxtries > 0; maxtries-- ) {
D("trying to find console-report client on tcp:%d", port);
s = socket_loopback_client( port, SOCKET_STREAM );
if (s >= 0)
break;
sleep_ms(1000);
}
if (s < 0) {
fprintf(stderr, "could not connect to server on TCP:%ld: %s\n",
port, errno_str);
exit(3);
}
}
} else if ( !strncmp( proto_port, "unix:", 5) ) {
#ifdef _WIN32
fprintf(stderr, "sorry, the unix: protocol is not supported on Win32\n");
exit(3);
#else
char* path = strdup(proto_port+5);
char* end = strchr(path, ',');
if (end != NULL) {
flags = get_report_console_options( end, &maxtries );
*end = 0;
}
if (flags & REPORT_CONSOLE_SERVER) {
s = socket_unix_server( path, SOCKET_STREAM );
if (s < 0) {
fprintf(stderr, "could not bind unix socket on '%s': %s\n",
proto_port+5, errno_str);
exit(3);
}
} else {
for ( ; maxtries > 0; maxtries-- ) {
s = socket_unix_client( path, SOCKET_STREAM );
if (s >= 0)
break;
sleep_ms(1000);
}
if (s < 0) {
fprintf(stderr, "could not connect to unix socket on '%s': %s\n",
path, errno_str);
exit(3);
}
}
free(path);
#endif
} else {
fprintf(stderr, "-report-console must be followed by a 'tcp:<port>' or 'unix:<path>'\n");
exit(3);
}
if (flags & REPORT_CONSOLE_SERVER) {
int tries = 3;
D( "waiting for console-reporting client" );
do {
s2 = socket_accept(s, NULL);
} while (s2 < 0 && --tries > 0);
if (s2 < 0) {
fprintf(stderr, "could not accept console-reporting client connection: %s\n",
errno_str);
exit(3);
}
socket_close(s);
s = s2;
}
/* simply send the console port in text */
{
char temp[12];
snprintf( temp, sizeof(temp), "%d", console_port );
if (socket_send(s, temp, strlen(temp)) < 0) {
fprintf(stderr, "could not send console number report: %d: %s\n",
errno, errno_str );
exit(3);
}
socket_close(s);
}
D( "console port number sent to remote. resuming boot" );
restore_sigalrm (&sigstate);
}
/* this function is used to perform auto-detection of the
* system directory in the case of a SDK installation.
*
* we want to deal with several historical usages, hence
* the slightly complicated logic.
*
* NOTE: the function returns the path to the directory
* containing 'fileName'. this is *not* the full
* path to 'fileName'.
*/
static char*
_getSdkImagePath( const char* fileName )
{
char temp[MAX_PATH];
char* p = temp;
char* end = p + sizeof(temp);
char* q;
char* app;
static const char* const searchPaths[] = {
"", /* program's directory */
"/lib/images", /* this is for SDK 1.0 */
"/../platforms/android-1.1/images", /* this is for SDK 1.1 */
NULL
};
app = bufprint_app_dir(temp, end);
if (app >= end)
return NULL;
do {
int nn;
/* first search a few well-known paths */
for (nn = 0; searchPaths[nn] != NULL; nn++) {
p = bufprint(app, end, "%s", searchPaths[nn]);
q = bufprint(p, end, "/%s", fileName);
if (q < end && path_exists(temp)) {
*p = 0;
goto FOUND_IT;
}
}
/* hmmm. let's assume that we are in a post-1.1 SDK
* scan ../platforms if it exists
*/
p = bufprint(app, end, "/../platforms");
if (p < end) {
DirScanner* scanner = dirScanner_new(temp);
if (scanner != NULL) {
int found = 0;
const char* subdir;
for (;;) {
subdir = dirScanner_next(scanner);
if (!subdir) break;
q = bufprint(p, end, "/%s/images/%s", subdir, fileName);
if (q >= end || !path_exists(temp))
continue;
found = 1;
p = bufprint(p, end, "/%s/images", subdir);
break;
}
dirScanner_free(scanner);
if (found)
break;
}
}
/* I'm out of ideas */
return NULL;
} while (0);
FOUND_IT:
//D("image auto-detection: %s/%s", temp, fileName);
return qemu_strdup(temp);
}
static char*
_getSdkImage( const char* path, const char* file )
{
char temp[MAX_PATH];
char *p = temp, *end = p + sizeof(temp);
p = bufprint(temp, end, "%s/%s", path, file);
if (p >= end || !path_exists(temp))
return NULL;
return qemu_strdup(temp);
}
static char*
_getSdkSystemImage( const char* path, const char* optionName, const char* file )
{
char* image = _getSdkImage(path, file);
if (image == NULL) {
derror("Your system directory is missing the '%s' image file.\n"
"Please specify one with the '%s <filepath>' option",
file, optionName);
exit(2);
}
return image;
}
static void
_forceAvdImagePath( AvdImageType imageType,
const char* path,
const char* description,
int required )
{
if (path == NULL)
return;
if (required && !path_exists(path)) {
derror("Cannot find %s image file: %s", description, path);
exit(1);
}
android_avdParams->forcePaths[imageType] = path;
}
#ifdef _WIN32
#undef main /* we don't want SDL to define main */
#endif
int main(int argc, char **argv)
{
char tmp[MAX_PATH];
char* tmpend = tmp + sizeof(tmp);
char* args[128];
int n;
char* opt;
int use_sdcard_img = 0;
int serial = 0;
int gps_serial = 0;
int radio_serial = 0;
int qemud_serial = 0;
int shell_serial = 0;
int dns_count = 0;
unsigned cachePartitionSize = 0;
AndroidHwConfig* hw;
//const char *appdir = get_app_dir();
char* android_build_root = NULL;
char* android_build_out = NULL;
AndroidOptions opts[1];
args[0] = argv[0];
if ( android_parse_options( &argc, &argv, opts ) < 0 ) {
exit(1);
}
while (argc-- > 1) {
opt = (++argv)[0];
if(!strcmp(opt, "-qemu")) {
argc--;
argv++;
break;
}
if (!strcmp(opt, "-help")) {
emulator_help();
}
if (!strncmp(opt, "-help-",6)) {
STRALLOC_DEFINE(out);
opt += 6;
if (!strcmp(opt, "all")) {
android_help_all(out);
}
else if (android_help_for_option(opt, out) == 0) {
/* ok */
}
else if (android_help_for_topic(opt, out) == 0) {
/* ok */
}
if (out->n > 0) {
printf("\n%.*s", out->n, out->s);
exit(0);
}
fprintf(stderr, "unknown option: -help-%s\n", opt);
fprintf(stderr, "please use -help for a list of valid topics\n");
exit(1);
}
if (opt[0] == '-') {
fprintf(stderr, "unknown option: %s\n", opt);
fprintf(stderr, "please use -help for a list of valid options\n");
exit(1);
}
fprintf(stderr, "invalid command-line parameter: %s.\n", opt);
fprintf(stderr, "Hint: use '@foo' to launch a virtual device named 'foo'.\n");
fprintf(stderr, "please use -help for more information\n");
exit(1);
}
/* special case, if -qemu -h is used, directly invoke the QEMU-specific help */
if (argc > 0) {
int nn;
for (nn = 0; nn < argc; nn++)
if (!strcmp(argv[nn], "-h")) {
qemu_help(0);
break;
}
}
android_charmap = android_charmaps[0];
if (opts->version) {
printf("Android emulator version %s\n"
"Copyright (C) 2006-2008 The Android Open Source Project and many others.\n"
"This program is a derivative of the QEMU CPU emulator (www.qemu.org).\n\n",
#if defined ANDROID_BUILD_ID
VERSION_STRING " (build_id " STRINGIFY(ANDROID_BUILD_ID) ")" );
#else
VERSION_STRING);
#endif
printf(" This software is licensed under the terms of the GNU General Public\n"
" License version 2, as published by the Free Software Foundation, and\n"
" may be copied, distributed, and modified under those terms.\n\n"
" This program is distributed in the hope that it will be useful,\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" GNU General Public License for more details.\n\n");
exit(0);
}
if (opts->timezone) {
if ( timezone_set(opts->timezone) < 0 ) {
fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n", opts->timezone);
}
}
/* legacy support: we used to use -system <dir> and -image <file>
* instead of -sysdir <dir> and -system <file>, so handle this by checking
* whether the options point to directories or files.
*/
if (opts->image != NULL) {
if (opts->system != NULL) {
if (opts->sysdir != NULL) {
derror( "You can't use -sysdir, -system and -image at the same time.\n"
"You should probably use '-sysdir <path> -system <file>'.\n" );
exit(2);
}
}
dwarning( "Please note that -image is obsolete and that -system is now used to point\n"
"to the system image. Next time, try using '-sysdir <path> -system <file>' instead.\n" );
opts->sysdir = opts->system;
opts->system = opts->image;
opts->image = NULL;
}
else if (opts->system != NULL && path_is_dir(opts->system)) {
if (opts->sysdir != NULL) {
derror( "Option -system should now be followed by a file path, not a directory one.\n"
"Please use '-sysdir <path>' to point to the system directory.\n" );
exit(1);
}
dwarning( "Please note that the -system option should now be used to point to the initial\n"
"system image (like the obsolete -image option). To point to the system directory\n"
"please now use '-sysdir <path>' instead.\n" );
opts->sysdir = opts->system;
opts->system = NULL;
}
if (opts->nojni)
opts->no_jni = opts->nojni;
if (opts->nocache)
opts->no_cache = opts->nocache;
if (opts->noaudio)
opts->no_audio = opts->noaudio;
if (opts->noskin)
opts->no_skin = opts->noskin;
if (opts->initdata) {
opts->init_data = opts->initdata;
opts->initdata = NULL;
}
/* If no AVD name was given, try to find the top of the
* Android build tree
*/
if (opts->avd == NULL) {
do {
char* out = getenv("ANDROID_PRODUCT_OUT");
if (out == NULL || out[0] == 0)
break;
if (!path_exists(out)) {
derror("Can't access ANDROID_PRODUCT_OUT as '%s\n"
"You need to build the Android system before launching the emulator",
out);
exit(2);
}
android_build_root = path_parent( out, 4 );
if (android_build_root == NULL || !path_exists(android_build_root)) {
derror("Can't find the Android build root from '%s'\n"
"Please check the definition of the ANDROID_PRODUCT_OUT variable.\n"
"It should point to your product-specific build output directory.\n",
out );
exit(2);
}
android_build_out = out;
D( "found Android build root: %s", android_build_root );
D( "found Android build out: %s", android_build_out );
} while (0);
}
/* if no virtual device name is given, and we're not in the
* Android build system, we'll need to perform some auto-detection
* magic :-)
*/
if (opts->avd == NULL && !android_build_out)
{
char dataDirIsSystem = 0;
if (!opts->sysdir) {
opts->sysdir = _getSdkImagePath("system.img");
if (!opts->sysdir) {
derror(
"You did not specify a virtual device name, and the system\n"
"directory could not be found.\n\n"
"If you are an Android SDK user, please use '@<name>' or '-avd <name>'\n"
"to start a given virtual device (see -help-avd for details).\n\n"
"Otherwise, follow the instructions in -help-disk-images to start the emulator\n"
);
exit(2);
}
D("autoconfig: -sysdir %s", opts->sysdir);
}
if (!opts->system) {
opts->system = _getSdkSystemImage(opts->sysdir, "-image", "system.img");
D("autoconfig: -image %s", opts->image);
}
if (!opts->kernel) {
opts->kernel = _getSdkSystemImage(opts->sysdir, "-kernel", "kernel-qemu");
D("autoconfig: -kernel %s", opts->kernel);
}
if (!opts->ramdisk) {
opts->ramdisk = _getSdkSystemImage(opts->sysdir, "-ramdisk", "ramdisk.img");
D("autoconfig: -ramdisk %s", opts->ramdisk);
}
/* if no data directory is specified, use the system directory */
if (!opts->datadir) {
opts->datadir = qemu_strdup(opts->sysdir);
dataDirIsSystem = 1;
D("autoconfig: -datadir %s", opts->sysdir);
}
if (!opts->data) {
/* check for userdata-qemu.img in the data directory */
bufprint(tmp, tmpend, "%s/userdata-qemu.img", opts->datadir);
if (!path_exists(tmp)) {
derror(
"You did not provide the name of an Android Virtual Device\n"
"with the '-avd <name>' option. Read -help-avd for more information.\n\n"
"If you *really* want to *NOT* run an AVD, consider using '-data <file>'\n"
"to specify a data partition image file (I hope you know what you're doing).\n"
);
exit(2);
}
opts->data = qemu_strdup(tmp);
D("autoconfig: -data %s", opts->data);
}
if (!opts->sdcard && opts->datadir) {
bufprint(tmp, tmpend, "%s/sdcard.img", opts->datadir);
if (path_exists(tmp)) {
opts->sdcard = qemu_strdup(tmp);
D("autoconfig: -sdcard %s", opts->sdcard);
}
}
}
/* setup the virtual device parameters from our options
*/
if (opts->no_cache) {
android_avdParams->flags |= AVDINFO_NO_CACHE;
}
if (opts->wipe_data) {
android_avdParams->flags |= AVDINFO_WIPE_DATA | AVDINFO_WIPE_CACHE;
}
/* if certain options are set, we can force the path of
* certain kernel/disk image files
*/
_forceAvdImagePath(AVD_IMAGE_KERNEL, opts->kernel, "kernel", 1);
_forceAvdImagePath(AVD_IMAGE_INITSYSTEM, opts->system, "system", 1);
_forceAvdImagePath(AVD_IMAGE_RAMDISK, opts->ramdisk,"ramdisk", 1);
_forceAvdImagePath(AVD_IMAGE_USERDATA, opts->data, "user data", 0);
_forceAvdImagePath(AVD_IMAGE_CACHE, opts->cache, "cache", 0);
_forceAvdImagePath(AVD_IMAGE_SDCARD, opts->sdcard, "SD Card", 0);
/* we don't accept -skindir without -skin now
* to simplify the autoconfig stuff with virtual devices
*/
if (opts->no_skin) {
opts->skin = "320x480";
opts->skindir = NULL;
}
if (opts->skindir) {
if (!opts->skin) {
derror( "the -skindir <path> option requires a -skin <name> option");
exit(1);
}
}
else {
if (!opts->skin && android_build_out) {
/* select default skin based on product type */
const char* p = strrchr(android_build_out,'/');
if (p) {
if (p[1] == 's') {
opts->skin = "HVGA"; /* used to be QVGA-L */
} else if (p[1] == 'd') {
opts->skin = "HVGA";
}
}
D("autoconfig: -skin %s", opts->skin);
}
android_avdParams->skinName = opts->skin;
}
/* setup the virtual device differently depending on whether
* we are in the Android build system or not
*/
if (opts->avd != NULL)
{
android_avdInfo = avdInfo_new( opts->avd, android_avdParams );
if (android_avdInfo == NULL) {
/* an error message has already been printed */
dprint("could not find virtual device named '%s'", opts->avd);
exit(1);
}
}
else
{
if (!android_build_out) {
android_build_out = android_build_root = opts->sysdir;
}
android_avdInfo = avdInfo_newForAndroidBuild(
android_build_root,
android_build_out,
android_avdParams );
if(android_avdInfo == NULL) {
D("could not start virtual device\n");
exit(1);
}
}
/* get the skin from the virtual device configuration */
opts->skin = (char*) avdInfo_getSkinName( android_avdInfo );
opts->skindir = (char*) avdInfo_getSkinDir( android_avdInfo );
if (opts->skin) {
D("autoconfig: -skin %s", opts->skin);
}
if (opts->skindir) {
D("autoconfig: -skindir %s", opts->skindir);
}
/* Read hardware configuration */
hw = android_hw;
if (avdInfo_getHwConfig(android_avdInfo, hw) < 0) {
derror("could not read hardware configuration ?");
exit(1);
}
#ifdef CONFIG_NAND_LIMITS
if (opts->nand_limits)
parse_nand_limits(opts->nand_limits);
#endif
if (opts->keyset) {
parse_keyset(opts->keyset, opts);
if (!android_keyset) {
fprintf(stderr,
"emulator: WARNING: could not find keyset file named '%s',"
" using defaults instead\n",
opts->keyset);
}
}
if (!android_keyset) {
parse_keyset("default", opts);
if (!android_keyset) {
android_keyset = skin_keyset_new_from_text( skin_keyset_get_default() );
if (!android_keyset) {
fprintf(stderr, "PANIC: default keyset file is corrupted !!\n" );
fprintf(stderr, "PANIC: please update the code in android/skin/keyset.c\n" );
exit(1);
}
if (!opts->keyset)
write_default_keyset();
}
}
/* the purpose of -no-audio is to disable sound output from the emulator,
* not to disable Audio emulation. So simply force the 'none' backends */
if (opts->no_audio)
opts->audio = "none";
if (opts->audio) {
if (opts->audio_in || opts->audio_out) {
derror( "you can't use -audio with -audio-in or -audio-out\n" );
exit(1);
}
if ( !audio_check_backend_name( 0, opts->audio ) ) {
derror( "'%s' is not a valid audio output backend. see -help-audio-out\n",
opts->audio);
exit(1);
}
opts->audio_out = opts->audio;
opts->audio_in = opts->audio;
if ( !audio_check_backend_name( 1, opts->audio ) ) {
fprintf(stderr,
"emulator: warning: '%s' is not a valid audio input backend. audio record disabled\n",
opts->audio);
opts->audio_in = "none";
}
}
if (opts->audio_in) {
static char env[64]; /* note: putenv needs a static unique string buffer */
if ( !audio_check_backend_name( 1, opts->audio_in ) ) {
derror( "'%s' is not a valid audio input backend. see -help-audio-in\n",
opts->audio_in);
exit(1);
}
bufprint( env, env+sizeof(env), "QEMU_AUDIO_IN_DRV=%s", opts->audio_in );
putenv( env );
if (!hw->hw_audioInput) {
dwarning( "Emulated hardware doesn't have audio input.");
}
}
if (opts->audio_out) {
static char env[64]; /* note: putenv needs a static unique string buffer */
if ( !audio_check_backend_name( 0, opts->audio_out ) ) {
derror( "'%s' is not a valid audio output backend. see -help-audio-out\n",
opts->audio_out);
exit(1);
}
bufprint( env, env+sizeof(env), "QEMU_AUDIO_OUT_DRV=%s", opts->audio_out );
putenv( env );
if (!hw->hw_audioOutput) {
dwarning( "Emulated hardware doesn't have audio output");
}
}
if (opts->cpu_delay) {
char* end;
long delay = strtol(opts->cpu_delay, &end, 0);
if (end == NULL || *end || delay < 0 || delay > 1000 ) {
fprintf(stderr, "option -cpu-delay must be an integer between 0 and 1000\n" );
exit(1);
}
if (delay > 0)
delay = (1000-delay);
qemu_cpu_delay = (int) delay;
}
emulator_config_init();
init_skinned_ui(opts->skindir, opts->skin, opts);
if (!opts->netspeed) {
if (skin_network_speed)
D("skin network speed: '%s'", skin_network_speed);
opts->netspeed = (char*)skin_network_speed;
}
if (!opts->netdelay) {
if (skin_network_delay)
D("skin network delay: '%s'", skin_network_delay);
opts->netdelay = (char*)skin_network_delay;
}
if ( android_parse_network_speed(opts->netspeed) < 0 ) {
fprintf(stderr, "invalid -netspeed parameter '%s', see emulator -usage\n", opts->netspeed);
emulator_help();
}
if ( android_parse_network_latency(opts->netdelay) < 0 ) {
fprintf(stderr, "invalid -netdelay parameter '%s', see emulator -usage\n", opts->netdelay);
emulator_help();
}
if (opts->netfast) {
qemu_net_download_speed = 0;
qemu_net_upload_speed = 0;
qemu_net_min_latency = 0;
qemu_net_max_latency = 0;
}
if (opts->trace) {
char* tracePath = avdInfo_getTracePath(android_avdInfo, opts->trace);
int ret;
if (tracePath == NULL) {
derror( "bad -trace parameter" );
exit(1);
}
ret = path_mkdir_if_needed( tracePath, 0755 );
if (ret < 0) {
fprintf(stderr, "could not create directory '%s'\n", tmp);
exit(2);
}
opts->trace = tracePath;
}
if (opts->tcpdump) {
if (qemu_tcpdump_start(opts->tcpdump) < 0) {
dwarning( "could not start packet capture: %s", strerror(errno));
}
}
if (opts->no_cache)
opts->cache = 0;
if (opts->dns_server) {
char* x = strchr(opts->dns_server, ',');
dns_count = 0;
if (x == NULL)
{
if ( add_dns_server( opts->dns_server ) == 0 )
dns_count = 1;
}
else
{
x = strdup(opts->dns_server);
while (*x) {
char* y = strchr(x, ',');
if (y != NULL)
*y = 0;
if (y == NULL || y > x) {
if ( add_dns_server( x ) == 0 )
dns_count += 1;
}
if (y == NULL)
break;
x = y+1;
}
}
if (dns_count == 0)
fprintf( stderr, "### WARNING: will use system default DNS server\n" );
}
if (dns_count == 0)
dns_count = slirp_get_system_dns_servers();
n = 1;
/* generate arguments for the underlying qemu main() */
args[n++] = "-kernel";
args[n++] = (char*) avdInfo_getImageFile(android_avdInfo, AVD_IMAGE_KERNEL);
args[n++] = "-initrd";
args[n++] = (char*) avdInfo_getImageFile(android_avdInfo, AVD_IMAGE_RAMDISK);
{
const char* filetype = "file";
if (avdInfo_isImageReadOnly(android_avdInfo, AVD_IMAGE_INITSYSTEM))
filetype = "initfile";
bufprint(tmp, tmpend,
"system,size=0x4200000,%s=%s", filetype,
avdInfo_getImageFile(android_avdInfo, AVD_IMAGE_INITSYSTEM));
args[n++] = "-nand";
args[n++] = strdup(tmp);
}
bufprint(tmp, tmpend,
"userdata,size=0x4200000,file=%s",
avdInfo_getImageFile(android_avdInfo, AVD_IMAGE_USERDATA));
args[n++] = "-nand";
args[n++] = strdup(tmp);
if (hw->disk_cachePartition) {
opts->cache = (char*) avdInfo_getImageFile(android_avdInfo, AVD_IMAGE_CACHE);
cachePartitionSize = hw->disk_cachePartition_size;
}
else if (opts->cache) {
dwarning( "Emulated hardware doesn't support a cache partition" );
opts->cache = NULL;
opts->no_cache = 1;
}
if (opts->cache) {
/* use a specific cache file */
sprintf(tmp, "cache,size=0x%0x,file=%s", cachePartitionSize, opts->cache);
args[n++] = "-nand";
args[n++] = strdup(tmp);
}
else if (!opts->no_cache) {
/* create a temporary cache partition file */
sprintf(tmp, "cache,size=0x%0x", cachePartitionSize);
args[n++] = "-nand";
args[n++] = strdup(tmp);
}
if (hw->hw_sdCard != 0)
opts->sdcard = (char*) avdInfo_getImageFile(android_avdInfo, AVD_IMAGE_SDCARD);
else if (opts->sdcard) {
dwarning( "Emulated hardware doesn't support SD Cards" );
opts->sdcard = NULL;
}
if(opts->sdcard) {
uint64_t size;
if (path_get_size(opts->sdcard, &size) == 0) {
/* see if we have an sdcard image. get its size if it exists */
if (size < 8*1024*1024ULL) {
fprintf(stderr, "### WARNING: SD Card files must be at least 8 MB, ignoring '%s'\n", opts->sdcard);
} else {
args[n++] = "-hda";
args[n++] = opts->sdcard;
use_sdcard_img = 1;
}
} else {
D("no SD Card image at '%s'", opts->sdcard);
}
}
if (!opts->logcat || opts->logcat[0] == 0) {
opts->logcat = getenv("ANDROID_LOG_TAGS");
if (opts->logcat && opts->logcat[0] == 0)
opts->logcat = NULL;
}
#if 0
if (opts->console) {
derror( "option -console is obsolete. please use -shell instead" );
exit(1);
}
#endif
/* we always send the kernel messages from ttyS0 to android_kmsg */
{
AndroidKmsgFlags flags = 0;
if (opts->show_kernel)
flags |= ANDROID_KMSG_PRINT_MESSAGES;
android_kmsg_init( flags );
args[n++] = "-serial";
args[n++] = "android-kmsg";
serial++;
}
/* XXXX: TODO: implement -shell and -logcat through qemud instead */
if (!opts->shell_serial) {
#ifdef _WIN32
opts->shell_serial = "con:";
#else
opts->shell_serial = "stdio";
#endif
}
else
opts->shell = 1;
if (opts->shell || opts->logcat) {
args[n++] = "-serial";
args[n++] = opts->shell_serial;
shell_serial = serial++;
}
if (opts->old_system)
{
if (opts->radio) {
args[n++] = "-serial";
args[n++] = opts->radio;
radio_serial = serial++;
}
else {
args[n++] = "-serial";
args[n++] = "android-modem";
radio_serial = serial++;
}
if (opts->gps) {
args[n++] = "-serial";
args[n++] = opts->gps;
gps_serial = serial++;
}
}
else /* !opts->old_system */
{
args[n++] = "-serial";
args[n++] = "android-qemud";
qemud_serial = serial++;
if (opts->radio) {
CharDriverState* cs = qemu_chr_open(opts->radio);
if (cs == NULL) {
derror( "unsupported character device specification: %s\n"
"used -help-char-devices for list of available formats\n", opts->radio );
exit(1);
}
android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
}
else if ( hw->hw_gsmModem != 0 ) {
if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
derror( "could not initialize qemud 'gsm' channel" );
exit(1);
}
}
if (opts->gps) {
CharDriverState* cs = qemu_chr_open(opts->gps);
if (cs == NULL) {
derror( "unsupported character device specification: %s\n"
"used -help-char-devices for list of available formats\n", opts->gps );
exit(1);
}
android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
}
else if ( hw->hw_gps != 0 ) {
if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
derror( "could not initialize qemud 'gps' channel" );
exit(1);
}
}
}
if (opts->memory) {
char* end;
long ramSize = strtol(opts->memory, &end, 0);
if (ramSize < 0 || *end != 0) {
derror( "-memory must be followed by a positive integer" );
exit(1);
}
if (ramSize < 32 || ramSize > 4096) {
derror( "physical memory size must be between 32 and 4096 MB" );
exit(1);
}
}
if (!opts->memory) {
bufprint(tmp, tmpend, "%d", hw->hw_ramSize);
opts->memory = qemu_strdup(tmp);
}
if (opts->trace) {
args[n++] = "-trace";
args[n++] = opts->trace;
args[n++] = "-tracing";
args[n++] = "off";
}
args[n++] = "-append";
if (opts->bootchart) {
char* end;
int timeout = strtol(opts->bootchart, &end, 10);
if (timeout == 0)
opts->bootchart = NULL;
else if (timeout < 0 || timeout > 15*60) {
derror( "timeout specified for -bootchart option is invalid.\n"
"please use integers between 1 and 900\n");
exit(1);
}
}
{
static char params[1024];
char *p = params, *end = p + sizeof(params);
p = bufprint(p, end, "qemu=1 console=ttyS0" );
if (opts->shell || opts->logcat) {
p = bufprint(p, end, " androidboot.console=ttyS%d", shell_serial );
}
if (opts->trace) {
p = bufprint(p, end, " android.tracing=1");
}
if (!opts->no_jni) {
p = bufprint(p, end, " android.checkjni=1");
}
if (opts->no_boot_anim) {
p = bufprint( p, end, " android.bootanim=0" );
}
if (opts->logcat) {
char* q = bufprint(p, end, " androidboot.logcat=%s", opts->logcat);
if (q < end) {
/* replace any space by a comma ! */
{
int nn;
for (nn = 1; p[nn] != 0; nn++)
if (p[nn] == ' ' || p[nn] == '\t')
p[nn] = ',';
p += nn;
}
}
p = q;
}
if (opts->old_system)
{
p = bufprint(p, end, " android.ril=ttyS%d", radio_serial);
if (opts->gps) {
p = bufprint(p, end, " android.gps=ttyS%d", gps_serial);
}
}
else
{
p = bufprint(p, end, " android.qemud=ttyS%d", qemud_serial);
}
if (dns_count > 0) {
p = bufprint(p, end, " android.ndns=%d", dns_count);
}
if (opts->bootchart) {
p = bufprint(p, end, " androidboot.bootchart=%s", opts->bootchart);
}
if (p >= end) {
fprintf(stderr, "### ERROR: kernel parameters too long\n");
exit(1);
}
args[n++] = strdup(params);
}
/* physical memory */
args[n++] = "-m";
args[n++] = opts->memory;
/* on Linux, the 'dynticks' clock sometimes doesn't work
* properly. this results in the UI freezing while emulation
* continues, for several seconds...
*/
#ifdef __linux__
args[n++] = "-clock";
args[n++] = "unix";
#endif
while(argc-- > 0) {
args[n++] = *argv++;
}
args[n] = 0;
if(VERBOSE_CHECK(init)) {
int i;
for(i = 0; i < n; i++) {
fprintf(stdout, "emulator: argv[%02d] = \"%s\"\n", i, args[i]);
}
}
return qemu_main(n, args);
}
/* this function is called from qemu_main() once all arguments have been parsed
* it should be used to setup any Android-specific items in the emulation before the
* main loop runs
*/
void android_emulation_setup( void )
{
int tries = 16;
int base_port = 5554;
int success = 0;
int s;
uint32_t guest_ip;
AndroidOptions* opts = qemulator->opts;
inet_strtoip("10.0.2.15", &guest_ip);
#if 0
if (opts->adb_port) {
fprintf( stderr, "option -adb-port is obsolete, use -port instead\n" );
exit(1);
}
#endif
if (opts->port && opts->ports) {
fprintf( stderr, "options -port and -ports cannot be used together.\n");
exit(1);
}
if (opts->ports) {
char* comma_location;
char* end;
int console_port = strtol( opts->ports, &comma_location, 0 );
if ( comma_location == NULL || *comma_location != ',' ) {
derror( "option -ports must be followed by two comma separated positive integer numbers" );
exit(1);
}
int adb_port = strtol( comma_location+1, &end, 0 );
if ( end == NULL || *end ) {
derror( "option -ports must be followed by two comma separated positive integer numbers" );
exit(1);
}
if ( console_port == adb_port ) {
derror( "option -ports must be followed by two different integer numbers" );
exit(1);
}
slirp_redir( 0, adb_port, guest_ip, 5555 );
if ( control_console_start( console_port ) < 0 ) {
slirp_unredir( 0, adb_port );
}
base_port = console_port;
} else {
if (opts->port) {
char* end;
int port = strtol( opts->port, &end, 0 );
if ( end == NULL || *end ||
(unsigned)((port - base_port) >> 1) >= (unsigned)tries ) {
derror( "option -port must be followed by an even integer number between %d and %d\n",
base_port, base_port + (tries-1)*2 );
exit(1);
}
if ( (port & 1) != 0 ) {
port &= ~1;
dwarning( "option -port must be followed by an even integer, using port number %d\n",
port );
}
base_port = port;
tries = 1;
}
for ( ; tries > 0; tries--, base_port += 2 ) {
/* setup first redirection for ADB, the Android Debug Bridge */
if ( slirp_redir( 0, base_port+1, guest_ip, 5555 ) < 0 )
continue;
/* setup second redirection for the emulator console */
if ( control_console_start( base_port ) < 0 ) {
slirp_unredir( 0, base_port+1 );
continue;
}
D( "control console listening on port %d, ADB on port %d", base_port, base_port+1 );
success = 1;
break;
}
if (!success) {
fprintf(stderr, "it seems too many emulator instances are running on this machine. Aborting\n" );
exit(1);
}
}
if (opts->report_console) {
report_console(opts->report_console, base_port);
}
android_modem_init( base_port );
android_base_port = base_port;
/* send a simple message to the ADB host server to tell it we just started.
* it should be listening on port 5037. if we can't reach it, don't bother
*/
do
{
SockAddress addr;
char tmp[32];
s = socket_create_inet( SOCKET_STREAM );
if (s < 0) {
D("can't create socket to talk to the ADB server");
break;
}
sock_address_init_inet( &addr, SOCK_ADDRESS_INET_LOOPBACK, 5037 );
if (socket_connect( s, &addr ) < 0) {
D("can't connect to ADB server: %s", errno_str );
break;
}
sprintf(tmp,"0012host:emulator:%d",base_port+1);
socket_send(s, tmp, 18+4);
D("sent '%s' to ADB server", tmp);
}
while (0);
if (s >= 0)
socket_close(s);
/* setup the http proxy, if any */
if (VERBOSE_CHECK(proxy))
proxy_set_verbose(1);
if (!opts->http_proxy) {
opts->http_proxy = getenv("http_proxy");
}
do
{
const char* env = opts->http_proxy;
int envlen;
ProxyOption option_tab[4];
ProxyOption* option = option_tab;
char* p;
char* q;
const char* proxy_name;
int proxy_name_len;
int proxy_port;
if (!env)
break;
envlen = strlen(env);
/* skip the 'http://' header, if present */
if (envlen >= 7 && !memcmp(env, "http://", 7)) {
env += 7;
envlen -= 7;
}
/* do we have a username:password pair ? */
p = strchr(env, '@');
if (p != 0) {
q = strchr(env, ':');
if (q == NULL) {
BadHttpProxyFormat:
dprint("http_proxy format unsupported, try 'proxy:port' or 'username:password@proxy:port'");
break;
}
option->type = PROXY_OPTION_AUTH_USERNAME;
option->string = env;
option->string_len = q - env;
option++;
option->type = PROXY_OPTION_AUTH_PASSWORD;
option->string = q+1;
option->string_len = p - (q+1);
option++;
env = p+1;
}
p = strchr(env,':');
if (p == NULL)
goto BadHttpProxyFormat;
proxy_name = env;
proxy_name_len = p - env;
proxy_port = atoi(p+1);
D( "setting up http proxy: server=%.*s port=%d",
proxy_name_len, proxy_name, proxy_port );
if ( proxy_http_setup( proxy_name, proxy_name_len, proxy_port,
option - option_tab, option_tab ) < 0 )
{
dprint( "http proxy setup failed, check your $http_proxy variable");
}
}
while (0);
/* cool, now try to run the "ddms ping" command, which will take care of pinging usage
* if the user agreed for it. the emulator itself never sends anything to any outside
* machine
*/
{
#ifdef _WIN32
# define _ANDROID_PING_PROGRAM "ddms.bat"
#else
# define _ANDROID_PING_PROGRAM "ddms"
#endif
char tmp[PATH_MAX];
const char* appdir = get_app_dir();
if (snprintf( tmp, PATH_MAX, "%s%s%s", appdir, PATH_SEP,
_ANDROID_PING_PROGRAM ) >= PATH_MAX) {
dprint( "Application directory too long: %s", appdir);
return;
}
/* if the program isn't there, don't bother */
D( "ping program: %s", tmp);
if (path_exists(tmp)) {
#ifdef _WIN32
STARTUPINFO startup;
PROCESS_INFORMATION pinfo;
ZeroMemory( &startup, sizeof(startup) );
startup.cb = sizeof(startup);
startup.dwFlags = STARTF_USESHOWWINDOW;
startup.wShowWindow = SW_SHOWMINIMIZED;
ZeroMemory( &pinfo, sizeof(pinfo) );
char* comspec = getenv("COMSPEC");
if (!comspec) comspec = "cmd.exe";
// Run
char args[PATH_MAX + 30];
if (snprintf( args, PATH_MAX, "/C \"%s\" ping emulator " VERSION_STRING,
tmp) >= PATH_MAX ) {
D( "DDMS path too long: %s", tmp);
return;
}
CreateProcess(
comspec, /* program path */
args, /* command line args */
NULL, /* process handle is not inheritable */
NULL, /* thread handle is not inheritable */
FALSE, /* no, don't inherit any handles */
DETACHED_PROCESS, /* the new process doesn't have a console */
NULL, /* use parent's environment block */
NULL, /* use parent's starting directory */
&startup, /* startup info, i.e. std handles */
&pinfo );
D( "ping command: %s %s", comspec, args );
#else
int pid;
/* disable SIGALRM for the fork(), the periodic signal seems to
* interefere badly with the fork() implementation on Linux running
* under VMWare.
*/
BEGIN_NOSIGALRM
pid = fork();
END_NOSIGALRM
if (pid == 0) {
int fd = open("/dev/null", O_WRONLY);
dup2(fd, 1);
dup2(fd, 2);
execl( tmp, _ANDROID_PING_PROGRAM, "ping", "emulator", VERSION_STRING, NULL );
}
/* don't do anything in the parent or in case of error */
strncat( tmp, " ping emulator " VERSION_STRING, PATH_MAX - strlen(tmp) );
D( "ping command: %s", tmp );
#endif
}
}
}
void android_emulation_teardown( void )
{
}
|