home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / KBIDLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-17  |  3.6 KB  |  133 lines

  1. /*
  2.       kbidle.c    10/02/87
  3.  
  4.     % kb_Idle
  5.     Replaces normal kb_Read() function with new handler.
  6.  
  7.     OWL 1.2
  8.     Copyright (c) 1987, 1988, 1989 by Oakland Group, Inc.
  9.     ALL RIGHTS RESERVED.
  10.  
  11.     Revision History:
  12.     -----------------
  13.      9/09/88 jmd    Ported to oakland lib
  14.      9/19/88 jmd    Added messages to idle func
  15.     10/04/88 jmd    Added support for kb_Check
  16.     10/27/88 jmd    Fixed bug in ReadHandler
  17.     12/01/88 jmd    modified for new KeyReadEvent
  18.      2/07/89 Ted    added timeout wait arg to hCheckEvent function.
  19.      7/07/89 gam    changed NULL to FNULL where applicable
  20.  
  21.     11/30/89 jmd    fixed CheckEvent test (now uses KEY_NONE)
  22.      3/28/90 jmd    ansi-fied
  23.      4/14/90 jmd    added return value to idle_func
  24.      4/24/90 ted    added wait processing to idle_func
  25.      5/12/90 jmd     changed scancodes to ints
  26. */
  27.  
  28. #include "oakhead.h"
  29. #include "disppriv.h"
  30. #include "digutil.h"    /* for dig_SubHsecs */
  31. #include "kbidle.h"
  32. #include "scancode.h"    /* for mouse pseudo-scancodes */
  33.  
  34. OSTATIC int kb_ReadHandler(moupos_struct *mposp);
  35. OSTATIC int kb_CheckHandler(unsigned wait);
  36.  
  37. /* static function pointers */
  38. static idle_fptr                kIdle_fptr         = FNULL;
  39. static dig_hReadEvent_func        ((*hReadEventFptr)) = FNULL;
  40. static dig_hCheckEvent_func        ((*hCheckEventFptr)) = FNULL;
  41.  
  42. static kistash = KEY_NONE;
  43.  
  44. /* -------------------------------------------------------------------------- */
  45.  
  46. void kb_Idle(idle_fptr idle)
  47. /*
  48.     Sets up idle key function.
  49.  
  50.     The idle function is called with an IDLE_START message.
  51.  
  52.     If idle == NULL turn off the idle function and call it with
  53.     an IDLE_STOP message.
  54. */
  55. {
  56.     if (hReadEventFptr != FNULL) {
  57.         /* reset kb functions */
  58.         (*kIdle_fptr)(IDLE_STOP, 0);
  59.         curr_dmgr->disp.dig.hReadEvent = hReadEventFptr;
  60.         curr_dmgr->disp.dig.hCheckEvent = hCheckEventFptr;
  61.         hReadEventFptr = FNULL;
  62.         hCheckEventFptr = FNULL;
  63.         kIdle_fptr = FNULL;
  64.     }
  65.  
  66.     if (idle != FNULL) {
  67.         /* save old keyboard handlers */
  68.         hReadEventFptr = curr_dmgr->disp.dig.hReadEvent;
  69.         hCheckEventFptr = curr_dmgr->disp.dig.hCheckEvent;
  70.  
  71.         /* set new keyboard handlers */
  72.         curr_dmgr->disp.dig.hReadEvent = kb_ReadHandler;
  73.         curr_dmgr->disp.dig.hCheckEvent = kb_CheckHandler;
  74.         kIdle_fptr = idle;
  75.         (*kIdle_fptr)(IDLE_START, 0);
  76.     }
  77. }
  78. /* -------------------------------------------------------------------------- */
  79.  
  80. static int kb_CheckHandler(unsigned wait)
  81. /*
  82.     Calls user idle function then calls original kb_Check routine
  83.     idle function is called with an IDLE_RUN message.
  84. */
  85. {
  86.     unsigned itime, elaps;
  87.  
  88.     if (wait != 0 && wait != (unsigned) -1) {
  89.         itime = hard_Timer();
  90.     }
  91.     if ((kistash =(*kIdle_fptr)(IDLE_CHECK, wait)) != KEY_NONE) {
  92.         return(kistash);
  93.     }
  94.     if (wait != 0 && wait != (unsigned) -1) {
  95.         elaps = dig_SubHsecs(itime, hard_Timer());
  96.         if (elaps > wait) {
  97.             wait = 0;
  98.         }
  99.         else {
  100.             wait -= elaps;
  101.         }
  102.     }
  103.     return((*hCheckEventFptr)(wait));
  104. }
  105. /* -------------------------------------------------------------------------- */
  106.  
  107. static int kb_ReadHandler(moupos_struct *mposp)
  108. /*
  109.     Calls user idle function while real kb_Check routine returns FALSE.
  110.     Calls original kb_Read when a keystroke is available
  111.     idle function is called with an IDLE_RUN message.
  112.  
  113.     The idle function should return KEY_NONE.  Any other value is
  114.     returned via kb_Read()
  115. */
  116. {
  117.     int key, tstash;
  118.  
  119.     if (kistash != KEY_NONE) {
  120.         tstash = kistash;
  121.         kistash = KEY_NONE;
  122.         return(tstash);
  123.     }
  124.     while ((*hCheckEventFptr)(0) == KEY_NONE) {
  125.         if ((key = (*kIdle_fptr)(IDLE_READ, 0)) != KEY_NONE) {
  126.             return(key);
  127.         }
  128.     }
  129.     return((*hReadEventFptr)(mposp));
  130. }
  131. /* -------------------------------------------------------------------------- */
  132.  
  133.