Android : copy stuff out of APK for ease of use, and pass data_dir to native side

- Also makes data_dir more useful for various platforms
This commit is contained in:
Aaron Culliney 2015-02-23 11:19:41 -08:00
parent 792c0a0b08
commit 0c3121ac65
7 changed files with 125 additions and 13 deletions

View File

@ -25,8 +25,13 @@ int main(int argc, char **argv) {
#else
void Java_org_deadc0de_apple2_Apple2Activity_nativeOnCreate(JNIEnv *env, jobject obj) {
LOG("%s", "native onCreate...");
void Java_org_deadc0de_apple2_Apple2Activity_nativeOnCreate(JNIEnv *env, jobject obj, jstring j_dataDir) {
const char *dataDir = (*env)->GetStringUTFChars(env, j_dataDir, 0);
data_dir = strdup(dataDir);
(*env)->ReleaseStringUTFChars(env, j_dataDir, dataDir);
LOG("nativeOnCreate(%s)...", data_dir);
#if !TESTING
// TODO ...

View File

@ -31,5 +31,5 @@ APPLE2_MAIN_SRC = \
$(APPLE2_SRC_PATH)/timing.c $(APPLE2_SRC_PATH)/zlib-helpers.c $(APPLE2_SRC_PATH)/joystick.c $(APPLE2_SRC_PATH)/keys.c \
$(APPLE2_SRC_PATH)/disk.c $(APPLE2_SRC_PATH)/cpu-supp.c
APPLE2_BASE_CFLAGS := -DAPPLE2IX=1 -DVIDEO_OPENGL=1 -DDEBUGGER=1 -DPACKAGE_NAME="\"$(PACKAGE_NAME)\"" -DCONFIG_DATADIR="\"/data/data/$(PACKAGE_IDENTIFIER)\"" -std=gnu11 -I$(APPLE2_SRC_PATH)
APPLE2_BASE_CFLAGS := -DAPPLE2IX=1 -DVIDEO_OPENGL=1 -DDEBUGGER=1 -std=gnu11 -I$(APPLE2_SRC_PATH)

View File

@ -1,5 +1,5 @@
/*
* Apple // emulator for *nix
* Apple // emulator for *nix
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
@ -12,23 +12,123 @@
package org.deadc0de.apple2;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileOutputStream;
public class Apple2Activity extends Activity {
private final static String TAG = "Apple2Activity";
private final static int BUF_SZ = 4096;
private final static String PREFS_CONFIGURED = "prefs_configured";
static {
System.loadLibrary("apple2ix");
}
}
private native void nativeOnCreate();
private native void nativeOnCreate(String dataDir);
private native void nativeOnResume();
private native void nativeOnPause();
// HACK NOTE 2015/02/22 : Apparently native code cannot easily access stuff in the APK ... so copy various resources
// out of the APK and into the /data/data/... for ease of access. Because this is FOSS software we don't care about
// these assets
private String firstTimeInitialization() {
String dataDir = null;
try {
PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
dataDir = pi.applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, ""+e);
System.exit(1);
}
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
if (settings.getBoolean(PREFS_CONFIGURED, false)) {
return dataDir;
}
Log.d(TAG, "First time copying stuff-n-things out of APK for ease-of-NDK access...");
try {
_copyFile(dataDir, "shaders", "Basic.vsh");
_copyFile(dataDir, "shaders", "Basic.fsh");
_copyFile(dataDir, "disks", "blank.dsk");
_copyFile(dataDir, "disks", "blank.nib");
_copyFile(dataDir, "disks", "blank.po");
_copyFile(dataDir, "disks", "etc.dsk");
_copyFile(dataDir, "disks", "flapple140.po");
_copyFile(dataDir, "disks", "mystery.dsk");
_copyFile(dataDir, "disks", "ProDOS.dsk");
_copyFile(dataDir, "disks", "speedtest.dsk");
_copyFile(dataDir, "disks", "testdisplay1.dsk");
_copyFile(dataDir, "disks", "testvm1.dsk");
} catch (IOException e) {
Log.e(TAG, "problem copying resources : "+e);
System.exit(1);
}
Log.d(TAG, "Saving default preferences");
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(PREFS_CONFIGURED, true);
editor.commit();
return dataDir;
}
private void _copyFile(String dataDir, String subdir, String assetName)
throws IOException
{
String outputPath = dataDir+File.separator+subdir;
Log.d(TAG, "Copying "+subdir+File.separator+assetName+" to "+outputPath+File.separator+assetName+" ...");
new File(outputPath).mkdirs();
InputStream is = getAssets().open(subdir+File.separator+assetName);
if (is == null) {
Log.e(TAG, "inputstream is null");
}
File file = new File(outputPath+File.separator+assetName);
file.setWritable(true);
FileOutputStream os = new FileOutputStream(file);
if (os == null) {
Log.e(TAG, "outputstream is null");
}
byte[] buf = new byte[BUF_SZ];
while (true) {
int len = is.read(buf, 0, BUF_SZ);
if (len < 0) {
break;
}
os.write(buf, 0, len);
}
os.flush();
os.close();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nativeOnCreate();
Log.e(TAG, "onCreate()");
String dataDir = firstTimeInitialization();
nativeOnCreate(dataDir);
TextView tv = new TextView(this);
tv.setText("Hello Apple2!");
setContentView(tv);

View File

@ -28,10 +28,18 @@ FILE *error_log = NULL;
int sound_volume = 2;
bool is_headless = false;
color_mode_t color_mode = COLOR;
const char *data_dir = NULL;
__attribute__((constructor))
static void _init_common() {
error_log = stderr;
#if defined(CONFIG_DATADIR)
data_dir = strdup(CONFIG_DATADIR "/" PACKAGE_NAME);
#elif defined(ANDROID)
// data_dir is set up in JNI nativeOnCreate()
#else
#error "Specify a CONFIG_DATADIR and PACKAGE_NAME"
#endif
}
GLUE_BANK_READ(read_ram_bank,base_d000_rd)
@ -443,7 +451,6 @@ void c_initialize_tables() {
void c_initialize_apple_ii_memory()
{
FILE *f;
int i;
for (i = 0; i < 0x10000; i++)

View File

@ -19,6 +19,8 @@
#ifndef __ASSEMBLER__
extern const char *data_dir;
/* Text characters */
extern const unsigned char ucase_glyphs[0x200];
extern const unsigned char lcase_glyphs[0x100];

View File

@ -162,7 +162,7 @@ int test_setup_boot_disk(const char *fileName, int readonly) {
}
CFRELEASE(filePath);
#else
asprintf(&disk, "./disks/%s", fileName);
asprintf(&disk, "%s/disks/%s", data_dir, fileName);
#endif
if (c_new_diskette_6(0, disk, readonly)) {
int len = strlen(disk);

View File

@ -498,17 +498,15 @@ static demoSource *_create_shader_source(const char *fileName) {
CFRELEASE(fileURL);
src = srcLoadSource(CFStringGetCStringPtr(filePath, CFStringGetSystemEncoding()));
CFRELEASE(filePath);
#elif defined(CONFIG_DATADIR)
#else
char *filePath = NULL;
asprintf(&filePath, "%s/%s/shaders/%s", CONFIG_DATADIR, PACKAGE_NAME, fileName);
asprintf(&filePath, "%s/shaders/%s", data_dir, fileName);
if (filePath) {
src = srcLoadSource(filePath);
free(filePath);
} else {
ERRLOG("OOPS Could not load shader from %s (%s)", filePath, fileName);
}
#else
#error need to specify a DATADIR for shader files, etc
#endif
return src;
}