This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio.cc
470 lines (390 loc) · 10.4 KB
/
io.cc
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*---------------------------------------------------------------------------
* ADOM Sage - frontend for ADOM
*
* Copyright (c) 2002 Joshua Kelley; see LICENSE for details
*
* Functions dealing with I/O.
*/
#include "adom-sage.h"
/*---------------------------------------------------------------------------
* Globals and type definitions
*/
typedef pair<attr_t, short> Color;
typedef STRING_CASE_HASH(Color) ColorMap;
int colorpair[8]; // Curses color pairs corresponding to basic colors
int highlight_colorpair[8];
ColorMap *colormap;
KeyQueue *key_queue;
KeyQueue *macro_queue;
KeyQueue *current_input;
KeyQueue *current_keys;
KeyQueue *prev_keys;
bool no_wrefresh;
/*---------------------------------------------------------------------------
* Initialization
*/
void init_io(void)
{
// I wish I knew enough about C++ to know if this code was really OK.
colormap = new ColorMap;
(*colormap)["blue"] = Color(A_NORMAL, COLOR_BLUE);
(*colormap)["green"] = Color(A_NORMAL, COLOR_GREEN);
(*colormap)["cyan"] = Color(A_NORMAL, COLOR_CYAN);
(*colormap)["red"] = Color(A_NORMAL, COLOR_RED);
(*colormap)["magenta"] = Color(A_NORMAL, COLOR_MAGENTA);
(*colormap)["brown"] = Color(A_NORMAL, COLOR_YELLOW);
(*colormap)["light gray"] = Color(A_NORMAL, COLOR_WHITE);
(*colormap)["dark gray"] = Color(A_BOLD, COLOR_BLACK);
(*colormap)["light blue"] = Color(A_BOLD, COLOR_BLUE);
(*colormap)["light green"] = Color(A_BOLD, COLOR_GREEN);
(*colormap)["light cyan"] = Color(A_BOLD, COLOR_CYAN);
(*colormap)["light red"] = Color(A_BOLD, COLOR_RED);
(*colormap)["light magenta"] = Color(A_BOLD, COLOR_MAGENTA);
(*colormap)["yellow"] = Color(A_BOLD, COLOR_YELLOW);
(*colormap)["white"] = Color(A_BOLD, COLOR_WHITE);
key_queue = new KeyQueue;
macro_queue = new KeyQueue;
current_input = new KeyQueue;
current_keys = new KeyQueue;
prev_keys = new KeyQueue;
}
/*---------------------------------------------------------------------------
* General I/O functions
*/
// To be called before waddch; converts to line-drawing chars if enabled
chtype convert_char (chtype in)
{
if (in == '#' && config->draw_blocks)
{
return ACS_CKBOARD;
}
else if (in == '-' && config->draw_lines)
{
return ACS_HLINE;
}
else if (in == '|' && config->draw_lines)
{
return ACS_VLINE;
}
else if (in == '.' && config->draw_dots)
{
return ACS_BULLET;
}
else
{
return in;
}
}
// To be called with winch; converts from line-drawing chars
// Hack - ncurses markes line-drawing chars with A_ALTCHARSET, but PDCurses
// just uses ASCII values above 127. We design our code to be able to handle
// both.
chtype reverse_convert_char (chtype in)
{
if ((in & A_ALTCHARSET) || ((in & A_CHARTEXT) > 127))
{
attr_t attr = (in & A_ATTRIBUTES) & !A_ALTCHARSET;
chtype ch = in & (A_CHARTEXT | A_ALTCHARSET);
if (ch == ACS_BULLET)
{
return attr | '.';
}
else if (ch == ACS_HLINE)
{
return attr | '-';
}
else if (ch == ACS_VLINE)
{
return attr | '|';
}
else if (ch == ACS_CKBOARD)
{
return attr | '#';
}
else
{
log(log_errors, "Error: unknown altcharset character %c\n",
(char) (in & A_CHARTEXT));
return in;
}
}
else
{
return in;
}
}
// Looks up a colorname and puts its attr and color in *attr and *color.
// Returns 1 if a valid colorname, 0 otherwise.
int get_color (char *colorname, attr_t *attr, short *color)
{
ColorMap::iterator iter;
iter = colormap->find(colorname);
if (iter == colormap->end())
{
return 0;
}
else
{
*attr = iter->second.first;
*color = iter->second.second;
return 1;
}
}
int morewait(WINDOW *win, int skip_more)
{
int ch;
if (skip_more)
{
return 1;
}
if (config->fast_more)
{
ch = real_wgetch(win);
}
else
{
do
{
ch = real_wgetch(win);
}
while (ch != ' ' && ch != '\r');
}
return (ch == '\r');
}
void show_msg(WINDOW *win, const char *msg)
{
int y, x;
getyx(win, y, x);
real_wmove(win, 1, 0);
wclrtoeol(win);
real_wmove(win, 0, 0);
wclrtoeol(win);
real_waddnstr(win, msg, -1);
real_waddnstr(win, " (more)", -1);
morewait(win, 0);
real_wmove(win, 0, 0);
wclrtoeol(win);
real_wmove(win, y, x);
}
int get_key(WINDOW *win, int cursor_visibility, attr_t cursor_attr,
bool allow_mouse)
{
int result;
KeyQueue *queue = !key_queue->empty() ? key_queue : macro_queue;
if (!queue->empty())
{
result = queue->front();
current_keys->push_back(result);
queue->pop_front();
no_wrefresh = config->quiet_macros && !queue->empty();
}
else
{
int y, x, old_visibility;
chtype ch;
attr_t attrs;
short pair;
// Handle cursor - redraw or hide if desired.
if (cursor_visibility != 1)
{
old_visibility = curs_set(cursor_visibility);
if (cursor_attr != A_NORMAL)
{
// If ADOM set A_BOLD earlier, it will get or'ed in here unless
// we reset to A_NORMAL. Curses library can be a bit of a pain.
getyx(win, y, x);
wattr_get(win, &attrs, &pair, NULL);
attrset(A_NORMAL);
ch = real_winch(win);
real_waddch(win, ch | cursor_attr);
real_wmove(win, y, x);
}
}
// Get key; make sure it's not a mouse event if asked to do so
do
{
result = real_wgetch(win);
}
while (!allow_mouse && result == KEY_MOUSE);
current_keys->push_back(result);
// Restore cursor to normal
if (cursor_visibility != 1)
{
curs_set(old_visibility);
if (cursor_attr != A_NORMAL)
{
real_waddch(win, ch);
real_wmove(win, y, x);
wattr_set(win, attrs, pair, NULL);
}
}
}
return result;
}
// Replacement for wgetnstr that takes input off of the input queue if available
int get_string(WINDOW *win, char *str, int n)
{
int ch;
vector<int> v;
KeyQueue *queue = !key_queue->empty() ? key_queue : macro_queue;
// ungetch prepends to beginning of queue, so copy input to a temporary
// vector so we can easily reverse it
while (!queue->empty())
{
ch = queue->front();
v.push_back(ch);
queue->pop_front();
if (ch == '\n')
{
break;
}
}
while (!v.empty())
{
ungetch(v.back());
v.pop_back();
}
no_wrefresh = config->quiet_macros && !queue->empty();
return real_wgetnstr(win, str, n);
}
// Pads a string to the desired length with forced spaces. ADOM uses
// underscores as forced spaces.
void pad_string(char *str, int pad_length)
{
int pad_count;
size_t i;
for (i = 0, pad_count = pad_length; str[i] != '\0'; i++)
if (str[i] != '\x03' && (str[i] <= '\xC0' || str[i] >= '\xCF'))
{
pad_count--;
}
for (i = strlen(str); pad_count > 0; i++, pad_count--)
{
str[i] = '_';
}
str[i] = '\0';
}
void fill_char(WINDOW *win, chtype ch, int y, int x, int len)
{
real_wmove(win, y, x);
for (int i = 0; i < len; i++)
{
real_waddch(win, ch);
}
}
/*---------------------------------------------------------------------------
* Mouse functions - system specific
*/
bool find_ch(WINDOW *win, chtype ch, int &y, int &x)
{
int cury, curx, starty, startx;
getyx(win, starty, startx);
cury = starty;
curx = startx;
while (1)
{
// Advance to next position
curx++;
if (curx == COLS)
{
curx = 0;
cury++;
if (cury == LINES - 3)
{
cury = 2;
}
}
if (cury == starty && curx == startx)
{
return false;
}
// Check position
real_wmove(win, cury, curx);
if ((reverse_convert_char(real_winch(win)) & A_CHARTEXT) == ch)
{
y = cury;
x = curx;
real_wmove(win, starty, startx);
log(log_libcalls, "Found at %i, %i\n", y, x);
return true;
}
}
}
void move_cursor(WINDOW *win, int y, int x)
{
int cury, curx, dir;
getyx(win, cury, curx);
while (curx != x || cury != y)
{
log(log_libcalls, "From %i, %i to %i, %i\n", cury, curx, y, x);
dir = (y < cury ? 2 : (y > cury ? 0 : 1)) * 3;
dir += (x < curx ? 0 : (x > curx ? 2 : 1));
key_queue->push_cmd((Command) (cmdMoveSW + dir));
cury += (y < cury ? -1 : (y > cury ? 1 : 0));
curx += (x < curx ? -1 : (x > curx ? 1 : 0));
}
}
/*---------------------------------------------------------------------------
* Mouse functions - system specific
*/
#ifndef _WIN32
MouseCookie mouse_enable(void)
{
MouseCookie result;
mousemask(ALL_MOUSE_EVENTS, &result);
return result;
}
void mouse_restore(MouseCookie cookie)
{
mousemask(cookie, NULL);
}
void get_mouse_event(int *y, int *x, int *button)
{
MEVENT mevent;
getmouse(&mevent);
*y = mevent.y;
*x = mevent.x;
switch (mevent.bstate)
{
case BUTTON1_CLICKED:
*button = 1;
break;
case BUTTON2_CLICKED:
*button = 2;
break;
case BUTTON3_CLICKED:
*button = 3;
break;
case BUTTON4_CLICKED:
*button = 4;
break;
default:
*button = 0;
}
}
#else
MouseCookie mouse_enable(void)
{
MouseCookie result;
result = getmouse();
mouse_set(ALL_MOUSE_EVENTS);
return result;
}
void mouse_restore(MouseCookie cookie)
{
mouse_set(cookie);
}
void get_mouse_event(int *y, int *x, int *button)
{
request_mouse_pos();
*y = MOUSE_Y_POS;
*x = MOUSE_X_POS;
*button = 0;
for (int i = 1; i <= 4; i++)
if (BUTTON_STATUS(i))
{
*button = i;
break;
}
}
#endif