home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / BLANKER.C < prev    next >
C/C++ Source or Header  |  1991-08-12  |  5KB  |  225 lines

  1. /*
  2. * BLANKER.C - Screen saver program.
  3. *
  4. *
  5. * PROGRAMMER:        Martti Ylikoski
  6. * CREATED:        11.7.1991
  7. */
  8. static char *VERSION = "Version  1.0" ;
  9. /*
  10. */
  11.  
  12. static char *progname ;
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <memory.h>
  17. #include <time.h>
  18. #include <process.h>
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. #define INCL_DOS
  22. #define INCL_KBD
  23. #define INCL_VIO
  24. #define INCL_DOSPROCESS
  25. #include <os2.h>
  26.  
  27. typedef struct kbdpacket
  28. {
  29.    unsigned monflags ;
  30.    KBDKEYINFO kbdkeyinfo ;
  31.    unsigned ddflags ;
  32. } KBDBUF ;
  33.  
  34. KBDBUF kbdbuf ;
  35.  
  36. #define STACKSIZE 4096
  37. static char monstack[12][STACKSIZE] ;
  38. static char timestack[STACKSIZE] ;
  39. ULONG ntime = 0L, otime = 0L ;
  40. time_t newtime, oldtime ;
  41. void monitor_th( USHORT tindex ) ;
  42. void time_th( void ) ;
  43. ULONG exitsem = 0L ;
  44. ULONG blanksem = 0L ;
  45. ULONG timesem = 0L ;
  46. static HMONITOR hmon ;
  47. static USHORT tindex ;
  48. static int sleeptime ;
  49. DEFINEMUXSEMLIST(MuxList, 2) ;
  50.  
  51. int main(int argc, char *argv[])
  52. {
  53. TID tid, time_tid ;
  54. USHORT ret ;
  55. USHORT fWait ;
  56. VIOMODEINFO viomodeinfo ;
  57. KBDKEYINFO  kbdkeyinfo ;
  58. int col, row, outf ;
  59. USHORT usSemIndex ;
  60.  
  61.  
  62.    fWait = VP_WAIT | VP_OPAQUE ;
  63.    MuxList.cmxs = 2 ;
  64.    MuxList.amxs[0].zero = 0 ;
  65.    MuxList.amxs[1].zero = 0 ;
  66.  
  67.    MuxList.amxs[0].hsem = &exitsem ;
  68.    MuxList.amxs[1].hsem = &blanksem ;
  69.  
  70.    DosSemSet(MuxList.amxs[0].hsem) ;
  71.    DosSemSet(MuxList.amxs[1].hsem) ;
  72.  
  73.    //DosSemSet(&exitsem) ;
  74.    DosSemClear(×em) ;
  75.    newtime = time(NULL) ;
  76.    oldtime = time(NULL) ;
  77.    if (argc == 1)
  78.    {
  79.       sleeptime = 300 ;
  80.    }
  81.    else
  82.       sleeptime = atoi(argv[1]) ;
  83.  
  84.    if (( ret = DosMonOpen("KBD$", &hmon)) != 0)
  85.    {
  86.       printf("%s: error opening monitor", progname) ;
  87.       return(1) ;
  88.    }
  89.  
  90.    if (( time_tid = _beginthread(time_th, timestack, sizeof(timestack), NULL)) == -1)
  91.  //  if (( ret = DosCreateThread(time_th, &time_tid, timestack + sizeof(monstack) )) != 0)
  92.    {
  93.       printf("%s: error in DosCreateThread", progname) ;
  94.       return( 1 ) ;
  95.    }
  96.  
  97.    for (tindex = 0 ; tindex < 12 ; tindex ++)
  98.       if (( tid = _beginthread(monitor_th, &monstack[tindex][0], sizeof(monstack[0]), tindex+4)) == -1)
  99.       //if (( ret = DosCreateThread(monitor_th, &tid, &monstack[tindex][0] + sizeof(monstack[0]) )) != 0)
  100.       {
  101.      printf("%s: error in DosCreateThread", progname) ;
  102.      return( 1 ) ;
  103.       }
  104.    ;
  105.    while (TRUE)
  106.    {
  107.       DosMuxSemWait(&usSemIndex, &MuxList, SEM_INDEFINITE_WAIT) ;
  108.  
  109.       if (usSemIndex == 0)
  110.      break ;
  111.  
  112.  
  113.       //DosSemWait(&exitsem, SEM_INDEFINITE_WAIT) ;
  114.       ret = DosSuspendThread(tid) ;
  115.       VioPopUp( &fWait, 0 ) ;
  116.       viomodeinfo.cb = sizeof(viomodeinfo) ;
  117.       VioGetMode(&viomodeinfo, 0) ;
  118.       outf = FALSE ;
  119.       while ( outf == FALSE)
  120.      for (row = 0; row < viomodeinfo.row && outf == FALSE ; row ++)
  121.         for (col = 0 ; col < viomodeinfo.col && outf == FALSE; col ++)
  122.         {
  123.     -    VioSetCurPos(row, col, 0) ;
  124.            ret = KbdCharIn(&kbdkeyinfo, IO_NOWAIT, 0) ;
  125.            if (kbdkeyinfo.fbStatus != 0)
  126.            {
  127.           outf = TRUE ;
  128.           break ;
  129.            }
  130.         DosSleep(1000L) ;
  131.         }
  132.  
  133.       putchar(7) ;
  134.       VioEndPopUp(0) ;
  135.       DosSemSet(MuxList.amxs[1].hsem) ;
  136.       ret = DosResumeThread(tid) ;
  137.    }
  138.  
  139.    return( 0 ) ;
  140. }
  141.  
  142.  
  143. void monitor_th( USHORT th_index )
  144. {
  145.  
  146. USHORT ret ;
  147. MONIN monin ;
  148. MONOUT monout ;
  149. SEL ginfo, linfo ;
  150. GINFOSEG FAR *pgis ;
  151. USHORT count ;
  152.  
  153.  
  154.    monin.cb = sizeof(monin) ;
  155.    monout.cb = sizeof(monout) ;
  156.    if (( ret = DosGetInfoSeg(&ginfo, &linfo)) != 0)
  157.    {
  158.       printf("%s: error in DosGetInfoSeg", progname) ;
  159.       return ;
  160.    }
  161.  
  162.    pgis = MAKEPGINFOSEG(ginfo) ;
  163.  
  164.    while ((ret = DosMonReg(hmon, (PBYTE) &monin, (PBYTE) &monout, MONITOR_DEFAULT, th_index /*pgis->sgCurrent*/)) != 0)
  165.       DosSleep(1500L) ;
  166.  
  167.    if ((ret = DosSetPrty(PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MINIMUM+1, 0)) != 0)
  168.    {
  169.       printf("%s: error raising priority", progname) ;
  170.       return ;
  171.    }
  172.  
  173.    while ( TRUE )
  174.    {
  175.       count = sizeof(kbdbuf) ;
  176.       if (( ret = DosMonRead( (PBYTE) &monin, DCWW_WAIT, (PBYTE) &kbdbuf, &count )) != 0)
  177.       {
  178.      printf("%s: error in DosMonRead", progname) ;
  179.      return ;
  180.       }
  181.  
  182.       if(kbdbuf.kbdkeyinfo.chScan == 0x67)
  183.      DosSemClear(MuxList.amxs[0].hsem) ;
  184.      //DosSemClear(&exitsem) ;
  185.  
  186.      if ( kbdbuf.kbdkeyinfo.fsState & ALT && kbdbuf.kbdkeyinfo.chScan == 48)
  187.     // if (kbdbuf.kbdkeyinfo.chChar == 'b'|| kbdbuf.kbdkeyinfo.chChar == 'B')
  188.        DosSemClear(MuxList.amxs[1].hsem) ; /* blanksem */
  189.        //DosSemClear(&exitsem) ;
  190.  
  191.       DosSemRequest(×em, SEM_INDEFINITE_WAIT) ;
  192.       ntime = kbdbuf.kbdkeyinfo.time ;
  193.       DosSemClear(×em) ;
  194.  
  195.       if (( ret = DosMonWrite( (PBYTE) &monout, (PBYTE) &kbdbuf, count)) != 0)
  196.       {
  197.      printf("%s: error in DosMonWrite", progname) ;
  198.      return ;
  199.       }
  200.    }
  201.  
  202.    DosMonClose(hmon) ;
  203.  
  204. }
  205.  
  206.  
  207. void time_th( void )
  208. {
  209.  
  210.    while( TRUE)
  211.    {
  212.       DosSemRequest(×em, SEM_INDEFINITE_WAIT) ;
  213.       newtime = time(NULL) ;
  214.       if (otime != ntime)
  215.       {
  216.      otime = ntime ;
  217.      oldtime = newtime ;
  218.       }
  219.       if (newtime - oldtime > sleeptime) /* default 300 seconds */
  220.      DosSemClear(&exitsem) ;
  221.       DosSemClear(×em) ;
  222.       DosSleep( 1000L ) ;
  223.    }
  224. }
  225.