Skip to content

Commit

Permalink
Set terminal alternate buffer height as terminal height.
Browse files Browse the repository at this point in the history
  • Loading branch information
corentin-soriano committed Oct 3, 2024
1 parent 783f4c5 commit 6a3dd6c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
55 changes: 55 additions & 0 deletions src/terminal/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,61 @@ guac_terminal_buffer* guac_terminal_buffer_alloc(int rows,

}

guac_terminal_buffer* guac_terminal_buffer_realloc(guac_terminal_buffer* buffer, int rows) {

int i;
guac_terminal_buffer_row* row;

/* No change */
if (rows == buffer->available)
return buffer;

/* Too many rows */
if (rows < buffer->available) {

/* Last buffer row */
row = buffer->rows + buffer->available;

/* Free excess rows characters */
for (i=rows; i>buffer->available; i++) {

/* Free row characters */
guac_mem_free(row->characters);

/* Previous row */
row++;
}
}

/* Realloc buffer rows */
buffer->rows = guac_mem_realloc(buffer->rows, sizeof(guac_terminal_buffer_row) * rows);

/* Init new rows characters */
if (rows > buffer->available) {

/* First uninitialized buffer row */
row = buffer->rows + buffer->available;

/* Init new scrollback rows */
for (i=buffer->available; i<rows; i++) {

/* Allocate row */
row->available = GUAC_TERMINAL_BUFFER_ROW_MIN_SIZE;
row->length = 0;
row->wrapped_row = false;
row->characters = guac_mem_alloc(sizeof(guac_terminal_char), row->available);

/* Next row */
row++;
}
}

/* Update scrollback length */
buffer->available = rows;

return buffer;
}

void guac_terminal_buffer_free(guac_terminal_buffer* buffer) {

int i;
Expand Down
11 changes: 7 additions & 4 deletions src/terminal/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,6 @@ guac_terminal* guac_terminal_create(guac_client* client,
if (initial_scrollback < GUAC_TERMINAL_MAX_ROWS)
initial_scrollback = GUAC_TERMINAL_MAX_ROWS;

/* Init current and alternate buffer */
term->current_buffer = term->normal_buffer = guac_terminal_buffer_alloc(initial_scrollback, &default_char);
term->alternate_buffer = guac_terminal_buffer_alloc(GUAC_TERMINAL_MAX_ROWS, &default_char);

/* Init display */
term->display = guac_terminal_display_alloc(client,
options->font_name, options->font_size, options->dpi,
Expand Down Expand Up @@ -444,6 +440,10 @@ guac_terminal* guac_terminal_create(guac_client* client,
term->height = adjusted_height;
term->width = adjusted_width;

/* Init current and alternate buffer */
term->current_buffer = term->normal_buffer = guac_terminal_buffer_alloc(initial_scrollback, &default_char);
term->alternate_buffer = guac_terminal_buffer_alloc(term->term_height, &default_char);

/* Open STDIN pipe */
if (pipe(term->stdin_pipe_fd)) {
guac_error = GUAC_STATUS_SEE_ERRNO;
Expand Down Expand Up @@ -1358,6 +1358,9 @@ int guac_terminal_resize(guac_terminal* terminal, int width, int height) {
terminal->scroll_end = rows - 1;
}

/* Realloc alternate_buffer to fit terminal height */
terminal->alternate_buffer = guac_terminal_buffer_realloc(terminal->alternate_buffer, terminal->term_height);

/* Notify scrollbar of resize */
guac_terminal_scrollbar_parent_resized(terminal->scrollbar,
terminal->outer_width, terminal->outer_height, terminal->term_height);
Expand Down
14 changes: 14 additions & 0 deletions src/terminal/terminal/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ typedef struct guac_terminal_buffer guac_terminal_buffer;
guac_terminal_buffer* guac_terminal_buffer_alloc(int rows,
const guac_terminal_char* default_character);

/**
* Reallocate existing buffer to add or remove rows.
*
* @param buffer
* The buffer to reallocate.
*
* @param rows
* Number of scrollback rows
*
* @return
* The reallocated buffer.
*/
guac_terminal_buffer* guac_terminal_buffer_realloc(guac_terminal_buffer* buffer, int rows);

/**
* Frees the given buffer.
*/
Expand Down

0 comments on commit 6a3dd6c

Please sign in to comment.