GUACAMOLE-630: Merge allow color scheme of active terminals to be changed.

This commit is contained in:
Nick Couchman 2018-11-10 10:45:56 -05:00
commit d2cb7a9ce9
17 changed files with 796 additions and 288 deletions

View File

@ -29,6 +29,7 @@ ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libguac-client-ssh.la
libguac_client_ssh_la_SOURCES = \
argv.c \
client.c \
clipboard.c \
input.c \
@ -40,6 +41,7 @@ libguac_client_ssh_la_SOURCES = \
user.c
noinst_HEADERS = \
argv.h \
client.h \
clipboard.h \
input.h \

128
src/protocols/ssh/argv.c Normal file
View File

@ -0,0 +1,128 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "config.h"
#include "argv.h"
#include "ssh.h"
#include "terminal/terminal.h"
#include <guacamole/protocol.h>
#include <guacamole/socket.h>
#include <guacamole/user.h>
#include <stdlib.h>
#include <string.h>
/**
* The value or current status of a connection parameter received over an
* "argv" stream.
*/
typedef struct guac_ssh_argv {
/**
* Buffer space for containing the received argument value.
*/
char buffer[GUAC_SSH_ARGV_MAX_LENGTH];
/**
* The number of bytes received so far.
*/
int length;
} guac_ssh_argv;
/**
* Handler for "blob" instructions which appends the data from received blobs
* to the end of the in-progress argument value buffer.
*
* @see guac_user_blob_handler
*/
static int guac_ssh_argv_blob_handler(guac_user* user,
guac_stream* stream, void* data, int length) {
guac_ssh_argv* argv = (guac_ssh_argv*) stream->data;
/* Calculate buffer size remaining, including space for null terminator,
* adjusting received length accordingly */
int remaining = sizeof(argv->buffer) - argv->length - 1;
if (length > remaining)
length = remaining;
/* Append received data to end of buffer */
memcpy(argv->buffer + argv->length, data, length);
argv->length += length;
return 0;
}
/**
* Handler for "end" instructions which applies the changes specified by the
* argument value buffer associated with the stream.
*
* @see guac_user_end_handler
*/
static int guac_ssh_argv_end_handler(guac_user* user,
guac_stream* stream) {
guac_client* client = user->client;
guac_ssh_client* telnet_client = (guac_ssh_client*) client->data;
guac_terminal* terminal = telnet_client->term;
/* Append null terminator to value */
guac_ssh_argv* argv = (guac_ssh_argv*) stream->data;
argv->buffer[argv->length] = '\0';
/* Update color scheme */
guac_terminal_apply_color_scheme(terminal, argv->buffer);
free(argv);
return 0;
}
int guac_ssh_argv_handler(guac_user* user, guac_stream* stream,
char* mimetype, char* name) {
/* Allow users to update the color scheme */
if (strcmp(name, "color-scheme") == 0) {
guac_ssh_argv* argv = malloc(sizeof(guac_ssh_argv));
argv->length = 0;
/* Prepare stream to receive argument value */
stream->blob_handler = guac_ssh_argv_blob_handler;
stream->end_handler = guac_ssh_argv_end_handler;
stream->data = argv;
/* Signal stream is ready */
guac_protocol_send_ack(user->socket, stream, "Ready for color "
"scheme.", GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(user->socket);
return 0;
}
/* No other connection parameters may be updated */
guac_protocol_send_ack(user->socket, stream, "Not allowed.",
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
guac_socket_flush(user->socket);
return 0;
}

41
src/protocols/ssh/argv.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef GUAC_SSH_ARGV_H
#define GUAC_SSH_ARGV_H
#include "config.h"
#include <guacamole/user.h>
/**
* The maximum number of bytes to allow for any argument value received via an
* argv stream, including null terminator.
*/
#define GUAC_SSH_ARGV_MAX_LENGTH 16384
/**
* Handles an incoming stream from a Guacamole "argv" instruction, updating the
* given connection parameter if that parameter is allowed to be updated.
*/
guac_user_argv_handler guac_ssh_argv_handler;
#endif

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "argv.h"
#include "clipboard.h"
#include "common/display.h"
#include "input.h"
@ -87,6 +88,9 @@ int guac_ssh_user_join_handler(guac_user* user, int argc, char** argv) {
/* STDIN redirection */
user->pipe_handler = guac_ssh_pipe_handler;
/* Updates to connection parameters */
user->argv_handler = guac_ssh_argv_handler;
/* Display size change events */
user->size_handler = guac_ssh_user_size_handler;

View File

@ -29,6 +29,7 @@ ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libguac-client-telnet.la
libguac_client_telnet_la_SOURCES = \
argv.c \
client.c \
clipboard.c \
input.c \
@ -38,6 +39,7 @@ libguac_client_telnet_la_SOURCES = \
user.c
noinst_HEADERS = \
argv.h \
client.h \
clipboard.h \
input.h \

128
src/protocols/telnet/argv.c Normal file
View File

@ -0,0 +1,128 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "config.h"
#include "argv.h"
#include "telnet.h"
#include "terminal/terminal.h"
#include <guacamole/protocol.h>
#include <guacamole/socket.h>
#include <guacamole/user.h>
#include <stdlib.h>
#include <string.h>
/**
* The value or current status of a connection parameter received over an
* "argv" stream.
*/
typedef struct guac_telnet_argv {
/**
* Buffer space for containing the received argument value.
*/
char buffer[GUAC_TELNET_ARGV_MAX_LENGTH];
/**
* The number of bytes received so far.
*/
int length;
} guac_telnet_argv;
/**
* Handler for "blob" instructions which appends the data from received blobs
* to the end of the in-progress argument value buffer.
*
* @see guac_user_blob_handler
*/
static int guac_telnet_argv_blob_handler(guac_user* user,
guac_stream* stream, void* data, int length) {
guac_telnet_argv* argv = (guac_telnet_argv*) stream->data;
/* Calculate buffer size remaining, including space for null terminator,
* adjusting received length accordingly */
int remaining = sizeof(argv->buffer) - argv->length - 1;
if (length > remaining)
length = remaining;
/* Append received data to end of buffer */
memcpy(argv->buffer + argv->length, data, length);
argv->length += length;
return 0;
}
/**
* Handler for "end" instructions which applies the changes specified by the
* argument value buffer associated with the stream.
*
* @see guac_user_end_handler
*/
static int guac_telnet_argv_end_handler(guac_user* user,
guac_stream* stream) {
guac_client* client = user->client;
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
guac_terminal* terminal = telnet_client->term;
/* Append null terminator to value */
guac_telnet_argv* argv = (guac_telnet_argv*) stream->data;
argv->buffer[argv->length] = '\0';
/* Update color scheme */
guac_terminal_apply_color_scheme(terminal, argv->buffer);
free(argv);
return 0;
}
int guac_telnet_argv_handler(guac_user* user, guac_stream* stream,
char* mimetype, char* name) {
/* Allow users to update the color scheme */
if (strcmp(name, "color-scheme") == 0) {
guac_telnet_argv* argv = malloc(sizeof(guac_telnet_argv));
argv->length = 0;
/* Prepare stream to receive argument value */
stream->blob_handler = guac_telnet_argv_blob_handler;
stream->end_handler = guac_telnet_argv_end_handler;
stream->data = argv;
/* Signal stream is ready */
guac_protocol_send_ack(user->socket, stream, "Ready for color "
"scheme.", GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(user->socket);
return 0;
}
/* No other connection parameters may be updated */
guac_protocol_send_ack(user->socket, stream, "Not allowed.",
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
guac_socket_flush(user->socket);
return 0;
}

View File

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef GUAC_TELNET_ARGV_H
#define GUAC_TELNET_ARGV_H
#include "config.h"
#include <guacamole/user.h>
/**
* The maximum number of bytes to allow for any argument value received via an
* argv stream, including null terminator.
*/
#define GUAC_TELNET_ARGV_MAX_LENGTH 16384
/**
* Handles an incoming stream from a Guacamole "argv" instruction, updating the
* given connection parameter if that parameter is allowed to be updated.
*/
guac_user_argv_handler guac_telnet_argv_handler;
#endif

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "argv.h"
#include "clipboard.h"
#include "input.h"
#include "pipe.h"
@ -86,6 +87,9 @@ int guac_telnet_user_join_handler(guac_user* user, int argc, char** argv) {
/* STDIN redirection */
user->pipe_handler = guac_telnet_pipe_handler;
/* Updates to connection parameters */
user->argv_handler = guac_telnet_argv_handler;
/* Display size change events */
user->size_handler = guac_telnet_user_size_handler;

View File

@ -32,6 +32,7 @@ noinst_HEADERS = \
terminal/buffer.h \
terminal/char_mappings.h \
terminal/common.h \
terminal/color-scheme.h \
terminal/display.h \
terminal/named-colors.h \
terminal/palette.h \
@ -46,6 +47,7 @@ noinst_HEADERS = \
libguac_terminal_la_SOURCES = \
buffer.c \
char_mappings.c \
color-scheme.c \
common.c \
display.c \
named-colors.c \

256
src/terminal/color-scheme.c Normal file
View File

@ -0,0 +1,256 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "config.h"
#include "terminal/color-scheme.h"
#include "terminal/palette.h"
#include "terminal/xparsecolor.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <guacamole/client.h>
/**
* Compare a non-null-terminated string to a null-terminated literal, in the
* same manner as strcmp().
*
* @param str_start
* Start of the non-null-terminated string.
*
* @param str_end
* End of the non-null-terminated string, after the last character.
*
* @param literal
* The null-terminated literal to compare against.
*
* @return
* Zero if the two strings are equal and non-zero otherwise.
*/
static int guac_terminal_color_scheme_compare_token(const char* str_start,
const char* str_end, const char* literal) {
const int result = strncmp(literal, str_start, str_end - str_start);
if (result != 0)
return result;
/* At this point, literal is same length or longer than
* | str_end - str_start |, so if the two are equal, literal should
* have its null-terminator at | str_end - str_start |. */
return (int) (unsigned char) literal[str_end - str_start];
}
/**
* Strip the leading and trailing spaces of a bounded string.
*
* @param[in,out] str_start
* Address of a pointer to the start of the string. On return, the pointer
* is advanced to after any leading spaces.
*
* @param[in,out] str_end
* Address of a pointer to the end of the string, after the last character.
* On return, the pointer is moved back to before any trailing spaces.
*/
static void guac_terminal_color_scheme_strip_spaces(const char** str_start,
const char** str_end) {
/* Strip leading spaces. */
while (*str_start < *str_end && isspace(**str_start))
(*str_start)++;
/* Strip trailing spaces. */
while (*str_end > *str_start && isspace(*(*str_end - 1)))
(*str_end)--;
}
/**
* Parse the name part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param name_start
* Start of the name string.
*
* @param name_end
* End of the name string, after the last character.
*
* @param foreground
* Pointer to the foreground color.
*
* @param background
* Pointer to the background color.
*
* @param palette
* Pointer to the palette array.
*
* @param[out] target
* On return, pointer to the color struct that corresponds to the name.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_name(guac_client* client,
const char* name_start, const char* name_end,
guac_terminal_color* foreground, guac_terminal_color* background,
guac_terminal_color (*palette)[256],
guac_terminal_color** target) {
guac_terminal_color_scheme_strip_spaces(&name_start, &name_end);
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_FOREGROUND)) {
*target = foreground;
return 0;
}
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_BACKGROUND)) {
*target = background;
return 0;
}
/* Parse color<n> value. */
int index = -1;
if (sscanf(name_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = &(*palette)[index];
return 0;
}
guac_client_log(client, GUAC_LOG_WARNING,
"Unknown color name: \"%.*s\".",
name_end - name_start, name_start);
return 1;
}
/**
* Parse the value part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param value_start
* Start of the value string.
*
* @param value_end
* End of the value string, after the last character.
*
* @param palette
* The current color palette.
*
* @param[out] target
* On return, the parsed color.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_value(guac_client* client,
const char* value_start, const char* value_end,
const guac_terminal_color (*palette)[256],
guac_terminal_color* target) {
guac_terminal_color_scheme_strip_spaces(&value_start, &value_end);
/* Parse color<n> value. */
int index = -1;
if (sscanf(value_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = (*palette)[index];
return 0;
}
/* Parse X11 value. */
if (!guac_terminal_xparsecolor(value_start, target))
return 0;
guac_client_log(client, GUAC_LOG_WARNING,
"Invalid color value: \"%.*s\".",
value_end - value_start, value_start);
return 1;
}
void guac_terminal_parse_color_scheme(guac_client* client,
const char* color_scheme, guac_terminal_color* foreground,
guac_terminal_color* background,
guac_terminal_color (*palette)[256]) {
/* Set default gray-black color scheme and initial palette. */
*foreground = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_GRAY];
*background = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_BLACK];
memcpy(palette, GUAC_TERMINAL_INITIAL_PALETTE,
sizeof(GUAC_TERMINAL_INITIAL_PALETTE));
/* Current char being parsed, or NULL if at end of parsing. */
const char* cursor = color_scheme;
while (cursor) {
/* Start of the current "name: value" pair. */
const char* pair_start = cursor;
/* End of the current name-value pair. */
const char* pair_end = strchr(pair_start, ';');
if (pair_end) {
cursor = pair_end + 1;
}
else {
pair_end = pair_start + strlen(pair_start);
cursor = NULL;
}
guac_terminal_color_scheme_strip_spaces(&pair_start, &pair_end);
if (pair_start >= pair_end)
/* Allow empty pairs, which happens, e.g., when the configuration
* string ends in a semi-colon. */
continue;
/* End of the name part of the pair. */
const char* name_end = memchr(pair_start, ':', pair_end - pair_start);
if (name_end == NULL) {
guac_client_log(client, GUAC_LOG_WARNING,
"Expecting colon: \"%.*s\".",
pair_end - pair_start, pair_start);
return;
}
/* The color that the name corresponds to. */
guac_terminal_color* color_target = NULL;
if (guac_terminal_parse_color_scheme_name(
client, pair_start, name_end, foreground, background,
palette, &color_target))
return; /* Parsing failed. */
if (guac_terminal_parse_color_scheme_value(
client, name_end + 1, pair_end,
(const guac_terminal_color(*)[256]) palette, color_target))
return; /* Parsing failed. */
}
/* Persist pseudo-index for foreground/background colors */
foreground->palette_index = GUAC_TERMINAL_COLOR_FOREGROUND;
background->palette_index = GUAC_TERMINAL_COLOR_BACKGROUND;
}

View File

@ -78,7 +78,12 @@ int __guac_terminal_set_colors(guac_terminal_display* display,
}
display->glyph_foreground = *foreground;
guac_terminal_display_lookup_color(display,
foreground->palette_index, &display->glyph_foreground);
display->glyph_background = *background;
guac_terminal_display_lookup_color(display,
background->palette_index, &display->glyph_background);
/* Modify color if half-bright (low intensity) */
if (attributes->half_bright && !attributes->bold) {
@ -199,7 +204,7 @@ int __guac_terminal_set(guac_terminal_display* display, int row, int col, int co
guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
const char* font_name, int font_size, int dpi,
guac_terminal_color* foreground, guac_terminal_color* background,
const guac_terminal_color (*palette)[256]) {
guac_terminal_color (*palette)[256]) {
PangoFontMap* font_map;
PangoFont* font;
@ -271,7 +276,7 @@ guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
void guac_terminal_display_free(guac_terminal_display* display) {
/* Free default palette. */
free((void*) display->default_palette);
free(display->default_palette);
/* Free operations buffers */
free(display->operations);
@ -315,6 +320,18 @@ int guac_terminal_display_assign_color(guac_terminal_display* display,
int guac_terminal_display_lookup_color(guac_terminal_display* display,
int index, guac_terminal_color* color) {
/* Use default foreground if foreground pseudo-index is given */
if (index == GUAC_TERMINAL_COLOR_FOREGROUND) {
*color = display->default_foreground;
return 0;
}
/* Use default background if background pseudo-index is given */
if (index == GUAC_TERMINAL_COLOR_BACKGROUND) {
*color = display->default_background;
return 0;
}
/* Lookup fails if out-of-bounds */
if (index < 0 || index > 255)
return 1;
@ -658,11 +675,15 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) {
int rect_width, rect_height;
/* Color of the rectangle to draw */
const guac_terminal_color* color;
guac_terminal_color color;
if (current->character.attributes.reverse != current->character.attributes.cursor)
color = &current->character.attributes.foreground;
color = current->character.attributes.foreground;
else
color = &current->character.attributes.background;
color = current->character.attributes.background;
/* Rely only on palette index if defined */
guac_terminal_display_lookup_color(display,
color.palette_index, &color);
/* Current row within a subrect */
guac_terminal_operation* rect_current_row;
@ -685,7 +706,7 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) {
/* If not identical operation, stop */
if (rect_current->type != GUAC_CHAR_SET
|| guac_terminal_has_glyph(rect_current->character.value)
|| guac_terminal_colorcmp(joining_color, color) != 0)
|| guac_terminal_colorcmp(joining_color, &color) != 0)
break;
/* Next column */
@ -730,7 +751,7 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) {
/* Mark clear operations as NOP */
if (rect_current->type == GUAC_CHAR_SET
&& !guac_terminal_has_glyph(rect_current->character.value)
&& guac_terminal_colorcmp(joining_color, color) == 0)
&& guac_terminal_colorcmp(joining_color, &color) == 0)
rect_current->type = GUAC_CHAR_NOP;
/* Next column */
@ -750,7 +771,7 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) {
row * display->char_height,
rect_width * display->char_width,
rect_height * display->char_height,
color->red, color->green, color->blue,
color.red, color.green, color.blue,
0xFF);
} /* end if clear operation */

View File

@ -289,6 +289,10 @@ const guac_terminal_color GUAC_TERMINAL_INITIAL_PALETTE[256] = {
int guac_terminal_colorcmp(const guac_terminal_color* a,
const guac_terminal_color* b) {
/* Compare palette index alone if not unknown */
if (a->palette_index != -1 && b->palette_index != -1)
return a->palette_index - b->palette_index;
/* Consider red component highest order ... */
if (a->red != b->red)
return a->red - b->red;

View File

@ -22,6 +22,7 @@
#include "common/clipboard.h"
#include "common/cursor.h"
#include "terminal/buffer.h"
#include "terminal/color-scheme.h"
#include "terminal/common.h"
#include "terminal/display.h"
#include "terminal/palette.h"
@ -30,7 +31,6 @@
#include "terminal/terminal_handlers.h"
#include "terminal/types.h"
#include "terminal/typescript.h"
#include "terminal/xparsecolor.h"
#include <ctype.h>
#include <errno.h>
@ -305,247 +305,6 @@ void* guac_terminal_thread(void* data) {
}
/**
* Compare a non-null-terminated string to a null-terminated literal, in the
* same manner as strcmp().
*
* @param str_start
* Start of the non-null-terminated string.
*
* @param str_end
* End of the non-null-terminated string, after the last character.
*
* @param literal
* The null-terminated literal to compare against.
*
* @return
* Zero if the two strings are equal and non-zero otherwise.
*/
static int guac_terminal_color_scheme_compare_token(const char* str_start,
const char* str_end, const char* literal) {
const int result = strncmp(literal, str_start, str_end - str_start);
if (result != 0)
return result;
/* At this point, literal is same length or longer than
* | str_end - str_start |, so if the two are equal, literal should
* have its null-terminator at | str_end - str_start |. */
return (int) (unsigned char) literal[str_end - str_start];
}
/**
* Strip the leading and trailing spaces of a bounded string.
*
* @param[in,out] str_start
* Address of a pointer to the start of the string. On return, the pointer
* is advanced to after any leading spaces.
*
* @param[in,out] str_end
* Address of a pointer to the end of the string, after the last character.
* On return, the pointer is moved back to before any trailing spaces.
*/
static void guac_terminal_color_scheme_strip_spaces(const char** str_start,
const char** str_end) {
/* Strip leading spaces. */
while (*str_start < *str_end && isspace(**str_start))
(*str_start)++;
/* Strip trailing spaces. */
while (*str_end > *str_start && isspace(*(*str_end - 1)))
(*str_end)--;
}
/**
* Parse the name part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param name_start
* Start of the name string.
*
* @param name_end
* End of the name string, after the last character.
*
* @param foreground
* Pointer to the foreground color.
*
* @param background
* Pointer to the background color.
*
* @param palette
* Pointer to the palette array.
*
* @param[out] target
* On return, pointer to the color struct that corresponds to the name.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_name(guac_client* client,
const char* name_start, const char* name_end,
guac_terminal_color* foreground, guac_terminal_color* background,
guac_terminal_color (*palette)[256],
guac_terminal_color** target) {
guac_terminal_color_scheme_strip_spaces(&name_start, &name_end);
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_FOREGROUND)) {
*target = foreground;
return 0;
}
if (!guac_terminal_color_scheme_compare_token(
name_start, name_end, GUAC_TERMINAL_SCHEME_BACKGROUND)) {
*target = background;
return 0;
}
/* Parse color<n> value. */
int index = -1;
if (sscanf(name_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = &(*palette)[index];
return 0;
}
guac_client_log(client, GUAC_LOG_WARNING,
"Unknown color name: \"%.*s\".",
name_end - name_start, name_start);
return 1;
}
/**
* Parse the value part of the name-value pair within the color-scheme
* configuration.
*
* @param client
* The client that the terminal is connected to.
*
* @param value_start
* Start of the value string.
*
* @param value_end
* End of the value string, after the last character.
*
* @param palette
* The current color palette.
*
* @param[out] target
* On return, the parsed color.
*
* @return
* Zero if successful or non-zero otherwise.
*/
static int guac_terminal_parse_color_scheme_value(guac_client* client,
const char* value_start, const char* value_end,
const guac_terminal_color (*palette)[256],
guac_terminal_color* target) {
guac_terminal_color_scheme_strip_spaces(&value_start, &value_end);
/* Parse color<n> value. */
int index = -1;
if (sscanf(value_start, GUAC_TERMINAL_SCHEME_NUMBERED "%d", &index) &&
index >= 0 && index <= 255) {
*target = (*palette)[index];
return 0;
}
/* Parse X11 value. */
if (!guac_terminal_xparsecolor(value_start, target))
return 0;
guac_client_log(client, GUAC_LOG_WARNING,
"Invalid color value: \"%.*s\".",
value_end - value_start, value_start);
return 1;
}
/**
* Parse a color-scheme configuration string, and return specified
* foreground/background colors and color palette.
*
* @param client
* The client that the terminal is connected to.
*
* @param color_scheme
* A semicolon-separated list of name-value pairs, i.e.
* "<name>: <value> [; <name>: <value> [; ...]]".
* For example, "color2: rgb:cc/33/22; background: color5".
*
* @param[out] foreground
* Parsed foreground color.
*
* @param[out] background
* Parsed background color.
*
* @param[in,out] palette
* Parsed color palette. The caller is responsible for allocating a mutable
* array on entry. On return, the array contains the parsed palette.
*/
static void guac_terminal_parse_color_scheme(guac_client* client,
const char* color_scheme, guac_terminal_color* foreground,
guac_terminal_color* background,
guac_terminal_color (*palette)[256]) {
/* Set default gray-black color scheme and initial palette. */
*foreground = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_GRAY];
*background = GUAC_TERMINAL_INITIAL_PALETTE[GUAC_TERMINAL_COLOR_BLACK];
memcpy(palette, GUAC_TERMINAL_INITIAL_PALETTE,
sizeof(GUAC_TERMINAL_INITIAL_PALETTE));
/* Current char being parsed, or NULL if at end of parsing. */
const char* cursor = color_scheme;
while (cursor) {
/* Start of the current "name: value" pair. */
const char* pair_start = cursor;
/* End of the current name-value pair. */
const char* pair_end = strchr(pair_start, ';');
if (pair_end) {
cursor = pair_end + 1;
}
else {
pair_end = pair_start + strlen(pair_start);
cursor = NULL;
}
guac_terminal_color_scheme_strip_spaces(&pair_start, &pair_end);
if (pair_start >= pair_end)
/* Allow empty pairs, which happens, e.g., when the configuration
* string ends in a semi-colon. */
continue;
/* End of the name part of the pair. */
const char* name_end = memchr(pair_start, ':', pair_end - pair_start);
if (name_end == NULL) {
guac_client_log(client, GUAC_LOG_WARNING,
"Expecting colon: \"%.*s\".",
pair_end - pair_start, pair_start);
return;
}
/* The color that the name corresponds to. */
guac_terminal_color* color_target = NULL;
if (guac_terminal_parse_color_scheme_name(
client, pair_start, name_end, foreground, background,
palette, &color_target))
return; /* Parsing failed. */
if (guac_terminal_parse_color_scheme_value(
client, name_end + 1, pair_end,
(const guac_terminal_color(*)[256]) palette, color_target))
return; /* Parsing failed. */
}
}
guac_terminal* guac_terminal_create(guac_client* client,
guac_common_clipboard* clipboard, int max_scrollback,
const char* font_name, int font_size, int dpi,
@ -625,7 +384,7 @@ guac_terminal* guac_terminal_create(guac_client* client,
font_name, font_size, dpi,
&default_char.attributes.foreground,
&default_char.attributes.background,
(const guac_terminal_color(*)[256]) default_palette);
(guac_terminal_color(*)[256]) default_palette);
/* Fail if display init failed */
if (term->display == NULL) {
@ -2181,3 +1940,31 @@ void guac_terminal_dup(guac_terminal* term, guac_user* user,
}
void guac_terminal_apply_color_scheme(guac_terminal* terminal,
const char* color_scheme) {
guac_client* client = terminal->client;
guac_terminal_char* default_char = &terminal->default_char;
guac_terminal_display* display = terminal->display;
/* Reinitialize default terminal colors with values from color scheme */
guac_terminal_parse_color_scheme(client, color_scheme,
&default_char->attributes.foreground,
&default_char->attributes.background,
display->default_palette);
/* Reinitialize default attributes of buffer and display */
guac_terminal_display_reset_palette(display);
display->default_foreground = default_char->attributes.foreground;
display->default_background = default_char->attributes.background;
/* Redraw terminal text and background */
guac_terminal_repaint_default_layer(terminal, client->socket);
__guac_terminal_redraw_rect(terminal, 0, 0,
terminal->term_height - 1,
terminal->term_width - 1);
guac_terminal_notify(terminal);
}

View File

@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef GUAC_TERMINAL_COLOR_SCHEME_H
#define GUAC_TERMINAL_COLOR_SCHEME_H
#include "config.h"
#include "terminal/palette.h"
#include <guacamole/client.h>
#include <stdlib.h>
#include <string.h>
/**
* The name of the color scheme having black foreground and white background.
*/
#define GUAC_TERMINAL_SCHEME_BLACK_WHITE "black-white"
/**
* The name of the color scheme having gray foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GRAY_BLACK "gray-black"
/**
* The name of the color scheme having green foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GREEN_BLACK "green-black"
/**
* The name of the color scheme having white foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_WHITE_BLACK "white-black"
/**
* Color name representing the foreground color.
*/
#define GUAC_TERMINAL_SCHEME_FOREGROUND "foreground"
/**
* Color name representing the background color.
*/
#define GUAC_TERMINAL_SCHEME_BACKGROUND "background"
/**
* Color name representing a numbered color.
*/
#define GUAC_TERMINAL_SCHEME_NUMBERED "color"
/**
* Parse a color-scheme configuration string, and return specified
* foreground/background colors and color palette.
*
* @param client
* The client that the terminal is connected to.
*
* @param color_scheme
* A semicolon-separated list of name-value pairs, i.e.
* "<name>: <value> [; <name>: <value> [; ...]]".
* For example, "color2: rgb:cc/33/22; background: color5".
*
* @param[out] foreground
* Parsed foreground color.
*
* @param[out] background
* Parsed background color.
*
* @param[in,out] palette
* Parsed color palette. The caller is responsible for allocating a mutable
* array on entry. On return, the array contains the parsed palette.
*/
void guac_terminal_parse_color_scheme(guac_client* client,
const char* color_scheme, guac_terminal_color* foreground,
guac_terminal_color* background,
guac_terminal_color (*palette)[256]);
#endif

View File

@ -142,7 +142,7 @@ typedef struct guac_terminal_display {
* The default palette. Use GUAC_TERMINAL_INITIAL_PALETTE if null.
* Must free on destruction if not null.
*/
const guac_terminal_color (*default_palette)[256];
guac_terminal_color (*default_palette)[256];
/**
* Default foreground color for all glyphs.
@ -215,7 +215,7 @@ typedef struct guac_terminal_display {
guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
const char* font_name, int font_size, int dpi,
guac_terminal_color* foreground, guac_terminal_color* background,
const guac_terminal_color (*palette)[256]);
guac_terminal_color (*palette)[256]);
/**
* Frees the given display.

View File

@ -24,6 +24,20 @@
#include <stdint.h>
/**
* The pseudo-index of the color set as the the default foreground color for
* the terminal. Regardless of what changes are made to the palette, this index
* will always return the current default foreground color.
*/
#define GUAC_TERMINAL_COLOR_FOREGROUND -2
/**
* The pseudo-index of the color set as the the default background color for
* the terminal. Regardless of what changes are made to the palette, this index
* will always return the current default background color.
*/
#define GUAC_TERMINAL_COLOR_BACKGROUND -3
/**
* The index of black within the terminal color palette.
*/

View File

@ -69,41 +69,6 @@
*/
#define GUAC_TERMINAL_WHEEL_SCROLL_AMOUNT 3
/**
* The name of the color scheme having black foreground and white background.
*/
#define GUAC_TERMINAL_SCHEME_BLACK_WHITE "black-white"
/**
* The name of the color scheme having gray foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GRAY_BLACK "gray-black"
/**
* The name of the color scheme having green foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_GREEN_BLACK "green-black"
/**
* The name of the color scheme having white foreground and black background.
*/
#define GUAC_TERMINAL_SCHEME_WHITE_BLACK "white-black"
/**
* Color name representing the foreground color.
*/
#define GUAC_TERMINAL_SCHEME_FOREGROUND "foreground"
/**
* Color name representing the background color.
*/
#define GUAC_TERMINAL_SCHEME_BACKGROUND "background"
/**
* Color name representing a numbered color.
*/
#define GUAC_TERMINAL_SCHEME_NUMBERED "color"
/**
* Flag which specifies that terminal output should be sent to both the current
* pipe stream and the user's display. By default, terminal output will be sent
@ -1107,5 +1072,19 @@ int guac_terminal_create_typescript(guac_terminal* term, const char* path,
*/
int guac_terminal_available_scroll(guac_terminal* term);
/**
* Immediately applies the given color scheme to the given terminal, overriding
* the color scheme provided when the terminal was created. Valid color schemes
* are those accepted by guac_terminal_parse_color_scheme().
*
* @param terminal
* The terminal to apply the color scheme to.
*
* @param color_scheme
* The color scheme to apply.
*/
void guac_terminal_apply_color_scheme(guac_terminal* terminal,
const char* color_scheme);
#endif