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