-
Notifications
You must be signed in to change notification settings - Fork 49
/
vcurses.c
405 lines (326 loc) · 8.64 KB
/
vcurses.c
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
/*
* This file is part of the Advance project.
*
* Copyright (C) 2004 Andrea Mazzoleni
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In addition, as a special exception, Andrea Mazzoleni
* gives permission to link the code of this program with
* the MAME library (or with modified versions of MAME that use the
* same license as MAME), and distribute linked combinations including
* the two. You must obey the GNU General Public License in all
* respects for all of the code used other than MAME. If you modify
* this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*/
#include "portable.h"
#include "vcurses.h"
#include "log.h"
#include "error.h"
#include "snstring.h"
#include "target.h"
#include "oslinux.h"
#include <curses.h>
/***************************************************************************/
/* State */
typedef struct curses_internal_struct {
adv_bool active;
adv_bool mode_active;
unsigned size_x;
unsigned size_y;
unsigned font_size_x;
unsigned font_size_y;
unsigned char* ptr;
} curses_internal;
static curses_internal curses_state;
unsigned char* (*curses_write_line)(unsigned y);
static adv_device DEVICE[] = {
{ "auto", -1, "CURSES video" },
{ 0, 0, 0 }
};
/***************************************************************************/
/* Functions */
static unsigned char* curses_linear_write_line(unsigned y)
{
return curses_state.ptr + curses_state.size_x * y * 2;
}
adv_bool curses_is_active(void)
{
return curses_state.active != 0;
}
adv_bool curses_mode_is_active(void)
{
return curses_state.mode_active != 0;
}
unsigned curses_flags(void)
{
assert(curses_is_active());
return VIDEO_DRIVER_FLAGS_MODE_TEXT | VIDEO_DRIVER_FLAGS_OUTPUT_FULLSCREEN;
}
adv_error curses_init(int device_id, adv_output output, unsigned overlay_size, adv_cursor cursor)
{
assert(!curses_is_active());
(void)cursor;
log_std(("video:curses: curses_init()\n"));
if (target_wm()) {
error_set("Unsupported in X.\n");
return -1;
}
if (output != adv_output_auto && output != adv_output_fullscreen) {
error_set("Only fullscreen output is supported.\n");
return -1;
}
if (!os_internal_curses_get()) {
error_set("Unsupported without the curses library.\n");
return -1;
}
curses_state.size_x = COLS;
curses_state.size_y = LINES;
curses_state.active = 1;
return 0;
}
void curses_done(void)
{
assert(curses_is_active() && !curses_mode_is_active());
log_std(("video:curses: curses_done()\n"));
curses_state.active = 0;
}
adv_error curses_mode_set(const curses_video_mode* mode)
{
unsigned size;
unsigned i;
assert(curses_is_active() && !curses_mode_is_active());
scrollok(stdscr, FALSE);
curses_state.font_size_x = mode->font_size_x;
curses_state.font_size_y = mode->font_size_y;
size = curses_state.size_x * curses_state.size_y;
curses_state.ptr = malloc(size * 2);
for (i = 0; i < size; ++i) {
curses_state.ptr[i * 2] = ' ';
curses_state.ptr[i * 2 + 1] = 0;
}
curses_write_line = curses_linear_write_line;
curses_state.mode_active = 1;
return 0;
}
void curses_mode_done(adv_bool restore)
{
assert(curses_is_active() && curses_mode_is_active());
free(curses_state.ptr);
scrollok(stdscr, TRUE);
endwin();
(void)restore; /* ignored */
curses_state.mode_active = 0;
}
unsigned curses_virtual_x(void)
{
return curses_state.size_x * curses_state.font_size_x;
}
unsigned curses_virtual_y(void)
{
return curses_state.size_y * curses_state.font_size_y;
}
unsigned curses_bytes_per_scanline(void)
{
return curses_state.size_x * 2;
}
static int COLOR[8] = {
COLOR_BLACK,
COLOR_BLUE,
COLOR_GREEN,
COLOR_CYAN,
COLOR_RED,
COLOR_MAGENTA,
COLOR_YELLOW,
COLOR_WHITE
};
#define COLOR_MAX 128
struct color_pair {
unsigned dos_attr;
unsigned attr;
};
static struct color_pair color_map[COLOR_MAX];
static unsigned color_mac = 1; /* the first color is reserved */
static unsigned getattr(unsigned dos_attr)
{
unsigned i;
for (i = 1; i < color_mac && i < COLOR_MAX; ++i)
if (color_map[i].dos_attr == dos_attr)
return color_map[i].attr;
if (color_mac < COLOR_MAX && color_mac < (unsigned)COLOR_PAIRS) {
unsigned f = (dos_attr) & 0x7;
unsigned b = (dos_attr >> 4) & 0x7;
unsigned attr;
init_pair(color_mac, COLOR[f], COLOR[b]);
attr = COLOR_PAIR(color_mac);
if (dos_attr & 0x08)
attr |= A_BOLD;
if (dos_attr & 0x80)
attr |= A_BLINK;
color_map[color_mac].dos_attr = dos_attr;
color_map[color_mac].attr = attr;
++color_mac;
return attr;
} else
return COLOR_PAIR(0);
}
void curses_wait_vsync(void)
{
int x, y;
unsigned old_a;
unsigned old_m;
assert(curses_is_active() && curses_mode_is_active());
old_a = 0x100;
old_m = 0;
for (y = 0; y < curses_state.size_y; ++y) {
unsigned char* line;
move(y, 0);
line = curses_write_line(y);
for (x = 0; x < curses_state.size_x; ++x) {
unsigned a = line[2 * x + 1];
unsigned c = line[2 * x];
if (a != old_a) {
old_m = getattr(a);
old_a = a;
}
addch(c | old_m);
}
}
refresh();
}
#define DRIVER(mode) ((curses_video_mode*)(&mode->driver_mode))
adv_error curses_mode_import(adv_mode* mode, const curses_video_mode* curses_mode)
{
sncpy(mode->name, MODE_NAME_MAX, "curses");
*DRIVER(mode) = *curses_mode;
mode->driver = &video_curses_driver;
mode->flags = MODE_FLAGS_INDEX_TEXT |
(mode->flags & MODE_FLAGS_USER_MASK);
mode->size_x = curses_state.size_x * curses_mode->font_size_x;
mode->size_y = curses_state.size_y * curses_mode->font_size_y;
mode->vclock = 0;
mode->hclock = 0;
mode->scan = 0; /* assume singlescan */
return 0;
}
adv_error curses_mode_grab(curses_video_mode* mode)
{
assert(curses_is_active());
mode->font_size_x = 8;
mode->font_size_y = 16;
return 0;
}
adv_error curses_mode_generate(curses_video_mode* mode, const adv_crtc* crtc, unsigned flags)
{
assert(curses_is_active());
error_nolog_set("Mode generation not supported.\n");
/* always fail, no hardware control (only the grabbed mode is used) */
return -1;
}
unsigned curses_font_size_x(void)
{
return curses_state.font_size_x;
}
unsigned curses_font_size_y(void)
{
return curses_state.font_size_y;
}
#define COMPARE(a, b) \
if (a < b) \
return -1; \
if (a > b) \
return 1
int curses_mode_compare(const curses_video_mode* a, const curses_video_mode* b)
{
COMPARE(a->font_size_y, b->font_size_y);
COMPARE(a->font_size_x, b->font_size_x);
return 0;
}
void curses_default(void)
{
}
void curses_reg(adv_conf* context)
{
assert(!curses_is_active());
}
adv_error curses_load(adv_conf* context)
{
assert(!curses_is_active());
return 0;
}
adv_color_def curses_color_def(void)
{
return color_def_make(adv_color_type_text);
}
/***************************************************************************/
/* Driver */
static adv_error curses_mode_set_void(const void* mode)
{
return curses_mode_set((const curses_video_mode*)mode);
}
static adv_error curses_mode_import_void(adv_mode* mode, const void* curses_mode)
{
return curses_mode_import(mode, (const curses_video_mode*)curses_mode);
}
static adv_error curses_mode_generate_void(void* mode, const adv_crtc* crtc, unsigned flags)
{
return curses_mode_generate((curses_video_mode*)mode, crtc, flags);
}
static int curses_mode_compare_void(const void* a, const void* b)
{
return curses_mode_compare((const curses_video_mode*)a, (const curses_video_mode*)b);
}
static unsigned curses_mode_size(void)
{
return sizeof(curses_video_mode);
}
static adv_error curses_mode_grab_void(void* mode)
{
return curses_mode_grab((curses_video_mode*)mode);
}
adv_video_driver video_curses_driver = {
"curses",
DEVICE,
curses_load,
curses_reg,
curses_init,
curses_done,
curses_flags,
curses_mode_set_void,
0,
curses_mode_done,
curses_virtual_x,
curses_virtual_y,
curses_font_size_x,
curses_font_size_y,
curses_bytes_per_scanline,
0,
curses_color_def,
0,
0,
&curses_write_line,
curses_wait_vsync,
0,
0,
0,
curses_mode_size,
curses_mode_grab_void,
curses_mode_generate_void,
curses_mode_import_void,
curses_mode_compare_void,
0
};