From 14e45c023020d2ff0bf93db2b818d33d935f41f7 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Wed, 17 Jan 2024 16:03:00 +0000 Subject: [PATCH] Add Text viewer (#87) ## Description Adds a basic text viewer. It does not (yet) support full file content. superseeds #21 ## Motivation and Context There were too many changes to merge from the original PR. ## How Has This Been Tested? On an SC64 via devcontainer. ## Screenshots ## Types of changes - [x] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. Signed-off-by: GITHUB_USER --------- Co-authored-by: Mateusz Faderewski Co-authored-by: Mateusz Faderewski --- Makefile | 1 + src/menu/menu.c | 1 + src/menu/menu_state.h | 2 + src/menu/views/browser.c | 11 +++++ src/menu/views/text_viewer.c | 78 ++++++++++++++++++++++++++++++++++++ src/menu/views/views.h | 3 ++ 6 files changed, 96 insertions(+) create mode 100644 src/menu/views/text_viewer.c diff --git a/Makefile b/Makefile index f9c10b67..7c0ec049 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ SRCS = \ menu/views/fault.c \ menu/views/file_info.c \ menu/views/image_viewer.c \ + menu/views/text_viewer.c \ menu/views/load_disk.c \ menu/views/load_emulator.c \ menu/views/load_rom.c \ diff --git a/src/menu/menu.c b/src/menu/menu.c index 53bd497f..1b8832cb 100644 --- a/src/menu/menu.c +++ b/src/menu/menu.c @@ -148,6 +148,7 @@ static view_t menu_views[] = { { MENU_MODE_FILE_INFO, view_file_info_init, view_file_info_display }, { MENU_MODE_SYSTEM_INFO, view_system_info_init, view_system_info_display }, { MENU_MODE_IMAGE_VIEWER, view_image_viewer_init, view_image_viewer_display }, + { MENU_MODE_TEXT_VIEWER, view_text_viewer_init, view_text_viewer_display }, { MENU_MODE_MUSIC_PLAYER, view_music_player_init, view_music_player_display }, { MENU_MODE_CREDITS, view_credits_init, view_credits_display }, { MENU_MODE_SETTINGS_EDITOR, view_settings_init, view_settings_display }, diff --git a/src/menu/menu_state.h b/src/menu/menu_state.h index 071ce99e..f0d93492 100644 --- a/src/menu/menu_state.h +++ b/src/menu/menu_state.h @@ -29,6 +29,7 @@ typedef enum { MENU_MODE_FILE_INFO, MENU_MODE_SYSTEM_INFO, MENU_MODE_IMAGE_VIEWER, + MENU_MODE_TEXT_VIEWER, MENU_MODE_MUSIC_PLAYER, MENU_MODE_CREDITS, MENU_MODE_SETTINGS_EDITOR, @@ -50,6 +51,7 @@ typedef enum { ENTRY_TYPE_EMULATOR, ENTRY_TYPE_SAVE, ENTRY_TYPE_IMAGE, + ENTRY_TYPE_TEXT, ENTRY_TYPE_MUSIC, ENTRY_TYPE_OTHER, } entry_type_t; diff --git a/src/menu/views/browser.c b/src/menu/views/browser.c index f6c2c9de..01cdb41b 100644 --- a/src/menu/views/browser.c +++ b/src/menu/views/browser.c @@ -14,6 +14,7 @@ static const char *disk_extensions[] = { "ndd", NULL }; static const char *emulator_extensions[] = { "nes", "sfc", "smc", "gb", "gbc", "sms", "gg", "sg", NULL }; static const char *save_extensions[] = { "sav", NULL }; // TODO: "eep", "sra", "srm", "fla" could be used if transfered from different flashcarts. static const char *image_extensions[] = { "png", NULL }; +static const char *text_extensions[] = { "txt", "ini", "yml", "yaml", NULL }; static const char *music_extensions[] = { "mp3", NULL }; @@ -46,6 +47,10 @@ static int compare_entry (const void *pa, const void *pb) { return -1; } else if (b->type == ENTRY_TYPE_IMAGE) { return 1; + } else if (a->type == ENTRY_TYPE_TEXT) { + return -1; + } else if (b->type == ENTRY_TYPE_TEXT) { + return 1; } else if (a->type == ENTRY_TYPE_MUSIC) { return -1; } else if (b->type == ENTRY_TYPE_MUSIC) { @@ -110,6 +115,8 @@ static bool load_directory (menu_t *menu) { entry->type = ENTRY_TYPE_SAVE; } else if (file_has_extensions(info.fname, image_extensions)) { entry->type = ENTRY_TYPE_IMAGE; + } else if (file_has_extensions(info.fname, text_extensions)) { + entry->type = ENTRY_TYPE_TEXT; } else if (file_has_extensions(info.fname, music_extensions)) { entry->type = ENTRY_TYPE_MUSIC; } else { @@ -297,6 +304,9 @@ static void process (menu_t *menu) { case ENTRY_TYPE_IMAGE: menu->next_mode = MENU_MODE_IMAGE_VIEWER; break; + case ENTRY_TYPE_TEXT: + menu->next_mode = MENU_MODE_TEXT_VIEWER; + break; case ENTRY_TYPE_MUSIC: menu->next_mode = MENU_MODE_MUSIC_PLAYER; break; @@ -334,6 +344,7 @@ static void draw (menu_t *menu, surface_t *d) { case ENTRY_TYPE_ROM: action = "A: Load"; break; case ENTRY_TYPE_DISK: action = "A: Load"; break; case ENTRY_TYPE_IMAGE: action = "A: Show"; break; + case ENTRY_TYPE_TEXT: action = "A: View"; break; case ENTRY_TYPE_MUSIC: action = "A: Play"; break; default: action = "A: Info"; break; } diff --git a/src/menu/views/text_viewer.c b/src/menu/views/text_viewer.c new file mode 100644 index 00000000..adef2df9 --- /dev/null +++ b/src/menu/views/text_viewer.c @@ -0,0 +1,78 @@ +#include + +#include "utils/fs.h" +#include "views.h" + +static char *file_content; + +static void process (menu_t *menu) { + if (menu->actions.back) { + menu->next_mode = MENU_MODE_BROWSER; + } +} + +static void draw (menu_t *menu, surface_t *d) { + rdpq_attach(d, NULL); + + component_background_draw(); + + component_layout_draw(); + + component_main_text_draw( + ALIGN_CENTER, VALIGN_TOP, + "TEXT VIEWER\n" + "\n" + ); + + component_main_text_draw( + ALIGN_LEFT, VALIGN_TOP, + "\n" + "\n" + "%s\n", + file_content + ); + + + component_actions_bar_text_draw( + ALIGN_LEFT, VALIGN_TOP, + "\n" + "B: Back" + ); + + rdpq_detach_show(); +} + + +void view_text_viewer_init (menu_t *menu) { + path_t *path = path_clone_push(menu->browser.directory, menu->browser.entry->name); + + uint32_t file_size = file_get_size(path_get(path)); + + if (file_size > 1024) { // FIXME: this is just a placeholder until scrolling is implemented. + file_size = 1024; // For the moment, we just set it to that, since any more would be a waste. + } + + file_content = calloc(file_size, 1); + + // read file content + FIL fil; + UINT br; + + if (f_open(&fil, strip_sd_prefix(path_get(path)), FA_READ) != FR_OK) { + debugf("Error loading file\n"); + } + if (f_read(&fil, file_content, file_size, &br) != FR_OK) { + f_close(&fil); + debugf("Error loading file content\n"); + } + if (f_close(&fil) != FR_OK) { + debugf("Error closing file\n"); + } + + path_free(path); +} + +void view_text_viewer_display (menu_t *menu, surface_t *display) { + process(menu); + draw(menu, display); +} diff --git a/src/menu/views/views.h b/src/menu/views/views.h index 74342dcc..8da900dc 100644 --- a/src/menu/views/views.h +++ b/src/menu/views/views.h @@ -32,6 +32,9 @@ void view_system_info_display (menu_t *menu, surface_t *display); void view_image_viewer_init (menu_t *menu); void view_image_viewer_display (menu_t *menu, surface_t *display); +void view_text_viewer_init (menu_t *menu); +void view_text_viewer_display (menu_t *menu, surface_t *display); + void view_music_player_init (menu_t *menu); void view_music_player_display (menu_t *menu, surface_t *display);