Home | History | Annotate | Download | only in avd
      1 /* Copyright (C) 2008 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 #include "android/avd/hw-config.h"
     13 #include "android/utils/ini.h"
     14 #include "android/utils/system.h"
     15 #include <string.h>
     16 #include <stdlib.h>
     17 
     18 
     19 /* the global variable containing the hardware config for this device */
     20 AndroidHwConfig   android_hw[1];
     21 
     22 static int
     23 stringToBoolean( const char* value )
     24 {
     25     if (!strcmp(value,"1")    ||
     26         !strcmp(value,"yes")  ||
     27         !strcmp(value,"YES")  ||
     28         !strcmp(value,"true") ||
     29         !strcmp(value,"TRUE"))
     30     {
     31         return 1;
     32     }
     33     else
     34         return 0;
     35 }
     36 
     37 static int64_t
     38 diskSizeToInt64( const char* diskSize )
     39 {
     40     char*   end;
     41     int64_t value;
     42 
     43     value = strtoll(diskSize, &end, 10);
     44     if (*end == 'k' || *end == 'K')
     45         value *= 1024ULL;
     46     else if (*end == 'm' || *end == 'M')
     47         value *= 1024*1024ULL;
     48     else if (*end == 'g' || *end == 'G')
     49         value *= 1024*1024*1024ULL;
     50 
     51     return value;
     52 }
     53 
     54 
     55 void
     56 androidHwConfig_init( AndroidHwConfig*  config,
     57                       int               apiLevel )
     58 {
     59 #define   HWCFG_BOOL(n,s,d,a,t)       config->n = stringToBoolean(d);
     60 #define   HWCFG_INT(n,s,d,a,t)        config->n = d;
     61 #define   HWCFG_STRING(n,s,d,a,t)     config->n = ASTRDUP(d);
     62 #define   HWCFG_DOUBLE(n,s,d,a,t)     config->n = d;
     63 #define   HWCFG_DISKSIZE(n,s,d,a,t)   config->n = diskSizeToInt64(d);
     64 
     65 #include "android/avd/hw-config-defs.h"
     66 
     67     /* Special case for hw.keyboard.lid, we need to set the
     68      * default to FALSE for apiLevel >= 12. This allows platform builds
     69      * to get correct orientation emulation even if they don't bring
     70      * a custom hardware.ini
     71      */
     72     if (apiLevel >= 12) {
     73         config->hw_keyboard_lid = 0;
     74     }
     75 }
     76 
     77 int
     78 androidHwConfig_read( AndroidHwConfig*  config,
     79                       IniFile*          ini )
     80 {
     81     if (ini == NULL)
     82         return -1;
     83 
     84     /* use the magic of macros to implement the hardware configuration loaded */
     85 
     86 #define   HWCFG_BOOL(n,s,d,a,t)       if (iniFile_getValue(ini, s)) { config->n = iniFile_getBoolean(ini, s, d); }
     87 #define   HWCFG_INT(n,s,d,a,t)        if (iniFile_getValue(ini, s)) { config->n = iniFile_getInteger(ini, s, d); }
     88 #define   HWCFG_STRING(n,s,d,a,t)     if (iniFile_getValue(ini, s)) { AFREE(config->n); config->n = iniFile_getString(ini, s, d); }
     89 #define   HWCFG_DOUBLE(n,s,d,a,t)     if (iniFile_getValue(ini, s)) { config->n = iniFile_getDouble(ini, s, d); }
     90 #define   HWCFG_DISKSIZE(n,s,d,a,t)   if (iniFile_getValue(ini, s)) { config->n = iniFile_getDiskSize(ini, s, d); }
     91 
     92 #include "android/avd/hw-config-defs.h"
     93 
     94     return 0;
     95 }
     96 
     97 int
     98 androidHwConfig_write( AndroidHwConfig* config,
     99                        IniFile*         ini )
    100 {
    101     if (ini == NULL)
    102         return -1;
    103 
    104     /* use the magic of macros to implement the hardware configuration loaded */
    105 
    106 #define   HWCFG_BOOL(n,s,d,a,t)       iniFile_setBoolean(ini, s, config->n);
    107 #define   HWCFG_INT(n,s,d,a,t)        iniFile_setInteger(ini, s, config->n);
    108 #define   HWCFG_STRING(n,s,d,a,t)     iniFile_setValue(ini, s, config->n);
    109 #define   HWCFG_DOUBLE(n,s,d,a,t)     iniFile_setDouble(ini, s, config->n);
    110 #define   HWCFG_DISKSIZE(n,s,d,a,t)   iniFile_setDiskSize(ini, s, config->n);
    111 
    112 #include "android/avd/hw-config-defs.h"
    113 
    114     return 0;
    115 }
    116 
    117 void
    118 androidHwConfig_done( AndroidHwConfig* config )
    119 {
    120 #define   HWCFG_BOOL(n,s,d,a,t)       config->n = 0;
    121 #define   HWCFG_INT(n,s,d,a,t)        config->n = 0;
    122 #define   HWCFG_STRING(n,s,d,a,t)     AFREE(config->n);
    123 #define   HWCFG_DOUBLE(n,s,d,a,t)     config->n = 0.0;
    124 #define   HWCFG_DISKSIZE(n,s,d,a,t)   config->n = 0;
    125 
    126 #include "android/avd/hw-config-defs.h"
    127 }
    128 
    129 int
    130 androidHwConfig_isScreenNoTouch( AndroidHwConfig* config )
    131 {
    132     return strcmp(config->hw_screen, "no-touch") == 0;
    133 }
    134 
    135 int
    136 androidHwConfig_isScreenTouch( AndroidHwConfig* config )
    137 {
    138     return strcmp(config->hw_screen, "touch") == 0;
    139 }
    140 
    141 int
    142 androidHwConfig_isScreenMultiTouch( AndroidHwConfig* config )
    143 {
    144     return strcmp(config->hw_screen, "multi-touch") == 0;
    145 }
    146