home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / telecomm / terms / term-4.1-source.lha / Extras / Source / term-Source.lha / termInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-27  |  98.0 KB  |  4,699 lines

  1. /*
  2. **    termInit.c
  3. **
  4. **    Program initialization and shutdown routines
  5. **
  6. **    Copyright © 1990-1994 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Spread a byte across a long word. */
  13.  
  14. #define SPREAD(i)    ((ULONG)(i) << 24 | (ULONG)(i) << 16 | (ULONG)(i) << 8 | (i))
  15.  
  16.     /* This variable helps us to remember whether the fast!
  17.      * macro panel was open or not.
  18.      */
  19.  
  20. STATIC BYTE HadFastMacros = FALSE;
  21.  
  22.     /* Remember whether we did pen allocation or not. */
  23.  
  24. STATIC BYTE AllocatedPens = FALSE;
  25.  
  26.     /* DeleteInterleavedBitMap():
  27.      *
  28.      *    Delete an interleaved bitmap as allocated by
  29.      *    CreateInterleavedBitMap().
  30.      */
  31.  
  32. STATIC VOID __regargs
  33. DeleteInterleavedBitMap(struct BitMap *SomeBitMap)
  34. {
  35.     WaitBlit();
  36.  
  37.     FreeVec(SomeBitMap -> Planes[0]);
  38.  
  39.     FreeVecPooled(SomeBitMap);
  40. }
  41.  
  42.     /* CreateInterleavedBitMap():
  43.      *
  44.      *    With special thanks to Leo Schwab, this routine will create an
  45.      *    interleaved BitMap structure suitable for optimized blitter
  46.      *    access.
  47.      */
  48.  
  49. STATIC struct BitMap * __regargs
  50. CreateInterleavedBitMap(LONG Width,LONG Height,WORD Depth)
  51. {
  52.         /* A single plane BitMap cannot be interleaved. */
  53.  
  54.     if(Depth > 1)
  55.     {
  56.         struct BitMap *SomeBitMap;
  57.  
  58.             /* Allocate space for the bitmap structure. */
  59.  
  60.         if(SomeBitMap = (struct BitMap *)AllocVecPooled(sizeof(struct BitMap),MEMF_ANY))
  61.         {
  62.                 /* Initialize with standard values, so we can check
  63.                  * whether the current system will be able to handle
  64.                  * an interleaved bitmap of the expected size.
  65.                  */
  66.  
  67.             InitBitMap(SomeBitMap,Depth,Width,Height);
  68.  
  69.                 /* Check for old standard blitter limits. */
  70.  
  71.             if(Height * Depth > 1024 || SomeBitMap -> BytesPerRow * Depth > 126)
  72.             {
  73.                     /* The current space requirements will operate
  74.                      * correctly only on a system equipped with a
  75.                      * Fat Agnus (or successor) chip, let's see
  76.                      * if we can find one.
  77.                      */
  78.  
  79.                 if(GfxBase -> ChipRevBits0 & GFXF_BIG_BLITS)
  80.                 {
  81.                         /* Unlikely, put still not impossible: check for
  82.                          * Fat Agnus size limits...
  83.                          */
  84.  
  85.                     if(Height * Depth > 32768 || SomeBitMap -> BytesPerRow * Depth > 4096)
  86.                     {
  87.                         FreeVecPooled(SomeBitMap);
  88.  
  89.                         return(NULL);
  90.                     }
  91.                 }
  92.                 else
  93.                 {
  94.                         /* Looks like a Big or old (A1000)
  95.                          * Agnus chip.
  96.                          */
  97.  
  98.                     FreeVecPooled(SomeBitMap);
  99.  
  100.                     return(NULL);
  101.                 }
  102.             }
  103.  
  104.                 /* Initialize to interleaved BitMap values. */
  105.  
  106.             InitBitMap(SomeBitMap,1,Width,Height * Depth);
  107.  
  108.                 /* Allocate plane data. */
  109.  
  110.             if(SomeBitMap -> Planes[0] = (PLANEPTR)AllocVec(SomeBitMap -> BytesPerRow * SomeBitMap -> Rows,MEMF_CHIP))
  111.             {
  112.                 PLANEPTR    Base;
  113.                 WORD        i,
  114.                         Size;
  115.  
  116.                     /* Remember previous data. */
  117.  
  118.                 Base = SomeBitMap -> Planes[0];
  119.                 Size = SomeBitMap -> BytesPerRow;
  120.  
  121.                     /* Clear the bitmap. */
  122.  
  123.                 BltBitMap(SomeBitMap,0,0,SomeBitMap,0,0,Width,Height,MINTERM_ZERO,~0,NULL);
  124.  
  125.                     /* Reinitialize. */
  126.  
  127.                 InitBitMap(SomeBitMap,Depth,Width,Height);
  128.  
  129.                     /* Modify for interleaved look. */
  130.  
  131.                 SomeBitMap -> BytesPerRow *= Depth;
  132.  
  133.                     /* Initialize the single planes. */
  134.  
  135.                 for(i = 0 ; i < Depth ; i++)
  136.                 {
  137.                     SomeBitMap -> Planes[i] = Base;
  138.  
  139.                     Base += Size;
  140.                 }
  141.  
  142.                     /* Return the ready bitmap. */
  143.  
  144.                 return(SomeBitMap);
  145.             }
  146.  
  147.                 /* Deallocate memory. */
  148.  
  149.             FreeVecPooled(SomeBitMap);
  150.         }
  151.     }
  152.  
  153.         /* Return failure. */
  154.  
  155.     return(NULL);
  156. }
  157.  
  158.     /* LoadKeyMap(STRPTR Name):
  159.      *
  160.      *    Load a keymap file from disk.
  161.      */
  162.  
  163. STATIC struct KeyMap * __regargs
  164. LoadKeyMap(STRPTR Name)
  165. {
  166.     struct KeyMapResource    *KeyMapResource;
  167.     struct KeyMap        *Map = NULL;
  168.  
  169.         /* Try to get access to the list of currently loaded
  170.          * keymap files.
  171.          */
  172.  
  173.     if(KeyMapResource = (struct KeyMapResource *)OpenResource("keymap.resource"))
  174.     {
  175.         struct KeyMapNode *Node;
  176.  
  177.             /* Try to find the keymap in the list. */
  178.  
  179.         Forbid();
  180.  
  181.         if(Node = (struct KeyMapNode *)FindName(&KeyMapResource -> kr_List,FilePart(Config -> TerminalConfig -> KeyMapFileName)))
  182.             Map = &Node -> kn_KeyMap;
  183.  
  184.         Permit();
  185.     }
  186.  
  187.         /* Still no keymap available? */
  188.  
  189.     if(!Map)
  190.     {
  191.         APTR OldPtr = ThisProcess -> pr_WindowPtr;
  192.  
  193.             /* Disable DOS requesters. */
  194.  
  195.         ThisProcess -> pr_WindowPtr = (APTR)-1;
  196.  
  197.             /* Unload the old keymap code. */
  198.  
  199.         if(KeySegment)
  200.             UnLoadSeg(KeySegment);
  201.  
  202.             /* Try to load the keymap from the
  203.              * name the user entered.
  204.              */
  205.  
  206.         if(!(KeySegment = LoadSeg(Config -> TerminalConfig -> KeyMapFileName)))
  207.         {
  208.                 /* Second try: load it from
  209.                   * the standard keymaps drawer.
  210.                   */
  211.  
  212.             strcpy(SharedBuffer,"KEYMAPS:");
  213.  
  214.             if(AddPart(SharedBuffer,FilePart(Config -> TerminalConfig -> KeyMapFileName),MAX_FILENAME_LENGTH))
  215.             {
  216.                 if(!(KeySegment = LoadSeg(SharedBuffer)))
  217.                 {
  218.                     strcpy(SharedBuffer,"Devs:Keymaps");
  219.  
  220.                     if(AddPart(SharedBuffer,FilePart(Config -> TerminalConfig -> KeyMapFileName),MAX_FILENAME_LENGTH))
  221.                         KeySegment = LoadSeg(SharedBuffer);
  222.                 }
  223.             }
  224.         }
  225.  
  226.             /* Did we get the keymap file? */
  227.  
  228.         if(KeySegment)
  229.         {
  230.             struct KeyMapNode *Node = (struct KeyMapNode *)&((ULONG *)BADDR(KeySegment))[1];
  231.  
  232.             Map = &Node -> kn_KeyMap;
  233.         }
  234.  
  235.             /* Enable DOS requesters again. */
  236.  
  237.         ThisProcess -> pr_WindowPtr = OldPtr;
  238.     }
  239.     else
  240.     {
  241.         if(KeySegment)
  242.         {
  243.             UnLoadSeg(KeySegment);
  244.  
  245.             KeySegment = NULL;
  246.         }
  247.     }
  248.  
  249.     return(Map);
  250. }
  251.  
  252.     /* DeleteOffsetTables(VOID):
  253.      *
  254.      *    Delete the line multiplication tables.
  255.      */
  256.  
  257. STATIC VOID
  258. DeleteOffsetTables(VOID)
  259. {
  260.     if(OffsetXTable)
  261.     {
  262.         FreeVecPooled(OffsetXTable);
  263.  
  264.         OffsetXTable = NULL;
  265.     }
  266.  
  267.     if(OffsetYTable)
  268.     {
  269.         FreeVecPooled(OffsetYTable);
  270.  
  271.         OffsetYTable = NULL;
  272.     }
  273. }
  274.  
  275.     /* CreateOffsetTables(VOID):
  276.      *
  277.      *    Allocate the line multiplication tables.
  278.      */
  279.  
  280. STATIC BYTE
  281. CreateOffsetTables(VOID)
  282. {
  283.     LONG    Width    = (Window -> WScreen -> Width  + TextFontWidth)  * 2 / TextFontWidth,
  284.         Height    = (Window -> WScreen -> Height + TextFontHeight) * 2 / TextFontHeight;
  285.  
  286.     DeleteOffsetTables();
  287.  
  288.     if(OffsetXTable = (LONG *)AllocVecPooled(Width * sizeof(LONG),MEMF_ANY))
  289.     {
  290.         if(OffsetYTable = (LONG *)AllocVecPooled(Height * sizeof(LONG),MEMF_ANY))
  291.         {
  292.             LONG i,j;
  293.  
  294.             for(i = j = 0 ; i < Width ; i++, j += TextFontWidth)
  295.                 OffsetXTable[i] = j;
  296.  
  297.             for(i = j = 0 ; i < Height ; i++, j += TextFontHeight)
  298.                 OffsetYTable[i] = j;
  299.  
  300.             return(TRUE);
  301.         }
  302.     }
  303.  
  304.     DeleteOffsetTables();
  305.  
  306.     return(FALSE);
  307. }
  308.  
  309.     /* CopyItemFlags(struct MenuItem *Src,struct MenuItem *Dst):
  310.      *
  311.      *    Copy single menu item flags from one menu strip
  312.      *    to another.
  313.      */
  314.  
  315. STATIC VOID __regargs
  316. CopyItemFlags(struct MenuItem *Src,struct MenuItem *Dst)
  317. {
  318.     while(Src && Dst)
  319.     {
  320.         if(Src -> SubItem)
  321.             CopyItemFlags(Src -> SubItem,Dst -> SubItem);
  322.  
  323.         Dst -> Flags = Src -> Flags;
  324.  
  325.         Src = Src -> NextItem;
  326.         Dst = Dst -> NextItem;
  327.     }
  328. }
  329.  
  330.     /* CopyMenuFlags(struct Menu *Src,struct Menu *Dst):
  331.      *
  332.      *    Copy menu flags from one menu strip to
  333.      *    another.
  334.      */
  335.  
  336. STATIC VOID __regargs
  337. CopyMenuFlags(struct Menu *Src,struct Menu *Dst)
  338. {
  339.     struct MenuItem *SrcItem,*DstItem;
  340.  
  341.     while(Src && Dst)
  342.     {
  343.         SrcItem = Src -> FirstItem;
  344.         DstItem = Dst -> FirstItem;
  345.  
  346.         while(SrcItem && DstItem)
  347.         {
  348.             CopyItemFlags(SrcItem,DstItem);
  349.  
  350.             SrcItem = SrcItem -> NextItem;
  351.             DstItem = DstItem -> NextItem;
  352.         }
  353.  
  354.         Src = Src -> NextMenu;
  355.         Dst = Dst -> NextMenu;
  356.     }
  357. }
  358.  
  359.     /* BuildMenu():
  360.      *
  361.      *    Create the menu strip, including quick dial menu.
  362.      */
  363.  
  364. STRPTR
  365. BuildMenu()
  366. {
  367.     struct NewMenu    *NewMenu;
  368.     struct Menu    *MenuNew;
  369.     LONG         PhoneCount = 0,
  370.              Count = 0,
  371.              i;
  372.  
  373.     BlockWindows();
  374.  
  375.         /* Clear the window menu strips. */
  376.  
  377.     if(Window)
  378.         ClearMenuStrip(Window);
  379.  
  380.     if(StatusWindow)
  381.         ClearMenuStrip(StatusWindow);
  382.  
  383.     if(FastWindow)
  384.         ClearMenuStrip(FastWindow);
  385.  
  386.         /* Count the number of menu entries in
  387.          * the base menu.
  388.          */
  389.  
  390.     while(TermMenu[Count++] . nm_Type != NM_END);
  391.  
  392.         /* Add the quick dial entries. */
  393.  
  394.     if(Phonebook)
  395.     {
  396.         for(i = 0 ; PhoneCount < DIAL_MENU_MAX && i < NumPhoneEntries ; i++)
  397.         {
  398.             if(Phonebook[i] -> Header -> QuickMenu)
  399.                 PhoneCount++;
  400.         }
  401.     }
  402.  
  403.         /* Allocate new menu prototypes. */
  404.  
  405.     if(NewMenu = (struct NewMenu *)AllocVecPooled((Count + PhoneCount) * sizeof(struct NewMenu),MEMF_ANY | MEMF_CLEAR))
  406.     {
  407.         CopyMem(TermMenu,NewMenu,Count * sizeof(struct NewMenu));
  408.  
  409.         if(PhoneCount)
  410.         {
  411.             Count--;
  412.  
  413.             PhoneCount = 0;
  414.  
  415.             FirstDialMenu = -1;
  416.  
  417.             for(i = 0 ; PhoneCount < DIAL_MENU_MAX && i < NumPhoneEntries ; i++)
  418.             {
  419.                 if(Phonebook[i] -> Header -> QuickMenu)
  420.                 {
  421.                     NewMenu[Count] . nm_Type    = NM_ITEM;
  422.                     NewMenu[Count] . nm_Label    = Phonebook[i] -> Header -> Name;
  423.                     NewMenu[Count] . nm_Flags    = CHECKIT;
  424.                     NewMenu[Count] . nm_UserData    = (APTR)(DIAL_MENU_LIMIT + i);
  425.  
  426.                     PhoneCount++;
  427.                     Count++;
  428.  
  429.                     if(FirstDialMenu == -1)
  430.                         FirstDialMenu = DIAL_MENU_LIMIT + i;
  431.                 }
  432.             }
  433.  
  434.             NewMenu[Count] . nm_Type = NM_END;
  435.         }
  436.         else
  437.             NewMenu[Count - 2] . nm_Type = NM_END;
  438.  
  439.             /* Create the menu strip. */
  440.  
  441.         if(!(MenuNew = CreateMenus(NewMenu,TAG_DONE)))
  442.         {
  443.             FreeVecPooled(NewMenu);
  444.  
  445.             goto Simple;
  446.         }
  447.  
  448.             /* Do the menu layout. */
  449.  
  450.         if(!LayoutMenus(MenuNew,VisualInfo,
  451.             GTMN_NewLookMenus,    TRUE,
  452.             GTMN_TextAttr,        &UserFont,
  453.  
  454.             AmigaGlyph ? GTMN_AmigaKey :  TAG_IGNORE, AmigaGlyph,
  455.             CheckGlyph ? GTMN_Checkmark : TAG_IGNORE, CheckGlyph,
  456.         TAG_DONE))
  457.         {
  458.             FreeVecPooled(NewMenu);
  459.  
  460.             FreeMenus(MenuNew);
  461.  
  462.             goto Simple;
  463.         }
  464.  
  465.         FreeVecPooled(NewMenu);
  466.     }
  467.     else
  468.     {
  469.             /* Create the menu strip. */
  470.  
  471. Simple:        if(!(MenuNew = CreateMenus(TermMenu,TAG_DONE)))
  472.         {
  473.             ReleaseWindows();
  474.  
  475.             return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_MENUS_TXT));
  476.         }
  477.  
  478.             /* Do the menu layout. */
  479.  
  480.         if(!LayoutMenus(MenuNew,VisualInfo,
  481.             AmigaGlyph ? GTMN_AmigaKey :  TAG_IGNORE, AmigaGlyph,
  482.             CheckGlyph ? GTMN_Checkmark : TAG_IGNORE, CheckGlyph,
  483.  
  484.             GTMN_NewLookMenus,    TRUE,
  485.             GTMN_TextAttr,        &UserFont,
  486.         TAG_DONE))
  487.         {
  488.             ReleaseWindows();
  489.  
  490.             FreeMenus(MenuNew);
  491.  
  492.             return(LocaleString(MSG_TERMINIT_FAILED_TO_LAYOUT_MENUS_TXT));
  493.         }
  494.     }
  495.  
  496.     if(Menu)
  497.     {
  498.         CopyMenuFlags(Menu,MenuNew);
  499.  
  500.         FreeMenus(Menu);
  501.     }
  502.  
  503.     Menu = MenuNew;
  504.  
  505.     if(Window)
  506.         SetMenuStrip(Window,Menu);
  507.  
  508.     if(StatusWindow)
  509.         SetMenuStrip(StatusWindow,Menu);
  510.  
  511.     if(FastWindow)
  512.         SetMenuStrip(FastWindow,Menu);
  513.  
  514.     ReleaseWindows();
  515.  
  516.     return(NULL);
  517. }
  518.  
  519.     /* PaletteSetup():
  520.      *
  521.      *    Set up colour palettes.
  522.      */
  523.  
  524. VOID __regargs
  525. PaletteSetup(struct Configuration *SomeConfig)
  526. {
  527.     WORD i;
  528.  
  529.     if(!SomeConfig)
  530.         SomeConfig = Config;
  531.  
  532.     CopyMem(SomeConfig -> ScreenConfig -> Colours, NormalColours    ,16 * sizeof(UWORD));
  533.     CopyMem(SomeConfig -> ScreenConfig -> Colours,&NormalColours[16],16 * sizeof(UWORD));
  534.  
  535.     CopyMem(NormalColours,BlinkColours,32 * sizeof(UWORD));
  536.  
  537.     switch(SomeConfig -> ScreenConfig -> ColourMode)
  538.     {
  539.         case COLOUR_EIGHT:
  540.  
  541.             if(SomeConfig -> ScreenConfig -> Blinking)
  542.             {
  543.                 for(i = 0 ; i < 8 ; i++)
  544.                     BlinkColours[8 + i] = BlinkColours[0];
  545.  
  546.                 PaletteSize = 16;
  547.             }
  548.             else
  549.                 PaletteSize = 8;
  550.  
  551.             break;
  552.  
  553.         case COLOUR_SIXTEEN:
  554.  
  555.             if(Window -> WScreen -> RastPort . BitMap -> Depth >= 5 && SomeConfig -> ScreenConfig -> Blinking)
  556.             {
  557.                 for(i = 0 ; i < 16 ; i++)
  558.                     BlinkColours[16 + i] = BlinkColours[0];
  559.  
  560.                 PaletteSize = 32;
  561.             }
  562.             else
  563.                 PaletteSize = 16;
  564.  
  565.             break;
  566.  
  567.         case COLOUR_AMIGA:
  568.  
  569.             BlinkColours[3] = BlinkColours[0];
  570.  
  571.             PaletteSize = 4;
  572.  
  573.             break;
  574.  
  575.         case COLOUR_MONO:
  576.  
  577.             PaletteSize = 2;
  578.             break;
  579.     }
  580. }
  581.  
  582.     /* ResetCursorKeys(struct CursorKeys *Keys):
  583.      *
  584.      *    Reset cursor key assignments to defaults.
  585.      */
  586.  
  587. VOID __regargs
  588. ResetCursorKeys(struct CursorKeys *Keys)
  589. {
  590.     STATIC STRPTR Defaults[4] =
  591.     {
  592.         "\\e[A",
  593.         "\\e[B",
  594.         "\\e[C",
  595.         "\\e[D"
  596.     };
  597.  
  598.     WORD i,j;
  599.  
  600.     for(i = 0 ; i < 4 ; i++)
  601.     {
  602.         for(j = 0 ; j < 4 ; j++)
  603.             strcpy(Keys -> Keys[j][i],Defaults[i]);
  604.     }
  605. }
  606.  
  607.     /* ResetMacroKeys(struct MacroKeys *Keys):
  608.      *
  609.      *    Reset the macro key assignments to defaults.
  610.      */
  611.  
  612. STATIC VOID __regargs
  613. ResetMacroKeys(struct MacroKeys *Keys)
  614. {
  615.     STATIC STRPTR FunctionKeyCodes[4] =
  616.     {
  617.         "\\eOP",
  618.         "\\eOQ",
  619.         "\\eOR",
  620.         "\\eOS"
  621.     };
  622.  
  623.     WORD i;
  624.  
  625.     memset(Keys,0,sizeof(struct MacroKeys));
  626.  
  627.     for(i = 0 ; i < 4 ; i++)
  628.         strcpy(Keys -> Keys[1][i],FunctionKeyCodes[i]);
  629. }
  630.  
  631.     /* ScreenSizeStuff():
  632.      *
  633.      *    Set up the terminal screen size.
  634.      */
  635.  
  636. VOID
  637. ScreenSizeStuff()
  638. {
  639.     ObtainSemaphore(&TerminalSemaphore);
  640.  
  641.         /* Is this really the built-in emulation? */
  642.  
  643.     if(Config -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL)
  644.     {
  645.         LONG    MaxColumns    = WindowWidth / TextFontWidth,
  646.             MaxLines    = WindowHeight / TextFontHeight,
  647.             Columns,
  648.             Lines;
  649.  
  650.             /* Drop the text area marker. */
  651.  
  652.         if(Marking)
  653.             DropMarker();
  654.  
  655.             /* Turn off the cursor. */
  656.  
  657.         ClearCursor();
  658.  
  659.             /* Set up the new screen width. */
  660.  
  661.         if(Config -> TerminalConfig -> NumColumns < 20)
  662.             Columns = MaxColumns;
  663.         else
  664.             Columns = Config -> TerminalConfig -> NumColumns;
  665.  
  666.             /* Set up the new screen height. */
  667.  
  668.         if(Config -> TerminalConfig -> NumLines < 20)
  669.             Lines = MaxLines;
  670.         else
  671.             Lines = Config -> TerminalConfig -> NumLines;
  672.  
  673.             /* More columns than we will be able to display? */
  674.  
  675.         if(Columns > MaxColumns)
  676.             Columns = MaxColumns;
  677.  
  678.             /* More lines than we will be able to display? */
  679.  
  680.         if(Lines > MaxLines)
  681.             Lines = MaxLines;
  682.  
  683.             /* Set up the central data. */
  684.  
  685.         LastColumn    = Columns - 1;
  686.         LastLine    = Lines - 1;
  687.         LastPixel    = MUL_X(Columns) - 1;
  688.  
  689.             /* Are we to clear the margin? */
  690.  
  691.         if(Columns < MaxColumns || Lines < MaxLines)
  692.         {
  693.                 /* Save the rendering attributes. */
  694.  
  695.             BackupRender();
  696.  
  697.                 /* Set the defaults. */
  698.  
  699.             SetAPen(RPort,MappedPens[0][0]);
  700.  
  701.             SetWrMsk(RPort,DepthMask);
  702.  
  703.                 /* Clear remaining columns. */
  704.  
  705.             if(Columns < MaxColumns)
  706.                 ScrollLineRectFill(RPort,MUL_X(LastColumn + 1),0,WindowWidth - 1,WindowHeight - 1);
  707.  
  708.                 /* Clear remaining lines. */
  709.  
  710.             if(Lines < MaxLines)
  711.                 ScrollLineRectFill(RPort,0,MUL_Y(LastLine + 1),WindowWidth - 1,WindowHeight - 1);
  712.  
  713.                 /* Restore rendering attributes. */
  714.  
  715.             BackupRender();
  716.         }
  717.  
  718.             /* Truncate illegal cursor position. */
  719.  
  720.         if(CursorY > LastLine)
  721.             CursorY = LastLine;
  722.  
  723.         ConFontScaleUpdate();
  724.  
  725.             /* Truncate illegal cursor position. */
  726.  
  727.         if(CursorX > LastColumn)
  728.             CursorX = LastColumn;
  729.  
  730.             /* Reset the cursor position. */
  731.  
  732.         if(Config -> EmulationConfig -> FontScale == SCALE_HALF)
  733.             CursorX *= 2;
  734.         else
  735.         {
  736.             if(PrivateConfig -> EmulationConfig -> FontScale == SCALE_HALF)
  737.                 CursorX /= 2;
  738.         }
  739.  
  740.             /* Fix scroll region button. */
  741.  
  742.         if(!RegionSet)
  743.             Bottom = LastLine;
  744.  
  745.             /* Turn the cursor back on. */
  746.  
  747.         DrawCursor();
  748.     }
  749.  
  750.     FixScreenSize = FALSE;
  751.  
  752.     ReleaseSemaphore(&TerminalSemaphore);
  753. }
  754.  
  755.     /* PubScreenStuff():
  756.      *
  757.      *    This part handles the public screen setup stuff.
  758.      */
  759.  
  760. VOID
  761. PubScreenStuff()
  762. {
  763.     if(Screen)
  764.     {
  765.             /* Are we to make our screen public? */
  766.  
  767.         if(Config -> ScreenConfig -> MakeScreenPublic)
  768.             PubScreenStatus(Screen,NULL);
  769.         else
  770.             PubScreenStatus(Screen,PSNF_PRIVATE);
  771.  
  772.             /* Are we to `shanghai' Workbench windows? */
  773.  
  774.         if(Config -> ScreenConfig -> ShanghaiWindows)
  775.         {
  776.             PublicModes |= SHANGHAI;
  777.  
  778.             SetPubScreenModes(PublicModes);
  779.  
  780.                 /* Make this the default public screen. */
  781.  
  782.             SetDefaultPubScreen(TermIDString);
  783.         }
  784.         else
  785.         {
  786.             PublicModes &= ~SHANGHAI;
  787.  
  788.             if(LockPubScreen(DefaultPubScreenName))
  789.             {
  790.                 SetDefaultPubScreen(DefaultPubScreenName);
  791.  
  792.                 UnlockPubScreen(DefaultPubScreenName,NULL);
  793.             }
  794.             else
  795.                 SetDefaultPubScreen(NULL);
  796.  
  797.             SetPubScreenModes(PublicModes);
  798.         }
  799.     }
  800.  
  801.     FixPubScreenMode = FALSE;
  802. }
  803.  
  804.     /* ConfigSetup():
  805.      *
  806.      *    Compare the current configuration with the
  807.      *    last backup and reset the serial device, terminal,
  808.      *    etc. if necessary.
  809.      */
  810.  
  811. VOID
  812. ConfigSetup()
  813. {
  814.     BYTE RasterWasEnabled = RasterEnabled;
  815.  
  816.         /* Hide or show the upload queue icon. */
  817.  
  818.     ToggleUploadQueueIcon(Config -> MiscConfig -> HideUploadIcon);
  819.  
  820.         /* Take care of the end-of-line translation. */
  821.  
  822.     Update_CR_LF_Translation();
  823.  
  824.         /* First we will take a look at the configuration
  825.          * and try to find those parts which have changed
  826.          * and require the main screen display to be
  827.          * reopened.
  828.          */
  829.  
  830.     if(PrivateConfig -> ScreenConfig -> FontHeight != Config -> ScreenConfig -> FontHeight)
  831.         ResetDisplay = TRUE;
  832.  
  833.     if(PrivateConfig -> ScreenConfig -> SplitStatus != Config -> ScreenConfig -> SplitStatus)
  834.         ResetDisplay = TRUE;
  835.  
  836.     if(PrivateConfig -> ScreenConfig -> ShareScreen != Config -> ScreenConfig -> ShareScreen)
  837.         ResetDisplay = TRUE;
  838.  
  839.     if(PrivateConfig -> ScreenConfig -> Depth != Config -> ScreenConfig -> Depth || PrivateConfig -> ScreenConfig -> OverscanType != Config -> ScreenConfig -> OverscanType)
  840.         ResetDisplay = TRUE;
  841.  
  842.     if(PrivateConfig -> ScreenConfig -> DisplayWidth != Config -> ScreenConfig -> DisplayWidth || PrivateConfig -> ScreenConfig -> DisplayHeight != Config -> ScreenConfig -> DisplayHeight)
  843.         ResetDisplay = TRUE;
  844.  
  845.     if(Stricmp(PrivateConfig -> ScreenConfig -> FontName,Config -> ScreenConfig -> FontName))
  846.         ResetDisplay = TRUE;
  847.  
  848.     if(PrivateConfig -> TerminalConfig -> FontMode != Config -> TerminalConfig -> FontMode)
  849.         ResetDisplay = TRUE;
  850.  
  851.     if(PrivateConfig -> TerminalConfig -> UseTerminalTask != Config -> TerminalConfig -> UseTerminalTask)
  852.         ResetDisplay = TRUE;
  853.  
  854.     if(PrivateConfig -> TerminalConfig -> TextFontHeight != Config -> TerminalConfig -> TextFontHeight)
  855.         ResetDisplay = TRUE;
  856.  
  857.     if(Stricmp(PrivateConfig -> TerminalConfig -> TextFontName,Config -> TerminalConfig -> TextFontName))
  858.         ResetDisplay = TRUE;
  859.  
  860.     if(PrivateConfig -> ScreenConfig -> DisplayMode != Config -> ScreenConfig -> DisplayMode || PrivateConfig -> ScreenConfig -> ColourMode != Config -> ScreenConfig -> ColourMode)
  861.         ResetDisplay = TRUE;
  862.  
  863.     if(PrivateConfig -> TerminalConfig -> IBMFontHeight != Config -> TerminalConfig -> IBMFontHeight)
  864.         ResetDisplay = TRUE;
  865.  
  866.     if(Stricmp(PrivateConfig -> TerminalConfig -> IBMFontName,Config -> TerminalConfig -> IBMFontName))
  867.         ResetDisplay = TRUE;
  868.  
  869.     if((PrivateConfig -> ScreenConfig -> ColourMode == Config -> ScreenConfig -> ColourMode) && (PrivateConfig -> ScreenConfig -> ColourMode == COLOUR_EIGHT || PrivateConfig -> ScreenConfig -> ColourMode == COLOUR_SIXTEEN))
  870.     {
  871.         if(PrivateConfig -> ScreenConfig -> Blinking != Config -> ScreenConfig -> Blinking)
  872.             ResetDisplay = TRUE;
  873.     }
  874.  
  875.     if(Kick30)
  876.     {
  877.         if(Config -> ScreenConfig -> UsePens != PrivateConfig -> ScreenConfig -> UsePens || memcmp(Config -> ScreenConfig -> PenArray,PrivateConfig -> ScreenConfig -> PenArray,sizeof(UWORD) * 12))
  878.             ResetDisplay = TRUE;
  879.     }
  880.  
  881.     if(Config -> ScreenConfig -> Depth != PrivateConfig -> ScreenConfig -> Depth)
  882.         ResetDisplay = TRUE;
  883.  
  884.     if(PrivateConfig -> ScreenConfig -> FasterLayout != Config -> ScreenConfig -> FasterLayout)
  885.         ResetDisplay = TRUE;
  886.  
  887.     if(PrivateConfig -> ScreenConfig -> StatusLine != Config -> ScreenConfig -> StatusLine)
  888.         ResetDisplay = TRUE;
  889.  
  890.     if(PrivateConfig -> ScreenConfig -> TitleBar != Config -> ScreenConfig -> TitleBar)
  891.         ResetDisplay = TRUE;
  892.  
  893.     if(PrivateConfig -> ScreenConfig -> UseWorkbench != Config -> ScreenConfig -> UseWorkbench)
  894.         ResetDisplay = TRUE;
  895.  
  896.     if(strcmp(PrivateConfig -> ScreenConfig -> PubScreenName,Config -> ScreenConfig -> PubScreenName) && Config -> ScreenConfig -> UseWorkbench)
  897.         ResetDisplay = TRUE;
  898.  
  899.         /* Now for the `harmless' actions which do not
  900.          * require to change the screen or other
  901.          * rendering data.
  902.          */
  903.  
  904.     if(!ResetDisplay)
  905.     {
  906.         if(PrivateConfig -> TerminalConfig -> NumColumns != Config -> TerminalConfig -> NumColumns || PrivateConfig -> TerminalConfig -> NumLines != Config -> TerminalConfig -> NumLines)
  907.         {
  908.             if(Config -> ScreenConfig -> UseWorkbench)
  909.             {
  910.                 UWORD    Width,
  911.                     Height;
  912.  
  913.                 if(Config -> TerminalConfig -> NumColumns < 20)
  914.                     Width = ScreenWidth;
  915.                 else
  916.                     Width = Window -> BorderLeft + TextFontWidth * Config -> TerminalConfig -> NumColumns + Window -> BorderRight;
  917.  
  918.                 if(Config -> TerminalConfig -> NumLines < 20)
  919.                     Height = ScreenHeight;
  920.                 else
  921.                     Height = Window -> BorderTop + TextFontHeight * Config -> TerminalConfig -> NumLines + Window -> BorderBottom;
  922.  
  923.                 ChangeWindowBox(Window,Window -> LeftEdge,Window -> TopEdge,Width,Height);
  924.  
  925.                 FixScreenSize = TRUE;
  926.             }
  927.             else
  928.                 ResetDisplay = TRUE;
  929.         }
  930.     }
  931.  
  932.     if(PrivateConfig -> ScreenConfig -> MakeScreenPublic != Config -> ScreenConfig -> MakeScreenPublic || PrivateConfig -> ScreenConfig -> ShanghaiWindows != Config -> ScreenConfig -> ShanghaiWindows)
  933.         PubScreenStuff();
  934.  
  935.     if(PrivateConfig -> ScreenConfig -> ColourMode == Config -> ScreenConfig -> ColourMode && memcmp(PrivateConfig -> ScreenConfig -> Colours,Config -> ScreenConfig -> Colours,sizeof(UWORD) * 16))
  936.     {
  937.         switch(Config -> ScreenConfig -> ColourMode)
  938.         {
  939.             case COLOUR_EIGHT:
  940.  
  941.                 CopyMem(Config -> ScreenConfig -> Colours,ANSIColours,16 * sizeof(UWORD));
  942.                 break;
  943.  
  944.             case COLOUR_SIXTEEN:
  945.  
  946.                 CopyMem(Config -> ScreenConfig -> Colours,EGAColours,16 * sizeof(UWORD));
  947.                 break;
  948.  
  949.             case COLOUR_AMIGA:
  950.  
  951.                 CopyMem(Config -> ScreenConfig -> Colours,DefaultColours,16 * sizeof(UWORD));
  952.                 break;
  953.  
  954.             case COLOUR_MONO:
  955.  
  956.                 CopyMem(Config -> ScreenConfig -> Colours,AtomicColours,16 * sizeof(UWORD));
  957.                 break;
  958.         }
  959.     }
  960.  
  961.         /* Are we to load a new transfer library? */
  962.  
  963.     if(Config -> TransferConfig -> DefaultLibrary[0] && strcmp(PrivateConfig -> TransferConfig -> DefaultLibrary,Config -> TransferConfig -> DefaultLibrary))
  964.     {
  965.         strcpy(LastXprLibrary,Config -> TransferConfig -> DefaultLibrary);
  966.  
  967.         ProtocolSetup(FALSE);
  968.     }
  969.  
  970.         /* No custom keymap this time? */
  971.  
  972.     if(!Config -> TerminalConfig -> KeyMapFileName[0])
  973.     {
  974.         KeyMap = NULL;
  975.  
  976.         if(KeySegment)
  977.         {
  978.             UnLoadSeg(KeySegment);
  979.  
  980.             KeySegment = NULL;
  981.         }
  982.     }
  983.     else
  984.     {
  985.             /* Check whether the keymap name has changed. */
  986.  
  987.         if(strcmp(PrivateConfig -> TerminalConfig -> KeyMapFileName,Config -> TerminalConfig -> KeyMapFileName))
  988.             KeyMap = LoadKeyMap(Config -> TerminalConfig -> KeyMapFileName);
  989.     }
  990.  
  991.         /* Are we to load the keyboard macro settings? */
  992.  
  993.     if(Config -> FileConfig -> MacroFileName[0] && Stricmp(PrivateConfig -> FileConfig -> MacroFileName,Config -> FileConfig -> MacroFileName))
  994.     {
  995.         if(!LoadMacros(Config -> FileConfig -> MacroFileName,MacroKeys))
  996.             ResetMacroKeys(MacroKeys);
  997.         else
  998.             strcpy(LastMacros,Config -> FileConfig -> MacroFileName);
  999.     }
  1000.  
  1001.         /* Are we to load the cursor key settings? */
  1002.  
  1003.     if(Config -> FileConfig -> CursorFileName[0] && Stricmp(PrivateConfig -> FileConfig -> CursorFileName,Config -> FileConfig -> CursorFileName))
  1004.     {
  1005.         if(!ReadIFFData(Config -> FileConfig -> CursorFileName,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  1006.             ResetCursorKeys(CursorKeys);
  1007.         else
  1008.             strcpy(LastCursorKeys,Config -> FileConfig -> CursorFileName);
  1009.     }
  1010.  
  1011.         /* Are we to load the translation tables? */
  1012.  
  1013.     if(Config -> FileConfig -> TranslationFileName[0] && Stricmp(PrivateConfig -> FileConfig -> TranslationFileName,Config -> FileConfig -> TranslationFileName))
  1014.     {
  1015.         if(SendTable)
  1016.         {
  1017.             FreeTranslationTable(SendTable);
  1018.  
  1019.             SendTable = NULL;
  1020.         }
  1021.  
  1022.         if(ReceiveTable)
  1023.         {
  1024.             FreeTranslationTable(ReceiveTable);
  1025.  
  1026.             ReceiveTable = NULL;
  1027.         }
  1028.  
  1029.         if(SendTable = AllocTranslationTable())
  1030.         {
  1031.             if(ReceiveTable = AllocTranslationTable())
  1032.             {
  1033.                 if(LoadTranslationTables(Config -> FileConfig -> TranslationFileName,SendTable,ReceiveTable))
  1034.                 {
  1035.                     strcpy(LastTranslation,Config -> FileConfig -> TranslationFileName);
  1036.  
  1037.                     if(IsStandardTable(SendTable) && IsStandardTable(ReceiveTable))
  1038.                     {
  1039.                         FreeTranslationTable(SendTable);
  1040.  
  1041.                         SendTable = NULL;
  1042.  
  1043.                         FreeTranslationTable(ReceiveTable);
  1044.  
  1045.                         ReceiveTable = NULL;
  1046.                     }
  1047.                 }
  1048.                 else
  1049.                 {
  1050.                     FreeTranslationTable(SendTable);
  1051.  
  1052.                     SendTable = NULL;
  1053.  
  1054.                     FreeTranslationTable(ReceiveTable);
  1055.  
  1056.                     ReceiveTable = NULL;
  1057.                 }
  1058.             }
  1059.             else
  1060.             {
  1061.                 FreeTranslationTable(SendTable);
  1062.  
  1063.                 SendTable = NULL;
  1064.             }
  1065.         }
  1066.     }
  1067.  
  1068.         /* Update the text sending functions. */
  1069.  
  1070.     SendSetup();
  1071.  
  1072.         /* Are we to load the fast macro settings? */
  1073.  
  1074.     if(Config -> FileConfig -> FastMacroFileName[0] && Stricmp(PrivateConfig -> FileConfig -> FastMacroFileName,Config -> FileConfig -> FastMacroFileName))
  1075.     {
  1076.         if(LoadFastMacros(Config -> FileConfig -> FastMacroFileName,&FastMacroList))
  1077.             strcpy(LastFastMacros,Config -> FileConfig -> FastMacroFileName);
  1078.     }
  1079.  
  1080.         /* Serial configuration needs updating? */
  1081.  
  1082.     ReconfigureSerial(Window,NULL);
  1083.  
  1084.         /* Are we to open the fast macro panel? */
  1085.  
  1086.     if(Config -> MiscConfig -> OpenFastMacroPanel)
  1087.         HadFastMacros = TRUE;
  1088.  
  1089.         /* Are we to freeze the text buffer? */
  1090.  
  1091.     if(!Config -> CaptureConfig -> BufferEnabled)
  1092.         BufferFrozen = TRUE;
  1093.  
  1094.         /* Now for the actions which require that the
  1095.          * screen stays open.
  1096.          */
  1097.  
  1098.     if(!ResetDisplay)
  1099.     {
  1100.         if(Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  1101.         {
  1102.             if(PrivateConfig -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL || (Config -> TerminalConfig -> EmulationFileName[0] && strcmp(PrivateConfig -> TerminalConfig -> EmulationFileName,Config -> TerminalConfig -> EmulationFileName)))
  1103.             {
  1104.                 if(!OpenEmulator(Config -> TerminalConfig -> EmulationFileName))
  1105.                 {
  1106.                     Config -> TerminalConfig -> EmulationMode = EMULATION_ANSIVT100;
  1107.  
  1108.                     ResetDisplay = TRUE;
  1109.  
  1110.                     RasterEnabled = TRUE;
  1111.  
  1112.                     MyEasyRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Config -> TerminalConfig -> EmulationFileName);
  1113.                 }
  1114.                 else
  1115.                     RasterEnabled = FALSE;
  1116.             }
  1117.         }
  1118.         else
  1119.         {
  1120.             if(XEmulatorBase && PrivateConfig -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  1121.             {
  1122.                 XEmulatorClearConsole(XEM_IO);
  1123.  
  1124.                 CloseEmulator();
  1125.  
  1126.                 RasterEnabled = TRUE;
  1127.  
  1128.                 ClearCursor();
  1129.  
  1130.                 Reset();
  1131.  
  1132.                 DrawCursor();
  1133.             }
  1134.             else
  1135.                 RasterEnabled = TRUE;
  1136.         }
  1137.  
  1138.         if(RasterEnabled != RasterWasEnabled)
  1139.             RasterEraseScreen(2);
  1140.  
  1141.         if(!Config -> ScreenConfig -> UseWorkbench && !SharedScreen)
  1142.         {
  1143.             PaletteSetup(Config);
  1144.  
  1145.             LoadRGB4(VPort,NormalColours,PaletteSize);
  1146.         }
  1147.  
  1148.         if(Config -> MiscConfig -> OpenFastMacroPanel && !FastWindow)
  1149.             OpenFastWindow();
  1150.  
  1151.         PubScreenStuff();
  1152.  
  1153.         if(Menu)
  1154.         {
  1155.             CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  1156.  
  1157.             if(!XProtocolBase)
  1158.                 SetTransferMenu(FALSE);
  1159.             else
  1160.                 SetTransferMenu(TRUE);
  1161.  
  1162.             SetRasterMenu(RasterEnabled);
  1163.         }
  1164.  
  1165.         Blocking = FALSE;
  1166.     }
  1167.     else
  1168.     {
  1169.             /* Are we no longer to use the external emulator? */
  1170.  
  1171.         if(Config -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL)
  1172.         {
  1173.             if(XEmulatorBase && PrivateConfig -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  1174.             {
  1175.                 XEmulatorClearConsole(XEM_IO);
  1176.  
  1177.                 CloseEmulator();
  1178.             }
  1179.         }
  1180.  
  1181.         RasterEnabled = TRUE;
  1182.     }
  1183.  
  1184.         /* Change the task priority. */
  1185.  
  1186.     SetTaskPri(ThisProcess,(LONG)Config -> MiscConfig -> Priority);
  1187.  
  1188.     ConOutputUpdate();
  1189.  
  1190.     ConFontScaleUpdate();
  1191.  
  1192.     ConProcessUpdate();
  1193.  
  1194.         /* Reset the scanner. */
  1195.  
  1196.     FlowInit(TRUE);
  1197. }
  1198.  
  1199.     /* DisplayReset():
  1200.      *
  1201.      *    Reset the entire display if necessary.
  1202.      */
  1203.  
  1204. BYTE
  1205. DisplayReset()
  1206. {
  1207.     UBYTE    *Result;
  1208.     BYTE     Success = TRUE;
  1209.  
  1210.         /* Delete the display (if possible).
  1211.          * This will go wrong if there
  1212.          * are any visitor windows on our
  1213.          * screen.
  1214.          */
  1215.  
  1216.     if(DeleteDisplay())
  1217.     {
  1218.         if(Result = CreateDisplay(FALSE))
  1219.         {
  1220.             DeleteDisplay();
  1221.  
  1222.             MyEasyRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Result);
  1223.  
  1224.             Success = FALSE;
  1225.         }
  1226.         else
  1227.         {
  1228.             BumpWindow(Window);
  1229.  
  1230.             PubScreenStuff();
  1231.  
  1232.             DisplayReopened = TRUE;
  1233.         }
  1234.     }
  1235.     else
  1236.     {
  1237.         SaveConfig(PrivateConfig,Config);
  1238.  
  1239.         BlockWindows();
  1240.  
  1241.         MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),LocaleString(MSG_TERMMAIN_CANNOT_CLOSE_SCREEN_YET_TXT));
  1242.  
  1243.         ReleaseWindows();
  1244.  
  1245.         Success = TRUE;
  1246.     }
  1247.  
  1248.     ResetDisplay = FALSE;
  1249.  
  1250.         /* Prepare for the worst case... */
  1251.  
  1252.     if(Success)
  1253.         Apocalypse = FALSE;
  1254.     else
  1255.         MainTerminated = Apocalypse = TRUE;
  1256.  
  1257.     return(Success);
  1258. }
  1259.  
  1260.     /* DeleteDisplay():
  1261.      *
  1262.      *    Free all resources associated with the terminal
  1263.      *    display (tasks, interrupts, screen, window, etc.).
  1264.      */
  1265.  
  1266. BYTE
  1267. DeleteDisplay()
  1268. {
  1269.     if(Screen)
  1270.     {
  1271.         struct List        *PubScreenList;
  1272.         struct PubScreenNode    *ScreenNode;
  1273.  
  1274.         PubScreenList = LockPubScreenList();
  1275.  
  1276.         for(ScreenNode = (struct PubScreenNode *)PubScreenList -> lh_Head ; ScreenNode -> psn_Node . ln_Succ ; ScreenNode = (struct PubScreenNode *)ScreenNode -> psn_Node . ln_Succ)
  1277.         {
  1278.             if(ScreenNode -> psn_Screen == Screen)
  1279.                 break;
  1280.         }
  1281.  
  1282.         if(ScreenNode && ScreenNode -> psn_Node . ln_Succ)
  1283.         {
  1284.             if(ScreenNode -> psn_VisitorCount)
  1285.             {
  1286.                 UnlockPubScreenList();
  1287.  
  1288.                 return(FALSE);
  1289.             }
  1290.             else
  1291.             {
  1292.                 Forbid();
  1293.  
  1294.                 UnlockPubScreenList();
  1295.  
  1296.                 PubScreenStatus(Screen,PSNF_PRIVATE);
  1297.  
  1298.                 Permit();
  1299.             }
  1300.         }
  1301.         else
  1302.             UnlockPubScreenList();
  1303.     }
  1304.  
  1305.     if(SharedScreen)
  1306.     {
  1307.         struct List        *PubScreenList;
  1308.         struct PubScreenNode    *ScreenNode;
  1309.  
  1310.         PubScreenList = LockPubScreenList();
  1311.  
  1312.         for(ScreenNode = (struct PubScreenNode *)PubScreenList -> lh_Head ; ScreenNode -> psn_Node . ln_Succ ; ScreenNode = (struct PubScreenNode *)ScreenNode -> psn_Node . ln_Succ)
  1313.         {
  1314.             if(ScreenNode -> psn_Screen == SharedScreen)
  1315.                 break;
  1316.         }
  1317.  
  1318.         if(ScreenNode && ScreenNode -> psn_Node . ln_Succ)
  1319.         {
  1320.             if(ScreenNode -> psn_VisitorCount)
  1321.             {
  1322.                 UnlockPubScreenList();
  1323.  
  1324.                 return(FALSE);
  1325.             }
  1326.             else
  1327.             {
  1328.                 Forbid();
  1329.  
  1330.                 UnlockPubScreenList();
  1331.  
  1332.                 PubScreenStatus(SharedScreen,PSNF_PRIVATE);
  1333.  
  1334.                 Permit();
  1335.             }
  1336.         }
  1337.         else
  1338.             UnlockPubScreenList();
  1339.     }
  1340.  
  1341.     GuideCleanup();
  1342.  
  1343.     DeleteLED();
  1344.  
  1345.     CloseQueueWindow();
  1346.  
  1347.     if(StatusProcess)
  1348.     {
  1349.         Forbid();
  1350.  
  1351.         Signal(StatusProcess,SIG_KILL);
  1352.  
  1353.         ClrSignal(SIG_HANDSHAKE);
  1354.  
  1355.         Wait(SIG_HANDSHAKE);
  1356.  
  1357.         Permit();
  1358.  
  1359.         StatusProcess = NULL;
  1360.     }
  1361.  
  1362.     if(Marking)
  1363.         FreeMarker();
  1364.  
  1365.     FirstClick    = TRUE;
  1366.     HoldClick    = FALSE;
  1367.  
  1368.     CloseInfoWindow();
  1369.  
  1370.     DeleteReview();
  1371.  
  1372.     DeleteEmulationProcess();
  1373.  
  1374.     if(Config)
  1375.     {
  1376.         if(Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL && XEmulatorBase)
  1377.             CloseEmulator();
  1378.     }
  1379.  
  1380.     DeleteRaster();
  1381.  
  1382.     DeleteScale();
  1383.  
  1384.     if(TabStops)
  1385.     {
  1386.         FreeVecPooled(TabStops);
  1387.  
  1388.         TabStops = NULL;
  1389.     }
  1390.  
  1391.     if(ScrollLines)
  1392.     {
  1393.         FreeVecPooled(ScrollLines);
  1394.  
  1395.         ScrollLines = NULL;
  1396.     }
  1397.  
  1398.     if(Screen)
  1399.         ScreenToBack(Screen);
  1400.  
  1401.     if(FastWindow)
  1402.     {
  1403.         HadFastMacros = TRUE;
  1404.  
  1405.         CloseFastWindow();
  1406.     }
  1407.     else
  1408.         HadFastMacros = FALSE;
  1409.  
  1410.         /* Clean up the menu glyphs. */
  1411.  
  1412.     DisposeObject(AmigaGlyph);
  1413.  
  1414.     AmigaGlyph = NULL;
  1415.  
  1416.     DisposeObject(CheckGlyph);
  1417.  
  1418.     CheckGlyph = NULL;
  1419.  
  1420.     if(StatusWindow)
  1421.     {
  1422.         ClearMenuStrip(StatusWindow);
  1423.         CloseWindowSafely(StatusWindow);
  1424.  
  1425.         StatusWindow = NULL;
  1426.     }
  1427.  
  1428.     if(DefaultPubScreen)
  1429.     {
  1430.         UnlockPubScreen(NULL,DefaultPubScreen);
  1431.  
  1432.         DefaultPubScreen = NULL;
  1433.     }
  1434.  
  1435.         /* Remove AppWindow link. */
  1436.  
  1437.     if(WorkbenchWindow)
  1438.     {
  1439.         RemoveAppWindow(WorkbenchWindow);
  1440.  
  1441.         WorkbenchWindow = NULL;
  1442.     }
  1443.  
  1444.         /* Remove AppWindow port and any pending messages. */
  1445.  
  1446.     if(WorkbenchPort)
  1447.     {
  1448.         struct Message *Message;
  1449.  
  1450.         while(Message = GetMsg(WorkbenchPort))
  1451.             ReplyMsg(Message);
  1452.  
  1453.         DeleteMsgPort(WorkbenchPort);
  1454.  
  1455.         WorkbenchPort = NULL;
  1456.     }
  1457.  
  1458.     if(DrawInfo)
  1459.     {
  1460.             /* Release the rendering pens. */
  1461.  
  1462.         FreeScreenDrawInfo(Window -> WScreen,DrawInfo);
  1463.  
  1464.         DrawInfo = NULL;
  1465.     }
  1466.  
  1467.     if(Window)
  1468.     {
  1469.         if(AllocatedPens && Kick30)
  1470.         {
  1471.             WORD i;
  1472.  
  1473.             ObtainSemaphore(&TerminalSemaphore);
  1474.  
  1475.                 /* Erase the window contents. We will
  1476.                  * want to release any pens we have
  1477.                  * allocated and want to avoid nasty
  1478.                  * flashing and flickering.
  1479.                  */
  1480.  
  1481.             SetAPen(RPort,0);
  1482.  
  1483.             RectFill(RPort,WindowLeft,WindowTop,WindowLeft + WindowWidth - 1,WindowTop + WindowHeight - 1);
  1484.  
  1485.                 /* Release any pens we have allocated. */
  1486.  
  1487.             for(i = 0 ; i < 16 ; i++)
  1488.             {
  1489.                 if(MappedPens[1][i])
  1490.                 {
  1491.                     ReleasePen(VPort -> ColorMap,MappedPens[0][i]);
  1492.  
  1493.                     MappedPens[0][i] = i;
  1494.                     MappedPens[1][i] = FALSE;
  1495.                 }
  1496.             }
  1497.  
  1498.             AllocatedPens = FALSE;
  1499.  
  1500.             ReleaseSemaphore(&TerminalSemaphore);
  1501.         }
  1502.  
  1503.         if(ClipRegion)
  1504.         {
  1505.             InstallClipRegion(Window -> WLayer,OldRegion);
  1506.  
  1507.             DisposeRegion(ClipRegion);
  1508.  
  1509.             ClipRegion = NULL;
  1510.         }
  1511.  
  1512.         ClearMenuStrip(Window);
  1513.  
  1514.         ThisProcess -> pr_WindowPtr = OldWindowPtr;
  1515.  
  1516.         PopWindow();
  1517.  
  1518.         if(TermPort)
  1519.             TermPort -> TopWindow = NULL;
  1520.  
  1521.         LT_DeleteWindowLock(Window);
  1522.  
  1523.         CloseWindow(Window);
  1524.  
  1525.         Window = NULL;
  1526.  
  1527.         if(StatusGadget)
  1528.             DeleteStatusGadget(StatusGadget);
  1529.  
  1530.         StatusGadget = NULL;
  1531.     }
  1532.  
  1533.     if(Menu)
  1534.     {
  1535.         FreeMenus(Menu);
  1536.  
  1537.         Menu = NULL;
  1538.     }
  1539.  
  1540.     if(VisualInfo)
  1541.     {
  1542.         FreeVisualInfo(VisualInfo);
  1543.  
  1544.         VisualInfo = NULL;
  1545.     }
  1546.  
  1547.     DeletePacketWindow(FALSE);
  1548.  
  1549.     if(UserTextFont)
  1550.     {
  1551.         CloseFont(UserTextFont);
  1552.  
  1553.         UserTextFont = NULL;
  1554.     }
  1555.  
  1556.         /* Before we can close screen we will have to
  1557.          * make sure that it is no longer the default
  1558.          * public screen.
  1559.          */
  1560.  
  1561.     if(Config)
  1562.     {
  1563.         if(Config -> ScreenConfig -> ShanghaiWindows)
  1564.         {
  1565.             if(LockPubScreen(DefaultPubScreenName))
  1566.             {
  1567.                 SetDefaultPubScreen(DefaultPubScreenName);
  1568.  
  1569.                 UnlockPubScreen(DefaultPubScreenName,NULL);
  1570.             }
  1571.             else
  1572.                 SetDefaultPubScreen(NULL);
  1573.         }
  1574.     }
  1575.  
  1576.     if(Screen)
  1577.     {
  1578.         CloseScreen(Screen);
  1579.  
  1580.         Screen = NULL;
  1581.     }
  1582.  
  1583.     if(SharedScreen)
  1584.     {
  1585.         CloseScreen(SharedScreen);
  1586.  
  1587.         SharedScreen = NULL;
  1588.     }
  1589.  
  1590.     if(InterleavedBitMap)
  1591.     {
  1592.         DeleteInterleavedBitMap(InterleavedBitMap);
  1593.  
  1594.         InterleavedBitMap = NULL;
  1595.     }
  1596.  
  1597.     if(GFX)
  1598.     {
  1599.         CloseFont(GFX);
  1600.  
  1601.         GFX = NULL;
  1602.     }
  1603.  
  1604.     if(TextFont)
  1605.     {
  1606.         CloseFont(TextFont);
  1607.  
  1608.         TextFont = NULL;
  1609.     }
  1610.  
  1611.     return(TRUE);
  1612. }
  1613.  
  1614. STATIC VOID __regargs
  1615. StatusSizeSetup(struct Screen *Screen,LONG *StatusWidth,LONG *StatusHeight)
  1616. {
  1617.     SZ_SizeSetup(Screen,&UserFont);
  1618.  
  1619.     if(Config -> ScreenConfig -> StatusLine != STATUSLINE_DISABLED)
  1620.     {
  1621.         if(Config -> ScreenConfig -> StatusLine == STATUSLINE_COMPRESSED)
  1622.         {
  1623.             *StatusWidth    = 80 * UserFontWidth;
  1624.             *StatusHeight    = UserFontHeight;
  1625.  
  1626.             if(Config -> ScreenConfig -> SplitStatus)
  1627.             {
  1628.                 *StatusWidth    += 2;
  1629.                 *StatusHeight    += 2;
  1630.             }
  1631.         }
  1632.         else
  1633.         {
  1634.             LONG    i,Len,Max;
  1635.             UWORD    ColumnLeft[4],
  1636.                 ColumnWidth[4];
  1637.  
  1638.             *StatusWidth    = 0;
  1639.             *StatusHeight    = SZ_BoxHeight(2);
  1640.  
  1641.             ColumnLeft[0] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_STATUS_TXT,MSG_TERMSTATUSDISPLAY_FONT_TXT,-1);
  1642.             ColumnLeft[1] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_PROTOCOL_TXT,MSG_TERMSTATUSDISPLAY_TERMINAL_TXT,-1);
  1643.             ColumnLeft[2] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_BAUDRATE_TXT,MSG_TERMSTATUSDISPLAY_PARAMETERS_TXT,-1);
  1644.             ColumnLeft[3] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_TIME_TXT,MSG_TERMSTATUSDISPLAY_ONLINE_TXT,-1);
  1645.  
  1646.             Max = 0;
  1647.  
  1648.             for(i = MSG_TERMAUX_READY_TXT ; i <= MSG_TERMAUX_HANG_UP_TXT ; i++)
  1649.             {
  1650.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  1651.                     Max = Len;
  1652.             }
  1653.  
  1654.             for(i = MSG_TERMSTATUSDISPLAY_FROZEN_TXT ; i <= MSG_TERMSTATUSDISPLAY_RECORDING_TXT ; i++)
  1655.             {
  1656.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  1657.                     Max = Len;
  1658.             }
  1659.  
  1660.             ColumnWidth[0] = Max;
  1661.  
  1662.             Max = SZ_BoxWidth(12);
  1663.  
  1664.             for(i = MSG_TERMAUX_ANSI_VT102_TXT ; i <= MSG_TERMAUX_HEX_TXT ; i++)
  1665.             {
  1666.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  1667.                     Max = Len;
  1668.             }
  1669.  
  1670.             ColumnWidth[1] = Max;
  1671.  
  1672.             Max = SZ_BoxWidth(10);
  1673.  
  1674.             for(i = MSG_TERMAUX_NONE_TXT ; i <= MSG_TERMAUX_SPACE_TXT ; i++)
  1675.             {
  1676.                 if((Len = SZ_BoxWidth(4 + strlen(LocaleString(i)))) > Max)
  1677.                     Max = Len;
  1678.             }
  1679.  
  1680.             ColumnWidth[2] = Max;
  1681.  
  1682.             ColumnWidth[3] = SZ_BoxWidth(8);
  1683.  
  1684.             for(i = 0 ; i < 4 ; i++)
  1685.                 *StatusWidth += ColumnWidth[i] + ColumnLeft[i];
  1686.  
  1687.             *StatusWidth += 3 * InterWidth;
  1688.  
  1689.             if(!Config -> ScreenConfig -> SplitStatus)
  1690.                 *StatusHeight += 3;
  1691.             else
  1692.             {
  1693.                 *StatusWidth    += 2;
  1694.                 *StatusHeight    += 2;
  1695.             }
  1696.         }
  1697.     }
  1698.     else
  1699.         *StatusHeight = 0;
  1700. }
  1701.  
  1702.     /* CreateDisplay(BYTE FirstSetup):
  1703.      *
  1704.      *    Open the display and allocate associated data.
  1705.      */
  1706.  
  1707. STRPTR __regargs
  1708. CreateDisplay(BYTE FirstSetup)
  1709. {
  1710.     UWORD            Count = 0,i;
  1711.     LONG            ErrorCode,Top,Height;
  1712.     ULONG            TagArray[9];
  1713.     struct Rectangle    DisplayClip;
  1714.     BYTE            OpenFailed = FALSE,
  1715.                 RealDepth;
  1716.     STRPTR            Error;
  1717.     ULONG            X_DPI,Y_DPI;
  1718.     UWORD            PenArray[16];
  1719.     LONG            StatusWidth,
  1720.                 StatusHeight;
  1721.  
  1722.     BlockNestCount = 0;
  1723.  
  1724.     WeAreBlocking = FALSE;
  1725.  
  1726.     SetQueueDiscard(SpecialQueue,FALSE);
  1727.  
  1728.     TagDPI[0] . ti_Tag = TAG_DONE;
  1729.  
  1730.         /* Don't permit weird settings. */
  1731.  
  1732.     if(!Config -> ScreenConfig -> StatusLine || (!Config -> ScreenConfig -> ShareScreen && !Config -> ScreenConfig -> UseWorkbench))
  1733.         Config -> ScreenConfig -> SplitStatus = FALSE;
  1734.  
  1735.     if(Config -> ScreenConfig -> UseWorkbench || Config -> ScreenConfig -> ShareScreen)
  1736.     {
  1737.         STRPTR ScreenName = NULL;
  1738.  
  1739.         if(Config -> ScreenConfig -> PubScreenName[0])
  1740.         {
  1741.             struct Screen *SomeScreen;
  1742.  
  1743.             if(SomeScreen = LockPubScreen(Config -> ScreenConfig -> PubScreenName))
  1744.             {
  1745.                 UnlockPubScreen(NULL,SomeScreen);
  1746.  
  1747.                 ScreenName = Config -> ScreenConfig -> PubScreenName;
  1748.             }
  1749.         }
  1750.  
  1751.         if(!(DefaultPubScreen = LockPubScreen(ScreenName)))
  1752.             return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_DEFAULT_PUBLIC_SCREEN_TXT));
  1753.         else
  1754.         {
  1755.             GetDPI(GetVPModeID(&DefaultPubScreen -> ViewPort),&X_DPI,&Y_DPI);
  1756.  
  1757.                 /* gadtools.library v37 objects don't look too pretty
  1758.                  * with a proportional-spaced font.
  1759.                  */
  1760.  
  1761. /*            if(!(DefaultPubScreen -> Font -> ta_Flags & FPF_PROPORTIONAL) || Kick30)*/
  1762.  
  1763.             if(TRUE)
  1764.             {
  1765.                 strcpy(UserFontName,DefaultPubScreen -> Font -> ta_Name);
  1766.  
  1767.                 UserFont . tta_Name    = UserFontName;
  1768.                 UserFont . tta_YSize    = DefaultPubScreen -> Font -> ta_YSize;
  1769.                 UserFont . tta_Style    = DefaultPubScreen -> Font -> ta_Style;
  1770.                 UserFont . tta_Flags    = DefaultPubScreen -> Font -> ta_Flags;
  1771.             }
  1772.             else
  1773.             {
  1774.                 UnlockPubScreen(NULL,DefaultPubScreen);
  1775.  
  1776.                 DefaultPubScreen = NULL;
  1777.  
  1778.                 Config -> ScreenConfig -> UseWorkbench = FALSE;
  1779.             }
  1780.         }
  1781.     }
  1782.  
  1783.     if(!Config -> ScreenConfig -> UseWorkbench)
  1784.     {
  1785.         GetDPI(Config -> ScreenConfig -> DisplayMode,&X_DPI,&Y_DPI);
  1786.  
  1787.         strcpy(UserFontName,Config -> ScreenConfig -> FontName);
  1788.  
  1789.         UserFont . tta_Name    = UserFontName;
  1790.         UserFont . tta_YSize    = Config -> ScreenConfig -> FontHeight;
  1791.         UserFont . tta_Style    = FS_NORMAL | FSF_TAGGED;
  1792.         UserFont . tta_Flags    = FPF_DESIGNED;
  1793.         UserFont . tta_Tags    = TagDPI;
  1794.  
  1795.         TagDPI[0] . ti_Tag     = TA_DeviceDPI;
  1796.         TagDPI[0] . ti_Data     = (X_DPI << 16) | Y_DPI;
  1797.         TagDPI[1] . ti_Tag     = TAG_DONE;
  1798.     }
  1799.  
  1800.     if(!(UserTextFont = OpenDiskFont(&UserFont)))
  1801.     {
  1802.         if(Config -> ScreenConfig -> UseWorkbench)
  1803.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1804.         else
  1805.         {
  1806.             strcpy(Config -> ScreenConfig -> FontName,    "topaz.font");
  1807.             strcpy(UserFontName,                "topaz.font");
  1808.  
  1809.             Config -> ScreenConfig -> FontHeight = 8;
  1810.  
  1811.             UserFont . tta_YSize    = Config -> ScreenConfig -> FontHeight;
  1812.             UserFont . tta_Style    = FS_NORMAL;
  1813.             UserFont . tta_Flags    = FPF_DESIGNED | FPF_ROMFONT;
  1814.  
  1815.             if(!(UserTextFont = OpenFont(&UserFont)))
  1816.                 return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1817.         }
  1818.     }
  1819.  
  1820. Reopen:    if(Config -> TerminalConfig -> FontMode != FONT_STANDARD)
  1821.     {
  1822.         strcpy(TextFontName,Config -> TerminalConfig -> IBMFontName);
  1823.  
  1824.         TextAttr . tta_YSize = Config -> TerminalConfig -> IBMFontHeight;
  1825.     }
  1826.     else
  1827.     {
  1828.         strcpy(TextFontName,Config -> TerminalConfig -> TextFontName);
  1829.  
  1830.         TextAttr . tta_YSize = Config -> TerminalConfig -> TextFontHeight;
  1831.     }
  1832.  
  1833.     TextAttr . tta_Name    = TextFontName;
  1834.     TextAttr . tta_Style    = FS_NORMAL | FSF_TAGGED;
  1835.     TextAttr . tta_Flags    = FPF_DESIGNED;
  1836.     TextAttr . tta_Tags    = TagDPI;
  1837.  
  1838.     if(!(TextFont = OpenDiskFont(&TextAttr)))
  1839.     {
  1840.         if(Config -> TerminalConfig -> FontMode != FONT_STANDARD)
  1841.         {
  1842.             Config -> TerminalConfig -> FontMode = FONT_STANDARD;
  1843.  
  1844.             goto Reopen;
  1845.         }
  1846.  
  1847.         strcpy(Config -> TerminalConfig -> TextFontName,    "topaz.font");
  1848.         strcpy(TextFontName,                    "topaz.font");
  1849.  
  1850.         Config -> TerminalConfig -> TextFontHeight = 8;
  1851.  
  1852.         TextAttr . tta_YSize    = Config -> TerminalConfig -> TextFontHeight;
  1853.         TextAttr . tta_Style    = FS_NORMAL;
  1854.         TextAttr . tta_Flags    = FPF_DESIGNED | FPF_ROMFONT;
  1855.         TextAttr . tta_Tags    = NULL;
  1856.  
  1857.         if(!(TextFont = OpenFont(&TextAttr)))
  1858.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_TEXT_TXT));
  1859.     }
  1860.  
  1861.     TextFontHeight    = TextFont -> tf_YSize;
  1862.     TextFontWidth    = TextFont -> tf_XSize;
  1863.     TextFontBase    = TextFont -> tf_Baseline;
  1864.  
  1865.         /* Determine extra font box width for slanted/boldface glyphs. */
  1866.  
  1867.     FontRightExtend    = MAX(TextFont -> tf_XSize / 2,TextFont -> tf_BoldSmear);
  1868.  
  1869.     CurrentFont = TextFont;
  1870.  
  1871.     GFXFont . ta_YSize = Config -> ScreenConfig -> FontHeight;
  1872.  
  1873.     if(GFX = (struct TextFont *)OpenDiskFont(&GFXFont))
  1874.     {
  1875.         if(GFX -> tf_XSize != TextFont -> tf_XSize || GFX -> tf_YSize != TextFont -> tf_YSize)
  1876.         {
  1877.             CloseFont(GFX);
  1878.  
  1879.             GFX = NULL;
  1880.         }
  1881.     }
  1882.  
  1883.     UserFontHeight    = UserTextFont -> tf_YSize;
  1884.     UserFontWidth    = UserTextFont -> tf_XSize;
  1885.     UserFontBase    = UserTextFont -> tf_Baseline;
  1886.  
  1887.     if(Config -> ScreenConfig -> UseWorkbench || Config -> ScreenConfig -> ShareScreen)
  1888.     {
  1889.         struct TagItem     SomeTags[7];
  1890.         LONG         FullWidth,
  1891.                  Height,Width,
  1892.                  Index = 0;
  1893.         struct Screen    *LocalScreen = DefaultPubScreen;
  1894.  
  1895.         if(Config -> ScreenConfig -> ShareScreen && !Config -> ScreenConfig -> UseWorkbench)
  1896.         {
  1897.             struct DimensionInfo DimensionInfo;
  1898.  
  1899.             if(ModeNotAvailable(Config -> ScreenConfig -> DisplayMode))
  1900.                 Config -> ScreenConfig -> DisplayMode = GetVPModeID(&DefaultPubScreen -> ViewPort);
  1901.  
  1902.             if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config -> ScreenConfig -> DisplayMode))
  1903.             {
  1904.                 LONG    Depth,ScreenWidth,ScreenHeight;
  1905.                 UWORD    Pens = (UWORD)~0;
  1906.  
  1907.                 if(Config -> ScreenConfig -> DisplayWidth && Config -> ScreenConfig -> DisplayHeight)
  1908.                 {
  1909.                     ScreenWidth    = Config -> ScreenConfig -> DisplayWidth;
  1910.                     ScreenHeight    = Config -> ScreenConfig -> DisplayHeight;
  1911.                 }
  1912.                 else
  1913.                 {
  1914.                     ScreenWidth    = 0;
  1915.                     ScreenHeight    = 0;
  1916.                 }
  1917.  
  1918.                 switch(Config -> ScreenConfig -> ColourMode)
  1919.                 {
  1920.                     case COLOUR_EIGHT:
  1921.  
  1922.                         Depth = 3;
  1923.                         break;
  1924.  
  1925.                     case COLOUR_SIXTEEN:
  1926.  
  1927.                         Depth = 4;
  1928.                         break;
  1929.  
  1930.                     case COLOUR_AMIGA:
  1931.  
  1932.                         Depth = 2;
  1933.                         break;
  1934.  
  1935.                     default:
  1936.  
  1937.                         Depth = 1;
  1938.                         break;
  1939.                 }
  1940.  
  1941.                 if(Depth > DimensionInfo . MaxDepth)
  1942.                     Depth = DimensionInfo . MaxDepth;
  1943.  
  1944.                 if(!Kick30 && Depth > 2)
  1945.                     Depth = 2;
  1946.  
  1947.                 if(Config -> ScreenConfig -> Depth && Config -> ScreenConfig -> Depth <= DimensionInfo . MaxDepth)
  1948.                     Depth = Config -> ScreenConfig -> Depth;
  1949.  
  1950. #ifdef _M68030
  1951.                 SPrintf(ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,"'030 ",TermDate,TermIDString);
  1952. #else
  1953.                 SPrintf(ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,"",TermDate,TermIDString);
  1954. #endif    /* _M68030 */
  1955.                 if(SharedScreen = OpenScreenTags(NULL,
  1956.                     ScreenWidth  ? SA_Width  : TAG_IGNORE,    ScreenWidth,
  1957.                     ScreenHeight ? SA_Height : TAG_IGNORE,    ScreenHeight,
  1958.  
  1959.                     SA_Title,    ScreenTitle,
  1960.                     SA_Depth,    Depth,
  1961.                     SA_Pens,    &Pens,
  1962.                     SA_Overscan,    Config -> ScreenConfig -> OverscanType,
  1963.                     SA_DisplayID,    Config -> ScreenConfig -> DisplayMode,
  1964.                     SA_Font,    &UserFont,
  1965.                     SA_AutoScroll,    TRUE,
  1966.                     SA_ShowTitle,    Config -> ScreenConfig -> TitleBar,
  1967.                     SA_PubName,    TermIDString,
  1968.                     SA_Interleaved,    Config -> ScreenConfig -> FasterLayout && Kick30 && Depth > 1,
  1969.                     SA_SharePens,    TRUE,
  1970.                 TAG_DONE))
  1971.                 {
  1972.                     LocalScreen = SharedScreen;
  1973.  
  1974.                     if(DefaultPubScreen)
  1975.                     {
  1976.                         UnlockPubScreen(NULL,DefaultPubScreen);
  1977.  
  1978.                         DefaultPubScreen = NULL;
  1979.                     }
  1980.  
  1981.                     if(Config -> ScreenConfig -> MakeScreenPublic)
  1982.                         PubScreenStatus(LocalScreen,NULL);
  1983.                     else
  1984.                         PubScreenStatus(LocalScreen,PSNF_PRIVATE);
  1985.                 }
  1986.             }
  1987.         }
  1988.  
  1989.         if(!SharedScreen)
  1990.         {
  1991. #ifdef _M68030
  1992.             SPrintf(ScreenTitle,"%s '030 (%s)",TermName,TermDate);
  1993. #else
  1994.             SPrintf(ScreenTitle,"%s (%s)",TermName,TermDate);
  1995. #endif    /* _M68030 */
  1996.  
  1997.         }
  1998.  
  1999.         StatusSizeSetup(LocalScreen,&StatusWidth,&StatusHeight);
  2000.  
  2001.         if(LocalScreen -> RastPort . BitMap -> Depth == 1)
  2002.             UseMasking = FALSE;
  2003.         else
  2004.         {
  2005.             if(Kick30)
  2006.             {
  2007.                 if(GetBitMapAttr(LocalScreen -> RastPort . BitMap,BMA_FLAGS) & BMF_INTERLEAVED)
  2008.                     UseMasking = FALSE;
  2009.                 else
  2010.                     UseMasking = TRUE;
  2011.             }
  2012.             else
  2013.                 UseMasking = TRUE;
  2014.         }
  2015.  
  2016.         VPort = &LocalScreen -> ViewPort;
  2017.  
  2018.             /* Get the current display dimensions. */
  2019.  
  2020.         if(VPort -> ColorMap -> cm_vpe)
  2021.         {
  2022.             struct ViewPortExtra *Extra;
  2023.  
  2024.             Extra = VPort -> ColorMap -> cm_vpe;
  2025.  
  2026.             ScreenWidth    = Extra -> DisplayClip . MaxX - Extra -> DisplayClip . MinX + 1;
  2027.             ScreenHeight    = Extra -> DisplayClip . MaxY - Extra -> DisplayClip . MinY + 1;
  2028.         }
  2029.         else
  2030.         {
  2031.             struct ViewPortExtra *Extra;
  2032.  
  2033.             if(Extra = (struct ViewPortExtra *)GfxLookUp(VPort))
  2034.             {
  2035.                 ScreenWidth    = Extra -> DisplayClip . MaxX - Extra -> DisplayClip . MinX + 1;
  2036.                 ScreenHeight    = Extra -> DisplayClip . MaxY - Extra -> DisplayClip . MinY + 1;
  2037.             }
  2038.             else
  2039.             {
  2040.                 ScreenWidth    = LocalScreen -> Width;
  2041.                 ScreenHeight    = LocalScreen -> Height;
  2042.             }
  2043.         }
  2044.  
  2045.         DepthMask = (1L << LocalScreen -> RastPort . BitMap -> Depth) - 1;
  2046.  
  2047.         switch(Config -> ScreenConfig -> ColourMode)
  2048.         {
  2049.             case COLOUR_SIXTEEN:
  2050.  
  2051.                 if(DepthMask < 15)
  2052.                 {
  2053.                     if(DepthMask >= 7)
  2054.                         Config -> ScreenConfig -> ColourMode = COLOUR_EIGHT;
  2055.                     else
  2056.                     {
  2057.                         if(DepthMask >= 3)
  2058.                             Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  2059.                         else
  2060.                             Config -> ScreenConfig -> ColourMode = COLOUR_MONO;
  2061.                     }
  2062.                 }
  2063.  
  2064.                 break;
  2065.  
  2066.             case COLOUR_EIGHT:
  2067.  
  2068.                 if(DepthMask < 7)
  2069.                 {
  2070.                     if(DepthMask >= 3)
  2071.                         Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  2072.                     else
  2073.                         Config -> ScreenConfig -> ColourMode = COLOUR_MONO;
  2074.                 }
  2075.  
  2076.                 break;
  2077.  
  2078.             case COLOUR_AMIGA:
  2079.  
  2080.                 if(DepthMask < 3)
  2081.                     Config -> ScreenConfig -> ColourMode = COLOUR_MONO;
  2082.  
  2083.                 break;
  2084.         }
  2085.  
  2086.         if(!(DrawInfo = GetScreenDrawInfo(LocalScreen)))
  2087.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_SCREEN_DRAWINFO_TXT));
  2088.  
  2089.         CreateMenuGlyphs(LocalScreen,DrawInfo,&AmigaGlyph,&CheckGlyph);
  2090.  
  2091.         SZ_SizeSetup(LocalScreen,&UserFont);
  2092.  
  2093.             /* Obtain visual info (whatever that may be). */
  2094.  
  2095.         if(!(VisualInfo = GetVisualInfo(LocalScreen,TAG_DONE)))
  2096.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  2097.  
  2098.         if(Config -> ScreenConfig -> StatusLine != STATUSLINE_DISABLED && !Config -> ScreenConfig -> SplitStatus)
  2099.         {
  2100.             if(!(StatusGadget = (struct Gadget *)CreateStatusGadget(LocalScreen -> Width,42)))
  2101.                 return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_STATUS_GADGET_TXT));
  2102.         }
  2103.  
  2104.         if(StatusGadget)
  2105.             GetAttr(SGA_FullWidth,StatusGadget,(ULONG *)&FullWidth);
  2106.         else
  2107.             FullWidth = 0;
  2108.  
  2109.         if(StatusGadget)
  2110.         {
  2111.             SomeTags[Index  ] . ti_Tag    = WA_Gadgets;
  2112.             SomeTags[Index++] . ti_Data    = (ULONG)StatusGadget;
  2113.         }
  2114.  
  2115.         if(WindowBox . Left != -1)
  2116.         {
  2117.             SomeTags[Index  ] . ti_Tag    = WA_Left;
  2118.             SomeTags[Index++] . ti_Data    = WindowBox . Left;
  2119.             SomeTags[Index  ] . ti_Tag    = WA_Top;
  2120.             SomeTags[Index++] . ti_Data    = WindowBox . Top;
  2121.  
  2122.             SomeTags[Index  ] . ti_Tag    = WA_Width;
  2123.             SomeTags[Index++] . ti_Data    = WindowBox . Width;
  2124.             SomeTags[Index  ] . ti_Tag    = WA_Height;
  2125.             SomeTags[Index++] . ti_Data    = WindowBox . Height;
  2126.  
  2127.             WindowBox . Left = -1;
  2128.         }
  2129.         else
  2130.         {
  2131.             if(Config -> TerminalConfig -> NumColumns < 20)
  2132.             {
  2133.                 LONG Width = GetScreenWidth(NULL);
  2134.  
  2135.                 if(FullWidth && Width < FullWidth)
  2136.                 {
  2137.                     SomeTags[Index  ] . ti_Tag    = WA_InnerWidth;
  2138.                     SomeTags[Index++] . ti_Data    = FullWidth;
  2139.                 }
  2140.                 else
  2141.                 {
  2142.                     SomeTags[Index  ] . ti_Tag    = WA_Width;
  2143.                     SomeTags[Index++] . ti_Data    = Width;
  2144.                 }
  2145.             }
  2146.             else
  2147.             {
  2148.                 SomeTags[Index  ] . ti_Tag    = WA_InnerWidth;
  2149.                 SomeTags[Index++] . ti_Data    = Config -> TerminalConfig -> NumColumns * TextFontWidth;
  2150.             }
  2151.  
  2152.             if(Config -> TerminalConfig -> NumLines < 20)
  2153.             {
  2154.                 SomeTags[Index  ] . ti_Tag    = WA_Height;
  2155.                 SomeTags[Index++] . ti_Data    = GetScreenHeight(NULL) - (LocalScreen -> BarHeight + 1);
  2156.             }
  2157.             else
  2158.             {
  2159.                 SomeTags[Index  ] . ti_Tag    = WA_InnerHeight;
  2160.                 SomeTags[Index++] . ti_Data    = Config -> TerminalConfig -> NumLines * TextFontHeight;
  2161.             }
  2162.  
  2163.             SomeTags[Index  ] . ti_Tag    = WA_Left;
  2164.             SomeTags[Index++] . ti_Data    = GetScreenLeft(NULL);
  2165.  
  2166.             SomeTags[Index  ] . ti_Tag    = WA_Top;
  2167.             SomeTags[Index++] . ti_Data    = GetScreenTop(NULL) + LocalScreen -> BarHeight + 1;
  2168.         }
  2169.  
  2170.         SomeTags[Index] . ti_Tag = TAG_DONE;
  2171.  
  2172.             /* Open the main window. */
  2173.  
  2174.         if(!(Window = OpenWindowTags(NULL,
  2175.             WA_MaxHeight,        LocalScreen -> Height,
  2176.             WA_MaxWidth,        LocalScreen -> Width,
  2177.             WA_SmartRefresh,    TRUE,
  2178.             WA_CustomScreen,    LocalScreen,
  2179.             WA_NewLookMenus,    TRUE,
  2180.             WA_RMBTrap,        TRUE,
  2181.             WA_IDCMP,        IDCMP_RAWKEY | IDCMP_INACTIVEWINDOW | IDCMP_ACTIVEWINDOW | IDCMP_MOUSEMOVE | IDCMP_GADGETUP | IDCMP_GADGETDOWN | IDCMP_MENUPICK | IDCMP_MOUSEMOVE | IDCMP_MOUSEBUTTONS | IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE | IDCMP_IDCMPUPDATE | IDCMP_MENUHELP,
  2182.             WA_DragBar,        TRUE,
  2183.             WA_DepthGadget,        TRUE,
  2184.             WA_CloseGadget,        TRUE,
  2185.             WA_SizeGadget,        TRUE,
  2186.             WA_SizeBBottom,        TRUE,
  2187.             WA_NoCareRefresh,    TRUE,
  2188.             WA_Title,        ScreenTitle,
  2189.             WA_MenuHelp,        TRUE,
  2190.  
  2191.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  2192.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  2193.  
  2194.             TAG_MORE,        SomeTags,
  2195.         TAG_DONE)))
  2196.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2197.  
  2198.             /* Create a user clip region to keep text from
  2199.              * leaking into the window borders.
  2200.              */
  2201.  
  2202.         if(!(ClipRegion = NewRegion()))
  2203.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2204.         else
  2205.         {
  2206.             struct Rectangle RegionRectangle;
  2207.  
  2208.                 /* Adjust the region to match the inner window area. */
  2209.  
  2210.             RegionRectangle . MinX = Window -> BorderLeft;
  2211.             RegionRectangle . MinY = Window -> BorderTop;
  2212.             RegionRectangle . MaxX = Window -> Width - (Window -> BorderRight + 1);
  2213.             RegionRectangle . MaxY = Window -> Height - (Window -> BorderBottom + 1);
  2214.  
  2215.                 /* Establish the region. */
  2216.  
  2217.             OrRectRegion(ClipRegion,&RegionRectangle);
  2218.  
  2219.                 /* Install the region. */
  2220.  
  2221.             OldRegion = InstallClipRegion(Window -> WLayer,ClipRegion);
  2222.         }
  2223.  
  2224.         if(FullWidth < 40 * TextFontWidth)
  2225.             FullWidth = 40 * TextFontWidth;
  2226.  
  2227.         Width    = Window -> BorderLeft + FullWidth + Window -> BorderRight;
  2228.         Height    = Window -> BorderTop + 20 * TextFontHeight + Window -> BorderBottom;
  2229.  
  2230.         WindowLimits(Window,Width,Height,0,0);
  2231.  
  2232.         if(WorkbenchBase)
  2233.         {
  2234.             if(WorkbenchPort = CreateMsgPort())
  2235.             {
  2236.                 if(!(WorkbenchWindow = AddAppWindow(0,0,Window,WorkbenchPort,TAG_DONE)))
  2237.                 {
  2238.                     DeleteMsgPort(WorkbenchPort);
  2239.  
  2240.                     WorkbenchPort = NULL;
  2241.                 }
  2242.             }
  2243.         }
  2244.     }
  2245.     else
  2246.     {
  2247.         struct DimensionInfo    DimensionInfo;
  2248.         WORD            MaxDepth,
  2249.                     ScreenDepth;
  2250.  
  2251.         if(ModeNotAvailable(Config -> ScreenConfig -> DisplayMode))
  2252.         {
  2253.             struct Screen *PubScreen;
  2254.  
  2255.             if(PubScreen = LockPubScreen(NULL))
  2256.             {
  2257.                 Config -> ScreenConfig -> DisplayMode = GetVPModeID(&PubScreen -> ViewPort);
  2258.  
  2259.                 UnlockPubScreen(NULL,PubScreen);
  2260.             }
  2261.         }
  2262.  
  2263.         if(!QueryOverscan(Config -> ScreenConfig -> DisplayMode,&DisplayClip,Config -> ScreenConfig -> OverscanType))
  2264.         {
  2265.             OpenFailed = TRUE;
  2266.  
  2267.             ErrorCode = ModeNotAvailable(Config -> ScreenConfig -> DisplayMode);
  2268.  
  2269.             goto OpenS;
  2270.         }
  2271.  
  2272.         if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config -> ScreenConfig -> DisplayMode))
  2273.         {
  2274.             UWORD    MaxWidth,
  2275.                 MaxHeight,
  2276.                 Width,
  2277.                 Height;
  2278.  
  2279.             MaxWidth    = DisplayClip . MaxX - DisplayClip . MinX + 1;
  2280.             MaxHeight    = DisplayClip . MaxY - DisplayClip . MinY + 1;
  2281.  
  2282.             if(Config -> ScreenConfig -> DisplayWidth && Config -> ScreenConfig -> DisplayHeight)
  2283.             {
  2284.                 ScreenWidth    = Config -> ScreenConfig -> DisplayWidth;
  2285.                 ScreenHeight    = Config -> ScreenConfig -> DisplayHeight;
  2286.             }
  2287.             else
  2288.             {
  2289.                 ScreenWidth    = MaxWidth;
  2290.                 ScreenHeight    = MaxHeight;
  2291.             }
  2292.  
  2293.             if(Config -> TerminalConfig -> NumColumns < 20)
  2294.                 Width = MaxWidth = ScreenWidth;
  2295.             else
  2296.             {
  2297.                 Width = TextFontWidth * Config -> TerminalConfig -> NumColumns;
  2298.  
  2299.                 ScreenWidth = 0;
  2300.             }
  2301.  
  2302.             if(Config -> TerminalConfig -> NumLines < 20)
  2303.                 Height = MaxHeight = ScreenHeight;
  2304.             else
  2305.             {
  2306.                 Height = TextFontHeight * Config -> TerminalConfig -> NumLines;
  2307.  
  2308.                 if(Config -> ScreenConfig -> TitleBar)
  2309.                     Height += UserFontHeight + 3;
  2310.  
  2311.                 if(Config -> ScreenConfig -> StatusLine != STATUSLINE_DISABLED)
  2312.                 {
  2313.                     if(Config -> ScreenConfig -> StatusLine == STATUSLINE_COMPRESSED)
  2314.                         Height += UserFontHeight;
  2315.                     else
  2316.                         Height += 2 + (2 + 2 * UserFontHeight + 2);
  2317.                 }
  2318.  
  2319.                 ScreenHeight = 0;
  2320.             }
  2321.  
  2322.             if(Height > MaxHeight)
  2323.                 Height = MaxHeight;
  2324.  
  2325.             if(Width > MaxWidth)
  2326.                 Width = MaxWidth;
  2327.  
  2328.             if(DimensionInfo . MinRasterWidth <= Width && Width <= DimensionInfo . MaxRasterWidth)
  2329.             {
  2330.                 UWORD Half;
  2331.  
  2332.                 Width = MaxWidth - Width;
  2333.  
  2334.                 Half = Width / 2;
  2335.  
  2336.                 DisplayClip . MinX += Half;
  2337.                 DisplayClip . MaxX -= Width - Half;
  2338.             }
  2339.  
  2340.             if(DimensionInfo . MinRasterHeight <= Height && Height <= DimensionInfo . MaxRasterHeight)
  2341.                 DisplayClip . MaxY = DisplayClip . MinY + Height - 1;
  2342.  
  2343.             if(!ScreenWidth)
  2344.                 ScreenWidth = DisplayClip . MaxX - DisplayClip . MinX + 1;
  2345.  
  2346.             if(!ScreenHeight)
  2347.                 ScreenHeight = DisplayClip . MaxY - DisplayClip . MinY + 1;
  2348.  
  2349.             MaxDepth = DimensionInfo . MaxDepth;
  2350.         }
  2351.         else
  2352.         {
  2353.             ScreenWidth = ScreenHeight = 0;
  2354.             MaxDepth = 4;
  2355.         }
  2356.  
  2357.             /* We'll configure the screen parameters at
  2358.              * run time, at first we'll set up the screen
  2359.              * depth.
  2360.              */
  2361.  
  2362. PenReset:    if(!Config -> ScreenConfig -> UsePens && Kick30)
  2363.         {
  2364.             for(i = DETAILPEN ; i <= BARTRIMPEN ; i++)
  2365.                 PenArray[i] = Config -> ScreenConfig -> PenArray[i];
  2366.  
  2367.             PenArray[i] = (UWORD)~0;
  2368.         }
  2369.         else
  2370.         {
  2371.             UWORD *Data;
  2372.  
  2373.             switch(Config -> ScreenConfig -> ColourMode)
  2374.             {
  2375.                 case COLOUR_EIGHT:
  2376.  
  2377.                     Data = ANSIPens;
  2378.                     break;
  2379.  
  2380.                 case COLOUR_SIXTEEN:
  2381.  
  2382.                     Data = EGAPens;
  2383.                     break;
  2384.  
  2385.                 case COLOUR_AMIGA:
  2386.  
  2387.                     Data = StandardPens;
  2388.                     break;
  2389.  
  2390.                 default:
  2391.  
  2392.                     Data = NULL;
  2393.                     break;
  2394.             }
  2395.  
  2396.             if(Data)
  2397.             {
  2398.                 for(i = DETAILPEN ; i <= BARTRIMPEN ; i++)
  2399.                     PenArray[i] = Data[i];
  2400.  
  2401.                 PenArray[i] = (UWORD)~0;
  2402.             }
  2403.         }
  2404.  
  2405.         switch(Config -> ScreenConfig -> ColourMode)
  2406.         {
  2407.             case COLOUR_EIGHT:
  2408.  
  2409.                     // Special screen depth requested?
  2410.  
  2411.                 if(Config -> ScreenConfig -> Depth)
  2412.                 {
  2413.                         // The minimum number of colours required
  2414.  
  2415.                     if(Config -> ScreenConfig -> Blinking)
  2416.                         ScreenDepth = 4;
  2417.                     else
  2418.                         ScreenDepth = 3;
  2419.  
  2420.                         // This is what the user wanted
  2421.  
  2422.                     RealDepth = Config -> ScreenConfig -> Depth;
  2423.  
  2424.                         // Too deep for this display mode?
  2425.  
  2426.                     if(RealDepth > MaxDepth)
  2427.                         RealDepth = MaxDepth;
  2428.  
  2429.                         // Less colours than required?
  2430.  
  2431.                     if(RealDepth < ScreenDepth && ScreenDepth <= MaxDepth)
  2432.                         RealDepth = ScreenDepth;
  2433.  
  2434.                         // Not enough colours to display it?
  2435.  
  2436.                     if(RealDepth < ScreenDepth)
  2437.                     {
  2438.                             // Return to standard mode
  2439.  
  2440.                         Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  2441.  
  2442.                         ConfigChanged = TRUE;
  2443.  
  2444.                         goto PenReset;
  2445.                     }
  2446.                 }
  2447.                 else
  2448.                 {
  2449.                         // The minimum number of colours
  2450.  
  2451.                     if(Config -> ScreenConfig -> Blinking)
  2452.                         ScreenDepth = 4;
  2453.                     else
  2454.                         ScreenDepth = 3;
  2455.  
  2456.                         // Too many for this mode?
  2457.  
  2458.                     if(ScreenDepth > MaxDepth)
  2459.                     {
  2460.                             // Return to standard mode
  2461.  
  2462.                         Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  2463.  
  2464.                         ConfigChanged = TRUE;
  2465.  
  2466.                         goto PenReset;
  2467.                     }
  2468.  
  2469.                     RealDepth = ScreenDepth;
  2470.                 }
  2471.  
  2472.                 TagArray[Count++] = SA_Pens;
  2473.                 TagArray[Count++] = (LONG)PenArray;
  2474.  
  2475.                 TagArray[Count++] = SA_BlockPen;
  2476.                 TagArray[Count++] = PenArray[SHADOWPEN];
  2477.  
  2478.                 TagArray[Count++] = SA_DetailPen;
  2479.                 TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2480.  
  2481.                 break;
  2482.  
  2483.             case COLOUR_SIXTEEN:
  2484.  
  2485.                 if(Config -> ScreenConfig -> Depth)
  2486.                 {
  2487.                     if(Config -> ScreenConfig -> Blinking && MaxDepth > 4)
  2488.                         ScreenDepth = 5;
  2489.                     else
  2490.                         ScreenDepth = 4;
  2491.  
  2492.                     RealDepth = Config -> ScreenConfig -> Depth;
  2493.  
  2494.                     if(RealDepth > MaxDepth)
  2495.                         RealDepth = MaxDepth;
  2496.  
  2497.                     if(RealDepth < ScreenDepth && ScreenDepth <= MaxDepth)
  2498.                         RealDepth = ScreenDepth;
  2499.  
  2500.                     if(RealDepth < ScreenDepth)
  2501.                     {
  2502.                         Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  2503.  
  2504.                         ConfigChanged = TRUE;
  2505.  
  2506.                         goto PenReset;
  2507.                     }
  2508.                 }
  2509.                 else
  2510.                 {
  2511.                     if(Config -> ScreenConfig -> Blinking && MaxDepth > 4)
  2512.                         ScreenDepth = 5;
  2513.                     else
  2514.                         ScreenDepth = 4;
  2515.  
  2516.                     if(ScreenDepth > MaxDepth)
  2517.                     {
  2518.                         Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  2519.  
  2520.                         ConfigChanged = TRUE;
  2521.  
  2522.                         goto PenReset;
  2523.                     }
  2524.  
  2525.                     RealDepth = ScreenDepth;
  2526.                 }
  2527.  
  2528.                 TagArray[Count++] = SA_Pens;
  2529.                 TagArray[Count++] = (LONG)PenArray;
  2530.  
  2531.                 TagArray[Count++] = SA_BlockPen;
  2532.                 TagArray[Count++] = PenArray[SHADOWPEN];
  2533.  
  2534.                 TagArray[Count++] = SA_DetailPen;
  2535.                 TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2536.  
  2537.                 break;
  2538.  
  2539.             case COLOUR_MONO:
  2540.  
  2541.                 if(Config -> ScreenConfig -> Depth)
  2542.                     RealDepth = Config -> ScreenConfig -> Depth;
  2543.                 else
  2544.                     RealDepth = 1;
  2545.  
  2546.                 if(RealDepth > MaxDepth)
  2547.                     RealDepth = MaxDepth;
  2548.  
  2549.                 break;
  2550.  
  2551.             case COLOUR_AMIGA:
  2552.  
  2553.                 if(Config -> ScreenConfig -> Depth)
  2554.                     RealDepth = Config -> ScreenConfig -> Depth;
  2555.                 else
  2556.                     RealDepth = 2;
  2557.  
  2558.                 if(RealDepth > MaxDepth)
  2559.                     RealDepth = MaxDepth;
  2560.  
  2561.                 TagArray[Count++] = SA_Pens;
  2562.                 TagArray[Count++] = (LONG)PenArray;
  2563.  
  2564.                 break;
  2565.         }
  2566.  
  2567.             /* Add the depth value. */
  2568.  
  2569.         TagArray[Count++] = SA_Depth;
  2570.         TagArray[Count++] = RealDepth;
  2571.  
  2572.             /* Terminate the tag array. */
  2573.  
  2574.         TagArray[Count] = TAG_END;
  2575.  
  2576.             /* Set the plane mask. */
  2577.  
  2578.         DepthMask = (1L << RealDepth) - 1;
  2579.  
  2580.             /* Inquire overscan limits and try to create an interleaved
  2581.              * bitmap if possible.
  2582.              */
  2583.  
  2584.         if(Config -> ScreenConfig -> FasterLayout && RealDepth > 1 && !Kick30)
  2585.             InterleavedBitMap = CreateInterleavedBitMap(DisplayClip . MaxX - DisplayClip . MinX + 1,DisplayClip . MaxY - DisplayClip . MinY + 1,RealDepth);
  2586.         else
  2587.             InterleavedBitMap = NULL;
  2588.  
  2589. #ifdef _M68030
  2590. OpenS:        SPrintf(ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,"'030 ",TermDate,TermIDString);
  2591. #else
  2592. OpenS:        SPrintf(ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,"",TermDate,TermIDString);
  2593. #endif    /* _M68030 */
  2594.  
  2595.         if(InterleavedBitMap)
  2596.         {
  2597.             if(Screen = (struct Screen *)OpenScreenTags(NULL,
  2598.                 ScreenWidth  ? SA_Width  : TAG_IGNORE,    ScreenWidth,
  2599.                 ScreenHeight ? SA_Height : TAG_IGNORE,    ScreenHeight,
  2600.  
  2601.                 SA_Title,    ScreenTitle,
  2602.                 SA_DClip,    &DisplayClip,
  2603.                 SA_BitMap,    InterleavedBitMap,
  2604.                 SA_DisplayID,    Config -> ScreenConfig -> DisplayMode,
  2605.                 SA_Font,    &UserFont,
  2606.                 SA_Behind,    TRUE,
  2607.                 SA_AutoScroll,    TRUE,
  2608.                 SA_ShowTitle,    Config -> ScreenConfig -> TitleBar,
  2609.                 SA_PubName,    TermIDString,
  2610.                 SA_ErrorCode,    &ErrorCode,
  2611.                 SA_BackFill,    &BackfillHook,
  2612.                 TAG_MORE,    TagArray,
  2613.             TAG_END))
  2614.                 UseMasking = FALSE;
  2615.         }
  2616.         else
  2617.         {
  2618.             BYTE Interleaved;
  2619.  
  2620.             if(Config -> ScreenConfig -> FasterLayout && Kick30 && RealDepth > 1)
  2621.                 Interleaved = TRUE;
  2622.             else
  2623.                 Interleaved = FALSE;
  2624.  
  2625.             if(Screen = (struct Screen *)OpenScreenTags(NULL,
  2626.                 ScreenWidth  ? SA_Width  : TAG_IGNORE,    ScreenWidth,
  2627.                 ScreenHeight ? SA_Height : TAG_IGNORE,    ScreenHeight,
  2628.  
  2629.                 SA_Title,    ScreenTitle,
  2630.                 SA_DClip,    &DisplayClip,
  2631.                 SA_DisplayID,    Config -> ScreenConfig -> DisplayMode,
  2632.                 SA_Font,    &UserFont,
  2633.                 SA_Behind,    TRUE,
  2634.                 SA_AutoScroll,    TRUE,
  2635.                 SA_ShowTitle,    Config -> ScreenConfig -> TitleBar,
  2636.                 SA_PubName,    TermIDString,
  2637.                 SA_ErrorCode,    &ErrorCode,
  2638.                 SA_Interleaved,    Interleaved,
  2639.                 SA_BackFill,    &BackfillHook,
  2640.                 TAG_MORE,    TagArray,
  2641.             TAG_END))
  2642.             {
  2643.                 if(Interleaved)
  2644.                     UseMasking = FALSE;
  2645.                 else
  2646.                     UseMasking = TRUE;
  2647.             }
  2648.         }
  2649.  
  2650.             /* We've got an error. */
  2651.  
  2652.         if(!Screen)
  2653.         {
  2654.             if(!OpenFailed)
  2655.             {
  2656.                 struct Screen *PubScreen;
  2657.  
  2658.                 switch(ErrorCode)
  2659.                 {
  2660.                         /* Can't open screen with these display
  2661.                          * modes.
  2662.                          */
  2663.  
  2664.                     case OSERR_NOMONITOR:
  2665.                     case OSERR_NOCHIPS:
  2666.                     case OSERR_UNKNOWNMODE:
  2667.                     case OSERR_NOTAVAILABLE:
  2668.  
  2669.                         if(PubScreen = LockPubScreen(NULL))
  2670.                         {
  2671.                             Config -> ScreenConfig -> DisplayMode = GetVPModeID(&PubScreen -> ViewPort);
  2672.  
  2673.                             UnlockPubScreen(NULL,PubScreen);
  2674.                         }
  2675.                         else
  2676.                             Config -> ScreenConfig -> DisplayMode = DEFAULT_MONITOR_ID | HIRESLACE_KEY;
  2677.  
  2678.                         OpenFailed = TRUE;
  2679.  
  2680.                         goto OpenS;
  2681.  
  2682.                     case OSERR_PUBNOTUNIQUE:
  2683.  
  2684.                         return(LocaleString(MSG_TERMINIT_SCREEN_ID_ALREADY_IN_USE_TXT));
  2685.                 }
  2686.             }
  2687.  
  2688.                 /* Some different error, probably out of
  2689.                  * memory.
  2690.                  */
  2691.  
  2692.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  2693.         }
  2694.  
  2695.         if(!(DrawInfo = GetScreenDrawInfo(Screen)))
  2696.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_SCREEN_DRAWINFO_TXT));
  2697.  
  2698.         CreateMenuGlyphs(Screen,DrawInfo,&AmigaGlyph,&CheckGlyph);
  2699.  
  2700.         VPort = &Screen -> ViewPort;
  2701.  
  2702.         ScreenWidth    = Screen -> Width;
  2703.         ScreenHeight    = Screen -> Height;
  2704.  
  2705.         StatusSizeSetup(Screen,&StatusWidth,&StatusHeight);
  2706.  
  2707.             /* Obtain visual info (whatever that may be). */
  2708.  
  2709.         if(!(VisualInfo = GetVisualInfo(Screen,TAG_DONE)))
  2710.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  2711.  
  2712.         if(Config -> ScreenConfig -> TitleBar)
  2713.         {
  2714.             Top = Screen -> BarHeight + 1;
  2715.  
  2716.             Height = Screen -> Height - (Screen -> BarHeight + 1 + StatusHeight);
  2717.         }
  2718.         else
  2719.         {
  2720.             Top = 0;
  2721.  
  2722.             Height = Screen -> Height - StatusHeight;
  2723.         }
  2724.  
  2725.             /* Open the main window. */
  2726.  
  2727.         if(!(Window = OpenWindowTags(NULL,
  2728.             WA_Top,        Top,
  2729.             WA_Left,    0,
  2730.             WA_Width,    Screen -> Width,
  2731.             WA_Height,    Height,
  2732.             WA_Backdrop,    TRUE,
  2733.             WA_Borderless,    TRUE,
  2734.             WA_SmartRefresh,TRUE,
  2735.             WA_CustomScreen,Screen,
  2736.             WA_NewLookMenus,TRUE,
  2737.             WA_RMBTrap,    TRUE,
  2738.             WA_IDCMP,    IDCMP_RAWKEY | IDCMP_INACTIVEWINDOW | IDCMP_ACTIVEWINDOW | IDCMP_MOUSEMOVE | IDCMP_GADGETUP | IDCMP_GADGETDOWN | IDCMP_MENUPICK | IDCMP_MOUSEMOVE | IDCMP_MOUSEBUTTONS | IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE | IDCMP_SIZEVERIFY | IDCMP_IDCMPUPDATE | IDCMP_MENUHELP,
  2739.             WA_MenuHelp,    TRUE,
  2740.  
  2741.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  2742.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  2743.         TAG_DONE)))
  2744.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2745.     }
  2746.  
  2747.         /* Fill the `default' colour with current values. */
  2748.  
  2749.     if(!Config -> ScreenConfig -> UseWorkbench && Initializing && !SharedScreen)
  2750.     {
  2751.         for(i = 0 ; i < 16 ; i++)
  2752.             DefaultColours[i] = GetRGB4(VPort -> ColorMap,i);
  2753.  
  2754.         Initializing = FALSE;
  2755.     }
  2756.  
  2757.         /* Load the approriate colours. */
  2758.  
  2759.     if(LoadColours)
  2760.     {
  2761.         switch(Config -> ScreenConfig -> ColourMode)
  2762.         {
  2763.             case COLOUR_EIGHT:
  2764.  
  2765.                 CopyMem(ANSIColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  2766.                 break;
  2767.  
  2768.             case COLOUR_SIXTEEN:
  2769.  
  2770.                 CopyMem(EGAColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  2771.                 break;
  2772.  
  2773.             case COLOUR_AMIGA:
  2774.  
  2775.                 CopyMem(DefaultColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  2776.                 break;
  2777.  
  2778.             case COLOUR_MONO:
  2779.  
  2780.                 CopyMem(AtomicColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  2781.                 break;
  2782.         }
  2783.  
  2784.         LoadColours = FALSE;
  2785.     }
  2786.  
  2787.         /* Reset the current colours and the blinking equivalents. */
  2788.  
  2789.     PaletteSetup(Config);
  2790.  
  2791.     if(!Config -> ScreenConfig -> UseWorkbench && !SharedScreen)
  2792.         LoadRGB4(VPort,NormalColours,PaletteSize);
  2793.  
  2794.         /* Get the vanilla rendering pens. */
  2795.  
  2796.     RenderPens[0] = DrawInfo -> dri_Pens[BACKGROUNDPEN];
  2797.     RenderPens[1] = DrawInfo -> dri_Pens[TEXTPEN];
  2798.     RenderPens[2] = DrawInfo -> dri_Pens[SHINEPEN];
  2799.     RenderPens[3] = DrawInfo -> dri_Pens[FILLPEN];
  2800.  
  2801.         /* Are we to use the Workbench screen for text output? */
  2802.  
  2803.     if(Config -> ScreenConfig -> UseWorkbench || (SharedScreen && Kick30))
  2804.     {
  2805.         if(Kick30 && (Config -> ScreenConfig -> ColourMode == COLOUR_EIGHT || Config -> ScreenConfig -> ColourMode == COLOUR_SIXTEEN))
  2806.         {
  2807.             ULONG    R,G,B;
  2808.             BYTE    GotAll = TRUE;
  2809.             WORD    NumPens;
  2810.  
  2811.             if(Config -> ScreenConfig -> ColourMode == COLOUR_EIGHT)
  2812.                 NumPens = 8;
  2813.             else
  2814.                 NumPens = 16;
  2815.  
  2816.             for(i = 0 ; i < 16 ; i++)
  2817.                 MappedPens[1][i] = FALSE;
  2818.  
  2819.                 /* Allocate the text rendering pens, note that
  2820.                  * we will use the currently installed palette
  2821.                  * to obtain those pens which match them best.
  2822.                  * The user will be unable to change these
  2823.                  * colours.
  2824.                  */
  2825.  
  2826.             for(i = 0 ; i < NumPens ; i++)
  2827.             {
  2828.                     /* Split the 12 bit colour palette entry. */
  2829.  
  2830.                 R = (NormalColours[i] >> 8) & 0xF;
  2831.                 G = (NormalColours[i] >> 4) & 0xF;
  2832.                 B =  NormalColours[i]       & 0xF;
  2833.  
  2834.                     /* Try to obtain a matching pen. */
  2835.  
  2836.                 if((MappedPens[0][i] = ObtainBestPen(VPort -> ColorMap,SPREAD((R << 4) | R),SPREAD((G << 4) | G),SPREAD((B << 4) | B),
  2837.                     OBP_FailIfBad,TRUE,
  2838.                 TAG_DONE)) == -1)
  2839.                 {
  2840.                     MappedPens[1][i] = FALSE;
  2841.  
  2842.                     GotAll = FALSE;
  2843.  
  2844.                     break;
  2845.                 }
  2846.                 else
  2847.                     MappedPens[1][i] = TRUE;
  2848.             }
  2849.  
  2850.                 /* Did we get what we wanted? */
  2851.  
  2852.             if(!GotAll)
  2853.             {
  2854.                     /* Release all the pens we succeeded
  2855.                      * in allocating.
  2856.                      */
  2857.  
  2858.                 for(i = 0 ; i < NumPens ; i++)
  2859.                 {
  2860.                     if(MappedPens[1][i])
  2861.                         ReleasePen(VPort -> ColorMap,MappedPens[0][i]);
  2862.                 }
  2863.  
  2864.                     /* Use the default rendering pens. */
  2865.  
  2866.                 for(i = 0 ; i < 4 ; i++)
  2867.                 {
  2868.                     MappedPens[0][i] = RenderPens[i];
  2869.                     MappedPens[1][i] = FALSE;
  2870.                 }
  2871.  
  2872.                     /* Set the remaining pens to defaults. */
  2873.  
  2874.                 for(i = 4 ; i < NumPens ; i++)
  2875.                 {
  2876.                     MappedPens[0][i] = RenderPens[1];
  2877.                     MappedPens[1][i] = FALSE;
  2878.                 }
  2879.             }
  2880.             else
  2881.                 AllocatedPens = TRUE;
  2882.         }
  2883.         else
  2884.         {
  2885.                 /* Use the default rendering pens. */
  2886.  
  2887.             if(Config -> ScreenConfig -> ColourMode == COLOUR_AMIGA)
  2888.             {
  2889.                 for(i = 0 ; i < 4 ; i++)
  2890.                 {
  2891.                     MappedPens[0][i] = RenderPens[i];
  2892.                     MappedPens[1][i] = FALSE;
  2893.                 }
  2894.  
  2895.                     /* Set the remaining pens to defaults. */
  2896.  
  2897.                 for(i = 4 ; i < 16 ; i++)
  2898.                 {
  2899.                     MappedPens[0][i] = RenderPens[1];
  2900.                     MappedPens[1][i] = FALSE;
  2901.                 }
  2902.             }
  2903.             else
  2904.             {
  2905.                 for(i = 0 ; i < 16 ; i++)
  2906.                 {
  2907.                     MappedPens[0][i] = RenderPens[i & 1];
  2908.                     MappedPens[1][i] = FALSE;
  2909.                 }
  2910.             }
  2911.         }
  2912.  
  2913.         for(i = 0 ; i < 16 ; i++)
  2914.         {
  2915.             MappedPens[0][i + 16] = MappedPens[0][i];
  2916.             MappedPens[1][i + 16] = FALSE;
  2917.         }
  2918.     }
  2919.     else
  2920.     {
  2921.             /* Reset the colour translation table. */
  2922.  
  2923.         for(i = 0 ; i < 32 ; i++)
  2924.         {
  2925.             MappedPens[0][i] = i;
  2926.             MappedPens[1][i] = FALSE;
  2927.         }
  2928.     }
  2929.  
  2930.         /* Determine default text rendering colour. */
  2931.  
  2932.     switch(Config -> ScreenConfig -> ColourMode)
  2933.     {
  2934.         case COLOUR_SIXTEEN:
  2935.  
  2936.             SafeTextPen = MappedPens[0][15];
  2937.             break;
  2938.  
  2939.         case COLOUR_EIGHT:
  2940.  
  2941.             SafeTextPen = MappedPens[0][7];
  2942.             break;
  2943.  
  2944.         case COLOUR_AMIGA:
  2945.         case COLOUR_MONO:
  2946.  
  2947.             SafeTextPen = MappedPens[0][1];
  2948.             break;
  2949.     }
  2950.  
  2951.         /* Determine window inner dimensions and top/left edge offsets. */
  2952.  
  2953.     WindowLeft    = Window -> BorderLeft;
  2954.     WindowTop    = Window -> BorderTop;
  2955.  
  2956.     WindowWidth    = Window -> Width - (Window -> BorderLeft + Window -> BorderRight);
  2957.     WindowHeight    = Window -> Height - (Window -> BorderTop + Window -> BorderBottom);
  2958.  
  2959.         /* Set up scaling data (bitmaps & rastports). */
  2960.  
  2961.     if(!CreateScale())
  2962.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_FONT_SCALING_INFO_TXT));
  2963.  
  2964.     if(!CreateOffsetTables())
  2965.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_OFFSET_TABLES_TXT));
  2966.  
  2967.     TabStopMax = Window -> WScreen -> Width / TextFontWidth;
  2968.  
  2969.         /* Allocate the tab stop line. */
  2970.  
  2971.     if(!(TabStops = (BYTE *)AllocVecPooled(TabStopMax,MEMF_ANY | MEMF_CLEAR)))
  2972.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  2973.  
  2974.         /* Push it on the window stack (should become bottommost
  2975.          * entry).
  2976.          */
  2977.  
  2978.     PushWindow(Window);
  2979.  
  2980.     if(TermPort)
  2981.         TermPort -> TopWindow = Window;
  2982.  
  2983.     StatusWindow = NULL;
  2984.  
  2985.     if(Config -> ScreenConfig -> StatusLine != STATUSLINE_DISABLED)
  2986.     {
  2987.         if(Config -> ScreenConfig -> SplitStatus)
  2988.         {
  2989.             if(!(StatusWindow = OpenWindowTags(NULL,
  2990.                 WA_Top,            Window -> TopEdge + Window -> Height,
  2991.                 WA_Left,        Window -> LeftEdge,
  2992.                 WA_InnerWidth,        StatusWidth,
  2993.                 WA_InnerHeight,        StatusHeight,
  2994.                 WA_GimmeZeroZero,    TRUE,
  2995.                 WA_DragBar,        TRUE,
  2996.                 WA_DepthGadget,        TRUE,
  2997.                 WA_NewLookMenus,    TRUE,
  2998.                 WA_Title,        ScreenTitle,
  2999.                 WA_CustomScreen,    Window -> WScreen,
  3000.                 WA_RMBTrap,        TRUE,
  3001.                 WA_CloseGadget,        TRUE,
  3002.                 WA_MenuHelp,        TRUE,
  3003.  
  3004.                 AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  3005.                 CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  3006.             TAG_DONE)))
  3007.                 return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_STATUS_WINDOW_TXT));
  3008.         }
  3009.         else
  3010.         {
  3011.             if(!Config -> ScreenConfig -> UseWorkbench && !SharedScreen)
  3012.             {
  3013.                 if(!(StatusWindow = OpenWindowTags(NULL,
  3014.                     WA_Top,        Window -> TopEdge + Window -> Height,
  3015.                     WA_Left,    0,
  3016.                     WA_Width,    Screen -> Width,
  3017.                     WA_Height,    Screen -> Height - (Window -> TopEdge + Window -> Height),
  3018.                     WA_Backdrop,    TRUE,
  3019.                     WA_Borderless,    TRUE,
  3020.                     WA_SmartRefresh,TRUE,
  3021.                     WA_NewLookMenus,TRUE,
  3022.                     WA_CustomScreen,Screen,
  3023.                     WA_RMBTrap,    TRUE,
  3024.                     WA_MenuHelp,    TRUE,
  3025.  
  3026.                     AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  3027.                     CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  3028.                 TAG_DONE)))
  3029.                     return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_STATUS_WINDOW_TXT));
  3030.             }
  3031.         }
  3032.     }
  3033.  
  3034.     if(StatusWindow)
  3035.     {
  3036.         StatusWindow -> UserPort = Window -> UserPort;
  3037.  
  3038.         ModifyIDCMP(StatusWindow,Window -> IDCMPFlags);
  3039.     }
  3040.  
  3041.     RPort = Window -> RPort;
  3042.  
  3043.         /* Default console setup. */
  3044.  
  3045.     CursorX = 0;
  3046.     CursorY    = 0;
  3047.  
  3048.     SetDrMd(RPort,JAM2);
  3049.  
  3050.         /* Set the font. */
  3051.  
  3052.     SetFont(RPort,CurrentFont);
  3053.  
  3054.         /* Redirect AmigaDOS requesters. */
  3055.  
  3056.     OldWindowPtr = ThisProcess -> pr_WindowPtr;
  3057.  
  3058.     ThisProcess -> pr_WindowPtr = (APTR)Window;
  3059.  
  3060.         /* Create the character raster. */
  3061.  
  3062.     if(!CreateRaster())
  3063.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_SCREEN_RASTER_TXT));
  3064.  
  3065.     ConOutputUpdate();
  3066.  
  3067.     ConFontScaleUpdate();
  3068.  
  3069.         /* Set up the scrolling info. */
  3070.  
  3071.     ScrollLineCount = Window -> WScreen -> Height / TextFontHeight;
  3072.  
  3073.     if(!(ScrollLines = (struct ScrollLineInfo *)AllocVecPooled(sizeof(struct ScrollLineInfo) * ScrollLineCount,MEMF_ANY|MEMF_CLEAR)))
  3074.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SCROLLING_SUPPORT_INFO_TXT));
  3075.  
  3076.         /* Create the menu strip. */
  3077.  
  3078.     if(Error = BuildMenu())
  3079.         return(Error);
  3080.  
  3081.         /* Disable the `Execute ARexx Command' menu item if
  3082.          * the rexx server is not available.
  3083.          */
  3084.  
  3085.     if(!RexxSysBase)
  3086.         OffItem(MEN_EXECUTE_REXX_COMMAND);
  3087.  
  3088.     if(Recording)
  3089.     {
  3090.         OnItem(MEN_RECORD_LINE);
  3091.  
  3092.         CheckItem(MEN_RECORD,TRUE);
  3093.         CheckItem(MEN_RECORD_LINE,RecordingLine);
  3094.     }
  3095.     else
  3096.     {
  3097.         OffItem(MEN_RECORD_LINE);
  3098.  
  3099.         CheckItem(MEN_RECORD,FALSE);
  3100.         CheckItem(MEN_RECORD_LINE,FALSE);
  3101.     }
  3102.  
  3103.     CheckItem(MEN_DISABLE_TRAPS,!(WatchTraps && GenericListTable[GLIST_TRAP] -> ListHeader . mlh_Head -> mln_Succ));
  3104.  
  3105.     if(StatusWindow)
  3106.     {
  3107.         SetMenuStrip(StatusWindow,Menu);
  3108.  
  3109.         StatusWindow -> Flags &= ~WFLG_RMBTRAP;
  3110.  
  3111.         SetDrMd(StatusWindow -> RPort,JAM2);
  3112.     }
  3113.  
  3114.         /* Add a tick if file capture is active. */
  3115.  
  3116.     if(FileCapture)
  3117.         CheckItem(MEN_CAPTURE_TO_FILE,TRUE);
  3118.     else
  3119.         CheckItem(MEN_CAPTURE_TO_FILE,FALSE);
  3120.  
  3121.         /* Add a tick if printer capture is active. */
  3122.  
  3123.     CheckItem(MEN_CAPTURE_TO_PRINTER,PrinterCapture != NULL);
  3124.  
  3125.         /* Add a tick if the buffer is frozen. */
  3126.  
  3127.     CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  3128.  
  3129.         /* Disable the dialing functions if online. */
  3130.  
  3131.     if(Online)
  3132.         SetDialMenu(FALSE);
  3133.     else
  3134.         SetDialMenu(TRUE);
  3135.  
  3136.         /* Update the clipboard menus. */
  3137.  
  3138.     SetClipMenu(FALSE);
  3139.  
  3140.     if(!XProtocolBase)
  3141.         SetTransferMenu(FALSE);
  3142.  
  3143.         /* Disable the `Print Screen' and `Save ASCII' functions
  3144.          * if raster is not enabled.
  3145.          */
  3146.  
  3147.     SetRasterMenu(RasterEnabled);
  3148.  
  3149.         /* Enable the menu. */
  3150.  
  3151.     Window -> Flags &= ~WFLG_RMBTRAP;
  3152.  
  3153.     strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  3154.  
  3155.         /* Create the status server. */
  3156.  
  3157.     Forbid();
  3158.  
  3159.     if(StatusProcess = CreateNewProcTags(
  3160.         NP_Entry,    StatusServer,
  3161.         NP_Name,    "term Status Process",
  3162.         NP_WindowPtr,    -1,
  3163.         NP_Priority,    5,
  3164.     TAG_DONE))
  3165.     {
  3166.         ClrSignal(SIG_HANDSHAKE);
  3167.  
  3168.         Wait(SIG_HANDSHAKE);
  3169.     }
  3170.  
  3171.     Permit();
  3172.  
  3173.         /* Status server has `died'. */
  3174.  
  3175.     if(!StatusProcess)
  3176.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_STATUS_TASK_TXT));
  3177.  
  3178.         /* Obtain the default public screen name just in case
  3179.          * we'll need it later.
  3180.          */
  3181.  
  3182.     GetDefaultPubScreen(DefaultPubScreenName);
  3183.  
  3184.         /* Set up the window size. */
  3185.  
  3186.     ScreenSizeStuff();
  3187.  
  3188.         /* Select the default console data processing routine. */
  3189.  
  3190.     Forbid();
  3191.  
  3192.     ConProcessData = ConProcessData8;
  3193.  
  3194.     Permit();
  3195.  
  3196.         /* Handle the remaining terminal setup. */
  3197.  
  3198.     if(Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL && Config -> TerminalConfig -> EmulationFileName[0])
  3199.     {
  3200.         if(!OpenEmulator(Config -> TerminalConfig -> EmulationFileName))
  3201.         {
  3202.             Config -> TerminalConfig -> EmulationMode = EMULATION_ANSIVT100;
  3203.  
  3204.             ResetDisplay = TRUE;
  3205.  
  3206.             RasterEnabled = TRUE;
  3207.  
  3208.             MyEasyRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),Config -> TerminalConfig -> EmulationFileName);
  3209.         }
  3210.         else
  3211.         {
  3212.             if(RasterEnabled)
  3213.                 RasterEnabled = FALSE;
  3214.  
  3215.             SetRasterMenu(RasterEnabled);
  3216.         }
  3217.     }
  3218.  
  3219.         /* Choose the right console data processing routine. */
  3220.  
  3221.     ConProcessUpdate();
  3222.  
  3223.         /* Reset terminal emulation. */
  3224.  
  3225.     if(Config -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL)
  3226.     {
  3227.         ClearCursor();
  3228.  
  3229.         Reset();
  3230.  
  3231.         DrawCursor();
  3232.     }
  3233.     else
  3234.     {
  3235.         if(XEmulatorBase)
  3236.             XEmulatorResetConsole(XEM_IO);
  3237.     }
  3238.  
  3239.         /* Restart the fast! macro panel. */
  3240.  
  3241.     if(HadFastMacros || Config -> MiscConfig -> OpenFastMacroPanel)
  3242.         OpenFastWindow();
  3243.  
  3244.     if(Config -> TerminalConfig -> UseTerminalTask && Config -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL)
  3245.         CreateEmulationProcess();
  3246.     else
  3247.         DeleteEmulationProcess();
  3248.  
  3249.     CreateLED();
  3250.  
  3251.     return(NULL);
  3252. }
  3253.  
  3254.     /* CloseAll():
  3255.      *
  3256.      *    Free all resources and leave the program.
  3257.      */
  3258.  
  3259. VOID __regargs
  3260. CloseAll(BYTE CloseDOS)
  3261. {
  3262.     WORD i;
  3263.  
  3264.     Forbid();
  3265.  
  3266.     if(RendezvousSemaphore . rs_Semaphore . ss_Link . ln_Name)
  3267.         RemSemaphore(&RendezvousSemaphore);
  3268.  
  3269.     Permit();
  3270.  
  3271. #ifdef DATAFEED
  3272.     {
  3273.         extern BPTR DataFeed;
  3274.  
  3275.         if(DataFeed)
  3276.         {
  3277.             Close(DataFeed);
  3278.  
  3279.             DataFeed = NULL;
  3280.         }
  3281.     }
  3282. #endif    /* DATAFEED */
  3283.  
  3284.     Forbid();
  3285.  
  3286.     if(DialMsg)
  3287.     {
  3288.         DialMsg -> rm_Result1 = RC_WARN;
  3289.         DialMsg -> rm_Result2 = 0;
  3290.  
  3291.         ReplyMsg(DialMsg);
  3292.  
  3293.         DialMsg = NULL;
  3294.     }
  3295.  
  3296.     Permit();
  3297.  
  3298.     DeleteRecord();
  3299.  
  3300.     DeleteQueueProcess();
  3301.  
  3302.     SoundExit();
  3303.  
  3304.     SZ_SizeCleanup();
  3305.  
  3306.     FreeDialList(TRUE);
  3307.  
  3308.     if(SpecialQueue)
  3309.     {
  3310.         DeleteMsgQueue(SpecialQueue);
  3311.  
  3312.         SpecialQueue = NULL;
  3313.     }
  3314.  
  3315.     if(SpecialTable)
  3316.     {
  3317.         FreeVecPooled(SpecialTable);
  3318.  
  3319.         SpecialTable = NULL;
  3320.     }
  3321.  
  3322.     if(AbortTable)
  3323.     {
  3324.         FreeVecPooled(AbortTable);
  3325.  
  3326.         AbortTable = NULL;
  3327.     }
  3328.  
  3329.     if(BackupConfig)
  3330.     {
  3331.         DeleteConfiguration(BackupConfig);
  3332.  
  3333.         BackupConfig = NULL;
  3334.     }
  3335.  
  3336.     if(IntuitionBase && Window)
  3337.         BlockWindows();
  3338.  
  3339.     /* ALWAYS */
  3340.     {
  3341.         extern struct MsgPort *RexxPort;
  3342.  
  3343.         if(RexxPort)
  3344.             RemPort(RexxPort);
  3345.     }
  3346.  
  3347.     if(EditList)
  3348.     {
  3349.         DeleteList(EditList);
  3350.  
  3351.         EditList = NULL;
  3352.     }
  3353.  
  3354.     if(EditLabels)
  3355.     {
  3356.         FreeVecPooled(EditLabels);
  3357.  
  3358.         EditLabels = NULL;
  3359.     }
  3360.  
  3361.     if(TermRexxPort)
  3362.     {
  3363.         if(RexxSysBase)
  3364.         {
  3365.             struct Message *Msg;
  3366.  
  3367.             while(Msg = GetMsg(TermRexxPort))
  3368.                 ReplyMsg(Msg);
  3369.         }
  3370.  
  3371.         DeleteMsgPort(TermRexxPort);
  3372.  
  3373.         TermRexxPort = NULL;
  3374.     }
  3375.  
  3376.     if(RexxProcess)
  3377.     {
  3378.         Forbid();
  3379.  
  3380.         Signal(RexxProcess,SIG_KILL);
  3381.  
  3382.         ClrSignal(SIG_HANDSHAKE);
  3383.  
  3384.         Wait(SIG_HANDSHAKE);
  3385.  
  3386.         Permit();
  3387.  
  3388.         RexxProcess = NULL;
  3389.     }
  3390.  
  3391.     if(RexxSysBase)
  3392.     {
  3393.         CloseLibrary(RexxSysBase);
  3394.  
  3395.         RexxSysBase = NULL;
  3396.     }
  3397.  
  3398.     if(XprIO && XProtocolBase)
  3399.         XProtocolCleanup(XprIO);
  3400.  
  3401.     if(XProtocolBase)
  3402.     {
  3403.         CloseLibrary(XProtocolBase);
  3404.  
  3405.         XProtocolBase = NULL;
  3406.     }
  3407.  
  3408.     if(XprIO)
  3409.     {
  3410.         FreeVec(XprIO);
  3411.  
  3412.         XprIO = NULL;
  3413.     }
  3414.  
  3415.     if(CursorKeys)
  3416.     {
  3417.         FreeVecPooled(CursorKeys);
  3418.  
  3419.         CursorKeys = NULL;
  3420.     }
  3421.  
  3422.     if(MacroKeys)
  3423.     {
  3424.         FreeVecPooled(MacroKeys);
  3425.  
  3426.         MacroKeys = NULL;
  3427.     }
  3428.  
  3429.     TerminateBuffer();
  3430.  
  3431.     DeleteSpeech();
  3432.  
  3433.     Forbid();
  3434.  
  3435.     BufferClosed = TRUE;
  3436.  
  3437.     DeleteBuffer();
  3438.  
  3439.     Permit();
  3440.  
  3441.     if(AttentionBuffers[0])
  3442.     {
  3443.         FreeVecPooled(AttentionBuffers[0]);
  3444.  
  3445.         AttentionBuffers[0] = NULL;
  3446.     }
  3447.  
  3448.     if(SendTable)
  3449.     {
  3450.         FreeTranslationTable(SendTable);
  3451.  
  3452.         SendTable = NULL;
  3453.     }
  3454.  
  3455.     if(ReceiveTable)
  3456.     {
  3457.         FreeTranslationTable(ReceiveTable);
  3458.  
  3459.         ReceiveTable = NULL;
  3460.     }
  3461.  
  3462.     FreeDialList(TRUE);
  3463.  
  3464.     DeleteOffsetTables();
  3465.  
  3466.     FreeList(&FastMacroList);
  3467.     FreeList((struct List *)&ReviewBufferHistory);
  3468.     FreeList((struct List *)&TextBufferHistory);
  3469.  
  3470.     for(i = GLIST_UPLOAD ; i < GLIST_COUNT ; i++)
  3471.     {
  3472.         if(GenericListTable[i])
  3473.         {
  3474.             DeleteGenericList(GenericListTable[i]);
  3475.  
  3476.             GenericListTable[i] = NULL;
  3477.         }
  3478.     }
  3479.  
  3480.     if(FileCapture)
  3481.     {
  3482.         BufferClose(FileCapture);
  3483.  
  3484.         if(!GetFileSize(CaptureName))
  3485.             DeleteFile(CaptureName);
  3486.         else
  3487.         {
  3488.             AddProtection(CaptureName,FIBF_EXECUTE);
  3489.  
  3490.             if(Config -> MiscConfig -> CreateIcons)
  3491.                 AddIcon(CaptureName,FILETYPE_TEXT,FALSE);
  3492.         }
  3493.  
  3494.         FileCapture = NULL;
  3495.     }
  3496.  
  3497.     if(PrinterCapture)
  3498.     {
  3499.         Close(PrinterCapture);
  3500.  
  3501.         PrinterCapture = NULL;
  3502.     }
  3503.  
  3504.         /* Close the external emulator. */
  3505.  
  3506.     if(XEmulatorBase)
  3507.     {
  3508.         if(XEM_IO)
  3509.         {
  3510.             XEmulatorMacroKeyFilter(XEM_IO,NULL);
  3511.             XEmulatorCloseConsole(XEM_IO);
  3512.             XEmulatorCleanup(XEM_IO);
  3513.  
  3514.             FreeVec(XEM_IO);
  3515.  
  3516.             XEM_IO = NULL;
  3517.         }
  3518.  
  3519.         CloseLibrary(XEmulatorBase);
  3520.  
  3521.         XEmulatorBase = NULL;
  3522.     }
  3523.  
  3524.     if(XEM_MacroKeys)
  3525.     {
  3526.         FreeVecPooled(XEM_MacroKeys);
  3527.  
  3528.         XEM_MacroKeys = NULL;
  3529.     }
  3530.  
  3531.     DeleteDisplay();
  3532.  
  3533.     if(KeySegment)
  3534.     {
  3535.         UnLoadSeg(KeySegment);
  3536.  
  3537.         KeySegment = NULL;
  3538.     }
  3539.  
  3540.     StopCall(TRUE);
  3541.  
  3542.     if(CheckBit != -1)
  3543.     {
  3544.         FreeSignal(CheckBit);
  3545.  
  3546.         CheckBit = -1;
  3547.     }
  3548.  
  3549.     ClearSerial();
  3550.  
  3551.     DeleteSerial();
  3552.  
  3553.     DeletePatternList(PatternList);
  3554.  
  3555.     if(TimeRequest)
  3556.     {
  3557.         if(TimeRequest -> tr_node . io_Device)
  3558.             CloseDevice(TimeRequest);
  3559.  
  3560.         DeleteIORequest(TimeRequest);
  3561.  
  3562.         TimeRequest = NULL;
  3563.     }
  3564.  
  3565.     if(TimePort)
  3566.     {
  3567.         DeleteMsgPort(TimePort);
  3568.  
  3569.         TimePort = NULL;
  3570.     }
  3571.  
  3572.     ShutdownCx();
  3573.  
  3574.     if(TermPort)
  3575.     {
  3576.         if(TermID != -1)
  3577.         {
  3578.             ObtainSemaphore(&TermPort -> OpenSemaphore);
  3579.  
  3580.             TermPort -> OpenCount--;
  3581.  
  3582.             if(TermPort -> OpenCount <= 0 && !TermPort -> HoldIt)
  3583.             {
  3584.                 RemPort(&TermPort -> ExecNode);
  3585.  
  3586.                 ReleaseSemaphore(&TermPort -> OpenSemaphore);
  3587.  
  3588.                 FreeVec(TermPort);
  3589.             }
  3590.             else
  3591.                 ReleaseSemaphore(&TermPort -> OpenSemaphore);
  3592.  
  3593.             TermID = -1;
  3594.         }
  3595.  
  3596.         TermPort = NULL;
  3597.     }
  3598.  
  3599.     CloseClip();
  3600.  
  3601.     if(GTLayoutBase)
  3602.     {
  3603.         CloseLibrary(GTLayoutBase);
  3604.  
  3605.         GTLayoutBase = NULL;
  3606.     }
  3607.  
  3608.     if(Config)
  3609.     {
  3610.         DeleteConfiguration(Config);
  3611.  
  3612.         Config = NULL;
  3613.     }
  3614.  
  3615.     if(PrivateConfig)
  3616.     {
  3617.         DeleteConfiguration(PrivateConfig);
  3618.  
  3619.         PrivateConfig = NULL;
  3620.     }
  3621.  
  3622.     if(FakeInputEvent)
  3623.     {
  3624.         FreeVecPooled(FakeInputEvent);
  3625.  
  3626.         FakeInputEvent = NULL;
  3627.     }
  3628.  
  3629.     if(ConsoleDevice)
  3630.     {
  3631.         CloseDevice(ConsoleRequest);
  3632.  
  3633.         ConsoleDevice = NULL;
  3634.     }
  3635.  
  3636.     if(ConsoleRequest)
  3637.     {
  3638.         FreeVecPooled(ConsoleRequest);
  3639.  
  3640.         ConsoleRequest = NULL;
  3641.     }
  3642.  
  3643.     if(IconBase)
  3644.     {
  3645.         CloseLibrary(IconBase);
  3646.  
  3647.         IconBase = NULL;
  3648.     }
  3649.  
  3650.     if(DataTypesBase)
  3651.     {
  3652.         CloseLibrary(DataTypesBase);
  3653.  
  3654.         DataTypesBase = NULL;
  3655.     }
  3656.  
  3657.     if(WorkbenchBase)
  3658.     {
  3659.         CloseLibrary(WorkbenchBase);
  3660.  
  3661.         WorkbenchBase = NULL;
  3662.     }
  3663.  
  3664.     if(OwnDevUnitBase)
  3665.     {
  3666.         CloseLibrary(OwnDevUnitBase);
  3667.  
  3668.         OwnDevUnitBase = NULL;
  3669.     }
  3670.  
  3671.     if(CxBase)
  3672.     {
  3673.         CloseLibrary(CxBase);
  3674.  
  3675.         CxBase = NULL;
  3676.     }
  3677.  
  3678.     if(IFFParseBase)
  3679.     {
  3680.         CloseLibrary(IFFParseBase);
  3681.  
  3682.         IFFParseBase = NULL;
  3683.     }
  3684.  
  3685.     if(AslBase)
  3686.     {
  3687.         CloseLibrary(AslBase);
  3688.  
  3689.         AslBase = NULL;
  3690.     }
  3691.  
  3692.     if(DiskfontBase)
  3693.     {
  3694.         CloseLibrary(DiskfontBase);
  3695.  
  3696.         DiskfontBase = NULL;
  3697.     }
  3698.  
  3699.     if(GadToolsBase)
  3700.     {
  3701.         CloseLibrary(GadToolsBase);
  3702.  
  3703.         GadToolsBase = NULL;
  3704.     }
  3705.  
  3706.     if(LayersBase)
  3707.     {
  3708.         CloseLibrary(LayersBase);
  3709.  
  3710.         LayersBase = NULL;
  3711.     }
  3712.  
  3713.     if(GfxBase)
  3714.     {
  3715.         CloseLibrary(GfxBase);
  3716.  
  3717.         GfxBase = NULL;
  3718.     }
  3719.  
  3720.     if(IntuitionBase)
  3721.     {
  3722.         CloseLibrary(IntuitionBase);
  3723.  
  3724.         IntuitionBase = NULL;
  3725.     }
  3726.  
  3727.     LocaleClose();
  3728.  
  3729.     if(UtilityBase)
  3730.     {
  3731.         CloseLibrary(UtilityBase);
  3732.  
  3733.         UtilityBase = NULL;
  3734.     }
  3735.  
  3736. #ifdef DEBUG
  3737.     DebugExit();
  3738. #endif    /* DEBUG */
  3739.  
  3740. #ifdef BETA
  3741.     StopBetaTask();
  3742. #endif    /* BETA */
  3743.  
  3744.     MemoryCleanup();
  3745.  
  3746.     if(WBenchMsg)
  3747.     {
  3748.         CurrentDir(WBenchLock);
  3749.  
  3750.         if(DOSBase)
  3751.         {
  3752.             CloseLibrary(DOSBase);
  3753.  
  3754.             DOSBase = NULL;
  3755.         }
  3756.  
  3757.         Forbid();
  3758.  
  3759.         ReplyMsg((struct Message *)WBenchMsg);
  3760.  
  3761.         WBenchMsg = NULL;
  3762.     }
  3763.     else
  3764.     {
  3765.         if(CloseDOS && DOSBase)
  3766.         {
  3767.             CloseLibrary(DOSBase);
  3768.  
  3769.             DOSBase = NULL;
  3770.         }
  3771.     }
  3772. }
  3773.  
  3774.     /* AddExtraAssignment(STRPTR LocalDir,STRPTR Assign):
  3775.      *
  3776.      *    Add assignments for local directories.
  3777.      */
  3778.  
  3779. STATIC VOID __regargs
  3780. AddExtraAssignment(STRPTR LocalDir,STRPTR Assign)
  3781. {
  3782.     UBYTE    LocalBuffer[40];
  3783.     BPTR    FileLock;
  3784.  
  3785.         // Add the colon, we'll need it later
  3786.  
  3787.     SPrintf(LocalBuffer,"%s:",Assign);
  3788.  
  3789.         // Is the local directory present?
  3790.  
  3791.     if(FileLock = Lock(LocalDir,ACCESS_READ))
  3792.     {
  3793.             // Is the assignment present?
  3794.  
  3795.         if(IsAssign(LocalBuffer))
  3796.         {
  3797.                 // Check to see if the local directory
  3798.                 // is already on the assignment list
  3799.  
  3800.             if(LockInAssign(FileLock,LocalBuffer))
  3801.             {
  3802.                 UnLock(FileLock);
  3803.  
  3804.                 FileLock = NULL;
  3805.             }
  3806.         }
  3807.     }
  3808.  
  3809.         // Can we attach the lock to the assignment list?
  3810.  
  3811.     if(FileLock)
  3812.     {
  3813.         Forbid();
  3814.  
  3815.             // If the assignment is already present, add the
  3816.             // new directory, else create a new assignment.
  3817.  
  3818.         if(IsAssign(LocalBuffer))
  3819.             AssignAdd(Assign,FileLock);
  3820.         else
  3821.             AssignLock(Assign,FileLock);
  3822.  
  3823.         Permit();
  3824.     }
  3825. }
  3826.  
  3827.     /* OpenAll():
  3828.      *
  3829.      *    Open all required resources or return an error message
  3830.      *    if anything went wrong.
  3831.      */
  3832.  
  3833. STRPTR __regargs
  3834. OpenAll(STRPTR ConfigPath)
  3835. {
  3836.     extern    ULONG HookEntry(struct Hook *,APTR,APTR);
  3837.  
  3838.     UBYTE         PathBuffer[MAX_FILENAME_LENGTH];
  3839.     STRPTR         Result,Error,ConfigFileName = NULL;
  3840.     WORD         i;
  3841.     struct Node    *Node;
  3842.     LONG         ErrorCode;
  3843.  
  3844.         /* Pretty cheap ;-) */
  3845.  
  3846.     Kick30 = (SysBase -> LibNode . lib_Version >= 39);
  3847.  
  3848.     if(!MemorySetup())
  3849.         return("Cannot create memory pool");
  3850.  
  3851. #ifdef DEBUG
  3852.     DebugInit();
  3853. #endif    /* DEBUG */
  3854.  
  3855.         /* Don't let it hit the ground! */
  3856.  
  3857.     ConTransfer = ConProcess;
  3858.  
  3859.         /* Remember the start of this session. */
  3860.  
  3861.     DateStamp(&SessionStart);
  3862.  
  3863.         /* Reset some flags. */
  3864.  
  3865.     BinaryTransfer    = TRUE;
  3866.  
  3867.     Status        = STATUS_READY;
  3868.  
  3869.     WasOnline    = FALSE;
  3870.     Online        = FALSE;
  3871.  
  3872.     InSequence    = FALSE;
  3873.     Quiet        = FALSE;
  3874.  
  3875.     TagDPI[0] . ti_Tag = TAG_DONE;
  3876.  
  3877.         /* Double buffered file locking. */
  3878.  
  3879.     NewList(&DoubleBufferList);
  3880.  
  3881.     InitSemaphore(&DoubleBufferSemaphore);
  3882.  
  3883.         /* Terminal emulation data. */
  3884.  
  3885.     InitSemaphore(&TerminalSemaphore);
  3886.  
  3887.         /* Phone number patterns and rates. */
  3888.  
  3889.     InitSemaphore(&PatternSemaphore);
  3890.  
  3891.         /* Online status. */
  3892.  
  3893.     InitSemaphore(&OnlineSemaphore);
  3894.  
  3895.         /* Text buffer task access semaphore. */
  3896.  
  3897.     InitSemaphore(&BufferTaskSemaphore);
  3898.  
  3899.         /* Set up all the lists. */
  3900.  
  3901.     NewList(&PacketHistoryList);
  3902.     NewList(&EmptyList);
  3903.     NewList(&FastMacroList);
  3904.     NewList(&TransferInfoList);
  3905.  
  3906.     NewList((struct List *)&ReviewBufferHistory);
  3907.     NewList((struct List *)&TextBufferHistory);
  3908.  
  3909.         /* Rendezvous setup. */
  3910.  
  3911.     InitSemaphore(&RendezvousSemaphore);
  3912.  
  3913.     RendezvousSemaphore . rs_Login        = RendezvousLogin;
  3914.     RendezvousSemaphore . rs_Logoff        = RendezvousLogoff;
  3915.     RendezvousSemaphore . rs_NewNode    = RendezvousNewNode;
  3916.  
  3917.         /* Open the translation tables. */
  3918.  
  3919.     LocaleOpen("term.catalog","english",20);
  3920.  
  3921.         /* Fill in the menu configuration. */
  3922.  
  3923.     LocalizeMenuTable(TermMenu,MenuLabels);
  3924.  
  3925.         /* Open intuition.library, any version. */
  3926.  
  3927.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  3928.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_INTUITION_LIBRARY_TXT));
  3929.  
  3930.     Forbid();
  3931.  
  3932.         /* Query the current public screen modes. */
  3933.  
  3934.     PublicModes = SetPubScreenModes(NULL);
  3935.  
  3936.         /* Set them back. */
  3937.  
  3938.     SetPubScreenModes(PublicModes);
  3939.  
  3940.     Permit();
  3941.  
  3942.         /* Open some more libraries. */
  3943.  
  3944.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
  3945.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GRAPHICS_LIBRARY_TXT));
  3946.  
  3947.     if(!(LayersBase = OpenLibrary("layers.library",0)))
  3948.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_LAYERS_LIBRARY_TXT));
  3949.  
  3950.         /* Install the correct routines to query
  3951.          * the rendering colours and drawing mode.
  3952.          */
  3953.  
  3954.     if(!Kick30)
  3955.     {
  3956.         ReadAPen = OldGetAPen;
  3957.         ReadBPen = OldGetBPen;
  3958.         ReadDrMd = OldGetDrMd;
  3959.         SetWrMsk = OldSetWrMsk;
  3960.     }
  3961.     else
  3962.     {
  3963.         ReadAPen = NewGetAPen;
  3964.         ReadBPen = NewGetBPen;
  3965.         ReadDrMd = NewGetDrMd;
  3966.         SetWrMsk = NewSetWrMsk;
  3967.     }
  3968.  
  3969.     if(!(UtilityBase = OpenLibrary("utility.library",0)))
  3970.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_UTILITY_LIBRARY_TXT));
  3971.  
  3972.         /* Check if locale.library has already installed the operating system
  3973.          * patches required for localization.
  3974.          */
  3975.  
  3976.     LanguageCheck();
  3977.  
  3978.         /* Open the remaining libraries. */
  3979.  
  3980.     if(!(GadToolsBase = OpenLibrary("gadtools.library",0)))
  3981.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GADTOOLS_LIBRARY_TXT));
  3982.  
  3983.     if(!(AslBase = OpenLibrary("asl.library",0)))
  3984.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_ASL_LIBRARY_TXT));
  3985.  
  3986.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  3987.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_IFFPARSE_LIBRARY_TXT));
  3988.  
  3989.     if(!(CxBase = OpenLibrary("commodities.library",0)))
  3990.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_COMMODITIES_LIBRARY_TXT));
  3991.  
  3992.     if(!(DiskfontBase = (struct Library *)OpenLibrary("diskfont.library",0)))
  3993.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_DISKFONT_LIBRARY_TXT));
  3994.  
  3995.         /* User interface. */
  3996.  
  3997.     if(!(GTLayoutBase = OpenLibrary("PROGDIR:gtlayout.library",0)))
  3998.     {
  3999.         if(!(GTLayoutBase = OpenLibrary("gtlayout.library",0)))
  4000.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GTLAYOUT_LIBRARY_TXT));
  4001.     }
  4002.  
  4003.         /* Open OwnDevUnit.library, don't complain if it fails. */
  4004.  
  4005.     OwnDevUnitBase = OpenLibrary(ODU_NAME,0);
  4006.  
  4007.         /* Open workbench.library, don't complain if it fails. */
  4008.  
  4009.     WorkbenchBase = OpenLibrary("workbench.library",0);
  4010.  
  4011.         /* Open icon.library as well, don't complain if it fails either. */
  4012.  
  4013.     IconBase = OpenLibrary("icon.library",0);
  4014.  
  4015.         /* Try to open datatypes.library, just for the fun of it. */
  4016.  
  4017.     DataTypesBase = OpenLibrary("datatypes.library",39);
  4018.  
  4019.     if(!(ConsoleRequest = (struct IOStdReq *)AllocVecPooled(sizeof(struct IOStdReq),MEMF_ANY|MEMF_CLEAR)))
  4020.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CONSOLE_REQUEST_TXT));
  4021.  
  4022.     if(OpenDevice("console.device",CONU_LIBRARY,ConsoleRequest,0))
  4023.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_CONSOLE_DEVICE_TXT));
  4024.  
  4025.     ConsoleDevice = &ConsoleRequest -> io_Device -> dd_Library;
  4026.  
  4027.     if(!(FakeInputEvent = (struct InputEvent *)AllocVecPooled(sizeof(struct InputEvent),MEMF_ANY|MEMF_CLEAR)))
  4028.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_INPUTEVENT_TXT));
  4029.  
  4030.     FakeInputEvent -> ie_Class = IECLASS_RAWKEY;
  4031.  
  4032.     if(!(MacroKeys = (struct MacroKeys *)AllocVecPooled(sizeof(struct MacroKeys),MEMF_ANY|MEMF_CLEAR)))
  4033.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACROKEYS_TXT));
  4034.  
  4035.     if(!(CursorKeys = (struct CursorKeys *)AllocVecPooled(sizeof(struct CursorKeys),MEMF_ANY|MEMF_CLEAR)))
  4036.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CURSORKEYS_TXT));
  4037.  
  4038.     ResetCursorKeys(CursorKeys);
  4039.  
  4040.         /* Add extra assignments. */
  4041.  
  4042.     AddExtraAssignment("PROGDIR:Fonts","Fonts");
  4043.     AddExtraAssignment("Fonts","Fonts");
  4044.     AddExtraAssignment("PROGDIR:Libs","Libs");
  4045.     AddExtraAssignment("Libs","Libs");
  4046.  
  4047.         /* Set up the attention buffers. */
  4048.  
  4049.     if(!(AttentionBuffers[0] = (STRPTR)AllocVecPooled(SCAN_COUNT * 260,MEMF_ANY|MEMF_CLEAR)))
  4050.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SEQUENCE_ATTENTION_INFO_TXT));
  4051.  
  4052.     for(i = 1 ; i < SCAN_COUNT ; i++)
  4053.         AttentionBuffers[i] = &AttentionBuffers[i - 1][260];
  4054.  
  4055.         /* Obtain the default environment storage
  4056.          * path.
  4057.          */
  4058.  
  4059.     if(!ConfigPath)
  4060.     {
  4061.         ConfigPath = PathBuffer;
  4062.  
  4063.         if(!GetEnvDOS("TERMCONFIGPATH",PathBuffer))
  4064.         {
  4065.             if(!GetEnvDOS("TERMPATH",PathBuffer))
  4066.             {
  4067.                 APTR LastPtr = ThisProcess -> pr_WindowPtr;
  4068.                 BPTR FileLock;
  4069.  
  4070.                 strcpy(PathBuffer,"TERM:config");
  4071.  
  4072.                 ThisProcess -> pr_WindowPtr = (APTR)-1;
  4073.  
  4074.                 if(FileLock = Lock("TERM:",ACCESS_READ))
  4075.                     UnLock(FileLock);
  4076.                 else
  4077.                 {
  4078.                     FileLock = DupLock(ThisProcess-> pr_HomeDir);
  4079.  
  4080.                         /* Create TERM: assignment referring to
  4081.                          * the directory `term' was loaded from.
  4082.                          */
  4083.  
  4084.                     if(!AssignLock("TERM",FileLock))
  4085.                         UnLock(FileLock);
  4086.                 }
  4087.  
  4088.                 if(!(FileLock = Lock(PathBuffer,ACCESS_READ)))
  4089.                     FileLock = CreateDir(PathBuffer);
  4090.  
  4091.                 if(FileLock)
  4092.                     UnLock(FileLock);
  4093.  
  4094.                 ThisProcess -> pr_WindowPtr = LastPtr;
  4095.             }
  4096.         }
  4097.     }
  4098.     else
  4099.     {
  4100.         if(GetFileSize(ConfigPath))
  4101.         {
  4102.             STRPTR Index;
  4103.  
  4104.             strcpy(PathBuffer,ConfigPath);
  4105.  
  4106.             Index = PathPart(PathBuffer);
  4107.  
  4108.             *Index = 0;
  4109.  
  4110.             ConfigFileName = ConfigPath;
  4111.  
  4112.             ConfigPath = PathBuffer;
  4113.         }
  4114.     }
  4115.  
  4116.         /* Check for proper assignment path if necessary. */
  4117.  
  4118.         if(!Strnicmp(ConfigPath,"TERM:",5))
  4119.         {
  4120.             APTR OldPtr = ThisProcess -> pr_WindowPtr;
  4121.             BPTR DirLock;
  4122.  
  4123.             /* Block dos requesters. */
  4124.  
  4125.             ThisProcess -> pr_WindowPtr = (APTR)-1;
  4126.  
  4127.             /* Try to get a lock on `TERM:' assignment. */
  4128.  
  4129.         if(DirLock = Lock("TERM:",ACCESS_READ))
  4130.             UnLock(DirLock);
  4131.         else
  4132.         {
  4133.                 /* Clone current directory lock. */
  4134.  
  4135.             DirLock = DupLock(ThisProcess-> pr_CurrentDir);
  4136.  
  4137.                 /* Create TERM: assignment referring to
  4138.                  * current directory.
  4139.                  */
  4140.  
  4141.             if(!AssignLock("TERM",DirLock))
  4142.                 UnLock(DirLock);
  4143.         }
  4144.  
  4145.         ThisProcess -> pr_WindowPtr = OldPtr;
  4146.         }
  4147.  
  4148.         /* Create proper path names. */
  4149.  
  4150.     if(ConfigFileName)
  4151.     {
  4152.         if(!GetFileSize(ConfigFileName))
  4153.             ConfigFileName = NULL;
  4154.     }
  4155.  
  4156.     if(!ConfigFileName)
  4157.     {
  4158.         strcpy(LastConfig,ConfigPath);
  4159.  
  4160.         AddPart(LastConfig,"term_preferences.iff",MAX_FILENAME_LENGTH);
  4161.  
  4162.         if(!GetFileSize(LastConfig))
  4163.         {
  4164.             strcpy(LastConfig,ConfigPath);
  4165.  
  4166.             AddPart(LastConfig,"term.prefs",MAX_FILENAME_LENGTH);
  4167.         }
  4168.     }
  4169.     else
  4170.         strcpy(LastConfig,ConfigFileName);
  4171.  
  4172.     strcpy(DefaultPubScreenName,"Workbench");
  4173.  
  4174.         /* Create both configuration buffers. */
  4175.  
  4176.     if(!(Config = CreateConfiguration(TRUE)))
  4177.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_PRIMARY_CONFIG_TXT));
  4178.  
  4179.     if(!(PrivateConfig = CreateConfiguration(TRUE)))
  4180.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_SECONDARY_CONFIG_TXT));
  4181.  
  4182.     ResetConfig(Config,ConfigPath);
  4183.  
  4184.         /* Read some more environment variables. */
  4185.  
  4186.     if(!WindowName[0])
  4187.     {
  4188.         if(!GetEnvDOS("TERMWINDOW",WindowName))
  4189.             strcpy(WindowName,"CON:0/11//100/term Output Window/CLOSE/SCREEN %s");
  4190.     }
  4191.  
  4192.     GetEnvDOS("EDITOR",Config -> PathConfig -> Editor);
  4193.  
  4194.         /* Look for the default configuration file. */
  4195.  
  4196.     if(!ReadConfig(LastConfig,Config))
  4197.     {
  4198.         ResetConfig(Config,ConfigPath);
  4199.  
  4200.         Initializing = TRUE;
  4201.  
  4202.         LoadColours = TRUE;
  4203.     }
  4204.     else
  4205.     {
  4206.         switch(Config -> ScreenConfig -> ColourMode)
  4207.         {
  4208.             case COLOUR_EIGHT:
  4209.  
  4210.                 CopyMem(Config -> ScreenConfig -> Colours,ANSIColours,16 * sizeof(UWORD));
  4211.                 break;
  4212.  
  4213.             case COLOUR_SIXTEEN:
  4214.  
  4215.                 CopyMem(Config -> ScreenConfig -> Colours,EGAColours,16 * sizeof(UWORD));
  4216.                 break;
  4217.  
  4218.             case COLOUR_AMIGA:
  4219.  
  4220.                 CopyMem(Config -> ScreenConfig -> Colours,DefaultColours,16 * sizeof(UWORD));
  4221.                 break;
  4222.  
  4223.             case COLOUR_MONO:
  4224.  
  4225.                 CopyMem(Config -> ScreenConfig -> Colours,AtomicColours,16 * sizeof(UWORD));
  4226.                 break;
  4227.         }
  4228.  
  4229.         if(Config -> ScreenConfig -> ColourMode == COLOUR_AMIGA)
  4230.             Initializing = FALSE;
  4231.         else
  4232.             Initializing = TRUE;
  4233.     }
  4234.  
  4235.     if(UseNewDevice)
  4236.         strcpy(Config -> SerialConfig -> SerialDevice,NewDevice);
  4237.  
  4238.     if(UseNewUnit)
  4239.         Config -> SerialConfig -> UnitNumber = NewUnit;
  4240.  
  4241.     if(Config -> MiscConfig -> OpenFastMacroPanel)
  4242.         HadFastMacros = TRUE;
  4243.  
  4244.     strcpy(LastPhone,    Config -> PathConfig -> DefaultStorage);
  4245.     AddPart(LastPhone,    "term_phonebook.iff",MAX_FILENAME_LENGTH);
  4246.  
  4247.     if(!GetFileSize(LastPhone))
  4248.     {
  4249.         strcpy(LastPhone,    Config -> PathConfig -> DefaultStorage);
  4250.         AddPart(LastPhone,    "phonebook.prefs",MAX_FILENAME_LENGTH);
  4251.     }
  4252.  
  4253.     strcpy(LastKeys,    Config -> PathConfig -> DefaultStorage);
  4254.     AddPart(LastKeys,    "term_hotkeys.iff",MAX_FILENAME_LENGTH);
  4255.  
  4256.     if(!GetFileSize(LastKeys))
  4257.     {
  4258.         strcpy(LastKeys,    Config -> PathConfig -> DefaultStorage);
  4259.         AddPart(LastKeys,    "hotkeys.prefs",MAX_FILENAME_LENGTH);
  4260.     }
  4261.  
  4262.     strcpy(LastSpeech,    Config -> PathConfig -> DefaultStorage);
  4263.     AddPart(LastSpeech,    "term_speech.iff",MAX_FILENAME_LENGTH);
  4264.  
  4265.     if(!GetFileSize(LastSpeech))
  4266.     {
  4267.         strcpy(LastSpeech,    Config -> PathConfig -> DefaultStorage);
  4268.         AddPart(LastSpeech,    "speech.prefs",MAX_FILENAME_LENGTH);
  4269.     }
  4270.  
  4271.     strcpy(LastFastMacros,    Config -> PathConfig -> DefaultStorage);
  4272.     AddPart(LastFastMacros,    "term_fastmacros.iff",MAX_FILENAME_LENGTH);
  4273.  
  4274.     if(!GetFileSize(LastFastMacros))
  4275.     {
  4276.         strcpy(LastFastMacros,    Config -> PathConfig -> DefaultStorage);
  4277.         AddPart(LastFastMacros,    "fastmacros.prefs",MAX_FILENAME_LENGTH);
  4278.     }
  4279.  
  4280.     if(Config -> FileConfig -> MacroFileName[0])
  4281.         strcpy(LastMacros,Config -> FileConfig -> MacroFileName);
  4282.     else
  4283.     {
  4284.         strcpy(LastMacros,    Config -> PathConfig -> DefaultStorage);
  4285.         AddPart(LastMacros,    "term_macros.iff",MAX_FILENAME_LENGTH);
  4286.  
  4287.         if(!GetFileSize(LastMacros))
  4288.         {
  4289.             strcpy(LastMacros,    Config -> PathConfig -> DefaultStorage);
  4290.             AddPart(LastMacros,    "macros.prefs",MAX_FILENAME_LENGTH);
  4291.  
  4292.             if(!GetFileSize(LastMacros))
  4293.             {
  4294.                 strcpy(LastMacros,    Config -> PathConfig -> DefaultStorage);
  4295.                 AddPart(LastMacros,    "functionkeys.prefs",MAX_FILENAME_LENGTH);
  4296.             }
  4297.         }
  4298.     }
  4299.  
  4300.         /* Load the keyboard macros. */
  4301.  
  4302.     if(!LoadMacros(LastMacros,MacroKeys))
  4303.         ResetMacroKeys(MacroKeys);
  4304.  
  4305.     if(Config -> FileConfig -> CursorFileName[0])
  4306.         strcpy(LastCursorKeys,Config -> FileConfig -> CursorFileName);
  4307.     else
  4308.     {
  4309.         strcpy(LastCursorKeys,    Config -> PathConfig -> DefaultStorage);
  4310.         AddPart(LastCursorKeys,    "cursorkeys.prefs",MAX_FILENAME_LENGTH);
  4311.     }
  4312.  
  4313.         /* Load the cursor keys. */
  4314.  
  4315.     if(!ReadIFFData(LastCursorKeys,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  4316.         ResetCursorKeys(CursorKeys);
  4317.  
  4318.     strcpy(LastSound,    Config -> PathConfig -> DefaultStorage);
  4319.     AddPart(LastSound,    "sound.prefs",MAX_FILENAME_LENGTH);
  4320.  
  4321.         /* Load the sound settings. */
  4322.  
  4323.     memset(&SoundConfig,0,sizeof(struct SoundConfig));
  4324.  
  4325.     SoundConfig . Volume = 100;
  4326.  
  4327.     if(!ReadIFFData(LastSound,&SoundConfig,sizeof(struct SoundConfig),ID_SOUN))
  4328.         strcpy(SoundConfig . BellFile,Config -> TerminalConfig -> BeepFileName);
  4329.  
  4330.         /* Initialize the sound support routines. */
  4331.  
  4332.     SoundInit();
  4333.  
  4334.         /* Load the phone number pattern / rates settings. */
  4335.  
  4336.     strcpy(LastPattern,    Config -> PathConfig -> DefaultStorage);
  4337.     AddPart(LastPattern,    "rates.prefs",MAX_FILENAME_LENGTH);
  4338.  
  4339.     if(!(PatternList = LoadTimeDateList(LastPattern,&ErrorCode)))
  4340.     {
  4341.         if(!(PatternList = (struct List *)AllocVecPooled(sizeof(struct List),MEMF_ANY)))
  4342.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4343.         else
  4344.             NewList(PatternList);
  4345.     }
  4346.  
  4347.         /* Are we to load the translation tables? */
  4348.  
  4349.     strcpy(LastTranslation,Config -> FileConfig -> TranslationFileName);
  4350.  
  4351.     if(Config -> FileConfig -> TranslationFileName[0])
  4352.     {
  4353.         if(SendTable = AllocTranslationTable())
  4354.         {
  4355.             if(ReceiveTable = AllocTranslationTable())
  4356.             {
  4357.                 if(!LoadTranslationTables(Config -> FileConfig -> TranslationFileName,SendTable,ReceiveTable))
  4358.                 {
  4359.                     FreeTranslationTable(SendTable);
  4360.  
  4361.                     SendTable = NULL;
  4362.  
  4363.                     FreeTranslationTable(ReceiveTable);
  4364.  
  4365.                     ReceiveTable = NULL;
  4366.                 }
  4367.                 else
  4368.                 {
  4369.                     if(IsStandardTable(SendTable) && IsStandardTable(ReceiveTable))
  4370.                     {
  4371.                         FreeTranslationTable(SendTable);
  4372.  
  4373.                         SendTable = NULL;
  4374.  
  4375.                         FreeTranslationTable(ReceiveTable);
  4376.  
  4377.                         ReceiveTable = NULL;
  4378.                     }
  4379.                 }
  4380.             }
  4381.             else
  4382.             {
  4383.                 FreeTranslationTable(SendTable);
  4384.  
  4385.                 SendTable = NULL;
  4386.             }
  4387.         }
  4388.     }
  4389.  
  4390.     SendSetup();
  4391.  
  4392.     ConOutputUpdate();
  4393.  
  4394.         /* Load the fast! macro settings. */
  4395.  
  4396.     LoadFastMacros(LastFastMacros,&FastMacroList);
  4397.  
  4398.         /* Load the speech settings. */
  4399.  
  4400.     if(!ReadIFFData(LastSpeech,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  4401.     {
  4402.         SpeechConfig . Rate        = DEFRATE;
  4403.         SpeechConfig . Pitch        = DEFPITCH;
  4404.         SpeechConfig . Frequency    = DEFFREQ;
  4405.         SpeechConfig . Volume        = DEFVOL;
  4406.         SpeechConfig . Sex        = DEFSEX;
  4407.         SpeechConfig . Enabled        = FALSE;
  4408.     }
  4409.  
  4410.         /* Load the hotkey settings. */
  4411.  
  4412.     if(!LoadHotkeys(LastKeys,&Hotkeys))
  4413.     {
  4414.         strcpy(Hotkeys . termScreenToFront,    "lshift rshift return");
  4415.         strcpy(Hotkeys . BufferScreenToFront,    "control rshift return");
  4416.         strcpy(Hotkeys . SkipDialEntry,        "control lshift rshift return");
  4417.         strcpy(Hotkeys . AbortARexx,        "lshift rshift escape");
  4418.  
  4419.         Hotkeys . CommodityPriority    = 0;
  4420.         Hotkeys . HotkeysEnabled    = TRUE;
  4421.     }
  4422.  
  4423.         /* Initialize the data flow parser. */
  4424.  
  4425.     FlowInit(TRUE);
  4426.  
  4427.         /* Set up the edit list labels for the phonebook. */
  4428.  
  4429.     if(!(EditList = (struct List *)AllocVecPooled(sizeof(struct List),MEMF_ANY)))
  4430.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4431.  
  4432.     NewList(EditList);
  4433.  
  4434.     if(!(EditLabels = (STRPTR *)AllocVecPooled(sizeof(STRPTR) * (MSG_PHONEPANEL_RATES_TXT - MSG_PHONEPANEL_SERIAL_TXT + 1),MEMF_ANY | MEMF_CLEAR)))
  4435.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4436.  
  4437.     for(i = MSG_PHONEPANEL_SERIAL_TXT ; i <= MSG_PHONEPANEL_RATES_TXT ; i++)
  4438.     {
  4439.         if(Node = (struct Node *)AllocVecPooled(sizeof(struct Node) + strlen(LocaleString(i)) + 2,MEMF_ANY))
  4440.         {
  4441.             EditLabels[i - MSG_PHONEPANEL_SERIAL_TXT] = Node -> ln_Name = (STRPTR)(Node + 1);
  4442.  
  4443.             Node -> ln_Name[0] = ' ';
  4444.  
  4445.             strcpy(&Node -> ln_Name[1],LocaleString(i));
  4446.  
  4447.             AddTail(EditList,Node);
  4448.         }
  4449.         else
  4450.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4451.     }
  4452.  
  4453.         /* Set up parsing jump tables. */
  4454.  
  4455.     if(!(SpecialTable = (JUMP *)AllocVecPooled(256 * sizeof(JUMP),MEMF_CLEAR | MEMF_ANY)))
  4456.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4457.  
  4458.     for(i = 0 ; i < sizeof(SpecialKeys) / sizeof(struct SpecialKey) ; i++)
  4459.         SpecialTable[SpecialKeys[i] . Key] = (JUMP)SpecialKeys[i] . Routine;
  4460.  
  4461.     if(!(AbortTable = (JUMP *)AllocVecPooled(256 * sizeof(JUMP),MEMF_ANY)))
  4462.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4463.  
  4464.     for(i = 0 ; i < 256 ; i++)
  4465.     {
  4466.         switch(AbortMap[i])
  4467.         {
  4468.             case 0:    AbortTable[i] = (JUMP)ParseCode;
  4469.                 break;
  4470.  
  4471.             case 1:    AbortTable[i] = (JUMP)DoCancel;
  4472.                 break;
  4473.  
  4474.             case 2:    AbortTable[i] = (JUMP)DoNewEsc;
  4475.                 break;
  4476.  
  4477.             case 3:    AbortTable[i] = (JUMP)DoNewCsi;
  4478.                 break;
  4479.         }
  4480.     }
  4481.  
  4482.         /* Create all generic lists. */
  4483.  
  4484.     for(i = GLIST_UPLOAD ; i < GLIST_COUNT ; i++)
  4485.     {
  4486.         if(!(GenericListTable[i] = CreateGenericList()))
  4487.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4488.     }
  4489.  
  4490.         /* Load the trap settings. */
  4491.  
  4492.     strcpy(LastTraps,    Config -> PathConfig -> DefaultStorage);
  4493.     AddPart(LastTraps,    "trap.prefs",MAX_FILENAME_LENGTH);
  4494.  
  4495.     WatchTraps = TRUE;
  4496.  
  4497.     LoadTraps(LastTraps,GenericListTable[GLIST_TRAP]);
  4498.  
  4499.         /* Create the special event queue. */
  4500.  
  4501.     if(!(SpecialQueue = CreateMsgQueue(NULL,0)))
  4502.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4503.  
  4504.         /* Set up the serial driver. */
  4505.  
  4506.     if(Error = CreateSerial())
  4507.     {
  4508.         MyEasyRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error);
  4509.  
  4510.         DeleteSerial();
  4511.     }
  4512.     else
  4513.     {
  4514.         if(SerialMessage)
  4515.         {
  4516.             MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  4517.  
  4518.             SerialMessage = NULL;
  4519.         }
  4520.     }
  4521.  
  4522.         /* Get a signal bit. */
  4523.  
  4524.     if((CheckBit = AllocSignal(-1)) == -1)
  4525.         return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_CHECK_SIGNAL_TXT));
  4526.  
  4527.     if(!(TimePort = (struct MsgPort *)CreateMsgPort()))
  4528.         return(LocaleString(MSG_GLOBAL_FAILED_TO_CREATE_MSGPORT_TXT));
  4529.  
  4530.     if(!(TimeRequest = (struct timerequest *)CreateIORequest(TimePort,sizeof(struct timerequest))))
  4531.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_IOREQUEST_TXT));
  4532.  
  4533.     if(OpenDevice("timer.device",UNIT_VBLANK,TimeRequest,0))
  4534.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_TIMER_DEVICE_TXT));
  4535.  
  4536.     TimerBase = &TimeRequest -> tr_node . io_Device -> dd_Library;
  4537.  
  4538.         /* Add the global term port. */
  4539.  
  4540.     if(!TermPort)
  4541.     {
  4542.         if(!(TermPort = (struct TermPort *)AllocVec(sizeof(struct TermPort) + 11,MEMF_PUBLIC|MEMF_CLEAR)))
  4543.             return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_GLOBAL_PORT_TXT));
  4544.         else
  4545.         {
  4546.             NewList(&TermPort -> ExecNode . mp_MsgList);
  4547.  
  4548.             InitSemaphore(&TermPort -> OpenSemaphore);
  4549.  
  4550.             TermPort -> ExecNode . mp_Flags            = PA_IGNORE;
  4551.             TermPort -> ExecNode . mp_Node . ln_Name    = (char *)(TermPort + 1);
  4552.  
  4553.             strcpy(TermPort -> ExecNode . mp_Node . ln_Name,"term Port");
  4554.  
  4555.             AddPort(&TermPort -> ExecNode);
  4556.         }
  4557.     }
  4558.  
  4559.         /* Keep another term task from removing the port. */
  4560.  
  4561.     TermPort -> HoldIt = TRUE;
  4562.  
  4563.         /* Install a new term process. */
  4564.  
  4565.     ObtainSemaphore(&TermPort -> OpenSemaphore);
  4566.  
  4567.     TermPort -> OpenCount++;
  4568.  
  4569.     TermPort -> HoldIt = FALSE;
  4570.  
  4571.     TermID = TermPort -> ID++;
  4572.  
  4573.     ReleaseSemaphore(&TermPort -> OpenSemaphore);
  4574.  
  4575.         /* Set up the ID string. */
  4576.  
  4577.     if(TermID)
  4578.         SPrintf(TermIDString,"TERM.%ld",TermID);
  4579.     else
  4580.         strcpy(TermIDString,"TERM");
  4581.  
  4582.     if(RexxPortName[0])
  4583.     {
  4584.         WORD i;
  4585.  
  4586.         for(i = 0 ; i < strlen(RexxPortName) ; i++)
  4587.             RexxPortName[i] = ToUpper(RexxPortName[i]);
  4588.  
  4589.         if(FindPort(RexxPortName))
  4590.             RexxPortName[0] = 0;
  4591.     }
  4592.  
  4593.     if(!RexxPortName[0])
  4594.         strcpy(RexxPortName,TermIDString);
  4595.  
  4596.         /* Install the hotkey handler. */
  4597.  
  4598.     SetupCx();
  4599.  
  4600.         /* Allocate the first few lines for the display buffer. */
  4601.  
  4602.     if(!CreateBuffer())
  4603.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_VIEW_BUFFER_TXT));
  4604.  
  4605.     if(!(XprIO = (struct XPR_IO *)AllocVec(sizeof(struct XPR_IO),MEMF_ANY|MEMF_CLEAR)))
  4606.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_PROTOCOL_BUFFER_TXT));
  4607.  
  4608.         /* Set up the external emulation macro data. */
  4609.  
  4610.     if(!(XEM_MacroKeys = (struct XEmulatorMacroKey *)AllocVecPooled((2 + 10 * 4) * sizeof(struct XEmulatorMacroKey),MEMF_ANY|MEMF_CLEAR)))
  4611.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACRO_KEY_DATA_TXT));
  4612.  
  4613.     strcpy(LastXprLibrary,Config -> TransferConfig -> DefaultLibrary);
  4614.  
  4615.     ProtocolSetup(FALSE);
  4616.  
  4617.         /* Load a keymap file if required. */
  4618.  
  4619.     if(Config -> TerminalConfig -> KeyMapFileName[0])
  4620.         KeyMap = LoadKeyMap(Config -> TerminalConfig -> KeyMapFileName);
  4621.  
  4622.     if(!(TermRexxPort = (struct MsgPort *)CreateMsgPort()))
  4623.         return(LocaleString(MSG_GLOBAL_FAILED_TO_CREATE_MSGPORT_TXT));
  4624.  
  4625.         /* If rexxsyslib.library opens cleanly it's time for
  4626.          * us to create the background term Rexx server.
  4627.          */
  4628.  
  4629.     if(RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME,0))
  4630.     {
  4631.             /* Create a background process handling the
  4632.              * rexx messages asynchronously.
  4633.              */
  4634.  
  4635.         Forbid();
  4636.  
  4637.         if(RexxProcess = (struct Process *)CreateNewProcTags(
  4638.             NP_Entry,    RexxServer,
  4639.             NP_Name,    "term Rexx Process",
  4640.             NP_Priority,    5,
  4641.             NP_StackSize,    8192,
  4642.             NP_WindowPtr,    -1,
  4643.         TAG_END))
  4644.         {
  4645.             ClrSignal(SIG_HANDSHAKE);
  4646.  
  4647.             Wait(SIG_HANDSHAKE);
  4648.         }
  4649.  
  4650.         Permit();
  4651.  
  4652.         if(!RexxProcess)
  4653.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_AREXX_PROCESS_TXT));
  4654.     }
  4655.  
  4656.         /* Install the public screen name, assumes that the user
  4657.          * wants the window to be opened on the screen, rather than
  4658.          * opening a custom screen.
  4659.          */
  4660.  
  4661.     if(SomePubScreenName[0])
  4662.     {
  4663.         strcpy(Config -> ScreenConfig -> PubScreenName,SomePubScreenName);
  4664.  
  4665.         Config -> ScreenConfig -> Blinking    = FALSE;
  4666.         Config -> ScreenConfig -> FasterLayout    = FALSE;
  4667.         Config -> ScreenConfig -> UseWorkbench    = TRUE;
  4668.  
  4669.         SomePubScreenName[0] = 0;
  4670.     }
  4671.  
  4672.     CreateQueueProcess();
  4673.  
  4674.     Forbid();
  4675.  
  4676.     RendezvousSemaphore . rs_Semaphore . ss_Link . ln_Name = RexxPortName;
  4677.     RendezvousSemaphore . rs_Semaphore . ss_Link . ln_Pri  = -127;
  4678.  
  4679.     AddSemaphore(&RendezvousSemaphore);
  4680.  
  4681.     Permit();
  4682.  
  4683.     if(DoIconify)
  4684.         return(NULL);
  4685.     else
  4686.     {
  4687.             /* Create the whole display. */
  4688.  
  4689.         if(Result = CreateDisplay(TRUE))
  4690.             return(Result);
  4691.         else
  4692.         {
  4693.             PubScreenStuff();
  4694.  
  4695.             return(NULL);
  4696.         }
  4697.     }
  4698. }
  4699.