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.1
- 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.
- */
-
- #include "oakhead.h"
- #include "disppriv.h"
- #include "digutil.h"
- #include "scancode.h" /* for mouse pseudo-scancodes */
- /* -------------------------------------------------------------------------- */
-
- unsigned kb_Read()
- /*
- 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.
- */
- {
- moupos_struct moupos;
- unsigned evcode;
-
- hard_Claim(); /* Here for re-entrancy protection */
-
- /* If anyone stashed a mouse event code, return it now */
- if (wmgr_mousecode() != MOU_IGNORE) {
- evcode = wmgr_mousecode();
- disp_SetMouseCode(MOU_IGNORE);
-
- /* Note: we set wasmouse TRUE because the mousecode is intended for */
- /* use in passing control by mouse events. */
- wmgr_SetWasMouse(TRUE); /* Set flag for mouse handler generated evcode.*/
- hard_Release();
- return(evcode);
- }
- /* 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 */
- hard_Release();
- 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.*/
- hard_Release();
- 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 */
- }
- }
- hard_Release();
- return(evcode);
- }
- /* -------------------------------------------------------------------------- */
-
- boolean kb_CheckWait(wait)
- unsigned wait; /* hundredths of a second to timeout before returning */
- /*
- 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.
- */
- {
- moupos_struct moupos;
- unsigned 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);
- }
- hard_Claim(); /* Here for re-entrancy protection */
-
- if (wmgr_mousecode() != MOU_IGNORE) {
- hard_Release();
- return(TRUE); /* If anyone stashed a mouse event code, return TRUE */
- }
- if (wmgr_ReplyStash() != MOU_IGNORE) {
- hard_Release();
- return(TRUE); /* If we already stashed an event code, 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.*/
- hard_Release();
- 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) {
- hard_Release();
- 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) {
- hard_Release();
- 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 */
- hard_Release();
- 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.*/
- hard_Release();
- return(TRUE);
- }
- else { /* code was MOU-IGNORE; continue loop if time still left */
- if (wait == 0) {
- hard_Release();
- 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) {
- hard_Release();
- return(FALSE); /* timeout */
- }
- }
- }
- }
- }
- }
- }
- /* -------------------------------------------------------------------------- */
-
- void kb_Clear()
- /*
- 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 */
- disp_SetMouseCode(MOU_IGNORE);
- wmgr_SetReplyStash(MOU_IGNORE);
- }
- }
- /* -------------------------------------------------------------------------- */
-
-