2 /* Copyright 1999 - Joseph Pranevich */
10 /* This is the console driver for systems that support the ncurses
14 /* Actually, this should work for curses, as well. But there may be
15 individual functions that are unsupported in plain curses or other
16 variants. Those should be detected and special-cased by autoconf.
19 /* When creating new drivers, you need to assign all the functions that
20 that driver supports into the driver struct. If it is a supplementary
21 driver, it should make sure to perserve the old values.
25 #undef ERR /* Use ncurses's err() */
34 SCREEN *ncurses_screen;
36 static int get_color_pair(int fg_color, int bg_color);
40 /* This should be the root driver so we can ignore anything
41 already in the struct. */
43 driver.norefresh = FALSE;
45 driver.init = NCURSES_Init;
46 driver.write = NCURSES_Write;
47 driver.close = NCURSES_Close;
48 driver.moveCursor = NCURSES_MoveCursor;
49 driver.getCursorPosition = NCURSES_GetCursorPosition;
50 driver.getCharacterAtCursor = NCURSES_GetCharacterAtCursor;
51 driver.clearScreen = NCURSES_ClearScreen;
52 driver.allocColor = NCURSES_AllocColor;
53 driver.setBackgroundColor = NCURSES_SetBackgroundColor;
54 #ifdef HAVE_RESIZETERM
55 driver.notifyResizeScreen = NCURSES_NotifyResizeScreen;
58 driver.checkForKeystroke = NCURSES_CheckForKeystroke;
59 driver.getKeystroke = NCURSES_GetKeystroke;
61 driver.refresh = NCURSES_Refresh;
66 ncurses_screen = newterm("xterm", driver.console_out,
68 set_term(ncurses_screen);
73 /* intrflush(stdscr, FALSE); */
75 nodelay(stdscr, TRUE);
78 void NCURSES_Write(char output, int fg, int bg, int attribute)
84 fg = COLOR_WHITE; /* Default */
87 bg = COLOR_BLACK; /* Default */
89 pair = get_color_pair(fg, bg);
91 if (waddch(stdscr, output | COLOR_PAIR(pair)) == ERR)
93 NCURSES_GetCursorPosition(&row, &col);
94 FIXME(console, "NCURSES: waddch() failed at %d, %d.\n", row, col);
103 void NCURSES_GetKeystroke(char *scan, char *ascii)
105 while (!NCURSES_CheckForKeystroke(scan, ascii))
106 {} /* Wait until keystroke is detected */
108 /* When it is detected, we will already have the right value
109 in scan and ascii, but we need to take this keystroke
110 out of the buffer. */
114 int NCURSES_CheckForKeystroke(char *scan, char *ascii)
116 /* We don't currently support scan codes here */
119 temp = wgetch(stdscr);
126 ungetch(temp); /* Keystroke not removed from buffer */
127 *ascii = (char) temp;
132 void NCURSES_MoveCursor(char row, char col)
134 if (wmove(stdscr, row, col) == ERR)
135 FIXME(console, "NCURSES: wmove() failed to %d, %d.\n", row, col);
138 void NCURSES_GetCursorPosition(char *row, char *col)
142 getyx(stdscr, trow, tcol); /* MACRO, no need to pass pointer */
148 void NCURSES_GetCharacterAtCursor(char *ch, int *fg_color, int
149 *bg_color, int *attribute)
151 /* If any of the pointers are NULL, ignore them */
152 /* We will eventually have to convert the color data */
154 *ch = (char) winch(stdscr);
156 *fg_color = WINE_WHITE;
158 *bg_color = WINE_BLACK;
163 void NCURSES_Refresh()
168 void NCURSES_ClearScreen()
173 int NCURSES_AllocColor(int color)
175 /* Currently support only internal colors */
178 case WINE_BLACK: return COLOR_BLACK;
179 case WINE_WHITE: return COLOR_WHITE;
180 case WINE_RED: return COLOR_RED;
181 case WINE_GREEN: return COLOR_GREEN;
182 case WINE_YELLOW: return COLOR_YELLOW;
183 case WINE_BLUE: return COLOR_BLUE;
184 case WINE_MAGENTA: return COLOR_MAGENTA;
185 case WINE_CYAN: return COLOR_CYAN;
188 FIXME(console, "Unable to allocate color %d\n", color);
190 /* Don't allocate a color... yet */
194 void NCURSES_SetBackgroundColor(int fg, int bg)
198 pair = get_color_pair(fg, bg);
200 bkgdset(COLOR_PAIR(pair));
203 #ifdef HAVE_RESIZETERM
205 void NCURSES_NotifyResizeScreen(int x, int y)
207 /* Note: This function gets called *after* another driver in the chain
208 calls ResizeScreen(). It is meant to resize the ncurses internal
209 data structures to know about the new window dimensions. */
214 #endif /* HAVE_RESIZETERM */
216 static int get_color_pair(int fg_color, int bg_color)
218 /* ncurses internally uses "color pairs" in addition to the "pallet" */
219 /* This isn't the best way to do this. Or even close */
221 static int current = 0;
222 static int fg[255]; /* 16 x 16 is enough */
226 /* The first pair is hardwired into ncurses */
230 for (x = 0; x <= current; x++)
232 if ((fg_color == fg[x]) && (bg_color == bg[x]))
234 TRACE(console, "Color pair: already allocated\n");
239 /* Need to allocate new color */
241 fg[current] = fg_color;
242 bg[current] = bg_color;
243 TRACE(console, "Color pair: allocated.\n");
244 return init_pair(current, fg_color, bg_color);
247 #endif /* WINE_NCURSES */