* factor out serial hardware init
[coreboot.git] / payloads / libpayload / curses / tinycurses.c
index c901ff66b0547c32436e0bef1662bc4b375a9513..6e83e670645356cacaf86813b4be39a211d869cd 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
  * Copyright (C) 2008 Ulf Jordan <jordan@chalmers.se>
+ * Copyright (C) 2008 coresystems GmbH
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -70,13 +71,13 @@ static int window_count = 1;
 static struct ldat ldat_list[MAX_WINDOWS][SCREEN_Y];
 static int ldat_count = 0;
 
-/* One item bigger than SCREEN_X to reverse place for a NUL byte. */
+/* One item bigger than SCREEN_X to reserve space for a NUL byte. */
 static NCURSES_CH_T linebuf_list[SCREEN_Y * MAX_WINDOWS][SCREEN_X + 1];
 static int linebuf_count = 0;
 
 /* Globals */
 int COLORS;            /* Currently unused? */
-int COLOR_PAIRS;
+int COLOR_PAIRS = 255;
 WINDOW *stdscr;
 WINDOW *curscr;
 WINDOW *newscr;
@@ -110,6 +111,10 @@ chtype fallback_acs_map[128] =
        '|',    '<',    '>',    '*',    '!',    'f',    'o',    ' ',
        };
 
+#ifdef CONFIG_SERIAL_CONSOLE
+#ifdef CONFIG_SERIAL_ACS_FALLBACK
+chtype serial_acs_map[128];
+#else
 /* See acsc of vt100. */
 chtype serial_acs_map[128] =
        {
@@ -130,7 +135,10 @@ chtype serial_acs_map[128] =
        'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
        'x',    'y',    'z',    '{',    '|',    '}',    '~',    0,
        };
+#endif
+#endif
 
+#ifdef CONFIG_VIDEO_CONSOLE
 /* See acsc of linux. */
 chtype console_acs_map[128] =
        {
@@ -151,6 +159,7 @@ chtype console_acs_map[128] =
        '\304', '\304', '\304', '_',    '\303', '\264', '\301', '\302',
        '\263', '\363', '\362', '\343', '\330', '\234', '\376', 0,
        };
+#endif
 
 // FIXME: Ugly (and insecure!) hack!
 char sprintf_tmp[1024];
@@ -189,7 +198,21 @@ int beep(void)
 int cbreak(void) { /* TODO */ return 0; }
 /* D */ int clearok(WINDOW *win, bool flag) { win->_clear = flag; return OK; }
 // int color_content(short color, short *r, short *g, short *b) {}
-// int curs_set(int) {}
+int curs_set(int on)
+{
+#ifdef CONFIG_SERIAL_CONSOLE
+       if (curses_flags & F_ENABLE_SERIAL) {
+               serial_cursor_enable(on);
+       }
+#endif
+#ifdef CONFIG_VIDEO_CONSOLE
+       if (curses_flags & F_ENABLE_CONSOLE) {
+               video_console_cursor_enable(on);
+       }
+#endif
+
+       return OK;
+}
 // int def_prog_mode(void) {}
 // int def_shell_mode(void) {}
 // int delay_output(int) {}
@@ -248,7 +271,7 @@ WINDOW *derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
 
        return win;
 }
-int doupdate(void) { /* TODO */ return(*(int *)0); }
+int doupdate(void) { /* TODO */ return(0); }
 // WINDOW * dupwin (WINDOW *) {}
 /* D */ int echo(void) { SP->_echo = TRUE; return OK; }
 int endwin(void)
@@ -268,7 +291,7 @@ int endwin(void)
 // int flash(void) {}
 int flushinp(void) { /* TODO */ return 0; }
 // WINDOW *getwin (FILE *) {}
-bool has_colors (void) { /* TODO */ return(*(bool *)0); }
+bool has_colors (void) { return(TRUE); }
 // bool has_ic (void) {}
 // bool has_il (void) {}
 // void idcok (WINDOW *, bool) {}
@@ -277,36 +300,33 @@ void immedok(WINDOW *win, bool flag) { win->_immed = flag; }
 /** Note: Must _not_ be called twice! */
 WINDOW *initscr(void)
 {
-       int x, y, i;
+       int i;
 
        // newterm(name, stdout, stdin);
        // def_prog_mode();
 
        for (i = 0; i < 128; i++)
          acs_map[i] = (chtype) i | A_ALTCHARSET;
-
+#ifdef CONFIG_SERIAL_CONSOLE
        if (curses_flags & F_ENABLE_SERIAL) {
                serial_clear();
        }
-
+#endif
+#ifdef CONFIG_VIDEO_CONSOLE
        if (curses_flags & F_ENABLE_CONSOLE) {
                /* Clear the screen and kill the cursor */
 
                video_console_clear();
                video_console_cursor_enable(0);
        }
+#endif
 
        // Speaker init?
 
        stdscr = newwin(SCREEN_Y, SCREEN_X + 1, 0, 0);
        // TODO: curscr, newscr?
 
-       for (y = 0; y < stdscr->_maxy; y++) {
-               for (x = 0; x < stdscr->_maxx; x++) {
-                       stdscr->_line[y].text[x].chars[0] = ' ';
-                       stdscr->_line[y].text[x].attr = A_NORMAL;
-               }
-       }
+       werase(stdscr);
 
        return stdscr;
 }
@@ -469,19 +489,27 @@ int vwprintw(WINDOW *win, const char *fmt, va_list argp)
        return waddstr(win, (char *)&sprintf_tmp);
 }
 // int vwscanw (WINDOW *, NCURSES_CONST char *,va_list) {}
-// int waddch (WINDOW *, const chtype) {}
 int waddch(WINDOW *win, const chtype ch)
 {
        int code = ERR;
        // NCURSES_CH_T wch;
        // SetChar2(wch, ch);
 
+       if (win->_line[win->_cury].firstchar == _NOCHANGE || 
+                       win->_line[win->_cury].firstchar > win->_curx)
+               win->_line[win->_cury].firstchar = win->_curx;
+
        win->_line[win->_cury].text[win->_curx].chars[0] =
                ((ch) & (chtype)A_CHARTEXT);
 
        win->_line[win->_cury].text[win->_curx].attr = WINDOW_ATTRS(win);
        win->_line[win->_cury].text[win->_curx].attr |=
                ((ch) & (chtype)A_ATTRIBUTES);
+
+       if (win->_line[win->_cury].lastchar == _NOCHANGE || 
+                       win->_line[win->_cury].lastchar < win->_curx)
+               win->_line[win->_cury].lastchar = win->_curx;
+
        win->_curx++;   // FIXME
 
        // if (win && (waddch_nosync(win, wch) != ERR)) {
@@ -503,6 +531,10 @@ int waddnstr(WINDOW *win, const char *astr, int n)
        if (n < 0)
                n = strlen(astr);
 
+       if (win->_line[win->_cury].firstchar == _NOCHANGE || 
+                       win->_line[win->_cury].firstchar > win->_curx)
+               win->_line[win->_cury].firstchar = win->_curx;
+
        while ((n-- > 0) && (*str != '\0')) {
        // while (*str != '\0') {
                win->_line[win->_cury].text[win->_curx].chars[0] = *str++;
@@ -517,6 +549,11 @@ int waddnstr(WINDOW *win, const char *astr, int n)
                //      break;
                // }
        }
+
+       if (win->_line[win->_cury].lastchar == _NOCHANGE || 
+                       win->_line[win->_cury].lastchar < win->_curx)
+               win->_line[win->_cury].lastchar = win->_curx;
+
        return code;
 }
 int wattr_on(WINDOW *win, attr_t at, void *opts GCC_UNUSED)
@@ -599,6 +636,9 @@ int werase(WINDOW *win)
                        win->_line[y].text[x].chars[0] = ' ';
                        win->_line[y].text[x].attr = WINDOW_ATTRS(win);
                }
+               // Should we check instead?
+               win->_line[y].firstchar = 0;
+               win->_line[y].lastchar = win->_maxx;
        }
        return OK;
 }
@@ -662,30 +702,44 @@ int whline(WINDOW *win, chtype ch, int n)
        (((c) & 0x4400) >> 2) | ((c) & 0xAA00) | (((c) & 0x1100) << 2)
 int wnoutrefresh(WINDOW *win)
 {
+#ifdef CONFIG_SERIAL_CONSOLE
        // FIXME.
        int serial_is_bold = 0;
+       int serial_is_reverse = 0;
        int serial_is_altcharset = 0;
+       int serial_cur_pair = 0;
 
+       int need_altcharset;
+       short fg, bg;
+#endif
        int x, y;
        chtype ch;
-       int need_altcharset;
 
+#ifdef CONFIG_SERIAL_CONSOLE
        serial_end_bold();
        serial_end_altcharset();
+#endif
 
        for (y = 0; y <= win->_maxy; y++) {
 
+               if (win->_line[y].firstchar == _NOCHANGE)
+                       continue;
+
                /* Position the serial cursor */
 
+#ifdef CONFIG_SERIAL_CONSOLE
                if (curses_flags & F_ENABLE_SERIAL)
-                       serial_set_cursor(win->_begy + y, win->_begx);
+                       serial_set_cursor(win->_begy + y, win->_begx +
+                                       win->_line[y].firstchar);
+#endif
 
-               for (x = 0; x <= win->_maxx; x++) {
+               for (x = win->_line[y].firstchar; x <= win->_line[y].lastchar; x++) {
                        attr_t attr = win->_line[y].text[x].attr;
 
                        unsigned int c =
                                ((int)color_pairs[PAIR_NUMBER(attr)]) << 8;
 
+#ifdef CONFIG_SERIAL_CONSOLE
                        if (curses_flags & F_ENABLE_SERIAL) {
                                ch = win->_line[y].text[x].chars[0];
 
@@ -694,11 +748,32 @@ int wnoutrefresh(WINDOW *win)
                                                serial_start_bold();
                                                serial_is_bold = 1;
                                        }
-                               }
-                               else {
+                               } else {
                                        if (serial_is_bold) {
                                                serial_end_bold();
                                                serial_is_bold = 0;
+                                               /* work around serial.c
+                                                * shortcoming:
+                                                */
+                                               serial_is_reverse = 0;
+                                               serial_cur_pair = 0;
+                                       }
+                               }
+
+                               if (attr & A_REVERSE) {
+                                       if (!serial_is_reverse) {
+                                               serial_start_reverse();
+                                               serial_is_reverse = 1;
+                                       }
+                               } else {
+                                       if (serial_is_reverse) {
+                                               serial_end_reverse();
+                                               serial_is_reverse = 0;
+                                               /* work around serial.c
+                                                * shortcoming:
+                                                */
+                                               serial_is_bold = 0;
+                                               serial_cur_pair = 0;
                                        }
                                }
 
@@ -719,9 +794,18 @@ int wnoutrefresh(WINDOW *win)
                                        serial_is_altcharset = 0;
                                }
 
+                               if (serial_cur_pair != PAIR_NUMBER(attr)) {
+                                       pair_content(PAIR_NUMBER(attr),
+                                                    &fg, &bg);
+                                       serial_set_color(fg, bg);
+                                       serial_cur_pair = PAIR_NUMBER(attr);
+                               }
+
                                serial_putchar(ch);
-                       }
 
+                       }
+#endif
+#ifdef CONFIG_VIDEO_CONSOLE
                        c = SWAP_RED_BLUE(c);
 
                        if (curses_flags & F_ENABLE_CONSOLE) {
@@ -753,9 +837,22 @@ int wnoutrefresh(WINDOW *win)
                                c |= (chtype) (ch & 0xff);
                                video_console_putc(win->_begy + y, win->_begx + x, c);
                        }
+#endif
                }
+               win->_line[y].firstchar = _NOCHANGE;
+               win->_line[y].lastchar = _NOCHANGE;
        }
 
+#ifdef CONFIG_SERIAL_CONSOLE
+       if (curses_flags & F_ENABLE_SERIAL)
+               serial_set_cursor(win->_begy + win->_cury, win->_begx + win->_curx);
+#endif
+
+#ifdef CONFIG_VIDEO_CONSOLE
+       if (curses_flags & F_ENABLE_CONSOLE)
+               video_console_set_cursor(win->_begx + win->_curx, win->_begy + win->_cury);
+#endif
+
        return OK;
 }
 int wprintw(WINDOW *win, const char *fmt, ...)
@@ -769,7 +866,19 @@ int wprintw(WINDOW *win, const char *fmt, ...)
 
        return code;
 }
-// int wredrawln (WINDOW *,int,int) {}
+
+int wredrawln (WINDOW *win, int beg_line, int num_lines)
+{
+       int i;
+
+       for (i = beg_line; i < beg_line + num_lines; i++) {
+               win->_line[i].firstchar = 0;
+               win->_line[i].lastchar = win->_maxx;
+       }
+
+       return OK;
+}
+
 int wrefresh(WINDOW *win)
 {
        // FIXME
@@ -799,13 +908,49 @@ int wrefresh(WINDOW *win)
 // int wscanw (WINDOW *, NCURSES_CONST char *,...) {}
 int wscrl(WINDOW *win, int n)
 {
+       int x, y;
+
        if (!win->_scroll)
                return ERR;
 
-       if (n != 0) {
-               // _nc_scroll_window(win, n, win->_regtop, win->_regbottom, win->_nc_bkgd);
-               // _nc_synchook(win);
+       if (n == 0)
+               return OK;
+
+       for (y = 0; y <= (win->_maxy - n); y++) {
+               win->_line[y].firstchar = win->_line[y + n].firstchar;
+               win->_line[y].lastchar = win->_line[y + n].lastchar;
+               for (x = 0; x <= win->_maxx; x++) {
+                       if ((win->_line[y].text[x].chars[0] != win->_line[y + n].text[x].chars[0]) || 
+                                       (win->_line[y].text[x].attr != win->_line[y + n].text[x].attr)) {
+                               if (win->_line[y].firstchar == _NOCHANGE)
+                                       win->_line[y].firstchar = x;
+
+                               win->_line[y].lastchar = x;
+
+                               win->_line[y].text[x].chars[0] = win->_line[y + n].text[x].chars[0];
+                               win->_line[y].text[x].attr = win->_line[y + n].text[x].attr;
+                       }
+               }
        }
+
+       for (y = (win->_maxy+1 - n); y <= win->_maxy; y++) {
+               for (x = 0; x <= win->_maxx; x++) {
+                       if ((win->_line[y].text[x].chars[0] != ' ') || 
+                                       (win->_line[y].text[x].attr != A_NORMAL)) {
+                               if (win->_line[y].firstchar == _NOCHANGE)
+                                       win->_line[y].firstchar = x;
+
+                               win->_line[y].lastchar = x;
+
+                               win->_line[y].text[x].chars[0] = ' ';
+                               win->_line[y].text[x].attr = A_NORMAL;
+                       }
+               }
+       }
+
+       // _nc_scroll_window(win, n, win->_regtop, win->_regbottom, win->_nc_bkgd);
+       // _nc_synchook(win);
+
        return OK;
 }
 int wsetscrreg(WINDOW *win, int top, int bottom)