filename | src/gui_android.c |
changeset | 1275:83b15705cdde |
prev | 1253:5aa14eeaad5a |
next | 1277:f727227cc4f8 |
author | nkeynes |
date | Tue Mar 20 08:29:38 2012 +1000 (11 years ago) |
permissions | -rw-r--r-- |
last change | More android WIP - Implement onPause/onResume (although resume is not actually working yet) - Implement BGRA => RGBA texture conversion (BGRA doesn't seem to work on the TFP) Boot swirl is now displayed, albeit depth buffering seems to be broken. |
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 = { NULL, 0, 0, 0 };
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_do_pause(void *data)
93 {
94 if( dreamcast_is_running() ) {
95 dreamcast_stop();
96 }
97 video_egl_clear_window();
98 INFO( "Paused" );
99 return 0;
100 }
102 int android_do_resume(void *data)
103 {
104 struct surface_info *surface = (struct surface_info *)data;
105 if( surface->win != NULL )
106 video_egl_set_window(surface->win, surface->width, surface->height, surface->format);
107 INFO( "Resumed" );
108 return 0;
109 }
111 int android_clear_surface(void *data)
112 {
113 struct surface_info *surface = (struct surface_info *)data;
115 android_do_pause(data); /* If we haven't already stopped, stop now */
116 ANativeWindow_release(surface->win);
117 surface->win = NULL;
118 return 0;
119 }
121 static pthread_t dreamcast_thread;
123 void android_start_thread()
124 {
125 pthread_attr_t attr;
127 pthread_attr_init(&attr);
128 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
129 int status = pthread_create(&dreamcast_thread, &attr, android_thread_main, NULL);
130 if( status != 0 ) {
131 /* Handle errors */
132 }
133 }
135 /** tqueue callback wrapper to get the right call type for simple events */
136 int android_callback_wrapper( void *fn )
137 {
138 void (*cast_fn)(void) = fn;
139 cast_fn();
140 }
142 int android_mount_disc( void *data )
143 {
144 char *s = (char *)data;
145 ERROR err;
146 gboolean result = gdrom_mount_image( s, &err ); /* TODO: Report error */
147 return result;
148 }
150 int android_toggle_run( void *data )
151 {
152 if( dreamcast_is_running() ) {
153 dreamcast_stop();
154 } else {
155 dreamcast_run();
156 }
157 }
159 uint32_t android_gui_run_slice( uint32_t nanosecs )
160 {
161 android_gui_nanos += nanosecs;
162 if( android_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
163 android_gui_nanos -= GUI_TICK_PERIOD;
164 android_gui_ticks ++;
165 uint32_t current_period = android_gui_ticks * GUI_TICK_PERIOD;
167 // Run the event loop
168 tqueue_process_all();
170 struct timeval tv;
171 gettimeofday(&tv,NULL);
172 uint32_t ns = ((tv.tv_sec - android_gui_lasttv.tv_sec) * 1000000000) +
173 (tv.tv_usec - android_gui_lasttv.tv_usec)*1000;
174 if( (ns * 1.05) < current_period ) {
175 // We've gotten ahead - sleep for a little bit
176 struct timespec tv;
177 tv.tv_sec = 0;
178 tv.tv_nsec = current_period - ns;
179 nanosleep(&tv, &tv);
180 }
182 #if 0
183 /* Update the display every 10 ticks (ie 10 times a second) and
184 * save the current tv value */
185 if( android_gui_ticks > 10 ) {
186 android_gui_ticks -= 10;
188 double speed = (float)( (double)current_period * 100.0 / ns );
189 android_gui_lasttv.tv_sec = tv.tv_sec;
190 android_gui_lasttv.tv_usec = tv.tv_usec;
191 main_window_set_speed( main_win, speed );
192 }
193 #endif
194 }
195 return nanosecs;
198 }
200 struct dreamcast_module android_gui_module = { "gui", NULL,
201 android_gui_reset,
202 android_gui_start,
203 android_gui_run_slice,
204 android_gui_stop,
205 NULL, NULL };
207 gboolean gui_error_dialog( const char *fmt, ... )
208 {
209 return FALSE; /* TODO */
210 }
212 void gui_update_state()
213 {
214 /* TODO */
215 }
217 void gui_set_use_grab( gboolean grab )
218 {
219 /* No implementation - mouse grab doesn't exist */
220 }
222 void gui_update_io_activity( io_activity_type activity, gboolean active )
223 {
224 /* No implementation */
225 }
227 void gui_do_later( do_later_callback_t func )
228 {
229 func(); /* TODO */
230 }
232 static void android_init( const char *appHomeDir )
233 {
234 set_global_log_level("info");
235 appHome = appHomeDir;
236 const char *confFile = g_strdup_printf("%s/lxdreamrc", appHome);
237 set_user_data_path(appHome);
238 lxdream_set_config_filename( confFile );
239 lxdream_make_config_dir( );
240 lxdream_load_config( );
241 iso_init();
242 gdrom_list_init();
243 vmulist_init();
244 dreamcast_init(1);
246 dreamcast_register_module( &android_gui_module );
247 audio_init_driver(NULL);
248 display_driver_t display_driver = get_display_driver_by_name(NULL);
249 display_set_driver(display_driver);
251 hotkeys_init();
252 serial_init();
253 maple_reattach_all();
255 ERROR err;
256 gchar *disc_file = lxdream_get_global_config_path_value( CONFIG_GDROM );
257 if( disc_file != NULL ) {
258 gboolean ok = gdrom_mount_image( disc_file, &err );
259 g_free(disc_file);
260 if( !ok ) {
261 WARN( err.msg );
262 } else {
263 INFO( "Mounted %s", disc_file );
264 }
265 }
267 INFO( "%s! ready...", APP_NAME );
268 android_start_thread();
269 }
272 /************************* Dreamcast native entry points **************************/
274 static char *getStringChars( JNIEnv *env, jstring str );
276 /**
277 * Main initialization entry point. We need to do all the setup that main()
278 * would normally do, as well as any UI specific setup.
279 */
280 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_init(JNIEnv * env, jclass obj, jstring homeDir )
281 {
282 android_init( getStringChars(env, homeDir) );
283 }
285 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_run(JNIEnv * env, jclass obj)
286 {
287 tqueue_post_message( android_callback_wrapper, dreamcast_run );
288 }
290 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_toggleRun(JNIEnv * env, jclass obj)
291 {
292 tqueue_post_message( android_toggle_run, NULL );
293 }
295 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_reset(JNIEnv * env, jclass obj)
296 {
297 tqueue_post_message( android_callback_wrapper, dreamcast_reset );
298 }
300 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_stop(JNIEnv * env, jclass obj)
301 {
302 /* Need to make sure this completely shuts down before we return */
303 tqueue_send_message( android_callback_wrapper, dreamcast_stop );
304 }
306 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_onAppPause(JNIEnv * env, jclass obj)
307 {
308 /* Need to make sure this completely shuts down before we return */
309 tqueue_send_message( android_do_pause, ¤t_surface );
310 }
312 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_onAppResume(JNIEnv * env, jclass obj)
313 {
314 tqueue_post_message( android_do_resume, ¤t_surface );
315 }
317 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunning(JNIEnv *env, jclass obj)
318 {
319 return dreamcast_is_running();
320 }
322 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunnable(JNIEnv *env, jclass obj)
323 {
324 return dreamcast_can_run();
325 }
327 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_mount(JNIEnv *env, jclass obj, jstring str)
328 {
329 char *s = getStringChars(env, str);
330 return tqueue_send_message( android_mount_disc, s );
331 }
333 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_unmount(JNIEnv *env, jclass obj)
334 {
335 tqueue_post_message( android_callback_wrapper, gdrom_unmount_disc );
336 }
339 /************************* LxdreamView native entry points **************************/
341 JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_setSurface(JNIEnv * env, jobject view, jobject surface, jint width, jint height)
342 {
343 current_surface.win = ANativeWindow_fromSurface(env, surface);
344 current_surface.width = width;
345 current_surface.height = height;
346 int fmt = ANativeWindow_getFormat(current_surface.win);
347 if( fmt == WINDOW_FORMAT_RGB_565 ) {
348 current_surface.format = COLFMT_RGB565;
349 } else {
350 current_surface.format = COLFMT_RGB888;
351 }
352 INFO( "Setting surface" );
353 tqueue_post_message( android_set_surface, ¤t_surface );
354 }
356 JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_clearSurface(JNIEnv * env, jobject view, jobject surface)
357 {
358 /* Need to make sure this completely shuts down before we return */
359 tqueue_send_message( android_clear_surface, ¤t_surface );
360 }
363 /************************* JNI Support functions **************************/
365 static char *getStringChars( JNIEnv *env, jstring str )
366 {
367 jboolean iscopy;
368 const char *p = (*env)->GetStringUTFChars(env, str, &iscopy);
369 char *result = strdup(p);
370 (*env)->ReleaseStringUTFChars(env,str,p);
371 return result;
372 }
.