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