;       SCCSID = @(#)bkspchar.asm	6.1 90/11/16
;       SCCSID = @(#)bkspchar.asm	6.5 90/02/14
; ****************************************************************************
; *                                                                          *
; *                       IBM/Microsoft Confidential                         *
; *                                                                          *
; *                 Copyright (c) IBM Corporation  1987, 1990                *
; *                 Copyright (c) Microsoft Corp.  1987, 1990                *
; *                           All Rights Reserved                            *
; *                                                                          *
; ****************************************************************************
        PAGE    60,132
        TITLE   BKSPCHAR.ASM - Extended Character Read (non-destructive).

;******************************************************************************
;*******   C H A N G E       H I S T O R Y   **********************************
;******************************************************************************
;@T52   90/01/15  TPL  DCR 704 work

        .286p                           ; 286 Protect Mode instructions

        .xlist
        INCLUDE struc.inc               ; Structured Macros
        INCLUDE bksxxx.inc              ; Equates and structure template file
        INCLUDE bksextrn.inc            ; External data declaration file
        .list

CSEG    SEGMENT BYTE PUBLIC 'CODE'
CSEG    ENDS

IDSEG   SEGMENT BYTE PUBLIC 'DATA'
IDSEG   ENDS

        EXTRN   DOSDEVIOCTL:FAR         ; Extended peek

;$$#I000
        EXTRN WINCONSOLEREADKEYEVENT:DWORD ;CDB2
        GDSEG SEGMENT BYTE PUBLIC 'FAR_DATA'                            ;@T52
        GDSEG ENDS
;$$#END

        PUBLIC  PeekAtIt

CSEG    SEGMENT BYTE PUBLIC 'CODE'
        ASSUME  CS:CSEG,DS:NOTHING,ES:NOTHING
        PAGE

;************************ START OF SPECIFICATIONS ***********************
;*                                                                      *
;*  SUBROUTINE NAME:    PeekAtIt                                        *
;*                                                                      *
;*  DESCRIPTIVE NAME:   Peek Character from KBD Device.                 *
;*                                                                      *
;*  FUNCTION:   Get a copy of the character data record from the        *
;*              keyboard input buffer if one is available.  This read   *
;*              operation will not remove the character data record     *
;*              from the keyboard input buffer.                         *
;*                                                                      *
;*  NOTES:      Executes on level 3 - Protect Mode ONLY                 *
;*                                                                      *
;*  ENTRY POINT:        PeekAtIt                                        *
;*      LINKAGE:        call near                                       *
;*                                                                      *
;*  INPUT:      Input parameters are on stack                           *
;*                                                                      *
;*                      KBD_Handle      word                            *
;*                      PCharDataO      @ of data record                *
;*                                                                      *
;*  EXIT NORMAL:        AX = 0                                          *
;*                                                                      *
;*  EXIT ERROR:         AX = ERROR_BAD_COMMAND                          *
;*                                                                      *
;*  INTERNAL REFERENCES:  Dev_Handle, Transfer_Count                    *
;*           ROUTINES:    None                                          *
;*                                                                      *
;*  EXTERNAL REFERENCES:  None                                          *
;*           ROUTINES:    DOSDEVIOCTL                                   *
;*                                                                      *
;************************* END OF SPECIFICATIONS ************************
        PAGE



PeekAtIt PROC   NEAR
        ASSUME  DS:IDSEG
;
;************************************************************************
;*  On entry:  DS = IDSEG   ES = GDSEG   AX = 0   DX = KBD_SG           *
;************************************************************************
;

;$$#I001
       .IF <dx A LastBaseSG>            ; Winthorn screen group?
           mov   dx,es                  ;@@CDB2
           push  hConsole               ;@@DTF1
           les   bx,[bp].PCharDataAdr
           mov   es:[bx].Status_Flag,0
           push  [bp].KBD_Handle
           push  NO_WAIT
           push  PEEK
           push  es
           push  bx
           mov   es,dx                  ;@@CDB2
           call  ES:WINCONSOLEREADKEYEVENT ;CDB2
          .if <ax eq 0>                 ;@@DTF
              les   bx,[bp].PCharDataAdr ;@@DTF
              .if   <es:[bx].Status_Flag ae 20h> ;@@DTF Char read in ?
                  mov   Transfer_Count,1 ;@@DTF ...yes
              .endif                    ;@@DTF
           .endif                       ;@@DTF
       .ELSE
;$$#END

        inc     ax                      ; @  Set transfer count to 1
        mov     Transfer_Count,ax       ; @  Read one character
        les     bx,[bp].PCharDataAdr    ; @  Establish addressability to
                                        ;      CharData record buffer
        mov     es:[bx].Status_Flag,ah  ; @  Indicate CharData record is empty
        ;
        ;****************************************************************
        ;*  Call the KBD deive driver ,with the caller parameters on    *
        ;*  the stack, to read a character, scan code.                  *
        ;*  Set up IOCTL parameters to do an extended read operation.   *
        ;*  Parameters are push onto the stack as follow:               *
        ;*                                                              *
        ;*      PUSH@   CharData        Data to be filled               *
        ;*      PUSH@   WORD            Record transfer count with      *
        ;*                                high bit used as wait/no wait *
        ;*                                indicator                     *
        ;*      PUSH    WORD            Extended Read device function   *
        ;*      PUSH    WORD            Keyboard device category        *
        ;*      PUSH    WORD            Keyboard device handle          *
        ;*                                                              *
        ;****************************************************************
        ;
        ;****************************************************************
        ;*  Set up parameter #1; address to CharData structure          *
        ;****************************************************************
        ;
        push    es                      ; Address to DATA parmlist
        push    bx
        ;
        ;****************************************************************
        ;*  Set up parameter #2; address to Transfer_Count; on return   *
        ;*  Transfer_Count contain the number of characters transfered  *
        ;*  and input mode information.                                 *
        ;*                                                              *
        ;*      Bit 15:   1 = Raw mode                                  *
        ;*                0 = Cooked mode                               *
        ;*      Bit 0-14: 1 = 1 character read                          *
        ;*                0 = 0 character read                          *
        ;****************************************************************
        ;
        push    ds                      ; Address to INPUT parmlist
        push    OFFSET Transfer_Count
        ;
        ;****************************************************************
        ;*  Set up the remaining parameters to satisfy the IOCTL call   *
        ;****************************************************************
        ;
        push    75H                     ; FUNCTIONCODE 75H: Extended Peek
                                        ;              74H: Extended Read
        push    04H                     ; CATEGORY 4; Keyboard Control
        push    Dev_Handle              ; Keyboard Device Handle
        call    DOSDEVIOCTL
;$$#I002
       .ENDIF
;$$#END
        ret

PeekAtIt ENDP

CSEG    ENDS
        END
