From 8c1e949993319f89428324b8b921962f253230c4 Mon Sep 17 00:00:00 2001 From: nift4 Date: Thu, 1 Aug 2024 16:04:46 +0200 Subject: [PATCH] untested simulator --- common/droidboot_platform_common.c | 9 +- common/droidboot_platform_common.h | 5 +- simulator/simulator.c | 132 +++++++++++++++++++++++++---- simulator/simulator.h | 3 +- 4 files changed, 122 insertions(+), 27 deletions(-) diff --git a/common/droidboot_platform_common.c b/common/droidboot_platform_common.c index f4a6bf4..290e0e3 100644 --- a/common/droidboot_platform_common.c +++ b/common/droidboot_platform_common.c @@ -42,12 +42,12 @@ droidboot_error droidboot_platform_init() return droidboot_internal_platform_init(); } -ssize_t dridboot_sd_read_block(void *buf, uint32_t block, uint count) +ssize_t droidboot_sd_read_block(void *buf, uint32_t block, uint count) { return dridboot_internal_sd_read_block(buf, block, count); } -ssize_t dridboot_sd_write_block(const void *buf, uint32_t block, uint count) +ssize_t droidboot_sd_write_block(const void *buf, uint32_t block, uint count) { return dridboot_internal_sd_write_block(buf, block, count); } @@ -72,11 +72,6 @@ bool droidboot_have_fallback() return droidboot_internal_have_fallback(); } -int droidboot_get_disp_buffer_height() -{ - return droidboot_internal_get_disp_buffer_height(); -} - bool droidboot_use_double_buffering() { return droidboot_internal_use_double_buffering(); diff --git a/common/droidboot_platform_common.h b/common/droidboot_platform_common.h index 307311a..77f233a 100644 --- a/common/droidboot_platform_common.h +++ b/common/droidboot_platform_common.h @@ -53,14 +53,13 @@ int droidboot_platform_settings_dev_open(struct ext4_blockdev *bdev); int droidboot_platform_settings_dev_close(struct ext4_blockdev *bdev); droidboot_error droidboot_platform_init(void); -ssize_t dridboot_sd_read_block(void *buf, uint32_t block, uint count); -ssize_t dridboot_sd_write_block(const void *buf, uint32_t block, uint count); +ssize_t droidboot_sd_read_block(void *buf, uint32_t block, uint count); +ssize_t droidboot_sd_write_block(const void *buf, uint32_t block, uint count); uint32_t droidboot_sd_blklen(void); uint64_t droidboot_sd_blkcnt(void); bool droidboot_sd_exists(void); bool droidboot_have_fallback(void); -int droidboot_get_disp_buffer_height(void); bool droidboot_use_double_buffering(void); void droidboot_lvgl_threads_init(void); diff --git a/simulator/simulator.c b/simulator/simulator.c index ed842c2..39902f8 100644 --- a/simulator/simulator.c +++ b/simulator/simulator.c @@ -1,50 +1,124 @@ #include #include "simulator.h" +#include "droidboot_main.h" #include #include +#include +#include +#include -void droidboot_internal_fb_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p) -{ - // TODO -} +static pthread_t t, t2; +static bool s_simulator_running, s_simulator_vol_down_pressed, s_simulator_vol_up_pressed, s_simulator_pwr_pressed; +static uint32_t last_pressed_key; +static JNIEnv* s_simulator_jnienv; +static jobject s_simulator_bitmap; +static jint s_simulator_h, s_simulator_w; int droidboot_internal_get_display_height() { - return 1920; // TODO + return s_simulator_h; } int droidboot_internal_get_display_width() { - return 1080; // TODO + return s_simulator_w; +} + +JNIEXPORT void simulator_start(JNIEnv* env, jobject bitmap, jint w, jint h) { + s_simulator_jnienv = env; + s_simulator_bitmap = bitmap; + s_simulator_h = h; + s_simulator_w = w; + droidboot_init(); + droidboot_show_dualboot_menu(); } -void droidboot_internal_key_read(lv_indev_drv_t * drv, lv_indev_data_t*data) +JNIEXPORT void simulator_key(jint key) { + s_simulator_vol_down_pressed = key == 1; + s_simulator_vol_up_pressed = key == 2; + s_simulator_pwr_pressed = key == 3; +} + +void droidboot_internal_fb_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p) { - // TODO + if (s_simulator_bitmap != NULL) { + void *addr; + while (!AndroidBitmap_lockPixels(s_simulator_jnienv, s_simulator_bitmap, &addr)) { + droidboot_internal_platform_system_log("failed locking bitmap, trying again"); + usleep(10000); + } + int w = (area->x2 - area->x1 + 1); + long int location = 0; + long int byte_location = 0; + unsigned char bit_location = 0; + int32_t y; + for (y = area->y1; y <= area->y2; y++) { + location = (area->x1 + 0) + (y + 0) * droidboot_internal_get_display_width(); + memcpy(&addr[location], (uint32_t *) color_p, (area->x2 - area->y2 + 1) * 4); + color_p += w; + } + + while (!AndroidBitmap_unlockPixels(s_simulator_jnienv, s_simulator_bitmap)) { + droidboot_internal_platform_system_log("failed unlocking bitmap, trying again"); + usleep(10000); + } + } + // Inform the graphics library that we are ready with the flushing + lv_disp_flush_ready(disp_drv); +} + +//Read keys state +void droidboot_internal_key_read(lv_indev_drv_t* drv, lv_indev_data_t* data) +{ + if (s_simulator_vol_up_pressed){ + data->key = LV_KEY_PREV; + last_pressed_key = LV_KEY_PREV; + data->state = LV_INDEV_STATE_PRESSED; + } else if (s_simulator_vol_down_pressed){ + data->key = LV_KEY_NEXT; + last_pressed_key = LV_KEY_NEXT; + data->state = LV_INDEV_STATE_PRESSED; + } else if (s_simulator_pwr_pressed){ + data->key = LV_KEY_ENTER; + last_pressed_key = LV_KEY_ENTER; + data->state = LV_INDEV_STATE_PRESSED; + } else { + data->key=last_pressed_key; + data->state = LV_INDEV_STATE_RELEASED; + } } droidboot_error droidboot_internal_platform_init() { + s_simulator_running = true; return DROIDBOOT_EOK; } ssize_t dridboot_internal_sd_read_block(void *buf, uint32_t block, uint count) { + if (!droidboot_internal_sd_exists()) + return 0; return 0; // TODO } ssize_t dridboot_internal_sd_write_block(const void *buf, uint32_t block, uint count) { + if (!droidboot_internal_sd_exists()) + return 0; return 0; // TODO } uint32_t droidboot_internal_sd_blklen() { + if (!droidboot_internal_sd_exists()) + return 0; return 0; // TODO } uint64_t droidboot_internal_sd_blkcnt() { + if (!droidboot_internal_sd_exists()) + return 0; return 0; // TODO } @@ -58,21 +132,46 @@ bool droidboot_internal_have_fallback() return true; } -int droidboot_internal_get_disp_buffer_height() +bool droidboot_internal_use_double_buffering() { + return true; +} + +//lvgl thread +static void* droidboot_lv_tick_inc_thread(void * arg) { + /*Handle LitlevGL tick*/ + while (s_simulator_running) { + sleep(1); + lv_tick_inc(1); + //lv_timer_handler(); + } return 0; } -bool droidboot_internal_use_double_buffering() -{ - return true; +//lvgl thread +static void* droidboot_lv_timer_handler_thread(void * arg) { + /*Handle LitlevGL tick*/ + while (s_simulator_running) { + sleep(1); + lv_timer_handler(); + } + return 0; } void droidboot_internal_lvgl_threads_init() { - // TODO + pthread_create(&t, NULL, droidboot_lv_tick_inc_thread, NULL); + pthread_create(&t2, NULL, droidboot_lv_timer_handler_thread, NULL); } +JNIEXPORT void simulator_stop() +{ + s_simulator_running = false; + pthread_join(t, NULL); + pthread_join(t2, NULL); + s_simulator_bitmap = NULL; + s_simulator_jnienv = NULL; +} void droidboot_internal_platform_on_screen_log(const char *buf) { @@ -86,7 +185,7 @@ void droidboot_internal_platform_system_log(const char *buf) void droidboot_internal_delay(unsigned int time) { - sleep(time); + usleep(time*1000); } void droidboot_internal_boot_linux_from_ram(void *kernel_raw, off_t kernel_raw_size, void *ramdisk_raw, off_t ramdisk_size, void *dtb_raw, off_t dtb_raw_size, void *dtbo_raw, off_t dtbo_raw_size, char *options) @@ -118,11 +217,14 @@ void *droidboot_internal_get_dtbo_load_addr() { return NULL; } + bool droidboot_internal_append_ramdisk_to_kernel() { return true; } +// Nothing to do here, we have threads void droidboot_internal_platform_tasks() { -} + usleep(200000); +} \ No newline at end of file diff --git a/simulator/simulator.h b/simulator/simulator.h index 0ff7638..cacbaed 100644 --- a/simulator/simulator.h +++ b/simulator/simulator.h @@ -19,7 +19,6 @@ uint32_t droidboot_internal_sd_blklen(); uint64_t droidboot_internal_sd_blkcnt(); bool droidboot_internal_sd_exists(); bool droidboot_internal_have_fallback(); -int droidboot_internal_get_disp_buffer_height(); bool droidboot_internal_use_double_buffering(); void droidboot_internal_lvgl_threads_init(); void droidboot_internal_platform_on_screen_log(const char *buf); @@ -33,6 +32,6 @@ void *droidboot_internal_get_dtb_load_addr(); void *droidboot_internal_get_dtbo_load_addr(); bool droidboot_internal_append_ramdisk_to_kernel(); void droidboot_internal_platform_tasks(); - +void simulator_teardown(); #endif //ABM_SIMULATOR_H