Release 990214.
[wine/multimedia.git] / console / ncurses.c
blobaf1fafd76fbb21d18e7a0eafbef04c48455e3d34
1 /* ncurses.c */
2 /* Copyright 1999 - Joseph Pranevich */
4 #include "config.h"
5 #include "console.h"
7 #ifdef WINE_NCURSES
9 /* This is the console driver for systems that support the ncurses
10 interface.
13 /* Actually, this should work for curses, as well. But there may be
14 individual functions that are unsupported in plain curses or other
15 variants. Those should be detected and special-cased by autoconf.
18 /* When creating new drivers, you need to assign all the functions that
19 that driver supports into the driver struct. If it is a supplementary
20 driver, it should make sure to perserve the old values.
23 #include "debug.h"
24 #undef ERR /* Use ncurses's err() */
25 #ifdef HAVE_NCURSES_H
26 # include <ncurses.h>
27 #else
28 # ifdef HAVE_CURSES_H
29 # include <curses.h>
30 # endif
31 #endif
33 SCREEN *ncurses_screen;
35 static int get_color_pair(int fg_color, int bg_color);
37 void NCURSES_Start()
39 /* This should be the root driver so we can ignore anything
40 already in the struct. */
42 driver.norefresh = FALSE;
44 driver.init = NCURSES_Init;
45 driver.write = NCURSES_Write;
46 driver.close = NCURSES_Close;
47 driver.moveCursor = NCURSES_MoveCursor;
48 driver.getCursorPosition = NCURSES_GetCursorPosition;
49 driver.getCharacterAtCursor = NCURSES_GetCharacterAtCursor;
50 driver.clearScreen = NCURSES_ClearScreen;
51 driver.allocColor = NCURSES_AllocColor;
52 driver.setBackgroundColor = NCURSES_SetBackgroundColor;
53 #ifdef HAVE_RESIZETERM
54 driver.notifyResizeScreen = NCURSES_NotifyResizeScreen;
55 #endif
57 driver.checkForKeystroke = NCURSES_CheckForKeystroke;
58 driver.getKeystroke = NCURSES_GetKeystroke;
60 driver.refresh = NCURSES_Refresh;
63 void NCURSES_Init()
65 ncurses_screen = newterm("xterm", driver.console_out,
66 driver.console_in);
67 set_term(ncurses_screen);
68 start_color();
69 raw();
70 noecho();
71 nonl();
72 /* intrflush(stdscr, FALSE); */
73 keypad(stdscr, TRUE);
74 nodelay(stdscr, TRUE);
77 void NCURSES_Write(char output, int fg, int bg, int attribute)
79 char row, col;
80 int pair;
82 if (!fg)
83 fg = COLOR_WHITE; /* Default */
85 if (!bg)
86 bg = COLOR_BLACK; /* Default */
88 pair = get_color_pair(fg, bg);
90 if (waddch(stdscr, output | COLOR_PAIR(pair)) == ERR)
92 NCURSES_GetCursorPosition(&row, &col);
93 FIXME(console, "NCURSES: waddch() failed at %d, %d.\n", row, col);
97 void NCURSES_Close()
99 endwin();
102 void NCURSES_GetKeystroke(char *scan, char *ascii)
104 while (!NCURSES_CheckForKeystroke(scan, ascii))
105 {} /* Wait until keystroke is detected */
107 /* When it is detected, we will already have the right value
108 in scan and ascii, but we need to take this keystroke
109 out of the buffer. */
110 wgetch(stdscr);
113 int NCURSES_CheckForKeystroke(char *scan, char *ascii)
115 /* We don't currently support scan codes here */
116 /* FIXME */
117 int temp;
118 temp = wgetch(stdscr);
119 if (temp == ERR)
121 return FALSE;
123 else
125 ungetch(temp); /* Keystroke not removed from buffer */
126 *ascii = (char) temp;
127 return TRUE;
131 void NCURSES_MoveCursor(char row, char col)
133 if (wmove(stdscr, row, col) == ERR)
134 FIXME(console, "NCURSES: wmove() failed to %d, %d.\n", row, col);
137 void NCURSES_GetCursorPosition(char *row, char *col)
139 int trow, tcol;
141 getyx(stdscr, trow, tcol); /* MACRO, no need to pass pointer */
143 *row = (char) trow;
144 *col = (char) tcol;
147 void NCURSES_GetCharacterAtCursor(char *ch, int *fg_color, int
148 *bg_color, int *attribute)
150 /* If any of the pointers are NULL, ignore them */
151 /* We will eventually have to convert the color data */
152 if (ch)
153 *ch = (char) winch(stdscr);
154 if (fg_color)
155 *fg_color = WINE_WHITE;
156 if (bg_color)
157 *bg_color = WINE_BLACK;
158 if (attribute)
159 *attribute = 0;
162 void NCURSES_Refresh()
164 wrefresh(stdscr);
167 void NCURSES_ClearScreen()
169 werase(stdscr);
172 int NCURSES_AllocColor(int color)
174 /* Currently support only internal colors */
175 switch (color)
177 case WINE_BLACK: return COLOR_BLACK;
178 case WINE_WHITE: return COLOR_WHITE;
179 case WINE_RED: return COLOR_RED;
180 case WINE_GREEN: return COLOR_GREEN;
181 case WINE_YELLOW: return COLOR_YELLOW;
182 case WINE_BLUE: return COLOR_BLUE;
183 case WINE_MAGENTA: return COLOR_MAGENTA;
184 case WINE_CYAN: return COLOR_CYAN;
187 FIXME(console, "Unable to allocate color %d\n", color);
189 /* Don't allocate a color... yet */
190 return 0;
193 void NCURSES_SetBackgroundColor(int fg, int bg)
195 int pair;
197 pair = get_color_pair(fg, bg);
199 bkgdset(COLOR_PAIR(pair));
202 #ifdef HAVE_RESIZETERM
204 void NCURSES_NotifyResizeScreen(int x, int y)
206 /* Note: This function gets called *after* another driver in the chain
207 calls ResizeScreen(). It is meant to resize the ncurses internal
208 data structures to know about the new window dimensions. */
210 resizeterm(y, x);
213 #endif /* HAVE_RESIZETERM */
215 static int get_color_pair(int fg_color, int bg_color)
217 /* ncurses internally uses "color pairs" in addition to the "pallet" */
218 /* This isn't the best way to do this. Or even close */
220 static int current = 0;
221 static int fg[255]; /* 16 x 16 is enough */
222 static int bg[255];
223 int x;
225 /* The first pair is hardwired into ncurses */
226 fg[0] = COLOR_WHITE;
227 bg[0] = COLOR_BLACK;
229 for (x = 0; x <= current; x++)
231 if ((fg_color == fg[x]) && (bg_color == bg[x]))
233 TRACE(console, "Color pair: already allocated\n");
234 return x;
238 /* Need to allocate new color */
239 current++;
240 fg[current] = fg_color;
241 bg[current] = bg_color;
242 TRACE(console, "Color pair: allocated.\n");
243 return init_pair(current, fg_color, bg_color);
246 #endif /* WINE_NCURSES */