GUACAMOLE-1538: Use an options struct instead of hardcoding params in constructor.

This commit is contained in:
James Muehlner 2022-02-17 14:16:05 -08:00
parent 05c6131cf3
commit 44d76da21a
11 changed files with 261 additions and 152 deletions

View File

@ -239,12 +239,24 @@ void* guac_kubernetes_client_thread(void* data) {
settings->recording_include_keys);
}
/* Create terminal options with required parameters */
guac_terminal_options* options = guac_terminal_options_create(
client, kubernetes_client->clipboard,
settings->width, settings->height, settings->resolution);
/* Set optional parameters */
options->disable_copy = settings->disable_copy;
options->max_scrollback = settings->max_scrollback;
options->font_name = settings->font_name;
options->font_size = settings->font_size;
options->color_scheme = settings->color_scheme;
options->backspace = settings->backspace;
/* Create terminal */
kubernetes_client->term = guac_terminal_create(client,
kubernetes_client->clipboard, settings->disable_copy,
settings->max_scrollback, settings->font_name, settings->font_size,
settings->resolution, settings->width, settings->height,
settings->color_scheme, settings->backspace);
kubernetes_client->term = guac_terminal_create(options);
/* Free options struct now that it's been used */
free(options);
/* Fail if terminal init failed */
if (kubernetes_client->term == NULL) {

View File

@ -19,6 +19,7 @@
#include "argv.h"
#include "settings.h"
#include "terminal/terminal.h"
#include <guacamole/user.h>
@ -215,8 +216,8 @@ enum KUBERNETES_ARGS_IDX {
IDX_READ_ONLY,
/**
* ASCII code, as an integer to use for the backspace key, or 127
* if not specified.
* ASCII code, as an integer to use for the backspace key, or
* GUAC_TERMINAL_DEFAULT_BACKSPACE if not specified.
*/
IDX_BACKSPACE,
@ -320,22 +321,22 @@ guac_kubernetes_settings* guac_kubernetes_parse_args(guac_user* user,
/* Read maximum scrollback size */
settings->max_scrollback =
guac_user_parse_args_int(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_SCROLLBACK, GUAC_KUBERNETES_DEFAULT_MAX_SCROLLBACK);
IDX_SCROLLBACK, GUAC_TERMINAL_DEFAULT_MAX_SCROLLBACK);
/* Read font name */
settings->font_name =
guac_user_parse_args_string(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_FONT_NAME, GUAC_KUBERNETES_DEFAULT_FONT_NAME);
IDX_FONT_NAME, GUAC_TERMINAL_DEFAULT_FONT_NAME);
/* Read font size */
settings->font_size =
guac_user_parse_args_int(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_FONT_SIZE, GUAC_KUBERNETES_DEFAULT_FONT_SIZE);
IDX_FONT_SIZE, GUAC_TERMINAL_DEFAULT_FONT_SIZE);
/* Copy requested color scheme */
settings->color_scheme =
guac_user_parse_args_string(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_COLOR_SCHEME, "");
IDX_COLOR_SCHEME, GUAC_TERMINAL_DEFAULT_COLOR_SCHEME);
/* Pull width/height/resolution directly from user */
settings->width = user->info.optimal_width;
@ -390,7 +391,7 @@ guac_kubernetes_settings* guac_kubernetes_parse_args(guac_user* user,
/* Parse backspace key code */
settings->backspace =
guac_user_parse_args_int(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_BACKSPACE, 127);
IDX_BACKSPACE, GUAC_TERMINAL_DEFAULT_BACKSPACE);
/* Parse clipboard copy disable flag */
settings->disable_copy =

View File

@ -24,17 +24,6 @@
#include <stdbool.h>
/**
* The name of the font to use for the terminal if no name is specified.
*/
#define GUAC_KUBERNETES_DEFAULT_FONT_NAME "monospace"
/**
* The size of the font to use for the terminal if no font size is specified,
* in points.
*/
#define GUAC_KUBERNETES_DEFAULT_FONT_SIZE 12
/**
* The port to connect to when initiating any Kubernetes connection, if no
* other port is specified.
@ -57,11 +46,6 @@
*/
#define GUAC_KUBERNETES_DEFAULT_RECORDING_NAME "recording"
/**
* The default maximum scrollback size in rows.
*/
#define GUAC_KUBERNETES_DEFAULT_MAX_SCROLLBACK 1000
/**
* Settings for the Kubernetes connection. The values for this structure are
* parsed from the arguments given during the Guacamole protocol handshake

View File

@ -23,6 +23,7 @@
#include "client.h"
#include "common/defaults.h"
#include "settings.h"
#include "terminal/terminal.h"
#include <guacamole/user.h>
#include <guacamole/wol-constants.h>
@ -248,8 +249,8 @@ enum SSH_ARGS_IDX {
/**
* The ASCII code, as an integer, to send for the backspace key, as configured
* by the SSH connection from the client. By default this will be 127,
* the ASCII DELETE code.
* by the SSH connection from the client. By default this will be
* GUAC_TERMINAL_DEFAULT_BACKSPACE.
*/
IDX_BACKSPACE,
@ -375,22 +376,22 @@ guac_ssh_settings* guac_ssh_parse_args(guac_user* user,
/* Read maximum scrollback size */
settings->max_scrollback =
guac_user_parse_args_int(user, GUAC_SSH_CLIENT_ARGS, argv,
IDX_SCROLLBACK, GUAC_SSH_DEFAULT_MAX_SCROLLBACK);
IDX_SCROLLBACK, GUAC_TERMINAL_DEFAULT_MAX_SCROLLBACK);
/* Read font name */
settings->font_name =
guac_user_parse_args_string(user, GUAC_SSH_CLIENT_ARGS, argv,
IDX_FONT_NAME, GUAC_SSH_DEFAULT_FONT_NAME);
IDX_FONT_NAME, GUAC_TERMINAL_DEFAULT_FONT_NAME);
/* Read font size */
settings->font_size =
guac_user_parse_args_int(user, GUAC_SSH_CLIENT_ARGS, argv,
IDX_FONT_SIZE, GUAC_SSH_DEFAULT_FONT_SIZE);
IDX_FONT_SIZE, GUAC_TERMINAL_DEFAULT_FONT_SIZE);
/* Copy requested color scheme */
settings->color_scheme =
guac_user_parse_args_string(user, GUAC_SSH_CLIENT_ARGS, argv,
IDX_COLOR_SCHEME, "");
IDX_COLOR_SCHEME, GUAC_TERMINAL_DEFAULT_COLOR_SCHEME);
/* Pull width/height/resolution directly from user */
settings->width = user->info.optimal_width;
@ -491,7 +492,7 @@ guac_ssh_settings* guac_ssh_parse_args(guac_user* user,
/* Parse backspace key setting */
settings->backspace =
guac_user_parse_args_int(user, GUAC_SSH_CLIENT_ARGS, argv,
IDX_BACKSPACE, 127);
IDX_BACKSPACE, GUAC_TERMINAL_DEFAULT_BACKSPACE);
/* Read terminal emulator type. */
settings->terminal_type =

View File

@ -26,17 +26,6 @@
#include <stdbool.h>
/**
* The name of the font to use for the terminal if no name is specified.
*/
#define GUAC_SSH_DEFAULT_FONT_NAME "monospace"
/**
* The size of the font to use for the terminal if no font size is specified,
* in points.
*/
#define GUAC_SSH_DEFAULT_FONT_SIZE 12
/**
* The port to connect to when initiating any SSH connection, if no other port
* is specified.
@ -58,11 +47,6 @@
*/
#define GUAC_SSH_DEFAULT_POLL_TIMEOUT 1000
/**
* The default maximum scrollback size in rows.
*/
#define GUAC_SSH_DEFAULT_MAX_SCROLLBACK 1000
/**
* Settings for the SSH connection. The values for this structure are parsed
* from the arguments given during the Guacamole protocol handshake using the

View File

@ -238,12 +238,24 @@ void* ssh_client_thread(void* data) {
settings->recording_include_keys);
}
/* Create terminal options with required parameters */
guac_terminal_options* options = guac_terminal_options_create(
client, ssh_client->clipboard,
settings->width, settings->height, settings->resolution);
/* Set optional parameters */
options->disable_copy = settings->disable_copy;
options->max_scrollback = settings->max_scrollback;
options->font_name = settings->font_name;
options->font_size = settings->font_size;
options->color_scheme = settings->color_scheme;
options->backspace = settings->backspace;
/* Create terminal */
ssh_client->term = guac_terminal_create(client, ssh_client->clipboard,
settings->disable_copy, settings->max_scrollback,
settings->font_name, settings->font_size, settings->resolution,
settings->width, settings->height, settings->color_scheme,
settings->backspace);
ssh_client->term = guac_terminal_create(options);
/* Free options struct now that it's been used */
free(options);
/* Fail if terminal init failed */
if (ssh_client->term == NULL) {

View File

@ -22,6 +22,7 @@
#include "argv.h"
#include "common/defaults.h"
#include "settings.h"
#include "terminal/terminal.h"
#include <guacamole/user.h>
#include <guacamole/wol-constants.h>
@ -192,8 +193,8 @@ enum TELNET_ARGS_IDX {
IDX_READ_ONLY,
/**
* ASCII code, as an integer to use for the backspace key, or 127
* if not specified.
* ASCII code, as an integer to use for the backspace key, or
* GUAC_TERMINAL_DEFAULT_BACKSPACE if not specified.
*/
IDX_BACKSPACE,
@ -401,22 +402,22 @@ guac_telnet_settings* guac_telnet_parse_args(guac_user* user,
/* Read maximum scrollback size */
settings->max_scrollback =
guac_user_parse_args_int(user, GUAC_TELNET_CLIENT_ARGS, argv,
IDX_SCROLLBACK, GUAC_TELNET_DEFAULT_MAX_SCROLLBACK);
IDX_SCROLLBACK, GUAC_TERMINAL_DEFAULT_MAX_SCROLLBACK);
/* Read font name */
settings->font_name =
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
IDX_FONT_NAME, GUAC_TELNET_DEFAULT_FONT_NAME);
IDX_FONT_NAME, GUAC_TERMINAL_DEFAULT_FONT_NAME);
/* Read font size */
settings->font_size =
guac_user_parse_args_int(user, GUAC_TELNET_CLIENT_ARGS, argv,
IDX_FONT_SIZE, GUAC_TELNET_DEFAULT_FONT_SIZE);
IDX_FONT_SIZE, GUAC_TERMINAL_DEFAULT_FONT_SIZE);
/* Copy requested color scheme */
settings->color_scheme =
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
IDX_COLOR_SCHEME, "");
IDX_COLOR_SCHEME, GUAC_TERMINAL_DEFAULT_COLOR_SCHEME);
/* Pull width/height/resolution directly from user */
settings->width = user->info.optimal_width;
@ -476,7 +477,7 @@ guac_telnet_settings* guac_telnet_parse_args(guac_user* user,
/* Parse backspace key code */
settings->backspace =
guac_user_parse_args_int(user, GUAC_TELNET_CLIENT_ARGS, argv,
IDX_BACKSPACE, 127);
IDX_BACKSPACE, GUAC_TERMINAL_DEFAULT_BACKSPACE);
/* Read terminal emulator type. */
settings->terminal_type =

View File

@ -27,18 +27,6 @@
#include <sys/types.h>
#include <regex.h>
#include <stdbool.h>
/**
* The name of the font to use for the terminal if no name is specified.
*/
#define GUAC_TELNET_DEFAULT_FONT_NAME "monospace"
/**
* The size of the font to use for the terminal if no font size is specified,
* in points.
*/
#define GUAC_TELNET_DEFAULT_FONT_SIZE 12
/**
* The port to connect to when initiating any telnet connection, if no other
* port is specified.
@ -67,11 +55,6 @@
*/
#define GUAC_TELNET_DEFAULT_PASSWORD_REGEX "[Pp]assword:"
/**
* The default maximum scrollback size in rows.
*/
#define GUAC_TELNET_DEFAULT_MAX_SCROLLBACK 1000
/**
* Settings for the telnet connection. The values for this structure are parsed
* from the arguments given during the Guacamole protocol handshake using the

View File

@ -585,12 +585,24 @@ void* guac_telnet_client_thread(void* data) {
settings->recording_include_keys);
}
/* Create terminal options with required parameters */
guac_terminal_options* options = guac_terminal_options_create(
client, telnet_client->clipboard,
settings->width, settings->height, settings->resolution);
/* Set optional parameters */
options->disable_copy = settings->disable_copy;
options->max_scrollback = settings->max_scrollback;
options->font_name = settings->font_name;
options->font_size = settings->font_size;
options->color_scheme = settings->color_scheme;
options->backspace = settings->backspace;
/* Create terminal */
telnet_client->term = guac_terminal_create(client,
telnet_client->clipboard, settings->disable_copy,
settings->max_scrollback, settings->font_name, settings->font_size,
settings->resolution, settings->width, settings->height,
settings->color_scheme, settings->backspace);
telnet_client->term = guac_terminal_create(options);
/* Free options struct now that it's been used */
free(options);
/* Fail if terminal init failed */
if (telnet_client->term == NULL) {

View File

@ -306,11 +306,35 @@ void* guac_terminal_thread(void* data) {
}
guac_terminal* guac_terminal_create(guac_client* client,
guac_common_clipboard* clipboard, bool disable_copy,
int max_scrollback, const char* font_name, int font_size, int dpi,
int width, int height, const char* color_scheme,
const int backspace) {
guac_terminal_options* guac_terminal_options_create(guac_client* client,
guac_common_clipboard* clipboard, int width, int height, int dpi) {
guac_terminal_options* options = malloc(sizeof(guac_terminal_options));
/* Set all required parameters */
options->client = client;
options->clipboard = clipboard;
options->width = width;
options->height = height;
options->dpi = dpi;
/* Set default values for all other parameters */
options->disable_copy = GUAC_TERMINAL_DEFAULT_DISABLE_COPY;
options->max_scrollback = GUAC_TERMINAL_DEFAULT_MAX_SCROLLBACK;
options->font_name = GUAC_TERMINAL_DEFAULT_FONT_NAME;
options->font_size = GUAC_TERMINAL_DEFAULT_FONT_SIZE;
options->color_scheme = GUAC_TERMINAL_DEFAULT_COLOR_SCHEME;
options->backspace = GUAC_TERMINAL_DEFAULT_BACKSPACE;
return options;
}
guac_terminal* guac_terminal_create(guac_terminal_options* options) {
/* The width and height may need to be changed from what's requested */
int width = options->width;
int height = options->height;
/* Build default character using default colors */
guac_terminal_char default_char = {
@ -328,7 +352,7 @@ guac_terminal* guac_terminal_create(guac_client* client,
guac_terminal_color (*default_palette)[256] = (guac_terminal_color(*)[256])
malloc(sizeof(guac_terminal_color[256]));
guac_terminal_parse_color_scheme(client, color_scheme,
guac_terminal_parse_color_scheme(options->client, options->color_scheme,
&default_char.attributes.foreground,
&default_char.attributes.background,
default_palette);
@ -340,14 +364,14 @@ guac_terminal* guac_terminal_create(guac_client* client,
guac_terminal* term = malloc(sizeof(guac_terminal));
term->started = false;
term->client = client;
term->client = options->client;
term->upload_path_handler = NULL;
term->file_download_handler = NULL;
/* Copy initially-provided color scheme and font details */
term->color_scheme = strdup(color_scheme);
term->font_name = strdup(font_name);
term->font_size = font_size;
term->color_scheme = strdup(options->color_scheme);
term->font_name = strdup(options->font_name);
term->font_size = options->font_size;
/* Set size of available screen area */
term->outer_width = width;
@ -359,12 +383,12 @@ guac_terminal* guac_terminal_create(guac_client* client,
pthread_mutex_init(&(term->modified_lock), NULL);
/* Maximum and requested scrollback are initially the same */
term->max_scrollback = max_scrollback;
term->requested_scrollback = max_scrollback;
term->max_scrollback = options->max_scrollback;
term->requested_scrollback = options->max_scrollback;
/* Allocate enough space for maximum scrollback, bumping up internal
* storage as necessary to allow screen to be resized to maximum height */
int initial_scrollback = max_scrollback;
int initial_scrollback = options->max_scrollback;
if (initial_scrollback < GUAC_TERMINAL_MAX_ROWS)
initial_scrollback = GUAC_TERMINAL_MAX_ROWS;
@ -373,27 +397,27 @@ guac_terminal* guac_terminal_create(guac_client* client,
&default_char);
/* Init display */
term->display = guac_terminal_display_alloc(client,
font_name, font_size, dpi,
term->display = guac_terminal_display_alloc(options->client,
options->font_name, options->font_size, options->dpi,
&default_char.attributes.foreground,
&default_char.attributes.background,
(guac_terminal_color(*)[256]) default_palette);
/* Fail if display init failed */
if (term->display == NULL) {
guac_client_log(client, GUAC_LOG_DEBUG, "Display initialization failed");
guac_client_log(options->client, GUAC_LOG_DEBUG, "Display initialization failed");
free(term);
return NULL;
}
/* Init common cursor */
term->cursor = guac_common_cursor_alloc(client);
term->cursor = guac_common_cursor_alloc(options->client);
/* Init terminal state */
term->current_attributes = default_char.attributes;
term->default_char = default_char;
term->clipboard = clipboard;
term->disable_copy = disable_copy;
term->clipboard = options->clipboard;
term->disable_copy = options->disable_copy;
/* Calculate character size */
int rows = height / term->display->char_height;
@ -440,12 +464,12 @@ guac_terminal* guac_terminal_create(guac_client* client,
pthread_mutex_init(&(term->lock), NULL);
/* Repaint and resize overall display */
guac_terminal_repaint_default_layer(term, client->socket);
guac_terminal_repaint_default_layer(term, term->client->socket);
guac_terminal_display_resize(term->display,
term->term_width, term->term_height);
/* Allocate scrollbar */
term->scrollbar = guac_terminal_scrollbar_alloc(client, GUAC_DEFAULT_LAYER,
term->scrollbar = guac_terminal_scrollbar_alloc(term->client, GUAC_DEFAULT_LAYER,
width, height, term->term_height);
/* Associate scrollbar with this terminal */
@ -471,7 +495,7 @@ guac_terminal* guac_terminal_create(guac_client* client,
}
/* Configure backspace */
term->backspace = backspace;
term->backspace = options->backspace;
return term;

View File

@ -37,6 +37,37 @@
#include <guacamole/client.h>
#include <guacamole/stream.h>
/**
* The name of the font to use for the terminal if no name is specified.
*/
#define GUAC_TERMINAL_DEFAULT_FONT_NAME "monospace"
/**
* The size of the font to use for the terminal if no font size is specified,
* in points.
*/
#define GUAC_TERMINAL_DEFAULT_FONT_SIZE 12
/**
* The default maximum scrollback size in rows.
*/
#define GUAC_TERMINAL_DEFAULT_MAX_SCROLLBACK 1000
/**
* The default ASCII code to use for the backspace key.
*/
#define GUAC_TERMINAL_DEFAULT_BACKSPACE 127
/**
* The default (unset) color scheme.
*/
#define GUAC_TERMINAL_DEFAULT_COLOR_SCHEME ""
/**
* The default value for the "disable copy" flag; by default copying is enabled.
*/
#define GUAC_TERMINAL_DEFAULT_DISABLE_COPY false
/**
* The absolute maximum number of rows to allow within the display.
*/
@ -547,6 +578,88 @@ struct guac_terminal {
};
/**
* Configuration options that may be passed when creating a new guac_terminal.
*
* Note that guac_terminal_options should not be instantiated directly -
* to create a new options struct, use guac_terminal_options_create.
*/
typedef struct guac_terminal_options {
/**
* The client to which the terminal will be rendered.
*/
guac_client* client;
/**
* The guac_common_clipboard which will contain the current clipboard
* state. It is expected that this clipboard instance will be updated
* both internally by the terminal and externally through received
* clipboard instructions. This clipboard will not be automatically
* freed when this terminal is freed.
*/
guac_common_clipboard* clipboard;
/**
* Whether copying from the terminal clipboard should be blocked. If set,
* the contents of the terminal can still be copied, but will be usable
* only within the terminal itself. The clipboard contents will not be
* automatically streamed to the client.
*/
bool disable_copy;
/**
* The maximum number of rows to allow within the scrollback buffer. The
* user may still alter the size of the scrollback buffer using terminal
* codes, however the size can never exceed the maximum size given here.
* Note that this space is shared with the display, with the scrollable
* area actually only containing the given number of rows less the number
* of rows currently displayed, and sufficient buffer space will always be
* allocated to represent the display area of the terminal regardless of
* the value given here.
*/
int max_scrollback;
/**
* The name of the font to use when rendering glyphs.
*/
char* font_name;
/**
* The size of each glyph, in points.
*/
int font_size;
/**
* The DPI of the display. The given font size will be adjusted to produce
* glyphs at the given DPI.
*/
int dpi;
/**
* The width of the terminal, in pixels.
*/
int width;
/**
* The height of the terminal, in pixels.
*/
int height;
/**
* The name of the color scheme to use. This string must be in the format
* accepted by guac_terminal_parse_color_scheme().
*/
char* color_scheme;
/**
* The integer ASCII code to send when backspace is pressed in
* the terminal.
*/
int backspace;
} guac_terminal_options;
/**
* Creates a new guac_terminal, having the given width and height, and
* rendering to the given client. As failover mechanisms and the Guacamole
@ -557,41 +670,30 @@ struct guac_terminal {
* either the underlying connection has truly succeeded, or until visible
* terminal output or user input is required.
*
* @param client
* @param terminal_options
* The configuration used for instantiating the terminal. For information
* about the options, see the guac_terminal_options definition.
*
* @return
* A new guac_terminal having the given font, dimensions, and attributes
* which renders all text to the given client.
*/
guac_terminal* guac_terminal_create(guac_terminal_options* terminal_options);
/**
* Create a new guac_terminal_options struct. All parameters are required.
* Any options that are not passed in this constructor will be set to
* default values unless overriden.
*
* The guac_terminal_options struct should only be created using this
* constructor.
*
* @param guac_client
* The client to which the terminal will be rendered.
*
* @param clipboard
* The guac_common_clipboard which will contain the current clipboard
* state. It is expected that this clipboard instance will be updated
* both internally by the terminal and externally through received
* clipboard instructions. This clipboard will not be automatically
* freed when this terminal is freed.
*
* @param disable_copy
* Whether copying from the terminal clipboard should be blocked. If set,
* the contents of the terminal can still be copied, but will be usable
* only within the terminal itself. The clipboard contents will not be
* automatically streamed to the client.
*
* @param max_scrollback
* The maximum number of rows to allow within the scrollback buffer. The
* user may still alter the size of the scrollback buffer using terminal
* codes, however the size can never exceed the maximum size given here.
* Note that this space is shared with the display, with the scrollable
* area actually only containing the given number of rows less the number
* of rows currently displayed, and sufficient buffer space will always be
* allocated to represent the display area of the terminal regardless of
* the value given here.
*
* @param font_name
* The name of the font to use when rendering glyphs.
*
* @param font_size
* The size of each glyph, in points.
*
* @param dpi
* The DPI of the display. The given font size will be adjusted to produce
* glyphs at the given DPI.
* state.
*
* @param width
* The width of the terminal, in pixels.
@ -599,23 +701,16 @@ struct guac_terminal {
* @param height
* The height of the terminal, in pixels.
*
* @param color_scheme
* The name of the color scheme to use. This string must be in the format
* accepted by guac_terminal_parse_color_scheme().
*
* @param backspace
* The integer ASCII code to send when backspace is pressed in
* this terminal.
* @param dpi
* The DPI of the display. The given font size will be adjusted to produce
* glyphs at the given DPI.
*
* @return
* A new guac_terminal having the given font, dimensions, and attributes
* which renders all text to the given client.
* A new terminal options struct with all required options populated,
* ready to have any defaults overriden as needed.
*/
guac_terminal* guac_terminal_create(guac_client* client,
guac_common_clipboard* clipboard, bool disable_copy,
int max_scrollback, const char* font_name, int font_size, int dpi,
int width, int height, const char* color_scheme,
const int backspace);
guac_terminal_options* guac_terminal_options_create(guac_client* client,
guac_common_clipboard* clipboard, int width, int height, int dpi);
/**
* Frees all resources associated with the given terminal.