From 1286f816fced71b30010cabb2082f003acff6c37 Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Fri, 3 May 2019 16:04:53 -0400 Subject: [PATCH 1/7] Remove duplicate function calls This was doing the same as refresh_screen except for updating the screen size and redrawing the window --- src/frontend.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/frontend.c b/src/frontend.c index 13aa43c..9300431 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -64,9 +64,7 @@ int main(int argc, char *argv[]) { print_status(test_msg, status_win); // Move cursor to starting location and redraw - wmove(canvas_win, cursor_y_to_canvas(cursor), cursor_x_to_canvas(cursor)); - wrefresh(status_win); - wrefresh(canvas_win); // Refresh Canvas last so it gets the cursor + refresh_screen(); //// Main loop State new_state = { From fa7690884c9781856415a062fed6dbbf642771b2 Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Fri, 3 May 2019 17:09:44 -0400 Subject: [PATCH 2/7] Draw status window before first keypress --- src/frontend.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/frontend.c b/src/frontend.c index 9300431..dad758c 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -60,10 +60,15 @@ int main(int argc, char *argv[]) { keypad(canvas_win, TRUE); keypad(status_win, TRUE); + // update the screen size first. This clears the status window on any changes + // (including the first time it's run), so refreshing after updating the + // status will clear it otherwise + update_screen_size(); + char test_msg[] = "Test mode"; print_status(test_msg, status_win); - // Move cursor to starting location and redraw + // Move cursor to starting location and redraw canvases refresh_screen(); //// Main loop From 5a8eb93fd540f2a24105c9555511033b443c8d7f Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Fri, 3 May 2019 17:11:14 -0400 Subject: [PATCH 3/7] Remove old call to update_screen_size The function doesn't accept any parameters, but there weren't any compiler warnings. I just learned that `function()` is different from `function(void)` - the former is a prototype, not a function that accepts zero arguments. > Welcome to the C language, this is part of the legacy standard. https://stackoverflow.com/q/12643202 --- src/frontend.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/frontend.c b/src/frontend.c index dad758c..e37f541 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -90,9 +90,6 @@ int main(int argc, char *argv[]) { mode_functions[state->current_mode](state, canvas_win, status_win); - // *(state->last_cursor) = *(state->cursor); - - update_screen_size(canvas_win, status_win, cursor); refresh_screen(); } From a7ee4946de03096835baf3b115109e8adee183b5 Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Fri, 3 May 2019 18:30:00 -0400 Subject: [PATCH 4/7] Redirect stderr --- src/frontend.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/frontend.c b/src/frontend.c index e37f541..f2aea40 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -8,10 +8,16 @@ #include "mode_id.h" #include "state.h" +#include +#include + WINDOW *canvas_win, *status_win; Cursor *cursor; View *view; +char *logfile_path = "out.txt"; +FILE *logfile = NULL; + /* Layout * ___________________________________________ * | 0 -- X, COLS | canvas window @@ -30,6 +36,17 @@ View *view; */ int main(int argc, char *argv[]) { + logfile = fopen(logfile_path, "a"); + if (logfile == NULL) { + perror("logfile fopen:"); + exit(1); + } + if (-1 == dup2(fileno(logfile), fileno(stderr))) { + perror("stderr dup2:"); + exit(1); + } + fprintf(stderr, "Starting\n"); + /* initialize your non-curses data structures here */ (void)signal(SIGINT, finish); /* arrange interrupts to terminate */ @@ -224,5 +241,8 @@ void finish(int sig) { /* do your non-curses wrapup here */ + if (logfile != NULL) { + fclose(logfile); + } exit(0); } From 44ba8ad9b73d2e4466837438aea9a2eb06688aba Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Mon, 15 Apr 2019 14:14:12 -0400 Subject: [PATCH 5/7] Add util header with eprintf --- src/util.h | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/util.h diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..dfda887 --- /dev/null +++ b/src/util.h @@ -0,0 +1,4 @@ +#include + +// printf to stderr +#define eprintf(...) fprintf (stderr, __VA_ARGS__) From 404e0eb482dfd4618d943ecf06432b4340bc08af Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Fri, 3 May 2019 19:30:15 -0400 Subject: [PATCH 6/7] Add debug log macro --- src/util.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util.h b/src/util.h index dfda887..32d0ba9 100644 --- a/src/util.h +++ b/src/util.h @@ -1,4 +1,17 @@ +#ifndef util_h +#define util_h + #include // printf to stderr -#define eprintf(...) fprintf (stderr, __VA_ARGS__) +#define eprintf(...) fprintf(stderr, __VA_ARGS__) + +#ifdef DEBUG +// log to stderr if DEBUG is defined +#define logd(...) eprintf(__VA_ARGS__) +#else +// do nothing +#define logd(...) +#endif + +#endif From 53e8790de7a86b99b8d0611acbaac5766200c93e Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Fri, 3 May 2019 19:30:43 -0400 Subject: [PATCH 7/7] Add define to enable logfile --- src/frontend.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/frontend.c b/src/frontend.c index f2aea40..010db35 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -7,6 +7,7 @@ #include "frontend.h" #include "mode_id.h" #include "state.h" +#include "util.h" #include #include @@ -15,8 +16,14 @@ WINDOW *canvas_win, *status_win; Cursor *cursor; View *view; +#ifdef DEBUG +#define LOG_TO_FILE +#endif + +#ifdef LOG_TO_FILE char *logfile_path = "out.txt"; FILE *logfile = NULL; +#endif /* Layout * ___________________________________________ @@ -36,6 +43,7 @@ FILE *logfile = NULL; */ int main(int argc, char *argv[]) { +#ifdef LOG_TO_FILE logfile = fopen(logfile_path, "a"); if (logfile == NULL) { perror("logfile fopen:"); @@ -45,7 +53,8 @@ int main(int argc, char *argv[]) { perror("stderr dup2:"); exit(1); } - fprintf(stderr, "Starting\n"); +#endif + logd("Starting frontend\n"); /* initialize your non-curses data structures here */ @@ -240,9 +249,10 @@ void finish(int sig) { endwin(); /* do your non-curses wrapup here */ - +#ifdef LOG_TO_FILE if (logfile != NULL) { fclose(logfile); } +#endif exit(0); }