[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] | 53 | chargrid_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,
|
---|
[fbcdeb8] | 62 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
|
---|
[faba839] | 63 | if (scrbuf == AS_MAP_FAILED)
|
---|
[7c014d1] | 64 | return NULL;
|
---|
| 65 | } else {
|
---|
[6d5e378] | 66 | scrbuf = (chargrid_t *) malloc(size);
|
---|
[7c014d1] | 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;
|
---|
[6d5e378] | 81 | chargrid_clear(scrbuf);
|
---|
[7c014d1] | 82 |
|
---|
| 83 | return scrbuf;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[6d5e378] | 86 | void chargrid_destroy(chargrid_t *srcbuf)
|
---|
[7c014d1] | 87 | {
|
---|
[6d5e378] | 88 | // TODO
|
---|
[7c014d1] | 89 | }
|
---|
| 90 |
|
---|
[6d5e378] | 91 | bool chargrid_cursor_at(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
|
---|
[7c014d1] | 92 | {
|
---|
| 93 | return ((scrbuf->cursor_visible) && (scrbuf->col == col) &&
|
---|
| 94 | (scrbuf->row == row));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[6d5e378] | 97 | sysarg_t chargrid_get_top_row(chargrid_t *scrbuf)
|
---|
[7c014d1] | 98 | {
|
---|
| 99 | return scrbuf->top_row;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[6d5e378] | 102 | static sysarg_t chargrid_update_rows(chargrid_t *scrbuf)
|
---|
[7c014d1] | 103 | {
|
---|
| 104 | if (scrbuf->row == scrbuf->rows) {
|
---|
| 105 | scrbuf->row = scrbuf->rows - 1;
|
---|
| 106 | scrbuf->top_row = (scrbuf->top_row + 1) % scrbuf->rows;
|
---|
[6d5e378] | 107 | chargrid_clear_row(scrbuf, scrbuf->row);
|
---|
[7c014d1] | 108 |
|
---|
| 109 | return scrbuf->rows;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | return 2;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[6d5e378] | 115 | static sysarg_t chargrid_update_cols(chargrid_t *scrbuf)
|
---|
[7c014d1] | 116 | {
|
---|
| 117 | /* Column overflow */
|
---|
| 118 | if (scrbuf->col == scrbuf->cols) {
|
---|
| 119 | scrbuf->col = 0;
|
---|
| 120 | scrbuf->row++;
|
---|
[6d5e378] | 121 | return chargrid_update_rows(scrbuf);
|
---|
[7c014d1] | 122 | }
|
---|
| 123 |
|
---|
| 124 | return 1;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[6d5e378] | 127 | /** Store one character to chargrid.
|
---|
[7c014d1] | 128 | *
|
---|
| 129 | * Its position is determined by scrbuf->col
|
---|
| 130 | * and scrbuf->row.
|
---|
| 131 | *
|
---|
[6d5e378] | 132 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 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 | */
|
---|
[6d5e378] | 141 | sysarg_t chargrid_putchar(chargrid_t *scrbuf, wchar_t ch, bool update)
|
---|
[7c014d1] | 142 | {
|
---|
| 143 | assert(scrbuf->col < scrbuf->cols);
|
---|
| 144 | assert(scrbuf->row < scrbuf->rows);
|
---|
| 145 |
|
---|
| 146 | charfield_t *field =
|
---|
[6d5e378] | 147 | chargrid_charfield_at(scrbuf, scrbuf->col, scrbuf->row);
|
---|
[7c014d1] | 148 |
|
---|
| 149 | field->ch = ch;
|
---|
| 150 | field->attrs = scrbuf->attrs;
|
---|
| 151 | field->flags |= CHAR_FLAG_DIRTY;
|
---|
| 152 |
|
---|
| 153 | if (update) {
|
---|
| 154 | scrbuf->col++;
|
---|
[6d5e378] | 155 | return chargrid_update_cols(scrbuf);
|
---|
[7c014d1] | 156 | }
|
---|
| 157 |
|
---|
| 158 | return 1;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[6d5e378] | 161 | /** Jump to a new row in chargrid.
|
---|
[7c014d1] | 162 | *
|
---|
[6d5e378] | 163 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 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 | */
|
---|
[6d5e378] | 170 | sysarg_t chargrid_newline(chargrid_t *scrbuf)
|
---|
[7c014d1] | 171 | {
|
---|
| 172 | assert(scrbuf->col < scrbuf->cols);
|
---|
| 173 | assert(scrbuf->row < scrbuf->rows);
|
---|
| 174 |
|
---|
| 175 | scrbuf->col = 0;
|
---|
| 176 | scrbuf->row++;
|
---|
| 177 |
|
---|
[6d5e378] | 178 | return chargrid_update_rows(scrbuf);
|
---|
[7c014d1] | 179 | }
|
---|
| 180 |
|
---|
[6d5e378] | 181 | /** Jump to a new row in chargrid.
|
---|
[7c014d1] | 182 | *
|
---|
[6d5e378] | 183 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 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 | */
|
---|
[6d5e378] | 191 | sysarg_t chargrid_tabstop(chargrid_t *scrbuf, sysarg_t tab_size)
|
---|
[7c014d1] | 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++)
|
---|
[6d5e378] | 200 | flush += chargrid_putchar(scrbuf, ' ', true) - 1;
|
---|
[7c014d1] | 201 |
|
---|
| 202 | return flush;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[6d5e378] | 205 | /** Jump to the previous character in chargrid.
|
---|
[7c014d1] | 206 | *
|
---|
| 207 | * Currently no scrollback is supported.
|
---|
| 208 | *
|
---|
[6d5e378] | 209 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 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 | */
|
---|
[6d5e378] | 217 | sysarg_t chargrid_backspace(chargrid_t *scrbuf)
|
---|
[7c014d1] | 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 |
|
---|
[6d5e378] | 229 | chargrid_putchar(scrbuf, ' ', false);
|
---|
[7c014d1] | 230 | return 2;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | scrbuf->col--;
|
---|
[6d5e378] | 234 | chargrid_putchar(scrbuf, ' ', false);
|
---|
[7c014d1] | 235 | return 1;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[6d5e378] | 238 | /** Clear the chargrid.
|
---|
[7c014d1] | 239 | *
|
---|
[6d5e378] | 240 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 241 | *
|
---|
| 242 | */
|
---|
[6d5e378] | 243 | void chargrid_clear(chargrid_t *scrbuf)
|
---|
[7c014d1] | 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 |
|
---|
[6d5e378] | 255 | /** Update current chargrid coordinates
|
---|
[7c014d1] | 256 | *
|
---|
[6d5e378] | 257 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 258 | * @param col New column.
|
---|
| 259 | * @param row New row.
|
---|
| 260 | *
|
---|
| 261 | */
|
---|
[6d5e378] | 262 | void chargrid_set_cursor(chargrid_t *scrbuf, sysarg_t col, sysarg_t row)
|
---|
[7c014d1] | 263 | {
|
---|
| 264 | scrbuf->col = col;
|
---|
| 265 | scrbuf->row = row;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[6d5e378] | 268 | void chargrid_set_cursor_visibility(chargrid_t *scrbuf, bool visible)
|
---|
[7c014d1] | 269 | {
|
---|
| 270 | scrbuf->cursor_visible = visible;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[6d5e378] | 273 | /** Get current chargrid coordinates
|
---|
[7c014d1] | 274 | *
|
---|
[6d5e378] | 275 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 276 | * @param col Column.
|
---|
| 277 | * @param row Row.
|
---|
| 278 | *
|
---|
| 279 | */
|
---|
[6d5e378] | 280 | void chargrid_get_cursor(chargrid_t *scrbuf, sysarg_t *col,
|
---|
[7c014d1] | 281 | sysarg_t *row)
|
---|
| 282 | {
|
---|
| 283 | assert(col);
|
---|
| 284 | assert(row);
|
---|
| 285 |
|
---|
| 286 | *col = scrbuf->col;
|
---|
| 287 | *row = scrbuf->row;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[6d5e378] | 290 | bool chargrid_get_cursor_visibility(chargrid_t *scrbuf)
|
---|
[7c014d1] | 291 | {
|
---|
| 292 | return scrbuf->cursor_visible;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | /** Clear one buffer row.
|
---|
| 296 | *
|
---|
[6d5e378] | 297 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 298 | * @param row Row to clear.
|
---|
| 299 | *
|
---|
| 300 | */
|
---|
[6d5e378] | 301 | void chargrid_clear_row(chargrid_t *scrbuf, sysarg_t row)
|
---|
[7c014d1] | 302 | {
|
---|
| 303 | for (sysarg_t col = 0; col < scrbuf->cols; col++) {
|
---|
| 304 | charfield_t *field =
|
---|
[6d5e378] | 305 | chargrid_charfield_at(scrbuf, col, row);
|
---|
[7c014d1] | 306 |
|
---|
| 307 | field->ch = 0;
|
---|
| 308 | field->attrs = scrbuf->attrs;
|
---|
| 309 | field->flags |= CHAR_FLAG_DIRTY;
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[6d5e378] | 313 | /** Set chargrid style.
|
---|
[7c014d1] | 314 | *
|
---|
[6d5e378] | 315 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 316 | * @param style Style.
|
---|
| 317 | *
|
---|
| 318 | */
|
---|
[6d5e378] | 319 | void chargrid_set_style(chargrid_t *scrbuf, console_style_t style)
|
---|
[7c014d1] | 320 | {
|
---|
| 321 | scrbuf->attrs.type = CHAR_ATTR_STYLE;
|
---|
| 322 | scrbuf->attrs.val.style = style;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[6d5e378] | 325 | /** Set chargrid color.
|
---|
[7c014d1] | 326 | *
|
---|
[6d5e378] | 327 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 328 | * @param bgcolor Background color.
|
---|
| 329 | * @param fgcolor Foreground color.
|
---|
| 330 | * @param attr Color attribute.
|
---|
| 331 | *
|
---|
| 332 | */
|
---|
[6d5e378] | 333 | void chargrid_set_color(chargrid_t *scrbuf, console_color_t bgcolor,
|
---|
[7c014d1] | 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 |
|
---|
[6d5e378] | 342 | /** Set chargrid RGB color.
|
---|
[7c014d1] | 343 | *
|
---|
[6d5e378] | 344 | * @param scrbuf Chargrid.
|
---|
[7c014d1] | 345 | * @param bgcolor Background color.
|
---|
| 346 | * @param fgcolor Foreground color.
|
---|
| 347 | *
|
---|
| 348 | */
|
---|
[6d5e378] | 349 | void chargrid_set_rgb_color(chargrid_t *scrbuf, pixel_t bgcolor,
|
---|
[7c014d1] | 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 | */
|
---|