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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
/*
* A terminal driver using the curses library
*
* $Header: /usr/build/vile/vile/RCS/curses.c,v 1.48 2010/02/24 23:51:30 tom Exp $
*/
#include "estruct.h"
#include "edef.h"
#if OPT_LOCALE
#include <locale.h>
#endif
#if DISP_CURSES
#undef WINDOW
#include "tcap.h"
#if OPT_LOCALE && defined(NCURSES_VERSION) && defined(HAVE_ADDNWSTR)
#define USE_MULTIBYTE 1
#else
#define USE_MULTIBYTE 0
#endif
#define is_default(color) (color < 0 || color == 255)
#if USE_TERMCAP
# define TCAPSLEN 1024
static char tc_parsed[TCAPSLEN];
#endif
#if OPT_COLOR
/* ANSI: black, red, green, yellow, blue, magenta, cyan, white */
static const char ANSI_palette[] =
{"0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"};
#ifdef HAVE_USE_DEFAULT_COLORS
#define DefaultColor(dft) -1
#define DEFAULT_FG -1
#define DEFAULT_BG -1
#else
#define DefaultColor(dft) dft
#define DEFAULT_FG COLOR_WHITE
#define DEFAULT_BG COLOR_BLACK
#endif
#define Num2Color(n,dft) ((n >= 0) ? ctrans[(n) & (ncolors-1)] : DefaultColor(dft))
#define Num2Fore(n) Num2Color(n, DEFAULT_FG)
#define Num2Back(n) Num2Color(n, DEFAULT_BG)
#endif /* OPT_COLOR */
#if SYS_OS2_EMX
#include "os2keys.h"
#endif
static ENC_CHOICES my_encoding = enc_DEFAULT;
static int i_am_xterm = 1;
static int i_was_closed = FALSE;
static int have_data = FALSE;
static int in_screen = FALSE;
static int can_color = FALSE;
#include "xtermkeys.h"
#if OPT_COLOR
static int used_bcolor = -999;
/*
* Compute a color pair number given foreground/background values.
*
* We use multiple foreground (text) colors on a given background color,
* which should leave enough room in the color-pairs array to allow for
* default colors as well.
*
* Color pair 0 in curses is special; it corresponds to default foreground
* and background colors.
*/
static int
compute_pair(int fg, int bg, int updateit)
{
int pair;
int map_fg = DEFAULT_FG;
int map_bg = DEFAULT_BG;
if (is_default(fg) && is_default(bg)) {
pair = 0;
fg = -1;
bg = -1;
} else if (is_default(fg)) {
pair = 1;
fg = -1;
map_bg = Num2Back(bg);
} else {
if (is_default(bg))
bg = -1;
else
map_bg = Num2Back(bg);
map_fg = Num2Fore(fg);
pair = 2 + fg;
}
if (updateit) {
TRACE(("init_pair %2d fg %2d, bg %2d -> %2d, %2d\n",
pair, fg, bg, map_fg, map_bg));
init_pair(pair, map_fg, map_bg);
}
return pair;
}
/*
* Initialize all color pairs when the palette is (re)initialized.
*/
static void
reinitialize_colors(void)
{
short fg;
TRACE((T_CALLED "reinitialize_colors\n"));
for (fg = -1; fg < ncolors; ++fg) {
(void) compute_pair(fg, gbcolor, TRUE);
}
used_bcolor = gbcolor;
returnVoid();
}
#endif /* OPT_COLOR */
static void
curs_set_encoding(ENC_CHOICES code)
{
/*
* Check if we're able to display UTF-8, and if not, decline attempts to
* use that in the display.
*
* ncurses differs slightly from X/Open (following the SVr4 pattern which
* appears to have been lost in committee) by allowing addch() to
* accumulate multibyte characters and display those. At the moment, this
* driver only uses addch(), rather than add_wch() or addnwstr(), which
* both would require us to accumulate and convert. So the ifdef checks
* for wide-character ncurses rather than just wide-character curses.
*/
#if !USE_MULTIBYTE
if (code > enc_LOCALE) {
code = enc_LOCALE;
}
#endif
my_encoding = code;
}
static ENC_CHOICES
curs_get_encoding(void)
{
return my_encoding;
}
static void
curs_initialize(void)
{
static int already_open = 0;
#if USE_TERMCAP
char tc_rawdata[4096];
char *p = tc_parsed;
#endif
unsigned i;
TRACE((T_CALLED "curs_open()\n"));
if (already_open) {
if (i_was_closed) {
i_was_closed = FALSE;
#if OPT_XTERM
if (i_am_xterm) {
xterm_open(0);
}
#endif /* OPT_XTERM */
#if OPT_TITLE
if (auto_set_title) {
term.set_title(tb_values(current_title));
}
#endif
}
returnVoid();
}
TRACE((T_CALLED "curs_initialize\n"));
#if OPT_LOCALE
if (okCTYPE2(vl_wide_enc)) {
TRACE(("setting locale to %s\n", vl_wide_enc.locale));
setlocale(LC_CTYPE, vl_wide_enc.locale);
} else {
TRACE(("setting locale to %s\n", vl_real_enc.locale));
setlocale(LC_CTYPE, vl_real_enc.locale);
}
#endif
initscr();
raw();
noecho();
nonl();
nodelay(stdscr, TRUE);
idlok(stdscr, TRUE);
/*
* Note: we do not set the locale to vl_real_enc since that would confuse
* curses when it adds multibyte characters to the display.
*/
#if USE_TERMCAP
if ((tgetent(tc_rawdata, getenv("TERM"))) != 1) {
fprintf(stderr, "Unknown terminal type %s!\n", getenv("TERM"));
ExitProgram(BADEXIT);
}
TRACE(("tc_rawdata used %d of %d\n", strlen(tc_rawdata), sizeof(tc_rawdata)));
#endif
#if OPT_XTERM
{
char *t = getenv("TERM");
I_AM_XTERM(t);
if (i_am_xterm) {
xterm_open(&term);
}
}
#endif
term.maxrows = term.rows = LINES;
term.maxcols = term.cols = COLS;
#if OPT_COLOR
if (has_colors()) {
can_color = TRUE;
start_color();
#ifdef HAVE_USE_DEFAULT_COLORS
use_default_colors();
#endif
set_colors(COLORS);
} else {
set_colors(2); /* white/black */
}
#endif
#if OPT_COLOR
set_palette(ANSI_palette);
#endif
/*
* Read the termcap data now so tcap_init_fkeys() does not depend on the
* state of tgetstr() vs the buffer.
*/
for (i = 0; i < TABLESIZE(keyseqs); ++i) {
keyseqs[i].result = TGETSTR(keyseqs[i].capname, &p);
#if USE_TERMINFO
if (NO_CAP(keyseqs[i].result))
keyseqs[i].result = 0;
#endif
}
tcap_init_fkeys();
ttopen();
already_open = TRUE;
returnVoid();
}
static void
curs_open(void)
{
TRACE(("curs_open\n"));
vl_save_tty();
#if OPT_LOCALE
vl_open_mbterm();
#endif
}
static void
curs_close(void)
{
TRACE(("curs_close\n"));
/* see tcap.c */
term.curmove(term.rows - 1, 0); /* cf: dumbterm.c */
term.eeol();
term.set_title(getenv("TERM"));
vl_restore_tty();
#if OPT_XTERM
if (i_am_xterm) {
xterm_close();
}
#endif
#if OPT_LOCALE
vl_close_mbterm();
#endif
i_was_closed = TRUE;
}
static int last_key = -1;
static int
curs_typahead(void)
{
int result = FALSE;
if (in_screen) {
if (last_key == -1) {
nodelay(stdscr, TRUE);
last_key = getch();
}
result = (last_key >= 0);
}
return result;
}
static int
curs_getc(void)
{
int result = last_key;
if (!in_screen) {
fflush(stdout);
result = getchar();
} else if (result == -1) {
#ifdef VAL_AUTOCOLOR
int acmilli = global_b_val(VAL_AUTOCOLOR);
if (acmilli != 0) {
timeout(acmilli);
for_ever {
result = getch();
if (result < 0) {
autocolor();
} else {
break;
}
}
} else {
nodelay(stdscr, FALSE);
result = getch();
}
#else
nodelay(stdscr, FALSE);
result = getch();
#endif
}
last_key = -1;
TRACE(("curs_getc:%d%s\n", result, in_screen ? "" : "*"));
return result;
}
static OUTC_DCL
curs_putc(int c)
{
c &= (N_chars - 1);
if (in_screen) {
have_data = TRUE;
OUTC_RET addch((UINT) (c));
} else {
OUTC_RET putchar(c);
}
}
static void
curs_flush(void)
{
if (in_screen && have_data) {
refresh();
TRACE(("curs_flush\n"));
have_data = FALSE;
}
}
static void
curs_kopen(void)
{
static int initialized = FALSE;
TRACE((T_CALLED "curs_kopen\n"));
term.mopen();
if (!initialized) {
initialized = TRUE;
curs_initialize();
}
in_screen = TRUE;
curs_flush();
returnVoid();
}
static void
curs_kclose(void)
{
TRACE((T_CALLED "curs_kclose\n"));
endwin();
term.mclose();
in_screen = FALSE;
have_data = FALSE;
returnVoid();
}
static void
curs_move(register int row, register int col)
{
move(row, col);
}
static void
curs_eeol(void)
{
clrtoeol();
}
static void
curs_eeop(void)
{
if (in_screen) {
if (sgarbf) {
erase();
wrefresh(curscr);
} else {
clrtobot();
}
}
}
/* move howmany lines starting at 'from' to 'to' */
static void
curs_scroll(int from, int to, int howmany)
{
scrollok(stdscr, TRUE);
if (from > to) {
setscrreg(to, from + howmany - 1);
} else if (from < to) {
setscrreg(from, to + howmany - 1);
}
scrl(from - to);
scrollok(stdscr, FALSE);
}
#if OPT_COLOR
static void
set_bkgd_colors(int fg, int bg)
{
if (used_bcolor != gbcolor) {
TRACE(("...bcolor changed from %d to %d\n", used_bcolor, gbcolor));
reinitialize_colors();
}
bkgdset((chtype) (COLOR_PAIR(compute_pair(fg, bg, TRUE)) | ' '));
}
static void
curs_fcol(int color)
{
short fg, bg;
short pair = (short) PAIR_NUMBER(getattrs(stdscr));
pair_content(pair, &fg, &bg);
set_bkgd_colors(color, bg);
}
static void
curs_bcol(int color)
{
short fg, bg;
short pair = (short) PAIR_NUMBER(getattrs(stdscr));
pair_content(pair, &fg, &bg);
set_bkgd_colors(fg, color);
}
static void
curs_spal(const char *thePalette)
{ /* reset the palette registers */
if (set_ctrans(thePalette)) {
TRACE(("palette changed\n"));
reinitialize_colors();
}
}
#endif /* OPT_COLOR */
#if OPT_VIDEO_ATTRS
static void
curs_attr(UINT attr)
{
chtype result = A_NORMAL;
if (attr & VABOLD)
result |= A_BOLD;
if (attr & VAUL)
result |= A_UNDERLINE;
if (attr & (VAREV | VASEL))
result |= A_REVERSE;
#if OPT_COLOR
if (can_color) {
int fg;
if (attr & VACOLOR) {
fg = (VCOLORNUM(attr) % ncolors);
result |= COLOR_PAIR(compute_pair(fg, gbcolor, FALSE));
}
}
#endif
attrset(result);
}
#else /* highlighting is a minimum attribute */
static void
curs_rev( /* change reverse video status */
UINT state)
{ /* FALSE = normal video, TRUE = reverse video */
if (state) {
standout();
} else {
standend();
}
}
#endif /* OPT_VIDEO_ATTRS */
/*
* Hide/show cursor. We do this in levels, so we can do the "right" thing with
* multimotion.
*/
static void
curs_cursor(int flag)
{
static int level;
if (flag) {
if (!++level) {
TRACE(("CURSOR ON\n"));
curs_set(1);
}
} else {
if (!level--) {
TRACE(("CURSOR OFF\n"));
curs_set(0);
}
}
}
static void
curs_beep(void)
{
#if OPT_FLASH
if (global_g_val(GMDFLASH))
flash();
else
#endif
beep();
}
TERM term =
{
0, /* the first four values are set dynamically */
0,
0,
0,
curs_set_encoding,
curs_get_encoding,
curs_open,
curs_close,
curs_kopen,
curs_kclose,
ttclean,
ttunclean,
nullterm_openup,
curs_getc,
curs_putc,
curs_typahead,
curs_flush,
curs_move,
curs_eeol,
curs_eeop,
curs_beep,
#if OPT_VIDEO_ATTRS
curs_attr,
#else
curs_rev,
#endif
nullterm_setdescrip,
#if OPT_COLOR
curs_fcol,
curs_bcol,
curs_spal,
#else
nullterm_setfore,
nullterm_setback,
nullterm_setpal,
#endif
nullterm_setccol,
curs_scroll,
nullterm_pflush,
nullterm_icursor,
nullterm_settitle,
ttwatchfd,
ttunwatchfd,
curs_cursor,
nullterm_mopen,
nullterm_mclose,
nullterm_mevent,
};
#endif /* DISP_CURSES */
|