From 29c27221a5badc541dcd5f9a6ad501b4726bde87 Mon Sep 17 00:00:00 2001 From: cy384 Date: Wed, 29 Dec 2021 20:32:01 -0500 Subject: [PATCH] add menu/shortcut capability to connect/disconnect/reconnect --- ssheven-console.c | 34 +++++++++++++++++++ ssheven-console.h | 1 + ssheven-net.c | 1 - ssheven.c | 84 ++++++++++++++++++++++++++++++++++++++--------- ssheven.h | 6 ++-- ssheven.r | 3 ++ 6 files changed, 110 insertions(+), 19 deletions(-) diff --git a/ssheven-console.c b/ssheven-console.c index 8262d8a..3cb2ba2 100644 --- a/ssheven-console.c +++ b/ssheven-console.c @@ -839,6 +839,40 @@ void font_size_change() ForeColor(save_font_fg); } +void reset_console(void) +{ + short save_fg = qd.thePort->fgColor; + short save_bg = qd.thePort->bkColor; + + ForeColor(prefs.fg_color); + BackColor(prefs.bg_color); + + EraseRect(&con.win->portRect); + + ForeColor(save_fg); + BackColor(save_bg); + + con.cursor_x = 0; + con.cursor_y = 0; + + VTermState* vtermstate = vterm_obtain_state(con.vterm); + vterm_state_reset(vtermstate, 1); + + VTermColor fg = { .type = VTERM_COLOR_INDEXED }; + fg.indexed.idx = qd2idx(prefs.fg_color); + + VTermColor bg = { .type = VTERM_COLOR_INDEXED }; + bg.indexed.idx = qd2idx(prefs.bg_color); + + vterm_state_set_default_colors(vtermstate, &fg, &bg); + + vterm_output_set_callback(con.vterm, output_callback, NULL); + + con.vts = vterm_obtain_screen(con.vterm); + vterm_screen_reset(con.vts, 1); + vterm_screen_set_callbacks(con.vts, &vtscrcb, NULL); +} + void console_setup(void) { // don't clobber font settings diff --git a/ssheven-console.h b/ssheven-console.h index a2dccee..620b6a7 100644 --- a/ssheven-console.h +++ b/ssheven-console.h @@ -9,6 +9,7 @@ #include "MacTypes.h" +void reset_console(void); void console_setup(void); void draw_screen(Rect* r); diff --git a/ssheven-net.c b/ssheven-net.c index 3a045d2..7d6b146 100644 --- a/ssheven-net.c +++ b/ssheven-net.c @@ -579,7 +579,6 @@ void* read_thread(void* arg) } else printf_i("unexpected failure: %s\r\n", libssh2_error_string(rc)); ok = 0; - process_login(); } } diff --git a/ssheven.c b/ssheven.c index f84864f..0710c2a 100644 --- a/ssheven.c +++ b/ssheven.c @@ -30,7 +30,7 @@ struct ssheven_ssh_connection ssh_con = { NULL, NULL, kOTInvalidEndpointRef, NUL struct preferences prefs; enum THREAD_COMMAND read_thread_command = WAIT; -enum THREAD_STATE read_thread_state = UNINTIALIZED; +enum THREAD_STATE read_thread_state = UNINITIALIZED; const uint8_t ascii_to_control_code[256] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; @@ -488,8 +488,13 @@ int process_menu_select(int32_t result) break; case MENU_FILE: - if (item == 1) preferences_window(); - if (item == 3) exit = 1; + if (item == 1) + { + if (connect() == 0) disconnect(); + } + if (item == 2) disconnect(); + if (item == 4) preferences_window(); + if (item == 6) exit = 1; break; case MENU_EDIT: @@ -552,6 +557,15 @@ int handle_keypress(EventRecord* event) { switch (c) { + case 'k': + if (read_thread_state == UNINITIALIZED || read_thread_state == DONE) + { + if (connect() == 0) disconnect(); + } + break; + case 'd': + if (read_thread_state == OPEN) disconnect(); + break; case 'q': return 1; break; @@ -1151,13 +1165,16 @@ int safety_checks(void) return 1; } -void process_login(void) +int connect(void) { OSStatus err = noErr; int ok = 1; ok = safety_checks(); + // reset the console if we have any crap from earlier + if (read_thread_state == DONE) reset_console(); + BeginUpdate(con.win); draw_screen(&(con.win->portRect)); EndUpdate(con.win); @@ -1205,13 +1222,23 @@ void process_login(void) // if we got the thread, tell it to begin operation if (ok) read_thread_command = READ; - // procede into our main event loop - event_loop(); + // allow disconnecting if we're ok + if (ok) + { + void* menu = GetMenuHandle(MENU_FILE); + DisableItem(menu, 1); + EnableItem(menu, 2); + } + return ok; +} + +void disconnect(void) +{ // tell the read thread to finish, then let it run to actually do so read_thread_command = EXIT; - if (read_thread_state != UNINTIALIZED) + if (read_thread_state != UNINITIALIZED) { while (read_thread_state != DONE) { @@ -1222,19 +1249,28 @@ void process_login(void) } } - if (ssh_con.recv_buffer != NULL) OTFreeMem(ssh_con.recv_buffer); - if (ssh_con.send_buffer != NULL) OTFreeMem(ssh_con.send_buffer); - - if (prefs.pubkey_path != NULL && prefs.pubkey_path[0] != '\0') free(prefs.pubkey_path); - if (prefs.privkey_path != NULL && prefs.privkey_path[0] != '\0') free(prefs.privkey_path); - - if (con.vterm != NULL) vterm_free(con.vterm); + if (ssh_con.recv_buffer != NULL) + { + OTFreeMem(ssh_con.recv_buffer); + ssh_con.recv_buffer = NULL; + } + if (ssh_con.send_buffer != NULL) + { + OTFreeMem(ssh_con.send_buffer); + ssh_con.send_buffer = NULL; + } if (ssh_con.endpoint != kOTInvalidEndpointRef) { - err = OTCancelSynchronousCalls(ssh_con.endpoint, kOTCanceledErr); + OTCancelSynchronousCalls(ssh_con.endpoint, kOTCanceledErr); CloseOpenTransport(); + ssh_con.endpoint = kOTInvalidEndpointRef; } + + // allow connecting if we're disconnected + void* menu = GetMenuHandle(MENU_FILE); + EnableItem(menu, 1); + DisableItem(menu, 2); } int main(int argc, char** argv) @@ -1274,6 +1310,11 @@ int main(int argc, char** argv) DisableItem(menu, 7); DisableItem(menu, 9); + // disable connect and disconnect + menu = GetMenuHandle(MENU_FILE); + DisableItem(menu, 1); + DisableItem(menu, 2); + DrawMenuBar(); generate_key_mapping(); @@ -1300,5 +1341,16 @@ int main(int argc, char** argv) draw_screen(&(con.win->portRect)); EndUpdate(con.win); - process_login(); + int ok = connect(); + + if (!ok) disconnect(); + + event_loop(); + + if (read_thread_command != EXIT) disconnect(); + + if (con.vterm != NULL) vterm_free(con.vterm); + + if (prefs.pubkey_path != NULL && prefs.pubkey_path[0] != '\0') free(prefs.pubkey_path); + if (prefs.privkey_path != NULL && prefs.privkey_path[0] != '\0') free(prefs.privkey_path); } diff --git a/ssheven.h b/ssheven.h index d0e34ed..fbd7dc8 100644 --- a/ssheven.h +++ b/ssheven.h @@ -99,7 +99,7 @@ extern struct preferences prefs; extern char key_to_vterm[256]; enum THREAD_COMMAND { WAIT, READ, EXIT }; -enum THREAD_STATE { UNINTIALIZED, OPEN, CLEANUP, DONE }; +enum THREAD_STATE { UNINITIALIZED, OPEN, CLEANUP, DONE }; extern enum THREAD_COMMAND read_thread_command; extern enum THREAD_STATE read_thread_state; @@ -110,4 +110,6 @@ void set_window_title(WindowPtr w, const char* c_name); OSErr FSpPathFromLocation(FSSpec* spec, int* length, Handle* fullPath); pascal void ButtonFrameProc(DialogRef dlg, DialogItemIndex itemNo); -void process_login(void); + +int connect(void); +void disconnect(void); diff --git a/ssheven.r b/ssheven.r index 19888dd..3f20805 100644 --- a/ssheven.r +++ b/ssheven.r @@ -454,6 +454,9 @@ resource 'MENU' (MENU_FILE) { allEnabled, enabled; "File"; { + "Connect...", noIcon, "K", noMark, plain; + "Disconnect", noIcon, "D", noMark, plain; + "-", noIcon, noKey, noMark, plain; "Preferences...", noIcon, noKey, noMark, plain; "-", noIcon, noKey, noMark, plain; "Quit", noIcon, "Q", noMark, plain;