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

  1. /*
  2.     kbread.c    12/12/88
  3.  
  4.     % Get Key/Mouse event function w/ mouse handler handling.
  5.     by Ted.
  6.  
  7.     OWL 1.2
  8.     Copyright (c) 1988, by Oakland Group, Inc.
  9.     ALL RIGHTS RESERVED.
  10.  
  11.     Revision History:
  12.     -----------------
  13.      2/04/89 Ted    changed kb_Check to kb_CheckWait w/ timeout arg.
  14.      2/08/88 ted:    Added wmgr_NewCurrentFlag stuff to fix mouse support.
  15.      6/23/89 ted    Checked for clear wmgr_lastmoupos for clean startup case.
  16.      6/23/89 ted    Changed around CheckWait to loop until timeout.
  17.      6/23/89 ted    Added tolerance for potential DIG quirk where a mouse is
  18.                      expected but a key is returned.
  19.      6/26/89 ted    Added mousecode stash support.
  20.  
  21.     12/14/89 ted    In case of mousecode, left WasMouse setting unchanged.
  22.      1/24/90 ted    Made priority of kb_Check's ReplyStash higher than mousecode.
  23.      3/28/90 jmd    ansi-fied
  24.      5/12/90 jmd     changed scancodes to ints
  25.      6/22/90 ted    added "void"s to no-parameter functions per ansii.
  26.      8/08/90 ted    Changed references from disp_SetMouseCode to kb_Stuff.
  27.      8/30/90 ted    Changed hard_Claim/Release calls to wmgr_EvClaim/EvRelease asserts.
  28.      9/06/90 jmd    added ascii function
  29.     10/16/90 jmd    added test for KEY_DUMMYVAL to ascii function
  30. */
  31.  
  32. #include "oakhead.h"
  33. #include "disppriv.h"
  34. #include "digutil.h"
  35. #include "scancode.h"    /* for mouse pseudo-scancodes */
  36. /* -------------------------------------------------------------------------- */
  37.  
  38. int kb_Read(void)
  39. /*
  40.     Returns with a keyhit or an accepted mouse event.
  41.     A mouse event position is relative to whatever window the mouse is in;
  42.     the position is never outside of the window.
  43.     kb_WasMouse() may be called to find out whether the source of the
  44.     code returned by this function was the keyboard or the mouse.
  45.     Note: calling kb_Check invalidates the kb_WasMouse() setting.
  46. */
  47. {
  48.     moupos_struct moupos;
  49.     int evcode;
  50.  
  51.     owl_Assert(wmgr_EvClaim(0), OE_KB_REENTER); /* Here for re-entrancy protection */
  52.  
  53.     /* If CheckWait stashed an event code, return it now */
  54.     if (wmgr_ReplyStash() != MOU_IGNORE) {
  55.         evcode = wmgr_ReplyStash();
  56.         wmgr_SetReplyStash(MOU_IGNORE);
  57.  
  58.         /* Note: wasmouse is set in CheckWait when stash is stashed */
  59.         owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  60.         return(evcode);
  61.     }
  62.     /* If anyone stuffed a mouse event code, return it now */
  63.     if (wmgr_stuffcode() != MOU_IGNORE) {
  64.         evcode = wmgr_stuffcode();
  65.         kb_Stuff(MOU_IGNORE);
  66.  
  67.         /* Leave WasMouse setting in effect from whatever set the stuffcode */
  68.         owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  69.         return(evcode);
  70.     }
  71.     /* If current win has changed since last event, send the right messages */
  72.     /*  Note: we clear the newcurrent flag first and pass newcurr as an */
  73.     /*   argument to SendMouseMsgs because a mouse handler might call a */
  74.     /*   function which sets the newcurrent flag again. */
  75.     if (wmgr_NewCurrentFlag()) {
  76.         wmgr_SetNewCurrentFlag(FALSE);
  77.  
  78.         if (!mev_IsEventClear(wmgr_lastmoupos())) {
  79.             evcode = wmgr_SendMouseMsgs(wmgr_lastmoupos(), TRUE);
  80.             if (evcode != MOU_IGNORE) {
  81.                 wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  82.                 owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  83.                 return(evcode);
  84.             }
  85.         }
  86.     }
  87.     /* Loop until we get an event that's not MOU-IGNORE from kb or mouse */
  88.     for (;;) {
  89.         evcode = hard_ReadEvent(&moupos);
  90.         if (evcode != HARD_MEV) {    /* test the mouse event */
  91.             wmgr_SetWasMouse(FALSE); /* Set flag for keyboard evcode */
  92.             break;        /* return if real keyhit gotten */
  93.         }
  94.         else {
  95.             memmove((VOID *)wmgr_lastmoupos(),
  96.                     (VOID *)&moupos, sizeof(moupos_struct));
  97.  
  98.             evcode = wmgr_SendMouseMsgs(&moupos, FALSE);
  99.             if (evcode != MOU_IGNORE) {
  100.                 wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  101.                 break;    /* return if the mouse handler didn't ignore the event */
  102.             }
  103.             /* else code was MOU-IGNORE; continue loop */
  104.         }
  105.     }
  106.     owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  107.     return(evcode);
  108. }
  109. /* -------------------------------------------------------------------------- */
  110.  
  111. boolean kb_CheckWait(unsigned wait)
  112. /*
  113.     Return TRUE if a key or raw mouse event is ready. A TRUE return from here
  114.     guarantees that the next call to kb_Read() will not block.
  115.     If wait is 0, returns immediately. If wait is -1, waits until an event is
  116.     ready.
  117.     'wait'  hundredths of a second to timeout before returning
  118. */
  119. {
  120.     moupos_struct moupos;
  121.     int evcode;
  122.     unsigned itime;
  123.  
  124.     /* If there's no support for event checking, just return TRUE so nobody */
  125.     /*  will decide not to ask for a key when one might be ready */
  126.     if (!disp_EvCheck()) {
  127.         return(TRUE);
  128.     }
  129.     owl_Assert(wmgr_EvClaim(0), OE_KB_REENTER); /* Here for re-entrancy protection */
  130.  
  131.     /* If anyone stashed a stuffcode or we stashed a reply, return TRUE */
  132.     if (wmgr_stuffcode() != MOU_IGNORE || wmgr_ReplyStash() != MOU_IGNORE) {
  133.         owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  134.         return(TRUE);
  135.     }
  136.     /* If current win has changed since last event, send the right messages */
  137.     if (wmgr_NewCurrentFlag()) {
  138.         wmgr_SetNewCurrentFlag(FALSE);
  139.  
  140.         if (!mev_IsEventClear(wmgr_lastmoupos())) {
  141.             evcode = wmgr_SendMouseMsgs(wmgr_lastmoupos(), TRUE);
  142.             if (evcode != MOU_IGNORE) {
  143.                 wmgr_SetReplyStash(evcode);
  144.                 wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  145.                 owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  146.                 return(TRUE);
  147.             }
  148.         }
  149.     }
  150.     /* Loop until we get an event that's not MOU-IGNORE from kb or mouse */
  151.     /* or until the wait time expires. */
  152.     if (wait != 0 && wait != (unsigned) -1) {
  153.         itime = hard_Timer();
  154.     }
  155.     for (;;) {
  156.         evcode = hard_CheckEvent(wait);
  157.         if (evcode != HARD_MEV) {
  158.             owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  159.             return(evcode != KEY_NONE);    /* code was a key or timeout */
  160.         }
  161.         else {    /* CheckEvent says a mouse event is waiting - read it now */
  162.             evcode = hard_ReadEvent(&moupos);
  163.             if (evcode != HARD_MEV) {
  164.                 /* We got a key event even though CheckEvent promised a mouse event. */
  165.                 if (evcode == KEY_NONE) {
  166.                     owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  167.                     return(FALSE); /* DIG error: it promised one but didn't deliver */
  168.                 }
  169.                 /* Note: this is an odd case in that the stash is a key code. */
  170.                 wmgr_SetReplyStash(evcode);
  171.                 wmgr_SetWasMouse(FALSE); /* Set flag for keyboard evcode */
  172.                 owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  173.                 return(TRUE);    /* code was a key */
  174.             }
  175.             else {
  176.                 memmove((VOID *)wmgr_lastmoupos(),
  177.                         (VOID *)&moupos, sizeof(moupos_struct));
  178.  
  179.                 evcode = wmgr_SendMouseMsgs(&moupos, FALSE);
  180.                 if (evcode != MOU_IGNORE) {
  181.                     wmgr_SetReplyStash(evcode);
  182.                     wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  183.                     owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  184.                     return(TRUE);
  185.                 }
  186.                 else {    /* code was MOU-IGNORE; continue loop if time still left */
  187.                     if (wait == 0) {
  188.                         owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  189.                         return(FALSE);    /* timeout */
  190.                     }
  191.                     /* else if we're not waiting forever, see if our time is up. */
  192.                     else if (wait != (unsigned) -1) {
  193.                         if (dig_SubHsecs(itime, hard_Timer()) > wait) {
  194.                             owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
  195.                             return(FALSE);    /* timeout */
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.      }
  202. }
  203. /* -------------------------------------------------------------------------- */
  204.  
  205. void kb_Clear(void)
  206. /*
  207.     Clear keyboard buffer.
  208.     Use with caution when mouse support is turned on.
  209. */
  210. {
  211.     if (hard_Control(HC_CLEAREVENTS, NULL, NULL)) {
  212.         /* Only clear these variables if the DIG supports clearing events */
  213.         kb_Stuff(MOU_IGNORE);
  214.         wmgr_SetReplyStash(MOU_IGNORE);
  215.     }
  216. }
  217. /* -------------------------------------------------------------------------- */
  218.  
  219. int ascii(int scancode)
  220. /*
  221.     Converts a scancode to an ascii value.
  222.  
  223.     Test if it's a user value or a pseudo value.
  224.     else strip off the high bytes.
  225. */
  226. {
  227.     if (((scancode & 0xFF00) == KEY_USERVAL) ||
  228.         ((scancode & 0xFF00) == KEY_PSEUDOVAL) ||
  229.         ((scancode & 0xFF00) == KEY_DUMMYVAL)) {
  230.  
  231.         return(0);
  232.     }
  233.     else {
  234.         return(scancode & 0x00FF);
  235.     }
  236. }
  237. /* -------------------------------------------------------------------------- */
  238.  
  239.