home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / KBREAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  6.8 KB  |  213 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.1
  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.  
  22. #include "oakhead.h"
  23. #include "disppriv.h"
  24. #include "digutil.h"
  25. #include "scancode.h"    /* for mouse pseudo-scancodes */
  26. /* -------------------------------------------------------------------------- */
  27.  
  28. unsigned kb_Read()
  29. /*
  30.     Returns with a keyhit or an accepted mouse event.
  31.     A mouse event position is relative to whatever window the mouse is in;
  32.     the position is never outside of the window.
  33.     kb_WasMouse() may be called to find out whether the source of the
  34.     code returned by this function was the keyboard or the mouse.
  35. */
  36. {
  37.     moupos_struct moupos;
  38.     unsigned evcode;
  39.  
  40.     hard_Claim();        /* Here for re-entrancy protection */
  41.  
  42.     /* If anyone stashed a mouse event code, return it now */
  43.     if (wmgr_mousecode() != MOU_IGNORE) {
  44.         evcode = wmgr_mousecode();
  45.         disp_SetMouseCode(MOU_IGNORE);
  46.  
  47.         /* Note: we set wasmouse TRUE because the mousecode is intended for */
  48.         /* use in passing control by mouse events. */
  49.         wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  50.         hard_Release();
  51.         return(evcode);
  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.         hard_Release();
  60.         return(evcode);
  61.     }
  62.     /* If current win has changed since last event, send the right messages */
  63.     /*  Note: we clear the newcurrent flag first and pass newcurr as an */
  64.     /*   argument to SendMouseMsgs because a mouse handler might call a */
  65.     /*   function which sets the newcurrent flag again. */
  66.     if (wmgr_NewCurrentFlag()) {
  67.         wmgr_SetNewCurrentFlag(FALSE);
  68.  
  69.         if (!mev_IsEventClear(wmgr_lastmoupos())) {
  70.             evcode = wmgr_SendMouseMsgs(wmgr_lastmoupos(), TRUE);
  71.             if (evcode != MOU_IGNORE) {
  72.                 wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  73.                 hard_Release();
  74.                 return(evcode);
  75.             }
  76.         }
  77.     }
  78.     /* Loop until we get an event that's not MOU-IGNORE from kb or mouse */
  79.     for (;;) {
  80.         evcode = hard_ReadEvent(&moupos);
  81.         if (evcode != HARD_MEV) {    /* test the mouse event */
  82.             wmgr_SetWasMouse(FALSE); /* Set flag for keyboard evcode */
  83.             break;        /* return if real keyhit gotten */
  84.         }
  85.         else {
  86.             memmove((VOID *)wmgr_lastmoupos(),
  87.                     (VOID *)&moupos, sizeof(moupos_struct));
  88.  
  89.             evcode = wmgr_SendMouseMsgs(&moupos, FALSE);
  90.             if (evcode != MOU_IGNORE) {
  91.                 wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  92.                 break;    /* return if the mouse handler didn't ignore the event */
  93.             }
  94.             /* else code was MOU-IGNORE; continue loop */
  95.         }
  96.     }
  97.     hard_Release();
  98.     return(evcode);
  99. }
  100. /* -------------------------------------------------------------------------- */
  101.  
  102. boolean kb_CheckWait(wait)
  103.     unsigned wait;        /* hundredths of a second to timeout before returning */
  104. /*
  105.     Return TRUE if a key or raw mouse event is ready. A TRUE return from here
  106.     guarantees that the next call to kb_Read() will not block.
  107.     If wait is 0, returns immediately. If wait is -1, waits until an event is
  108.     ready.
  109. */
  110. {
  111.     moupos_struct moupos;
  112.     unsigned evcode;
  113.     unsigned itime;
  114.  
  115.     /* If there's no support for event checking, just return TRUE so nobody */
  116.     /*  will decide not to ask for a key when one might be ready */
  117.     if (!disp_EvCheck()) {
  118.         return(TRUE);
  119.     }
  120.     hard_Claim();        /* Here for re-entrancy protection */
  121.  
  122.     if (wmgr_mousecode() != MOU_IGNORE) {
  123.         hard_Release();
  124.         return(TRUE);    /* If anyone stashed a mouse event code, return TRUE */
  125.     }
  126.     if (wmgr_ReplyStash() != MOU_IGNORE) {
  127.         hard_Release();
  128.         return(TRUE);    /* If we already stashed an event code, return TRUE */
  129.     }
  130.     /* If current win has changed since last event, send the right messages */
  131.     if (wmgr_NewCurrentFlag()) {
  132.         wmgr_SetNewCurrentFlag(FALSE);
  133.  
  134.         if (!mev_IsEventClear(wmgr_lastmoupos())) {
  135.             evcode = wmgr_SendMouseMsgs(wmgr_lastmoupos(), TRUE);
  136.             if (evcode != MOU_IGNORE) {
  137.                 wmgr_SetReplyStash(evcode);
  138.                 wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  139.                 hard_Release();
  140.                 return(TRUE);
  141.             }
  142.         }
  143.     }
  144.     /* Loop until we get an event that's not MOU-IGNORE from kb or mouse */
  145.     /* or until the wait time expires. */
  146.     if (wait != 0 && wait != (unsigned) -1) {
  147.         itime = hard_Timer();
  148.     }
  149.     for (;;) {
  150.         evcode = hard_CheckEvent(wait);
  151.         if (evcode != HARD_MEV) {
  152.             hard_Release();
  153.             return(evcode != KEY_NONE);    /* code was a key or timeout */
  154.         }
  155.         else {    /* CheckEvent says a mouse event is waiting - read it now */
  156.             evcode = hard_ReadEvent(&moupos);
  157.             if (evcode != HARD_MEV) {
  158.                 /* We got a key event even though CheckEvent promised a mouse event. */
  159.                 if (evcode == KEY_NONE) {
  160.                     hard_Release();
  161.                     return(FALSE); /* DIG error: it promised one but didn't deliver */
  162.                 }
  163.                 /* Note: this is an odd case in that the stash is a key code. */
  164.                 wmgr_SetReplyStash(evcode);
  165.                 wmgr_SetWasMouse(FALSE); /* Set flag for keyboard evcode */
  166.                 hard_Release();
  167.                 return(TRUE);    /* code was a key */
  168.             }
  169.             else {
  170.                 memmove((VOID *)wmgr_lastmoupos(),
  171.                         (VOID *)&moupos, sizeof(moupos_struct));
  172.  
  173.                 evcode = wmgr_SendMouseMsgs(&moupos, FALSE);
  174.                 if (evcode != MOU_IGNORE) {
  175.                     wmgr_SetReplyStash(evcode);
  176.                     wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
  177.                     hard_Release();
  178.                     return(TRUE);
  179.                 }
  180.                 else {    /* code was MOU-IGNORE; continue loop if time still left */
  181.                     if (wait == 0) {
  182.                         hard_Release();
  183.                         return(FALSE);    /* timeout */
  184.                     }
  185.                     /* else if we're not waiting forever, see if our time is up. */
  186.                     else if (wait != (unsigned) -1) {
  187.                         if (dig_SubHsecs(itime, hard_Timer()) > wait) {
  188.                             hard_Release();
  189.                             return(FALSE);    /* timeout */
  190.                         }
  191.                     }
  192.                 }
  193.             }
  194.         }
  195.      }
  196. }
  197. /* -------------------------------------------------------------------------- */
  198.  
  199. void kb_Clear()
  200. /*
  201.     Clear keyboard buffer.
  202.     Use with caution when mouse support is turned on.
  203. */
  204. {
  205.     if (hard_Control(HC_CLEAREVENTS, NULL, NULL)) {
  206.         /* Only clear these variables if the DIG supports clearing events */
  207.         disp_SetMouseCode(MOU_IGNORE);
  208.         wmgr_SetReplyStash(MOU_IGNORE);
  209.     }
  210. }
  211. /* -------------------------------------------------------------------------- */
  212.  
  213.