| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Android Java/JNI system driver |
| 12 |
* |
| 13 |
* By Thomas Fjellstrom. |
| 14 |
* |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "allegro5/allegro.h" |
| 18 |
#include "allegro5/allegro_android.h" |
| 19 |
#include "allegro5/platform/aintunix.h" |
| 20 |
#include "allegro5/internal/aintern_android.h" |
| 21 |
#include "allegro5/internal/aintern_tls.h" |
| 22 |
#include "allegro5/platform/aintandroid.h" |
| 23 |
#include "allegro5/platform/alandroid.h" |
| 24 |
#include "allegro5/threads.h" |
| 25 |
|
| 26 |
#include <dlfcn.h> |
| 27 |
#include <jni.h> |
| 28 |
|
| 29 |
#include "allegro5/allegro_opengl.h" |
| 30 |
#include "allegro5/internal/aintern_opengl.h" |
| 31 |
|
| 32 |
ALLEGRO_DEBUG_CHANNEL("android") |
| 33 |
|
| 34 |
struct system_data_t { |
| 35 |
JNIEnv *env; |
| 36 |
jobject activity_object; |
| 37 |
jclass input_stream_class; |
| 38 |
jclass illegal_argument_exception_class; |
| 39 |
jclass apk_stream_class; |
| 40 |
jclass image_loader_class; |
| 41 |
jclass clipboard_class; |
| 42 |
jclass apk_fs_class; |
| 43 |
|
| 44 |
ALLEGRO_SYSTEM_ANDROID *system; |
| 45 |
ALLEGRO_MUTEX *mutex; |
| 46 |
ALLEGRO_COND *cond; |
| 47 |
ALLEGRO_THREAD *trampoline; |
| 48 |
bool trampoline_running; |
| 49 |
|
| 50 |
ALLEGRO_USTR *user_lib_name; |
| 51 |
ALLEGRO_USTR *resources_dir; |
| 52 |
ALLEGRO_USTR *data_dir; |
| 53 |
ALLEGRO_USTR *temp_dir; |
| 54 |
ALLEGRO_USTR *apk_path; |
| 55 |
ALLEGRO_USTR *model; |
| 56 |
ALLEGRO_USTR *manufacturer; |
| 57 |
|
| 58 |
void *user_lib; |
| 59 |
int (*user_main)(int argc, char **argv); |
| 60 |
|
| 61 |
int orientation; |
| 62 |
|
| 63 |
bool paused; |
| 64 |
}; |
| 65 |
|
| 66 |
static struct system_data_t system_data; |
| 67 |
static JavaVM* javavm; |
| 68 |
static JNIEnv *main_env; |
| 69 |
|
| 70 |
static const char *_real_al_android_get_os_version(JNIEnv *env); |
| 71 |
|
| 72 |
bool _al_android_is_paused(void) |
| 73 |
{ |
| 74 |
return system_data.paused; |
| 75 |
} |
| 76 |
|
| 77 |
int _al_android_get_display_orientation(void) |
| 78 |
{ |
| 79 |
return system_data.orientation; |
| 80 |
} |
| 81 |
|
| 82 |
jclass _al_android_input_stream_class(void) |
| 83 |
{ |
| 84 |
return system_data.input_stream_class; |
| 85 |
} |
| 86 |
|
| 87 |
jclass _al_android_apk_stream_class(void) |
| 88 |
{ |
| 89 |
return system_data.apk_stream_class; |
| 90 |
} |
| 91 |
|
| 92 |
jclass _al_android_image_loader_class(void) |
| 93 |
{ |
| 94 |
return system_data.image_loader_class; |
| 95 |
} |
| 96 |
|
| 97 |
jclass _al_android_clipboard_class(void) |
| 98 |
{ |
| 99 |
return system_data.clipboard_class; |
| 100 |
} |
| 101 |
|
| 102 |
jobject _al_android_activity_object() |
| 103 |
{ |
| 104 |
return system_data.activity_object; |
| 105 |
} |
| 106 |
|
| 107 |
jclass _al_android_apk_fs_class(void) |
| 108 |
{ |
| 109 |
return system_data.apk_fs_class; |
| 110 |
} |
| 111 |
|
| 112 |
static void finish_activity(JNIEnv *env); |
| 113 |
|
| 114 |
static bool already_cleaned_up = false; |
| 115 |
|
| 116 |
/* NOTE: don't put any ALLEGRO_DEBUG in here! */ |
| 117 |
static void android_cleanup(bool uninstall_system) |
| 118 |
{ |
| 119 |
if (already_cleaned_up) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
already_cleaned_up = true; |
| 124 |
|
| 125 |
if (uninstall_system) { |
| 126 |
/* I don't think android calls our atexit() stuff since we're in a shared lib |
| 127 |
so make sure al_uninstall_system is called */ |
| 128 |
al_uninstall_system(); |
| 129 |
} |
| 130 |
|
| 131 |
finish_activity(_al_android_get_jnienv()); |
| 132 |
|
| 133 |
(*javavm)->DetachCurrentThread(javavm); |
| 134 |
} |
| 135 |
|
| 136 |
static void *android_app_trampoline(ALLEGRO_THREAD *thr, void *arg) |
| 137 |
{ |
| 138 |
const int argc = 1; |
| 139 |
const char *argv[2] = {system_data.user_lib, NULL}; |
| 140 |
int ret; |
| 141 |
|
| 142 |
(void)thr; |
| 143 |
(void)arg; |
| 144 |
|
| 145 |
ALLEGRO_DEBUG("signaling running"); |
| 146 |
|
| 147 |
al_lock_mutex(system_data.mutex); |
| 148 |
system_data.trampoline_running = true; |
| 149 |
al_broadcast_cond(system_data.cond); |
| 150 |
al_unlock_mutex(system_data.mutex); |
| 151 |
|
| 152 |
ALLEGRO_DEBUG("entering main function %p", system_data.user_main); |
| 153 |
|
| 154 |
ret = (system_data.user_main)(argc, (char **)argv); |
| 155 |
|
| 156 |
/* Can we do anything with this exit code? */ |
| 157 |
ALLEGRO_DEBUG("returned from main function, exit code = %d", ret); |
| 158 |
|
| 159 |
/* NOTE: don't put any ALLEGRO_DEBUG in here after running main! */ |
| 160 |
|
| 161 |
android_cleanup(true); |
| 162 |
|
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
/* called by JNI/Java */ |
| 167 |
jint JNI_OnLoad(JavaVM* vm, void* reserved) |
| 168 |
{ |
| 169 |
(void)reserved; |
| 170 |
javavm = vm; |
| 171 |
return JNI_VERSION_1_4; |
| 172 |
} |
| 173 |
|
| 174 |
JNI_FUNC(bool, AllegroActivity, nativeOnCreate, (JNIEnv *env, jobject obj)) |
| 175 |
{ |
| 176 |
ALLEGRO_SYSTEM_ANDROID *na_sys = NULL; |
| 177 |
jclass iae; |
| 178 |
jclass aisc; |
| 179 |
jclass asc; |
| 180 |
|
| 181 |
ALLEGRO_DEBUG("entered nativeOnCreate"); |
| 182 |
|
| 183 |
// we're already initialized, we REALLY don't want to run all the stuff below again. |
| 184 |
if(system_data.system) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
pthread_t self = pthread_self(); |
| 189 |
ALLEGRO_DEBUG("pthread_self:%p", (void*)self); |
| 190 |
ALLEGRO_DEBUG("nativeOnCreate begin"); |
| 191 |
|
| 192 |
memset(&system_data, 0, sizeof(system_data)); |
| 193 |
|
| 194 |
ALLEGRO_DEBUG("grab activity global refs"); |
| 195 |
system_data.env = env; |
| 196 |
system_data.activity_object = (*env)->NewGlobalRef(env, obj); |
| 197 |
|
| 198 |
iae = (*env)->FindClass(env, "java/lang/IllegalArgumentException"); |
| 199 |
system_data.illegal_argument_exception_class = (*env)->NewGlobalRef(env, iae); |
| 200 |
|
| 201 |
aisc = (*env)->FindClass(env, ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/AllegroInputStream"); |
| 202 |
system_data.input_stream_class = (*env)->NewGlobalRef(env, aisc); |
| 203 |
|
| 204 |
asc = (*env)->FindClass(env, ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/AllegroAPKStream"); |
| 205 |
system_data.apk_stream_class = (*env)->NewGlobalRef(env, asc); |
| 206 |
|
| 207 |
asc = (*env)->FindClass(env, ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/ImageLoader"); |
| 208 |
system_data.image_loader_class = (*env)->NewGlobalRef(env, asc); |
| 209 |
|
| 210 |
asc = (*env)->FindClass(env, ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/Clipboard"); |
| 211 |
system_data.clipboard_class = (*env)->NewGlobalRef(env, asc); |
| 212 |
|
| 213 |
asc = (*env)->FindClass(env, ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/AllegroAPKList"); |
| 214 |
system_data.apk_fs_class = (*env)->NewGlobalRef(env, asc); |
| 215 |
|
| 216 |
ALLEGRO_DEBUG("create mutex and cond objects"); |
| 217 |
system_data.mutex = al_create_mutex(); |
| 218 |
system_data.cond = al_create_cond(); |
| 219 |
|
| 220 |
ALLEGRO_DEBUG("get directories"); |
| 221 |
system_data.user_lib_name = _jni_callStringMethod(env, system_data.activity_object, "getUserLibName", "()Ljava/lang/String;"); |
| 222 |
system_data.resources_dir = _jni_callStringMethod(env, system_data.activity_object, "getResourcesDir", "()Ljava/lang/String;"); |
| 223 |
system_data.data_dir = _jni_callStringMethod(env, system_data.activity_object, "getPubDataDir", "()Ljava/lang/String;"); |
| 224 |
system_data.temp_dir = _jni_callStringMethod(env, system_data.activity_object, "getTempDir", "()Ljava/lang/String;"); |
| 225 |
system_data.apk_path = _jni_callStringMethod(env, system_data.activity_object, "getApkPath", "()Ljava/lang/String;"); |
| 226 |
system_data.model = _jni_callStringMethod(env, system_data.activity_object, "getModel", "()Ljava/lang/String;"); |
| 227 |
system_data.manufacturer = _jni_callStringMethod(env, system_data.activity_object, "getManufacturer", "()Ljava/lang/String;"); |
| 228 |
ALLEGRO_DEBUG("resources_dir: %s", al_cstr(system_data.resources_dir)); |
| 229 |
ALLEGRO_DEBUG("data_dir: %s", al_cstr(system_data.data_dir)); |
| 230 |
ALLEGRO_DEBUG("temp_dir: %s", al_cstr(system_data.temp_dir)); |
| 231 |
ALLEGRO_DEBUG("apk_path: %s", al_cstr(system_data.apk_path)); |
| 232 |
ALLEGRO_DEBUG("model: %s", al_cstr(system_data.model)); |
| 233 |
ALLEGRO_DEBUG("manufacturer: %s", al_cstr(system_data.manufacturer)); |
| 234 |
|
| 235 |
ALLEGRO_DEBUG("creating ALLEGRO_SYSTEM_ANDROID struct"); |
| 236 |
na_sys = system_data.system = (ALLEGRO_SYSTEM_ANDROID*)al_malloc(sizeof *na_sys); |
| 237 |
memset(na_sys, 0, sizeof *na_sys); |
| 238 |
|
| 239 |
ALLEGRO_DEBUG("get system pointer"); |
| 240 |
ALLEGRO_SYSTEM *sys = &na_sys->system; |
| 241 |
ALLEGRO_DEBUG("get system interface"); |
| 242 |
sys->vt = _al_system_android_interface(); |
| 243 |
|
| 244 |
ALLEGRO_DEBUG("init display vector"); |
| 245 |
_al_vector_init(&sys->displays, sizeof(ALLEGRO_DISPLAY_ANDROID *)); |
| 246 |
|
| 247 |
ALLEGRO_DEBUG("init time"); |
| 248 |
_al_unix_init_time(); |
| 249 |
|
| 250 |
const char *user_lib_name = al_cstr(system_data.user_lib_name); |
| 251 |
ALLEGRO_DEBUG("load user lib: %s", user_lib_name); |
| 252 |
system_data.user_lib = dlopen(user_lib_name, RTLD_LAZY|RTLD_GLOBAL); |
| 253 |
if (!system_data.user_lib) { |
| 254 |
ALLEGRO_ERROR("failed to load user lib: %s", user_lib_name); |
| 255 |
ALLEGRO_ERROR("%s", dlerror()); |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
system_data.user_main = dlsym(system_data.user_lib, "main"); |
| 260 |
if (!system_data.user_main) { |
| 261 |
ALLEGRO_ERROR("failed to locate symbol main: %s", dlerror()); |
| 262 |
dlclose(system_data.user_lib); |
| 263 |
return false; |
| 264 |
} |
| 265 |
ALLEGRO_DEBUG("main function address: %p\n", system_data.user_main); |
| 266 |
|
| 267 |
ALLEGRO_DEBUG("creating trampoline for app thread"); |
| 268 |
system_data.trampoline = al_create_thread(android_app_trampoline, NULL); |
| 269 |
al_start_thread(system_data.trampoline); |
| 270 |
|
| 271 |
ALLEGRO_DEBUG("waiting for app trampoline to signal running"); |
| 272 |
al_lock_mutex(system_data.mutex); |
| 273 |
while(!system_data.trampoline_running) { |
| 274 |
al_wait_cond(system_data.cond, system_data.mutex); |
| 275 |
} |
| 276 |
al_unlock_mutex(system_data.mutex); |
| 277 |
|
| 278 |
ALLEGRO_DEBUG("setup done. returning to dalvik."); |
| 279 |
|
| 280 |
return true; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
JNI_FUNC(void, AllegroActivity, nativeOnPause, (JNIEnv *env, jobject obj)) |
| 285 |
{ |
| 286 |
(void)env; |
| 287 |
(void)obj; |
| 288 |
|
| 289 |
ALLEGRO_DEBUG("pause activity\n"); |
| 290 |
|
| 291 |
system_data.paused = true; |
| 292 |
|
| 293 |
ALLEGRO_SYSTEM *sys = (void *)al_get_system_driver(); |
| 294 |
|
| 295 |
if (!system_data.system || !sys) { |
| 296 |
ALLEGRO_DEBUG("no system driver"); |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
if (!_al_vector_size(&sys->displays)) { |
| 301 |
ALLEGRO_DEBUG("no display, not sending SWITCH_OUT event"); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
ALLEGRO_DISPLAY *display = *(ALLEGRO_DISPLAY**)_al_vector_ref(&sys->displays, 0); |
| 306 |
|
| 307 |
if (display) { |
| 308 |
ALLEGRO_EVENT event; |
| 309 |
_al_event_source_lock(&display->es); |
| 310 |
|
| 311 |
if(_al_event_source_needs_to_generate_event(&display->es)) { |
| 312 |
event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT; |
| 313 |
event.display.timestamp = al_current_time(); |
| 314 |
_al_event_source_emit_event(&display->es, &event); |
| 315 |
} |
| 316 |
_al_event_source_unlock(&display->es); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
JNI_FUNC(void, AllegroActivity, nativeOnResume, (JNIEnv *env, jobject obj)) |
| 321 |
{ |
| 322 |
ALLEGRO_SYSTEM *sys = &system_data.system->system; |
| 323 |
ALLEGRO_DISPLAY *d = NULL; |
| 324 |
|
| 325 |
(void)obj; |
| 326 |
|
| 327 |
system_data.paused = false; |
| 328 |
|
| 329 |
ALLEGRO_DEBUG("resume activity"); |
| 330 |
|
| 331 |
if(!system_data.system || !sys) { |
| 332 |
ALLEGRO_DEBUG("no system driver"); |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
if(!_al_vector_size(&sys->displays)) { |
| 337 |
ALLEGRO_DEBUG("no display, not sending SWITCH_IN event"); |
| 338 |
return; |
| 339 |
} |
| 340 |
|
| 341 |
d = *(ALLEGRO_DISPLAY**)_al_vector_ref(&sys->displays, 0); |
| 342 |
ALLEGRO_DEBUG("got display: %p", d); |
| 343 |
|
| 344 |
if(!((ALLEGRO_DISPLAY_ANDROID*)d)->created) { |
| 345 |
_al_android_create_surface(env, true); // request android create our surface |
| 346 |
} |
| 347 |
|
| 348 |
ALLEGRO_DISPLAY *display = *(ALLEGRO_DISPLAY**)_al_vector_ref(&sys->displays, 0); |
| 349 |
|
| 350 |
if (display) { |
| 351 |
ALLEGRO_EVENT event; |
| 352 |
_al_event_source_lock(&display->es); |
| 353 |
|
| 354 |
if(_al_event_source_needs_to_generate_event(&display->es)) { |
| 355 |
event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN; |
| 356 |
event.display.timestamp = al_current_time(); |
| 357 |
_al_event_source_emit_event(&display->es, &event); |
| 358 |
} |
| 359 |
_al_event_source_unlock(&display->es); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/* NOTE: don't put any ALLEGRO_DEBUG in here! */ |
| 364 |
JNI_FUNC(void, AllegroActivity, nativeOnDestroy, (JNIEnv *env, jobject obj)) |
| 365 |
{ |
| 366 |
(void)obj; |
| 367 |
|
| 368 |
/* onDestroy can be called before main returns, for example if you start |
| 369 |
* a new activity, your Allegro game will get onDestroy when it returns. |
| 370 |
* At that point there's nothing you can do and any code you execute will |
| 371 |
* crash, so this attempts to handle that more gracefully. Calling |
| 372 |
* android_cleanup() eventually leads back here anyway. We ask android_cleanup |
| 373 |
* not to call al_uninstall_system because GPU access causes a crash at |
| 374 |
* this point (cleaning up bitmaps/displays etc.) The trampoline will exit |
| 375 |
* eventually too so we guard against android_cleanup() being called twice. |
| 376 |
*/ |
| 377 |
bool main_returned = _jni_callBooleanMethodV( |
| 378 |
env, |
| 379 |
system_data.activity_object, |
| 380 |
"getMainReturned", |
| 381 |
"()Z" |
| 382 |
); |
| 383 |
|
| 384 |
if (!main_returned) { |
| 385 |
exit(0); |
| 386 |
} |
| 387 |
|
| 388 |
if(!system_data.user_lib) { |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
system_data.user_main = NULL; |
| 393 |
if(dlclose(system_data.user_lib) != 0) { |
| 394 |
return; |
| 395 |
} |
| 396 |
|
| 397 |
(*env)->DeleteGlobalRef(env, system_data.activity_object); |
| 398 |
(*env)->DeleteGlobalRef(env, system_data.illegal_argument_exception_class); |
| 399 |
(*env)->DeleteGlobalRef(env, system_data.input_stream_class); |
| 400 |
|
| 401 |
free(system_data.system); |
| 402 |
|
| 403 |
memset(&system_data, 0, sizeof(system_data)); |
| 404 |
} |
| 405 |
|
| 406 |
JNI_FUNC(void, AllegroActivity, nativeOnOrientationChange, (JNIEnv *env, jobject obj, int orientation, bool init)) |
| 407 |
{ |
| 408 |
ALLEGRO_SYSTEM *sys = &system_data.system->system; |
| 409 |
ALLEGRO_DISPLAY *d = NULL; |
| 410 |
ALLEGRO_EVENT event; |
| 411 |
|
| 412 |
(void)env; (void)obj; |
| 413 |
|
| 414 |
ALLEGRO_DEBUG("got orientation change!"); |
| 415 |
|
| 416 |
system_data.orientation = orientation; |
| 417 |
|
| 418 |
if (!init) { |
| 419 |
|
| 420 |
/* no display, just skip */ |
| 421 |
if (!_al_vector_size(&sys->displays)) { |
| 422 |
ALLEGRO_DEBUG("no display, not sending orientation change event"); |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
d = *(ALLEGRO_DISPLAY**)_al_vector_ref(&sys->displays, 0); |
| 427 |
ASSERT(d != NULL); |
| 428 |
|
| 429 |
ALLEGRO_DEBUG("locking display event source: %p %p", d, &d->es); |
| 430 |
|
| 431 |
_al_event_source_lock(&d->es); |
| 432 |
|
| 433 |
if(_al_event_source_needs_to_generate_event(&d->es)) { |
| 434 |
ALLEGRO_DEBUG("emit event"); |
| 435 |
event.display.type = ALLEGRO_EVENT_DISPLAY_ORIENTATION; |
| 436 |
event.display.timestamp = al_current_time(); |
| 437 |
event.display.orientation = orientation; |
| 438 |
_al_event_source_emit_event(&d->es, &event); |
| 439 |
} |
| 440 |
|
| 441 |
ALLEGRO_DEBUG("unlocking display event source"); |
| 442 |
_al_event_source_unlock(&d->es); |
| 443 |
|
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
JNI_FUNC(void, AllegroActivity, nativeSendJoystickConfigurationEvent, (JNIEnv *env, jobject obj)) |
| 448 |
{ |
| 449 |
(void)env; |
| 450 |
(void)obj; |
| 451 |
|
| 452 |
if (!al_is_joystick_installed()) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); |
| 457 |
_al_event_source_lock(es); |
| 458 |
ALLEGRO_EVENT event; |
| 459 |
event.type = ALLEGRO_EVENT_JOYSTICK_CONFIGURATION; |
| 460 |
_al_event_source_emit_event(es, &event); |
| 461 |
_al_event_source_unlock(es); |
| 462 |
} |
| 463 |
|
| 464 |
/* NOTE: don't put any ALLEGRO_DEBUG in here! */ |
| 465 |
static void finish_activity(JNIEnv *env) |
| 466 |
{ |
| 467 |
_jni_callVoidMethod(env, system_data.activity_object, "postFinish"); |
| 468 |
} |
| 469 |
|
| 470 |
static ALLEGRO_SYSTEM *android_initialize(int flags) |
| 471 |
{ |
| 472 |
(void)flags; |
| 473 |
|
| 474 |
ALLEGRO_DEBUG("android_initialize"); |
| 475 |
|
| 476 |
/* This was stored before user main ran, to make it easy and accessible |
| 477 |
* the same way for all threads, we set it in tls |
| 478 |
*/ |
| 479 |
_al_android_set_jnienv(main_env); |
| 480 |
|
| 481 |
return &system_data.system->system; |
| 482 |
} |
| 483 |
|
| 484 |
static ALLEGRO_JOYSTICK_DRIVER *android_get_joystick_driver(void) |
| 485 |
{ |
| 486 |
return &_al_android_joystick_driver; |
| 487 |
} |
| 488 |
|
| 489 |
static int android_get_num_video_adapters(void) |
| 490 |
{ |
| 491 |
return 1; |
| 492 |
} |
| 493 |
|
| 494 |
static bool android_get_monitor_info(int adapter, ALLEGRO_MONITOR_INFO *info) |
| 495 |
{ |
| 496 |
if (adapter >= android_get_num_video_adapters()) |
| 497 |
return false; |
| 498 |
|
| 499 |
JNIEnv * env = (JNIEnv *)_al_android_get_jnienv(); |
| 500 |
jobject rect = _jni_callObjectMethod(env, _al_android_activity_object(), "getDisplaySize", "()Landroid/graphics/Rect;"); |
| 501 |
|
| 502 |
info->x1 = 0; |
| 503 |
info->y1 = 0; |
| 504 |
info->x2 = _jni_callIntMethod(env, rect, "width"); |
| 505 |
info->y2 = _jni_callIntMethod(env, rect, "height"); |
| 506 |
|
| 507 |
ALLEGRO_DEBUG("Monitor Info: %d:%d", info->x2, info->y2); |
| 508 |
|
| 509 |
_jni_callv(env, DeleteLocalRef, rect); |
| 510 |
|
| 511 |
return true; |
| 512 |
} |
| 513 |
|
| 514 |
static void android_shutdown_system(void) |
| 515 |
{ |
| 516 |
ALLEGRO_SYSTEM *s = al_get_system_driver(); |
| 517 |
/* Close all open displays. */ |
| 518 |
while (_al_vector_size(&s->displays) > 0) { |
| 519 |
ALLEGRO_DISPLAY **dptr = _al_vector_ref(&s->displays, 0); |
| 520 |
ALLEGRO_DISPLAY *d = *dptr; |
| 521 |
al_destroy_display(d); |
| 522 |
} |
| 523 |
_al_vector_free(&s->displays); |
| 524 |
} |
| 525 |
|
| 526 |
static bool android_inhibit_screensaver(bool inhibit) |
| 527 |
{ |
| 528 |
return _jni_callBooleanMethodV(_al_android_get_jnienv(), system_data.activity_object, "inhibitScreenLock", "(Z)Z", inhibit); |
| 529 |
} |
| 530 |
|
| 531 |
static ALLEGRO_SYSTEM_INTERFACE *android_vt; |
| 532 |
|
| 533 |
ALLEGRO_SYSTEM_INTERFACE *_al_system_android_interface() |
| 534 |
{ |
| 535 |
if(android_vt) |
| 536 |
return android_vt; |
| 537 |
|
| 538 |
android_vt = al_malloc(sizeof *android_vt); |
| 539 |
memset(android_vt, 0, sizeof *android_vt); |
| 540 |
|
| 541 |
android_vt->id = ALLEGRO_SYSTEM_ID_ANDROID; |
| 542 |
android_vt->initialize = android_initialize; |
| 543 |
android_vt->get_display_driver = _al_get_android_display_driver; |
| 544 |
android_vt->get_keyboard_driver = _al_get_android_keyboard_driver; |
| 545 |
android_vt->get_mouse_driver = _al_get_android_mouse_driver; |
| 546 |
android_vt->get_touch_input_driver = _al_get_android_touch_input_driver; |
| 547 |
android_vt->get_joystick_driver = android_get_joystick_driver; |
| 548 |
android_vt->get_num_video_adapters = android_get_num_video_adapters; |
| 549 |
android_vt->get_monitor_info = android_get_monitor_info; |
| 550 |
android_vt->get_path = _al_android_get_path; |
| 551 |
android_vt->shutdown_system = android_shutdown_system; |
| 552 |
android_vt->inhibit_screensaver = android_inhibit_screensaver; |
| 553 |
android_vt->get_time = _al_unix_get_time; |
| 554 |
android_vt->rest = _al_unix_rest; |
| 555 |
android_vt->init_timeout = _al_unix_init_timeout; |
| 556 |
|
| 557 |
return android_vt; |
| 558 |
} |
| 559 |
|
| 560 |
ALLEGRO_PATH *_al_android_get_path(int id) |
| 561 |
{ |
| 562 |
ALLEGRO_PATH *path = NULL; |
| 563 |
|
| 564 |
switch(id) { |
| 565 |
case ALLEGRO_RESOURCES_PATH: |
| 566 |
/* path to bundle's files */ |
| 567 |
path = al_create_path_for_directory(al_cstr(system_data.resources_dir)); |
| 568 |
break; |
| 569 |
|
| 570 |
case ALLEGRO_TEMP_PATH: |
| 571 |
/* path to the application cache */ |
| 572 |
path = al_create_path_for_directory(al_cstr(system_data.temp_dir)); |
| 573 |
break; |
| 574 |
|
| 575 |
case ALLEGRO_USER_DATA_PATH: |
| 576 |
case ALLEGRO_USER_HOME_PATH: |
| 577 |
case ALLEGRO_USER_SETTINGS_PATH: |
| 578 |
case ALLEGRO_USER_DOCUMENTS_PATH: |
| 579 |
/* path to sdcard */ |
| 580 |
path = al_create_path_for_directory(al_cstr(system_data.data_dir)); |
| 581 |
break; |
| 582 |
|
| 583 |
case ALLEGRO_EXENAME_PATH: |
| 584 |
/* bundle path + bundle name */ |
| 585 |
// FIXME! |
| 586 |
path = al_create_path(al_cstr(system_data.apk_path)); |
| 587 |
break; |
| 588 |
|
| 589 |
default: |
| 590 |
path = al_create_path_for_directory("/DANGER/WILL/ROBINSON"); |
| 591 |
break; |
| 592 |
} |
| 593 |
|
| 594 |
return path; |
| 595 |
} |
| 596 |
|
| 597 |
static const char *_real_al_android_get_os_version(JNIEnv *env) |
| 598 |
{ |
| 599 |
static char buffer[25]; |
| 600 |
ALLEGRO_USTR *s = _jni_callStringMethod(env, system_data.activity_object, "getOsVersion", "()Ljava/lang/String;"); |
| 601 |
strncpy(buffer, al_cstr(s), 25); |
| 602 |
al_ustr_free(s); |
| 603 |
return buffer; |
| 604 |
} |
| 605 |
|
| 606 |
/* Function: al_android_get_os_version |
| 607 |
*/ |
| 608 |
const char *al_android_get_os_version(void) |
| 609 |
{ |
| 610 |
return _real_al_android_get_os_version(_al_android_get_jnienv()); |
| 611 |
} |
| 612 |
|
| 613 |
void _al_android_thread_created(void) |
| 614 |
{ |
| 615 |
JNIEnv *env; |
| 616 |
JavaVMAttachArgs attach_args = { JNI_VERSION_1_4, "trampoline", NULL }; |
| 617 |
(*javavm)->AttachCurrentThread(javavm, &env, &attach_args); |
| 618 |
/* This function runs once before al_init, so before TLS is initialized |
| 619 |
* so we save the environment and set it later in that case. |
| 620 |
*/ |
| 621 |
ALLEGRO_SYSTEM *s = al_get_system_driver(); |
| 622 |
if (s && s->installed) { |
| 623 |
_al_android_set_jnienv(env); |
| 624 |
} |
| 625 |
else { |
| 626 |
main_env = env; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
void _al_android_thread_ended(void) |
| 631 |
{ |
| 632 |
(*javavm)->DetachCurrentThread(javavm); |
| 633 |
} |
| 634 |
|
| 635 |
void _al_android_set_capture_volume_keys(ALLEGRO_DISPLAY *display, bool onoff) |
| 636 |
{ |
| 637 |
ALLEGRO_DISPLAY_ANDROID *d = (ALLEGRO_DISPLAY_ANDROID *)display; |
| 638 |
_jni_callVoidMethodV(_al_android_get_jnienv(), d->surface_object, "setCaptureVolumeKeys", "(Z)V", onoff); |
| 639 |
} |
| 640 |
|
| 641 |
/* register system interfaces */ |
| 642 |
|
| 643 |
void _al_register_system_interfaces(void) |
| 644 |
{ |
| 645 |
ALLEGRO_SYSTEM_INTERFACE **add; |
| 646 |
|
| 647 |
/* add the native activity driver */ |
| 648 |
add = _al_vector_alloc_back(&_al_system_interfaces); |
| 649 |
*add = _al_system_android_interface(); |
| 650 |
|
| 651 |
/* TODO: add the non native activity driver */ |
| 652 |
} |
| 653 |
|
| 654 |
/* Function: al_android_get_jni_env |
| 655 |
*/ |
| 656 |
JNIEnv *al_android_get_jni_env(void) |
| 657 |
{ |
| 658 |
return _al_android_get_jnienv(); |
| 659 |
} |
| 660 |
|
| 661 |
/* Function: al_android_get_activity |
| 662 |
*/ |
| 663 |
jobject al_android_get_activity(void) |
| 664 |
{ |
| 665 |
return _al_android_activity_object(); |
| 666 |
} |
| 667 |
|
| 668 |
/* Function: al_android_open_fd |
| 669 |
*/ |
| 670 |
int al_android_open_fd(const char *uri, const char *mode) |
| 671 |
{ |
| 672 |
JNIEnv *env = _al_android_get_jnienv(); |
| 673 |
jobject activity = _al_android_activity_object(); |
| 674 |
|
| 675 |
jstring juri = _jni_call(env, jstring, NewStringUTF, uri != NULL ? uri : ""); /* content:// or file:// */ |
| 676 |
jstring jmode = _jni_call(env, jstring, NewStringUTF, mode != NULL ? mode : ""); /* "r", "w", "rw", "wa", "wt", "rwt" */ |
| 677 |
|
| 678 |
int fd = _jni_callIntMethodV(env, activity, "openFileDescriptor", "(Ljava/lang/String;Ljava/lang/String;)I", juri, jmode); |
| 679 |
|
| 680 |
_jni_callv(env, DeleteLocalRef, jmode); |
| 681 |
_jni_callv(env, DeleteLocalRef, juri); |
| 682 |
|
| 683 |
if (fd == -1) |
| 684 |
al_set_errno(ENOENT); |
| 685 |
else if (fd < 0) |
| 686 |
al_set_errno(ENOTSUP); |
| 687 |
|
| 688 |
return fd; |
| 689 |
} |
| 690 |
|
| 691 |
/* vim: set sts=3 sw=3 et: */ |