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