[3993b3d] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
---|
[3993b3d] | 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 |
|
---|
[ce5bcb4] | 29 | /** @addtogroup console
|
---|
[1601f3c] | 30 | * @{
|
---|
[ce5bcb4] | 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[369a5f8] | 35 | #ifndef LIBC_SCREENBUFFER_H__
|
---|
| 36 | #define LIBC_SCREENBUFFER_H__
|
---|
[3993b3d] | 37 |
|
---|
[76fca31] | 38 | #include <stdint.h>
|
---|
[7ce3cb2] | 39 | #include <sys/types.h>
|
---|
[424cd43] | 40 | #include <bool.h>
|
---|
[3993b3d] | 41 |
|
---|
[369a5f8] | 42 | typedef enum {
|
---|
| 43 | at_style,
|
---|
| 44 | at_idx,
|
---|
| 45 | at_rgb
|
---|
| 46 | } attr_type_t;
|
---|
[3993b3d] | 47 |
|
---|
[9805cde] | 48 | typedef struct {
|
---|
| 49 | uint8_t style;
|
---|
| 50 | } attr_style_t;
|
---|
| 51 |
|
---|
| 52 | typedef struct {
|
---|
| 53 | uint8_t fg_color;
|
---|
| 54 | uint8_t bg_color;
|
---|
| 55 | uint8_t flags;
|
---|
| 56 | } attr_idx_t;
|
---|
| 57 |
|
---|
[3993b3d] | 58 | typedef struct {
|
---|
[1601f3c] | 59 | uint32_t bg_color; /**< background color */
|
---|
| 60 | uint32_t fg_color; /**< foreground color */
|
---|
[9805cde] | 61 | } attr_rgb_t;
|
---|
| 62 |
|
---|
[369a5f8] | 63 | typedef union {
|
---|
| 64 | attr_style_t s;
|
---|
| 65 | attr_idx_t i;
|
---|
| 66 | attr_rgb_t r;
|
---|
| 67 | } attr_val_t;
|
---|
| 68 |
|
---|
[9805cde] | 69 | typedef struct {
|
---|
[369a5f8] | 70 | attr_type_t t;
|
---|
| 71 | attr_val_t a;
|
---|
[9805cde] | 72 | } attrs_t;
|
---|
[3993b3d] | 73 |
|
---|
[a9bd960c] | 74 | /** One field on screen. It contain one character and its attributes. */
|
---|
[3993b3d] | 75 | typedef struct {
|
---|
[1601f3c] | 76 | wchar_t character; /**< Character itself */
|
---|
[424cd43] | 77 | attrs_t attrs; /**< Character attributes */
|
---|
[3993b3d] | 78 | } keyfield_t;
|
---|
| 79 |
|
---|
| 80 | /** Structure for buffering state of one virtual console.
|
---|
| 81 | */
|
---|
| 82 | typedef struct {
|
---|
[424cd43] | 83 | keyfield_t *buffer; /**< Screen content - characters and
|
---|
| 84 | their attributes (used as a circular buffer) */
|
---|
[96b02eb9] | 85 | sysarg_t size_x; /**< Number of columns */
|
---|
| 86 | sysarg_t size_y; /**< Number of rows */
|
---|
[1601f3c] | 87 |
|
---|
| 88 | /** Coordinates of last printed character for determining cursor position */
|
---|
[96b02eb9] | 89 | sysarg_t position_x;
|
---|
| 90 | sysarg_t position_y;
|
---|
[1601f3c] | 91 |
|
---|
[424cd43] | 92 | attrs_t attrs; /**< Current attributes. */
|
---|
| 93 | size_t top_line; /**< Points to buffer[][] line that will
|
---|
| 94 | be printed at screen as the first line */
|
---|
| 95 | bool is_cursor_visible; /**< Cursor state - default is visible */
|
---|
[3993b3d] | 96 | } screenbuffer_t;
|
---|
| 97 |
|
---|
[1601f3c] | 98 | /** Returns keyfield for position on screen
|
---|
| 99 | *
|
---|
| 100 | * Screenbuffer->buffer is cyclic buffer so we
|
---|
| 101 | * must couted in index of the topmost line.
|
---|
| 102 | *
|
---|
| 103 | * @param scr Screenbuffer
|
---|
| 104 | * @param x Position on screen
|
---|
| 105 | * @param y Position on screen
|
---|
| 106 | *
|
---|
| 107 | * @return Keyfield structure with character and its attributes on x, y
|
---|
| 108 | *
|
---|
[a9bd960c] | 109 | */
|
---|
[96b02eb9] | 110 | static inline keyfield_t *get_field_at(screenbuffer_t *scr, sysarg_t x, sysarg_t y)
|
---|
[3993b3d] | 111 | {
|
---|
| 112 | return scr->buffer + x + ((y + scr->top_line) % scr->size_y) * scr->size_x;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[9805cde] | 115 | /** Compares two sets of attributes.
|
---|
[1601f3c] | 116 | *
|
---|
| 117 | * @param s1 First style
|
---|
| 118 | * @param s2 Second style
|
---|
| 119 | *
|
---|
| 120 | * @return Nonzero on equality
|
---|
| 121 | *
|
---|
[a9bd960c] | 122 | */
|
---|
[369a5f8] | 123 | static inline bool attrs_same(attrs_t a1, attrs_t a2)
|
---|
[6b5111a] | 124 | {
|
---|
[1601f3c] | 125 | if (a1.t != a2.t)
|
---|
[369a5f8] | 126 | return false;
|
---|
[1601f3c] | 127 |
|
---|
[9805cde] | 128 | switch (a1.t) {
|
---|
[1601f3c] | 129 | case at_style:
|
---|
| 130 | return (a1.a.s.style == a2.a.s.style);
|
---|
| 131 | case at_idx:
|
---|
| 132 | return (a1.a.i.fg_color == a2.a.i.fg_color)
|
---|
| 133 | && (a1.a.i.bg_color == a2.a.i.bg_color)
|
---|
| 134 | && (a1.a.i.flags == a2.a.i.flags);
|
---|
| 135 | case at_rgb:
|
---|
| 136 | return (a1.a.r.fg_color == a2.a.r.fg_color)
|
---|
| 137 | && (a1.a.r.bg_color == a2.a.r.bg_color);
|
---|
[9805cde] | 138 | }
|
---|
[c063d96e] | 139 |
|
---|
[369a5f8] | 140 | return false;
|
---|
[6b5111a] | 141 | }
|
---|
| 142 |
|
---|
[9f1362d4] | 143 | extern void screenbuffer_putchar(screenbuffer_t *, wchar_t);
|
---|
[96b02eb9] | 144 | extern screenbuffer_t *screenbuffer_init(screenbuffer_t *, sysarg_t, sysarg_t);
|
---|
[3993b3d] | 145 |
|
---|
[9f1362d4] | 146 | extern void screenbuffer_clear(screenbuffer_t *);
|
---|
[96b02eb9] | 147 | extern void screenbuffer_clear_line(screenbuffer_t *, sysarg_t);
|
---|
[9f1362d4] | 148 | extern void screenbuffer_copy_buffer(screenbuffer_t *, keyfield_t *);
|
---|
[96b02eb9] | 149 | extern void screenbuffer_goto(screenbuffer_t *, sysarg_t, sysarg_t);
|
---|
[9f1362d4] | 150 | extern void screenbuffer_set_style(screenbuffer_t *, uint8_t);
|
---|
| 151 | extern void screenbuffer_set_color(screenbuffer_t *, uint8_t, uint8_t, uint8_t);
|
---|
| 152 | extern void screenbuffer_set_rgb_color(screenbuffer_t *, uint32_t, uint32_t);
|
---|
[3993b3d] | 153 |
|
---|
| 154 | #endif
|
---|
| 155 |
|
---|
[ce5bcb4] | 156 | /** @}
|
---|
| 157 | */
|
---|