Search
lxdream.org :: lxdream/src/gui_android.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui_android.c
changeset 1253:5aa14eeaad5a
prev1245:01e0020adf88
next1275:83b15705cdde
author nkeynes
date Mon Mar 05 11:41:03 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Small cleanups:
Refactor the post-windowing setup into gl_init_driver() in video_gl.c
Move gl_sl.c into src/drivers and tidy up a bit.
Fix OS X compiling plugins with -mdynamic-no-pic
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Native shims for the Android front-end (implemented in Java).
     5  *
     6  * This is complicated by the fact that all the emulation runs in a
     7  * separate thread.
     8  *
     9  * Copyright (c) 2012 Nathan Keynes.
    10  *
    11  * This program is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU General Public License as published by
    13  * the Free Software Foundation; either version 2 of the License, or
    14  * (at your option) any later version.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  */
    22 #include <jni.h>
    23 #include <pthread.h>
    24 #include <android/log.h>
    25 #include <android/native_window_jni.h>
    26 #include <libisofs.h>
    27 #include "dream.h"
    28 #include "dreamcast.h"
    29 #include "gui.h"
    30 #include "config.h"
    31 #include "lxpaths.h"
    32 #include "tqueue.h"
    33 #include "display.h"
    34 #include "gdlist.h"
    35 #include "gdrom/gdrom.h"
    36 #include "hotkeys.h"
    37 #include "serial.h"
    38 #include "aica/audio.h"
    39 #include "drivers/video_egl.h"
    40 #include "maple/maple.h"
    41 #include "vmu/vmulist.h"
    43 struct surface_info {
    44     ANativeWindow *win;
    45     int width, height, format;
    46 };
    48 static struct surface_info current_surface;
    49 static const char *appHome = NULL;
    51 /**
    52  * Count of running nanoseconds - used to cut back on the GUI runtime
    53  */
    54 static uint32_t android_gui_nanos = 0;
    55 static uint32_t android_gui_ticks = 0;
    56 static struct timeval android_gui_lasttv;
    59 void android_gui_start( void )
    60 {
    61     /* Dreamcast starting up hook */
    62 }
    64 void android_gui_stop( void )
    65 {
    66     /* Dreamcast stopping hook */
    67 }
    69 void android_gui_reset( void )
    70 {
    71     /* Dreamcast reset hook */
    72 }
    74 /**
    75  * The main emulation thread. (as opposed to the UI thread).
    76  */
    77 void *android_thread_main(void *data)
    78 {
    79     while(1) {
    80         tqueue_process_wait();
    81     }
    82     return NULL;
    83 }
    85 int android_set_surface(void *data)
    86 {
    87     struct surface_info *surface = (struct surface_info *)data;
    88     video_egl_set_window(surface->win, surface->width, surface->height, surface->format);
    89     return 0;
    90 }
    92 int android_clear_surface(void *data)
    93 {
    94     struct surface_info *surface = (struct surface_info *)data;
    96     if( dreamcast_is_running() ) {
    97         dreamcast_stop();
    98     }
    99     video_egl_clear_window();
   100     ANativeWindow_release(surface->win);
   101     surface->win = NULL;
   102     return 0;
   103 }
   105 static pthread_t dreamcast_thread;
   107 void android_start_thread()
   108 {
   109     pthread_attr_t attr;
   111     pthread_attr_init(&attr);
   112     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
   113     int status = pthread_create(&dreamcast_thread, &attr, android_thread_main, NULL);
   114     if( status != 0 ) {
   115         /* Handle errors */
   116     }
   117 }
   119 /** tqueue callback wrapper to get the right call type for simple events */
   120 int android_callback_wrapper( void *fn )
   121 {
   122     void (*cast_fn)(void) = fn;
   123     cast_fn();
   124 }
   126 int android_mount_disc( void *data )
   127 {
   128     char *s = (char *)data;
   129     ERROR err;
   130     gboolean result = gdrom_mount_image( s, &err ); /* TODO: Report error */
   131     return result;
   132 }
   134 int android_toggle_run( void *data )
   135 {
   136     if( dreamcast_is_running() ) {
   137         dreamcast_stop();
   138     } else {
   139         dreamcast_run();
   140     }
   141 }
   143 uint32_t android_gui_run_slice( uint32_t nanosecs )
   144 {
   145     android_gui_nanos += nanosecs;
   146     if( android_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   147         android_gui_nanos -= GUI_TICK_PERIOD;
   148         android_gui_ticks ++;
   149         uint32_t current_period = android_gui_ticks * GUI_TICK_PERIOD;
   151         // Run the event loop
   152         tqueue_process_all();
   154         struct timeval tv;
   155         gettimeofday(&tv,NULL);
   156         uint32_t ns = ((tv.tv_sec - android_gui_lasttv.tv_sec) * 1000000000) +
   157         (tv.tv_usec - android_gui_lasttv.tv_usec)*1000;
   158         if( (ns * 1.05) < current_period ) {
   159             // We've gotten ahead - sleep for a little bit
   160             struct timespec tv;
   161             tv.tv_sec = 0;
   162             tv.tv_nsec = current_period - ns;
   163             nanosleep(&tv, &tv);
   164         }
   166 #if 0
   167         /* Update the display every 10 ticks (ie 10 times a second) and
   168          * save the current tv value */
   169         if( android_gui_ticks > 10 ) {
   170             android_gui_ticks -= 10;
   172             double speed = (float)( (double)current_period * 100.0 / ns );
   173             android_gui_lasttv.tv_sec = tv.tv_sec;
   174             android_gui_lasttv.tv_usec = tv.tv_usec;
   175             main_window_set_speed( main_win, speed );
   176         }
   177 #endif
   178     }
   179     return nanosecs;
   182 }
   184 struct dreamcast_module android_gui_module = { "gui", NULL,
   185         android_gui_reset,
   186         android_gui_start,
   187         android_gui_run_slice,
   188         android_gui_stop,
   189         NULL, NULL };
   191 gboolean gui_error_dialog( const char *fmt, ... )
   192 {
   193     return FALSE; /* TODO */
   194 }
   196 void gui_update_state()
   197 {
   198     /* TODO */
   199 }
   201 void gui_set_use_grab( gboolean grab )
   202 {
   203     /* No implementation - mouse grab doesn't exist */
   204 }
   206 void gui_update_io_activity( io_activity_type activity, gboolean active )
   207 {
   208     /* No implementation */
   209 }
   211 void gui_do_later( do_later_callback_t func )
   212 {
   213     func(); /* TODO */
   214 }
   216 static void android_init( const char *appHomeDir )
   217 {
   218     set_global_log_level("info");
   219     appHome = appHomeDir;
   220     const char *confFile = g_strdup_printf("%s/lxdreamrc", appHome);
   221     set_user_data_path(appHome);
   222     lxdream_set_config_filename( confFile );
   223     lxdream_make_config_dir( );
   224     lxdream_load_config( );
   225     iso_init();
   226     gdrom_list_init();
   227     vmulist_init();
   228     dreamcast_init(1);
   230     dreamcast_register_module( &android_gui_module );
   231     audio_init_driver(NULL);
   232     display_driver_t display_driver = get_display_driver_by_name(NULL);
   233     display_set_driver(display_driver);
   235     hotkeys_init();
   236     serial_init();
   237     maple_reattach_all();
   239     ERROR err;
   240     gchar *disc_file = lxdream_get_global_config_path_value( CONFIG_GDROM );
   241     if( disc_file != NULL ) {
   242         gboolean ok = gdrom_mount_image( disc_file, &err );
   243         g_free(disc_file);
   244         if( !ok ) {
   245             WARN( err.msg );
   246         } else {
   247             INFO( "Mounted %s", disc_file );
   248         }
   249     }
   251     INFO( "%s! ready...", APP_NAME );
   252     android_start_thread();
   253 }
   256 /************************* Dreamcast native entry points **************************/
   258 static char *getStringChars( JNIEnv *env, jstring str );
   260 /**
   261  * Main initialization entry point. We need to do all the setup that main()
   262  * would normally do, as well as any UI specific setup.
   263  */
   264 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_init(JNIEnv * env, jclass obj,  jstring homeDir )
   265 {
   266     android_init( getStringChars(env, homeDir) );
   267 }
   269 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_run(JNIEnv * env, jclass obj)
   270 {
   271     tqueue_post_message( android_callback_wrapper, dreamcast_run );
   272 }
   274 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_toggleRun(JNIEnv * env, jclass obj)
   275 {
   276     tqueue_post_message( android_toggle_run, NULL );
   277 }
   279 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_reset(JNIEnv * env, jclass obj)
   280 {
   281     tqueue_post_message( android_callback_wrapper, dreamcast_reset );
   282 }
   284 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_stop(JNIEnv * env, jclass obj)
   285 {
   286     /* Need to make sure this completely shuts down before we return */
   287     tqueue_send_message( android_callback_wrapper, dreamcast_stop );
   288 }
   290 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunning(JNIEnv *env, jclass obj)
   291 {
   292     return dreamcast_is_running();
   293 }
   295 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunnable(JNIEnv *env, jclass obj)
   296 {
   297     return dreamcast_can_run();
   298 }
   300 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_mount(JNIEnv *env, jclass obj, jstring str)
   301 {
   302     char *s = getStringChars(env, str);
   303     return tqueue_send_message( android_mount_disc, s );
   304 }
   306 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_unmount(JNIEnv *env, jclass obj)
   307 {
   308     tqueue_post_message( android_callback_wrapper, gdrom_unmount_disc );
   309 }
   312 /************************* LxdreamView native entry points **************************/
   314 JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_setSurface(JNIEnv * env, jobject view, jobject surface, jint width, jint height)
   315 {
   316     current_surface.win = ANativeWindow_fromSurface(env, surface);
   317     current_surface.width = width;
   318     current_surface.height = height;
   319     int fmt = ANativeWindow_getFormat(current_surface.win);
   320     if( fmt == WINDOW_FORMAT_RGB_565 ) {
   321         current_surface.format = COLFMT_RGB565;
   322     } else {
   323         current_surface.format = COLFMT_RGB888;
   324     }
   325     tqueue_post_message( android_set_surface, &current_surface );
   326 }
   328 JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_clearSurface(JNIEnv * env, jobject view, jobject surface)
   329 {
   330     /* Need to make sure this completely shuts down before we return */
   331     tqueue_send_message( android_clear_surface, &current_surface );
   332 }
   335 /************************* JNI Support functions **************************/
   337 static char *getStringChars( JNIEnv *env, jstring str )
   338 {
   339     jboolean iscopy;
   340     const char *p = (*env)->GetStringUTFChars(env, str, &iscopy);
   341     char *result = strdup(p);
   342     (*env)->ReleaseStringUTFChars(env,str,p);
   343     return result;
   344 }
.