[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 |
|
---|
[3993b3d] | 35 | #include <screenbuffer.h>
|
---|
[424cd43] | 36 | #include <io/style.h>
|
---|
[3993b3d] | 37 | #include <malloc.h>
|
---|
| 38 | #include <unistd.h>
|
---|
| 39 |
|
---|
[1601f3c] | 40 | /** Store one character to screenbuffer.
|
---|
| 41 | *
|
---|
| 42 | * Its position is determined by scr->position_x
|
---|
| 43 | * and scr->position_y.
|
---|
| 44 | *
|
---|
| 45 | * @param scr Screenbuffer
|
---|
| 46 | * @param c Stored character
|
---|
[c738d65] | 47 | *
|
---|
[3993b3d] | 48 | */
|
---|
[7ce3cb2] | 49 | void screenbuffer_putchar(screenbuffer_t *scr, wchar_t ch)
|
---|
[3993b3d] | 50 | {
|
---|
[1601f3c] | 51 | keyfield_t *field =
|
---|
| 52 | get_field_at(scr, scr->position_x, scr->position_y);
|
---|
| 53 |
|
---|
[7ce3cb2] | 54 | field->character = ch;
|
---|
[9805cde] | 55 | field->attrs = scr->attrs;
|
---|
[3993b3d] | 56 | }
|
---|
| 57 |
|
---|
[1601f3c] | 58 | /** Initilize screenbuffer.
|
---|
| 59 | *
|
---|
| 60 | * Allocate space for screen content in accordance to given size.
|
---|
| 61 | *
|
---|
| 62 | * @param scr Initialized screenbuffer
|
---|
| 63 | * @param size_x Width in characters
|
---|
| 64 | * @param size_y Height in characters
|
---|
| 65 | *
|
---|
| 66 | * @return Pointer to screenbuffer (same as scr parameter) or NULL
|
---|
| 67 | *
|
---|
[a9bd960c] | 68 | */
|
---|
[424cd43] | 69 | screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, size_t size_x, size_t size_y)
|
---|
[3993b3d] | 70 | {
|
---|
[c738d65] | 71 | scr->buffer = (keyfield_t *) malloc(sizeof(keyfield_t) * size_x * size_y);
|
---|
[1601f3c] | 72 | if (!scr->buffer)
|
---|
[3993b3d] | 73 | return NULL;
|
---|
| 74 |
|
---|
| 75 | scr->size_x = size_x;
|
---|
| 76 | scr->size_y = size_y;
|
---|
[9805cde] | 77 | scr->attrs.t = at_style;
|
---|
| 78 | scr->attrs.a.s.style = STYLE_NORMAL;
|
---|
[a8b2b5b2] | 79 | scr->is_cursor_visible = 1;
|
---|
[10569b1] | 80 |
|
---|
| 81 | screenbuffer_clear(scr);
|
---|
| 82 |
|
---|
[3993b3d] | 83 | return scr;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[1601f3c] | 86 | /** Clear screenbuffer.
|
---|
| 87 | *
|
---|
| 88 | * @param scr Screenbuffer
|
---|
| 89 | *
|
---|
[e87e18f] | 90 | */
|
---|
[3993b3d] | 91 | void screenbuffer_clear(screenbuffer_t *scr)
|
---|
| 92 | {
|
---|
[424cd43] | 93 | size_t i;
|
---|
[3993b3d] | 94 |
|
---|
[10569b1] | 95 | for (i = 0; i < (scr->size_x * scr->size_y); i++) {
|
---|
[3993b3d] | 96 | scr->buffer[i].character = ' ';
|
---|
[9805cde] | 97 | scr->buffer[i].attrs = scr->attrs;
|
---|
[3993b3d] | 98 | }
|
---|
[1601f3c] | 99 |
|
---|
[3993b3d] | 100 | scr->top_line = 0;
|
---|
| 101 | scr->position_x = 0;
|
---|
[424cd43] | 102 | scr->position_y = 0;
|
---|
[3993b3d] | 103 | }
|
---|
| 104 |
|
---|
[e87e18f] | 105 | /** Clear one buffer line.
|
---|
[1601f3c] | 106 | *
|
---|
[3993b3d] | 107 | * @param scr
|
---|
| 108 | * @param line One buffer line (not a screen line!)
|
---|
[1601f3c] | 109 | *
|
---|
[3993b3d] | 110 | */
|
---|
[424cd43] | 111 | void screenbuffer_clear_line(screenbuffer_t *scr, size_t line)
|
---|
[3993b3d] | 112 | {
|
---|
[424cd43] | 113 | size_t x;
|
---|
[3993b3d] | 114 |
|
---|
[424cd43] | 115 | for (x = 0; x < scr->size_x; x++) {
|
---|
| 116 | scr->buffer[x + line * scr->size_x].character = ' ';
|
---|
| 117 | scr->buffer[x + line * scr->size_x].attrs = scr->attrs;
|
---|
[3993b3d] | 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[e87e18f] | 121 | /** Copy content buffer from screenbuffer to given memory.
|
---|
[1601f3c] | 122 | *
|
---|
| 123 | * @param scr Source screenbuffer
|
---|
| 124 | * @param dest Destination
|
---|
| 125 | *
|
---|
[e87e18f] | 126 | */
|
---|
[424cd43] | 127 | void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest)
|
---|
[3993b3d] | 128 | {
|
---|
[424cd43] | 129 | size_t i;
|
---|
[3993b3d] | 130 |
|
---|
[424cd43] | 131 | for (i = 0; i < (scr->size_x * scr->size_y); i++)
|
---|
[3993b3d] | 132 | dest[i] = scr->buffer[i];
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[e87e18f] | 135 | /** Set new cursor position in screenbuffer.
|
---|
[1601f3c] | 136 | *
|
---|
[e87e18f] | 137 | * @param scr
|
---|
| 138 | * @param x
|
---|
| 139 | * @param y
|
---|
[1601f3c] | 140 | *
|
---|
[e87e18f] | 141 | */
|
---|
[424cd43] | 142 | void screenbuffer_goto(screenbuffer_t *scr, size_t x, size_t y)
|
---|
[3993b3d] | 143 | {
|
---|
| 144 | scr->position_x = x % scr->size_x;
|
---|
[a9bd960c] | 145 | scr->position_y = y % scr->size_y;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[e87e18f] | 148 | /** Set new style.
|
---|
[1601f3c] | 149 | *
|
---|
[e87e18f] | 150 | * @param scr
|
---|
| 151 | * @param fg_color
|
---|
| 152 | * @param bg_color
|
---|
[1601f3c] | 153 | *
|
---|
[e87e18f] | 154 | */
|
---|
[424cd43] | 155 | void screenbuffer_set_style(screenbuffer_t *scr, uint8_t style)
|
---|
[a9bd960c] | 156 | {
|
---|
[9805cde] | 157 | scr->attrs.t = at_style;
|
---|
| 158 | scr->attrs.a.s.style = style;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /** Set new color.
|
---|
[1601f3c] | 162 | *
|
---|
[9805cde] | 163 | * @param scr
|
---|
| 164 | * @param fg_color
|
---|
| 165 | * @param bg_color
|
---|
[1601f3c] | 166 | *
|
---|
[9805cde] | 167 | */
|
---|
[424cd43] | 168 | void screenbuffer_set_color(screenbuffer_t *scr, uint8_t fg_color, uint8_t bg_color, uint8_t flags)
|
---|
[9805cde] | 169 | {
|
---|
| 170 | scr->attrs.t = at_idx;
|
---|
| 171 | scr->attrs.a.i.fg_color = fg_color;
|
---|
| 172 | scr->attrs.a.i.bg_color = bg_color;
|
---|
| 173 | scr->attrs.a.i.flags = flags;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /** Set new RGB color.
|
---|
[1601f3c] | 177 | *
|
---|
[9805cde] | 178 | * @param scr
|
---|
| 179 | * @param fg_color
|
---|
| 180 | * @param bg_color
|
---|
[1601f3c] | 181 | *
|
---|
[9805cde] | 182 | */
|
---|
[424cd43] | 183 | void screenbuffer_set_rgb_color(screenbuffer_t *scr, uint32_t fg_color, uint32_t bg_color)
|
---|
[9805cde] | 184 | {
|
---|
| 185 | scr->attrs.t = at_rgb;
|
---|
| 186 | scr->attrs.a.r.fg_color = fg_color;
|
---|
| 187 | scr->attrs.a.r.bg_color = bg_color;
|
---|
[3993b3d] | 188 | }
|
---|
| 189 |
|
---|
[ce5bcb4] | 190 | /** @}
|
---|
| 191 | */
|
---|