diff --git a/protocols/ssh/include/terminal.h b/protocols/ssh/include/terminal.h index 8138a812..d9006eeb 100644 --- a/protocols/ssh/include/terminal.h +++ b/protocols/ssh/include/terminal.h @@ -224,6 +224,11 @@ struct guac_terminal { guac_terminal* guac_terminal_create(guac_client* client, int width, int height); +/** + * Resets the state of the given terminal, as if it were just allocated. + */ +void guac_terminal_reset(guac_terminal* term); + /** * Frees all resources associated with the given terminal. */ diff --git a/protocols/ssh/src/terminal.c b/protocols/ssh/src/terminal.c index afc00ef3..b7229877 100644 --- a/protocols/ssh/src/terminal.c +++ b/protocols/ssh/src/terminal.c @@ -55,6 +55,35 @@ #include "terminal.h" #include "terminal_handlers.h" +void guac_terminal_reset(guac_terminal* term) { + + int row; + + /* Set current state */ + term->char_handler = guac_terminal_echo; + + /* Reset cursor location */ + term->cursor_row = term->visible_cursor_row = term->saved_cursor_row = 0; + term->cursor_col = term->visible_cursor_col = term->saved_cursor_col = 0; + + /* Clear scrollback, buffer, and scoll region */ + term->buffer->top = 0; + term->buffer->length = 0; + term->scroll_start = 0; + term->scroll_end = term->term_height - 1; + term->scroll_offset = 0; + + /* Reset flags */ + term->text_selected = false; + term->application_cursor_keys = false; + term->automatic_carriage_return = false; + + /* Clear terminal */ + for (row=0; rowterm_height; row++) + guac_terminal_set_columns(term, row, 0, term->term_width, &(term->default_char)); + +} + guac_terminal* guac_terminal_create(guac_client* client, int width, int height) { @@ -73,7 +102,6 @@ guac_terminal* guac_terminal_create(guac_client* client, /* Init buffer */ term->buffer = guac_terminal_buffer_alloc(1000, &default_char); - term->scroll_offset = 0; /* Init display */ term->display = guac_terminal_display_alloc(client, @@ -84,19 +112,8 @@ guac_terminal* guac_terminal_create(guac_client* client, term->current_attributes = default_char.attributes; term->default_char = default_char; - term->cursor_row = term->visible_cursor_row = term->saved_cursor_row = 0; - term->cursor_col = term->visible_cursor_col = term->saved_cursor_col = 0; - term->term_width = width / term->display->char_width; term->term_height = height / term->display->char_height; - term->char_handler = guac_terminal_echo; - - term->scroll_start = 0; - term->scroll_end = term->term_height - 1; - - term->text_selected = false; - term->application_cursor_keys = false; - term->automatic_carriage_return = false; /* Open STDOUT pipe */ if (pipe(term->stdout_pipe_fd)) { @@ -114,12 +131,15 @@ guac_terminal* guac_terminal_create(guac_client* client, return NULL; } + /* Init terminal lock */ + pthread_mutex_init(&(term->lock), NULL); + /* Size display */ guac_terminal_display_resize(term->display, term->term_width, term->term_height); - /* Init terminal lock */ - pthread_mutex_init(&(term->lock), NULL); + /* Init terminal */ + guac_terminal_reset(term); return term; diff --git a/protocols/ssh/src/terminal_handlers.c b/protocols/ssh/src/terminal_handlers.c index a7891c2d..72e737b3 100644 --- a/protocols/ssh/src/terminal_handlers.c +++ b/protocols/ssh/src/terminal_handlers.c @@ -278,6 +278,17 @@ int guac_terminal_escape(guac_terminal* term, char c) { term->char_handler = guac_terminal_echo; break; + /* DEC Identify */ + case 'Z': + guac_terminal_write_all(term->stdin_pipe_fd[1], "\x1B[?6c", 5); + term->char_handler = guac_terminal_echo; + break; + + /* Reset */ + case 'c': + guac_terminal_reset(term); + break; + default: guac_client_log_info(term->client, "Unhandled ESC sequence: %c", c); term->char_handler = guac_terminal_echo;