From 074059f648fc0d61022e74b119ed41fa911372e1 Mon Sep 17 00:00:00 2001 From: cy384 Date: Fri, 21 Aug 2020 21:29:34 -0400 Subject: [PATCH] add blinking cursor --- ssheven-console.c | 29 +++++++++++++++++++++++++++++ ssheven-console.h | 3 +++ ssheven.c | 10 ++++++++-- ssheven.h | 3 +++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ssheven-console.c b/ssheven-console.c index 23f67b9..6766e40 100644 --- a/ssheven-console.c +++ b/ssheven-console.c @@ -14,8 +14,24 @@ void draw_char(int x, int y, Rect* r, char c) DrawChar(c); } +void toggle_cursor(void) +{ + con.cursor_state = !con.cursor_state; + Rect cursor = cell_rect(con.cursor_x, con.cursor_y, con.win->portRect); + InvalRect(&cursor); } +void check_cursor(void) +{ + long int now = TickCount(); + if ((now - con.last_cursor_blink) > GetCaretTime()) + { + toggle_cursor(); + con.last_cursor_blink = now; + } +} + +// closely inspired by the retro68 console library void draw_screen(Rect* r) { // get the intersection of our console region and the update region @@ -42,7 +58,20 @@ void draw_screen(Rect* r) for(int i = minRow; i < maxRow; i++) { for (int j = minCol; j < maxCol; j++) + { draw_char(j, i, r, con.data[j][i]); + } + } + + // do the cursor if needed + if (con.cursor_state == 1 && + con.cursor_y >= minRow && + con.cursor_y <= maxRow && + con.cursor_x >= minCol && + con.cursor_x <= maxCol) + { + Rect cursor = cell_rect(con.cursor_x, con.cursor_y, con.win->portRect); + InvertRect(&cursor); } TextFont(save_font); diff --git a/ssheven-console.h b/ssheven-console.h index cfc8dcb..ab93929 100644 --- a/ssheven-console.h +++ b/ssheven-console.h @@ -30,3 +30,6 @@ void set_window_title(WindowPtr w, const char* c_name); void ruler(Rect* r); Rect cell_rect(int x, int y, Rect bounds); + +void toggle_cursor(void); +void check_cursor(void); diff --git a/ssheven.c b/ssheven.c index c87d92e..a8a440e 100644 --- a/ssheven.c +++ b/ssheven.c @@ -16,7 +16,7 @@ #define SSH_CHECK(X) rc = (X); if (rc != LIBSSH2_ERROR_NONE) { printf_i("" #X " failed: %s\n", libssh2_error_string(rc)); return 0;}; // sinful globals -struct ssheven_console con = { NULL, {0}, 0, 0, 0 , 0 }; +struct ssheven_console con = { NULL, {0}, 0, 0, 0, 0, 0, 0}; struct ssheven_ssh_connection ssh_con = { NULL, NULL, kOTInvalidEndpointRef, NULL, NULL }; enum { WAIT, READ, EXIT } read_thread_command = WAIT; @@ -274,10 +274,16 @@ void event_loop(void) while (!WaitNextEvent(everyEvent, &event, sleep_time, NULL)) { // timed out without any GUI events - // let any other threads run before we wait for events again + // toggle our cursor if needed + check_cursor(); + + // then let any other threads run before we wait for events again YieldToAnyThread(); } + // might need to toggle our cursor even if we got an event + check_cursor(); + // handle any mac gui events char c = 0; int r = 0; diff --git a/ssheven.h b/ssheven.h index c5ed411..d4faed8 100644 --- a/ssheven.h +++ b/ssheven.h @@ -40,6 +40,9 @@ struct ssheven_console int cell_height; int cell_width; + + int cursor_state; + long int last_cursor_blink; }; extern struct ssheven_console con;