android_jni.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <jni.h>
  2. #include <android/log.h>
  3. #define TAG "sdlpal-jni"
  4. #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG,__VA_ARGS__)
  5. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , TAG,__VA_ARGS__)
  6. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO , TAG,__VA_ARGS__)
  7. #define LOGW(...) __android_log_print(ANDROID_LOG_WARN , TAG,__VA_ARGS__)
  8. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , TAG,__VA_ARGS__)
  9. #include "palcommon.h"
  10. #include "global.h"
  11. #include "palcfg.h"
  12. char externalStoragePath[255];
  13. char midiInterFile[255];
  14. static int jstring_to_utf8(JNIEnv *env, jstring j_str, char *buffer, int capacity)
  15. {
  16. jsize pos = 0, length = (*env)->GetStringLength(env, j_str);
  17. const jchar * const base = (*env)->GetStringCritical(env, j_str, NULL);
  18. if (base == NULL)
  19. {
  20. return 0;
  21. }
  22. // Convert at char boundary, no incomplete output can be generated
  23. for(const jchar *str = base;pos < length && pos < capacity - 1;str++)
  24. {
  25. if (*str > 4095 && pos < capacity - 3)
  26. {
  27. buffer[pos++] = 0xe0 | (*str >> 12);
  28. buffer[pos++] = 0x80 | ((*str >> 6) & 0x3f);
  29. buffer[pos++] = 0x80 | (*str & 0x3f);
  30. }
  31. else if (*str > 127 && pos < capacity - 2)
  32. {
  33. buffer[pos++] = 0xc0 | (*str >> 6);
  34. buffer[pos++] = 0x80 | (*str & 0x3f);
  35. }
  36. else if (*str <= 127)
  37. {
  38. buffer[pos++] = *str;
  39. }
  40. else
  41. {
  42. break;
  43. }
  44. }
  45. (*env)->ReleaseStringCritical(env, j_str, base);
  46. buffer[pos] = '\0';
  47. return pos;
  48. }
  49. static jstring jstring_from_utf8(JNIEnv *env, const char *str)
  50. {
  51. jstring retval = NULL;
  52. jchar *temp = NULL;
  53. int wlen = 0, len = strlen(str);
  54. // Count length of the UTF-8 string, stop at any error
  55. for(int i = 0, state = 0, count = 0;i < len;i++)
  56. {
  57. if (state == 0)
  58. {
  59. if (str[i] < 127)
  60. {
  61. wlen++;
  62. }
  63. else if (str[i] >= 0xc0 && str[i] < 0xf0)
  64. {
  65. state = 1;
  66. count = (str[i] >> 5) - 5;
  67. }
  68. else
  69. {
  70. break;
  71. }
  72. }
  73. else
  74. {
  75. if (str[i] >= 0x80 && str[i] < 0xc0)
  76. {
  77. if (count == 0)
  78. {
  79. state = 0;
  80. wlen++;
  81. }
  82. else
  83. {
  84. count--;
  85. }
  86. }
  87. else
  88. {
  89. break;
  90. }
  91. }
  92. }
  93. if (wlen == 0)
  94. {
  95. return (*env)->NewString(env, L"", 0);
  96. }
  97. temp = (jchar *)malloc(wlen * sizeof(jchar));
  98. for(int i = 0, j = 0;j < wlen;j++)
  99. {
  100. if (str[i] > 127)
  101. {
  102. // Trick here:
  103. // 2-byte form: 110x xxxx -> 0xxx xx000000
  104. // 3-byte form: 1110 xxxx -> 10xx xx000000
  105. temp[j] = (str[i++] & 0x3f) << 6;
  106. temp[j] |= str[i++] & 0x3f;
  107. if (temp[j] & 0x800)
  108. {
  109. // 3-byte form, the top-most bit will be dicarded during shift
  110. temp[j] <<= 6;
  111. temp[j] |= str[i++] & 0x3f;
  112. }
  113. }
  114. else
  115. {
  116. temp[j] = str[i++];
  117. }
  118. }
  119. retval = (*env)->NewString(env, temp, wlen);
  120. free(temp);
  121. return retval;
  122. }
  123. JavaVM *globalVM;
  124. jint JNI_OnLoad(JavaVM* vm, void* reserved)
  125. {
  126. JNIEnv* env;
  127. if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_2) != JNI_OK) {
  128. return -1;
  129. }
  130. globalVM = vm;
  131. return JNI_VERSION_1_2;
  132. }
  133. JNIEnv *getJNIEnv()
  134. {
  135. JNIEnv* env;
  136. if ((*globalVM)->GetEnv(globalVM, (void**)&env, JNI_VERSION_1_2) != JNI_OK) {
  137. return NULL;
  138. }
  139. return env;
  140. }
  141. /*
  142. * Class: io_github_sdlpal_PalActivity
  143. * Method: setExternalStorage
  144. * Signature: (Ljava/lang/String;)V
  145. */
  146. JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setExternalStorage(JNIEnv *env, jclass cls, jstring j_str)
  147. {
  148. jstring_to_utf8(env, j_str, externalStoragePath, 255 - 8);
  149. strncat(externalStoragePath, "/sdlpal/", 8);
  150. return;
  151. }
  152. /*
  153. * Class: io_github_sdlpal_PalActivity
  154. * Method: setMIDIInterFile
  155. * Signature: (Ljava/lang/String;)V
  156. */
  157. JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setMIDIInterFile(JNIEnv *env, jclass cls, jstring j_str)
  158. {
  159. jstring_to_utf8(env, j_str, midiInterFile, 255);
  160. LOGV("JNI got midi inter filename:%s", midiInterFile);
  161. return;
  162. }
  163. void JNI_mediaplayer_load(const char *filename)
  164. {
  165. JNIEnv *env = getJNIEnv();
  166. jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
  167. jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_load", "(Ljava/lang/String;)V");
  168. (*env)->CallStaticVoidMethod(env, clazz, mid, jstring_from_utf8(env, filename));
  169. }
  170. void JNI_mediaplayer_play()
  171. {
  172. JNIEnv *env = getJNIEnv();
  173. jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
  174. jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_play", "()V");
  175. (*env)->CallStaticVoidMethod(env, clazz, mid);
  176. }
  177. void JNI_mediaplayer_stop()
  178. {
  179. JNIEnv *env = getJNIEnv();
  180. jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
  181. jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_stop", "()V");
  182. (*env)->CallStaticVoidMethod(env, clazz, mid);
  183. }
  184. int JNI_mediaplayer_isplaying()
  185. {
  186. return 0;
  187. }
  188. void JNI_mediaplayer_setvolume(int volume)
  189. {
  190. }
  191. LPCSTR
  192. UTIL_BasePath(
  193. VOID
  194. )
  195. {
  196. return externalStoragePath;
  197. }
  198. LPCSTR
  199. UTIL_SavePath(
  200. VOID
  201. )
  202. {
  203. return externalStoragePath;
  204. }
  205. BOOL
  206. UTIL_GetScreenSize(
  207. DWORD *pdwScreenWidth,
  208. DWORD *pdwScreenHeight
  209. )
  210. {
  211. *pdwScreenWidth = 640;
  212. *pdwScreenHeight = 400;
  213. return TRUE;
  214. }
  215. BOOL
  216. UTIL_IsAbsolutePath(
  217. LPCSTR lpszFileName
  218. )
  219. {
  220. return FALSE;
  221. }
  222. INT
  223. UTIL_Platform_Init(
  224. int argc,
  225. char* argv[]
  226. )
  227. {
  228. gConfig.fLaunchSetting = FALSE;
  229. return 0;
  230. }
  231. VOID
  232. UTIL_Platform_Quit(
  233. VOID
  234. )
  235. {
  236. }