;       SCCSID = @(#)bksfocus.asm	6.1 90/11/16
;       SCCSID = @(#)bksfocus.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   BKSFOCUS.ASM - Get Logical to Physical Bond

;******************************************************************************
;*******   C H A N G E       H I S T O R Y   **********************************
;
;       87/2/12  TPL    Boost the calling process's priority, if it is running
;                       in the foreground screen group.
;       87/7/06  TPL    Call DosEnterCritSec and DosExitCritSec to bracket
;                       critical section.
;P2847  88/06/01  THG   Call Win API with logical handle -or- Win_Handle
;@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 WORD PUBLIC 'CODE'
CSEG    ENDS

IDSEG   SEGMENT WORD PUBLIC 'DATA'      ; Per process instance data segment
IDSEG   ENDS
;
;       System services routine name(s)
;
        EXTRN   DOSDEVIOCTL:FAR         ; Set codepage
        EXTRN   KBDSETFGND:FAR          ; Keyboard set foreground
        EXTRN   DOSENTERCRITSEC:FAR     ; Enter critical section
        EXTRN   DOSEXITCRITSEC:FAR      ; Exit critical section

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

        PUBLIC  GetFocus

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

;/*********************** START OF SPECIFICATIONS ***************************/
;/* SUBROUTINE NAME: GetFocus                                               */
;/*                                                                         */
;/* DESCRIPTIVE NAME: BKS function for KBDGETFOCUS                          */
;/*                                                                         */
;/* FUNCTION: Bind the logical keyboard to the physical keyboard.           */
;/*                                                                         */
;/* NOTES: Once a bond occurs, all keystrokes from the physical keyboard    */
;/*        will go to the process which issued the KBDGETFOCUS call.        */
;/*        This scenario will continue until the                            */
;/*                                                                         */
;/* ENTRY POINT: bksgetf                                                    */
;/*     LINKAGE: NEAR CALL  (internal call from BKSMAIN)                    */
;/*                                                                         */
;/* INPUT: push KBDHANDLE - keyboard handle (WORD)                          */
;/*        push callersCS - caller's return CS (WORD)                       */
;/*        push callersIP - caller's return IP (WORD)                       */
;/*        push func_code - KBDXXX function code (WORD)                     */
;/*        push KBDXXX_IP - KBDXXX return IP (WORD)                         */
;/*        push callersDS - caller's DS save area (WORD)                    */
;/*        push kbdroutCS - kbdrout return CS (WORD)                        */
;/*        push kbdroutIP - kbdrout return IP (WORD)                        */
;/*        push BKSMAINIP - BKSMAIN return IP (WORD)                        */
;/*        push DS        - done by BKSMAIN                                 */
;/*        push BP        - done by BKSMAIN                                 */
;/*                                                                         */
;/*        NOTE:  On entry to this routine, it is assumed that DS           */
;/*               has been set to the IDSEG data selector                   */
;/*               by BKSMAIN.                                               */
;/*               Also, it is assumed that BP has been set to the           */
;/*               stack frame (described above) by BKSMAIN.                 */
;/*                                                                         */
;/* EXIT-NORMAL: AX = NO_ERROR                                              */
;/*                                                                         */
;/* EXIT-ERROR: AX = error code from DOSDEVIOCTL                            */
;/*             AX = ERROR_KBD_UNABLE_TO_FOCUS                              */
;/*                                                                         */
;/* EFFECTS: Caller's AX is destroyed.                                      */
;/*                                                                         */
;/* INTERNAL                                                                */
;/* REFERENCES: bond_handle - keyboard handle that currently has a bond     */
;/*   ROUTINES: None.                                                       */
;/*                                                                         */
;/* EXTERNAL                                                                */
;/* REFERENCES: None.                                                       */
;/*   ROUTINES: DOSDEVIOCTL - close device handle                           */
;/*                                                                         */
;/*********************** END OF SPECIFICATIONS *****************************/
        PAGE



; GetFocus PSUEDO CODE
;
;************************************************************************
;*  On entry:  DS = IDSEG selector   AX = 0   BX = [bp].KBD_Handle      *
;************************************************************************
;
; DS:Bond_Handle <-- [BP].KBD_Handle    ; Keep track of bond handle
;.if <[BP].KBD_Handle ne DEFAULT>       ; Non default handle bond?
;|  call DOSDEVIOCTL                    ; Set KCB
;.endif
;.if <AX eq NO_ERROR>
;|  DS:Flag <- FOCUS_ON                 ; Indicate focus ON
;.else                                  ; Attempt failed
;|  DS:Bond_Handle <-- 0                ; Reset bond handle
;|  AX <-- ERROR_KBD_UNABLE_TO_FOCUS
;.endif
;
; ret
        PAGE



GetFocus PROC NEAR                      ; @@ Version 1.1 code - BEGIN
        ASSUME DS:IDSEG
;
;************************************************************************
;*  On entry:  DS = IDSEG selector   AX = 0   BX = [bp].KBD_Handle      *
;*             ES = GDSEG            DX = KBD_SG                        *
;************************************************************************
;

;$$#I001
       .IF <dx A LastBaseSG>           ; Winthorn screen group?
           push  hConsole              ;@@DTF1
           push  bx                     ; User supplied handle          ;P2847
           call  ES:WINCONSOLEGETFOCUS  ;CDB2
       .ELSE
;$$#END

        call    DOSENTERCRITSEC         ;
        mov     Bond_Handle,bx          ; Keep track of bond handle
        or      bx,bx                   ;
       .if nz                           ; Non default handle bond?
         push   0                       ; Address to DATA parmlist
         push   0                       ;
         push   ds                      ; Address to INPUT parmlist
         push   offset Bond_Handle      ;
         push   57H                     ; Function code (set KCB)
         push   KEYBOARD                ; Category code (KEYBOARD)
         push   bx                      ; Device handle
         call   DOSDEVIOCTL             ;
       .endif                           ;
        or      ax,ax                   ;
       .if z                            ;
         or     Flag,FOCUS_ON           ; Indicate focus ON
         call   KBDSETFGND              ; Make sure current process belonging
         sub    ax,ax                   ;   to the foreground screen group
                                        ;   gets foreground priority
       .else                            ;
         mov    Bond_Handle,0           ; Reset bond handle
         mov    ax,ERROR_KBD_UNABLE_TO_FOCUS
       .endif                           ;
        push    ax                      ; Save return code
        call    DOSEXITCRITSEC          ;
        pop     ax                      ; Restore return code
;$$#I002
       .ENDIF
;$$#END
        ret                             ; Near return to BKSMAIN

GetFocus ENDP                           ; @@ Version 1.1 code - END

CSEG    ENDS
        END
