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
|
/* SCCS Id: @(#)mactty.c 3.1 93/03/01 */
/* Copyright (c) Jon W{tte 1993. */
/* NetHack may be freely redistributed. See license for details. */
/*
* mactty.c
*
* This file contains the actual code for the tty library. For a
* description, see the file mactty.h, which contains all you
* need to know to use the library.
*/
#include "hack.h" /* to get flags */
#include "mttypriv.h"
#if !TARGET_API_MAC_CARBON
#include <Resources.h>
#endif
char game_active = 0; /* flag to window rendering routines not to use ppat */
/* these declarations are here because I can't include macwin.h without including the world */
extern void dprintf(char *, ...); /* dprintf.c */
/*
* Borrowed from the Mac tty port
*/
extern WindowPtr _mt_window;
static void select_onscreen_window (tty_record *record);
static void select_offscreen_port (tty_record *record);
#define MEMORY_MARGIN 30000
/*
* Convenience macro for most functions - put last in declaration
*/
#define RECORD_EXISTS(record) \
tty_record * record; \
if (!window || !(record = (tty_record *) GetWRefCon (window))) \
return general_failure;
/*
* Simple macro for deciding wether we draw at once or delay
*/
#define DRAW_DIRECT (TA_ALWAYS_REFRESH & record->attribute [TTY_ATTRIB_FLAGS])
/*
* Table of special characters. Zero is ALWAYS special; it means
* end of string and would be MISSED if it was not included here.
*/
#define COOKED_CONTROLS 0X00002581
#define RAW_CONTROLS 1
static unsigned long s_control = COOKED_CONTROLS;
/*
* Memory-related error
*/
static short
mem_err (void) {
short ret_val = MemError();
if (!ret_val) {
ret_val = general_failure;
}
return ret_val;
}
/*
* Make a rectangle empty
*/
static void
empty_rect (Rect *r) {
r->right = -20000;
r->left = 20000;
r->top = 20000;
r->bottom = -20000;
}
/*
* Union twp rect together
*/
static void
union_rect (Rect *r1, Rect *r2, Rect *dest) {
dest->left = min (r1->left, r2->left);
dest->top = min (r1->top, r2 ->top);
dest->bottom = max (r1->bottom, r2->bottom);
dest->right = max (r1->right, r2->right);
}
/*
* Dispose a pointer using the set memory-allocator
*/
static short
dispose_ptr (void *ptr) {
if (!ptr) {
return noErr; /* Silently accept disposing nulls */
}
DisposePtr (ptr);
return MemError ();
}
#if 0 /* Use alloc.c instead */
/*
* Allocate a pointer using the set memory-allocator
*/
static short
alloc_ptr (void **ptr, long size) {
*ptr = NewPtr (size);
return MemError ();
}
#endif
/*
* Set up a GWorld in the record
*/
static short
allocate_offscreen_world (tty_record *record) {
GWorldPtr gw = (GWorldPtr)0;
GWorldFlags world_flags = 0;
long mem_here, mem_there, other, required_mem;
Point p = {0, 0};
Rect r_screen;
GDHandle gdh;
short s_err;
select_onscreen_window (record);
LocalToGlobal (&p);
r_screen = record->its_bits.bounds;
OffsetRect (&r_screen, p.h, p.v);
gdh = GetMaxDevice (&r_screen);
required_mem = (long) (*((*gdh)->gdPMap))->pixelSize *
((long) record->its_bits.bounds.right *
record->its_bits.bounds.bottom) >> 3;
PurgeSpace (&other, &mem_here);
if (other < mem_here + MEMORY_MARGIN) {
mem_here = other - MEMORY_MARGIN;
}
dprintf ("Heap %ld Required %ld", mem_here, required_mem);
if (required_mem > mem_here) {
mem_there = required_mem;
if (required_mem > TempMaxMem (&mem_there)) {
dprintf ("No memory");
return memFullErr;
}
world_flags |= useTempMem;
}
s_err = NewGWorld (&gw, 0, &r_screen, (CTabHandle) 0, (GDHandle) 0, world_flags);
if (!s_err) {
record->offscreen_world = gw;
select_offscreen_port (record);
SetOrigin (0, 0);
select_onscreen_window (record);
dprintf ("New GWorld @ %lx;dm", gw);
}
return s_err;
}
/*
* Done with GWorld, release data
*/
static short
deallocate_gworld (tty_record *record) {
if (record->offscreen_world) {
DisposeGWorld (record->offscreen_world);
record->offscreen_world = (GWorldPtr) 0;
}
return noErr;
}
/*
* Get rid of offscreen bitmap
*/
static short
free_bits (tty_record *record) {
short s_err;
if (record->uses_gworld) {
s_err = deallocate_gworld (record);
#if !TARGET_API_MAC_CARBON
} else {
s_err = dispose_ptr (record->its_bits.baseAddr);
if (!s_err) {
record->its_bits.baseAddr = (char *)0;
if (record->offscreen_port) {
ClosePort (record->offscreen_port);
s_err = dispose_ptr (record->offscreen_port);
if (!s_err) {
record->offscreen_port = (GrafPtr) 0;
}
}
}
#endif
}
return s_err;
}
/*
* Snatch a window from the resource fork. Create the record.
* Otherwise, do nothing.
*/
short
create_tty (WindowRef *window, short resource_id, Boolean in_color)
{
tty_record * record;
Boolean was_allocated = !!*window;
if (in_color) {
*window = GetNewCWindow (resource_id, (Ptr) *window, (WindowRef) -1L);
} else {
*window = GetNewWindow (resource_id, (Ptr) *window, (WindowRef) -1L);
}
if (!*window) {
return mem_err ();
}
record = (tty_record *) NewPtrClear (sizeof (tty_record));
if (!record) {
#if !TARGET_API_MAC_CARBON
if (was_allocated) {
CloseWindow (*window);
} else {
#endif
DisposeWindow (*window);
#if !TARGET_API_MAC_CARBON
}
#endif
return mem_err ();
}
record->its_window = *window;
SetWRefCon (*window, (long) record);
record->was_allocated = was_allocated;
record->its_bits.baseAddr = (char *)0;
record->curs_state = TRUE;
/*
* We need to keep the window world around if we switch worlds
*/
record->offscreen_world = (GWorldPtr) 0;
record->uses_gworld = in_color;
if (in_color) {
GDHandle gh;
SetPortWindowPort(*window);
GetGWorld(&(record->its_window_world), &gh);
} else {
record->its_window_world = (GWorldPtr)0;
}
#if CLIP_RECT_ONLY
empty_rect (&(record->invalid_rect));
#else
record->invalid_part = NewRgn ();
if (!record->invalid_part) {
return destroy_tty (*window);
}
#endif
return noErr;
}
short
init_tty_number (WindowPtr window, short font_number, short font_size,
short x_size, short y_size) {
RECORD_EXISTS (record);
record->font_number = font_number;
record->font_size = font_size;
record->x_size = x_size;
record->y_size = y_size;
return force_tty_coordinate_system_recalc (window);
}
/*
* Done with a window - destroy it. Release the memory only if
* it wasn't allocated when we got it!
*/
short
destroy_tty (WindowPtr window) {
short s_err;
RECORD_EXISTS (record);
s_err = free_bits (record);
if (!s_err) {
#if !TARGET_API_MAC_CARBON
if (record->was_allocated) {
CloseWindow (window);
} else {
#endif
DisposeWindow (window);
#if !TARGET_API_MAC_CARBON
}
#endif
s_err = dispose_ptr (record);
}
return s_err;
}
static void
do_set_port_font (tty_record *record) {
PenNormal ();
TextFont (record->font_number);
TextSize (record->font_size);
if (0L != (record->attribute [TTY_ATTRIB_FLAGS] & TA_OVERSTRIKE)) {
TextMode (srcOr);
} else {
TextMode (srcCopy);
}
}
/*
* Fill in some fields from some other fields that may have changed
*/
static void
calc_font_sizes (tty_record *record) {
FontInfo font_info;
do_set_port_font (record);
GetFontInfo (&font_info);
record->char_width = font_info.widMax;
record->ascent_height = font_info.ascent + font_info.leading;
record->row_height = record->ascent_height + font_info.descent;
}
/*
* Allocate memory for the bitmap holding the tty window
*/
static short
alloc_bits (tty_record *record) {
short s_err;
SetRect (&record->its_bits.bounds, 0, 0,
record->char_width * record->x_size,
record->row_height * record->y_size);
/*
* Clear two highest and lowest bit - not a color pixMap, and even in size
*/
record->its_bits.rowBytes = ((record->its_bits.bounds.right + 15)
>> 3) & 0x1ffe;
if (record->uses_gworld) {
s_err = allocate_offscreen_world (record);
#if !TARGET_API_MAC_CARBON
} else {
s_err = alloc_ptr ((void **) &(record->its_bits.baseAddr),
record->its_bits.rowBytes * record->its_bits.bounds.bottom);
if (!s_err) {
s_err = alloc_ptr ((void **) &(record->offscreen_port),
sizeof (GrafPort));
}
if (!s_err) {
OpenPort (record->offscreen_port);
SetPort (record->offscreen_port);
ClipRect (&(record->its_bits.bounds));
SetPortBits (&(record->its_bits));
}
#endif
}
return s_err;
}
/*
* Save the current port/world in a safe place for later retrieval
*/
static void
save_port (tty_record *record, void *save) {
GWorldPtr gw;
GDHandle gh;
GrafPtr gp;
if (record->uses_gworld) {
GetGWorld (&gw, &gh);
*(GWorldPtr *) save = gw;
} else {
GetPort (&gp);
*(GrafPtr *) save = gp;
}
}
/*
* Restore current port/world after a save
*/
static void
use_port (tty_record *record, void *port) {
if (record->uses_gworld) {
PixMapHandle pix_map;
SetGWorld ((GWorldPtr) port, (GDHandle) 0);
pix_map = GetGWorldPixMap (record->offscreen_world);
if (pix_map) {
if (port == record->offscreen_world)
LockPixels (pix_map);
else
UnlockPixels (pix_map);
}
} else {
SetPort ((GrafPtr) port);
}
}
/*
* Use offscreen drawing - lock the pixels through use_port
*/
static void
select_offscreen_port (tty_record *record) {
if (record->uses_gworld) {
use_port (record, record->offscreen_world);
} else {
use_port (record, record->offscreen_port);
}
}
/*
* Use the window - unlock pixels
*/
static void
select_onscreen_window (tty_record *record) {
if (record->uses_gworld) {
use_port (record, record->its_window_world);
SetPortWindowPort(record->its_window);
} else {
use_port(record, record->its_window);
}
}
/*
* Do bits copy depending on if we're using color or not
*/
static void
copy_bits(tty_record *record, Rect *bounds, short xfer_mode, RgnHandle mask_rgn)
{
GWorldFlags pix_state;
BitMap * source;
if (record->uses_gworld) {
pix_state = GetPixelsState (GetGWorldPixMap (record->offscreen_world));
LockPixels (GetGWorldPixMap (record->offscreen_world));
source = (BitMapPtr) *GetGWorldPixMap(record->offscreen_world);
}
else source = &record->its_bits;
SetPortWindowPort(record->its_window);
CopyBits(source, GetPortBitMapForCopyBits(GetWindowPort(record->its_window)),
bounds, bounds, xfer_mode, mask_rgn);
if (record->uses_gworld) {
SetPixelsState (GetGWorldPixMap (record->offscreen_world), pix_state);
}
}
/*
* Fill an area with the background color
*/
static void
erase_rect (tty_record *record, Rect *area) {
if (game_active && u.uhp > 0 && iflags.use_stone && record->its_window == _mt_window) {
PixPatHandle ppat;
ppat = GetPixPat(iflags.use_stone + 127); /* find which pat to get */
if (ppat) { /* in game window, using backgroung pattern, and have pattern */
FillCRect (area, ppat);
DisposePixPat (ppat);
return;
}
}
EraseRect (area);
}
/*
* Recalculate the window based on new size, font, extent values,
* and re-allocate the bitmap.
*/
short
force_tty_coordinate_system_recalc (WindowPtr window) {
short s_err;
RECORD_EXISTS (record);
s_err = free_bits (record);
if (s_err) {
return s_err;
}
calc_font_sizes (record);
s_err = alloc_bits (record);
if (s_err) {
/*
* Catastrophe! We could not allocate memory for the bitmap! Things may go very
* much downhill from here!
*/
dprintf ("alloc_bits returned null in force_tty_coordinate_system_recalc!");
return s_err;
}
select_offscreen_port (record);
do_set_port_font (record);
return clear_tty (window);
}
#if 0
/*
* Update TTY according to new color environment for the window
*/
static short
tty_environment_changed (tty_record *record) {
Point p = {0, 0};
Rect r_screen;
if (record->uses_gworld) {
r_screen = record->its_bits.bounds;
LocalToGlobal (&p);
OffsetRect (&r_screen, p.h, p.v);
UpdateGWorld (&(record->offscreen_world), 0, &r_screen,
(CTabHandle) 0, (GDHandle) 0, stretchPix);
select_offscreen_port (record);
SetOrigin (0, 0);
select_onscreen_window (record);
}
return 0;
}
#endif
/*
* Read a lot of interesting and useful information from the current tty
*/
short
get_tty_metrics (WindowPtr window, short *x_size, short *y_size,
short *x_size_pixels, short *y_size_pixels, short *font_number,
short *font_size, short *char_width, short *row_height) {
RECORD_EXISTS (record);
/*
* First, test that we actually have something to draw to...
*/
if ((((char *)0 == record->its_bits.baseAddr) && !record->uses_gworld) ||
(((GWorldPtr)0 == record->offscreen_world) && record->uses_gworld)) {
return general_failure;
}
*x_size = record->x_size;
*y_size = record->y_size;
*x_size_pixels = record->its_bits.bounds.right;
*y_size_pixels = record->its_bits.bounds.bottom;
*font_number = record->font_number;
*font_size = record->font_size;
*char_width = record->char_width;
*row_height = record->row_height;
return noErr;
}
/*
* Map a position on the map to screen coordinates
*/
static void
pos_rect (tty_record *record, Rect *r, short x_pos, short y_pos,
short x_end, short y_end) {
SetRect (r, x_pos * (record->char_width), y_pos * (record->row_height),
(1 + x_end) * (record->char_width) , (1 + y_end) *
(record->row_height));
}
static void
accumulate_rect (tty_record *record, Rect *rect) {
#if CLIP_RECT_ONLY
union_rect (rect, &(record->invalid_rect), &(record->invalid_rect));
#else
RgnHandle rh = NewRgn ();
RectRgn (rh, rect);
UnionRgn (record->invalid_part, rh, record->invalid_part);
DisposeRgn (rh);
#endif
}
/*
* get and set window invalid region. exposed for HandleUpdateEvent in macwin.c
* to correct display problem
*/
short get_invalid_region (WindowPtr window, Rect *inval_rect) {
RECORD_EXISTS (record);
#if CLIP_RECT_ONLY
if (record->invalid_rect.right <= record->invalid_rect.left ||
record->invalid_rect.bottom <= record->invalid_rect.top) {
return general_failure;
}
*inval_rect = record->invalid_rect;
#else
if (EmptyRgn (record->invalid_part)) {
return general_failure;
}
*inval_rect = (*(record->invalid_part))->rgnBBox;
#endif
return noErr;
}
short set_invalid_region (WindowPtr window, Rect *inval_rect) {
RECORD_EXISTS (record);
accumulate_rect (record, inval_rect);
return noErr;
}
/*
* Invert the specified position
*/
static void
curs_pos (tty_record *record, short x_pos, short y_pos, short to_state) {
Rect r;
if (record->curs_state == to_state) {
return;
}
record->curs_state = to_state;
pos_rect (record, &r, x_pos, y_pos, x_pos, y_pos);
if (DRAW_DIRECT) {
void *old_port;
save_port (record, &old_port);
select_onscreen_window (record);
InvertRect (&r);
use_port (record, old_port);
} else {
accumulate_rect (record, &r);
}
}
/*
* Move the cursor (both as displayed and where drawing goes)
* HOWEVER: The cursor is NOT stored in the bitmap!
*/
short
move_tty_cursor (WindowPtr window, short x_pos, short y_pos) {
RECORD_EXISTS (record);
if (record->x_curs == x_pos && record->y_curs == y_pos) {
return noErr;
}
if (record->x_size <= x_pos || x_pos < 0 ||
record->y_size <= y_pos || y_pos < 0) {
return general_failure;
}
curs_pos (record, record->x_curs, record->y_curs, 0);
record->x_curs = x_pos;
record->y_curs = y_pos;
curs_pos (record, x_pos, y_pos, 1);
return noErr;
}
/*
* Update the screen to match the current bitmap, after adding stuff
* with add_tty_char etc.
*/
short
update_tty (WindowPtr window) {
Rect r;
RECORD_EXISTS (record);
#if CLIP_RECT_ONLY
if (record->invalid_rect.right <= record->invalid_rect.left ||
record->invalid_rect.bottom <= record->invalid_rect.top) {
return noErr;
}
r = record->invalid_rect;
#else
if (EmptyRgn (record->invalid_part)) {
return noErr;
}
r = (*(record->invalid_part))->rgnBBox;
#endif
select_onscreen_window (record);
copy_bits (record, &r, srcCopy, (RgnHandle) 0);
#if CLIP_RECT_ONLY
empty_rect (&(record->invalid_rect));
#else
SetEmptyRgn (record->invalid_part);
#endif
if (record->curs_state) {
pos_rect (record, &r, record->x_curs, record->y_curs,
record->x_curs, record->y_curs);
InvertRect (&r);
}
return noErr;
}
/*
* Low level add to screen
*/
static void
do_add_string (tty_record *record, char *str, short len) {
Rect r;
if (len < 1) {
return;
}
select_offscreen_port (record);
MoveTo (record->x_curs * record->char_width, record->y_curs *
record->row_height + record->ascent_height);
DrawText (str, 0, len);
pos_rect (record, &r, record->x_curs, record->y_curs,
record->x_curs + len - 1, record->y_curs);
select_onscreen_window (record);
if (DRAW_DIRECT) {
copy_bits (record, &r, srcCopy, (RgnHandle)0);
} else {
accumulate_rect (record, &r);
}
}
/*
* Low-level cursor handling routine
*/
static void
do_add_cursor (tty_record *record, short x_pos) {
record->x_curs = x_pos;
if (record->x_curs >= record->x_size) {
if (0L != (record->attribute [TTY_ATTRIB_FLAGS] & TA_WRAP_AROUND)) {
record->y_curs ++;
record->x_curs = 0;
if (record->y_curs >= record->y_size) {
if (0L != (record->attribute [TTY_ATTRIB_FLAGS] &
TA_INHIBIT_VERT_SCROLL)) {
record->y_curs = record->y_size;
} else {
scroll_tty (record->its_window, 0, 1 + record->y_curs -
record->y_size);
}
}
} else {
record->x_curs = record->x_size;
}
}
}
/*
* Do control character
*/
static void
do_control (tty_record *record, short character) {
int recurse = 0;
/*
* Check recursion because nl_add_cr and cr_add_nl may both be set and invoke each other
*/
do {
switch (character) {
case CHAR_CR :
record->x_curs = 0;
if (!recurse && (record->attribute [TTY_ATTRIB_CURSOR] & TA_CR_ADD_NL)) {
recurse = 1;
}
else {
recurse = 0;
break;
} /* FALL-THROUGH: if CR-LF, don't bother with loop */
case CHAR_LF :
record->y_curs++;
if (record->y_curs >= record->y_size) {
scroll_tty (record->its_window, 0, 1 + record->y_curs - record->y_size);
}
if (!recurse && (record->attribute [TTY_ATTRIB_CURSOR] & TA_NL_ADD_CR)) {
character = CHAR_CR;
recurse = 1;
}
else recurse = 0;
break;
case CHAR_BELL :
tty_nhbell();
break;
case CHAR_BS :
if (record->x_curs > 0)
record->x_curs --;
default :
break;
}
} while (recurse);
}
/*
* Add a single character. It is drawn directly if the correct flag is set,
* else deferred to the next update event or call of update_tty()
*/
short
add_tty_char (WindowPtr window, short character) {
register char is_control;
char ch;
RECORD_EXISTS (record);
if (!(record->attribute [TTY_ATTRIB_FLAGS] & TA_WRAP_AROUND) &&
record->x_curs >= record->x_size)
return noErr; /* Optimize away drawing across border without wrap */
if (record->curs_state != 0)
curs_pos (record, record->x_curs, record->y_curs, 0);
ch = character;
is_control = (ch < sizeof(long) * 8) && ((s_control & (1 << ch)) != 0L);
if (is_control)
do_control (record, ch);
else {
do_add_string (record, (char *)&ch, 1);
do_add_cursor (record, record->x_curs + 1);
}
return noErr;
}
/*
* Add a null-terminated string of characters
*/
short
add_tty_string(WindowPtr window, const char *string)
{
register const unsigned char * start_c;
register const unsigned char * the_c;
register unsigned char ch, is_control = 0, tty_wrap;
register short max_x, pos_x;
RECORD_EXISTS (record);
if (record->curs_state != 0)
curs_pos (record, record->x_curs, record->y_curs, 0);
the_c = (const unsigned char *) string;
max_x = record->x_size;
tty_wrap = (record->attribute [TTY_ATTRIB_FLAGS] & TA_WRAP_AROUND);
for (;;) {
pos_x = record->x_curs;
if (!tty_wrap && pos_x >= max_x)
break; /* Optimize away drawing across border without wrap */
start_c = the_c;
ch = *the_c;
while (pos_x < max_x) {
is_control = (ch < sizeof(long) * 8) && ((s_control & (1 << ch)) != 0L);
if (is_control)
break;
the_c ++; ch = *the_c;
pos_x ++;
}
do_add_string (record, (char *) start_c, the_c - start_c);
do_add_cursor (record, pos_x);
if (!ch)
break;
if (is_control) {
do_control (record, ch);
the_c ++;
}
}
return noErr;
}
/*
* Read or change attributes for the tty. Note that some attribs may
* very well clear and reallocate the bitmap when changed, whereas
* others (color, highlight, ...) are guaranteed not to.
*/
short get_tty_attrib (WindowPtr window, tty_attrib attrib, long *value) {
RECORD_EXISTS (record);
if (attrib < 0 || attrib >= TTY_NUMBER_ATTRIBUTES) {
return general_failure;
}
*value = record->attribute [attrib];
return noErr;
}
short set_tty_attrib (WindowPtr window, tty_attrib attrib, long value) {
RGBColor rgb_color;
RECORD_EXISTS (record);
if (attrib < 0 || attrib >= TTY_NUMBER_ATTRIBUTES) {
return general_failure;
}
record->attribute [attrib] = value;
/*
* Presently, no attributes generate a new bitmap.
*/
switch (attrib) {
case TTY_ATTRIB_CURSOR :
/*
* Check if we should change tables
*/
if (0L != (value & TA_RAW_OUTPUT)) {
s_control = RAW_CONTROLS;
} else {
s_control = COOKED_CONTROLS;
}
break;
case TTY_ATTRIB_FLAGS :
/*
* Check if we should flush the output going from cached to draw-direct
*/
if (0L != (value & TA_ALWAYS_REFRESH)) {
update_tty (window);
}
break;
case TTY_ATTRIB_FOREGROUND :
/*
* Set foreground color
*/
TA_TO_RGB (value, rgb_color);
select_offscreen_port (record);
RGBForeColor (&rgb_color);
select_onscreen_window (record);
break;
case TTY_ATTRIB_BACKGROUND :
/*
* Set background color
*/
TA_TO_RGB (value, rgb_color);
select_offscreen_port (record);
RGBBackColor (&rgb_color);
select_onscreen_window (record);
break;
default :
break;
}
return noErr;
}
/*
* Scroll the window. Positive is up/left. scroll_tty ( window, 0, 1 ) is a line feed.
* Scroll flushes the accumulated update area by calling update_tty().
*/
short scroll_tty (WindowPtr window, short delta_x, short delta_y) {
RgnHandle rgn;
short s_err;
RECORD_EXISTS (record);
s_err = update_tty (window);
rgn = NewRgn ();
select_offscreen_port (record);
ScrollRect (&(record->its_bits.bounds), -delta_x * record->char_width,
-delta_y * record->row_height, rgn);
EraseRgn (rgn);
SetEmptyRgn (rgn);
select_onscreen_window (record);
ScrollRect (&(record->its_bits.bounds), -delta_x * record->char_width,
-delta_y*record->row_height, rgn);
EraseRgn (rgn);
DisposeRgn (rgn);
record->y_curs -= delta_y;
record->x_curs -= delta_x;
return noErr;
}
/*
* Clear the screen. Immediate.
*/
short clear_tty (WindowPtr window) {
RECORD_EXISTS (record);
record->curs_state = 0;
select_offscreen_port (record);
erase_rect (record, &(record->its_bits.bounds));
accumulate_rect (record, &(record->its_bits.bounds));
update_tty (window);
return noErr;
}
/*
* Blink cursor on window if necessary
*/
short blink_cursor (WindowPtr window, long when) {
RECORD_EXISTS (record);
if ((record->attribute [TTY_ATTRIB_CURSOR] & TA_BLINKING_CURSOR)) {
if (when > record->last_cursor + GetCaretTime ()) {
curs_pos (record, record->x_curs, record->y_curs, !record->curs_state);
record->last_cursor = when;
update_tty (window);
}
}
return 0;
}
/*
* Draw an image of the tty - used for update events and can be called
* for screen dumps.
*/
short
image_tty (EventRecord *theEvent, WindowPtr window) {
#if defined(__SC__) || defined(__MRC__)
# pragma unused(theEvent)
#endif
RECORD_EXISTS (record);
#if CLIP_RECT_ONLY
record->invalid_rect = record->its_bits.bounds;
#else
RgnHandle rh = NewRgn ();
RectRgn (rh, record ->its_bits.bounds);
UnionRgn (record->invalid_part, rh, record->invalid_part);
DisposeRgn (rh);
#endif
return update_tty (window);
}
/*
* Clear an area
*/
short clear_tty_window (WindowPtr window, short from_x, short from_y,
short to_x, short to_y) {
Rect r;
RECORD_EXISTS (record);
if (from_x > to_x || from_y > to_y) {
return general_failure;
}
pos_rect (record, &r, from_x, from_y, to_x, to_y);
select_offscreen_port (record);
erase_rect (record, &r);
accumulate_rect (record, &r);
if (DRAW_DIRECT) {
update_tty (window);
} else
select_onscreen_window (record);
return noErr;
}
#if EXTENDED_SUPPORT
/*
* Delete or insert operations used by many terminals can bottleneck through
* here. Note that the order of executin for row/colum insertions is NOT
* specified. Negative values for num_ mean delete, zero means no effect.
*/
short mangle_tty_rows_columns (WindowPtr window, short from_row, short num_rows,
short from_column, short num_columns) {
Rect r;
RgnHandle rh = NewRgn ();
RECORD_EXISTS (record);
update_tty (window); /* Always make sure screen is OK */
curs_pos (record, record->x_curs, record->y_curs, 0);
if (num_rows) {
pos_rect (record, &r, 0, from_row, record->x_size - 1,
record->y_size - 1);
select_offscreen_port (record);
ScrollRect (&r, 0, num_rows * record->row_height, rh);
EraseRgn (rh);
SetEmptyRgn (rh);
select_onscreen_window (record);
ScrollRect (&r, 0, num_rows * record->row_height, rh);
EraseRgn (rh);
SetEmptyRgn (rh);
}
if (num_columns) {
pos_rect (record, &r, from_column, 0, record->x_size - 1,
record->y_size - 1);
select_offscreen_port (record);
ScrollRect (&r, num_columns * record->char_width, 0, rh);
EraseRgn (rh);
SetEmptyRgn (rh);
select_onscreen_window (record);
ScrollRect (&r, num_columns * record->char_width, 0, rh);
EraseRgn (rh);
SetEmptyRgn (rh);
}
DisposeRgn (rh);
if (record->x_curs >= from_column) {
record->x_curs += num_columns;
}
if (record->y_curs >= from_row) {
record->y_curs += num_rows;
}
curs_pos (record, record->x_curs, record->y_curs, 1);
return noErr;
}
/*
* Frame an area in an aesthetically pleasing way.
*/
short frame_tty_window (WindowPtr window, short from_x, short from_y ,
short to_x, short to_y, short frame_fatness) {
Rect r;
RECORD_EXISTS (record);
if (from_x > to_x || from_y > to_y) {
return general_failure;
}
pos_rect (record, & r, from_x, from_y, to_x, to_y);
select_offscreen_port (record);
PenSize (frame_fatness, frame_fatness);
FrameRect (&r);
PenNormal ();
accumulate_rect (record, &r);
if (DRAW_DIRECT) {
update_tty (window);
} else
select_onscreen_window (record);
}
/*
* Highlighting a specific part of the tty window
*/
short invert_tty_window (WindowPtr window, short from_x, short from_y ,
short to_x, short to_y) {
Rect r;
RECORD_EXISTS (record);
if (from_x > to_x || from_y > to_y) {
return general_failure;
}
pos_rect (record, &r, from_x, from_y, to_x, to_y);
select_offscreen_port (record);
InvertRect ( &r);
accumulate_rect (record, &r);
if (DRAW_DIRECT) {
update_tty (window);
} else
select_onscreen_window (record);
}
static void
canonical_rect (Rect * r, short x1, short y1, short x2, short y2) {
if (x1 < x2) {
if (y1 < y2) {
SetRect (r, x1, x2, y1, y2);
} else {
SetRect (r, x1, x2, y2, y1);
}
} else {
if (y1 < y2) {
SetRect (r, x2, x1, y1, y2);
} else {
SetRect (r, x2, x1, y2, y1);
}
}
}
/*
* Line drawing - very device dependent
*/
short draw_tty_line (WindowPtr window, short from_x, short from_y ,
short to_x, short to_y) {
Rect r;
RECORD_EXISTS (record);
select_offscreen_port (record);
MoveTo (from_x, from_y);
LineTo (to_x, to_y);
canonical_rect (&r, from_x, from_y, to_x, to_y);
accumulate_rect (record, &r);
if (DRAW_DIRECT) {
update_tty (window);
} else
select_onscreen_window (record);
}
#endif /* EXTENDED_SUPPORT */
|