blob: b15d7519291de2786121be26da1707c5b1868c93 [file] [log] [blame]
Vladimir Chtchetkine01193622010-05-11 13:07:22 -07001/* Copyright (C) 2006-2010 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
12
Vladimir Chtchetkine01193622010-05-11 13:07:22 -070013#include "android/utils/debug.h"
14#include "android/utils/bufprint.h"
15#include "android/globals.h"
16#include "android/qemulator.h"
Vladimir Chtchetkine777eb682011-01-26 11:19:19 -080017#include "android/protocol/core-commands-api.h"
18#include "android/protocol/ui-commands-api.h"
David 'Digit' Turner055ae422010-07-27 11:34:16 -070019#include "user-events.h"
Vladimir Chtchetkine01193622010-05-11 13:07:22 -070020
21#define D(...) do { if (VERBOSE_CHECK(init)) dprint(__VA_ARGS__); } while (0)
22static double get_default_scale( AndroidOptions* opts );
23
24/* QEmulator structure instance. */
25static QEmulator qemulator[1];
26
David 'Digit' Turner055ae422010-07-27 11:34:16 -070027static void handle_key_command( void* opaque, SkinKeyCommand command, int param );
28static void qemulator_refresh(QEmulator* emulator);
Vladimir Chtchetkine6b985d72011-01-20 18:02:35 -080029extern void qemu_system_shutdown_request(void);
David 'Digit' Turner055ae422010-07-27 11:34:16 -070030
Vladimir Chtchetkine01193622010-05-11 13:07:22 -070031static void
32qemulator_light_brightness( void* opaque, const char* light, int value )
33{
34 QEmulator* emulator = opaque;
35
36 VERBOSE_PRINT(hw_control,"%s: light='%s' value=%d window=%p", __FUNCTION__, light, value, emulator->window);
37 if ( !strcmp(light, "lcd_backlight") ) {
38 emulator->lcd_brightness = value;
39 if (emulator->window)
40 skin_window_set_lcd_brightness( emulator->window, value );
41 return;
42 }
43}
44
45static void
46qemulator_setup( QEmulator* emulator )
47{
48 AndroidOptions* opts = emulator->opts;
49
50 if ( !emulator->window && !opts->no_window ) {
51 SkinLayout* layout = emulator->layout;
52 double scale = get_default_scale(emulator->opts);
53
54 emulator->window = skin_window_create( layout, emulator->win_x, emulator->win_y, scale, 0);
55 if (emulator->window == NULL)
56 return;
57
58 {
59 SkinTrackBall* ball;
60 SkinTrackBallParameters params;
61
62 params.diameter = 30;
63 params.ring = 2;
64 params.ball_color = 0xffe0e0e0;
65 params.dot_color = 0xff202020;
66 params.ring_color = 0xff000000;
67
68 ball = skin_trackball_create( &params );
69 emulator->trackball = ball;
70 skin_window_set_trackball( emulator->window, ball );
71
72 emulator->lcd_brightness = 128; /* 50% */
73 skin_window_set_lcd_brightness( emulator->window, emulator->lcd_brightness );
74 }
75
76 if ( emulator->onion != NULL )
77 skin_window_set_onion( emulator->window,
78 emulator->onion,
79 emulator->onion_rotation,
80 emulator->onion_alpha );
81
82 qemulator_set_title(emulator);
83
84 skin_window_enable_touch ( emulator->window, android_hw->hw_touchScreen != 0 );
85 skin_window_enable_dpad ( emulator->window, android_hw->hw_dPad != 0 );
86 skin_window_enable_qwerty( emulator->window, android_hw->hw_keyboard != 0 );
87 skin_window_enable_trackball( emulator->window, android_hw->hw_trackBall != 0 );
88 }
89
90 /* initialize hardware control support */
Vladimir Chtchetkine777eb682011-01-26 11:19:19 -080091 uicmd_set_brightness_change_callback(qemulator_light_brightness,
92 emulator);
Vladimir Chtchetkine01193622010-05-11 13:07:22 -070093}
94
95static void
96qemulator_fb_update( void* _emulator, int x, int y, int w, int h )
97{
98 QEmulator* emulator = _emulator;
99
David 'Digit' Turner07db3492011-02-02 17:36:34 +0100100 if (!emulator->window) {
David 'Digit' Turnera1dcb6c2011-02-09 17:32:56 +0100101 if (emulator->opts->no_window)
102 return;
David 'Digit' Turner07db3492011-02-02 17:36:34 +0100103 qemulator_setup( emulator );
104 }
105 skin_window_update_display( emulator->window, x, y, w, h );
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700106}
107
108static void
109qemulator_fb_rotate( void* _emulator, int rotation )
110{
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700111 QEmulator* emulator = _emulator;
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700112
113 qemulator_setup( emulator );
114}
115
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700116static void
117qemulator_fb_poll( void* _emulator )
118{
119 QEmulator* emulator = _emulator;
120 qemulator_refresh(emulator);
121}
122
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700123QEmulator*
124qemulator_get(void)
125{
126 return qemulator;
127}
128
129int
130qemulator_init( QEmulator* emulator,
131 AConfig* aconfig,
132 const char* basepath,
133 int x,
134 int y,
135 AndroidOptions* opts )
136{
137 emulator->aconfig = aconfig;
138 emulator->layout_file = skin_file_create_from_aconfig(aconfig, basepath);
139 emulator->layout = emulator->layout_file->layouts;
140 // If we have a custom charmap use it to initialize keyboard.
141 // Otherwise initialize keyboard from configuration settings.
142 // Another way to configure keyboard to use a custom charmap would
143 // be saving a custom charmap name into AConfig's keyboard->charmap
144 // property, and calling single skin_keyboard_create_from_aconfig
145 // routine to initialize keyboard.
146 if (NULL != opts->charmap) {
147 emulator->keyboard = skin_keyboard_create_from_kcm(opts->charmap, opts->raw_keys);
148 } else {
149 emulator->keyboard = skin_keyboard_create_from_aconfig(aconfig, opts->raw_keys);
150 }
151 emulator->window = NULL;
152 emulator->win_x = x;
153 emulator->win_y = y;
154 emulator->opts[0] = opts[0];
155
156 /* register as a framebuffer clients for all displays defined in the skin file */
157 SKIN_FILE_LOOP_PARTS( emulator->layout_file, part )
158 SkinDisplay* disp = part->display;
159 if (disp->valid) {
160 qframebuffer_add_client( disp->qfbuff,
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700161 emulator,
162 qemulator_fb_update,
163 qemulator_fb_rotate,
164 qemulator_fb_poll,
165 NULL );
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700166 }
167 SKIN_FILE_LOOP_END_PARTS
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700168
169 skin_keyboard_enable( emulator->keyboard, 1 );
170 skin_keyboard_on_command( emulator->keyboard, handle_key_command, emulator );
171
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700172 return 0;
173}
174
175void
176qemulator_done(QEmulator* emulator)
177{
178 if (emulator->window) {
179 skin_window_free(emulator->window);
180 emulator->window = NULL;
181 }
182 if (emulator->trackball) {
183 skin_trackball_destroy(emulator->trackball);
184 emulator->trackball = NULL;
185 }
186 if (emulator->keyboard) {
187 skin_keyboard_free(emulator->keyboard);
188 emulator->keyboard = NULL;
189 }
190 emulator->layout = NULL;
191 if (emulator->layout_file) {
192 skin_file_free(emulator->layout_file);
193 emulator->layout_file = NULL;
194 }
195}
196
197SkinLayout*
198qemulator_get_layout(QEmulator* emulator)
199{
200 return emulator->layout;
201}
202
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -0800203QFrameBuffer*
204qemulator_get_first_framebuffer(QEmulator* emulator)
205{
206 /* register as a framebuffer clients for all displays defined in the skin file */
207 SKIN_FILE_LOOP_PARTS( emulator->layout_file, part )
208 SkinDisplay* disp = part->display;
209 if (disp->valid) {
210 return disp->qfbuff;
211 }
212 SKIN_FILE_LOOP_END_PARTS
213 return NULL;
214}
215
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700216void
217qemulator_set_title(QEmulator* emulator)
218{
219 char temp[128], *p=temp, *end=p+sizeof temp;;
220
221 if (emulator->window == NULL)
222 return;
223
224 if (emulator->show_trackball) {
225 SkinKeyBinding bindings[ SKIN_KEY_COMMAND_MAX_BINDINGS ];
226 int count;
227
228 count = skin_keyset_get_bindings( android_keyset,
229 SKIN_KEY_COMMAND_TOGGLE_TRACKBALL,
230 bindings );
231
232 if (count > 0) {
233 int nn;
234 p = bufprint( p, end, "Press " );
235 for (nn = 0; nn < count; nn++) {
236 if (nn > 0) {
237 if (nn < count-1)
238 p = bufprint(p, end, ", ");
239 else
240 p = bufprint(p, end, " or ");
241 }
242 p = bufprint(p, end, "%s",
243 skin_key_symmod_to_str( bindings[nn].sym,
244 bindings[nn].mod ) );
245 }
246 p = bufprint(p, end, " to leave trackball mode. ");
247 }
248 }
249
250 p = bufprint(p, end, "%d:%s",
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800251 android_base_port,
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700252 avdInfo_getName( android_avdInfo ));
253
254 skin_window_set_title( emulator->window, temp );
255}
256
257/*
258 * Helper routines
259 */
260
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +0100261static int
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700262get_device_dpi( AndroidOptions* opts )
263{
Vladimir Chtchetkine777eb682011-01-26 11:19:19 -0800264 int dpi_device = corecmd_get_hw_lcd_density();
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700265
266 if (opts->dpi_device != NULL) {
267 char* end;
268 dpi_device = strtol( opts->dpi_device, &end, 0 );
269 if (end == NULL || *end != 0 || dpi_device <= 0) {
270 fprintf(stderr, "argument for -dpi-device must be a positive integer. Aborting\n" );
271 exit(1);
272 }
273 }
274 return dpi_device;
275}
276
277static double
278get_default_scale( AndroidOptions* opts )
279{
280 int dpi_device = get_device_dpi( opts );
281 int dpi_monitor = -1;
282 double scale = 0.0;
283
284 /* possible values for the 'scale' option are
285 * 'auto' : try to determine the scale automatically
286 * '<number>dpi' : indicates the host monitor dpi, compute scale accordingly
287 * '<fraction>' : use direct scale coefficient
288 */
289
290 if (opts->scale) {
291 if (!strcmp(opts->scale, "auto"))
292 {
293 /* we need to get the host dpi resolution ? */
294 int xdpi, ydpi;
295
296 if ( SDL_WM_GetMonitorDPI( &xdpi, &ydpi ) < 0 ) {
297 fprintf(stderr, "could not get monitor DPI resolution from system. please use -dpi-monitor to specify one\n" );
298 exit(1);
299 }
300 D( "system reported monitor resolutions: xdpi=%d ydpi=%d\n", xdpi, ydpi);
301 dpi_monitor = (xdpi + ydpi+1)/2;
302 }
303 else
304 {
305 char* end;
306 scale = strtod( opts->scale, &end );
307
308 if (end && end[0] == 'd' && end[1] == 'p' && end[2] == 'i' && end[3] == 0) {
309 if ( scale < 20 || scale > 1000 ) {
310 fprintf(stderr, "emulator: ignoring bad -scale argument '%s': %s\n", opts->scale,
311 "host dpi number must be between 20 and 1000" );
312 exit(1);
313 }
314 dpi_monitor = scale;
315 scale = 0.0;
316 }
317 else if (end == NULL || *end != 0) {
318 fprintf(stderr, "emulator: ignoring bad -scale argument '%s': %s\n", opts->scale,
319 "not a number or the 'auto' keyword" );
320 exit(1);
321 }
322 else if ( scale < 0.1 || scale > 3. ) {
323 fprintf(stderr, "emulator: ignoring bad -window-scale argument '%s': %s\n", opts->scale,
324 "must be between 0.1 and 3.0" );
325 exit(1);
326 }
327 }
328 }
329
330 if (scale == 0.0 && dpi_monitor > 0)
331 scale = dpi_monitor*1.0/dpi_device;
332
333 if (scale == 0.0)
334 scale = 1.0;
335
336 return scale;
337}
338
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700339/* used to respond to a given keyboard command shortcut
340 */
341static void
342handle_key_command( void* opaque, SkinKeyCommand command, int down )
343{
344 static const struct { SkinKeyCommand cmd; AndroidKeyCode kcode; } keycodes[] =
345 {
346 { SKIN_KEY_COMMAND_BUTTON_CALL, kKeyCodeCall },
347 { SKIN_KEY_COMMAND_BUTTON_HOME, kKeyCodeHome },
348 { SKIN_KEY_COMMAND_BUTTON_BACK, kKeyCodeBack },
349 { SKIN_KEY_COMMAND_BUTTON_HANGUP, kKeyCodeEndCall },
350 { SKIN_KEY_COMMAND_BUTTON_POWER, kKeyCodePower },
351 { SKIN_KEY_COMMAND_BUTTON_SEARCH, kKeyCodeSearch },
352 { SKIN_KEY_COMMAND_BUTTON_MENU, kKeyCodeMenu },
353 { SKIN_KEY_COMMAND_BUTTON_DPAD_UP, kKeyCodeDpadUp },
354 { SKIN_KEY_COMMAND_BUTTON_DPAD_LEFT, kKeyCodeDpadLeft },
355 { SKIN_KEY_COMMAND_BUTTON_DPAD_RIGHT, kKeyCodeDpadRight },
356 { SKIN_KEY_COMMAND_BUTTON_DPAD_DOWN, kKeyCodeDpadDown },
357 { SKIN_KEY_COMMAND_BUTTON_DPAD_CENTER, kKeyCodeDpadCenter },
358 { SKIN_KEY_COMMAND_BUTTON_VOLUME_UP, kKeyCodeVolumeUp },
359 { SKIN_KEY_COMMAND_BUTTON_VOLUME_DOWN, kKeyCodeVolumeDown },
360 { SKIN_KEY_COMMAND_BUTTON_CAMERA, kKeyCodeCamera },
361 { SKIN_KEY_COMMAND_NONE, 0 }
362 };
363 int nn;
364#ifdef CONFIG_TRACE
365 static int tracing = 0;
366#endif
367 QEmulator* emulator = opaque;
368
369
370 for (nn = 0; keycodes[nn].kcode != 0; nn++) {
371 if (command == keycodes[nn].cmd) {
372 unsigned code = keycodes[nn].kcode;
373 if (down)
374 code |= 0x200;
375 user_event_keycode( code );
376 return;
377 }
378 }
379
380 // for the show-trackball command, handle down events to enable, and
381 // up events to disable
382 if (command == SKIN_KEY_COMMAND_SHOW_TRACKBALL) {
383 emulator->show_trackball = (down != 0);
384 skin_window_show_trackball( emulator->window, emulator->show_trackball );
385 //qemulator_set_title( emulator );
386 return;
387 }
388
389 // only handle down events for the rest
390 if (down == 0)
391 return;
392
393 switch (command)
394 {
395 case SKIN_KEY_COMMAND_TOGGLE_NETWORK:
396 {
Vladimir Chtchetkine777eb682011-01-26 11:19:19 -0800397 corecmd_toggle_network();
398 D( "network is now %s", corecmd_is_network_disabled() ?
Vladimir Chtchetkinecefa7442010-09-01 09:17:11 -0700399 "disconnected" : "connected" );
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700400 }
401 break;
402
403 case SKIN_KEY_COMMAND_TOGGLE_FULLSCREEN:
404 if (emulator->window) {
405 skin_window_toggle_fullscreen(emulator->window);
406 }
407 break;
408
409 case SKIN_KEY_COMMAND_TOGGLE_TRACING:
410 {
411#ifdef CONFIG_TRACE
412 tracing = !tracing;
Vladimir Chtchetkine777eb682011-01-26 11:19:19 -0800413 corecmd_trace_control(tracing);
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700414#endif
415 }
416 break;
417
418 case SKIN_KEY_COMMAND_TOGGLE_TRACKBALL:
419 emulator->show_trackball = !emulator->show_trackball;
420 skin_window_show_trackball( emulator->window, emulator->show_trackball );
421 qemulator_set_title(emulator);
422 break;
423
424 case SKIN_KEY_COMMAND_ONION_ALPHA_UP:
425 case SKIN_KEY_COMMAND_ONION_ALPHA_DOWN:
426 if (emulator->onion)
427 {
428 int alpha = emulator->onion_alpha;
429
430 if (command == SKIN_KEY_COMMAND_ONION_ALPHA_UP)
431 alpha += 16;
432 else
433 alpha -= 16;
434
435 if (alpha > 256)
436 alpha = 256;
437 else if (alpha < 0)
438 alpha = 0;
439
440 emulator->onion_alpha = alpha;
441 skin_window_set_onion( emulator->window, emulator->onion, emulator->onion_rotation, alpha );
442 skin_window_redraw( emulator->window, NULL );
443 //dprint( "onion alpha set to %d (%.f %%)", alpha, alpha/2.56 );
444 }
445 break;
446
447 case SKIN_KEY_COMMAND_CHANGE_LAYOUT_PREV:
448 case SKIN_KEY_COMMAND_CHANGE_LAYOUT_NEXT:
449 {
450 SkinLayout* layout = NULL;
451
452 if (command == SKIN_KEY_COMMAND_CHANGE_LAYOUT_NEXT) {
453 layout = emulator->layout->next;
454 if (layout == NULL)
455 layout = emulator->layout_file->layouts;
456 }
457 else if (command == SKIN_KEY_COMMAND_CHANGE_LAYOUT_PREV) {
458 layout = emulator->layout_file->layouts;
459 while (layout->next && layout->next != emulator->layout)
460 layout = layout->next;
461 }
462 if (layout != NULL) {
463 SkinRotation rotation;
464
465 emulator->layout = layout;
466 skin_window_reset( emulator->window, layout );
467
468 rotation = skin_layout_get_dpad_rotation( layout );
469
470 if (emulator->keyboard)
471 skin_keyboard_set_rotation( emulator->keyboard, rotation );
472
473 if (emulator->trackball) {
474 skin_trackball_set_rotation( emulator->trackball, rotation );
475 skin_window_set_trackball( emulator->window, emulator->trackball );
476 skin_window_show_trackball( emulator->window, emulator->show_trackball );
477 }
478
479 skin_window_set_lcd_brightness( emulator->window, emulator->lcd_brightness );
480
481 qframebuffer_invalidate_all();
482 qframebuffer_check_updates();
483 }
484 }
485 break;
486
487 default:
488 /* XXX: TODO ? */
489 ;
490 }
491}
492
493/* called periodically to poll for user input events */
494static void qemulator_refresh(QEmulator* emulator)
495{
496 SDL_Event ev;
497 SkinWindow* window = emulator->window;
498 SkinKeyboard* keyboard = emulator->keyboard;
499
500 /* this will eventually call sdl_update if the content of the VGA framebuffer
501 * has changed */
502 qframebuffer_check_updates();
503
504 if (window == NULL)
505 return;
506
507 while(SDL_PollEvent(&ev)){
508 switch(ev.type){
509 case SDL_VIDEOEXPOSE:
510 skin_window_redraw( window, NULL );
511 break;
512
513 case SDL_KEYDOWN:
514#ifdef _WIN32
515 /* special code to deal with Alt-F4 properly */
516 if (ev.key.keysym.sym == SDLK_F4 &&
517 ev.key.keysym.mod & KMOD_ALT) {
518 goto CleanExit;
519 }
520#endif
521#ifdef __APPLE__
522 /* special code to deal with Command-Q properly */
523 if (ev.key.keysym.sym == SDLK_q &&
524 ev.key.keysym.mod & KMOD_META) {
525 goto CleanExit;
526 }
527#endif
528 skin_keyboard_process_event( keyboard, &ev, 1 );
529 break;
530
531 case SDL_KEYUP:
532 skin_keyboard_process_event( keyboard, &ev, 0 );
533 break;
534
535 case SDL_MOUSEMOTION:
536 skin_window_process_event( window, &ev );
537 break;
538
539 case SDL_MOUSEBUTTONDOWN:
540 case SDL_MOUSEBUTTONUP:
541 {
542 int down = (ev.type == SDL_MOUSEBUTTONDOWN);
543 if (ev.button.button == 4)
544 {
545 /* scroll-wheel simulates DPad up */
546 AndroidKeyCode kcode;
547
548 kcode = // qemulator_rotate_keycode(kKeyCodeDpadUp);
549 android_keycode_rotate(kKeyCodeDpadUp,
550 skin_layout_get_dpad_rotation(qemulator_get_layout(qemulator_get())));
551 user_event_key( kcode, down );
552 }
553 else if (ev.button.button == 5)
554 {
555 /* scroll-wheel simulates DPad down */
556 AndroidKeyCode kcode;
557
558 kcode = // qemulator_rotate_keycode(kKeyCodeDpadDown);
559 android_keycode_rotate(kKeyCodeDpadDown,
560 skin_layout_get_dpad_rotation(qemulator_get_layout(qemulator_get())));
561 user_event_key( kcode, down );
562 }
563 else if (ev.button.button == SDL_BUTTON_LEFT) {
564 skin_window_process_event( window, &ev );
565 }
566#if 0
567 else {
568 fprintf(stderr, "... mouse button %s: button=%d state=%04x x=%d y=%d\n",
569 down ? "down" : "up ",
570 ev.button.button, ev.button.state, ev.button.x, ev.button.y);
571 }
572#endif
573 }
574 break;
575
576 case SDL_QUIT:
577#if defined _WIN32 || defined __APPLE__
578 CleanExit:
579#endif
580 /* only save emulator config through clean exit */
581 qemulator_done(qemulator_get());
Vladimir Chtchetkine6b985d72011-01-20 18:02:35 -0800582 qemu_system_shutdown_request();
David 'Digit' Turner055ae422010-07-27 11:34:16 -0700583 return;
584 }
585 }
586
587 skin_keyboard_flush( keyboard );
588}
589
Vladimir Chtchetkine01193622010-05-11 13:07:22 -0700590/*
591 * android/console.c helper routines.
592 */
593
594SkinKeyboard*
595android_emulator_get_keyboard(void)
596{
597 return qemulator->keyboard;
598}
599
600void
601android_emulator_set_window_scale( double scale, int is_dpi )
602{
603 QEmulator* emulator = qemulator;
604
605 if (is_dpi)
606 scale /= get_device_dpi( emulator->opts );
607
608 if (emulator->window)
609 skin_window_set_scale( emulator->window, scale );
610}
611
David 'Digit' Turner3cf34f22010-07-30 16:53:39 -0700612
613void
614android_emulator_set_base_port( int port )
615{
Vladimir Chtchetkinecefa7442010-09-01 09:17:11 -0700616 /* Base port is already set in the emulator's core. */
David 'Digit' Turner3cf34f22010-07-30 16:53:39 -0700617 qemulator_set_title(qemulator);
618}