;       SCCSID = @(#)bksgstat.asm	6.1 90/11/16
;       SCCSID = @(#)bksgstat.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   BKSGSTAT.ASM - Return Current Keyboard Status.

;******************************************************************************
;*******   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 error2.inc              ; Extended error code file
        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         ; DOS Generic IOCTL

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

        PUBLIC  GetStatus

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

;************************ START OF SPECIFICATIONS ***********************
;*                                                                      *
;*  SUBROUTINE NAME:    GetStatus                                       *
;*                                                                      *
;*  DESCRIPTIVE NAME:   Return Current Status of the Keyboard.          *
;*                                                                      *
;*  FUNCTION:   Information such as:                                    *
;*                      Echo Mode   - ON/OFF                            *
;*                      Input Mode  - RAW/COOKED                        *
;*                      Shift State                                     *
;*                      TurnAround Character                            *
;*              are return to the caller.                               *
;*                                                                      *
;*  NOTES:      Executes on level 3 - Protect Mode ONLY                 *
;*                                                                      *
;*  ENTRY POINT:        GetStatus                                       *
;*      LINKAGE:        call near                                       *
;*                                                                      *
;*  INPUT:      Input parameters are on stack                           *
;*                                                                      *
;*                      KBD_Handle      word                            *
;*                      StatBufO        @ of status record              *
;*                                                                      *
;*  EXIT NORMAL:        AX = 0                                          *
;*                                                                      *
;*  EXIT ERROR:         AX = ERROR_KBD_INVALID_LENGTH                   *
;*                           ERROR_BAD_COMMAND                          *
;*                                                                      *
;*  INTERNAL REFERENCES:  Char_Code, Dev_Handle, Echo_Mode, TA_Char     *
;*                        TA_Type                                       *
;*           ROUTINES:    None                                          *
;*                                                                      *
;*  EXTERNAL REFERENCES:  None                                          *
;*           ROUTINES:    DOSDEVIOCTL, KBDSETFGND                       *
;*                                                                      *
;************************* END OF SPECIFICATIONS ************************
        PAGE



GetStatus PROC  NEAR
        ASSUME  DS:IDSEG
;
;************************************************************************
;*  On entry:  DS = IDSEG   ES = GDSEG   DX = KBD_SG                    *
;************************************************************************
;
;
;       Get current keyboard state and stuff it into the caller's
;       buffer area.
;
        sub     cx,cx                   ; @  CX = 0
        les     di,[bp].StatBufO        ; ES:DI -> KBDSTATE structure
        mov     ax,es:[di].S_LENGTH     ; Get structure length from stack
       .IF <ax AE 10> NEAR              ; Buffer has sufficient length?

;$$#I001
        .IF <dx A LastBaseSG>           ; Winthorn screen group?
           push  es                     ; CDB2
;          push  ds                     ;@@DTF1
;          push  GDSEG                  ;@@DTF1
;          pop   ds                     ;@@DTF1
           push  hConsole
           push  [bp].KBD_Handle
           push  es                     ; Receiver address segment
           push  di                     ; Receiver address offset
           push  GDSEG                  ;@@DTF1 CDB2
           pop   es                     ; CDB2
           call  ES:WINCONSOLEGETSTATUS ;CDB2
           pop   es                     ; CDB2
;          pop   ds                     ;@@DTF1
        .ELSE
;$$#END

         inc    di                      ; Index to Bit Mask word
         inc    di
         push   ds                      ; Address of input mode to be returned
         push   OFFSET Char_Code
         push   cx                      ; @  Dummy input parmlist
         push   cx                      ; @
         push   71H                     ; Function code: Get input mode
         push   04H                     ; Category code: Keyboard control
         push   Dev_Handle              ; Device Handle to KBD:
         call   DOSDEVIOCTL
         or     ax,ax                   ; Error?
        .IF Z
;
;       Set Function Mask word accordingly:
;       @@  bit 8 = Shift report ON
;           bit 7 = Extened TurnAround indicator if set
;           bit 6 = Ignored
;           bit 5 = Ignored
;           bit 4 = Ignored
;           bit 3 = Cooked mode ON if set
;           bit 2 = Raw mode ON if set
;           bit 1 = Echo mode OFF if set
;           bit 0 = Echo mode ON if set
;
          mov   al,Echo_Mode            ; Get current echo mode
          test  Char_Code,RAW           ; @@
         .IF NZ                         ; @@ Raw mode ON?
           or   al,RAW_ON               ; @@ Yes, set bit 2
           test Char_Code,ON            ; @@
          .IF NZ                        ; @@ Shift report ON?
            or  ah,ON                   ; @@ Yes, set bit 8
          .ENDIF
         .ELSE
           or   al,COOKED_ON            ; @@ NO, set bit 3
         .ENDIF
          test  TA_Type,ON
         .IF NZ                         ; Extended TA type?
           or   al,80H                  ; Set Extended TA character type bit
         .ENDIF
          cld
          stosw                         ; Stuff input and echo mode!
          sub   ah,ah                   ; Just in case Shift report is ON
          mov   al,TA_Char              ; Get current TurnAround character
          stosw                         ; Stuff it!
          push  es                      ; Address of where interim character
          push  di                      ;   will be placed
          push  cx                      ; @@ Dummy input parmlist
          push  cx                      ; @@
          push  72H                     ; Device function; Get interim flags
          push  04H                     ; Device category; Keyboard
          push  Dev_Handle              ; Device Handle to KBD:
          call  DOSDEVIOCTL
          or    ax,ax
         .IF Z
           inc  di                      ; Index to Shift state
           push es                      ; Address of where shift state
           push di                      ;   will be returned
           push ax                      ; Dummy input parmlist
           push ax
           push 73H                     ; Device function; Get shift state
           push 04H                     ; Device category; Keyboard
           push Dev_Handle              ; Device Handle to KBD:
           call DOSDEVIOCTL
           or   ax,ax
          .IF Z
            mov bx,es:[di]              ; Get shift state
            mov al,es:[di+2]            ; Get NLS shift state
            stosb                       ; Reverse its order
            mov ax,bx
            stosw
            sub ax,ax
          .ENDIF
         .ENDIF
        .ENDIF
;$$#I002
        .ENDIF
;$$#END
       .ELSE
         mov    ax,ERROR_KBD_INVALID_LENGTH
       .ENDIF
        ret

GetStatus ENDP

CSEG    ENDS
        END
