-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid_main.c
2620 lines (2222 loc) · 77.2 KB
/
android_main.c
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
/* 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>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#else
#include <winsock2.h>
#include <process.h>
#endif
#include "libslirp.h"
#include "sockets.h"
#include "android.h"
#include "vl.h"
#include <SDL.h>
#include <SDL_syswm.h>
#include "math.h"
#include "android.h"
#include "android_charmap.h"
#include "modem_driver.h"
#include "shaper.h"
#include "proxy_http.h"
#include "android_utils.h"
#include "android_resource.h"
#include "android_config.h"
#include "skin_image.h"
#include "skin_trackball.h"
#include "skin_keyboard.h"
#include "skin_file.h"
#include "skin_window.h"
#include "skin_keyset.h"
#include "android_timezone.h"
#include "android_gps.h"
#include "android_qemud.h"
#include "android_kmsg.h"
#include "android_option.h"
#include "android_help.h"
#include "hw/goldfish_nand.h"
#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 AConfig* emulator_config;
static int emulator_config_found;
static char emulator_configpath[256];
void
emulator_config_init( void )
{
char* end = emulator_configpath + sizeof(emulator_configpath);
char* p;
void* config;
emulator_config = aconfig_node("","");
p = bufprint_config_file(emulator_configpath, end, "emulator.cfg");
if (p >= end) {
dwarning( "emulator configuration path too long" );
emulator_configpath[0] = 0;
return;
}
config = load_text_file( emulator_configpath );
if (config == NULL)
D( "cannot load emulator configuration at '%s'\n",
emulator_configpath );
else {
aconfig_load( emulator_config, config );
emulator_config_found = 1;
}
}
/* only call this function on normal exits, so that ^C doesn't save the configuration */
void
emulator_config_done( void )
{
int save = 0; /* only save config if we see changes */
AConfig* guid_node;
char guid_value[32];
AConfig* window_node;
int prev_x, prev_y, win_x, win_y;
if (!emulator_configpath[0]) {
D("no config path ?");
return;
}
/* compare window positions */
{
SDL_WM_GetPos( &win_x, &win_y );
prev_x = win_x - 1;
prev_y = win_y - 1;
window_node = aconfig_find( emulator_config, "window" );
if (window_node == NULL) {
aconfig_set( emulator_config, "window", "" );
window_node = aconfig_find( emulator_config, "window" );
save = 1;
}
prev_x = (int)aconfig_unsigned( window_node, "x", 0 );
prev_y = (int)aconfig_unsigned( window_node, "y", 0 );
save = (prev_x != win_x) || (prev_y != win_y);
/* Beware: If the new window position is definitely off-screen,
* we don't want to save it in the configuration file. This can
* happen for example on Linux where certain window managers
* will 'minimize' a window by moving it to coordinates like
* (-5000,-5000)
*/
if ( !SDL_WM_IsFullyVisible(0) ) {
D( "not saving new emulator window position since it is not fully visible" );
save = 0;
}
else if (save)
D( "emulator window position changed and will be saved as (%d, %d)", win_x, win_y );
}
/* If there is no guid node, create one with the current time in
* milliseconds. Thus, the value doesn't correspond to any system or
* user-specific data
*/
#define GUID_NAME "unique-id"
guid_node = aconfig_find( emulator_config, GUID_NAME );
if (!guid_node) {
struct timeval tm;
gettimeofday( &tm, NULL );
sprintf( guid_value, "%lld", (long long)tm.tv_sec*1000 + tm.tv_usec/1000 );
save = 1;
aconfig_set( emulator_config, GUID_NAME, guid_value );
}
if (save) {
char xbuf[16], ybuf[16];
sprintf( xbuf, "%d", win_x );
sprintf( ybuf, "%d", win_y );
aconfig_set( window_node, "x", xbuf );
aconfig_set( window_node, "y", ybuf );
/* do we need to create the $HOME/.android directory ? */
if ( !path_exists(emulator_configpath) ) {
char* dir = path_parent(emulator_configpath, 1);
if (dir == NULL) {
D("invalid user-specific config directory: '%s'", emulator_configpath);
return;
}
if ( path_mkdir_if_needed(dir, 0755) < 0 ) {
D("cannot create directory '%s', configuration lost", dir);
free(dir);
return;
}
free(dir);
}
if ( aconfig_save_file( emulator_config, emulator_configpath ) < 0 ) {
D( "cannot save configuration to %s", emulator_configpath);
} else
D( "configuration saved to %s", emulator_configpath );
}
}
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;
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 (%d)", 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_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 );
}
if ( emulator->onion != NULL )
skin_window_set_onion( emulator->window,
emulator->onion,
emulator->onion_rotation,
emulator->onion_alpha );
qemulator_set_title( emulator );
}
}
/* called by the emulated framebuffer device each time the framebuffer
* is resized or rotated */
static void
sdl_resize(DisplayState *ds, int w, int h, int rotation)
{
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_NONE, 0 }
};
int nn;
static int tracing = 0;
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 trackball command, handle down events to enable, and
// up events to disable
if (command == SKIN_KEY_COMMAND_TOGGLE_TRACKBALL) {
skin_window_toggle_trackball( emulator->window );
emulator->show_trackball = !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_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) {
emulator->layout = layout;
skin_window_reset( emulator->window, layout );
if (emulator->keyboard)
skin_keyboard_set_rotation( emulator->keyboard,
skin_layout_get_dpad_rotation( layout ) );
if (emulator->trackball)
skin_window_set_trackball( emulator->window, emulator->trackball );
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)
{
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 applyin the alias */
sprintf(tmp, "%s/%s", path, name);
if (!path_exists(tmp)) {
/* directory is invalid, apply alias */
int nn;