Skip to content

Commit

Permalink
Merge pull request #36 from olin/frontend-fixes
Browse files Browse the repository at this point in the history
Frontend fixes
  • Loading branch information
labseven authored May 5, 2019
2 parents bc41358 + 53e8790 commit b08e0a2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@
#include "frontend.h"
#include "mode_id.h"
#include "state.h"
#include "util.h"

#include <stdio.h>
#include <unistd.h>

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
* ___________________________________________
* | 0 -- X, COLS | canvas window
Expand All @@ -30,6 +43,19 @@ View *view;
*/

int main(int argc, char *argv[]) {
#ifdef LOG_TO_FILE
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);
}
#endif
logd("Starting frontend\n");

/* initialize your non-curses data structures here */

(void)signal(SIGINT, finish); /* arrange interrupts to terminate */
Expand Down Expand Up @@ -60,13 +86,16 @@ 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
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
// Move cursor to starting location and redraw canvases
refresh_screen();

//// Main loop
State new_state = {
Expand All @@ -87,9 +116,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();
}

Expand Down Expand Up @@ -223,6 +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);
}
17 changes: 17 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef util_h
#define util_h

#include <stdio.h>

// printf to stderr
#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

0 comments on commit b08e0a2

Please sign in to comment.