Rev 1526 | Rev 1575 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1526 | Rev 1553 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Josef Cejka |
2 | * Copyright (C) 2006 Josef Cejka |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <screenbuffer.h> |
29 | #include <screenbuffer.h> |
30 | #include <malloc.h> |
30 | #include <malloc.h> |
31 | #include <unistd.h> |
31 | #include <unistd.h> |
32 | 32 | ||
33 | /** Store one character to screenbuffer. Its position is determined by scr->position_x and scr->position_y. |
33 | /** Store one character to screenbuffer. Its position is determined by scr->position_x and scr->position_y. |
34 | * @param scr screenbuffer |
34 | * @param scr screenbuffer |
35 | * @param c stored character |
35 | * @param c stored character |
36 | */ |
36 | */ |
37 | void screenbuffer_putchar(screenbuffer_t *scr, char c) |
37 | void screenbuffer_putchar(screenbuffer_t *scr, char c) |
38 | { |
38 | { |
39 | keyfield_t *field; |
39 | keyfield_t *field; |
40 | 40 | ||
41 | field = get_field_at(scr, scr->position_x, scr->position_y); |
41 | field = get_field_at(scr, scr->position_x, scr->position_y); |
42 | 42 | ||
43 | field->character = c; |
43 | field->character = c; |
44 | field->style = scr->style; |
44 | field->style = scr->style; |
45 | } |
45 | } |
46 | 46 | ||
47 | /** Initilize screenbuffer. Allocate space for screen content in accordance to given size. |
47 | /** Initilize screenbuffer. Allocate space for screen content in accordance to given size. |
48 | * @param scr initialized screenbuffer |
48 | * @param scr initialized screenbuffer |
49 | * @param size_x width in characters |
49 | * @param size_x width in characters |
50 | * @param size_y height in characters |
50 | * @param size_y height in characters |
51 | * @return pointer to screenbuffer (same as scr parameter) or NULL |
51 | * @return pointer to screenbuffer (same as scr parameter) or NULL |
52 | */ |
52 | */ |
53 | screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, int size_x, int size_y) |
53 | screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, int size_x, int size_y) |
54 | { |
54 | { |
55 | if ((scr->buffer = (keyfield_t *)malloc(sizeof(keyfield_t) * size_x * size_y)) == NULL) { |
55 | if ((scr->buffer = (keyfield_t *)malloc(sizeof(keyfield_t) * size_x * size_y)) == NULL) { |
56 | return NULL; |
56 | return NULL; |
57 | } |
57 | } |
58 | 58 | ||
59 | scr->size_x = size_x; |
59 | scr->size_x = size_x; |
60 | scr->size_y = size_y; |
60 | scr->size_y = size_y; |
61 | scr->style.fg_color = DEFAULT_FOREGROUND_COLOR; |
61 | scr->style.fg_color = DEFAULT_FOREGROUND; |
62 | scr->style.bg_color = DEFAULT_BACKGROUND_COLOR; |
62 | scr->style.bg_color = DEFAULT_BACKGROUND; |
63 | 63 | ||
64 | screenbuffer_clear(scr); |
64 | screenbuffer_clear(scr); |
65 | 65 | ||
66 | return scr; |
66 | return scr; |
67 | } |
67 | } |
68 | 68 | ||
69 | /** Clear screenbuffer. |
69 | /** Clear screenbuffer. |
70 | * @param scr screenbuffer |
70 | * @param scr screenbuffer |
71 | */ |
71 | */ |
72 | void screenbuffer_clear(screenbuffer_t *scr) |
72 | void screenbuffer_clear(screenbuffer_t *scr) |
73 | { |
73 | { |
74 | unsigned int i; |
74 | unsigned int i; |
75 | 75 | ||
76 | for (i = 0; i < (scr->size_x * scr->size_y); i++) { |
76 | for (i = 0; i < (scr->size_x * scr->size_y); i++) { |
77 | scr->buffer[i].character = ' '; |
77 | scr->buffer[i].character = ' '; |
78 | scr->buffer[i].style = scr->style; |
78 | scr->buffer[i].style = scr->style; |
79 | } |
79 | } |
80 | 80 | ||
81 | scr->top_line = 0; |
81 | scr->top_line = 0; |
82 | scr->position_y = 0; |
82 | scr->position_y = 0; |
83 | scr->position_x = 0; |
83 | scr->position_x = 0; |
84 | } |
84 | } |
85 | 85 | ||
86 | /** Clear one buffer line. |
86 | /** Clear one buffer line. |
87 | * @param scr |
87 | * @param scr |
88 | * @param line One buffer line (not a screen line!) |
88 | * @param line One buffer line (not a screen line!) |
89 | */ |
89 | */ |
90 | void screenbuffer_clear_line(screenbuffer_t *scr, unsigned int line) |
90 | void screenbuffer_clear_line(screenbuffer_t *scr, unsigned int line) |
91 | { |
91 | { |
92 | unsigned int i; |
92 | unsigned int i; |
93 | 93 | ||
94 | for (i = 0; i < scr->size_x; i++) { |
94 | for (i = 0; i < scr->size_x; i++) { |
95 | scr->buffer[i + line*scr->size_x].character = ' '; |
95 | scr->buffer[i + line*scr->size_x].character = ' '; |
96 | scr->buffer[i + line*scr->size_x].style = scr->style; |
96 | scr->buffer[i + line*scr->size_x].style = scr->style; |
97 | } |
97 | } |
98 | } |
98 | } |
99 | 99 | ||
100 | /** Copy content buffer from screenbuffer to given memory. |
100 | /** Copy content buffer from screenbuffer to given memory. |
101 | * @param scr source screenbuffer |
101 | * @param scr source screenbuffer |
102 | * @param dest destination |
102 | * @param dest destination |
103 | */ |
103 | */ |
104 | void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest) |
104 | void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest) |
105 | { |
105 | { |
106 | unsigned int i; |
106 | unsigned int i; |
107 | 107 | ||
108 | for (i = 0; i < scr->size_x * scr->size_y; i++) { |
108 | for (i = 0; i < scr->size_x * scr->size_y; i++) { |
109 | dest[i] = scr->buffer[i]; |
109 | dest[i] = scr->buffer[i]; |
110 | } |
110 | } |
111 | } |
111 | } |
112 | 112 | ||
113 | /** Set new cursor position in screenbuffer. |
113 | /** Set new cursor position in screenbuffer. |
114 | * @param scr |
114 | * @param scr |
115 | * @param x |
115 | * @param x |
116 | * @param y |
116 | * @param y |
117 | */ |
117 | */ |
118 | void screenbuffer_goto(screenbuffer_t *scr, unsigned int x, unsigned int y) |
118 | void screenbuffer_goto(screenbuffer_t *scr, unsigned int x, unsigned int y) |
119 | { |
119 | { |
120 | scr->position_x = x % scr->size_x; |
120 | scr->position_x = x % scr->size_x; |
121 | scr->position_y = y % scr->size_y; |
121 | scr->position_y = y % scr->size_y; |
122 | } |
122 | } |
123 | 123 | ||
124 | /** Set new style. |
124 | /** Set new style. |
125 | * @param scr |
125 | * @param scr |
126 | * @param fg_color |
126 | * @param fg_color |
127 | * @param bg_color |
127 | * @param bg_color |
128 | */ |
128 | */ |
129 | void screenbuffer_set_style(screenbuffer_t *scr, unsigned int fg_color, unsigned int bg_color) |
129 | void screenbuffer_set_style(screenbuffer_t *scr, unsigned int fg_color, unsigned int bg_color) |
130 | { |
130 | { |
131 | scr->style.fg_color = fg_color; |
131 | scr->style.fg_color = fg_color; |
132 | scr->style.bg_color = bg_color; |
132 | scr->style.bg_color = bg_color; |
133 | } |
133 | } |
134 | 134 | ||
135 | 135 |