summaryrefslogtreecommitdiff
path: root/extras/ezio/ezio.c
blob: 6024766e205921d6d173f67c3558fda4fbbfc639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Copyright (c) 2008, 2009 Nicira Networks, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, Nicira Networks gives permission
 * to link the code of its release of vswitchd with the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the same
 * license as the "OpenSSL" library), and distribute the linked
 * executables.  You must obey the GNU General Public License in all
 * respects for all of the code used other than "OpenSSL".  If you modify
 * this file, you may extend this exception to your version of the file,
 * but you are not obligated to do so.  If you do not wish to do so,
 * delete this exception statement from your version.
 *
 */

#include <config.h>
#include "ezio.h"
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include "util.h"

static void remove_elements(uint8_t *p, size_t n_elems, size_t elem_size,
                            int pos, int n_del);
static void insert_elements(uint8_t *p, size_t n_elems, size_t elem_size,
                            int pos, int n_insert);
static int range(int value, int min, int max);

void
ezio_init(struct ezio *e)
{
    memset(e->icons, 0, sizeof e->icons);
    ezio_clear(e);
    e->x_ofs = 0;
    e->show_cursor = true;
    e->blink_cursor = false;
}

void
ezio_set_icon(struct ezio *e, int idx,
              int row0, int row1, int row2, int row3,
              int row4, int row5, int row6, int row7)
{
    e->icons[idx][0] = row0;
    e->icons[idx][1] = row1;
    e->icons[idx][2] = row2;
    e->icons[idx][3] = row3;
    e->icons[idx][4] = row4;
    e->icons[idx][5] = row5;
    e->icons[idx][6] = row6;
    e->icons[idx][7] = row7;
}

void
ezio_set_default_icon(struct ezio *e, int idx)
{
    uint8_t *icon;

    assert(idx >= 0 && idx < 8);
    icon = e->icons[idx];
    if (idx == 6) {
        ezio_set_icon(e, idx,
                      e_____,
                      eX____,
                      e_X___,
                      e__X__,
                      e___X_,
                      e____X,
                      e_____,
                      e_____);
    } else if (idx == 7) {
        ezio_set_icon(e, idx,
                      e_____,
                      e_____,
                      e_X___,
                      eX_X_X,
                      eX_X_X,
                      e___X_,
                      e_____,
                      e_____);
    } else {
        ezio_set_icon(e, idx,
                      e_____,
                      e_____,
                      e_____,
                      e_____,
                      e_____,
                      e_____,
                      e_____,
                      e_____);
    }
}

void
ezio_clear(struct ezio *e)
{
    memset(e->chars, ' ', sizeof e->chars);
    e->x = e->y = 0;
}

void
ezio_put_char(struct ezio *e, int x, int y, uint8_t c)
{
    assert(x >= 0 && x <= 39);
    assert(y >= 0 && y <= 1);
    e->chars[y][x] = c != 0xfe ? c : 0xff;
}

void
ezio_line_feed(struct ezio *e)
{
    if (++e->y >= 2) {
        e->y = 1;
        ezio_scroll_up(e, 1);
    }
}

void
ezio_newline(struct ezio *e)
{
    e->x = 0;
    ezio_line_feed(e);
}

void
ezio_delete_char(struct ezio *e, int x, int y, int n)
{
    remove_elements(&e->chars[y][0], 40, 1, x, n);
}

void
ezio_delete_line(struct ezio *e, int y, int n)
{
    remove_elements(e->chars[0], 2, 40, y, n);
}

void
ezio_insert_char(struct ezio *e, int x, int y, int n)
{
    insert_elements(&e->chars[y][0], 40, 1, x, n);
}

void
ezio_insert_line(struct ezio *e, int y, int n)
{
    insert_elements(&e->chars[0][0], 2, 40, y, n);
}

void
ezio_scroll_left(struct ezio *e, int n)
{
    int y;
    for (y = 0; y < 2; y++) {
        ezio_delete_char(e, 0, y, n);
    }
}

void
ezio_scroll_right(struct ezio *e, int n)
{
    int y;

    for (y = 0; y < 2; y++) {
        ezio_insert_char(e, 0, y, n);
    }
}

void
ezio_scroll_up(struct ezio *e, int n)
{
    ezio_delete_line(e, 0, n);
}

void
ezio_scroll_down(struct ezio *e, int n)
{
    ezio_insert_line(e, 0, n);
}

bool
ezio_chars_differ(const struct ezio *a, const struct ezio *b, int x0, int x1,
                  int *xp, int *yp)
{
    int x, y;

    x0 = range(x0, 0, 39);
    x1 = range(x1, 1, 40);
    for (y = 0; y < 2; y++) {
        for (x = x0; x < x1; x++) {
            if (a->chars[y][x] != b->chars[y][x]) {
                *xp = x;
                *yp = y;
                return true;
            }
        }
    }
    return false;
}

static void
remove_elements(uint8_t *p, size_t n_elems, size_t elem_size,
                int pos, int n_del)
{
    if (pos >= 0 && pos < n_elems) {
        n_del = MIN(n_del, n_elems - pos);
        memmove(p + elem_size * pos,
                p + elem_size * (pos + n_del),
                elem_size * (n_elems - pos - n_del));
        memset(p + elem_size * (n_elems - n_del), ' ', n_del * elem_size);
    }
}

static void
insert_elements(uint8_t *p, size_t n_elems, size_t elem_size,
                int pos, int n_insert)
{
    if (pos >= 0 && pos < n_elems) {
        n_insert = MIN(n_insert, n_elems - pos);
        memmove(p + elem_size * (pos + n_insert),
                p + elem_size * pos,
                elem_size * (n_elems - pos - n_insert));
        memset(p + elem_size * pos, ' ', n_insert * elem_size);
    }
}

static int
range(int value, int min, int max)
{
    return value < min ? min : value > max ? max : value;
}