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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since eca9fd0 was 6aeca0d, checked in by Jakub Jermar <jakub@…>, 9 years ago

Make as_area_create() accept phone to external pager

  • 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
[6d5e378]29/** @addtogroup libc
[7c014d1]30 * @{
31 */
32/** @file
33 */
34
35#include <io/style.h>
36#include <malloc.h>
37#include <unistd.h>
38#include <assert.h>
[3e6a98c5]39#include <stdbool.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;
[7c014d1]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 }
71
72 scrbuf->size = size;
73 scrbuf->flags = flags;
74 scrbuf->cols = cols;
75 scrbuf->rows = rows;
76 scrbuf->cursor_visible = false;
77
78 scrbuf->attrs.type = CHAR_ATTR_STYLE;
79 scrbuf->attrs.val.style = STYLE_NORMAL;
80
81 scrbuf->top_row = 0;
[6d5e378]82 chargrid_clear(scrbuf);
[7c014d1]83
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);
[7c014d1]109
110 return scrbuf->rows;
111 }
112
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 }
124
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 */
[6d5e378]142sysarg_t chargrid_putchar(chargrid_t *scrbuf, wchar_t ch, bool update)
[7c014d1]143{
144 assert(scrbuf->col < scrbuf->cols);
145 assert(scrbuf->row < scrbuf->rows);
146
147 charfield_t *field =
[6d5e378]148 chargrid_charfield_at(scrbuf, scrbuf->col, scrbuf->row);
[7c014d1]149
150 field->ch = ch;
151 field->attrs = scrbuf->attrs;
152 field->flags |= CHAR_FLAG_DIRTY;
153
154 if (update) {
155 scrbuf->col++;
[6d5e378]156 return chargrid_update_cols(scrbuf);
[7c014d1]157 }
158
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);
175
176 scrbuf->col = 0;
177 scrbuf->row++;
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);
196
197 sysarg_t spaces = tab_size - scrbuf->cols % tab_size;
198 sysarg_t flush = 1;
199
200 for (sysarg_t i = 0; i < spaces; i++)
[6d5e378]201 flush += chargrid_putchar(scrbuf, ' ', true) - 1;
[7c014d1]202
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);
222
223 if ((scrbuf->col == 0) && (scrbuf->row == 0))
224 return 0;
225
226 if (scrbuf->col == 0) {
227 scrbuf->col = scrbuf->cols - 1;
228 scrbuf->row--;
229
[6d5e378]230 chargrid_putchar(scrbuf, ' ', false);
[7c014d1]231 return 2;
232 }
233
234 scrbuf->col--;
[6d5e378]235 chargrid_putchar(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 }
251
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{
265 scrbuf->col = col;
266 scrbuf->row = row;
267}
268
[6d5e378]269void chargrid_set_cursor_visibility(chargrid_t *scrbuf, bool visible)
[7c014d1]270{
271 scrbuf->cursor_visible = visible;
272}
273
[6d5e378]274/** Get current chargrid coordinates
[7c014d1]275 *
[6d5e378]276 * @param scrbuf Chargrid.
[7c014d1]277 * @param col Column.
278 * @param row Row.
279 *
280 */
[6d5e378]281void chargrid_get_cursor(chargrid_t *scrbuf, sysarg_t *col,
[7c014d1]282 sysarg_t *row)
283{
284 assert(col);
285 assert(row);
286
287 *col = scrbuf->col;
288 *row = scrbuf->row;
289}
290
[6d5e378]291bool chargrid_get_cursor_visibility(chargrid_t *scrbuf)
[7c014d1]292{
293 return scrbuf->cursor_visible;
294}
295
296/** Clear one buffer row.
297 *
[6d5e378]298 * @param scrbuf Chargrid.
[7c014d1]299 * @param row Row to clear.
300 *
301 */
[6d5e378]302void chargrid_clear_row(chargrid_t *scrbuf, sysarg_t row)
[7c014d1]303{
304 for (sysarg_t col = 0; col < scrbuf->cols; col++) {
305 charfield_t *field =
[6d5e378]306 chargrid_charfield_at(scrbuf, col, row);
[7c014d1]307
308 field->ch = 0;
309 field->attrs = scrbuf->attrs;
310 field->flags |= CHAR_FLAG_DIRTY;
311 }
312}
313
[6d5e378]314/** Set chargrid style.
[7c014d1]315 *
[6d5e378]316 * @param scrbuf Chargrid.
[7c014d1]317 * @param style Style.
318 *
319 */
[6d5e378]320void chargrid_set_style(chargrid_t *scrbuf, console_style_t style)
[7c014d1]321{
322 scrbuf->attrs.type = CHAR_ATTR_STYLE;
323 scrbuf->attrs.val.style = style;
324}
325
[6d5e378]326/** Set chargrid color.
[7c014d1]327 *
[6d5e378]328 * @param scrbuf Chargrid.
[7c014d1]329 * @param bgcolor Background color.
330 * @param fgcolor Foreground color.
331 * @param attr Color attribute.
332 *
333 */
[6d5e378]334void chargrid_set_color(chargrid_t *scrbuf, console_color_t bgcolor,
[7c014d1]335 console_color_t fgcolor, console_color_attr_t attr)
336{
337 scrbuf->attrs.type = CHAR_ATTR_INDEX;
338 scrbuf->attrs.val.index.bgcolor = bgcolor;
339 scrbuf->attrs.val.index.fgcolor = fgcolor;
340 scrbuf->attrs.val.index.attr = attr;
341}
342
[6d5e378]343/** Set chargrid RGB color.
[7c014d1]344 *
[6d5e378]345 * @param scrbuf Chargrid.
[7c014d1]346 * @param bgcolor Background color.
347 * @param fgcolor Foreground color.
348 *
349 */
[6d5e378]350void chargrid_set_rgb_color(chargrid_t *scrbuf, pixel_t bgcolor,
[7c014d1]351 pixel_t fgcolor)
352{
353 scrbuf->attrs.type = CHAR_ATTR_RGB;
354 scrbuf->attrs.val.rgb.bgcolor = bgcolor;
355 scrbuf->attrs.val.rgb.fgcolor = fgcolor;
356}
357
358/** @}
359 */
Note: See TracBrowser for help on using the repository browser.