Copyright clean-up (part 1):
[AROS.git] / arch / all-android / bootstrap / main.c
blob2a5a0b98b638f221ac9fbdb300d54950150dc246
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <android/bitmap.h>
7 #include <sys/stat.h>
9 #include <dlfcn.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
15 #include <runtime.h>
17 #include "android.h"
18 #include "bootstrap.h"
20 #define D(x)
22 /* These variables are used by DisplayError() */
23 JNIEnv *Java_Env;
24 jobject Java_Object;
25 jmethodID DisplayError_mid;
26 jmethodID HandleExit_mid;
28 /* Dynamically loaded libjnigraphics.so functions */
29 void *libjnigraphics;
30 static int (*bm_GetInfo)(JNIEnv* env, jobject jbitmap, AndroidBitmapInfo* info);
31 static int (*bm_Lock)(JNIEnv* env, jobject jbitmap, void** addrPtr);
32 static int (*bm_Unlock)(JNIEnv* env, jobject jbitmap);
35 * This is the main bootstrap entry point.
36 * It parses the configuration file, prepares the environment and loads the kickstart.
37 * In Android it returns after that, allowing Java code to execute Kick() method,
38 * which actually runs the loaded kickstart.
39 * Kick() can be executed multiple times, this is how warm reboot works.
41 int Java_org_aros_bootstrap_AROSBootstrap_Load(JNIEnv* env, jobject this, jstring basedir)
43 jclass Java_Class;
44 jboolean is_copy;
45 const char *arospath;
46 int res;
48 Java_Env = env;
49 Java_Object = this;
50 Java_Class = (*env)->GetObjectClass(env, this);
52 DisplayError_mid = (*env)->GetMethodID(env, Java_Class, "DisplayError", "(Ljava/lang/String;)V");
53 HandleExit_mid = (*env)->GetMethodID(env, Java_Class, "HandleExit", "(I)V");
55 arospath = (*env)->GetStringUTFChars(env, basedir, &is_copy);
56 res = chdir(arospath);
58 if (res)
60 DisplayError("Failed to locate AROS root directory (%s): %s", arospath, strerror(errno));
61 (*env)->ReleaseStringUTFChars(env, basedir, arospath);
63 else
65 (*env)->ReleaseStringUTFChars(env, basedir, arospath);
66 res = bootstrap(0, NULL); /* We can't pass any arguments (yet) */
69 return res;
73 * Wrap a given memory region into ByteBuffer object.
74 * Needed for accessing AROS shared RAM.
75 * FIXME: It would be better to pass in longs here, however looks
76 * like Android gcc has a bug and misaligns arguments in this case. I got
77 * wrong values ('size' was actually 'addr' and 'addr' contained some weird garbage.
78 * Since Android is a 32-bit system, i hope ints are ok for now.
80 jobject Java_org_aros_bootstrap_AROSBootstrap_MapMemory(JNIEnv* env, jobject this, jint addr, jint size)
82 D(kprintf("[Bootstrap] Mapping %u bytes at %p\n", size, addr));
84 return (*env)->NewDirectByteBuffer(env, (void *)addr, size);
88 * Retrieve a string at a given memory address as array of bytes.
89 * FIXME: See above why srcAddr is jint.
92 jbyteArray Java_org_aros_bootstrap_AROSBootstrap_GetString(JNIEnv* env, jobject this, jint addr)
94 jsize size = strlen((void *)addr);
95 jbyteArray ret = (*env)->NewByteArray(env, size);
97 (*env)->SetByteArrayRegion(env, ret, 0, size - 1, (const jbyte *)addr);
99 return ret;
103 * Copy a given region from AROS displayable bitmap to Java bitmap object.
104 * FIXME: See above why srcAddr is jint.
106 jint Java_org_aros_bootstrap_AROSBootstrap_GetBitmap(JNIEnv* env, jobject this, jobject bitmap, jint srcAddr,
107 jint x, jint y, jint width, jint height, jint bytesPerLine)
109 void *src = (void *)srcAddr;
110 void *dest;
111 AndroidBitmapInfo bmdata;
112 int yc;
113 jint rc;
115 rc = bm_GetInfo(env, bitmap, &bmdata);
116 if (rc < 0)
118 D(kprintf("[Bootstrap] Failed to obtain information about bitmap %p\n", bitmap));
119 return rc;
122 rc = bm_Lock(env, bitmap, &dest);
123 if (rc < 0)
125 D(kprintf("[Bootstrap] Failed to lock bitmap %p\n", bitmap));
126 return rc;
129 /* This is ARGB bitmap, 4 bytes per pixel */
130 src += y * bytesPerLine + (x << 2);
131 dest += y * bmdata.stride + (x << 2);
132 width <<= 2;
134 for (yc = 0; yc < height; yc++)
136 memcpy(dest, src, width);
137 src += bytesPerLine;
138 dest += bmdata.stride;
141 return bm_Unlock(env, bitmap);
144 jint JNI_OnLoad(JavaVM *vm, void *reserved)
146 libjnigraphics = dlopen("libjnigraphics.so", RTLD_NOW);
147 D(kprintf("[Bootstrap] libjnigraphics.so handle: %p\n"));
149 if (libjnigraphics)
151 bm_GetInfo = dlsym(libjnigraphics, "AndroidBitmap_getInfo");
152 bm_Lock = dlsym(libjnigraphics, "AndroidBitmap_lockPixels");
153 bm_Unlock = dlsym(libjnigraphics, "AndroidBitmap_unlockPixels");
156 /* Dalvik wants a minimum of 1.4. Returning 1.1 causes exception. */
157 return JNI_VERSION_1_4;
160 jboolean Java_org_aros_bootstrap_AROSBootstrap_HaveNativeGraphics(JNIEnv* env, jobject this)
162 return libjnigraphics ? JNI_TRUE : JNI_FALSE;