Remove UI dependency from goldfish_event_device.c

Change-Id: Iea5edfbae0caff45161c814d631d35d2e6d61d1c
diff --git a/android/charmap.c b/android/charmap.c
index af4163a..553ad67 100644
--- a/android/charmap.c
+++ b/android/charmap.c
@@ -265,8 +265,8 @@
 /* Custom character map created with -charmap option. */
 static AKeyCharmap android_custom_charmap = { 0 };
 
-const AKeyCharmap** android_charmaps = 0;
-int                 android_charmap_count = 0;
+static const AKeyCharmap** android_charmaps = 0;
+static int                 android_charmap_count = 0;
 
 /* Checks if a character represents an end of the line.
  * Returns a non-zero value if ch is an EOL character. Returns
@@ -693,6 +693,13 @@
 
 int
 android_charmap_setup(const char* kcm_file_path) {
+    // android_charmap_count being non-zero is used here as a flag,
+    //  indicating that charmap has been initialized for the running
+    // executable.
+    if (android_charmap_count != 0) {
+        return 0;
+    }
+
     if (NULL != kcm_file_path) {
         if (!parse_kcm_file(kcm_file_path, &android_custom_charmap)) {
             // Here we have two default charmaps and the custom one.
@@ -820,3 +827,15 @@
     /* no match */
     return 0;
 }
+
+const AKeyCharmap*
+android_get_default_charmap(void)
+{
+    return android_get_charmap_by_index(0);
+}
+
+const char*
+android_get_default_charmap_name(void)
+{
+    return android_get_default_charmap()->name;
+}
diff --git a/android/charmap.h b/android/charmap.h
index 08b059d..6bf35c0 100644
--- a/android/charmap.h
+++ b/android/charmap.h
@@ -90,4 +90,10 @@
                                     int  down,
                                     AKeycodeBuffer* keycodes);
 
+/* Gets default charmap (index 0) */
+const AKeyCharmap* android_get_default_charmap(void);
+
+/* Gets name of the default charmap (index 0) */
+const char* android_get_default_charmap_name(void);
+
 #endif /* _android_charmap_h */
diff --git a/android/console.c b/android/console.c
index a7b55e9..f913f7d 100644
--- a/android/console.c
+++ b/android/console.c
@@ -1807,7 +1807,7 @@
     }
 
     /* Get default charmap. */
-    charmap = android_get_charmap_by_index(0);
+    charmap = android_get_default_charmap();
     if (charmap == NULL) {
         control_write( client, "KO: no character map active in current device layout/config\r\n" );
         return -1;
diff --git a/android/main.c b/android/main.c
index 04d715c..88ce36b 100644
--- a/android/main.c
+++ b/android/main.c
@@ -2174,6 +2174,11 @@
         args[n++] = opts->http_proxy;
     }
 
+    if (opts->charmap) {
+        args[n++] = "-charmap";
+        args[n++] = opts->charmap;
+    }
+
     /* physical memory */
     args[n++] = "-m";
     args[n++] = opts->memory;
diff --git a/hw/goldfish_events_device.c b/hw/goldfish_events_device.c
index 9050aa6..9e98dac 100644
--- a/hw/goldfish_events_device.c
+++ b/hw/goldfish_events_device.c
@@ -274,7 +274,8 @@
     AndroidHwConfig*  config = android_hw;
 
     s = (events_state *) qemu_mallocz(sizeof(events_state));
-    s->name = android_skin_keycharmap;
+    // Use name of the default charmap.
+    s->name = android_get_default_charmap_name();
 
     /* now set the events capability bits depending on hardware configuration */
     /* apparently, the EV_SYN array is used to indicate which other
diff --git a/qemu-options.hx b/qemu-options.hx
index e67033a..ed89bc5 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1619,4 +1619,8 @@
     "-http-proxy <proxy>"
     " make TCP connections through a HTTP/HTTPS proxy\n")
 
+DEF("charmap", HAS_ARG, QEMU_OPTION_charmap, \
+    "-charmap <file>"
+    " use specific key character map\n")
+
 #endif
diff --git a/vl-android.c b/vl-android.c
index 1673426..ea40275 100644
--- a/vl-android.c
+++ b/vl-android.c
@@ -186,7 +186,7 @@
 unsigned long   android_verbose;
 #endif  // CONFIG_STANDALONE_CORE
 
-#ifdef CONFIG_SKINS
+#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
 #undef main
 #define main qemu_main
 #endif
@@ -334,6 +334,8 @@
 extern char* android_op_port;
 extern char* android_op_report_console;
 extern char* op_http_proxy;
+// Path to the file containing specific key character map.
+char* op_charmap_file = NULL;
 
 extern void  dprint( const char* format, ... );
 
@@ -5740,10 +5742,27 @@
             case QEMU_OPTION_http_proxy:
                 op_http_proxy = (char*)optarg;
                 break;
+
+            case QEMU_OPTION_charmap:
+                op_charmap_file = (char*)optarg;
+                break;
             }
         }
     }
 
+    /* Initialize character map. */
+    if (android_charmap_setup(op_charmap_file)) {
+        if (op_charmap_file) {
+            fprintf(stderr,
+                    "Unable to initialize character map from file %s.\n",
+                    op_charmap_file);
+        } else {
+            fprintf(stderr,
+                    "Unable to initialize default character map.\n");
+        }
+        exit(1);
+    }
+
     /* If no data_dir is specified then try to find it relative to the
        executable path.  */
     if (!data_dir) {