2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is libguac-client-ssh.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Michael Jumper.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2011-12-30 22:34:04 +00:00
|
|
|
* James Muehlner <dagger10k@users.sourceforge.net>
|
2011-08-04 18:46:21 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-04-07 23:55:06 +00:00
|
|
|
#include <pthread.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
#include <cairo/cairo.h>
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
#include <guacamole/socket.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/client.h>
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
#include "terminal.h"
|
|
|
|
#include "terminal_handlers.h"
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
const guac_terminal_color guac_terminal_palette[16] = {
|
2011-08-05 19:14:15 +00:00
|
|
|
|
|
|
|
/* Normal colors */
|
|
|
|
{0x00, 0x00, 0x00}, /* Black */
|
2011-08-17 19:30:53 +00:00
|
|
|
{0x99, 0x3E, 0x3E}, /* Red */
|
|
|
|
{0x3E, 0x99, 0x3E}, /* Green */
|
|
|
|
{0x99, 0x99, 0x3E}, /* Brown */
|
|
|
|
{0x3E, 0x3E, 0x99}, /* Blue */
|
|
|
|
{0x99, 0x3E, 0x99}, /* Magenta */
|
|
|
|
{0x3E, 0x99, 0x99}, /* Cyan */
|
|
|
|
{0x99, 0x99, 0x99}, /* White */
|
2011-08-05 19:14:15 +00:00
|
|
|
|
|
|
|
/* Intense colors */
|
2011-08-17 19:30:53 +00:00
|
|
|
{0x3E, 0x3E, 0x3E}, /* Black */
|
|
|
|
{0xFF, 0x67, 0x67}, /* Red */
|
|
|
|
{0x67, 0xFF, 0x67}, /* Green */
|
|
|
|
{0xFF, 0xFF, 0x67}, /* Brown */
|
|
|
|
{0x67, 0x67, 0xFF}, /* Blue */
|
|
|
|
{0xFF, 0x67, 0xFF}, /* Magenta */
|
|
|
|
{0x67, 0xFF, 0xFF}, /* Cyan */
|
2011-08-05 19:14:15 +00:00
|
|
|
{0xFF, 0xFF, 0xFF}, /* White */
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal* guac_terminal_create(guac_client* client,
|
2012-10-23 08:38:10 +00:00
|
|
|
int width, int height) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2013-03-20 05:48:43 +00:00
|
|
|
guac_terminal_attributes default_attributes = {
|
|
|
|
.foreground = 7,
|
|
|
|
.background = 0,
|
|
|
|
.bold = false,
|
|
|
|
.reverse = false,
|
|
|
|
.underscore = false
|
|
|
|
};
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
PangoFontMap* font_map;
|
|
|
|
PangoFont* font;
|
|
|
|
PangoFontMetrics* metrics;
|
|
|
|
PangoContext* context;
|
|
|
|
|
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;
|
|
|
|
|
2013-03-20 05:48:43 +00:00
|
|
|
term->current_attributes =
|
|
|
|
term->default_attributes = default_attributes;
|
|
|
|
term->glyph_foreground = default_attributes.foreground;
|
|
|
|
term->glyph_background = default_attributes.background;
|
2011-08-05 19:14:15 +00:00
|
|
|
|
2011-08-19 01:09:20 +00:00
|
|
|
memset(term->glyphs, 0, sizeof(term->glyphs));
|
|
|
|
term->glyph_stroke = guac_client_alloc_buffer(client);
|
|
|
|
term->filled_glyphs = guac_client_alloc_buffer(client);
|
|
|
|
|
2012-10-23 08:38:10 +00:00
|
|
|
/* Get font */
|
|
|
|
term->font_desc = pango_font_description_new();
|
|
|
|
pango_font_description_set_family(term->font_desc, "monospace");
|
|
|
|
pango_font_description_set_weight(term->font_desc, PANGO_WEIGHT_NORMAL);
|
2013-03-25 10:04:14 +00:00
|
|
|
pango_font_description_set_size(term->font_desc, 12*PANGO_SCALE);
|
2012-10-23 08:38:10 +00:00
|
|
|
|
|
|
|
font_map = pango_cairo_font_map_get_default();
|
|
|
|
context = pango_font_map_create_context(font_map);
|
|
|
|
|
|
|
|
font = pango_font_map_load_font(font_map, context, term->font_desc);
|
|
|
|
if (font == NULL) {
|
|
|
|
guac_client_log_error(term->client, "Unable to get font.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics = pango_font_get_metrics(font, NULL);
|
|
|
|
if (metrics == NULL) {
|
|
|
|
guac_client_log_error(term->client, "Unable to get font metrics.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate character dimensions */
|
|
|
|
term->char_width =
|
|
|
|
pango_font_metrics_get_approximate_digit_width(metrics) / PANGO_SCALE;
|
|
|
|
term->char_height =
|
|
|
|
(pango_font_metrics_get_descent(metrics)
|
|
|
|
+ pango_font_metrics_get_ascent(metrics)) / PANGO_SCALE;
|
|
|
|
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
term->cursor_row = 0;
|
|
|
|
term->cursor_col = 0;
|
|
|
|
|
2012-10-23 08:38:10 +00:00
|
|
|
term->term_width = width / term->char_width;
|
|
|
|
term->term_height = height / term->char_height;
|
2012-12-09 08:35:37 +00:00
|
|
|
term->char_handler = guac_terminal_echo;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2011-08-06 05:59:42 +00:00
|
|
|
term->scroll_start = 0;
|
|
|
|
term->scroll_end = term->term_height - 1;
|
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
term->text_selected = false;
|
|
|
|
|
2013-04-05 08:32:33 +00:00
|
|
|
/* Init scrollback buffer */
|
|
|
|
term->scrollback = guac_terminal_scrollback_buffer_alloc(1000);
|
|
|
|
term->scroll_offset = 0;
|
2011-08-09 19:31:03 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* Init delta */
|
|
|
|
term->delta = guac_terminal_delta_alloc(term->term_width,
|
2013-03-25 08:54:44 +00:00
|
|
|
term->term_height);
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-29 08:56:27 +00:00
|
|
|
/* Init buffer */
|
|
|
|
term->buffer = guac_terminal_buffer_alloc(term->term_width,
|
|
|
|
term->term_height);
|
|
|
|
|
2011-08-05 19:14:15 +00:00
|
|
|
/* Clear with background color */
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_clear(term,
|
2013-03-29 09:51:31 +00:00
|
|
|
0, 0, term->term_height, term->term_width);
|
2011-08-05 19:14:15 +00:00
|
|
|
|
2013-04-07 23:55:06 +00:00
|
|
|
/* Init terminal lock */
|
|
|
|
pthread_mutex_init(&(term->lock), 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) {
|
2011-12-30 22:34:04 +00:00
|
|
|
|
|
|
|
/* Free scrollback buffer */
|
2013-04-05 08:32:33 +00:00
|
|
|
guac_terminal_scrollback_buffer_free(term->scrollback);
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* Free delta */
|
|
|
|
guac_terminal_delta_free(term->delta);
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-29 08:56:27 +00:00
|
|
|
/* Free buffer */
|
|
|
|
guac_terminal_buffer_free(term->buffer);
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the location of the given character in the glyph cache layer,
|
|
|
|
* sending it first if necessary. The location returned is in characters,
|
|
|
|
* and thus must be multiplied by the glyph width to obtain the actual
|
|
|
|
* location within the glyph cache layer.
|
|
|
|
*/
|
2012-12-09 08:35:37 +00:00
|
|
|
int __guac_terminal_get_glyph(guac_terminal* term, char c) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_socket* socket = term->client->socket;
|
2011-08-19 01:09:20 +00:00
|
|
|
int location;
|
2011-08-05 19:14:15 +00:00
|
|
|
|
2011-08-19 01:09:20 +00:00
|
|
|
/* Use foreground color */
|
2012-12-09 08:35:37 +00:00
|
|
|
const guac_terminal_color* color =
|
|
|
|
&guac_terminal_palette[term->glyph_foreground];
|
2011-08-19 01:09:20 +00:00
|
|
|
|
|
|
|
/* Use background color */
|
2012-12-09 08:35:37 +00:00
|
|
|
const guac_terminal_color* background =
|
|
|
|
&guac_terminal_palette[term->glyph_background];
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
cairo_surface_t* surface;
|
|
|
|
cairo_t* cairo;
|
|
|
|
|
|
|
|
PangoLayout* layout;
|
|
|
|
|
|
|
|
/* Return glyph if exists */
|
|
|
|
if (term->glyphs[(int) c])
|
2011-08-19 01:09:20 +00:00
|
|
|
return term->glyphs[(int) c] - 1;
|
|
|
|
|
|
|
|
location = term->next_glyph++;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
/* Otherwise, draw glyph */
|
|
|
|
surface = cairo_image_surface_create(
|
|
|
|
CAIRO_FORMAT_ARGB32,
|
|
|
|
term->char_width, term->char_height);
|
|
|
|
cairo = cairo_create(surface);
|
|
|
|
|
|
|
|
/* Get layout */
|
|
|
|
layout = pango_cairo_create_layout(cairo);
|
|
|
|
pango_layout_set_font_description(layout, term->font_desc);
|
|
|
|
pango_layout_set_text(layout, &c, 1);
|
|
|
|
|
|
|
|
/* Draw */
|
2011-08-05 19:14:15 +00:00
|
|
|
cairo_set_source_rgba(cairo,
|
|
|
|
color->red / 255.0,
|
|
|
|
color->green / 255.0,
|
|
|
|
color->blue / 255.0,
|
|
|
|
1.0 /* alpha */ );
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
cairo_move_to(cairo, 0.0, 0.0);
|
|
|
|
pango_cairo_show_layout(cairo, layout);
|
|
|
|
|
|
|
|
/* Free all */
|
|
|
|
g_object_unref(layout);
|
|
|
|
cairo_destroy(cairo);
|
|
|
|
|
2011-08-19 01:09:20 +00:00
|
|
|
/* Send glyph and update filled flyphs */
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_protocol_send_png(socket, GUAC_COMP_OVER, term->glyph_stroke, location * term->char_width, 0, surface);
|
2011-08-19 01:09:20 +00:00
|
|
|
|
2012-03-12 04:46:43 +00:00
|
|
|
guac_protocol_send_rect(socket, term->filled_glyphs,
|
2011-08-19 01:09:20 +00:00
|
|
|
location * term->char_width, 0,
|
2012-03-12 04:46:43 +00:00
|
|
|
term->char_width, term->char_height);
|
|
|
|
|
|
|
|
guac_protocol_send_cfill(socket, GUAC_COMP_OVER, term->filled_glyphs,
|
2011-08-19 01:09:20 +00:00
|
|
|
background->red,
|
|
|
|
background->green,
|
|
|
|
background->blue,
|
|
|
|
0xFF);
|
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_protocol_send_copy(socket, term->glyph_stroke,
|
2011-08-19 01:09:20 +00:00
|
|
|
location * term->char_width, 0, term->char_width, term->char_height,
|
|
|
|
GUAC_COMP_OVER, term->filled_glyphs, location * term->char_width, 0);
|
|
|
|
|
|
|
|
term->glyphs[(int) c] = location+1;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
|
|
|
/* Return glyph */
|
2011-08-19 01:09:20 +00:00
|
|
|
return location;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
/**
|
|
|
|
* Sets the attributes of the glyph cache layer such that future copies from
|
|
|
|
* this layer will display as expected.
|
|
|
|
*/
|
|
|
|
int __guac_terminal_set_colors(guac_terminal* term,
|
|
|
|
guac_terminal_attributes* attributes) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_socket* socket = term->client->socket;
|
2012-12-09 08:35:37 +00:00
|
|
|
const guac_terminal_color* background_color;
|
2013-03-24 23:56:17 +00:00
|
|
|
int background, foreground;
|
|
|
|
|
|
|
|
/* Handle reverse video */
|
2013-04-15 08:22:05 +00:00
|
|
|
if (attributes->reverse != attributes->selected) {
|
2013-03-24 23:56:17 +00:00
|
|
|
background = attributes->foreground;
|
|
|
|
foreground = attributes->background;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreground = attributes->foreground;
|
|
|
|
background = attributes->background;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle bold */
|
|
|
|
if (attributes->bold && foreground <= 7)
|
|
|
|
foreground += 8;
|
2011-08-05 20:49:47 +00:00
|
|
|
|
2011-08-05 19:41:21 +00:00
|
|
|
/* Get background color */
|
2012-12-09 08:35:37 +00:00
|
|
|
background_color = &guac_terminal_palette[background];
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2011-08-19 01:09:20 +00:00
|
|
|
/* If foreground different from current, colorize */
|
|
|
|
if (foreground != term->glyph_foreground) {
|
2011-08-05 19:41:21 +00:00
|
|
|
|
|
|
|
/* Get color */
|
2012-12-09 08:35:37 +00:00
|
|
|
const guac_terminal_color* color =
|
|
|
|
&guac_terminal_palette[foreground];
|
2011-08-05 19:41:21 +00:00
|
|
|
|
|
|
|
/* Colorize letter */
|
2012-03-12 04:46:43 +00:00
|
|
|
guac_protocol_send_rect(socket, term->glyph_stroke,
|
2011-08-19 01:09:20 +00:00
|
|
|
0, 0,
|
2012-03-12 04:46:43 +00:00
|
|
|
term->char_width * term->next_glyph, term->char_height);
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2012-03-12 04:46:43 +00:00
|
|
|
guac_protocol_send_cfill(socket, GUAC_COMP_ATOP, term->glyph_stroke,
|
2011-08-05 19:41:21 +00:00
|
|
|
color->red,
|
|
|
|
color->green,
|
|
|
|
color->blue,
|
|
|
|
255);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-19 01:09:20 +00:00
|
|
|
/* If any color change at all, update filled */
|
2013-03-24 23:56:17 +00:00
|
|
|
if (foreground != term->glyph_foreground
|
|
|
|
|| background != term->glyph_background) {
|
2011-08-19 01:09:20 +00:00
|
|
|
|
|
|
|
/* Set background */
|
2012-03-12 04:46:43 +00:00
|
|
|
guac_protocol_send_rect(socket, term->filled_glyphs,
|
2011-08-19 01:09:20 +00:00
|
|
|
0, 0,
|
2012-03-12 04:46:43 +00:00
|
|
|
term->char_width * term->next_glyph, term->char_height);
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2012-03-12 04:46:43 +00:00
|
|
|
guac_protocol_send_cfill(socket, GUAC_COMP_OVER, term->filled_glyphs,
|
2011-08-19 01:09:20 +00:00
|
|
|
background_color->red,
|
|
|
|
background_color->green,
|
|
|
|
background_color->blue,
|
|
|
|
255);
|
2011-08-05 19:41:21 +00:00
|
|
|
|
2011-08-19 01:09:20 +00:00
|
|
|
/* Copy stroke */
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_protocol_send_copy(socket, term->glyph_stroke,
|
2011-08-19 01:09:20 +00:00
|
|
|
|
|
|
|
0, 0,
|
|
|
|
term->char_width * term->next_glyph, term->char_height,
|
|
|
|
|
|
|
|
GUAC_COMP_OVER, term->filled_glyphs,
|
|
|
|
0, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
term->glyph_foreground = foreground;
|
|
|
|
term->glyph_background = background;
|
2011-08-05 19:41:21 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
/**
|
|
|
|
* Sends the given character to the terminal at the given row and column,
|
2013-03-27 19:44:40 +00:00
|
|
|
* rendering the charater immediately. This bypasses the guac_terminal_delta
|
2013-03-24 23:56:17 +00:00
|
|
|
* mechanism and is intended for flushing of updates only.
|
|
|
|
*/
|
|
|
|
int __guac_terminal_set(guac_terminal* term, int row, int col, char c) {
|
2011-08-19 01:09:20 +00:00
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_socket* socket = term->client->socket;
|
2012-12-09 08:35:37 +00:00
|
|
|
int location = __guac_terminal_get_glyph(term, c);
|
2011-08-19 01:09:20 +00:00
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
return guac_protocol_send_copy(socket,
|
2011-08-19 01:09:20 +00:00
|
|
|
term->filled_glyphs,
|
|
|
|
location * term->char_width, 0, term->char_width, term->char_height,
|
2011-09-06 07:01:37 +00:00
|
|
|
GUAC_COMP_OVER, GUAC_DEFAULT_LAYER,
|
2011-08-19 01:09:20 +00:00
|
|
|
term->char_width * col,
|
|
|
|
term->char_height * row);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
int guac_terminal_set(guac_terminal* term, int row, int col, char c) {
|
|
|
|
|
2013-04-05 20:12:18 +00:00
|
|
|
int scrolled_row = row + term->scroll_offset;
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
/* Build character with current attributes */
|
|
|
|
guac_terminal_char guac_char;
|
|
|
|
guac_char.value = c;
|
|
|
|
guac_char.attributes = term->current_attributes;
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* Set delta */
|
2013-04-05 20:12:18 +00:00
|
|
|
if (scrolled_row < term->delta->height)
|
|
|
|
guac_terminal_delta_set(term->delta, scrolled_row, col, &guac_char);
|
2013-03-29 08:56:27 +00:00
|
|
|
|
|
|
|
/* Set buffer */
|
|
|
|
guac_terminal_buffer_set(term->buffer, row, col, &guac_char);
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-29 09:51:31 +00:00
|
|
|
int guac_terminal_toggle_reverse(guac_terminal* term, int row, int col) {
|
|
|
|
|
2013-04-05 20:12:18 +00:00
|
|
|
int scrolled_row = row + term->scroll_offset;
|
|
|
|
|
2013-03-29 09:51:31 +00:00
|
|
|
/* Get character from buffer */
|
|
|
|
guac_terminal_char* guac_char =
|
|
|
|
&(term->buffer->characters[row*term->buffer->width + col]);
|
|
|
|
|
|
|
|
/* Toggle reverse */
|
|
|
|
guac_char->attributes.reverse = !(guac_char->attributes.reverse);
|
|
|
|
|
|
|
|
/* Set delta */
|
2013-04-05 20:12:18 +00:00
|
|
|
if (scrolled_row < term->delta->height)
|
|
|
|
guac_terminal_delta_set(term->delta, scrolled_row, col, guac_char);
|
2013-03-29 09:51:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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) {
|
2011-08-05 02:17:44 +00:00
|
|
|
term->char_handler(term, *(c++));
|
2011-08-04 18:46:21 +00:00
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
int guac_terminal_copy(guac_terminal* term,
|
2011-08-05 07:20:09 +00:00
|
|
|
int src_row, int src_col, int rows, int cols,
|
|
|
|
int dst_row, int dst_col) {
|
|
|
|
|
2013-04-05 20:12:18 +00:00
|
|
|
int scrolled_src_row = src_row + term->scroll_offset;
|
|
|
|
int scrolled_dst_row = dst_row + term->scroll_offset;
|
|
|
|
|
|
|
|
int scrolled_rows = rows;
|
|
|
|
|
|
|
|
/* FIXME: If source (but not dest) is partially scrolled out of view, then
|
|
|
|
* the delta will not be updated properly. We need to pull the data
|
|
|
|
* from the buffer in such a case.
|
|
|
|
*/
|
|
|
|
|
2013-04-08 07:14:25 +00:00
|
|
|
if (scrolled_src_row < term->delta->height &&
|
|
|
|
scrolled_dst_row < term->delta->height) {
|
|
|
|
|
|
|
|
/* Adjust delta rect height if scrolled out of view */
|
|
|
|
if (scrolled_src_row + scrolled_rows > term->delta->height)
|
|
|
|
scrolled_rows = term->delta->height - scrolled_src_row;
|
|
|
|
|
|
|
|
if (scrolled_dst_row + scrolled_rows > term->delta->height)
|
|
|
|
scrolled_rows = term->delta->height - scrolled_dst_row;
|
|
|
|
|
|
|
|
/* Update delta */
|
|
|
|
guac_terminal_delta_copy(term->delta,
|
|
|
|
scrolled_dst_row, dst_col,
|
|
|
|
scrolled_src_row, src_col,
|
|
|
|
cols, rows);
|
|
|
|
|
|
|
|
}
|
2011-08-05 07:20:09 +00:00
|
|
|
|
2013-03-29 08:56:27 +00:00
|
|
|
/* Update buffer */
|
|
|
|
guac_terminal_buffer_copy(term->buffer,
|
|
|
|
dst_row, dst_col,
|
|
|
|
src_row, src_col,
|
|
|
|
cols, rows);
|
|
|
|
|
2013-03-24 23:56:17 +00:00
|
|
|
return 0;
|
2011-08-05 07:20:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
int guac_terminal_clear(guac_terminal* term,
|
2013-03-29 09:51:31 +00:00
|
|
|
int row, int col, int rows, int cols) {
|
2011-08-05 07:20:09 +00:00
|
|
|
|
2013-04-05 20:12:18 +00:00
|
|
|
int scrolled_row = row + term->scroll_offset;
|
|
|
|
int scrolled_rows = rows;
|
|
|
|
|
2013-03-25 01:05:15 +00:00
|
|
|
/* Build space */
|
|
|
|
guac_terminal_char character;
|
|
|
|
character.value = ' ';
|
2013-03-29 09:51:31 +00:00
|
|
|
character.attributes = term->current_attributes;
|
2011-08-05 07:20:09 +00:00
|
|
|
|
2013-04-08 07:14:25 +00:00
|
|
|
if (scrolled_row < term->delta->height) {
|
|
|
|
|
|
|
|
/* Adjust delta rect height if scrolled out of view */
|
|
|
|
if (scrolled_row + scrolled_rows > term->delta->height)
|
|
|
|
scrolled_rows = term->delta->height - scrolled_row;
|
|
|
|
|
|
|
|
/* Fill with color */
|
|
|
|
guac_terminal_delta_set_rect(term->delta,
|
|
|
|
scrolled_row, col, cols, scrolled_rows, &character);
|
|
|
|
|
|
|
|
}
|
2011-08-05 07:20:09 +00:00
|
|
|
|
2013-03-29 08:56:27 +00:00
|
|
|
guac_terminal_buffer_set_rect(term->buffer,
|
|
|
|
row, col, cols, rows, &character);
|
|
|
|
|
2013-03-25 01:05:15 +00:00
|
|
|
return 0;
|
2011-08-05 07:20:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
/* Calculate height of scroll region */
|
|
|
|
int height = end_row - start_row + 1;
|
2011-08-05 19:14:15 +00:00
|
|
|
|
2013-04-01 08:10:47 +00:00
|
|
|
/* If scroll region is entire screen, push rows into scrollback */
|
2013-04-05 08:32:33 +00:00
|
|
|
if (start_row == 0 && end_row == term->term_height-1)
|
|
|
|
guac_terminal_scrollback_buffer_append(term->scrollback, term, amount);
|
2013-04-01 08:10:47 +00:00
|
|
|
|
2011-08-05 07:20:09 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
/* Move rows within scroll region up by the given amount */
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_copy(term,
|
2011-08-05 07:20:09 +00:00
|
|
|
start_row + amount, 0,
|
|
|
|
height - amount, term->term_width,
|
|
|
|
start_row, 0)
|
|
|
|
|
|
|
|
/* Fill new rows with background */
|
2012-12-09 08:35:37 +00:00
|
|
|
|| guac_terminal_clear(term,
|
2013-03-29 09:51:31 +00:00
|
|
|
end_row - amount + 1, 0, amount, term->term_width);
|
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) {
|
|
|
|
|
|
|
|
/* Calculate height of scroll region */
|
|
|
|
int height = end_row - start_row + 1;
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
/* Move rows within scroll region down by the given amount */
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_copy(term,
|
2011-08-10 01:32:54 +00:00
|
|
|
start_row, 0,
|
|
|
|
height - amount, term->term_width,
|
|
|
|
start_row + amount, 0)
|
|
|
|
|
|
|
|
/* Fill new rows with background */
|
2012-12-09 08:35:37 +00:00
|
|
|
|| guac_terminal_clear(term,
|
2013-03-29 09:51:31 +00:00
|
|
|
start_row, 0, amount, term->term_width);
|
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 */
|
2012-12-09 08:35:37 +00:00
|
|
|
if (guac_terminal_clear(term,
|
2013-03-29 09:51:31 +00:00
|
|
|
start_row, start_col, 1, term->term_width - start_col))
|
2011-08-05 07:20:09 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* 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 */
|
2012-12-09 08:35:37 +00:00
|
|
|
if (guac_terminal_clear(term,
|
2013-03-29 09:51:31 +00:00
|
|
|
end_row, 0, 1, end_col + 1))
|
2011-08-05 07:20:09 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* One less row to clear */
|
|
|
|
end_row--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remaining region now guaranteed rectangular. Clear, if possible */
|
|
|
|
if (start_row <= end_row) {
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
if (guac_terminal_clear(term,
|
2013-03-29 09:51:31 +00:00
|
|
|
start_row, 0, end_row - start_row + 1, term->term_width))
|
2011-08-05 07:20:09 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_delta* guac_terminal_delta_alloc(int width, int height) {
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current;
|
2013-03-24 00:43:35 +00:00
|
|
|
int x, y;
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* Allocate delta */
|
|
|
|
guac_terminal_delta* delta = malloc(sizeof(guac_terminal_delta));
|
2013-03-24 00:43:35 +00:00
|
|
|
|
|
|
|
/* Set width and height */
|
2013-03-27 19:44:40 +00:00
|
|
|
delta->width = width;
|
|
|
|
delta->height = height;
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* Alloc operations */
|
|
|
|
delta->operations = malloc(width * height *
|
|
|
|
sizeof(guac_terminal_operation));
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* Init each operation buffer row */
|
|
|
|
current = delta->operations;
|
2013-03-24 00:43:35 +00:00
|
|
|
for (y=0; y<height; y++) {
|
|
|
|
|
|
|
|
/* Init entire row to NOP */
|
2013-03-27 19:44:40 +00:00
|
|
|
for (x=0; x<width; x++)
|
|
|
|
(current++)->type = GUAC_CHAR_NOP;
|
2013-03-24 00:43:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-05 20:15:19 +00:00
|
|
|
/* Alloc scratch area */
|
|
|
|
delta->scratch = malloc(width * height * sizeof(guac_terminal_operation));
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
return delta;
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-24 00:06:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void guac_terminal_delta_free(guac_terminal_delta* delta) {
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-04-05 20:15:19 +00:00
|
|
|
/* Free operations buffers */
|
2013-03-27 19:44:40 +00:00
|
|
|
free(delta->operations);
|
2013-04-05 20:15:19 +00:00
|
|
|
free(delta->scratch);
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* Free delta */
|
|
|
|
free(delta);
|
2013-03-24 00:43:35 +00:00
|
|
|
|
2013-03-24 00:06:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void guac_terminal_delta_resize(guac_terminal_delta* delta,
|
2013-03-24 00:06:02 +00:00
|
|
|
int width, int height) {
|
|
|
|
/* STUB */
|
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void guac_terminal_delta_set(guac_terminal_delta* delta, int r, int c,
|
2013-03-24 00:06:02 +00:00
|
|
|
guac_terminal_char* character) {
|
2013-03-25 01:16:04 +00:00
|
|
|
|
|
|
|
/* Get operation at coordinate */
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* op = &(delta->operations[r*delta->width + c]);
|
2013-03-25 01:16:04 +00:00
|
|
|
|
|
|
|
/* Store operation */
|
2013-03-27 19:44:40 +00:00
|
|
|
op->type = GUAC_CHAR_SET;
|
|
|
|
op->character = *character;
|
2013-03-25 01:16:04 +00:00
|
|
|
|
2013-03-24 00:06:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void guac_terminal_delta_copy(guac_terminal_delta* delta,
|
2013-03-24 00:06:02 +00:00
|
|
|
int dst_row, int dst_column,
|
|
|
|
int src_row, int src_column,
|
|
|
|
int w, int h) {
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
int row, column;
|
|
|
|
|
|
|
|
/* FIXME: Handle intersections between src and dst rects */
|
|
|
|
|
2013-04-05 20:15:19 +00:00
|
|
|
memcpy(delta->scratch, delta->operations,
|
2013-04-05 08:32:33 +00:00
|
|
|
sizeof(guac_terminal_operation) * delta->width * delta->height);
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current_row =
|
|
|
|
&(delta->operations[dst_row*delta->width + dst_column]);
|
2013-03-25 09:56:59 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* src_current_row =
|
2013-04-05 20:15:19 +00:00
|
|
|
&(delta->scratch[src_row*delta->width + src_column]);
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* Set rectangle to copy operations */
|
|
|
|
for (row=0; row<h; row++) {
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current = current_row;
|
|
|
|
guac_terminal_operation* src_current = src_current_row;
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
for (column=0; column<w; column++) {
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
/* If copying existing delta operation, just copy that rather
|
2013-03-25 09:56:59 +00:00
|
|
|
* than create a new copy op */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (src_current->type != GUAC_CHAR_NOP)
|
|
|
|
*current = *src_current;
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* Store operation */
|
|
|
|
else {
|
2013-03-27 19:44:40 +00:00
|
|
|
current->type = GUAC_CHAR_COPY;
|
|
|
|
current->row = src_row + row;
|
|
|
|
current->column = src_column + column;
|
2013-03-25 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Next column */
|
|
|
|
current++;
|
|
|
|
src_current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next row */
|
2013-03-27 19:44:40 +00:00
|
|
|
current_row += delta->width;
|
|
|
|
src_current_row += delta->width;
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-24 00:06:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void guac_terminal_delta_set_rect(guac_terminal_delta* delta,
|
2013-03-25 01:05:15 +00:00
|
|
|
int row, int column, int w, int h,
|
|
|
|
guac_terminal_char* character) {
|
2013-03-25 09:32:23 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current_row =
|
|
|
|
&(delta->operations[row*delta->width + column]);
|
2013-03-25 09:32:23 +00:00
|
|
|
|
|
|
|
/* Set rectangle contents to given character */
|
|
|
|
for (row=0; row<h; row++) {
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current = current_row;
|
2013-03-25 09:32:23 +00:00
|
|
|
|
|
|
|
for (column=0; column<w; column++) {
|
|
|
|
|
|
|
|
/* Store operation */
|
2013-03-27 19:44:40 +00:00
|
|
|
current->type = GUAC_CHAR_SET;
|
|
|
|
current->character = *character;
|
2013-03-25 09:32:23 +00:00
|
|
|
|
|
|
|
/* Next column */
|
|
|
|
current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next row */
|
2013-03-27 19:44:40 +00:00
|
|
|
current_row += delta->width;
|
2013-03-25 09:32:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-25 01:05:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void __guac_terminal_delta_flush_copy(guac_terminal_delta* delta,
|
2013-03-25 08:54:44 +00:00
|
|
|
guac_terminal* terminal) {
|
2013-03-25 09:56:59 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current = delta->operations;
|
2013-03-25 09:56:59 +00:00
|
|
|
int row, col;
|
|
|
|
|
|
|
|
/* For each operation */
|
2013-03-27 19:44:40 +00:00
|
|
|
for (row=0; row<delta->height; row++) {
|
|
|
|
for (col=0; col<delta->width; col++) {
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* If operation is a copy operation */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (current->type == GUAC_CHAR_COPY) {
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* The determined bounds of the rectangle of contiguous
|
|
|
|
* operations */
|
|
|
|
int detected_right = -1;
|
|
|
|
int detected_bottom = row;
|
|
|
|
|
|
|
|
/* The current row or column within a rectangle */
|
|
|
|
int rect_row, rect_col;
|
|
|
|
|
|
|
|
/* The dimensions of the rectangle as determined */
|
|
|
|
int rect_width, rect_height;
|
|
|
|
|
|
|
|
/* The expected row and column source for the next copy
|
|
|
|
* operation (if adjacent to current) */
|
|
|
|
int expected_row, expected_col;
|
|
|
|
|
|
|
|
/* Current row within a subrect */
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* rect_current_row;
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* Determine bounds of rectangle */
|
|
|
|
rect_current_row = current;
|
2013-03-27 19:44:40 +00:00
|
|
|
expected_row = current->row;
|
|
|
|
for (rect_row=row; rect_row<delta->height; rect_row++) {
|
2013-03-25 09:56:59 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* rect_current = rect_current_row;
|
|
|
|
expected_col = current->column;
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* Find width */
|
2013-03-27 19:44:40 +00:00
|
|
|
for (rect_col=col; rect_col<delta->width; rect_col++) {
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* If not identical operation, stop */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (rect_current->type != GUAC_CHAR_COPY
|
|
|
|
|| rect_current->row != expected_row
|
|
|
|
|| rect_current->column != expected_col)
|
2013-03-25 09:56:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Next column */
|
|
|
|
rect_current++;
|
|
|
|
expected_col++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If too small, cannot append row */
|
|
|
|
if (rect_col-1 < detected_right)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* As row has been accepted, update rect_row of rect */
|
|
|
|
detected_bottom = rect_row;
|
|
|
|
|
|
|
|
/* For now, only set rect_col bound if uninitialized */
|
|
|
|
if (detected_right == -1)
|
|
|
|
detected_right = rect_col - 1;
|
|
|
|
|
|
|
|
/* Next row */
|
2013-03-27 19:44:40 +00:00
|
|
|
rect_current_row += delta->width;
|
2013-03-25 09:56:59 +00:00
|
|
|
expected_row++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate dimensions */
|
|
|
|
rect_width = detected_right - col + 1;
|
|
|
|
rect_height = detected_bottom - row + 1;
|
|
|
|
|
|
|
|
/* Mark rect as NOP (as it has been handled) */
|
|
|
|
rect_current_row = current;
|
2013-03-27 19:44:40 +00:00
|
|
|
expected_row = current->row;
|
2013-03-25 09:56:59 +00:00
|
|
|
for (rect_row=0; rect_row<rect_height; rect_row++) {
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* rect_current = rect_current_row;
|
|
|
|
expected_col = current->column;
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
for (rect_col=0; rect_col<rect_width; rect_col++) {
|
|
|
|
|
|
|
|
/* Mark copy operations as NOP */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (rect_current->type == GUAC_CHAR_COPY
|
|
|
|
&& rect_current->row == expected_row
|
|
|
|
&& rect_current->column == expected_col)
|
|
|
|
rect_current->type = GUAC_CHAR_NOP;
|
2013-03-25 09:56:59 +00:00
|
|
|
|
|
|
|
/* Next column */
|
|
|
|
rect_current++;
|
|
|
|
expected_col++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next row */
|
2013-03-27 19:44:40 +00:00
|
|
|
rect_current_row += delta->width;
|
2013-03-25 09:56:59 +00:00
|
|
|
expected_row++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send copy */
|
|
|
|
guac_protocol_send_copy(terminal->client->socket,
|
|
|
|
|
|
|
|
GUAC_DEFAULT_LAYER,
|
2013-03-27 19:44:40 +00:00
|
|
|
current->column * terminal->char_width,
|
|
|
|
current->row * terminal->char_height,
|
2013-03-25 09:56:59 +00:00
|
|
|
rect_width * terminal->char_width,
|
|
|
|
rect_height * terminal->char_height,
|
|
|
|
|
|
|
|
GUAC_COMP_OVER,
|
|
|
|
GUAC_DEFAULT_LAYER,
|
|
|
|
col * terminal->char_width,
|
|
|
|
row * terminal->char_height);
|
|
|
|
|
|
|
|
|
|
|
|
} /* end if copy operation */
|
|
|
|
|
|
|
|
/* Next operation */
|
|
|
|
current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 08:54:44 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void __guac_terminal_delta_flush_clear(guac_terminal_delta* delta,
|
2013-03-25 08:54:44 +00:00
|
|
|
guac_terminal* terminal) {
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current = delta->operations;
|
2013-03-25 08:54:44 +00:00
|
|
|
int row, col;
|
|
|
|
|
|
|
|
/* For each operation */
|
2013-03-27 19:44:40 +00:00
|
|
|
for (row=0; row<delta->height; row++) {
|
|
|
|
for (col=0; col<delta->width; col++) {
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* If operation is a cler operation (set to space) */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (current->type == GUAC_CHAR_SET &&
|
|
|
|
current->character.value == ' ') {
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* The determined bounds of the rectangle of contiguous
|
|
|
|
* operations */
|
|
|
|
int detected_right = -1;
|
|
|
|
int detected_bottom = row;
|
|
|
|
|
|
|
|
/* The current row or column within a rectangle */
|
|
|
|
int rect_row, rect_col;
|
|
|
|
|
|
|
|
/* The dimensions of the rectangle as determined */
|
|
|
|
int rect_width, rect_height;
|
|
|
|
|
|
|
|
/* Color of the rectangle to draw */
|
2013-03-25 09:32:23 +00:00
|
|
|
int color;
|
2013-04-15 08:22:05 +00:00
|
|
|
if (current->character.attributes.reverse != current->character.attributes.selected)
|
2013-03-27 19:44:40 +00:00
|
|
|
color = current->character.attributes.foreground;
|
2013-03-25 09:32:23 +00:00
|
|
|
else
|
2013-03-27 19:44:40 +00:00
|
|
|
color = current->character.attributes.background;
|
2013-03-25 09:32:23 +00:00
|
|
|
|
2013-03-25 08:54:44 +00:00
|
|
|
const guac_terminal_color* guac_color =
|
|
|
|
&guac_terminal_palette[color];
|
|
|
|
|
|
|
|
/* Current row within a subrect */
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* rect_current_row;
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* Determine bounds of rectangle */
|
|
|
|
rect_current_row = current;
|
2013-03-27 19:44:40 +00:00
|
|
|
for (rect_row=row; rect_row<delta->height; rect_row++) {
|
2013-03-25 08:54:44 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* rect_current = rect_current_row;
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* Find width */
|
2013-03-27 19:44:40 +00:00
|
|
|
for (rect_col=col; rect_col<delta->width; rect_col++) {
|
2013-03-25 08:54:44 +00:00
|
|
|
|
2013-03-25 09:32:23 +00:00
|
|
|
int joining_color;
|
2013-03-27 19:44:40 +00:00
|
|
|
if (rect_current->character.attributes.reverse)
|
|
|
|
joining_color = current->character.attributes.foreground;
|
2013-03-25 09:32:23 +00:00
|
|
|
else
|
2013-03-27 19:44:40 +00:00
|
|
|
joining_color = current->character.attributes.background;
|
2013-03-25 09:32:23 +00:00
|
|
|
|
2013-03-25 08:54:44 +00:00
|
|
|
/* If not identical operation, stop */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (rect_current->type != GUAC_CHAR_SET
|
|
|
|
|| rect_current->character.value != ' '
|
2013-03-25 09:32:23 +00:00
|
|
|
|| joining_color != color)
|
2013-03-25 08:54:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Next column */
|
|
|
|
rect_current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If too small, cannot append row */
|
|
|
|
if (rect_col-1 < detected_right)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* As row has been accepted, update rect_row of rect */
|
|
|
|
detected_bottom = rect_row;
|
|
|
|
|
|
|
|
/* For now, only set rect_col bound if uninitialized */
|
|
|
|
if (detected_right == -1)
|
|
|
|
detected_right = rect_col - 1;
|
|
|
|
|
|
|
|
/* Next row */
|
2013-03-27 19:44:40 +00:00
|
|
|
rect_current_row += delta->width;
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate dimensions */
|
|
|
|
rect_width = detected_right - col + 1;
|
|
|
|
rect_height = detected_bottom - row + 1;
|
|
|
|
|
|
|
|
/* Mark rect as NOP (as it has been handled) */
|
|
|
|
rect_current_row = current;
|
|
|
|
for (rect_row=0; rect_row<rect_height; rect_row++) {
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* rect_current = rect_current_row;
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
for (rect_col=0; rect_col<rect_width; rect_col++) {
|
|
|
|
|
2013-03-25 09:32:23 +00:00
|
|
|
int joining_color;
|
2013-03-27 19:44:40 +00:00
|
|
|
if (rect_current->character.attributes.reverse)
|
|
|
|
joining_color = current->character.attributes.foreground;
|
2013-03-25 09:32:23 +00:00
|
|
|
else
|
2013-03-27 19:44:40 +00:00
|
|
|
joining_color = current->character.attributes.background;
|
2013-03-25 09:32:23 +00:00
|
|
|
|
2013-03-25 08:54:44 +00:00
|
|
|
/* Mark clear operations as NOP */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (rect_current->type == GUAC_CHAR_SET
|
|
|
|
&& rect_current->character.value == ' '
|
2013-03-25 09:32:23 +00:00
|
|
|
&& joining_color == color)
|
2013-03-27 19:44:40 +00:00
|
|
|
rect_current->type = GUAC_CHAR_NOP;
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* Next column */
|
|
|
|
rect_current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next row */
|
2013-03-27 19:44:40 +00:00
|
|
|
rect_current_row += delta->width;
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send rect */
|
|
|
|
guac_protocol_send_rect(terminal->client->socket,
|
|
|
|
GUAC_DEFAULT_LAYER,
|
|
|
|
col * terminal->char_width,
|
|
|
|
row * terminal->char_height,
|
|
|
|
rect_width * terminal->char_width,
|
|
|
|
rect_height * terminal->char_height);
|
|
|
|
|
|
|
|
guac_protocol_send_cfill(terminal->client->socket,
|
|
|
|
GUAC_COMP_OVER,
|
|
|
|
GUAC_DEFAULT_LAYER,
|
|
|
|
guac_color->red, guac_color->green, guac_color->blue,
|
|
|
|
0xFF);
|
|
|
|
|
2013-03-25 09:32:23 +00:00
|
|
|
} /* end if clear operation */
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* Next operation */
|
|
|
|
current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void __guac_terminal_delta_flush_set(guac_terminal_delta* delta,
|
2013-03-24 00:06:02 +00:00
|
|
|
guac_terminal* terminal) {
|
2013-03-25 01:46:47 +00:00
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
guac_terminal_operation* current = delta->operations;
|
2013-03-25 01:46:47 +00:00
|
|
|
int row, col;
|
|
|
|
|
|
|
|
/* For each operation */
|
2013-03-27 19:44:40 +00:00
|
|
|
for (row=0; row<delta->height; row++) {
|
|
|
|
for (col=0; col<delta->width; col++) {
|
2013-03-25 01:46:47 +00:00
|
|
|
|
|
|
|
/* Perform given operation */
|
2013-03-27 19:44:40 +00:00
|
|
|
if (current->type == GUAC_CHAR_SET) {
|
2013-03-25 01:46:47 +00:00
|
|
|
|
2013-03-25 08:54:44 +00:00
|
|
|
/* Set attributes */
|
|
|
|
__guac_terminal_set_colors(terminal,
|
2013-03-27 19:44:40 +00:00
|
|
|
&(current->character.attributes));
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* Send character */
|
2013-03-25 01:46:47 +00:00
|
|
|
__guac_terminal_set(terminal, row, col,
|
2013-03-27 19:44:40 +00:00
|
|
|
current->character.value);
|
2013-03-25 01:46:47 +00:00
|
|
|
|
|
|
|
/* Mark operation as handled */
|
2013-03-27 19:44:40 +00:00
|
|
|
current->type = GUAC_CHAR_NOP;
|
2013-03-25 01:46:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next operation */
|
|
|
|
current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 00:06:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 19:44:40 +00:00
|
|
|
void guac_terminal_delta_flush(guac_terminal_delta* delta,
|
2013-03-25 08:54:44 +00:00
|
|
|
guac_terminal* terminal) {
|
|
|
|
|
|
|
|
/* Flush copy operations first */
|
2013-03-27 19:44:40 +00:00
|
|
|
__guac_terminal_delta_flush_copy(delta, terminal);
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* Flush clear operations (as they're just rects) */
|
2013-03-27 19:44:40 +00:00
|
|
|
__guac_terminal_delta_flush_clear(delta, terminal);
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
/* Flush set operations (the only operations remaining) */
|
2013-03-27 19:44:40 +00:00
|
|
|
__guac_terminal_delta_flush_set(delta, terminal);
|
2013-03-25 08:54:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-29 08:56:27 +00:00
|
|
|
guac_terminal_buffer* guac_terminal_buffer_alloc(int width, int height) {
|
|
|
|
|
|
|
|
/* Allocate buffer */
|
|
|
|
guac_terminal_buffer* buffer = malloc(sizeof(guac_terminal_buffer));
|
|
|
|
|
|
|
|
/* Set width and height */
|
|
|
|
buffer->width = width;
|
|
|
|
buffer->height = height;
|
|
|
|
|
|
|
|
/* Alloc characters */
|
|
|
|
buffer->characters = malloc(width * height *
|
|
|
|
sizeof(guac_terminal_char));
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_buffer_resize(guac_terminal_buffer* buffer,
|
|
|
|
int width, int height) {
|
|
|
|
/* STUB */
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_buffer_free(guac_terminal_buffer* buffer) {
|
|
|
|
|
|
|
|
/* Free characters */
|
|
|
|
free(buffer->characters);
|
|
|
|
|
|
|
|
/* Free buffer*/
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_buffer_set(guac_terminal_buffer* buffer, int r, int c,
|
|
|
|
guac_terminal_char* character) {
|
|
|
|
|
|
|
|
/* Store character */
|
|
|
|
buffer->characters[r * buffer->width + c] = *character;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_buffer_copy(guac_terminal_buffer* buffer,
|
|
|
|
int dst_row, int dst_column,
|
|
|
|
int src_row, int src_column,
|
|
|
|
int w, int h) {
|
2013-04-01 08:10:47 +00:00
|
|
|
|
|
|
|
int row, column;
|
|
|
|
|
|
|
|
/* FIXME: Handle intersections between src and dst rects */
|
|
|
|
|
|
|
|
guac_terminal_char* current_row =
|
|
|
|
&(buffer->characters[dst_row*buffer->width + dst_column]);
|
|
|
|
|
|
|
|
guac_terminal_char* src_current_row =
|
|
|
|
&(buffer->characters[src_row*buffer->width + src_column]);
|
|
|
|
|
|
|
|
/* Set rectangle to copy operations */
|
|
|
|
for (row=0; row<h; row++) {
|
|
|
|
|
|
|
|
guac_terminal_char* current = current_row;
|
|
|
|
guac_terminal_char* src_current = src_current_row;
|
|
|
|
|
|
|
|
for (column=0; column<w; column++) {
|
|
|
|
|
|
|
|
*current = *src_current;
|
|
|
|
|
|
|
|
/* Next column */
|
|
|
|
current++;
|
|
|
|
src_current++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next row */
|
|
|
|
current_row += buffer->width;
|
|
|
|
src_current_row += buffer->width;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-29 08:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_buffer_set_rect(guac_terminal_buffer* buffer,
|
|
|
|
int row, int column, int w, int h,
|
|
|
|
guac_terminal_char* character) {
|
|
|
|
|
|
|
|
guac_terminal_char* current_row =
|
|
|
|
&(buffer->characters[row*buffer->width + column]);
|
|
|
|
|
|
|
|
/* Set rectangle contents to given character */
|
|
|
|
for (row=0; row<h; row++) {
|
|
|
|
|
|
|
|
/* Copy character throughout row */
|
|
|
|
guac_terminal_char* current = current_row;
|
|
|
|
for (column=0; column<w; column++)
|
|
|
|
*(current++) = *character;
|
|
|
|
|
|
|
|
/* Next row */
|
|
|
|
current_row += buffer->width;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-05 08:32:33 +00:00
|
|
|
guac_terminal_scrollback_buffer*
|
|
|
|
guac_terminal_scrollback_buffer_alloc(int rows) {
|
|
|
|
|
|
|
|
/* Allocate scrollback */
|
|
|
|
guac_terminal_scrollback_buffer* buffer =
|
|
|
|
malloc(sizeof(guac_terminal_scrollback_buffer));
|
|
|
|
|
|
|
|
int i;
|
|
|
|
guac_terminal_scrollback_row* row;
|
|
|
|
|
|
|
|
/* Init scrollback data */
|
|
|
|
buffer->rows = rows;
|
|
|
|
buffer->top = 0;
|
|
|
|
buffer->length = 0;
|
|
|
|
buffer->scrollback = malloc(sizeof(guac_terminal_scrollback_row) *
|
|
|
|
buffer->rows);
|
|
|
|
|
|
|
|
/* Init scrollback rows */
|
|
|
|
row = buffer->scrollback;
|
|
|
|
for (i=0; i<rows; i++) {
|
|
|
|
|
|
|
|
/* Allocate row */
|
|
|
|
row->available = 256;
|
|
|
|
row->length = 0;
|
|
|
|
row->characters = malloc(sizeof(guac_terminal_char) * row->available);
|
|
|
|
|
|
|
|
/* Next row */
|
|
|
|
row++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_scrollback_buffer_free(
|
|
|
|
guac_terminal_scrollback_buffer* buffer) {
|
|
|
|
|
|
|
|
int i;
|
|
|
|
guac_terminal_scrollback_row* row = buffer->scrollback;
|
|
|
|
|
|
|
|
/* Free all rows */
|
|
|
|
for (i=0; i<buffer->rows; i++) {
|
|
|
|
free(row->characters);
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free actual buffer */
|
|
|
|
free(buffer->scrollback);
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_scrollback_buffer_append(
|
|
|
|
guac_terminal_scrollback_buffer* buffer,
|
|
|
|
guac_terminal* terminal, int rows) {
|
|
|
|
|
|
|
|
int row, column;
|
|
|
|
|
|
|
|
/* Copy data into scrollback */
|
|
|
|
guac_terminal_scrollback_row* scrollback_row =
|
|
|
|
&(buffer->scrollback[buffer->top]);
|
|
|
|
guac_terminal_char* current = terminal->buffer->characters;
|
|
|
|
|
|
|
|
for (row=0; row<rows; row++) {
|
|
|
|
|
|
|
|
/* FIXME: Assumes scrollback row large enough */
|
|
|
|
|
|
|
|
/* Copy character data for row */
|
|
|
|
guac_terminal_char* dest = scrollback_row->characters;
|
|
|
|
for (column=0; column < terminal->buffer->width; column++)
|
|
|
|
*(dest++) = *(current++);
|
|
|
|
|
|
|
|
scrollback_row->length = terminal->buffer->width;
|
|
|
|
|
|
|
|
/* Next scrollback row */
|
|
|
|
scrollback_row++;
|
|
|
|
buffer->top++;
|
|
|
|
|
|
|
|
/* Wrap around when bottom reached */
|
|
|
|
if (buffer->top == buffer->rows) {
|
|
|
|
buffer->top = 0;
|
|
|
|
scrollback_row = buffer->scrollback;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* end for each row */
|
|
|
|
|
|
|
|
/* Increment row count */
|
|
|
|
buffer->length += rows;
|
|
|
|
if (buffer->length > buffer->rows)
|
|
|
|
buffer->length = buffer->rows;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
guac_terminal_char* guac_terminal_get_row(guac_terminal* terminal, int row, int* length) {
|
|
|
|
|
|
|
|
/* If row in past, pull from scrollback */
|
|
|
|
if (row < 0) {
|
|
|
|
guac_terminal_scrollback_row* scrollback_row =
|
|
|
|
guac_terminal_scrollback_buffer_get_row(terminal->scrollback, row);
|
|
|
|
|
|
|
|
*length = scrollback_row->length;
|
|
|
|
return scrollback_row->characters;
|
|
|
|
}
|
|
|
|
|
|
|
|
*length = terminal->buffer->width;
|
|
|
|
return &(terminal->buffer->characters[terminal->buffer->width * row]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
if (scroll_amount == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Shift screen up */
|
|
|
|
if (terminal->term_height > scroll_amount)
|
|
|
|
guac_terminal_delta_copy(terminal->delta,
|
|
|
|
0, 0, /* Destination row, col */
|
|
|
|
scroll_amount, 0, /* source row,col */
|
|
|
|
terminal->term_width, terminal->term_height - scroll_amount);
|
|
|
|
|
|
|
|
/* Advance by scroll amount */
|
|
|
|
terminal->scroll_offset -= scroll_amount;
|
|
|
|
|
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-15 08:22:05 +00:00
|
|
|
int length;
|
|
|
|
guac_terminal_char* current = guac_terminal_get_row(terminal, row, &length);
|
2013-04-05 19:54:59 +00:00
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
for (column=0; column<length; column++)
|
|
|
|
guac_terminal_delta_set(terminal->delta, dest_row, column,
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Should flush somewhere more sensible */
|
|
|
|
guac_terminal_delta_flush(terminal->delta, terminal);
|
|
|
|
guac_socket_flush(terminal->client->socket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
if (terminal->scroll_offset + scroll_amount > terminal->scrollback->length)
|
|
|
|
scroll_amount = terminal->scrollback->length - terminal->scroll_offset;
|
|
|
|
|
|
|
|
/* If not scrolling at all, don't bother trying */
|
|
|
|
if (scroll_amount == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Shift screen down */
|
|
|
|
if (terminal->term_height > scroll_amount)
|
|
|
|
guac_terminal_delta_copy(terminal->delta,
|
|
|
|
scroll_amount, 0, /* Destination row,col */
|
|
|
|
0, 0, /* Source row, col */
|
|
|
|
terminal->term_width, terminal->term_height - scroll_amount);
|
|
|
|
|
|
|
|
/* Advance by scroll amount */
|
|
|
|
terminal->scroll_offset += scroll_amount;
|
|
|
|
|
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 */
|
|
|
|
guac_terminal_scrollback_row* scrollback_row =
|
|
|
|
guac_terminal_scrollback_buffer_get_row(terminal->scrollback, row);
|
2013-04-05 08:32:33 +00:00
|
|
|
|
|
|
|
/* Draw row */
|
2013-04-05 18:31:46 +00:00
|
|
|
/* FIXME: Clear row first */
|
2013-04-05 08:32:33 +00:00
|
|
|
guac_terminal_char* current = scrollback_row->characters;
|
|
|
|
for (column=0; column<scrollback_row->length; column++)
|
2013-04-05 18:31:46 +00:00
|
|
|
guac_terminal_delta_set(terminal->delta, dest_row, column,
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Should flush somewhere more sensible */
|
|
|
|
guac_terminal_delta_flush(terminal->delta, terminal);
|
|
|
|
guac_socket_flush(terminal->client->socket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-05 18:31:46 +00:00
|
|
|
guac_terminal_scrollback_row* guac_terminal_scrollback_buffer_get_row(
|
|
|
|
guac_terminal_scrollback_buffer* buffer, int row) {
|
|
|
|
|
|
|
|
/* Calculate scrollback row index */
|
|
|
|
int index = buffer->top + row;
|
|
|
|
if (index < 0) index += buffer->rows;
|
|
|
|
|
|
|
|
/* Return found row */
|
|
|
|
return &(buffer->scrollback[index]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-15 08:22:05 +00:00
|
|
|
void guac_terminal_select_start(guac_terminal* terminal, int row, int column) {
|
|
|
|
|
|
|
|
guac_terminal_char* guac_char;
|
|
|
|
guac_terminal_operation* guac_operation;
|
|
|
|
|
|
|
|
/* Update selection coordinates */
|
|
|
|
terminal->selection_start_row =
|
|
|
|
terminal->selection_end_row = row;
|
|
|
|
terminal->selection_start_column =
|
|
|
|
terminal->selection_end_column = column;
|
|
|
|
terminal->text_selected = true;
|
|
|
|
|
|
|
|
/* Get char and operation */
|
|
|
|
guac_char = &(terminal->buffer->characters[terminal->buffer->width * row + column]);
|
|
|
|
guac_operation = &(terminal->delta->operations[terminal->delta->width * row + column]);
|
|
|
|
|
|
|
|
/* Set character as selected */
|
|
|
|
guac_char->attributes.selected = true;
|
|
|
|
guac_operation->type = GUAC_CHAR_SET;
|
|
|
|
guac_operation->character = *guac_char;
|
|
|
|
|
|
|
|
guac_terminal_delta_flush(terminal->delta, terminal);
|
|
|
|
guac_socket_flush(terminal->client->socket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_select_update(guac_terminal* terminal, int row, int column) {
|
|
|
|
|
|
|
|
int start_index = terminal->selection_start_row * terminal->buffer->width
|
|
|
|
+ terminal->selection_start_column;
|
|
|
|
|
|
|
|
int old_end_index = terminal->selection_end_row * terminal->buffer->width
|
|
|
|
+ terminal->selection_end_column;
|
|
|
|
|
|
|
|
int new_end_index = row * terminal->buffer->width + column;
|
|
|
|
|
|
|
|
int old_index_a, old_index_b;
|
|
|
|
int new_index_a, new_index_b;
|
|
|
|
|
|
|
|
int search_index_a, search_index_b;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
guac_terminal_char* guac_char;
|
|
|
|
guac_terminal_operation* guac_operation;
|
|
|
|
|
|
|
|
/* If unchanged, do nothing */
|
|
|
|
if (old_end_index == new_end_index) return;
|
|
|
|
|
|
|
|
/* Calculate old selection range */
|
|
|
|
if (start_index < old_end_index) {
|
|
|
|
old_index_a = start_index;
|
|
|
|
old_index_b = old_end_index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
old_index_a = old_end_index;
|
|
|
|
old_index_b = start_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate new selection range */
|
|
|
|
if (start_index < new_end_index) {
|
|
|
|
new_index_a = start_index;
|
|
|
|
new_index_b = new_end_index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_index_a = new_end_index;
|
|
|
|
new_index_b = start_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_index_a < old_index_a)
|
|
|
|
search_index_a = new_index_a;
|
|
|
|
else
|
|
|
|
search_index_a = old_index_a;
|
|
|
|
|
|
|
|
if (new_index_b > old_index_b)
|
|
|
|
search_index_b = new_index_b;
|
|
|
|
else
|
|
|
|
search_index_b = old_index_b;
|
|
|
|
|
|
|
|
/* Get first character */
|
|
|
|
guac_char = &(terminal->buffer->characters[search_index_a]);
|
|
|
|
guac_operation = &(terminal->delta->operations[search_index_a]);
|
|
|
|
|
|
|
|
/* Invert modified area */
|
|
|
|
for (i=search_index_a; i<=search_index_b; i++) {
|
|
|
|
|
|
|
|
/* If now selected, mark as such */
|
|
|
|
if (i >= new_index_a && i <= new_index_b &&
|
|
|
|
(i < old_index_a || i > old_index_b)) {
|
|
|
|
|
|
|
|
guac_char->attributes.selected = true;
|
|
|
|
guac_operation->type = GUAC_CHAR_SET;
|
|
|
|
guac_operation->character = *guac_char;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If now unselected, mark as such */
|
|
|
|
else if (i >= old_index_a && i <= old_index_b &&
|
|
|
|
(i < new_index_a || i > new_index_b)) {
|
|
|
|
|
|
|
|
guac_char->attributes.selected = false;
|
|
|
|
guac_operation->type = GUAC_CHAR_SET;
|
|
|
|
guac_operation->character = *guac_char;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next char */
|
|
|
|
guac_char++;
|
|
|
|
guac_operation++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal->selection_end_row = row;
|
|
|
|
terminal->selection_end_column = column;
|
|
|
|
|
|
|
|
guac_terminal_delta_flush(terminal->delta, terminal);
|
|
|
|
guac_socket_flush(terminal->client->socket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_terminal_select_end(guac_terminal* terminal) {
|
|
|
|
|
|
|
|
int index_a = terminal->selection_end_row * terminal->buffer->width
|
|
|
|
+ terminal->selection_end_column;
|
|
|
|
|
|
|
|
int index_b = terminal->selection_start_row * terminal->buffer->width
|
|
|
|
+ terminal->selection_start_column;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
guac_terminal_char* guac_char;
|
|
|
|
guac_terminal_operation* guac_operation;
|
|
|
|
|
|
|
|
/* The start and end indices of all characters in selection */
|
|
|
|
int start_index;
|
|
|
|
int end_index;
|
|
|
|
|
|
|
|
/* Order indices such that end is after start */
|
|
|
|
if (index_a > index_b) {
|
|
|
|
start_index = index_b;
|
|
|
|
end_index = index_a;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
start_index = index_a;
|
|
|
|
end_index = index_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get first character */
|
|
|
|
guac_char = &(terminal->buffer->characters[start_index]);
|
|
|
|
guac_operation = &(terminal->delta->operations[start_index]);
|
|
|
|
|
|
|
|
/* Restore state from buffer */
|
|
|
|
for (i=start_index; i<=end_index; i++) {
|
|
|
|
|
|
|
|
/* Restore state */
|
|
|
|
guac_char->attributes.selected = false;
|
|
|
|
guac_operation->type = GUAC_CHAR_SET;
|
|
|
|
guac_operation->character = *guac_char;
|
|
|
|
|
|
|
|
/* Next char */
|
|
|
|
guac_char++;
|
|
|
|
guac_operation++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal->text_selected = false;
|
|
|
|
|
|
|
|
guac_terminal_delta_flush(terminal->delta, terminal);
|
|
|
|
guac_socket_flush(terminal->client->socket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|