home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / sysutl / tess_c.arc / TESSDEMO.C < prev    next >
C/C++ Source or Header  |  1988-07-03  |  34KB  |  1,050 lines

  1.     /*
  2.      * TESSDEMO.C -- TesSeRact Demonstration Program
  3.      */
  4.  
  5. /******************************< TESSDEMO.C >********************************
  6. *                                                                           *
  7. *                     TesSeRact Demonstration Program                       *
  8. *                     --------------------------------                      *
  9. *                                                                           *
  10. *   Copyright (c) 1986, 1987, 1988, TesSeRact Development Team              *
  11. *                                                                           *
  12. *************************************************************************CR*/
  13.     /*
  14.      * Compiled with Turbo-C 1.5 for demonstration purposes
  15.      *   Used with small model
  16.      *   
  17.      * To compile with MSC, use /DMSC5 command-line switch
  18.      *   Note that if this is compiled with MSC 5.0, a lot of warning
  19.      *   messages are generated -- this is because of the bug in MSC5.0
  20.      *   having to do with functions prototyped with a 'void' parameter.
  21.      *   It's not worth it to fix bugs in the compiler.
  22.      * 
  23.      */
  24.  
  25. #ifndef MSC5                            /* if MSC5 not defined             */
  26. #define TC                              /*    assume Turbo C               */
  27. #endif
  28.  
  29. #include <stdio.h>
  30. #include <dos.h>
  31. #include <bios.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <io.h>
  35.  
  36. #ifdef TC
  37. #include <conio.h>
  38. #include <dir.h>
  39. #endif
  40.  
  41. #include <ctype.h>
  42. #include <errno.h>
  43.  
  44. #include "tess.h"                       /* Include file for TesSeRact     */
  45.                                         /*    structures and prototypes    */
  46.  
  47.     /*
  48.      * #define NOTSR to test routines
  49.      */
  50. /* #define NOTSR 1 */
  51.  
  52.     /*
  53.      * Function prototypes for this file
  54.      */
  55. void c_str(int row, char *str);
  56. void SaveCursor(void);
  57. void RestoreCursor(void);
  58. void NoCursor(void);
  59. void BigCursor(void);
  60. void DisplayTime(void);
  61. void AdjustTime(void);
  62. void fixrows(void);
  63. unsigned SizeOfCode(unsigned type);
  64. void InitTsrDemo(void);
  65. void do_cpyrt(void);
  66.  
  67.  
  68.     /*
  69.      * Defines to be used for 'type' parameter
  70.      *   of SizeOfCode
  71.      */
  72. #define NOHEAP  1
  73. #define ALLHEAP 2
  74. #define ALLSTACK 3
  75.  
  76. #ifdef TC
  77.     /*
  78.      * Variables specific to Turbo-C
  79.      */
  80. extern unsigned _heaplen = 128;         /* Use 128-byte heap               */
  81. extern char _video[];                   /* undocumented video structure    */
  82. extern void _VideoInt(void);            /* undocumented INT 10h call that  */
  83.                                         /*    also saves BP register to    */
  84.                                         /*    compensate for buggy BIOSes  */
  85. #endif
  86.  
  87.     /*
  88.      * Variables for Turbo-C Video Services
  89.      */
  90.  
  91. #define MAXVIDROWS  60                  /* VGA allows 50 lines             */
  92.                                         /*   But I have 60 on my screen    */
  93. #define MAXVIDCOLS  80                  /* Only dealing with 80-cols       */
  94. #define MAXVIDSIZE  (MAXVIDROWS * MAXVIDCOLS * 2)
  95.  
  96. #ifdef MSC5
  97.  
  98. #define COLORNORM   0x1e
  99. #define COLORREV    0x4f
  100.  
  101. #else
  102.  
  103. #define COLORNORM   (YELLOW + (BLUE << 4))
  104. #define COLORREV    (WHITE + (RED << 4))
  105.  
  106. #endif
  107.  
  108. #define MONONORM    0x07
  109. #define MONOREV     0x70
  110.  
  111. #ifdef TC
  112.  
  113. #define cputc putch                     /* easier to understand            */
  114.  
  115. #else
  116.  
  117. #define cprintf printf                  /* CONSOLE I/O doesn't work with   */
  118. #define cputs puts                      /*   MSC 5.x!  Some kind of problem*/
  119. #define cputc putchar                   /*   with reentrancy, I assume     */
  120.  
  121. #endif
  122.  
  123. char savescreen[MAXVIDSIZE + 10];       /* buffer to save screen image     */
  124.                                         /*   includes 10-byte fudge factor */
  125.  
  126. unsigned NormAtt,                       /* Default Normal Attribute        */
  127.          RevAtt,                        /* Default Reverse Attribute       */
  128.          curmode,                       /* Current video mode              */
  129.          pagenum,                       /* Current video page              */
  130.          curtype,                       /* Default cursor type             */
  131.          oldcur,                        /* Old Cursor shape                */
  132.          oldpos;                        /* Old Cursor position             */
  133.  
  134. unsigned maxrows = 25;                  /* Maximum rows on screen          */
  135.  
  136. unsigned char far *biosvid;             /* Pointer to video buffer         */
  137.  
  138.     /*
  139.      * Other global variables
  140.      */
  141. char buffer[10];                        /* work buffer for date/time format*/
  142.  
  143. unsigned idnum,                         /* TSR Identification Number       */
  144.          hours,                         /* Current hour of day             */
  145.          mins,                          /* Current minute of hour          */
  146.          secs,                          /* Current seconds of minute       */
  147.          ticks;                         /* Timer-tick counter              */
  148.  
  149. unsigned BackFlag = 0;                  /* Background flag to signal       */
  150.                                         /*   additional processing         */
  151.  
  152. FILE *fp = NULL;                        /* file structure for file to keep */
  153.                                         /*   open between popups           */
  154.  
  155. #define FLUSHIT(fp) fclose(fp); \
  156.                         fp = fopen("Tessdemo.dat","a+b")
  157.                         
  158.  
  159. #ifdef MSC5
  160.  
  161.         /************************************************************
  162.         * MSC 5.x compatibility routines                            *
  163.         *********************************************************CR*/
  164.  
  165. void gotoxy(unsigned x, unsigned y)     /* position cursor using BIOS      */
  166. {
  167.     union REGS regs;
  168.  
  169.     regs.h.dh = y - 1;
  170.     regs.h.dl = x - 1;
  171.  
  172.     regs.h.bh = 0;
  173.     regs.h.ah = 2;
  174.  
  175.     int86(0x10, ®s, ®s);
  176. }
  177.  
  178. void textmode(unsigned mode)            /* set video mode using BIOS       */
  179. {
  180.     union REGS regs;
  181.  
  182.     regs.h.ah = 0;
  183.     regs.h.al = mode;
  184.  
  185.     int86(0x10, ®s, ®s);
  186. }
  187.  
  188. unsigned currentattr = 0x07,            /* only needed for MSC5            */
  189.          ulc = 0, 
  190.          lrc = 0x184f;
  191.  
  192.                                         /* set local variables             */
  193. void window(int left, int top, int right, int bottom)
  194. {
  195.     ulc = ((top - 1) << 8) + (left - 1);
  196.     lrc = ((bottom - 1) << 8) + (right - 1);
  197. }
  198.  
  199. void textattr(unsigned attr)
  200. {
  201.     currentattr = attr;                 /* we don't change atts with MSC   */
  202. }
  203.  
  204. void clrscr(void)                       /* clear active window area        */
  205. {
  206.     union REGS regs;
  207.  
  208.     regs.x.ax = 0x0600;
  209.     regs.h.bh = currentattr;
  210.  
  211.     regs.x.cx = ulc;
  212.     regs.x.dx = lrc;
  213.  
  214.     int86(0x10, ®s, ®s);
  215. }
  216.  
  217.                                         /* get text from video to buffer   */
  218. int gettext(int left, int top, int right, int bottom, char *buffer)
  219. {
  220.     int i,j;
  221.     char far *video;
  222.  
  223.     for(i = top - 1; i < bottom; i++)
  224.     {
  225.         FP_SEG(video) = FP_SEG(biosvid);
  226.         FP_OFF(video) = ((i * 80) + (left - 1) ) * 2;
  227.  
  228.         for(j = left - 1; j < right; j++)
  229.         {
  230.             *buffer++ = *video++;
  231.             *buffer++ = *video++;
  232.         }
  233.     }
  234.     return(0);
  235. }
  236.  
  237.                                         /* put text from buffer into video */
  238. int puttext(int left, int top, int right, int bottom, char *buffer)
  239. {
  240.     int i,j;
  241.     char far *video;
  242.  
  243.     for(i = top - 1; i < bottom; i++)
  244.     {
  245.         FP_SEG(video) = FP_SEG(biosvid);
  246.         FP_OFF(video) = ((i * 80) + (left - 1) ) * 2;
  247.  
  248.         for(j = left - 1; j < right; j++)
  249.         {
  250.             *video++ = *buffer++;
  251.             *video++ = *buffer++;
  252.         }
  253.     }
  254.     return(0);
  255. }
  256.  
  257. int wherex(void)                        /* get current x coordinate        */
  258. {
  259.     union REGS regs;
  260.  
  261.     regs.h.ah = 0x03;
  262.     regs.h.bh = 0;
  263.  
  264.     int86(0x10, ®s, ®s);
  265.  
  266.     return(regs.h.dl + 1);
  267. }
  268.  
  269. int wherey(void)                        /* get current y coordinate        */
  270. {
  271.    union REGS regs;
  272.  
  273.     regs.h.ah = 0x03;
  274.     regs.h.bh = 0;
  275.  
  276.     int86(0x10, ®s, ®s);
  277.  
  278.     return(regs.h.dh + 1);
  279. }
  280. #define bioskey(c) _bios_keybrd((c))
  281.  
  282. #endif
  283.         /************************************************************
  284.         *  Video Support Routines                                   *
  285.         *********************************************************CR*/
  286.  
  287. void c_str(int row, char *str)          /* Print a string, centered        */
  288. {
  289.     unsigned wid;                       /* temporary width variable        */
  290.  
  291.     wid = (80 - strlen(str))/2;         /* calculate cursor position       */
  292.  
  293.     gotoxy(wid,row);                    /* go there                        */
  294.  
  295.     cputs(str);                         /* display the string              */
  296. }
  297.  
  298. void SaveCursor(void)                   /* save current cursor size and    */
  299. {                                       /*   position                      */
  300. #ifdef MSC5
  301.  
  302. #define MONO 7
  303.  
  304.     union REGS regs;
  305.  
  306.     regs.h.ah = 3;
  307.     regs.h.bh = 0;
  308.  
  309.     int86(0x10, ®s, ®s);
  310.  
  311.     oldpos = regs.x.dx;
  312.     oldcur = regs.x.cx;
  313.  
  314. #else
  315.  
  316.     _AH = 3;                            /* Get Cursor Position             */
  317.     _BH = 0;
  318.     _VideoInt();
  319.  
  320.     oldpos = _DX;                       /* Save return values              */
  321.     oldcur = _CX;
  322.  
  323. #endif
  324.                                         /* known bug on some monochrome    */
  325.                                         /*   adapters reports the wrong    */
  326.                                         /*   cursor shape when both color  */
  327.                                         /*   and monochrome systems are    */
  328.                                         /*   installed.                    */
  329.     if( (curmode == MONO) && (oldcur == 0x0607) )
  330.         oldcur = 0x0c0d;
  331.  
  332.     NoCursor();                         /* Make cursor hidden              */
  333. }
  334.  
  335. void RestoreCursor(void)                /* restore saved cursor position   */
  336. {                                       /*   and size                      */
  337. #ifdef MSC5
  338.  
  339.     union REGS regs;
  340.  
  341.     regs.h.ah = 2;
  342.     regs.h.bh = 0;
  343.     regs.x.dx = oldpos;
  344.  
  345.     int86(0x10, ®s, ®s);
  346.  
  347.     regs.h.ah = 1;
  348.     regs.h.bh = 0;
  349.     regs.x.cx = oldcur;
  350.  
  351.     int86(0x10, ®s, ®s);
  352.  
  353. #else
  354.  
  355.     _AH = 2;                            /* restore saved position          */
  356.     _BH = 0;
  357.     _DX = oldpos;
  358.     _VideoInt();
  359.  
  360.     _AH = 1;                            /* restore saved cursor type       */
  361.     _BH = 0;
  362.     _CX = oldcur;
  363.     _VideoInt();
  364.  
  365. #endif
  366. }
  367.  
  368. void NoCursor(void)                     /* turn off cursor                 */
  369. {
  370. #ifdef MSC5
  371.  
  372.     union REGS regs;
  373.  
  374.     regs.h.ah = 1;
  375.     regs.x.cx = 0xf0f0;
  376.  
  377.     int86(0x10, ®s, ®s);
  378.  
  379. #else
  380.  
  381.     _AH = 1;
  382.     _CX = 0xf0f0;
  383.     _VideoInt();
  384.  
  385. #endif
  386. }
  387.  
  388. void BigCursor(void)                    /* use block cursor                */
  389. {
  390. #ifdef MSC5
  391.  
  392.     union REGS regs;
  393.  
  394.     regs.h.ah = 1;
  395.     regs.x.cx = curtype;
  396.  
  397.     int86(0x10, ®s, ®s);
  398. #else
  399.  
  400.     _AH = 1;
  401.     _CX = curtype;
  402.     _VideoInt();
  403.  
  404. #endif
  405. }
  406.  
  407. void GetVideoMode(void)
  408. {
  409. #ifdef MSC5
  410.  
  411.     union REGS regs;
  412.  
  413.     regs.h.ah = 0x0f;
  414.  
  415.     int86(0x10, ®s, ®s);
  416.  
  417.     curmode = regs.h.al;
  418.     pagenum = regs.h.bh;
  419.  
  420. #else
  421.  
  422.     _AH = 0x0f;
  423.     _VideoInt();
  424.  
  425.     curmode = _AL;
  426.     pagenum = _BH;
  427.  
  428. #endif
  429. }
  430.  
  431. void fixrows(void)                      /* Re-initialize current video     */
  432. {                                       /*   information for new instance  */
  433.                                         /*   of video usage                */
  434. #ifdef TC
  435.     extern char _video[];               /* Undocumented Video Data region  */
  436.     extern void _crtinit(int newmode);  /* Internal Initialization Routine */
  437. #endif
  438.  
  439. #ifdef MSC5
  440. #define BW40 0
  441. #define C40  1
  442. #define BW80 2
  443. #define C80  3
  444.  
  445.     GetVideoMode();
  446.  
  447.     maxrows = (*((unsigned char far *)0x484) + 1);
  448.     if(maxrows < 25)
  449.         maxrows = 25;
  450.  
  451. #else
  452.     _crtinit(curmode);                  /* re-initialize video subsystem   */
  453.  
  454.     maxrows = _video[7];
  455. #endif
  456.  
  457.     switch(curmode)                     /* deal with text mode             */
  458.     {
  459.         case BW40:
  460.             textmode(BW80);             /* we need 80 columns              */
  461.         case BW80:
  462.         case MONO:
  463.             NormAtt = MONONORM;         /* use Monochrome Attributes       */
  464.             RevAtt = MONOREV;
  465.             break;
  466.         case C40:
  467.             textmode(C80);              /* we need 80 columns              */
  468.         case C80:
  469.             NormAtt = COLORNORM;        /* use Color attributes            */
  470.             RevAtt = COLORREV;
  471.             break;
  472.     }
  473.  
  474.  
  475.     if(curmode == MONO)                 /* If monochrome ....              */
  476.     {
  477. #ifdef MSC5
  478.         FP_SEG(biosvid) = 0xb000;
  479.         FP_OFF(biosvid) = 0;
  480. #else
  481.         biosvid = MK_FP(0xb000,0);      /* ... set pointer and cursor      */
  482. #endif
  483.         curtype = 0x000d;
  484.     }
  485.     else                                /* That means color ....           */
  486.     {
  487. #ifdef MSC5
  488.         FP_SEG(biosvid) = 0xb800;
  489.         FP_OFF(biosvid) = 0;
  490. #else
  491.         biosvid = MK_FP(0xb800,0);      /* ... so set pointer and cursor   */
  492.         curtype = 0x0007;
  493. #endif
  494.     }
  495.  
  496. }
  497.  
  498.  
  499. /*****************************< main          >******************************
  500. *                                                                           *
  501. *                         main routine of C program                         *
  502. *                         -------------------------                         *
  503. *                                                                           *
  504. *   Simple-minded main.  Calculates top of background stack region,         *
  505. *       sets the stack points for the TSR; tests to see if we are already   *
  506. *       resident; if so, displays ID number and exits.  If it is OK         *
  507. *       to install it goes resident with DoTsrInit(). Note that InitTsrDemo *
  508. *       is called by TsrCleanUp().                                          *
  509. *                                                                           *
  510. *   Parameters:                                                             *
  511. *       none                                                                *
  512. *                                                                           *
  513. *   Returns:                                                                *
  514. *       none                                                                *
  515. *                                                                           *
  516. *************************************************************************CR*/
  517.  
  518.  
  519. struct ExtraHot MyKeys[2] = {
  520.     { TSRHOT_X, TSRPOPALT, 1 },
  521.     { TSRHOT_Y, TSRPOPCTRL, 2 }
  522.     };
  523.  
  524. void main(void)
  525. {
  526.     char far *stackptr1,                /* Pointer to top of Popup Stack   */
  527.          far *stackptr2;                /* Pointer to top of Background    */
  528.                                         /*   stack area                    */
  529. #ifdef MSC5
  530.     extern unsigned _atopsp;            /* undocumented offset of top of   */
  531.                                         /*   MSC5 stack area               */
  532.     extern void *end;                   /* undocumented base of            */
  533.                                         /*   MSC5 stack area               */
  534.     extern unsigned pascal STKHQQ;      /* undocumented offset of base of  */
  535.                                         /*   MSC5 stack area (plus fudge)  */
  536.     struct SREGS sregs;
  537. #else
  538.     extern unsigned __heapbase,         /* undocumented offset of base of  */
  539.                                         /*   TC 1.5 heap area              */
  540.                     _heaplen,           /* size of heap                    */
  541.                     _stklen;            /* size of stack                   */
  542. #endif
  543.  
  544. #ifdef MSC5
  545.  
  546.     segread(&sregs);
  547.  
  548.     FP_SEG(stackptr2) = sregs.ds;
  549.     FP_OFF(stackptr2) = (unsigned)end + ((_atopsp - STKHQQ) / 2);
  550.  
  551.     FP_SEG(stackptr1) = sregs.ds;
  552.     FP_OFF(stackptr1) = _atopsp;
  553.  
  554. #else
  555.     stackptr1 = MK_FP(_DS, __heapbase + _heaplen + (_stklen / 2) - 16);
  556.     stackptr2 = MK_FP(_DS, __heapbase + _heaplen + _stklen - 16);
  557. #endif
  558.  
  559.     TsSetStack(stackptr1, stackptr2);   /* Set Popup Stack to stackptr1    */
  560.                                         /*   background stack to stackptr2 */
  561.  
  562.  
  563.                                         /* Are we already here?            */
  564. #ifdef MSC5
  565.     if(TsCheckResident("TESSMSC ",&idnum) == 0xffff)
  566. #else
  567.     if(TsCheckResident("TESSDEMO",&idnum) == 0xffff)
  568. #endif
  569.     {
  570.                                         /* Yep!                            */
  571.         puts("The TesSeRact Demonstration TSR has already been loaded");
  572.  
  573.         if(idnum & 0xff00)              /* if released                     */
  574.         {
  575.             puts("  But it is currently waiting to be released from memory");
  576.             puts("  Restarting the TesSeRact Demonstration Program Now");
  577.             TsRestart(idnum & 0x00ff);
  578.         }
  579.         else
  580.         {
  581.             puts("  Use ALT-LeftShift-R to PopUp the TsrMain() routine");
  582.             printf("  Use ID Number %d to communicate through TesSeRact " \
  583.                 "Multiplex functions\n",idnum);
  584.         }
  585.         exit(1);
  586.     }
  587.  
  588.     if(TsCheckHotkey(TSRHOT_R) == 0xffff) /* is hotkey safe?                 */
  589.     {                                   /*  ... nope!                      */
  590.         puts("The TesSeRact Demonstration TSR cannot be loaded because");
  591.         puts("  another TSR currently resident on this system is using");
  592.         puts("  the same hotkey!");
  593.         exit(1);
  594.     }
  595. #ifdef NOTSR
  596.     TsrMain();                          /* Test to call TsrMain            */
  597.     bioskey(0);
  598. #else
  599.  
  600.     if( TsDoInit(
  601.         TSRHOT_R,
  602.         TSRPOPALT + TSRPOPLSHIFT,
  603.         TSRUSEPOPUP + TSRUSEBACK + TSRUSETIMER + TSRUSEUSER + NOPOPGRAPH,
  604.         SizeOfCode(ALLSTACK)) )
  605.         puts("Bad DoInit\n");
  606. #endif
  607.  
  608. }
  609.  
  610. /*****************************< SizeOfCode    >******************************
  611. *                                                                           *
  612. *                 Determine size of program to keep resident                *
  613. *                 ------------------------------------------                *
  614. *                                                                           *
  615. *   This function is an example of a function that can be used to determine *
  616. *       the size of the TSR that is to remain resident.  There are three    *
  617. *       options to this function -- NOHEAP, ALLHEAP, and ALLSTACK.  ALLHEAP *
  618. *       and ALLSTACK are identical with MSC 5.0 -- the stack is below       *
  619. *       the heap, and the stack will be part of the NOHEAP version as well. *
  620. *       In Turbo C 1.5, with the stack ABOVE the heap in tiny and small     *
  621. *       models, we can keep part of the heap, but drop off the stack.       *
  622. *       Example code is shown for both MSC 5 and TC; other compilers and    *
  623. *       langauges can determine the appropriate info as well.               *
  624. *                                                                           *
  625. *   Parameters:                                                             *
  626. *       type             NOHEAP, ALLHEAP or ALLSTACK parameters define above*
  627. *                                                                           *
  628. *   Returns:                                                                *
  629. *       Number of 16-byte paragraphs of memory to keep when going resident. *
  630. *                                                                           *
  631. *************************************************************************CR*/
  632.  
  633. unsigned SizeOfCode(unsigned type)
  634. {
  635. #ifdef  MSC5
  636.     unsigned int far *PSP;              /* far pointer to PSP              */
  637.     extern unsigned _psp,               /* segment of PSP                  */
  638.                     _atopsp;            /* undocumented offset of top of   */
  639.                                         /*   MSC 5.0 stack                 */
  640. #endif  /* End of MSC5 */
  641. #ifdef  TC
  642.     extern unsigned _psp,               /* segment address of PSP          */
  643.                     __heapbase,         /* undocumented offset of base of  */
  644.                                         /*   TC 1.5 heap area              */
  645.                     _heaplen,           /* size of heap                    */
  646.                     _stklen;            /* size of stack                   */
  647. #endif  /* End of TC */
  648.  
  649.     unsigned used;                      /* variable to save paragraphs     */
  650.     struct SREGS sregs;                 /* segment register structure      */
  651.  
  652.     segread(&sregs);                    /* read the segment regs           */
  653.  
  654.     switch(type)
  655.     {
  656.         case ALLSTACK:
  657. #ifdef TC
  658.             used = (((__heapbase + 16 + _heaplen + _stklen) >> 4) + sregs.ds) - _psp;
  659.             break;
  660. #endif
  661.         case ALLHEAP:
  662. #ifdef MSC5
  663.             FP_SEG(PSP) = _psp;         /* segment address of psp          */
  664.             FP_OFF(PSP) = 0;            /* offset of the psp is zero       */
  665.             used = *(PSP+1) - _psp;     /* number of paras used by program */
  666. #endif
  667. #ifdef TC
  668.             used = (((__heapbase + 16 + _heaplen) >> 4) + sregs.ds) - _psp;
  669. #endif
  670.             break;
  671.         case NOHEAP:
  672. #ifdef  MSC5
  673.             used = (((_atopsp + 16) >> 4) + sregs.ds) - _psp;
  674. #endif  /* End of MSC5 */
  675. #ifdef  TC
  676.             used = (((__heapbase + 16) >> 4) + sregs.ds) - _psp;
  677. #endif  /* End of TC */
  678.             break;
  679.     }
  680.  
  681.     return(used);                       /* return number of paragraphs     */
  682. }
  683.  
  684. /*****************************< do_cpyrt      >******************************
  685. *                                                                           *
  686. *                       Display Copyright Information                       *
  687. *                       -----------------------------                       *
  688. *                                                                           *
  689. *   Function to display formatted copyright information on the screen.      *
  690. *                                                                           *
  691. *   Parameters:                                                             *
  692. *       none                                                                *
  693. *                                                                           *
  694. *   Returns:                                                                *
  695. *       none                                                                *
  696. *                                                                           *
  697. *************************************************************************CR*/
  698.  
  699. void do_cpyrt(void)
  700. {
  701.     textattr(RevAtt);
  702.     c_str(2,"The TesSeRact Demonstration Program");
  703.     textattr(NormAtt);
  704.  
  705.     gotoxy(12,4);
  706.     cputs("Copyright 1986, 1987, 1988, TesSeRact Development Team");
  707.  
  708.     gotoxy(12,5);
  709.     cputs("All Rights Reserved");
  710. }
  711.  
  712. /*****************************< DisplayTime   >******************************
  713. *                                                                           *
  714. *                     'Poke' current time into video RAM                    *
  715. *                     ----------------------------------                    *
  716. *                                                                           *
  717. *   Adjusts minutes and seconds, and then pokes the holding buffer into     *
  718. *       the first 8 character bytes of the Video RAM segment.  Note that    *
  719. *       the 'hours' will be adjusted by the AdjustTime function.            *
  720. *                                                                           *
  721. *   Parameters:                                                             *
  722. *       none                                                                *
  723. *                                                                           *
  724. *   Returns:                                                                *
  725. *       none                                                                *
  726. *                                                                           *
  727. *************************************************************************CR***/
  728.  
  729. void DisplayTime(void)
  730. {
  731.     int i;
  732.  
  733.     buffer[0] = (hours / 10) + 0x30;
  734.     buffer[1] = (hours % 10) + 0x30;
  735.  
  736.     buffer[3] = (mins / 10) + 0x30;
  737.     buffer[4] = (mins % 10) + 0x30;
  738.  
  739.     buffer[6] = (secs / 10) + 0x30;
  740.     buffer[7] = (secs % 10) + 0x30;
  741.  
  742.     for(i=0;i<8;i++)
  743.         biosvid[i*2] = buffer[i];
  744. }
  745.  
  746. /*****************************< AdjustTime    >******************************
  747. *                                                                           *
  748. *                     Call DOS to get the current time                      *
  749. *                     --------------------------------                      *
  750. *                                                                           *
  751. *   Calls DOS to get the current time, save it to clobal values, and then   *
  752. *       calls the C runtime sprintf() function to format it into the buffer *
  753. *                                                                           *
  754. *   Parameters:                                                             *
  755. *       none                                                                *
  756. *                                                                           *
  757. *   Returns:                                                                *
  758. *       none                                                                *
  759. *                                                                           *
  760. *************************************************************************CR***/
  761.  
  762. void AdjustTime(void)
  763. {
  764. #ifdef MSC5
  765.     struct dostime_t timep;
  766.  
  767.     _dos_gettime(&timep);
  768.  
  769.     hours = timep.hour;
  770.     mins = timep.minute;
  771.     secs = timep.second;
  772. #else
  773.     struct time timep;
  774.  
  775.     gettime(&timep);
  776.  
  777.     hours = timep.ti_hour;
  778.     mins = timep.ti_min;
  779.     secs = timep.ti_sec;
  780. #endif
  781.  
  782.     sprintf(buffer,"%02d:%02d:%02d",hours,mins,secs);
  783. }
  784.  
  785. /*****************************< InitTsrDemo   >******************************
  786. *                                                                           *
  787. *                      Initialize variables and video                       *
  788. *                      ------------------------------                       *
  789. *                                                                           *
  790. *   This function just initializes everything, displays a sign-on message,  *
  791. *       and gets the clock info for the first time.                         *
  792. *                                                                           *
  793. *   Parameters:                                                             *
  794. *       none                                                                *
  795. *                                                                           *
  796. *   Returns:                                                                *
  797. *       none                                                                *
  798. *                                                                           *
  799. *************************************************************************CR***/
  800.  
  801. void InitTsrDemo(void)
  802. {
  803.     GetVideoMode();                     /* save current mode for later     */
  804.  
  805.     fixrows();
  806.  
  807.     clrscr();
  808.  
  809.     window(1,1,80,8);
  810.     textattr(NormAtt);
  811.     clrscr();
  812.  
  813.     do_cpyrt();
  814.  
  815.     c_str(7,"Press Alt-LeftShift-R to activate the TesSeRact " \
  816.         "Demonstration Program\n ");
  817.  
  818.     AdjustTime();
  819.     DisplayTime();
  820.  
  821.     if(fp == NULL)
  822.     {
  823.         fp = fopen("Tessdemo.dat","a+b");
  824.         fprintf(fp,"TesSeRact Demonstration Program loaded at %s\n\r",buffer);
  825.         FLUSHIT(fp);
  826.     }
  827. }
  828.  
  829.         /************************************************************
  830.         *   TSR Procedures                                          *
  831.         *********************************************************CR*/
  832.  
  833. char *StuffBuf = "\x72\x13\x69\x17\x6e\x31\x67\x22";
  834. unsigned StuffLen = 4;
  835.  
  836. void far pascal TsrMain(void)
  837. {
  838.     unsigned oldstat,curdisk,ret;
  839.     long bypercl, frees, total;
  840.     unsigned char SaveVideoPageNum = 0;
  841.     struct TsrParms far *ParmsPtr;
  842. #ifdef MSC5
  843.     struct diskfree_t diskfree;
  844.     union REGS regs;
  845. #else
  846.     struct dfree diskfree;
  847. #endif
  848.  
  849.     GetVideoMode();                     /* save current mode for later     */
  850.  
  851.     SaveCursor();
  852.  
  853.     if(pagenum)
  854.     {
  855.         SaveVideoPageNum = pagenum;
  856.  
  857. #ifdef MSC5
  858.         regs.x.ax = 0x0500;
  859.         int86(0x10, ®s, ®s);
  860. #else
  861.         _AX = 0x0500;
  862.         _VideoInt();
  863. #endif
  864.     }
  865.  
  866.     fixrows();
  867.  
  868.     window(1,1,80,maxrows);
  869.     gettext(1,1,80,maxrows,savescreen);
  870.  
  871.     textattr(NormAtt);
  872.     clrscr();
  873.  
  874.     do_cpyrt();
  875.  
  876.     ParmsPtr = TsGetParms(idnum);
  877.  
  878.     oldstat = TsGetStat(idnum);
  879.  
  880.     gotoxy(5,7);
  881.     cprintf("This TSR popped up with HotKey #%d, and is using the "
  882.                 "following procedures:", ParmsPtr->HotKeyFlag);
  883.  
  884.     if(oldstat & TSRUSEPOPUP)
  885.     {
  886.         gotoxy(10,wherey()+1);
  887.         cputs("User-Defined PopUp Procedure");
  888.     }
  889.     if(oldstat & TSRUSEBACK)
  890.     {
  891.         gotoxy(10,wherey()+1);
  892.         cputs("User-Defined Background Procedure");
  893.     }
  894.     if(oldstat & TSRUSETIMER)
  895.     {
  896.         gotoxy(10,wherey()+1);
  897.         cputs("User-Defined Timer Procedure");
  898.     }
  899.     if(oldstat & TSRUSEUSER)
  900.     {
  901.         gotoxy(10,wherey()+1);
  902.         cputs("User-Defined User Communication Procedure");
  903.     }
  904.  
  905. #ifdef MSC5
  906.  
  907.     _dos_getdrive(&curdisk);
  908.  
  909.     _dos_getdiskfree(curdisk, &diskfree);
  910.  
  911.     bypercl = (long)(diskfree.bytes_per_sector * diskfree.sectors_per_cluster);
  912.     frees = bypercl * diskfree.avail_clusters;
  913.     total = bypercl * diskfree.total_clusters;
  914.  
  915. #else
  916.  
  917.     curdisk = getdisk() + 1;
  918.  
  919.     getdfree(curdisk, &diskfree);
  920.  
  921.     bypercl = (long)(diskfree.df_bsec * diskfree.df_sclus);
  922.     frees = bypercl * diskfree.df_avail;
  923.     total = bypercl * diskfree.df_total;
  924.  
  925. #endif
  926.  
  927.     gotoxy(5,19);
  928.     cprintf("Current disk is %c:, with %ld bytes available, %ld total bytes",
  929.         curdisk + 0x40, frees, total);
  930.  
  931.     fprintf(fp,"TesSeRact Demonstration Program popped up at %s\n\r",buffer);
  932.     FLUSHIT(fp);
  933.  
  934.     gotoxy(5,21);
  935.     cprintf("This TSR is called %-8.8Fs, and has a PSP at segment %04x",
  936.         ParmsPtr->IdCode, ParmsPtr->TsrPSP);
  937.  
  938.     gotoxy(25,22);
  939.     cprintf("Supported functions are %lx", ParmsPtr->FuncFlags);
  940.     
  941.     c_str(24,"Press 'R' to remove TSR from RAM; 'K' to stuff keyboard; any other key to exit");
  942.  
  943.     ret = bioskey(0) & 0xff;
  944.     if(toupper(ret) == 'R')
  945.         TsRelease(idnum);
  946.     else
  947.         if(toupper(ret) == 'K')
  948.             TsStuffKeyboard(idnum, StuffBuf, StuffLen, STUFF_FAST);
  949.     puttext(1,1,80,maxrows,savescreen);
  950.  
  951.     if(SaveVideoPageNum)
  952.     {
  953. #ifdef MSC5
  954.         regs.h.ah = 0x05;
  955.         regs.h.al = SaveVideoPageNum;
  956.         int86(0x10, ®s, ®s);
  957. #else
  958.         _AH = 0x05;                     /* Routine to restore correct      */
  959.         _AL = SaveVideoPageNum;         /*   video page; courtesy of Bruce */
  960.         _VideoInt();                    /*   Kitchin                       */
  961. #endif
  962.     }
  963.  
  964.     RestoreCursor();
  965. }
  966.  
  967. unsigned far pascal TsrBackCheck(void)
  968. {
  969.     return(BackFlag);
  970. }
  971.  
  972. void far pascal TsrBackProc(void)
  973. {
  974.     AdjustTime();
  975.     fprintf(fp,"TesSeRact Demonstration Program adjusted time at %s\n\r",buffer);
  976.     FLUSHIT(fp);
  977.     BackFlag = 0;
  978. }
  979.  
  980. void far pascal TsrTimerProc(void)
  981. {
  982.     if(++ticks > 18)
  983.     {
  984.         ticks = 0;                      /* always clear ticks if > 18      */
  985.         secs++;
  986.         switch(secs)
  987.         {
  988.             case 60:
  989.                 secs = 0;               /* reset ticks for display & count */
  990.                 if(++mins > 59)         /* inc mins                        */
  991.                 {
  992.                     mins = 0;           /* flip the minutes                */
  993.                     if(++hours > 23)    /* update the hours                */
  994.                         hours = 0;
  995.                 }
  996.                 BackFlag = 1;
  997.                 break;
  998.             case 20:
  999.             case 40:
  1000.                 secs++;                 /* fudge for approx ticks          */
  1001.                 break;
  1002.         }
  1003.         DisplayTime();                  /* always display time!            */
  1004.     }
  1005. }
  1006.  
  1007. void far pascal TsrUserProc(void far *UserPtr)
  1008. {
  1009.     printf("This is the user procedure:  Passed ptr = %Fs\n",UserPtr);
  1010. }
  1011.  
  1012. void far pascal TsrCleanUp(unsigned InitOrShutdown)
  1013. {
  1014. #ifdef TC
  1015.     extern void _restorezero(void);
  1016. #else
  1017.     extern void _ctermsub(void);
  1018. #endif  /* End of  */
  1019.  
  1020.     if(InitOrShutdown)      /* if we're shutting down          */
  1021.     {
  1022.         if(fp != NULL)
  1023.         {
  1024.             fprintf(fp,"TesSeRact Demonstration Program "
  1025.                 "released at %s\n\r",buffer);
  1026.             fclose(fp);
  1027.         }
  1028.     /*
  1029.      * Please note that it is *absolutely* vital to call _restorezero()
  1030.      *   or _ctermsub() at this point -- otherwise, the INT 0 vector
  1031.      *   is not restored, and a divide-by-zero exception will cause
  1032.      *   a crash, rather than a clean exit.  Note that this routine
  1033.      *   is compiler-dependent .... CR
  1034.      */
  1035.  
  1036. #ifdef TC
  1037.         _restorezero();
  1038. #else
  1039.         _ctermsub();
  1040. #endif
  1041.  
  1042.     }
  1043.     else
  1044.     {
  1045.         TsSetExtraHot(idnum, 2, MyKeys);
  1046.         InitTsrDemo();
  1047.     }
  1048. }
  1049.  
  1050.