/*****************************************************/
/* -- Test if the system VM (the one that all        */
/*    Windows apps run in) has the keyboard focus.   */
/*****************************************************/
#include <windows.h>
#include "wddjkeyf.h"

#define wGetCallbackId  0x1684

typedef DWORD (CALLBACK * LPFN_VXD_API)(VOID);

int
WDosOwnsKeyboard(VOID)
/*****************************************************/
/* -- Call the WddjKeyf VxD to see if a DOS box owns */
/*    owns the keyboard.                             */
/* -- Return TRUE if it does, FALSE otherwise.       */
/* -- Return -1 in case of error.                    */
/*****************************************************/
    {
    LPFN_VXD_API    lpfn;
    int             sb, ib;

    /* Get the address of the WddjKeyf VxD's protect */
    /* mode API dispatcher. */
    _asm
        {
        xor     di, di
        mov     es, di
        mov     ax, wGetCallbackId
        mov     bx, wWddjKeyfDeviceId
        int     0x2f
        mov     ib, di
        mov     sb, es
        }
    lpfn = (LPFN_VXD_API)MAKELONG(ib, sb);
    if (lpfn == NULL)
        return -1;

    /* Make sure expected version is installed. */
    _asm    mov     ax, apiGetVersion;
    if ((WORD)(*lpfn)() < wWddjKeyfVersion)
        return -1;

    /* Call the VxD and find out who owns the */
    /* keyboard. */
    _asm    mov     ax, apiFocusTest;
    return (*lpfn)() != 0L;
    }


