From c9625876f63ac407dcb9151f8488c733fb52cf66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Tue, 29 Jun 2021 14:30:00 +0200 Subject: [PATCH] refactor: split filebrowser main to more functions --- file-browser/src/main.c | 126 +++++++++--------- lib-pheripherals/include/mzapo_pheripherals.h | 2 + lib-pheripherals/src/mzapo_pheripherals.c | 7 + 3 files changed, 74 insertions(+), 61 deletions(-) diff --git a/file-browser/src/main.c b/file-browser/src/main.c index c9e9b064a6ac4a9342129e72a085beec7afa048a..6af2b72c18ddd292a49e8ffb38ae3bb75bbabb64 100644 --- a/file-browser/src/main.c +++ b/file-browser/src/main.c @@ -3,6 +3,8 @@ #include "file_access.h" #include "file_browser.h" +#include "mzapo_led_strip.h" +#include "mzapo_rgb_led.h" #include "options.h" #include "font.h" #include "logger.h" @@ -14,33 +16,29 @@ #include "mzapo_sdl.h" #endif -// soubory otvírat podle mime typů - config s mime typy a přiřazeným editorem -// pokud nic nevyhovuje a je spustitelný, tak spustit -// pokud neplatí ani jedno, otevřít v text vieweru - ten schovat pod mime type -// text, který bude default - // TODO: okna //DONE initial - lokální fs nebo nějaký unmounted //DONE browser - ukazuje current složku, v ní soubory a info o nich, nahoře název //DONE složky -//WIP contextmenu - ukazuje info o souboru a jeho nabídku na smazání, přesunutí, +//TODO contextmenu - ukazuje info o souboru a jeho nabídku na smazání, přesunutí, // zkopírování, zobrazení v textovém move/copy - slouží podobně jako initial a // browser, ale k přesunutí nebo // zkopírování objektu -// options - nastavení nabídka -// mimetypes - jaký mime type otevírá co, defaultně dát jen pár mime types s možností přidat přes stdin (příp. virtuální klávesnici, když zbude čas) -// dialog +//DONE dialog // TODO: dokončit dokumentaci -// options, contextmenu -// TODO: dialog s chybou -// TODO: seřazení souborů, aby . a .. byly první -// spouštění souborů // 15 - 16 +// TODO: controls - textový soubor +// TODO: lepší controls v prohlížeči textu + +// periferie v průzkumníku 10:30 - 11:30 +// učesat kód 11 - 14 (15) +// controls 12 +// context menu 15 - 16 +// DEADLINE kód 16:00 +// DEADLINE dokumentace 23:00 // dokončit dokumentaci a vygenerovat doxygen - úterý -// lazení - ponděli -// očesat kód - pondělí typedef enum { ERROR_SUCCESS, @@ -50,7 +48,7 @@ typedef enum { #define OPTIONS_PATH "eoptions.cfg" -void exec_options_create_default() { +static void exec_options_create_default() { exec_option_t options_arr[] = { {.mime = "image/png", .program = "./bin/image-viewer"}, {.mime = "image/jpeg", .program = "./bin/image-viewer"}, @@ -67,33 +65,71 @@ void exec_options_create_default() { exec_options_save(&options, OPTIONS_PATH); } +static file_operation_error_t exec_options_init(exec_options_loader_t *loader, char *exec_buffer, logger_t *logger, file_operation_error_t error) { + if (error == FILOPER_SUCCESS) { + error = exec_options_loader_load(loader, exec_buffer); + } + + if (error == FILOPER_DOES_NOT_EXIST) { + exec_options_create_default(); + } + + if (error != FILOPER_SUCCESS) { + return error; + } + browser_exec_options = loader->exec_options; + return error; +} + +static error_t file_browser_start(logger_t *logger, mzapo_ledstrip_t ledstrip, mzapo_rgb_led_t rgb_leds, display_t display, void *knobs) { + struct termios oldstdin; + if (!mzapo_check_pheripherals(&ledstrip, &rgb_leds, &display, &knobs)) { + logger_error(logger, __FILE__, __FUNCTION__, __LINE__, + "Could not initialize some of the pheripherals."); + rgb_led_set_red(&rgb_leds, LED_LEFT); + return ERROR_PHERIPHERALS; + } + + mzapo_pheripherals_t pheripherals = + mzapo_pheripherals_create(&ledstrip, &rgb_leds, &display, &knobs); + mzapo_pheripherals_clear(&pheripherals); + + font_t font = font_family_create(font_wTahoma_22, &fontFamily_wTahoma); + font.char_spacing = 2; + + file_browser_t file_browser = + file_browser_create(pheripherals, logger, font); + + file_set_nonblocking(STDIN_FILENO, &oldstdin); + logger_info(logger, __FILE__, __FUNCTION__, __LINE__, + "Starting file browser"); + file_browser_start_loop(&file_browser); + logger_info(logger, __FILE__, __FUNCTION__, __LINE__, "Closing application"); + + file_browser_destroy(&file_browser); + mzapo_pheripherals_clear(&pheripherals); + file_set_blocking(STDIN_FILENO, &oldstdin); + + return ERROR_SUCCESS; +} + int main(int argc, char *argv[]) { #ifdef COMPUTER mzapo_sdl_init(); #endif - struct termios oldstdin; logger_t logger = logger_create(LOG_DEBUG, stdout, stdout, stderr, stderr, NULL); exec_options_loader_t loader = exec_options_loader_create(OPTIONS_PATH); file_operation_error_t error = exec_options_loader_get_size(&loader); - char exec_buffer[loader.bytes_size]; - if (error == FILOPER_SUCCESS) { - error = exec_options_loader_load(&loader, exec_buffer); - } - - if (error == FILOPER_DOES_NOT_EXIST) { - exec_options_create_default(); - } - + error = exec_options_init(&loader, exec_buffer, &logger, error); if (error != FILOPER_SUCCESS) { fileaccess_log_error(&logger, error); return ERROR_OPTIONS; } - browser_exec_options = loader.exec_options; /* Try to acquire lock the first */ if (serialize_lock(1) <= 0) { @@ -108,42 +144,12 @@ int main(int argc, char *argv[]) { } mzapo_rgb_led_t rgb_leds = mzapo_create_rgb_led(); - rgb_led_clear(&rgb_leds, LED_LEFT); - rgb_led_clear(&rgb_leds, LED_RIGHT); display_t display = mzapo_create_display(); mzapo_ledstrip_t ledstrip = mzapo_create_ledstrip(); void* knobs = mzapo_get_knobs_address(); - // TODO: check permissions for mounted devices - - if (!mzapo_check_pheripherals(&ledstrip, &rgb_leds, &display, &knobs)) { - logger_error(&logger, __FILE__, __FUNCTION__, __LINE__, "Could not initialize some of the pheripherals."); - rgb_led_set_red(&rgb_leds, LED_LEFT); - return ERROR_PHERIPHERALS; - } - - mzapo_pheripherals_t pheripherals = mzapo_pheripherals_create(&ledstrip, &rgb_leds, &display, &knobs); - - font_t font = font_family_create(font_wTahoma_22, &fontFamily_wTahoma); - font.char_spacing = 2; - - file_browser_t file_browser = file_browser_create(pheripherals, &logger, font); - - file_set_nonblocking(STDIN_FILENO, &oldstdin); - logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, - "Starting file browser"); - file_browser_start_loop(&file_browser); - logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, - "Closing application"); - - file_browser_destroy(&file_browser); - rgb_led_clear(pheripherals.rgb_leds, LED_LEFT); - rgb_led_clear(pheripherals.rgb_leds, LED_RIGHT); - ledstrip_clear(pheripherals.ledstrip); - display_deinit(&display); - - file_set_blocking(STDIN_FILENO, &oldstdin); + error_t rerror = file_browser_start(&logger, ledstrip, rgb_leds, display, knobs); serialize_unlock(); @@ -153,7 +159,5 @@ int main(int argc, char *argv[]) { #ifdef COMPUTER mzapo_sdl_deinit(); #endif - return ERROR_SUCCESS; - - return 0; + return rerror; } diff --git a/lib-pheripherals/include/mzapo_pheripherals.h b/lib-pheripherals/include/mzapo_pheripherals.h index 33e024ed9cc8771a737adef9147651ebb8c370cf..0775ac183088ffa048eb5236b19a3756c8f5a33b 100644 --- a/lib-pheripherals/include/mzapo_pheripherals.h +++ b/lib-pheripherals/include/mzapo_pheripherals.h @@ -28,6 +28,8 @@ extern "C" { mzapo_rgb_led_t *rgb_led, display_t *display, void **mzapo_knobs_address); + void mzapo_pheripherals_clear(mzapo_pheripherals_t *pheripherals); + #ifdef __cplusplus } /* extern "C"*/ #endif diff --git a/lib-pheripherals/src/mzapo_pheripherals.c b/lib-pheripherals/src/mzapo_pheripherals.c index 075cfe08e4435ced6cee2ed0bb1177ce40f1d86c..3633341e84cd536957d57b16d5b656c5b59ccc93 100644 --- a/lib-pheripherals/src/mzapo_pheripherals.c +++ b/lib-pheripherals/src/mzapo_pheripherals.c @@ -86,3 +86,10 @@ bool mzapo_check_pheripherals(mzapo_ledstrip_t *ledstrip, return true; } + +void mzapo_pheripherals_clear(mzapo_pheripherals_t *pheripherals) { + rgb_led_clear(pheripherals->rgb_leds, LED_LEFT); + rgb_led_clear(pheripherals->rgb_leds, LED_RIGHT); + ledstrip_clear(pheripherals->ledstrip); + display_deinit(pheripherals->display); +}