source: rtems/c/src/lib/libbsp/i386/pc386/console/outch.c @ 3cd35d46

4.104.114.95
Last change on this file since 3cd35d46 was 3cd35d46, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/18/08 at 10:46:09

Add missing prototypes.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * outch.c  - This file contains code for displaying characters
3 *            on the console uisng information that should be
4 *            maintained by the BIOS in its data Area.
5 *
6 * Copyright (C) 1998  Eric Valette (valette@crf.canon.fr)
7 *                     Canon Centre Recherche France.
8 *
9 *  The license and distribution terms for this file may be
10 *  found in found in the file LICENSE in this distribution or at
11 *  http://www.rtems.com/license/LICENSE.
12 *
13 * Till Straumann <strauman@slac.stanford.edu>, 2003/9:
14 *  - added handling of basic escape sequences (cursor movement
15 *    and erasing; just enough for the line editor 'libtecla' to
16 *    work...)
17 * $Id$
18 */
19
20#include <bsp.h>
21
22#include <stdlib.h>
23#include <string.h>
24
25#include <crt.h>
26
27extern void wr_cursor(int, unsigned short);
28
29#define TAB_SPACE 4
30static unsigned short *bitMapBaseAddr;
31static unsigned short ioCrtBaseAddr;
32static unsigned short maxCol;
33static unsigned short maxRow;
34static unsigned char  row;
35static unsigned char  column;
36static unsigned short attribute;
37static unsigned int   nLines;
38
39static void
40scroll(void)
41{
42    int i, j;                               /* Counters */
43    unsigned short *pt_scroll, *pt_bitmap;  /* Pointers on the bit-map  */
44
45    pt_bitmap = bitMapBaseAddr;
46    j = 0;
47    pt_bitmap = pt_bitmap + j;
48    pt_scroll = pt_bitmap + maxCol;
49    for (i = j; i < (maxRow - 1) * maxCol; i++) {
50        *pt_bitmap++ = *pt_scroll++;
51    }
52
53    /*
54     * Blank characters are displayed on the last line.
55     */
56    for (i = 0; i < maxCol; i++) {
57        *pt_bitmap++ = (short) (' ' | attribute);
58    }
59}
60
61static void
62doCRNL(int cr, int nl)
63{
64        if (nl) {
65    if (++row == maxRow) {
66        scroll();       /* Scroll the screen now */
67        row = maxRow - 1;
68    }
69    nLines++;
70        }
71        if (cr)
72        column = 0;
73    /* Move cursor on the next location  */
74        if (cr || nl)
75        wr_cursor(row * maxCol + column, ioCrtBaseAddr);
76}
77
78int (*videoHook)(char, int *)=0;
79
80static void
81advanceCursor(void)
82{
83  if (++column == maxCol)
84        doCRNL(1,1);
85  else
86        wr_cursor(row * maxCol + column, ioCrtBaseAddr);
87}
88
89static void
90gotorc(int r, int c)
91{
92        column = c;
93        row    = r;
94
95        wr_cursor(row * maxCol + column, ioCrtBaseAddr);
96}
97
98#define ESC             ((char)27)
99/* erase current location without moving the cursor */
100#define BLANK   ((char)0x7f)
101
102static void
103videoPutChar(char car)
104{
105    unsigned short *pt_bitmap = bitMapBaseAddr + row * maxCol + column;
106
107    switch (car) {
108    case '\b': {
109            if (column) column--;
110            /* Move cursor on the previous location  */
111            wr_cursor(row * maxCol + column, ioCrtBaseAddr);
112            return;
113        }
114        case '\t': {
115            int i;
116
117            i = TAB_SPACE - (column & (TAB_SPACE - 1));
118            column += i;
119            if (column >= maxCol) {
120                doCRNL(1,1);
121                return;
122            }
123            while (i--) *pt_bitmap++ = ' ' | attribute;
124            wr_cursor(row * maxCol + column, ioCrtBaseAddr);
125            return;
126        }
127        case '\n': {
128            doCRNL(0,1);
129            return;
130        }
131    case 7:             {       /* Bell code must be inserted here */
132            return;
133        }
134        case '\r' : {
135                doCRNL(1,0);
136            return;
137        }
138        case BLANK: {
139            *pt_bitmap = ' ' | attribute;
140                /* DONT move the cursor... */
141                return;
142        }
143        default: {
144            *pt_bitmap = (unsigned char)car | attribute;
145                advanceCursor();
146            return;
147        }
148    }
149}
150
151/* trivial state machine to handle escape sequences:
152 *
153 *                    ---------------------------------
154 *                   |                                 |
155 *                   |                                 |
156 * KEY:        esc   V    [          DCABHKJ       esc |
157 * STATE:   0 -----> 27 -----> '[' ----------> -1 -----
158 *          ^\        \          \               \
159 * KEY:     | \other   \ other    \ other         \ other
160 *           <-------------------------------------
161 *
162 * in state '-1', the DCABHKJ cases are handled
163 *
164 * (cursor motion and screen clearing)
165 */
166
167#define DONE  (-1)
168
169static int
170handleEscape(int oldState, char car)
171{
172int rval = 0;
173int ro,co;
174
175        switch ( oldState ) {
176                case DONE:      /*  means the previous char terminated an ESC sequence... */
177                case 0:
178                        if ( 27 == car ) {
179                                rval = 27;       /* START of an ESC sequence */
180                        }
181                break;
182
183                case 27:
184                        if ( '[' == car ) {
185                                rval = car;  /* received ESC '[', so far */
186                        } else {
187                                /* dump suppressed 'ESC'; outch will append the char */
188                                videoPutChar(ESC);
189                        }
190                break;
191
192                case '[':
193                        /* handle 'ESC' '[' sequences here */
194                        ro = row; co = column;
195                        rval = DONE; /* done */
196
197                        switch (car) {
198                                case 'D': /* left */
199                                        if ( co > 0 )      co--;
200                                break;
201                                case 'C': /* right */
202                                        if ( co < maxCol ) co++;
203                                break;
204                                case 'A': /* up    */
205                                        if ( ro > 0 )      ro--;
206                                break;
207                                case 'B': /* down */
208                                        if ( ro < maxRow ) ro++;
209                                break;
210                                case 'H': /* home */
211                                        ro = co = 0;
212                                break;
213                                case 'K': /* clear to end of line */
214                                        while ( column < maxCol - 1 )
215                                videoPutChar(' ');
216                        videoPutChar(BLANK);
217                        break;
218                        case 'J': /* clear to end of screen */
219                                        while (  ((row < maxRow-1) || (column < maxCol-1)) )
220                                                videoPutChar(' ');
221                                        videoPutChar(BLANK);
222                        break;
223                        default:
224                        videoPutChar(ESC);
225                        videoPutChar('[');
226                                        /* DONT move the cursor */
227                                        ro   = -1;
228                                        rval = 0;
229                        break;
230                        }
231                        /* reset cursor */
232                        if ( ro >= 0)
233                                gotorc(ro,co);
234
235                default:
236                break;
237
238        }
239
240        return rval;
241}
242
243void
244clear_screen(void)
245{
246    int i,j;
247
248    for (j = 0; j <= maxRow; j++) {
249      for (i = 0; i <= maxCol; i++) {
250        videoPutChar(' ');
251      }
252    }
253    column  = 0;
254    row     = 0;
255}
256
257/*-------------------------------------------------------------------------+
258|         Function: _IBMPC_outch
259|      Description: Higher level (console) interface to consPutc.
260| Global Variables: None.
261|        Arguments: c - character to write to console.
262|          Returns: Nothing.
263+--------------------------------------------------------------------------*/
264void
265_IBMPC_outch(char c)
266{
267static int escaped = 0;
268
269  if ( ! (escaped = handleEscape(escaped, c)) ) {
270    if ( '\n' == c )
271      videoPutChar('\r');
272        videoPutChar(c);
273  }
274} /* _IBMPC_outch */
275
276/*-------------------------------------------------------------------------+
277|         Function: _IBMPC_initVideo
278|      Description: Video system initialization. Hook for any early setup.
279| Global Variables: bitMapBaseAddr, ioCrtBaseAddr, maxCol, maxRow, row
280|                   column, attribute, nLines;
281|        Arguments: None.
282|          Returns: Nothing.
283+--------------------------------------------------------------------------*/
284void
285_IBMPC_initVideo(void)
286{
287    unsigned char* pt = (unsigned char*) (VIDEO_MODE_ADDR);
288
289    if (*pt == VGAMODE7) {
290      bitMapBaseAddr = (unsigned short*) V_MONO;
291    }
292    else {
293      bitMapBaseAddr = (unsigned short*) V_COLOR;
294    }
295    ioCrtBaseAddr = *(unsigned short*) DISPLAY_CRT_BASE_IO_ADDR;
296    maxCol  = * (unsigned short*) NB_MAX_COL_ADDR;
297    maxRow  = * (unsigned char*)  NB_MAX_ROW_ADDR;
298    column  = 0;
299    row     = 0;
300    attribute = ((BLACK << 4) | WHITE)<<8;
301    nLines = 0;
302    clear_screen();
303#ifdef DEBUG_EARLY_STAGE
304    printk("bitMapBaseAddr = %X, display controller base IO = %X\n",
305           (unsigned) bitMapBaseAddr,
306           (unsigned) ioCrtBaseAddr);
307    videoPrintf("maxCol = %d, maxRow = %d\n", (unsigned) maxCol, (unsigned) maxRow);
308#endif
309} /* _IBMPC_initVideo */
310
311/* for old DOS compatibility n-curses type of applications */
312void gotoxy( int x, int y )
313{
314  gotorc(y,x);
315}
316
317int whereX( void )
318{
319  return row;
320}
321
322int whereY( void )
323{
324  return column;
325}
Note: See TracBrowser for help on using the repository browser.