event.c

来自「适合KS8695X」· C语言 代码 · 共 566 行 · 第 1/2 页

C
566
字号
		    if (EVT.oldMove != -1) {
			EVT.evtq[EVT.oldMove].where_x = evt.where_x;/* Modify existing one  */
			EVT.evtq[EVT.oldMove].where_y = evt.where_y;
			}
		    else {
			EVT.oldMove = EVT.freeHead; /* Save id of this move event   */
			if (EVT.count < EVENTQSIZE)
			    addEvent(&evt);
			}
		    }

		/* Save current mouse state */
		oldMouseState = mouse.fs;
		}
	    MouGetNumQueEl(&mqueue,_EVT_hMouse);
	    }
	    noInput = FALSE;
	}

    /* If there was no input available, give up the current timeslice
     * Note: DosSleep(0) will effectively do nothing if no other thread is ready. Hence
     * DosSleep(0) will still use 100% CPU _but_ should not interfere with other programs.
     */
    if (noInput)
	DosSleep(0);
}

/****************************************************************************
REMARKS:
This macro/function is used to converts the scan codes reported by the
keyboard to our event libraries normalised format. We only have one scan
code for the 'A' key, and use shift modifiers to determine if it is a
Ctrl-F1, Alt-F1 etc. The raw scan codes from the keyboard work this way,
but the OS gives us 'cooked' scan codes, we have to translate them back
to the raw format.
****************************************************************************/
#define _EVT_maskKeyCode(evt)

/****************************************************************************
REMARKS:
Keyboard monitor thread. Needed to catch both keyup and keydown events.
****************************************************************************/
static void _kbdMonThread(
    void *params)
{
    APIRET       rc;
    KEYPACKET    kp;
    USHORT       count = sizeof(KEYPACKET);
    MONBUF       monInbuf;
    MONBUF       monOutbuf;
    int          kpNew;

    /* Raise thread priority for higher responsiveness */
    DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
    monInbuf.cb  = sizeof(monInbuf) - sizeof(monInbuf.cb);
    monOutbuf.cb = sizeof(monOutbuf) - sizeof(monOutbuf.cb);
    bMonRunning = FALSE;

    /* Register the buffers to be used for monitoring for current session */
    if (DosMonReg(_EVT_hKbdMon, &monInbuf, (ULONG*)&monOutbuf,MONITOR_END, -1)) {
	DosPostEventSem(hevStart);  /* unblock the main thread */
	return;
	}

    /* Unblock the main thread and tell it we're OK*/
    bMonRunning = TRUE;
    DosPostEventSem(hevStart);
    while (bMonRunning) {  /* Start an endless loop */
	/* Read data from keyboard driver */
	rc = DosMonRead((PBYTE)&monInbuf, IO_WAIT, (PBYTE)&kp, (PUSHORT)&count);
	if (rc) {
#ifdef CHECKED
	    if (bMonRunning)
		printf("Error in DosMonRead, rc = %ld\n", rc);
#endif
	    bMonRunning = FALSE;
	    return;
	    }

	/* Pass FLUSH packets immediately */
	if (kp.MonFlagWord & 4) {
#ifdef CHECKED
	    printf("Flush packet!\n");
#endif
	    DosMonWrite((PBYTE)&monOutbuf, (PBYTE)&kp, count);
	    continue;
	    }

	/*TODO: to be removed */
	/* Skip extended scancodes & some others */
	if (((kp.MonFlagWord >> 8) == 0xE0) || ((kp.KbdDDFlagWord & 0x0F) == 0x0F)) {
	    DosMonWrite((PBYTE)&monOutbuf, (PBYTE)&kp, count);
	    continue;
	    }

/*      printf("RawScan = %X, XlatedScan = %X, fbStatus = %X, KbdDDFlags = %X\n", */
/*          kp.MonFlagWord >> 8, kp.XlatedScan, kp.u.ShiftState, kp.KbdDDFlagWord); */

	/* Protect access to buffer with mutex semaphore */
	rc = DosRequestMutexSem(hmtxKeyBuf, 1000);
	if (rc) {
#ifdef CHECKED
	    printf("Can't get access to mutex, rc = %ld\n", rc);
#endif
	    bMonRunning = FALSE;
	    return;
	    }

	/* Store packet in circular buffer, drop it if it's full */
	kpNew = kpHead + 1;
	if (kpNew == KEYBUFSIZE)
	    kpNew = 0;
	if (kpNew != kpTail) {
	    memcpy(&keyMonPkts[kpHead], &kp, sizeof(KEYPACKET));
	    /* TODO: fix this! */
	    /* Convert break to make code */
	    keyMonPkts[kpHead].MonFlagWord &= 0x7FFF;
	    kpHead = kpNew;
	    }
	DosReleaseMutexSem(hmtxKeyBuf);

	/* Finally write the packet */
	rc = DosMonWrite((PBYTE)&monOutbuf, (PBYTE)&kp, count);
	if (rc) {
#ifdef CHECKED
	    if (bMonRunning)
		printf("Error in DosMonWrite, rc = %ld\n", rc);
#endif
	    bMonRunning = FALSE;
	    return;
	    }
	}
    (void)params;
}

/****************************************************************************
REMARKS:
Safely abort the event module upon catching a fatal error.
****************************************************************************/
void _EVT_abort(
    int signal)
{
    EVT_exit();
    PM_fatalError("Unhandled exception!");
}

/****************************************************************************
PARAMETERS:
mouseMove   - Callback function to call wheneve the mouse needs to be moved

REMARKS:
Initiliase the event handling module. Here we install our mouse handling ISR
to be called whenever any button's are pressed or released. We also build
the free list of events in the event queue.

We use handler number 2 of the mouse libraries interrupt handlers for our
event handling routines.
****************************************************************************/
void EVTAPI EVT_init(
    _EVT_mouseMoveHandler mouseMove)
{
    ushort  stat;

    /* Initialise the event queue */
    PM_init();
    EVT.mouseMove = mouseMove;
    initEventQueue();
    oldMouseState = 0;
    oldKeyMessage = 0;
    memset(keyUpMsg,0,sizeof(keyUpMsg));

    /* Open the mouse driver, and set it up to report events in mickeys */
    MouOpen(NULL,&_EVT_hMouse);
    stat = 0x7F;
    MouSetEventMask(&stat,_EVT_hMouse);
    stat = (MOU_NODRAW | MOU_MICKEYS) << 8;
    MouSetDevStatus(&stat,_EVT_hMouse);

    /* Open the keyboard monitor  */
    if (DosMonOpen((PSZ)"KBD$", &_EVT_hKbdMon))
	PM_fatalError("Unable to open keyboard monitor!");

    /* Create event semaphore, the monitor will post it when it's initalized */
    if (DosCreateEventSem(NULL, &hevStart, 0, FALSE))
	PM_fatalError("Unable to create event semaphore!");

    /* Create mutex semaphore protecting the keypacket buffer */
    if (DosCreateMutexSem(NULL, &hmtxKeyBuf, 0, FALSE))
	PM_fatalError("Unable to create mutex semaphore!");

    /* Start keyboard monitor thread, use 32K stack */
    kbdMonTID = _beginthread(_kbdMonThread, NULL, 0x8000, NULL);

    /* Now block until the monitor thread is up and running */
    /* Give the thread one second */
    DosWaitEventSem(hevStart, 1000);
    if (!bMonRunning) {  /* Check the thread is OK */
	DosMonClose(_EVT_hKbdMon);
	PM_fatalError("Keyboard monitor thread didn't initialize!");
	}

    /* Catch program termination signals so we can clean up properly */
    signal(SIGABRT, _EVT_abort);
    signal(SIGFPE, _EVT_abort);
    signal(SIGINT, _EVT_abort);
}

/****************************************************************************
REMARKS
Changes the range of coordinates returned by the mouse functions to the
specified range of values. This is used when changing between graphics
modes set the range of mouse coordinates for the new display mode.
****************************************************************************/
void EVTAPI EVT_setMouseRange(
    int xRes,
    int yRes)
{
    rangeX = xRes;
    rangeY = yRes;
}

/****************************************************************************
REMARKS
Modifes the mouse coordinates as necessary if scaling to OS coordinates,
and sets the OS mouse cursor position.
****************************************************************************/
#define _EVT_setMousePos(x,y)

/****************************************************************************
REMARKS:
Initiailises the internal event handling modules. The EVT_suspend function
can be called to suspend event handling (such as when shelling out to DOS),
and this function can be used to resume it again later.
****************************************************************************/
void EVT_resume(void)
{
    /* Do nothing for OS/2 */
}

/****************************************************************************
REMARKS
Suspends all of our event handling operations. This is also used to
de-install the event handling code.
****************************************************************************/
void EVT_suspend(void)
{
    /* Do nothing for OS/2 */
}

/****************************************************************************
REMARKS
Exits the event module for program terminatation.
****************************************************************************/
void EVT_exit(void)
{
    APIRET   rc;

    /* Restore signal handlers */
    signal(SIGABRT, SIG_DFL);
    signal(SIGFPE, SIG_DFL);
    signal(SIGINT, SIG_DFL);

    /* Close the mouse driver */
    MouClose(_EVT_hMouse);

    /* Stop the keyboard monitor thread and close the monitor */
    bMonRunning = FALSE;
    rc = DosKillThread(kbdMonTID);
#ifdef CHECKED
    if (rc)
	printf("DosKillThread failed, rc = %ld\n", rc);
#endif
    rc = DosMonClose(_EVT_hKbdMon);
#ifdef CHECKED
    if (rc) {
	printf("DosMonClose failed, rc = %ld\n", rc);
	}
#endif
    DosCloseEventSem(hevStart);
    DosCloseMutexSem(hmtxKeyBuf);
    KbdFlushBuffer(0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?