source: mainline/uspace/lib/c/generic/io/chargrid.c@ 1be9ee0

topic/simplify-dev-export
Last change on this file since 1be9ee0 was 1be9ee0, checked in by Jiri Svoboda <jiri@…>, 19 months ago

Move output API to separate library

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[7c014d1]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
[1be9ee0]29/** @addtogroup libc
[7c014d1]30 * @{
31 */
32/** @file
33 */
34
35#include <io/style.h>
[38d150e]36#include <stdlib.h>
[7c014d1]37#include <assert.h>
[3e6a98c5]38#include <stdbool.h>
[582a0b8]39#include <stddef.h>
[7c014d1]40#include <as.h>
[6d5e378]41#include <io/chargrid.h>
[7c014d1]42
[6d5e378]43/** Create a chargrid.
[7c014d1]44 *
45 * @param[in] cols Number of columns.
46 * @param[in] rows Number of rows.
[6d5e378]47 * @param[in] flags Chargrid flags.
[7c014d1]48 *
[6d5e378]49 * @return New chargrid.
[7c014d1]50 * @return NULL on failure.
51 *
52 */
[6d5e378]53chargrid_t *chargrid_create(sysarg_t cols, sysarg_t rows,
54 chargrid_flag_t flags)
[7c014d1]55{
56 size_t size =
[6d5e378]57 sizeof(chargrid_t) + cols * rows * sizeof(charfield_t);
58 chargrid_t *scrbuf;
[a35b458]59
[6d5e378]60 if ((flags & CHARGRID_FLAG_SHARED) == CHARGRID_FLAG_SHARED) {
61 scrbuf = (chargrid_t *) as_area_create(AS_AREA_ANY, size,
[6aeca0d]62 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
63 AS_AREA_UNPAGED);
[faba839]64 if (scrbuf == AS_MAP_FAILED)
[7c014d1]65 return NULL;
66 } else {
[6d5e378]67 scrbuf = (chargrid_t *) malloc(size);
[7c014d1]68 if (scrbuf == NULL)
69 return NULL;
70 }
[a35b458]71
[7c014d1]72 scrbuf->size = size;
73 scrbuf->flags = flags;
74 scrbuf->cols = cols;
75 scrbuf->rows = rows;
76 scrbuf->cursor_visible = false;
[a35b458]77
[7c014d1]78 scrbuf->attrs.type = CHAR_ATTR_STYLE;
79 scrbuf->attrs.val.style = STYLE_NORMAL;
[a35b458]80
[7c014d1]81 scrbuf->top_row = 0;
[6d5e378]82 chargrid_clear(scrbuf);
[a35b458]83
[7c014d1]84 return scrbuf;
85}
86
[6d5e378]87void chargrid_destroy(chargrid_t *srcbuf)
[7c014d1]88{
[6d5e378]89 // TODO
[7c014d1]90}
91
[6d5e378]92bool chargrid_cursor_at(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
[7c014d1]93{
94 return ((scrbuf->cursor_visible) && (scrbuf->col == col) &&
95 (scrbuf->row == row));
96}
97
[6d5e378]98sysarg_t chargrid_get_top_row(chargrid_t *scrbuf)
[7c014d1]99{
100 return scrbuf->top_row;
101}
102
[6d5e378]103static sysarg_t chargrid_update_rows(chargrid_t *scrbuf)
[7c014d1]104{
105 if (scrbuf->row == scrbuf->rows) {
106 scrbuf->row = scrbuf->rows - 1;
107 scrbuf->top_row = (scrbuf->top_row + 1) % scrbuf->rows;
[6d5e378]108 chargrid_clear_row(scrbuf, scrbuf->row);
[a35b458]109
[7c014d1]110 return scrbuf->rows;
111 }
[a35b458]112
[7c014d1]113 return 2;
114}
115
[6d5e378]116static sysarg_t chargrid_update_cols(chargrid_t *scrbuf)
[7c014d1]117{
118 /* Column overflow */
119 if (scrbuf->col == scrbuf->cols) {
120 scrbuf->col = 0;
121 scrbuf->row++;
[6d5e378]122 return chargrid_update_rows(scrbuf);
[7c014d1]123 }
[a35b458]124
[7c014d1]125 return 1;
126}
127
[6d5e378]128/** Store one character to chargrid.
[7c014d1]129 *
130 * Its position is determined by scrbuf->col
131 * and scrbuf->row.
132 *
[6d5e378]133 * @param scrbuf Chargrid.
[7c014d1]134 * @param ch Character to store.
135 * @param update Update coordinates.
136 *
137 * @return Number of rows which have been affected. In usual
138 * situations this is 1. If the current position was
139 * updated to a new row, this value is 2.
140 *
141 */
[28a5ebd]142sysarg_t chargrid_putuchar(chargrid_t *scrbuf, char32_t ch, bool update)
[7c014d1]143{
144 assert(scrbuf->col < scrbuf->cols);
145 assert(scrbuf->row < scrbuf->rows);
[a35b458]146
[7c014d1]147 charfield_t *field =
[6d5e378]148 chargrid_charfield_at(scrbuf, scrbuf->col, scrbuf->row);
[a35b458]149
[7c014d1]150 field->ch = ch;
151 field->attrs = scrbuf->attrs;
152 field->flags |= CHAR_FLAG_DIRTY;
[a35b458]153
[7c014d1]154 if (update) {
155 scrbuf->col++;
[6d5e378]156 return chargrid_update_cols(scrbuf);
[7c014d1]157 }
[a35b458]158
[7c014d1]159 return 1;
160}
161
[6d5e378]162/** Jump to a new row in chargrid.
[7c014d1]163 *
[6d5e378]164 * @param scrbuf Chargrid.
[7c014d1]165 *
166 * @return Number of rows which have been affected. In usual
167 * situations this is 2 (the original row and the new
168 * row).
169 *
170 */
[6d5e378]171sysarg_t chargrid_newline(chargrid_t *scrbuf)
[7c014d1]172{
173 assert(scrbuf->col < scrbuf->cols);
174 assert(scrbuf->row < scrbuf->rows);
[a35b458]175
[7c014d1]176 scrbuf->col = 0;
177 scrbuf->row++;
[a35b458]178
[6d5e378]179 return chargrid_update_rows(scrbuf);
[7c014d1]180}
181
[6d5e378]182/** Jump to a new row in chargrid.
[7c014d1]183 *
[6d5e378]184 * @param scrbuf Chargrid.
[7c014d1]185 * @param tab_size Tab size.
186 *
187 * @return Number of rows which have been affected. In usual
188 * situations this is 1. If the current position was
189 * updated to a new row, this value is 2.
190 *
191 */
[6d5e378]192sysarg_t chargrid_tabstop(chargrid_t *scrbuf, sysarg_t tab_size)
[7c014d1]193{
194 assert(scrbuf->col < scrbuf->cols);
195 assert(scrbuf->row < scrbuf->rows);
[a35b458]196
[7c014d1]197 sysarg_t spaces = tab_size - scrbuf->cols % tab_size;
198 sysarg_t flush = 1;
[a35b458]199
[7c014d1]200 for (sysarg_t i = 0; i < spaces; i++)
[28a5ebd]201 flush += chargrid_putuchar(scrbuf, ' ', true) - 1;
[a35b458]202
[7c014d1]203 return flush;
204}
205
[6d5e378]206/** Jump to the previous character in chargrid.
[7c014d1]207 *
208 * Currently no scrollback is supported.
209 *
[6d5e378]210 * @param scrbuf Chargrid.
[7c014d1]211 *
212 * @return Number of rows which have been affected. In usual
213 * situations this is 1. If the current position was
214 * updated to the previous row, this value is 2.
215 * @return 0 if no backspace is possible.
216 *
217 */
[6d5e378]218sysarg_t chargrid_backspace(chargrid_t *scrbuf)
[7c014d1]219{
220 assert(scrbuf->col < scrbuf->cols);
221 assert(scrbuf->row < scrbuf->rows);
[a35b458]222
[7c014d1]223 if ((scrbuf->col == 0) && (scrbuf->row == 0))
224 return 0;
[a35b458]225
[7c014d1]226 if (scrbuf->col == 0) {
227 scrbuf->col = scrbuf->cols - 1;
228 scrbuf->row--;
[a35b458]229
[28a5ebd]230 chargrid_putuchar(scrbuf, ' ', false);
[7c014d1]231 return 2;
232 }
[a35b458]233
[7c014d1]234 scrbuf->col--;
[28a5ebd]235 chargrid_putuchar(scrbuf, ' ', false);
[7c014d1]236 return 1;
237}
238
[6d5e378]239/** Clear the chargrid.
[7c014d1]240 *
[6d5e378]241 * @param scrbuf Chargrid.
[7c014d1]242 *
243 */
[6d5e378]244void chargrid_clear(chargrid_t *scrbuf)
[7c014d1]245{
246 for (size_t pos = 0; pos < (scrbuf->cols * scrbuf->rows); pos++) {
247 scrbuf->data[pos].ch = 0;
248 scrbuf->data[pos].attrs = scrbuf->attrs;
249 scrbuf->data[pos].flags = CHAR_FLAG_DIRTY;
250 }
[a35b458]251
[7c014d1]252 scrbuf->col = 0;
253 scrbuf->row = 0;
254}
255
[6d5e378]256/** Update current chargrid coordinates
[7c014d1]257 *
[6d5e378]258 * @param scrbuf Chargrid.
[7c014d1]259 * @param col New column.
260 * @param row New row.
261 *
262 */
[6d5e378]263void chargrid_set_cursor(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
[7c014d1]264{
[68f1254c]265 if (col >= scrbuf->cols || row >= scrbuf->rows)
266 return;
267
[7c014d1]268 scrbuf->col = col;
269 scrbuf->row = row;
270}
271
[6d5e378]272void chargrid_set_cursor_visibility(chargrid_t *scrbuf, bool visible)
[7c014d1]273{
274 scrbuf->cursor_visible = visible;
275}
276
[6d5e378]277/** Get current chargrid coordinates
[7c014d1]278 *
[6d5e378]279 * @param scrbuf Chargrid.
[7c014d1]280 * @param col Column.
281 * @param row Row.
282 *
283 */
[6d5e378]284void chargrid_get_cursor(chargrid_t *scrbuf, sysarg_t *col,
[7c014d1]285 sysarg_t *row)
286{
287 assert(col);
288 assert(row);
[a35b458]289
[7c014d1]290 *col = scrbuf->col;
291 *row = scrbuf->row;
292}
293
[6d5e378]294bool chargrid_get_cursor_visibility(chargrid_t *scrbuf)
[7c014d1]295{
296 return scrbuf->cursor_visible;
297}
298
299/** Clear one buffer row.
300 *
[6d5e378]301 * @param scrbuf Chargrid.
[7c014d1]302 * @param row Row to clear.
303 *
304 */
[6d5e378]305void chargrid_clear_row(chargrid_t *scrbuf, sysarg_t row)
[7c014d1]306{
307 for (sysarg_t col = 0; col < scrbuf->cols; col++) {
308 charfield_t *field =
[6d5e378]309 chargrid_charfield_at(scrbuf, col, row);
[a35b458]310
[7c014d1]311 field->ch = 0;
312 field->attrs = scrbuf->attrs;
313 field->flags |= CHAR_FLAG_DIRTY;
314 }
315}
316
[6d5e378]317/** Set chargrid style.
[7c014d1]318 *
[6d5e378]319 * @param scrbuf Chargrid.
[7c014d1]320 * @param style Style.
321 *
322 */
[6d5e378]323void chargrid_set_style(chargrid_t *scrbuf, console_style_t style)
[7c014d1]324{
325 scrbuf->attrs.type = CHAR_ATTR_STYLE;
326 scrbuf->attrs.val.style = style;
327}
328
[6d5e378]329/** Set chargrid color.
[7c014d1]330 *
[6d5e378]331 * @param scrbuf Chargrid.
[7c014d1]332 * @param bgcolor Background color.
333 * @param fgcolor Foreground color.
334 * @param attr Color attribute.
335 *
336 */
[6d5e378]337void chargrid_set_color(chargrid_t *scrbuf, console_color_t bgcolor,
[7c014d1]338 console_color_t fgcolor, console_color_attr_t attr)
339{
340 scrbuf->attrs.type = CHAR_ATTR_INDEX;
341 scrbuf->attrs.val.index.bgcolor = bgcolor;
342 scrbuf->attrs.val.index.fgcolor = fgcolor;
343 scrbuf->attrs.val.index.attr = attr;
344}
345
[6d5e378]346/** Set chargrid RGB color.
[7c014d1]347 *
[6d5e378]348 * @param scrbuf Chargrid.
[7c014d1]349 * @param bgcolor Background color.
350 * @param fgcolor Foreground color.
351 *
352 */
[6d5e378]353void chargrid_set_rgb_color(chargrid_t *scrbuf, pixel_t bgcolor,
[7c014d1]354 pixel_t fgcolor)
355{
356 scrbuf->attrs.type = CHAR_ATTR_RGB;
357 scrbuf->attrs.val.rgb.bgcolor = bgcolor;
358 scrbuf->attrs.val.rgb.fgcolor = fgcolor;
359}
360
361/** @}
362 */
Note: See TracBrowser for help on using the repository browser.