android: Add facility for java code to wait native code to be ready.
[maemo-rb.git] / firmware / target / hosted / android / lcd-android.c
blob15c844bbd68b95dc3195c3b997da8374948b99d7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2010 Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include <jni.h>
24 #include <string.h>
25 #include "config.h"
26 #include "system.h"
27 #include "kernel.h"
28 #include "lcd.h"
29 #include "button.h"
31 extern JNIEnv *env_ptr;
32 extern jobject RockboxService_instance;
34 static jobject RockboxFramebuffer_instance;
35 static jmethodID java_lcd_update;
36 static jmethodID java_lcd_update_rect;
37 static jmethodID java_lcd_init;
39 static int dpi;
40 static int scroll_threshold;
41 static bool display_on;
43 /* this might actually be called before lcd_init_device() or even main(), so
44 * be sure to only access static storage initalized at library loading,
45 * and not more */
46 void connect_with_java(JNIEnv* env, jobject fb_instance)
48 JNIEnv e = *env;
49 static bool have_class;
51 if (!have_class)
53 jclass fb_class = e->GetObjectClass(env, fb_instance);
54 /* cache update functions */
55 java_lcd_update = e->GetMethodID(env, fb_class,
56 "java_lcd_update",
57 "()V");
58 java_lcd_update_rect = e->GetMethodID(env, fb_class,
59 "java_lcd_update_rect",
60 "(IIII)V");
61 jmethodID get_dpi = e->GetMethodID(env, fb_class,
62 "getDpi", "()I");
63 jmethodID thresh = e->GetMethodID(env, fb_class,
64 "getScrollThreshold", "()I");
65 /* these don't change with new instances so call them now */
66 dpi = e->CallIntMethod(env, fb_instance, get_dpi);
67 scroll_threshold = e->CallIntMethod(env, fb_instance, thresh);
69 java_lcd_init = e->GetMethodID(env, fb_class,
70 "java_lcd_init",
71 "(IILjava/nio/ByteBuffer;)V");
73 have_class = true;
76 /* Create native_buffer */
77 jobject buffer = (*env)->NewDirectByteBuffer(env, lcd_framebuffer,
78 (jlong) FRAMEBUFFER_SIZE);
80 /* we need to setup parts for the java object every time */
81 (*env)->CallVoidMethod(env, fb_instance, java_lcd_init,
82 (jint)LCD_WIDTH, (jint)LCD_HEIGHT, buffer);
86 * Do nothing here and connect with the java object later (if it isn't already)
88 void lcd_init_device(void)
92 void lcd_update(void)
94 if (display_on)
95 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
96 java_lcd_update);
99 void lcd_update_rect(int x, int y, int width, int height)
101 if (display_on)
102 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
103 java_lcd_update_rect, x, y, width, height);
107 * this is called when the surface is created, which called is everytime
108 * the activity is brought in front and the RockboxFramebuffer gains focus
110 * Note this is considered interrupt context
112 JNIEXPORT void JNICALL
113 Java_org_rockbox_RockboxFramebuffer_surfaceCreated(JNIEnv *env, jobject this,
114 jobject surfaceholder)
116 (void)surfaceholder;
118 /* Update RockboxFramebuffer_instance */
119 RockboxFramebuffer_instance = (*env)->NewGlobalRef(env, this);
121 /* possibly a new instance - reconnect */
122 connect_with_java(env, this);
123 display_on = true;
125 /* need to wait for button_queue to be valid to post to */
126 wait_rockbox_ready();
128 send_event(LCD_EVENT_ACTIVATION, NULL);
129 /* Force an update, since the newly created surface is initially black
130 * waiting for the next normal update results in a longish black screen */
131 queue_post(&button_queue, BUTTON_FORCE_REDRAW, 0);
135 * the surface is destroyed everytime the RockboxFramebuffer loses focus and
136 * goes invisible
138 JNIEXPORT void JNICALL
139 Java_org_rockbox_RockboxFramebuffer_surfaceDestroyed(JNIEnv *e, jobject this,
140 jobject surfaceholder)
142 (void)this; (void)surfaceholder;
144 display_on = false;
146 (*e)->DeleteGlobalRef(e, RockboxFramebuffer_instance);
147 RockboxFramebuffer_instance = NULL;
150 bool lcd_active(void)
152 return display_on;
155 int lcd_get_dpi(void)
157 return dpi;
160 int touchscreen_get_scroll_threshold(void)
162 return scroll_threshold;