c4ad195956de109947f58f8eb43b5c4b79695e94
[cascardo/ovs.git] / extras / ezio / ezio.c
1 /* Copyright (c) 2008, 2009 Nicira Networks, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17 #include "ezio.h"
18 #include <assert.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include "util.h"
22
23 static void remove_elements(uint8_t *p, size_t n_elems, size_t elem_size,
24                             int pos, int n_del);
25 static void insert_elements(uint8_t *p, size_t n_elems, size_t elem_size,
26                             int pos, int n_insert);
27 static int range(int value, int min, int max);
28
29 void
30 ezio_init(struct ezio *e)
31 {
32     memset(e->icons, 0, sizeof e->icons);
33     ezio_clear(e);
34     e->x_ofs = 0;
35     e->show_cursor = true;
36     e->blink_cursor = false;
37 }
38
39 void
40 ezio_set_icon(struct ezio *e, int idx,
41               int row0, int row1, int row2, int row3,
42               int row4, int row5, int row6, int row7)
43 {
44     e->icons[idx][0] = row0;
45     e->icons[idx][1] = row1;
46     e->icons[idx][2] = row2;
47     e->icons[idx][3] = row3;
48     e->icons[idx][4] = row4;
49     e->icons[idx][5] = row5;
50     e->icons[idx][6] = row6;
51     e->icons[idx][7] = row7;
52 }
53
54 void
55 ezio_set_default_icon(struct ezio *e, int idx)
56 {
57     uint8_t *icon;
58
59     assert(idx >= 0 && idx < 8);
60     icon = e->icons[idx];
61     if (idx == 6) {
62         ezio_set_icon(e, idx,
63                       e_____,
64                       eX____,
65                       e_X___,
66                       e__X__,
67                       e___X_,
68                       e____X,
69                       e_____,
70                       e_____);
71     } else if (idx == 7) {
72         ezio_set_icon(e, idx,
73                       e_____,
74                       e_____,
75                       e_X___,
76                       eX_X_X,
77                       eX_X_X,
78                       e___X_,
79                       e_____,
80                       e_____);
81     } else {
82         ezio_set_icon(e, idx,
83                       e_____,
84                       e_____,
85                       e_____,
86                       e_____,
87                       e_____,
88                       e_____,
89                       e_____,
90                       e_____);
91     }
92 }
93
94 void
95 ezio_clear(struct ezio *e)
96 {
97     memset(e->chars, ' ', sizeof e->chars);
98     e->x = e->y = 0;
99 }
100
101 void
102 ezio_put_char(struct ezio *e, int x, int y, uint8_t c)
103 {
104     assert(x >= 0 && x <= 39);
105     assert(y >= 0 && y <= 1);
106     e->chars[y][x] = c != 0xfe ? c : 0xff;
107 }
108
109 void
110 ezio_line_feed(struct ezio *e)
111 {
112     if (++e->y >= 2) {
113         e->y = 1;
114         ezio_scroll_up(e, 1);
115     }
116 }
117
118 void
119 ezio_newline(struct ezio *e)
120 {
121     e->x = 0;
122     ezio_line_feed(e);
123 }
124
125 void
126 ezio_delete_char(struct ezio *e, int x, int y, int n)
127 {
128     remove_elements(&e->chars[y][0], 40, 1, x, n);
129 }
130
131 void
132 ezio_delete_line(struct ezio *e, int y, int n)
133 {
134     remove_elements(e->chars[0], 2, 40, y, n);
135 }
136
137 void
138 ezio_insert_char(struct ezio *e, int x, int y, int n)
139 {
140     insert_elements(&e->chars[y][0], 40, 1, x, n);
141 }
142
143 void
144 ezio_insert_line(struct ezio *e, int y, int n)
145 {
146     insert_elements(&e->chars[0][0], 2, 40, y, n);
147 }
148
149 void
150 ezio_scroll_left(struct ezio *e, int n)
151 {
152     int y;
153     for (y = 0; y < 2; y++) {
154         ezio_delete_char(e, 0, y, n);
155     }
156 }
157
158 void
159 ezio_scroll_right(struct ezio *e, int n)
160 {
161     int y;
162
163     for (y = 0; y < 2; y++) {
164         ezio_insert_char(e, 0, y, n);
165     }
166 }
167
168 void
169 ezio_scroll_up(struct ezio *e, int n)
170 {
171     ezio_delete_line(e, 0, n);
172 }
173
174 void
175 ezio_scroll_down(struct ezio *e, int n)
176 {
177     ezio_insert_line(e, 0, n);
178 }
179
180 bool
181 ezio_chars_differ(const struct ezio *a, const struct ezio *b, int x0, int x1,
182                   int *xp, int *yp)
183 {
184     int x, y;
185
186     x0 = range(x0, 0, 39);
187     x1 = range(x1, 1, 40);
188     for (y = 0; y < 2; y++) {
189         for (x = x0; x < x1; x++) {
190             if (a->chars[y][x] != b->chars[y][x]) {
191                 *xp = x;
192                 *yp = y;
193                 return true;
194             }
195         }
196     }
197     return false;
198 }
199
200 static void
201 remove_elements(uint8_t *p, size_t n_elems, size_t elem_size,
202                 int pos, int n_del)
203 {
204     if (pos >= 0 && pos < n_elems) {
205         n_del = MIN(n_del, n_elems - pos);
206         memmove(p + elem_size * pos,
207                 p + elem_size * (pos + n_del),
208                 elem_size * (n_elems - pos - n_del));
209         memset(p + elem_size * (n_elems - n_del), ' ', n_del * elem_size);
210     }
211 }
212
213 static void
214 insert_elements(uint8_t *p, size_t n_elems, size_t elem_size,
215                 int pos, int n_insert)
216 {
217     if (pos >= 0 && pos < n_elems) {
218         n_insert = MIN(n_insert, n_elems - pos);
219         memmove(p + elem_size * (pos + n_insert),
220                 p + elem_size * pos,
221                 elem_size * (n_elems - pos - n_insert));
222         memset(p + elem_size * pos, ' ', n_insert * elem_size);
223     }
224 }
225
226 static int
227 range(int value, int min, int max)
228 {
229     return value < min ? min : value > max ? max : value;
230 }
231