File: console_common.c

package info (click to toggle)
openbios-sparc 1.0%2Bsvn640-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,412 kB
  • ctags: 12,091
  • sloc: ansic: 57,249; asm: 2,680; xml: 1,335; cpp: 414; makefile: 224; sh: 190
file content (414 lines) | stat: -rw-r--r-- 9,791 bytes parent folder | download | duplicates (2)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 *	<console.c>
 *
 *	Simple text console
 *
 *   Copyright (C) 2002, 2003 Samuel Rydh (samuel@ibrium.se)
 *   Copyright (C) 2005 Stefan Reinauer <stepan@openbios.org>
 *
 *   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
 *
 */

#include "openbios/config.h"
#include "openbios/bindings.h"
#include "openbios/fontdata.h"
#include "video_subr.h"

#define FONT_ADJ_HEIGHT	 (FONT_HEIGHT + 2)

// Warning: will hang on purpose when encountering unknown codes
//#define DEBUG_CONSOLE
#ifdef DEBUG_CONSOLE
#define DPRINTF(fmt, args...)                   \
    do {                                        \
        printk(fmt , ##args);                   \
        for (;;);                               \
    } while (0)
#else
#define DPRINTF(fmt, args...) do {} while(0)
#endif

typedef enum {
    ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
    EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
    ESpalette
} vc_state_t;

#define NPAR 16
static struct {
	int	inited;
	int	physw, physh;
	int	w,h;

	int	x,y;
	char	*buf;

	int	cursor_on;
	vc_state_t vc_state;
	unsigned int vc_npar,vc_par[NPAR]; /* Parameters of current
                                              escape sequence */
} cons;

static int
get_conschar( int x, int y )
{
	if( (uint)x < cons.w && (uint)y < cons.h )
		return cons.buf[y*cons.w + x];
	return ' ';
}

static void
draw_char( uint h, uint v )
{
        const unsigned char *c = fontdata;
	int x, y, xx, rskip, m;
	int invert = (h==cons.x && v==cons.y && cons.cursor_on);
	int ch = get_conschar( h, v );

	while( h >= cons.w || v >= cons.h )
		return;

	h *= FONT_WIDTH;
	v *= FONT_ADJ_HEIGHT;

	rskip = (FONT_WIDTH > 8)? 2 : 1;
	c += rskip * (unsigned int)(ch & 0xff) * FONT_HEIGHT;

	for( x=0; x<FONT_WIDTH; x++ ) {
		xx = x % 8;
		if( x && !xx )
			c++;
		m = (1<<(7-xx));
		for( y=0; y<FONT_HEIGHT; y++ ){
			int col = ((!(c[rskip*y] & m)) != invert) ? 254 : 0;
			draw_pixel( h+x, v+y+1, col );
		}
		draw_pixel( h+x, v, 254 );
		draw_pixel( h+x, v+FONT_HEIGHT+1, 254 );
	}
}

static void
show_cursor( int show )
{
	if( cons.cursor_on == show )
		return;
	cons.cursor_on = show;
	draw_char( cons.x, cons.y );
}


static void
draw_line( int n )
{
	int i;

	if( n >= cons.h || n < 0 )
		return;
	for( i=0; i<cons.w; i++ )
		draw_char( i, n );
}

#if 0
static void
refresh( void )
{
	int i;
	for( i=0; i<cons.h; i++ )
		draw_line(i);
}
#endif

int
console_init( void )
{
	if( video_get_res(&cons.physw,&cons.physh) < 0 )
		return -1;

	set_color( 0, 0 );

	cons.w = cons.physw/FONT_WIDTH;
	cons.h = cons.physh/FONT_ADJ_HEIGHT;
	cons.buf = malloc( cons.w * cons.h );
	cons.inited = 1;
	cons.x = cons.y = 0;
        cons.vc_state = ESnormal;
	return 0;
}

void
console_close( void )
{
 	if( !cons.inited )
		return;
	free( cons.buf );
	cons.inited = 0;
}

static void
rec_char( int ch, int x, int y )
{
	if( (uint)x < cons.w && (uint)y < cons.h ) {
		cons.buf[y*cons.w + x] = ch;
		draw_char( x, y );
	}
}

static void
scroll1( void )
{
    int x, y;

    video_scroll(FONT_ADJ_HEIGHT);

    for (y = 1; y < cons.h - 1; y++) {
        for (x = 0; x < cons.w; x++)
            cons.buf[(y - 1) * cons.w + x] = cons.buf[y * cons.w + x];
    }
    for (x = 0; x < cons.w; x++)
        cons.buf[(cons.h - 1) * cons.w + x] = ' ';
    draw_line(cons.h - 1);
}

// Insert char
static void csi_at(unsigned int nr)
{
    unsigned int x;

    if (nr > cons.w - cons.x)
        nr = cons.w - cons.x;
    else if (!nr)
        return;

    for (x = cons.x + nr; x < cons.w - 1; x++)
        cons.buf[cons.y * cons.w + x - nr] = cons.buf[cons.y * cons.w + x];
    for (x = cons.x; x < cons.x + nr; x++)
        cons.buf[cons.y * cons.w + x] = ' ';
    draw_line(cons.y);
}

static void
do_con_trol(unsigned char ch)
{
    unsigned int i, j;

    switch (ch) {
    case 7:
        // BEL
        return;
    case 8:
        // BS
        if (cons.x)
            cons.x--;
        return;
    case 9:
        // HT
        cons.x = (cons.x + 8) & ~7;
        return;
    case 10:
        // LF
        cons.x = 0;
        cons.y++;
        return;
    case 12:
        // FF
        for (i = 0; i < cons.h; i++) {
            for (j = 0; j < cons.w; j++)
                cons.buf[i * cons.w + j] = ' ';
            draw_line(i);
        }
        cons.x = cons.y = 0;
        return;
    case 13:
        // CR
        cons.x = 0;
        return;
    case 25:
        // EM
        return;
    case 24: // CAN
    case 26: // SUB
        cons.vc_state = ESnormal;
        return;
    case 27:
        // ESC
        cons.vc_state = ESesc;
        return;
    }
    if (ch < 32)
        DPRINTF("Unhandled control char %d\n", ch);

    switch (cons.vc_state) {
    case ESesc:
        cons.vc_state = ESnormal;
        switch (ch) {
        case '[':
            cons.vc_state = ESsquare;
            return;
        case 'M':
            scroll1();
            return;
        default:
            DPRINTF("Unhandled basic escape code '%c'\n", ch);
            return;
        }
        return;
    case ESsquare:
        for(cons.vc_npar = 0; cons.vc_npar < NPAR ; cons.vc_npar++)
            cons.vc_par[cons.vc_npar] = 0;
        cons.vc_npar = 0;
        cons.vc_state = ESgetpars;
        // Fall through
    case ESgetpars:
        if (ch == ';' && cons.vc_npar < NPAR - 1) {
            cons.vc_npar++;
            return;
        } else if (ch >= '0' && ch <= '9') {
            cons.vc_par[cons.vc_npar] *= 10;
            cons.vc_par[cons.vc_npar] += ch - '0';
            return;
        } else
            cons.vc_state=ESgotpars;
        // Fall through
    case ESgotpars:
        cons.vc_state = ESnormal;
        switch(ch) {
        case 'A':
            // Cursor up
            if (cons.vc_par[0] == 0)
                cons.vc_par[0] = 1;
            if (cons.y - cons.vc_par[0] > 0)
                cons.y -= cons.vc_par[0];
            return;
        case 'B':
            // Cursor down
            if (cons.vc_par[0] == 0)
                cons.vc_par[0] = 1;
            if (cons.y + cons.vc_par[0] < cons.h - 1)
                cons.y += cons.vc_par[0];
            return;
        case 'C':
            // Cursor right
            if (cons.vc_par[0] == 0)
                cons.vc_par[0] = 1;
            if (cons.x + cons.vc_par[0] < cons.w - 1)
                cons.x += cons.vc_par[0];
            return;
        case 'D':
            // Cursor left
            if (cons.vc_par[0] == 0)
                cons.vc_par[0] = 1;
            if (cons.x - cons.vc_par[0] > 0)
                cons.x -= cons.vc_par[0];
            return;
        case 'H':
        case 'f':
            // Set cursor position
            if (cons.vc_par[0])
                cons.vc_par[0]--;

            if (cons.vc_par[1])
                cons.vc_par[1]--;

            cons.x = cons.vc_par[1];
            cons.y = cons.vc_par[0];
            return;
        case 'J':
            if (cons.vc_par[0] == 0 && (uint)cons.y < (uint)cons.h &&
                (uint)cons.x < (uint)cons.w) {
                // erase from cursor to end of display
                for (i = cons.x; i < cons.w; i++)
                    cons.buf[cons.y * cons.w + i] = ' ';
                draw_line(cons.y);
                for (j = cons.y + 1; j < cons.h; j++) {
                    for (i = 0; i < cons.w; i++)
                        cons.buf[j * cons.w + i] = ' ';
                    draw_line(j);
                }
            } else {
                DPRINTF("Unhandled CSI J code '%c'\n", cons.vc_par[0]);
            }
            return;
        case 'K':
            switch (cons.vc_par[0]) {
            case 0: /* erase from cursor to end of line */
                for (i = cons.x; i < cons.w; i++)
                    cons.buf[cons.y * cons.w + i] = ' ';
                draw_line(cons.y);
                return;
            case 1: /* erase from start of line to cursor */
                for (i = 0; i <= cons.x; i++)
                    cons.buf[cons.y * cons.w + i] = ' ';
                draw_line(cons.y);
                return;
            case 2: /* erase whole line */
                for (i = 0; i <= cons.w; i++)
                    cons.buf[cons.y * cons.w + i] = ' ';
                draw_line(cons.y);
                return;
            default:
                DPRINTF("Unhandled CSI K code '%c'\n", cons.vc_par[0]);
                return;
            }
            return;
        case 'M':
            if (cons.vc_par[0] == 1)
                scroll1();
            else
                DPRINTF("Unhandled CSI M %d\n", cons.vc_par[0]);
            return;
        case 'm':
            // Attributes are ignored
            return;
        case '@':
            csi_at(cons.vc_par[0]);
            return;
        default:
            DPRINTF("Unhandled escape code '%c', par[%d, %d, %d, %d, %d]\n",
                    ch, cons.vc_par[0], cons.vc_par[1], cons.vc_par[2],
                    cons.vc_par[3], cons.vc_par[4]);
            return;
        }
        return;
    default:
        cons.vc_state = ESnormal;
        rec_char(ch, cons.x++, cons.y);
        return;
    }
}

int
console_draw_str( const char *str )
{
        unsigned int y, x;
        unsigned char ch;

	if (!str) {
		return 0;
	}

	if( !cons.inited && console_init() )
		return -1;

	show_cursor(0);
	while( (ch=*str++) ) {
		do_con_trol(ch);

		if( cons.x >= cons.w ) {
			cons.x=0, cons.y++;
		}
		if( cons.y >= cons.h ) {
			for( y=0; y<cons.h-1; y++ )
				for( x=0; x<cons.w; x++ )
					cons.buf[y*cons.w + x] = cons.buf[(y+1)*cons.w + x];
			cons.y = cons.h-1;
			cons.x = 0;
			scroll1();
		}
	}
	show_cursor(1);
	return 0;
}