Skip to content

Commit 650f974

Browse files
committed
simulator io support
1 parent 9a92a53 commit 650f974

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

simulator/simulator.c

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static pthread_t t, t2;
1111
static bool s_simulator_running, s_simulator_vol_down_pressed, s_simulator_vol_up_pressed, s_simulator_pwr_pressed;
1212
static uint32_t last_pressed_key;
1313
static JavaVM* s_simulator_jvm;
14-
static jobject s_simulator_bitmap;
14+
static jobject s_simulator_bitmap, s_simulator_thiz;
1515
static jint s_simulator_h, s_simulator_w;
1616

1717
int droidboot_internal_get_display_height()
@@ -24,16 +24,33 @@ int droidboot_internal_get_display_width()
2424
return s_simulator_w;
2525
}
2626

27-
JNIEXPORT void simulator_start(JNIEnv* env, jobject bitmap, jint w, jint h) {
27+
JNIEXPORT void simulator_stop(JNIEnv* env)
28+
{
29+
s_simulator_running = false;
30+
pthread_join(t, NULL);
31+
pthread_join(t2, NULL);
32+
s_simulator_jvm = NULL;
33+
(*env)->DeleteGlobalRef(env, s_simulator_bitmap);
34+
s_simulator_bitmap = NULL;
35+
(*env)->DeleteGlobalRef(env, s_simulator_thiz);
36+
s_simulator_thiz = NULL;
37+
// TODO finish activity
38+
}
39+
40+
JNIEXPORT void simulator_start(JNIEnv* env, jobject thiz, jobject bitmap, jint w, jint h)
41+
{
2842
(*env)->GetJavaVM(env, &s_simulator_jvm);
2943
s_simulator_bitmap = (*env)->NewGlobalRef(env, bitmap);
44+
s_simulator_thiz = (*env)->NewGlobalRef(env, thiz);
3045
s_simulator_h = h;
3146
s_simulator_w = w;
3247
droidboot_init();
3348
droidboot_show_dualboot_menu();
49+
simulator_stop(env);
3450
}
3551

36-
JNIEXPORT void simulator_key(jint key) {
52+
JNIEXPORT void simulator_key(jint key)
53+
{
3754
s_simulator_vol_down_pressed = key == 1;
3855
s_simulator_vol_up_pressed = key == 2;
3956
s_simulator_pwr_pressed = key == 3;
@@ -70,18 +87,17 @@ void droidboot_internal_fb_flush(lv_disp_drv_t * disp_drv, const lv_area_t * are
7087
lv_disp_flush_ready(disp_drv);
7188
}
7289

73-
//Read keys state
7490
void droidboot_internal_key_read(lv_indev_drv_t* drv, lv_indev_data_t* data)
7591
{
76-
if (s_simulator_vol_up_pressed){
92+
if (s_simulator_vol_up_pressed) {
7793
data->key = LV_KEY_PREV;
7894
last_pressed_key = LV_KEY_PREV;
7995
data->state = LV_INDEV_STATE_PRESSED;
80-
} else if (s_simulator_vol_down_pressed){
96+
} else if (s_simulator_vol_down_pressed) {
8197
data->key = LV_KEY_NEXT;
8298
last_pressed_key = LV_KEY_NEXT;
8399
data->state = LV_INDEV_STATE_PRESSED;
84-
} else if (s_simulator_pwr_pressed){
100+
} else if (s_simulator_pwr_pressed) {
85101
data->key = LV_KEY_ENTER;
86102
last_pressed_key = LV_KEY_ENTER;
87103
data->state = LV_INDEV_STATE_PRESSED;
@@ -101,33 +117,52 @@ ssize_t dridboot_internal_sd_read_block(void *buf, uint32_t block, uint count)
101117
{
102118
if (!droidboot_internal_sd_exists())
103119
return 0;
104-
return 0; // TODO
120+
JNIEnv* env;
121+
int ret;
122+
if ((ret = (*s_simulator_jvm)->GetEnv(s_simulator_jvm, (void **) &env, JNI_VERSION_1_6)) != JNI_OK) {
123+
__android_log_print(ANDROID_LOG_ERROR, "droidboot", "failed to get jni env: %d", ret);
124+
}
125+
126+
jclass cls = (*env)->GetObjectClass(env, s_simulator_thiz);
127+
jmethodID readBlk = (*env)->GetMethodID(env, cls, "readBlocks", "(JI)[B");
128+
jbyteArray arr = (*env)->CallObjectMethod(env, s_simulator_thiz, readBlk, (jlong)block*droidboot_internal_sd_blklen(), (jint)count*droidboot_internal_sd_blklen());
129+
jbyte* bufferPtr = (*env)->GetByteArrayElements(env, arr, NULL);
130+
jsize lengthOfArray = (*env)->GetArrayLength(env, arr);
131+
memcpy(buf, bufferPtr, lengthOfArray);
132+
(*env)->ReleaseByteArrayElements(env, arr, bufferPtr, JNI_ABORT);
133+
return lengthOfArray;
105134
}
106135

107136
ssize_t dridboot_internal_sd_write_block(const void *buf, uint32_t block, uint count)
108137
{
109-
if (!droidboot_internal_sd_exists())
110-
return 0;
111-
return 0; // TODO
138+
return 0; // do not break our sd card
112139
}
113140

114141
uint32_t droidboot_internal_sd_blklen()
115142
{
116143
if (!droidboot_internal_sd_exists())
117144
return 0;
118-
return 0; // TODO
145+
return 512;
119146
}
120147

121148
uint64_t droidboot_internal_sd_blkcnt()
122149
{
123150
if (!droidboot_internal_sd_exists())
124151
return 0;
125-
return 0; // TODO
152+
JNIEnv* env;
153+
int ret;
154+
if ((ret = (*s_simulator_jvm)->GetEnv(s_simulator_jvm, (void **) &env, JNI_VERSION_1_6)) != JNI_OK) {
155+
__android_log_print(ANDROID_LOG_ERROR, "droidboot", "failed to get jni env: %d", ret);
156+
}
157+
158+
jclass cls = (*env)->GetObjectClass(env, s_simulator_thiz);
159+
jmethodID countBlk = (*env)->GetMethodID(env, cls, "blockCount", "()J");
160+
return (uint64_t)(*env)->CallLongMethod(env, s_simulator_thiz, countBlk);
126161
}
127162

128163
bool droidboot_internal_sd_exists()
129164
{
130-
return true;
165+
return s_simulator_running;
131166
}
132167

133168
bool droidboot_internal_have_fallback()
@@ -181,16 +216,6 @@ void droidboot_internal_lvgl_threads_init()
181216
pthread_create(&t2, NULL, droidboot_lv_timer_handler_thread, NULL);
182217
}
183218

184-
JNIEXPORT void simulator_stop(JNIEnv* env)
185-
{
186-
s_simulator_running = false;
187-
pthread_join(t, NULL);
188-
pthread_join(t2, NULL);
189-
s_simulator_jvm = NULL;
190-
(*env)->DeleteGlobalRef(env, s_simulator_bitmap);
191-
s_simulator_bitmap = NULL;
192-
}
193-
194219
void droidboot_internal_platform_on_screen_log(const char *buf)
195220
{
196221
__android_log_print(ANDROID_LOG_ERROR, "droidboot", "%s", buf); // TODO

0 commit comments

Comments
 (0)