| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Native dialog addon for Android. |
| 12 |
* |
| 13 |
* By Alexandre Martins. |
| 14 |
*/ |
| 15 |
|
| 16 |
#define ALLEGRO_INTERNAL_UNSTABLE |
| 17 |
#include "allegro5/allegro.h" |
| 18 |
#include "allegro5/allegro_native_dialog.h" |
| 19 |
#include "allegro5/internal/aintern_android.h" |
| 20 |
#include "allegro5/internal/aintern_native_dialog.h" |
| 21 |
|
| 22 |
ALLEGRO_DEBUG_CHANNEL("android") |
| 23 |
|
| 24 |
static ALLEGRO_DISPLAY *get_active_display(void); |
| 25 |
static void wait_for_display_events(ALLEGRO_DISPLAY *dpy); |
| 26 |
static bool open_file_chooser(int flags, const char *patterns, const char *initial_path, ALLEGRO_PATH ***out_uri_strings, size_t *out_uri_count); |
| 27 |
static char *really_open_file_chooser(int flags, const char *patterns, const char *initial_path); |
| 28 |
static int show_message_box(const char *title, const char *message, const char *buttons, int flags); |
| 29 |
static void append_to_textlog(const char *tag, const char *message); |
| 30 |
|
| 31 |
static ALLEGRO_EVENT_QUEUE *queue = NULL; |
| 32 |
static ALLEGRO_MUTEX *mutex = NULL; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
bool _al_init_native_dialog_addon(void) |
| 38 |
{ |
| 39 |
if (NULL == (mutex = al_create_mutex())) |
| 40 |
return false; |
| 41 |
|
| 42 |
if (NULL == (queue = al_create_event_queue())) { |
| 43 |
al_destroy_mutex(mutex); |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
void _al_shutdown_native_dialog_addon(void) |
| 51 |
{ |
| 52 |
al_destroy_mutex(mutex); |
| 53 |
mutex = NULL; |
| 54 |
|
| 55 |
/*al_destroy_event_queue(queue);*/ /* already released by Allegro's destructors */ |
| 56 |
queue = NULL; |
| 57 |
} |
| 58 |
|
| 59 |
bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display, ALLEGRO_NATIVE_DIALOG *fd) |
| 60 |
{ |
| 61 |
/* al_show_native_file_dialog() has a blocking interface. Since there is a |
| 62 |
need to handle drawing halt and drawing resume events before this |
| 63 |
function returns, users must call it from a separate thread. */ |
| 64 |
(void)display; /* possibly NULL */ |
| 65 |
|
| 66 |
/* fail if the native dialog addon is not initialized */ |
| 67 |
if (!al_is_native_dialog_addon_initialized()) { |
| 68 |
ALLEGRO_DEBUG("the native dialog addon is not initialized"); |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
/* only one file chooser may be opened at a time */ |
| 73 |
al_lock_mutex(mutex); |
| 74 |
|
| 75 |
/* initial path (optional) */ |
| 76 |
const char *initial_path = NULL; |
| 77 |
if (fd->fc_initial_path != NULL) |
| 78 |
initial_path = al_path_cstr(fd->fc_initial_path, '/'); |
| 79 |
|
| 80 |
/* release any pre-existing paths */ |
| 81 |
if (fd->fc_paths != NULL) { |
| 82 |
for (size_t i = 0; i < fd->fc_path_count; i++) |
| 83 |
al_destroy_path(fd->fc_paths[i]); |
| 84 |
al_free(fd->fc_paths); |
| 85 |
} |
| 86 |
|
| 87 |
/* register the event source */ |
| 88 |
ALLEGRO_DISPLAY *dpy = get_active_display(); |
| 89 |
if (dpy != NULL) |
| 90 |
al_register_event_source(queue, &dpy->es); |
| 91 |
|
| 92 |
/* open the file chooser */ |
| 93 |
ALLEGRO_DEBUG("waiting for the file chooser"); |
| 94 |
ALLEGRO_USTR *mime_patterns = al_ustr_new(""); |
| 95 |
bool first = true; |
| 96 |
bool any_catchalls = false; |
| 97 |
for (size_t i = 0; i < _al_vector_size(&fd->fc_patterns); i++) { |
| 98 |
_AL_PATTERNS_AND_DESC *patterns_and_desc = _al_vector_ref(&fd->fc_patterns, (int)i); |
| 99 |
for (size_t j = 0; j < _al_vector_size(&patterns_and_desc->patterns_vec); j++) { |
| 100 |
_AL_PATTERN *pattern = _al_vector_ref(&patterns_and_desc->patterns_vec, (int)j); |
| 101 |
if (pattern->is_catchall) { |
| 102 |
any_catchalls = true; |
| 103 |
break; |
| 104 |
} |
| 105 |
if (pattern->is_mime) |
| 106 |
{ |
| 107 |
if (!first) |
| 108 |
al_ustr_append_chr(mime_patterns, ';'); |
| 109 |
first = false; |
| 110 |
al_ustr_append(mime_patterns, al_ref_info(&pattern->info)); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
if (any_catchalls) |
| 115 |
al_ustr_truncate(mime_patterns, 0); |
| 116 |
bool ret = open_file_chooser(fd->flags, al_cstr(mime_patterns), initial_path, &fd->fc_paths, &fd->fc_path_count); |
| 117 |
al_ustr_free(mime_patterns); |
| 118 |
ALLEGRO_DEBUG("done waiting for the file chooser"); |
| 119 |
|
| 120 |
/* ensure predictable behavior */ |
| 121 |
if (dpy != NULL) { |
| 122 |
|
| 123 |
/* We expect al_show_native_file_dialog() to be called before a drawing |
| 124 |
halt. We expect it to return after a drawing resume. */ |
| 125 |
wait_for_display_events(dpy); |
| 126 |
|
| 127 |
/* unregister the event source */ |
| 128 |
al_unregister_event_source(queue, &dpy->es); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
/* done! */ |
| 133 |
ALLEGRO_DEBUG("done"); |
| 134 |
|
| 135 |
al_unlock_mutex(mutex); |
| 136 |
return ret; |
| 137 |
} |
| 138 |
|
| 139 |
int _al_show_native_message_box(ALLEGRO_DISPLAY *display, ALLEGRO_NATIVE_DIALOG *nd) |
| 140 |
{ |
| 141 |
const char *heading = al_cstr(nd->mb_heading); |
| 142 |
const char *text = al_cstr(nd->mb_text); |
| 143 |
const char *buttons = nd->mb_buttons != NULL ? al_cstr(nd->mb_buttons) : NULL; |
| 144 |
(void)display; |
| 145 |
|
| 146 |
return show_message_box(heading, text, buttons, nd->flags); |
| 147 |
} |
| 148 |
|
| 149 |
bool _al_open_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog) |
| 150 |
{ |
| 151 |
textlog->is_active = true; |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
void _al_close_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog) |
| 156 |
{ |
| 157 |
textlog->is_active = false; |
| 158 |
} |
| 159 |
|
| 160 |
void _al_append_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog) |
| 161 |
{ |
| 162 |
if (textlog->is_active) { |
| 163 |
const char *title = al_cstr(textlog->title); |
| 164 |
const char *text = al_cstr(textlog->tl_pending_text); |
| 165 |
|
| 166 |
append_to_textlog(title, text); |
| 167 |
|
| 168 |
al_ustr_truncate(textlog->tl_pending_text, 0); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
bool _al_init_menu(ALLEGRO_MENU *menu) |
| 173 |
{ |
| 174 |
(void)menu; |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
bool _al_init_popup_menu(ALLEGRO_MENU *menu) |
| 179 |
{ |
| 180 |
(void)menu; |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
bool _al_insert_menu_item_at(ALLEGRO_MENU_ITEM *item, int i) |
| 185 |
{ |
| 186 |
(void)item; |
| 187 |
(void)i; |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
bool _al_destroy_menu_item_at(ALLEGRO_MENU_ITEM *item, int i) |
| 192 |
{ |
| 193 |
(void)item; |
| 194 |
(void)i; |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
bool _al_update_menu_item_at(ALLEGRO_MENU_ITEM *item, int i) |
| 199 |
{ |
| 200 |
(void)item; |
| 201 |
(void)i; |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
bool _al_show_display_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu) |
| 206 |
{ |
| 207 |
(void)display; |
| 208 |
(void)menu; |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
bool _al_hide_display_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu) |
| 213 |
{ |
| 214 |
(void)display; |
| 215 |
(void)menu; |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
bool _al_show_popup_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu) |
| 220 |
{ |
| 221 |
(void)display; |
| 222 |
(void)menu; |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
int _al_get_menu_display_height(void) |
| 227 |
{ |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
ALLEGRO_DISPLAY *get_active_display(void) |
| 235 |
{ |
| 236 |
ALLEGRO_SYSTEM *sys = al_get_system_driver(); |
| 237 |
ASSERT(sys); |
| 238 |
|
| 239 |
if (_al_vector_size(&sys->displays) == 0) |
| 240 |
return NULL; |
| 241 |
|
| 242 |
ALLEGRO_DISPLAY **dptr = (ALLEGRO_DISPLAY **)_al_vector_ref(&sys->displays, 0); |
| 243 |
return *dptr; |
| 244 |
} |
| 245 |
|
| 246 |
void wait_for_display_events(ALLEGRO_DISPLAY *dpy) |
| 247 |
{ |
| 248 |
ALLEGRO_DISPLAY_ANDROID *d = (ALLEGRO_DISPLAY_ANDROID *)dpy; |
| 249 |
ALLEGRO_TIMEOUT timeout; |
| 250 |
ALLEGRO_EVENT event; |
| 251 |
bool expected_state = false; |
| 252 |
|
| 253 |
memset(&event, 0, sizeof(event)); |
| 254 |
|
| 255 |
/* We expect a drawing halt event to be on the queue. If that is not true, |
| 256 |
then the drawing halt did not take place for some unusual reason */ |
| 257 |
ALLEGRO_DEBUG("looking for a ALLEGRO_EVENT_DISPLAY_HALT_DRAWING"); |
| 258 |
while (al_get_next_event(queue, &event)) { |
| 259 |
if (event.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING) { |
| 260 |
expected_state = true; |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/* skip if we're in an unexpected state */ |
| 266 |
if (!expected_state) { |
| 267 |
ALLEGRO_DEBUG("ALLEGRO_EVENT_DISPLAY_HALT_DRAWING not found"); |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
wait_for_drawing_resume: |
| 272 |
|
| 273 |
/* wait for ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING */ |
| 274 |
ALLEGRO_DEBUG("waiting for ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING"); |
| 275 |
while (event.type != ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING) |
| 276 |
al_wait_for_event(queue, &event); |
| 277 |
ALLEGRO_DEBUG("done waiting for ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING"); |
| 278 |
|
| 279 |
/* wait for al_acknowledge_drawing_resume() */ |
| 280 |
ALLEGRO_DEBUG("waiting for al_acknowledge_drawing_resume"); |
| 281 |
al_lock_mutex(d->mutex); |
| 282 |
while (!d->resumed) |
| 283 |
al_wait_cond(d->cond, d->mutex); |
| 284 |
al_unlock_mutex(d->mutex); |
| 285 |
ALLEGRO_DEBUG("done waiting for al_acknowledge_drawing_resume"); |
| 286 |
|
| 287 |
/* A resize event takes place here, as can be seen in the implementation of |
| 288 |
AllegroSurface.nativeOnChange() at src/android_display.c (at the time of |
| 289 |
this writing). However, the Allegro documentation does not specify that |
| 290 |
a resize event must follow a drawing resume. |
| 291 |
|
| 292 |
We expect that the user will call al_acknowledge_resize() immediately. |
| 293 |
We don't wait for the acknowledgement of the resize event, in case the |
| 294 |
implementation changes someday. We just wait a little bit. */ |
| 295 |
; |
| 296 |
|
| 297 |
/* check if a new ALLEGRO_EVENT_DISPLAY_HALT_DRAWING is emitted */ |
| 298 |
ALLEGRO_DEBUG("waiting for another ALLEGRO_EVENT_DISPLAY_HALT_DRAWING"); |
| 299 |
al_init_timeout(&timeout, 0.5); |
| 300 |
while (al_wait_for_event_until(queue, &event, &timeout)) { |
| 301 |
if (event.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING) |
| 302 |
goto wait_for_drawing_resume; |
| 303 |
else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) |
| 304 |
al_init_timeout(&timeout, 0.5); |
| 305 |
} |
| 306 |
ALLEGRO_DEBUG("done waiting for another ALLEGRO_EVENT_DISPLAY_HALT_DRAWING"); |
| 307 |
} |
| 308 |
|
| 309 |
bool open_file_chooser(int flags, const char *patterns, const char *initial_path, ALLEGRO_PATH ***out_uri_strings, size_t *out_uri_count) |
| 310 |
{ |
| 311 |
const char URI_DELIMITER = '\n'; |
| 312 |
char *result = NULL; |
| 313 |
|
| 314 |
/* initialize the results */ |
| 315 |
*out_uri_count = 0; |
| 316 |
*out_uri_strings = NULL; |
| 317 |
|
| 318 |
/* open the file chooser */ |
| 319 |
result = really_open_file_chooser(flags, patterns, initial_path); |
| 320 |
|
| 321 |
/* error? */ |
| 322 |
if (result == NULL) |
| 323 |
return false; |
| 324 |
|
| 325 |
/* split the returned string. If the file chooser was cancelled, variable result is an empty string */ |
| 326 |
for (char *next_uri = result, *p = result; *p; p++) { |
| 327 |
if (*p == URI_DELIMITER) { |
| 328 |
int last = (*out_uri_count)++; |
| 329 |
*out_uri_strings = al_realloc(*out_uri_strings, (*out_uri_count) * sizeof(ALLEGRO_PATH**)); |
| 330 |
|
| 331 |
/* ALLEGRO_PATHs don't explicitly support URIs at this time, but this works nonetheless. |
| 332 |
See parse_path_string() at src/path.c */ |
| 333 |
*p = '\0'; |
| 334 |
(*out_uri_strings)[last] = al_create_path(next_uri); |
| 335 |
next_uri = p+1; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/* success! */ |
| 340 |
free(result); |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
char *really_open_file_chooser(int flags, const char *patterns, const char *initial_path) |
| 345 |
{ |
| 346 |
char *result = NULL; |
| 347 |
|
| 348 |
JNIEnv *env = _al_android_get_jnienv(); |
| 349 |
jobject activity = _al_android_activity_object(); |
| 350 |
jobject dialog = _jni_callObjectMethod(env, activity, "getNativeDialogAddon", "()L" ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/AllegroDialog;"); |
| 351 |
|
| 352 |
jstring jpatterns = _jni_call(env, jstring, NewStringUTF, patterns != NULL ? patterns : ""); |
| 353 |
jstring jinitial_path = _jni_call(env, jstring, NewStringUTF, initial_path != NULL ? initial_path : ""); |
| 354 |
|
| 355 |
jstring jresult = (jstring)_jni_callObjectMethodV(env, dialog, "openFileChooser", "(ILjava/lang/String;Ljava/lang/String;)Ljava/lang/String;", (jint)flags, jpatterns, jinitial_path); |
| 356 |
jboolean is_null_result = _jni_call(env, jboolean, IsSameObject, jresult, NULL); |
| 357 |
if (!is_null_result) { |
| 358 |
const char *tmp = _jni_call(env, const char*, GetStringUTFChars, jresult, NULL); |
| 359 |
result = strdup(tmp); |
| 360 |
_jni_callv(env, ReleaseStringUTFChars, jresult, tmp); |
| 361 |
} |
| 362 |
_jni_callv(env, DeleteLocalRef, jresult); |
| 363 |
|
| 364 |
_jni_callv(env, DeleteLocalRef, jinitial_path); |
| 365 |
_jni_callv(env, DeleteLocalRef, jpatterns); |
| 366 |
|
| 367 |
_jni_callv(env, DeleteLocalRef, dialog); |
| 368 |
|
| 369 |
return result; |
| 370 |
} |
| 371 |
|
| 372 |
int show_message_box(const char *title, const char *message, const char *buttons, int flags) |
| 373 |
{ |
| 374 |
JNIEnv *env = _al_android_get_jnienv(); |
| 375 |
jobject activity = _al_android_activity_object(); |
| 376 |
jobject dialog = _jni_callObjectMethod(env, activity, "getNativeDialogAddon", "()L" ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/AllegroDialog;"); |
| 377 |
|
| 378 |
jstring jtitle = _jni_call(env, jstring, NewStringUTF, title != NULL ? title : ""); |
| 379 |
jstring jmessage = _jni_call(env, jstring, NewStringUTF, message != NULL ? message : ""); |
| 380 |
jstring jbuttons = _jni_call(env, jstring, NewStringUTF, buttons != NULL ? buttons : ""); |
| 381 |
|
| 382 |
int result = _jni_callIntMethodV(env, dialog, "showMessageBox", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)I", jtitle, jmessage, jbuttons, (jint)flags); |
| 383 |
|
| 384 |
_jni_callv(env, DeleteLocalRef, jbuttons); |
| 385 |
_jni_callv(env, DeleteLocalRef, jmessage); |
| 386 |
_jni_callv(env, DeleteLocalRef, jtitle); |
| 387 |
|
| 388 |
_jni_callv(env, DeleteLocalRef, dialog); |
| 389 |
|
| 390 |
return result; |
| 391 |
} |
| 392 |
|
| 393 |
void append_to_textlog(const char *tag, const char *message) |
| 394 |
{ |
| 395 |
JNIEnv *env = _al_android_get_jnienv(); |
| 396 |
jobject activity = _al_android_activity_object(); |
| 397 |
jobject dialog = _jni_callObjectMethod(env, activity, "getNativeDialogAddon", "()L" ALLEGRO_ANDROID_PACKAGE_NAME_SLASH "/AllegroDialog;"); |
| 398 |
|
| 399 |
jstring jtag = _jni_call(env, jstring, NewStringUTF, tag != NULL ? tag : ""); |
| 400 |
jstring jmessage = _jni_call(env, jstring, NewStringUTF, message != NULL ? message : ""); |
| 401 |
|
| 402 |
_jni_callVoidMethodV(env, dialog, "appendToTextLog", "(Ljava/lang/String;Ljava/lang/String;)V", jtag, jmessage); |
| 403 |
|
| 404 |
_jni_callv(env, DeleteLocalRef, jmessage); |
| 405 |
_jni_callv(env, DeleteLocalRef, jtag); |
| 406 |
|
| 407 |
_jni_callv(env, DeleteLocalRef, dialog); |
| 408 |
} |
| 409 |
|
| 410 |
/* vim: set sts=4 sw=4 et: */ |