;*****************************************************;
; -- VxD whose only purpose is to detect if the       ;
;    system VM has the keyboard focus.                ;
;*****************************************************;
.386p

;*****************************************************;
; Header files.                                       ;
;*****************************************************;
include vmm.inc
include vkd.inc

;*****************************************************;
; Constants.                                          ;
;*****************************************************;

; Permanent device id obtained from Microsoft.
wDeviceId     EQU 2953H

; VxD version number.
bVersionMajor EQU 01
bVersionMinor EQU 00
wVersion      EQU (bVersionMajor * 256 + bVersionMinor)

; API id's.
apiGetVersion EQU 0
apiFocusTest  EQU 1

;*****************************************************;
; Device Header.                                      ;
;*****************************************************;
Declare_Virtual_Device  wddjkeyf, bVersionMajor,      \
    bVersionMinor, WddjKeyfControl, wDeviceId,        \
    Undefined_Init_Order,, WddjKeyfApi

;*****************************************************;
; Control Procedures.                                 ;
;*****************************************************;
VxD_Locked_Code_Seg

;*****************************************************;
; -- Called for protect mode initialization.  Not     ;
;    much to do.                                      ;
;*****************************************************;
BeginProc   WddjKeyfInit
    clc
    ret
EndProc     WddjKeyfInit

;*****************************************************;
; -- Control dispatcher.  Not much to do here either. ;
;*****************************************************;
BeginProc    WddjKeyfControl
    Control_Dispatch    Device_Init, WddjKeyfInit
    clc
    ret
EndProc WddjKeyfControl
VxD_Locked_Code_Ends


;*****************************************************;
; API's.                                              ;
;*****************************************************;
VxD_Code_Seg

;*****************************************************;
; -- Main dispatch point for protected mode API.      ;
; -- EAX : API id to execute.                         ;
; -- Jump to corresponding API routine.               ;
;*****************************************************;
BeginProc   WddjKeyfApi, Public
    ; Innocent until proven guilty (no error yet).
    mov     [ebp].Client_Flags, 0

    ; Which function is required?
    movzx   eax, [ebp].Client_AX

    cmp     ax, apiGetVersion
    jz      short WddjKeyfGetVersion

    cmp     ax, apiFocusTest
    jz      short WddjKeyfTest

    ; Guilty.  Set carry flag to indicate failure.
    or      [ebp].Client_Flags, CF_Mask
    ret
EndProc     WddjKeyfApi

;*****************************************************;
; -- Return the version number.                       ;
;*****************************************************;
BeginProc   WddjKeyfGetVersion, Public
    mov     [ebp].Client_AX, wVersion
    ret
EndProc     WddjKeyfGetVersion

;*****************************************************;
; -- Test if the system VM has the keyboard focus.    ;
; -- Returns via DS:AX :                              ;
;    -- zero if system VM has keyboard focus,         ;
;    -- handle of VM with keyboard focus otherwise.   ;
;*****************************************************;
BeginProc   WddjKeyfTest, Public
    ; VM handle of keyboard owner returned in EBX.
    VxDCall VKD_Get_Kbd_Owner
    mov     eax, ebx    ; Save it away.

    ; System VM handle returned in EBX.
    VMMCall     Get_Sys_VM_Handle
    cmp     eax, ebx    ; Same as keyboard owner?
    jz      short SysVMHasFocus

    ; Return non-system VM owning keyboard.
    mov     [ebp].Client_AX, ax
    shr     eax, 16
    mov     [ebp].Client_DX, ax
    jmp     short WddjKeyfTestExit

SysVMHasFocus:
    xor     ax, ax

    mov     [ebp].Client_AX, ax
    mov     [ebp].Client_DX, ax

WddjKeyfTestExit:
    ret
EndProc     WddjKeyfTest
VxD_Code_Ends
End


