home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / FILEMAN / TV0001 / HOST_DIR.ZIP / tv.cpp < prev    next >
C/C++ Source or Header  |  1994-04-12  |  6KB  |  221 lines

  1.  
  2. #define INCL_DOSPROCESS
  3. #define INCL_DOSMEMMGR
  4.  
  5. #include <os2.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <process.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "win.h"
  12. #include "keybd.h"
  13.  
  14. #define MAXWIN 10
  15.  
  16. /************************************************ globals */
  17. char *ScreenData[ MAXWIN ];                        // holds screen client info
  18. char *KeyData[ MAXWIN ];                            // holds keyboard buffers
  19. WindowClass Window[ MAXWIN ];                        // make window classes
  20. WindowClass *foreground = NULL;                    // pointer to FG window
  21. int nFG = 0;                                            // index of fg window
  22. int INUSE[ MAXWIN ];
  23.  
  24. /*********************************************** init shared memory */
  25. void initshared() {
  26.     int count;
  27.     char memname[ 50 ];                                // holds memory name
  28.  
  29.     for ( count = 0; count < MAXWIN; count++ ) {
  30.         sprintf( memname, "WINDOW%d", count );
  31.         Window[ count ].settitle( memname );  // set the window name
  32.  
  33.         // allocate share memory for screen gettings
  34.         sprintf( memname, "\\SHAREMEM\\%s", Window[count].gettitle() );
  35.         if ( DosAllocSharedMem( (PPVOID) &ScreenData[count], memname,
  36.                                         4096, PAG_READ | PAG_COMMIT ) ) {
  37.             cprintf( "Shared Mem Error" );
  38.             exit( 1 );
  39.         }
  40.         sprintf( memname, "\\SHAREMEM\\K%s", Window[count].gettitle() );
  41.         if ( DosAllocSharedMem( (PPVOID) &KeyData[count], memname,
  42.                                         10, PAG_READ | PAG_COMMIT | PAG_WRITE ) ) {
  43.             cprintf( "Shared Mem Error" );
  44.             exit( 1 );
  45.         }
  46.  
  47.  
  48.     }// for
  49. }
  50.  
  51.  
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // this function performs a total redraw of the host screen
  55. /////////////////////////////////////////////////////////////////////////////
  56.  
  57. void sysredraw() {                // redraw all windows
  58.     textcolor( 7 );
  59.     textbackground( 0 );
  60.     clrscr();
  61.     int count;
  62.     for ( count = 0; count < MAXWIN; count++ ) {
  63.         if ( INUSE[ count ] )
  64.             Window[ count ].redraw();
  65.     }
  66.  
  67.     foreground -> redraw();
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // this function redraws the foreground window every once in a while so that
  72. // it looks real-time, but isn't so energy (cpu) inefficient
  73. /////////////////////////////////////////////////////////////////////////////
  74.  
  75. void redraw_daemon( void *arglist ) {
  76.     int count;
  77.     while( 1 ) {
  78.     DosSleep( 125 );                                    // sleep a bit
  79.     for ( count = 0; count < MAXWIN; count++ ) {
  80.         Window[ count ].setcurpos( atoi( KeyData[ nFG ] + 3 ), 
  81.                                              atoi( KeyData[ nFG ] + 6 ) );
  82.         Window[ count ].clientdata( ScreenData[ count ] );// redraw client area
  83.     }
  84.  
  85.     foreground -> clientdraw();
  86.     }
  87. }
  88.  
  89. void startsession( int number ) {
  90.     INUSE[ number ] = 1;
  91.     char cmdline[ 75 ];
  92.     sprintf( cmdline, "start /bg /c /fs capd %s", Window[ number ].gettitle() );
  93.     system( cmdline );
  94.     Window[ number ].gofg();
  95. }
  96.  
  97. void sysmenu() {
  98.     textcolor( 14 );
  99.     textbackground( 1 );
  100.     gotoxy( 1, 25 );
  101.     cprintf( " Options:  Move, Resize, Switch, Begin " );
  102.  
  103.     int in = getch();
  104.     switch( in ) {
  105.  
  106.         case 'm':
  107.         case 'M':
  108.              gotoxy( 2, 25 );
  109.             cprintf( "Cursor keys move window.  Enter to commit and redraw." );
  110.             while( 1 ) {
  111.                 int in = getch();
  112.                 if ( in == 0 )                         // function key hit
  113.                     continue;
  114.                 if ( in == RIGHT ) 
  115.                     foreground -> relocate( 1, 0 );
  116.                 if ( in == LEFT )
  117.                     foreground -> relocate( -1, 0 );
  118.                 if ( in == DOWN )
  119.                     foreground -> relocate( 0, 1 );
  120.                 if ( in == UP )
  121.                     foreground -> relocate( 0, -1 );
  122.                 if ( in == 13 )                        // enter key
  123.                     break;
  124.             }
  125.             sysredraw();                                // redraw screen
  126.             break;
  127.  
  128.         case 'r':
  129.         case 'R':                                        // resize
  130.             gotoxy( 2, 25 );
  131.             cprintf( "Cursor keys resize window.  Enter to commit and redraw." );
  132.             while( 1 ) {
  133.                 int in = getch();
  134.                 if ( in == 0 )
  135.                     continue;
  136.                 if ( in == RIGHT ) 
  137.                     foreground -> resize( 1, 0 );
  138.                 if ( in == LEFT )
  139.                     foreground -> resize( -1, 0 );
  140.                 if ( in == UP )
  141.                     foreground -> resize( 0, -1 );
  142.                 if ( in == DOWN )
  143.                     foreground -> resize( 0, 1 );
  144.                 if ( in == 13 )
  145.                     break;
  146.             }
  147.             sysredraw();
  148.             break;
  149.  
  150.         case 'S':
  151.         case 's': {          // switch windows
  152.             gotoxy( 2, 25 );
  153.             cprintf( "Type the window number to switch to ( 0 - 9 )" );
  154.             int in = getch();
  155.             if ( ( ( in - '0' ) <= 9 ) && ( ( in - '0' ) >= 0 ) ) {
  156.                     // ensure we are within range
  157.                 foreground -> gobg();    
  158.                 nFG = ( in - '0' );
  159.                 foreground = &Window[ ( in - '0' ) ];
  160.                 foreground -> gofg();
  161.             }
  162.             sysredraw();
  163.             break;
  164.         }
  165.  
  166.         case 'B':
  167.         case 'b':
  168.             startsession( nFG );
  169.             break;
  170.  
  171.         default:
  172.             sysredraw();
  173.             break;
  174.     }// case
  175. }
  176.  
  177.  
  178. int keyproc() {
  179.     register int counter = 0;
  180.     int in;
  181.     in = getch();
  182.  
  183.     if ( in == 28 ) {                                    // system menu requested
  184.         sysmenu();
  185.         return 1;
  186.     }
  187.                  
  188.     while ( KeyData[ nFG ][0] != '\0' )    {        // wait until memory is clear
  189.         DosSleep( 5 );
  190.         counter++;
  191.         if ( counter > 100 ) {                        // window has stalled
  192.             INUSE[ nFG ] = 0;                                // terminate window
  193.             sysredraw();
  194.             gotoxy( 50, 25 );
  195.             cprintf( "%s has quit responding.", Window[ nFG ].gettitle() );
  196.             sysmenu();
  197.             return 1;
  198.         }
  199.     }
  200.  
  201.     if    ( in == 0 ) {                                    // trap function key
  202.         in = getch();                                    // get the scan code
  203.         KeyData[ nFG ][ 1 ] = 1;                    // let monior know of f-key
  204.     }
  205.     KeyData[ nFG ][ 0 ] = in;
  206.     return 1;
  207. }
  208.  
  209.  
  210. int main() {
  211.     clrscr();
  212.     nFG = 0;
  213.     initshared();
  214.     startsession( 0 );
  215.     foreground = &Window[ 0 ];
  216.     _beginthread( redraw_daemon, 4096, NULL );
  217.     sysredraw();
  218.     while ( keyproc() );
  219.     return 0;
  220. }
  221.