add menu/shortcut capability to connect/disconnect/reconnect

This commit is contained in:
cy384 2021-12-29 20:32:01 -05:00
parent 1521e6f852
commit 29c27221a5
6 changed files with 110 additions and 19 deletions

View File

@ -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

View File

@ -9,6 +9,7 @@
#include "MacTypes.h"
void reset_console(void);
void console_setup(void);
void draw_screen(Rect* r);

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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;