/* 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 #include #include #include #ifdef _WIN32 #include #endif #include "libslirp.h" #include "sockets.h" #include "android/android.h" #include "qemu-common.h" #include "sysemu.h" #include "console.h" #include #include #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/hw-qemud.h" #include "android/hw-kmsg.h" #include "android/hw-lcd.h" #include "android/hw-control.h" #include "android/hw-sensors.h" #include "android/boot-properties.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/cmdline-option.h" #include "android/help.h" #include "hw/goldfish_nand.h" #ifdef CONFIG_MEMCHECK #include "memcheck/memcheck.h" #endif // CONFIG_MEMCHECK #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 #ifdef ANDROID_SDK_TOOLS_REVISION # define VERSION_STRING STRINGIFY(ANDROID_SDK_TOOLS_REVISION)".0" #else # define VERSION_STRING "standalone" #endif #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 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 HOST_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 */ } } /* see http://en.wikipedia.org/wiki/List_of_device_bandwidths or a complete list */ const NetworkSpeed android_netspeeds[] = { { "gsm", "GSM/CSD", 14400, 14400 }, { "hscsd", "HSCSD", 14400, 43200 }, { "gprs", "GPRS", 40000, 80000 }, { "edge", "EDGE/EGPRS", 118400, 236800 }, { "umts", "UMTS/3G", 128000, 1920000 }, { "hsdpa", "HSDPA", 348000, 14400000 }, { "full", "no limit", 0, 0 }, { NULL, NULL, 0, 0 } }; const NetworkLatency android_netdelays[] = { /* FIXME: these numbers are totally imaginary */ { "gprs", "GPRS", 150, 550 }, { "edge", "EDGE/EGPRS", 80, 400 }, { "umts", "UMTS/3G", 35, 200 }, { "none", "no latency", 0, 0 }, { NULL, NULL, 0, 0 } }; #define ONE_MB (1024*1024) unsigned convertBytesToMB( uint64_t size ) { if (size == 0) return 0; size = (size + ONE_MB-1) >> 20; if (size > UINT_MAX) size = UINT_MAX; return (unsigned) size; } uint64_t convertMBToBytes( unsigned megaBytes ) { return ((uint64_t)megaBytes << 20); } /***********************************************************************/ /***********************************************************************/ /***** *****/ /***** 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; // If we have a custom charmap use it to initialize keyboard. // Otherwise initialize keyboard from configuration settings. // Another way to configure keyboard to use a custom charmap would // be saving a custom charmap name into AConfig's keyboard->charmap // property, and calling single skin_keyboard_create_from_aconfig // routine to initialize keyboard. if (NULL != opts->charmap) { emulator->keyboard = skin_keyboard_create_from_kcm(opts->charmap, opts->raw_keys); } else { 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 ) { return android_keycode_rotate( sym, skin_layout_get_dpad_rotation(emulator->layout) ); } static int get_device_dpi( AndroidOptions* opts ) { int dpi_device = android_hw->hw_lcd_density; 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 * 'dpi' : indicates the host monitor dpi, compute scale accordingly * '' : use direct scale coefficient */ if (opts->scale) { if (!strcmp(opts->scale, "auto")) { /* we need to get the host dpi resolution ? */ int xdpi, ydpi; if ( SDL_WM_GetMonitorDPI( &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[128], *p=temp, *end=p+sizeof temp;; if (emulator->window == NULL) return; if (emulator->show_trackball) { SkinKeyBinding bindings[ SKIN_KEY_COMMAND_MAX_BINDINGS ]; int count; count = skin_keyset_get_bindings( android_keyset, SKIN_KEY_COMMAND_TOGGLE_TRACKBALL, bindings ); if (count > 0) { int nn; p = bufprint( p, end, "Press " ); for (nn = 0; nn < count; nn++) { if (nn > 0) { if (nn < count-1) p = bufprint(p, end, ", "); else p = bufprint(p, end, " or "); } p = bufprint(p, end, "%s", skin_key_symmod_to_str( bindings[nn].sym, bindings[nn].mod ) ); } p = bufprint(p, end, " to leave trackball mode. "); } } p = bufprint(p, end, "%d:%s", android_base_port, avdInfo_getName( android_avdInfo )); 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) { //fprintf(stderr, "weird, sdl_resize being called with framebuffer interface\n"); //exit(1); } /* called periodically to poll for user input events */ 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 ); qemulator_set_title( emulator ); 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); DisplayChangeListener* dcl; int width, height; if (disp->rotation & 1) { width = disp->rect.size.h; height = disp->rect.size.w; } else { width = disp->rect.size.w; height = disp->rect.size.h; } /* Register a display state object for the emulated framebuffer */ ds->allocator = &default_allocator; ds->opaque = emulator; ds->surface = qemu_create_displaysurface(ds, width, height); register_displaystate(ds); /* Register a change listener for it */ dcl = (DisplayChangeListener *) qemu_mallocz(sizeof(DisplayChangeListener)); dcl->dpy_update = sdl_update; dcl->dpy_resize = sdl_resize; dcl->dpy_refresh = sdl_refresh; dcl->dpy_text_cursor = NULL; register_displaychangelistener(ds, dcl); 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 = ""; 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=, 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=, 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=" ); exit(1); } else if (signal < 0) { derror( "bad parameter: missing signal=" ); 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 [,