1 | /*
|
---|
2 | * Copyright (c) 2006 Josef Cejka
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <io/style.h>
|
---|
36 | #include <malloc.h>
|
---|
37 | #include <unistd.h>
|
---|
38 | #include <assert.h>
|
---|
39 | #include <stdbool.h>
|
---|
40 | #include <as.h>
|
---|
41 | #include <io/chargrid.h>
|
---|
42 |
|
---|
43 | /** Create a chargrid.
|
---|
44 | *
|
---|
45 | * @param[in] cols Number of columns.
|
---|
46 | * @param[in] rows Number of rows.
|
---|
47 | * @param[in] flags Chargrid flags.
|
---|
48 | *
|
---|
49 | * @return New chargrid.
|
---|
50 | * @return NULL on failure.
|
---|
51 | *
|
---|
52 | */
|
---|
53 | chargrid_t *chargrid_create(sysarg_t cols, sysarg_t rows,
|
---|
54 | chargrid_flag_t flags)
|
---|
55 | {
|
---|
56 | size_t size =
|
---|
57 | sizeof(chargrid_t) + cols * rows * sizeof(charfield_t);
|
---|
58 | chargrid_t *scrbuf;
|
---|
59 |
|
---|
60 | if ((flags & CHARGRID_FLAG_SHARED) == CHARGRID_FLAG_SHARED) {
|
---|
61 | scrbuf = (chargrid_t *) as_area_create(AS_AREA_ANY, size,
|
---|
62 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
|
---|
63 | if (scrbuf == AS_MAP_FAILED)
|
---|
64 | return NULL;
|
---|
65 | } else {
|
---|
66 | scrbuf = (chargrid_t *) malloc(size);
|
---|
67 | if (scrbuf == NULL)
|
---|
68 | return NULL;
|
---|
69 | }
|
---|
70 |
|
---|
71 | scrbuf->size = size;
|
---|
72 | scrbuf->flags = flags;
|
---|
73 | scrbuf->cols = cols;
|
---|
74 | scrbuf->rows = rows;
|
---|
75 | scrbuf->cursor_visible = false;
|
---|
76 |
|
---|
77 | scrbuf->attrs.type = CHAR_ATTR_STYLE;
|
---|
78 | scrbuf->attrs.val.style = STYLE_NORMAL;
|
---|
79 |
|
---|
80 | scrbuf->top_row = 0;
|
---|
81 | chargrid_clear(scrbuf);
|
---|
82 |
|
---|
83 | return scrbuf;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void chargrid_destroy(chargrid_t *srcbuf)
|
---|
87 | {
|
---|
88 | // TODO
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool chargrid_cursor_at(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
|
---|
92 | {
|
---|
93 | return ((scrbuf->cursor_visible) && (scrbuf->col == col) &&
|
---|
94 | (scrbuf->row == row));
|
---|
95 | }
|
---|
96 |
|
---|
97 | sysarg_t chargrid_get_top_row(chargrid_t *scrbuf)
|
---|
98 | {
|
---|
99 | return scrbuf->top_row;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static sysarg_t chargrid_update_rows(chargrid_t *scrbuf)
|
---|
103 | {
|
---|
104 | if (scrbuf->row == scrbuf->rows) {
|
---|
105 | scrbuf->row = scrbuf->rows - 1;
|
---|
106 | scrbuf->top_row = (scrbuf->top_row + 1) % scrbuf->rows;
|
---|
107 | chargrid_clear_row(scrbuf, scrbuf->row);
|
---|
108 |
|
---|
109 | return scrbuf->rows;
|
---|
110 | }
|
---|
111 |
|
---|
112 | return 2;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static sysarg_t chargrid_update_cols(chargrid_t *scrbuf)
|
---|
116 | {
|
---|
117 | /* Column overflow */
|
---|
118 | if (scrbuf->col == scrbuf->cols) {
|
---|
119 | scrbuf->col = 0;
|
---|
120 | scrbuf->row++;
|
---|
121 | return chargrid_update_rows(scrbuf);
|
---|
122 | }
|
---|
123 |
|
---|
124 | return 1;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Store one character to chargrid.
|
---|
128 | *
|
---|
129 | * Its position is determined by scrbuf->col
|
---|
130 | * and scrbuf->row.
|
---|
131 | *
|
---|
132 | * @param scrbuf Chargrid.
|
---|
133 | * @param ch Character to store.
|
---|
134 | * @param update Update coordinates.
|
---|
135 | *
|
---|
136 | * @return Number of rows which have been affected. In usual
|
---|
137 | * situations this is 1. If the current position was
|
---|
138 | * updated to a new row, this value is 2.
|
---|
139 | *
|
---|
140 | */
|
---|
141 | sysarg_t chargrid_putchar(chargrid_t *scrbuf, wchar_t ch, bool update)
|
---|
142 | {
|
---|
143 | assert(scrbuf->col < scrbuf->cols);
|
---|
144 | assert(scrbuf->row < scrbuf->rows);
|
---|
145 |
|
---|
146 | charfield_t *field =
|
---|
147 | chargrid_charfield_at(scrbuf, scrbuf->col, scrbuf->row);
|
---|
148 |
|
---|
149 | field->ch = ch;
|
---|
150 | field->attrs = scrbuf->attrs;
|
---|
151 | field->flags |= CHAR_FLAG_DIRTY;
|
---|
152 |
|
---|
153 | if (update) {
|
---|
154 | scrbuf->col++;
|
---|
155 | return chargrid_update_cols(scrbuf);
|
---|
156 | }
|
---|
157 |
|
---|
158 | return 1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Jump to a new row in chargrid.
|
---|
162 | *
|
---|
163 | * @param scrbuf Chargrid.
|
---|
164 | *
|
---|
165 | * @return Number of rows which have been affected. In usual
|
---|
166 | * situations this is 2 (the original row and the new
|
---|
167 | * row).
|
---|
168 | *
|
---|
169 | */
|
---|
170 | sysarg_t chargrid_newline(chargrid_t *scrbuf)
|
---|
171 | {
|
---|
172 | assert(scrbuf->col < scrbuf->cols);
|
---|
173 | assert(scrbuf->row < scrbuf->rows);
|
---|
174 |
|
---|
175 | scrbuf->col = 0;
|
---|
176 | scrbuf->row++;
|
---|
177 |
|
---|
178 | return chargrid_update_rows(scrbuf);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Jump to a new row in chargrid.
|
---|
182 | *
|
---|
183 | * @param scrbuf Chargrid.
|
---|
184 | * @param tab_size Tab size.
|
---|
185 | *
|
---|
186 | * @return Number of rows which have been affected. In usual
|
---|
187 | * situations this is 1. If the current position was
|
---|
188 | * updated to a new row, this value is 2.
|
---|
189 | *
|
---|
190 | */
|
---|
191 | sysarg_t chargrid_tabstop(chargrid_t *scrbuf, sysarg_t tab_size)
|
---|
192 | {
|
---|
193 | assert(scrbuf->col < scrbuf->cols);
|
---|
194 | assert(scrbuf->row < scrbuf->rows);
|
---|
195 |
|
---|
196 | sysarg_t spaces = tab_size - scrbuf->cols % tab_size;
|
---|
197 | sysarg_t flush = 1;
|
---|
198 |
|
---|
199 | for (sysarg_t i = 0; i < spaces; i++)
|
---|
200 | flush += chargrid_putchar(scrbuf, ' ', true) - 1;
|
---|
201 |
|
---|
202 | return flush;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Jump to the previous character in chargrid.
|
---|
206 | *
|
---|
207 | * Currently no scrollback is supported.
|
---|
208 | *
|
---|
209 | * @param scrbuf Chargrid.
|
---|
210 | *
|
---|
211 | * @return Number of rows which have been affected. In usual
|
---|
212 | * situations this is 1. If the current position was
|
---|
213 | * updated to the previous row, this value is 2.
|
---|
214 | * @return 0 if no backspace is possible.
|
---|
215 | *
|
---|
216 | */
|
---|
217 | sysarg_t chargrid_backspace(chargrid_t *scrbuf)
|
---|
218 | {
|
---|
219 | assert(scrbuf->col < scrbuf->cols);
|
---|
220 | assert(scrbuf->row < scrbuf->rows);
|
---|
221 |
|
---|
222 | if ((scrbuf->col == 0) && (scrbuf->row == 0))
|
---|
223 | return 0;
|
---|
224 |
|
---|
225 | if (scrbuf->col == 0) {
|
---|
226 | scrbuf->col = scrbuf->cols - 1;
|
---|
227 | scrbuf->row--;
|
---|
228 |
|
---|
229 | chargrid_putchar(scrbuf, ' ', false);
|
---|
230 | return 2;
|
---|
231 | }
|
---|
232 |
|
---|
233 | scrbuf->col--;
|
---|
234 | chargrid_putchar(scrbuf, ' ', false);
|
---|
235 | return 1;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /** Clear the chargrid.
|
---|
239 | *
|
---|
240 | * @param scrbuf Chargrid.
|
---|
241 | *
|
---|
242 | */
|
---|
243 | void chargrid_clear(chargrid_t *scrbuf)
|
---|
244 | {
|
---|
245 | for (size_t pos = 0; pos < (scrbuf->cols * scrbuf->rows); pos++) {
|
---|
246 | scrbuf->data[pos].ch = 0;
|
---|
247 | scrbuf->data[pos].attrs = scrbuf->attrs;
|
---|
248 | scrbuf->data[pos].flags = CHAR_FLAG_DIRTY;
|
---|
249 | }
|
---|
250 |
|
---|
251 | scrbuf->col = 0;
|
---|
252 | scrbuf->row = 0;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /** Update current chargrid coordinates
|
---|
256 | *
|
---|
257 | * @param scrbuf Chargrid.
|
---|
258 | * @param col New column.
|
---|
259 | * @param row New row.
|
---|
260 | *
|
---|
261 | */
|
---|
262 | void chargrid_set_cursor(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
|
---|
263 | {
|
---|
264 | scrbuf->col = col;
|
---|
265 | scrbuf->row = row;
|
---|
266 | }
|
---|
267 |
|
---|
268 | void chargrid_set_cursor_visibility(chargrid_t *scrbuf, bool visible)
|
---|
269 | {
|
---|
270 | scrbuf->cursor_visible = visible;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /** Get current chargrid coordinates
|
---|
274 | *
|
---|
275 | * @param scrbuf Chargrid.
|
---|
276 | * @param col Column.
|
---|
277 | * @param row Row.
|
---|
278 | *
|
---|
279 | */
|
---|
280 | void chargrid_get_cursor(chargrid_t *scrbuf, sysarg_t *col,
|
---|
281 | sysarg_t *row)
|
---|
282 | {
|
---|
283 | assert(col);
|
---|
284 | assert(row);
|
---|
285 |
|
---|
286 | *col = scrbuf->col;
|
---|
287 | *row = scrbuf->row;
|
---|
288 | }
|
---|
289 |
|
---|
290 | bool chargrid_get_cursor_visibility(chargrid_t *scrbuf)
|
---|
291 | {
|
---|
292 | return scrbuf->cursor_visible;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /** Clear one buffer row.
|
---|
296 | *
|
---|
297 | * @param scrbuf Chargrid.
|
---|
298 | * @param row Row to clear.
|
---|
299 | *
|
---|
300 | */
|
---|
301 | void chargrid_clear_row(chargrid_t *scrbuf, sysarg_t row)
|
---|
302 | {
|
---|
303 | for (sysarg_t col = 0; col < scrbuf->cols; col++) {
|
---|
304 | charfield_t *field =
|
---|
305 | chargrid_charfield_at(scrbuf, col, row);
|
---|
306 |
|
---|
307 | field->ch = 0;
|
---|
308 | field->attrs = scrbuf->attrs;
|
---|
309 | field->flags |= CHAR_FLAG_DIRTY;
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | /** Set chargrid style.
|
---|
314 | *
|
---|
315 | * @param scrbuf Chargrid.
|
---|
316 | * @param style Style.
|
---|
317 | *
|
---|
318 | */
|
---|
319 | void chargrid_set_style(chargrid_t *scrbuf, console_style_t style)
|
---|
320 | {
|
---|
321 | scrbuf->attrs.type = CHAR_ATTR_STYLE;
|
---|
322 | scrbuf->attrs.val.style = style;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /** Set chargrid color.
|
---|
326 | *
|
---|
327 | * @param scrbuf Chargrid.
|
---|
328 | * @param bgcolor Background color.
|
---|
329 | * @param fgcolor Foreground color.
|
---|
330 | * @param attr Color attribute.
|
---|
331 | *
|
---|
332 | */
|
---|
333 | void chargrid_set_color(chargrid_t *scrbuf, console_color_t bgcolor,
|
---|
334 | console_color_t fgcolor, console_color_attr_t attr)
|
---|
335 | {
|
---|
336 | scrbuf->attrs.type = CHAR_ATTR_INDEX;
|
---|
337 | scrbuf->attrs.val.index.bgcolor = bgcolor;
|
---|
338 | scrbuf->attrs.val.index.fgcolor = fgcolor;
|
---|
339 | scrbuf->attrs.val.index.attr = attr;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /** Set chargrid RGB color.
|
---|
343 | *
|
---|
344 | * @param scrbuf Chargrid.
|
---|
345 | * @param bgcolor Background color.
|
---|
346 | * @param fgcolor Foreground color.
|
---|
347 | *
|
---|
348 | */
|
---|
349 | void chargrid_set_rgb_color(chargrid_t *scrbuf, pixel_t bgcolor,
|
---|
350 | pixel_t fgcolor)
|
---|
351 | {
|
---|
352 | scrbuf->attrs.type = CHAR_ATTR_RGB;
|
---|
353 | scrbuf->attrs.val.rgb.bgcolor = bgcolor;
|
---|
354 | scrbuf->attrs.val.rgb.fgcolor = fgcolor;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /** @}
|
---|
358 | */
|
---|