Make "qwerty2" the default charmap.

This patch makes "qwerty2" the default charmap for skins that
don't specifiy one explicitely. Note that the "qwerty" charmap
is here for historical reasons only. All skins provided by all
SDK Android platforms explicitely mention "qwerty2", so this patch
will mainly impact "magic skins" like those created by an option
like "-skin 1024x768".

This gets rid of the obsolete "qwerty" charmap (it was never
used by any of the skins provided by the SDK, and is incorrect).

+ Simplify android/charmap.[hc] code.
+ Lazy-load the charmap name in hw/goldfish_events_device.c
diff --git a/android/charmap.c b/android/charmap.c
index 81c866a..95dacee 100644
--- a/android/charmap.c
+++ b/android/charmap.c
@@ -133,73 +133,9 @@
 
 /* the following is automatically generated by the 'gen-charmap.py' script
  * do not touch. the generation command was:
- *   gen-charmap.py qwerty.kcm qwerty2.kcm
+ *   gen-charmap.py qwerty2.kcm
  */
 
-static const AKeyEntry  _qwerty_keys[] =
-{
-   /* keycode                   base   caps    fn  caps+fn   number */
-
-    { kKeyCodeA             ,   'a',   'A',   '#',   0x00,   '#' },
-    { kKeyCodeB             ,   'b',   'B',   '<',   0x00,   '<' },
-    { kKeyCodeC             ,   'c',   'C',   '9', 0x00E7,   '9' },
-    { kKeyCodeD             ,   'd',   'D',   '5',   0x00,   '5' },
-    { kKeyCodeE             ,   'e',   'E',   '2', 0x0301,   '2' },
-    { kKeyCodeF             ,   'f',   'F',   '6', 0x00A5,   '6' },
-    { kKeyCodeG             ,   'g',   'G',   '-',    '_',   '-' },
-    { kKeyCodeH             ,   'h',   'H',   '[',    '{',   '[' },
-    { kKeyCodeI             ,   'i',   'I',   '$', 0x0302,   '$' },
-    { kKeyCodeJ             ,   'j',   'J',   ']',    '}',   ']' },
-    { kKeyCodeK             ,   'k',   'K',   '"',    '~',   '"' },
-    { kKeyCodeL             ,   'l',   'L',  '\'',    '`',  '\'' },
-    { kKeyCodeM             ,   'm',   'M',   '!',   0x00,   '!' },
-    { kKeyCodeN             ,   'n',   'N',   '>', 0x0303,   '>' },
-    { kKeyCodeO             ,   'o',   'O',   '(',   0x00,   '(' },
-    { kKeyCodeP             ,   'p',   'P',   ')',   0x00,   ')' },
-    { kKeyCodeQ             ,   'q',   'Q',   '*', 0x0300,   '*' },
-    { kKeyCodeR             ,   'r',   'R',   '3', 0x20AC,   '3' },
-    { kKeyCodeS             ,   's',   'S',   '4', 0x00DF,   '4' },
-    { kKeyCodeT             ,   't',   'T',   '+', 0x00A3,   '+' },
-    { kKeyCodeU             ,   'u',   'U',   '&', 0x0308,   '&' },
-    { kKeyCodeV             ,   'v',   'V',   '=',    '^',   '=' },
-    { kKeyCodeW             ,   'w',   'W',   '1',   0x00,   '1' },
-    { kKeyCodeX             ,   'x',   'X',   '8',   0x00,   '8' },
-    { kKeyCodeY             ,   'y',   'Y',   '%', 0x00A1,   '%' },
-    { kKeyCodeZ             ,   'z',   'Z',   '7',   0x00,   '7' },
-    { kKeyCodeComma         ,   ',',   ';',   ';',    '|',   ',' },
-    { kKeyCodePeriod        ,   '.',   ':',   ':', 0x2026,   '.' },
-    { kKeyCodeAt            ,   '@',   '0',   '0', 0x2022,   '0' },
-    { kKeyCodeSlash         ,   '/',   '?',   '?',   '\\',   '/' },
-    { kKeyCodeSpace         ,  0x20,  0x20,   0x9,    0x9,  0x20 },
-    { kKeyCodeNewline       ,   0xa,   0xa,   0xa,    0xa,   0xa },
-    { kKeyCodeTab           ,   0x9,   0x9,   0x9,    0x9,   0x9 },
-    { kKeyCode0             ,   '0',   ')',   '0',    ')',   '0' },
-    { kKeyCode1             ,   '1',   '!',   '1',    '!',   '1' },
-    { kKeyCode2             ,   '2',   '@',   '2',    '@',   '2' },
-    { kKeyCode3             ,   '3',   '#',   '3',    '#',   '3' },
-    { kKeyCode4             ,   '4',   '$',   '4',    '$',   '4' },
-    { kKeyCode5             ,   '5',   '%',   '5',    '%',   '5' },
-    { kKeyCode6             ,   '6',   '^',   '6',    '^',   '6' },
-    { kKeyCode7             ,   '7',   '&',   '7',    '&',   '7' },
-    { kKeyCode8             ,   '8',   '*',   '8',    '*',   '8' },
-    { kKeyCode9             ,   '9',   '(',   '9',    '(',   '9' },
-    { kKeyCodeGrave         ,   '`',   '~',   '`',    '~',   '`' },
-    { kKeyCodeMinus         ,   '-',   '_',   '-',    '_',   '-' },
-    { kKeyCodeEquals        ,   '=',   '+',   '=',    '+',   '=' },
-    { kKeyCodeLeftBracket   ,   '[',   '{',   '[',    '{',   '[' },
-    { kKeyCodeRightBracket  ,   ']',   '}',   ']',    '}',   ']' },
-    { kKeyCodeBackslash     ,  '\\',   '|',  '\\',    '|',  '\\' },
-    { kKeyCodeSemicolon     ,   ';',   ':',   ';',    ':',   ';' },
-    { kKeyCodeApostrophe    ,  '\'',   '"',  '\'',    '"',  '\'' },
-};
-
-static const AKeyCharmap  _qwerty_charmap =
-{
-    _qwerty_keys,
-    51,
-    "qwerty"
-};
-
 static const AKeyEntry  _qwerty2_keys[] =
 {
    /* keycode                   base   caps    fn  caps+fn   number */
@@ -257,7 +193,7 @@
     { kKeyCodeApostrophe    ,  '\'',   '"',  '\'',    '"',  '\'' },
 };
 
-static const AKeyCharmap  _qwerty2_charmap =
+static const AKeyCharmap  _default_charmap =
 {
     _qwerty2_keys,
     51,
@@ -267,8 +203,7 @@
 /* Custom character map created with -charmap option. */
 static AKeyCharmap android_custom_charmap = { 0 };
 
-static const AKeyCharmap** android_charmaps = 0;
-static int                 android_charmap_count = 0;
+static const AKeyCharmap* android_charmap = &_default_charmap;
 
 /* Checks if a character represents an end of the line.
  * Returns a non-zero value if ch is an EOL character. Returns
@@ -692,31 +627,17 @@
 
 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.
-            android_charmap_count = 3;
-            AARRAY_NEW(android_charmaps, android_charmap_count);
-            android_charmaps[0] = &android_custom_charmap;
-            android_charmaps[1] = &_qwerty_charmap;
-            android_charmaps[2] = &_qwerty2_charmap;
-        } else {
-            derror("Unable to parse kcm file.");
-            return -1;
-        }
+    /* Return if we already loaded a charmap */
+    if (android_charmap != &_default_charmap || kcm_file_path == NULL)
+        return 0;
+
+    if (!parse_kcm_file(kcm_file_path, &android_custom_charmap)) {
+        // Here we have the default charmap and the custom one.
+        android_charmap = &android_custom_charmap;
     } else {
-        // Here we have only two default charmaps.
-        android_charmap_count = 2;
-        AARRAY_NEW(android_charmaps, android_charmap_count);
-        android_charmaps[0] = &_qwerty_charmap;
-        android_charmaps[1] = &_qwerty2_charmap;
+        derror("Unable to parse kcm file.");
+        return -1;
     }
 
     return 0;
@@ -724,40 +645,21 @@
 
 void
 android_charmap_done(void) {
-  if (NULL != android_charmaps) {
-      int n;
-      for (n = 0; n < android_charmap_count; n++) {
-          // Entries for qwerty and qwerty2 character maps are
-          // static entries defined in charmap.c
-          if ((_qwerty_charmap.entries != android_charmaps[n]->entries) &&
-              (_qwerty2_charmap.entries != android_charmaps[n]->entries)) {
-              AFREE((void*)android_charmaps[n]->entries);
-          }
-      }
-      AFREE(android_charmaps);
-  }
+    if (android_charmap != &_default_charmap)
+        AFREE((void*)android_charmap->entries);
 }
 
 const AKeyCharmap*
 android_get_charmap_by_name(const char* name) {
-    int nn;
-
     if (name != NULL) {
-        // Find charmap by its name in the array of available charmaps.
-        for (nn = 0; nn < android_charmap_count; nn++) {
-            if (!strcmp(android_charmaps[nn]->name, name)) {
-                return android_charmaps[nn];
-            }
-        }
+        if (!strcmp(android_charmap->name, name))
+            return android_charmap;
+        if (!strcmp(_default_charmap.name, name))
+            return &_default_charmap;
     }
     return NULL;
 }
 
-const AKeyCharmap*
-android_get_charmap_by_index(unsigned int index) {
-    return index < android_charmap_count ? android_charmaps[index] : NULL;
-}
-
 int
 android_charmap_reverse_map_unicode(const AKeyCharmap* cmap,
                                     unsigned int unicode,
@@ -825,14 +727,17 @@
     return 0;
 }
 
-const AKeyCharmap*
-android_get_default_charmap(void)
+const AKeyCharmap* android_get_default_charmap(void)
 {
-    return android_get_charmap_by_index(0);
+    return &_default_charmap;
 }
 
-const char*
-android_get_default_charmap_name(void)
+const AKeyCharmap* android_get_charmap(void)
 {
-    return android_get_default_charmap()->name;
+    return android_charmap;
+}
+
+const char* android_get_charmap_name(void)
+{
+    return android_get_charmap()->name;
 }
diff --git a/android/charmap.h b/android/charmap.h
index 6bf35c0..4c39495 100644
--- a/android/charmap.h
+++ b/android/charmap.h
@@ -51,7 +51,10 @@
                               char* charmap_name,
                               int max_len);
 
-/* Initialzes key charmap array.
+/* Gets a pointer to the default hard-coded charmap */
+const AKeyCharmap* android_get_default_charmap(void);
+
+/* Parse a charmap file and add it to our list.
  * Key charmap array always contains two maps: one for qwerty, and
  * another for qwerty2 keyboard layout. However, a custom layout can
  * be requested with -charmap option. In tha case kcm_file_path
@@ -61,24 +64,21 @@
  * kcm_file_path is NULL and final key charmap array will contain only
  * two default entries.
  * Returns a zero value on success, or -1 on failure.
-*/
+ *
+ * Note: on success, the charmap will be returned by android_get_charmap()
+ */
 int android_charmap_setup(const char* kcm_file_path);
 
 /* Cleanups initialization performed in android_charmap_setup routine. */
 void android_charmap_done(void);
 
 /* Gets charmap descriptor by its name.
- * This routine finds and returns pointer to a descriptor in the array of
- * charmaps that matches given name. If no such descriptor has been found, this
- * routine returns NULL.
+ * This routine tries to find a charmap by name. This will compare the
+ * name to the default charmap's name, or any charmap loaded with
+ * android_charmap_setup(). Returns NULL on failure.
  */
 const AKeyCharmap* android_get_charmap_by_name(const char* name);
 
-/* Gets charmap descriptor by its index in the array of charmaps.
- * If index is greater than charmap array size, this routine returns NULL.
- */
-const AKeyCharmap* android_get_charmap_by_index(unsigned int index);
-
 /* Maps given unicode key character into a keycode and adds mapped keycode into
  * keycode array. This routine uses charmap passed as cmap parameter to do the
  * translation, and 'down' parameter to generate appropriate ('down' or 'up')
@@ -90,10 +90,15 @@
                                     int  down,
                                     AKeycodeBuffer* keycodes);
 
-/* Gets default charmap (index 0) */
-const AKeyCharmap* android_get_default_charmap(void);
+/* Return a pointer to the active charmap. If android_charmap_setup() was
+ * called succesfully, this corresponds to the newly loaded charmap.
+ *
+ * Otherwise, return a pointer to the default charmap.
+ */
+const AKeyCharmap* android_get_charmap(void);
 
-/* Gets name of the default charmap (index 0) */
-const char* android_get_default_charmap_name(void);
+/* Return the name of the charmap to be used. Same as
+ * android_get_charmap()->name */
+const char* android_get_charmap_name(void);
 
 #endif /* _android_charmap_h */
diff --git a/android/console.c b/android/console.c
index a44d831..96cc005 100644
--- a/android/console.c
+++ b/android/console.c
@@ -1968,8 +1968,8 @@
         return -1;
     }
 
-    /* Get default charmap. */
-    charmap = android_get_default_charmap();
+    /* Get active charmap. */
+    charmap = android_get_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/skin/keyboard.c b/android/skin/keyboard.c
index 8102e33..3ee3366 100644
--- a/android/skin/keyboard.c
+++ b/android/skin/keyboard.c
@@ -25,6 +25,7 @@
 #  define  D(...)  ((void)0)
 #endif
 
+#define DEFAULT_ANDROID_CHARMAP  "qwerty2"
 
 /** LAST PRESSED KEYS
  ** a small buffer of last pressed keys, this is used to properly
@@ -77,7 +78,7 @@
     if (keyboard && keyboard->charmap)
         return keyboard->charmap->name;
 
-    return "qwerty";
+    return DEFAULT_ANDROID_CHARMAP;
 }
 
 void
@@ -520,8 +521,8 @@
 
     kb->charmap = android_get_charmap_by_name(charmap_name);
     if (!kb->charmap) {
-        // Charmap name was not found. Default to the first charmap in the array.
-        kb->charmap = android_get_charmap_by_index(0);
+        // Charmap name was not found. Default to "qwerty2" */
+        kb->charmap = android_get_charmap_by_name(DEFAULT_ANDROID_CHARMAP);
         fprintf(stderr, "### warning, skin requires unknown '%s' charmap, reverting to '%s'\n",
                 charmap_name, kb->charmap->name );
     }
@@ -540,7 +541,7 @@
 SkinKeyboard*
 skin_keyboard_create_from_aconfig( AConfig*  aconfig, int  use_raw_keys )
 {
-    const char*    charmap_name = "qwerty";
+    const char*    charmap_name = DEFAULT_ANDROID_CHARMAP;
     AConfig*       node = aconfig_find( aconfig, "keyboard" );
     if (node != NULL) {
         charmap_name = aconfig_str(node, "charmap", charmap_name);