Extracting parts of the formatting method into configuration and an options helper...
[cslatevm.git] / src / plugins / old / ncurses / ncurses-console.c
blob010057643ffeb87cce4bd05a6b2df3f01d3e2f45
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <ncurses.h>
7 #include "slatevm.h"
8 #include "image.h"
9 #include "ncurses-console.h"
11 extern int setupterm(char* term, int fildes, int* errret);
13 // this is a copy paste from boot.c
14 void
15 error (const char * message, ...)
17 va_list args;
19 va_start (args, message);
20 fprintf (stderr, "Slate: ");
21 vfprintf (stderr, message, args);
22 fputc ('\n', stderr);
23 va_end (args);
24 exit (EXIT_FAILURE);
27 int
28 sc_isAvailable ()
30 int err;
32 if (setupterm((char *)0, 1, &err) == ERR)
33 return FALSE;
35 return tigetstr("cup") != NULL;
38 int
39 sc_enterStructuredMode ()
41 if (initscr() == NULL)
42 return FALSE;
44 start_color();
46 scrollok(stdscr, TRUE);
47 immedok(stdscr, FALSE);
48 leaveok(stdscr, FALSE);
49 idlok(stdscr, TRUE);
50 idcok(stdscr, TRUE);
51 intrflush(stdscr, FALSE);
52 keypad(stdscr, FALSE);
54 notimeout(stdscr, FALSE);
56 raw();
57 noecho();
58 nl();
60 return TRUE;
63 int
64 sc_leaveStructuredMode ()
66 return (endwin() != ERR);
69 // TODO: this assumes string = byte array vm setup
70 char *
71 sc_keySequenceOf(char *keyName)
73 char *keySequence = tigetstr(keyName);
74 if ((int)keySequence == -1 || (int)keySequence == 0)
75 return FALSE;
77 if (strlen(keySequence) > 1 && keySequence[0] == 27)
78 keySequence += 1;
80 return keySequence;
83 int
84 sc_columns ()
86 int cols, lines;
87 getmaxyx(stdscr, lines, cols);
89 return cols;
92 int
93 sc_rows ()
95 int cols, rows;
96 getmaxyx(stdscr, rows, cols);
98 return rows;
102 sc_write (void *bytes, int size, int offset)
104 return (waddnstr(stdscr, ((const char *)bytes) + offset, size) != ERR);
108 sc_scroll (int lines)
110 return (wscrl(stdscr, lines) != ERR);
114 sc_clear ()
116 return (wclear(stdscr) != ERR);
120 sc_clearToEOS ()
122 return (wclrtobot(stdscr) != ERR);
126 sc_clearToEOL()
128 return (wclrtoeol(stdscr) != ERR);
132 sc_moveToXY (int x, int y)
134 return (wmove(stdscr, y, x) != ERR);
138 sc_cursorX ()
140 int x, y;
141 getyx(stdscr, y, x);
142 return x;
146 sc_cursorY ()
148 int x, y;
149 getyx(stdscr, y, x);
150 return y;
154 sc_nextEvent (int blockingMillisecs)
156 int ch;
158 wtimeout(stdscr, blockingMillisecs);
159 ch = wgetch(stdscr);
160 if (ch == ERR)
162 if (blockingMillisecs < 0)
163 error("Error reading console?!");
164 else
165 return -1;
167 return ch;
171 sc_hasEvent ()
173 int ch;
175 wtimeout(stdscr, 0);
176 ch = wgetch(stdscr);
177 if (ch == ERR)
179 return FALSE;
181 else
183 ungetch(ch);
184 return TRUE;
189 sc_flush()
191 return (wrefresh(stdscr) != ERR);
195 sc_deleteChar()
197 return (wdelch(stdscr) != ERR);
201 sc_deleteLines(int lines)
203 return (winsdelln(stdscr, -lines) != ERR);
207 sc_insertLines(int lines)
209 return (winsdelln(stdscr, lines) != ERR);
213 sc_hideCursor()
215 return curs_set(0);
219 sc_showCursor()
221 return curs_set(1);
225 sc_initColorPair(int pair, int fg, int bg)
227 return (init_pair(pair, fg, bg) != ERR);
231 sc_maxColorPairs()
233 return COLOR_PAIRS;
236 void
237 sc_setAttributes(int mode, int pair)
239 wattrset(stdscr, (mode << 16) | COLOR_PAIR(pair));
243 void
244 sc_setColor(int foreground, int background)
246 short color;
247 init_pair(color, foreground, background);
249 wcolor_set(stdscr, color, NULL);
252 void
253 sc_turnOnAttribute(int attribute)
255 wattron(stdscr, attribute);
258 void
259 sc_turnOffAttribute(int attribute)
261 wattr_off(stdscr, attribute);