From d781e9c8ba863955db6d452173d0c50463acd085 Mon Sep 17 00:00:00 2001 From: a bellenir Date: Mon, 28 Jul 2014 04:16:05 +0000 Subject: [PATCH] split frame from screen. rm some unneeded includes. add focused_workdir getter to state --- source/aardvark.c | 3 +- source/input.c | 26 +++++---- source/main.c | 2 - source/screen.c | 137 ++++++---------------------------------------- source/screen.h | 15 ----- source/state.c | 40 ++++++-------- source/state.h | 9 ++- source/workdir.c | 3 +- 8 files changed, 59 insertions(+), 176 deletions(-) diff --git a/source/aardvark.c b/source/aardvark.c index 25df8cf..a9ebcdf 100644 --- a/source/aardvark.c +++ b/source/aardvark.c @@ -1,7 +1,8 @@ -#include "aardvark.h" #include #include +#include "aardvark.h" + typedef struct { uint8_t a, b, c; } Triplet_T; diff --git a/source/input.c b/source/input.c index 6916a2b..e1ab9b3 100644 --- a/source/input.c +++ b/source/input.c @@ -1,9 +1,11 @@ +#include +#include + #include "input.h" #include "state.h" #include "workdir.h" #include "screen.h" -#include -#include +#include "frame.h" #define ESC 27 @@ -24,39 +26,39 @@ static void handle_quit(void) { } static void handle_next(void) { - workdir_next(state_get_focused_frame()->workdir); + workdir_next(state_get_focused_workdir()); } static void handle_prev(void) { - workdir_prev(state_get_focused_frame()->workdir); + workdir_prev(state_get_focused_workdir()); } static void handle_cd(void) { - workdir_cd(state_get_focused_frame()->workdir); + workdir_cd(state_get_focused_workdir()); } static void handle_scroll_to_top(void) { - workdir_scroll_to_top(state_get_focused_frame()->workdir); + workdir_scroll_to_top(state_get_focused_workdir()); } static void handle_scroll_to_bottom(void) { - workdir_scroll_to_bot(state_get_focused_frame()->workdir); + workdir_scroll_to_bot(state_get_focused_workdir()); } static void handle_page_up(void){ - screen_frame_page_up(state_get_focused_frame()); + frame_page_up(state_get_focused_frame()); } static void handle_page_down(void){ - screen_frame_page_down(state_get_focused_frame()); + frame_page_down(state_get_focused_frame()); } static void handle_expand(void){ - workdir_expand_selected(state_get_focused_frame()->workdir); + workdir_expand_selected(state_get_focused_workdir()); } static void handle_collapse(void){ - workdir_collapse_selected(state_get_focused_frame()->workdir); + workdir_collapse_selected(state_get_focused_workdir()); } static void search_mode(void){ @@ -81,7 +83,7 @@ static void search_mode(void){ searchstr[searchlen] = inpt; searchlen += 1; searchstr[searchlen] = 0; - workdir_seek(state_get_focused_frame()->workdir, searchstr); + workdir_seek(state_get_focused_workdir(), searchstr); } if(state_get_screen_dirty()) screen_update(); } diff --git a/source/main.c b/source/main.c index cf7f83a..2d4dc6d 100644 --- a/source/main.c +++ b/source/main.c @@ -6,11 +6,9 @@ #include /*TODO: anything using this is almost certainly broken on windows */ #include -#include "aardvark.h" #include "state.h" #include "input.h" #include "screen.h" -#include "workdir.h" void handle_signal(int sig) { state_set_screen_dirty(true); diff --git a/source/screen.c b/source/screen.c index 96a1484..8767b40 100644 --- a/source/screen.c +++ b/source/screen.c @@ -11,32 +11,27 @@ /* internal headers */ #include "screen.h" +#include "frame.h" #include "state.h" #include "aardvark.h" -#include "workdir.h" static void screen_place_windows(void); static void screen_refresh_curr_frame(void); -static frame_t* screen_frame_new(void); -static void screen_frame_free(void* p_frame); -void screen_frame_draw_files(frame_t* frame); -static list_t* Screen_List; -static int FrameTopBuffer = 2; -static int FrameBotBuffer = 2; +static list_t* Frame_List; -static frame_t* master_frame(void){ - return (frame_t*) Screen_List->head->contents; +static Frame_T* master_frame(void){ + return (Frame_T*) Frame_List->head->contents; } void screen_init(void) { - Screen_List = list_new(); - list_push_back(Screen_List, screen_frame_new()); + Frame_List = list_new(); + list_push_back(Frame_List, frame_new()); state_set_focused_frame(master_frame()); } void screen_deinit(void) { - mem_release(Screen_List); + mem_release(Frame_List); } void screen_update(void) { @@ -54,15 +49,15 @@ void screen_update(void) { } void screen_open(void) { - list_push_back(Screen_List, screen_frame_new()); + list_push_back(Frame_List, frame_new()); state_set_screen_dirty(true); state_set_screen_resized(true); } void screen_close(void) { - int num_frames = list_size(Screen_List); + int num_frames = list_size(Frame_List); if(num_frames > 1){ - list_delete(Screen_List, 0); + list_delete(Frame_List, 0); state_set_focused_frame(master_frame()); } state_set_screen_dirty(true); @@ -70,23 +65,23 @@ void screen_close(void) { } static void screen_place_windows(void) { - frame_t* p_frame; + Frame_T* p_frame; int id, pos, lines, cols; - int num_frames = list_size(Screen_List); + int num_frames = list_size(Frame_List); list_node_t* p_node; getmaxyx(stdscr, lines, cols); /* Print the master frame */ - p_frame = list_at(Screen_List,0)->contents; + p_frame = list_at(Frame_List,0)->contents; mvwin(p_frame->p_win, 0, 0); wresize(p_frame->p_win, lines, (num_frames > 1) ? cols/2 : cols); wclear(p_frame->p_win); - screen_frame_draw_files(p_frame); + frame_draw_files(p_frame); box(p_frame->p_win, 0 , 0); wrefresh(p_frame->p_win); /* Print any other frames we might have */ - p_node = list_at(Screen_List,1); + p_node = list_at(Frame_List,1); pos = 0; id = 1; while(p_node != NULL) { @@ -98,7 +93,7 @@ static void screen_place_windows(void) { mvwin(p_frame->p_win, pos, cols/2); wresize(p_frame->p_win, height, cols/2); wclear(p_frame->p_win); - screen_frame_draw_files(p_frame); + frame_draw_files(p_frame); wmove(p_frame->p_win, 1, 1); box(p_frame->p_win, 0 , 0); wrefresh(p_frame->p_win); @@ -109,108 +104,12 @@ static void screen_place_windows(void) { } } - static void screen_refresh_curr_frame(void) { /* Print the master frame */ - frame_t* p_frame = list_at(Screen_List,0)->contents; + Frame_T* p_frame = list_at(Frame_List,0)->contents; wclear(p_frame->p_win); - screen_frame_draw_files(p_frame); + frame_draw_files(p_frame); box(p_frame->p_win, 0 , 0); wrefresh(p_frame->p_win); } -//get the curent directory and copy it into a ref-counted memory block -//return a pointer to the new block -char* pwd(){ - char* dir = getcwd(NULL, 0); - char* rid = mem_allocate(sizeof(char)*(1+strlen(dir)), NULL); - strcpy(rid, dir); - free(dir); - return rid; -} - -static frame_t* screen_frame_new(void) { - frame_t* p_frame = (frame_t*)mem_allocate(sizeof(frame_t),&screen_frame_free); - p_frame->p_win = newwin(1, 1, 0, 0); - p_frame->top_index = 0; - bool first_window = !state_get_focused_frame(); - char* path = first_window ? pwd() : state_get_focused_frame()->workdir->path; - p_frame->workdir = workdir_new(path); - if(first_window) mem_release(path); - return p_frame; -} - -static void screen_frame_free(void* p_frame_ptr) { - frame_t* p_frame = (frame_t*)p_frame_ptr; - wclear(p_frame->p_win); - wborder(p_frame->p_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); - wrefresh(p_frame->p_win); - delwin(p_frame->p_win); - if(p_frame->workdir) mem_release(p_frame->workdir); -} - -static int count_double_lines(frame_t* p_frame){ - int count = 0; - for(int i = p_frame->top_index; i <= p_frame->workdir->idx; i++) - if (((File_T*)vec_at(p_frame->workdir->vfiles, i))->expanded) count++; - return count; -} - -static void screen_frame_scroll(frame_t* p_frame){ - int rows,cols; - getmaxyx(p_frame->p_win, rows, cols); - (void) cols; - if(p_frame->workdir->idx < p_frame->top_index){ - p_frame->top_index = p_frame->workdir->idx; - }else{ - int doublelines = count_double_lines(p_frame); - if (p_frame->top_index < doublelines+p_frame->workdir->idx-(rows-FrameTopBuffer-FrameBotBuffer)) - p_frame->top_index = doublelines+p_frame->workdir->idx-(rows-FrameTopBuffer-FrameBotBuffer); - } -} - -void screen_frame_draw_files(frame_t* frame){ - int file_i, frame_i = FrameTopBuffer; - int rows, cols; - getmaxyx(frame->p_win, rows, cols); - screen_frame_scroll(frame); - file_i = frame->top_index; - //draw path - wattron(frame->p_win, A_UNDERLINE); - mvwaddnstr(frame->p_win, 1, 1, frame->workdir->path, cols-2); - wattroff(frame->p_win, A_UNDERLINE); - //list files - while (file_i < vec_size(frame->workdir->vfiles)){ - File_T* file = (File_T*)vec_at(frame->workdir->vfiles, file_i); - bool dir = is_dir(file->path); - if(frame == state_get_focused_frame() && file_i == frame->workdir->idx){ - wattron(frame->p_win, A_STANDOUT | A_BOLD); - } - if(dir) wattron(frame->p_win, COLOR_PAIR(DIRECTORY)); - mvwaddnstr(frame->p_win, frame_i, 1, file->name, cols-2); - frame_i++; - if(file->expanded){ - mvwprintw(frame->p_win, frame_i, 1, " owned by user id %d, group id %d", file->uid, file->gid); - frame_i++; - } - wstandend(frame->p_win); - file_i++; - if((frame_i+FrameBotBuffer) > rows) break; - } -} - -int realrows(frame_t* p_frame){ - int rows, cols; - getmaxyx(p_frame->p_win, rows, cols); - (void) cols; - return rows - FrameTopBuffer - FrameBotBuffer; -} - -void screen_frame_page_up(frame_t* p_frame){ - workdir_set_idx(p_frame->workdir, p_frame->workdir->idx - realrows(p_frame)); -} - -void screen_frame_page_down(frame_t* p_frame){ - workdir_set_idx(p_frame->workdir, p_frame->workdir->idx+realrows(p_frame)); -} - diff --git a/source/screen.h b/source/screen.h index 82dc985..c082b19 100644 --- a/source/screen.h +++ b/source/screen.h @@ -7,26 +7,11 @@ #ifndef SCREEN_H #define SCREEN_H -#include -#include "workdir.h" - void screen_init(void); void screen_deinit(void); void screen_update(void); void screen_open(void); void screen_close(void); -typedef struct { - WINDOW* p_win; - WorkDir_T* workdir; - int top_index; -} frame_t; - -enum ColorPairs { - DIRECTORY = 1 -}; - -void screen_frame_page_up(frame_t* p_frame); -void screen_frame_page_down(frame_t* p_frame); #endif /* SCREEN_H */ diff --git a/source/state.c b/source/state.c index 20e3f84..5972fa6 100644 --- a/source/state.c +++ b/source/state.c @@ -1,3 +1,4 @@ +#include "frame.h" #include "state.h" #include "screen.h" @@ -14,64 +15,59 @@ static bool Resized = true; static bool AardvarkOn = false; /** A pointer to the currently focused frame */ -static frame_t* Focused_Frame = 0; +static Frame_T* Focused_Frame = 0; static Mode_T CurrentMode = 0; -bool state_get_running(void) -{ +bool state_get_running(void) { return Running; } -void state_set_running(bool val) -{ +void state_set_running(bool val) { Running = val; } -bool state_get_screen_dirty(void) -{ +bool state_get_screen_dirty(void) { return Screen_Dirty; } -void state_set_screen_dirty(bool val) -{ +void state_set_screen_dirty(bool val) { Screen_Dirty = val; } -bool state_get_screen_resized(void) -{ +bool state_get_screen_resized(void) { return Resized; } -void state_set_screen_resized(bool val) -{ +void state_set_screen_resized(bool val) { Resized = val; } -bool state_get_aardvark_mode(void) -{ +bool state_get_aardvark_mode(void) { return AardvarkOn; } -void state_set_aardvark_mode(bool val) -{ +void state_set_aardvark_mode(bool val) { AardvarkOn = val; } -frame_t* state_get_focused_frame(void) { +Frame_T* state_get_focused_frame(void) { return Focused_Frame; } -void state_set_focused_frame(frame_t *p_frame) -{ +WorkDir_T* state_get_focused_workdir(void) { + return Focused_Frame->workdir; +} + +void state_set_focused_frame(Frame_T *p_frame) { Focused_Frame = p_frame; } -Mode_T state_get_mode(){ +Mode_T state_get_mode() { return CurrentMode; } -void state_set_mode(Mode_T m){ +void state_set_mode(Mode_T m) { CurrentMode = m; } diff --git a/source/state.h b/source/state.h index cc342ef..de24383 100644 --- a/source/state.h +++ b/source/state.h @@ -8,7 +8,9 @@ #define STATE_H #include -#include "screen.h" +#include "frame.h" +#include "workdir.h" + typedef enum{ MODE_NORMAL, MODE_SEARCH } Mode_T; @@ -20,8 +22,9 @@ 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); -frame_t* state_get_focused_frame(void); -void state_set_focused_frame(frame_t* p_frame); +Frame_T* state_get_focused_frame(void); +WorkDir_T* state_get_focused_workdir(void); +void state_set_focused_frame(Frame_T* p_frame); Mode_T state_get_mode(void); void state_set_mode(Mode_T); diff --git a/source/workdir.c b/source/workdir.c index 19d2583..37a0b01 100644 --- a/source/workdir.c +++ b/source/workdir.c @@ -11,9 +11,8 @@ #include "mem.h" /* internal headers */ -#include "state.h" #include "workdir.h" -#include "screen.h" +#include "state.h" bool is_dir(char* path) { struct stat s; -- 2.52.0