| 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 | 
 | 
|---|
| 29 | /** @addtogroup console
 | 
|---|
| 30 |  * @{
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | /** @file
 | 
|---|
| 33 |  */
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include <io/style.h>
 | 
|---|
| 36 | #include <io/screenbuffer.h>
 | 
|---|
| 37 | #include <malloc.h>
 | 
|---|
| 38 | #include <unistd.h>
 | 
|---|
| 39 | 
 | 
|---|
| 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
 | 
|---|
| 47 |  *
 | 
|---|
| 48 |  */
 | 
|---|
| 49 | void screenbuffer_putchar(screenbuffer_t *scr, wchar_t ch)
 | 
|---|
| 50 | {
 | 
|---|
| 51 |         keyfield_t *field =
 | 
|---|
| 52 |             get_field_at(scr, scr->position_x, scr->position_y);
 | 
|---|
| 53 |         
 | 
|---|
| 54 |         field->character = ch;
 | 
|---|
| 55 |         field->attrs = scr->attrs;
 | 
|---|
| 56 | }
 | 
|---|
| 57 | 
 | 
|---|
| 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 |  *
 | 
|---|
| 68 |  */
 | 
|---|
| 69 | screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, ipcarg_t size_x,
 | 
|---|
| 70 |     ipcarg_t size_y)
 | 
|---|
| 71 | {
 | 
|---|
| 72 |         scr->buffer = (keyfield_t *) malloc(sizeof(keyfield_t) * size_x * size_y);
 | 
|---|
| 73 |         if (!scr->buffer)
 | 
|---|
| 74 |                 return NULL;
 | 
|---|
| 75 |         
 | 
|---|
| 76 |         scr->size_x = size_x;
 | 
|---|
| 77 |         scr->size_y = size_y;
 | 
|---|
| 78 |         scr->attrs.t = at_style;
 | 
|---|
| 79 |         scr->attrs.a.s.style = STYLE_NORMAL;
 | 
|---|
| 80 |         scr->is_cursor_visible = 1;
 | 
|---|
| 81 |         
 | 
|---|
| 82 |         screenbuffer_clear(scr);
 | 
|---|
| 83 |         
 | 
|---|
| 84 |         return scr;
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | /** Clear screenbuffer.
 | 
|---|
| 88 |  *
 | 
|---|
| 89 |  * @param scr Screenbuffer
 | 
|---|
| 90 |  *
 | 
|---|
| 91 |  */
 | 
|---|
| 92 | void screenbuffer_clear(screenbuffer_t *scr)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |         size_t i;
 | 
|---|
| 95 |         
 | 
|---|
| 96 |         for (i = 0; i < (scr->size_x * scr->size_y); i++) {
 | 
|---|
| 97 |                 scr->buffer[i].character = ' ';
 | 
|---|
| 98 |                 scr->buffer[i].attrs = scr->attrs;
 | 
|---|
| 99 |         }
 | 
|---|
| 100 |         
 | 
|---|
| 101 |         scr->top_line = 0;
 | 
|---|
| 102 |         scr->position_x = 0;
 | 
|---|
| 103 |         scr->position_y = 0;
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | /** Clear one buffer line.
 | 
|---|
| 107 |  *
 | 
|---|
| 108 |  * @param scr
 | 
|---|
| 109 |  * @param line One buffer line (not a screen line!)
 | 
|---|
| 110 |  *
 | 
|---|
| 111 |  */
 | 
|---|
| 112 | void screenbuffer_clear_line(screenbuffer_t *scr, ipcarg_t line)
 | 
|---|
| 113 | {
 | 
|---|
| 114 |         ipcarg_t x;
 | 
|---|
| 115 |         
 | 
|---|
| 116 |         for (x = 0; x < scr->size_x; x++) {
 | 
|---|
| 117 |                 scr->buffer[x + line * scr->size_x].character = ' ';
 | 
|---|
| 118 |                 scr->buffer[x + line * scr->size_x].attrs = scr->attrs;
 | 
|---|
| 119 |         }
 | 
|---|
| 120 | }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | /** Copy content buffer from screenbuffer to given memory.
 | 
|---|
| 123 |  *
 | 
|---|
| 124 |  * @param scr  Source screenbuffer
 | 
|---|
| 125 |  * @param dest Destination
 | 
|---|
| 126 |  *
 | 
|---|
| 127 |  */
 | 
|---|
| 128 | void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest)
 | 
|---|
| 129 | {
 | 
|---|
| 130 |         size_t i;
 | 
|---|
| 131 |         
 | 
|---|
| 132 |         for (i = 0; i < (scr->size_x * scr->size_y); i++)
 | 
|---|
| 133 |                 dest[i] = scr->buffer[i];
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | /** Set new cursor position in screenbuffer.
 | 
|---|
| 137 |  *
 | 
|---|
| 138 |  * @param scr
 | 
|---|
| 139 |  * @param x
 | 
|---|
| 140 |  * @param y
 | 
|---|
| 141 |  *
 | 
|---|
| 142 |  */
 | 
|---|
| 143 | void screenbuffer_goto(screenbuffer_t *scr, ipcarg_t x, ipcarg_t y)
 | 
|---|
| 144 | {
 | 
|---|
| 145 |         scr->position_x = x % scr->size_x;
 | 
|---|
| 146 |         scr->position_y = y % scr->size_y;
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | /** Set new style.
 | 
|---|
| 150 |  *
 | 
|---|
| 151 |  * @param scr
 | 
|---|
| 152 |  * @param fg_color
 | 
|---|
| 153 |  * @param bg_color
 | 
|---|
| 154 |  *
 | 
|---|
| 155 |  */
 | 
|---|
| 156 | void screenbuffer_set_style(screenbuffer_t *scr, uint8_t style)
 | 
|---|
| 157 | {
 | 
|---|
| 158 |         scr->attrs.t = at_style;
 | 
|---|
| 159 |         scr->attrs.a.s.style = style;
 | 
|---|
| 160 | }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | /** Set new color.
 | 
|---|
| 163 |  *
 | 
|---|
| 164 |  * @param scr
 | 
|---|
| 165 |  * @param fg_color
 | 
|---|
| 166 |  * @param bg_color
 | 
|---|
| 167 |  *
 | 
|---|
| 168 |  */
 | 
|---|
| 169 | void screenbuffer_set_color(screenbuffer_t *scr, uint8_t fg_color,
 | 
|---|
| 170 |     uint8_t bg_color, uint8_t flags)
 | 
|---|
| 171 | {
 | 
|---|
| 172 |         scr->attrs.t = at_idx;
 | 
|---|
| 173 |         scr->attrs.a.i.fg_color = fg_color;
 | 
|---|
| 174 |         scr->attrs.a.i.bg_color = bg_color;
 | 
|---|
| 175 |         scr->attrs.a.i.flags = flags;
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | /** Set new RGB color.
 | 
|---|
| 179 |  *
 | 
|---|
| 180 |  * @param scr
 | 
|---|
| 181 |  * @param fg_color
 | 
|---|
| 182 |  * @param bg_color
 | 
|---|
| 183 |  *
 | 
|---|
| 184 |  */
 | 
|---|
| 185 | void screenbuffer_set_rgb_color(screenbuffer_t *scr, uint32_t fg_color,
 | 
|---|
| 186 |     uint32_t bg_color)
 | 
|---|
| 187 | {
 | 
|---|
| 188 |         scr->attrs.t = at_rgb;
 | 
|---|
| 189 |         scr->attrs.a.r.fg_color = fg_color;
 | 
|---|
| 190 |         scr->attrs.a.r.bg_color = bg_color;
 | 
|---|
| 191 | }
 | 
|---|
| 192 | 
 | 
|---|
| 193 | /** @}
 | 
|---|
| 194 |  */
 | 
|---|