summaryrefslogtreecommitdiff
path: root/com32/lib/sys/ansicon_write.c
blob: a963923d749d07e772e84eb5ad8dedb4fd515291 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2004-2009 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * ansicon_write.c
 *
 * Write to the screen using ANSI control codes (about as capable as
 * DOS' ANSI.SYS.)
 */

#include <errno.h>
#include <string.h>
#include <com32.h>
#include <minmax.h>
#include <colortbl.h>
#include <klibc/compiler.h>
#include <syslinux/config.h>
#include "file.h"
#include "ansi.h"
#include <syslinux/firmware.h>

static void ansicon_erase(const struct term_state *, int, int, int, int);
static void ansicon_write_char(int, int, uint8_t, const struct term_state *);
static void ansicon_showcursor(const struct term_state *);
static void ansicon_scroll_up(const struct term_state *);
static void ansicon_set_cursor(int, int, bool);

static struct term_state ts;
struct ansi_ops __ansicon_ops = {
    .erase = ansicon_erase,
    .write_char = ansicon_write_char,
    .showcursor = ansicon_showcursor,
    .set_cursor = ansicon_set_cursor,
    .scroll_up = ansicon_scroll_up,
    .beep = __ansicon_beep,
};

static struct term_info ti = {
    .disabled = 0,
    .ts = &ts,
    .op = &__ansicon_ops
};

#define BIOS_CURXY ((struct curxy *)0x450)	/* Array for each page */
#define BIOS_ROWS (*(uint8_t *)0x484)	/* Minus one; if zero use 24 (= 25 lines) */
#define BIOS_COLS (*(uint16_t *)0x44A)
#define BIOS_PAGE (*(uint8_t *)0x462)

#define TEXT_MODE 0x0005

/* Reference counter to the screen, to keep track of if we need
   reinitialization. */
static int ansicon_counter = 0;

static uint16_t cursor_type;	/* Saved cursor pattern */

void bios_set_mode(uint16_t mode)
{
    com32sys_t ireg;

    ireg.eax.w[0] = mode;
    __intcall(0x22, &ireg, NULL);
}

void bios_get_mode(int *cols, int *rows)
{
    *rows = BIOS_ROWS ? BIOS_ROWS + 1 : 25;
    *cols = BIOS_COLS;
}

void bios_get_cursor(int *x, int *y)
{
    com32sys_t ireg, oreg;

    memset(&ireg, 0, sizeof(ireg));

    ireg.eax.b[1] = 0x03;
    ireg.ebx.b[1] = BIOS_PAGE;
    __intcall(0x10, &ireg, &oreg);
    cursor_type = oreg.ecx.w[0];
    *x = oreg.edx.b[0];
    *y = oreg.edx.b[1];
}

/* Common setup */
int __ansicon_open(struct file_info *fp)
{
    static com32sys_t ireg;	/* Auto-initalized to all zero */
    com32sys_t oreg;

    if (!ansicon_counter) {
	/* Are we disabled? */
	if (syslinux_serial_console_info()->flowctl & 0x8000) {
	    ti.disabled = 1;
	    ti.rows = 25;
	    ti.cols = 80;
	} else {
	    /* Force text mode */
	    firmware->o_ops->set_mode(TEXT_MODE);

	    /* Initial state */
	    firmware->o_ops->get_mode(&ti.cols, &ti.rows);
	    __ansi_init(&ti);

	    /* Get cursor shape and position */
	    firmware->o_ops->get_cursor(&ti.ts->xy.x, &ti.ts->xy.y);
	}
    }

    fp->o.rows = ti.rows;
    fp->o.cols = ti.cols;

    ansicon_counter++;
    return 0;
}

int __ansicon_close(struct file_info *fp)
{
    (void)fp;

    ansicon_counter--;
    return 0;
}

/* Turn ANSI attributes into VGA attributes */
static uint8_t ansicon_attribute(const struct term_state *st)
{
    int bg = st->bg;
    int fg;

    if (st->underline)
	fg = 0x01;
    else if (st->intensity == 0)
	fg = 0x08;
    else
	fg = st->fg;

    if (st->reverse) {
	bg = fg & 0x07;
	fg &= 0x08;
	fg |= st->bg;
    }

    if (st->blink)
	bg ^= 0x08;

    if (st->intensity == 2)
	fg ^= 0x08;

    return (bg << 4) | fg;
}

void bios_erase(int x0, int y0, int x1, int y1, uint8_t attribute)
{
    static com32sys_t ireg;

    ireg.eax.w[0] = 0x0600;	/* Clear window */
    ireg.ebx.b[1] = attribute;
    ireg.ecx.b[0] = x0;
    ireg.ecx.b[1] = y0;
    ireg.edx.b[0] = x1;
    ireg.edx.b[1] = y1;
    __intcall(0x10, &ireg, NULL);
}

/* Erase a region of the screen */
static void ansicon_erase(const struct term_state *st,
			  int x0, int y0, int x1, int y1)
{
    uint8_t attribute = ansicon_attribute(st);

    if (firmware->o_ops->erase)
	firmware->o_ops->erase(x0, y0, x1, y1, attribute);
}

void bios_showcursor(uint16_t cursor)
{
    static com32sys_t ireg;

    ireg.eax.b[1] = 0x01;
    ireg.ecx.w[0] = cursor;
    __intcall(0x10, &ireg, NULL);
}

/* Show or hide the cursor */
static void ansicon_showcursor(const struct term_state *st)
{
    uint16_t cursor = st->cursor ? cursor_type : 0x2020;
    firmware->o_ops->showcursor(cursor);
}

void bios_set_cursor(int x, int y, bool visible)
{
    const int page = BIOS_PAGE;
    struct curxy xy = BIOS_CURXY[page];
    static com32sys_t ireg;

    (void)visible;

    if (xy.x != x || xy.y != y) {
	ireg.eax.b[1] = 0x02;
	ireg.ebx.b[1] = page;
	ireg.edx.b[1] = y;
	ireg.edx.b[0] = x;
	__intcall(0x10, &ireg, NULL);
    }
}

static void ansicon_set_cursor(int x, int y, bool visible)
{
    firmware->o_ops->set_cursor(x, y, visible);
}

void bios_write_char(uint8_t ch, uint8_t attribute)
{
    static com32sys_t ireg;

    ireg.eax.b[1] = 0x09;
    ireg.eax.b[0] = ch;
    ireg.ebx.b[1] = BIOS_PAGE;
    ireg.ebx.b[0] = attribute;
    ireg.ecx.w[0] = 1;
    __intcall(0x10, &ireg, NULL);
}

static void ansicon_write_char(int x, int y, uint8_t ch,
			       const struct term_state *st)
{
    uint8_t attribute = ansicon_attribute(st);
    ansicon_set_cursor(x, y, false);

    firmware->o_ops->write_char(ch, attribute);
}

void bios_scroll_up(uint8_t cols, uint8_t rows, uint8_t attribute)
{
    static com32sys_t ireg;

    ireg.eax.w[0] = 0x0601;
    ireg.ebx.b[1] = attribute;
    ireg.ecx.w[0] = 0;
    ireg.edx.b[1] = rows;
    ireg.edx.b[0] = cols;
    __intcall(0x10, &ireg, NULL);	/* Scroll */
}

static void ansicon_scroll_up(const struct term_state *st)
{
    uint8_t rows, cols, attribute;

    /*
     * Earlier code set ti.cols to 1 causing console output one char
     * per line.
     */
    cols = 1;
    rows = ti.rows - 1;
    attribute = ansicon_attribute(st);

    firmware->o_ops->scroll_up(cols, rows, attribute);
}

ssize_t __ansicon_write(struct file_info *fp, const void *buf, size_t count)
{
    const unsigned char *bufp = buf;
    size_t n = 0;

    (void)fp;

    if (ti.disabled)
	return count;		/* Nothing to do */

    while (count--) {
	__ansi_putchar(&ti, *bufp++);
	n++;
    }

    return n;
}

void bios_beep(void)
{
    static com32sys_t ireg;

    ireg.eax.w[0] = 0x0e07;
    ireg.ebx.b[1] = BIOS_PAGE;
    __intcall(0x10, &ireg, NULL);
}

void __ansicon_beep(void)
{
    if (firmware->o_ops->beep)
	firmware->o_ops->beep();
}

const struct output_dev dev_ansicon_w = {
    .dev_magic = __DEV_MAGIC,
    .flags = __DEV_TTY | __DEV_OUTPUT,
    .fileflags = O_WRONLY | O_CREAT | O_TRUNC | O_APPEND,
    .write = __ansicon_write,
    .close = __ansicon_close,
    .open = __ansicon_open,
};