Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

ODScrn.c 77.9 KB
Newer Older
rswindell's avatar
rswindell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* OpenDoors Online Software Programming Toolkit
 * (C) Copyright 1991 - 1999 by Brian Pirie.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
Deucе's avatar
Deucе committed
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
rswindell's avatar
rswindell committed
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
 *
 *
 *        File: ODScrn.c
 *
 * Description: Functions used to access the local display screen buffer, which
 *              keeps a copy of the text that is displayed on the remote
 *              terminal. The local display screen buffer also displays the
 *              OpenDoors status lines on some platforms. In addition to
 *              maintaining the current screen buffer, the odscrn.c module
 *              also contains the code to display this buffer on the screen.
 *
 *   Revisions: Date          Ver   Who  Change
 *              ---------------------------------------------------------------
 *              Oct 13, 1994  6.00  BP   New file header format.
 *              Dec 09, 1994  6.00  BP   Standardized coding style.
 *              Dec 31, 1994  6.00  BP   Remove #ifndef USEINLINE DOS code.
 *              Dec 31, 1994  6.00  BP   Use new multitasker variable.
 *              Nov 11, 1995  6.00  BP   Removed register keyword.
 *              Nov 14, 1995  6.00  BP   32-bit portability.
 *              Nov 14, 1995  6.00  BP   Created odscrn.h.
 *              Nov 14, 1995  6.00  BP   Make screen size configurable.
 *              Nov 16, 1995  6.00  BP   Removed oddoor.h, added odcore.h.
 *              Nov 21, 1995  6.00  BP   Port to Win32.
 *              Jan 20, 1996  6.00  BP   Prompt for user name if force_local.
 *              Jan 21, 1996  6.00  BP   Added ODScrnShowMessage() and related.
 *              Jan 27, 1996  6.00  BP   Expand tab ('\t') characters.
 *              Jan 27, 1996  6.00  BP   Added ODScrollUpAndInvalidate().
 *              Jan 27, 1996  6.00  BP   Made text-mode window f'ns static.
 *              Jan 31, 1996  6.00  BP   Made them non-static again.
 *              Jan 31, 1996  6.00  BP   Added ODScrnLocalInput().
 *              Feb 06, 1996  6.00  BP   Added od_silent_mode.
 *              Feb 16, 1996  6.00  BP   Make caret visible after local login.
 *              Feb 17, 1996  6.00  BP   Recognize non-ASCII keys under Win32.
 *              Feb 19, 1996  6.00  BP   Changed version number to 6.00.
 *              Feb 21, 1996  6.00  BP   Forward SC_KEYMENU to frame thread.
 *              Feb 21, 1996  6.00  BP   Don't beep in "silent mode".
 *              Mar 03, 1996  6.10  BP   Begin version 6.10.
 *              Mar 13, 1996  6.10  BP   Added od_local_win_col.
 *              Mar 17, 1996  6.10  BP   Terminate string in ODScrnLocalInput()
 *              Mar 19, 1996  6.10  BP   MSVC15 source-level compatibility.
57
 *              Aug 10, 2003  6.23  SH   *nix support
rswindell's avatar
rswindell committed
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
 */

#define BUILDING_OPENDOORS

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

#include "OpenDoor.h"
#include "ODCore.h"
#include "ODGen.h"
#include "ODPlat.h"
#include "ODScrn.h"
#include "ODUtil.h"
#include "ODFrame.h"
#include "ODInEx.h"
#ifdef ODPLAT_WIN32
#include "ODKrnl.h"
#include "ODRes.h"
#endif /* ODPLAT_WIN32 */


/* ========================================================================= */
/* Definitions of variables used by the local screen module.                 */
/* ========================================================================= */

/* Manifest constants used in this module. */
#define SCREEN_BUFFER_SIZE          (OD_SCREEN_WIDTH * OD_SCREEN_HEIGHT * 2)
#define SCREEN_BUFFER_SEGMENT_SIZE  (SCREEN_BUFFER_SIZE / 16)
#define BYTES_PER_CHAR              2
#define BUFFER_LINE_BYTES           (OD_SCREEN_WIDTH * BYTES_PER_CHAR)
#define LINE_BUFFER_SIZE            (OD_SCREEN_WIDTH + 1)


/* Private variables used by the screen I/O functions. */

deuce's avatar
deuce committed
96
#if defined(ODPLAT_DOS) || defined(ODPLAT_NIX)
rswindell's avatar
rswindell committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static void *pAllocatedBufferMemory;
#endif /* ODPLAT_DOS */

/* Far pointer to video buffer. */
static void ODFAR *pScrnBuffer;

/* Current cursor position. */
static BYTE btCursorColumn;
static BYTE btCursorRow;

/* Current output boundaries. */
static BYTE btLeftBoundary;
static BYTE btTopBoundary;
static BYTE btRightBoundary;
static BYTE btBottomBoundary;

/* Current display color. */
static BYTE btCurrentAttribute;

/* Is scrolling enabled. */
static BOOL bScrollEnabled;

119 120 121
#ifdef ODPLAT_DOS
/* Segment address of video buffer. */
static WORD wBufferSegment;
rswindell's avatar
rswindell committed
122 123
/* Display page to use. */
static BYTE btDisplayPage;
124
#endif
rswindell's avatar
rswindell committed
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

/* Is cursor currently on. */
static BYTE bCaretOn;

/* Static temporary working buffer. */
static char szBuffer[LINE_BUFFER_SIZE];


/* Private function prototypes. */
static void ODScrnGetCursorPos(void);
static void ODScrnUpdateCaretPos(void);
static void ODScrnScrollUpOneLine(void);
static void ODScrnScrollUpAndInvalidate(void);


/* ========================================================================= */
/* Implementation of the local screen window for the Win32 platform.         */
/* ========================================================================= */

#ifdef ODPLAT_WIN32

/* Screen thread startup information. */
typedef struct
{
   HWND hwndFrame;
   HANDLE hInstance;
} tODScrnThreadInfo;

/* Handle to the screen window. */
static HWND hwndScreenWindow;

/* Does the screen window currently have input focus? */
BOOL bScreenHasFocus;

/* Current font-related information. */
static HFONT hCurrentFont;
static INT nFontCellWidth;
static INT nFontCellHeight;

/* Table to translate from PC text color values used in the screen buffer */
/* to their corresponding RGB values.                                     */
COLORREF acrPCTextColors[] =
{
   RGB(0x00, 0x00, 0x00),
   RGB(0x00, 0x00, 0xc0),
   RGB(0x00, 0xc0, 0x00),
   RGB(0x00, 0xc0, 0xc0),
   RGB(0xc0, 0x00, 0x00),
   RGB(0xc0, 0x00, 0xc0),
   RGB(0xc0, 0xc0, 0x00),
   RGB(0xc0, 0xc0, 0xc0),
   RGB(0x7f, 0x7f, 0x7f),
   RGB(0x00, 0x00, 0xff),
   RGB(0x00, 0xff, 0x00),
   RGB(0x00, 0xff, 0xff),
   RGB(0xff, 0x00, 0x00),
   RGB(0xff, 0x00, 0xff),
   RGB(0xff, 0xff, 0x00),
   RGB(0xff, 0xff, 0xff),
};

/* Table to translate from Windows key codes to OpenDoors key codes. */
typedef struct
{
   int nVirtKey;
   BYTE btODKey;
} tWinKeyToODKey;

tWinKeyToODKey aWinKeyToODKey[] =
{
   {VK_UP,        OD_KEY_UP},
   {VK_DOWN,      OD_KEY_DOWN},
   {VK_LEFT,      OD_KEY_LEFT},
   {VK_RIGHT,     OD_KEY_RIGHT},
   {VK_INSERT,    OD_KEY_INSERT},
   {VK_DELETE,    OD_KEY_DELETE},
   {VK_END,       OD_KEY_END},
   {VK_HOME,      OD_KEY_HOME},
   {VK_PRIOR,     OD_KEY_PGUP},
   {VK_NEXT,      OD_KEY_PGDN},
   {VK_F1,        OD_KEY_F1},
   {VK_F2,        OD_KEY_F2},
   {VK_F3,        OD_KEY_F3},
   {VK_F4,        OD_KEY_F4},
   {VK_F5,        OD_KEY_F5},
   {VK_F6,        OD_KEY_F6},
   {VK_F7,        OD_KEY_F7},
   {VK_F8,        OD_KEY_F8},
   {VK_F9,        OD_KEY_F9},
   {VK_F10,       OD_KEY_F10},
};

/* Utility macros. */
#define COLUMN_AS_XPIXEL(nColumn)   (((INT)(nColumn)) * nFontCellWidth)
#define ROW_AS_YPIXEL(nRow)         (((INT)(nRow)) * nFontCellHeight)
#define XPIXEL_AS_COLUMN(nX)        (((INT)(nX)) / nFontCellWidth)
#define YPIXEL_AS_ROW(nY)           (((INT)(nY)) / nFontCellHeight)

/* User defined messages. */
#define WM_MOVE_YOUR_CARET          (WM_USER + 1)
#define WM_KEYDOWN_RELAY            (WM_USER + 2)

/* Height of the flashing caret, in pixels. */
#define CARET_HEIGHT   3

/* Local function prototypes. */
LRESULT CALLBACK ODScrnWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
   LPARAM lParam);
static HWND ODScrnCreateWin(HWND hwndFrame, HANDLE hInstance);
static void ODScrnMessageLoop(HANDLE hInstance, HWND hwndScreen);
DWORD OD_THREAD_FUNC ODScrnThreadProc(void *pParam);
static void ODScrnPaint(HDC hdc, INT nLeft, INT nTop, INT nRight, INT nBottom);
static void ODScrnInvalidate(BYTE btLeft, BYTE btTop, BYTE btRight,
   BYTE btBottom);
static void ODScrnSetCurrentFont(HWND hwndScreen, HFONT hNewFont);
static void ODScrnSetWinCaretPos(void);


/* ----------------------------------------------------------------------------
 * ODScrnCreateWin()                                   *** PRIVATE FUNCTION ***
 *
 * Creates the local screen window, which covers the client area of the
 * OpenDoors frame window.
 *
 * Parameters: hwndFrame   - Handle to the frame window.
 *
 *             hInstance   - Handle to application instance.
 *
 *     Return: A handle to the newly created window, or NULL on failure.
 */
static HWND ODScrnCreateWin(HWND hwndFrame, HANDLE hInstance)
{
   HWND hwndScreen = NULL;
   WNDCLASS wcScreenWindow;

   ASSERT(hwndFrame != NULL);
   ASSERT(hInstance != NULL);

   /* Register the screen window's window class. */
   memset(&wcScreenWindow, 0, sizeof(wcScreenWindow));
   wcScreenWindow.style = CS_HREDRAW | CS_VREDRAW;
   wcScreenWindow.lpfnWndProc = ODScrnWindowProc;
   wcScreenWindow.cbClsExtra = 0;
   wcScreenWindow.cbWndExtra = 0;
   wcScreenWindow.hInstance = hInstance;
   wcScreenWindow.hIcon = NULL;
   wcScreenWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
   wcScreenWindow.hbrBackground = NULL;
   wcScreenWindow.lpszMenuName = NULL;
   wcScreenWindow.lpszClassName = "ODScreen";

   RegisterClass(&wcScreenWindow);

   /* Create the screen window. */
   if((hwndScreen = CreateWindowEx(
      WS_EX_CLIENTEDGE,
      wcScreenWindow.lpszClassName,
      "",
      WS_CHILD | WS_BORDER,
      0,
      0,
      500,
      300,
      hwndFrame,
      NULL,
      hInstance,
      (LPVOID)hInstance)) == NULL)
   {
      /* On window creation failure, return NULL. */
      return(NULL);
   }

   /* Store handle to screen window for access from screen. */
   hwndScreenWindow = hwndScreen;

   return(hwndScreen);
}


/* ----------------------------------------------------------------------------
 * ODScrnWindowProc()                                  *** PRIVATE FUNCTION ***
 *
 * The local screen window proceedure.
 *
 * Parameters: hwnd   - Handle to the local screen window.
 *
 *             uMsg   - Specifies the message.
 *
 *             wParam - Specifies additional message information. The content
 *                      of this parameter depends on the value of the uMsg
 *                      parameter.
 *
 *             lParam - Specifies additional message information. The content
 *                      of this parameter depends on the value of the uMsg
 *                      parameter.
 *
 *     Return: The return value is the result of the message processing and
 *             depends on the message.
 */
LRESULT CALLBACK ODScrnWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
   LPARAM lParam)
{
   HINSTANCE hInstance;

   ASSERT(hwnd != NULL);

   hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_USERDATA);

   switch(uMsg)
   {
      case WM_SYSCOMMAND:
         /* We move any SC_KEYMENU WM_SYSCOMMAND messages to the frame */
         /* window's message queue so that the screen window thread    */
         /* can continue to process messages when the menu is          */
         /* activated from the keyboard when the screen window has     */
         /* the keyboard focus. If this isn't done, the menu will not  */
         /* behave correctly when activated this way.                  */
         if(wParam == SC_KEYMENU)
         {
            PostMessage(GetParent(hwnd), uMsg, wParam, lParam);
         }
         else
         {
            return(DefWindowProc(hwnd, uMsg, wParam, lParam));
         }
         break;

      case WM_CREATE:
      {
         CREATESTRUCT *pCreateStruct = (CREATESTRUCT *)lParam;
         hInstance = (HINSTANCE)pCreateStruct->lpCreateParams;
         SetWindowLong(hwnd, GWL_USERDATA, (LONG)hInstance);
         break;
      }

      case WM_PAINT:
      {
         PAINTSTRUCT PaintStruct;
         HDC hdc;

         /* Obtain device context and paint information. */
         hdc = BeginPaint(hwnd, &PaintStruct);

         if(hdc != NULL)
         {
            /* Redraw the portion of the window that has been invalidated. */
            ODScrnPaint(hdc,
               XPIXEL_AS_COLUMN(PaintStruct.rcPaint.left),
               YPIXEL_AS_ROW(PaintStruct.rcPaint.top),
               XPIXEL_AS_COLUMN(PaintStruct.rcPaint.right),
               YPIXEL_AS_ROW(PaintStruct.rcPaint.bottom));

            /* Release device context. */
            EndPaint(hwnd, &PaintStruct);
         }
         break;
      }

      case WM_MOVE_YOUR_CARET:
         ODScrnSetWinCaretPos();
         break;

      case WM_LBUTTONDOWN:
         SetFocus(hwnd);
         break;

      case WM_SETFOCUS:
         /* Turn on the caret when we receive the input focus. */
         /* First, create the caret. */
         CreateCaret(hwnd, NULL, nFontCellWidth, CARET_HEIGHT);

         /* Remember that we now have the input focus. */
         bScreenHasFocus = TRUE;

         /* Update the position of the caret. */
         ODScrnSetWinCaretPos();

         /* Now, make the caret visible. */
         ShowCaret(hwnd);
         break;

      case WM_KILLFOCUS:
         /* Remember that we no longer have the input focus. */
         bScreenHasFocus = FALSE;

         /* Turn off the caret when we loose the input focus. */
         DestroyCaret();
         break;

      case WM_KEYDOWN_RELAY:
      {
         int nVirtKeyPressed = (int)wParam;
         WORD wRepeatCount = LOWORD(lParam);
         int nKeyTableIndex;
         WORD wKey = 0;

         /* Look for a matching key in the OpenDoors key table. */
         for(nKeyTableIndex = 0; nKeyTableIndex < DIM(aWinKeyToODKey);
            ++nKeyTableIndex)
         {
            if(aWinKeyToODKey[nKeyTableIndex].nVirtKey == nVirtKeyPressed)
            {
               wKey = MAKEWORD(0, aWinKeyToODKey[nKeyTableIndex].btODKey);
               break;
            }
         }

         /* If a matching key was found, then add it to the queue. */
         if(wKey != 0)
         {
            while(wRepeatCount--)
            {
               ODKrnlHandleLocalKey(wKey);
            }
         }

         break;
      }

      case WM_CHAR:
      {
         WORD wRepeatCount = LOWORD(lParam);
         BYTE btScanCode = LOBYTE(HIWORD(lParam));
         TCHAR chCharCode = (TCHAR)wParam;
         WORD wKey;
         
         wKey = MAKEWORD(chCharCode, btScanCode);

         /* Loop for each repitition of this key. */
         while(wRepeatCount--)
         {
            ODKrnlHandleLocalKey(wKey);
         }
         break;
      }

      default:
         /* Pass messages that we don't explicitly handle on to the */
         /* default window proc.                                    */
         return(DefWindowProc(hwnd, uMsg, wParam, lParam));
   }

   return(0);
}


/* ----------------------------------------------------------------------------
 * ODScrnPaint()                                       *** PRIVATE FUNCTION ***
 *
 * Draws the specified portion of the screen on the provided device context.
 *
 * Parameters: hdc     - Handle to the device context to draw on.
 *
 *             nLeft   - Left column to draw.
 *
 *             nTop    - Top row to draw.
 *
 *             nRight  - Right column to draw.
 *
 *             nBottom - Bottom row to draw.
 *
 *     Return: void.
 */
static void ODScrnPaint(HDC hdc, INT nLeft, INT nTop, INT nRight, INT nBottom)
{
   INT nIDSavedState;
   INT nCurrentLine;
   INT nStartColumn;
   INT nEndColumn;
   BYTE *pbtBufferContents;
   char achStringToOutput[OD_SCREEN_WIDTH];
   char *pchNextChar;
   BYTE btCurrentAttribute;

   ASSERT(hdc != NULL);
   ASSERT(nLeft >= 0);
   ASSERT(nTop >= 0);
   ASSERT(nRight >= nLeft);
   ASSERT(nBottom >= nTop);

   /* Ensure that parameters are within valid range. */
   if(nRight >= OD_SCREEN_WIDTH) nRight = OD_SCREEN_WIDTH - 1;
   if(nBottom >= OD_SCREEN_HEIGHT) nBottom = OD_SCREEN_HEIGHT - 1;

   /* Save the current state of the device context so that we can restore */
   /* it before returning.                                                */
   nIDSavedState = SaveDC(hdc);

   /* Setup device context for displaying text from the screen buffer. */
   SetBkMode(hdc, OPAQUE);
   SelectObject(hdc, hCurrentFont);

   /* Loop through each line that is to be painted. */
   for(nCurrentLine = nTop; nCurrentLine <= nBottom; ++nCurrentLine)
   {
      /* Obtain a pointer to the first byte representing this line in */
      /* the screen buffer.                                           */
      pbtBufferContents = (BYTE *)(pScrnBuffer) +
         ((nCurrentLine * OD_SCREEN_WIDTH) + nLeft) * 2;

      /* Loop for each portion of this line that can be drawn in a single */
      /* TextOut() call.                                                  */
      for(nStartColumn = nLeft; nStartColumn <= nRight;
         nStartColumn = nEndColumn)
      {
         /* Begin constructing a string containing the text to output */
         /* in this call to TextOut().                                */
         pchNextChar = achStringToOutput;

         /* Determine the color of this portion. */
         btCurrentAttribute = pbtBufferContents[1];

         /* Loop, finding the first column that has an incompatible color. */
         for(nEndColumn = nStartColumn; nEndColumn <= nRight; ++nEndColumn)
         {
            /* Stop looping if we come to a non-equivalent color */
            /* attribute.                                        */
            if(btCurrentAttribute != pbtBufferContents[1])
            {
               break;
            }

            /* Otherwise, add this character to the string to output. */
            *pchNextChar++ = *pbtBufferContents;

            /* Move to the next position in the buffer. */
            pbtBufferContents += 2;
         }

         /* Change current display colors to match the current color */
         /* attribute.                                               */
         SetTextColor(hdc, acrPCTextColors[btCurrentAttribute & 0x0f]);
         SetBkColor(hdc, acrPCTextColors[(btCurrentAttribute & 0xf0) >> 4]);

         /* Output the string. */
         TextOut(hdc,
            COLUMN_AS_XPIXEL(nStartColumn),
            ROW_AS_YPIXEL(nCurrentLine),
            achStringToOutput,
            (nEndColumn - nStartColumn));
      }
   }

   /* Restore the device context to its original state before this function */
   /* was called.                                                           */
   RestoreDC(hdc, nIDSavedState);
}


/* ----------------------------------------------------------------------------
 * ODScrnInvalidate()                                  *** PRIVATE FUNCTION ***
 *
 * Marks the specified area of the screen window as invalid, forcing the
 * screen thread to redraw it.
 *
 * Parameters: btLeft   - The left most column to invalidate.
 *
 *             btTop    - The top most row to invalidate.
 *
 *             btRight  - The right most column to invalidate.
 *
 *             btBottom - The bottom most row to invalidate.
 *
 *     Return: void.
 */
static void ODScrnInvalidate(BYTE btLeft, BYTE btTop, BYTE btRight,
   BYTE btBottom)
{
   RECT rcToInvalidate;

   /* If the screen window has not been created yet, then return without */
   /* doing anything.                                                    */
   if(hwndScreenWindow == NULL) return;

   /* Obtain rectangle in client window coordinates, to be invalidated. */
   rcToInvalidate.left = COLUMN_AS_XPIXEL(btLeft);
   rcToInvalidate.top = ROW_AS_YPIXEL(btTop);
   rcToInvalidate.right = COLUMN_AS_XPIXEL(btRight + 1);
   rcToInvalidate.bottom = ROW_AS_YPIXEL(btBottom + 1);

   /* Mark this rectangle as invalid. */
   InvalidateRect(hwndScreenWindow, &rcToInvalidate, FALSE);
}


/* ----------------------------------------------------------------------------
 * ODScrnSetCurrentFont()                              *** PRIVATE FUNCTION ***
 *
 * Changes the current font to be used for drawing, updating anything that
 * needs updating.
 *
 * Parameters: hwndScreen - Handle to the screen window.
 *
 *             nNewFont   - Handle to the font to switch to.
 *
 *     Return: void.
 */
static void ODScrnSetCurrentFont(HWND hwndScreen, HFONT hNewFont)
{
   HDC hdc;
   INT nIDSavedState;
   TEXTMETRIC TextMetrics;

   /* Obtain a handle to the a device context for the screen window. */
   hdc = GetDC(hwndScreen);

   /* If we are unable to obtian a device context, then return without */
   /* doing anything.                                                  */
   if(hdc == NULL)
   {
      return;
   }

   /* Change the current font. */
   hCurrentFont = hNewFont;

   /* Obtain text metrics from the device context, and then release the */
   /* device context.                                                   */
   nIDSavedState = SaveDC(hdc);
   SelectObject(hdc, hCurrentFont);
   GetTextMetrics(hdc, &TextMetrics);
   RestoreDC(hdc, nIDSavedState);
   ReleaseDC(hwndScreen, hdc);

   /* Determine the new size of a character cell. */
   nFontCellWidth = TextMetrics.tmMaxCharWidth;
   nFontCellHeight = TextMetrics.tmHeight;

   /* Force window sizes to be adjusted for the new font size. */
   ODScrnAdjustWindows();
   ODScrnAdjustWindows();
}


/* ----------------------------------------------------------------------------
 * ODScrnAdjustWindows()
 *
 * Resizes and repositions the screen window to the appropriate size based
 * on the current font, portions of the frame window's client area that are
 * in use, etc. Other windows whose size depends on the size of the screen
 * window are also updated.
 *
 * Parameters: None.
 *
 *     Return: void.
 */
void ODScrnAdjustWindows(void)
{
   INT nNewClientWidth;
   INT nNewClientHeight;
   RECT rcClient;
   RECT rcWindow;
   INT nNonClientWidth;
   INT nNonClientHeight;
   INT nScreenWindowWidth;
   INT nScreenWindowHeight;
   HWND hwndFrame;
   INT nTopFrameUsed;
   INT nBottomFrameUsed;
   HWND hwndScreen;

   hwndScreen = hwndScreenWindow;
   ASSERT(hwndScreen != NULL);

   hwndFrame = GetParent(hwndScreen);
   ASSERT(hwndFrame != NULL);

   /* Determine areas of the frame window's client area that are already */
   /* in use.                                                            */
   nTopFrameUsed = ODFrameGetUsedClientAtTop(hwndFrame);
   nBottomFrameUsed = ODFrameGetUsedClientAtBottom(hwndFrame);

   /* Determine the new required size of the window's client area. */
   nNewClientWidth = nFontCellWidth * OD_SCREEN_WIDTH;
   nNewClientHeight = nFontCellHeight * OD_SCREEN_HEIGHT;

   /* Determine the size of the window's non-client area. */
   GetClientRect(hwndScreen, &rcClient);
   GetWindowRect(hwndScreen, &rcWindow);
   nNonClientWidth = (rcWindow.right - rcWindow.left)
      - (rcClient.right - rcClient.left);
   nNonClientHeight = (rcWindow.bottom - rcWindow.top)
      - (rcClient.bottom - rcClient.top);

   /* Determine the overall size required for the screen window. */
   nScreenWindowWidth = nNewClientWidth + nNonClientWidth;
   nScreenWindowHeight = nNewClientHeight + nNonClientHeight;

   /* Resize the screen window accordingly. */
   SetWindowPos(hwndScreen, NULL, 0, nTopFrameUsed, nScreenWindowWidth,
      nScreenWindowHeight, SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOZORDER);

   /* Resize the OpenDoors frame window (which is the screen window's  */
   /* parent) so that the screen window just fill's the frame window's */
   /* remaining client area.                                           */
   GetClientRect(hwndFrame, &rcClient);
   GetWindowRect(hwndFrame, &rcWindow);
   nNonClientWidth = (rcWindow.right - rcWindow.left)
      - (rcClient.right - rcClient.left);
   nNonClientHeight = (rcWindow.bottom - rcWindow.top)
      - (rcClient.bottom - rcClient.top);

   SetWindowPos(hwndFrame, NULL, 0, 0, nScreenWindowWidth + nNonClientWidth,
      nScreenWindowHeight + nNonClientHeight + nTopFrameUsed
      + nBottomFrameUsed,
      SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOZORDER);
}


/* ----------------------------------------------------------------------------
 * ODScrnMessageLoop()                                 *** PRIVATE FUNCTION ***
 *
 * Message loop for OpenDoors screen window thread.
 *
 * Parameters: hInstance   - Handle to current instance.
 *
 *             hwndScreen  - Handle to the screen window.
 *
 *     Return: void.
 */
static void ODScrnMessageLoop(HANDLE hInstance, HWND hwndScreen)
{
   MSG msg;
   HWND hwndFrame;

   ASSERT(hInstance != NULL);
   ASSERT(hwndScreen != NULL);

   /* Obtain a handle to the OpenDoors main frame window. */
   hwndFrame = GetParent(hwndScreen);

   /* Loop, fetching, translating and dispatching messages for any windows */
   /* created by this thread. (GetMessage() blocks when no messages are    */
   /* available.)                                                          */
   while(GetMessage(&msg, NULL, 0, 0))
   {
      if(!ODFrameTranslateAccelerator(hwndFrame, &msg))
      {
         TranslateMessage(&msg);
         if(msg.message == WM_KEYDOWN)
         {
            PostMessage(hwndScreen, WM_KEYDOWN_RELAY, msg.wParam, msg.lParam);
         }
         DispatchMessage(&msg);
      }
   }
}


/* ----------------------------------------------------------------------------
 * ODScrnThreadProc()                                  *** PRIVATE FUNCTION ***
 *
 * Function that execute the OpenDoors screen window thread. This thread's
 * primary task is to draw the screen window contents, when needed.
 *
 * Parameters: pParam   - The thread parameter, which is a pointer to a
 *                        tODScrnThreadInfo structure.
 *
 *     Return: TRUE on success, or FALSE on failure.
 */
DWORD OD_THREAD_FUNC ODScrnThreadProc(void *pParam)
{
   tODScrnThreadInfo *pScrnThreadInfo = (tODScrnThreadInfo *)pParam;
   HWND hwndScreen;
   HANDLE hInstance = pScrnThreadInfo->hInstance;
   HWND hwndFrame = pScrnThreadInfo->hwndFrame;

   /* We are now done with the thread startup information structure, */
   /* so deallocate it.                                              */
   free(pScrnThreadInfo);

   /* Create the screen window. */
   hwndScreen = ODScrnCreateWin(hwndFrame, hInstance);

   if(hwndScreen == NULL)
   {
      return(FALSE);
   }

   /* Set the current font for the window. This, in turn will force the  */
   /* window to be adjusted to the appropriate size, and will adjust the */
   /* size of the OpenDoors frame window accordingly.                    */
   ODScrnSetCurrentFont(hwndScreen, GetStockObject(OEM_FIXED_FONT));

   /* Prompt for the user's name before showing the windows, if required. */   
#ifdef ODPLAT_WIN32
   if(bPromptForUserName)
   {
      if(DialogBox(hInstance, MAKEINTRESOURCE(IDD_LOGIN), hwndFrame,
         ODInitLoginDlgProc) == IDCANCEL)
      {
         exit(od_control.od_errorlevel[1]);
      }

      PostMessage(hwndScreen, WM_SETFOCUS, 0, 0L);
   }
#endif /* ODPLAT_WIN32 */

   /* Now, we can make the frame window visible. */
   if(od_control.od_cmd_show == SW_MINIMIZE ||
      od_control.od_cmd_show == SW_SHOWMINIMIZED ||
      od_control.od_cmd_show == SW_SHOWMINNOACTIVE)
   {
      ShowWindow(hwndFrame, SW_SHOWMINNOACTIVE);
   }
   else
   {
      ShowWindow(hwndFrame, SW_RESTORE);
   }

   /* Now, show the screen window. */
   ShowWindow(hwndScreen, SW_SHOW);

   /* Loop, processing messages for the screen window. */
   ODScrnMessageLoop(hInstance, hwndScreen);

   /* Destroy the screen window. */
   DestroyWindow(hwndScreen);

   return(TRUE);
}


/* ----------------------------------------------------------------------------
 * ODScrnStartWindow()
 *
 * Function that starts up the screen window thread, which in turn creates
 * and manages the screen window.
 *
 * Parameters: hInstance      - Handle to the current application instance.
 *
 *             phScreenThread - Pointer to location where screen thread handle
 *                              should be stored.
 *
 *             hwndFrame      - Handle to already created frame window.
 *
 *     Return: kODRCSuccess on success, or an error code on failure.
 */
tODResult ODScrnStartWindow(HANDLE hInstance, tODThreadHandle *phScreenThread,
   HWND hwndFrame)
{
   tODScrnThreadInfo *pScrnThreadInfo;

   ASSERT(hInstance != NULL);
   ASSERT(phScreenThread != NULL);
   ASSERT(hwndFrame != NULL);

   /* Setup thread information to pass into the screen thread at startup. */
   if((pScrnThreadInfo = malloc(sizeof(tODScrnThreadInfo))) == NULL)
   {
      return(kODRCNoMemory);
   }

   pScrnThreadInfo->hInstance = hInstance;
   pScrnThreadInfo->hwndFrame = hwndFrame;

   /* Create the screen thread. */
   return(ODThreadCreate(phScreenThread, ODScrnThreadProc,
      pScrnThreadInfo));
}


/* ----------------------------------------------------------------------------
 * ODScrnSetFocusToWindow()
 *
 * Sets the current input focus to the screen window.
 *
 * Parameters: none
 *
 *     Return: void
 */
void ODScrnSetFocusToWindow(void)
{
   if(hwndScreenWindow != NULL)
   {
      SetFocus(hwndScreenWindow);
   }
}


/* ----------------------------------------------------------------------------
 * ODScrnSetWinCaretPos()
 *
 * Repositions the Windows caret to the position of our cursor, if
 * appropriate.
 *
 * Parameters: none
 *
 *     Return: void
 */
static void ODScrnSetWinCaretPos(void)
{
   /* Only move the caret if we have focus, and thus we are the one who */
   /* owns the caret.                                                   */
   if(bScreenHasFocus)
   {
      SetCaretPos(COLUMN_AS_XPIXEL(btCursorColumn + btLeftBoundary),
         ROW_AS_YPIXEL(btCursorRow + btTopBoundary + 1) - CARET_HEIGHT);
   }
}


#endif /* ODPLAT_WIN32 */



/* ========================================================================= */
/* Functions used throughout OpenDoors to manipulate local screen buffer.    */
/* ========================================================================= */

/* ----------------------------------------------------------------------------
 * ODScrnInitialize()
 *
 * Initializes the local screen module.
 *
 * Parameters: none
 *
 *     Return: kODRCSuccess on success, or an error code on failure.
 */
tODResult ODScrnInitialize(void)
{
   BOOL bClear = TRUE;

deuce's avatar
deuce committed
948
#if defined(ODPLAT_DOS) || defined(ODPLAT_NIX)
rswindell's avatar
rswindell committed
949 950
   /* In silent mode, we perform all output in a block of memory that is */
   /* never displayed.                                                   */
951
   /* *nix is always in "silent mode"                                    */
deuce's avatar
deuce committed
952
#ifndef ODPLAT_NIX
rswindell's avatar
rswindell committed
953 954
   if(od_control.od_silent_mode)
   {
deuce's avatar
deuce committed
955
#endif
rswindell's avatar
rswindell committed
956 957 958 959 960 961 962 963 964 965 966 967
      /* Allocate memory for screen buffer, using standard pointer type */
      /* for current memory model.                                      */
      pAllocatedBufferMemory = malloc(SCREEN_BUFFER_SIZE);

      if(pAllocatedBufferMemory == NULL)
      {
         return(kODRCNoMemory);
      }

      /* Set the screen buffer far pointer to point to the allocated */
      /* buffer.                                                     */
      pScrnBuffer = pAllocatedBufferMemory;
deuce's avatar
deuce committed
968
#ifndef ODPLAT_NIX
rswindell's avatar
rswindell committed
969 970 971
   }
   else
   {
972 973
      BYTE btDisplayMode;

rswindell's avatar
rswindell committed
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
      /* Get current video mode. */
      ASM    push si
      ASM    push di
      ASM    mov ah, 0x0f
      ASM    int 0x10
      ASM    mov btDisplayMode, al
      ASM    pop di
      ASM    pop si

      switch(btDisplayMode & 0x7f)
      {
         /* No need to change mode, already colour 80x25. */
         case 0x02:
         case 0x03:
            wBufferSegment = 0xb800;
            pScrnBuffer = (void ODFAR *)0xb8000000L;
            bClear = TRUE;
            break;

         /* No need to change mode, already monochrome 80x25. */
         case 0x07:
            wBufferSegment = 0xb000;
            pScrnBuffer = (void ODFAR *)0xb0000000L;
            bClear = TRUE;
            break;

         /* Must change mode to monochrome 80x25. */
         case 0x21:
            wBufferSegment = 0xb000;
            pScrnBuffer = (void ODFAR *)0xb0000000L;
            bClear = FALSE;

            /* set mode to 0x07 */
            ASM    push si
            ASM    push di
            ASM    mov ax, 0x0007
            ASM    int 0x10
            ASM    pop di
            ASM    pop si
            break;

         /* Must change mode to colour 80x25. */
         default:
            wBufferSegment = 0xb800;
            pScrnBuffer = (void ODFAR *)0xb8000000L;
            bClear = FALSE;

            /* set mode to 0x03. */
            ASM    push si
            ASM    push di
            ASM    mov ax, 0x0003
            ASM    int 0x10
            ASM    pop di
            ASM    pop si
      }



      /* Adjust address for display page which is being used. */
      ASM    push si
      ASM    push di
      ASM    mov ah, 0x0f
      ASM    int 0x10
      ASM    mov btDisplayPage, bh
      ASM    pop di
      ASM    pop si

      if(btDisplayPage!=0)
      {
         wBufferSegment += (SCREEN_BUFFER_SEGMENT_SIZE * btDisplayPage);
         ((char ODFAR *)pScrnBuffer) += (SCREEN_BUFFER_SIZE * btDisplayPage);
      }

      if(ODMultitasker == kMultitaskerDV)
      {
         /* Determine address of DV screen buffer. */
         /* This doesn't check rows, bh = rows, bl = columns. */
         ASM    mov ax, 0x2b02
         ASM    mov cx, 0x4445
         ASM    mov dx, 0x5351
         ASM    int 0x21
         ASM    cmp bx, 0x1950
         ASM    jne no_change
         ASM    mov wBufferSegment, dx

         (long)pScrnBuffer = ODDWordShiftLeft((long)wBufferSegment, 16);
   no_change: ;
      }
   }
#endif /* ODPLAT_DOS */
deuce's avatar
deuce committed
1064
#endif /* ODPLAT_DOS/NIX */
rswindell's avatar
rswindell committed
1065

deuce's avatar
deuce committed
1066
#ifdef ODPLAT_WIN32
rswindell's avatar
rswindell committed
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
   /* Allocate memory for screen buffer. */
   pScrnBuffer = malloc(SCREEN_BUFFER_SIZE);

   if(pScrnBuffer == NULL)
   {
      return(kODRCNoMemory);
   }
#endif /* ODPLAT_WIN32 */

   /* Initialize display system variables. */
   btLeftBoundary = 0;
   btRightBoundary = 79;
   btTopBoundary = 0;
   btBottomBoundary = 24;
   btCurrentAttribute = 0x07;
   bScrollEnabled = 1;

   /* Clear local screen. */
   if(bClear)
   {
      ODScrnClear();
   }

   /* Enable flashing cursor. */
   bCaretOn = FALSE;
   ODScrnEnableCaret(TRUE);

   /* Return with success. */
   return(kODRCSuccess);
}


/* ----------------------------------------------------------------------------
 * ODScrnShutdown()
 *
 * De-initializes the screen module.
 *
 * Parameters: none
 *
 *     Return: void
 */
void ODScrnShutdown(void)
{
deuce's avatar
deuce committed
1110
#ifdef ODPLAT_WIN32
rswindell's avatar
rswindell committed
1111 1112 1113 1114 1115 1116
   /* Deallocate screen buffer memory. */
   if(pScrnBuffer != NULL)
   {
      free(pScrnBuffer);
      pScrnBuffer = NULL;
   }
deuce's avatar
deuce committed
1117
#else /* !ODPLAT_WIN32 */
rswindell's avatar
rswindell committed
1118
   /* In silent mode, we must deallocate screen buffer memory. */
1119
   /* *nix is always in silent mode                            */
deuce's avatar
deuce committed
1120
#ifndef ODPLAT_NIX
rswindell's avatar
rswindell committed
1121 1122
   if(od_control.od_silent_mode && pAllocatedBufferMemory != NULL)
   {
deuce's avatar
deuce committed
1123
#endif
rswindell's avatar
rswindell committed
1124 1125 1126
      free(pAllocatedBufferMemory);
      pAllocatedBufferMemory = NULL;
      pScrnBuffer = NULL;
deuce's avatar
deuce committed
1127
#ifndef ODPLAT_NIX
rswindell's avatar
rswindell committed
1128 1129
   }
#endif
deuce's avatar
deuce committed
1130
#endif
rswindell's avatar
rswindell committed
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 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
}


/* ----------------------------------------------------------------------------
 * ODScrnSetBoundary()
 *
 * Sets the current boundary area on the screen. All output is constrained
 * within this boundary area.
 *
 * Parameters: btLeft   - 1-based column number of the left edge of the area.
 *
 *             btTop    - 1-based row number of the top edge of the area.
 *
 *             btRight  - 1-based column number of the right edge of the area.
 *
 *             btBottom - 1-based row number of the bottom edge of the area.
 *
 *     Return: void
 */
void ODScrnSetBoundary(BYTE btLeft, BYTE btTop, BYTE btRight, BYTE btBottom)
{
   /* Set internal window location variables. */
   btLeftBoundary = btLeft - 1;
   btRightBoundary = btRight - 1;
   btTopBoundary = btTop - 1;
   btBottomBoundary = btBottom - 1;

   /* Ensure that the cursor is located within the new window boundaries. */
   if(btCursorColumn > btRightBoundary - btLeftBoundary)
   {
      btCursorColumn = btRightBoundary - btLeftBoundary;
   }
   else if(btCursorColumn < btLeftBoundary)
   {
      btCursorColumn = btLeftBoundary;
   }

   if(btCursorRow > btBottomBoundary - btTopBoundary)
   {
      btCursorRow = btBottomBoundary - btTopBoundary;
   }
   else if(btCursorRow < btTopBoundary)
   {
      btCursorRow = btTopBoundary;
   }

   /* Execute the position flashing cursor primitive. */
   ODScrnUpdateCaretPos();
}


/* ----------------------------------------------------------------------------
 * ODScrnSetCursorPos()
 *
 * Sets the current cursor position. The cursor position is where the caret
 * (flashing cursor) appears (if it is currently turned on), and is the
 * location where ODScrnDisplayChar(), ODScrnDisplayString() and ODScrnPrintf()
 * will perform their output. Each of these functions, update the cursor
 * position to the next character cell after the end of their output. Other
 * ODScrn...() functions may also change the current cursor position.
 *
 * Parameters: btColumn - The 1-based column number where the cursor will
 *                        be placed.
 *
 *     Return: void
 */
void ODScrnSetCursorPos(BYTE btColumn, BYTE btRow)
{
   /* Set internal cursor position values. */
   btCursorColumn = btColumn - 1;
   btCursorRow = btRow - 1;

   /* Ensure that cursor falls within the current output window. */
   if(btCursorColumn > btRightBoundary - btLeftBoundary)
      btCursorColumn = btRightBoundary - btLeftBoundary;

   if(btCursorRow > btBottomBoundary - btTopBoundary)
      btCursorRow = btBottomBoundary - btTopBoundary;

   /* Execute the position flashing cursor primitive. */
   ODScrnUpdateCaretPos();
}


/* ----------------------------------------------------------------------------
 * ODScrnSetAttribute()
 *
 * Sets the current display attribute, to be used by ODScrnDisplayChar(),
 * ODScrnDisplayString(), ODScrnPrintf() and ODScrnClear(). The display
 * attribute byte is always in the IBM color attribute format, with the
 * lower 4 bits indicating the foreground color, and the next 3 bits
 * indicating the background color. The upper bit specifies whether the text
 * is flashing, although this code may not actually show flashing text on
 * all platforms.
 *
 * Parameters: btAttribute - The new color attribute to use.
 *
 *     Return: void
 */
void ODScrnSetAttribute(BYTE btAttribute)
{
   /* Set internal display colour attribute. */
   btCurrentAttribute = btAttribute;
}


/* ----------------------------------------------------------------------------
 * ODScrnEnableScrolling()
 *
 * Enables or disables scrolling of text within the currently defined boundary
 * area when a carriage return is sent with the cursor located on the bottom
 * line of bounary area.
 *
 * Parameters: bEnable - TRUE to enable scrolling, FALSE to disable scrolling.
 *
 *     Return: void
 */
void ODScrnEnableScrolling(BOOL bEnable)
{
   /* Stores the current scrolling setting. */
   bScrollEnabled = bEnable;
}


/* ----------------------------------------------------------------------------
 * ODScrnEnableCaret()
 *
 * Turns the caret (flashing indicator of the current cursor location) on or
 * off. Under the Win32 platform, the caret is always active when the
 * window has input focus, and inactive at any other time. Hene, under
 * Win32, this function has no effect.
 *
 * Parameters: bEnable - TRUE to turn on the flashing caret, FALSE to turn it
 *                       off.
 *
 *     Return: void
 */
void ODScrnEnableCaret(BOOL bEnable)
{
#ifdef ODPLAT_DOS
   if(bCaretOn == bEnable) return;

   bCaretOn = bEnable;

   /* Execute the cursor on / off primitive. */
   ASM    push si
   ASM    push di
   ASM    mov ah, 0x03
   ASM    mov bh, btDisplayPage
   ASM    int 0x10

   /* ch = start line, cl = end line. */
   ASM    push cx                
   ASM    mov ah, 0x0f
   ASM    int 0x10
   ASM    pop cx

   /* al = video mode. */
   ASM    push ax
   ASM    and ch, 0x1f
   ASM    mov al, bCaretOn
   ASM    and al, al
   ASM    jnz set_cursor
   /* ch bits 5-6 = blink attr */
   /*               00 = normal */
   /*               01 = invisible */
   ASM    or ch, 0x20
set_cursor:
   ASM    pop ax
   ASM    mov bh, btDisplayPage
   ASM    mov ah, 0x01
   ASM    int 0x10
   ASM    pop di
   ASM    pop si


   if(bCaretOn)
   {
      /* Turn on the local caret, updating its position. */
      ODScrnUpdateCaretPos();
   }
   else
   {
      /* Turn off the local caret. */
      ASM    mov ah, 0x02
      ASM    mov bh, btDisplayPage
      ASM    mov dh, OD_SCREEN_HEIGHT
      ASM    mov dl, OD_SCREEN_WIDTH
      ASM    push si
      ASM    push di
      ASM    int 0x10
      ASM    pop di
      ASM    pop si
   }
#endif /* ODPLAT_DOS */
}


/* ----------------------------------------------------------------------------
 * ODScrnGetTextInfo()
 *
 * Fills a structure with information about the current display settings,
 * including the position of the current boundary area (output window),
 * color attribute and cursor location.
 *
 * Parameters: pTextInfo - Pointer to the structure to store the current text
 *                         settings information in.
 *
 *     Return: void
 */
void ODScrnGetTextInfo(tODScrnTextInfo *pTextInfo)
{
   pTextInfo->wintop = btTopBoundary + 1;
   pTextInfo->winleft = btLeftBoundary + 1;
   pTextInfo->winright = btRightBoundary + 1;
   pTextInfo->winbottom = btBottomBoundary + 1;
   pTextInfo->attribute = btCurrentAttribute;
   pTextInfo->curx = btCursorColumn + 1;
   pTextInfo->cury = btCursorRow + 1;
}


/* ----------------------------------------------------------------------------
 * ODScrnPrintf()
 *
 * Performs formatted output within the current boundary area.
 *
 * Parameters: pszFormat - Format string, which is in the same format as is
 *                         used by the standard C printf() function.
 *
 *             The semantics of additional parameters is specified by the
 *             contents of the pszFormat string.
 *
 *     Return: The standard printf() return value.
 */
INT ODScrnPrintf(char *pszFormat, ...)
{
   va_list pArgumentList;
   INT nToReturn;

   /* Generate string to display. */
   va_start(pArgumentList, pszFormat);
   nToReturn = vsprintf(szBuffer, pszFormat, pArgumentList);
   va_end(pArgumentList);

   /* Ensure that we didn't overrun the buffer. */
   ASSERT(strlen(szBuffer) <= sizeof(szBuffer) - 1);

   /* Display generated string. */
   ODScrnDisplayString(szBuffer);

   /* Return appropriate value. */
   return (nToReturn);
}


/* ----------------------------------------------------------------------------
 * ODScrnDisplayChar()
 *
 * Writes a single character within the current boundary area, advancing the
 * cursor.
 *
 * Parameters: chToOutput  - The character to display.
 *
 *     Return: void
 */
void ODScrnDisplayChar(unsigned char chToOutput)
{
   BYTE ODFAR *pbtDest;
   
   ODScrnGetCursorPos();

   if(btCursorColumn > btRightBoundary - btLeftBoundary)
   {
      btCursorColumn = btRightBoundary - btLeftBoundary;
   }

   if(btCursorRow > btBottomBoundary - btTopBoundary)
   {
      btCursorRow = btBottomBoundary - btTopBoundary;
   }

   switch(chToOutput)
   {
      /* If character is a carriage return. */
      case '\r':
         btCursorColumn = 0;
         break;

      /* If character is a line feed. */
      case '\n':
         /* If cursor is at bottom of output window. */
         if(btCursorRow == btBottomBoundary - btTopBoundary)
         {
            /* Scroll the screen up by one line. */
            ODScrnScrollUpAndInvalidate();
         }
         /* If cursor is not at bottom of output window. */
         else
         {
            /* Move the cursor down one line. */
            ++btCursorRow;
         }
         break;

      case '\b':
         /* If backspace. */
         if(btCursorColumn != 0) --btCursorColumn;
         break;

      case '\t':
         /* If tab character. */
         btCursorColumn = ((btCursorColumn / 8) + 1) * 8;
         if(btCursorColumn > btRightBoundary - btLeftBoundary)
         {
            btCursorColumn = 0;

            /* If moving cursor down one line advances past end of window. */
            if(++btCursorRow > btBottomBoundary - btTopBoundary)
            {
               /* Move cursor back to bottom line of window. */
               btCursorRow = btBottomBoundary - btTopBoundary;

               /* Scroll the screen up by one line. */
               ODScrnScrollUpAndInvalidate();
            }
         }
         break;

      case '\a':
         /* If bell. */
         if(!od_control.od_silent_mode)
         {
#ifdef ODPLAT_DOS
            ASM    mov ah, 0x02
            ASM    mov dl, 7
            ASM    int 0x21
#endif /* ODPLAT_DOS */
#ifdef ODPLAT_WIN32
            MessageBeep(0xffffffff);
#endif /* ODPLAT_WIN32 */
         }
         break;

      /* If character is not a control character. */
      default:
         /* Output character to display buffer. */
         pbtDest = (BYTE ODFAR *)pScrnBuffer
            + ((btTopBoundary + btCursorRow) * BUFFER_LINE_BYTES
            + (btLeftBoundary + btCursorColumn) * BYTES_PER_CHAR);
         *pbtDest++ = chToOutput;
         *pbtDest = btCurrentAttribute;

         ASSERT(pbtDest >= (BYTE ODFAR *)pScrnBuffer);
         ASSERT(pbtDest < (BYTE ODFAR *)pScrnBuffer + SCREEN_BUFFER_SIZE);

#ifdef ODPLAT_WIN32
         /* Force the updated area of the screen window to be redrawn. */
         ODScrnInvalidate((BYTE)(btCursorColumn + btLeftBoundary),
            (BYTE)(btCursorRow + btTopBoundary),
            (BYTE)(btCursorColumn + btLeftBoundary),
            (BYTE)(btCursorRow + btTopBoundary));
#endif /* ODPLAT_WIN32 */

         /* Advance cursor. If at end of line ... */
         if(++btCursorColumn > btRightBoundary - btLeftBoundary)
         {
            /* Wrap cursor if necessary. */
            btCursorColumn = 0;

            /* If moving cursor down one line advances past end of window. */
            if(++btCursorRow > btBottomBoundary - btTopBoundary)
            {
               /* Move cursor back to bottom line of window. */
               btCursorRow = btBottomBoundary - btTopBoundary;

               /* Scroll the screen up by one line. */
               ODScrnScrollUpAndInvalidate();
            }
         }
   }

   /* Execute the update flashing cursor primitive. */
   ODScrnUpdateCaretPos();
}


/* ----------------------------------------------------------------------------
 * ODScrnGetCursorPos()                                *** PRIVATE FUNCTION ***
 *
 * Updates the current cursor position (output position) from the location of
 * the caret (flashing cursor). This function doesn't do anything on the
 * Win32 platform, since we nobody else can reposition the cursor.
 *
 * Parameters: none
 *
 *     Return: void
 */
static void ODScrnGetCursorPos(void)
{
#ifdef ODPLAT_DOS
   if(!bCaretOn) return;

   ASM    mov ah, 0x03
   ASM    mov bh, btDisplayPage
   ASM    push si
   ASM    push di
   ASM    int 0x10
   ASM    pop di
   ASM    pop si
   ASM    sub dh, btTopBoundary
   ASM    mov btCursorRow, dh
   ASM    sub dl, btLeftBoundary
   ASM    mov btCursorColumn, dl
#endif /* ODPLAT_DOS */
}


/* ----------------------------------------------------------------------------
 * ODScrnUpdateCaretPos()                              *** PRIVATE FUNCTION ***
 *
 * Updates the position of the caret (flashing cursor) from the current cursor
 * location (output position).
 *
 * Parameters: none
 *
 *     Return: void
 */
static void ODScrnUpdateCaretPos(void)
{
#ifdef ODPLAT_DOS
   if(!bCaretOn) return;

   /* Update position of flashing cursor on screen */
   ASM    mov ah, 0x02
   ASM    mov bh, btDisplayPage
   ASM    mov dh, btCursorRow
   ASM    add dh, btTopBoundary
   ASM    mov dl, btCursorColumn
   ASM    add dl, btLeftBoundary
   ASM    push si
   ASM    push di
   ASM    int 0x10
   ASM    pop di
   ASM    pop si
#endif /* ODPLAT_DOS */

#ifdef ODPLAT_WIN32
   if(hwndScreenWindow != NULL)
   {
      PostMessage(hwndScreenWindow, WM_MOVE_YOUR_CARET, 0, 0);
   }
#endif /* ODPLAT_WIN32 */
}


/* ----------------------------------------------------------------------------
 * ODScrnClear()
 *
 * Clears the text within the currently defined boundary area, setting the
 * display attribute of the entire boundary area to the current display
 * color.
 *
 * Parameters: none
 *
 *     Return: void
 */
void ODScrnClear(void)
{
   WORD ODFAR *pDest = (WORD ODFAR *)pScrnBuffer +
      ((btTopBoundary * OD_SCREEN_WIDTH) + btLeftBoundary);
   WORD wBlank = (((WORD)btCurrentAttribute) << 8) | 32;
   BYTE btCurColumn;
   BYTE btCurLine = (btBottomBoundary - btTopBoundary) + 1;
   BYTE btColumnStart = (btRightBoundary - btLeftBoundary) + 1;
   BYTE btSkip = OD_SCREEN_WIDTH - btColumnStart;

   /* Clear contents of current window. */
   do
   {
      btCurColumn = btColumnStart;
      do
      {
         ASSERT(pDest >= (WORD ODFAR *)pScrnBuffer);
         ASSERT(pDest <= (WORD ODFAR *)pScrnBuffer + 2000);
         *(pDest++) = wBlank;
      } while ((--btCurColumn) != 0);
      pDest += btSkip;
   } while((--btCurLine) != 0);

   /* Move cursor to top left-hand corner of current window. */
   btCursorColumn = btCursorRow = 0;

   /* Execute the update flashing cursor primitive. */
   ODScrnUpdateCaretPos();

#ifdef ODPLAT_WIN32
   /* Force the updated area of the screen window to be redrawn. */
   ODScrnInvalidate(btLeftBoundary, btTopBoundary, btRightBoundary,
      btBottomBoundary);
#endif /* ODPLAT_WIN32 */
}


/* ----------------------------------------------------------------------------
 * ODScrnScrollUpAndInvalidate()                       *** PRIVATE FUNCTION ***
 *
 * Scrolls the entire screen up by one line, only if scrolling is enabled.
 * If scrolling is performed, invalidates area that was scrolled. Scrolling
 * is accomplished using ODScrnScrollUpOneLine().
 *
 * Parameters: none
 *
 *     Return: void
 */
static void ODScrnScrollUpAndInvalidate(void)
{
   /* If scrolling is enabled. */
   if(bScrollEnabled)
   {
      /* Execute the scroll primitive. */
      ODScrnScrollUpOneLine();

#ifdef ODPLAT_WIN32
      /* Force the updated area of the screen window to be redrawn. */
      ODScrnInvalidate(btLeftBoundary, btTopBoundary, btRightBoundary,
         btBottomBoundary);
#endif /* ODPLAT_WIN32 */
   }
}


/* ----------------------------------------------------------------------------
 * ODScrnScrollUpOneLine()                             *** PRIVATE FUNCTION ***
 *
 * Scrolls the area within the current output boundary up one line, leaving the
 * newly created line at the bottom of the area blank, with the current display
 * attribute.
 *
 * Parameters: none
 *
 *     Return: void
 */
static void ODScrnScrollUpOneLine(void)
{
   WORD ODFAR *pwDest = (WORD ODFAR *)pScrnBuffer
      + (btTopBoundary * OD_SCREEN_WIDTH + btLeftBoundary);
   WORD ODFAR *pwSource;
   BYTE btCurColumn;
   BYTE btCurLine = btBottomBoundary - btTopBoundary;
   BYTE btColumnStart = btRightBoundary - btLeftBoundary + 1;
   BYTE btSkip = OD_SCREEN_WIDTH - btColumnStart;
   WORD wBlank = (((WORD)btCurrentAttribute) << 8) | 32;

   pwSource = pwDest + OD_SCREEN_WIDTH;

   ASSERT(btSkip >= 0 && btSkip <= OD_SCREEN_WIDTH);

   /* Move text in area of window up one line. */
   do
   {
      btCurColumn = btColumnStart;
      do
      {
         ASSERT(pwDest >= (WORD ODFAR *)pScrnBuffer);
         ASSERT(pwDest <= (WORD ODFAR *)pScrnBuffer + 2000);
         ASSERT(pwSource >= (WORD ODFAR *)pScrnBuffer);
         ASSERT(pwSource <= (WORD ODFAR *)pScrnBuffer+2000);
         *(pwDest++) = *(pwSource++);
      } while((--btCurColumn) != 0);
      pwDest += btSkip;
      pwSource += btSkip;
   } while ((--btCurLine) != 0);

   /* Clear newly created line at bottom of window. */
   btCurColumn = btColumnStart;
   do
   {
      ASSERT(pwDest >= (WORD ODFAR *)pScrnBuffer);
      ASSERT(pwDest <= (WORD ODFAR *)pScrnBuffer + 2000);
      *(pwDest++) = wBlank;
   } while((--btCurColumn) != 0);
}


/* ----------------------------------------------------------------------------
 * ODScrnGetText()
 *
 * Copies a portion of the currently displayed text and corresponding color
 * attributes to a buffer provided by the caller.
 *
 * Parameters: btLeft    - Column number of the left edge of the area to copy
 *                         from.
 *
 *             btTop     - Row number of the top edge of the area to copy from.
 *
 *             btRight   - Column number of the right edge of the area to copy
 *                         from.
 *
 *             btBottom  - Row number of the bottom edge of the area to copy
 *                         from.
 *
 *             pbtBuffer - A pointer to the buffer to copy to. It is the
 *                         caller's responsibility to ensure that this buffer
 *                         is large enough. This buffer must be at least
1736
 *                         2 x (Width of area) x (Height of area) bytes in size.
rswindell's avatar
rswindell committed
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
 *
 *     Return: TRUE on success, or FALSE on failure.
 */
BOOL ODScrnGetText(BYTE btLeft, BYTE btTop, BYTE btRight, BYTE btBottom,
   void *pbtBuffer)
{
   WORD *pwBuffer = (WORD *)pbtBuffer;
   WORD ODFAR *pSource = (WORD ODFAR *)pScrnBuffer
      + ((((--btTop) + btTopBoundary)
      * OD_SCREEN_WIDTH) + btLeftBoundary + (--btLeft));
   BYTE btCurColumn;
   BYTE btCurLine = (--btBottom) - btTop + 1;
   BYTE btColumnStart = (--btRight) - btLeft + 1;
   BYTE btSkip = OD_SCREEN_WIDTH - btColumnStart;

   ASSERT(btLeft >= 0);
   ASSERT(btTop >= 0);
   ASSERT(btRight <= btRightBoundary - btLeftBoundary);
   ASSERT(btBottom <= btBottomBoundary - btTopBoundary);
   ASSERT(pbtBuffer);

   /* Copy contents of screen block to buffer */
   do 
   {
      btCurColumn = btColumnStart;
      do
      {
         ASSERT(pSource >= (WORD ODFAR *)pScrnBuffer);
         ASSERT(pSource <= (WORD ODFAR *)pScrnBuffer + 2000);
         ASSERT(pwBuffer >= (WORD *)pbtBuffer);
         ASSERT(pwBuffer <= (WORD *)pbtBuffer + 2000);
         *(pwBuffer++) = *(pSource++);
      } while ((--btCurColumn) != 0);
      pSource += btSkip;
   } while((--btCurLine) != 0);

   return(TRUE);
}


/* ----------------------------------------------------------------------------
 * ODScrnPutText()
 *
 * Changes the currently displayed text and corresponding color attributes in
 * the specified area, to the values taken from the buffer. This buffer is in
 * the same format as is produce by the ODScrnGetText() function.
 *
 * Parameters: btLeft    - Column number of the left edge of the area to copy
 *                         to.
 *
 *             btTop     - Row number of the top edge of the area to copy to.
 *
 *             btRight   - Column number of the right edge of the area to copy
 *                         to.
 *
 *             btBottom  - Row number of the bottom edge of the area to copy
 *                         to.
 *
 *             pbtBuffer - A pointer to the buffer to copy from.
 *
 *     Return: TRUE on success, or FALSE on failure.
 */
BOOL ODScrnPutText(BYTE btLeft, BYTE btTop, BYTE btRight, BYTE btBottom,
   void *pbtBuffer)
{
   WORD *pwBuffer = (WORD *)pbtBuffer;
   WORD ODFAR *pDest = (WORD ODFAR *)pScrnBuffer
      + ((((--btTop) + btTopBoundary)
      * OD_SCREEN_WIDTH) + btLeftBoundary + (--btLeft));
   BYTE btCurColumn;
   BYTE btCurLine = (--btBottom) - btTop + 1;
   BYTE btColumnStart = (--btRight) - btLeft + 1;
   BYTE btSkip = OD_SCREEN_WIDTH - btColumnStart;

   ASSERT(btLeft >= 0 && btTop >= 0);
   ASSERT(btLeft <= btRightBoundary - btLeftBoundary);
   ASSERT(btTop <= btBottomBoundary - btTopBoundary);
   ASSERT(btRight >= 0 && btBottom >= 0);
   ASSERT(btRight <= btRightBoundary - btLeftBoundary);
   ASSERT(btBottom <= btBottomBoundary - btTopBoundary);
   ASSERT(pbtBuffer != NULL);

   /* Copy contents of screen block to buffer. */
   do
   {
      btCurColumn = btColumnStart;
      do
      {
         ASSERT(pDest >= (WORD ODFAR *)pScrnBuffer);
         ASSERT(pDest <= (WORD ODFAR *)pScrnBuffer + 2000);
         ASSERT(pwBuffer >= (WORD *)pbtBuffer);
         ASSERT(pwBuffer <= (WORD *)pbtBuffer + 2000);
         *(pDest++) = *(pwBuffer++);
      } while ((--btCurColumn) != 0);
      pDest += btSkip;
   } while((--btCurLine) != 0);

#ifdef ODPLAT_WIN32
   /* Force the updated area of the screen window to be redrawn. */
   ODScrnInvalidate((BYTE)(btLeftBoundary + btLeft), 
      (BYTE)(btTopBoundary + btTop),
      (BYTE)(btRightBoundary + btRight),
      (BYTE)(btBottomBoundary + btBottom));
#endif /* ODPLAT_WIN32 */

   return(TRUE);
}


/* ----------------------------------------------------------------------------
 * ODScrnDisplayString()
 *
 * Copies the contents of a string to the display, using the currently set
 * color attributes. The cursor location is updated to the end of the string
 * on the screen.
 *
 * Parameters: pszString - Pointer to the string to display.
 *
 *     Return: void.
 */
deuce's avatar
deuce committed
1857
void ODScrnDisplayString(const char *pszString)
rswindell's avatar
rswindell committed
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
{
   ODScrnDisplayBuffer(pszString, strlen(pszString));
}


/* ----------------------------------------------------------------------------
 * ODScrnDisplayBuffer()
 *
 * Copies the contents of a buffer to the display, using the currently set
 * color attributes. The cursor location is updated to the end of the text
 * displayed to the screen.
 *
 * Parameters: pBuffer         - Pointer to a buffer containing the
 *                               character(s) to display.
 *
 *             nCharsToDisplay - Count of number of characters to display from
 *                               the buffer.
 *
 *     Return: void.
 */
deuce's avatar
deuce committed
1878
void ODScrnDisplayBuffer(const char *pBuffer, INT nCharsToDisplay)
rswindell's avatar
rswindell committed
1879
{
deuce's avatar
deuce committed
1880
   const char *pchCurrentChar = pBuffer;
rswindell's avatar
rswindell committed
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
   INT nCharsLeft = nCharsToDisplay;
   BYTE ODFAR *pDest;
   BYTE btLeftColumn;
   BYTE btAttribute = btCurrentAttribute;
   BYTE btCurrentColumn;
   BYTE btBottom = btBottomBoundary - btTopBoundary;
#ifdef ODPLAT_WIN32
   BOOL bAnythingInvalid = FALSE;
   BYTE btLeftMost;
   BYTE btRightMost;
   BYTE btTopMost;
   BYTE btBottomMost;
#endif /* ODPLAT_WIN32 */

   ASSERT(pBuffer != NULL);
   ASSERT(nCharsToDisplay >= 0);

   ODScrnGetCursorPos();

   if(btCursorColumn > btRightBoundary - btLeftBoundary)
   {
      btCursorColumn = btRightBoundary - btLeftBoundary;
   }

   if(btCursorRow > btBottomBoundary - btTopBoundary)
   {
      btCursorRow = btBottomBoundary - btTopBoundary;
   }

   btCurrentColumn = btCursorColumn;

   btLeftColumn = btRightBoundary - (btCurrentColumn + btLeftBoundary);
   pDest = (BYTE ODFAR *) pScrnBuffer + (((btTopBoundary + btCursorRow)
      * BUFFER_LINE_BYTES)
      + (btLeftBoundary + btCursorColumn) * BYTES_PER_CHAR);

   while(nCharsLeft--)
   {
      ASSERT(pDest >= (BYTE ODFAR *)pScrnBuffer);
      ASSERT(pDest <= (BYTE ODFAR *)pScrnBuffer + SCREEN_BUFFER_SIZE);
      switch(*pchCurrentChar)
      {
         case '\r':
            btCurrentColumn = 0;
            btLeftColumn = btRightBoundary - btLeftBoundary;
            pDest = (BYTE ODFAR *)pScrnBuffer + ((btTopBoundary + btCursorRow)
               * BUFFER_LINE_BYTES + btLeftBoundary * BYTES_PER_CHAR);
            pchCurrentChar++;
            break;
         case '\n':
            if (btCursorRow < btBottom)
            {
               ++btCursorRow;
               pDest += BUFFER_LINE_BYTES;
            }
            else if(bScrollEnabled)
            {
               ODScrnScrollUpOneLine();
#ifdef ODPLAT_WIN32
               /* Entire boundary area is now invalid. */
               bAnythingInvalid = TRUE;
               btLeftMost = btLeftBoundary;
               btRightMost = btRightBoundary;
               btTopMost = btTopBoundary;
               btBottomMost = btBottomBoundary;
#endif /* ODPLAT_WIN32 */
            }
            pchCurrentChar++;
            break;

         case '\a':
            /* If bell */
            if(!od_control.od_silent_mode)
            {
#ifdef ODPLAT_DOS
               ASM    mov ah, 0x02
               ASM    mov dl, 7
               ASM    int 0x21
#endif /* ODPLAT_DOS */
#ifdef ODPLAT_WIN32
               MessageBeep(0xffffffff);
#endif /* ODPLAT_WIN32 */
               pchCurrentChar++;
            }
            break;

         case '\t':
            /* If tab character. */
            btCurrentColumn = ((btCurrentColumn / 8) + 1) * 8;
            if(btCurrentColumn > btRightBoundary - btLeftBoundary)
            {
               btCurrentColumn = 0;

1974
               /* If moving cursor down one line advances past end of window. */
rswindell's avatar
rswindell committed
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
               if(++btCursorRow > btBottomBoundary - btTopBoundary)
               {
                  /* Move cursor back to bottom line of window. */
                  btCursorRow = btBottomBoundary - btTopBoundary;

                  /* If scrolling is enabled. */
                  if(bScrollEnabled)
                  {
                     /* Execute the scroll primitive .*/
                     ODScrnScrollUpOneLine();
#ifdef ODPLAT_WIN32
                     /* Entire boundary area is now invalid. */
                     bAnythingInvalid = TRUE;
                     btLeftMost = btLeftBoundary;
                     btRightMost = btRightBoundary;
                     btTopMost = btTopBoundary;
                     btBottomMost = btBottomBoundary;
#endif /* ODPLAT_WIN32 */
                  }
               }
            }

            /* Determine new buffer destination address. */
            pDest = (BYTE ODFAR *) pScrnBuffer
               + (((btTopBoundary + btCursorRow) * BUFFER_LINE_BYTES)
               + (btLeftBoundary + btCursorColumn) * BYTES_PER_CHAR);
            break;

         case '\b':
            if(btCurrentColumn > 0)
            {
               --btCurrentColumn;
               pDest-=2;
               btLeftColumn++;
            }
            pchCurrentChar++;
            break;

         default:
            *(pDest++) = *(pchCurrentChar++);
            *(pDest++) = btAttribute;

#ifdef ODPLAT_WIN32
            /* Expand area to invalidate, if needed. */
            if(!bAnythingInvalid)
            {
               bAnythingInvalid = TRUE;
               btLeftMost = btLeftBoundary + btCurrentColumn;
               btRightMost = btLeftBoundary + btCurrentColumn;
               btTopMost = btTopBoundary + btCursorRow;
               btBottomMost = btTopBoundary + btCursorRow;
            }
            else
            {
               BYTE btColumn = btLeftBoundary + btCurrentColumn;
               BYTE btRow = btTopBoundary + btCursorRow;

               if(btColumn < btLeftMost) btLeftMost = btColumn;
               if(btColumn > btRightMost) btRightMost = btColumn;
               if(btRow < btTopMost) btTopMost = btRow;
               if(btRow > btBottomMost) btBottomMost = btRow;
            }
#endif /* ODPLAT_WIN32 */

            if(btLeftColumn--)
            {
               ++btCurrentColumn;
            }
            else
            {
               btCurrentColumn = 0;
               btLeftColumn = btRightBoundary - btLeftBoundary;

               if(btCursorRow < btBottom)
               {
                  ++btCursorRow;
               }
               else if(bScrollEnabled)
               {
                  ODScrnScrollUpOneLine();
#ifdef ODPLAT_WIN32
                  /* Entire boundary area is now invalid. */
                  bAnythingInvalid = TRUE;
                  btLeftMost = btLeftBoundary;
                  btRightMost = btRightBoundary;
                  btTopMost = btTopBoundary;
                  btBottomMost = btBottomBoundary;
#endif /* ODPLAT_WIN32 */
               }

               pDest = (BYTE ODFAR *)pScrnBuffer
                  + ((btTopBoundary + btCursorRow)
                  * BUFFER_LINE_BYTES + btLeftBoundary * BYTES_PER_CHAR);
            }
      }
   }

   btCursorColumn = btCurrentColumn;
   ODScrnUpdateCaretPos();

#ifdef ODPLAT_WIN32
   if(bAnythingInvalid)
   {
      /* Force the updated area of the screen window to be redrawn. */
      ODScrnInvalidate(btLeftMost, btTopMost, btRightMost,
         btBottomMost);
   }
#endif /* ODPLAT_WIN32 */
}


/* ----------------------------------------------------------------------------
 * ODScrnCopyText()
 *
 * Copies the contents of the specified area on the screen to another location
 * on the screen. The destination location must be such that the entire area
 * specified as the source can be displayed without falling off the edge of the
 * screen.
 *
 * Parameters: btLeft       - Column number of the left edge of the area to
 *                            copy from.
 *
 *             btTop        - Row number of the top edge of the area to copy
 *                            from.
 *
 *             btRight      - Column number of the right edge of the area to
 *                            copy from.
 *
 *             btBottom     - Row number of the bottom edge of the area to
 *                            copy from.
 *
 *             btDestColumn - Column number where the upper right corner of
 *                            the area should be copied to.
 *
 *             btDestRow    - Row number where the upper right cornder of the
 *                            area should be copied to.
 *
 *     Return: TRUE on success, or FALSE on failure. May fail due to
 *             insufficient available memory.
 */
BOOL ODScrnCopyText(BYTE btLeft, BYTE btTop, BYTE btRight, BYTE btBottom,
   BYTE btDestColumn, BYTE btDestRow)
{
   void *pScrnBuffer;

   ASSERT(btLeft >= 0 && btTop >= 0);
   ASSERT(btLeft <= btRightBoundary - btLeftBoundary);
   ASSERT(btTop <= btBottomBoundary - btTopBoundary);
   ASSERT(btRight >= 0 && btBottom >= 0);
   ASSERT(btRight <= btRightBoundary - btLeftBoundary);
   ASSERT(btBottom <= btBottomBoundary - btTopBoundary);
   ASSERT(btDestColumn >= 0 && btDestRow >= 0);
   ASSERT(btDestColumn <= btRightBoundary - btLeftBoundary);
   ASSERT(btDestRow <= btBottomBoundary - btTopBoundary);

   if(   !(btLeft <= btRightBoundary - btLeftBoundary
         && btTop <= btBottomBoundary - btTopBoundary)
      || !(btRight <= btRightBoundary - btLeftBoundary
         && btBottom <= btBottomBoundary - btTopBoundary)
      || !(btDestColumn <= btRightBoundary - btLeftBoundary
         && btDestRow <= btBottomBoundary - btTopBoundary))
   {
      return(FALSE);
   }


   if((pScrnBuffer = malloc((btRight - btLeft + 1) * (btBottom - btTop + 1)
      * BYTES_PER_CHAR)) == NULL)
   {
      /* Insufficient memory, return with failure. */
      return (FALSE);
   }

   ODScrnGetText(btLeft, btTop, btRight, btBottom, pScrnBuffer);
   ODScrnPutText(btDestColumn, btDestRow,
      (BYTE)(btRight + (btDestColumn - btLeft)),
      (BYTE)(btBottom + (btDestRow - btTop)), pScrnBuffer);
   free(pScrnBuffer);

   return(TRUE);
}


/* ----------------------------------------------------------------------------
 * ODScrnClearToEndOfLine()
 *
 * Clears the contents of the current line, from the current cursor location
 * to the end of the line.
 *
 * Parameters: none
 *
 *     Return: void
 */
void ODScrnClearToEndOfLine(void)
{
   unsigned char btCharsToDelete = btRightBoundary
      - (btLeftBoundary + btCursorColumn);
   BYTE ODFAR *pDest = (BYTE ODFAR *) pScrnBuffer
      + (((btTopBoundary + btCursorRow) * BUFFER_LINE_BYTES)
      + (btLeftBoundary + btCursorColumn) * BYTES_PER_CHAR);
   BYTE btAttribute = btCurrentAttribute;

   while(btCharsToDelete--)
   {
      *(pDest++) = ' ';
      *(pDest++) = btAttribute;
   }

#ifdef ODPLAT_WIN32
   /* Force the updated area of the screen window to be redrawn. */
   ODScrnInvalidate((BYTE)(btLeftBoundary + btCursorColumn),
      (BYTE)(btTopBoundary + btCursorRow), btRightBoundary,
      (BYTE)(btTopBoundary + btCursorRow));
#endif /* ODPLAT_WIN32 */
}


/* ----------------------------------------------------------------------------
 * ODScrnCreateWindow()
 *
 * Creates a text-based window on the local terminal.
 *
 * Parameters: btLeft           - Column numebr of the left of the window.
 *
 *             btTop            - Row number of the top of the window.
 *
 *             btRight          - Column number of the right of the window.
 *
 *             btBottom         - Row number of the bottom of the window.
 *
 *             btAttribute      - Display attribute for the window boarder and
 *                                the area inside the window.
 *
 *             pszTitle         - Pointer to a string containing the title to
 *                                display, or "" for none.
 *
 *             btTitleAttribute - Display attribute for the title text.
 *
 *     Return: void
 */
#ifdef OD_TEXTMODE
void *ODScrnCreateWindow(BYTE btLeft, BYTE btTop, BYTE btRight,
   BYTE btBottom, BYTE btAttribute, char *pszTitle, BYTE btTitleAttribute)
{
   void *pUnder;
   INT nBetween;
   INT nCount;
   INT nFirst;
   char *pszString;
   int nTitleWidth;

   ASSERT(pszTitle != NULL);

   /* Alocate space to store screen contents "under" window. */
   if((pUnder = malloc((btRight - btLeft + 1) * (btBottom - btTop + 1)
      * BYTES_PER_CHAR + 4 * sizeof(BYTE))) == NULL)
   {
      return(NULL);
   }

   /* Store the window's position in the buffer. */
   ((BYTE *)pUnder)[0] = btLeft;
   ((BYTE *)pUnder)[1] = btTop;
   ((BYTE *)pUnder)[2] = btRight;
   ((BYTE *)pUnder)[3] = btBottom;

   /* Retrieve screen contents in window area. */
   ODScrnGetText(btLeft, btTop, btRight, btBottom, ((BYTE *)pUnder) + 4);

   /* Determine area between left & right of window, distance of line before */
   /* title, and distance of line after title.                               */
   if(strlen(pszTitle) == 0)
   {
      nTitleWidth = 0;
   }
   else
   {
      nTitleWidth = strlen(