1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 
21 #ifndef _GUAC_TERMINAL_BUFFER_H
22 #define _GUAC_TERMINAL_BUFFER_H
23 
24 #include "config.h"
25 
26 #include "types.h"
27 
28 /**
29  * A single variable-length row of terminal data.
30  */
31 typedef struct guac_terminal_buffer_row {
32 
33     /**
34      * Array of guac_terminal_char representing the contents of the row.
35      */
36     guac_terminal_char* characters;
37 
38     /**
39      * The length of this row in characters. This is the number of initialized
40      * characters in the buffer, usually equal to the number of characters
41      * in the screen width at the time this row was created.
42      */
43     int length;
44 
45     /**
46      * The number of elements in the characters array. After the length
47      * equals this value, the array must be resized.
48      */
49     int available;
50 
51 } guac_terminal_buffer_row;
52 
53 /**
54  * A buffer containing a constant number of arbitrary-length rows.
55  * New rows can be appended to the buffer, with the oldest row replaced with
56  * the new row.
57  */
58 typedef struct guac_terminal_buffer {
59 
60     /**
61      * The character to assign to newly-allocated cells.
62      */
63     guac_terminal_char default_character;
64 
65     /**
66      * Array of buffer rows. This array functions as a ring buffer.
67      * When a new row needs to be appended, the top reference is moved down
68      * and the old top row is replaced.
69      */
70     guac_terminal_buffer_row* rows;
71 
72     /**
73      * The index of the first row in the buffer (the row which represents row 0
74      * with respect to the terminal display). This is also the index of the row
75      * to replace when insufficient space remains in the buffer to add a new
76      * row.
77      */
78     int top;
79 
80     /**
81      * The number of rows currently stored in the buffer.
82      */
83     int length;
84 
85     /**
86      * The number of rows in the buffer. This is the total capacity
87      * of the buffer.
88      */
89     int available;
90 
91 } guac_terminal_buffer;
92 
93 /**
94  * Allocates a new buffer having the given maximum number of rows. New character cells will
95  * be initialized to the given character.
96  */
97 guac_terminal_buffer* guac_terminal_buffer_alloc(int rows, guac_terminal_char* default_character);
98 
99 /**
100  * Frees the given buffer.
101  */
102 void guac_terminal_buffer_free(guac_terminal_buffer* buffer);
103 
104 /**
105  * Returns the row at the given location. The row returned is guaranteed to be at least the given
106  * width.
107  */
108 guac_terminal_buffer_row* guac_terminal_buffer_get_row(guac_terminal_buffer* buffer, int row, int width);
109 
110 /**
111  * Copies the given range of columns to a new location, offset from
112  * the original by the given number of columns.
113  */
114 void guac_terminal_buffer_copy_columns(guac_terminal_buffer* buffer, int row,
115         int start_column, int end_column, int offset);
116 
117 /**
118  * Copies the given range of rows to a new location, offset from the
119  * original by the given number of rows.
120  */
121 void guac_terminal_buffer_copy_rows(guac_terminal_buffer* buffer,
122         int start_row, int end_row, int offset);
123 
124 /**
125  * Sets the given range of columns within the given row to the given
126  * character.
127  */
128 void guac_terminal_buffer_set_columns(guac_terminal_buffer* buffer, int row,
129         int start_column, int end_column, guac_terminal_char* character);
130 
131 #endif
132 
133