From 91a6432c83e495f7b4d352dd0a70d0a3a02c8190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Tue, 29 Jun 2021 15:06:21 +0200 Subject: [PATCH] refactor: split textviewer to more functions and files --- text-viewer/include/text_viewer.h | 23 +-- text-viewer/include/text_viewer_handlers.h | 8 + text-viewer/include/text_viewer_loader.h | 32 ++++ text-viewer/src/main.c | 83 +++++----- text-viewer/src/text_viewer.c | 175 +-------------------- text-viewer/src/text_viewer_handlers.c | 106 +++++++++++++ text-viewer/src/text_viewer_loader.c | 77 +++++++++ 7 files changed, 269 insertions(+), 235 deletions(-) create mode 100644 text-viewer/include/text_viewer_handlers.h create mode 100644 text-viewer/include/text_viewer_loader.h create mode 100644 text-viewer/src/text_viewer_handlers.c create mode 100644 text-viewer/src/text_viewer_loader.c diff --git a/text-viewer/include/text_viewer.h b/text-viewer/include/text_viewer.h index 60f6ec20809d1fe3584d1ab0369bd10f2d7e497b..9ff8f8629a07498bf0be80d1fd42138ffbdd2eed 100644 --- a/text-viewer/include/text_viewer.h +++ b/text-viewer/include/text_viewer.h @@ -4,28 +4,7 @@ #include "gui.h" #include "gui_component_text_view.h" #include "mzapo_pheripherals.h" - -typedef enum { - FILER_SUCCESS, - FILER_NOT_FOUND, - FILER_NO_PERMISSIONS, - FILER_FILE_CANT_OPEN, - FILER_CANNOT_READ, - FILER_UNKNOWN, -} file_error_t; - -typedef struct { - gui_t gui; - char *path; - - mzapo_pheripherals_t pheripherals; - multiline_text_t *multiline_text; - logger_t *logger; - - bool running; - - font_t font; -} text_viewer_t; +#include "text_viewer_loader.h" /** * @brief Create text viewer struct diff --git a/text-viewer/include/text_viewer_handlers.h b/text-viewer/include/text_viewer_handlers.h new file mode 100644 index 0000000000000000000000000000000000000000..ad74044643b910bf119135b7f4b3888c3de6c465 --- /dev/null +++ b/text-viewer/include/text_viewer_handlers.h @@ -0,0 +1,8 @@ +#ifndef __TEXT_VIEWER_HANDLERS_H__ +#define __TEXT_VIEWER_HANDLERS_H__ + +#include "gui.h" + +void gui_text_view_register_commands(gui_t *gui, component_t *text_view); + +#endif // __TEXT_VIEWER_HANDLERS_H__ diff --git a/text-viewer/include/text_viewer_loader.h b/text-viewer/include/text_viewer_loader.h new file mode 100644 index 0000000000000000000000000000000000000000..efe7ea61817f57367524bd43b7ca0971e546fbd5 --- /dev/null +++ b/text-viewer/include/text_viewer_loader.h @@ -0,0 +1,32 @@ +#ifndef __TEXT_VIEWER_LOADER_H__ +#define __TEXT_VIEWER_LOADER_H__ + +#include "mzapo_pheripherals.h" +#include "gui.h" +#include "gui_component_text_view.h" + +typedef enum { + FILER_SUCCESS, + FILER_NOT_FOUND, + FILER_NO_PERMISSIONS, + FILER_FILE_CANT_OPEN, + FILER_CANNOT_READ, + FILER_UNKNOWN, +} file_error_t; + +typedef struct { + gui_t gui; + char *path; + + mzapo_pheripherals_t pheripherals; + multiline_text_t *multiline_text; + logger_t *logger; + + bool running; + + font_t font; +} text_viewer_t; + +file_error_t text_viewer_load_file(text_viewer_t *text_viewer); + +#endif // __TEXT_VIEWER_LOADER_H__ diff --git a/text-viewer/src/main.c b/text-viewer/src/main.c index bab466b94a3ca879911de695d978e23f5f12ed5b..2d3fffd8d51ce29b37cde5e9b5797ad2f5030412 100644 --- a/text-viewer/src/main.c +++ b/text-viewer/src/main.c @@ -21,44 +21,14 @@ typedef enum { ERROR_PHERIPHERALS, } error_t; -int main(int argc, char *argv[]) { - #ifdef COMPUTER - mzapo_sdl_init(); - #endif +error_t text_viewer_start(logger_t *logger, char* file_name, mzapo_rgb_led_t rgb_leds) { struct termios oldstdin; - - logger_t logger = - logger_create(LOG_DEBUG, stdout, stdout, stderr, stderr, NULL); - - /* Try to acquire lock the first */ - if (serialize_lock(1) <= 0) { - printf("System is occupied\n"); - logger_warn(&logger, __FILE__, __FUNCTION__, __LINE__, "System is occupied"); - - if (1) { - /* Wait till application holding lock releases IT or exits */ - logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, - "Waiting"); - serialize_lock(0); - } - } - - mzapo_rgb_led_t rgb_leds = mzapo_create_rgb_led(); - rgb_led_clear(&rgb_leds, LED_LEFT); - rgb_led_clear(&rgb_leds, LED_RIGHT); - - if (argc < 2) { - logger_error(&logger, __FILE__, __FUNCTION__, __LINE__, "Not enough arguments"); - rgb_led_set_red(&rgb_leds, LED_LEFT); - return ERROR_NOT_ENOUGH_ARGUMENTS; - } - display_t display = mzapo_create_display(); mzapo_ledstrip_t ledstrip = mzapo_create_ledstrip(); void* knobs = mzapo_get_knobs_address(); if (!mzapo_check_pheripherals(&ledstrip, &rgb_leds, &display, &knobs)) { - logger_error(&logger, __FILE__, __FUNCTION__, __LINE__, "Could not initialize some of the pheripherals."); + logger_error(logger, __FILE__, __FUNCTION__, __LINE__, "Could not initialize some of the pheripherals."); rgb_led_set_red(&rgb_leds, LED_LEFT); return ERROR_PHERIPHERALS; } @@ -69,28 +39,63 @@ int main(int argc, char *argv[]) { font.size = 20; font.char_spacing = 2; - text_viewer_t text_viewer = text_viewer_create(argv[1], pheripherals, &logger, font); + text_viewer_t text_viewer = text_viewer_create(file_name, pheripherals, logger, font); - logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, + logger_info(logger, __FILE__, __FUNCTION__, __LINE__, "Loading file"); file_error_t error = text_viewer_load_file(&text_viewer); if (error != FILER_SUCCESS) { - file_error_log(&logger, error); + file_error_log(logger, error); rgb_led_set_red(&rgb_leds, LED_RIGHT); return ERROR_CANT_OPEN_FILE; } file_set_nonblocking(STDIN_FILENO, &oldstdin); - logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, + logger_info(logger, __FILE__, __FUNCTION__, __LINE__, "Starting text viewer"); text_viewer_start_loop(&text_viewer); - logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, + logger_info(logger, __FILE__, __FUNCTION__, __LINE__, "Closing application"); text_viewer_destroy(&text_viewer); - display_deinit(&display); + 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 + + logger_t logger = + logger_create(LOG_DEBUG, stdout, stdout, stderr, stderr, NULL); + + /* Try to acquire lock the first */ + if (serialize_lock(1) <= 0) { + printf("System is occupied\n"); + logger_warn(&logger, __FILE__, __FUNCTION__, __LINE__, "System is occupied"); + + if (1) { + /* Wait till application holding lock releases IT or exits */ + logger_info(&logger, __FILE__, __FUNCTION__, __LINE__, + "Waiting"); + serialize_lock(0); + } + } + + mzapo_rgb_led_t rgb_leds = mzapo_create_rgb_led(); + rgb_led_clear(&rgb_leds, LED_LEFT); + rgb_led_clear(&rgb_leds, LED_RIGHT); + + if (argc < 2) { + logger_error(&logger, __FILE__, __FUNCTION__, __LINE__, "Not enough arguments"); + rgb_led_set_red(&rgb_leds, LED_LEFT); + return ERROR_NOT_ENOUGH_ARGUMENTS; + } + + error_t rerror = text_viewer_start(&logger, argv[1], rgb_leds); serialize_unlock(); @@ -100,5 +105,5 @@ int main(int argc, char *argv[]) { #ifdef COMPUTER mzapo_sdl_deinit(); #endif - return ERROR_SUCCESS; + return rerror; } diff --git a/text-viewer/src/text_viewer.c b/text-viewer/src/text_viewer.c index 5c51793f3547b288a7bab6444d3c553f52bee5d0..a9020dfebc2b2a19bc45a5bd36597f2a6e35731f 100644 --- a/text-viewer/src/text_viewer.c +++ b/text-viewer/src/text_viewer.c @@ -1,6 +1,7 @@ #include "text_viewer.h" #include "direction.h" #include "display_utils.h" +#include "text_viewer_handlers.h" #include "gui.h" #include "gui_component_line.h" #include "gui_component_text.h" @@ -44,79 +45,6 @@ void text_viewer_destroy(text_viewer_t *text_viewer) { } } -file_error_t file_error_from_errno() { - switch (errno) { - case ENOENT: - return FILER_NOT_FOUND; - case EACCES: - return FILER_NO_PERMISSIONS; - default: - return FILER_FILE_CANT_OPEN; - } -} - -file_error_t text_viewer_load_file(text_viewer_t *text_viewer) { - FILE *file = fopen(text_viewer->path, "r"); - - if (file == NULL) { - return file_error_from_errno(); - } - - fseek(file, 0, SEEK_END); - long fsize = ftell(file); - fseek(file, 0, SEEK_SET); - - char *data = malloc(sizeof(char) * (fsize + 2)); - data[fsize - 1] = '\0'; - data[fsize] = '\0'; - data[fsize + 1] = '\0'; - - if (data == NULL) { - return FILER_UNKNOWN; - } - - long read = 0; - const int perc = 5; - const int iters = 100 / perc; - for (int i = 0; i < iters; i++) { - long to_read = fsize / iters; - if (to_read == 0) { - i = iters - 1; - } - - if (i == iters - 1) { - to_read = fsize - read - 1; - } - - if (to_read == 0 || fsize == read) { - break; - } - - long result = fread(data + read, sizeof(char), to_read, file); - read += result; - - if (result != to_read) { - fclose(file); - return FILER_CANNOT_READ; - } - - ledstrip_progress_bar_step(text_viewer->pheripherals.ledstrip, i * perc); - } - - fclose(file); - - multiline_text_t *text = - gui_multiline_text_create(&text_viewer->font, WHITE_PIXEL, data); - if (text == NULL) { - return FILER_UNKNOWN; - } - - text_viewer->multiline_text = text; - - ledstrip_clear(text_viewer->pheripherals.ledstrip); - return FILER_SUCCESS; -} - static void text_viewer_init_gui(text_viewer_t *text_viewer, commands_t *commands, renderer_t *renderer) { gui_t gui = gui_create(text_viewer->logger, commands, renderer, @@ -180,70 +108,6 @@ static component_t *text_viewer_gui_add_text_view(text_viewer_t *text_viewer, return gui_one_container_set_component(view_container, text_view); } -static void command_handler_move(void *state, direction_t direction, - int amount) { - component_t *text_view = (component_t *)state; - if (text_view->focused) { - int32_t x = 0; - int32_t y = 0; - direction_move_xy(direction, &x, &y, amount); - gui_text_view_scroll(text_view, x, y); - } -} - -static void command_handler_move_down(void *state, int amount) { - command_handler_move(state, DOWN, amount); -} - -static void command_handler_move_up(void *state, int amount) { - command_handler_move(state, UP, amount); -} - -static void command_handler_move_left(void *state, int amount) { - command_handler_move(state, LEFT, amount); -} -static void command_handler_move_right(void *state, int amount) { - command_handler_move(state, RIGHT, amount); -} - -static void command_handler_reset(void *state, int amount) { - gui_text_view_reset_scroll((component_t *)state); -} - -static void command_handler_full_scroll(void *state, int amount) { - gui_text_view_full_scroll((component_t *)state); -} - -static void command_handler_zoom_in(void *state, int amount) { - component_t *component = (component_t *)state; - multiline_text_t* text = (multiline_text_t*) (component)->state; - uint16_t old_size = text->font->size; - - amount = amount > 1 ? 1 : -1; - text->font->size += amount; - if (text->font->size == 0) { - text->font->size = 1; - } - - component->y += amount * (component->y / (old_size + text->font->line_spacing)); -} - -static void command_handler_zoom_out(void *state, int amount) { - command_handler_zoom_in(state, -amount); -} - -static void command_handler_zoom_reset(void *state, int amount) { - component_t *component = (component_t *)state; - multiline_text_t *text = (multiline_text_t *)(component)->state; - uint16_t old_size = text->font->size; - text->font->size = text->font->font.height; - - - amount = text->font->size - old_size; - component->y += - amount * (component->y / (old_size + text->font->line_spacing)); -} - component_t gui_text_view_create(gui_t *gui, multiline_text_t *text, int16_t x, int16_t y) { component_t text_view = gui_component_create(x, y, 1, 1, gui_text_view_render, @@ -254,43 +118,6 @@ component_t gui_text_view_create(gui_t *gui, multiline_text_t *text, int16_t x, return text_view; } -void gui_text_view_register_commands(gui_t *gui, component_t *text_view) { - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_LEFT, - command_handler_move_left, text_view); - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_RIGHT, - command_handler_move_right, text_view); - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_DOWN, - command_handler_move_down, text_view); - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_UP, - command_handler_move_up, text_view); - commands_register(gui->commands, IN_KEYBOARD, 'r', command_handler_reset, - text_view); - commands_register(gui->commands, IN_KEYBOARD, 't', - command_handler_full_scroll, text_view); - commands_register(gui->commands, IN_KEYBOARD, 'f', - command_handler_zoom_reset, text_view); - - commands_register(gui->commands, IN_ENCODER_ROTATE, - ROTATION_ENCODER_HORIZONTAL, command_handler_move_right, - text_view); - - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_IN, - command_handler_zoom_in, text_view); - commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_OUT, - command_handler_zoom_out, text_view); - - commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_ZOOM, - command_handler_zoom_in, text_view); - - commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_VERTICAL, - command_handler_move_down, text_view); - - commands_register(gui->commands, IN_ENCODER_CLICK, ROTATION_ENCODER_VERTICAL, - command_handler_reset, text_view); - commands_register(gui->commands, IN_ENCODER_CLICK, ROTATION_ENCODER_ZOOM, - command_handler_full_scroll, text_view); -} - void text_viewer_start_loop(text_viewer_t *text_viewer) { command_t command_arr[20]; commands_t commands = diff --git a/text-viewer/src/text_viewer_handlers.c b/text-viewer/src/text_viewer_handlers.c new file mode 100644 index 0000000000000000000000000000000000000000..161cff182c52e0b3bcd467647fff576edc67b24b --- /dev/null +++ b/text-viewer/src/text_viewer_handlers.c @@ -0,0 +1,106 @@ +#include "text_viewer_handlers.h" +#include "direction.h" +#include "gui_component_text_view.h" +#include "keyboard_const.h" +#include "rotation_const.h" + +static void command_handler_move(void *state, direction_t direction, + int amount) { + component_t *text_view = (component_t *)state; + if (text_view->focused) { + int32_t x = 0; + int32_t y = 0; + direction_move_xy(direction, &x, &y, amount); + gui_text_view_scroll(text_view, x, y); + } +} + +static void command_handler_move_down(void *state, int amount) { + command_handler_move(state, DOWN, amount); +} + +static void command_handler_move_up(void *state, int amount) { + command_handler_move(state, UP, amount); +} + +static void command_handler_move_left(void *state, int amount) { + command_handler_move(state, LEFT, amount); +} +static void command_handler_move_right(void *state, int amount) { + command_handler_move(state, RIGHT, amount); +} + +static void command_handler_reset(void *state, int amount) { + gui_text_view_reset_scroll((component_t *)state); +} + +static void command_handler_full_scroll(void *state, int amount) { + gui_text_view_full_scroll((component_t *)state); +} + +static void command_handler_zoom_in(void *state, int amount) { + component_t *component = (component_t *)state; + multiline_text_t* text = (multiline_text_t*) (component)->state; + uint16_t old_size = text->font->size; + + amount = amount > 1 ? 1 : -1; + text->font->size += amount; + if (text->font->size == 0) { + text->font->size = 1; + } + + component->y += amount * (component->y / (old_size + text->font->line_spacing)); +} + +static void command_handler_zoom_out(void *state, int amount) { + command_handler_zoom_in(state, -amount); +} + +static void command_handler_zoom_reset(void *state, int amount) { + component_t *component = (component_t *)state; + multiline_text_t *text = (multiline_text_t *)(component)->state; + uint16_t old_size = text->font->size; + text->font->size = text->font->font.height; + + + amount = text->font->size - old_size; + component->y += + amount * (component->y / (old_size + text->font->line_spacing)); +} + +void gui_text_view_register_commands(gui_t *gui, component_t *text_view) { + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_LEFT, + command_handler_move_left, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_RIGHT, + command_handler_move_right, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_DOWN, + command_handler_move_down, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_UP, + command_handler_move_up, text_view); + commands_register(gui->commands, IN_KEYBOARD, 'r', command_handler_reset, + text_view); + commands_register(gui->commands, IN_KEYBOARD, 't', + command_handler_full_scroll, text_view); + commands_register(gui->commands, IN_KEYBOARD, 'f', + command_handler_zoom_reset, text_view); + + commands_register(gui->commands, IN_ENCODER_ROTATE, + ROTATION_ENCODER_HORIZONTAL, command_handler_move_right, + text_view); + + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_IN, + command_handler_zoom_in, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_OUT, + command_handler_zoom_out, text_view); + + commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_ZOOM, + command_handler_zoom_in, text_view); + + commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_VERTICAL, + command_handler_move_down, text_view); + + commands_register(gui->commands, IN_ENCODER_CLICK, ROTATION_ENCODER_VERTICAL, + command_handler_reset, text_view); + commands_register(gui->commands, IN_ENCODER_CLICK, ROTATION_ENCODER_ZOOM, + command_handler_full_scroll, text_view); +} diff --git a/text-viewer/src/text_viewer_loader.c b/text-viewer/src/text_viewer_loader.c new file mode 100644 index 0000000000000000000000000000000000000000..f6e00446f545c1eb638bcb81cb3c50dbf431fb7c --- /dev/null +++ b/text-viewer/src/text_viewer_loader.c @@ -0,0 +1,77 @@ +#include "text_viewer_loader.h" +#include +#include +#include + +static file_error_t file_error_from_errno() { + switch (errno) { + case ENOENT: + return FILER_NOT_FOUND; + case EACCES: + return FILER_NO_PERMISSIONS; + default: + return FILER_FILE_CANT_OPEN; + } +} + +file_error_t text_viewer_load_file(text_viewer_t *text_viewer) { + FILE *file = fopen(text_viewer->path, "r"); + + if (file == NULL) { + return file_error_from_errno(); + } + + fseek(file, 0, SEEK_END); + long fsize = ftell(file); + fseek(file, 0, SEEK_SET); + + char *data = malloc(sizeof(char) * (fsize + 2)); + data[fsize - 1] = '\0'; + data[fsize] = '\0'; + data[fsize + 1] = '\0'; + + if (data == NULL) { + return FILER_UNKNOWN; + } + + long read = 0; + const int perc = 5; + const int iters = 100 / perc; + for (int i = 0; i < iters; i++) { + long to_read = fsize / iters; + if (to_read == 0) { + i = iters - 1; + } + + if (i == iters - 1) { + to_read = fsize - read - 1; + } + + if (to_read == 0 || fsize == read) { + break; + } + + long result = fread(data + read, sizeof(char), to_read, file); + read += result; + + if (result != to_read) { + fclose(file); + return FILER_CANNOT_READ; + } + + ledstrip_progress_bar_step(text_viewer->pheripherals.ledstrip, i * perc); + } + + fclose(file); + + multiline_text_t *text = + gui_multiline_text_create(&text_viewer->font, WHITE_PIXEL, data); + if (text == NULL) { + return FILER_UNKNOWN; + } + + text_viewer->multiline_text = text; + + ledstrip_clear(text_viewer->pheripherals.ledstrip); + return FILER_SUCCESS; +}