diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-08-28 04:56:19 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-08-28 04:56:19 +0330 |
commit | 4cc1b51a5e532f14ece5b11c66cd56091eb97a97 (patch) | |
tree | 41c2b780ca35a79989690d89fab869a7e6021870 /src/ui | |
parent | 4c49de0cb60179f20fc579dfdba5aeca20be8abc (diff) |
fixed memory leaks
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/tui.c | 49 | ||||
-rw-r--r-- | src/ui/tui.h | 2 |
2 files changed, 15 insertions, 36 deletions
diff --git a/src/ui/tui.c b/src/ui/tui.c index 0c88fde..54ae511 100644 --- a/src/ui/tui.c +++ b/src/ui/tui.c @@ -7,28 +7,6 @@ #include <time.h> #include <unistd.h> -void _tui_init_callbacks(TUI *tui) { - const int width = tui_get_width(tui); - const int height = tui_get_height(tui); - - tui->on_click_callbacks = malloc(width * sizeof(ON_CLICK_CALLBACK *)); - for (int i = 0; i < width; ++i) { - tui->on_click_callbacks[i] = malloc(height * sizeof(ON_CLICK_CALLBACK)); - - for (int j = 0; j < height; ++j) { - tui->on_click_callbacks[i][j] = NULL; - } - } -} - -void _tui_delete_callbacks(TUI *tui, int width) { - for (int i = 0; i < width; ++i) { - free(tui->on_click_callbacks[i]); - } - - free(tui->on_click_callbacks); -} - void _tui_clear_cells(TUI *tui) { const TERMINAL_CELL empty = {.c = ' ', .color = COLOR_NO_COLOR, @@ -72,7 +50,6 @@ TUI *tui_init() { // Enable mouse tracking write(STDOUT_FILENO, "\e[?9h", 5); - _tui_init_callbacks(tui); _tui_init_cells(tui); tui_refresh(tui); @@ -89,7 +66,7 @@ void tui_delete(TUI *tui) { tui_move_to(tui->init_cursor_x, tui->init_cursor_y); - _tui_delete_callbacks(tui, width); + _tui_delete_cells(tui); free(tui); } @@ -102,9 +79,7 @@ void tui_refresh(TUI *tui) { if (width == tui_get_width(tui) && height == tui_get_height(tui)) { return; } - _tui_delete_callbacks(tui, width); _tui_delete_cells(tui); - _tui_init_callbacks(tui); _tui_init_cells(tui); } @@ -176,7 +151,7 @@ void _tui_set_cell_on_click_callback(TUI *tui, int x, int y, void tui_handle_mouse_action(TUI *tui, MOUSE_ACTION mouse_action) { const ON_CLICK_CALLBACK callback = - tui->on_click_callbacks[mouse_action.x][mouse_action.y]; + tui->cells[_tui_get_cell_index(tui, mouse_action.x, mouse_action.y)].on_click_callback; if (callback != NULL) { callback(mouse_action); } @@ -433,6 +408,8 @@ void tui_main_loop(TUI *tui, WIDGET_BUILDER widget_builder) { _tui_draw_cells_to_terminal(tui); tui_delete_widget(root_widget); + tui_move_to(30, 30); + printf("%ld",clock()-start); tui_restore_cursor(); if (handle_input(tui)) { return; @@ -522,10 +499,7 @@ COLUMN_METADATA *_tui_make_column_metadata(WIDGET_ARRAY *children) { void _tui_delete_column(WIDGET *column) { COLUMN_METADATA *metadata = column->metadata; - for (size_t i = 0; i < metadata->children->size; ++i) { - tui_delete_widget(metadata->children->widgets[i]); - } - free(metadata->children); + _tui_delete_widget_array(metadata->children); free(column->metadata); } @@ -541,10 +515,7 @@ ROW_METADATA *_tui_make_row_metadata(WIDGET_ARRAY *children) { void _tui_delete_row(WIDGET *row) { ROW_METADATA *metadata = row->metadata; - for (size_t i = 0; i < metadata->children->size; ++i) { - tui_delete_widget(metadata->children->widgets[i]); - } - free(metadata->children); + _tui_delete_widget_array(metadata->children); free(row->metadata); } @@ -585,3 +556,11 @@ WIDGET_ARRAY *tui_make_widget_array(int size, ...) { return widget_array; } + +void _tui_delete_widget_array(WIDGET_ARRAY *widget_array){ + for (size_t i = 0; i < widget_array->size; ++i) { + tui_delete_widget(widget_array->widgets[i]); + } + free(widget_array->widgets); + free(widget_array); +} diff --git a/src/ui/tui.h b/src/ui/tui.h index 15b65d6..0ca71b3 100644 --- a/src/ui/tui.h +++ b/src/ui/tui.h @@ -47,7 +47,6 @@ typedef struct TERMINAL_CELL { typedef struct TUI { struct winsize size; struct termios original, raw, helper; - ON_CLICK_CALLBACK **on_click_callbacks; int init_cursor_x, init_cursor_y; TERMINAL_CELL *cells; size_t cells_length; @@ -142,5 +141,6 @@ extern BOX_METADATA *_tui_make_box_metadata(WIDGET *child, int width, extern void _tui_delete_box(WIDGET *box); extern WIDGET_ARRAY *tui_make_widget_array(int size, ...); +extern void _tui_delete_widget_array(WIDGET_ARRAY *widget_array); #endif |