2 * Copyright 1999 - Joseph Pranevich
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "console.h" /* Must define WINE_NCURSES */
27 /* This is the console driver for systems that support the ncurses
31 /* Actually, this should work for curses, as well. But there may be
32 individual functions that are unsupported in plain curses or other
33 variants. Those should be detected and special-cased by autoconf.
36 /* When creating new drivers, you need to assign all the functions that
37 that driver supports into the driver struct. If it is a supplementary
38 driver, it should make sure to perserve the old values.
41 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(console);
46 #undef ERR /* Use ncurses's err() */
55 SCREEN *ncurses_screen;
57 static int get_color_pair(int fg_color, int bg_color);
59 const char *color_names[] = {"null", "black", "blue", "green",
60 "cyan", "magenta", "brown", "red", "light gray", "dark gray",
61 "light blue", "light green", "light red", "light magenta",
62 "light cyan", "yellow", "white"};
66 /* This should be the root driver so we can ignore anything
67 already in the struct. */
69 driver.norefresh = FALSE;
71 driver.init = NCURSES_Init;
72 driver.write = NCURSES_Write;
73 driver.close = NCURSES_Close;
74 driver.moveCursor = NCURSES_MoveCursor;
75 driver.getCursorPosition = NCURSES_GetCursorPosition;
76 driver.getCharacterAtCursor = NCURSES_GetCharacterAtCursor;
77 driver.clearScreen = NCURSES_ClearScreen;
78 driver.allocColor = NCURSES_AllocColor;
80 driver.setBackgroundColor = NCURSES_SetBackgroundColor;
82 #ifdef HAVE_RESIZETERM
83 driver.notifyResizeScreen = NCURSES_NotifyResizeScreen;
84 #endif /* HAVE_RESIZETERM */
86 driver.checkForKeystroke = NCURSES_CheckForKeystroke;
87 driver.getKeystroke = NCURSES_GetKeystroke;
89 driver.refresh = NCURSES_Refresh;
94 char terminal_type[80];
96 PROFILE_GetWineIniString("console", "TerminalType",
97 "xterm", terminal_type, 79);
99 ncurses_screen = newterm(terminal_type, driver.console_out,
101 set_term(ncurses_screen);
106 intrflush(stdscr, FALSE);
107 keypad(stdscr, TRUE);
108 nodelay(stdscr, TRUE);
111 void NCURSES_Write(char output, int fg, int bg, int attribute)
117 fg = COLOR_WHITE; /* Default */
120 bg = COLOR_BLACK; /* Default */
122 pair = get_color_pair(fg, bg);
124 if (waddch(stdscr, output | COLOR_PAIR(pair)) == ERR)
126 NCURSES_GetCursorPosition(&row, &col);
127 FIXME("NCURSES: waddch() failed at %d, %d.\n", row, col);
136 void NCURSES_GetKeystroke(char *scan, char *ascii)
138 while (!NCURSES_CheckForKeystroke(scan, ascii))
139 {} /* Wait until keystroke is detected */
141 /* When it is detected, we will already have the right value
142 in scan and ascii, but we need to take this keystroke
143 out of the buffer. */
147 int NCURSES_CheckForKeystroke(char *scan, char *ascii)
149 /* We don't currently support scan codes here */
152 temp = wgetch(stdscr);
159 ungetch(temp); /* Keystroke not removed from buffer */
160 *ascii = (char) temp;
165 void NCURSES_MoveCursor(char row, char col)
167 if (wmove(stdscr, row, col) == ERR)
168 FIXME("NCURSES: wmove() failed to %d, %d.\n", row, col);
171 void NCURSES_GetCursorPosition(char *row, char *col)
175 getyx(stdscr, trow, tcol); /* MACRO, no need to pass pointer */
181 void NCURSES_GetCharacterAtCursor(char *ch, int *fg_color, int
182 *bg_color, int *attribute)
184 /* If any of the pointers are NULL, ignore them */
185 /* We will eventually have to convert the color data */
187 *ch = (char) winch(stdscr);
189 *fg_color = WINE_WHITE;
191 *bg_color = WINE_BLACK;
196 void NCURSES_Refresh()
201 void NCURSES_ClearScreen()
206 int NCURSES_AllocColor(int color)
208 /* Currently support only internal colors */
211 case WINE_BLACK: return COLOR_BLACK;
212 case WINE_WHITE: return COLOR_WHITE;
213 case WINE_RED: return COLOR_RED;
214 case WINE_GREEN: return COLOR_GREEN;
215 case WINE_YELLOW: return COLOR_YELLOW;
216 case WINE_BLUE: return COLOR_BLUE;
217 case WINE_MAGENTA: return COLOR_MAGENTA;
218 case WINE_CYAN: return COLOR_CYAN;
221 FIXME("Unable to allocate color %d (%s)\n", color,
224 /* Don't allocate a color... yet */
228 void NCURSES_SetBackgroundColor(int fg, int bg)
232 pair = get_color_pair(fg, bg);
234 wbkgd(stdscr, COLOR_PAIR(pair));
238 void NCURSES_GetBackgroundColor(int *fg, int *bg)
241 short pair, sfg, sbg;
243 background = getbkgd(stdscr);
245 pair = (!A_CHARTEXT & background);
247 pair_content(pair, &sfg, &sbg);
252 #endif /* HAVE_GETBKGD */
254 #ifdef HAVE_RESIZETERM
256 void NCURSES_NotifyResizeScreen(int x, int y)
258 /* Note: This function gets called *after* another driver in the chain
259 calls ResizeScreen(). It is meant to resize the ncurses internal
260 data structures to know about the new window dimensions. */
262 TRACE("Terminal resized to y: %d, x: %d\n", y, x);
267 #endif /* HAVE_RESIZETERM */
269 static int get_color_pair(int fg_color, int bg_color)
271 /* ncurses internally uses "color pairs" in addition to the "pallet" */
272 /* This isn't the best way to do this. Or even close */
274 static int current = 0;
275 static int fg[255]; /* 16 x 16 is enough */
279 /* The first pair is hardwired into ncurses */
283 for (x = 0; x <= current; x++)
285 if ((fg_color == fg[x]) && (bg_color == bg[x]))
287 TRACE("Color pair: already allocated\n");
292 /* Need to allocate new color */
294 fg[current] = fg_color;
295 bg[current] = bg_color;
296 TRACE("Color pair: allocated.\n");
297 return init_pair(current, fg_color, bg_color);
300 #endif /* WINE_NCURSES */