diff options
-rw-r--r-- | android/avd/hw-config.c | 20 | ||||
-rw-r--r-- | android/avd/hw-config.h | 7 | ||||
-rw-r--r-- | android/main-ui.c | 26 | ||||
-rw-r--r-- | android/main.c | 26 | ||||
-rw-r--r-- | android/utils/ini.c | 143 | ||||
-rw-r--r-- | android/utils/ini.h | 19 | ||||
-rw-r--r-- | android/utils/system.h | 13 |
7 files changed, 213 insertions, 41 deletions
diff --git a/android/avd/hw-config.c b/android/avd/hw-config.c index 2362b59..b449bb3 100644 --- a/android/avd/hw-config.c +++ b/android/avd/hw-config.c @@ -37,3 +37,23 @@ androidHwConfig_read( AndroidHwConfig* config, return 0; } + +int +androidHwConfig_write( AndroidHwConfig* config, + IniFile* ini ) +{ + if (ini == NULL) + return -1; + + /* use the magic of macros to implement the hardware configuration loaded */ + +#define HWCFG_BOOL(n,s,d,a,t) iniFile_setBoolean(ini, s, config->n); +#define HWCFG_INT(n,s,d,a,t) iniFile_setInteger(ini, s, config->n); +#define HWCFG_STRING(n,s,d,a,t) iniFile_setValue(ini, s, config->n); +#define HWCFG_DOUBLE(n,s,d,a,t) iniFile_setDouble(ini, s, config->n); +#define HWCFG_DISKSIZE(n,s,d,a,t) iniFile_setDiskSize(ini, s, config->n); + +#include "android/avd/hw-config-defs.h" + + return 0; +} diff --git a/android/avd/hw-config.h b/android/avd/hw-config.h index 05eb828..c79ccad 100644 --- a/android/avd/hw-config.h +++ b/android/avd/hw-config.h @@ -43,4 +43,11 @@ typedef struct { int androidHwConfig_read( AndroidHwConfig* hwConfig, IniFile* configFile ); +/* Write a hardware configuration to a config file object. + * Returns 0 in case of success. Note that any value that is set to the + * default will not bet written. + */ +int androidHwConfig_write( AndroidHwConfig* hwConfig, + IniFile* configFile ); + #endif /* _ANDROID_AVD_HW_CONFIG_H */ diff --git a/android/main-ui.c b/android/main-ui.c index ac5bfbe..541d83b 100644 --- a/android/main-ui.c +++ b/android/main-ui.c @@ -48,6 +48,7 @@ #include "android/utils/bufprint.h" #include "android/utils/dirscanner.h" #include "android/utils/path.h" +#include "android/utils/tempfile.h" #include "android/cmdline-option.h" #include "android/help.h" @@ -1944,6 +1945,31 @@ int main(int argc, char **argv) } args[n] = 0; + /* Generate a temporary hardware.ini for this AVD. The real hardware + * configuration is ususally stored in several files, e.g. the AVD's + * config.ini plus the skin-specific hardware.ini. + * + * The new temp file will group all definitions and will be used to + * launch the core with the -android-hw <file> option. + */ + { + TempFile* tempHw = tempfile_create(); + if (tempHw == NULL) { + derror("Could not create temporary hardware.ini: %s", strerror(errno)); + exit(2); + } + + const char* tempHwPath = tempfile_path(tempHw); + IniFile* hwIni = iniFile_newFromMemory("", NULL); + androidHwConfig_write(hw, hwIni); + if (iniFile_saveToFile(hwIni, tempHwPath) < 0) { + derror("Could not write temporary hardware.ini: %s", tempHwPath); + exit(2); + } + args[n++] = "-android-hw"; + args[n++] = strdup(tempHwPath); + } + if(VERBOSE_CHECK(init)) { int i; printf("QEMU options list:\n"); diff --git a/android/main.c b/android/main.c index 793fa23..d6c2d8d 100644 --- a/android/main.c +++ b/android/main.c @@ -49,6 +49,7 @@ #include "android/utils/dirscanner.h" #include "android/utils/path.h" #include "android/utils/timezone.h" +#include "android/utils/tempfile.h" #include "android/cmdline-option.h" #include "android/help.h" @@ -1717,6 +1718,31 @@ int main(int argc, char **argv) } args[n] = 0; + /* Generate a temporary hardware.ini for this AVD. The real hardware + * configuration is ususally stored in several files, e.g. the AVD's + * config.ini plus the skin-specific hardware.ini. + * + * The new temp file will group all definitions and will be used to + * launch the core with the -android-hw <file> option. + */ + { + TempFile* tempHw = tempfile_create(); + if (tempHw == NULL) { + derror("Could not create temporary hardware.ini: %s", strerror(errno)); + exit(2); + } + + const char* tempHwPath = tempfile_path(tempHw); + IniFile* hwIni = iniFile_newFromMemory("", NULL); + androidHwConfig_write(hw, hwIni); + if (iniFile_saveToFile(hwIni, tempHwPath) < 0) { + derror("Could not write temporary hardware.ini: %s", tempHwPath); + exit(2); + } + args[n++] = "-android-hw"; + args[n++] = strdup(tempHwPath); + } + if(VERBOSE_CHECK(init)) { int i; printf("QEMU options list:\n"); diff --git a/android/utils/ini.c b/android/utils/ini.c index 317d233..1a1449c 100644 --- a/android/utils/ini.c +++ b/android/utils/ini.c @@ -62,8 +62,33 @@ iniFile_alloc( void ) } static void -iniFile_addPair( IniFile* i, const char* key, int keyLen, - const char* value, int valueLen ) +iniPair_init( IniPair* pair, const char* key, int keyLen, + const char* value, int valueLen ) +{ + AARRAY_NEW(pair->key, keyLen + valueLen + 2); + memcpy(pair->key, key, keyLen); + pair->key[keyLen] = 0; + + pair->value = pair->key + keyLen + 1; + memcpy(pair->value, value, valueLen); + pair->value[valueLen] = 0; +} + +static void +iniPair_replaceValue( IniPair* pair, const char* value ) +{ + char* key = pair->key; + int keyLen = strlen(key); + int valueLen = strlen(value); + + iniPair_init(pair, key, keyLen, value, valueLen); + AFREE(key); +} + +static void +iniFile_addPair( IniFile* i, + const char* key, int keyLen, + const char* value, int valueLen ) { IniPair* pair; @@ -76,53 +101,39 @@ iniFile_addPair( IniFile* i, const char* key, int keyLen, } pair = i->pairs + i->numPairs; - - AARRAY_NEW(pair->key, keyLen + valueLen + 2); - memcpy(pair->key, key, keyLen); - pair->key[keyLen] = 0; - - pair->value = pair->key + keyLen + 1; - memcpy(pair->value, value, valueLen); - pair->value[valueLen] = 0; + iniPair_init(pair, key, keyLen, value, valueLen); i->numPairs += 1; } -const char* -iniFile_getValue( IniFile* i, const char* key ) +static IniPair* +iniFile_getPair( IniFile* i, const char* key ) { if (i && key) { int nn; for (nn = 0; nn < i->numPairs; nn++) { if (!strcmp(i->pairs[nn].key,key)) - return i->pairs[nn].value; + return &i->pairs[nn]; } } return NULL; } -int -iniFile_getPairCount( IniFile* i ) +const char* +iniFile_getValue( IniFile* i, const char* key ) { - return i ? i->numPairs : 0; + IniPair* pair = iniFile_getPair(i, key); + if (pair) + return pair->value; + else + return NULL; } -void -iniFile_getPair( IniFile* i, - int index, - const char* *pKey, - const char* *pValue ) +int +iniFile_getPairCount( IniFile* i ) { - const char* key = NULL; - const char* value = NULL; - - if (i && index >= 0 && index < i->numPairs) { - key = i->pairs[index].key; - value = i->pairs[index].value; - } - *pKey = key; - *pValue = value; + return i ? i->numPairs : 0; } /* NOTE: we avoid using <ctype.h> functions to avoid locale-specific @@ -415,3 +426,75 @@ iniFile_getInt64( IniFile* f, const char* key, int64_t defaultValue ) return value; } +void +iniFile_setValue( IniFile* f, const char* key, const char* value ) +{ + IniPair* pair; + + if (f == NULL || key == NULL || value == NULL) + return; + + pair = iniFile_getPair(f, key); + if (pair != NULL) { + iniPair_replaceValue(pair, value); + } else { + iniFile_addPair(f, key, strlen(key), value, strlen(value)); + } +} + +void +iniFile_setInteger( IniFile* f, const char* key, int value ) +{ + char temp[16]; + snprintf(temp, sizeof temp, "%d", value); + iniFile_setValue(f, key, temp); +} + +void +iniFile_setInt64( IniFile* f, const char* key, int64_t value ) +{ + char temp[32]; + snprintf(temp, sizeof temp, "%" PRId64, value); + iniFile_setValue(f, key, temp); +} + +void +iniFile_setDouble( IniFile* f, const char* key, double value ) +{ + char temp[32]; + snprintf(temp, sizeof temp, "%g", value); + iniFile_setValue(f, key, temp); +} + +void +iniFile_setBoolean( IniFile* f, const char* key, int value ) +{ + iniFile_setValue(f, key, value ? "yes" : "no"); +} + +void +iniFile_setDiskSize( IniFile* f, const char* key, int64_t size ) +{ + char temp[32]; + int64_t divisor = 0; + char suffix = '\0'; + + if (size >= 0) { + if (!(size % 1024)) { + suffix = 'k'; + divisor = 1024; + } else if (!(size % 1024*1024)) { + divisor = 1024*1024; + suffix = 'm'; + } else if (!(size % 1024*1024*1024LL)) { + divisor = 1024*1024*1024; + suffix = 'g'; + } + } + if (divisor) { + snprintf(temp, sizeof temp, "%" PRId64 "%c", size/divisor, suffix); + } else { + snprintf(temp, sizeof temp, "%" PRId64, size); + } + iniFile_setValue(f, key, temp); +} diff --git a/android/utils/ini.h b/android/utils/ini.h index 83d2027..77d760f 100644 --- a/android/utils/ini.h +++ b/android/utils/ini.h @@ -43,17 +43,6 @@ void iniFile_free( IniFile* f ); /* returns the number of (key.value) pairs in an IniFile */ int iniFile_getPairCount( IniFile* f ); -/* return a specific (key,value) pair from an IniFile. - * if the index is not correct, both '*pKey' and '*pValue' will be - * set to NULL. - * - * you should probably use iniFile_getValue() and its variants instead - */ -void iniFile_getPair( IniFile* f, - int index, - const char* *pKey, - const char* *pValue ); - /* returns the value of a given key from an IniFile. * NULL if the key is not assigned in the corresponding configuration file */ @@ -96,6 +85,14 @@ int iniFile_getBoolean( IniFile* f, const char* key, const char* defaultV */ int64_t iniFile_getDiskSize( IniFile* f, const char* key, const char* defaultValue ); +/* These functions are used to set values in an IniFile */ +void iniFile_setValue( IniFile* f, const char* key, const char* value ); +void iniFile_setInteger( IniFile* f, const char* key, int value ); +void iniFile_setInt64( IniFile* f, const char* key, int64_t value ); +void iniFile_setDouble( IniFile* f, const char* key, double value ); +void iniFile_setBoolean( IniFile* f, const char* key, int value ); +void iniFile_setDiskSize( IniFile* f, const char* key, int64_t size ); + /* */ #endif /* _ANDROID_UTILS_INI_H */ diff --git a/android/utils/system.h b/android/utils/system.h index 5053786..c8163c6 100644 --- a/android/utils/system.h +++ b/android/utils/system.h @@ -14,6 +14,7 @@ #include <string.h> #include <stdint.h> +#include <inttypes.h> /* for PRId64 et al. */ #include "android/utils/assert.h" /* internal helpers */ @@ -161,6 +162,18 @@ extern void restore_sigalrm( signal_state_t *state ); extern void sleep_ms( int timeout ); +/** FORMATTING int64_t in printf() statements + ** + ** Normally defined in <inttypes.h> except on Windows and maybe others. + **/ + +#ifndef PRId64 +# define PRId64 "lld" +#endif +#ifndef PRIx64 +# define PRIx64 "llx" +#endif + /* */ #endif /* _ANDROID_UTILS_SYSTEM_H */ |