From f2781ed0cc07e122fd6ca2c96ead1b056b45d312 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 5 May 2014 18:58:53 -0700 Subject: [PATCH] GUAC-653: Move mouse and key handling into terminal. Move clipboard into terminal. --- configure.ac | 2 +- src/protocols/ssh/Makefile.am | 4 +- src/protocols/ssh/client.c | 14 -- src/protocols/ssh/client.h | 47 ----- src/protocols/ssh/clipboard.c | 6 +- src/protocols/ssh/guac_handlers.c | 286 +++--------------------------- src/terminal/Makefile.am | 4 +- src/terminal/terminal.c | 284 ++++++++++++++++++++++++++++- src/terminal/terminal.h | 84 ++++++++- 9 files changed, 393 insertions(+), 338 deletions(-) diff --git a/configure.ac b/configure.ac index cfc36b43..cc11d0ca 100644 --- a/configure.ac +++ b/configure.ac @@ -94,7 +94,7 @@ AC_SUBST([COMMON_INCLUDE], '-I$(top_srcdir)/src/common') # Terminal emulator AC_SUBST([TERMINAL_LTLIB], '$(top_builddir)/src/terminal/libguac_terminal.la') -AC_SUBST([TERMINAL_INCLUDE], '-I$(top_srcdir)/src/terminal $(PANGO_CFLAGS) $(PANGOCAIRO_CFLAGS)') +AC_SUBST([TERMINAL_INCLUDE], '-I$(top_srcdir)/src/terminal $(PANGO_CFLAGS) $(PANGOCAIRO_CFLAGS) $(COMMON_INCLUDE)') # Options AC_ARG_WITH(init_dir, diff --git a/src/protocols/ssh/Makefile.am b/src/protocols/ssh/Makefile.am index ff8660dd..28dd9d8f 100644 --- a/src/protocols/ssh/Makefile.am +++ b/src/protocols/ssh/Makefile.am @@ -50,7 +50,7 @@ libguac_client_ssh_la_SOURCES += ssh_agent.c noinst_HEADERS += ssh_agent.h endif -libguac_client_ssh_la_CFLAGS = -Werror -Wall -Iinclude @LIBGUAC_INCLUDE@ @COMMON_INCLUDE@ @TERMINAL_INCLUDE@ -libguac_client_ssh_la_LIBADD = @LIBGUAC_LTLIB@ @COMMON_LTLIB@ @TERMINAL_LTLIB@ +libguac_client_ssh_la_CFLAGS = -Werror -Wall -Iinclude @LIBGUAC_INCLUDE@ @TERMINAL_INCLUDE@ +libguac_client_ssh_la_LIBADD = @LIBGUAC_LTLIB@ @TERMINAL_LTLIB@ libguac_client_ssh_la_LDFLAGS = -version-info 0:0:0 @SSH_LIBS@ @SSL_LIBS@ @PTHREAD_LIBS@ diff --git a/src/protocols/ssh/client.c b/src/protocols/ssh/client.c index 676a5f74..a3f0bcde 100644 --- a/src/protocols/ssh/client.c +++ b/src/protocols/ssh/client.c @@ -125,10 +125,6 @@ int guac_client_init(guac_client* client, int argc, char** argv) { /* Init client data */ client->data = client_data; - client_data->mod_alt = - client_data->mod_ctrl = - client_data->mod_shift = 0; - client_data->clipboard = guac_common_clipboard_alloc(GUAC_SSH_CLIPBOARD_MAX_LENGTH); client_data->term_channel = NULL; if (argc != SSH_ARGS_COUNT) { @@ -186,19 +182,9 @@ int guac_client_init(guac_client* client, int argc, char** argv) { return -1; } - /* Set up I-bar pointer */ - client_data->ibar_cursor = guac_terminal_create_ibar(client); - - /* Set up blank pointer */ - client_data->blank_cursor = guac_terminal_create_blank(client); - /* Send initial name */ guac_protocol_send_name(socket, client_data->hostname); - /* Initialize pointer */ - client_data->current_cursor = client_data->blank_cursor; - guac_terminal_set_cursor(client, client_data->current_cursor); - guac_socket_flush(socket); /* Set basic handlers */ diff --git a/src/protocols/ssh/client.h b/src/protocols/ssh/client.h index 18b326f2..4022f486 100644 --- a/src/protocols/ssh/client.h +++ b/src/protocols/ssh/client.h @@ -26,8 +26,6 @@ #include "config.h" -#include "cursor.h" -#include "guac_clipboard.h" #include "sftp.h" #include "ssh_key.h" #include "terminal.h" @@ -41,11 +39,6 @@ #include "ssh_agent.h" #endif -/** - * The maximum number of bytes to allow within the clipboard. - */ -#define GUAC_SSH_CLIPBOARD_MAX_LENGTH 262144 - /** * SSH-specific client data. */ @@ -148,46 +141,6 @@ typedef struct ssh_guac_client_data { */ guac_terminal* term; - /** - * The current contents of the clipboard. - */ - guac_common_clipboard* clipboard; - - /** - * Whether the alt key is currently being held down. - */ - int mod_alt; - - /** - * Whether the control key is currently being held down. - */ - int mod_ctrl; - - /** - * Whether the shift key is currently being held down. - */ - int mod_shift; - - /** - * The current mouse button state. - */ - int mouse_mask; - - /** - * The cached I-bar cursor. - */ - guac_terminal_cursor* ibar_cursor; - - /** - * The cached invisible (blank) cursor. - */ - guac_terminal_cursor* blank_cursor; - - /** - * The current cursor, used to avoid re-setting the cursor. - */ - guac_terminal_cursor* current_cursor; - } ssh_guac_client_data; #endif diff --git a/src/protocols/ssh/clipboard.c b/src/protocols/ssh/clipboard.c index fce9d77f..5ec732db 100644 --- a/src/protocols/ssh/clipboard.c +++ b/src/protocols/ssh/clipboard.c @@ -23,15 +23,13 @@ #include "config.h" #include "client.h" #include "clipboard.h" -#include "guac_clipboard.h" -#include "guac_iconv.h" int guac_ssh_clipboard_handler(guac_client* client, guac_stream* stream, char* mimetype) { /* Clear clipboard and prepare for new data */ ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data; - guac_common_clipboard_reset(client_data->clipboard, mimetype); + guac_terminal_clipboard_reset(client_data->term, mimetype); /* Set handlers for clipboard stream */ stream->blob_handler = guac_ssh_clipboard_blob_handler; @@ -45,7 +43,7 @@ int guac_ssh_clipboard_blob_handler(guac_client* client, guac_stream* stream, /* Append new data */ ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data; - guac_common_clipboard_append(client_data->clipboard, (char*) data, length); + guac_terminal_clipboard_append(client_data->term, data, length); return 0; } diff --git a/src/protocols/ssh/guac_handlers.c b/src/protocols/ssh/guac_handlers.c index 4ee12f01..cf58c0c2 100644 --- a/src/protocols/ssh/guac_handlers.c +++ b/src/protocols/ssh/guac_handlers.c @@ -65,8 +65,7 @@ int ssh_guac_client_handle_messages(guac_client* client) { int bytes_read = 0; - /* Lock terminal access */ - pthread_mutex_lock(&(client_data->term->lock)); + guac_terminal_lock(client_data->term); /* Read data, write to terminal */ if ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) { @@ -84,14 +83,9 @@ int ssh_guac_client_handle_messages(guac_client* client) { return 1; } - /* Update cursor */ - guac_terminal_commit_cursor(client_data->term); - - /* Flush terminal display */ - guac_terminal_display_flush(client_data->term->display); - - /* Unlock terminal access */ - pthread_mutex_unlock(&(client_data->term->lock)); + /* Flush terminal */ + guac_terminal_flush(client_data->term); + guac_terminal_unlock(client_data->term); } else if (ret_val < 0) { @@ -108,88 +102,10 @@ int ssh_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) { ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data; guac_terminal* term = client_data->term; - /* Determine which buttons were just released and pressed */ - int released_mask = client_data->mouse_mask & ~mask; - int pressed_mask = ~client_data->mouse_mask & mask; - - client_data->mouse_mask = mask; - - /* Show mouse cursor if not already shown */ - if (client_data->current_cursor != client_data->ibar_cursor) { - pthread_mutex_lock(&(term->lock)); - - client_data->current_cursor = client_data->ibar_cursor; - guac_terminal_set_cursor(client, client_data->ibar_cursor); - guac_socket_flush(client->socket); - - pthread_mutex_unlock(&(term->lock)); - } - - /* Paste contents of clipboard on right or middle mouse button up */ - if ((released_mask & GUAC_CLIENT_MOUSE_RIGHT) || (released_mask & GUAC_CLIENT_MOUSE_MIDDLE)) - return guac_terminal_send_data(term, client_data->clipboard->buffer, client_data->clipboard->length); - - /* If text selected, change state based on left mouse mouse button */ - if (term->text_selected) { - pthread_mutex_lock(&(term->lock)); - - /* If mouse button released, stop selection */ - if (released_mask & GUAC_CLIENT_MOUSE_LEFT) { - - int selected_length; - - /* End selection and get selected text */ - int selectable_size = term->term_width * term->term_height * sizeof(char); - char* string = malloc(selectable_size); - guac_terminal_select_end(term, string); - - selected_length = strnlen(string, selectable_size); - - /* Store new data */ - guac_common_clipboard_reset(client_data->clipboard, "text/plain"); - guac_common_clipboard_append(client_data->clipboard, string, selected_length); - free(string); - - /* Send data */ - guac_common_clipboard_send(client_data->clipboard, client); - guac_socket_flush(client->socket); - - } - - /* Otherwise, just update */ - else - guac_terminal_select_update(term, - y / term->display->char_height - term->scroll_offset, - x / term->display->char_width); - - pthread_mutex_unlock(&(term->lock)); - } - - /* Otherwise, if mouse button pressed AND moved, start selection */ - else if (!(pressed_mask & GUAC_CLIENT_MOUSE_LEFT) && - mask & GUAC_CLIENT_MOUSE_LEFT) { - pthread_mutex_lock(&(term->lock)); - - guac_terminal_select_start(term, - y / term->display->char_height - term->scroll_offset, - x / term->display->char_width); - - pthread_mutex_unlock(&(term->lock)); - } - - /* Scroll up if wheel moved up */ - if (released_mask & GUAC_CLIENT_MOUSE_SCROLL_UP) { - pthread_mutex_lock(&(term->lock)); - guac_terminal_scroll_display_up(term, GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT); - pthread_mutex_unlock(&(term->lock)); - } - - /* Scroll down if wheel moved down */ - if (released_mask & GUAC_CLIENT_MOUSE_SCROLL_DOWN) { - pthread_mutex_lock(&(term->lock)); - guac_terminal_scroll_display_down(term, GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT); - pthread_mutex_unlock(&(term->lock)); - } + /* Send mouse event */ + guac_terminal_lock(term); + guac_terminal_send_mouse(term, x, y, mask); + guac_terminal_unlock(term); return 0; @@ -200,144 +116,10 @@ int ssh_guac_client_key_handler(guac_client* client, int keysym, int pressed) { ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data; guac_terminal* term = client_data->term; - /* Hide mouse cursor if not already hidden */ - if (client_data->current_cursor != client_data->blank_cursor) { - pthread_mutex_lock(&(term->lock)); - - client_data->current_cursor = client_data->blank_cursor; - guac_terminal_set_cursor(client, client_data->blank_cursor); - guac_socket_flush(client->socket); - - pthread_mutex_unlock(&(term->lock)); - } - - /* Track modifiers */ - if (keysym == 0xFFE3) - client_data->mod_ctrl = pressed; - else if (keysym == 0xFFE9) - client_data->mod_alt = pressed; - else if (keysym == 0xFFE1) - client_data->mod_shift = pressed; - - /* If key pressed */ - else if (pressed) { - - /* Ctrl+Shift+V shortcut for paste */ - if (keysym == 'V' && client_data->mod_ctrl) - return guac_terminal_send_data(term, client_data->clipboard->buffer, client_data->clipboard->length); - - /* Shift+PgUp / Shift+PgDown shortcuts for scrolling */ - if (client_data->mod_shift) { - - /* Page up */ - if (keysym == 0xFF55) { - pthread_mutex_lock(&(term->lock)); - guac_terminal_scroll_display_up(term, term->term_height); - pthread_mutex_unlock(&(term->lock)); - return 0; - } - - /* Page down */ - if (keysym == 0xFF56) { - pthread_mutex_lock(&(term->lock)); - guac_terminal_scroll_display_down(term, term->term_height); - pthread_mutex_unlock(&(term->lock)); - return 0; - } - - } - - /* Reset scroll */ - if (term->scroll_offset != 0) { - pthread_mutex_lock(&(term->lock)); - guac_terminal_scroll_display_down(term, term->scroll_offset); - pthread_mutex_unlock(&(term->lock)); - } - - /* If alt being held, also send escape character */ - if (client_data->mod_alt) - return guac_terminal_send_string(term, "\x1B"); - - /* Translate Ctrl+letter to control code */ - if (client_data->mod_ctrl) { - - char data; - - /* If valid control code, send it */ - if (keysym >= 'A' && keysym <= 'Z') - data = (char) (keysym - 'A' + 1); - else if (keysym >= 'a' && keysym <= 'z') - data = (char) (keysym - 'a' + 1); - - /* Otherwise ignore */ - else - return 0; - - return guac_terminal_send_data(term, &data, 1); - - } - - /* Translate Unicode to UTF-8 */ - else if ((keysym >= 0x00 && keysym <= 0xFF) || ((keysym & 0xFFFF0000) == 0x01000000)) { - - int length; - char data[5]; - - length = guac_terminal_encode_utf8(keysym & 0xFFFF, data); - return guac_terminal_send_data(term, data, length); - - } - - /* Non-printable keys */ - else { - - if (keysym == 0xFF08) return guac_terminal_send_string(term, "\x7F"); /* Backspace */ - if (keysym == 0xFF09) return guac_terminal_send_string(term, "\x09"); /* Tab */ - if (keysym == 0xFF0D) return guac_terminal_send_string(term, "\x0D"); /* Enter */ - if (keysym == 0xFF1B) return guac_terminal_send_string(term, "\x1B"); /* Esc */ - - if (keysym == 0xFF50) return guac_terminal_send_string(term, "\x1B[1~"); /* Home */ - - /* Arrow keys w/ application cursor */ - if (term->application_cursor_keys) { - if (keysym == 0xFF51) return guac_terminal_send_string(term, "\x1BOD"); /* Left */ - if (keysym == 0xFF52) return guac_terminal_send_string(term, "\x1BOA"); /* Up */ - if (keysym == 0xFF53) return guac_terminal_send_string(term, "\x1BOC"); /* Right */ - if (keysym == 0xFF54) return guac_terminal_send_string(term, "\x1BOB"); /* Down */ - } - else { - if (keysym == 0xFF51) return guac_terminal_send_string(term, "\x1B[D"); /* Left */ - if (keysym == 0xFF52) return guac_terminal_send_string(term, "\x1B[A"); /* Up */ - if (keysym == 0xFF53) return guac_terminal_send_string(term, "\x1B[C"); /* Right */ - if (keysym == 0xFF54) return guac_terminal_send_string(term, "\x1B[B"); /* Down */ - } - - if (keysym == 0xFF55) return guac_terminal_send_string(term, "\x1B[5~"); /* Page up */ - if (keysym == 0xFF56) return guac_terminal_send_string(term, "\x1B[6~"); /* Page down */ - if (keysym == 0xFF57) return guac_terminal_send_string(term, "\x1B[4~"); /* End */ - - if (keysym == 0xFF63) return guac_terminal_send_string(term, "\x1B[2~"); /* Insert */ - - if (keysym == 0xFFBE) return guac_terminal_send_string(term, "\x1B[[A"); /* F1 */ - if (keysym == 0xFFBF) return guac_terminal_send_string(term, "\x1B[[B"); /* F2 */ - if (keysym == 0xFFC0) return guac_terminal_send_string(term, "\x1B[[C"); /* F3 */ - if (keysym == 0xFFC1) return guac_terminal_send_string(term, "\x1B[[D"); /* F4 */ - if (keysym == 0xFFC2) return guac_terminal_send_string(term, "\x1B[[E"); /* F5 */ - - if (keysym == 0xFFC3) return guac_terminal_send_string(term, "\x1B[17~"); /* F6 */ - if (keysym == 0xFFC4) return guac_terminal_send_string(term, "\x1B[18~"); /* F7 */ - if (keysym == 0xFFC5) return guac_terminal_send_string(term, "\x1B[19~"); /* F8 */ - if (keysym == 0xFFC6) return guac_terminal_send_string(term, "\x1B[20~"); /* F9 */ - if (keysym == 0xFFC7) return guac_terminal_send_string(term, "\x1B[21~"); /* F10 */ - if (keysym == 0xFFC8) return guac_terminal_send_string(term, "\x1B[22~"); /* F11 */ - if (keysym == 0xFFC9) return guac_terminal_send_string(term, "\x1B[23~"); /* F12 */ - - if (keysym == 0xFFFF) return guac_terminal_send_string(term, "\x1B[3~"); /* Delete */ - - /* Ignore unknown keys */ - } - - } + /* Send key */ + guac_terminal_lock(term); + guac_terminal_send_key(term, keysym, pressed); + guac_terminal_unlock(term); return 0; @@ -349,36 +131,15 @@ int ssh_guac_client_size_handler(guac_client* client, int width, int height) { ssh_guac_client_data* guac_client_data = (ssh_guac_client_data*) client->data; guac_terminal* terminal = guac_client_data->term; - /* Calculate dimensions */ - int rows = height / terminal->display->char_height; - int columns = width / terminal->display->char_width; + /* Resize terminal */ + guac_terminal_lock(terminal); + guac_terminal_resize(terminal, width, height); + guac_terminal_unlock(terminal); - pthread_mutex_lock(&(terminal->lock)); - - /* If size has changed */ - if (columns != terminal->term_width || rows != terminal->term_height) { - - /* Resize terminal */ - guac_terminal_resize(terminal, columns, rows); - - /* Update cursor */ - guac_terminal_commit_cursor(terminal); - - /* Update SSH pty size if connected */ - if (guac_client_data->term_channel != NULL) - libssh2_channel_request_pty_size(guac_client_data->term_channel, - terminal->term_width, terminal->term_height); - - /* Reset scroll region */ - terminal->scroll_end = rows - 1; - - guac_terminal_display_flush(terminal->display); - guac_protocol_send_sync(terminal->client->socket, - client->last_sent_timestamp); - guac_socket_flush(terminal->client->socket); - } - - pthread_mutex_unlock(&(terminal->lock)); + /* Update SSH pty size if connected */ + if (guac_client_data->term_channel != NULL) + libssh2_channel_request_pty_size(guac_client_data->term_channel, + terminal->term_width, terminal->term_height); return 0; } @@ -417,13 +178,6 @@ int ssh_guac_client_free_handler(guac_client* client) { if (guac_client_data->key != NULL) ssh_key_free(guac_client_data->key); - /* Free clipboard */ - guac_common_clipboard_free(guac_client_data->clipboard); - - /* Free cursors */ - guac_terminal_cursor_free(client, guac_client_data->ibar_cursor); - guac_terminal_cursor_free(client, guac_client_data->blank_cursor); - /* Free generic data struct */ free(client->data); diff --git a/src/terminal/Makefile.am b/src/terminal/Makefile.am index 7a48695c..8d286fe1 100644 --- a/src/terminal/Makefile.am +++ b/src/terminal/Makefile.am @@ -22,7 +22,7 @@ AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 -AM_CFLAGS = -Werror -Wall -pedantic @PANGO_CFLAGS@ @PANGOCAIRO_CFLAGS@ @LIBGUAC_INCLUDE@ +AM_CFLAGS = -Werror -Wall -pedantic @PANGO_CFLAGS@ @PANGOCAIRO_CFLAGS@ @LIBGUAC_INCLUDE@ @COMMON_INCLUDE@ noinst_LTLIBRARIES = libguac_terminal.la @@ -49,6 +49,6 @@ libguac_terminal_la_SOURCES = \ terminal.c \ terminal_handlers.c -libguac_terminal_la_LIBADD = @LIBGUAC_LTLIB@ +libguac_terminal_la_LIBADD = @LIBGUAC_LTLIB@ @COMMON_LTLIB@ libguac_terminal_la_LDFLAGS = @PTHREAD_LIBS@ @PANGO_LIBS@ @PANGOCAIRO_LIBS@ @CAIRO_LIBS@ diff --git a/src/terminal/terminal.c b/src/terminal/terminal.c index 6692ed91..07e5d5e0 100644 --- a/src/terminal/terminal.c +++ b/src/terminal/terminal.c @@ -23,8 +23,12 @@ #include "config.h" #include "buffer.h" +#include "blank.h" #include "common.h" +#include "cursor.h" #include "display.h" +#include "ibar.h" +#include "guac_clipboard.h" #include "terminal.h" #include "terminal_handlers.h" #include "types.h" @@ -145,6 +149,21 @@ guac_terminal* guac_terminal_create(guac_client* client, /* Init terminal */ guac_terminal_reset(term); + term->mod_alt = + term->mod_ctrl = + term->mod_shift = 0; + + /* Set up mouse cursors */ + term->ibar_cursor = guac_terminal_create_ibar(client); + term->blank_cursor = guac_terminal_create_blank(client); + + /* Initialize mouse cursor */ + term->current_cursor = term->blank_cursor; + guac_terminal_set_cursor(term->client, term->current_cursor); + + /* Allocate clipboard */ + term->clipboard = guac_common_clipboard_alloc(GUAC_TERMINAL_CLIPBOARD_MAX_LENGTH); + return term; } @@ -165,6 +184,13 @@ void guac_terminal_free(guac_terminal* term) { /* Free buffer */ guac_terminal_buffer_free(term->buffer); + /* Free clipboard */ + guac_common_clipboard_free(term->clipboard); + + /* Free cursors */ + guac_terminal_cursor_free(term->client, term->ibar_cursor); + guac_terminal_cursor_free(term->client, term->blank_cursor); + } int guac_terminal_set(guac_terminal* term, int row, int col, int codepoint) { @@ -656,7 +682,11 @@ static void __guac_terminal_redraw_rect(guac_terminal* term, int start_row, int } -void guac_terminal_resize(guac_terminal* term, int width, int height) { +/** + * Internal terminal resize routine. Accepts width/height in CHARACTERS + * (not pixels like the public function). + */ +static void __guac_terminal_resize(guac_terminal* term, int width, int height) { /* If height is decreasing, shift display up */ if (height < term->term_height) { @@ -765,6 +795,41 @@ void guac_terminal_resize(guac_terminal* term, int width, int height) { } +int guac_terminal_resize(guac_terminal* terminal, int width, int height) { + + /* Calculate dimensions */ + int rows = height / terminal->display->char_height; + int columns = width / terminal->display->char_width; + + /* If size has changed */ + if (columns != terminal->term_width || rows != terminal->term_height) { + + /* Resize terminal */ + __guac_terminal_resize(terminal, columns, rows); + + /* Reset scroll region */ + terminal->scroll_end = rows - 1; + + guac_terminal_flush(terminal); + } + + return 0; + +} + +void guac_terminal_flush(guac_terminal* terminal) { + guac_terminal_commit_cursor(terminal); + guac_terminal_display_flush(terminal->display); +} + +void guac_terminal_lock(guac_terminal* terminal) { + pthread_mutex_lock(&(terminal->lock)); +} + +void guac_terminal_unlock(guac_terminal* terminal) { + pthread_mutex_unlock(&(terminal->lock)); +} + int guac_terminal_send_data(guac_terminal* term, const char* data, int length) { return guac_terminal_write_all(term->stdin_pipe_fd[1], data, length); } @@ -773,6 +838,223 @@ int guac_terminal_send_string(guac_terminal* term, const char* data) { return guac_terminal_write_all(term->stdin_pipe_fd[1], data, strlen(data)); } +int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed) { + + /* Hide mouse cursor if not already hidden */ + if (term->current_cursor != term->blank_cursor) { + term->current_cursor = term->blank_cursor; + guac_terminal_set_cursor(term->client, term->blank_cursor); + guac_socket_flush(term->client->socket); + } + + /* Track modifiers */ + if (keysym == 0xFFE3) + term->mod_ctrl = pressed; + else if (keysym == 0xFFE9) + term->mod_alt = pressed; + else if (keysym == 0xFFE1) + term->mod_shift = pressed; + + /* If key pressed */ + else if (pressed) { + + /* Ctrl+Shift+V shortcut for paste */ + if (keysym == 'V' && term->mod_ctrl) + return guac_terminal_send_data(term, term->clipboard->buffer, term->clipboard->length); + + /* Shift+PgUp / Shift+PgDown shortcuts for scrolling */ + if (term->mod_shift) { + + /* Page up */ + if (keysym == 0xFF55) { + guac_terminal_scroll_display_up(term, term->term_height); + return 0; + } + + /* Page down */ + if (keysym == 0xFF56) { + guac_terminal_scroll_display_down(term, term->term_height); + return 0; + } + + } + + /* Reset scroll */ + if (term->scroll_offset != 0) + guac_terminal_scroll_display_down(term, term->scroll_offset); + + /* If alt being held, also send escape character */ + if (term->mod_alt) + return guac_terminal_send_string(term, "\x1B"); + + /* Translate Ctrl+letter to control code */ + if (term->mod_ctrl) { + + char data; + + /* If valid control code, send it */ + if (keysym >= 'A' && keysym <= 'Z') + data = (char) (keysym - 'A' + 1); + else if (keysym >= 'a' && keysym <= 'z') + data = (char) (keysym - 'a' + 1); + + /* Otherwise ignore */ + else + return 0; + + return guac_terminal_send_data(term, &data, 1); + + } + + /* Translate Unicode to UTF-8 */ + else if ((keysym >= 0x00 && keysym <= 0xFF) || ((keysym & 0xFFFF0000) == 0x01000000)) { + + int length; + char data[5]; + + length = guac_terminal_encode_utf8(keysym & 0xFFFF, data); + return guac_terminal_send_data(term, data, length); + + } + + /* Non-printable keys */ + else { + + if (keysym == 0xFF08) return guac_terminal_send_string(term, "\x7F"); /* Backspace */ + if (keysym == 0xFF09) return guac_terminal_send_string(term, "\x09"); /* Tab */ + if (keysym == 0xFF0D) return guac_terminal_send_string(term, "\x0D"); /* Enter */ + if (keysym == 0xFF1B) return guac_terminal_send_string(term, "\x1B"); /* Esc */ + + if (keysym == 0xFF50) return guac_terminal_send_string(term, "\x1B[1~"); /* Home */ + + /* Arrow keys w/ application cursor */ + if (term->application_cursor_keys) { + if (keysym == 0xFF51) return guac_terminal_send_string(term, "\x1BOD"); /* Left */ + if (keysym == 0xFF52) return guac_terminal_send_string(term, "\x1BOA"); /* Up */ + if (keysym == 0xFF53) return guac_terminal_send_string(term, "\x1BOC"); /* Right */ + if (keysym == 0xFF54) return guac_terminal_send_string(term, "\x1BOB"); /* Down */ + } + else { + if (keysym == 0xFF51) return guac_terminal_send_string(term, "\x1B[D"); /* Left */ + if (keysym == 0xFF52) return guac_terminal_send_string(term, "\x1B[A"); /* Up */ + if (keysym == 0xFF53) return guac_terminal_send_string(term, "\x1B[C"); /* Right */ + if (keysym == 0xFF54) return guac_terminal_send_string(term, "\x1B[B"); /* Down */ + } + + if (keysym == 0xFF55) return guac_terminal_send_string(term, "\x1B[5~"); /* Page up */ + if (keysym == 0xFF56) return guac_terminal_send_string(term, "\x1B[6~"); /* Page down */ + if (keysym == 0xFF57) return guac_terminal_send_string(term, "\x1B[4~"); /* End */ + + if (keysym == 0xFF63) return guac_terminal_send_string(term, "\x1B[2~"); /* Insert */ + + if (keysym == 0xFFBE) return guac_terminal_send_string(term, "\x1B[[A"); /* F1 */ + if (keysym == 0xFFBF) return guac_terminal_send_string(term, "\x1B[[B"); /* F2 */ + if (keysym == 0xFFC0) return guac_terminal_send_string(term, "\x1B[[C"); /* F3 */ + if (keysym == 0xFFC1) return guac_terminal_send_string(term, "\x1B[[D"); /* F4 */ + if (keysym == 0xFFC2) return guac_terminal_send_string(term, "\x1B[[E"); /* F5 */ + + if (keysym == 0xFFC3) return guac_terminal_send_string(term, "\x1B[17~"); /* F6 */ + if (keysym == 0xFFC4) return guac_terminal_send_string(term, "\x1B[18~"); /* F7 */ + if (keysym == 0xFFC5) return guac_terminal_send_string(term, "\x1B[19~"); /* F8 */ + if (keysym == 0xFFC6) return guac_terminal_send_string(term, "\x1B[20~"); /* F9 */ + if (keysym == 0xFFC7) return guac_terminal_send_string(term, "\x1B[21~"); /* F10 */ + if (keysym == 0xFFC8) return guac_terminal_send_string(term, "\x1B[22~"); /* F11 */ + if (keysym == 0xFFC9) return guac_terminal_send_string(term, "\x1B[23~"); /* F12 */ + + if (keysym == 0xFFFF) return guac_terminal_send_string(term, "\x1B[3~"); /* Delete */ + + /* Ignore unknown keys */ + } + + } + + return 0; + + +} + +int guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mask) { + + /* Determine which buttons were just released and pressed */ + int released_mask = term->mouse_mask & ~mask; + int pressed_mask = ~term->mouse_mask & mask; + + term->mouse_mask = mask; + + /* Show mouse cursor if not already shown */ + if (term->current_cursor != term->ibar_cursor) { + term->current_cursor = term->ibar_cursor; + guac_terminal_set_cursor(term->client, term->ibar_cursor); + guac_socket_flush(term->client->socket); + } + + /* Paste contents of clipboard on right or middle mouse button up */ + if ((released_mask & GUAC_CLIENT_MOUSE_RIGHT) || (released_mask & GUAC_CLIENT_MOUSE_MIDDLE)) + return guac_terminal_send_data(term, term->clipboard->buffer, term->clipboard->length); + + /* If text selected, change state based on left mouse mouse button */ + if (term->text_selected) { + + /* If mouse button released, stop selection */ + if (released_mask & GUAC_CLIENT_MOUSE_LEFT) { + + int selected_length; + + /* End selection and get selected text */ + int selectable_size = term->term_width * term->term_height * sizeof(char); + char* string = malloc(selectable_size); + guac_terminal_select_end(term, string); + + selected_length = strnlen(string, selectable_size); + + /* Store new data */ + guac_common_clipboard_reset(term->clipboard, "text/plain"); + guac_common_clipboard_append(term->clipboard, string, selected_length); + free(string); + + /* Send data */ + guac_common_clipboard_send(term->clipboard, term->client); + guac_socket_flush(term->client->socket); + + } + + /* Otherwise, just update */ + else + guac_terminal_select_update(term, + y / term->display->char_height - term->scroll_offset, + x / term->display->char_width); + + } + + /* Otherwise, if mouse button pressed AND moved, start selection */ + else if (!(pressed_mask & GUAC_CLIENT_MOUSE_LEFT) && + mask & GUAC_CLIENT_MOUSE_LEFT) + guac_terminal_select_start(term, + y / term->display->char_height - term->scroll_offset, + x / term->display->char_width); + + /* Scroll up if wheel moved up */ + if (released_mask & GUAC_CLIENT_MOUSE_SCROLL_UP) + guac_terminal_scroll_display_up(term, GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT); + + /* Scroll down if wheel moved down */ + if (released_mask & GUAC_CLIENT_MOUSE_SCROLL_DOWN) + guac_terminal_scroll_display_down(term, GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT); + + return 0; + + + +} + +void guac_terminal_clipboard_reset(guac_terminal* term, const char* mimetype) { + guac_common_clipboard_reset(term->clipboard, mimetype); +} + +void guac_terminal_clipboard_append(guac_terminal* term, const void* data, int length) { + guac_common_clipboard_append(term->clipboard, data, length); +} + int guac_terminal_sendf(guac_terminal* term, const char* format, ...) { int written; diff --git a/src/terminal/terminal.h b/src/terminal/terminal.h index dab6630c..fbb96306 100644 --- a/src/terminal/terminal.h +++ b/src/terminal/terminal.h @@ -27,7 +27,9 @@ #include "config.h" #include "buffer.h" +#include "cursor.h" #include "display.h" +#include "guac_clipboard.h" #include "types.h" #include @@ -45,6 +47,11 @@ */ #define GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT 3 +/** + * The maximum number of bytes to allow within the clipboard. + */ +#define GUAC_TERMINAL_CLIPBOARD_MAX_LENGTH 262144 + typedef struct guac_terminal guac_terminal; /** @@ -241,6 +248,46 @@ struct guac_terminal { */ bool insert_mode; + /** + * Whether the alt key is currently being held down. + */ + int mod_alt; + + /** + * Whether the control key is currently being held down. + */ + int mod_ctrl; + + /** + * Whether the shift key is currently being held down. + */ + int mod_shift; + + /** + * The current mouse button state. + */ + int mouse_mask; + + /** + * The cached I-bar cursor. + */ + guac_terminal_cursor* ibar_cursor; + + /** + * The cached invisible (blank) cursor. + */ + guac_terminal_cursor* blank_cursor; + + /** + * The current cursor, used to avoid re-setting the cursor. + */ + guac_terminal_cursor* current_cursor; + + /** + * The current contents of the clipboard. + */ + guac_common_clipboard* clipboard; + }; /** @@ -359,13 +406,25 @@ void guac_terminal_set_columns(guac_terminal* terminal, int row, /** * Resize the terminal to the given dimensions. */ -void guac_terminal_resize(guac_terminal* term, int width, int height); +int guac_terminal_resize(guac_terminal* term, int width, int height); /** * Flushes all pending operations within the given guac_terminal. */ void guac_terminal_flush(guac_terminal* terminal); +/** + * Acquires exclusive access to the terminal. Note that enforcing this + * exclusive access requires that ALL users of the terminal call this + * function before making further calls to the terminal. + */ +void guac_terminal_lock(guac_terminal* terminal); + +/** + * Releases exclusive access to the terminal. + */ +void guac_terminal_unlock(guac_terminal* terminal); + /** * Sends the given string as if typed by the user. */ @@ -376,6 +435,29 @@ int guac_terminal_send_data(guac_terminal* term, const char* data, int length); */ int guac_terminal_send_string(guac_terminal* term, const char* data); +/** + * Handles the given key event, sending data, scrolling, pasting clipboard + * data, etc. as necessary. + */ +int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed); + +/** + * Handles the given mouse event, sending data, scrolling, pasting clipboard + * data, etc. as necessary. + */ +int guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mask); + +/** + * Clears the current clipboard contents and sets the mimetype for future + * contents. + */ +void guac_terminal_clipboard_reset(guac_terminal* term, const char* mimetype); + +/** + * Appends the given data to the current clipboard. + */ +void guac_terminal_clipboard_append(guac_terminal* term, const void* data, int length); + /** * Sends data through STDIN as if typed by the user, using the format * string given and any args (similar to printf).