Search
lxdream.org :: lxdream/src/gui_android.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui_android.c
changeset 1245:01e0020adf88
prevsrc/gui_jni.c@1241:74f8e11ab4b8
next1253:5aa14eeaad5a
author nkeynes
date Fri Mar 02 23:49:10 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Android WIP:
* Rename gui_jni.c to gui_android.c - now quite android specific.
* Implement generic EGL driver with very minimal Java wrapper
* Run emulation in separate thread, and implement simple queue for
inter-thread communication.
* Add menu/action-bar items for start + reset
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@1245
    48
static struct surface_info current_surface;
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@1245
    89
    return 0;
nkeynes@1245
    90
}
nkeynes@1245
    91
nkeynes@1245
    92
int android_clear_surface(void *data)
nkeynes@1245
    93
{
nkeynes@1245
    94
    struct surface_info *surface = (struct surface_info *)data;
nkeynes@1245
    95
nkeynes@1245
    96
    if( dreamcast_is_running() ) {
nkeynes@1245
    97
        dreamcast_stop();
nkeynes@1245
    98
    }
nkeynes@1245
    99
    video_egl_clear_window();
nkeynes@1245
   100
    ANativeWindow_release(surface->win);
nkeynes@1245
   101
    surface->win = NULL;
nkeynes@1245
   102
    return 0;
nkeynes@1245
   103
}
nkeynes@1245
   104
nkeynes@1245
   105
static pthread_t dreamcast_thread;
nkeynes@1245
   106
nkeynes@1245
   107
void android_start_thread()
nkeynes@1245
   108
{
nkeynes@1245
   109
    pthread_attr_t attr;
nkeynes@1245
   110
nkeynes@1245
   111
    pthread_attr_init(&attr);
nkeynes@1245
   112
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
nkeynes@1245
   113
    int status = pthread_create(&dreamcast_thread, &attr, android_thread_main, NULL);
nkeynes@1245
   114
    if( status != 0 ) {
nkeynes@1245
   115
        /* Handle errors */
nkeynes@1245
   116
    }
nkeynes@1245
   117
}
nkeynes@1245
   118
nkeynes@1245
   119
/** tqueue callback wrapper to get the right call type for simple events */
nkeynes@1245
   120
int android_callback_wrapper( void *fn )
nkeynes@1245
   121
{
nkeynes@1245
   122
    void (*cast_fn)(void) = fn;
nkeynes@1245
   123
    cast_fn();
nkeynes@1245
   124
}
nkeynes@1245
   125
nkeynes@1245
   126
int android_mount_disc( void *data )
nkeynes@1245
   127
{
nkeynes@1245
   128
    char *s = (char *)data;
nkeynes@1245
   129
    ERROR err;
nkeynes@1245
   130
    gboolean result = gdrom_mount_image( s, &err ); /* TODO: Report error */
nkeynes@1241
   131
    return result;
nkeynes@1241
   132
}
nkeynes@1241
   133
nkeynes@1245
   134
int android_toggle_run( void *data )
nkeynes@1245
   135
{
nkeynes@1245
   136
    if( dreamcast_is_running() ) {
nkeynes@1245
   137
        dreamcast_stop();
nkeynes@1245
   138
    } else {
nkeynes@1245
   139
        dreamcast_run();
nkeynes@1245
   140
    }
nkeynes@1245
   141
}
nkeynes@1241
   142
nkeynes@1245
   143
uint32_t android_gui_run_slice( uint32_t nanosecs )
nkeynes@1241
   144
{
nkeynes@1245
   145
    android_gui_nanos += nanosecs;
nkeynes@1245
   146
    if( android_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@1245
   147
        android_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@1245
   148
        android_gui_ticks ++;
nkeynes@1245
   149
        uint32_t current_period = android_gui_ticks * GUI_TICK_PERIOD;
nkeynes@1245
   150
nkeynes@1245
   151
        // Run the event loop
nkeynes@1245
   152
        tqueue_process_all();
nkeynes@1245
   153
nkeynes@1245
   154
        struct timeval tv;
nkeynes@1245
   155
        gettimeofday(&tv,NULL);
nkeynes@1245
   156
        uint32_t ns = ((tv.tv_sec - android_gui_lasttv.tv_sec) * 1000000000) +
nkeynes@1245
   157
        (tv.tv_usec - android_gui_lasttv.tv_usec)*1000;
nkeynes@1245
   158
        if( (ns * 1.05) < current_period ) {
nkeynes@1245
   159
            // We've gotten ahead - sleep for a little bit
nkeynes@1245
   160
            struct timespec tv;
nkeynes@1245
   161
            tv.tv_sec = 0;
nkeynes@1245
   162
            tv.tv_nsec = current_period - ns;
nkeynes@1245
   163
            nanosleep(&tv, &tv);
nkeynes@1245
   164
        }
nkeynes@1245
   165
nkeynes@1245
   166
#if 0
nkeynes@1245
   167
        /* Update the display every 10 ticks (ie 10 times a second) and
nkeynes@1245
   168
         * save the current tv value */
nkeynes@1245
   169
        if( android_gui_ticks > 10 ) {
nkeynes@1245
   170
            android_gui_ticks -= 10;
nkeynes@1245
   171
nkeynes@1245
   172
            double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@1245
   173
            android_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@1245
   174
            android_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@1245
   175
            main_window_set_speed( main_win, speed );
nkeynes@1245
   176
        }
nkeynes@1245
   177
#endif
nkeynes@1245
   178
    }
nkeynes@1245
   179
    return nanosecs;
nkeynes@1245
   180
nkeynes@1245
   181
nkeynes@1245
   182
}
nkeynes@1245
   183
nkeynes@1245
   184
struct dreamcast_module android_gui_module = { "gui", NULL,
nkeynes@1245
   185
        android_gui_reset,
nkeynes@1245
   186
        android_gui_start,
nkeynes@1245
   187
        android_gui_run_slice,
nkeynes@1245
   188
        android_gui_stop,
nkeynes@1245
   189
        NULL, NULL };
nkeynes@1245
   190
nkeynes@1245
   191
gboolean gui_error_dialog( const char *fmt, ... )
nkeynes@1245
   192
{
nkeynes@1245
   193
    return FALSE; /* TODO */
nkeynes@1245
   194
}
nkeynes@1245
   195
nkeynes@1245
   196
void gui_update_state()
nkeynes@1245
   197
{
nkeynes@1245
   198
    /* TODO */
nkeynes@1245
   199
}
nkeynes@1245
   200
nkeynes@1245
   201
void gui_set_use_grab( gboolean grab )
nkeynes@1245
   202
{
nkeynes@1245
   203
    /* No implementation - mouse grab doesn't exist */
nkeynes@1245
   204
}
nkeynes@1245
   205
nkeynes@1245
   206
void gui_update_io_activity( io_activity_type activity, gboolean active )
nkeynes@1245
   207
{
nkeynes@1245
   208
    /* No implementation */
nkeynes@1245
   209
}
nkeynes@1245
   210
nkeynes@1245
   211
void gui_do_later( do_later_callback_t func )
nkeynes@1245
   212
{
nkeynes@1245
   213
    func(); /* TODO */
nkeynes@1245
   214
}
nkeynes@1245
   215
nkeynes@1245
   216
static void android_init( const char *appHomeDir )
nkeynes@1245
   217
{
nkeynes@1245
   218
    set_global_log_level("info");
nkeynes@1245
   219
    appHome = appHomeDir;
nkeynes@1241
   220
    const char *confFile = g_strdup_printf("%s/lxdreamrc", appHome);
nkeynes@1241
   221
    set_user_data_path(appHome);
nkeynes@1241
   222
    lxdream_set_config_filename( confFile );
nkeynes@1239
   223
    lxdream_make_config_dir( );
nkeynes@1239
   224
    lxdream_load_config( );
nkeynes@1239
   225
    iso_init();
nkeynes@1239
   226
    gdrom_list_init();
nkeynes@1239
   227
    vmulist_init();
nkeynes@1239
   228
    dreamcast_init(1);
nkeynes@1239
   229
nkeynes@1245
   230
    dreamcast_register_module( &android_gui_module );
nkeynes@1239
   231
    audio_init_driver(NULL);
nkeynes@1239
   232
    display_driver_t display_driver = get_display_driver_by_name(NULL);
nkeynes@1239
   233
    display_set_driver(display_driver);
nkeynes@1239
   234
nkeynes@1239
   235
    hotkeys_init();
nkeynes@1239
   236
    serial_init();
nkeynes@1239
   237
    maple_reattach_all();
nkeynes@1239
   238
    INFO( "%s! ready...", APP_NAME );
nkeynes@1245
   239
    android_start_thread();
nkeynes@1239
   240
}
nkeynes@1239
   241
nkeynes@1245
   242
nkeynes@1245
   243
/************************* Dreamcast native entry points **************************/
nkeynes@1245
   244
nkeynes@1245
   245
static char *getStringChars( JNIEnv *env, jstring str );
nkeynes@1245
   246
nkeynes@1245
   247
/**
nkeynes@1245
   248
 * Main initialization entry point. We need to do all the setup that main()
nkeynes@1245
   249
 * would normally do, as well as any UI specific setup.
nkeynes@1245
   250
 */
nkeynes@1245
   251
JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_init(JNIEnv * env, jclass obj,  jstring homeDir )
nkeynes@1239
   252
{
nkeynes@1245
   253
    android_init( getStringChars(env, homeDir) );
nkeynes@1239
   254
}
nkeynes@1239
   255
nkeynes@1241
   256
JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_run(JNIEnv * env, jclass obj)
nkeynes@1239
   257
{
nkeynes@1245
   258
    tqueue_post_message( android_callback_wrapper, dreamcast_run );
nkeynes@1245
   259
}
nkeynes@1245
   260
nkeynes@1245
   261
JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_toggleRun(JNIEnv * env, jclass obj)
nkeynes@1245
   262
{
nkeynes@1245
   263
    tqueue_post_message( android_toggle_run, NULL );
nkeynes@1245
   264
}
nkeynes@1245
   265
nkeynes@1245
   266
JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_reset(JNIEnv * env, jclass obj)
nkeynes@1245
   267
{
nkeynes@1245
   268
    tqueue_post_message( android_callback_wrapper, dreamcast_reset );
nkeynes@1239
   269
}
nkeynes@1239
   270
nkeynes@1241
   271
JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_stop(JNIEnv * env, jclass obj)
nkeynes@1239
   272
{
nkeynes@1245
   273
    /* Need to make sure this completely shuts down before we return */
nkeynes@1245
   274
    tqueue_send_message( android_callback_wrapper, dreamcast_stop );
nkeynes@1239
   275
}
nkeynes@1239
   276
nkeynes@1245
   277
JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunning(JNIEnv *env, jclass obj)
nkeynes@1239
   278
{
nkeynes@1245
   279
    return dreamcast_is_running();
nkeynes@1239
   280
}
nkeynes@1239
   281
nkeynes@1245
   282
JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunnable(JNIEnv *env, jclass obj)
nkeynes@1239
   283
{
nkeynes@1245
   284
    return dreamcast_can_run();
nkeynes@1239
   285
}
nkeynes@1239
   286
nkeynes@1245
   287
JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_mount(JNIEnv *env, jclass obj, jstring str)
nkeynes@1245
   288
{
nkeynes@1245
   289
    char *s = getStringChars(env, str);
nkeynes@1245
   290
    return tqueue_send_message( android_mount_disc, s );
nkeynes@1239
   291
}
nkeynes@1239
   292
nkeynes@1245
   293
JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_unmount(JNIEnv *env, jclass obj)
nkeynes@1239
   294
{
nkeynes@1245
   295
    tqueue_post_message( android_callback_wrapper, gdrom_unmount_disc );
nkeynes@1239
   296
}
nkeynes@1239
   297
nkeynes@1245
   298
nkeynes@1245
   299
/************************* LxdreamView native entry points **************************/
nkeynes@1245
   300
nkeynes@1245
   301
JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_setSurface(JNIEnv * env, jobject view, jobject surface, jint width, jint height)
nkeynes@1239
   302
{
nkeynes@1245
   303
    current_surface.win = ANativeWindow_fromSurface(env, surface);
nkeynes@1245
   304
    current_surface.width = width;
nkeynes@1245
   305
    current_surface.height = height;
nkeynes@1245
   306
    int fmt = ANativeWindow_getFormat(current_surface.win);
nkeynes@1245
   307
    if( fmt == WINDOW_FORMAT_RGB_565 ) {
nkeynes@1245
   308
        current_surface.format = COLFMT_RGB565;
nkeynes@1245
   309
    } else {
nkeynes@1245
   310
        current_surface.format = COLFMT_RGB888;
nkeynes@1245
   311
    }
nkeynes@1245
   312
    tqueue_post_message( android_set_surface, &current_surface );
nkeynes@1239
   313
}
nkeynes@1239
   314
nkeynes@1245
   315
JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_clearSurface(JNIEnv * env, jobject view, jobject surface)
nkeynes@1239
   316
{
nkeynes@1245
   317
    /* Need to make sure this completely shuts down before we return */
nkeynes@1245
   318
    tqueue_send_message( android_clear_surface, &current_surface );
nkeynes@1239
   319
}
nkeynes@1239
   320
nkeynes@1245
   321
nkeynes@1245
   322
/************************* JNI Support functions **************************/
nkeynes@1245
   323
nkeynes@1245
   324
static char *getStringChars( JNIEnv *env, jstring str )
nkeynes@1239
   325
{
nkeynes@1245
   326
    jboolean iscopy;
nkeynes@1245
   327
    const char *p = (*env)->GetStringUTFChars(env, str, &iscopy);
nkeynes@1245
   328
    char *result = strdup(p);
nkeynes@1245
   329
    (*env)->ReleaseStringUTFChars(env,str,p);
nkeynes@1245
   330
    return result;
nkeynes@1239
   331
}
.