GUAC-653: Move mouse and key handling into terminal. Move clipboard into terminal.

This commit is contained in:
Michael Jumper 2014-05-05 18:58:53 -07:00
parent 455f2e543a
commit f2781ed0cc
9 changed files with 393 additions and 338 deletions

View File

@ -94,7 +94,7 @@ AC_SUBST([COMMON_INCLUDE], '-I$(top_srcdir)/src/common')
# Terminal emulator # Terminal emulator
AC_SUBST([TERMINAL_LTLIB], '$(top_builddir)/src/terminal/libguac_terminal.la') 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 # Options
AC_ARG_WITH(init_dir, AC_ARG_WITH(init_dir,

View File

@ -50,7 +50,7 @@ libguac_client_ssh_la_SOURCES += ssh_agent.c
noinst_HEADERS += ssh_agent.h noinst_HEADERS += ssh_agent.h
endif endif
libguac_client_ssh_la_CFLAGS = -Werror -Wall -Iinclude @LIBGUAC_INCLUDE@ @COMMON_INCLUDE@ @TERMINAL_INCLUDE@ libguac_client_ssh_la_CFLAGS = -Werror -Wall -Iinclude @LIBGUAC_INCLUDE@ @TERMINAL_INCLUDE@
libguac_client_ssh_la_LIBADD = @LIBGUAC_LTLIB@ @COMMON_LTLIB@ @TERMINAL_LTLIB@ 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@ libguac_client_ssh_la_LDFLAGS = -version-info 0:0:0 @SSH_LIBS@ @SSL_LIBS@ @PTHREAD_LIBS@

View File

@ -125,10 +125,6 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
/* Init client data */ /* Init client data */
client->data = 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; client_data->term_channel = NULL;
if (argc != SSH_ARGS_COUNT) { if (argc != SSH_ARGS_COUNT) {
@ -186,19 +182,9 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
return -1; 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 */ /* Send initial name */
guac_protocol_send_name(socket, client_data->hostname); 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); guac_socket_flush(socket);
/* Set basic handlers */ /* Set basic handlers */

View File

@ -26,8 +26,6 @@
#include "config.h" #include "config.h"
#include "cursor.h"
#include "guac_clipboard.h"
#include "sftp.h" #include "sftp.h"
#include "ssh_key.h" #include "ssh_key.h"
#include "terminal.h" #include "terminal.h"
@ -41,11 +39,6 @@
#include "ssh_agent.h" #include "ssh_agent.h"
#endif #endif
/**
* The maximum number of bytes to allow within the clipboard.
*/
#define GUAC_SSH_CLIPBOARD_MAX_LENGTH 262144
/** /**
* SSH-specific client data. * SSH-specific client data.
*/ */
@ -148,46 +141,6 @@ typedef struct ssh_guac_client_data {
*/ */
guac_terminal* term; 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; } ssh_guac_client_data;
#endif #endif

View File

@ -23,15 +23,13 @@
#include "config.h" #include "config.h"
#include "client.h" #include "client.h"
#include "clipboard.h" #include "clipboard.h"
#include "guac_clipboard.h"
#include "guac_iconv.h"
int guac_ssh_clipboard_handler(guac_client* client, guac_stream* stream, int guac_ssh_clipboard_handler(guac_client* client, guac_stream* stream,
char* mimetype) { char* mimetype) {
/* Clear clipboard and prepare for new data */ /* Clear clipboard and prepare for new data */
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->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 */ /* Set handlers for clipboard stream */
stream->blob_handler = guac_ssh_clipboard_blob_handler; 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 */ /* Append new data */
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->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; return 0;
} }

View File

@ -65,8 +65,7 @@ int ssh_guac_client_handle_messages(guac_client* client) {
int bytes_read = 0; int bytes_read = 0;
/* Lock terminal access */ guac_terminal_lock(client_data->term);
pthread_mutex_lock(&(client_data->term->lock));
/* Read data, write to terminal */ /* Read data, write to terminal */
if ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) { if ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
@ -84,14 +83,9 @@ int ssh_guac_client_handle_messages(guac_client* client) {
return 1; return 1;
} }
/* Update cursor */ /* Flush terminal */
guac_terminal_commit_cursor(client_data->term); guac_terminal_flush(client_data->term);
guac_terminal_unlock(client_data->term);
/* Flush terminal display */
guac_terminal_display_flush(client_data->term->display);
/* Unlock terminal access */
pthread_mutex_unlock(&(client_data->term->lock));
} }
else if (ret_val < 0) { 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; ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
guac_terminal* term = client_data->term; guac_terminal* term = client_data->term;
/* Determine which buttons were just released and pressed */ /* Send mouse event */
int released_mask = client_data->mouse_mask & ~mask; guac_terminal_lock(term);
int pressed_mask = ~client_data->mouse_mask & mask; guac_terminal_send_mouse(term, x, y, mask);
guac_terminal_unlock(term);
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));
}
return 0; 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; ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
guac_terminal* term = client_data->term; guac_terminal* term = client_data->term;
/* Hide mouse cursor if not already hidden */ /* Send key */
if (client_data->current_cursor != client_data->blank_cursor) { guac_terminal_lock(term);
pthread_mutex_lock(&(term->lock)); guac_terminal_send_key(term, keysym, pressed);
guac_terminal_unlock(term);
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 */
}
}
return 0; 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; ssh_guac_client_data* guac_client_data = (ssh_guac_client_data*) client->data;
guac_terminal* terminal = guac_client_data->term; guac_terminal* terminal = guac_client_data->term;
/* Calculate dimensions */ /* Resize terminal */
int rows = height / terminal->display->char_height; guac_terminal_lock(terminal);
int columns = width / terminal->display->char_width; guac_terminal_resize(terminal, width, height);
guac_terminal_unlock(terminal);
pthread_mutex_lock(&(terminal->lock)); /* Update SSH pty size if connected */
if (guac_client_data->term_channel != NULL)
/* If size has changed */ libssh2_channel_request_pty_size(guac_client_data->term_channel,
if (columns != terminal->term_width || rows != terminal->term_height) { terminal->term_width, 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));
return 0; return 0;
} }
@ -417,13 +178,6 @@ int ssh_guac_client_free_handler(guac_client* client) {
if (guac_client_data->key != NULL) if (guac_client_data->key != NULL)
ssh_key_free(guac_client_data->key); 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 generic data struct */
free(client->data); free(client->data);

View File

@ -22,7 +22,7 @@
AUTOMAKE_OPTIONS = foreign AUTOMAKE_OPTIONS = foreign
ACLOCAL_AMFLAGS = -I m4 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 noinst_LTLIBRARIES = libguac_terminal.la
@ -49,6 +49,6 @@ libguac_terminal_la_SOURCES = \
terminal.c \ terminal.c \
terminal_handlers.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@ libguac_terminal_la_LDFLAGS = @PTHREAD_LIBS@ @PANGO_LIBS@ @PANGOCAIRO_LIBS@ @CAIRO_LIBS@

View File

@ -23,8 +23,12 @@
#include "config.h" #include "config.h"
#include "buffer.h" #include "buffer.h"
#include "blank.h"
#include "common.h" #include "common.h"
#include "cursor.h"
#include "display.h" #include "display.h"
#include "ibar.h"
#include "guac_clipboard.h"
#include "terminal.h" #include "terminal.h"
#include "terminal_handlers.h" #include "terminal_handlers.h"
#include "types.h" #include "types.h"
@ -145,6 +149,21 @@ guac_terminal* guac_terminal_create(guac_client* client,
/* Init terminal */ /* Init terminal */
guac_terminal_reset(term); 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; return term;
} }
@ -165,6 +184,13 @@ void guac_terminal_free(guac_terminal* term) {
/* Free buffer */ /* Free buffer */
guac_terminal_buffer_free(term->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) { 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 is decreasing, shift display up */
if (height < term->term_height) { 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) { 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); 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)); 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 guac_terminal_sendf(guac_terminal* term, const char* format, ...) {
int written; int written;

View File

@ -27,7 +27,9 @@
#include "config.h" #include "config.h"
#include "buffer.h" #include "buffer.h"
#include "cursor.h"
#include "display.h" #include "display.h"
#include "guac_clipboard.h"
#include "types.h" #include "types.h"
#include <pthread.h> #include <pthread.h>
@ -45,6 +47,11 @@
*/ */
#define GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT 3 #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; typedef struct guac_terminal guac_terminal;
/** /**
@ -241,6 +248,46 @@ struct guac_terminal {
*/ */
bool insert_mode; 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. * 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. * Flushes all pending operations within the given guac_terminal.
*/ */
void guac_terminal_flush(guac_terminal* 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. * 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); 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 * Sends data through STDIN as if typed by the user, using the format
* string given and any args (similar to printf). * string given and any args (similar to printf).