2 /* Copyright 1999 - Joseph Pranevich */
8 #include "console.h" /* Must define WINE_NCURSES */
12 /* This is the console driver for systems that support the ncurses
16 /* Actually, this should work for curses, as well. But there may be
17 individual functions that are unsupported in plain curses or other
18 variants. Those should be detected and special-cased by autoconf.
21 /* When creating new drivers, you need to assign all the functions that
22 that driver supports into the driver struct. If it is a supplementary
23 driver, it should make sure to perserve the old values.
26 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(console);
31 #undef ERR /* Use ncurses's err() */
40 SCREEN *ncurses_screen;
42 static int get_color_pair(int fg_color, int bg_color);
44 const char *color_names[] = {"null", "black", "blue", "green",
45 "cyan", "magenta", "brown", "red", "light gray", "dark gray",
46 "light blue", "light green", "light red", "light magenta",
47 "light cyan", "yellow", "white"};
51 /* This should be the root driver so we can ignore anything
52 already in the struct. */
54 driver.norefresh = FALSE;
56 driver.init = NCURSES_Init;
57 driver.write = NCURSES_Write;
58 driver.close = NCURSES_Close;
59 driver.moveCursor = NCURSES_MoveCursor;
60 driver.getCursorPosition = NCURSES_GetCursorPosition;
61 driver.getCharacterAtCursor = NCURSES_GetCharacterAtCursor;
62 driver.clearScreen = NCURSES_ClearScreen;
63 driver.allocColor = NCURSES_AllocColor;
65 driver.setBackgroundColor = NCURSES_SetBackgroundColor;
67 #ifdef HAVE_RESIZETERM
68 driver.notifyResizeScreen = NCURSES_NotifyResizeScreen;
69 #endif /* HAVE_RESIZETERM */
71 driver.checkForKeystroke = NCURSES_CheckForKeystroke;
72 driver.getKeystroke = NCURSES_GetKeystroke;
74 driver.refresh = NCURSES_Refresh;
79 char terminal_type[80];
81 PROFILE_GetWineIniString("console", "TerminalType",
82 "xterm", terminal_type, 79);
84 ncurses_screen = newterm(terminal_type, driver.console_out,
86 set_term(ncurses_screen);
91 intrflush(stdscr, FALSE);
93 nodelay(stdscr, TRUE);
96 void NCURSES_Write(char output, int fg, int bg, int attribute)
102 fg = COLOR_WHITE; /* Default */
105 bg = COLOR_BLACK; /* Default */
107 pair = get_color_pair(fg, bg);
109 if (waddch(stdscr, output | COLOR_PAIR(pair)) == ERR)
111 NCURSES_GetCursorPosition(&row, &col);
112 FIXME("NCURSES: waddch() failed at %d, %d.\n", row, col);
121 void NCURSES_GetKeystroke(char *scan, char *ascii)
123 while (!NCURSES_CheckForKeystroke(scan, ascii))
124 {} /* Wait until keystroke is detected */
126 /* When it is detected, we will already have the right value
127 in scan and ascii, but we need to take this keystroke
128 out of the buffer. */
132 int NCURSES_CheckForKeystroke(char *scan, char *ascii)
134 /* We don't currently support scan codes here */
137 temp = wgetch(stdscr);
144 ungetch(temp); /* Keystroke not removed from buffer */
145 *ascii = (char) temp;
150 void NCURSES_MoveCursor(char row, char col)
152 if (wmove(stdscr, row, col) == ERR)
153 FIXME("NCURSES: wmove() failed to %d, %d.\n", row, col);
156 void NCURSES_GetCursorPosition(char *row, char *col)
160 getyx(stdscr, trow, tcol); /* MACRO, no need to pass pointer */
166 void NCURSES_GetCharacterAtCursor(char *ch, int *fg_color, int
167 *bg_color, int *attribute)
169 /* If any of the pointers are NULL, ignore them */
170 /* We will eventually have to convert the color data */
172 *ch = (char) winch(stdscr);
174 *fg_color = WINE_WHITE;
176 *bg_color = WINE_BLACK;
181 void NCURSES_Refresh()
186 void NCURSES_ClearScreen()
191 int NCURSES_AllocColor(int color)
193 /* Currently support only internal colors */
196 case WINE_BLACK: return COLOR_BLACK;
197 case WINE_WHITE: return COLOR_WHITE;
198 case WINE_RED: return COLOR_RED;
199 case WINE_GREEN: return COLOR_GREEN;
200 case WINE_YELLOW: return COLOR_YELLOW;
201 case WINE_BLUE: return COLOR_BLUE;
202 case WINE_MAGENTA: return COLOR_MAGENTA;
203 case WINE_CYAN: return COLOR_CYAN;
206 FIXME("Unable to allocate color %d (%s)\n", color,
209 /* Don't allocate a color... yet */
213 void NCURSES_SetBackgroundColor(int fg, int bg)
217 pair = get_color_pair(fg, bg);
219 wbkgd(stdscr, COLOR_PAIR(pair));
223 void NCURSES_GetBackgroundColor(int *fg, int *bg)
226 short pair, sfg, sbg;
228 background = getbkgd(stdscr);
230 pair = (!A_CHARTEXT & background);
232 pair_content(pair, &sfg, &sbg);
237 #endif /* HAVE_GETBKGD */
239 #ifdef HAVE_RESIZETERM
241 void NCURSES_NotifyResizeScreen(int x, int y)
243 /* Note: This function gets called *after* another driver in the chain
244 calls ResizeScreen(). It is meant to resize the ncurses internal
245 data structures to know about the new window dimensions. */
247 TRACE("Terminal resized to y: %d, x: %d\n", y, x);
252 #endif /* HAVE_RESIZETERM */
254 static int get_color_pair(int fg_color, int bg_color)
256 /* ncurses internally uses "color pairs" in addition to the "pallet" */
257 /* This isn't the best way to do this. Or even close */
259 static int current = 0;
260 static int fg[255]; /* 16 x 16 is enough */
264 /* The first pair is hardwired into ncurses */
268 for (x = 0; x <= current; x++)
270 if ((fg_color == fg[x]) && (bg_color == bg[x]))
272 TRACE("Color pair: already allocated\n");
277 /* Need to allocate new color */
279 fg[current] = fg_color;
280 bg[current] = bg_color;
281 TRACE("Color pair: allocated.\n");
282 return init_pair(current, fg_color, bg_color);
285 #endif /* WINE_NCURSES */