-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathport.cpp
283 lines (251 loc) · 6.8 KB
/
port.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <stdarg.h>
#include <locale.h>
#include <ncurses.h>
#include "colours.h"
#include "port.h"
static WINDOW *win = NULL;
static int begin_x;
static int begin_y;
static void init_pairs();
static unsigned short cp437[256] =
{
0x0000, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
0x25b6, 0x25c0, 0x2195, 0x203c, 0x00b6, 0x00a7, 0x25ac, 0x21a8,
0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc,
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x2302,
0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7,
0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9,
0x00ff, 0x00d6, 0x00dc, 0x00a2, 0x00a3, 0x00a5, 0x20a7, 0x0192,
0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba,
0x00bf, 0x2310, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
0x03b1, 0x00df, 0x0393, 0x03c0, 0x03a3, 0x03c3, 0x00b5, 0x03c4,
0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x03c6, 0x03b5, 0x2229,
0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248,
0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0,
};
char *itoa(int n, char *s, int radix)
{
if (n == 0)
{
*s++ = '0';
*s++ = 0;
return s;
}
if (n < 0)
{
*s++ = '-';
n = -n;
}
for (int tmp = radix; tmp <= n; tmp *= radix)
s++;
*++s = 0;
while (n)
{
int d = n % radix;
n /= radix;
*--s = '0' + d;
}
return s;
}
int stricmp(char *s1, char *s2)
{
return strcasecmp(s1, s2);
}
void startup()
{
setlocale(LC_CTYPE, "");
win = initscr();
/* FIXME: is this cast a good idea? */
atexit((void (*)()) endwin);
start_color();
init_pairs();
cbreak();
noecho();
nonl();
intrflush(win, FALSE);
meta(win, TRUE);
}
int kbhit(void)
{
int c;
nodelay(win, TRUE);
wtimeout(win, 0);
c = wgetch(win);
nodelay(win, FALSE);
if (c == -1)
c = 0;
else
ungetch(c);
return c;
}
int putch(unsigned char c)
{
wchar_t w = cp437[c];
int res;
res = waddnwstr(win, &w, 1);
wrefresh(win);
return res;
}
int getche(void)
{
int c = wgetch(win);
waddch(win, c);
wrefresh(win);
return c;
}
int cprintf(const char *fmt, ...)
{
int ret;
char buf[2048];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof buf, fmt, ap);
va_end(ap);
// FIXME: This should probably do CP437 translation
ret = waddstr(win, buf);
wrefresh(win);
return ret;
}
// TODO: Should these use screen or window-relative coordinates?
// (probably the latter)
void gotoxy(int x, int y)
{
wmove(win, y - 1, x - 1);
wcursyncup(win);
}
int wherex()
{
int x, y;
getyx(win, y, x);
(void) y;
return begin_x + x + 1;
}
int wherey()
{
int x, y;
getyx(win, y, x);
return begin_y + y + 1;
}
void window(int left, int top, int right, int bottom)
{
if (win != NULL && win != stdscr)
delwin(win);
begin_y = top - 1;
begin_x = left - 1;
int lines = bottom - top + 1;
int columns = right - left + 1;
win = subwin(stdscr, lines, columns, begin_y, begin_x);
}
void gettext(int left, int top, int right, int bottom, unsigned char *dest)
{
int orig_y, orig_x;
getyx(stdscr, orig_y, orig_x);
for (int y = top - 1; y <= bottom - 1; y++)
{
for (int x = left - 1; x <= right - 1; x++)
{
// FIXME make this not crap
// FIXME make it handle attributes too
wchar_t w;
mvinnwstr(y, x, &w, 1);
for (unsigned int i = 0; i < 256; i++)
{
if (cp437[i] == w)
{
*dest++ = w;
}
}
*dest = PAIR_NUMBER(inch() & A_COLOR) - 1;
if (*dest > WHITE)
*dest = MAGENTA;
if (inch() & A_BOLD)
*dest += DARKGRAY;
dest++;
}
}
move(orig_y, orig_x);
}
void puttext(int left, int top, int right, int bottom, unsigned char *str)
{
int orig_y, orig_x;
attr_t attrs;
short pair;
getyx(stdscr, orig_y, orig_x);
attr_get(&attrs, &pair, NULL);
for (int y = top - 1; y <= bottom - 1; y++)
{
for (int x = left - 1; x <= right - 1; x++)
{
unsigned char chr = *str++;
unsigned char col = *str++;
wchar_t w = cp437[chr];
if (w == 0) w = (wchar_t)' ';
textcolor(col);
mvaddnwstr(y, x, &w, 1);
}
}
attr_set(attrs, pair, NULL);
move(orig_y, orig_x);
}
static void init_pairs(void)
{
init_pair(BLACK + 1, COLOR_BLACK, COLOR_BLACK);
init_pair(BLUE + 1, COLOR_BLUE, COLOR_BLACK);
init_pair(GREEN + 1, COLOR_GREEN, COLOR_BLACK);
init_pair(CYAN + 1, COLOR_CYAN, COLOR_BLACK);
init_pair(RED + 1, COLOR_RED, COLOR_BLACK);
init_pair(MAGENTA + 1, COLOR_MAGENTA, COLOR_BLACK);
init_pair(BROWN + 1, COLOR_YELLOW, COLOR_BLACK);
init_pair(LIGHTGRAY + 1, COLOR_WHITE, COLOR_BLACK);
}
void textcolor(int col)
{
int attrs = 0;
if (col > LIGHTGRAY)
{
attrs |= A_BOLD;
col -= DARKGRAY;
}
attrs |= COLOR_PAIR(col + 1);
attrset(attrs);
wattrset(win, attrs);
}
void textbackground(int col)
{
/* never called with anything but col = 0 */
}
void clrscr()
{
textcolor(LIGHTGRAY);
wclear(win);
}
void delay(int time)
{
usleep(time * 1000);
}
void _setcursortype(int cursortype)
{
curs_set(cursortype);
}