Added -android-gui command line option to qemu-android

Change-Id: I9fd9170f2b2e3ad7d80071888f7da2ead54c21cf
diff --git a/vl-android.c b/vl-android.c
index ea40275..c09d8a7 100644
--- a/vl-android.c
+++ b/vl-android.c
@@ -337,6 +337,13 @@
 // Path to the file containing specific key character map.
 char* op_charmap_file = NULL;
 
+/* Framebuffer dimensions, passed with -android-gui option. */
+char* android_op_gui = NULL;
+
+extern int android_display_width;
+extern int android_display_height;
+extern int android_display_bpp;
+
 extern void  dprint( const char* format, ... );
 
 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
@@ -423,6 +430,42 @@
 #endif
 }
 
+/* Parses -android-gui command line option, extracting width, height and bits
+ * per pixel parameters for the GUI console used in this session of the
+ * emulator. -android-gui option contains exactly three comma-separated positive
+ * integer numbers in strict order: width goes first, width goes next, and bits
+ * per pixel goes third. This routine verifies that format and return 0 if all
+ * three numbers were extracted, or -1 if string format was incorrect for that
+ * option. Note that this routine does not verify that extracted values are
+ * correct!
+ */
+static int
+parse_androig_gui_option(const char* op, int* width, int* height, int* bpp)
+{
+    char val[128];
+
+    if (get_param_value(val, 128, "width", op)) {
+        *width = strtol(val, NULL, 0);
+    } else {
+        fprintf(stderr, "option -android-gui is missing width parameter\n");
+        return -1;
+    }
+    if (get_param_value(val, 128, "height", op)) {
+        *height = strtol(val, NULL, 0);
+    } else {
+        fprintf(stderr, "option -android-gui is missing height parameter\n");
+        return -1;
+    }
+    if (get_param_value(val, 128, "bpp", op)) {
+        *bpp = strtol(val, NULL, 0);
+    } else {
+        fprintf(stderr, "option -android-gui is missing bpp parameter\n");
+        return -1;
+    }
+
+    return 0;
+}
+
 /***********************************************************/
 void hw_error(const char *fmt, ...)
 {
@@ -2859,6 +2902,15 @@
     register_displaystate(ds);
 }
 
+static void
+android_display_init_from(int width, int height, int rotation, int bpp)
+{
+    DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
+    ds->allocator = &default_allocator;
+    ds->surface = qemu_create_displaysurface(ds, width, height);
+    register_displaystate(ds);
+}
+
 /***********************************************************/
 /* I/O handling */
 
@@ -5746,6 +5798,10 @@
             case QEMU_OPTION_charmap:
                 op_charmap_file = (char*)optarg;
                 break;
+
+            case QEMU_OPTION_android_gui:
+                android_op_gui = (char*)optarg;
+                break;
             }
         }
     }
@@ -6169,8 +6225,34 @@
         }
     }
 
-    if (!display_state)
-        dumb_display_init();
+    if (!display_state) {
+        if (android_op_gui) {
+            /* Initialize display from the command line parameters. */
+            if (parse_androig_gui_option(android_op_gui,
+                                         &android_display_width,
+                                         &android_display_height,
+                                         &android_display_bpp)) {
+                exit(1);
+            }
+            android_display_init_from(android_display_width,
+                                      android_display_height, 0,
+                                      android_display_bpp);
+        } else {
+            dumb_display_init();
+        }
+    } else if (android_op_gui) {
+        /* Resize display from the command line parameters. */
+        if (parse_androig_gui_option(android_op_gui,
+                                     &android_display_width,
+                                     &android_display_height,
+                                     &android_display_bpp)) {
+            exit(1);
+        }
+        display_state->surface = qemu_resize_displaysurface(display_state,
+                                                            android_display_width,
+                                                            android_display_height);
+    }
+
     /* just use the first displaystate for the moment */
     ds = display_state;