;       SCCSID = @(#)smgiallc.asm       6.2 92/02/29
;       SCCSID @(#)smgiallc.asm 6.2 92/02/29
PAGE 60,132                             ;
; ****************************************************************************
; *                                                                          *
; *                       IBM/Microsoft Confidential                         *
; *                                                                          *
; *                 Copyright (c) IBM Corporation  1987, 1990                *
; *                 Copyright (c) Microsoft Corp.  1987, 1990                *
; *                           All Rights Reserved                            *
; *                                                                          *
; ****************************************************************************

;***************************************************************************
;
; Source File Name:     SMGIALLC.ASM
;
; Subroutine Name:      DOSSMINITALLOC
;
; Descriptive Name:     Session Manager Initialization Allocation
;
; Function:     Allocate and initialize screen group queue and
;               semaphores.
;
; Notes:
;       Dependencies:   None
;
;       Restrictions:   Maximum number of total screen groups that can
;                       be supported without code modifications is 254.
;                       Maximum number of screen groups per class (ie.
;                       full screen, windowed, PM, or VDM) is 16.
;
; Change History:
;   @@AE - 03/09/90  PTR B788388 - Put DosSMGetStatus back in
;   @TL7 - 04/05/90  PTR B712376 - Enable for MMIO
;   @D1088 07/07/90  DCR 1088 Add support for special needs.
;   @MHW - 09/13/90  PTR B715818 - Removed MMIO support
;   CJ56   02/29/92  PTR B733713 Don't import DLLs - dynmaically link.
;
; Entry Point:
;       Linkage:  EXTRN DOSSMINITALLOC:NEAR
;                       CALL    DOSSMINITALLOC
;
; Inputs:
;       BX      Totalscrngrps  Maximum number of screen groups
;       DS      RDATA          Session Manager global data segment
;
; Exit Normal:
;       AX      ReturnCode Value = 0, Initialization successful
;
; Exit Errors:
;       AX      ReturnCode DOSALLOCSEG    errors
;                          DOSALLOCSHRSEG errors
;                          DOSOPEN        errors
;                          DOSDEVIOCTL    errors
;
; Effects:      All other registers preserved.
;
; Internal References:
;          Routines:
;
; External References:
;          Routines:    DOSALLOCSEG
;                       DOSALLOCSHRSEG
;                       DOSSEMSET
;                       DOSSEMCLEAR
;                       DOSOPEN
;                       DOSDEVIOCTL
;                       MOUSHELLINIT
;
;***************************************************************************
ifdef I1
;
;   DOSALLOCSHRSEG (Allocate shared session title segment)
;   IF (No_Error)
;      Initialize active queue header
;      DOSSEMSET (Start RAM semaphore to block DOSCWAIT thread)
;      DOSSEMSET (Title change RAM semaphore for VIO)
;      DOSSEMSET (Popup RAM semaphore for VIO)
;      DOSSMADDSGCB (Add sgcb for sg #1 [base shell])
;      IF (No error)
;         MOUSHELLINIT (Initialize Mouse Router)
;      ENDIF
;      DOSOPEN (Open Keyboard)
;      DOSDEVIOCTL (Tell keyboard sg #1 is in foreground)
;   ENDIF
;
;***************************************************************************
else

.386C
    EXTRN BaseShell        : DWORD
    EXTRN BottomMost       : DWORD
    EXTRN CurrentDebugger  : WORD
    EXTRN FreeGroups       : DWORD
    EXTRN HdeVioRamSem     : DWORD      ; Popup ram semaphore for VIO
    EXTRN IncomingDebugger : WORD
    EXTRN KeyFileName      : BYTE       ; Keyboard device name
    EXTRN LastElement      : DWORD
    EXTRN LocalSelector    : WORD
    EXTRN MaxFullScrnGrps  : WORD
    EXTRN MseFileName      : BYTE       ; Mouse device name
    EXTRN PM_Present       : BYTE       ; PM Present indicator
    EXTRN PopUpScrnGrp2    : DWORD
    EXTRN PrsOpenAction    : WORD       ; Action if device doesn't exist
    EXTRN QHKEYBDHANDLE    : WORD       ; Hanlde to the keyboard
    EXTRN QHMOUSEHANDLE    : WORD       ; Handle to the mouse
    EXTRN QueueFlags       : WORD
    EXTRN SGTitleName      : BYTE       ; Name of SG titles shared seg.
    EXTRN STitleRamSem     : DWORD      ; Title change semaphore
    EXTRN ShellPID         : WORD
    EXTRN StartRamSem      : DWORD                                      ;CJ10
    EXTRN SwitchRamSem     : DWORD
    EXTRN SwtMseParm1      : WORD       ; Notify KBD of foregrnd SG
    EXTRN TitleSelector    : WORD
    EXTRN TopMost          : DWORD
    EXTRN UsedScrnGrps     : WORD
;   EXTRN MouShellInit_Ord : WORD                                        ;MDLL
    EXTRN MOUSHELLINIT     : FAR                                         ;MDLL
;   EXTRN Moucalls_Handle  : WORD                                        ;MDLL
;@D1088 EXTRN NotDD_DosDev_Data_Len   : WORD ; Data area for DosDevIOCTL ParmList
;@D1088 EXTRN NotDD_Notification_Type : WORD
;@D1088 EXTRN NotDD_Incoming_SG       : WORD
;@D1088 EXTRN NotDD_Current_SG        : WORD

;   EXTRN DOSWINPROC       : NEAR ; LoadModule and get proc addrs       ;MDLL
    EXTRN DOSSMADDSGCB     : NEAR       ; Add a new SGCB
    EXTRN DOSSMGETMEM      : FAR
    EXTRN DOSSMNOTIFYDD    : FAR
    EXTRN DOSALLOCSHRSEG   : FAR        ; Allocate shared segment
    EXTRN DOSDEVIOCTL2     : FAR        ; Notify Device Drivers via IOCTL
    EXTRN DOSOPEN          : FAR        ; Open mouse device
    EXTRN DOSSEMCLEAR      : FAR        ; Release semaphore ownership
    EXTRN DOSSEMSET        : FAR        ; Take ownership of semaphore
;CJ56    EXTRN MOUSHELLINIT     : FAR        ; Initialize mouse router

.xlist
    INCLUDE SMRSGCB.INC
    INCLUDE SMRRDATA.INC
;@D1088    INCLUDE SMRNOTDD.INC
.xcref
    INCLUDE INFOSEG.INC
    INCLUDE ERROR2.INC
    INCLUDE STRUC.INC
.cref
.list

CODE SEGMENT WORD PUBLIC 'CODE' USE16
    ASSUME CS:CODE,DS:RDATA,SS:NOTHING,ES:NOTHING

    PUBLIC DOSSMINITALLOC
DOSSMINITALLOC PROC NEAR

    Local_Bytes  EQU 4
    Local_Var1   EQU SS:[BP][-2]        ; Local variable on stack
    Local_Var2   EQU SS:[BP][-4]        ; Local variable on stack

    ENTER Local_Bytes,0
    PUSH DS                             ; Save global data segment selector
    PUSH DI
    PUSH ES
    PUSH FS                                                             ;CJ56
    PUSH SI                                                             ;CJ56
    PUSH CX

;   *********************************************************
;   *  Allocate storage for the active screen group titles  *
;   *********************************************************
;   AX = TotalScrnGrps * Length of Title

    .IF  < PM_Present e Yes >           ; If PM present, then
      MOV  AX, 10                       ; Segment size = WORDSIZE. For Hard Err
    .ELSE                               ; PM isn't here (oh noooo!)        @@AE
      MOV  AX, SGCB_TitleLen
      SHL  BL, 1                        ; Double for VDMs                  @@AE
      MUL  BL                           ; BX = maximum number of screen grps
      SHR  BL, 1                        ; Restore to # of FS sessions      @@AE
    .ENDIF                              ; To detect Mouse Present Flag

;   IF PM IS PRESENT THIS EATS UP 4K FOR A 10 BYTE SEGMENT
    PUSH AX                             ; Segment size
    PUSH DS                             ; Seg. name(\SHAREMEM\SMG\SMGTITLE)
    PUSH OFFSET SGTitleName
    PUSH DS                             ; Returned screen grp title selector
    PUSH OFFSET TitleSelector
    CALL DOSALLOCSHRSEG                 ; Alloc. shared title segment

;   *************************************************
;   *  Initialize active screen group queue header  *
;   *************************************************
    .IF  < zero AX > near                                               ;CJ10
      MOV  MaxFullScrnGrps, BX          ; Maximum # of Full Screen Grps
      MOV  PopUpScrnGrp2, 0FFFFH        ; Popup sgid

;     ***********************************************************
;     *  Initialize FreeGroups, which is an array of 8 DWORDS.  *
;     ***********************************************************
      MOV  FreeGroups, 0000000Dh        ; Reserve SG 0, 2, and 3

;     *******************************************************
;     *  Initialize all system and RAM semaphores required  *
;     *******************************************************
      PUSH DS
      PUSH OFFSET StartRamSem
      CALL DOSSEMSET                    ; Set start ram semaphore

      PUSH DS
      PUSH OFFSET STitleRamSem
      CALL DOSSEMSET                    ; Set popup sem. for VIO

      PUSH DS
      PUSH OFFSET HdeVioRamSem
      CALL DOSSEMSET                    ; Set popup sem.for VIO

;     *********************************
;     *  Add SGCB for the Base Shell  *
;     *********************************
      MOV  ES, LocalSelector            ; Local information seg
      MOV  CX, ES:LIS_CurProcID         ; Base Shell process ID
      MOV  ShellPID, CX                 ; This better be the shell running now
      XOR  BX, BX                       ; No parent screen group
      MOV  DX, FS_Type                  ; Full Screen Session Type
      MOV  DI, BX                       ; No Base Shell Title
      MOV  ES, BX
      CALL DOSSMADDSGCB                 ; ES:[DI] --> SGCB #1

;     *****************************************
;     *  Initialize SGCB header information.  *
;     *****************************************
      PUSH EBX                          ; Save EBX
      MOV  BX, ES                       ; EBX = ES:DI --> Shell's SGCB
      SHL  EBX, 16                      ; Put DI in upper word of EBX
      MOV  BX, DI                       ; ES in lower word of EBX

      MOV  BaseShell, EBX               ; @ of Shell's SGCB
      MOV  TopMost, EBX
      MOV  BottomMost, EBX
      MOV  LastElement, EBX

      MOV  IncomingDebugger, AX         ; Put 0 in for no debugger
      MOV  CurrentDebugger, AX
      POP  EBX                          ; Restore EBX (mostly for upper word)


;     *********************
;     *  Open the mouse.  *
;     *********************
      PUSH DS                           ; 'MOUSE'
      PUSH OFFSET MseFileName
      PUSH DS                           ; Mouse device driver handle
      PUSH OFFSET QHMOUSEHANDLE
      PUSH DS
      PUSH OFFSET PrsOpenAction
      PUSH DWORD PTR 0                  ; Size                          ;CJ10
;CJ10 PUSH 0
;CJ10 PUSH 0                            ; Attribute
      PUSH DWORD PTR 1                  ; Fail if doesn't exist         ;CJ10
      PUSH 20C1H                        ; Shared access, no hard error errors
      PUSH DWORD PTR 0                  ; Reserved                      ;CJ10
;CJ10 PUSH 0                            ; Reserved
      CALL DOSOPEN                      ; Open the mouse

      MOV  ES, TitleSelector            ; Get title segment
      XOR  DI, DI                       ; SG 0 title offset

;     ********************************************************
;     * If the mouse is there, initialize the Mouse Router.  *
;     ********************************************************
      .IF  < zero AX >                                                  ;CJ10
;MDLL   MOV  AX, RDATA                  ; Setup for DosWinProc          ;CJ56
;MDLL   MOV  FS, AX                                                     ;CJ56
;MDLL   MOV  CX, 1                      ; Set up loop - load only 1 proc;CJ56
;MDLL   LEA  BX, MouShellInit_Ord                                       ;CJ56
;MDLL   LEA  DX, MouShellInit                                           ;CJ56
;MDLL   LEA  SI, Moucalls_Handle        ; Pass module name/hdle loc     ;CJ56
;MDLL   CALL DOSWINPROC                                                 ;CJ56

;MDLL   .IF  < zero AX >                ; If no error loading           ;CJ56
          CALL MOUSHELLINIT             ; Initialize Mouse router       ;MDLL
;MDLL   .ENDIF                                                          ;CJ56
        XOR  AX, AX                     ; Hard error MOUOPEN flag
      .ENDIF
      MOV  ES:[DI], AX                  ; Hard error MOUOPEN flag

;     ***********************
;     *  Open the Keyboard  *
;     ***********************
      PUSH DS                           ; 'KBD'
      PUSH OFFSET KeyFileName
      PUSH DS                           ; Keyboard device handle
      PUSH OFFSET QHKEYBDHANDLE
      PUSH DS
      PUSH OFFSET PrsOpenAction
      PUSH DWORD PTR 0                  ; Size                          ;CJ10
;CJ10 PUSH 0
;CJ10 PUSH 4                            ; Attribute
      PUSH DWORD PTR 40001H             ; Fail if doesn't exist         ;CJ10
      PUSH 042H                         ; access mode - not inherited = C2H
      PUSH DWORD PTR 0                  ; Reserved                      ;CJ10
;CJ10 PUSH 0                            ; Reserved
      CALL DOSOPEN                      ; Open the Keyboard
    .ENDIF


    POP  CX                             ; Restore global data segment
    POP  SI                                                             ;CJ56
    POP  FS                                                             ;CJ56
    POP  ES
    POP  DI
    POP  DS

    LEAVE
    RET                                 ; Return to caller

DOSSMINITALLOC ENDP

CODE ENDS
endif
    END
