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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
|
/*
* Module curses (Hopefully) machine-independant curses library.
* Author larry gensch, ESQ
*
* Note: The inspiration for this module was an article by
* Allen I. Holub in Dr. Dobbs Journal C Chest column.
* Extensive modifications were made by larry gensch
* to make the module conform to System V curses.
*
* Copyright (c) 1987, 1988, 1989
* by Larry Gensch / 104 Lowell Road / Salem, NH 03079
*
* This code may be included in any work, public or private, with the
* exception of creating a commercial curses-compatible subroutine
* library. (In other words, use the code all you want, but please don't
* rip off the author by reselling this code as your own).
*
* If you make modifications to this code (specifically, enhancements that
* are compatible with System V.x curses), please send them to larry gensch at
* the address above to be considered for inclusion in subsequent releases.
* Any machine specific implementation modules (similar to v-msdos.c) for
* other machines are welcomed.
*/
#include "curses.h"
#include <ctype.h>
#include <stdlib.h>
/****
* There is no vsscanf function in ANSI standard, so it can't be relied
* on. Define VSSCANF to the appropriate function name if it is supplied
* with the compiler. If VSSCANF is not defined, the scanw family of calls
* will not be included in compilation.
****/
#ifdef THINK_C
# define VSSCANF _vsscanf
#endif
#ifdef __WATCOMC__
# define VSSCANF vsscanf
#endif
/*
* EXPORT is used to show entry points into this module. Simply grep
* using the pattern '^EXPORT' to list them out.
*/
#ifndef EXPORT
#define EXPORT
#endif
/*
* Exported variables (accessible by user programs):
*/
static char prntw_buf[512];
EXPORT int COLS = 0;
EXPORT int LINES = 0;
EXPORT WINDOW* stdscr;
EXPORT char *_curses_prntw = prntw_buf;
EXPORT unsigned _curses_prntw_size = sizeof(prntw_buf);
EXPORT int _curses_tab_wid = 8;
static bool Echo = TRUE; /* echo()/noecho() state */
static bool Cbreak = FALSE; /* cbreak()/nocbreak() state */
static bool Nl = TRUE; /* nl()/nonl() state */
static bool Endwin = TRUE; /* isendwin() state */
static bool Raw = FALSE; /* raw()/noraw() state */
static chtype Kbbuf[256]; /* Keyboard input buffer */
static chtype *Kbptr = Kbbuf; /* Buffer pointer */
static int Kbcount = 0; /* Nbr characters in buffer */
static chtype ungot_char = -1; /* Character pushed back by ungetch. */
/****
* The following variables are used to communicate between waddch
* and get_input (wgetch) when echo is on:
****/
static int waddch_scroll_cnt; /* Number of lines window was scrolled up in get_input call. */
static int waddch_end_of_window_flag; /* Set to TRUE if end of window reached and cannot scroll. */
static int waddch_allow_backspace_up; /* Set to TRUE to allow backspace character to go up a line. */
/*
* initscr() - initialize the screen and curses internal variables.
*
* Returns a pointer to stdscr if successful, otherwise prints a message
* to stderr and exits.
*/
EXPORT WINDOW *initscr (void)
{
if (! Endwin)
return NULL;
if (! CURS_INIT_FN()) {
#ifdef THINK_C
maccur_alert_msg("curses: error in terminal initialization.");
#else
fputs("curses: error in terminal initialization.\n", stderr);
#endif
exit(1);
}
if (LINES == 0)
LINES = 24;
if (COLS == 0)
COLS = 80;
stdscr = newwin(0,0,0,0);
if (stdscr == NULL) {
#ifdef THINK_C
maccur_alert_msg("curses: cannot allocate stdscr window.");
#else
fputs("curses: cannot allocate stdscr window.\n", stderr);
#endif
abort();
}
Endwin = FALSE;
return stdscr;
}
/*
* endwin() - reset terminal into previous state. Required before any call to
* system() or exit().
*/
EXPORT int endwin (void)
{
if (stdscr == NULL)
return ERR;
Endwin = TRUE;
return CURS_END_FN();
}
/*
* isendwin() - determine if endwin() has been called without any subsequent
* calls to wrefresh().
*/
EXPORT bool isendwin (void)
{
return Endwin;
}
/* -------------------------------------------- */
/* Window manipulation */
/* -------------------------------------------- */
/*
* wrefresh() - Write window to terminal. Returns ERR if an error occurs.
*/
EXPORT int wrefresh (WINDOW *win)
{
if (win != NULL && (win->_flags & _ISPAD) == 0) {
Endwin = FALSE;
if (win->_flags & (_WINCHANGED | _WINMOVED)) {
win->_flags &= ~(_WINCHANGED | _WINMOVED);
return CURS_REFRESH_FN(win, TRUE);
}
return OK;
}
return ERR;
}
/*
* wnoutrefresh() - Update virtual terminal image (physical image updated by
* doupdate()).
*/
EXPORT int wnoutrefresh (WINDOW *win)
{
if (win != NULL && (win->_flags & _ISPAD) == 0) {
if (win->_flags & (_WINCHANGED | _WINMOVED))
{
win->_flags &= ~(_WINCHANGED | _WINMOVED);
return CURS_REFRESH_FN(win, FALSE);
}
return OK;
}
return ERR;
}
/*
* pad_map: Used by prefresh/pnoutrefresh to determine what part of
* pad to display. Returns ERR on bad parameters. Sets _WINMOVED
* flag if pad is not mapped to same location as previous call.
*/
static int pad_map (WINDOW *pad, int pminy, int pminx,
int sminy, int sminx, int smaxy, int smaxx)
{
int pmaxx, pmaxy;
pminy = MAX(pminy, 0);
pminx = MAX(pminx, 0);
sminy = MAX(sminy, 0);
sminx = MAX(sminx, 0);
if (sminx >= smaxx && sminy >= smaxy) return ERR;
pmaxx = pminx + (smaxx - sminx + 1);
pmaxy = pminy + (smaxy - sminy + 1);
pmaxx = MIN(pmaxx, pad->_maxx);
pmaxy = MIN(pmaxy, pad->_maxy);
if (sminy != pad->_begy || sminx != pad->_begx ||
pminy != pad->_pmap_orgy || pminx != pad->_pmap_orgx ||
pmaxy != pad->_pmap_maxy || pmaxx != pad->_pmap_maxx)
{
pad->_flags |= _WINMOVED;
pad->_begy = sminy;
pad->_begx = sminx;
pad->_pmap_orgy = pminy;
pad->_pmap_orgx = pminx;
pad->_pmap_maxy = pmaxy;
pad->_pmap_maxx = pmaxx;
}
return OK;
}
/*
* prefresh() - Map and write pad to terminal. Returns ERR if an error occurs.
*/
EXPORT int prefresh (WINDOW *pad, int pminy, int pminx,
int sminy, int sminx, int smaxy, int smaxx)
{
if (pad != NULL && (pad->_flags & _ISPAD))
{
Endwin = FALSE;
if (pad_map(pad, pminy, pminx, sminy, sminx,
smaxy, smaxx) == ERR)
return ERR;
if (pad->_flags & (_WINCHANGED | _WINMOVED))
{
pad->_flags &= ~(_WINCHANGED | _WINMOVED);
return CURS_REFRESH_FN(pad, TRUE);
}
return OK;
}
return ERR;
}
/*
* pnoutrefresh() - Update virtual terminal image (physical image updated by
* doupdate()).
*/
EXPORT int pnoutrefresh (WINDOW *pad, int pminy, int pminx,
int sminy, int sminx, int smaxy, int smaxx)
{
if (pad != NULL && (pad->_flags & _ISPAD))
{
if (pad_map(pad, pminy, pminx, sminy, sminx,
smaxy, smaxx) == ERR)
return ERR;
if (pad->_flags & (_WINCHANGED | _WINMOVED))
{
pad->_flags &= ~(_WINCHANGED | _WINMOVED);
return CURS_REFRESH_FN(pad, FALSE);
}
return OK;
}
return ERR;
}
/*
* doupdate() - Update the physical terminal image.
*/
EXPORT int doupdate (void)
{
Endwin = FALSE;
return CURS_REFRESH_FN(NULL, TRUE);
}
/*
* window_init() - Initialize window structure.
*/
static void window_init (WINDOW *win, int nlines, int ncols, int beg_y, int beg_x)
{
win->_cury = 0;
win->_curx = 0;
win->_maxy = nlines;
win->_maxx = ncols;
win->_xdim = ncols;
win->_begy = beg_y;
win->_begx = beg_x;
win->_flags = _WINCHANGED;
win->_attrs = 0;
win->_clear = FALSE;
win->_leave = FALSE;
win->_scroll = FALSE;
win->_use_idl = FALSE;
win->_use_keypad = FALSE;
win->_use_meta = FALSE;
win->_nodelay = FALSE;
win->_firstch = NULL;
win->_lastch = NULL;
win->_notimeout = FALSE;
win->_need_idl = FALSE;
win->_tmarg = 0;
win->_bmarg = nlines;
}
/*
* newwin() - Create and return a pointer to a new window.
*/
EXPORT WINDOW* newwin (int nlines, int ncols, int beg_y, int beg_x)
{
WINDOW *win;
chtype *screen;
int i;
if (nlines > LINES || ncols > COLS)
return NULL;
if (nlines < 1)
nlines = LINES;
if (ncols < 1)
ncols = COLS;
if ( (beg_y < 0 || beg_y >= LINES) || /* RZ: Was >= on x/y + beg compare to lines/cols */
(beg_x < 0 || beg_x >= COLS) ||
(nlines + beg_y > LINES) ||
(ncols + beg_x > COLS) )
return NULL;
if ((win = calloc(1, sizeof(WINDOW))) == NULL)
return NULL;
if ((screen = calloc(nlines * ncols, sizeof(chtype))) == NULL) {
free(win);
return NULL;
}
window_init(win, nlines, ncols, beg_y, beg_x);
win->_y = screen;
for (i = 0; i < nlines * ncols; i++, screen++)
*screen = ' ';
return win;
}
/*
* subwin() - Create and return a pointer to a new window.
*/
EXPORT WINDOW* subwin (WINDOW *parnt_win, int nlines, int ncols, int beg_y, int beg_x)
{
WINDOW *win;
if (parnt_win == NULL)
return NULL;
if (nlines < 1 || ncols < 1)
return NULL;
if ( (beg_y < parnt_win->_begy) ||
(beg_x < parnt_win->_begx) ||
(nlines + beg_y > parnt_win->_maxy + parnt_win->_begy) ||
(ncols + beg_x > parnt_win->_maxx + parnt_win->_begx) )
return NULL;
if ((win = calloc(1, sizeof(WINDOW))) == NULL)
return NULL;
window_init(win, nlines, ncols, beg_y, beg_x);
win->_flags |= _SUBWIN;
win->_y = parnt_win->_y + ((beg_y - parnt_win->_begy) * parnt_win->_xdim +
beg_x - parnt_win->_begx);
win->_xdim = parnt_win->_maxx;
return win;
}
/*
* newpad() - Create and return a pointer to a new pad.
*/
EXPORT WINDOW* newpad (int nlines, int ncols)
{
WINDOW *pad;
chtype *screen;
int i;
if ( nlines < 1 || ncols < 1 )
return NULL;
if ((pad = calloc(1, sizeof(WINDOW))) == NULL)
return NULL;
if ((screen = calloc(nlines * ncols, sizeof(chtype))) == NULL) {
free(pad);
return NULL;
}
window_init(pad, nlines, ncols, 0, 0);
pad->_pmap_orgy = 0;
pad->_pmap_orgx = 0;
pad->_pmap_maxy = LINES;
pad->_pmap_maxx = COLS;
pad->_flags |= _ISPAD;
pad->_y = screen;
for (i = 0; i < nlines * ncols; i++, screen++)
*screen = ' ';
return pad;
}
/*
* subpad() - Create and return a pointer to a pad inside another pad.
*/
EXPORT WINDOW* subpad (WINDOW *parnt_pad, int nlines, int ncols, int beg_y, int beg_x)
{
WINDOW *pad;
if (parnt_pad == NULL || (parnt_pad->_flags & _ISPAD) == 0)
return NULL;
if (nlines < 1 || ncols < 1)
return NULL;
if ( (beg_y < 0) ||
(beg_x < 0) ||
(nlines + beg_y > parnt_pad->_maxy) ||
(ncols + beg_x > parnt_pad->_maxx) )
return NULL;
if ((pad = calloc(1, sizeof(WINDOW))) == NULL)
return NULL;
window_init(pad, nlines, ncols, beg_y, beg_x);
pad->_pmap_orgy = parnt_pad->_pmap_orgy;
pad->_pmap_orgx = parnt_pad->_pmap_orgx;
pad->_pmap_maxy = parnt_pad->_pmap_maxy;
pad->_pmap_maxx = parnt_pad->_pmap_maxx;
pad->_flags |= (_SUBWIN | _ISPAD);
pad->_y = parnt_pad->_y + (beg_y * parnt_pad->_xdim + beg_x);
pad->_xdim = parnt_pad->_maxx;
return pad;
}
/*
* mvwin() - move window so that the upper left corner will be at the specified
* position.
*/
EXPORT int mvwin (WINDOW *win, int y, int x)
{
if (win == NULL || (win->_flags & _ISPAD))
return ERR;
if ( (x + win->_maxx > COLS) ||
(y + win->_maxy > LINES) ) /* RZ -- Check was >=, eliminated OK mappings. */
return ERR;
win->_begx = x;
win->_begy = y;
win->_flags |= _WINMOVED;
return OK;
}
/*
* delwin() - delete the named window, freeing all memory associated with it.
*/
EXPORT int delwin (WINDOW *win)
{
if (win == NULL)
return ERR;
free(win->_y);
free(win);
return OK;
}
/*
* waddch() - Output the specified character to the specified window.
*
* Note: The "unctrl" routine in the default: case below is
* specific to the ASCII character set.
*
*/
EXPORT int waddch (WINDOW *win, chtype ch)
{
int rval = OK;
chtype c, attr;
if (win == NULL)
return ERR;
win->_flags |= _WINCHANGED;
waddch_scroll_cnt = 0;
waddch_end_of_window_flag = FALSE;
c = (ch & A_CHARTEXT);
attr = (ch & A_ATTRIBUTES);
switch (c | (ch & A_ALTCHARSET))
{
case '\b':
if (win->_curx > 0 || (waddch_allow_backspace_up && win->_cury > 0))
{
win->_y[win->_cury*win->_xdim + --win->_curx] = ' ' | win->_attrs;
if (win->_curx < 0)
{
win->_cury--;
win->_curx = win->_maxx - 1;
}
}
else rval = ERR;
break;
case '\t':
do {
waddch(win, ' ' | attr);
} while (win->_curx % _curses_tab_wid &&
win->_curx > 0 && !waddch_end_of_window_flag); /* Don't let tab spill into new line. */
break;
case '\r':
win->_curx = 0;
break;
default:
if ( ( (ch & A_ALTCHARSET) == 0) &&
iscntrl(c))
{
waddch(win, '^' | attr);
ch = ((c == 0x7f) ? '?' : c + 'A' - 1) | attr;
}
win->_y[win->_cury*win->_xdim+win->_curx] = ch | win->_attrs;
if (++(win->_curx) < win->_maxx)
break;
/* fall through to newline */
case '\n':
if (ch == '\n')
wclrtoeol(win);
win->_curx = 0;
win->_cury++;
if (win->_scroll && win->_cury >= win->_bmarg )
{
if ((rval = scroll(win)) != ERR)
{
win->_cury = win->_bmarg - 1;
waddch_scroll_cnt++;
}
}
break;
}
if (win->_cury >= win->_maxy)
{
win->_cury = win->_maxy - 1;
win->_curx = win->_maxx - 1;
waddch_end_of_window_flag = TRUE;
rval = ERR;
}
win->_flags |= _WINCHANGED;
waddch_allow_backspace_up = FALSE;
return rval;
}
/*
* wechochar() - waddch() followed by refresh()
*/
EXPORT int wechochar (WINDOW *win, chtype ch)
{
waddch(win,ch);
return wrefresh(win);
}
/*
* pechochar() - waddch() followed by prefresh()
*/
EXPORT int pechochar (WINDOW *pad, chtype ch);
EXPORT int pechochar (WINDOW *pad, chtype ch)
{
waddch(pad,ch);
return prefresh(pad, pad->_pmap_orgy, pad->_pmap_orgx,
pad->_begy, pad->_begx,
pad->_begy + pad->_pmap_maxy - pad->_pmap_orgy - 1,
pad->_begx + pad->_pmap_maxx - pad->_pmap_orgx - 1);
}
/*
* waddstr() - Write the characters in the string to the window.
*/
EXPORT int waddstr (WINDOW *win, char *str)
{
int rval = OK;
while (*str)
if (waddch(win, (chtype) *(str++)) == ERR)
rval = ERR;
return rval;
}
/*
* wattroff() - Turn off the specified attributes in the window.
*/
EXPORT int wattroff (WINDOW *win, chtype attrs)
{
if (win == NULL)
return ERR;
win->_attrs &= ~(attrs & A_ATTRIBUTES);
return OK;
}
/*
* wattron() - Turn on the specified attributes in the window.
*/
EXPORT int wattron (WINDOW *win, chtype attrs)
{
if (win == NULL)
return ERR;
win->_attrs |= (attrs & A_ATTRIBUTES);
return OK;
}
/*
* wattrset() - Set the specified attributes in the window.
*/
EXPORT int wattrset (WINDOW *win, chtype attrs)
{
if (win == NULL)
return ERR;
win->_attrs = (attrs & A_ATTRIBUTES);
return OK;
}
/*
* wstandout() - Turn on standout mode.
*/
EXPORT int wstandout (WINDOW *win)
{
return wattron(win, A_STANDOUT);
}
/*
* wstandend() - Turn off all attributes.
*/
EXPORT int wstandend (WINDOW *win)
{
return wattrset(win, A_NORMAL);
}
/*
* beep() - Sound the audible alarm()
*/
EXPORT int beep (void)
{
CURS_BEEP_FN(FALSE);
return OK;
}
/*
* flash() - Flash the screen (alternately, sound the alarm)
*/
EXPORT int alarm (void)
{
CURS_BEEP_FN(TRUE);
return OK;
}
/*
* box() - Draw a box around the edge of the window.
*/
EXPORT int box (WINDOW *win, chtype vertch, chtype horch)
{
chtype ul, ur, ll, lr;
int i;
int ox, oy;
bool os;
if (win == NULL)
return ERR;
if (horch == '\b' || vertch == '\b') /* These could cause infinite loop. */
return ERR;
if (vertch == 0)
vertch = ACS_VLINE;
if (horch == 0)
horch = ACS_HLINE;
if (vertch == ACS_VLINE && horch == ACS_HLINE) {
ul = ACS_ULCORNER;
ur = ACS_URCORNER;
ll = ACS_LLCORNER;
lr = ACS_LRCORNER;
}
else
ul = ur = ll = lr = horch;
ox = win->_cury;
oy = win->_curx;
os = win->_scroll;
wmove(win, 0, 0);
waddch(win, ul);
win->_scroll = FALSE;
for (i = 2; i < win->_maxx; i++)
waddch(win, horch);
waddch(win, ur);
for (i = 1; i < win->_maxy - 1; i++) {
wmove(win, i, 0);
waddch(win, vertch);
wmove(win, i, win->_maxx-1);
waddch(win, vertch);
}
mvwaddch(win, win->_maxy - 1, 0, ll);
for (i = 2; i < win->_maxx; i++)
waddch(win, horch);
waddch(win, lr);
win->_cury = ox;
win->_curx = oy;
win->_scroll = os;
win->_flags |= _WINCHANGED;
return OK;
}
/*
* werase() - Erase a window.
*/
EXPORT int werase (WINDOW *win)
{
int i, i_rw;
chtype ch;
chtype *screen, *dst_ptr;
if (win == NULL)
return ERR;
ch = ' ';
screen = win->_y;
i = win->_maxx * win->_maxy;
for (i_rw = 0; i_rw < win->_maxy; i_rw++) /* Have to do row-by-row because of possible subwin/pad. */
{
dst_ptr = screen + i_rw * win->_xdim;
for (i = 0; i < win->_maxx; i++)
*(dst_ptr + i) = ch;
}
win->_flags |= _WINCHANGED;
return OK;
}
/*
* wclear() - Clear the window (functionally identical to erase())
*/
EXPORT int wclear (WINDOW *win)
{
return werase(win);
}
/*
* wclrtobot() - Clear from cursor position to the bottom of the window.
*/
EXPORT int wclrtobot (WINDOW *win)
{
int i, i_rw;
int max;
chtype *dst_ptr;
chtype ch;
if (win == NULL)
return ERR;
win->_flags |= _WINCHANGED;
ch = ' ';
for (i_rw = win->_cury; i_rw < win->_maxy; i_rw++) /* Have to do row-by-row because of possible subwin/pad. */
{
dst_ptr = win->_y + i_rw * win->_xdim;
for (i = (i_rw == win->_cury ? win->_curx : 0); i < win->_maxx; i++)
*(dst_ptr + i) = ch;
}
return OK;
}
/*
* wclrtoeol() - Clear from cursor position to end of line in window.
*/
EXPORT int wclrtoeol (WINDOW *win)
{
int i;
chtype *screen;
chtype ch;
if (win == NULL)
return ERR;
if (win->_cury >= win->_maxy)
return ERR;
win->_flags |= _WINCHANGED;
screen = win->_y + (win->_cury * win->_xdim);
ch = ' ';
for (i = win->_curx; i < win->_maxx; i++)
*(screen + i) = ch;
return OK;
}
/*
* delch() - Delete character under cursor, moving all characters to end of
* line in the window to the right one character.
*/
void *memmove(void *s1, const void *s2, size_t n);
EXPORT int wdelch (WINDOW *win)
{
chtype *dst_ptr;
int n;
if (win == NULL)
return ERR;
win->_flags |= _WINCHANGED;
dst_ptr = win->_y + (win->_cury * win->_xdim + win->_curx);
n = win->_maxx - win->_curx - 1;
memmove(dst_ptr, dst_ptr + 1, n * sizeof(chtype));
*(dst_ptr + n) = ' ' | win->_attrs;
return OK;
}
/*
* wdeleteln() - Delete the line containing the cursor, moving all subsequent
* lines in the window up one line.
*/
void *memcpy(void *s1, const void *s2, size_t n);
EXPORT int wdeleteln (WINDOW *win)
{
int i, i_rw;
chtype *dst_ptr;
chtype ch = 0;
if (win == NULL)
return ERR;
win->_flags |= _WINCHANGED;
for (i_rw = win->_cury; i_rw < win->_maxy - 1; i_rw++)
{
dst_ptr = win->_y + (i_rw * win->_xdim);
memcpy(dst_ptr, dst_ptr + win->_xdim, win->_maxx * sizeof(chtype));
}
dst_ptr = win->_y + ((win->_maxy - 1) * win->_xdim);
for (i = 0; i < win->_maxx; i++)
*(dst_ptr + i) = ch;
return OK;
}
/*
* winsch() - Insert a character at the cursor position in the window, moving
* all subsequent characters (to end of line) to the right one character.
*/
EXPORT int winsch (WINDOW *win, chtype ch)
{
chtype *src_ptr;
if (win == NULL)
return ERR;
win->_flags |= _WINCHANGED;
src_ptr = win->_y + (win->_cury * win->_xdim + win->_curx);
memmove(src_ptr + 1, src_ptr, (win->_maxx - win->_curx - 1) * sizeof(chtype));
*src_ptr = ch | win->_attrs;
return OK;
}
/*
* winsertln() - Insert a blank line above current line. (had a bug)
*/
EXPORT int winsertln (WINDOW *win)
{
int i, i_rw;
chtype *dst_ptr;
chtype ch;
if (win == NULL)
return ERR;
win->_flags |= _WINCHANGED;
ch = ' ';
for (i_rw = win->_cury + 1; i_rw < win->_maxy; i_rw++)
{
dst_ptr = win->_y + (i_rw * win->_xdim);
memcpy(dst_ptr, dst_ptr - win->_xdim, win->_maxx * sizeof(chtype));
}
dst_ptr = win->_y + (win->_cury * win->_xdim);
for (i = 0; i < win->_maxx; i++)
*(dst_ptr + i) = ch;
return OK;
}
/*
* printw() - Print a printf()-formatted string onto stdscr.
*/
EXPORT int printw (char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsprintf(_curses_prntw, fmt, args);
va_end(args);
return addstr(_curses_prntw);
}
/*
* mvprintw() - Print a printf()-formatted string onto stdscr after positioning.
*/
EXPORT int mvprintw (int y, int x, char *fmt, ...)
{
va_list args;
move(y, x);
va_start(args, fmt);
vsprintf(_curses_prntw, fmt, args);
va_end(args);
return addstr(_curses_prntw);
}
/*
* wprintw() - Print a printf()-formatted string onto a window.
*/
EXPORT int wprintw (WINDOW *win, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsprintf(_curses_prntw, fmt, args);
va_end(args);
return waddstr(win, _curses_prntw);
}
/*
* mvwprintw() - Print a printf()-formatted string onto a window after positioning.
*/
EXPORT int mvwprintw (WINDOW *win, int y, int x, char *fmt, ...)
{
va_list args;
wmove(win, y, x);
va_start(args, fmt);
vsprintf(_curses_prntw, fmt, args);
va_end(args);
return waddstr(win, _curses_prntw);
}
/*
* vwprintw() - Print a printf()-formatted string onto a window using a
* variable argument list.
*/
EXPORT int vwprintw (WINDOW *win, char *fmt, va_list ap)
{
va_list args;
vsprintf(_curses_prntw, fmt, ap);
return waddstr(win, _curses_prntw);
}
/*
* scroll() - Scroll a window up one line.
* This is the same as wdeleteln on the top scrolling char.
*/
EXPORT int scroll (WINDOW *win)
{
int i, i_rw;
chtype *dst_ptr;
chtype ch;
if (win == NULL)
return ERR;
if (!win->_scroll)
return ERR;
win->_flags |= _WINCHANGED;
ch = ' ' | win->_attrs;
for (i_rw = win->_tmarg; i_rw < win->_bmarg - 1; i_rw++)
{
dst_ptr = win->_y + (i_rw * win->_xdim);
memcpy(dst_ptr, dst_ptr + win->_xdim, win->_maxx * sizeof(chtype));
}
dst_ptr = win->_y + ((win->_bmarg - 1) * win->_xdim);
for (i = 0; i < win->_maxx; i++)
*(dst_ptr + i) = ch;
return OK;
}
/*
* touchwin() - Pretend that the window has been modified since last refresh().
*/
EXPORT int touchwin (WINDOW *win)
{
if (win == NULL)
return ERR;
win->_flags |= _WINCHANGED;
return OK;
}
/*
* touchline() - Functionally identical to touchwin()
*/
EXPORT int touchline (WINDOW *win, int start, int count)
{
return touchwin(win);
}
/*
* wmove() - Set the cursor position within a window.
*/
EXPORT int wmove (WINDOW *win, int ypos, int xpos)
{
if (win == NULL)
return ERR;
if ( (xpos < 0 || xpos >= win->_maxx) ||
(ypos < 0 || ypos >= win->_maxy) )
return ERR;
win->_curx = xpos;
win->_cury = ypos;
return OK;
}
/*
* copywin() - Move text from one window to another.
*/
EXPORT int copywin (WINDOW *src_win, WINDOW *dst_win,
int sminy, int sminx, int dminy, int dminx,
int dmaxy, int dmaxx, bool overlay_flag)
{
int nrows, nchars, i, j;
chtype *src_ptr, *dst_ptr;
sminy = MAX(sminy, 0);
sminx = MAX(sminx, 0);
dminy = MAX(dminy, 0);
dminx = MAX(dminx, 0);
if (sminy >= src_win->_maxy || sminx >= src_win->_maxx ||
dminy >= dst_win->_maxy || dminx >= dst_win->_maxx ||
dminy >= dmaxy || dminx >= dmaxx)
return ERR;
dst_win->_flags |= _WINCHANGED;
nrows = MIN(dmaxy + 1, dst_win->_maxy) - dminy;
nrows = MIN(nrows, src_win->_maxy - sminy);
nchars = MIN(dmaxx + 1, dst_win->_maxx) - dminx;
nchars = MIN(nchars, src_win->_maxx - sminx);
src_ptr = src_win->_y + (sminy * src_win->_xdim) + sminx;
dst_ptr = dst_win->_y + (dminy * dst_win->_xdim) + dminx;
for (i = 0; i < nrows; i++)
{
if (overlay_flag)
{
for (j = 0; j < nchars; j++)
if ((*src_ptr & A_CHARTEXT) != ' ')
*(dst_ptr + j) = *(src_ptr + j);
}
else
memmove(dst_ptr, src_ptr, nchars * sizeof(chtype));
src_ptr += src_win->_xdim;
dst_ptr += dst_win->_xdim;
}
return OK;
}
/*
* overwrite()/overlay() - Copy one window onto another. If either is
* a pad, the copy is done as if the two windows/pads have the same
* upper left corner.
*/
EXPORT int overwrite (WINDOW *src_win, WINDOW *dst_win)
{
if ((src_win->_flags & _ISPAD) || (dst_win->_flags & _ISPAD))
return copywin(src_win, dst_win, 0, 0, 0, 0,
dst_win->_maxy - 1,
dst_win->_maxx - 1,
FALSE);
else
return copywin(src_win, dst_win, 0, 0,
src_win->_begy - dst_win->_begy,
src_win->_begx - dst_win->_begx,
src_win->_maxy + src_win->_begy - dst_win->_begy - 1,
src_win->_maxx + src_win->_begx - dst_win->_begx - 1,
FALSE);
}
EXPORT int overlay (WINDOW *src_win, WINDOW *dst_win)
{
if ((src_win->_flags & _ISPAD) || (dst_win->_flags & _ISPAD))
return copywin(src_win, dst_win, 0, 0, 0, 0,
dst_win->_maxy - 1,
dst_win->_maxx - 1,
TRUE);
else
return copywin(src_win, dst_win, 0, 0,
src_win->_begy - dst_win->_begy,
src_win->_begx - dst_win->_begx,
src_win->_maxy + src_win->_begy - dst_win->_begy - 1,
src_win->_maxx + src_win->_begx - dst_win->_begx - 1,
TRUE);
}
/*
* cbreak()/nocbreak() - Put the terminal into cbreak mode (ignored currently)
*/
EXPORT int cbreak (void)
{
return (Cbreak = TRUE);
}
EXPORT int nocbreak (void)
{
return (Cbreak = FALSE);
}
/*
* echo()/noecho() - Echo characters as they are typed
*/
EXPORT int echo (void)
{
return (Echo = TRUE);
}
EXPORT int noecho (void)
{
return (Echo = FALSE);
}
/*
* nl()/nonl() - CR -> NL mapping
*/
EXPORT int nl (void)
{
return (Nl = TRUE);
}
EXPORT int nonl (void)
{
return (Nl = FALSE);
}
/*
* halfdelay() - ignored
*/
EXPORT int halfdelay (int tenths)
{
return OK;
}
/*
* intrflush() - Determines whether or not to flush input on interrupt.
*/
EXPORT int intrflush (WINDOW *win, bool flag)
{
if (win == NULL)
return ERR;
win->_flags |= _FLUSH;
return OK;
}
/*
* keypad() - Turns on/off special key mapping.
*/
EXPORT int keypad (WINDOW *win, bool flag)
{
if (win == NULL)
return ERR;
win->_use_keypad = flag;
return OK;
}
/*
* meta() - Turns on/off meta mapping.
*/
EXPORT int meta (WINDOW *win, bool flag)
{
if (win == NULL)
return ERR;
win->_use_meta = flag;
return OK;
}
/*
* nodelay() - Turns wgetch() into a non-blocking call.
*/
EXPORT int nodelay (WINDOW *win, bool flag)
{
if (win == NULL)
return ERR;
win->_nodelay = flag;
return OK;
}
/*
* notimeout() - Turn on/off escape sequence timing.
*/
EXPORT int notimeout (WINDOW *win, bool flag)
{
if (win == NULL)
return ERR;
win->_notimeout = flag;
return OK;
}
/*
* raw()/noraw() - Return characters as they are received.
*/
EXPORT int raw (void)
{
return (Raw = TRUE);
}
EXPORT int noraw (void)
{
return (Raw = FALSE);
}
/*
* get_input() - Get input from keyboard. Does an implicit refresh of screen.
*/
static int get_input (WINDOW *win, chtype *chrs, int chrs_max,
bool cbrk_flag, bool raw_flag)
{
chtype c, save_attr;
int c_pos, start_col, start_row, i;
c_pos = 0;
start_col = win->_curx;
start_row = win->_cury;
wrefresh(win);
for (;;)
{
c = CURS_KBINP_FN(win, raw_flag, cbrk_flag);
if (c == ERR)
return ERR;
if (Nl && c == '\r')
c = '\n';
if (cbrk_flag)
{
*(chrs + c_pos++) = c;
if (Echo)
waddch(win, c);
}
else
{
switch (c)
{
case '\b': /* Backspace (delete previous character) key. */
if (!raw_flag) /* Raw mode will fall through to default case. */
{
if (c_pos <= 0) {
beep();
break;
}
if (Echo)
{
waddch_allow_backspace_up = TRUE;
waddch(win, c);
if (iscntrl( (*(chrs + (c_pos - 1))) & A_CHARTEXT ))
{
waddch_allow_backspace_up = TRUE;
waddch(win, c);
}
}
c_pos--;
break;
}
case '\025': /* ctrl-U => Clear input. */
if (!raw_flag) /* Raw mode will fall through to default case. */
{
if (Echo)
{
win->_curx = start_col;
win->_cury = start_row;
save_attr = win->_attrs;
win->_attrs = 0;
for (i = 0; i < c_pos; i++)
{
if ( ((*(chrs + i)) & A_CHARTEXT) == '\t')
waddch(win, '\t');
else
{
waddch(win, ' ');
if (iscntrl((*(chrs + i)) & A_CHARTEXT))
waddch(win, ' ');
}
if (waddch_end_of_window_flag) break;
}
win->_curx = start_col;
win->_cury = start_row;
win->_attrs = save_attr;
}
c_pos = 0;
break;
}
default:
if (chrs_max != -1 && c_pos >= chrs_max)
{
beep();
break;
}
*(chrs + c_pos++) = c;
if (Echo)
{
waddch(win, c);
start_row -= waddch_scroll_cnt;
}
break;
}
}
if (Echo)
wrefresh(win);
if (cbrk_flag || c == '\n' || c == '\r')
return c_pos;
}
}
/*
* ungetch() - Push back a character to be returned by next
* call to wgetch or wgetstr.
*/
EXPORT int ungetch (chtype ch)
{
ungot_char = ch;
return ch;
}
/*
* wgetch() - Get character from keyboard.
*/
EXPORT int wgetch (WINDOW *win)
{
int cnt;
if (ungot_char != -1)
{
chtype ch = ungot_char;
ungot_char = -1;
return ch;
}
if (Kbcount <= 0)
{
Kbptr = Kbbuf;
if ((cnt = get_input(win, Kbbuf, sizeof(Kbbuf),
Cbreak, Raw)) == ERR || cnt == 0)
return ERR;
Kbcount = cnt;
}
Kbcount--;
return *(Kbptr++);
}
/*
* wgetstr() - Get a string from keyboard. Input is terminated by return
* or newline (which is stripped from string and replaced with null termination).
*/
EXPORT int wgetstr (WINDOW *win, char *str)
{
int i, ungot_off, cnt, c;
if (ungot_char != -1)
{
if ((*(str) = (ungot_char & A_CHARTEXT)) == '\n' || *str == '\r')
{
ungot_char = -1;
*str = '\0';
return OK;
}
ungot_off = 1;
ungot_char = -1;
}
else ungot_off = 0;
if (Kbcount <= 0)
{
Kbptr = Kbbuf;
if ((cnt = get_input(win, Kbbuf, sizeof(Kbbuf),
FALSE, FALSE)) == ERR || cnt == 0)
return ERR;
Kbcount = cnt;
}
for (i = 0; i < Kbcount - 1; i++)
*(str + ungot_off + i) = Kbbuf[i] & A_CHARTEXT;
if ((c = (Kbbuf[i] & A_CHARTEXT)) != '\n' && c != '\r')
*(str + ungot_off + i++) = c;
Kbcount = 0;
*(str + ungot_off + i) = '\0';
return OK;
}
#ifdef VSSCANF
/*
* scanw() - Read a scanf()-formatted string from stdscr.
*/
EXPORT int scanw (char *fmt, ...)
{
va_list args;
int rtn;
if (wgetstr(stdscr, _curses_prntw) == ERR)
return ERR;
va_start(args, fmt);
rtn = VSSCANF(_curses_prntw, fmt, args);
va_end(args);
return rtn;
}
/*
* mvscanw() - Read a scanf()-formatted string from stdscr after positioning.
*/
EXPORT int mvscanw (int y, int x, char *fmt, ...)
{
va_list args;
int rtn;
move(y, x);
if (wgetstr(stdscr, _curses_prntw) == ERR)
return ERR;
va_start(args, fmt);
rtn = VSSCANF(_curses_prntw, fmt, args);
va_end(args);
return rtn;
}
/*
* wscanw() - Read a scanf()-formatted string from a window.
*/
EXPORT int wscanw (WINDOW *win, char *fmt, ...)
{
va_list args;
int rtn;
if (wgetstr(win, _curses_prntw) == ERR)
return ERR;
va_start(args, fmt);
rtn = VSSCANF(_curses_prntw, fmt, args);
va_end(args);
return rtn;
}
/*
* mvwscanw() - Read a scanf()-formatted string from a window after positioning.
*/
EXPORT int mvwscanw (WINDOW *win, int y, int x, char *fmt, ...)
{
va_list args;
int rtn;
wmove(win, y, x);;
if (wgetstr(win, _curses_prntw) == ERR)
return ERR;
va_start(args, fmt);
rtn = VSSCANF(_curses_prntw, fmt, args);
va_end(args);
return rtn;
}
/*
* vwscanw() - Read a scanf()-formatted string from a window using a
* variable argument list.
*/
EXPORT int vwscanw (WINDOW *win, char *fmt, va_list ap)
{
va_list args;
if (wgetstr(win, _curses_prntw) == ERR)
return ERR;
return VSSCANF(_curses_prntw, fmt, ap);
}
#endif /* VSSCANF */
/*
* flushinp() - Flush input queue
*/
EXPORT int flushinp (void)
{
Kbcount = 0;
Kbptr = Kbbuf;
#ifdef CURS_FLUSHINP_FN
CURS_FLUSHINP_FN();
#endif
return OK;
}
|