android_jni.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. static char externalStoragePath[1024];
  13. static char midiInterFile[1024];
  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, sizeof(externalStoragePath) - 8);
  149. strncat(externalStoragePath, "/sdlpal/", 8);
  150. }
  151. /*
  152. * Class: io_github_sdlpal_PalActivity
  153. * Method: setMIDIInterFile
  154. * Signature: (Ljava/lang/String;)V
  155. */
  156. JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setMIDIInterFile(JNIEnv *env, jclass cls, jstring j_str)
  157. {
  158. jstring_to_utf8(env, j_str, midiInterFile, sizeof(externalStoragePath));
  159. LOGV("JNI got midi inter filename:%s", midiInterFile);
  160. }
  161. void* JNI_mediaplayer_load(const char *filename)
  162. {
  163. JNIEnv *env = getJNIEnv();
  164. jclass clazz = (*env)->FindClass(env, "io/github/sdlpal/PalActivity");
  165. jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "JNI_mediaplayer_load", "(Ljava/lang/String;)Landroid/media/MediaPlayer;");
  166. jobject player = (*env)->CallStaticObjectMethod(env, clazz, mid, jstring_from_utf8(env, filename));
  167. return (*env)->NewGlobalRef(env, player);
  168. }
  169. void JNI_mediaplayer_free(void *player)
  170. {
  171. (*env)->DeleteGlobalRef(env, (jobject)player);
  172. }
  173. void JNI_mediaplayer_play(void *player, int looping)
  174. {
  175. JNIEnv *env = getJNIEnv();
  176. jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
  177. (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "setLooping", "(Z)V"), looping ? JNI_TRUE : JNI_FALSE);
  178. (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "start", "()V"));
  179. }
  180. void JNI_mediaplayer_stop(void *player)
  181. {
  182. JNIEnv *env = getJNIEnv();
  183. jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
  184. (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "stop", "()V"));
  185. }
  186. int JNI_mediaplayer_isplaying(void *player)
  187. {
  188. JNIEnv *env = getJNIEnv();
  189. jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
  190. return (*env)->CallBooleanMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "isPlaying", "()Z"));
  191. }
  192. void JNI_mediaplayer_setvolume(void *player, int volume)
  193. {
  194. float vol = (float)volume / 127.0f;
  195. JNIEnv *env = getJNIEnv();
  196. jclass clazz = (*env)->FindClass(env, "android/media/MediaPlayer");
  197. return (*env)->CallVoidMethod(env, (jobject)player, (*env)->GetMethodID(env, clazz, "setVolume", "(FF)V"), vol, vol);
  198. }
  199. LPCSTR
  200. UTIL_BasePath(
  201. VOID
  202. )
  203. {
  204. return externalStoragePath;
  205. }
  206. LPCSTR
  207. UTIL_SavePath(
  208. VOID
  209. )
  210. {
  211. return externalStoragePath;
  212. }
  213. BOOL
  214. UTIL_GetScreenSize(
  215. DWORD *pdwScreenWidth,
  216. DWORD *pdwScreenHeight
  217. )
  218. {
  219. *pdwScreenWidth = 640;
  220. *pdwScreenHeight = 400;
  221. return TRUE;
  222. }
  223. BOOL
  224. UTIL_IsAbsolutePath(
  225. LPCSTR lpszFileName
  226. )
  227. {
  228. return FALSE;
  229. }
  230. INT
  231. UTIL_Platform_Init(
  232. int argc,
  233. char* argv[]
  234. )
  235. {
  236. gConfig.fLaunchSetting = FALSE;
  237. return 0;
  238. }
  239. VOID
  240. UTIL_Platform_Quit(
  241. VOID
  242. )
  243. {
  244. }