Initial stab at rearchitecting - will not compile as is. Partial.

This commit is contained in:
Michael Jumper 2013-04-25 11:54:00 -07:00
parent cb4abb9115
commit 396bba6ea3
10 changed files with 1321 additions and 1213 deletions

View File

@ -42,7 +42,9 @@ lib_LTLIBRARIES = libguac-client-ssh.la
libguac_client_ssh_la_SOURCES = \ libguac_client_ssh_la_SOURCES = \
src/blank.c \ src/blank.c \
src/buffer.c \
src/cursor.c \ src/cursor.c \
src/delta.c \
src/ibar.c \ src/ibar.c \
src/ssh_client.c \ src/ssh_client.c \
src/ssh_handlers.c \ src/ssh_handlers.c \
@ -51,12 +53,15 @@ libguac_client_ssh_la_SOURCES = \
noinst_HEADERS = \ noinst_HEADERS = \
include/blank.h \ include/blank.h \
include/buffer.h \
include/cursor.h \ include/cursor.h \
include/delta.h \
include/ibar.h \ include/ibar.h \
include/ssh_client.h \ include/ssh_client.h \
include/ssh_handlers.h \ include/ssh_handlers.h \
include/terminal.h \ include/terminal.h \
include/terminal_handlers.h include/terminal_handlers.h \
include/types.h
libguac_client_ssh_la_CFLAGS = -Werror -Wall -pedantic -Iinclude @PANGO_CFLAGS@ @PANGOCAIRO_CFLAGS@ libguac_client_ssh_la_CFLAGS = -Werror -Wall -pedantic -Iinclude @PANGO_CFLAGS@ @PANGOCAIRO_CFLAGS@
libguac_client_ssh_la_LIBADD = @PANGO_LIBS@ @PANGOCAIRO_LIBS@ libguac_client_ssh_la_LIBADD = @PANGO_LIBS@ @PANGOCAIRO_LIBS@

View File

@ -0,0 +1,132 @@
/* ***** 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):
*
* 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 ***** */
#ifndef _SSH_GUAC_BUFFER_H
#define _SSH_GUAC_BUFFER_H
#include "types.h"
/**
* A single variable-length row of terminal data.
*/
typedef struct guac_terminal_buffer_row {
/**
* Array of guac_terminal_char representing the contents of the row.
*/
guac_terminal_char* characters;
/**
* The length of this row in characters. This is the number of initialized
* characters in the buffer, usually equal to the number of characters
* in the screen width at the time this row was created.
*/
int length;
/**
* The number of elements in the characters array. After the length
* equals this value, the array must be resized.
*/
int available;
} guac_terminal_buffer_row;
/**
* A buffer containing a constant number of arbitrary-length rows.
* New rows can be appended to the buffer, with the oldest row replaced with
* the new row.
*/
typedef struct guac_terminal_buffer {
/**
* Array of buffer rows. This array functions as a ring buffer.
* When a new row needs to be appended, the top reference is moved down
* and the old top row is replaced.
*/
guac_terminal_buffer_row* rows;
/**
* The row to replace when adding a new row to the buffer.
*/
int top;
/**
* The number of rows currently stored in the buffer.
*/
int length;
/**
* The number of rows in the buffer. This is the total capacity
* of the buffer.
*/
int available;
} guac_terminal_buffer;
/**
* Allocates a new buffer having the given maximum number of rows.
*/
guac_terminal_buffer* guac_terminal_buffer_alloc(int rows);
/**
* Frees the given buffer.
*/
void guac_terminal_buffer_free(guac_terminal_buffer* buffer);
/**
* Copies the given range of columns to a new location, offset from
* the original by the given number of columns.
*/
void guac_terminal_buffer_copy_columns(guac_terminal_buffer* buffer, int row,
int start_column, int end_column, int offset);
/**
* Copies the given range of rows to a new location, offset from the
* original by the given number of rows.
*/
void guac_terminal_buffer_copy_rows(guac_terminal_buffer* buffer, int src_row, int rows,
int start_row, int end_row, int offset);
/**
* Sets the given range of columns within the given row to the given
* character.
*/
void guac_terminal_buffer_set_columns(guac_terminal_buffer* buffer, int row,
int start_column, int end_column, guac_terminal_char* character);
#endif

View File

@ -0,0 +1,228 @@
/* ***** 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):
*
* 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 ***** */
#ifndef _SSH_GUAC_DELTA_H
#define _SSH_GUAC_DELTA_H
#include <pango/pangocairo.h>
#include <guacamole/client.h>
#include "types.h"
/**
* The available color palette. All integer colors within structures
* here are indices into this palette.
*/
extern const guac_terminal_color guac_terminal_palette[16];
/**
* All available terminal operations which affect character cells.
*/
typedef enum guac_terminal_operation_type {
/**
* Operation which does nothing.
*/
GUAC_CHAR_NOP,
/**
* Operation which copies a character from a given row/column coordinate.
*/
GUAC_CHAR_COPY,
/**
* Operation which sets the character and attributes.
*/
GUAC_CHAR_SET
} guac_terminal_operation_type;
/**
* A pairing of a guac_terminal_operation_type and all parameters required by
* that operation type.
*/
typedef struct guac_terminal_operation {
/**
* The type of operation to perform.
*/
guac_terminal_operation_type type;
/**
* The character (and attributes) to set the current location to. This is
* only applicable to GUAC_CHAR_SET.
*/
guac_terminal_char character;
/**
* The row to copy a character from. This is only applicable to
* GUAC_CHAR_COPY.
*/
int row;
/**
* The column to copy a character from. This is only applicable to
* GUAC_CHAR_COPY.
*/
int column;
} guac_terminal_operation;
/**
* Set of all pending operations for the currently-visible screen area.
*/
typedef struct guac_terminal_delta {
/**
* The Guacamole client this delta will use for rendering.
*/
guac_client* client;
/**
* Array of all operations pending for the visible screen area.
*/
guac_terminal_operation* operations;
/**
* Scratch area of same size as the operations buffer, facilitating copies
* of overlapping regions.
*/
guac_terminal_operation* scratch;
/**
* The width of the screen, in characters.
*/
int width;
/**
* The height of the screen, in characters.
*/
int height;
/**
* The description of the font to use for rendering.
*/
PangoFontDescription* font_desc;
/**
* The width of each character, in pixels.
*/
int char_width;
/**
* The height of each character, in pixels.
*/
int char_height;
/**
* Index of next glyph to create
*/
int next_glyph;
/**
* Index of locations for each glyph in the stroke and fill layers.
*/
int glyphs[256];
/**
* Color of glyphs in copy buffer
*/
int glyph_foreground;
/**
* Color of glyphs in copy buffer
*/
int glyph_background;
/**
* A single wide layer holding each glyph, with each glyph only
* colored with foreground color (background remains transparent).
*/
guac_layer* glyph_stroke;
/**
* A single wide layer holding each glyph, with each glyph properly
* colored with foreground and background color (no transparency at all).
*/
guac_layer* filled_glyphs;
} guac_terminal_delta;
/**
* Allocates a new delta having the given dimensions.
*/
guac_terminal_delta* guac_terminal_delta_alloc(guac_client* client, int width, int height,
int foreground, int background);
/**
* Frees the given delta.
*/
void guac_terminal_delta_free(guac_terminal_delta* delta);
/**
* Copies the given range of columns to a new location, offset from
* the original by the given number of columns.
*/
void guac_terminal_delta_copy_columns(guac_terminal_delta* delta, int row,
int start_column, int end_column, int offset);
/**
* Copies the given range of rows to a new location, offset from the
* original by the given number of rows.
*/
void guac_terminal_delta_copy_rows(guac_terminal_delta* delta, int src_row, int rows,
int start_row, int end_row, int offset);
/**
* Sets the given range of columns within the given row to the given
* character.
*/
void guac_terminal_delta_set_columns(guac_terminal_delta* delta, int row,
int start_column, int end_column, guac_terminal_char* character);
/**
* Resize the terminal to the given dimensions.
*/
void guac_terminal_delta_resize(guac_terminal_delta* delta, int rows, int cols);
/**
* Flushes all pending operations within the given guac_terminal_delta.
*/
void guac_terminal_delta_flush(guac_terminal_delta* delta);
#endif

View File

@ -41,10 +41,12 @@
#include <stdbool.h> #include <stdbool.h>
#include <pthread.h> #include <pthread.h>
#include <pango/pangocairo.h>
#include <guacamole/client.h> #include <guacamole/client.h>
#include "types.h"
#include "delta.h"
#include "buffer.h"
#define GUAC_SSH_WHEEL_SCROLL_AMOUNT 3 #define GUAC_SSH_WHEEL_SCROLL_AMOUNT 3
typedef struct guac_terminal guac_terminal; typedef struct guac_terminal guac_terminal;
@ -56,250 +58,6 @@ typedef struct guac_terminal guac_terminal;
*/ */
typedef int guac_terminal_char_handler(guac_terminal* term, char c); typedef int guac_terminal_char_handler(guac_terminal* term, char c);
/**
* An RGB color, where each component ranges from 0 to 255.
*/
typedef struct guac_terminal_color {
/**
* The red component of this color.
*/
int red;
/**
* The green component of this color.
*/
int green;
/**
* The blue component of this color.
*/
int blue;
} guac_terminal_color;
/**
* Terminal attributes, as can be applied to a single character.
*/
typedef struct guac_terminal_attributes {
/**
* Whether the character should be rendered bold.
*/
bool bold;
/**
* Whether the character should be rendered with reversed colors
* (background becomes foreground and vice-versa).
*/
bool reverse;
/**
* Whether the associated character is selected.
*/
bool selected;
/**
* Whether to render the character with underscore.
*/
bool underscore;
/**
* The foreground color of this character, as a palette index.
*/
int foreground;
/**
* The background color of this character, as a palette index.
*/
int background;
} guac_terminal_attributes;
/**
* The available color palette. All integer colors within structures
* here are indices into this palette.
*/
extern const guac_terminal_color guac_terminal_palette[16];
/**
* Represents a single character for display in a terminal, including actual
* character value, foreground color, and background color.
*/
typedef struct guac_terminal_char {
/**
* The character value of the character to display.
*/
char value;
/**
* The attributes of the character to display.
*/
guac_terminal_attributes attributes;
} guac_terminal_char;
/**
* All available terminal operations which affect character cells.
*/
typedef enum guac_terminal_operation_type {
/**
* Operation which does nothing.
*/
GUAC_CHAR_NOP,
/**
* Operation which copies a character from a given row/column coordinate.
*/
GUAC_CHAR_COPY,
/**
* Operation which sets the character and attributes.
*/
GUAC_CHAR_SET
} guac_terminal_operation_type;
/**
* A pairing of a guac_terminal_operation_type and all parameters required by
* that operation type.
*/
typedef struct guac_terminal_operation {
/**
* The type of operation to perform.
*/
guac_terminal_operation_type type;
/**
* The character (and attributes) to set the current location to. This is
* only applicable to GUAC_CHAR_SET.
*/
guac_terminal_char character;
/**
* The row to copy a character from. This is only applicable to
* GUAC_CHAR_COPY.
*/
int row;
/**
* The column to copy a character from. This is only applicable to
* GUAC_CHAR_COPY.
*/
int column;
} guac_terminal_operation;
/**
* Set of all pending operations for the currently-visible screen area.
*/
typedef struct guac_terminal_delta {
/**
* Array of all operations pending for the visible screen area.
*/
guac_terminal_operation* operations;
/**
* Scratch area of same size as the operations buffer, facilitating copies
* of overlapping regions.
*/
guac_terminal_operation* scratch;
/**
* The width of the screen, in characters.
*/
int width;
/**
* The height of the screen, in characters.
*/
int height;
} guac_terminal_delta;
/**
* A single variable-length row of terminal data.
*/
typedef struct guac_terminal_scrollback_row {
/**
* Array of guac_terminal_char representing the contents of the row.
*/
guac_terminal_char* characters;
/**
* The length of this row in characters. This is the number of initialized
* characters in the buffer, usually equal to the number of characters
* in the screen width at the time this row was created.
*/
int length;
/**
* The number of elements in the characters array. After the length
* equals this value, the array must be resized.
*/
int available;
} guac_terminal_scrollback_row;
/**
* A scrollback buffer containing a constant number of arbitrary-length rows.
* New rows can be appended to the buffer, with the oldest row replaced with
* the new row.
*/
typedef struct guac_terminal_scrollback_buffer {
/**
* Array of scrollback buffer rows. This array functions as a ring buffer.
* When a new row needs to be appended, the top reference is moved down
* and the old top row is replaced.
*/
guac_terminal_scrollback_row* scrollback;
/**
* The number of rows in the scrollback buffer. This is the total capacity
* of the buffer.
*/
int rows;
/**
* The row to replace when adding a new row to the scrollback.
*/
int top;
/**
* The number of rows currently stored in the scrollback buffer.
*/
int length;
} guac_terminal_scrollback_buffer;
/**
* Dynamically-resizable character buffer.
*/
typedef struct guac_terminal_buffer {
/**
* Array of characters.
*/
guac_terminal_char* characters;
/**
* The width of this buffer in characters.
*/
int width;
/**
* The height of this buffer in characters.
*/
int height;
} guac_terminal_buffer;
/** /**
* Represents a terminal emulator which uses a given Guacamole client to * Represents a terminal emulator which uses a given Guacamole client to
* render itself. * render itself.
@ -317,48 +75,6 @@ struct guac_terminal {
*/ */
pthread_mutex_t lock; pthread_mutex_t lock;
/**
* The description of the font to use for rendering.
*/
PangoFontDescription* font_desc;
/**
* Index of next glyph to create
*/
int next_glyph;
/**
* Index of locations for each glyph in the stroke and fill layers.
*/
int glyphs[256];
/**
* Color of glyphs in copy buffer
*/
int glyph_foreground;
/**
* Color of glyphs in copy buffer
*/
int glyph_background;
/**
* A single wide layer holding each glyph, with each glyph only
* colored with foreground color (background remains transparent).
*/
guac_layer* glyph_stroke;
/**
* A single wide layer holding each glyph, with each glyph properly
* colored with foreground and background color (no transparency at all).
*/
guac_layer* filled_glyphs;
/**
* The scrollback buffer.
*/
guac_terminal_scrollback_buffer* scrollback;
/** /**
* The relative offset of the display. A positive value indicates that * The relative offset of the display. A positive value indicates that
* many rows have been scrolled into view, zero indicates that no * many rows have been scrolled into view, zero indicates that no
@ -366,16 +82,6 @@ struct guac_terminal {
*/ */
int scroll_offset; int scroll_offset;
/**
* The width of each character, in pixels.
*/
int char_width;
/**
* The height of each character, in pixels.
*/
int char_height;
/** /**
* The width of the terminal, in characters. * The width of the terminal, in characters.
*/ */
@ -485,14 +191,6 @@ int guac_terminal_write(guac_terminal* term, const char* c, int size);
*/ */
int guac_terminal_set(guac_terminal* term, int row, int col, char c); int guac_terminal_set(guac_terminal* term, int row, int col, char c);
/**
* Copies a rectangular region of characters which may overlap with the
* destination.
*/
int guac_terminal_copy(guac_terminal* term,
int src_row, int src_col, int rows, int cols,
int dst_row, int dst_col);
/** /**
* Clears a rectangular region of characters, replacing them with the * Clears a rectangular region of characters, replacing them with the
* current background color and attributes. * current background color and attributes.
@ -525,128 +223,16 @@ int guac_terminal_scroll_down(guac_terminal* term,
*/ */
int guac_terminal_toggle_reverse(guac_terminal* term, int row, int col); int guac_terminal_toggle_reverse(guac_terminal* term, int row, int col);
/**
* Allocates a new guac_terminal_delta.
*/
guac_terminal_delta* guac_terminal_delta_alloc(int width, int height);
/**
* Frees the given guac_terminal_delta.
*/
void guac_terminal_delta_free(guac_terminal_delta* delta);
/**
* Resizes the given guac_terminal_delta to the given dimensions.
*/
void guac_terminal_delta_resize(guac_terminal_delta* delta,
int width, int height);
/**
* Stores a set operation at the given location.
*/
void guac_terminal_delta_set(guac_terminal_delta* delta, int r, int c,
guac_terminal_char* character);
/**
* Stores a rectangle of copy operations, copying existing operations as
* necessary.
*/
void guac_terminal_delta_copy(guac_terminal_delta* delta,
int dst_row, int dst_column,
int src_row, int src_column,
int w, int h);
/**
* Sets a rectangle of character data to the given character value.
*/
void guac_terminal_delta_set_rect(guac_terminal_delta* delta,
int row, int column, int w, int h,
guac_terminal_char* character);
/**
* Flushes all pending operations within the given guac_client_delta to the
* given guac_terminal.
*/
void guac_terminal_delta_flush(guac_terminal_delta* delta,
guac_terminal* terminal);
/**
* Allocates a new character buffer having the given dimensions.
*/
guac_terminal_buffer* guac_terminal_buffer_alloc(int width, int height);
/**
* Resizes the given character buffer to the given dimensions.
*/
void guac_terminal_buffer_resize(guac_terminal_buffer* buffer,
int width, int height);
/**
* Sets the character at the given location within the buffer to the given
* value.
*/
void guac_terminal_buffer_set(guac_terminal_buffer* buffer, int r, int c,
guac_terminal_char* character);
/**
* Copies a rectangle of character data within the buffer. The source and
* destination may overlap.
*/
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);
/**
* Sets a rectangle of character data to the given character value.
*/
void guac_terminal_buffer_set_rect(guac_terminal_buffer* buffer,
int row, int column, int w, int h,
guac_terminal_char* character);
/**
* Frees the given character buffer.
*/
void guac_terminal_buffer_free(guac_terminal_buffer* buffer);
/**
* Allocates a new scrollback buffer having the given number of rows.
*/
guac_terminal_scrollback_buffer*
guac_terminal_scrollback_buffer_alloc(int rows);
/**
* Frees the given scrollback buffer.
*/
void guac_terminal_scrollback_buffer_free(
guac_terminal_scrollback_buffer* buffer);
/**
* Pushes the given number of rows into the scrollback, maintaining display
* position within the scrollback as possible.
*/
void guac_terminal_scrollback_buffer_append(
guac_terminal_scrollback_buffer* buffer,
guac_terminal* terminal, int rows);
/**
* Returns the row within the scrollback at the given location. The index
* of the row given is a negative number, denoting the number of rows into
* the past to look.
*/
guac_terminal_scrollback_row* guac_terminal_scrollback_buffer_get_row(
guac_terminal_scrollback_buffer* buffer, int row);
/** /**
* Scroll down the display by the given amount, replacing the new space with * Scroll down the display by the given amount, replacing the new space with
* data from the scrollback. If not enough data is available, the maximum * data from the buffer. If not enough data is available, the maximum
* amount will be scrolled. * amount will be scrolled.
*/ */
void guac_terminal_scroll_display_down(guac_terminal* terminal, int amount); void guac_terminal_scroll_display_down(guac_terminal* terminal, int amount);
/** /**
* Scroll up the display by the given amount, replacing the new space with data * Scroll up the display by the given amount, replacing the new space with data
* from either the scrollback or the terminal buffer. If not enough data is * from either the buffer or the terminal buffer. If not enough data is
* available, the maximum amount will be scrolled. * available, the maximum amount will be scrolled.
*/ */
void guac_terminal_scroll_display_up(guac_terminal* terminal, int amount); void guac_terminal_scroll_display_up(guac_terminal* terminal, int amount);
@ -666,11 +252,38 @@ void guac_terminal_select_update(guac_terminal* terminal, int row, int column);
*/ */
void guac_terminal_select_end(guac_terminal* terminal); void guac_terminal_select_end(guac_terminal* terminal);
/* LOW-LEVEL TERMINAL OPERATIONS */
/** /**
* Returns a row of character data, whether that data be from the scrollback buffer or the main backing buffer. The length * Copies the given range of columns to a new location, offset from
* parameter given here is a pointer to the int variable that should receive the length of the character array returned. * the original by the given number of columns.
*/ */
guac_terminal_char* guac_terminal_get_row(guac_terminal* terminal, int row, int* length); void guac_terminal_copy_columns(guac_terminal* terminal, int row,
int start_column, int end_column, int offset);
/**
* Copies the given range of rows to a new location, offset from the
* original by the given number of rows.
*/
void guac_terminal_copy_rows(guac_terminal* terminal, int src_row, int rows,
int start_row, int end_row, int offset);
/**
* Sets the given range of columns within the given row to the given
* character.
*/
void guac_terminal_set_columns(guac_terminal* terminal, int row,
int start_column, int end_column, guac_terminal_char* character);
/**
* Resize the terminal to the given dimensions.
*/
void guac_terminal_resize(guac_terminal* term, int rows, int cols);
/**
* Flushes all pending operations within the given guac_terminal.
*/
void guac_terminal_flush(guac_terminal* terminal);
#endif #endif

View File

@ -0,0 +1,122 @@
/* ***** 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):
*
* 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 ***** */
#ifndef _SSH_GUAC_TYPES_H
#define _SSH_GUAC_TYPES_H
#include <stdbool.h>
/**
* An RGB color, where each component ranges from 0 to 255.
*/
typedef struct guac_terminal_color {
/**
* The red component of this color.
*/
int red;
/**
* The green component of this color.
*/
int green;
/**
* The blue component of this color.
*/
int blue;
} guac_terminal_color;
/**
* Terminal attributes, as can be applied to a single character.
*/
typedef struct guac_terminal_attributes {
/**
* Whether the character should be rendered bold.
*/
bool bold;
/**
* Whether the character should be rendered with reversed colors
* (background becomes foreground and vice-versa).
*/
bool reverse;
/**
* Whether the associated character is selected.
*/
bool selected;
/**
* Whether to render the character with underscore.
*/
bool underscore;
/**
* The foreground color of this character, as a palette index.
*/
int foreground;
/**
* The background color of this character, as a palette index.
*/
int background;
} guac_terminal_attributes;
/**
* Represents a single character for display in a terminal, including actual
* character value, foreground color, and background color.
*/
typedef struct guac_terminal_char {
/**
* The character value of the character to display.
*/
char value;
/**
* The attributes of the character to display.
*/
guac_terminal_attributes attributes;
} guac_terminal_char;
#endif

107
protocols/ssh/src/buffer.c Normal file
View File

@ -0,0 +1,107 @@
/* ***** 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):
*
* 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 "buffer.h"
guac_terminal_buffer* guac_terminal_buffer_alloc(int rows) {
/* Allocate scrollback */
guac_terminal_buffer* buffer =
malloc(sizeof(guac_terminal_buffer));
int i;
guac_terminal_buffer_row* row;
/* Init scrollback data */
buffer->available = rows;
buffer->top = 0;
buffer->length = 0;
buffer->rows = malloc(sizeof(guac_terminal_buffer_row) *
buffer->available);
/* Init scrollback rows */
row = buffer->rows;
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_buffer_free(guac_terminal_buffer* buffer) {
int i;
guac_terminal_buffer_row* row = buffer->rows;
/* Free all rows */
for (i=0; i<buffer->available; i++) {
free(row->characters);
row++;
}
/* Free actual buffer */
free(buffer->rows);
free(buffer);
}
void guac_terminal_buffer_copy_columns(guac_terminal_buffer* buffer, int row,
int start_column, int end_column, int offset) {
/* STUB */
}
void guac_terminal_buffer_copy_rows(guac_terminal_buffer* buffer, int src_row, int rows,
int start_row, int end_row, int offset) {
/* STUB */
}
void guac_terminal_buffer_set_columns(guac_terminal_buffer* buffer, int row,
int start_column, int end_column, guac_terminal_char* character) {
/* STUB */
}

667
protocols/ssh/src/delta.c Normal file
View File

@ -0,0 +1,667 @@
/* ***** 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):
*
* 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>
#include <guacamole/client.h>
#include <guacamole/protocol.h>
#include "types.h"
#include "delta.h"
const guac_terminal_color guac_terminal_palette[16] = {
/* Normal colors */
{0x00, 0x00, 0x00}, /* Black */
{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 */
/* Intense colors */
{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 */
{0xFF, 0xFF, 0xFF}, /* White */
};
/**
* 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.
*/
int __guac_terminal_get_glyph(guac_terminal_delta* delta, char c) {
guac_socket* socket = delta->client->socket;
int location;
/* Use foreground color */
const guac_terminal_color* color =
&guac_terminal_palette[delta->glyph_foreground];
/* Use background color */
const guac_terminal_color* background =
&guac_terminal_palette[delta->glyph_background];
cairo_surface_t* surface;
cairo_t* cairo;
PangoLayout* layout;
/* Return glyph if exists */
if (delta->glyphs[(int) c])
return delta->glyphs[(int) c] - 1;
location = delta->next_glyph++;
/* Otherwise, draw glyph */
surface = cairo_image_surface_create(
CAIRO_FORMAT_ARGB32,
delta->char_width, delta->char_height);
cairo = cairo_create(surface);
/* Get layout */
layout = pango_cairo_create_layout(cairo);
pango_layout_set_font_description(layout, delta->font_desc);
pango_layout_set_text(layout, &c, 1);
/* Draw */
cairo_set_source_rgba(cairo,
color->red / 255.0,
color->green / 255.0,
color->blue / 255.0,
1.0 /* alpha */ );
cairo_move_to(cairo, 0.0, 0.0);
pango_cairo_show_layout(cairo, layout);
/* Free all */
g_object_unref(layout);
cairo_destroy(cairo);
/* Send glyph and update filled flyphs */
guac_protocol_send_png(socket, GUAC_COMP_OVER, delta->glyph_stroke, location * delta->char_width, 0, surface);
guac_protocol_send_rect(socket, delta->filled_glyphs,
location * delta->char_width, 0,
delta->char_width, delta->char_height);
guac_protocol_send_cfill(socket, GUAC_COMP_OVER, delta->filled_glyphs,
background->red,
background->green,
background->blue,
0xFF);
guac_protocol_send_copy(socket, delta->glyph_stroke,
location * delta->char_width, 0, delta->char_width, delta->char_height,
GUAC_COMP_OVER, delta->filled_glyphs, location * delta->char_width, 0);
delta->glyphs[(int) c] = location+1;
cairo_surface_destroy(surface);
/* Return glyph */
return location;
}
/**
* 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_delta* delta,
guac_terminal_attributes* attributes) {
guac_socket* socket = delta->client->socket;
const guac_terminal_color* background_color;
int background, foreground;
/* Handle reverse video */
if (attributes->reverse != attributes->selected) {
background = attributes->foreground;
foreground = attributes->background;
}
else {
foreground = attributes->foreground;
background = attributes->background;
}
/* Handle bold */
if (attributes->bold && foreground <= 7)
foreground += 8;
/* Get background color */
background_color = &guac_terminal_palette[background];
/* If foreground different from current, colorize */
if (foreground != delta->glyph_foreground) {
/* Get color */
const guac_terminal_color* color =
&guac_terminal_palette[foreground];
/* Colorize letter */
guac_protocol_send_rect(socket, delta->glyph_stroke,
0, 0,
delta->char_width * delta->next_glyph, delta->char_height);
guac_protocol_send_cfill(socket, GUAC_COMP_ATOP, delta->glyph_stroke,
color->red,
color->green,
color->blue,
255);
}
/* If any color change at all, update filled */
if (foreground != delta->glyph_foreground
|| background != delta->glyph_background) {
/* Set background */
guac_protocol_send_rect(socket, delta->filled_glyphs,
0, 0,
delta->char_width * delta->next_glyph, delta->char_height);
guac_protocol_send_cfill(socket, GUAC_COMP_OVER, delta->filled_glyphs,
background_color->red,
background_color->green,
background_color->blue,
255);
/* Copy stroke */
guac_protocol_send_copy(socket, delta->glyph_stroke,
0, 0,
delta->char_width * delta->next_glyph, delta->char_height,
GUAC_COMP_OVER, delta->filled_glyphs,
0, 0);
}
delta->glyph_foreground = foreground;
delta->glyph_background = background;
return 0;
}
/**
* Sends the given character to the terminal at the given row and column,
* rendering the charater immediately. This bypasses the guac_terminal_delta
* mechanism and is intended for flushing of updates only.
*/
int __guac_terminal_set(guac_terminal_delta* delta, int row, int col, char c) {
guac_socket* socket = delta->client->socket;
int location = __guac_terminal_get_glyph(delta, c);
return guac_protocol_send_copy(socket,
delta->filled_glyphs,
location * delta->char_width, 0, delta->char_width, delta->char_height,
GUAC_COMP_OVER, GUAC_DEFAULT_LAYER,
delta->char_width * col,
delta->char_height * row);
}
guac_terminal_delta* guac_terminal_delta_alloc(guac_client* client, int width, int height,
int foreground, int background) {
guac_terminal_operation* current;
int x, y;
PangoFontMap* font_map;
PangoFont* font;
PangoFontMetrics* metrics;
PangoContext* context;
/* Allocate delta */
guac_terminal_delta* delta = malloc(sizeof(guac_terminal_delta));
delta->client = client;
memset(delta->glyphs, 0, sizeof(delta->glyphs));
delta->glyph_stroke = guac_client_alloc_buffer(client);
delta->filled_glyphs = guac_client_alloc_buffer(client);
/* Get font */
delta->font_desc = pango_font_description_new();
pango_font_description_set_family(delta->font_desc, "monospace");
pango_font_description_set_weight(delta->font_desc, PANGO_WEIGHT_NORMAL);
pango_font_description_set_size(delta->font_desc, 12*PANGO_SCALE);
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, delta->font_desc);
if (font == NULL) {
guac_client_log_error(delta->client, "Unable to get font.");
return NULL;
}
metrics = pango_font_get_metrics(font, NULL);
if (metrics == NULL) {
guac_client_log_error(delta->client, "Unable to get font metrics.");
return NULL;
}
delta->glyph_foreground = foreground;
delta->glyph_background = background;
/* Calculate character dimensions */
delta->char_width =
pango_font_metrics_get_approximate_digit_width(metrics) / PANGO_SCALE;
delta->char_height =
(pango_font_metrics_get_descent(metrics)
+ pango_font_metrics_get_ascent(metrics)) / PANGO_SCALE;
/* Set width and height */
delta->width = width;
delta->height = height;
/* Alloc operations */
delta->operations = malloc(width * height *
sizeof(guac_terminal_operation));
/* Init each operation buffer row */
current = delta->operations;
for (y=0; y<height; y++) {
/* Init entire row to NOP */
for (x=0; x<width; x++)
(current++)->type = GUAC_CHAR_NOP;
}
/* Alloc scratch area */
delta->scratch = malloc(width * height * sizeof(guac_terminal_operation));
/* Send initial display size */
guac_protocol_send_size(client->socket,
GUAC_DEFAULT_LAYER,
delta->char_width * width,
delta->char_height * height);
return delta;
}
void guac_terminal_delta_free(guac_terminal_delta* delta) {
/* Free operations buffers */
free(delta->operations);
free(delta->scratch);
/* Free delta */
free(delta);
}
void guac_terminal_delta_copy_columns(guac_terminal_delta* delta, int row,
int start_column, int end_column, int offset) {
/* STUB */
}
void guac_terminal_delta_copy_rows(guac_terminal_delta* delta, int src_row, int rows,
int start_row, int end_row, int offset) {
/* STUB */
}
void guac_terminal_delta_set_columns(guac_terminal_delta* delta, int row,
int start_column, int end_column, guac_terminal_char* character) {
/* STUB */
}
void guac_terminal_delta_resize(guac_terminal_delta* delta, int rows, int cols) {
/* STUB */
}
void __guac_terminal_delta_flush_copy(guac_terminal_delta* delta) {
guac_terminal_operation* current = delta->operations;
int row, col;
/* For each operation */
for (row=0; row<delta->height; row++) {
for (col=0; col<delta->width; col++) {
/* If operation is a copy operation */
if (current->type == GUAC_CHAR_COPY) {
/* 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 */
guac_terminal_operation* rect_current_row;
/* Determine bounds of rectangle */
rect_current_row = current;
expected_row = current->row;
for (rect_row=row; rect_row<delta->height; rect_row++) {
guac_terminal_operation* rect_current = rect_current_row;
expected_col = current->column;
/* Find width */
for (rect_col=col; rect_col<delta->width; rect_col++) {
/* If not identical operation, stop */
if (rect_current->type != GUAC_CHAR_COPY
|| rect_current->row != expected_row
|| rect_current->column != expected_col)
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 */
rect_current_row += delta->width;
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;
expected_row = current->row;
for (rect_row=0; rect_row<rect_height; rect_row++) {
guac_terminal_operation* rect_current = rect_current_row;
expected_col = current->column;
for (rect_col=0; rect_col<rect_width; rect_col++) {
/* Mark copy operations as NOP */
if (rect_current->type == GUAC_CHAR_COPY
&& rect_current->row == expected_row
&& rect_current->column == expected_col)
rect_current->type = GUAC_CHAR_NOP;
/* Next column */
rect_current++;
expected_col++;
}
/* Next row */
rect_current_row += delta->width;
expected_row++;
}
/* Send copy */
guac_protocol_send_copy(delta->client->socket,
GUAC_DEFAULT_LAYER,
current->column * delta->char_width,
current->row * delta->char_height,
rect_width * delta->char_width,
rect_height * delta->char_height,
GUAC_COMP_OVER,
GUAC_DEFAULT_LAYER,
col * delta->char_width,
row * delta->char_height);
} /* end if copy operation */
/* Next operation */
current++;
}
}
}
void __guac_terminal_delta_flush_clear(guac_terminal_delta* delta) {
guac_terminal_operation* current = delta->operations;
int row, col;
/* For each operation */
for (row=0; row<delta->height; row++) {
for (col=0; col<delta->width; col++) {
/* If operation is a cler operation (set to space) */
if (current->type == GUAC_CHAR_SET &&
current->character.value == ' ') {
/* 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 */
int color;
if (current->character.attributes.reverse != current->character.attributes.selected)
color = current->character.attributes.foreground;
else
color = current->character.attributes.background;
const guac_terminal_color* guac_color =
&guac_terminal_palette[color];
/* Current row within a subrect */
guac_terminal_operation* rect_current_row;
/* Determine bounds of rectangle */
rect_current_row = current;
for (rect_row=row; rect_row<delta->height; rect_row++) {
guac_terminal_operation* rect_current = rect_current_row;
/* Find width */
for (rect_col=col; rect_col<delta->width; rect_col++) {
int joining_color;
if (rect_current->character.attributes.reverse)
joining_color = current->character.attributes.foreground;
else
joining_color = current->character.attributes.background;
/* If not identical operation, stop */
if (rect_current->type != GUAC_CHAR_SET
|| rect_current->character.value != ' '
|| joining_color != color)
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 */
rect_current_row += delta->width;
}
/* 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++) {
guac_terminal_operation* rect_current = rect_current_row;
for (rect_col=0; rect_col<rect_width; rect_col++) {
int joining_color;
if (rect_current->character.attributes.reverse)
joining_color = current->character.attributes.foreground;
else
joining_color = current->character.attributes.background;
/* Mark clear operations as NOP */
if (rect_current->type == GUAC_CHAR_SET
&& rect_current->character.value == ' '
&& joining_color == color)
rect_current->type = GUAC_CHAR_NOP;
/* Next column */
rect_current++;
}
/* Next row */
rect_current_row += delta->width;
}
/* Send rect */
guac_protocol_send_rect(delta->client->socket,
GUAC_DEFAULT_LAYER,
col * delta->char_width,
row * delta->char_height,
rect_width * delta->char_width,
rect_height * delta->char_height);
guac_protocol_send_cfill(delta->client->socket,
GUAC_COMP_OVER,
GUAC_DEFAULT_LAYER,
guac_color->red, guac_color->green, guac_color->blue,
0xFF);
} /* end if clear operation */
/* Next operation */
current++;
}
}
}
void __guac_terminal_delta_flush_set(guac_terminal_delta* delta) {
guac_terminal_operation* current = delta->operations;
int row, col;
/* For each operation */
for (row=0; row<delta->height; row++) {
for (col=0; col<delta->width; col++) {
/* Perform given operation */
if (current->type == GUAC_CHAR_SET) {
/* Set attributes */
__guac_terminal_set_colors(delta,
&(current->character.attributes));
/* Send character */
__guac_terminal_set(delta, row, col,
current->character.value);
/* Mark operation as handled */
current->type = GUAC_CHAR_NOP;
}
/* Next operation */
current++;
}
}
}
void guac_terminal_delta_flush(guac_terminal_delta* delta) {
/* Flush operations, copies first, then clears, then sets. */
__guac_terminal_delta_flush_copy(delta);
__guac_terminal_delta_flush_clear(delta);
__guac_terminal_delta_flush_set(delta);
}

View File

@ -125,10 +125,6 @@ int guac_client_init(guac_client* client, int argc, char** argv) {
/* Send name and dimensions */ /* Send name and dimensions */
guac_protocol_send_name(socket, "SSH TEST"); guac_protocol_send_name(socket, "SSH TEST");
guac_protocol_send_size(socket,
GUAC_DEFAULT_LAYER,
term->char_width * term->term_width,
term->char_height * term->term_height);
/* Initialize pointer */ /* Initialize pointer */
client_data->current_cursor = client_data->blank_cursor; client_data->current_cursor = client_data->blank_cursor;

View File

@ -118,7 +118,7 @@ int ssh_guac_client_handle_messages(guac_client* client) {
client_data->term->cursor_col); client_data->term->cursor_col);
/* Flush terminal delta */ /* Flush terminal delta */
guac_terminal_delta_flush(client_data->term->delta, client_data->term); guac_terminal_delta_flush(client_data->term->delta);
/* Unlock terminal access */ /* Unlock terminal access */
pthread_mutex_unlock(&(client_data->term->lock)); pthread_mutex_unlock(&(client_data->term->lock));
@ -186,8 +186,8 @@ int ssh_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
/* Otherwise, just update */ /* Otherwise, just update */
else else
guac_terminal_select_update(term, guac_terminal_select_update(term,
y / term->char_height - term->scroll_offset, y / term->delta->char_height - term->scroll_offset,
x / term->char_width); x / term->delta->char_width);
pthread_mutex_unlock(&(term->lock)); pthread_mutex_unlock(&(term->lock));
} }
@ -197,8 +197,8 @@ int ssh_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
pthread_mutex_lock(&(term->lock)); pthread_mutex_lock(&(term->lock));
guac_terminal_select_start(term, guac_terminal_select_start(term,
y / term->char_height - term->scroll_offset, y / term->delta->char_height - term->scroll_offset,
x / term->char_width); x / term->delta->char_width);
pthread_mutex_unlock(&(term->lock)); pthread_mutex_unlock(&(term->lock));
} }

View File

@ -47,33 +47,12 @@
#include <guacamole/protocol.h> #include <guacamole/protocol.h>
#include <guacamole/client.h> #include <guacamole/client.h>
#include "types.h"
#include "buffer.h"
#include "delta.h"
#include "terminal.h" #include "terminal.h"
#include "terminal_handlers.h" #include "terminal_handlers.h"
const guac_terminal_color guac_terminal_palette[16] = {
/* Normal colors */
{0x00, 0x00, 0x00}, /* Black */
{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 */
/* Intense colors */
{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 */
{0xFF, 0xFF, 0xFF}, /* White */
};
guac_terminal* guac_terminal_create(guac_client* client, guac_terminal* guac_terminal_create(guac_client* client,
int width, int height) { int width, int height) {
@ -85,57 +64,28 @@ guac_terminal* guac_terminal_create(guac_client* client,
.underscore = false .underscore = false
}; };
PangoFontMap* font_map;
PangoFont* font;
PangoFontMetrics* metrics;
PangoContext* context;
guac_terminal* term = malloc(sizeof(guac_terminal)); guac_terminal* term = malloc(sizeof(guac_terminal));
term->client = client; term->client = client;
/* Init buffer */
term->buffer = guac_terminal_buffer_alloc(1000);
term->scroll_offset = 0;
/* Init delta */
term->delta = guac_terminal_delta_alloc(client,
term->term_width, term->term_height,
default_attributes.foreground,
default_attributes.background);
/* Init terminal state */
term->current_attributes = term->current_attributes =
term->default_attributes = default_attributes; term->default_attributes = default_attributes;
term->glyph_foreground = default_attributes.foreground;
term->glyph_background = default_attributes.background;
memset(term->glyphs, 0, sizeof(term->glyphs));
term->glyph_stroke = guac_client_alloc_buffer(client);
term->filled_glyphs = guac_client_alloc_buffer(client);
/* 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);
pango_font_description_set_size(term->font_desc, 12*PANGO_SCALE);
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;
term->cursor_row = 0; term->cursor_row = 0;
term->cursor_col = 0; term->cursor_col = 0;
term->term_width = width / term->char_width; term->term_width = width / term->delta->char_width;
term->term_height = height / term->char_height; term->term_height = height / term->delta->char_height;
term->char_handler = guac_terminal_echo; term->char_handler = guac_terminal_echo;
term->scroll_start = 0; term->scroll_start = 0;
@ -143,18 +93,6 @@ guac_terminal* guac_terminal_create(guac_client* client,
term->text_selected = false; term->text_selected = false;
/* Init scrollback buffer */
term->scrollback = guac_terminal_scrollback_buffer_alloc(1000);
term->scroll_offset = 0;
/* Init delta */
term->delta = guac_terminal_delta_alloc(term->term_width,
term->term_height);
/* Init buffer */
term->buffer = guac_terminal_buffer_alloc(term->term_width,
term->term_height);
/* Clear with background color */ /* Clear with background color */
guac_terminal_clear(term, guac_terminal_clear(term,
0, 0, term->term_height, term->term_width); 0, 0, term->term_height, term->term_width);
@ -168,9 +106,6 @@ guac_terminal* guac_terminal_create(guac_client* client,
void guac_terminal_free(guac_terminal* term) { void guac_terminal_free(guac_terminal* term) {
/* Free scrollback buffer */
guac_terminal_scrollback_buffer_free(term->scrollback);
/* Free delta */ /* Free delta */
guac_terminal_delta_free(term->delta); guac_terminal_delta_free(term->delta);
@ -179,187 +114,6 @@ void guac_terminal_free(guac_terminal* term) {
} }
/**
* 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.
*/
int __guac_terminal_get_glyph(guac_terminal* term, char c) {
guac_socket* socket = term->client->socket;
int location;
/* Use foreground color */
const guac_terminal_color* color =
&guac_terminal_palette[term->glyph_foreground];
/* Use background color */
const guac_terminal_color* background =
&guac_terminal_palette[term->glyph_background];
cairo_surface_t* surface;
cairo_t* cairo;
PangoLayout* layout;
/* Return glyph if exists */
if (term->glyphs[(int) c])
return term->glyphs[(int) c] - 1;
location = term->next_glyph++;
/* 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 */
cairo_set_source_rgba(cairo,
color->red / 255.0,
color->green / 255.0,
color->blue / 255.0,
1.0 /* alpha */ );
cairo_move_to(cairo, 0.0, 0.0);
pango_cairo_show_layout(cairo, layout);
/* Free all */
g_object_unref(layout);
cairo_destroy(cairo);
/* Send glyph and update filled flyphs */
guac_protocol_send_png(socket, GUAC_COMP_OVER, term->glyph_stroke, location * term->char_width, 0, surface);
guac_protocol_send_rect(socket, term->filled_glyphs,
location * term->char_width, 0,
term->char_width, term->char_height);
guac_protocol_send_cfill(socket, GUAC_COMP_OVER, term->filled_glyphs,
background->red,
background->green,
background->blue,
0xFF);
guac_protocol_send_copy(socket, term->glyph_stroke,
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;
cairo_surface_destroy(surface);
/* Return glyph */
return location;
}
/**
* 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) {
guac_socket* socket = term->client->socket;
const guac_terminal_color* background_color;
int background, foreground;
/* Handle reverse video */
if (attributes->reverse != attributes->selected) {
background = attributes->foreground;
foreground = attributes->background;
}
else {
foreground = attributes->foreground;
background = attributes->background;
}
/* Handle bold */
if (attributes->bold && foreground <= 7)
foreground += 8;
/* Get background color */
background_color = &guac_terminal_palette[background];
/* If foreground different from current, colorize */
if (foreground != term->glyph_foreground) {
/* Get color */
const guac_terminal_color* color =
&guac_terminal_palette[foreground];
/* Colorize letter */
guac_protocol_send_rect(socket, term->glyph_stroke,
0, 0,
term->char_width * term->next_glyph, term->char_height);
guac_protocol_send_cfill(socket, GUAC_COMP_ATOP, term->glyph_stroke,
color->red,
color->green,
color->blue,
255);
}
/* If any color change at all, update filled */
if (foreground != term->glyph_foreground
|| background != term->glyph_background) {
/* Set background */
guac_protocol_send_rect(socket, term->filled_glyphs,
0, 0,
term->char_width * term->next_glyph, term->char_height);
guac_protocol_send_cfill(socket, GUAC_COMP_OVER, term->filled_glyphs,
background_color->red,
background_color->green,
background_color->blue,
255);
/* Copy stroke */
guac_protocol_send_copy(socket, term->glyph_stroke,
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;
return 0;
}
/**
* Sends the given character to the terminal at the given row and column,
* rendering the charater immediately. This bypasses the guac_terminal_delta
* mechanism and is intended for flushing of updates only.
*/
int __guac_terminal_set(guac_terminal* term, int row, int col, char c) {
guac_socket* socket = term->client->socket;
int location = __guac_terminal_get_glyph(term, c);
return guac_protocol_send_copy(socket,
term->filled_glyphs,
location * term->char_width, 0, term->char_width, term->char_height,
GUAC_COMP_OVER, GUAC_DEFAULT_LAYER,
term->char_width * col,
term->char_height * row);
}
int guac_terminal_set(guac_terminal* term, int row, int col, char c) { int guac_terminal_set(guac_terminal* term, int row, int col, char c) {
int scrolled_row = row + term->scroll_offset; int scrolled_row = row + term->scroll_offset;
@ -371,10 +125,10 @@ int guac_terminal_set(guac_terminal* term, int row, int col, char c) {
/* Set delta */ /* Set delta */
if (scrolled_row < term->delta->height) if (scrolled_row < term->delta->height)
guac_terminal_delta_set(term->delta, scrolled_row, col, &guac_char); guac_terminal_delta_set_columns(term->delta, scrolled_row, col, col, &guac_char);
/* Set buffer */ /* Set buffer */
guac_terminal_buffer_set(term->buffer, row, col, &guac_char); guac_terminal_buffer_set_columns(term->buffer, row, col, col, &guac_char);
return 0; return 0;
@ -569,54 +323,6 @@ int guac_terminal_clear_range(guac_terminal* term,
} }
guac_terminal_delta* guac_terminal_delta_alloc(int width, int height) {
guac_terminal_operation* current;
int x, y;
/* Allocate delta */
guac_terminal_delta* delta = malloc(sizeof(guac_terminal_delta));
/* Set width and height */
delta->width = width;
delta->height = height;
/* Alloc operations */
delta->operations = malloc(width * height *
sizeof(guac_terminal_operation));
/* Init each operation buffer row */
current = delta->operations;
for (y=0; y<height; y++) {
/* Init entire row to NOP */
for (x=0; x<width; x++)
(current++)->type = GUAC_CHAR_NOP;
}
/* Alloc scratch area */
delta->scratch = malloc(width * height * sizeof(guac_terminal_operation));
return delta;
}
void guac_terminal_delta_free(guac_terminal_delta* delta) {
/* Free operations buffers */
free(delta->operations);
free(delta->scratch);
/* Free delta */
free(delta);
}
void guac_terminal_delta_resize(guac_terminal_delta* delta,
int width, int height) {
/* STUB */
}
void guac_terminal_delta_set(guac_terminal_delta* delta, int r, int c, void guac_terminal_delta_set(guac_terminal_delta* delta, int r, int c,
guac_terminal_char* character) { guac_terminal_char* character) {
@ -714,474 +420,6 @@ void guac_terminal_delta_set_rect(guac_terminal_delta* delta,
} }
void __guac_terminal_delta_flush_copy(guac_terminal_delta* delta,
guac_terminal* terminal) {
guac_terminal_operation* current = delta->operations;
int row, col;
/* For each operation */
for (row=0; row<delta->height; row++) {
for (col=0; col<delta->width; col++) {
/* If operation is a copy operation */
if (current->type == GUAC_CHAR_COPY) {
/* 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 */
guac_terminal_operation* rect_current_row;
/* Determine bounds of rectangle */
rect_current_row = current;
expected_row = current->row;
for (rect_row=row; rect_row<delta->height; rect_row++) {
guac_terminal_operation* rect_current = rect_current_row;
expected_col = current->column;
/* Find width */
for (rect_col=col; rect_col<delta->width; rect_col++) {
/* If not identical operation, stop */
if (rect_current->type != GUAC_CHAR_COPY
|| rect_current->row != expected_row
|| rect_current->column != expected_col)
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 */
rect_current_row += delta->width;
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;
expected_row = current->row;
for (rect_row=0; rect_row<rect_height; rect_row++) {
guac_terminal_operation* rect_current = rect_current_row;
expected_col = current->column;
for (rect_col=0; rect_col<rect_width; rect_col++) {
/* Mark copy operations as NOP */
if (rect_current->type == GUAC_CHAR_COPY
&& rect_current->row == expected_row
&& rect_current->column == expected_col)
rect_current->type = GUAC_CHAR_NOP;
/* Next column */
rect_current++;
expected_col++;
}
/* Next row */
rect_current_row += delta->width;
expected_row++;
}
/* Send copy */
guac_protocol_send_copy(terminal->client->socket,
GUAC_DEFAULT_LAYER,
current->column * terminal->char_width,
current->row * terminal->char_height,
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++;
}
}
}
void __guac_terminal_delta_flush_clear(guac_terminal_delta* delta,
guac_terminal* terminal) {
guac_terminal_operation* current = delta->operations;
int row, col;
/* For each operation */
for (row=0; row<delta->height; row++) {
for (col=0; col<delta->width; col++) {
/* If operation is a cler operation (set to space) */
if (current->type == GUAC_CHAR_SET &&
current->character.value == ' ') {
/* 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 */
int color;
if (current->character.attributes.reverse != current->character.attributes.selected)
color = current->character.attributes.foreground;
else
color = current->character.attributes.background;
const guac_terminal_color* guac_color =
&guac_terminal_palette[color];
/* Current row within a subrect */
guac_terminal_operation* rect_current_row;
/* Determine bounds of rectangle */
rect_current_row = current;
for (rect_row=row; rect_row<delta->height; rect_row++) {
guac_terminal_operation* rect_current = rect_current_row;
/* Find width */
for (rect_col=col; rect_col<delta->width; rect_col++) {
int joining_color;
if (rect_current->character.attributes.reverse)
joining_color = current->character.attributes.foreground;
else
joining_color = current->character.attributes.background;
/* If not identical operation, stop */
if (rect_current->type != GUAC_CHAR_SET
|| rect_current->character.value != ' '
|| joining_color != color)
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 */
rect_current_row += delta->width;
}
/* 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++) {
guac_terminal_operation* rect_current = rect_current_row;
for (rect_col=0; rect_col<rect_width; rect_col++) {
int joining_color;
if (rect_current->character.attributes.reverse)
joining_color = current->character.attributes.foreground;
else
joining_color = current->character.attributes.background;
/* Mark clear operations as NOP */
if (rect_current->type == GUAC_CHAR_SET
&& rect_current->character.value == ' '
&& joining_color == color)
rect_current->type = GUAC_CHAR_NOP;
/* Next column */
rect_current++;
}
/* Next row */
rect_current_row += delta->width;
}
/* 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);
} /* end if clear operation */
/* Next operation */
current++;
}
}
}
void __guac_terminal_delta_flush_set(guac_terminal_delta* delta,
guac_terminal* terminal) {
guac_terminal_operation* current = delta->operations;
int row, col;
/* For each operation */
for (row=0; row<delta->height; row++) {
for (col=0; col<delta->width; col++) {
/* Perform given operation */
if (current->type == GUAC_CHAR_SET) {
/* Set attributes */
__guac_terminal_set_colors(terminal,
&(current->character.attributes));
/* Send character */
__guac_terminal_set(terminal, row, col,
current->character.value);
/* Mark operation as handled */
current->type = GUAC_CHAR_NOP;
}
/* Next operation */
current++;
}
}
}
void guac_terminal_delta_flush(guac_terminal_delta* delta,
guac_terminal* terminal) {
/* Flush copy operations first */
__guac_terminal_delta_flush_copy(delta, terminal);
/* Flush clear operations (as they're just rects) */
__guac_terminal_delta_flush_clear(delta, terminal);
/* Flush set operations (the only operations remaining) */
__guac_terminal_delta_flush_set(delta, terminal);
}
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) {
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;
}
}
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;
}
}
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( void guac_terminal_scrollback_buffer_append(
guac_terminal_scrollback_buffer* buffer, guac_terminal_scrollback_buffer* buffer,
guac_terminal* terminal, int rows) { guac_terminal* terminal, int rows) {