home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sniff16.zip / TESTAPP / WRMHELP.C < prev    next >
C/C++ Source or Header  |  1993-03-12  |  11KB  |  333 lines

  1. /*=========================================================================\
  2.  *                                                                         *
  3.  *       FILE:wrmhelp.c                                                    *
  4.  *                                                                         *
  5.  *       DESCRIPTION:                                                      *
  6.  *                                                                         *
  7.  *                                                                         *
  8.  *                                                                         *
  9.  *      Copyright 1991 , IBM Corp.                                         *
  10.  *                                                                         *
  11.  *-------------------------------------------------------------------------*
  12.  *                                                                         *
  13.  *                                                                         *
  14.  *                                                                         *
  15.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is        *
  16.  *      sample code created by IBM Corporation. This sample code is not    *
  17.  *      part of any standard or IBM product and is provided to you solely  *
  18.  *      for  the purpose of assisting you in the development of your       *    *
  19.  *      applications.  The code is provided "AS IS", without               *
  20.  *      warranty of any kind.  IBM shall not be liable for any damages     *
  21.  *      arising out of your use of the sample code, even if they have been *
  22.  *      advised of the possibility of   such damages.                      *                               *
  23.  *                                                                         *
  24.  *                                                                         *
  25.  *                                                                         *
  26.  *                                                                         *
  27.  *                                                                         *
  28.  *                                                                         *
  29.  *                                                                         *
  30.  *                                                                         *
  31.  *--------------------------------------------------------------------------
  32.  *
  33.  *  This source file contains the following functions:
  34.  *
  35.  * WormHelp
  36.  * SuspendAllThreads
  37.  * SaveScreen
  38.  * ResumeAllThreads
  39.  * RedisplayScreen
  40. \*==============================================================*/
  41.  
  42. /*--------------------------------------------------------------*\
  43.  *  Include files, macros, defined constants, and externs
  44. \*--------------------------------------------------------------*/
  45. #define  INCL_DOSMEMMGR
  46. #define  INCL_KBD
  47. #define  INCL_VIO
  48. #define  INCL_MOU
  49. #define  INCL_DOSPROCESS
  50. #define  INCL_DOSSEMAPHORES
  51. #include <os2.h>
  52. #include <string.h>
  53. #include "worms.h"
  54. #include "wrmthrd.h"
  55. #include "wrmhelp.h"
  56.  
  57. /*--------------------------------------------------------------*\
  58.  *  Global variables  and definitions for this file
  59. \*--------------------------------------------------------------*/
  60.  
  61. CHAR *pszHelp =
  62. "                                                                              \
  63.                                                                                 \
  64.                         Worms  Mixed Mode Overview                              \
  65.                                                                                 \
  66.                                                                                 \
  67.      This program is a sample application   intended to demonstrate calling     \
  68.      16 - bit API's from 32 bit code.   The Vio, Mou, and KBD subsystem are     \
  69.      used for this example. Each thread is started with a unique color code     \
  70.      and  will  randomly  move  about the screen. Each time another worm is     \
  71.      encountered  the  worms  color  will  change to red, then revert back.     \
  72.                                                                                 \
  73.                         Worms Mixed Mode Help                                   \
  74.                                                                                 \
  75.      Additional threads (Worms) may be started by entering 'A' at the keyboard. \
  76.      Worms may be Deleted by entering 'D' at the keyboard. The mouse may also   \
  77.      be used to add worms or delete worms by using the mouse buttons.           \
  78.      To exit from the program enter 'E'.                                        \
  79.                                                                                 \
  80.      From the command line the following options may be  specified.             \
  81.                                                                                 \
  82.                                                                                 \
  83.       worms -s         Silent Mode                                              \
  84.       worms -x         Where x  is a value from 1 to 30 indicating the number   \
  85.                        of worms to start of with. Default is 1.                 \
  86.                                                                                 \
  87.                                                                                 \
  88.                        Copyright     IBM Corp.   1992                           \
  89.                                                                                 \
  90.                                                                                 \
  91.                                                                                 \
  92. ";
  93.  
  94. PVOID pvSaveWindBuf;
  95.  
  96.  
  97. /*--------------------------------------------------------------*\
  98.  *  Entry point declarations
  99. \*--------------------------------------------------------------*/
  100.  
  101. /****************************************************************\
  102.  *
  103.  *--------------------------------------------------------------
  104.  *
  105.  *  Name:WormHelp()
  106.  *
  107.  *  Purpose: This routine displays the help screen for the
  108.  *           worms program.
  109.  *
  110.  *
  111.  *  Usage:
  112.  *
  113.  *  Method:    This routine is the main flow control for the help
  114.  *          -  routine. The other threads will be suspended,the
  115.  *             current screen is saved in a allocated buffer,
  116.  *          -  display the help screen, wait for a keystroke,
  117.  *          -  restore the old screen,resume child threads and
  118.  *             free buffer.
  119.  *          -
  120.  *          -
  121.  *
  122.  *  Returns:
  123.  *          TRUE    - Error Occurred.
  124.  *          FALSE     No Error Occurred
  125. \****************************************************************/
  126. BOOL
  127. WormHelp(VOID )
  128. {
  129.  
  130.     KBDKEYINFO    kbdkeyinfo;
  131.     PTIB   ptib ;
  132.     PPIB   ppib  ;
  133.     ULONG  ulPosts;
  134.  
  135.     bHelpEnabled  = TRUE;
  136.  
  137.      /*
  138.       *freeze all worms
  139.       */
  140.       if(SuspendAllThreads() )
  141.       {
  142.           return(TRUE);
  143.       }
  144.  
  145.      /*
  146.       *read the screen
  147.       *and save it
  148.       */
  149.       if(SaveScreen(&pvSaveWindBuf) )
  150.       {
  151.           return(TRUE);
  152.       }
  153.  
  154.      /*
  155.       *display the help screen
  156.       */
  157.       DisplayHelp();
  158.  
  159.       DosGetInfoBlocks(&ptib,&ppib);
  160.       /*
  161.        *if we are not
  162.        *calling from thread
  163.        *one suspend thread,
  164.        *so only one thread at a time
  165.        *is hitting the keyboard queue
  166.        */
  167.       if(ptib->tib_ptib2->tib2_ultid != 1 )
  168.       {
  169.           DosWaitEventSem(hevWormSem,SEM_INDEFINITE_WAIT);
  170.           DosResetEventSem(hevWormSem,&ulPosts);
  171.       }
  172.       else
  173.       {
  174.          /*
  175.           *wait for any
  176.           *response if thread 1
  177.           */
  178.           KbdCharIn(&kbdkeyinfo,IO_WAIT,
  179.                          0) ;
  180.       }
  181.  
  182.  
  183.  
  184.      /*
  185.       *redisplay the saved screen
  186.       */
  187.       ReDisplayScreen(pvSaveWindBuf);
  188.  
  189.  
  190.  
  191.      /*
  192.       *allow all child threads to resume
  193.       */
  194.      bHelpEnabled  = FALSE;
  195.  
  196.  
  197.      ResumeAllThreads();
  198.      return(FALSE);
  199.  
  200. }
  201. /****************************************************************\
  202.  *
  203.  *--------------------------------------------------------------
  204.  *
  205.  *  Name:SuspendAllThreads()
  206.  *
  207.  *  Purpose: This routine allow the calling thread to force the
  208.  *           other threads that are drawing to the screen to wait
  209.  *           until this thread is done. The worm (threads) will block
  210.  *           on the hevDrawSem until it is cleared again. The calling
  211.  *           thread will wait until the post count on the hevDrawOK
  212.  *           sem is equal to the number of worms before returnning.
  213.  *
  214.  *  Returns:
  215.  *          FALSE - if sucessful execution completed
  216.  *          TRUE  - if error
  217. \****************************************************************/
  218. BOOL
  219. SuspendAllThreads(VOID )
  220. {
  221.      BOOL fError = FALSE;
  222.      ULONG ulPosts =0L;
  223.  
  224.      fDraw = FALSE;
  225.  
  226.      /*
  227.       *make sure we don't get
  228.       *pre-empted
  229.       */
  230.      DosEnterCritSec();
  231.      DosResetEventSem(hevDrawOk,&ulPosts);
  232.      ulPosts = 0;
  233.  
  234.      DosResetEventSem(hevDrawSem,&ulPosts);
  235.      ulPosts = 0;
  236.      DosExitCritSec();
  237.      /*
  238.       *wait till all of the
  239.       *threads have
  240.       *posted
  241.       */
  242.      while(ulPosts < sCurrentThread)
  243.      {
  244.           DosWaitEventSem(hevDrawOk,SEM_INDEFINITE_WAIT);
  245.           DosQueryEventSem(hevDrawOk,&ulPosts);
  246.  
  247.      }
  248.  
  249.      return(fError );
  250.  
  251. }
  252. /****************************************************************\
  253.  *
  254.  *--------------------------------------------------------------
  255.  *
  256.  *  Name: DisplayHelp()
  257.  *
  258.  *  Purpose:
  259.  *
  260.  *
  261.  *
  262.  *  Usage:
  263.  *
  264.  *  Method:
  265.  *          -
  266.  *
  267.  *          -
  268.  *          -
  269.  *
  270.  *          -
  271.  *          -
  272.  *
  273.  *  Returns:
  274.  *         TRUE   - Error occurred
  275.  *         FALSE    No error occurred
  276. \****************************************************************/
  277. BOOL
  278. DisplayHelp(VOID )
  279. {
  280.      BYTE bCell[2];
  281.      BYTE bhAttr =  MKATRB(WM_PALEGRAY,WM_BLUE);
  282.  
  283.  
  284.      bCell[0] = 0x20;
  285.      bCell[1] = MKATRB(WM_PALEGRAY,WM_BLUE);
  286.  
  287.  
  288.      VioScrollDn(0,0,0xFFFF,0xFFFF,0xFFFF,bCell,hvio);
  289.      VioWrtCharStrAtt(pszHelp,strlen(pszHelp),
  290.                               TOP_ROW,LEFT_COL,&bhAttr,hvio );
  291. }
  292.  
  293.  
  294. /****************************************************************\
  295.  *
  296.  *--------------------------------------------------------------
  297.  *
  298.  *  Name:ResumeAllThreads()
  299.  *
  300.  *  Purpose:
  301.  *
  302.  *
  303.  *
  304.  *  Usage:
  305.  *
  306.  *  Method:
  307.  *          -
  308.  *
  309.  *          -
  310.  *          -
  311.  *
  312.  *          -
  313.  *          -
  314.  *
  315.  *  Returns:
  316.  *          FALSE - is always returned
  317. \****************************************************************/
  318. BOOL
  319. ResumeAllThreads(VOID )
  320. {
  321.  
  322.      fDraw = TRUE;
  323.      DosPostEventSem(hevDrawSem);
  324.      return( FALSE );
  325. }
  326. /*--------------------------------------------------------------*\
  327.  *  End of file :wrmhelp.c
  328. \*--------------------------------------------------------------*/
  329.  
  330.  
  331.  
  332.  
  333.