From: Michael D. Lowis Date: Sat, 19 Jul 2014 18:28:02 +0000 (-0400) Subject: Added state.c module for maintining global state X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0d6c7443e2a86293dcfdd139ca8a258abe960933;p=archive%2Fafm.git Added state.c module for maintining global state --- diff --git a/source/main.c b/source/main.c index 561f14b..b78ad15 100644 --- a/source/main.c +++ b/source/main.c @@ -7,6 +7,7 @@ #include #include "aardvark.h" +#include "state.h" typedef struct { int idx; @@ -17,13 +18,8 @@ typedef struct { char* title; } Window_T; -static bool Running = true; -static bool Screen_Dirty = true; -static bool Resized = true; /*TODO: arbitrary number of windows */ static Window_T Windows[1]; -static int FocusedWindex = 0; -static bool AardvarkOn = false; //number of lines to leave before/after dir contents static int TopBuffer = 2; @@ -124,35 +120,37 @@ void list_files(int windex) { } void scroll_down(){ + int index = state_get_focused_frame(); //do nothing if at the end of the file list - if(Windows[FocusedWindex].idx < Windows[FocusedWindex].file_count){ - Windows[FocusedWindex].idx += 1; + if(Windows[index].idx < Windows[index].file_count){ + Windows[index].idx += 1; int rows,cols; getmaxyx(stdscr, rows,cols); (void) cols; - if((TopBuffer+Windows[FocusedWindex].idx+BotBuffer) > rows) - Windows[FocusedWindex].top_index = Windows[FocusedWindex].idx-(rows-TopBuffer-BotBuffer); + if((TopBuffer+Windows[index].idx+BotBuffer) > rows) + Windows[index].top_index = Windows[index].idx-(rows-TopBuffer-BotBuffer); } } void scroll_up(){ + int index = state_get_focused_frame(); //do nothing if at the top of the file list - if(Windows[FocusedWindex].idx > 0){ - Windows[FocusedWindex].idx -= 1; - if(Windows[FocusedWindex].idx < Windows[FocusedWindex].top_index) - Windows[FocusedWindex].top_index = Windows[FocusedWindex].idx; + if(Windows[index].idx > 0){ + Windows[index].idx -= 1; + if(Windows[index].idx < Windows[index].top_index) + Windows[index].top_index = Windows[index].idx; } } void update_screen(void) { /* Clear screen and update LINES and COLS */ - if(Resized){ + if(state_get_screen_resized()){ endwin(); - Resized = false; + state_set_screen_resized(false); } clear(); //should probably redraw all, but since only one window exists, it doesn't matter - list_files(FocusedWindex); - if(AardvarkOn) aardvark_draw(); + list_files(state_get_focused_frame()); + if(state_get_aardvark_mode()) aardvark_draw(); /* Draw the Border */ mvaddch(0, 0, ACS_ULCORNER); mvhline(0, 1, ACS_HLINE, COLS-2); @@ -164,33 +162,34 @@ void update_screen(void) { mvvline(1, COLS-1, ACS_VLINE, LINES-2); /* Refresh and mark complete */ refresh(); - Screen_Dirty = false; + state_set_screen_dirty(false); } void handle_input(char ch) { /* Assume screen is dirty by default */ bool is_screen_dirty = true; switch(ch){ - case 'a': AardvarkOn = !AardvarkOn; + case 'a': state_set_aardvark_mode(!state_get_aardvark_mode()); break; - case 'q': Running = false; + case 'q': state_set_running(false); break; case 'j': scroll_down(); break; case 'k': scroll_up(); break; - case 'e': cd(FocusedWindex); + case 'e': cd(state_get_focused_frame()); break; default: is_screen_dirty = false; break; } - Screen_Dirty = Screen_Dirty || is_screen_dirty; + /* Update the screen if we need to */ + state_set_screen_dirty(state_get_screen_dirty() || is_screen_dirty); } void handle_signal(int sig) { signal(SIGWINCH, handle_signal); - Screen_Dirty = true; - Resized = true; + state_set_screen_dirty(true); + state_set_screen_resized(true); } void init_window_t(windex){ @@ -201,7 +200,7 @@ void init_window_t(windex){ int main(int argc, char** argv) { init_window_t(0); /* Handle terminal resizing */ - //signal(SIGWINCH, handle_signal); + signal(SIGWINCH, handle_signal); /* Initialize ncurses and user input settings */ initscr(); raw(); @@ -209,8 +208,8 @@ int main(int argc, char** argv) { noecho(); timeout(25); refresh(); - while(Running) { - if(Screen_Dirty) update_screen(); + while(state_get_running()) { + if(state_get_screen_dirty()) update_screen(); handle_input(getch()); } erase(); diff --git a/source/state.c b/source/state.c new file mode 100644 index 0000000..18532da --- /dev/null +++ b/source/state.c @@ -0,0 +1,67 @@ +#include "state.h" + +/** Whether the system is currently running or not. */ +static bool Running = true; + +/** Whether the screen should be refreshed or not. */ +static bool Screen_Dirty = true; + +/** Whether the terminal has been resized or not. */ +static bool Resized = true; + +/** Whether the aardvark should be displayed */ +static bool AardvarkOn = false; + +/** The currently focused frame id */ +static int Focused_Index = 0; + +bool state_get_running(void) +{ + return Running; +} + +void state_set_running(bool val) +{ + Running = val; +} + +bool state_get_screen_dirty(void) +{ + return Screen_Dirty; +} + +void state_set_screen_dirty(bool val) +{ + Screen_Dirty = val; +} + +bool state_get_screen_resized(void) +{ + return Resized; +} + +void state_set_screen_resized(bool val) +{ + Resized = val; +} + +bool state_get_aardvark_mode(void) +{ + return AardvarkOn; +} + +void state_set_aardvark_mode(bool val) +{ + AardvarkOn = val; +} + +int state_get_focused_frame(void) +{ + return Focused_Index; +} + +void state_set_focused_frame(int id) +{ + Focused_Index = id; +} + diff --git a/source/state.h b/source/state.h new file mode 100644 index 0000000..6c89af8 --- /dev/null +++ b/source/state.h @@ -0,0 +1,23 @@ +/** + @file state.h + @brief TODO: Describe this file + $Revision$ + $HeadURL$ + */ +#ifndef STATE_H +#define STATE_H + +#include + +bool state_get_running(void); +void state_set_running(bool val); +bool state_get_screen_dirty(void); +void state_set_screen_dirty(bool val); +bool state_get_screen_resized(void); +void state_set_screen_resized(bool val); +bool state_get_aardvark_mode(void); +void state_set_aardvark_mode(bool val); +int state_get_focused_frame(void); +void state_set_focused_frame(int id); + +#endif /* STATE_H */