2013-12-29 04:53:12 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
2011-08-04 18:46:21 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-08-04 18:46:21 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-12-29 04:53:12 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "display.h"
|
2014-05-06 01:58:53 +00:00
|
|
|
#include "guac_clipboard.h"
|
2016-03-01 05:41:56 +00:00
|
|
|
#include "guac_cursor.h"
|
2015-02-04 21:08:02 +00:00
|
|
|
#include "packet.h"
|
2015-01-26 21:37:07 +00:00
|
|
|
#include "scrollbar.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "terminal.h"
|
|
|
|
#include "terminal_handlers.h"
|
|
|
|
#include "types.h"
|
2016-01-26 01:04:15 +00:00
|
|
|
#include "typescript.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
|
2016-01-27 20:40:09 +00:00
|
|
|
#include <errno.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <pthread.h>
|
2013-05-26 05:45:26 +00:00
|
|
|
#include <stdarg.h>
|
2014-06-11 00:40:58 +00:00
|
|
|
#include <stdio.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <stdlib.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
#include <string.h>
|
2014-05-06 23:12:29 +00:00
|
|
|
#include <sys/select.h>
|
2014-06-11 00:40:58 +00:00
|
|
|
#include <sys/time.h>
|
2014-05-06 23:12:29 +00:00
|
|
|
#include <unistd.h>
|
2014-06-04 20:49:35 +00:00
|
|
|
#include <wchar.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
#include <guacamole/client.h>
|
2013-05-22 05:02:11 +00:00
|
|
|
#include <guacamole/error.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/socket.h>
|
2015-02-04 22:01:25 +00:00
|
|
|
#include <guacamole/timestamp.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2014-06-04 22:52:50 +00:00
|
|
|
/**
|
|
|
|
* Sets the given range of columns to the given character.
|
|
|
|
*/
|
|
|
|
static void __guac_terminal_set_columns(guac_terminal* terminal, int row,
|
|
|
|
int start_column, int end_column, guac_terminal_char* character) {
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(terminal->display, row + terminal->scroll_offset,
|
2014-06-04 22:52:50 +00:00
|
|
|
start_column, end_column, character);
|
|
|
|
|
|
|
|
guac_terminal_buffer_set_columns(terminal->buffer, row,
|
|
|
|
start_column, end_column, character);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-05 18:55:21 +00:00
|
|
|
* Enforces a character break at the given edge, ensuring that the left side
|
|
|
|
* of the edge is the final column of a character, and the right side of the
|
|
|
|
* edge is the initial column of a DIFFERENT character.
|
|
|
|
*
|
|
|
|
* For a character in a column N, the left edge number is N, and the right
|
|
|
|
* edge is N+1.
|
2014-06-04 22:52:50 +00:00
|
|
|
*/
|
2014-06-05 18:55:21 +00:00
|
|
|
static void __guac_terminal_force_break(guac_terminal* terminal, int row, int edge) {
|
2014-06-04 22:52:50 +00:00
|
|
|
|
|
|
|
guac_terminal_buffer_row* buffer_row = guac_terminal_buffer_get_row(terminal->buffer, row, 0);
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Ensure character to left of edge is unbroken */
|
|
|
|
if (edge > 0) {
|
|
|
|
|
|
|
|
int end_column = edge - 1;
|
|
|
|
int start_column = end_column;
|
2014-06-04 22:52:50 +00:00
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
guac_terminal_char* start_char = &(buffer_row->characters[start_column]);
|
2014-06-04 22:52:50 +00:00
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Determine start column */
|
2014-06-05 19:13:05 +00:00
|
|
|
while (start_column > 0 && start_char->value == GUAC_CHAR_CONTINUATION) {
|
2014-06-04 22:52:50 +00:00
|
|
|
start_char--;
|
2014-06-05 18:55:21 +00:00
|
|
|
start_column--;
|
2014-06-04 22:52:50 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 19:31:03 +00:00
|
|
|
/* Advance to start of broken character if necessary */
|
|
|
|
if (start_char->value != GUAC_CHAR_CONTINUATION && start_char->width < end_column - start_column + 1) {
|
|
|
|
start_column += start_char->width;
|
|
|
|
start_char += start_char->width;
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Clear character if broken */
|
2014-06-05 19:13:05 +00:00
|
|
|
if (start_char->value == GUAC_CHAR_CONTINUATION || start_char->width != end_column - start_column + 1) {
|
2014-06-04 22:52:50 +00:00
|
|
|
|
|
|
|
guac_terminal_char cleared_char;
|
|
|
|
cleared_char.value = ' ';
|
|
|
|
cleared_char.attributes = start_char->attributes;
|
|
|
|
cleared_char.width = 1;
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
__guac_terminal_set_columns(terminal, row, start_column, end_column, &cleared_char);
|
2014-06-04 22:52:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Ensure character to right of edge is unbroken */
|
|
|
|
if (edge >= 0 && edge < buffer_row->length) {
|
2014-06-04 22:52:50 +00:00
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
int start_column = edge;
|
|
|
|
int end_column = start_column;
|
2014-06-04 22:52:50 +00:00
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
guac_terminal_char* start_char = &(buffer_row->characters[start_column]);
|
|
|
|
guac_terminal_char* end_char = &(buffer_row->characters[end_column]);
|
2014-06-04 22:52:50 +00:00
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Determine end column */
|
|
|
|
while (end_column+1 < buffer_row->length && (end_char+1)->value == GUAC_CHAR_CONTINUATION) {
|
|
|
|
end_char++;
|
|
|
|
end_column++;
|
2014-06-04 22:52:50 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 19:31:03 +00:00
|
|
|
/* Advance to start of broken character if necessary */
|
|
|
|
if (start_char->value != GUAC_CHAR_CONTINUATION && start_char->width < end_column - start_column + 1) {
|
|
|
|
start_column += start_char->width;
|
|
|
|
start_char += start_char->width;
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Clear character if broken */
|
2014-06-05 19:13:05 +00:00
|
|
|
if (start_char->value == GUAC_CHAR_CONTINUATION || start_char->width != end_column - start_column + 1) {
|
2014-06-04 22:52:50 +00:00
|
|
|
|
|
|
|
guac_terminal_char cleared_char;
|
|
|
|
cleared_char.value = ' ';
|
2014-06-05 19:31:03 +00:00
|
|
|
cleared_char.attributes = start_char->attributes;
|
2014-06-04 22:52:50 +00:00
|
|
|
cleared_char.width = 1;
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
__guac_terminal_set_columns(terminal, row, start_column, end_column, &cleared_char);
|
2014-06-04 22:52:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-24 23:29:43 +00:00
|
|
|
void guac_terminal_reset(guac_terminal* term) {
|
|
|
|
|
|
|
|
int row;
|
|
|
|
|
|
|
|
/* Set current state */
|
|
|
|
term->char_handler = guac_terminal_echo;
|
2013-05-25 04:18:47 +00:00
|
|
|
term->active_char_set = 0;
|
|
|
|
term->char_mapping[0] =
|
|
|
|
term->char_mapping[1] = NULL;
|
2013-05-24 23:29:43 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2015-01-26 21:37:07 +00:00
|
|
|
/* Clear scrollback, buffer, and scroll region */
|
2013-05-24 23:29:43 +00:00
|
|
|
term->buffer->top = 0;
|
|
|
|
term->buffer->length = 0;
|
|
|
|
term->scroll_start = 0;
|
|
|
|
term->scroll_end = term->term_height - 1;
|
|
|
|
term->scroll_offset = 0;
|
|
|
|
|
2015-01-26 23:51:50 +00:00
|
|
|
/* Reset scrollbar bounds */
|
|
|
|
guac_terminal_scrollbar_set_bounds(term->scrollbar, term->term_height - term->buffer->length, 0);
|
|
|
|
guac_terminal_scrollbar_set_value(term->scrollbar, -term->scroll_offset);
|
|
|
|
|
2013-05-24 23:29:43 +00:00
|
|
|
/* Reset flags */
|
|
|
|
term->text_selected = false;
|
|
|
|
term->application_cursor_keys = false;
|
|
|
|
term->automatic_carriage_return = false;
|
2013-05-26 05:45:26 +00:00
|
|
|
term->insert_mode = false;
|
2013-05-24 23:29:43 +00:00
|
|
|
|
2013-05-26 08:49:47 +00:00
|
|
|
/* Reset tabs */
|
|
|
|
term->tab_interval = 8;
|
|
|
|
memset(term->custom_tabs, 0, sizeof(term->custom_tabs));
|
|
|
|
|
2013-05-24 23:29:43 +00:00
|
|
|
/* Clear terminal */
|
|
|
|
for (row=0; row<term->term_height; row++)
|
|
|
|
guac_terminal_set_columns(term, row, 0, term->term_width, &(term->default_char));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:59:20 +00:00
|
|
|
/**
|
|
|
|
* Paints or repaints the background of the terminal display. This painting
|
|
|
|
* occurs beneath the actual terminal and scrollbar layers, and thus will not
|
|
|
|
* overwrite any text or other content currently on the screen. This is only
|
|
|
|
* necessary to paint over parts of the terminal background which may otherwise
|
|
|
|
* be transparent (the default layer background).
|
|
|
|
*
|
|
|
|
* @param terminal
|
|
|
|
* The terminal whose background should be painted or repainted.
|
|
|
|
*
|
2016-03-14 22:15:32 +00:00
|
|
|
* @param socket
|
|
|
|
* The socket over which instructions required to paint / repaint the
|
|
|
|
* terminal background should be send.
|
2015-07-28 22:59:20 +00:00
|
|
|
*/
|
2016-03-14 22:56:37 +00:00
|
|
|
static void guac_terminal_repaint_default_layer(guac_terminal* terminal,
|
|
|
|
guac_socket* socket) {
|
2015-07-28 22:59:20 +00:00
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
int width = terminal->width;
|
|
|
|
int height = terminal->height;
|
|
|
|
guac_terminal_display* display = terminal->display;
|
2015-07-28 22:59:20 +00:00
|
|
|
|
|
|
|
/* Get background color */
|
|
|
|
const guac_terminal_color* color =
|
|
|
|
&guac_terminal_palette[display->default_background];
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
/* Reset size */
|
|
|
|
guac_protocol_send_size(socket, GUAC_DEFAULT_LAYER, width, height);
|
|
|
|
|
2015-07-28 22:59:20 +00:00
|
|
|
/* Paint background color */
|
|
|
|
guac_protocol_send_rect(socket, GUAC_DEFAULT_LAYER, 0, 0, width, height);
|
|
|
|
guac_protocol_send_cfill(socket, GUAC_COMP_OVER, GUAC_DEFAULT_LAYER,
|
|
|
|
color->red, color->green, color->blue, 0xFF);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
/**
|
|
|
|
* Automatically and continuously renders frames of terminal data while the
|
|
|
|
* associated guac_client is running.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* A pointer to the guac_terminal that should be continuously rendered
|
|
|
|
* while its associated guac_client is running.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
|
|
|
*/
|
|
|
|
void* guac_terminal_thread(void* data) {
|
|
|
|
|
|
|
|
guac_terminal* terminal = (guac_terminal*) data;
|
|
|
|
guac_client* client = terminal->client;
|
|
|
|
|
|
|
|
/* Render frames only while client is running */
|
|
|
|
while (client->state == GUAC_CLIENT_RUNNING) {
|
|
|
|
|
|
|
|
/* Stop rendering if an error occurs */
|
|
|
|
if (guac_terminal_render_frame(terminal))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Signal end of frame */
|
|
|
|
guac_client_end_frame(client);
|
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The client has stopped or an error has occurred */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal* guac_terminal_create(guac_client* client,
|
2013-12-27 08:28:23 +00:00
|
|
|
const char* font_name, int font_size, int dpi,
|
2015-07-28 23:43:23 +00:00
|
|
|
int width, int height, const char* color_scheme) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2015-07-28 23:43:23 +00:00
|
|
|
int default_foreground;
|
|
|
|
int default_background;
|
|
|
|
|
|
|
|
/* Default to "gray-black" color scheme if no scheme provided */
|
|
|
|
if (color_scheme == NULL || color_scheme[0] == '\0') {
|
|
|
|
default_foreground = GUAC_TERMINAL_COLOR_GRAY;
|
|
|
|
default_background = GUAC_TERMINAL_COLOR_BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, parse color scheme */
|
|
|
|
else if (strcmp(color_scheme, GUAC_TERMINAL_SCHEME_GRAY_BLACK) == 0) {
|
|
|
|
default_foreground = GUAC_TERMINAL_COLOR_GRAY;
|
|
|
|
default_background = GUAC_TERMINAL_COLOR_BLACK;
|
|
|
|
}
|
|
|
|
else if (strcmp(color_scheme, GUAC_TERMINAL_SCHEME_BLACK_WHITE) == 0) {
|
|
|
|
default_foreground = GUAC_TERMINAL_COLOR_BLACK;
|
|
|
|
default_background = GUAC_TERMINAL_COLOR_WHITE;
|
|
|
|
}
|
|
|
|
else if (strcmp(color_scheme, GUAC_TERMINAL_SCHEME_GREEN_BLACK) == 0) {
|
|
|
|
default_foreground = GUAC_TERMINAL_COLOR_DARK_GREEN;
|
|
|
|
default_background = GUAC_TERMINAL_COLOR_BLACK;
|
|
|
|
}
|
|
|
|
else if (strcmp(color_scheme, GUAC_TERMINAL_SCHEME_WHITE_BLACK) == 0) {
|
|
|
|
default_foreground = GUAC_TERMINAL_COLOR_WHITE;
|
|
|
|
default_background = GUAC_TERMINAL_COLOR_BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If invalid, default to "gray-black" */
|
|
|
|
else {
|
|
|
|
guac_client_log(client, GUAC_LOG_WARNING,
|
|
|
|
"Invalid color scheme: \"%s\". Defaulting to \"gray-black\".",
|
|
|
|
color_scheme);
|
|
|
|
default_foreground = GUAC_TERMINAL_COLOR_GRAY;
|
|
|
|
default_background = GUAC_TERMINAL_COLOR_BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build default character using default colors */
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_char default_char = {
|
2013-05-15 17:11:47 +00:00
|
|
|
.value = 0,
|
2013-04-26 08:53:19 +00:00
|
|
|
.attributes = {
|
2015-07-28 22:59:20 +00:00
|
|
|
.foreground = default_foreground,
|
|
|
|
.background = default_background,
|
2013-04-26 08:53:19 +00:00
|
|
|
.bold = false,
|
|
|
|
.reverse = false,
|
|
|
|
.underscore = false
|
2014-06-04 18:23:07 +00:00
|
|
|
},
|
2015-07-28 22:59:20 +00:00
|
|
|
.width = 1
|
|
|
|
};
|
2013-03-20 05:48:43 +00:00
|
|
|
|
2015-01-29 01:06:18 +00:00
|
|
|
/* Calculate available display area */
|
|
|
|
int available_width = width - GUAC_TERMINAL_SCROLLBAR_WIDTH;
|
|
|
|
if (available_width < 0)
|
|
|
|
available_width = 0;
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal* term = malloc(sizeof(guac_terminal));
|
2011-08-04 18:46:21 +00:00
|
|
|
term->client = client;
|
2014-05-07 00:14:40 +00:00
|
|
|
term->upload_path_handler = NULL;
|
|
|
|
term->file_download_handler = NULL;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2013-04-25 18:54:00 +00:00
|
|
|
/* Init buffer */
|
2013-04-26 09:29:30 +00:00
|
|
|
term->buffer = guac_terminal_buffer_alloc(1000, &default_char);
|
2012-10-23 08:38:10 +00:00
|
|
|
|
2013-04-25 18:55:50 +00:00
|
|
|
/* Init display */
|
2016-03-14 22:56:37 +00:00
|
|
|
term->display = guac_terminal_display_alloc(client,
|
2013-12-27 08:28:23 +00:00
|
|
|
font_name, font_size, dpi,
|
2013-04-26 08:53:19 +00:00
|
|
|
default_char.attributes.foreground,
|
|
|
|
default_char.attributes.background);
|
2012-10-23 08:38:10 +00:00
|
|
|
|
2013-08-23 21:10:51 +00:00
|
|
|
/* Fail if display init failed */
|
2016-03-14 22:56:37 +00:00
|
|
|
if (term->display == NULL) {
|
2014-11-10 06:59:53 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Display initialization failed");
|
2013-08-23 21:10:51 +00:00
|
|
|
free(term);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
/* Init common cursor */
|
|
|
|
term->cursor = guac_common_cursor_alloc(client);
|
|
|
|
|
2013-04-25 18:54:00 +00:00
|
|
|
/* Init terminal state */
|
2013-04-26 08:53:19 +00:00
|
|
|
term->current_attributes = default_char.attributes;
|
|
|
|
term->default_char = default_char;
|
2012-10-23 08:38:10 +00:00
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
/* Set pixel size */
|
|
|
|
term->width = width;
|
|
|
|
term->height = height;
|
|
|
|
|
|
|
|
/* Calculate character size */
|
|
|
|
term->term_width = available_width / term->display->char_width;
|
|
|
|
term->term_height = height / term->display->char_height;
|
2013-04-15 08:22:05 +00:00
|
|
|
|
2013-05-22 05:02:11 +00:00
|
|
|
/* Open STDOUT pipe */
|
|
|
|
if (pipe(term->stdout_pipe_fd)) {
|
|
|
|
guac_error = GUAC_STATUS_SEE_ERRNO;
|
|
|
|
guac_error_message = "Unable to open pipe for STDOUT";
|
|
|
|
free(term);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open STDIN pipe */
|
|
|
|
if (pipe(term->stdin_pipe_fd)) {
|
|
|
|
guac_error = GUAC_STATUS_SEE_ERRNO;
|
|
|
|
guac_error_message = "Unable to open pipe for STDIN";
|
|
|
|
free(term);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-13 00:51:32 +00:00
|
|
|
/* Init pipe stream (output to display by default) */
|
|
|
|
term->pipe_stream = NULL;
|
|
|
|
|
2016-01-26 01:04:15 +00:00
|
|
|
/* No typescript by default */
|
|
|
|
term->typescript = NULL;
|
|
|
|
|
2013-05-24 23:29:43 +00:00
|
|
|
/* Init terminal lock */
|
|
|
|
pthread_mutex_init(&(term->lock), NULL);
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
/* Repaint and resize overall display */
|
|
|
|
guac_terminal_repaint_default_layer(term, client->socket);
|
|
|
|
guac_terminal_display_resize(term->display,
|
2013-04-26 21:14:19 +00:00
|
|
|
term->term_width, term->term_height);
|
|
|
|
|
2015-01-26 23:51:50 +00:00
|
|
|
/* Allocate scrollbar */
|
2016-03-01 05:41:56 +00:00
|
|
|
term->scrollbar = guac_terminal_scrollbar_alloc(client, GUAC_DEFAULT_LAYER,
|
|
|
|
width, height, term->term_height);
|
2015-01-26 23:51:50 +00:00
|
|
|
|
2015-02-04 08:41:45 +00:00
|
|
|
/* Associate scrollbar with this terminal */
|
|
|
|
term->scrollbar->data = term;
|
|
|
|
term->scrollbar->scroll_handler = guac_terminal_scroll_handler;
|
|
|
|
|
2013-05-24 23:29:43 +00:00
|
|
|
/* Init terminal */
|
|
|
|
guac_terminal_reset(term);
|
2013-04-07 23:55:06 +00:00
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
term->mod_alt =
|
|
|
|
term->mod_ctrl =
|
|
|
|
term->mod_shift = 0;
|
|
|
|
|
|
|
|
/* Initialize mouse cursor */
|
2016-03-01 05:41:56 +00:00
|
|
|
term->current_cursor = GUAC_TERMINAL_CURSOR_BLANK;
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_common_cursor_set_blank(term->cursor);
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
/* Allocate clipboard */
|
|
|
|
term->clipboard = guac_common_clipboard_alloc(GUAC_TERMINAL_CLIPBOARD_MAX_LENGTH);
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
/* Start terminal thread */
|
|
|
|
if (pthread_create(&(term->thread), NULL,
|
|
|
|
guac_terminal_thread, (void*) term)) {
|
|
|
|
guac_terminal_free(term);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
return term;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
void guac_terminal_free(guac_terminal* term) {
|
2016-01-14 02:07:37 +00:00
|
|
|
|
2013-05-22 05:02:11 +00:00
|
|
|
/* Close terminal output pipe */
|
|
|
|
close(term->stdout_pipe_fd[1]);
|
|
|
|
close(term->stdout_pipe_fd[0]);
|
|
|
|
|
|
|
|
/* Close user input pipe */
|
|
|
|
close(term->stdin_pipe_fd[1]);
|
|
|
|
close(term->stdin_pipe_fd[0]);
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
/* Wait for render thread to finish */
|
|
|
|
pthread_join(term->thread, NULL);
|
|
|
|
|
|
|
|
/* Close and flush any open pipe stream */
|
|
|
|
guac_terminal_pipe_stream_close(term);
|
|
|
|
|
|
|
|
/* Close and flush any active typescript */
|
|
|
|
guac_terminal_typescript_free(term->typescript);
|
|
|
|
|
2013-04-25 18:55:50 +00:00
|
|
|
/* Free display */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_free(term->display);
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-29 08:56:27 +00:00
|
|
|
/* Free buffer */
|
|
|
|
guac_terminal_buffer_free(term->buffer);
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Free clipboard */
|
|
|
|
guac_common_clipboard_free(term->clipboard);
|
|
|
|
|
2015-01-26 21:37:07 +00:00
|
|
|
/* Free scrollbar */
|
|
|
|
guac_terminal_scrollbar_free(term->scrollbar);
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
/* Free the terminal itself */
|
|
|
|
free(term);
|
2014-05-06 01:58:53 +00:00
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
/**
|
|
|
|
* Waits for data to become available on the given file descriptor.
|
|
|
|
*
|
|
|
|
* @param fd
|
|
|
|
* The file descriptor to wait on.
|
|
|
|
*
|
|
|
|
* @param msec_timeout
|
|
|
|
* The maximum amount of time to wait, in milliseconds.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* A positive if data is available, zero if the timeout has elapsed without
|
|
|
|
* data becoming available, or negative if an error occurred.
|
|
|
|
*/
|
|
|
|
static int guac_terminal_wait_for_data(int fd, int msec_timeout) {
|
|
|
|
|
|
|
|
/* Build fd_set */
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
|
|
|
|
/* Split millisecond timeout into seconds and microseconds */
|
|
|
|
struct timeval timeout = {
|
|
|
|
.tv_sec = msec_timeout / 1000,
|
|
|
|
.tv_usec = (msec_timeout % 1000) * 1000
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Wait for data */
|
|
|
|
return select(fd+1, &fds, NULL, NULL, &timeout);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-06 23:12:29 +00:00
|
|
|
int guac_terminal_render_frame(guac_terminal* terminal) {
|
|
|
|
|
|
|
|
guac_client* client = terminal->client;
|
2015-02-04 21:08:02 +00:00
|
|
|
char buffer[GUAC_TERMINAL_PACKET_SIZE];
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
int wait_result;
|
2014-05-06 23:12:29 +00:00
|
|
|
int fd = terminal->stdout_pipe_fd[0];
|
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
/* Wait for data to be available */
|
|
|
|
wait_result = guac_terminal_wait_for_data(fd, 1000);
|
|
|
|
if (wait_result > 0) {
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
guac_terminal_lock(terminal);
|
|
|
|
guac_timestamp frame_start = guac_timestamp_current();
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
do {
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
guac_timestamp frame_end;
|
|
|
|
int frame_remaining;
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
int bytes_read;
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
/* Read data, write to terminal */
|
|
|
|
if ((bytes_read = guac_terminal_packet_read(fd,
|
|
|
|
buffer, sizeof(buffer))) > 0) {
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
if (guac_terminal_write(terminal, buffer, bytes_read)) {
|
|
|
|
guac_client_abort(client,
|
|
|
|
GUAC_PROTOCOL_STATUS_SERVER_ERROR,
|
|
|
|
"Error writing data");
|
|
|
|
guac_terminal_unlock(terminal);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify on error */
|
|
|
|
if (bytes_read < 0) {
|
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
|
|
|
|
"Error reading data");
|
|
|
|
guac_terminal_unlock(terminal);
|
2014-05-06 23:12:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
/* Calculate time remaining in frame */
|
|
|
|
frame_end = guac_timestamp_current();
|
|
|
|
frame_remaining = frame_start + GUAC_TERMINAL_FRAME_DURATION
|
|
|
|
- frame_end;
|
2014-05-06 23:12:29 +00:00
|
|
|
|
2015-02-04 22:01:25 +00:00
|
|
|
/* Wait again if frame remaining */
|
|
|
|
if (frame_remaining > 0)
|
|
|
|
wait_result = guac_terminal_wait_for_data(fd,
|
|
|
|
GUAC_TERMINAL_FRAME_TIMEOUT);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
} while (wait_result > 0);
|
2014-05-06 23:12:29 +00:00
|
|
|
|
|
|
|
/* Flush terminal */
|
|
|
|
guac_terminal_flush(terminal);
|
|
|
|
guac_terminal_unlock(terminal);
|
|
|
|
|
|
|
|
}
|
2015-02-04 22:01:25 +00:00
|
|
|
|
|
|
|
/* Notify of any errors */
|
|
|
|
if (wait_result < 0) {
|
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
|
|
|
|
"Error waiting for data");
|
2014-05-06 23:12:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-06 23:41:48 +00:00
|
|
|
int guac_terminal_read_stdin(guac_terminal* terminal, char* c, int size) {
|
|
|
|
int stdin_fd = terminal->stdin_pipe_fd[0];
|
|
|
|
return read(stdin_fd, c, size);
|
|
|
|
}
|
|
|
|
|
2015-02-04 21:08:02 +00:00
|
|
|
int guac_terminal_write_stdout(guac_terminal* terminal, const char* c,
|
|
|
|
int size) {
|
|
|
|
|
|
|
|
/* Write maximally-sized packets until only one packet remains */
|
|
|
|
while (size > GUAC_TERMINAL_PACKET_SIZE) {
|
|
|
|
|
|
|
|
/* Write maximally-sized packet */
|
|
|
|
if (guac_terminal_packet_write(terminal->stdout_pipe_fd[1], c,
|
|
|
|
GUAC_TERMINAL_PACKET_SIZE) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Advance to next packet */
|
|
|
|
c += GUAC_TERMINAL_PACKET_SIZE;
|
|
|
|
size -= GUAC_TERMINAL_PACKET_SIZE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return guac_terminal_packet_write(terminal->stdout_pipe_fd[1], c, size);
|
2014-05-06 23:41:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 21:19:40 +00:00
|
|
|
int guac_terminal_notify(guac_terminal* terminal) {
|
|
|
|
return guac_terminal_packet_write(terminal->stdout_pipe_fd[1], NULL, 0);
|
|
|
|
}
|
|
|
|
|
2014-05-06 23:41:48 +00:00
|
|
|
int guac_terminal_printf(guac_terminal* terminal, const char* format, ...) {
|
|
|
|
|
|
|
|
int written;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
/* Print to buffer */
|
|
|
|
va_start(ap, format);
|
|
|
|
written = vsnprintf(buffer, sizeof(buffer)-1, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (written < 0)
|
|
|
|
return written;
|
|
|
|
|
|
|
|
/* Write to STDOUT */
|
|
|
|
return guac_terminal_write_stdout(terminal, buffer, written);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
char* guac_terminal_prompt(guac_terminal* terminal, const char* title,
|
|
|
|
bool echo) {
|
|
|
|
|
|
|
|
char buffer[1024];
|
2014-05-06 23:41:48 +00:00
|
|
|
|
|
|
|
int pos;
|
|
|
|
char in_byte;
|
|
|
|
|
|
|
|
/* Print title */
|
|
|
|
guac_terminal_printf(terminal, "%s", title);
|
|
|
|
|
|
|
|
/* Read bytes until newline */
|
|
|
|
pos = 0;
|
2016-03-01 05:41:56 +00:00
|
|
|
while (guac_terminal_read_stdin(terminal, &in_byte, 1) == 1) {
|
2014-05-06 23:41:48 +00:00
|
|
|
|
|
|
|
/* Backspace */
|
|
|
|
if (in_byte == 0x7F) {
|
|
|
|
if (pos > 0) {
|
|
|
|
guac_terminal_printf(terminal, "\b \b");
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CR (end of input */
|
|
|
|
else if (in_byte == 0x0D) {
|
|
|
|
guac_terminal_printf(terminal, "\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
/* Otherwise, store byte if there is room */
|
|
|
|
else if (pos < sizeof(buffer) - 1) {
|
2014-05-06 23:41:48 +00:00
|
|
|
|
|
|
|
/* Store character, update buffers */
|
2016-03-01 05:41:56 +00:00
|
|
|
buffer[pos++] = in_byte;
|
2014-05-06 23:41:48 +00:00
|
|
|
|
|
|
|
/* Print character if echoing */
|
|
|
|
if (echo)
|
|
|
|
guac_terminal_printf(terminal, "%c", in_byte);
|
|
|
|
else
|
|
|
|
guac_terminal_printf(terminal, "*");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
/* Ignore all other input */
|
|
|
|
|
2014-05-06 23:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate string */
|
2016-03-01 05:41:56 +00:00
|
|
|
buffer[pos] = 0;
|
|
|
|
|
|
|
|
/* Return newly-allocated string containing result */
|
|
|
|
return strdup(buffer);
|
2014-05-06 23:41:48 +00:00
|
|
|
|
2014-05-06 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 21:56:20 +00:00
|
|
|
int guac_terminal_set(guac_terminal* term, int row, int col, int codepoint) {
|
2013-03-24 23:56:17 +00:00
|
|
|
|
2014-06-04 20:49:35 +00:00
|
|
|
int width;
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
/* Build character with current attributes */
|
|
|
|
guac_terminal_char guac_char;
|
2013-05-02 21:56:20 +00:00
|
|
|
guac_char.value = codepoint;
|
2013-03-24 23:56:17 +00:00
|
|
|
guac_char.attributes = term->current_attributes;
|
|
|
|
|
2014-06-04 20:49:35 +00:00
|
|
|
width = wcwidth(codepoint);
|
|
|
|
if (width < 0)
|
|
|
|
width = 1;
|
|
|
|
|
|
|
|
guac_char.width = width;
|
|
|
|
|
|
|
|
guac_terminal_set_columns(term, row, col, col + width - 1, &guac_char);
|
2013-03-29 08:56:27 +00:00
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:46:26 +00:00
|
|
|
void guac_terminal_commit_cursor(guac_terminal* term) {
|
2013-03-29 09:51:31 +00:00
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_char* guac_char;
|
2013-04-05 20:12:18 +00:00
|
|
|
|
2013-05-15 19:46:26 +00:00
|
|
|
guac_terminal_buffer_row* old_row;
|
|
|
|
guac_terminal_buffer_row* new_row;
|
2013-03-29 09:51:31 +00:00
|
|
|
|
2013-05-15 19:46:26 +00:00
|
|
|
/* If no change, done */
|
|
|
|
if (term->visible_cursor_row == term->cursor_row && term->visible_cursor_col == term->cursor_col)
|
|
|
|
return;
|
2013-03-29 09:51:31 +00:00
|
|
|
|
2013-05-15 19:46:26 +00:00
|
|
|
/* Get old and new rows with cursor */
|
|
|
|
new_row = guac_terminal_buffer_get_row(term->buffer, term->cursor_row, term->cursor_col+1);
|
|
|
|
old_row = guac_terminal_buffer_get_row(term->buffer, term->visible_cursor_row, term->visible_cursor_col+1);
|
2013-03-29 09:51:31 +00:00
|
|
|
|
2013-05-15 19:46:26 +00:00
|
|
|
/* Clear cursor */
|
|
|
|
guac_char = &(old_row->characters[term->visible_cursor_col]);
|
|
|
|
guac_char->attributes.cursor = false;
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(term->display, term->visible_cursor_row + term->scroll_offset,
|
2013-05-15 19:46:26 +00:00
|
|
|
term->visible_cursor_col, term->visible_cursor_col, guac_char);
|
|
|
|
|
|
|
|
/* Set cursor */
|
|
|
|
guac_char = &(new_row->characters[term->cursor_col]);
|
|
|
|
guac_char->attributes.cursor = true;
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(term->display, term->cursor_row + term->scroll_offset,
|
2013-05-15 19:46:26 +00:00
|
|
|
term->cursor_col, term->cursor_col, guac_char);
|
|
|
|
|
|
|
|
term->visible_cursor_row = term->cursor_row;
|
|
|
|
term->visible_cursor_col = term->cursor_col;
|
|
|
|
|
|
|
|
return;
|
2013-03-29 09:51:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
int guac_terminal_write(guac_terminal* term, const char* c, int size) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
while (size > 0) {
|
2016-01-27 00:42:49 +00:00
|
|
|
|
|
|
|
/* Read and advance to next character */
|
|
|
|
char current = *(c++);
|
2011-08-04 18:46:21 +00:00
|
|
|
size--;
|
2016-01-27 00:42:49 +00:00
|
|
|
|
|
|
|
/* Write character to typescript, if any */
|
|
|
|
if (term->typescript != NULL)
|
|
|
|
guac_terminal_typescript_write(term->typescript, current);
|
|
|
|
|
|
|
|
/* Handle character and its meaning */
|
|
|
|
term->char_handler(term, current);
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
int guac_terminal_scroll_up(guac_terminal* term,
|
2011-08-05 07:20:09 +00:00
|
|
|
int start_row, int end_row, int amount) {
|
2013-04-26 17:55:55 +00:00
|
|
|
|
2013-04-26 21:52:51 +00:00
|
|
|
/* If scrolling entire display, update scroll offset */
|
|
|
|
if (start_row == 0 && end_row == term->term_height - 1) {
|
|
|
|
|
|
|
|
/* Scroll up visibly */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_copy_rows(term->display, start_row + amount, end_row, -amount);
|
2013-04-26 21:52:51 +00:00
|
|
|
|
|
|
|
/* Advance by scroll amount */
|
|
|
|
term->buffer->top += amount;
|
2013-04-28 08:28:49 +00:00
|
|
|
if (term->buffer->top >= term->buffer->available)
|
|
|
|
term->buffer->top -= term->buffer->available;
|
|
|
|
|
2013-04-26 21:52:51 +00:00
|
|
|
term->buffer->length += amount;
|
2013-04-28 08:28:49 +00:00
|
|
|
if (term->buffer->length > term->buffer->available)
|
|
|
|
term->buffer->length = term->buffer->available;
|
2013-04-26 21:52:51 +00:00
|
|
|
|
2015-01-26 23:51:50 +00:00
|
|
|
/* Reset scrollbar bounds */
|
|
|
|
guac_terminal_scrollbar_set_bounds(term->scrollbar, term->term_height - term->buffer->length, 0);
|
|
|
|
|
2013-05-15 20:55:40 +00:00
|
|
|
/* Update cursor location if within region */
|
|
|
|
if (term->visible_cursor_row >= start_row &&
|
|
|
|
term->visible_cursor_row <= end_row)
|
|
|
|
term->visible_cursor_row -= amount;
|
|
|
|
|
2013-04-26 21:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, just copy row data upwards */
|
|
|
|
else
|
|
|
|
guac_terminal_copy_rows(term, start_row + amount, end_row, -amount);
|
2013-04-26 17:55:55 +00:00
|
|
|
|
|
|
|
/* Clear new area */
|
|
|
|
guac_terminal_clear_range(term,
|
|
|
|
end_row - amount + 1, 0,
|
|
|
|
end_row, term->term_width - 1);
|
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
return 0;
|
2011-08-05 07:20:09 +00:00
|
|
|
}
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
int guac_terminal_scroll_down(guac_terminal* term,
|
2011-08-10 01:32:54 +00:00
|
|
|
int start_row, int end_row, int amount) {
|
2013-05-03 05:50:33 +00:00
|
|
|
|
|
|
|
guac_terminal_copy_rows(term, start_row, end_row - amount, amount);
|
|
|
|
|
|
|
|
/* Clear new area */
|
|
|
|
guac_terminal_clear_range(term,
|
|
|
|
start_row, 0,
|
|
|
|
start_row + amount - 1, term->term_width - 1);
|
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-08-10 01:32:54 +00:00
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
int guac_terminal_clear_columns(guac_terminal* term,
|
|
|
|
int row, int start_col, int end_col) {
|
|
|
|
|
|
|
|
/* Build space */
|
|
|
|
guac_terminal_char blank;
|
2013-05-15 17:11:47 +00:00
|
|
|
blank.value = 0;
|
2013-04-26 08:53:19 +00:00
|
|
|
blank.attributes = term->current_attributes;
|
2014-06-04 20:49:35 +00:00
|
|
|
blank.width = 1;
|
2011-08-10 01:32:54 +00:00
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
/* Clear */
|
|
|
|
guac_terminal_set_columns(term,
|
|
|
|
row, start_col, end_col, &blank);
|
2011-08-10 01:32:54 +00:00
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
return 0;
|
2011-08-10 01:32:54 +00:00
|
|
|
|
|
|
|
}
|
2011-08-05 07:20:09 +00:00
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
int guac_terminal_clear_range(guac_terminal* term,
|
2011-08-05 07:20:09 +00:00
|
|
|
int start_row, int start_col,
|
2013-03-29 09:51:31 +00:00
|
|
|
int end_row, int end_col) {
|
2011-08-05 07:20:09 +00:00
|
|
|
|
|
|
|
/* If not at far left, must clear sub-region to far right */
|
|
|
|
if (start_col > 0) {
|
|
|
|
|
|
|
|
/* Clear from start_col to far right */
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_columns(term,
|
|
|
|
start_row, start_col, term->term_width - 1);
|
2011-08-05 07:20:09 +00:00
|
|
|
|
|
|
|
/* One less row to clear */
|
|
|
|
start_row++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not at far right, must clear sub-region to far left */
|
|
|
|
if (end_col < term->term_width - 1) {
|
|
|
|
|
|
|
|
/* Clear from far left to end_col */
|
2013-04-26 08:53:19 +00:00
|
|
|
guac_terminal_clear_columns(term, end_row, 0, end_col);
|
2011-08-05 07:20:09 +00:00
|
|
|
|
|
|
|
/* One less row to clear */
|
|
|
|
end_row--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remaining region now guaranteed rectangular. Clear, if possible */
|
|
|
|
if (start_row <= end_row) {
|
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
int row;
|
|
|
|
for (row=start_row; row<=end_row; row++) {
|
|
|
|
|
|
|
|
/* Clear entire row */
|
|
|
|
guac_terminal_clear_columns(term, row, 0, term->term_width - 1);
|
|
|
|
|
|
|
|
}
|
2011-08-05 07:20:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-08 07:47:08 +00:00
|
|
|
void guac_terminal_scroll_display_down(guac_terminal* terminal,
|
|
|
|
int scroll_amount) {
|
2013-04-05 08:32:33 +00:00
|
|
|
|
2013-04-05 18:31:46 +00:00
|
|
|
int start_row, end_row;
|
|
|
|
int dest_row;
|
2013-04-05 08:32:33 +00:00
|
|
|
int row, column;
|
|
|
|
|
|
|
|
/* Limit scroll amount by size of scrollback buffer */
|
|
|
|
if (scroll_amount > terminal->scroll_offset)
|
|
|
|
scroll_amount = terminal->scroll_offset;
|
|
|
|
|
|
|
|
/* If not scrolling at all, don't bother trying */
|
2013-04-28 08:28:49 +00:00
|
|
|
if (scroll_amount <= 0)
|
2013-04-05 08:32:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Shift screen up */
|
|
|
|
if (terminal->term_height > scroll_amount)
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_copy_rows(terminal->display,
|
2013-04-26 17:55:55 +00:00
|
|
|
scroll_amount, terminal->term_height - 1,
|
|
|
|
-scroll_amount);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
/* Advance by scroll amount */
|
|
|
|
terminal->scroll_offset -= scroll_amount;
|
2015-01-26 23:51:50 +00:00
|
|
|
guac_terminal_scrollbar_set_value(terminal->scrollbar, -terminal->scroll_offset);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
2013-04-05 18:31:46 +00:00
|
|
|
/* Get row range */
|
|
|
|
end_row = terminal->term_height - terminal->scroll_offset - 1;
|
|
|
|
start_row = end_row - scroll_amount + 1;
|
|
|
|
dest_row = terminal->term_height - scroll_amount;
|
|
|
|
|
2013-04-05 08:32:33 +00:00
|
|
|
/* Draw new rows from scrollback */
|
2013-04-05 18:31:46 +00:00
|
|
|
for (row=start_row; row<=end_row; row++) {
|
2013-04-05 08:32:33 +00:00
|
|
|
|
2013-04-25 19:10:01 +00:00
|
|
|
/* Get row from scrollback */
|
|
|
|
guac_terminal_buffer_row* buffer_row =
|
2013-04-26 09:29:30 +00:00
|
|
|
guac_terminal_buffer_get_row(terminal->buffer, row, 0);
|
2013-04-05 19:54:59 +00:00
|
|
|
|
2013-04-26 21:52:51 +00:00
|
|
|
/* Clear row */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(terminal->display,
|
|
|
|
dest_row, 0, terminal->display->width, &(terminal->default_char));
|
2013-04-26 21:52:51 +00:00
|
|
|
|
2013-04-25 19:10:01 +00:00
|
|
|
/* Draw row */
|
|
|
|
guac_terminal_char* current = buffer_row->characters;
|
2014-06-04 20:49:35 +00:00
|
|
|
for (column=0; column<buffer_row->length; column++) {
|
|
|
|
|
|
|
|
/* Only draw if not blank */
|
|
|
|
if (guac_terminal_has_glyph(current->value))
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(terminal->display, dest_row, column, column, current);
|
2014-06-04 20:49:35 +00:00
|
|
|
|
|
|
|
current++;
|
|
|
|
|
|
|
|
}
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
/* Next row */
|
2013-04-05 18:31:46 +00:00
|
|
|
dest_row++;
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-04 21:19:40 +00:00
|
|
|
guac_terminal_notify(terminal);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-08 07:47:08 +00:00
|
|
|
void guac_terminal_scroll_display_up(guac_terminal* terminal,
|
|
|
|
int scroll_amount) {
|
2013-04-05 08:32:33 +00:00
|
|
|
|
2013-04-05 18:31:46 +00:00
|
|
|
int start_row, end_row;
|
|
|
|
int dest_row;
|
2013-04-05 08:32:33 +00:00
|
|
|
int row, column;
|
|
|
|
|
|
|
|
/* Limit scroll amount by size of scrollback buffer */
|
2013-04-28 08:28:49 +00:00
|
|
|
if (terminal->scroll_offset + scroll_amount > terminal->buffer->length - terminal->term_height)
|
|
|
|
scroll_amount = terminal->buffer->length - terminal->scroll_offset - terminal->term_height;
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
/* If not scrolling at all, don't bother trying */
|
2013-04-28 08:28:49 +00:00
|
|
|
if (scroll_amount <= 0)
|
2013-04-05 08:32:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Shift screen down */
|
|
|
|
if (terminal->term_height > scroll_amount)
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_copy_rows(terminal->display,
|
2013-04-26 17:55:55 +00:00
|
|
|
0, terminal->term_height - scroll_amount - 1,
|
|
|
|
scroll_amount);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
/* Advance by scroll amount */
|
|
|
|
terminal->scroll_offset += scroll_amount;
|
2015-01-26 23:51:50 +00:00
|
|
|
guac_terminal_scrollbar_set_value(terminal->scrollbar, -terminal->scroll_offset);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
2013-04-05 18:31:46 +00:00
|
|
|
/* Get row range */
|
|
|
|
start_row = -terminal->scroll_offset;
|
|
|
|
end_row = start_row + scroll_amount - 1;
|
|
|
|
dest_row = 0;
|
|
|
|
|
2013-04-05 08:32:33 +00:00
|
|
|
/* Draw new rows from scrollback */
|
2013-04-05 18:31:46 +00:00
|
|
|
for (row=start_row; row<=end_row; row++) {
|
2013-04-05 08:32:33 +00:00
|
|
|
|
2013-04-05 18:31:46 +00:00
|
|
|
/* Get row from scrollback */
|
2013-04-25 19:10:01 +00:00
|
|
|
guac_terminal_buffer_row* buffer_row =
|
2013-04-26 09:29:30 +00:00
|
|
|
guac_terminal_buffer_get_row(terminal->buffer, row, 0);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
2013-04-26 21:52:51 +00:00
|
|
|
/* Clear row */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(terminal->display,
|
|
|
|
dest_row, 0, terminal->display->width, &(terminal->default_char));
|
2013-04-26 21:52:51 +00:00
|
|
|
|
2013-04-05 08:32:33 +00:00
|
|
|
/* Draw row */
|
2013-04-25 19:10:01 +00:00
|
|
|
guac_terminal_char* current = buffer_row->characters;
|
2014-06-04 20:49:35 +00:00
|
|
|
for (column=0; column<buffer_row->length; column++) {
|
|
|
|
|
|
|
|
/* Only draw if not blank */
|
|
|
|
if (guac_terminal_has_glyph(current->value))
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(terminal->display, dest_row, column, column, current);
|
2014-06-04 20:49:35 +00:00
|
|
|
|
|
|
|
current++;
|
|
|
|
|
|
|
|
}
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
/* Next row */
|
2013-04-05 18:31:46 +00:00
|
|
|
dest_row++;
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-04 21:19:40 +00:00
|
|
|
guac_terminal_notify(terminal);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-06 08:02:23 +00:00
|
|
|
void guac_terminal_select_redraw(guac_terminal* terminal) {
|
|
|
|
|
2014-06-06 21:05:41 +00:00
|
|
|
int start_row = terminal->selection_start_row + terminal->scroll_offset;
|
|
|
|
int start_column = terminal->selection_start_column;
|
|
|
|
|
|
|
|
int end_row = terminal->selection_end_row + terminal->scroll_offset;
|
|
|
|
int end_column = terminal->selection_end_column;
|
|
|
|
|
|
|
|
/* Update start/end columns to include character width */
|
|
|
|
if (start_row > end_row || (start_row == end_row && start_column > end_column))
|
|
|
|
start_column += terminal->selection_start_width - 1;
|
|
|
|
else
|
|
|
|
end_column += terminal->selection_end_width - 1;
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_select(terminal->display, start_row, start_column, end_row, end_column);
|
2014-06-06 21:05:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Locates the beginning of the character at the given row and column, updating
|
|
|
|
* the column to the starting column of that character. The width, if available,
|
|
|
|
* is returned. If the character has no defined width, 1 is returned.
|
|
|
|
*/
|
|
|
|
static int __guac_terminal_find_char(guac_terminal* terminal, int row, int* column) {
|
|
|
|
|
|
|
|
int start_column = *column;
|
|
|
|
|
|
|
|
guac_terminal_buffer_row* buffer_row = guac_terminal_buffer_get_row(terminal->buffer, row, 0);
|
|
|
|
if (start_column < buffer_row->length) {
|
|
|
|
|
|
|
|
/* Find beginning of character */
|
|
|
|
guac_terminal_char* start_char = &(buffer_row->characters[start_column]);
|
|
|
|
while (start_column > 0 && start_char->value == GUAC_CHAR_CONTINUATION) {
|
|
|
|
start_char--;
|
|
|
|
start_column--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use width, if available */
|
|
|
|
if (start_char->value != GUAC_CHAR_CONTINUATION) {
|
|
|
|
*column = start_column;
|
|
|
|
return start_char->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Default to one column wide */
|
|
|
|
return 1;
|
2013-05-06 08:02:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
void guac_terminal_select_start(guac_terminal* terminal, int row, int column) {
|
2013-05-06 08:02:23 +00:00
|
|
|
|
2014-06-06 21:05:41 +00:00
|
|
|
int width = __guac_terminal_find_char(terminal, row, &column);
|
|
|
|
|
2013-05-06 08:02:23 +00:00
|
|
|
terminal->selection_start_row =
|
|
|
|
terminal->selection_end_row = row;
|
|
|
|
|
|
|
|
terminal->selection_start_column =
|
|
|
|
terminal->selection_end_column = column;
|
|
|
|
|
2014-06-06 21:05:41 +00:00
|
|
|
terminal->selection_start_width =
|
|
|
|
terminal->selection_end_width = width;
|
|
|
|
|
2013-05-06 08:02:23 +00:00
|
|
|
terminal->text_selected = true;
|
|
|
|
|
|
|
|
guac_terminal_select_redraw(terminal);
|
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_select_update(guac_terminal* terminal, int row, int column) {
|
2013-05-06 08:02:23 +00:00
|
|
|
|
2014-06-06 21:05:41 +00:00
|
|
|
/* Only update if selection has changed */
|
|
|
|
if (row != terminal->selection_end_row
|
|
|
|
|| column < terminal->selection_end_column
|
|
|
|
|| column >= terminal->selection_end_column + terminal->selection_end_width) {
|
|
|
|
|
|
|
|
int width = __guac_terminal_find_char(terminal, row, &column);
|
|
|
|
|
|
|
|
terminal->selection_end_row = row;
|
|
|
|
terminal->selection_end_column = column;
|
|
|
|
terminal->selection_end_width = width;
|
2013-05-06 08:02:23 +00:00
|
|
|
|
|
|
|
guac_terminal_select_redraw(terminal);
|
|
|
|
}
|
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 18:06:21 +00:00
|
|
|
int __guac_terminal_buffer_string(guac_terminal_buffer_row* row, int start, int end, char* string) {
|
|
|
|
|
|
|
|
int length = 0;
|
|
|
|
int i;
|
|
|
|
for (i=start; i<=end; i++) {
|
2013-05-15 17:11:47 +00:00
|
|
|
|
|
|
|
int codepoint = row->characters[i].value;
|
|
|
|
|
|
|
|
/* If not null (blank), add to string */
|
2014-06-04 20:49:35 +00:00
|
|
|
if (codepoint != 0 && codepoint != GUAC_CHAR_CONTINUATION) {
|
2013-05-15 17:11:47 +00:00
|
|
|
int bytes = guac_terminal_encode_utf8(codepoint, string);
|
|
|
|
string += bytes;
|
|
|
|
length += bytes;
|
|
|
|
}
|
|
|
|
|
2013-05-06 18:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_select_end(guac_terminal* terminal, char* string) {
|
|
|
|
|
|
|
|
/* Deselect */
|
2013-05-06 08:02:23 +00:00
|
|
|
terminal->text_selected = false;
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_commit_select(terminal->display);
|
2013-05-06 18:06:21 +00:00
|
|
|
|
|
|
|
guac_terminal_buffer_row* buffer_row;
|
|
|
|
|
|
|
|
int row;
|
|
|
|
|
|
|
|
int start_row, start_col;
|
|
|
|
int end_row, end_col;
|
|
|
|
|
|
|
|
/* Ensure proper ordering of start and end coords */
|
2014-06-06 21:05:41 +00:00
|
|
|
if (terminal->selection_start_row < terminal->selection_end_row
|
|
|
|
|| (terminal->selection_start_row == terminal->selection_end_row
|
|
|
|
&& terminal->selection_start_column < terminal->selection_end_column)) {
|
|
|
|
|
2013-05-06 18:06:21 +00:00
|
|
|
start_row = terminal->selection_start_row;
|
|
|
|
start_col = terminal->selection_start_column;
|
|
|
|
end_row = terminal->selection_end_row;
|
2014-06-06 21:05:41 +00:00
|
|
|
end_col = terminal->selection_end_column + terminal->selection_end_width - 1;
|
|
|
|
|
2013-05-06 18:06:21 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
end_row = terminal->selection_start_row;
|
2014-06-06 21:05:41 +00:00
|
|
|
end_col = terminal->selection_start_column + terminal->selection_start_width - 1;
|
2013-05-06 18:06:21 +00:00
|
|
|
start_row = terminal->selection_end_row;
|
|
|
|
start_col = terminal->selection_end_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If only one row, simply copy */
|
|
|
|
buffer_row = guac_terminal_buffer_get_row(terminal->buffer, start_row, 0);
|
|
|
|
if (end_row == start_row) {
|
|
|
|
if (buffer_row->length - 1 < end_col)
|
|
|
|
end_col = buffer_row->length - 1;
|
|
|
|
string += __guac_terminal_buffer_string(buffer_row, start_col, end_col, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, copy multiple rows */
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* Store first row */
|
|
|
|
string += __guac_terminal_buffer_string(buffer_row, start_col, buffer_row->length - 1, string);
|
|
|
|
|
|
|
|
/* Store all middle rows */
|
|
|
|
for (row=start_row+1; row<end_row; row++) {
|
|
|
|
|
|
|
|
buffer_row = guac_terminal_buffer_get_row(terminal->buffer, row, 0);
|
|
|
|
|
|
|
|
*(string++) = '\n';
|
|
|
|
string += __guac_terminal_buffer_string(buffer_row, 0, buffer_row->length - 1, string);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store last row */
|
|
|
|
buffer_row = guac_terminal_buffer_get_row(terminal->buffer, end_row, 0);
|
|
|
|
if (buffer_row->length - 1 < end_col)
|
|
|
|
end_col = buffer_row->length - 1;
|
2013-05-06 19:18:56 +00:00
|
|
|
|
|
|
|
*(string++) = '\n';
|
2013-05-06 18:06:21 +00:00
|
|
|
string += __guac_terminal_buffer_string(buffer_row, 0, end_col, string);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Null terminator */
|
|
|
|
*string = 0;
|
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
}
|
2013-04-15 08:22:05 +00:00
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
void guac_terminal_copy_columns(guac_terminal* terminal, int row,
|
|
|
|
int start_column, int end_column, int offset) {
|
2013-04-26 17:55:55 +00:00
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_copy_columns(terminal->display, row + terminal->scroll_offset,
|
2013-04-26 17:55:55 +00:00
|
|
|
start_column, end_column, offset);
|
|
|
|
|
|
|
|
guac_terminal_buffer_copy_columns(terminal->buffer, row,
|
|
|
|
start_column, end_column, offset);
|
|
|
|
|
2013-05-15 19:46:26 +00:00
|
|
|
/* Update cursor location if within region */
|
|
|
|
if (row == terminal->visible_cursor_row &&
|
|
|
|
terminal->visible_cursor_col >= start_column &&
|
|
|
|
terminal->visible_cursor_col <= end_column)
|
|
|
|
terminal->visible_cursor_col += offset;
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Force breaks around destination region */
|
|
|
|
__guac_terminal_force_break(terminal, row, start_column + offset);
|
|
|
|
__guac_terminal_force_break(terminal, row, end_column + offset + 1);
|
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
}
|
2013-04-15 08:22:05 +00:00
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
void guac_terminal_copy_rows(guac_terminal* terminal,
|
|
|
|
int start_row, int end_row, int offset) {
|
2013-04-26 17:55:55 +00:00
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_copy_rows(terminal->display,
|
2013-04-30 07:20:21 +00:00
|
|
|
start_row + terminal->scroll_offset, end_row + terminal->scroll_offset, offset);
|
2013-04-26 17:55:55 +00:00
|
|
|
|
|
|
|
guac_terminal_buffer_copy_rows(terminal->buffer,
|
|
|
|
start_row, end_row, offset);
|
|
|
|
|
2013-05-15 19:46:26 +00:00
|
|
|
/* Update cursor location if within region */
|
|
|
|
if (terminal->visible_cursor_row >= start_row &&
|
|
|
|
terminal->visible_cursor_row <= end_row)
|
|
|
|
terminal->visible_cursor_row += offset;
|
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
}
|
2013-04-15 08:22:05 +00:00
|
|
|
|
2013-04-26 08:53:19 +00:00
|
|
|
void guac_terminal_set_columns(guac_terminal* terminal, int row,
|
|
|
|
int start_column, int end_column, guac_terminal_char* character) {
|
2013-04-26 09:29:30 +00:00
|
|
|
|
2014-06-04 22:52:50 +00:00
|
|
|
__guac_terminal_set_columns(terminal, row, start_column, end_column, character);
|
2013-04-26 09:29:30 +00:00
|
|
|
|
2013-05-22 18:51:01 +00:00
|
|
|
/* If visible cursor in current row, preserve state */
|
|
|
|
if (row == terminal->visible_cursor_row
|
|
|
|
&& terminal->visible_cursor_col >= start_column
|
|
|
|
&& terminal->visible_cursor_col <= end_column) {
|
|
|
|
|
|
|
|
/* Create copy of character with cursor attribute set */
|
|
|
|
guac_terminal_char cursor_character = *character;
|
|
|
|
cursor_character.attributes.cursor = true;
|
|
|
|
|
2014-06-04 22:52:50 +00:00
|
|
|
__guac_terminal_set_columns(terminal, row,
|
2013-05-22 18:51:01 +00:00
|
|
|
terminal->visible_cursor_col, terminal->visible_cursor_col, &cursor_character);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:55:21 +00:00
|
|
|
/* Force breaks around destination region */
|
|
|
|
__guac_terminal_force_break(terminal, row, start_column);
|
|
|
|
__guac_terminal_force_break(terminal, row, end_column + 1);
|
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 19:35:20 +00:00
|
|
|
static void __guac_terminal_redraw_rect(guac_terminal* term, int start_row, int start_col, int end_row, int end_col) {
|
|
|
|
|
|
|
|
int row, col;
|
|
|
|
|
|
|
|
/* Redraw region */
|
|
|
|
for (row=start_row; row<=end_row; row++) {
|
|
|
|
|
|
|
|
guac_terminal_buffer_row* buffer_row =
|
|
|
|
guac_terminal_buffer_get_row(term->buffer, row - term->scroll_offset, 0);
|
|
|
|
|
|
|
|
/* Clear row */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(term->display,
|
2013-05-02 19:35:20 +00:00
|
|
|
row, start_col, end_col, &(term->default_char));
|
|
|
|
|
|
|
|
/* Copy characters */
|
2014-06-04 20:49:35 +00:00
|
|
|
for (col=start_col; col <= end_col && col < buffer_row->length; col++) {
|
|
|
|
|
|
|
|
/* Only redraw if not blank */
|
|
|
|
guac_terminal_char* c = &(buffer_row->characters[col]);
|
|
|
|
if (guac_terminal_has_glyph(c->value))
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_set_columns(term->display, row, col, col, c);
|
2014-06-04 20:49:35 +00:00
|
|
|
|
|
|
|
}
|
2013-05-02 19:35:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2013-05-01 23:54:29 +00:00
|
|
|
|
2013-05-02 10:18:10 +00:00
|
|
|
/* If height is decreasing, shift display up */
|
|
|
|
if (height < term->term_height) {
|
|
|
|
|
|
|
|
int shift_amount;
|
|
|
|
|
|
|
|
/* Get number of rows actually occupying terminal space */
|
|
|
|
int used_height = term->buffer->length;
|
|
|
|
if (used_height > term->term_height)
|
|
|
|
used_height = term->term_height;
|
|
|
|
|
|
|
|
shift_amount = used_height - height;
|
|
|
|
|
|
|
|
/* If the new terminal bottom covers N rows, shift up N rows */
|
|
|
|
if (shift_amount > 0) {
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_copy_rows(term->display,
|
|
|
|
shift_amount, term->display->height - 1, -shift_amount);
|
2013-05-02 10:18:10 +00:00
|
|
|
|
|
|
|
/* Update buffer top and cursor row based on shift */
|
|
|
|
term->buffer->top += shift_amount;
|
|
|
|
term->cursor_row -= shift_amount;
|
2013-05-20 17:27:53 +00:00
|
|
|
term->visible_cursor_row -= shift_amount;
|
2013-05-02 10:18:10 +00:00
|
|
|
|
2013-05-02 19:35:20 +00:00
|
|
|
/* Redraw characters within old region */
|
|
|
|
__guac_terminal_redraw_rect(term, height - shift_amount, 0, height-1, width-1);
|
|
|
|
|
2013-05-02 10:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-01 23:54:29 +00:00
|
|
|
/* Resize display */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_flush(term->display);
|
|
|
|
guac_terminal_display_resize(term->display, width, height);
|
2013-05-01 23:54:29 +00:00
|
|
|
|
2013-05-02 19:35:20 +00:00
|
|
|
/* Reraw any characters on right if widening */
|
|
|
|
if (width > term->term_width)
|
|
|
|
__guac_terminal_redraw_rect(term, 0, term->term_width-1, height-1, width-1);
|
|
|
|
|
2013-05-02 10:18:10 +00:00
|
|
|
/* If height is increasing, shift display down */
|
|
|
|
if (height > term->term_height) {
|
|
|
|
|
|
|
|
/* If undisplayed rows exist in the buffer, shift them into view */
|
|
|
|
if (term->term_height < term->buffer->length) {
|
|
|
|
|
|
|
|
/* If the new terminal bottom reveals N rows, shift down N rows */
|
|
|
|
int shift_amount = height - term->term_height;
|
|
|
|
|
|
|
|
/* The maximum amount we can shift is the number of undisplayed rows */
|
|
|
|
int max_shift = term->buffer->length - term->term_height;
|
|
|
|
|
|
|
|
if (shift_amount > max_shift)
|
|
|
|
shift_amount = max_shift;
|
|
|
|
|
|
|
|
/* Update buffer top and cursor row based on shift */
|
|
|
|
term->buffer->top -= shift_amount;
|
|
|
|
term->cursor_row += shift_amount;
|
2013-05-20 17:27:53 +00:00
|
|
|
term->visible_cursor_row += shift_amount;
|
2013-05-02 10:18:10 +00:00
|
|
|
|
|
|
|
/* If scrolled enough, use scroll to fulfill entire resize */
|
2013-05-02 19:35:20 +00:00
|
|
|
if (term->scroll_offset >= shift_amount) {
|
|
|
|
|
2013-05-02 10:18:10 +00:00
|
|
|
term->scroll_offset -= shift_amount;
|
2015-01-26 23:51:50 +00:00
|
|
|
guac_terminal_scrollbar_set_value(term->scrollbar, -term->scroll_offset);
|
2013-05-02 10:18:10 +00:00
|
|
|
|
2013-05-02 19:35:20 +00:00
|
|
|
/* Draw characters from scroll at bottom */
|
|
|
|
__guac_terminal_redraw_rect(term, term->term_height, 0, term->term_height + shift_amount - 1, width-1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-02 10:18:10 +00:00
|
|
|
/* Otherwise, fulfill with as much scroll as possible */
|
|
|
|
else {
|
|
|
|
|
2013-05-02 19:35:20 +00:00
|
|
|
/* Draw characters from scroll at bottom */
|
|
|
|
__guac_terminal_redraw_rect(term, term->term_height, 0, term->term_height + term->scroll_offset - 1, width-1);
|
|
|
|
|
|
|
|
/* Update shift_amount and scroll based on new rows */
|
2013-05-02 10:18:10 +00:00
|
|
|
shift_amount -= term->scroll_offset;
|
|
|
|
term->scroll_offset = 0;
|
2015-01-26 23:51:50 +00:00
|
|
|
guac_terminal_scrollbar_set_value(term->scrollbar, -term->scroll_offset);
|
2013-05-02 10:18:10 +00:00
|
|
|
|
|
|
|
/* If anything remains, move screen as necessary */
|
2013-05-02 19:35:20 +00:00
|
|
|
if (shift_amount > 0) {
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_copy_rows(term->display,
|
|
|
|
0, term->display->height - shift_amount - 1, shift_amount);
|
2013-05-02 10:18:10 +00:00
|
|
|
|
2013-05-02 19:35:20 +00:00
|
|
|
/* Draw characters at top from scroll */
|
|
|
|
__guac_terminal_redraw_rect(term, 0, 0, shift_amount - 1, width-1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-02 10:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* end if undisplayed rows exist */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-16 21:46:00 +00:00
|
|
|
/* Keep cursor on screen */
|
|
|
|
if (term->cursor_row < 0) term->cursor_row = 0;
|
|
|
|
if (term->cursor_row >= height) term->cursor_row = height-1;
|
|
|
|
if (term->cursor_col < 0) term->cursor_col = 0;
|
|
|
|
if (term->cursor_col >= width) term->cursor_col = width-1;
|
|
|
|
|
2013-05-01 23:54:29 +00:00
|
|
|
/* Commit new dimensions */
|
|
|
|
term->term_width = width;
|
|
|
|
term->term_height = height;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
int guac_terminal_resize(guac_terminal* terminal, int width, int height) {
|
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display* display = terminal->display;
|
2014-06-03 01:31:38 +00:00
|
|
|
guac_client* client = display->client;
|
|
|
|
|
2015-02-06 01:21:33 +00:00
|
|
|
/* Acquire exclusive access to terminal */
|
|
|
|
guac_terminal_lock(terminal);
|
|
|
|
|
2015-01-29 01:06:18 +00:00
|
|
|
/* Calculate available display area */
|
|
|
|
int available_width = width - GUAC_TERMINAL_SCROLLBAR_WIDTH;
|
|
|
|
if (available_width < 0)
|
|
|
|
available_width = 0;
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Calculate dimensions */
|
2014-06-03 01:31:38 +00:00
|
|
|
int rows = height / display->char_height;
|
2015-01-29 01:06:18 +00:00
|
|
|
int columns = available_width / display->char_width;
|
2014-06-03 01:31:38 +00:00
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
/* Set pixel sizes */
|
|
|
|
terminal->width = width;
|
|
|
|
terminal->height = height;
|
|
|
|
|
2014-06-03 01:31:38 +00:00
|
|
|
/* Resize default layer to given pixel dimensions */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_repaint_default_layer(terminal, client->socket);
|
2014-05-06 01:58:53 +00:00
|
|
|
|
2015-01-26 21:37:07 +00:00
|
|
|
/* Notify scrollbar of resize */
|
2015-01-26 23:51:50 +00:00
|
|
|
guac_terminal_scrollbar_parent_resized(terminal->scrollbar, width, height, rows);
|
|
|
|
guac_terminal_scrollbar_set_bounds(terminal->scrollbar, rows - terminal->buffer->length, 0);
|
2015-01-26 21:37:07 +00:00
|
|
|
|
2014-06-03 01:31:38 +00:00
|
|
|
/* Resize terminal if row/column dimensions have changed */
|
2014-05-06 01:58:53 +00:00
|
|
|
if (columns != terminal->term_width || rows != terminal->term_height) {
|
|
|
|
|
2014-06-03 01:31:38 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
2014-11-29 01:20:02 +00:00
|
|
|
"Resizing terminal to %ix%i", rows, columns);
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Resize terminal */
|
|
|
|
__guac_terminal_resize(terminal, columns, rows);
|
|
|
|
|
|
|
|
/* Reset scroll region */
|
|
|
|
terminal->scroll_end = rows - 1;
|
|
|
|
|
2014-06-03 01:31:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 01:21:33 +00:00
|
|
|
/* Release terminal */
|
|
|
|
guac_terminal_unlock(terminal);
|
|
|
|
|
2015-02-04 21:19:40 +00:00
|
|
|
guac_terminal_notify(terminal);
|
2014-05-06 01:58:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_flush(guac_terminal* terminal) {
|
2016-01-27 00:42:49 +00:00
|
|
|
|
|
|
|
/* Flush typescript if in use */
|
|
|
|
if (terminal->typescript != NULL)
|
|
|
|
guac_terminal_typescript_flush(terminal->typescript);
|
|
|
|
|
|
|
|
/* Flush display state */
|
2014-05-06 01:58:53 +00:00
|
|
|
guac_terminal_commit_cursor(terminal);
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_display_flush(terminal->display);
|
2015-01-29 00:43:03 +00:00
|
|
|
guac_terminal_scrollbar_flush(terminal->scrollbar);
|
2016-01-27 00:42:49 +00:00
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_lock(guac_terminal* terminal) {
|
|
|
|
pthread_mutex_lock(&(terminal->lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_unlock(guac_terminal* terminal) {
|
|
|
|
pthread_mutex_unlock(&(terminal->lock));
|
|
|
|
}
|
|
|
|
|
2013-05-26 05:45:26 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_terminal_send_string(guac_terminal* term, const char* data) {
|
|
|
|
return guac_terminal_write_all(term->stdin_pipe_fd[1], data, strlen(data));
|
|
|
|
}
|
|
|
|
|
2014-05-06 23:12:29 +00:00
|
|
|
static int __guac_terminal_send_key(guac_terminal* term, int keysym, int pressed) {
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
/* Hide mouse cursor if not already hidden */
|
2016-03-01 05:41:56 +00:00
|
|
|
if (term->current_cursor != GUAC_TERMINAL_CURSOR_BLANK) {
|
|
|
|
term->current_cursor = GUAC_TERMINAL_CURSOR_BLANK;
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_common_cursor_set_blank(term->cursor);
|
2015-02-04 21:19:40 +00:00
|
|
|
guac_terminal_notify(term);
|
2014-05-06 01:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2014-09-11 20:00:12 +00:00
|
|
|
/* Keysyms for '@' through '_' are all conveniently in C0 order */
|
|
|
|
if (keysym >= '@' && keysym <= '_')
|
|
|
|
data = (char) (keysym - '@');
|
|
|
|
|
|
|
|
/* Handle lowercase as well */
|
2014-05-06 01:58:53 +00:00
|
|
|
else if (keysym >= 'a' && keysym <= 'z')
|
|
|
|
data = (char) (keysym - 'a' + 1);
|
|
|
|
|
2014-09-11 20:00:12 +00:00
|
|
|
/* Ctrl+? is DEL (0x7f) */
|
|
|
|
else if (keysym == '?')
|
|
|
|
data = 0x7F;
|
|
|
|
|
2014-09-11 21:20:57 +00:00
|
|
|
/* Map Ctrl+2 to same result as Ctrl+@ */
|
|
|
|
else if (keysym == '2')
|
|
|
|
data = 0x00;
|
|
|
|
|
|
|
|
/* Map Ctrl+3 through Ctrl-7 to the remaining C0 characters such that Ctrl+6 is the same as Ctrl+^ */
|
|
|
|
else if (keysym >= '3' && keysym <= '7')
|
|
|
|
data = (char) (keysym - '3' + 0x1B);
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:19:15 +00:00
|
|
|
/* Typeable keys of number pad */
|
|
|
|
else if (keysym >= 0xFFAA && keysym <= 0xFFB9) {
|
|
|
|
char value = keysym - 0xFF80;
|
|
|
|
guac_terminal_send_data(term, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Non-printable keys */
|
|
|
|
else {
|
|
|
|
|
|
|
|
if (keysym == 0xFF08) return guac_terminal_send_string(term, "\x7F"); /* Backspace */
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFF09 || keysym == 0xFF89) return guac_terminal_send_string(term, "\x09"); /* Tab */
|
|
|
|
if (keysym == 0xFF0D || keysym == 0xFF8D) return guac_terminal_send_string(term, "\x0D"); /* Enter */
|
2014-05-06 01:58:53 +00:00
|
|
|
if (keysym == 0xFF1B) return guac_terminal_send_string(term, "\x1B"); /* Esc */
|
|
|
|
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFF50 || keysym == 0xFF95) return guac_terminal_send_string(term, "\x1B[1~"); /* Home */
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
/* Arrow keys w/ application cursor */
|
|
|
|
if (term->application_cursor_keys) {
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFF51 || keysym == 0xFF96) return guac_terminal_send_string(term, "\x1BOD"); /* Left */
|
|
|
|
if (keysym == 0xFF52 || keysym == 0xFF97) return guac_terminal_send_string(term, "\x1BOA"); /* Up */
|
|
|
|
if (keysym == 0xFF53 || keysym == 0xFF98) return guac_terminal_send_string(term, "\x1BOC"); /* Right */
|
|
|
|
if (keysym == 0xFF54 || keysym == 0xFF99) return guac_terminal_send_string(term, "\x1BOB"); /* Down */
|
2014-05-06 01:58:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFF51 || keysym == 0xFF96) return guac_terminal_send_string(term, "\x1B[D"); /* Left */
|
|
|
|
if (keysym == 0xFF52 || keysym == 0xFF97) return guac_terminal_send_string(term, "\x1B[A"); /* Up */
|
|
|
|
if (keysym == 0xFF53 || keysym == 0xFF98) return guac_terminal_send_string(term, "\x1B[C"); /* Right */
|
|
|
|
if (keysym == 0xFF54 || keysym == 0xFF99) return guac_terminal_send_string(term, "\x1B[B"); /* Down */
|
2014-05-06 01:58:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFF55 || keysym == 0xFF9A) return guac_terminal_send_string(term, "\x1B[5~"); /* Page up */
|
|
|
|
if (keysym == 0xFF56 || keysym == 0xFF9B) return guac_terminal_send_string(term, "\x1B[6~"); /* Page down */
|
|
|
|
if (keysym == 0xFF57 || keysym == 0xFF9C) return guac_terminal_send_string(term, "\x1B[4~"); /* End */
|
2014-05-06 01:58:53 +00:00
|
|
|
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFF63 || keysym == 0xFF9E) return guac_terminal_send_string(term, "\x1B[2~"); /* Insert */
|
2014-05-06 01:58:53 +00:00
|
|
|
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFFBE || keysym == 0xFF91) return guac_terminal_send_string(term, "\x1B[[A"); /* F1 */
|
|
|
|
if (keysym == 0xFFBF || keysym == 0xFF92) return guac_terminal_send_string(term, "\x1B[[B"); /* F2 */
|
|
|
|
if (keysym == 0xFFC0 || keysym == 0xFF93) return guac_terminal_send_string(term, "\x1B[[C"); /* F3 */
|
|
|
|
if (keysym == 0xFFC1 || keysym == 0xFF94) return guac_terminal_send_string(term, "\x1B[[D"); /* F4 */
|
2014-05-06 01:58:53 +00:00
|
|
|
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 */
|
|
|
|
|
2016-06-14 21:19:15 +00:00
|
|
|
if (keysym == 0xFFFF || keysym == 0xFF9F) return guac_terminal_send_string(term, "\x1B[3~"); /* Delete */
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
/* Ignore unknown keys */
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(term->client, GUAC_LOG_DEBUG,
|
|
|
|
"Ignoring unknown keysym: 0x%X", keysym);
|
2014-05-06 01:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-05-06 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed) {
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
guac_terminal_lock(term);
|
|
|
|
result = __guac_terminal_send_key(term, keysym, pressed);
|
|
|
|
guac_terminal_unlock(term);
|
|
|
|
|
|
|
|
return result;
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
static int __guac_terminal_send_mouse(guac_terminal* term, guac_user* user,
|
|
|
|
int x, int y, int mask) {
|
2014-05-06 01:58:53 +00:00
|
|
|
|
2015-02-04 07:51:04 +00:00
|
|
|
guac_client* client = term->client;
|
|
|
|
guac_socket* socket = client->socket;
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Determine which buttons were just released and pressed */
|
|
|
|
int released_mask = term->mouse_mask & ~mask;
|
|
|
|
int pressed_mask = ~term->mouse_mask & mask;
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
/* Store current mouse location */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_common_cursor_move(term->cursor, user, x, y);
|
2016-03-01 05:41:56 +00:00
|
|
|
|
2015-02-04 07:51:04 +00:00
|
|
|
/* Notify scrollbar, do not handle anything handled by scrollbar */
|
|
|
|
if (guac_terminal_scrollbar_handle_mouse(term->scrollbar, x, y, mask)) {
|
2015-02-04 09:01:39 +00:00
|
|
|
|
|
|
|
/* Set pointer cursor if mouse is over scrollbar */
|
2016-03-01 05:41:56 +00:00
|
|
|
if (term->current_cursor != GUAC_TERMINAL_CURSOR_POINTER) {
|
|
|
|
term->current_cursor = GUAC_TERMINAL_CURSOR_POINTER;
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_common_cursor_set_pointer(term->cursor);
|
2016-03-01 05:41:56 +00:00
|
|
|
guac_terminal_notify(term);
|
2015-02-04 09:01:39 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 21:19:40 +00:00
|
|
|
guac_terminal_notify(term);
|
2015-02-04 07:51:04 +00:00
|
|
|
return 0;
|
2015-02-04 09:01:39 +00:00
|
|
|
|
2015-02-04 07:51:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
term->mouse_mask = mask;
|
|
|
|
|
|
|
|
/* Show mouse cursor if not already shown */
|
2016-03-01 05:41:56 +00:00
|
|
|
if (term->current_cursor != GUAC_TERMINAL_CURSOR_IBAR) {
|
|
|
|
term->current_cursor = GUAC_TERMINAL_CURSOR_IBAR;
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_common_cursor_set_ibar(term->cursor);
|
2015-02-04 21:19:40 +00:00
|
|
|
guac_terminal_notify(term);
|
2014-05-06 01:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2015-02-04 07:51:04 +00:00
|
|
|
guac_common_clipboard_send(term->clipboard, client);
|
|
|
|
guac_socket_flush(socket);
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, just update */
|
|
|
|
else
|
|
|
|
guac_terminal_select_update(term,
|
2016-03-14 22:56:37 +00:00
|
|
|
y / term->display->char_height - term->scroll_offset,
|
|
|
|
x / term->display->char_width);
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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,
|
2016-03-14 22:56:37 +00:00
|
|
|
y / term->display->char_height - term->scroll_offset,
|
|
|
|
x / term->display->char_width);
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2014-05-06 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
int guac_terminal_send_mouse(guac_terminal* term, guac_user* user,
|
|
|
|
int x, int y, int mask) {
|
2014-05-06 23:12:29 +00:00
|
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
guac_terminal_lock(term);
|
2016-03-01 05:41:56 +00:00
|
|
|
result = __guac_terminal_send_mouse(term, user, x, y, mask);
|
2014-05-06 23:12:29 +00:00
|
|
|
guac_terminal_unlock(term);
|
2014-05-06 01:58:53 +00:00
|
|
|
|
2014-05-06 23:12:29 +00:00
|
|
|
return result;
|
2014-05-06 01:58:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:41:45 +00:00
|
|
|
void guac_terminal_scroll_handler(guac_terminal_scrollbar* scrollbar, int value) {
|
|
|
|
|
|
|
|
guac_terminal* terminal = (guac_terminal*) scrollbar->data;
|
|
|
|
|
|
|
|
/* Calculate change in scroll offset */
|
|
|
|
int delta = -value - terminal->scroll_offset;
|
|
|
|
|
|
|
|
/* Update terminal based on change in scroll offset */
|
|
|
|
if (delta < 0)
|
|
|
|
guac_terminal_scroll_display_down(terminal, -delta);
|
|
|
|
else if (delta > 0)
|
|
|
|
guac_terminal_scroll_display_up(terminal, delta);
|
|
|
|
|
|
|
|
/* Update scrollbar value */
|
|
|
|
guac_terminal_scrollbar_set_value(scrollbar, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-05-26 05:45:26 +00:00
|
|
|
int guac_terminal_sendf(guac_terminal* term, const char* format, ...) {
|
|
|
|
|
|
|
|
int written;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
/* Print to buffer */
|
|
|
|
va_start(ap, format);
|
|
|
|
written = vsnprintf(buffer, sizeof(buffer)-1, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (written < 0)
|
|
|
|
return written;
|
|
|
|
|
|
|
|
/* Write to STDIN */
|
|
|
|
return guac_terminal_write_all(term->stdin_pipe_fd[1], buffer, written);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-26 08:49:47 +00:00
|
|
|
void guac_terminal_set_tab(guac_terminal* term, int column) {
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Search for available space, set if available */
|
|
|
|
for (i=0; i<GUAC_TERMINAL_MAX_TABS; i++) {
|
|
|
|
|
|
|
|
/* Set tab if space free */
|
|
|
|
if (term->custom_tabs[i] == 0) {
|
|
|
|
term->custom_tabs[i] = column+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_unset_tab(guac_terminal* term, int column) {
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Search for given tab, unset if found */
|
|
|
|
for (i=0; i<GUAC_TERMINAL_MAX_TABS; i++) {
|
|
|
|
|
|
|
|
/* Unset tab if found */
|
|
|
|
if (term->custom_tabs[i] == column+1) {
|
|
|
|
term->custom_tabs[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_clear_tabs(guac_terminal* term) {
|
|
|
|
term->tab_interval = 0;
|
|
|
|
memset(term->custom_tabs, 0, sizeof(term->custom_tabs));
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_terminal_next_tab(guac_terminal* term, int column) {
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Determine tab stop from interval */
|
|
|
|
int tabstop;
|
|
|
|
if (term->tab_interval != 0)
|
|
|
|
tabstop = (column / term->tab_interval + 1) * term->tab_interval;
|
|
|
|
else
|
|
|
|
tabstop = term->term_width - 1;
|
|
|
|
|
|
|
|
/* Walk custom tabs, trying to find an earlier occurrence */
|
|
|
|
for (i=0; i<GUAC_TERMINAL_MAX_TABS; i++) {
|
|
|
|
|
|
|
|
int custom_tabstop = term->custom_tabs[i] - 1;
|
|
|
|
if (custom_tabstop != -1 && custom_tabstop > column && custom_tabstop < tabstop)
|
|
|
|
tabstop = custom_tabstop;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return tabstop;
|
|
|
|
}
|
|
|
|
|
2016-01-14 01:38:13 +00:00
|
|
|
void guac_terminal_pipe_stream_open(guac_terminal* term, const char* name) {
|
|
|
|
|
|
|
|
guac_client* client = term->client;
|
|
|
|
guac_socket* socket = client->socket;
|
|
|
|
|
|
|
|
/* Close existing stream, if any */
|
|
|
|
guac_terminal_pipe_stream_close(term);
|
|
|
|
|
|
|
|
/* Allocate and assign new pipe stream */
|
|
|
|
term->pipe_stream = guac_client_alloc_stream(client);
|
|
|
|
term->pipe_buffer_length = 0;
|
|
|
|
|
|
|
|
/* Open new pipe stream */
|
|
|
|
guac_protocol_send_pipe(socket, term->pipe_stream, "text/plain", name);
|
|
|
|
|
|
|
|
/* Log redirect at debug level */
|
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
|
|
|
"Terminal output now redirected to pipe '%s'.", name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_pipe_stream_write(guac_terminal* term, char c) {
|
|
|
|
|
|
|
|
/* Append byte to buffer only if pipe is open */
|
|
|
|
if (term->pipe_stream != NULL) {
|
|
|
|
|
|
|
|
/* Flush buffer if no space is available */
|
|
|
|
if (term->pipe_buffer_length == sizeof(term->pipe_buffer))
|
|
|
|
guac_terminal_pipe_stream_flush(term);
|
|
|
|
|
|
|
|
/* Append single byte to buffer */
|
|
|
|
term->pipe_buffer[term->pipe_buffer_length++] = c;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_pipe_stream_flush(guac_terminal* term) {
|
|
|
|
|
|
|
|
guac_client* client = term->client;
|
|
|
|
guac_socket* socket = client->socket;
|
|
|
|
guac_stream* pipe_stream = term->pipe_stream;
|
|
|
|
|
|
|
|
/* Write blob if data exists in buffer */
|
|
|
|
if (pipe_stream != NULL && term->pipe_buffer_length > 0) {
|
|
|
|
guac_protocol_send_blob(socket, pipe_stream,
|
|
|
|
term->pipe_buffer, term->pipe_buffer_length);
|
|
|
|
term->pipe_buffer_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_pipe_stream_close(guac_terminal* term) {
|
|
|
|
|
|
|
|
guac_client* client = term->client;
|
|
|
|
guac_socket* socket = client->socket;
|
|
|
|
guac_stream* pipe_stream = term->pipe_stream;
|
|
|
|
|
|
|
|
/* Close any existing pipe */
|
|
|
|
if (pipe_stream != NULL) {
|
|
|
|
|
|
|
|
/* Write end of stream */
|
|
|
|
guac_terminal_pipe_stream_flush(term);
|
|
|
|
guac_protocol_send_end(socket, pipe_stream);
|
|
|
|
|
|
|
|
/* Destroy stream */
|
|
|
|
guac_client_free_stream(client, pipe_stream);
|
|
|
|
term->pipe_stream = NULL;
|
|
|
|
|
|
|
|
/* Log redirect at debug level */
|
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
|
|
|
"Terminal output now redirected to display.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-26 01:04:15 +00:00
|
|
|
int guac_terminal_create_typescript(guac_terminal* term, const char* path,
|
|
|
|
const char* name, int create_path) {
|
|
|
|
|
|
|
|
/* Create typescript */
|
|
|
|
term->typescript = guac_terminal_typescript_alloc(path, name, create_path);
|
|
|
|
|
2016-01-27 20:40:09 +00:00
|
|
|
/* Log failure */
|
|
|
|
if (term->typescript == NULL) {
|
|
|
|
guac_client_log(term->client, GUAC_LOG_ERROR,
|
|
|
|
"Creation of typescript failed: %s", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If typescript was successfully created, log filenames */
|
|
|
|
guac_client_log(term->client, GUAC_LOG_INFO,
|
|
|
|
"Typescript of terminal session will be saved to \"%s\". "
|
|
|
|
"Timing file is \"%s\".",
|
|
|
|
term->typescript->data_filename,
|
|
|
|
term->typescript->timing_filename);
|
|
|
|
|
|
|
|
/* Typescript creation succeeded */
|
|
|
|
return 0;
|
2016-01-26 01:04:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-14 23:03:05 +00:00
|
|
|
void guac_terminal_dup(guac_terminal* term, guac_user* user,
|
2016-03-01 05:41:56 +00:00
|
|
|
guac_socket* socket) {
|
|
|
|
|
|
|
|
/* Synchronize display state with new user */
|
2016-03-14 22:56:37 +00:00
|
|
|
guac_terminal_repaint_default_layer(term, socket);
|
|
|
|
guac_terminal_display_dup(term->display, user, socket);
|
2016-03-01 05:41:56 +00:00
|
|
|
|
2016-03-14 22:56:37 +00:00
|
|
|
/* Synchronize mouse cursor */
|
|
|
|
guac_common_cursor_dup(term->cursor, user, socket);
|
|
|
|
|
|
|
|
/* Paint scrollbar for joining user */
|
|
|
|
guac_terminal_scrollbar_dup(term->scrollbar, user, socket);
|
2016-03-14 22:15:32 +00:00
|
|
|
|
2016-03-01 05:41:56 +00:00
|
|
|
}
|
|
|
|
|