;SCCSID = @(#)bvsmain.asm       6.12 92/03/26
        PAGE    58,132
        TITLE   BvsMain.Asm -- Base Video Subsystem Main Entry Point
; ****************************************************************************
; *                                                                          *
; *                       IBM/Microsoft Confidential                         *
; *                                                                          *
; *                 Copyright (c) IBM Corporation  1987, 1990                *
; *                 Copyright (c) Microsoft Corp.  1987, 1990                *
; *                           All Rights Reserved                            *
; *                                                                          *
; ****************************************************************************

;/*********************** START OF SPECIFICATIONS ***********************/
;/*                                                                     */
;/* SOURCE FILE NAME:   BvsMain.Asm                                     */
;/*                                                                     */
;/* DESCRIPTIVE NAME:   Base Video Subsystem                            */
;/*                                                                     */
;/* COPYRIGHT:  na                                                      */
;/*                                                                     */
;/* STATUS:     na                                                      */
;/*                                                                     */
;/* NOTES:      Executes on level 3 - Protect Mode ONLY                 */
;/*                                                                     */
;/*    DEPENDENCIES:    None                                            */
;/*                                                                     */
;/*    RESTRICTIONS:    None                                            */
;/*                                                                     */
;/*    PATCH LABEL:     na                                              */
;/*                                                                     */
;/* ENTRY POINT: BVSMAIN                                                */
;/*                                                                     */
;/* EXTERNAL REFERENCES:  DOSDEVIOCTL, DOSOPEN                          */
;/*                                                                     */
;/************************ END OF SPECIFICATIONS ************************/

        INCLUDE bvscb.inc               ;BVS Data Structures
        INCLUDE bvsparms.inc            ;BVS Parameter Equates
        INCLUDE error2.inc              ;CP/DOS Error Codes
        INCLUDE infoseg.inc             ;DOS Info Seg
        .xlist
        INCLUDE basemaca.inc            ;;;;;; 2.0 unique (dosmac replacement)
        INCLUDE struc.inc               ;Structured Macros
        INCLUDE dc1segs.inc                                             ;MDLL
        .list

        .386p                   ;@D549

;STJFMT,,16                             ;Formatting parms for STJFMT

        extrn   _end_CDATA:BYTE                                         ;@T52

;***********************************************************************;@T73
;       Besure to make corresponding changes in BVSCB.INC and           ;@T73
;       BVSLDRVR.H whenever BVSGSEG is changed.                         ;@T73
;       Also remake the BVH DLLs.                                       ;
;***********************************************************************;@T73

BVSGSEG SEGMENT WORD PUBLIC 'FAR_DATA' USE16 ;Global data segment @D549;@T52;MDLL

BVSGLOBAL       label   far             ;Start of global data           ;@T52
                public  BVSGLOBAL
bvsg_calltables dd      16 dup (0)      ;Allow for WDH and 15 devices   ;@T52
                public  bvsg_calltables                                 ;@T52
bvsg_flags      dw      0               ;Global flags                   ;@T52
                public  bvsg_flags                                      ;@T52
bvsg_instance   dw      0               ;Instance Selector              ;@T52
                public  bvsg_instance                                   ;@T52
bvsg_bvscbsize  dw      0FFFEh AND (1+SIZE bvscb) ;Size of the BVSCB    ;@T52
                public  bvsg_bvscbsize                                  ;@T52
bvsg_SISsel     dw      0               ;System InfoSeg GDT selector    ;@T52
                public  bvsg_SISsel                                     ;@T52
bvsg_LISsel     dw      0               ;Local InfoSeg LDT selector     ;@T52
                public  bvsg_LISsel                                     ;@T52
bvsg_maxbase    dw      0               ;Maximum Base session number    ;@T52
                public  bvsg_maxbase                                    ;@T52
bvsg_initpid    dw      0               ;PID at initialization time     ;@T52
                public  bvsg_initpid                                    ;@T52
bvsg_scrhandle  dw      0               ;BVS File Handle                ;@T52
                public  bvsg_scrhandle                                  ;@T52
bvsg_sg0flag    dw      0               ;SG0 on behalf of other SG      ;@T52
                public  bvsg_sg0flag                                    ;@T52
bvsg_sg0bvssel  dw      0               ;SG0 BVSCB selector             ;@T52
                public  bvsg_sg0bvssel                                  ;@T52
bvsg_sg0envsel  dw      0               ;SG0 ENVB selector              ;@T52
                public  bvsg_sg0envsel                                  ;@T52
bvsg_sg0lvbsel  dw      0               ;SG0 LVB selector               ;@T52
                public  bvsg_sg0lvbsel                                  ;@T52
bvsg_open_a     dw      0               ;Open action return field       ;@T52
                public  bvsg_open_a                                     ;@T52
bvsg_prtsclen   dw      0               ;Print screen length            ;@T52
                public  bvsg_prtsclen                                   ;@T52
bvsg_prtsc_buffer db    134 dup (' ')   ;Print screen buffer            ;@T52
                public  bvsg_prtsc_buffer                               ;@T52
bvsg_cprtemp    db      01Bh,'[00;00R',00Dh ;Template for CurPos Report * Do  ;@T52
                public  bvsg_cprtemp                                    ;@T52
bvsg_bvscbname  db      '\SHAREMEM\BVS\BVSsg',0 ;Name of BVSCB          * Not ;@T52
                public  bvsg_bvscbname                                  ;@T52
bvsg_mouddname  db      'MOUSE$',0      ;Mouse DD name            ;@T25 * Move;@T52
                public  bvsg_mouddname                                  ;@T52
bvsg_prtname    db      'PRN',0         ;Printer name asciiz string     ;@T52
                public  bvsg_prtname                                    ;@T52
bvsg_scrddname  db      'SCREEN$',0     ;Screen DD name                 ;@T52
                public  bvsg_scrddname                                  ;@T52
bvsg_blanks     db      8 dup (' ')     ;Blanks for tab removal         ;@T52
                public  bvsg_blanks                                     ;@T52
bvsg_3XBcpos    dw      0               ;3xBox cursor position save area;@T52
                public  bvsg_3XBcpos                                    ;@T52
bvsg_parmpkt    db      0               ; parameter packet area         ;@T52
                public  bvsg_parmpkt                                    ;@T52
bvsg_pjtitlelen dw      ?               ; length of print job title     ;@T52
                public  bvsg_pjtitlelen                                 ;@T52
bvsg_pjtitleoff dw      ?               ; offset of print job title     ;@T52
                public  bvsg_pjtitleoff                                 ;@T52
bvsg_pjtitlesel dw      ?               ; segment of print job title    ;@T52
                public  bvsg_pjtitlesel                                 ;@T52
bvsg_cprtsc_title db    'Ctrl PrtSc',0  ; asciiz of cprtsc job title    ;@T52
                public  bvsg_cprtsc_title                               ;@T52
bvsg_cprtsc_titlelen db $ - bvsg_cprtsc_title ;length of title          ;@T52
                public  bvsg_cprtsc_titlelen                            ;@T52
bvsg_prtsc_title db    'Print Screen',0 ; asciiz of cprtsc job title    ;@T52
                public  bvsg_prtsc_title                                ;@T52
bvsg_prtsc_titlelen db  $ - bvsg_prtsc_title  ;length of title          ;@T52
                public  bvsg_prtsc_titlelen                             ;@T52
bvsg_vdd1       db      'VVIDEO1$',0    ;primary display for MVDM       ;@C39
                public  bvsg_vdd1                                       ;@C39
bvsg_vdd2       db      'VVIDEO2$',0    ;secondary display for MVDM     ;@C39
                public  bvsg_vdd2                                       ;@C39
                db      0               ;Word alignment                 ;@T73
bvsg_vddhandle  dd      0               ;MVDM VDD handle                ;@C39
                public  bvsg_vddhandle                                  ;@C39
bvsg_seltablesz dw      0               ;Selector table entry size      ;@T52
                public  bvsg_seltablesz                                 ;@T52
bvsg_seltable   dd      256 dup (0)     ;Selector table offset          ;@T52
                public  bvsg_seltable                                   ;@T52

WndwConfig      configdata <>           ;Config data for VIO windowable ;@T45
                public  WndwConfig      ;                               ;@T45

iLastVDH        dw      0               ;@P1    ;Last VDH initialized (Count of VDHs)
                public  ILASTVDH        ;@P1

MaxRedrawSize   dd      0               ;Maximum Redraw size required   ;@P1
                public  MaxRedrawSize   ;@P1

;@T52 __acrtused      dw      0               ;@P1
;@T52                 public  __acrtused      ;@P1

szDefEntry      db      'DEVENABLE',0   ;@P1
                public  SZDEFENTRY      ;@P1
szVideoDevices  db      'VIDEO_DEVICES',0 ;D261
                public  SZVIDEODEVICES  ;@P1;D261
szDeviceTag     db      'DEVICE(',0     ;D261
                public  SZDEVICETAG     ;@P1;D261
szPtrDevRTag    db      'PTRDEVR(',0    ;D261
                public  SZPTRDEVRTAG    ;@P1
szPtrDevPTag    db      'PTRDEVP(',0    ;D261
                public  SZPTRDEVPTAG    ;@P1
szGeneric8514   db      'BVHVGA,BVH8514A',0 ;D261                       ;@T38
                public  SZGENERIC8514   ;D261
szGenericVGA    db      'BVHVGA',0      ;@P1                            ;@T38
                public  SZGENERICVGA    ;@P1
szGenericEGA    db      'BVHEGA',0      ;@P1                            ;@T38
                public  SZGENERICEGA    ;@P1
szGenericCGA    db      'BVHCGA',0      ;@P1                            ;@T38
                public  SZGENERICCGA    ;@P1
szGenericVDH    db      'BVHINIT',0     ;@P1                            ;@T38
                public  SZGENERICVDH    ;@P1

szDefPtrDev     db      'POINTER$'      ;@P1                            ;@T25
                public  SZDEFPTRDEV     ;@P1
szDevPrefix     db      0               ;@P1                            ;@T25
                public  SZDEVPREFIX     ;@P1

;@C50 szAnsiMod       db      'ANSICALL',0    ;Ansicall module name     ;@T25
;@C50                 public  SZANSIMOD       ;                         ;@T25
;@C50 szMouMod        db      'MOUCALLS',0    ;Moucall module name      ;@T25
;@C50                 public  SZMOUMOD        ;                         ;@T25
;@C50 szMouInitReal   db      'MOUINITREAL',0 ;MouInitReal entry name   ;@T25
;@C50                 public  SZMOUINITREAL   ;                         ;@T25

maskFillDevBlk  dd      0               ;@P1
fFillDevBlk     dd      0               ;@P1
pszGenericVDH   dd      0               ;@P1
pchGenCallTbl   dd      0               ;@P1
GlobalFlags     dw      0               ;@P1

                public  MASKFILLDEVBLK  ;@P1
                public  FFILLDEVBLK     ;@P1
                public  PSZGENERICVDH   ;@P1
                public  GLOBALFLAGS     ;@P1
                public  PCHGENCALLTBL   ;@P1

; These pointers will be assigned as the strings are dynamically allocated

cVideoDevices   dw      0               ;D261
                public  CVIDEODEVICES   ;D261
pszDeviceString dd      ccalltbls dup (0) ;@P1
                public  PSZDEVICESTRING ;@P1
pszProtPtrNames dd      ccalltbls dup (BVSGSEG:szDefPtrDev) ;@P1;D261
                public  PSZREALPTRNAMES ;@P1
pszRealPtrNames dd      ccalltbls dup (BVSGSEG:szDefPtrDev) ;@P1;D261
                public  PSZPROTPTRNAMES ;@P1

pDevGroups      dd      ccalltbls dup (0) ;D261
                public  PDEVGROUPS      ;D261
cDevGroups      dw      0               ;@P1; number of VDH Blocks
                public  CDEVGROUPS      ;@P1

pszWindowBVH    dd      0               ;@S42
                public  PSZWINDOWBVH    ;@S42
szWindowBVH     db      'BVHWNDW',0     ;@S42
                public  SZWINDOWBVH     ;@S42
szVideoWindow   db      'VIDEO_WINDOW',0 ;@S42
                public  SZVIDEOWINDOW   ;@S42

fShellInited    db      0               ;Flag completion of Shell Init ;@S10
                public  FSHELLINITED    ;Also used for word alignment ;@S42;@P1

cbBvsGSeg       dw      _end_CDATA+1    ;size of CDATA segment          ;@T52
                public  CBBVSGSEG       ;@P1

;   stuff past here needs to be dynamically allocated

BVSGSEG         ENDS


BVSISEG SEGMENT WORD PUBLIC 'FAR_DATA' USE16 ;Instance data segment @D549;@T52;MDLL
                                                                        ;@T52
; start of BVH instance data            ;BESURE TO UPDATE BVSCB.INC     ;@T52
                                        ;  ALSO WHEN MAKING CHANGES     ;@T52
BVHINSTANCE     label   far             ;  TO BVH OR WDH INSTANCE       ;@T52
                public  BVHINSTANCE     ;  DATA                         ;@T52
                dw      0               ;bvhi_ScreenDD                  ;@T52
                dw      0               ;bvhi_PVBAddrLo                 ;@T52
                dw      0               ;bvhi_PVBAddrHi                 ;@T52
                dw      0               ;bvhi_PVBSizeLo                 ;@T52
                dw      0               ;bvhi_PVBSizeHi                 ;@T52
                dw      0               ;bvhi_PVBOffset                  @T52
                dw      0               ;bvhi_PVBSel                    ;@T52

; start of BVHWNDW instance data

                dw      0               ;LastBaseSG                     ;@T52
                dw      0               ;Scratch_Area1                  ;@T52
                dw      0               ;Scratch_Area2                  ;@T52
                dw      0               ;save_viohandle                 ;@T52
                dw      0               ;ppbvs_sema4                    ;@T52
                dw      0               ;ppbvs_pssel                    ;@T52
                dw      2               ;cell_size                      ;@T52
                dw      0               ;attr1                          ;@T52
                dw      0               ;attr2                          ;@T52
                dw      129 dup (0)     ;addr_list                      ;@@SW
                dw      0               ;ppbvs_bvssel                   ;@T52
                dw      0               ;ppbvs_lvbsel                   ;@T52
                dw      0               ;ppbvs_gdtsel                   ;@T52
                dw      0               ;ppbvs_ldtsel                   ;@T52
                db      32 dup (0)      ;wdhi_wininits                  ;@T52
                dw      0               ;wdhi_start_row                 ;@T52
                dw      0               ;                               ;@T52
                dw      0               ;wdhi_start_col                 ;@T52
                dw      0               ;                               ;@T52
                dw      0               ;wdhi_length_width              ;@T52
                dw      0               ;                               ;@T52
                dw      0               ;wdhi_rect_height               ;@T52
                dw      0               ;                               ;@T52
                dw      0               ;wdhi_horiz_count               ;@T52
                dw      0               ;                               ;@T52
                dw      0               ;wdhi_vert_count                ;@T52
                dw      0               ;                               ;@T52
                dw      0               ;wdhi_fill_cell_off             ;@T52
                dw      0               ;wdhi_fill_cell_sel             ;@T52
                dd      0               ;wdhi_Scratch                   ;@T52
;               dd      0               ;avio_entry                     ;@T52
;               dw      54h dup (0)     ;avio_filler                    ;@T52
                dw      0FFFEh AND (1+SIZE bvscb) ;cmn_bvscbsize        ;@T52
                dw      0               ;cmn_SISsel        ;;;;;;;;;;;;;;@T52
                dw      0               ;cmn_LISsel        ;;;;;;;;;;;;;;@T52
                                                           ;;;          ;@T52
; start of BVS instance data                               ;;; MUST     ;@T52
                                                           ;;;          ;@T52
BVSINSTANCE     label   far             ;                  ;;; BE       ;@T52
                public  BVSINSTANCE                        ;;;          ;@T52
bvsi_bvssel     dw      0               ;BVSCB Selector    ;;; KEPT     ;@T52
                public  bvsi_bvssel                        ;;;          ;@T52
bvsi_envsel     dw      0               ;ENVB Selector     ;;; TOGETHER ;@T52
                public  bvsi_envsel                        ;;;          ;@T52
bvsi_lvbsel     dw      0               ;LVB Selector      ;;;          ;@T52
                public  bvsi_lvbsel                        ;;;;;;;;;;;;;;@T52
bvsi_sgnum      dw      0               ;Screen group number            ;@T52
                public  bvsi_sgnum                                      ;@T52
bvsi_gdtsel     dw      0               ;DOS Info GDT Selector          ;@T52
                public  bvsi_gdtsel                                     ;@T52
bvsi_ldtsel     dw      0               ;DOS Info LDT Selector          ;@T52
                public  bvsi_ldtsel                                     ;@T52
bvsi_level      dw      0               ;Level of Recursion             ;@T52
                public  bvsi_level                                      ;@T52
bvsi_flags      dw      0               ;Per Process flags              ;@T52
                public  bvsi_flags                                      ;@T52
bvsi_pvbsel     dw      0               ;PVB Selector                   ;@T52
                public  bvsi_pvbsel                                     ;@T52
bvsi_biosel     dw      0               ;BIOS Selector                  ;@T52
                public  bvsi_biosel                                     ;@T52
bvsi_physaddrl  dw      0               ;Physical address of            ;@T52
                public  bvsi_physaddrl                                  ;@T52
bvsi_physaddrh  dw      0               ; video memory buffer           ;@T52
                public  bvsi_physaddrh                                  ;@T52
bvsi_length     dw      0               ;Length of video buffer         ;@T52
                public  bvsi_length                                     ;@T52
bvsi_lenhigh    dw      0               ;Length of video buffer         ;@T52
                public  bvsi_lenhigh                                    ;@T52
bvsi_srflen     dw      0               ;Length return area for BVSSRF  ;@T52
                public  bvsi_srflen                                     ;@T52
bvsi_retlen     dw      0               ;Length return area             ;@T52
                public  bvsi_retlen                                     ;@T52
bvsi_scrhandle  dw      0               ;Alternate handle for SCR DD    ;@T52
                public  bvsi_scrhandle                                  ;@T52
bvsi_prthandle  dw      0               ;Handle for print requests      ;@T52
                public  bvsi_prthandle                                  ;@T52
bvsi_pop_sgnum  dw      0               ;Original SG number             ;@T52
                public  bvsi_pop_sgnum                                  ;@T52
bvsi_pop_bvssel dw      0               ;Original BVSCB sel             ;@T52
                public  bvsi_pop_bvssel                                 ;@T52
bvsi_pop_popsel dw      0               ;Previous popup selector        ;@T52
                public  bvsi_pop_popsel                                 ;@T52
bvsi_pop_vdh    dw      0               ;1 - fullscreen VDH(s) loaded   ;@T52
                public  bvsi_pop_vdh                                    ;@T52
bvsi_tty_dbcs   db      0               ;dbcs flag for TTY              ;@T52
                public  bvsi_tty_dbcs                                   ;@T52
bvsi_tty_dbcsl  db      0               ;dbcs leading byte              ;@T52
                public  bvsi_tty_dbcsl                                  ;@T52
bvsi_tty_dbcst  db      0               ;dbcs trailing byte             ;@T52
                public  bvsi_tty_dbcst                                  ;@T52
bvsi_tty_flag   db      0               ;dbcs flags for TTY             ;@T52
                public  bvsi_tty_flag                                   ;@T52
bvsi_register   dw      0               ;Registration info from AX      ;@T52
                public  bvsi_register                                   ;@T52
bvsi_sg1_bvssel dw      0               ;Saved BVSCB                    ;@T71
                public  bvsi_sg1_bvssel                                 ;@T71
                dw      0               ;Saved flag                     ;@T71
                dw      0               ;Saved environment              ;@T71
                dw      0               ;Saved LVB                      ;@T71

ptmpmemBlock    dd      0               ;pointer to temporary storage block
                public  PTMPMEMBLOCK
cTmpMemUseCount dw      0               ;number of temp buffers in use
                public  CTMPMEMUSECOUNT

fDevValid       db      16 dup (0)      ;device valid in context flags
                public  FDEVVALID
fPerProcDataValid db    0               ;initialized per process data flag
                public  FPERPROCDATAVALID
fIsGenericVDHInited db  0               ;generic VDH initialized flag
                public  FISGENERICVDHINITED

;@C50 THUNKDEF        MOUCLOSE, szMouMod,  Mou_Close ;@T25
;@C50 THUNKDEF        MOUOPEN, szMouMod,  Mou_Open ;@T25

BVSISEG         ENDS

;STJFMT,                                ;Formatting parms for STJFMT

        extrn   DOSFREESEG:FAR          ;Dos Free Segment routine
        extrn   DOSGETSEG:FAR           ;Dos Get Segment access

        extrn   DOSSLEEP:FAR            ;Dos Sleep routine ;@151935

        extrn   DOSOPEN:FAR             ;Dos File Open routine
        extrn   DOSCLOSE:FAR            ;Dos File Close routine
        extrn   DOSDEVIOCTL:FAR         ;Dos Generic I/O Control routine
        EXTRN   DOSSMGETMEM:FAR         ;Dos SesMgr Get Memory ;@D549

        extrn   BvsGetPhysBuf:NEAR
        extrn   BvsGetBuf:NEAR
        extrn   BvsShowBuf:NEAR
        extrn   BvsGetConfig:NEAR       ;Entry points in BvsBufs.Asm

        extrn   BvsGetCurPos:NEAR
        extrn   BvsGetCurType:NEAR
        extrn   BvsSetCurPos:NEAR
        extrn   BvsSetCurType:NEAR      ;Entry points in BvsCursr.Asm

        extrn   BVSDINIT:FAR            ;Entry points in BvsDInit.C

        extrn   GetBVSCB:NEAR
        extrn   InitConfig:NEAR         ;B703158
        extrn   BvsInit:NEAR            ;Entry points in BvsInit.Asm

        extrn   BVSGETVIDEOCALLTABLES:FAR ;Entry points in BvsLDrvr.C

        extrn   BvsPrtSc:NEAR
        extrn   BvsTogglePrtSc:NEAR
        extrn   BvsGetAnsi:NEAR
        extrn   BvsSetAnsi:NEAR
        extrn   BvsGetCP:NEAR
        extrn   BvsSetCP:NEAR           ;Entry points in BvsPrt.Asm

        extrn   BvsSave:NEAR
        extrn   BvsRestore:NEAR
        extrn   BvsFree:NEAR            ;Entry Points in BvsSRF.Asm

        extrn   BvsGetMode:NEAR
        extrn   BvsSetMode:NEAR         ;Entry Points in BvsMode.Asm

        extrn   BvsReadCellStr:NEAR
        extrn   BvsReadCharStr:NEAR
        extrn   BvsScrollUp:NEAR
        extrn   BvsScrollDn:NEAR
        extrn   BvsScrollLf:NEAR
        extrn   BvsScrollRt:NEAR
        extrn   BvsWrtCellStr:NEAR
        extrn   BvsWrtCharStr:NEAR
        extrn   BvsWrtCharStrAtt:NEAR
        extrn   BvsWrtNAttr:NEAR
        extrn   BvsWrtNCell:NEAR
        extrn   BvsWrtNChar:NEAR        ;Entry Points in BvsSubs.Asm

        extrn   BvsWrtTTY:NEAR          ;Entry Points in BvsTTY.Asm

        extrn   BvsGetFont:NEAR
        extrn   BvsSetFont:NEAR         ;Entry Points in BvsFont.Asm

        extrn   BvsGetState:NEAR
        extrn   BvsSetState:NEAR        ;Entry Points in BvsState.Asm

        extrn   ManageCAS:NEAR          ;@P1
        extrn   BvsCreateCA:NEAR        ;@P1
        extrn   BvsGetCAState:NEAR      ;@P1
        extrn   BvsSetCAState:NEAR      ;@P1
        extrn   BvsDestroyCA:NEAR       ;@P1
        extrn   BvsCheckCharType:NEAR   ;Entry Points in BvsExt.Asm ;@P1

        extrn   MOUOPEN:FAR             ;@C50
        extrn   MOUCLOSE:FAR            ;@C50
;@C50;; extrn   Thunker:NEAR            ;                               ;@T25
;@C50;; extrn   ThunkerFarRet:FAR       ;                               ;@T25



CTEXT_TUNE5 SEGMENT BYTE PUBLIC 'CODE' USE16                            ;MDLL;@D549
        ASSUME  CS:CODE02                                               ;MDLL

;/*********************** START OF SPECIFICATIONS ***********************/
;/*                                                                     */
;/* SUBROUTINE NAME:  BVSMAIN                                           */
;/*                                                                     */
;/* DESCRIPTIVE NAME:  Base Video Subsystem Main Program                */
;/*                                                                     */
;/* FUNCTION:  Routing of all functions for the Base Video Subsystem    */
;/*                                                                     */
;/* NOTES:  Executes on Level 3                                         */
;/*         BX=0 throughout BVS to optimize addressing and compares     */
;/*                                                                     */
;/* ENTRY POINT:  BVSMAIN                                               */
;/*   LINKAGE:  Far Call to BVSMAIN from VIOROUT                        */
;/*                                                                     */
;/* INPUT:  ParmList structure as defined in BvsParms.Inc               */
;/*                                                                     */
;/*   Registers to BvsXxxx:      DS      Selector of BVSCB              */
;/*                              ES      Selector of PPBVSCB            */
;/*                              BP      pointer to StackFrame above    */
;/*                                                                     */
;/* EXIT-NORMAL:  AX = 0                                                */
;/*                                                                     */
;/* EXIT-ERROR:  AX = VIOxxx return code                                */
;/*                   ERROR_VIO_INVALID_HANDLE                          */
;/*                   ERROR_VIO_INTERNAL_RESOURCE                       */
;/*                                                                     */
;/* EFFECTS:  None                                                      */
;/*                                                                     */
;/* INTERNAL REFERENCES:  BVSsg = Base Video Subsystem Control Block    */
;/*                     PPBVSCB = Per Process BVSCB (Instance Data)     */
;/*                       LVBsg = Logical Video Buffer per Screen Group */
;/*                                                                     */
;/*   ROUTINES: BvsGetPhysBuf, BvsGetBuf, BvsShowBuf, BvsGetCurPos,     */
;/*             BvsGetCurType, BvsGetMode, BvsSetCurPos, BvsSetCurType, */
;/*             BvsSetMode, BvsReadCharStr, BvsReadCellStr,             */
;/*             BvsWrtNChar, BvsWrtNAttr, BvsWrtNCell, BvsWrtCharStr,   */
;/*             BvsWrtCharStrAtt, BvsWrtCellStr, BvsWrtTTY,             */
;/*             BvsScrollUp, BvsScrollDn, BvsScrollLf, BvsScrollRt,     */
;/*             BvsGetANSI, BvsSetANSI, BvsGetCP, BvsSetCP, BvsInit,    */
;/*             PVBSelector, LoadWinthorn, BvsQueryConsole              */
;/*                                                                     */
;/* EXTERNAL REFERENCES:  None                                          */
;/*   ROUTINES: AvioSubs, BvsDInit                                      */
;/*                                                                     */
;/************************ END OF SPECIFICATIONS ************************/

;Begin BvsMain
;    If ShellInit not yet received
;        Call BvsDInit in BVSISEG (discardable segment)
;    Endif
;    If not ShellInit
;        If call on behalf of another session
;            Change current session to target session
;        Endif
;        If per process data not yet initialized
;            Call BvsInit to initialize this process
;        Endif
;        If Winthorn unique request
;            Rout to Winthorn routines (AVioSubs, QueryConsole, CreatePS)
;        Else
;            Call BvsXxx routine according to the function code from VIOROUT
;        Endif
;    Endif
;End BvsMain

BVSMAIN PROC    FAR
        PUBLIC  BVSMAIN

        pushf                           ;D198
        pusha
        push    ds
        push    es
        push    gs                                                        ;@T80
        cld
        sub     sp,p_regfs              ;Allocate VDH Parameter block ;198;@D549
        mov     bp,sp                   ;Get parameter list base

        sub     bx,bx                   ;Get zero reg for code optimization
        mov     [bp].vp_parmlength,8    ;Set default Parameter block length
        mov     [bp].vp_flags,bx        ;Set default Parameter block flags

        mov     cx,BVSISEG              ;@S22
        mov     ds,cx                   ;DS = Instance data ;@S22
        mov     ds:bvsi_register,ax     ;Save VioRegister flag ;@S22    ;@T52
        mov     ax,BVSGSEG
        mov     es,ax                   ;ES = Global data

;----------------------------------------------------------------------
;Setup anchors in the Global data segment as needed
;----------------------------------------------------------------------

        .if     <es:fShellInited eq bl> ;Shell Init not yet successful ;@S10
            mov     es:bvsg_instance,ds ;Anchor Instance to Global ;@S10;@T52
            push    [bp].p_viofunc
            Call    BVSDINIT            ;Perform Pre-Init processing
            .if     <nonzero ax>        ; @@TB3 (uncommented these 3 LOCs
                jmp     finis           ; @@TB3
            .endif                      ; @@TB3
            .if   <[bp].p_viofunc eq vf_ShellInit> and ;@861 If Shell init....
            .if   <es:WndwConfig.cf_adapter ne bx>     ;@861 If WndwConfig was
              mov   [bp].vp_offset, OFFSET WndwConfig  ;@861 set to generic VDH,
              mov   [bp].vp_selector, es               ;@861 Reset it with the
              mov   word ptr es:WndwConfig, size configdata ;@861
              mov   di, fn_GetConfig                   ;@861 current VDH config.
              call  ALLVDH                             ;@861
            .endif                                     ;@861
        .endif                          ;ShellInit->BvsDinit->BvsInit->finis

;----------------------------------------------------------------------
;Copy Global selectors to Instance data, as needed
;----------------------------------------------------------------------

        .if     <ds:bvsi_gdtsel eq bx>                                  ;@T52
            mov     ax,es:bvsg_SISsel                                   ;@T52
            mov     ds:bvsi_gdtsel,ax   ;System Info Seg                ;@T52
            mov     ax,es:bvsg_LISsel                                   ;@T52
            mov     ds:bvsi_ldtsel,ax   ;System Info Seg                ;@T52

            push    ds                  ;                       @T19    ;@T71
            mov     ds,ax               ;                       @T19    ;@T71
            mov     ax,ds:[bx].LIS_CurScrnGrp ;                 @T19    ;@T71
            pop     ds                  ;  will load            @T19    ;@T71
            mov     ds:bvsi_sgnum,ax    ;Setup sgnum so VDHWNDW @T19    ;@T52
            sub     eax,eax                                             ;@T71
            push    es
            push    offset bvsg_calltables ;table of far ptrs to call tbls;@T52
;@T71       push    bx                  ;pointer to list of protect mode
            push    eax                 ;  pointer draw device names    ;@T71
;@T71       push    bx                  ;pointer to list of real mode
            push    eax                 ;  pointer draw device names    ;@T71
            Call    BVSGETVIDEOCALLTABLES ;Init all VDH's for this task

            .if     <nonzero ax>        ; @@TB3  Prepare to give a hard
                mov     ds:bvsi_gdtsel,0                                ;@C43
                jmp     finis           ; @@TB3   error popup if any error.
            .endif                      ; @@TB3
            .if     <ds:bvsi_sgnum eq 3>                                ;@T14f
                mov     ds:bvsi_sgnum,bx ;Clear it for BVSINIT @T19,@T52,@T14f
            .endif                                                      ;@T14f
        .endif

;----------------------------------------------------------------------
;Perform special processing required at Shell Initialization time.
;    This includes getting the InfoSeg selectors and pre-allocating the
;    control blocks for session 0 thru 3.
;----------------------------------------------------------------------

        .if     <[bp].p_viofunc eq vf_ShellInit> NEAR
            push    ds
            mov     ds,ds:bvsi_ldtsel                                   ;@T52
            mov     ax,[bx].LIS_CurProcID
            pop     ds
            mov     es:bvsg_initpid,ax  ;Save in common segment         ;@T52
            sub     eax,eax                                             ;@T71
            push    es
            push    offset bvsg_mouddname ;Device name ASCIIZ string    ;@T52
            push    ds
            push    offset bvsi_prthandle ;Return for device handle     ;@T52
            push    ds
            push    offset bvsi_retlen    ;Action Taken                 ;@T52
;@T71       push    bx
            push    eax                 ;File Size                      ;@T71
;@T71       push    bx                  ;File Attribute
            push    dword ptr 00001h    ;Open Flag                      ;@T71
            push    000C2h              ;Open Mode
;@T71       push    bx
            push    eax                 ;Reserved (0:0)                 ;@T71
            Call    DOSOPEN
            .if     <zero ax>
                or      es:bvsg_flags,bvsg_f_mouactive  ;Mark active    ;@T52
                push    ds:bvsi_prthandle                               ;@T52
                Call    DOSCLOSE        ;Close the Pointer DD
            .endif
            mov     ds:bvsi_prthandle,bx                                ;@T52

            sub     si,si

            .repeat
                mov     ds:bvsi_bvssel,bx       ;Clear for init         ;@T18
                Call    GetBVSCB        ;Alloc Session SI, Error --> finis
                push    ds              ;D261
                mov     di,si           ;shift index by 4 to align Dword;@D549
                shl     di,2                                            ;@D549
                lgs     di,es:bvsg_seltable[di]                    ;@T52,@T80
                mov     ds,gs:[di].selt_bvscb                      ;@S10,@T80
                mov     [bx].bvs_bvssize,bx ;Force reinit of VDH
                pop     ds              ;D261

                inc     si
                .if     <si e 2>        ;skip Real mode session         ;@D549
                    inc     si                                          ;@D549
                    mov     dx,ds:bvsi_bvssel   ;Stop at SGCB for SG 1  ;@T18
                .endif                                                  ;@D549
            .until  <si a 3>
            mov     ds:bvsi_bvssel,dx   ;Use SG 1 BVSCB selector        ;@T18
            Call    InitConfig          ;Initialize Session 1 ;B703158
            Call    NotifyMouse         ;Notify Mouse about mode data

            sub     ax,ax
            jmp     finis
        .endif

;-----------------------------------------------------------------------
; Check for creation of a new AVIO Presentation Space or Query Console
;-----------------------------------------------------------------------

        mov     gs,es:bvsg_LISsel       ;FS = Local Infoseg        ;@T52,@T80
        mov     si,gs:[bx].LIS_CurScrnGrp  ;Session number (0-255)      ;@T80
        mov     dx,[bp].p_viofunc       ;Requested function
        .if     <gs:[bx].LIS_ProcType eq LIS_PT_PRESMGR> or        ;@T71,@T80
        .if     <si e 1>                ;Shell                          ;@T71
            mov     [bp].vp_ev_selector,es ;Init Global selector for AVIO
            mov     [bp].vp_ev_session,si ;Init Session number for AVIO
;@T71       mov     dx,[bp].p_viofunc
            .if     <dl eq vf_CreatePS> ;CreatePS for AVIO
                mov     ds:bvsi_bvssel,bx ;Clear potential garbage      ;@T52
                mov     ds:bvsi_envsel,bx ; selectors from the per      ;@T52
                mov     ds:bvsi_lvbsel,bx ; process data area           ;@T52
                sub     ax,ax                                           ;@T71
                .if     <si a 1>                                        ;@T71
                    push    es          ;allocate seltable entry        ;@D549
                    shl     si,2        ;shift sg # by 4 to align Dword ;@D549
                    lea     ax,es:bvsg_seltable[si]               ;@D549;@T52
                    push    ax                                          ;@D549
                    push    es:bvsg_seltablesz  ;size of sellist  ;@D549;@T52
                    call    DOSSMGETMEM         ;get memory from SesMgr ;@D549
                .endif                                                  ;@T71
                .if     <nonzero ax>         ;error returned            ;@D549
                    mov     ax,ERROR_VIO_INTERNAL_RESOURCE              ;@D549
                .else                                                   ;@D549
                    mov     di,fn_ExtVIO     ;call wdh for create_ps
                    Call    AllVDH
                .endif                                                  ;@D549
                jmp     finis           ;Exit immediately
            .endif
        .endif

;-----------------------------------------------------------------------
; Validate the VIO handle
;-----------------------------------------------------------------------

        .if     <dl eq vf_QueryConsole> or ;@T33
        .if     <dl ae vf_Save> and     ;@T33
        .if     <dl be vf_ShellInit>    ;CX not really the handle       ;@T33
            sub     ax,ax               ;@T33
        .else  near                     ;@T33                           ;@T71
            mov     ax,ERROR_VIO_INVALID_HANDLE
            mov     cx,[bp].p_viohandle
            .if     <cx be 128>         ;Enforce max number of PSs      ;@@SW
                .if     <ncxz>          ;Handle specified
;@T72               mov     ax,gs:[bx].LIS_CurScrnGrp              ;@T71,@T80
                    .if     <gs:[bx].LIS_ProcType eq LIS_PT_PRESMGR> or ;@T80
                    .if     <si e 1>    ;Shell making AVIO calls?       ;@T71,@T72
                        push    dx      ;Save func. # before AllVDH destroys it
                        .if     <si e 1> and              ;Need to save ;@T71,@T72
                        .if     <ds:bvsi_sg1_bvssel e bx> ; SG1 sellist?;@T71
                            mov     ds:bvsi_sgnum,si                    ;@T71,@T72
                            sub     dx,dx                 ;Save sellist ;@T71
                            call    TrackSellist                        ;@T71
                        .endif                                          ;@T71
                        mov     [bp].p_viofunc,-1 ;Indicate handle validation
                        mov     di,fn_ExtVIO
                        mov     ds:bvsi_bvssel,bx                       ;@T52
                        mov     ds:bvsi_envsel,bx                       ;@T52
                        mov     ds:bvsi_lvbsel,bx ;Force new selectors  ;@T52
                        Call    AllVDH  ;Validate and set selectors
                        pop     [bp].p_viofunc

                        .if     <zero ax> ;@S14
                            push    ds  ;@S14
                            mov     ds,ds:bvsi_bvssel ;@S14             ;@T52
                            .if     <bit [bx].bvs_flags nand bvs_f_avio> ;@S14
                                mov     [bx].bvs_bvssize,bx ;Force re-init ;@S14
                            .endif      ;@S14
                            pop     ds  ;@S14
                        .endif          ;@S14
                    .endif
                .else                   ;Handle not specified
                    .if     <dl b vf_AssociateDC> or
                    .if     <dl a vf_GetPSAddress> ;Not AVIO specific
                        .if     <gs:[bx].LIS_ProcType ne LIS_PT_PRESMGR> ;@T80
                            sub     ax,ax
                        .else           ;AVIO session!
                            push    ds
                            mov     ds, es:bvsg_LISsel                  ;@T52
                            .if     <[bx].LIS_CurScrnGrp e 3> ;Popup screen group?
                                sub     ax,ax
                            .endif
                            pop     ds
                        .endif
                    .endif
                .endif
            .endif
            .if     <nonzero ax> and                                    ;@T84
            test    es:bvsg_flags,bvsg_f_popup + bvsg_f_tpopup          ;@T84
            .if     <nz>                                                ;@T84
                and     es:bvsg_flags,NOT (bvsg_f_popup + bvsg_f_tpopup);@T84
                mov     si,ds:bvsi_pop_sgnum                            ;@T84
                mov     ds:bvsi_sgnum,si        ;Restore Session number ;@T84
                mov     si,ds:bvsi_pop_bvssel   ;Restore orginal        ;@T84
                mov     ds:bvsi_bvssel,si       ;  BVSCB selector       ;@T84
                and     ds:bvsi_flags,NOT bvsi_f_popup                  ;@T84f
            .endif                                                      ;@T84
        .endif

;-----------------------------------------------------------------------
; Validate the LVB and invoke the requested routine
;-----------------------------------------------------------------------

        .if     <zero ax>  NEAR
            Call    BvsInit             ;Perform normal selector update

            mov     ax,BVSISEG
            mov     es,ax               ;ES = Instance data
            mov     ds,es:bvsi_bvssel   ;DS = BVSCB                     ;@T52

;-----------------------------------------------------------------------;77397
; if the BVSCB has been NULLed (by vf_DestroyPS) then just get out      ;77397
;-----------------------------------------------------------------------;77397
            mov     ax,ds                                               ;77397
            .if     <zero ax>                                           ;77397
               mov     ax,ERROR_VIO_INVALID_HANDLE                      ;77397
               jmp     finis                                            ;77397
            .endif                                                      ;77397

            mov     ax,es:bvsi_lvbsel                                   ;@T52
            .if     <nonzero ax>        ;LVB allocated
                verw    ax
                .if     <nz>            ;LVB unavailable
                    push    ax
                    Call    DOSGETSEG   ;Put LVB in LDT
                .endif
            .endif

            mov     ax,[bp].p_viofunc   ;Get function code
            .if     <al ae vf_Save> and
            .if     <al be vf_Free>
                sub     al,vf_Save-vf_Save2 ;Convert 41-43 ==> 19-1B
            .endif

            .if     <al ae vf_NextAvail> ;Extended VIO calls
                mov     ax,es:bvsi_sgnum                                ;@T52
                mov     [bp].vp_ev_session,ax ;Provide session number
                mov     di,fn_ExtVIO
                mov     ds,bx           ;Avoid bad selector after DestroyPS
                Call    AllVDH          ;Call extended VIO
            .else
                mov     si,ax
                add     si,si           ;@P1
                add     si,ax           ;Each entry in the table is 6 bytes;@P1
                add     si,si           ; (si) = ax * 6 = call table offset;@P1
                .if     <[bx].bvs_CA_pid eq bx> ; No CAs are active         ;@P1
                    Call    CS:CallTbl[si].func_sbcs ;Call the BVS routine  ;@P1
                .else                   ;@P1
                    mov     ax,ERROR_VIO_INVALID_PARMS ;D261
                    .if     <BIT es:bvsi_flags nand bvsi_f_caproc> or ;@P1;D261;@T52
                    .if     <BIT CS:CallTbl[si].func_flags and call_f_allowCA> ;@P1;D261;@T31
                        Call    CS:CallTbl[si].func_dbcs ;Call the BVS routine
                    .endif              ;@P1
                .endif                  ;@P1
            .endif

            push    ax                  ;Save the return code
            sub     bx,bx
            Call    NotifyMouse

            mov     ax,BVSISEG
            mov     ds,ax               ;Instance data segment
            dec     ds:bvsi_level       ;Reduce recursion level         ;@T52
            pop     ax                  ;Restore the return code
        .endif

;-----------------------------------------------------------------------
; Common exit point for all BVS routines
;-----------------------------------------------------------------------

finis   LABEL   NEAR
        public  finis                   ;Error exit point for Bvs

        sub     bx,bx                   ;@S37
        mov     [bp].p_regax,ax         ;Save return code for POPA
        push    BVSISEG                 ;@S37                           ;@T71
        pop     ds                      ;@S37                           ;@T71
        .if     <ax eq ERROR_VIO_INTERNAL_RESOURCE> ;@S37
            mov     ds:bvsi_level,bx    ;reset recursion level          ;@C46
            mov     es,ds:bvsi_ldtsel   ;Don't force a Load Module if   ;AR06893
            .if     <es:[LIS_CurScrnGrp] a 3> ; it came from SG 0,1,3   ;AR06893
                mov     ds:bvsi_gdtsel,bx ;Force new DosLoadModule ;@S37,@T52
                mov     ds:bvsi_bvssel,bx ; selectors from the per      ;B721007
                mov     ds:bvsi_envsel,bx ; process data area           ;B721007
                mov     ds:bvsi_lvbsel,bx ;                             ;B721007
            .endif                                                      ;AR06893
        .endif                          ;@S37

        .if     <[bp].p_viofunc eq vf_DestroyPS>                ;B716112
          mov     ds:bvsi_bvssel,bx   ; selectors from the per  ;B716112
          mov     ds:bvsi_envsel,bx   ; process data area       ;B716112
          mov     ds:bvsi_lvbsel,bx   ;                         ;B716112
        .endif                                                  ;B716112

        .if     <ds:bvsi_sgnum e 1> and                                 ;@T71
        .if     <ds:bvsi_sg1_bvssel a bx>                               ;@T71
            mov     dx,1                                                ;@T71
            call    TrackSellist                                        ;@T71
        .endif                                                          ;@T71

        mov     ax,BVSGSEG
        mov     es,ax
        mov     ax,es:bvsg_sg0flag                                      ;@T52
        .if     <nonzero ax> and        ;SG0 Task for Other SG
        .if     <ax eq [bp].p_viofunc>  ;Same as time of save
;@T71       push    BVSISEG
;@T71       pop     ds
            push    ds:bvsi_bvssel                                      ;@T52
            mov     ax,es:bvsg_sg0bvssel                                ;@T52
            mov     ds:bvsi_bvssel,ax   ;Restore SG0 BVSCB              ;@T52
            CALL    DOSFREESEG          ;Release other BVSCB
            push    ds:bvsi_lvbsel                                      ;@T52
            mov     ax,es:bvsg_sg0lvbsel                                ;@T52
            mov     ds:bvsi_lvbsel,ax   ;Restore SG0 LVB                ;@T52
            CALL    DOSFREESEG          ;Release other LVB
            mov     es:bvsg_sg0flag,bx  ;Don't do it again              ;@T52
        .endif

        lea     sp,[bp].p_regfs       ;Deallocate VDH Parameter block ;D198;@D549
        pop     gs                                                      ;@T80
        pop     es
        pop     ds
        popa
        popf                            ;D198
        ret
BVSMAIN ENDP

;-----------------------------------------------------------------------
; AllVDH -- Common VDH calling routine for BVS
;     This routine routs the requests to the VDH selected by bvs_curconfig.
;     If the foreground flag is on when AllVDH is called, it will be
;       forced off
;     This routine will commonly be called via JMP instructions from
;       AllXxx routines so that AllVDH will return to the caller of AllXxx.
;     On entry, DI contains the function number.
;     On exit, only AX is changed.
;-----------------------------------------------------------------------

AllVDH  PROC    NEAR
        public  AllVDH

        push    cx                      ;@B14 save regs
        push    dx                      ;@B14
        push    es                      ;@B14
        push    si                      ;@B14

        push    ds
        push    di

        mov     ax,BVSISEG
        mov     ds,ax

        push    ds:bvsi_envsel                                          ;@T52
        push    bx                      ;Set the Environment Buffer ptr
        push    ss
        push    bp                      ;Set the Parameter Block ptr
        push    bx
        push    di                      ;Set the function number

        .if     <ds:bvsi_bvssel ne bx>  ;BVSCB has been allocated       ;@T52
            test    [bp].vp_flags,vp_f_physical
            .if     <nz>                ;Physical flag already set
                and     [bp].vp_flags,NOT vp_f_physical ;Mark as background
            .else
                push    ds
                mov     ds,ds:bvsi_gdtsel                               ;@T52
                mov     al,[bx].SIS_CurScrnGrp
                cbw                                                     ;@T52
                pop     ds
                .if     <ax eq <ds:bvsi_sgnum>> ;Foreground session     ;@T52
                    .if     <di ne fn_GetConfig>
                        or      [bp].vp_flags,vp_f_physical ;Physical update
                    .endif
                .else
                    .if     <di eq fn_BufferUpdate> ;Background update
                        or      [bp].vp_flags,vp_f_logical ;Use the LVB
                        push 0        ; @151935
                        push 0        ; @151935
                        call DOSSLEEP ; @151935
                    .endif
                .endif
            .endif
            mov     ds,ds:bvsi_bvssel                                   ;@T52
            mov     bx,[bx].bvs_curconfig ;Get current configuration
        .endif

        shl     bx,2                    ;Get offset of table ptr
        mov     ax,BVSGSEG
        mov     ds,ax                   ;Get selector of table ptrs
        lds     bx,ds:[bx].bvsg_calltables                              ;@T52
        sub     di,100h                 ;Adjust for smaller table ;D261
        shl     di,2                    ;Get offset of requested function ptr

        Call    dword ptr [bx+di]       ;Call the VDH routine

        sub     bx,bx
        mov     [bp].vp_parmlength,8    ;reset default Parameter block length
        mov     [bp].vp_flags,bx        ;reset default Parameter block flags

        pop     di
        pop     ds

        pop     si                      ;@B14 restore regs
        pop     es                      ;@B14
        pop     dx                      ;@B14
        pop     cx                      ;@B14

        ret
AllVDH  ENDP

;-----------------------------------------------------------------------
; NotifyMouse -- Notify the mouse about a video mode change
;-----------------------------------------------------------------------

NotifyMouse PROC NEAR
        public  NotifyMouse

        mov     dx,[bp].p_viofunc       ;Get function code

        .if     <dl ne vf_Free> near and
        .if     <dl ne vf_ShieldTerm> near and
        .if     <dl ne vf_DestroyPS>  near and ;Control blocks stil valid
        push    BVSGSEG
        pop     ds                      ;DS = Global data segment
        test    ds:bvsg_flags,bvsg_f_harderror                          ;@T52
        .if     <z> near and            ;Not in a hard error popup
        push    BVSISEG
        pop     ds                      ;DS = Instance data segment
        .if     <ds:bvsi_level be 1> and ;Not a recursive call          ;@T52
        mov     ds,ds:bvsi_bvssel       ;DS = Session data segment      ;@T52
        test    [bx].bvs_flags,bvs_f_mouactive
        .if     <nz>                    ;Mouse is active for this session
            .if     <dl ne vf_Save> and ;B784080
            .if     <dl ne vf_Restore>  ;B784080

                test    [bx].bvs_flags,bvs_f_mounotify
                .if     <nz>            ;if notify needed
                    push    bx
                    push    [bx].bvs_curconfig ;Current configuration
                    push    ds          ; Address for Mouse Handle
                    push    bvs_scratch
                    call    MouOpen     ;@C50
;@C50;;;;;;;;;;;;;; THUNKCALL MOUOPEN   ;@T25

                    .if     <zero ax>   ;Good! Pass the data
                        mov     cx,[bx].bvs_scratch ;Get the handle in CX
;@T71                   push    bx
                        push    dword ptr 0 ;dummy ptr to MOU ptr       ;@T71
                        push    ds      ;get it from the bvscb
                        push    bvs_mdlen ;mode structure
                        push    51h     ;function = mode
                        push    07h     ;category = Mouse
                        push    cx
                        Call    DOSDEVIOCTL

;@T71                   push    bx
                        push    dword ptr 0 ;dummy ptr to MOU ptr       ;@T71
                        push    ds      ;get it from the bvscb
                        push    bvs_mdlen ;mode structure
                        push    5Dh     ;function = Mode Done
                        push    07h     ;category = Mouse
                        push    cx
                        Call    DOSDEVIOCTL

                        push    cx
                        call    MouClose                                ;@C50
;@C50;;;;;;;;;;;;;;;;;; THUNKCALL MOUCLOSE ;@T25
                    .endif

                    and     [bx].bvs_flags,NOT bvs_f_mounotify
                .endif
            .endif                      ;B784080
        .endif

        ret
NotifyMouse ENDP

CTEXT_TUNE5 ENDS                                                        ;MDLL




BVS3SEG SEGMENT BYTE PUBLIC 'CODE' USE16                           ;MDLL;@D549
        ASSUME  CS:CODE02                                               ;MDLL

;-----------------------------------------------------------------------
; GetScrHandle -- Routine to get SCREEN$ handle once for each process
;-----------------------------------------------------------------------

GetScrHandle PROC NEAR
        PUBLIC  GetScrHandle

        mov     dx,es:bvsi_scrhandle                                    ;@T52
        .if     <zero dx>
            sub     eax,eax                                             ;@T71
            push    BVSGSEG
            push    offset bvsg_scrddname ;Device name ASCIIZ string    ;@T52
            push    es
            push    offset bvsi_scrhandle ;Return for device handle     ;@T52
            push    es
            push    offset bvsi_retlen    ;Action Taken                 ;@T52
;@T71       push    bx
            push    eax                 ;File Size                      ;@T71
;@T71       push    bx                  ;File Attribute
            push    dword ptr 00001h    ;Open Flag                      ;@T71
            push    000C2h              ;Open Mode
;@T71       push    bx
            push    eax                 ;Reserved (0:0)                 ;@T71
            Call    DOSOPEN
            .if     <nonzero ax>
                mov     es:bvsi_level,bx                                ;@T52
                mov     ax,error_vio_internal_resource
                jmp     finis
            .endif

            mov     dx,es:bvsi_scrhandle                                ;@T52
        .endif

        ret
GetScrHandle ENDP

;-----------------------------------------------------------------------
; Call table used for internal routing of BVS routines
;-----------------------------------------------------------------------

        public  CallTbl
CallTbl dw      BvsGetPhysBuf           ; 00 normal
        dw      BvsGetPhysBuf           ; 00 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - not allowed for CA proc ;@P1
        dw      BvsGetBuf               ; 01 normal
        dw      BvsGetBuf               ; 01 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsShowBuf              ; 02 normal
        dw      BvsShowBuf              ; 02 CAs present
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsGetCurPos            ; 03 normal
        dw      BvsGetCurPos            ; 03 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsGetCurType           ; 04 normal
        dw      BvsGetCurType           ; 04 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsGetMode              ; 05 normal
        dw      BvsGetMode              ; 05 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsSetCurPos            ; 06 normal
        dw      BvsSetCurPos            ; 06 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsSetCurType           ; 07 normal
        dw      BvsSetCurType           ; 07 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsSetMode              ; 08 normal
        dw      BvsSetMode              ; 08 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsReadCharStr          ; 09 normal
        dw      ManageCAS               ; 09 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsReadCellStr          ; 0A normal
        dw      ManageCAS               ; 0A CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsWrtNChar             ; 0B normal
        dw      ManageCAS               ; 0B CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsWrtNAttr             ; 0C normal
        dw      ManageCAS               ; 0C CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsWrtNCell             ; 0D normal
        dw      ManageCAS               ; 0D CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsWrtCharStr           ; 0E normal
        dw      ManageCAS               ; 0E CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsWrtCharStrAtt        ; 0F normal
        dw      ManageCAS               ; 0F CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsWrtCellStr           ; 10 normal
        dw      ManageCAS               ; 10 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsWrtTTY               ; 11 normal
        dw      ManageCAS               ; 11 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsScrollUp             ; 12 normal
        dw      ManageCAS               ; 12 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsScrollDn             ; 13 normal
        dw      ManageCAS               ; 13 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsScrollLf             ; 14 normal
        dw      ManageCAS               ; 14 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsScrollRt             ; 15 normal
        dw      ManageCAS               ; 15 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsSetAnsi              ; 16 normal
        dw      BvsSetAnsi              ; 16 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsGetAnsi              ; 17 normal
        dw      BvsGetAnsi              ; 17 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsPrtSc                ; 18 normal
        dw      BvsPrtSc                ; 18 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsSave                 ; 19 / 41 normal
        dw      BvsSave                 ; 19 / 41 CAs present             ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsRestore              ; 1A / 42 normal
        dw      BvsRestore              ; 1A / 42 CAs present             ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsFree                 ; 1B / 43 normal
        dw      BvsFree                 ; 1B / 43 CAs present             ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsError                ; 1C normal
        dw      BvsError                ; 1C CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsError                ; 1D normal
        dw      BvsError                ; 1D CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsError                ; 1E normal
        dw      BvsError                ; 1E CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsTogglePrtSc          ; 1F normal
        dw      BvsTogglePrtSc          ; 1F CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsError                ; 20 normal
        dw      BvsError                ; 20 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsError                ; 21 normal
        dw      BvsError                ; 21 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsGetFont              ; 22 normal
        dw      BvsGetFont              ; 22 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsGetConfig            ; 23 normal
        dw      BvsGetConfig            ; 23 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsSetCP                ; 24 normal
        dw      BvsSetCP                ; 24 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsGetCP                ; 25 normal
        dw      BvsGetCP                ; 25 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsSetFont              ; 26 normal
        dw      BvsSetFont              ; 26 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsGetState             ; 27 normal
        dw      BvsGetState             ; 27 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsSetState             ; 28 normal
        dw      BvsSetState             ; 28 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsError                ; 29 normal
        dw      BvsError                ; 29 CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsError                ; 2A normal
        dw      BvsError                ; 2A CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsError                ; 2B normal
        dw      BvsError                ; 2B CAs present                  ;@P1
        dw      0                       ; flags - not allowed for CA proc ;@P1
        dw      BvsCreateCA             ; 2C normal
        dw      BvsCreateCA             ; 2C CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsGetCAState           ; 2D normal
        dw      BvsGetCAState           ; 2D CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsSetCAState           ; 2E normal
        dw      BvsSetCAState           ; 2E CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsDestroyCA            ; 2F normal
        dw      BvsDestroyCA            ; 2F CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1
        dw      BvsCheckCharType        ; 30 normal
        dw      ManageCAS               ; 30 CAs present                  ;@P1
        dw      call_f_allowCA          ; flags - allowed for CA proc     ;@P1

;-----------------------------------------------------------------------
; BvsError -- Dummy routine to replace invalide function numbers
;-----------------------------------------------------------------------

BvsError PROC   NEAR                    ;Routine for unsupported functions
        public  BvsError

        mov     ax,ERROR_VIO_INVALID_PARMS
        ret
BvsError ENDP

;------------------------------------------------------------------------------
; Local variables used by SaveRegs and RestoreRegs
;------------------------------------------------------------------------------

SavedBX equ     WORD PTR [bp+6]         ; Storage for original BX register
SavedCX equ     WORD PTR [bp+8]         ; Storage for original CX register
SavedDX equ     WORD PTR [bp+10]        ; Storage for original DX register
SavedES equ     WORD PTR [bp+12]        ; Storage for original ES register
SavedFLAGS equ  WORD PTR [bp+14]        ; Storage for original flag register

;/**********************  START OF SPECIFICATIONS  **********************/
;/*                                                                     */
;/*  SUBROUTINE NAME: SaveRegs                                          */
;/*                                                                     */
;/*  DESCRIPTIVE NAME: Save registers and flags                         */
;/*                                                                     */
;/*  FUNCTION: SaveRegs is the first call made by each of the toplevel  */
;/*            VDH entry points.                                        */
;/*                                                                     */
;/*  ENTRY POINT: SaveRegs                                              */
;/*    LINKAGE:   CALL FAR                                              */
;/*                                                                     */
;/*  INPUT: NONE                                                        */
;/*                                                                     */
;/*  EXIT-NORMAL: Registers and flags are saved on the stack            */
;/*                                                                     */
;/*  INTERNAL REFERENCES:                                               */
;/*    ROUTINES: NONE                                                   */
;/*                                                                     */
;/*  EXTERNAL REFERENCES:                                               */
;/*    ROUTINES: NONE                                                   */
;/*                                                                     */
;/***********************  END OF SPECIFICATIONS  ***********************/

        PUBLIC  SAVEREGS
SAVEREGS PROC   FAR

;------------------------------------------------------------------------------
; ENTRY: SS:SP =                                       NextIP  NextCS
; EXIT:  SS:SP = NextIP NextCS SavedBX SavedCX SavedDX SavedES SavedFLAGS
;------------------------------------------------------------------------------

        sub     sp, 10                  ; Allocate temporary storage on stack
        push    bp
        mov     bp, sp

        mov     ax, [bp+12]             ; Move far return address to    ;@T71f
        mov     [bp+2], ax              ;   top of the stack            ;@T71f
        mov     ax, [bp+14]
        mov     [bp+4], ax

        mov     SavedBX, bx             ; Now save the registers
        mov     SavedCX, cx
        mov     SavedDX, dx
        mov     SavedES, es
        pushf
        pop     SavedFLAGS

        pop     bp
        ret
SAVEREGS ENDP

;/**********************  START OF SPECIFICATIONS  **********************/
;/*                                                                     */
;/*  SUBROUTINE NAME: RestoreRegs                                       */
;/*                                                                     */
;/*  DESCRIPTIVE NAME: Restore registers and flags                      */
;/*                                                                     */
;/*  FUNCTION: RestoreRegs is the last call made by each of the toplevel*/
;/*            VDH entry points, prior to returning.                    */
;/*                                                                     */
;/*  ENTRY POINT: RestoreRegs                                           */
;/*    LINKAGE:   CALL FAR                                              */
;/*                                                                     */
;/*  INPUT: NONE                                                        */
;/*                                                                     */
;/*  EXIT-NORMAL: Registers and flags are restored.                     */
;/*                                                                     */
;/*  INTERNAL REFERENCES:                                               */
;/*    ROUTINES: NONE                                                   */
;/*                                                                     */
;/*  EXTERNAL REFERENCES:                                               */
;/*    ROUTINES: NONE                                                   */
;/*                                                                     */
;/***********************  END OF SPECIFICATIONS  ***********************/

        PUBLIC  RESTOREREGS
RESTOREREGS PROC FAR

;------------------------------------------------------------------------------
; ENTRY: SS:SP = NextIP NextCS SavedBX SavedCX SavedDX SavedES SavedFLAGS
; EXIT:  SS:SP = NextIP NextCS
;------------------------------------------------------------------------------

        push    bp
        mov     bp, sp

        push    SavedFLAGS
        popf

        mov     bx, SavedBX             ; Restore registers
        mov     cx, SavedCX
        mov     dx, SavedDX
        verr    SavedES
        .if     < nz >
            push    0
            pop     es                  ; If bad selector, replace with zero
        .else
            mov     es, SavedES
        .endif

        pop     bp
        ret     10
RESTOREREGS ENDP

TrackSellist  PROC NEAR
        PUBLIC  TrackSellist

;------------------------------------------------------------------------------
; ENTRY: DX = 0 (Save Shell's sellist)
;             1 (Restore Shell's sellist)
;        BX = 0
;
; EXIT:  destroys AX, CX, DI, SI, ES
;------------------------------------------------------------------------------

        mov     ax,ds                           ;AX = BVSISEG
        mov     di,BVSGSEG                      ;
        mov     ds,di                           ;
        mov     di,offset bvsi_sg1_bvssel       ;
        lds     si,dword ptr ds:[bvsg_seltable+4]
        add     si,2                            ;Skip pass length field
        .if     <zero dx>                       ;Save sellist?
            mov     es,ax                       ;
        .else                                   ;Restore sellist
            push    ds                          ;
            pop     es                          ;
            mov     ds,ax                       ;
            xchg    si,di                       ;
        .endif                                  ;
        mov     cx,2                            ;Copy sellist data
        rep     movsd                           ;
        mov     ds,ax                           ;DS = BVSISEG
        .if     <nonzero dx>                    ;
            mov     ds:bvsi_sg1_bvssel,bx       ;Indicate restore is done
        .endif                                  ;
        ret                                     ;
TrackSellist  ENDP

DEBUGSTOP PROC FAR                      ;@S36
        PUBLIC  DEBUGSTOP               ;@S36
        int     3                       ;@S36
        ret                             ;@S36
DEBUGSTOP ENDP                          ;@S36

BVS3SEG ENDS
        END

;@@TB3 = 03/08/89 TLB,  Check for error from call to BVSDINIT and from call
;       to BVSGETVIDEOCALLTABLES.  If error, prepare to give a hard error
;       popup.  PTR B700462
;@T19 = 03/16/89 TPL, Avoid loading AVIO at boot time, P6741
;@T21 = 03/22/89 TPL, DCR 203 fix, SM83191
;@P1  = 03/25/89  PL, DCR 132 changes
;@B1  = 03/26/89  BB, Reduce LVB hugh selector allocation, B700554
;@T25 = 04/04/89 TPL, Sizzle work, B701685
;D198 = 04/07/89 STJ, Install BvsPrtSc and BvsWrtTTY as default VDH routines
;D261 = 04/21/89 STJ, Use CONFIG.SYS instead of OS2.INI and optimize
;@S10 = 05/11/89 STJ, Retain separate ENVB/LVB for each config, B701174
;@T31 = 05/18/89 TPL, PhilipL suggested changes for DCR 132, B783850
;@S14 = 06/08/89 STJ, Fix initialization of AVIO session, B703158
;@T33 = 06/14/89 TPL, Functions with no handle should be checked 1st, HM00637
;@T38 = 06/26/89 TPL, Change all VDH prefixes to BVH, B703924
;@B14 = 07/05/89 WKB, Performance enhancements, B702527
;B784080 = 07/20/89 STJ, Reduce calls to NotifyMouse and eliminate PVBSelector
;@S22 = 08/15/89 STJ, Pass registration information to WrtTTY, B785244
;@T45 = 08/31/89 TPL, Don't load other BVHs when starting VIO Wndw, B707495
;@S36 = 10/10/89 STJ, Add DebugStop for C routines, AR05111
;@D549 = 11/02/89 CJJ, Seltable changes due to rangeless sessions, DCR 549
;@S37 = 10/18/89 STJ, Force new DosLoadModule after resource error, AR05111
;@S42 = 11/08/89 STJ, Add VIDEO_WINDOW support
;@T52 = 01/16/90 TPL, DCR 704 work
;@T18 = 04/30/90 TPL, Avoid doing DosGetSegs to SGCB if not necessary, B712103
;@T71 = 07/06/90 TPL, Allow Shell to make AVIO calls, Hursley DCR 25177, B714347
;@T72 = 07/30/90 TPL, @T71 fix - Invalid handle check regression, B714820
;@T73 = 07/27/90 TPL, Code/data reduction/optimization and swap tune
;B716112 = 09/14/90 WKB, Clear per process selectors, B716112
;AR06893 = 09/25/90 TPL, Don't clear bvsi_gdtsel for SG 0, 1 & 3
;@C43 = 10/09/90  CJJ, Reload call tables on error, PTR AR07246
;@C46 = 11/30/90  CJJ, Reset bvsi_level at finis if 'out of resource', AR07702
;@C39 = 01/15/91 CJJ, MVDM transparent popup changes, PTR SM84577
;@T14f= 05/08/91 TPL, Allow Detached to issue multiple popups, B790735
;@C50 = 08/01/91 CJJ, B724984 Don't use thunk calls anymore.
;B721007 08/03/91 WKB, Reset bvsi BVSCB/ENV/LVB at finis if 'out of resource' (rollover B725473)
;@861 = 11/11/91 WCH,  Reload the VDH config right after shellinit, B729776 (rollover SM00861)
;@T80 = 02/06/92 TPL, Don't use the reserved register FS, B732479
;@T84 = 02/21/92 TPL, Reset bvsi_sgnum for PM Popup guys, B733254
;@T84f= 03/26/92 TPL, More Popup problems, B735167
;@@SW  = 03/03/92 TPL  Increase # of PS from 32 to 128 (62978)
;@T71f = 04/19/93 TPL, Don't mess with high-part of EAX (67657)
;MDLL  = 01/10/94 BJN, Page Tune segments for Merged DLL.
;77397   07/27/94 Changeteam  Get out of BVSMAIN if we've already called vf_DestroyPS
;151935  04/15/96 WKB, Yield the timeslice for background sessions after vdh call
