/
ae_exports.cpp
291 lines (251 loc) · 11.5 KB
/
ae_exports.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Mupen64PlusAE, an N64 emulator for the Android platform
*
* Copyright (C) 2013 Paul Lamb
*
* This file is part of Mupen64PlusAE.
*
* Mupen64PlusAE is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Mupen64PlusAE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mupen64PlusAE. If
* not, see <http://www.gnu.org/licenses/>.
*
* Authors: Paul Lamb, littleguy77
*/
#include <stdlib.h>
#include <dlfcn.h>
#include "SDL.h"
#include "m64p_types.h"
#include "ae_imports.h"
#ifdef M64P_BIG_ENDIAN
#define sl(mot) mot
#else
#define sl(mot) (((mot & 0xFF) << 24) | ((mot & 0xFF00) << 8) | ((mot & 0xFF0000) >> 8) | ((mot & 0xFF000000) >> 24))
#endif
/*******************************************************************************
Variables used internally
*******************************************************************************/
// JNI objects
static JavaVM* mVm;
static void* mReserved;
// Library handles
static void *handleAEI; // libae-imports.so
static void *handleSDL; // libSDL2.so
static void *handleCore; // libmupen64plus-core.so
static void *handleFront; // libmupen64plus-ui-console.so
// Function types
typedef jint (*pJNI_OnLoad) (JavaVM* vm, void* reserved);
typedef int (*pAeiInit) (JNIEnv* env, jclass cls);
typedef int (*pSdlInit) (JNIEnv* env, jclass cls);
typedef void (*pSdlSetScreen) (int width, int height, Uint32 format);
typedef void (*pVoidFunc) ();
typedef m64p_error (*pCoreDoCommand) (m64p_command, int, void *);
typedef int (*pFrontMain) (int argc, char* argv[]);
// Function pointers
static pAeiInit aeiInit = NULL;
static pSdlInit sdlInit = NULL;
static pSdlSetScreen sdlSetScreen = NULL;
static pVoidFunc sdlMainReady = NULL;
static pCoreDoCommand coreDoCommand = NULL;
static pFrontMain frontMain = NULL;
/*******************************************************************************
Functions called automatically by JNI framework
*******************************************************************************/
extern jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
mVm = vm;
mReserved = reserved;
return JNI_VERSION_1_4;
}
/*******************************************************************************
Functions called by Java code
*******************************************************************************/
extern "C" DECLSPEC void SDLCALL Java_paulscode_android_mupen64plusae_jni_NativeExports_loadLibraries(JNIEnv* env, jclass cls, jstring jlibPath)
{
// Unload the libraries to ensure that static variables are re-initialized next time
LOGI("Unloading native libraries");
if (handleFront && !dlclose(handleFront))
LOGE("Failed to unload libmupen64plus-ui-console.so");
if (handleCore && !dlclose(handleCore))
LOGE("Failed to unload libmupen64plus-core.so");
if (handleSDL && !dlclose(handleSDL))
LOGE("Failed to unload libSDL2.so");
if (handleAEI && !dlclose(handleAEI))
LOGE("Failed to unload libae-imports.so");
// Construct the library paths
const char *libPath = env->GetStringUTFChars(jlibPath, 0);
char pathAEI[256];
char pathSDL[256];
char pathCore[256];
char pathFront[256];
sprintf(pathAEI, "%s/libae-imports.so", libPath);
sprintf(pathSDL, "%s/libSDL2.so", libPath);
sprintf(pathCore, "%s/libmupen64plus-core.so", libPath);
sprintf(pathFront, "%s/libmupen64plus-ui-console.so", libPath);
env->ReleaseStringUTFChars(jlibPath, libPath);
// Open shared libraries
LOGI("Loading native libraries");
handleAEI = dlopen(pathAEI, RTLD_NOW);
handleSDL = dlopen(pathSDL, RTLD_NOW);
handleCore = dlopen(pathCore, RTLD_NOW);
handleFront = dlopen(pathFront, RTLD_NOW);
// Make sure we don't have any typos
if (!handleAEI || !handleSDL || !handleCore || !handleFront)
{
LOGE("Could not load libraries: be sure the paths are correct");
}
// Find and call the JNI_OnLoad functions manually since we aren't loading the libraries from Java
pJNI_OnLoad JNI_OnLoad0 = (pJNI_OnLoad) dlsym(handleAEI, "JNI_OnLoad");
pJNI_OnLoad JNI_OnLoad1 = (pJNI_OnLoad) dlsym(handleSDL, "JNI_OnLoad");
JNI_OnLoad0(mVm, mReserved);
JNI_OnLoad1(mVm, mReserved);
JNI_OnLoad0 = NULL;
JNI_OnLoad1 = NULL;
// Find library functions
aeiInit = (pAeiInit) dlsym(handleAEI, "Android_JNI_InitImports");
sdlInit = (pSdlInit) dlsym(handleSDL, "SDL_Android_Init");
sdlSetScreen = (pSdlSetScreen) dlsym(handleSDL, "Android_SetScreenResolution");
sdlMainReady = (pVoidFunc) dlsym(handleSDL, "SDL_SetMainReady");
coreDoCommand = (pCoreDoCommand) dlsym(handleCore, "CoreDoCommand");
frontMain = (pFrontMain) dlsym(handleFront, "SDL_main");
// Make sure we don't have any typos
if (!aeiInit || !sdlInit || !sdlSetScreen || !sdlMainReady || !coreDoCommand || !frontMain)
{
LOGE("Could not load library functions: be sure they are named and typedef'd correctly");
}
}
extern "C" DECLSPEC void SDLCALL Java_paulscode_android_mupen64plusae_jni_NativeExports_unloadLibraries(JNIEnv* env, jclass cls)
{
// Nullify function pointers
aeiInit = NULL;
sdlInit = NULL;
sdlSetScreen = NULL;
sdlMainReady = NULL;
coreDoCommand = NULL;
frontMain = NULL;
}
extern "C" DECLSPEC jint SDLCALL Java_paulscode_android_mupen64plusae_jni_NativeExports_emuStart(JNIEnv* env, jclass cls, jstring juserDataPath, jstring juserCachePath, jobjectArray jargv)
{
// Define some environment variables needed by rice video plugin
const char *userDataPath = env->GetStringUTFChars(juserDataPath, 0);
const char *userCachePath = env->GetStringUTFChars(juserCachePath, 0);
setenv( "XDG_DATA_HOME", userDataPath, 1 );
setenv( "XDG_CACHE_HOME", userCachePath, 1 );
env->ReleaseStringUTFChars(juserDataPath, userDataPath);
env->ReleaseStringUTFChars(juserCachePath, userCachePath);
// Initialize dependencies
jclass nativeImports = env->FindClass("paulscode/android/mupen64plusae/jni/NativeImports");
jclass nativeSDL = env->FindClass("paulscode/android/mupen64plusae/jni/NativeSDL");
aeiInit(env, nativeImports);
sdlInit(env, nativeSDL);
sdlSetScreen(0, 0, SDL_PIXELFORMAT_RGB565);
sdlMainReady();
// Repackage the command-line args
int argc = env->GetArrayLength(jargv);
char **argv = (char **) malloc(sizeof(char *) * argc);
for (int i = 0; i < argc; i++)
{
jstring jarg = (jstring) env->GetObjectArrayElement(jargv, i);
const char *arg = env->GetStringUTFChars(jarg, 0);
argv[i] = strdup(arg);
env->ReleaseStringUTFChars(jarg, arg);
}
// Launch main emulator loop (continues until emuStop is called)
return frontMain(argc, argv);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuStop(JNIEnv* env, jclass cls)
{
if (coreDoCommand) coreDoCommand(M64CMD_STOP, 0, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuResume(JNIEnv* env, jclass cls)
{
if (coreDoCommand) coreDoCommand(M64CMD_RESUME, 0, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuPause(JNIEnv* env, jclass cls)
{
if (coreDoCommand) coreDoCommand(M64CMD_PAUSE, 0, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuAdvanceFrame(JNIEnv* env, jclass cls)
{
if (coreDoCommand) coreDoCommand(M64CMD_ADVANCE_FRAME, 0, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuSetSpeed(JNIEnv* env, jclass cls, jint percent)
{
int speed_factor = (int) percent;
if (coreDoCommand) coreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_SPEED_FACTOR, &speed_factor);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuSetFramelimiter(JNIEnv* env, jclass cls, jboolean enabled)
{
int e = enabled == JNI_TRUE ? 1 : 0;
if (coreDoCommand) coreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_SPEED_LIMITER, &e);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuSetSlot(JNIEnv* env, jclass cls, jint slotID)
{
if (coreDoCommand) coreDoCommand(M64CMD_STATE_SET_SLOT, (int) slotID, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuLoadSlot(JNIEnv* env, jclass cls)
{
if (coreDoCommand) coreDoCommand(M64CMD_STATE_LOAD, 0, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuSaveSlot(JNIEnv* env, jclass cls)
{
if (coreDoCommand) coreDoCommand(M64CMD_STATE_SAVE, 1, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuLoadFile(JNIEnv* env, jclass cls, jstring filename)
{
const char *nativeString = env->GetStringUTFChars(filename, 0);
if (coreDoCommand) coreDoCommand(M64CMD_STATE_LOAD, 0, (void *) nativeString);
env->ReleaseStringUTFChars(filename, nativeString);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuSaveFile(JNIEnv* env, jclass cls, jstring filename)
{
const char *nativeString = env->GetStringUTFChars(filename, 0);
if (coreDoCommand) coreDoCommand(M64CMD_STATE_SAVE, 1, (void *) nativeString);
env->ReleaseStringUTFChars(filename, nativeString);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuScreenshot(JNIEnv* env, jclass cls)
{
if (coreDoCommand) coreDoCommand(M64CMD_TAKE_NEXT_SCREENSHOT, 0, NULL);
}
extern "C" DECLSPEC void Java_paulscode_android_mupen64plusae_jni_NativeExports_emuGameShark(JNIEnv* env, jclass cls, jboolean pressed)
{
int p = pressed == JNI_TRUE ? 1 : 0;
if (coreDoCommand) coreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_INPUT_GAMESHARK, &p);
}
extern "C" DECLSPEC jint Java_paulscode_android_mupen64plusae_jni_NativeExports_emuGetState(JNIEnv* env, jclass cls)
{
int state = 0;
if (coreDoCommand) coreDoCommand(M64CMD_CORE_STATE_QUERY, M64CORE_EMU_STATE, &state);
if (state == M64EMU_STOPPED)
return (jint) 1;
else if (state == M64EMU_RUNNING)
return (jint) 2;
else if (state == M64EMU_PAUSED)
return (jint) 3;
else
return (jint) 0;
}
extern "C" DECLSPEC jint Java_paulscode_android_mupen64plusae_jni_NativeExports_emuGetSpeed(JNIEnv* env, jclass cls)
{
int speed = 100;
if (coreDoCommand) coreDoCommand(M64CMD_CORE_STATE_QUERY, M64CORE_SPEED_FACTOR, &speed);
return (jint) speed;
}
extern "C" DECLSPEC jboolean Java_paulscode_android_mupen64plusae_jni_NativeExports_emuGetFramelimiter(JNIEnv* env, jclass cls)
{
int e = 1;
if (coreDoCommand) coreDoCommand(M64CMD_CORE_STATE_QUERY, M64CORE_SPEED_LIMITER, &e);
return (jboolean) (e ? JNI_TRUE : JNI_FALSE);
}
extern "C" DECLSPEC jint Java_paulscode_android_mupen64plusae_jni_NativeExports_emuGetSlot(JNIEnv* env, jclass cls)
{
int slot = 0;
if (coreDoCommand) coreDoCommand(M64CMD_CORE_STATE_QUERY, M64CORE_SAVESTATE_SLOT, &slot);
return (jint) slot;
}