home *** CD-ROM | disk | FTP | other *** search
- /*
- kbread.c 12/12/88
-
- % Get Key/Mouse event function w/ mouse handler handling.
- by Ted.
-
- OWL 1.2
- Copyright (c) 1988, by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 2/04/89 Ted changed kb_Check to kb_CheckWait w/ timeout arg.
- 2/08/88 ted: Added wmgr_NewCurrentFlag stuff to fix mouse support.
- 6/23/89 ted Checked for clear wmgr_lastmoupos for clean startup case.
- 6/23/89 ted Changed around CheckWait to loop until timeout.
- 6/23/89 ted Added tolerance for potential DIG quirk where a mouse is
- expected but a key is returned.
- 6/26/89 ted Added mousecode stash support.
-
- 12/14/89 ted In case of mousecode, left WasMouse setting unchanged.
- 1/24/90 ted Made priority of kb_Check's ReplyStash higher than mousecode.
- 3/28/90 jmd ansi-fied
- 5/12/90 jmd changed scancodes to ints
- 6/22/90 ted added "void"s to no-parameter functions per ansii.
- 8/08/90 ted Changed references from disp_SetMouseCode to kb_Stuff.
- 8/30/90 ted Changed hard_Claim/Release calls to wmgr_EvClaim/EvRelease asserts.
- 9/06/90 jmd added ascii function
- 10/16/90 jmd added test for KEY_DUMMYVAL to ascii function
- */
-
- #include "oakhead.h"
- #include "disppriv.h"
- #include "digutil.h"
- #include "scancode.h" /* for mouse pseudo-scancodes */
- /* -------------------------------------------------------------------------- */
-
- int kb_Read(void)
- /*
- Returns with a keyhit or an accepted mouse event.
- A mouse event position is relative to whatever window the mouse is in;
- the position is never outside of the window.
- kb_WasMouse() may be called to find out whether the source of the
- code returned by this function was the keyboard or the mouse.
- Note: calling kb_Check invalidates the kb_WasMouse() setting.
- */
- {
- moupos_struct moupos;
- int evcode;
-
- owl_Assert(wmgr_EvClaim(0), OE_KB_REENTER); /* Here for re-entrancy protection */
-
- /* If CheckWait stashed an event code, return it now */
- if (wmgr_ReplyStash() != MOU_IGNORE) {
- evcode = wmgr_ReplyStash();
- wmgr_SetReplyStash(MOU_IGNORE);
-
- /* Note: wasmouse is set in CheckWait when stash is stashed */
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(evcode);
- }
- /* If anyone stuffed a mouse event code, return it now */
- if (wmgr_stuffcode() != MOU_IGNORE) {
- evcode = wmgr_stuffcode();
- kb_Stuff(MOU_IGNORE);
-
- /* Leave WasMouse setting in effect from whatever set the stuffcode */
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(evcode);
- }
- /* If current win has changed since last event, send the right messages */
- /* Note: we clear the newcurrent flag first and pass newcurr as an */
- /* argument to SendMouseMsgs because a mouse handler might call a */
- /* function which sets the newcurrent flag again. */
- if (wmgr_NewCurrentFlag()) {
- wmgr_SetNewCurrentFlag(FALSE);
-
- if (!mev_IsEventClear(wmgr_lastmoupos())) {
- evcode = wmgr_SendMouseMsgs(wmgr_lastmoupos(), TRUE);
- if (evcode != MOU_IGNORE) {
- wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(evcode);
- }
- }
- }
- /* Loop until we get an event that's not MOU-IGNORE from kb or mouse */
- for (;;) {
- evcode = hard_ReadEvent(&moupos);
- if (evcode != HARD_MEV) { /* test the mouse event */
- wmgr_SetWasMouse(FALSE); /* Set flag for keyboard evcode */
- break; /* return if real keyhit gotten */
- }
- else {
- memmove((VOID *)wmgr_lastmoupos(),
- (VOID *)&moupos, sizeof(moupos_struct));
-
- evcode = wmgr_SendMouseMsgs(&moupos, FALSE);
- if (evcode != MOU_IGNORE) {
- wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
- break; /* return if the mouse handler didn't ignore the event */
- }
- /* else code was MOU-IGNORE; continue loop */
- }
- }
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(evcode);
- }
- /* -------------------------------------------------------------------------- */
-
- boolean kb_CheckWait(unsigned wait)
- /*
- Return TRUE if a key or raw mouse event is ready. A TRUE return from here
- guarantees that the next call to kb_Read() will not block.
- If wait is 0, returns immediately. If wait is -1, waits until an event is
- ready.
- 'wait' hundredths of a second to timeout before returning
- */
- {
- moupos_struct moupos;
- int evcode;
- unsigned itime;
-
- /* If there's no support for event checking, just return TRUE so nobody */
- /* will decide not to ask for a key when one might be ready */
- if (!disp_EvCheck()) {
- return(TRUE);
- }
- owl_Assert(wmgr_EvClaim(0), OE_KB_REENTER); /* Here for re-entrancy protection */
-
- /* If anyone stashed a stuffcode or we stashed a reply, return TRUE */
- if (wmgr_stuffcode() != MOU_IGNORE || wmgr_ReplyStash() != MOU_IGNORE) {
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(TRUE);
- }
- /* If current win has changed since last event, send the right messages */
- if (wmgr_NewCurrentFlag()) {
- wmgr_SetNewCurrentFlag(FALSE);
-
- if (!mev_IsEventClear(wmgr_lastmoupos())) {
- evcode = wmgr_SendMouseMsgs(wmgr_lastmoupos(), TRUE);
- if (evcode != MOU_IGNORE) {
- wmgr_SetReplyStash(evcode);
- wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(TRUE);
- }
- }
- }
- /* Loop until we get an event that's not MOU-IGNORE from kb or mouse */
- /* or until the wait time expires. */
- if (wait != 0 && wait != (unsigned) -1) {
- itime = hard_Timer();
- }
- for (;;) {
- evcode = hard_CheckEvent(wait);
- if (evcode != HARD_MEV) {
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(evcode != KEY_NONE); /* code was a key or timeout */
- }
- else { /* CheckEvent says a mouse event is waiting - read it now */
- evcode = hard_ReadEvent(&moupos);
- if (evcode != HARD_MEV) {
- /* We got a key event even though CheckEvent promised a mouse event. */
- if (evcode == KEY_NONE) {
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(FALSE); /* DIG error: it promised one but didn't deliver */
- }
- /* Note: this is an odd case in that the stash is a key code. */
- wmgr_SetReplyStash(evcode);
- wmgr_SetWasMouse(FALSE); /* Set flag for keyboard evcode */
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(TRUE); /* code was a key */
- }
- else {
- memmove((VOID *)wmgr_lastmoupos(),
- (VOID *)&moupos, sizeof(moupos_struct));
-
- evcode = wmgr_SendMouseMsgs(&moupos, FALSE);
- if (evcode != MOU_IGNORE) {
- wmgr_SetReplyStash(evcode);
- wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(TRUE);
- }
- else { /* code was MOU-IGNORE; continue loop if time still left */
- if (wait == 0) {
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(FALSE); /* timeout */
- }
- /* else if we're not waiting forever, see if our time is up. */
- else if (wait != (unsigned) -1) {
- if (dig_SubHsecs(itime, hard_Timer()) > wait) {
- owl_Assert(wmgr_EvRelease(0), OE_KB_REENTER);
- return(FALSE); /* timeout */
- }
- }
- }
- }
- }
- }
- }
- /* -------------------------------------------------------------------------- */
-
- void kb_Clear(void)
- /*
- Clear keyboard buffer.
- Use with caution when mouse support is turned on.
- */
- {
- if (hard_Control(HC_CLEAREVENTS, NULL, NULL)) {
- /* Only clear these variables if the DIG supports clearing events */
- kb_Stuff(MOU_IGNORE);
- wmgr_SetReplyStash(MOU_IGNORE);
- }
- }
- /* -------------------------------------------------------------------------- */
-
- int ascii(int scancode)
- /*
- Converts a scancode to an ascii value.
-
- Test if it's a user value or a pseudo value.
- else strip off the high bytes.
- */
- {
- if (((scancode & 0xFF00) == KEY_USERVAL) ||
- ((scancode & 0xFF00) == KEY_PSEUDOVAL) ||
- ((scancode & 0xFF00) == KEY_DUMMYVAL)) {
-
- return(0);
- }
- else {
- return(scancode & 0x00FF);
- }
- }
- /* -------------------------------------------------------------------------- */
-