home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d832 / term.lha / Term / term-3.1-Source.lha / termInit.c < prev    next >
C/C++ Source or Header  |  1993-02-20  |  74KB  |  3,416 lines

  1. /*
  2. **    termInit.c
  3. **
  4. **    Program initialization and shutdown routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* A couple of private strings which are to `impersonate' the
  13.      * control sequences associated with the four function keys.
  14.      */
  15.  
  16. STATIC STRPTR FunctionKeyCodes[4] =
  17. {
  18.     "\\eOP",
  19.     "\\eOQ",
  20.     "\\eOR",
  21.     "\\eOS"
  22. };
  23.  
  24.     /* This variable helps us to remember whether the fast!
  25.      * macro panel was open or not.
  26.      */
  27.  
  28. STATIC BYTE HadFastMacros = FALSE;
  29.  
  30.     /* Remember whether we did pen allocation or not. */
  31.  
  32. STATIC BYTE AllocatedPens = FALSE;
  33.  
  34.     /* DeleteInterleavedBitMap():
  35.      *
  36.      *    Delete an interleaved bitmap as allocated by
  37.      *    CreateInterleavedBitMap().
  38.      */
  39.  
  40. STATIC VOID __regargs
  41. DeleteInterleavedBitMap(struct BitMap *SomeBitMap)
  42. {
  43.     FreeVec(SomeBitMap -> Planes[0]);
  44.  
  45.     FreeVec(SomeBitMap);
  46. }
  47.  
  48.     /* CreateInterleavedBitMap():
  49.      *
  50.      *    With special thanks to Leo Schwab, this routine will create an
  51.      *    interleaved BitMap structure suitable for optimized blitter
  52.      *    access.
  53.      */
  54.  
  55. STATIC struct BitMap * __regargs
  56. CreateInterleavedBitMap(LONG Width,LONG Height,WORD Depth)
  57. {
  58.         /* A single plane BitMap cannot be interleaved. */
  59.  
  60.     if(Depth > 1)
  61.     {
  62.         struct BitMap *SomeBitMap;
  63.  
  64.             /* Allocate space for the bitmap structure. */
  65.  
  66.         if(SomeBitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_ANY))
  67.         {
  68.                 /* Initialize with standard values, so we can check
  69.                  * whether the current system will be able to handle
  70.                  * an interleaved bitmap of the expected size.
  71.                  */
  72.  
  73.             InitBitMap(SomeBitMap,Depth,Width,Height);
  74.  
  75.                 /* Check for old standard blitter limits. */
  76.  
  77.             if(Height * Depth > 1024 || SomeBitMap -> BytesPerRow * Depth > 126)
  78.             {
  79.                     /* The current space requirements will operate
  80.                      * correctly only on a system equipped with a
  81.                      * Fat Agnus (or successor) chip, let's see
  82.                      * if we can find one.
  83.                      */
  84.  
  85.                 if(GfxBase -> ChipRevBits0 & GFXF_BIG_BLITS)
  86.                 {
  87.                         /* Unlikely, put still not impossible: check for
  88.                          * Fat Agnus size limits...
  89.                          */
  90.  
  91.                     if(Height * Depth > 32768 || SomeBitMap -> BytesPerRow * Depth > 4096)
  92.                     {
  93.                         FreeVec(SomeBitMap);
  94.  
  95.                         return(NULL);
  96.                     }
  97.                 }
  98.                 else
  99.                 {
  100.                         /* Looks like a Big or old (A1000)
  101.                          * Agnus chip.
  102.                          */
  103.  
  104.                     FreeVec(SomeBitMap);
  105.  
  106.                     return(NULL);
  107.                 }
  108.             }
  109.  
  110.                 /* Initialize to interleaved BitMap values. */
  111.  
  112.             InitBitMap(SomeBitMap,1,Width,Height * Depth);
  113.  
  114.                 /* Allocate plane data. */
  115.  
  116.             if(SomeBitMap -> Planes[0] = (PLANEPTR)AllocVec(SomeBitMap -> BytesPerRow * SomeBitMap -> Rows,MEMF_CHIP))
  117.             {
  118.                 PLANEPTR    Base;
  119.                 WORD        i,
  120.                         Size;
  121.  
  122.                     /* Remember previous data. */
  123.  
  124.                 Base = SomeBitMap -> Planes[0];
  125.                 Size = SomeBitMap -> BytesPerRow;
  126.  
  127.                     /* Clear the bitmap. */
  128.  
  129.                 BltBitMap(SomeBitMap,0,0,SomeBitMap,0,0,Width,Height,0,~0,NULL);
  130.  
  131.                     /* Reinitialize. */
  132.  
  133.                 InitBitMap(SomeBitMap,Depth,Width,Height);
  134.  
  135.                     /* Modify for interleaved look. */
  136.  
  137.                 SomeBitMap -> BytesPerRow *= Depth;
  138.  
  139.                     /* Initialize the single planes. */
  140.  
  141.                 for(i = 0 ; i < Depth ; i++)
  142.                 {
  143.                     SomeBitMap -> Planes[i] = Base;
  144.  
  145.                     Base += Size;
  146.                 }
  147.  
  148.                     /* Return the ready bitmap. */
  149.  
  150.                 return(SomeBitMap);
  151.             }
  152.  
  153.                 /* Deallocate memory. */
  154.  
  155.             FreeVec(SomeBitMap);
  156.         }
  157.     }
  158.  
  159.         /* Return failure. */
  160.  
  161.     return(NULL);
  162. }
  163.  
  164.     /* LoadKeyMap(STRPTR Name):
  165.      *
  166.      *    Load a keymap file from disk.
  167.      */
  168.  
  169. STATIC struct KeyMap * __regargs
  170. LoadKeyMap(STRPTR Name)
  171. {
  172.     struct KeyMapResource    *KeyMapResource;
  173.     struct KeyMap        *Map = NULL;
  174.  
  175.         /* Try to get access to the list of currently loaded
  176.          * keymap files.
  177.          */
  178.  
  179.     if(KeyMapResource = (struct KeyMapResource *)OpenResource("keymap.resource"))
  180.     {
  181.         struct KeyMapNode *Node;
  182.  
  183.             /* Try to find the keymap in the list. */
  184.  
  185.         Forbid();
  186.  
  187.         if(Node = (struct KeyMapNode *)FindName(&KeyMapResource -> kr_List,FilePart(Config -> TerminalConfig -> KeyMapFileName)))
  188.             Map = &Node -> kn_KeyMap;
  189.  
  190.         Permit();
  191.     }
  192.  
  193.         /* Still no keymap available? */
  194.  
  195.     if(!Map)
  196.     {
  197.         APTR OldPtr = ThisProcess -> pr_WindowPtr;
  198.  
  199.             /* Disable DOS requesters. */
  200.  
  201.         ThisProcess -> pr_WindowPtr = (APTR)-1;
  202.  
  203.             /* Unload the old keymap code. */
  204.  
  205.         if(KeySegment)
  206.             UnLoadSeg(KeySegment);
  207.  
  208.             /* Try to load the keymap from the
  209.              * name the user entered.
  210.              */
  211.  
  212.         if(!(KeySegment = LoadSeg(Config -> TerminalConfig -> KeyMapFileName)))
  213.         {
  214.                 /* Second try: load it from
  215.                   * the standard keymaps drawer.
  216.                   */
  217.  
  218.             strcpy(SharedBuffer,"KEYMAPS:");
  219.  
  220.             if(AddPart(SharedBuffer,FilePart(Config -> TerminalConfig -> KeyMapFileName),MAX_FILENAME_LENGTH))
  221.             {
  222.                 if(!(KeySegment = LoadSeg(SharedBuffer)))
  223.                 {
  224.                     strcpy(SharedBuffer,"Devs:Keymaps");
  225.  
  226.                     if(AddPart(SharedBuffer,FilePart(Config -> TerminalConfig -> KeyMapFileName),MAX_FILENAME_LENGTH))
  227.                         KeySegment = LoadSeg(SharedBuffer);
  228.                 }
  229.             }
  230.         }
  231.  
  232.             /* Did we get the keymap file? */
  233.  
  234.         if(KeySegment)
  235.         {
  236.             struct KeyMapNode *Node = (struct KeyMapNode *)&((ULONG *)BADDR(KeySegment))[1];
  237.  
  238.             Map = &Node -> kn_KeyMap;
  239.         }
  240.  
  241.             /* Enable DOS requesters again. */
  242.  
  243.         ThisProcess -> pr_WindowPtr = OldPtr;
  244.     }
  245.     else
  246.     {
  247.         if(KeySegment)
  248.         {
  249.             UnLoadSeg(KeySegment);
  250.  
  251.             KeySegment = NULL;
  252.         }
  253.     }
  254.  
  255.     return(Map);
  256. }
  257.  
  258.     /* DeleteOffsetTables(VOID):
  259.      *
  260.      *    Delete the line multiplication tables.
  261.      */
  262.  
  263. STATIC VOID
  264. DeleteOffsetTables(VOID)
  265. {
  266.     if(OffsetXTable)
  267.     {
  268.         FreeVec(OffsetXTable);
  269.  
  270.         OffsetXTable = NULL;
  271.     }
  272.  
  273.     if(OffsetYTable)
  274.     {
  275.         FreeVec(OffsetYTable);
  276.  
  277.         OffsetYTable = NULL;
  278.     }
  279. }
  280.  
  281.     /* CreateOffsetTables(VOID):
  282.      *
  283.      *    Allocate the line multiplication tables.
  284.      */
  285.  
  286. STATIC BYTE
  287. CreateOffsetTables(VOID)
  288. {
  289.     LONG    Width    = (Window -> WScreen -> Width  + TextFontWidth)  * 2 / TextFontWidth,
  290.         Height    = (Window -> WScreen -> Height + TextFontHeight) * 2 / TextFontHeight;
  291.  
  292.     DeleteOffsetTables();
  293.  
  294.     if(OffsetXTable = (LONG *)AllocVec(Width * sizeof(LONG),MEMF_ANY))
  295.     {
  296.         if(OffsetYTable = (LONG *)AllocVec(Height * sizeof(LONG),MEMF_ANY))
  297.         {
  298.             LONG i,j;
  299.  
  300.             for(i = j = 0 ; i < Width ; i++, j += TextFontWidth)
  301.                 OffsetXTable[i] = j;
  302.  
  303.             for(i = j = 0 ; i < Height ; i++, j += TextFontHeight)
  304.                 OffsetYTable[i] = j;
  305.  
  306.             return(TRUE);
  307.         }
  308.     }
  309.  
  310.     DeleteOffsetTables();
  311.  
  312.     return(FALSE);
  313. }
  314.  
  315.     /* ResetCursorKeys(struct CursorKeys *Keys):
  316.      *
  317.      *    Reset cursor key assignments to defaults.
  318.      */
  319.  
  320. VOID
  321. ResetCursorKeys(struct CursorKeys *Keys)
  322. {
  323.     STATIC STRPTR Defaults[4] =
  324.     {
  325.         "\\e[A",
  326.         "\\e[B",
  327.         "\\e[C",
  328.         "\\e[D"
  329.     };
  330.  
  331.     WORD i,j;
  332.  
  333.     for(i = 0 ; i < 4 ; i++)
  334.     {
  335.         for(j = 0 ; j < 4 ; j++)
  336.             strcpy(Keys -> Keys[j][i],Defaults[i]);
  337.     }
  338. }
  339.  
  340.     /* ScreenSizeStuff():
  341.      *
  342.      *    Set up the terminal screen size.
  343.      */
  344.  
  345. VOID
  346. ScreenSizeStuff()
  347. {
  348.         /* Is this really the built-in emulation? */
  349.  
  350.     if(Config -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL)
  351.     {
  352.         LONG    MaxColumns    = WindowWidth / TextFontWidth,
  353.             MaxLines    = WindowHeight / TextFontHeight,
  354.             Columns,
  355.             Lines;
  356.  
  357.             /* Turn off the cursor. */
  358.  
  359.         ClearCursor();
  360.  
  361.             /* Set up the new screen width. */
  362.  
  363.         if(Config -> TerminalConfig -> NumColumns < 20)
  364.             Columns = MaxColumns;
  365.         else
  366.             Columns = Config -> TerminalConfig -> NumColumns;
  367.  
  368.             /* Set up the new screen height. */
  369.  
  370.         if(Config -> TerminalConfig -> NumLines < 20)
  371.             Lines = MaxLines;
  372.         else
  373.             Lines = Config -> TerminalConfig -> NumLines;
  374.  
  375.             /* More columns than we will be able to display? */
  376.  
  377.         if(Columns > MaxColumns)
  378.             Columns = MaxColumns;
  379.  
  380.             /* More lines than we will be able to display? */
  381.  
  382.         if(Lines > MaxLines)
  383.             Lines = MaxLines;
  384.  
  385.             /* Set up the central data. */
  386.  
  387.         LastColumn    = Columns - 1;
  388.         LastLine    = Lines - 1;
  389.         LastPixel    = MUL_X(Columns) - 1;
  390.  
  391.             /* Are we to clear the margin? */
  392.  
  393.         if(Columns < MaxColumns || Lines < MaxLines)
  394.         {
  395.                 /* Save the rendering attributes. */
  396.  
  397.             BackupRender();
  398.  
  399.                 /* Set the defaults. */
  400.  
  401.             SetAPen(RPort,MappedPens[0][0]);
  402.  
  403.             SetWrMsk(RPort,DepthMask);
  404.  
  405.                 /* Clear remaining columns. */
  406.  
  407.             if(Columns < MaxColumns)
  408.                 ScrollLineRectFill(RPort,MUL_X(LastColumn + 1),0,WindowWidth - 1,WindowHeight - 1);
  409.  
  410.                 /* Clear remaining lines. */
  411.  
  412.             if(Lines < MaxLines)
  413.                 ScrollLineRectFill(RPort,0,MUL_Y(LastLine + 1),WindowWidth - 1,WindowHeight - 1);
  414.  
  415.                 /* Restore rendering attributes. */
  416.  
  417.             BackupRender();
  418.         }
  419.  
  420.             /* Truncate illegal cursor position. */
  421.  
  422.         if(CursorY > LastLine)
  423.             CursorY = LastLine;
  424.  
  425.         ConFontScaleUpdate();
  426.  
  427.             /* Truncate illegal cursor position. */
  428.  
  429.         if(CursorX > LastColumn)
  430.             CursorX = LastColumn;
  431.  
  432.             /* Reset the cursor position. */
  433.  
  434.         if(Config -> EmulationConfig -> FontScale == SCALE_HALF)
  435.             CursorX *= 2;
  436.         else
  437.         {
  438.             if(PrivateConfig -> EmulationConfig -> FontScale == SCALE_HALF)
  439.                 CursorX /= 2;
  440.         }
  441.  
  442.             /* Fix scroll region button. */
  443.  
  444.         if(!RegionSet)
  445.             Bottom = LastLine;
  446.  
  447.             /* Turn the cursor back on. */
  448.  
  449.         DrawCursor();
  450.     }
  451.  
  452.     FixScreenSize = FALSE;
  453. }
  454.  
  455.     /* PubScreenStuff():
  456.      *
  457.      *    This part handles the public screen setup stuff.
  458.      */
  459.  
  460. VOID
  461. PubScreenStuff()
  462. {
  463.     if(Screen)
  464.     {
  465.             /* Are we to make our screen public? */
  466.  
  467.         if(Config -> ScreenConfig -> MakeScreenPublic)
  468.             PubScreenStatus(Screen,NULL);
  469.         else
  470.             PubScreenStatus(Screen,PSNF_PRIVATE);
  471.  
  472.             /* Are we to `shanghai' Workbench windows? */
  473.  
  474.         if(Config -> ScreenConfig -> ShanghaiWindows)
  475.         {
  476.             PublicModes |= SHANGHAI;
  477.  
  478.             SetPubScreenModes(PublicModes);
  479.  
  480.                 /* Make this the default public screen. */
  481.  
  482.             SetDefaultPubScreen(TermIDString);
  483.         }
  484.         else
  485.         {
  486.             PublicModes &= ~SHANGHAI;
  487.  
  488.             if(LockPubScreen(DefaultPubScreenName))
  489.             {
  490.                 SetDefaultPubScreen(DefaultPubScreenName);
  491.  
  492.                 UnlockPubScreen(DefaultPubScreenName,NULL);
  493.             }
  494.             else
  495.                 SetDefaultPubScreen(NULL);
  496.  
  497.             SetPubScreenModes(PublicModes);
  498.         }
  499.     }
  500.  
  501.     FixPubScreenMode = FALSE;
  502. }
  503.  
  504.     /* ConfigSetup():
  505.      *
  506.      *    Compare the current configuration with the
  507.      *    last backup and reset the serial device, terminal,
  508.      *    etc. if necessary.
  509.      */
  510.  
  511. VOID
  512. ConfigSetup()
  513. {
  514.     BYTE RasterWasEnabled = RasterEnabled;
  515.  
  516.         /* First we will take a look at the configuration
  517.          * and try to find those parts which have changed
  518.          * and require the main screen display to be
  519.          * reopened.
  520.          */
  521.  
  522.     if(PrivateConfig -> ScreenConfig -> FontHeight != Config -> ScreenConfig -> FontHeight)
  523.         ResetDisplay = TRUE;
  524.  
  525.     if(Stricmp(PrivateConfig -> ScreenConfig -> FontName,Config -> ScreenConfig -> FontName))
  526.         ResetDisplay = TRUE;
  527.  
  528.     if(PrivateConfig -> TerminalConfig -> FontMode != Config -> TerminalConfig -> FontMode)
  529.         ResetDisplay = TRUE;
  530.  
  531.     if(PrivateConfig -> TerminalConfig -> TextFontHeight != Config -> TerminalConfig -> TextFontHeight)
  532.         ResetDisplay = TRUE;
  533.  
  534.     if(Stricmp(PrivateConfig -> TerminalConfig -> TextFontName,Config -> TerminalConfig -> TextFontName))
  535.         ResetDisplay = TRUE;
  536.  
  537.     if(PrivateConfig -> ScreenConfig -> DisplayMode != Config -> ScreenConfig -> DisplayMode || PrivateConfig -> ScreenConfig -> ColourMode != Config -> ScreenConfig -> ColourMode)
  538.         ResetDisplay = TRUE;
  539.  
  540.     if(PrivateConfig -> ScreenConfig -> ColourMode == COLOUR_EIGHT && Config -> ScreenConfig -> ColourMode == COLOUR_EIGHT)
  541.     {
  542.         if(PrivateConfig -> ScreenConfig -> Blinking != Config -> ScreenConfig -> Blinking)
  543.             ResetDisplay = TRUE;
  544.     }
  545.  
  546.     if(PrivateConfig -> ScreenConfig -> FasterLayout != Config -> ScreenConfig -> FasterLayout)
  547.         ResetDisplay = TRUE;
  548.  
  549.     if(PrivateConfig -> ScreenConfig -> StatusLine != Config -> ScreenConfig -> StatusLine)
  550.         ResetDisplay = TRUE;
  551.  
  552.     if(PrivateConfig -> ScreenConfig -> TitleBar != Config -> ScreenConfig -> TitleBar)
  553.         ResetDisplay = TRUE;
  554.  
  555.     if(PrivateConfig -> ScreenConfig -> UseWorkbench != Config -> ScreenConfig -> UseWorkbench)
  556.         ResetDisplay = TRUE;
  557.  
  558.     if(strcmp(PrivateConfig -> ScreenConfig -> PubScreenName,Config -> ScreenConfig -> PubScreenName) && Config -> ScreenConfig -> UseWorkbench)
  559.         ResetDisplay = TRUE;
  560.  
  561.         /* Now for the `harmless' actions which do not
  562.          * require to change the screen or other
  563.          * rendering data.
  564.          */
  565.  
  566.     if(PrivateConfig -> TerminalConfig -> NumColumns != Config -> TerminalConfig -> NumColumns || PrivateConfig -> TerminalConfig -> NumLines != Config -> TerminalConfig -> NumLines)
  567.         FixScreenSize = TRUE;
  568.  
  569.     if(PrivateConfig -> ScreenConfig -> MakeScreenPublic != Config -> ScreenConfig -> MakeScreenPublic || PrivateConfig -> ScreenConfig -> ShanghaiWindows != Config -> ScreenConfig -> ShanghaiWindows)
  570.         PubScreenStuff();
  571.  
  572.     if(PrivateConfig -> ScreenConfig -> ColourMode == Config -> ScreenConfig -> ColourMode && memcmp(PrivateConfig -> ScreenConfig -> Colours,Config -> ScreenConfig -> Colours,sizeof(UWORD) * 16))
  573.     {
  574.         switch(Config -> ScreenConfig -> ColourMode)
  575.         {
  576.             case COLOUR_EIGHT:
  577.  
  578.                 CopyMem(Config -> ScreenConfig -> Colours,ANSIColours,16 * sizeof(UWORD));
  579.                 break;
  580.  
  581.             case COLOUR_SIXTEEN:
  582.  
  583.                 CopyMem(Config -> ScreenConfig -> Colours,EGAColours,16 * sizeof(UWORD));
  584.                 break;
  585.  
  586.             case COLOUR_AMIGA:
  587.  
  588.                 CopyMem(Config -> ScreenConfig -> Colours,DefaultColours,16 * sizeof(UWORD));
  589.                 break;
  590.  
  591.             case COLOUR_MONO:
  592.  
  593.                 CopyMem(Config -> ScreenConfig -> Colours,AtomicColours,16 * sizeof(UWORD));
  594.                 break;
  595.         }
  596.     }
  597.  
  598.         /* Are we to load a new transfer library? */
  599.  
  600.     if(Config -> FileConfig -> ProtocolFileName[0] && strcmp(PrivateConfig -> FileConfig -> ProtocolFileName,Config -> FileConfig -> ProtocolFileName))
  601.     {
  602.         strcpy(LastXprLibrary,Config -> FileConfig -> ProtocolFileName);
  603.  
  604.         ProtocolSetup();
  605.     }
  606.  
  607.         /* No custom keymap this time? */
  608.  
  609.     if(!Config -> TerminalConfig -> KeyMapFileName[0])
  610.     {
  611.         KeyMap = NULL;
  612.  
  613.         if(KeySegment)
  614.         {
  615.             UnLoadSeg(KeySegment);
  616.  
  617.             KeySegment = NULL;
  618.         }
  619.     }
  620.     else
  621.     {
  622.             /* Check whether the keymap name has changed. */
  623.  
  624.         if(strcmp(PrivateConfig -> TerminalConfig -> KeyMapFileName,Config -> TerminalConfig -> KeyMapFileName))
  625.             KeyMap = LoadKeyMap(Config -> TerminalConfig -> KeyMapFileName);
  626.     }
  627.  
  628.         /* Are we to load the keyboard macro settings? */
  629.  
  630.     if(Config -> FileConfig -> MacroFileName[0] && Stricmp(PrivateConfig -> FileConfig -> MacroFileName,Config -> FileConfig -> MacroFileName))
  631.     {
  632.         if(!LoadMacros(Config -> FileConfig -> MacroFileName,MacroKeys))
  633.         {
  634.             WORD i,j;
  635.  
  636.             for(j = 0 ; j < 4 ; j++)
  637.             {
  638.                 for(i = 0 ; i < 10 ; i++)
  639.                     MacroKeys -> Keys[j][i][0] = 0;
  640.             }
  641.  
  642.             for(i = 0 ; i < 4 ; i++)
  643.                 strcpy(MacroKeys -> Keys[1][i],FunctionKeyCodes[i]);
  644.         }
  645.         else
  646.             strcpy(LastMacros,Config -> FileConfig -> MacroFileName);
  647.     }
  648.  
  649.         /* Are we to load the cursor key settings? */
  650.  
  651.     if(Config -> FileConfig -> CursorFileName[0] && Stricmp(PrivateConfig -> FileConfig -> CursorFileName,Config -> FileConfig -> CursorFileName))
  652.     {
  653.         if(!ReadIFFData(Config -> FileConfig -> CursorFileName,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  654.             ResetCursorKeys(CursorKeys);
  655.         else
  656.             strcpy(LastCursorKeys,Config -> FileConfig -> CursorFileName);
  657.     }
  658.  
  659.         /* Are we to load the translation tables? */
  660.  
  661.     if(Config -> FileConfig -> TranslationFileName[0] && Stricmp(PrivateConfig -> FileConfig -> TranslationFileName,Config -> FileConfig -> TranslationFileName))
  662.     {
  663.         if(SendTable)
  664.         {
  665.             FreeTranslationTable(SendTable);
  666.  
  667.             SendTable = NULL;
  668.         }
  669.  
  670.         if(ReceiveTable)
  671.         {
  672.             FreeTranslationTable(ReceiveTable);
  673.  
  674.             ReceiveTable = NULL;
  675.         }
  676.  
  677.         if(SendTable = AllocTranslationTable())
  678.         {
  679.             if(ReceiveTable = AllocTranslationTable())
  680.             {
  681.                 if(LoadTranslationTables(Config -> FileConfig -> TranslationFileName,SendTable,ReceiveTable))
  682.                 {
  683.                     strcpy(LastTranslation,Config -> FileConfig -> TranslationFileName);
  684.  
  685.                     if(IsStandardTable(SendTable) && IsStandardTable(ReceiveTable))
  686.                     {
  687.                         FreeTranslationTable(SendTable);
  688.  
  689.                         SendTable = NULL;
  690.  
  691.                         FreeTranslationTable(ReceiveTable);
  692.  
  693.                         ReceiveTable = NULL;
  694.                     }
  695.                 }
  696.                 else
  697.                 {
  698.                     FreeTranslationTable(SendTable);
  699.  
  700.                     SendTable = NULL;
  701.  
  702.                     FreeTranslationTable(ReceiveTable);
  703.  
  704.                     ReceiveTable = NULL;
  705.                 }
  706.             }
  707.             else
  708.             {
  709.                 FreeTranslationTable(SendTable);
  710.  
  711.                 SendTable = NULL;
  712.             }
  713.         }
  714.     }
  715.  
  716.         /* Are we to load the fast macro settings? */
  717.  
  718.     if(Config -> FileConfig -> FastMacroFileName[0] && Stricmp(PrivateConfig -> FileConfig -> FastMacroFileName,Config -> FileConfig -> FastMacroFileName))
  719.     {
  720.         if(LoadFastMacros(Config -> FileConfig -> FastMacroFileName))
  721.             strcpy(LastFastMacros,Config -> FileConfig -> FastMacroFileName);
  722.     }
  723.  
  724.         /* Are we to load or reset the default beep sound? */
  725.  
  726.     if(Stricmp(Config -> TerminalConfig -> BeepFileName,PrivateConfig -> TerminalConfig -> BeepFileName))
  727.     {
  728.         DeleteBeep();
  729.  
  730.         if(Config -> TerminalConfig -> BeepFileName[0])
  731.         {
  732.             if(!OpenSound(Config -> TerminalConfig -> BeepFileName))
  733.                 MyEasyRequest(Window,LocaleString(MSG_TERMMAIN_FAILED_TO_OPEN_SOUND_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Config -> TerminalConfig -> BeepFileName);
  734.         }
  735.  
  736.         CreateBeep();
  737.     }
  738.  
  739.         /* Serial configuration needs updating? */
  740.  
  741.     ReconfigureSerial(Window,NULL);
  742.  
  743.         /* Are we to open the fast macro panel? */
  744.  
  745.     if(Config -> MiscConfig -> OpenFastMacroPanel)
  746.         HadFastMacros = TRUE;
  747.  
  748.         /* Are we to freeze the text buffer? */
  749.  
  750.     if(!Config -> CaptureConfig -> BufferEnabled)
  751.         BufferFrozen = TRUE;
  752.  
  753.         /* Now for the actions which require that the
  754.          * screen stays open.
  755.          */
  756.  
  757.     if(!ResetDisplay)
  758.     {
  759.         if(Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  760.         {
  761.             if(PrivateConfig -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL || (Config -> TerminalConfig -> EmulationFileName[0] && strcmp(PrivateConfig -> TerminalConfig -> EmulationFileName,Config -> TerminalConfig -> EmulationFileName)))
  762.             {
  763.                 if(!OpenEmulator(Config -> TerminalConfig -> EmulationFileName))
  764.                 {
  765.                     Config -> TerminalConfig -> EmulationMode = EMULATION_ANSIVT100;
  766.  
  767.                     ResetDisplay = TRUE;
  768.  
  769.                     RasterEnabled = TRUE;
  770.  
  771.                     MyEasyRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Config -> TerminalConfig -> EmulationFileName);
  772.                 }
  773.                 else
  774.                     RasterEnabled = FALSE;
  775.             }
  776.         }
  777.         else
  778.         {
  779.             if(XEmulatorBase && PrivateConfig -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  780.             {
  781.                 XEmulatorClearConsole(XEM_IO);
  782.  
  783.                 CloseEmulator();
  784.  
  785.                 RasterEnabled = TRUE;
  786.  
  787.                 ClearCursor();
  788.  
  789.                 Reset();
  790.  
  791.                 DrawCursor();
  792.             }
  793.             else
  794.                 RasterEnabled = TRUE;
  795.         }
  796.  
  797.         if(RasterEnabled != RasterWasEnabled)
  798.             RasterEraseScreen(2);
  799.  
  800.         if(!Config -> ScreenConfig -> UseWorkbench)
  801.         {
  802.             if(memcmp(PrivateConfig -> ScreenConfig -> Colours,Config -> ScreenConfig -> Colours,sizeof(UWORD) * 16))
  803.             {
  804.                 WORD i;
  805.  
  806.                 for(i = 0 ; i < 16 ; i++)
  807.                     BlinkColours[i] = Config -> ScreenConfig -> Colours[i];
  808.  
  809.                 LoadRGB4(VPort,Config -> ScreenConfig -> Colours,DepthMask + 1);
  810.  
  811.                 switch(Config -> ScreenConfig -> ColourMode)
  812.                 {
  813.                     case COLOUR_EIGHT:
  814.  
  815.                         for(i = 0 ; i < 8 ; i++)
  816.                             BlinkColours[i + 8] = BlinkColours[0];
  817.  
  818.                         break;
  819.  
  820.                     case COLOUR_AMIGA:
  821.  
  822.                         BlinkColours[3] = BlinkColours[0];
  823.                         break;
  824.                 }
  825.             }
  826.             else
  827.             {
  828.                 if(!Config -> ScreenConfig -> Blinking)
  829.                     LoadRGB4(VPort,Config -> ScreenConfig -> Colours,16);
  830.             }
  831.         }
  832.  
  833.         if(Config -> MiscConfig -> OpenFastMacroPanel && !FastWindow)
  834.             OpenFastWindow();
  835.  
  836.         PubScreenStuff();
  837.  
  838.         if(Menu)
  839.         {
  840.             CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  841.  
  842.             if(!XProtocolBase)
  843.                 SetTransferMenu(FALSE);
  844.             else
  845.                 SetTransferMenu(TRUE);
  846.  
  847.             SetRasterMenu(RasterEnabled);
  848.         }
  849.  
  850.         Blocking = FALSE;
  851.     }
  852.     else
  853.     {
  854.             /* Are we no longer to use the external emulator? */
  855.  
  856.         if(Config -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL)
  857.         {
  858.             if(XEmulatorBase && PrivateConfig -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  859.             {
  860.                 XEmulatorClearConsole(XEM_IO);
  861.  
  862.                 CloseEmulator();
  863.             }
  864.         }
  865.  
  866.         RasterEnabled = TRUE;
  867.     }
  868.  
  869.         /* Change the task priority. */
  870.  
  871.     SetTaskPri(ThisProcess,(LONG)Config -> MiscConfig -> Priority);
  872.  
  873.     ConOutputUpdate();
  874.  
  875.     ConFontScaleUpdate();
  876.  
  877.         /* Choose the right console data processing routine. */
  878.  
  879.     if(XEmulatorBase && Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  880.     {
  881.         if(ReceiveTable)
  882.             ConProcessData = ConProcessDataTransExternal;
  883.         else
  884.             ConProcessData = ConProcessDataExternal;
  885.     }
  886.     else
  887.     {
  888.         if(Config -> SerialConfig -> StripBit8)
  889.             ConProcessData = ConProcessData7;
  890.         else
  891.             ConProcessData = ConProcessData8;
  892.     }
  893.  
  894.         /* Reset the scanner. */
  895.  
  896.     FlowInit(TRUE);
  897. }
  898.  
  899.     /* DisplayReset():
  900.      *
  901.      *    Reset the entire display if necessary.
  902.      */
  903.  
  904. BYTE
  905. DisplayReset()
  906. {
  907.     UBYTE    *Result;
  908.     BYTE     Success = TRUE;
  909.  
  910.         /* Delete the display (if possible).
  911.          * This will go wrong if there
  912.          * are any visitor windows on our
  913.          * screen.
  914.          */
  915.  
  916.     if(DeleteDisplay())
  917.     {
  918.         if(Result = CreateDisplay(FALSE))
  919.         {
  920.             ThisProcess -> pr_WindowPtr = (APTR)Window;
  921.  
  922.             MyEasyRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Result);
  923.  
  924.             Success = FALSE;
  925.         }
  926.         else
  927.         {
  928.             BumpWindow(Window);
  929.  
  930.             PubScreenStuff();
  931.  
  932.             DisplayReopened = TRUE;
  933.         }
  934.     }
  935.     else
  936.     {
  937.         SaveConfig(PrivateConfig,Config);
  938.  
  939.         BlockWindows();
  940.  
  941.         MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),LocaleString(MSG_TERMMAIN_CANNOT_CLOSE_SCREEN_YET_TXT));
  942.  
  943.         ReleaseWindows();
  944.  
  945.         Success = TRUE;
  946.     }
  947.  
  948.     ResetDisplay = FALSE;
  949.  
  950.     return(Success);
  951. }
  952.  
  953.     /* DeleteDisplay():
  954.      *
  955.      *    Free all resources associated with the terminal
  956.      *    display (tasks, interrupts, screen, window, etc.).
  957.      */
  958.  
  959. BYTE
  960. DeleteDisplay()
  961. {
  962.     GuideCleanup();
  963.  
  964.     if(Screen)
  965.     {
  966.         struct List        *PubScreenList;
  967.         struct PubScreenNode    *ScreenNode;
  968.  
  969.         PubScreenList = LockPubScreenList();
  970.  
  971.         for(ScreenNode = (struct PubScreenNode *)PubScreenList -> lh_Head ; ScreenNode -> psn_Node . ln_Succ ; ScreenNode = (struct PubScreenNode *)ScreenNode -> psn_Node . ln_Succ)
  972.         {
  973.             if(ScreenNode -> psn_Screen == Screen)
  974.                 break;
  975.         }
  976.  
  977.         if(ScreenNode)
  978.         {
  979.             if(ScreenNode -> psn_VisitorCount)
  980.             {
  981.                 UnlockPubScreenList();
  982.  
  983.                 return(FALSE);
  984.             }
  985.             else
  986.             {
  987.                 Forbid();
  988.  
  989.                 UnlockPubScreenList();
  990.  
  991.                 PubScreenStatus(Screen,PSNF_PRIVATE);
  992.  
  993.                 Permit();
  994.             }
  995.         }
  996.         else
  997.             UnlockPubScreenList();
  998.     }
  999.  
  1000.     if(StatusProcess)
  1001.     {
  1002.         Forbid();
  1003.  
  1004.         Signal(StatusProcess,SIG_KILL);
  1005.  
  1006.         SetSignal(0,SIG_HANDSHAKE);
  1007.  
  1008.         Wait(SIG_HANDSHAKE);
  1009.  
  1010.         Permit();
  1011.     }
  1012.  
  1013.     if(Marking)
  1014.         FreeMarker();
  1015.  
  1016.     FirstClick    = TRUE;
  1017.     HoldClick    = FALSE;
  1018.  
  1019.     CloseInfoWindow();
  1020.  
  1021.     DeleteReview();
  1022.  
  1023.     if(Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL && XEmulatorBase)
  1024.         CloseEmulator();
  1025.  
  1026.     DeleteRaster();
  1027.  
  1028.     DeleteScale();
  1029.  
  1030.     if(TabStops)
  1031.     {
  1032.         FreeVec(TabStops);
  1033.  
  1034.         TabStops = NULL;
  1035.     }
  1036.  
  1037.     if(ScrollLines)
  1038.     {
  1039.         FreeVec(ScrollLines);
  1040.  
  1041.         ScrollLines = NULL;
  1042.     }
  1043.  
  1044.     if(Screen)
  1045.         ScreenToBack(Screen);
  1046.  
  1047.     if(FastWindow)
  1048.     {
  1049.         HadFastMacros = TRUE;
  1050.  
  1051.         CloseFastWindow();
  1052.     }
  1053.     else
  1054.         HadFastMacros = FALSE;
  1055.  
  1056.     if(StatusWindow)
  1057.     {
  1058.         ClearMenuStrip(StatusWindow);
  1059.         CloseWindowSafely(StatusWindow);
  1060.  
  1061.         StatusWindow = NULL;
  1062.     }
  1063.  
  1064.     if(DefaultPubScreen)
  1065.     {
  1066.         UnlockPubScreen(NULL,DefaultPubScreen);
  1067.  
  1068.         DefaultPubScreen = NULL;
  1069.     }
  1070.  
  1071.         /* Remove AppWindow link. */
  1072.  
  1073.     if(WorkbenchWindow)
  1074.     {
  1075.         RemoveAppWindow(WorkbenchWindow);
  1076.  
  1077.         WorkbenchWindow = NULL;
  1078.     }
  1079.  
  1080.         /* Remove AppWindow port and any pending messages. */
  1081.  
  1082.     if(WorkbenchPort)
  1083.     {
  1084.         struct Message *Message;
  1085.  
  1086.         while(Message = GetMsg(WorkbenchPort))
  1087.             ReplyMsg(Message);
  1088.  
  1089.         DeleteMsgPort(WorkbenchPort);
  1090.  
  1091.         WorkbenchPort = NULL;
  1092.     }
  1093.  
  1094.     if(DrawInfo)
  1095.     {
  1096.             /* Release the rendering pens. */
  1097.  
  1098.         FreeScreenDrawInfo(Window -> WScreen,DrawInfo);
  1099.  
  1100.         DrawInfo = NULL;
  1101.     }
  1102.  
  1103.     if(Window)
  1104.     {
  1105.         if(AllocatedPens && GfxBase -> LibNode . lib_Version >= 39)
  1106.         {
  1107.             WORD i;
  1108.  
  1109.                 /* Erase the window contents. We will
  1110.                  * want to release any pens we have
  1111.                  * allocated and want to avoid nasty
  1112.                  * flashing and flickering.
  1113.                  */
  1114.  
  1115.             SetAPen(RPort,0);
  1116.  
  1117.             RectFill(RPort,WindowLeft,WindowTop,WindowLeft + WindowWidth - 1,WindowTop + WindowHeight - 1);
  1118.  
  1119.                 /* Release any pens we have allocated. */
  1120.  
  1121.             for(i = 0 ; i < 16 ; i++)
  1122.             {
  1123.                 if(MappedPens[1][i])
  1124.                 {
  1125.                     ReleasePen(VPort -> ColorMap,i);
  1126.  
  1127.                     MappedPens[0][i] = i;
  1128.                     MappedPens[1][i] = FALSE;
  1129.                 }
  1130.             }
  1131.  
  1132.             AllocatedPens = FALSE;
  1133.         }
  1134.  
  1135.         ClearMenuStrip(Window);
  1136.  
  1137.         ThisProcess -> pr_WindowPtr = OldWindowPtr;
  1138.  
  1139.         PopWindow();
  1140.  
  1141.         if(TermPort)
  1142.             TermPort -> TopWindow = NULL;
  1143.  
  1144.         CloseWindow(Window);
  1145.  
  1146.         Window = NULL;
  1147.  
  1148.         if(StatusGadget)
  1149.             DeleteStatusGadget(StatusGadget);
  1150.  
  1151.         StatusGadget = NULL;
  1152.     }
  1153.  
  1154.     if(Menu)
  1155.     {
  1156.         FreeMenus(Menu);
  1157.  
  1158.         Menu = NULL;
  1159.     }
  1160.  
  1161.     if(VisualInfo)
  1162.     {
  1163.         FreeVisualInfo(VisualInfo);
  1164.  
  1165.         VisualInfo = NULL;
  1166.     }
  1167.  
  1168.     DeletePacketWindow(FALSE);
  1169.  
  1170.     if(UserTextFont)
  1171.     {
  1172.         CloseFont(UserTextFont);
  1173.  
  1174.         UserTextFont = NULL;
  1175.     }
  1176.  
  1177.         /* Before we can close screen we will have to
  1178.          * make sure that it is no longer the default
  1179.          * public screen.
  1180.          */
  1181.  
  1182.     if(Config -> ScreenConfig -> ShanghaiWindows)
  1183.     {
  1184.         if(LockPubScreen(DefaultPubScreenName))
  1185.         {
  1186.             SetDefaultPubScreen(DefaultPubScreenName);
  1187.  
  1188.             UnlockPubScreen(DefaultPubScreenName,NULL);
  1189.         }
  1190.         else
  1191.             SetDefaultPubScreen(NULL);
  1192.     }
  1193.  
  1194.     if(Screen)
  1195.     {
  1196.         CloseScreen(Screen);
  1197.  
  1198.         Screen = NULL;
  1199.     }
  1200.  
  1201.     if(InterleavedBitMap)
  1202.     {
  1203.         DeleteInterleavedBitMap(InterleavedBitMap);
  1204.  
  1205.         InterleavedBitMap = NULL;
  1206.     }
  1207.  
  1208.     if(GFX)
  1209.     {
  1210.         CloseFont(GFX);
  1211.  
  1212.         GFX = NULL;
  1213.     }
  1214.  
  1215.     if(TextFont)
  1216.     {
  1217.         CloseFont(TextFont);
  1218.  
  1219.         TextFont = NULL;
  1220.     }
  1221.  
  1222.     return(TRUE);
  1223. }
  1224.  
  1225.     /* CreateDisplay(BYTE FirstSetup):
  1226.      *
  1227.      *    Open the display and allocate associated data.
  1228.      */
  1229.  
  1230. STRPTR
  1231. CreateDisplay(BYTE FirstSetup)
  1232. {
  1233.     UWORD             Count = 0,i;
  1234.     LONG             ErrorCode,Top,Height;
  1235.     ULONG             TagArray[9];
  1236.     struct Rectangle     OverscanSize;
  1237.     BYTE             OpenFailed = FALSE,
  1238.                  ScreenDepth;
  1239.  
  1240.     if(Config -> ScreenConfig -> UseWorkbench)
  1241.     {
  1242.         STRPTR ScreenName = NULL;
  1243.  
  1244.         if(Config -> ScreenConfig -> PubScreenName[0])
  1245.         {
  1246.             struct Screen *SomeScreen;
  1247.  
  1248.             if(SomeScreen = LockPubScreen(Config -> ScreenConfig -> PubScreenName))
  1249.             {
  1250.                 UnlockPubScreen(NULL,SomeScreen);
  1251.  
  1252.                 ScreenName = Config -> ScreenConfig -> PubScreenName;
  1253.             }
  1254.         }
  1255.  
  1256.         if(!(DefaultPubScreen = LockPubScreen(ScreenName)))
  1257.             return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_DEFAULT_PUBLIC_SCREEN_TXT));
  1258.         else
  1259.         {
  1260.                 /* gadtools.library v37 objects don't look too pretty
  1261.                  * with a proportional-spaced font.
  1262.                  */
  1263.  
  1264.             if(!(DefaultPubScreen -> Font -> ta_Flags & FPF_PROPORTIONAL) || GadToolsBase -> lib_Version >= 39)
  1265.             {
  1266.                 strcpy(UserFontName,DefaultPubScreen -> Font -> ta_Name);
  1267.  
  1268.                 UserFont . ta_Name    = UserFontName;
  1269.                 UserFont . ta_YSize    = DefaultPubScreen -> Font -> ta_YSize;
  1270.                 UserFont . ta_Style    = DefaultPubScreen -> Font -> ta_Style;
  1271.                 UserFont . ta_Flags    = DefaultPubScreen -> Font -> ta_Flags;
  1272.             }
  1273.             else
  1274.             {
  1275.                 UnlockPubScreen(NULL,DefaultPubScreen);
  1276.  
  1277.                 DefaultPubScreen = NULL;
  1278.  
  1279.                 Config -> ScreenConfig -> UseWorkbench = FALSE;
  1280.             }
  1281.         }
  1282.     }
  1283.  
  1284.     if(!Config -> ScreenConfig -> UseWorkbench)
  1285.     {
  1286.         strcpy(UserFontName,Config -> ScreenConfig -> FontName);
  1287.  
  1288.         UserFont . ta_Name    = UserFontName;
  1289.         UserFont . ta_YSize    = Config -> ScreenConfig -> FontHeight;
  1290.         UserFont . ta_Style    = FS_NORMAL;
  1291.         UserFont . ta_Flags    = FPF_DESIGNED;
  1292.     }
  1293.  
  1294.     if(!(UserTextFont = OpenDiskFont(&UserFont)))
  1295.     {
  1296.         if(Config -> ScreenConfig -> UseWorkbench)
  1297.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1298.         else
  1299.         {
  1300.             strcpy(Config -> ScreenConfig -> FontName,    "topaz.font");
  1301.             strcpy(UserFontName,                "topaz.font");
  1302.  
  1303.             Config -> ScreenConfig -> FontHeight = 8;
  1304.  
  1305.             UserFont . ta_YSize    = Config -> ScreenConfig -> FontHeight;
  1306.             UserFont . ta_Style    = FS_NORMAL;
  1307.             UserFont . ta_Flags    = FPF_DESIGNED | FPF_ROMFONT;
  1308.  
  1309.             if(!(UserTextFont = OpenFont(&UserFont)))
  1310.                 return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1311.         }
  1312.     }
  1313.  
  1314. Reopen:    if(Config -> TerminalConfig -> FontMode != FONT_STANDARD)
  1315.         strcpy(TextFontName,"IBM.font");
  1316.     else
  1317.         strcpy(TextFontName,Config -> TerminalConfig -> TextFontName);
  1318.  
  1319.     TextAttr . ta_Name    = TextFontName;
  1320.     TextAttr . ta_YSize    = Config -> TerminalConfig -> TextFontHeight;
  1321.     TextAttr . ta_Style    = FS_NORMAL;
  1322.     TextAttr . ta_Flags    = 0;
  1323.  
  1324.     if(!(TextFont = OpenDiskFont(&TextAttr)))
  1325.     {
  1326.         if(!Stricmp(TextFontName,"IBM.font") && Stricmp("IBM.font",Config -> TerminalConfig -> TextFontName))
  1327.         {
  1328.             Config -> TerminalConfig -> FontMode = FONT_STANDARD;
  1329.  
  1330.             goto Reopen;
  1331.         }
  1332.  
  1333.         strcpy(Config -> TerminalConfig -> TextFontName,    "topaz.font");
  1334.         strcpy(TextFontName,                    "topaz.font");
  1335.  
  1336.         Config -> TerminalConfig -> TextFontHeight = 8;
  1337.  
  1338.         TextAttr . ta_YSize    = Config -> TerminalConfig -> TextFontHeight;
  1339.         TextAttr . ta_Style    = FS_NORMAL;
  1340.         TextAttr . ta_Flags    = FPF_DESIGNED | FPF_ROMFONT;
  1341.  
  1342.         if(!(TextFont = OpenFont(&TextAttr)))
  1343.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_TEXT_TXT));
  1344.     }
  1345.  
  1346.     TextFontHeight    = TextFont -> tf_YSize;
  1347.     TextFontWidth    = TextFont -> tf_XSize;
  1348.     TextFontBase    = TextFont -> tf_Baseline;
  1349.  
  1350.     CurrentFont = TextFont;
  1351.  
  1352.     GFXFont . ta_YSize = Config -> ScreenConfig -> FontHeight;
  1353.  
  1354.     if(GFX = (struct TextFont *)OpenDiskFont(&GFXFont))
  1355.     {
  1356.         if(GFX -> tf_XSize != TextFont -> tf_XSize || GFX -> tf_YSize != TextFont -> tf_YSize)
  1357.         {
  1358.             CloseFont(GFX);
  1359.  
  1360.             GFX = NULL;
  1361.         }
  1362.     }
  1363.  
  1364.     UserFontHeight    = UserTextFont -> tf_YSize;
  1365.     UserFontWidth    = UserTextFont -> tf_XSize;
  1366.     UserFontBase    = UserTextFont -> tf_Baseline;
  1367.  
  1368.     if(!Config -> ScreenConfig -> UseWorkbench)
  1369.     {
  1370.             /* We'll configure the screen parameters at
  1371.              * run time, at first we'll set up the screen
  1372.              * depth.
  1373.              */
  1374.  
  1375.         TagArray[Count++] = SA_Depth;
  1376.  
  1377.             /* Now set up the approriate colour mode. */
  1378.  
  1379.         switch(Config -> ScreenConfig -> ColourMode)
  1380.         {
  1381.             case COLOUR_EIGHT:
  1382.  
  1383.                 if(Config -> ScreenConfig -> Blinking)
  1384.                     TagArray[Count++] = ScreenDepth = 4;
  1385.                 else
  1386.                     TagArray[Count++] = ScreenDepth = 3;
  1387.  
  1388.                 TagArray[Count++] = SA_DetailPen;
  1389.                 TagArray[Count++] = 0;
  1390.  
  1391.                 if(Config -> ScreenConfig -> FasterLayout)
  1392.                 {
  1393.                     TagArray[Count++] = SA_BlockPen;
  1394.                     TagArray[Count++] = 7;
  1395.                 }
  1396.                 else
  1397.                 {
  1398.                     TagArray[Count++] = SA_Pens;
  1399.                     TagArray[Count++] = (LONG)ANSIPens;
  1400.  
  1401.                     TagArray[Count++] = SA_BlockPen;
  1402.                     TagArray[Count++] = 4;
  1403.                 }
  1404.  
  1405.                 break;
  1406.  
  1407.             case COLOUR_SIXTEEN:
  1408.  
  1409.                 TagArray[Count++] = ScreenDepth = 4;
  1410.  
  1411.                 TagArray[Count++] = SA_DetailPen;
  1412.                 TagArray[Count++] = 0;
  1413.  
  1414.                 if(Config -> ScreenConfig -> FasterLayout)
  1415.                 {
  1416.                     TagArray[Count++] = SA_BlockPen;
  1417.                     TagArray[Count++] = 15;
  1418.                 }
  1419.                 else
  1420.                 {
  1421.                     TagArray[Count++] = SA_Pens;
  1422.                     TagArray[Count++] = (LONG)EGAPens;
  1423.  
  1424.                     TagArray[Count++] = SA_BlockPen;
  1425.                     TagArray[Count++] = 8;
  1426.                 }
  1427.  
  1428.                 break;
  1429.  
  1430.             case COLOUR_MONO:
  1431.  
  1432.                 TagArray[Count++] = ScreenDepth = 1;
  1433.                 break;
  1434.  
  1435.             case COLOUR_AMIGA:
  1436.  
  1437.                 TagArray[Count++] = ScreenDepth = 2;
  1438.  
  1439.                 if(!Config -> ScreenConfig -> FasterLayout)
  1440.                 {
  1441.                     TagArray[Count++] = SA_Pens;
  1442.                     TagArray[Count++] = (LONG)StandardPens;
  1443.                 }
  1444.  
  1445.                 break;
  1446.         }
  1447.  
  1448.             /* Terminate the tag array. */
  1449.  
  1450.         TagArray[Count] = TAG_END;
  1451.  
  1452.             /* Set the plane mask. */
  1453.  
  1454.         DepthMask = (1 << ScreenDepth) - 1;
  1455.  
  1456.             /* Inquire overscan limits and try to create an interleaved
  1457.              * bitmap if possible.
  1458.              */
  1459.  
  1460.         if(Config -> ScreenConfig -> FasterLayout && ScreenDepth > 1 && IntuitionBase -> LibNode . lib_Version < 39)
  1461.         {
  1462.             if(QueryOverscan(Config -> ScreenConfig -> DisplayMode,&OverscanSize,OSCAN_TEXT))
  1463.                 InterleavedBitMap = CreateInterleavedBitMap(OverscanSize . MaxX - OverscanSize . MinX + 1,OverscanSize . MaxY - OverscanSize . MinY + 1,ScreenDepth);
  1464.             else
  1465.                 InterleavedBitMap = NULL;
  1466.         }
  1467.         else
  1468.             InterleavedBitMap = NULL;
  1469.  
  1470. #ifdef _M68030
  1471. OpenS:        SPrintf(ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,"'030 ",TermDate,TermIDString);
  1472. #else
  1473. OpenS:        SPrintf(ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,"",TermDate,TermIDString);
  1474. #endif    /* _M68030 */
  1475.  
  1476.         if(InterleavedBitMap)
  1477.         {
  1478.             if(Screen = (struct Screen *)OpenScreenTags(NULL,
  1479.                 SA_Title,    ScreenTitle,
  1480.                 SA_DClip,    &OverscanSize,
  1481.                 SA_BitMap,    InterleavedBitMap,
  1482.                 SA_DisplayID,    Config -> ScreenConfig -> DisplayMode,
  1483.                 SA_Font,    &UserFont,
  1484.                 SA_Behind,    TRUE,
  1485.                 SA_AutoScroll,    TRUE,
  1486.                 SA_ShowTitle,    Config -> ScreenConfig -> TitleBar,
  1487.                 SA_PubName,    TermIDString,
  1488.                 SA_ErrorCode,    &ErrorCode,
  1489.                 TAG_MORE,    TagArray,
  1490.             TAG_END))
  1491.                 UseMasking = FALSE;
  1492.         }
  1493.         else
  1494.         {
  1495.             BYTE Interleaved;
  1496.  
  1497.             if(Config -> ScreenConfig -> FasterLayout && IntuitionBase -> LibNode . lib_Version >= 39 && ScreenDepth > 1)
  1498.                 Interleaved = TRUE;
  1499.             else
  1500.                 Interleaved = FALSE;
  1501.  
  1502.             if(Screen = (struct Screen *)OpenScreenTags(NULL,
  1503.                 SA_Title,    ScreenTitle,
  1504.                 SA_Overscan,    OSCAN_TEXT,
  1505.                 SA_DisplayID,    Config -> ScreenConfig -> DisplayMode,
  1506.                 SA_Font,    &UserFont,
  1507.                 SA_Behind,    TRUE,
  1508.                 SA_AutoScroll,    TRUE,
  1509.                 SA_ShowTitle,    Config -> ScreenConfig -> TitleBar,
  1510.                 SA_PubName,    TermIDString,
  1511.                 SA_ErrorCode,    &ErrorCode,
  1512.                 SA_Interleaved,    Interleaved,
  1513.                 TAG_MORE,    TagArray,
  1514.             TAG_END))
  1515.             {
  1516.                 if(Interleaved)
  1517.                     UseMasking = FALSE;
  1518.                 else
  1519.                     UseMasking = TRUE;
  1520.             }
  1521.         }
  1522.  
  1523.             /* We've got an error. */
  1524.  
  1525.         if(!Screen)
  1526.         {
  1527.             if(!OpenFailed)
  1528.             {
  1529.                 switch(ErrorCode)
  1530.                 {
  1531.                         /* Can't open screen with these display
  1532.                          * modes.
  1533.                          */
  1534.  
  1535.                     case OSERR_NOMONITOR:
  1536.                     case OSERR_NOCHIPS:
  1537.                     case OSERR_UNKNOWNMODE:
  1538.  
  1539.                         if(Config -> ScreenConfig -> DisplayMode & LACE)
  1540.                             Config -> ScreenConfig -> DisplayMode = HIRESLACE_KEY;
  1541.                         else
  1542.                             Config -> ScreenConfig -> DisplayMode = HIRES_KEY;
  1543.  
  1544.                         OpenFailed = TRUE;
  1545.  
  1546.                         goto OpenS;
  1547.  
  1548.                     case OSERR_PUBNOTUNIQUE:
  1549.  
  1550.                         return(LocaleString(MSG_TERMINIT_SCREEN_ID_ALREADY_IN_USE_TXT));
  1551.                 }
  1552.             }
  1553.  
  1554.                 /* Some different error, probably out of
  1555.                  * memory.
  1556.                  */
  1557.  
  1558.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  1559.         }
  1560.  
  1561.         VPort = &Screen -> ViewPort;
  1562.  
  1563.         ScreenWidth    = Screen -> Width;
  1564.         ScreenHeight    = Screen -> Height;
  1565.  
  1566.         SZ_SizeSetup(Screen,&UserFont,TRUE);
  1567.  
  1568.             /* Obtain visual info (whatever that may be). */
  1569.  
  1570.         if(!(VisualInfo = GetVisualInfo(Screen,TAG_DONE)))
  1571.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  1572.  
  1573.         if(Config -> ScreenConfig -> TitleBar)
  1574.         {
  1575.             Top = Screen -> BarHeight + 1;
  1576.  
  1577.             if(Config -> ScreenConfig -> StatusLine != STATUSLINE_DISABLED)
  1578.             {
  1579.                 if(Config -> ScreenConfig -> StatusLine == STATUSLINE_COMPRESSED)
  1580.                     Height = Screen -> Height - (Screen -> BarHeight + 1) - UserFontHeight;
  1581.                 else
  1582.                     Height = Screen -> Height - (Screen -> BarHeight + 1) - (2 + SZ_BoxHeight(2));
  1583.             }
  1584.             else
  1585.                 Height = Screen -> Height - (Screen -> BarHeight + 1);
  1586.         }
  1587.         else
  1588.         {
  1589.             Top = 0;
  1590.  
  1591.             if(Config -> ScreenConfig -> StatusLine)
  1592.             {
  1593.                 if(Config -> ScreenConfig -> StatusLine == STATUSLINE_COMPRESSED)
  1594.                     Height = Screen -> Height - UserFontHeight;
  1595.                 else
  1596.                     Height = Screen -> Height - (2 + SZ_BoxHeight(2));
  1597.             }
  1598.             else
  1599.                 Height = Screen -> Height;
  1600.         }
  1601.  
  1602.             /* Open the main window. */
  1603.  
  1604.         if(!(Window = OpenWindowTags(NULL,
  1605.             WA_Top,        Top,
  1606.             WA_Left,    0,
  1607.             WA_Width,    Screen -> Width,
  1608.             WA_Height,    Height,
  1609.             WA_Backdrop,    TRUE,
  1610.             WA_Borderless,    TRUE,
  1611.             WA_SmartRefresh,TRUE,
  1612.             WA_CustomScreen,Screen,
  1613.             WA_NewLookMenus,TRUE,
  1614.             WA_RMBTrap,    TRUE,
  1615.             WA_IDCMP,    IDCMP_RAWKEY | IDCMP_INACTIVEWINDOW | IDCMP_MOUSEMOVE | IDCMP_GADGETUP | IDCMP_MENUPICK | IDCMP_MOUSEMOVE | IDCMP_MOUSEBUTTONS | IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE | LISTVIEWIDCMP,
  1616.         TAG_DONE)))
  1617.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  1618.     }
  1619.     else
  1620.     {
  1621.         struct TagItem    SomeTags[4];
  1622.         LONG        FullWidth,
  1623.                 Height,Width,
  1624.                 Index = 0;
  1625.  
  1626.         if(DefaultPubScreen -> RastPort . BitMap -> Depth == 1)
  1627.             UseMasking = FALSE;
  1628.         else
  1629.         {
  1630.             if(GfxBase -> LibNode . lib_Version >= 39)
  1631.             {
  1632.                 if(GetBitMapAttr(DefaultPubScreen -> RastPort . BitMap,BMA_FLAGS) & BMF_INTERLEAVED)
  1633.                     UseMasking = FALSE;
  1634.                 else
  1635.                     UseMasking = TRUE;
  1636.             }
  1637.             else
  1638.                 UseMasking = TRUE;
  1639.         }
  1640.  
  1641.         VPort = &DefaultPubScreen -> ViewPort;
  1642.  
  1643.             /* Get the current display dimensions. */
  1644.  
  1645.         if(VPort -> ColorMap -> cm_vpe)
  1646.         {
  1647.             struct ViewPortExtra *Extra;
  1648.  
  1649.             Extra = VPort -> ColorMap -> cm_vpe;
  1650.  
  1651.             ScreenWidth    = Extra -> DisplayClip . MaxX - Extra -> DisplayClip . MinX + 1;
  1652.             ScreenHeight    = Extra -> DisplayClip . MaxY - Extra -> DisplayClip . MinY + 1;
  1653.         }
  1654.         else
  1655.         {
  1656.             struct ViewPortExtra *Extra;
  1657.  
  1658.             if(Extra = (struct ViewPortExtra *)GfxLookUp(VPort))
  1659.             {
  1660.                 ScreenWidth    = Extra -> DisplayClip . MaxX - Extra -> DisplayClip . MinX + 1;
  1661.                 ScreenHeight    = Extra -> DisplayClip . MaxY - Extra -> DisplayClip . MinY + 1;
  1662.             }
  1663.             else
  1664.             {
  1665.                 ScreenWidth    = DefaultPubScreen -> Width;
  1666.                 ScreenHeight    = DefaultPubScreen -> Height;
  1667.             }
  1668.         }
  1669.  
  1670.         DepthMask = (1 << DefaultPubScreen -> RastPort . BitMap -> Depth) - 1;
  1671.  
  1672.         switch(Config -> ScreenConfig -> ColourMode)
  1673.         {
  1674.             case COLOUR_SIXTEEN:
  1675.  
  1676.                 if(DepthMask < 15)
  1677.                 {
  1678.                     if(DepthMask >= 7)
  1679.                         Config -> ScreenConfig -> ColourMode = COLOUR_EIGHT;
  1680.                     else
  1681.                     {
  1682.                         if(DepthMask >= 3)
  1683.                             Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  1684.                         else
  1685.                             Config -> ScreenConfig -> ColourMode = COLOUR_MONO;
  1686.                     }
  1687.                 }
  1688.  
  1689.                 break;
  1690.  
  1691.             case COLOUR_EIGHT:
  1692.  
  1693.                 if(DepthMask < 7)
  1694.                 {
  1695.                     if(DepthMask >= 3)
  1696.                         Config -> ScreenConfig -> ColourMode = COLOUR_AMIGA;
  1697.                     else
  1698.                         Config -> ScreenConfig -> ColourMode = COLOUR_MONO;
  1699.                 }
  1700.  
  1701.                 break;
  1702.  
  1703.             case COLOUR_AMIGA:
  1704.  
  1705.                 if(DepthMask < 3)
  1706.                     Config -> ScreenConfig -> ColourMode = COLOUR_MONO;
  1707.  
  1708.                 break;
  1709.         }
  1710.  
  1711.         SZ_SizeSetup(DefaultPubScreen,&UserFont,TRUE);
  1712.  
  1713.             /* Obtain visual info (whatever that may be). */
  1714.  
  1715.         if(!(VisualInfo = GetVisualInfo(DefaultPubScreen,TAG_DONE)))
  1716.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  1717.  
  1718.         if(Config -> ScreenConfig -> StatusLine != STATUSLINE_DISABLED)
  1719.         {
  1720.             if(!(StatusGadget = (struct Gadget *)CreateStatusGadget(DefaultPubScreen -> Width,42)))
  1721.                 return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_STATUS_GADGET_TXT));
  1722.         }
  1723.  
  1724.         if(StatusGadget)
  1725.             GetAttr(SGA_FullWidth,StatusGadget,&FullWidth);
  1726.         else
  1727.             FullWidth = 0;
  1728.  
  1729. #ifdef _M68030
  1730.         SPrintf(ScreenTitle,"%s '030 (%s)",TermName,TermDate);
  1731. #else
  1732.         SPrintf(ScreenTitle,"%s (%s)",TermName,TermDate);
  1733. #endif    /* _M68030 */
  1734.  
  1735.         if(StatusGadget)
  1736.         {
  1737.             SomeTags[Index  ] . ti_Tag    = WA_Gadgets;
  1738.             SomeTags[Index++] . ti_Data    = (ULONG)StatusGadget;
  1739.         }
  1740.  
  1741.         if(Config -> TerminalConfig -> NumColumns < 20)
  1742.         {
  1743.             LONG Width = GetScreenWidth(NULL);
  1744.  
  1745.             if(FullWidth && Width < FullWidth)
  1746.             {
  1747.                 SomeTags[Index  ] . ti_Tag    = WA_InnerWidth;
  1748.                 SomeTags[Index++] . ti_Data    = FullWidth;
  1749.             }
  1750.             else
  1751.             {
  1752.                 SomeTags[Index  ] . ti_Tag    = WA_Width;
  1753.                 SomeTags[Index++] . ti_Data    = Width;
  1754.             }
  1755.         }
  1756.         else
  1757.         {
  1758.             SomeTags[Index  ] . ti_Tag    = WA_InnerWidth;
  1759.             SomeTags[Index++] . ti_Data    = Config -> TerminalConfig -> NumColumns * TextFontWidth;
  1760.         }
  1761.  
  1762.         if(Config -> TerminalConfig -> NumLines < 20)
  1763.         {
  1764.             SomeTags[Index  ] . ti_Tag    = WA_Height;
  1765.             SomeTags[Index++] . ti_Data    = GetScreenHeight(NULL) - (DefaultPubScreen -> BarHeight + 1);
  1766.         }
  1767.         else
  1768.         {
  1769.             SomeTags[Index  ] . ti_Tag    = WA_InnerHeight;
  1770.             SomeTags[Index++] . ti_Data    = Config -> TerminalConfig -> NumLines * TextFontHeight;
  1771.         }
  1772.  
  1773.         SomeTags[Index] . ti_Tag = TAG_DONE;
  1774.  
  1775.             /* Open the main window. */
  1776.  
  1777.         if(!(Window = OpenWindowTags(NULL,
  1778.             WA_Left,        GetScreenLeft(NULL),
  1779.             WA_Top,            GetScreenTop(NULL) + DefaultPubScreen -> BarHeight + 1,
  1780.             WA_MaxHeight,        DefaultPubScreen -> Height,
  1781.             WA_MaxWidth,        DefaultPubScreen -> Width,
  1782.             WA_SmartRefresh,    TRUE,
  1783.             WA_CustomScreen,    DefaultPubScreen,
  1784.             WA_NewLookMenus,    TRUE,
  1785.             WA_RMBTrap,        TRUE,
  1786.             WA_IDCMP,        IDCMP_RAWKEY | IDCMP_INACTIVEWINDOW | IDCMP_MOUSEMOVE | IDCMP_GADGETUP | IDCMP_MENUPICK | IDCMP_MOUSEMOVE | IDCMP_MOUSEBUTTONS | IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE | LISTVIEWIDCMP,
  1787.             WA_DragBar,        TRUE,
  1788.             WA_DepthGadget,        TRUE,
  1789.             WA_CloseGadget,        TRUE,
  1790.             WA_SizeGadget,        TRUE,
  1791.             WA_SizeBBottom,        TRUE,
  1792.             WA_NoCareRefresh,    TRUE,
  1793.             WA_Title,        ScreenTitle,
  1794.             TAG_MORE,        SomeTags,
  1795.         TAG_DONE)))
  1796.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  1797.  
  1798.         if(FullWidth < 40 * TextFontWidth)
  1799.             FullWidth = 40 * TextFontWidth;
  1800.  
  1801.         Width    = Window -> BorderLeft + FullWidth + Window -> BorderRight;
  1802.         Height    = Window -> BorderTop + 20 * TextFontHeight + Window -> BorderBottom;
  1803.  
  1804.         WindowLimits(Window,Width,Height,0,0);
  1805.  
  1806.         if(WorkbenchBase)
  1807.         {
  1808.             if(WorkbenchPort = CreateMsgPort())
  1809.             {
  1810.                 if(!(WorkbenchWindow = AddAppWindow(0,0,Window,WorkbenchPort,TAG_DONE)))
  1811.                 {
  1812.                     DeleteMsgPort(WorkbenchPort);
  1813.  
  1814.                     WorkbenchPort = NULL;
  1815.                 }
  1816.             }
  1817.         }
  1818.     }
  1819.  
  1820.         /* Fill the `default' colour with current values. */
  1821.  
  1822.     if(!Config -> ScreenConfig -> UseWorkbench && Initializing)
  1823.     {
  1824.         for(i = 0 ; i < 16 ; i++)
  1825.             DefaultColours[i] = GetRGB4(VPort -> ColorMap,i);
  1826.  
  1827.         Initializing = FALSE;
  1828.     }
  1829.  
  1830.         /* Load the approriate colours. */
  1831.  
  1832.     if(LoadColours)
  1833.     {
  1834.         switch(Config -> ScreenConfig -> ColourMode)
  1835.         {
  1836.             case COLOUR_EIGHT:
  1837.  
  1838.                 CopyMem(ANSIColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  1839.                 break;
  1840.  
  1841.             case COLOUR_SIXTEEN:
  1842.  
  1843.                 CopyMem(EGAColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  1844.                 break;
  1845.  
  1846.             case COLOUR_AMIGA:
  1847.  
  1848.                 CopyMem(DefaultColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  1849.                 break;
  1850.  
  1851.             case COLOUR_MONO:
  1852.  
  1853.                 CopyMem(AtomicColours,Config -> ScreenConfig -> Colours,16 * sizeof(UWORD));
  1854.                 break;
  1855.         }
  1856.  
  1857.         LoadColours = FALSE;
  1858.     }
  1859.  
  1860.         /* Reset the current colours and the blinking equivalents. */
  1861.  
  1862.     if(!Config -> ScreenConfig -> UseWorkbench)
  1863.     {
  1864.         for(i = 0 ; i < 16 ; i++)
  1865.             BlinkColours[i] = Config -> ScreenConfig -> Colours[i];
  1866.  
  1867.         LoadRGB4(VPort,Config -> ScreenConfig -> Colours,DepthMask + 1);
  1868.  
  1869.             /* Fiddle with the blinking colours. */
  1870.  
  1871.         switch(Config -> ScreenConfig -> ColourMode)
  1872.         {
  1873.             case COLOUR_EIGHT:
  1874.  
  1875.                 for(i = 0 ; i < 8 ; i++)
  1876.                     BlinkColours[i + 8] = BlinkColours[0];
  1877.  
  1878.                 break;
  1879.  
  1880.             case COLOUR_AMIGA:
  1881.  
  1882.                 BlinkColours[3] = BlinkColours[0];
  1883.                 break;
  1884.         }
  1885.     }
  1886.  
  1887.     if(!(DrawInfo = GetScreenDrawInfo(Window -> WScreen)))
  1888.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_SCREEN_DRAWINFO_TXT));
  1889.  
  1890.         /* Get the vanilla rendering pens. */
  1891.  
  1892.     RenderPens[0] = DrawInfo -> dri_Pens[BACKGROUNDPEN];
  1893.     RenderPens[1] = DrawInfo -> dri_Pens[TEXTPEN];
  1894.     RenderPens[2] = DrawInfo -> dri_Pens[SHINEPEN];
  1895.     RenderPens[3] = DrawInfo -> dri_Pens[FILLPEN];
  1896.  
  1897.         /* Are we to use the Workbench screen for text output? */
  1898.  
  1899.     if(Config -> ScreenConfig -> UseWorkbench)
  1900.     {
  1901.         if(GfxBase -> LibNode . lib_Version >= 39 && (Config -> ScreenConfig -> ColourMode == COLOUR_EIGHT || Config -> ScreenConfig -> ColourMode == COLOUR_SIXTEEN))
  1902.         {
  1903.             ULONG     R,G,B;
  1904.             BYTE     GotAll = TRUE;
  1905.  
  1906.             for(i = 0 ; i < 16 ; i++)
  1907.                 MappedPens[1][i] = FALSE;
  1908.  
  1909.                 /* Allocate the text rendering pens, note that
  1910.                  * we will use the currently installed palette
  1911.                  * to obtain those pens which match them best.
  1912.                  * The user will be unable to change these
  1913.                  * colours.
  1914.                  */
  1915.  
  1916.             for(i = 0 ; i < DepthMask + 1 ; i++)
  1917.             {
  1918.                     /* Split the 12 bit colour palette entry. */
  1919.  
  1920.                 R = (Config -> ScreenConfig -> Colours[i] >> 8) & 0xF;
  1921.                 G = (Config -> ScreenConfig -> Colours[i] >> 4) & 0xF;
  1922.                 B = (Config -> ScreenConfig -> Colours[i])      & 0xF;
  1923.  
  1924.                     /* Try to obtain a matching pen. */
  1925.  
  1926.                 if((MappedPens[0][i] = ObtainBestPen(VPort -> ColorMap,(((R << 4) | R) << 24) | 0x00FFFFFF,(((G << 4) | G) << 24) | 0x00FFFFFF,(((B << 4) | B) << 24) | 0x00FFFFFF,
  1927.                     OBP_FailIfBad,TRUE,
  1928.                 TAG_DONE)) == -1)
  1929.                 {
  1930.                     GotAll = FALSE;
  1931.  
  1932.                     break;
  1933.                 }
  1934.                 else
  1935.                     MappedPens[1][i] = TRUE;
  1936.             }
  1937.  
  1938.                 /* Did we get what we wanted? */
  1939.  
  1940.             if(!GotAll)
  1941.             {
  1942.                     /* Release all the pens we succeeded
  1943.                      * in allocating.
  1944.                      */
  1945.  
  1946.                 for(i = 0 ; i < DepthMask + 1 ; i++)
  1947.                 {
  1948.                     if(MappedPens[1][i])
  1949.                         ReleasePen(VPort -> ColorMap,i);
  1950.                 }
  1951.  
  1952.                     /* Use the default rendering pens. */
  1953.  
  1954.                 for(i = 0 ; i < 4 ; i++)
  1955.                 {
  1956.                     MappedPens[0][i] = RenderPens[i];
  1957.                     MappedPens[1][i] = FALSE;
  1958.                 }
  1959.  
  1960.                     /* Set the remaining pens to defaults. */
  1961.  
  1962.                 for(i = 4 ; i < DepthMask + 1 ; i++)
  1963.                 {
  1964.                     MappedPens[0][i] = RenderPens[1];
  1965.                     MappedPens[1][i] = FALSE;
  1966.                 }
  1967.             }
  1968.             else
  1969.                 AllocatedPens = TRUE;
  1970.         }
  1971.         else
  1972.         {
  1973.                 /* Use the default rendering pens. */
  1974.  
  1975.             if(Config -> ScreenConfig -> ColourMode == COLOUR_AMIGA)
  1976.             {
  1977.                 for(i = 0 ; i < 4 ; i++)
  1978.                 {
  1979.                     MappedPens[0][i] = RenderPens[i];
  1980.                     MappedPens[1][i] = FALSE;
  1981.                 }
  1982.  
  1983.                     /* Set the remaining pens to defaults. */
  1984.  
  1985.                 for(i = 4 ; i < DepthMask + 1 ; i++)
  1986.                 {
  1987.                     MappedPens[0][i] = RenderPens[1];
  1988.                     MappedPens[1][i] = FALSE;
  1989.                 }
  1990.             }
  1991.             else
  1992.             {
  1993.                 for(i = 0 ; i < DepthMask + 1 ; i++)
  1994.                 {
  1995.                     MappedPens[0][i] = RenderPens[i & 1];
  1996.                     MappedPens[1][i] = FALSE;
  1997.                 }
  1998.             }
  1999.         }
  2000.     }
  2001.     else
  2002.     {
  2003.             /* Reset the colour translation table. */
  2004.  
  2005.         for(i = 0 ; i < 16 ; i++)
  2006.         {
  2007.             MappedPens[0][i] = i;
  2008.             MappedPens[1][i] = FALSE;
  2009.         }
  2010.     }
  2011.  
  2012.         /* Determine default text rendering colour. */
  2013.  
  2014.     switch(Config -> ScreenConfig -> ColourMode)
  2015.     {
  2016.         case COLOUR_SIXTEEN:
  2017.  
  2018.             SafeTextPen = MappedPens[0][15];
  2019.             break;
  2020.  
  2021.         case COLOUR_EIGHT:
  2022.  
  2023.             SafeTextPen = MappedPens[0][7];
  2024.             break;
  2025.  
  2026.         case COLOUR_AMIGA:
  2027.         case COLOUR_MONO:
  2028.  
  2029.             SafeTextPen = MappedPens[0][1];
  2030.             break;
  2031.     }
  2032.  
  2033.         /* Determine window inner dimensions and top/left edge offsets. */
  2034.  
  2035.     WindowLeft    = Window -> BorderLeft;
  2036.     WindowTop    = Window -> BorderTop;
  2037.  
  2038.     WindowWidth    = Window -> Width - (Window -> BorderLeft + Window -> BorderRight);
  2039.     WindowHeight    = Window -> Height - (Window -> BorderTop + Window -> BorderBottom);
  2040.  
  2041.         /* Set up scaling data (bitmaps & rastports). */
  2042.  
  2043.     if(!CreateScale())
  2044.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_FONT_SCALING_INFO_TXT));
  2045.  
  2046.     if(!CreateOffsetTables())
  2047.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_OFFSET_TABLES_TXT));
  2048.  
  2049.     TabStopMax = Window -> WScreen -> Width / TextFontWidth;
  2050.  
  2051.         /* Allocate the tab stop line. */
  2052.  
  2053.     if(!(TabStops = (BYTE *)AllocVec(TabStopMax,MEMF_ANY | MEMF_CLEAR)))
  2054.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  2055.  
  2056.         /* Push it on the window stack (should become bottommost
  2057.          * entry).
  2058.          */
  2059.  
  2060.     PushWindow(Window);
  2061.  
  2062.     if(TermPort)
  2063.         TermPort -> TopWindow = Window;
  2064.  
  2065.     if(!Config -> ScreenConfig -> UseWorkbench)
  2066.     {
  2067.             /* Open the tiny status window. */
  2068.  
  2069.         if(Config -> ScreenConfig -> StatusLine != STATUSLINE_DISABLED)
  2070.         {
  2071.             if(!(StatusWindow = OpenWindowTags(NULL,
  2072.                 WA_Top,        Window -> TopEdge + Window -> Height,
  2073.                 WA_Left,    0,
  2074.                 WA_Width,    Screen -> Width,
  2075.                 WA_Height,    Screen -> Height - (Window -> TopEdge + Window -> Height),
  2076.                 WA_Backdrop,    TRUE,
  2077.                 WA_Borderless,    TRUE,
  2078.                 WA_SmartRefresh,TRUE,
  2079.                 WA_NewLookMenus,TRUE,
  2080.                 WA_CustomScreen,Screen,
  2081.                 WA_RMBTrap,    TRUE,
  2082.             TAG_DONE)))
  2083.                 return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_STATUS_WINDOW_TXT));
  2084.         }
  2085.         else
  2086.             StatusWindow = NULL;
  2087.  
  2088.         if(StatusWindow)
  2089.         {
  2090.             StatusWindow -> UserPort = Window -> UserPort;
  2091.  
  2092.             ModifyIDCMP(StatusWindow,Window -> IDCMPFlags);
  2093.         }
  2094.     }
  2095.  
  2096.     RPort = Window -> RPort;
  2097.  
  2098.         /* Default console setup. */
  2099.  
  2100.     CursorX = 0;
  2101.     CursorY    = 0;
  2102.  
  2103.     SetDrMd(RPort,JAM2);
  2104.  
  2105.         /* Set the font. */
  2106.  
  2107.     SetFont(RPort,CurrentFont);
  2108.  
  2109.         /* Redirect AmigaDOS requesters. */
  2110.  
  2111.     OldWindowPtr = ThisProcess -> pr_WindowPtr;
  2112.  
  2113.     ThisProcess -> pr_WindowPtr = (APTR)Window;
  2114.  
  2115.         /* Create the character raster. */
  2116.  
  2117.     if(!CreateRaster())
  2118.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_SCREEN_RASTER_TXT));
  2119.  
  2120.     ConOutputUpdate();
  2121.  
  2122.     ConFontScaleUpdate();
  2123.  
  2124.         /* Set up the scrolling info. */
  2125.  
  2126.     ScrollLineCount = Window -> WScreen -> Height / TextFontHeight;
  2127.  
  2128.     if(!(ScrollLines = (struct ScrollLineInfo *)AllocVec(sizeof(struct ScrollLineInfo) * ScrollLineCount,MEMF_ANY|MEMF_CLEAR)))
  2129.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SCROLLING_SUPPORT_INFO_TXT));
  2130.  
  2131.         /* Create the menu strip. */
  2132.  
  2133.     if(!(Menu = CreateMenus(TermMenu,TAG_DONE)))
  2134.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_MENUS_TXT));
  2135.  
  2136.         /* Do the menu layout. */
  2137.  
  2138.     if(!LayoutMenus(Menu,VisualInfo,
  2139.         GTMN_NewLookMenus,    TRUE,
  2140.         GTMN_TextAttr,        &UserFont,
  2141.     TAG_DONE))
  2142.         return(LocaleString(MSG_TERMINIT_FAILED_TO_LAYOUT_MENUS_TXT));
  2143.  
  2144.         /* Add the menu to the windows. */
  2145.  
  2146.     SetMenuStrip(Window,Menu);
  2147.  
  2148.         /* Disable the `Execute ARexx Command' menu item if
  2149.          * the rexx server is not available.
  2150.          */
  2151.  
  2152. #ifdef USE_AREXX
  2153.  
  2154.     if(!RexxSysBase)
  2155.         OffItem(MEN_EXECUTE_REXX_COMMAND);
  2156.  
  2157. #else
  2158.  
  2159.     OffItem(MEN_EXECUTE_REXX_COMMAND);
  2160.  
  2161. #endif    /* USE_AREXX */
  2162.  
  2163.     if(StatusWindow)
  2164.     {
  2165.         SetMenuStrip(StatusWindow,Menu);
  2166.  
  2167.         StatusWindow -> Flags &= ~WFLG_RMBTRAP;
  2168.  
  2169.         SetDrMd(StatusWindow -> RPort,JAM2);
  2170.     }
  2171.  
  2172.         /* Add a tick if file capture is active. */
  2173.  
  2174.     if(FileCapture)
  2175.         CheckItem(MEN_CAPTURE_TO_FILE,TRUE);
  2176.     else
  2177.         CheckItem(MEN_CAPTURE_TO_FILE,FALSE);
  2178.  
  2179.         /* Add a tick if printer capture is active. */
  2180.  
  2181.     CheckItem(MEN_CAPTURE_TO_PRINTER,PrinterCapture);
  2182.  
  2183.         /* Add a tick if the buffer is frozen. */
  2184.  
  2185.     CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  2186.  
  2187.         /* Disable the dialing functions if online. */
  2188.  
  2189.     if(Online)
  2190.         SetDialMenu(FALSE);
  2191.     else
  2192.         SetDialMenu(TRUE);
  2193.  
  2194.     if(!XProtocolBase)
  2195.         SetTransferMenu(FALSE);
  2196.  
  2197.         /* Disable the `Print Screen' and `Save ASCII' functions
  2198.          * if raster is not enabled.
  2199.          */
  2200.  
  2201.     SetRasterMenu(RasterEnabled);
  2202.  
  2203.         /* Enable the menu. */
  2204.  
  2205.     Window -> Flags &= ~WFLG_RMBTRAP;
  2206.  
  2207.     strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  2208.  
  2209.         /* Create the status server. */
  2210.  
  2211.     Forbid();
  2212.  
  2213.     if(StatusProcess = CreateNewProcTags(
  2214.         NP_Entry,    StatusServer,
  2215.         NP_Name,    "term status process",
  2216.         NP_WindowPtr,    -1,
  2217.         NP_Priority,    5,
  2218.     TAG_DONE))
  2219.     {
  2220.         SetSignal(0,SIG_HANDSHAKE);
  2221.  
  2222.         Wait(SIG_HANDSHAKE);
  2223.     }
  2224.  
  2225.     Permit();
  2226.  
  2227.         /* Status server has `died'. */
  2228.  
  2229.     if(!StatusProcess)
  2230.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_STATUS_TASK_TXT));
  2231.  
  2232.         /* Obtain the default public screen name just in case
  2233.          * we'll need it later.
  2234.          */
  2235.  
  2236.     GetDefaultPubScreen(DefaultPubScreenName);
  2237.  
  2238.         /* Set up the window size. */
  2239.  
  2240.     ScreenSizeStuff();
  2241.  
  2242.         /* Select the default console data processing routine. */
  2243.  
  2244.     ConProcessData = ConProcessData8;
  2245.  
  2246.         /* Handle the remaining terminal setup. */
  2247.  
  2248.     if(Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL && Config -> TerminalConfig -> EmulationFileName[0])
  2249.     {
  2250.         if(!OpenEmulator(Config -> TerminalConfig -> EmulationFileName))
  2251.         {
  2252.             Config -> TerminalConfig -> EmulationMode = EMULATION_ANSIVT100;
  2253.  
  2254.             ResetDisplay = TRUE;
  2255.  
  2256.             RasterEnabled = TRUE;
  2257.  
  2258.             MyEasyRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),Config -> TerminalConfig -> EmulationFileName);
  2259.         }
  2260.         else
  2261.         {
  2262.             if(RasterEnabled)
  2263.                 RasterEnabled = FALSE;
  2264.  
  2265.             SetRasterMenu(RasterEnabled);
  2266.         }
  2267.     }
  2268.  
  2269.         /* Choose the right console data processing routine. */
  2270.  
  2271.     if(XEmulatorBase && Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  2272.     {
  2273.         if(ReceiveTable)
  2274.             ConProcessData = ConProcessDataTransExternal;
  2275.         else
  2276.             ConProcessData = ConProcessDataExternal;
  2277.     }
  2278.     else
  2279.     {
  2280.         if(Config -> SerialConfig -> StripBit8)
  2281.             ConProcessData = ConProcessData7;
  2282.         else
  2283.             ConProcessData = ConProcessData8;
  2284.     }
  2285.  
  2286.         /* Reset terminal emulation. */
  2287.  
  2288.     if(Config -> TerminalConfig -> EmulationMode != EMULATION_EXTERNAL)
  2289.     {
  2290.         ClearCursor();
  2291.  
  2292.         Reset();
  2293.  
  2294.         DrawCursor();
  2295.     }
  2296.     else
  2297.     {
  2298.         if(XEmulatorBase)
  2299.             XEmulatorResetConsole(XEM_IO);
  2300.     }
  2301.  
  2302.         /* Restart the fast! macro panel. */
  2303.  
  2304.     if(HadFastMacros || Config -> MiscConfig -> OpenFastMacroPanel)
  2305.         OpenFastWindow();
  2306.  
  2307.     return(NULL);
  2308. }
  2309.  
  2310.     /* CloseAll():
  2311.      *
  2312.      *    Free all resources and leave the program.
  2313.      */
  2314.  
  2315. VOID
  2316. CloseAll(BYTE CloseDOS)
  2317. {
  2318.     WORD i;
  2319.  
  2320.     DeleteInputHandler();
  2321.  
  2322.     SZ_SizeCleanup();
  2323.  
  2324.     FreeDialList(TRUE);
  2325.  
  2326.     if(SpecialTable)
  2327.         FreeVec(SpecialTable);
  2328.  
  2329.     if(AbortTable)
  2330.         FreeVec(AbortTable);
  2331.  
  2332.     if(BackupConfig)
  2333.         DeleteConfiguration(BackupConfig);
  2334.  
  2335.     if(IntuitionBase && Window)
  2336.         BlockWindows();
  2337.  
  2338. #ifdef USE_AREXX
  2339.  
  2340.     {
  2341.         extern struct MsgPort *RexxPort;
  2342.  
  2343.         if(RexxPort)
  2344.             RemPort(RexxPort);
  2345.     }
  2346.  
  2347.     if(TermRexxPort)
  2348.     {
  2349.         if(RexxSysBase)
  2350.         {
  2351.             struct Message *Msg;
  2352.  
  2353.             while(Msg = GetMsg(TermRexxPort))
  2354.                 ReplyMsg(Msg);
  2355.         }
  2356.  
  2357.         DeleteMsgPort(TermRexxPort);
  2358.     }
  2359.  
  2360.     if(RexxProcess)
  2361.     {
  2362.         Forbid();
  2363.  
  2364.         Signal(RexxProcess,SIG_KILL);
  2365.  
  2366.         SetSignal(0,SIG_HANDSHAKE);
  2367.  
  2368.         Wait(SIG_HANDSHAKE);
  2369.  
  2370.         Permit();
  2371.     }
  2372.  
  2373.     if(RexxSysBase)
  2374.     {
  2375.         CloseLibrary(RexxSysBase);
  2376.  
  2377.         RexxSysBase = NULL;
  2378.     }
  2379.  
  2380. #endif    /* USE_AREXX */
  2381.  
  2382.     if(ClipProcess)
  2383.     {
  2384.         Forbid();
  2385.  
  2386.         Signal(ClipProcess,SIG_KILL);
  2387.  
  2388.         SetSignal(0,SIG_HANDSHAKE);
  2389.  
  2390.         Wait(SIG_HANDSHAKE);
  2391.  
  2392.         Permit();
  2393.     }
  2394.  
  2395.     if(BufferProcess)
  2396.     {
  2397.         Forbid();
  2398.  
  2399.         Signal(BufferProcess,SIG_KILL);
  2400.  
  2401.         SetSignal(0,SIG_HANDSHAKE);
  2402.  
  2403.         Wait(SIG_HANDSHAKE);
  2404.  
  2405.         Permit();
  2406.     }
  2407.  
  2408.     if(XprIO && XProtocolBase)
  2409.         XProtocolCleanup(XprIO);
  2410.  
  2411.     if(XProtocolBase)
  2412.     {
  2413.         CloseLibrary(XProtocolBase);
  2414.  
  2415.         XProtocolBase = NULL;
  2416.     }
  2417.  
  2418.     if(XprIO)
  2419.         FreeVec(XprIO);
  2420.  
  2421.     if(CursorKeys)
  2422.         FreeVec(CursorKeys);
  2423.  
  2424.     if(MacroKeys)
  2425.         FreeVec(MacroKeys);
  2426.  
  2427.     TerminateBuffer();
  2428.  
  2429.     DeleteSpeech();
  2430.  
  2431.     Forbid();
  2432.  
  2433.     BufferClosed = TRUE;
  2434.  
  2435.     DeleteBuffer();
  2436.  
  2437.     Permit();
  2438.  
  2439.     if(AttentionBuffers[0])
  2440.         FreeVec(AttentionBuffers[0]);
  2441.  
  2442.     if(SendTable)
  2443.         FreeTranslationTable(SendTable);
  2444.  
  2445.     if(ReceiveTable)
  2446.         FreeTranslationTable(ReceiveTable);
  2447.  
  2448.     FreeDialList(TRUE);
  2449.  
  2450.     DeleteOffsetTables();
  2451.  
  2452.     ClearFastMacroList(&FastMacroList);
  2453.  
  2454.     for(i = GLIST_UPLOAD ; i <= GLIST_WAIT ; i++)
  2455.     {
  2456.         if(GenericListTable[i])
  2457.             DeleteGenericList(GenericListTable[i]);
  2458.     }
  2459.  
  2460.     if(FileCapture)
  2461.     {
  2462.         BufferClose(FileCapture);
  2463.  
  2464.         if(!GetFileSize(CaptureName))
  2465.             DeleteFile(CaptureName);
  2466.         else
  2467.             SetProtection(CaptureName,FIBF_EXECUTE);
  2468.     }
  2469.  
  2470.     if(PrinterCapture)
  2471.         Close(PrinterCapture);
  2472.  
  2473.     CloseEmulator();
  2474.  
  2475.     if(XEM_MacroKeys)
  2476.         FreeVec(XEM_MacroKeys);
  2477.  
  2478.     DeleteDisplay();
  2479.  
  2480.     if(KeySegment)
  2481.         UnLoadSeg(KeySegment);
  2482.  
  2483.     StopCall(TRUE);
  2484.  
  2485.     if(ClipBit != -1)
  2486.         FreeSignal(ClipBit);
  2487.  
  2488.     if(CheckBit != -1)
  2489.         FreeSignal(CheckBit);
  2490.  
  2491.     ClearSerial();
  2492.  
  2493.     DeleteSerial();
  2494.  
  2495.     if(TimeRequest)
  2496.     {
  2497.         if(TimeRequest -> tr_node . io_Device)
  2498.             CloseDevice(TimeRequest);
  2499.  
  2500.         DeleteIORequest(TimeRequest);
  2501.     }
  2502.  
  2503.     if(TimePort)
  2504.         DeleteMsgPort(TimePort);
  2505.  
  2506.     DeleteBeep();
  2507.  
  2508.     ShutdownCx();
  2509.  
  2510.     if(TermPort)
  2511.     {
  2512.         if(TermID != -1)
  2513.         {
  2514.             ObtainSemaphore(&TermPort -> OpenSemaphore);
  2515.  
  2516.             TermPort -> OpenCount--;
  2517.  
  2518.             if(TermPort -> OpenCount <= 0 && !TermPort -> HoldIt)
  2519.             {
  2520.                 RemPort(&TermPort -> ExecNode);
  2521.  
  2522.                 ReleaseSemaphore(&TermPort -> OpenSemaphore);
  2523.  
  2524.                 FreeVec(TermPort);
  2525.             }
  2526.             else
  2527.                 ReleaseSemaphore(&TermPort -> OpenSemaphore);
  2528.         }
  2529.     }
  2530.  
  2531.     CloseClip();
  2532.  
  2533.     if(Config)
  2534.         DeleteConfiguration(Config);
  2535.  
  2536.     if(PrivateConfig)
  2537.         DeleteConfiguration(PrivateConfig);
  2538.  
  2539.     if(RequesterList)
  2540.         FreeVec(RequesterList);
  2541.  
  2542.     if(FakeInputEvent)
  2543.         FreeVec(FakeInputEvent);
  2544.  
  2545.     if(ConsoleDevice)
  2546.         CloseDevice(ConsoleRequest);
  2547.  
  2548.     if(ConsoleRequest)
  2549.         FreeVec(ConsoleRequest);
  2550.  
  2551.     if(WorkbenchBase)
  2552.     {
  2553.         CloseLibrary(WorkbenchBase);
  2554.  
  2555.         WorkbenchBase = NULL;
  2556.     }
  2557.  
  2558.     if(OwnDevUnitBase)
  2559.     {
  2560.         CloseLibrary(OwnDevUnitBase);
  2561.  
  2562.         OwnDevUnitBase = NULL;
  2563.     }
  2564.  
  2565.     if(CxBase)
  2566.     {
  2567.         CloseLibrary(CxBase);
  2568.  
  2569.         CxBase = NULL;
  2570.     }
  2571.  
  2572.     if(IFFParseBase)
  2573.     {
  2574.         CloseLibrary(IFFParseBase);
  2575.  
  2576.         IFFParseBase = NULL;
  2577.     }
  2578.  
  2579.     if(AslBase)
  2580.     {
  2581.         CloseLibrary(AslBase);
  2582.  
  2583.         AslBase = NULL;
  2584.     }
  2585.  
  2586.     if(DiskfontBase)
  2587.     {
  2588.         CloseLibrary(DiskfontBase);
  2589.  
  2590.         DiskfontBase = NULL;
  2591.     }
  2592.  
  2593.     if(GadToolsBase)
  2594.     {
  2595.         CloseLibrary(GadToolsBase);
  2596.  
  2597.         GadToolsBase = NULL;
  2598.     }
  2599.  
  2600.     if(GfxBase)
  2601.     {
  2602.         CloseLibrary(GfxBase);
  2603.  
  2604.         GfxBase = NULL;
  2605.     }
  2606.  
  2607.     if(IntuitionBase)
  2608.     {
  2609.         CloseLibrary(IntuitionBase);
  2610.  
  2611.         IntuitionBase = NULL;
  2612.     }
  2613.  
  2614.     LocaleClose();
  2615.  
  2616.     if(UtilityBase)
  2617.     {
  2618.         CloseLibrary(UtilityBase);
  2619.  
  2620.         UtilityBase = NULL;
  2621.     }
  2622.  
  2623.     if(WBenchMsg)
  2624.     {
  2625.         CurrentDir(WBenchLock);
  2626.  
  2627.         if(DOSBase)
  2628.         {
  2629.             CloseLibrary(DOSBase);
  2630.  
  2631.             DOSBase = NULL;
  2632.         }
  2633.  
  2634.         Forbid();
  2635.  
  2636.         ReplyMsg((struct Message *)WBenchMsg);
  2637.     }
  2638.     else
  2639.     {
  2640.         if(CloseDOS && DOSBase)
  2641.         {
  2642.             CloseLibrary(DOSBase);
  2643.  
  2644.             DOSBase = NULL;
  2645.         }
  2646.     }
  2647. }
  2648.  
  2649.     /* OpenAll():
  2650.      *
  2651.      *    Open all required resources or return an error message
  2652.      *    if anything went wrong.
  2653.      */
  2654.  
  2655. STRPTR
  2656. OpenAll(STRPTR ConfigPath)
  2657. {
  2658.     extern    ULONG HookEntry(struct Hook *,APTR,APTR);
  2659.  
  2660.     UBYTE    PathBuffer[MAX_FILENAME_LENGTH];
  2661.     WORD    i;
  2662.     STRPTR    Result,Error,ConfigFileName = NULL;
  2663.  
  2664.         /* Don't let it hit the ground! */
  2665.  
  2666.     ConTransfer = ConProcess;
  2667.  
  2668.         /* Remember the start of this session. */
  2669.  
  2670.     DateStamp(&SessionStart);
  2671.  
  2672.         /* Reset some flags. */
  2673.  
  2674.     BinaryTransfer    = TRUE;
  2675.  
  2676.     Status        = STATUS_READY;
  2677.     Online        = FALSE;
  2678.  
  2679.     InSequence    = FALSE;
  2680.     Quiet        = FALSE;
  2681.  
  2682.         /* Set up two string gadget hooks. */
  2683.  
  2684.     CommandHook . h_Entry        = HookEntry;
  2685.     CommandHook . h_SubEntry    = CommandKey;
  2686.     CommandHook . h_Data        = NULL;
  2687.  
  2688.     PasswordHook . h_Entry        = HookEntry;
  2689.     PasswordHook . h_SubEntry    = PasswordKey;
  2690.     PasswordHook . h_Data        = NULL;
  2691.  
  2692.         /* Double buffered file locking. */
  2693.  
  2694.     NewList(&DoubleBufferList);
  2695.  
  2696.     InitSemaphore(&DoubleBufferSemaphore);
  2697.  
  2698.         /* Set up all the lists. */
  2699.  
  2700.     NewList(&PacketHistoryList);
  2701.     NewList(&EmptyList);
  2702.     NewList(&FastMacroList);
  2703.     NewList(&TransferInfoList);
  2704.  
  2705.         /* Open the translation tables. */
  2706.  
  2707.     LocaleOpen("term.catalog","english",11);
  2708.  
  2709.         /* Fill in the menu configuration. */
  2710.  
  2711.     LocalizeMenu(TermMenu,MSG_TERMDATA_PROJECT_MEN);
  2712.  
  2713.         /* Open intuition.library, any version. */
  2714.  
  2715.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  2716.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_INTUITION_LIBRARY_TXT));
  2717.  
  2718.     Forbid();
  2719.  
  2720.         /* Query the current public screen modes. */
  2721.  
  2722.     PublicModes = SetPubScreenModes(NULL);
  2723.  
  2724.         /* Set them back. */
  2725.  
  2726.     SetPubScreenModes(PublicModes);
  2727.  
  2728.     Permit();
  2729.  
  2730.         /* Open some more libraries. */
  2731.  
  2732.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
  2733.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GRAPHICS_LIBRARY_TXT));
  2734.  
  2735.         /* Install the correct routines to query
  2736.          * the rendering colours and drawing mode.
  2737.          */
  2738.  
  2739.     if(GfxBase -> LibNode . lib_Version < 39)
  2740.     {
  2741.         ReadAPen = OldGetAPen;
  2742.         ReadBPen = OldGetBPen;
  2743.         ReadDrMd = OldGetDrMd;
  2744.         SetWrMsk = OldSetWrMsk;
  2745.     }
  2746.     else
  2747.     {
  2748.         ReadAPen = NewGetAPen;
  2749.         ReadBPen = NewGetBPen;
  2750.         ReadDrMd = NewGetDrMd;
  2751.         SetWrMsk = NewSetWrMsk;
  2752.     }
  2753.  
  2754.     if(!(UtilityBase = OpenLibrary("utility.library",0)))
  2755.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_UTILITY_LIBRARY_TXT));
  2756.  
  2757.         /* Check if locale.library has already installed the operating system
  2758.          * patches required for localization.
  2759.          */
  2760.  
  2761.     LanguageCheck();
  2762.  
  2763.         /* Open the remaining libraries. */
  2764.  
  2765.     if(!(GadToolsBase = OpenLibrary("gadtools.library",0)))
  2766.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GADTOOLS_LIBRARY_TXT));
  2767.  
  2768.     if(!(AslBase = OpenLibrary("asl.library",0)))
  2769.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_ASL_LIBRARY_TXT));
  2770.  
  2771.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  2772.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_IFFPARSE_LIBRARY_TXT));
  2773.  
  2774.     if(!(CxBase = OpenLibrary("commodities.library",0)))
  2775.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_COMMODITIES_LIBRARY_TXT));
  2776.  
  2777.     if(!(DiskfontBase = (struct Library *)OpenLibrary("diskfont.library",0)))
  2778.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_DISKFONT_LIBRARY_TXT));
  2779.  
  2780.         /* Open OwnDevUnit.library, don't complain if it fails. */
  2781.  
  2782.     OwnDevUnitBase = OpenLibrary(ODU_NAME,0);
  2783.  
  2784.         /* Open workbench.library, don't complain if it fails. */
  2785.  
  2786.     WorkbenchBase = OpenLibrary("workbench.library",0);
  2787.  
  2788.     if(!(ConsoleRequest = (struct IOStdReq *)AllocVec(sizeof(struct IOStdReq),MEMF_ANY|MEMF_CLEAR)))
  2789.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CONSOLE_REQUEST_TXT));
  2790.  
  2791.     if(OpenDevice("console.device",CONU_LIBRARY,ConsoleRequest,0))
  2792.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_CONSOLE_DEVICE_TXT));
  2793.  
  2794.     ConsoleDevice = &ConsoleRequest -> io_Device -> dd_Library;
  2795.  
  2796.     if(!(FakeInputEvent = (struct InputEvent *)AllocVec(sizeof(struct InputEvent),MEMF_ANY|MEMF_CLEAR)))
  2797.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_INPUTEVENT_TXT));
  2798.  
  2799.     FakeInputEvent -> ie_Class = IECLASS_RAWKEY;
  2800.  
  2801.     if(!(MacroKeys = (struct MacroKeys *)AllocVec(sizeof(struct MacroKeys),MEMF_ANY|MEMF_CLEAR)))
  2802.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACROKEYS_TXT));
  2803.  
  2804.     if(!(CursorKeys = (struct CursorKeys *)AllocVec(sizeof(struct CursorKeys),MEMF_ANY|MEMF_CLEAR)))
  2805.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CURSORKEYS_TXT));
  2806.  
  2807.     ResetCursorKeys(CursorKeys);
  2808.  
  2809.     if(!(RequesterList = (struct Requester *)AllocVec(10 * sizeof(struct Requester),MEMF_ANY|MEMF_CLEAR)))
  2810.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_REQUESTER_DATA_TXT));
  2811.  
  2812.         /* Set up the attention buffers. */
  2813.  
  2814.     if(!(AttentionBuffers[0] = (STRPTR)AllocVec(SCAN_COUNT * 260,MEMF_ANY|MEMF_CLEAR)))
  2815.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SEQUENCE_ATTENTION_INFO_TXT));
  2816.  
  2817.     for(i = 1 ; i < SCAN_COUNT ; i++)
  2818.         AttentionBuffers[i] = &AttentionBuffers[i - 1][260];
  2819.  
  2820.         /* Obtain the default environment storage
  2821.          * path.
  2822.          */
  2823.  
  2824.     if(!ConfigPath)
  2825.     {
  2826.         ConfigPath = PathBuffer;
  2827.  
  2828.         if(!GetEnvDOS("TERMCONFIGPATH",PathBuffer))
  2829.         {
  2830.             if(!GetEnvDOS("TERMPATH",PathBuffer))
  2831.             {
  2832.                 APTR LastPtr = ThisProcess -> pr_WindowPtr;
  2833.                 BPTR FileLock;
  2834.  
  2835.                 strcpy(PathBuffer,"TERM:config");
  2836.  
  2837.                 ThisProcess -> pr_WindowPtr = (APTR)-1;
  2838.  
  2839.                 if(FileLock = Lock("TERM:",ACCESS_READ))
  2840.                     UnLock(FileLock);
  2841.                 else
  2842.                 {
  2843.                     FileLock = DupLock(ThisProcess-> pr_CurrentDir);
  2844.  
  2845.                         /* Create TERM: assignment referring to
  2846.                          * current directory.
  2847.                          */
  2848.  
  2849.                     if(!AssignLock("TERM",FileLock))
  2850.                         UnLock(FileLock);
  2851.                 }
  2852.  
  2853.                 if(!(FileLock = Lock(PathBuffer,ACCESS_READ)))
  2854.                     FileLock = CreateDir(PathBuffer);
  2855.  
  2856.                 if(FileLock)
  2857.                     UnLock(FileLock);
  2858.  
  2859.                 ThisProcess -> pr_WindowPtr = LastPtr;
  2860.             }
  2861.         }
  2862.     }
  2863.     else
  2864.     {
  2865.         if(GetFileSize(ConfigPath))
  2866.         {
  2867.             STRPTR Index;
  2868.  
  2869.             strcpy(PathBuffer,ConfigPath);
  2870.  
  2871.             Index = PathPart(PathBuffer);
  2872.  
  2873.             *Index = 0;
  2874.  
  2875.             ConfigFileName = ConfigPath;
  2876.  
  2877.             ConfigPath = PathBuffer;
  2878.         }
  2879.     }
  2880.  
  2881.         /* Check for proper assignment path if necessary. */
  2882.  
  2883.         if(!Strnicmp(ConfigPath,"TERM:",5))
  2884.         {
  2885.             APTR OldPtr = ThisProcess -> pr_WindowPtr;
  2886.             BPTR DirLock;
  2887.  
  2888.             /* Block dos requesters. */
  2889.  
  2890.             ThisProcess -> pr_WindowPtr = (APTR)-1;
  2891.  
  2892.             /* Try to get a lock on `TERM:' assignment. */
  2893.  
  2894.         if(DirLock = Lock("TERM:",ACCESS_READ))
  2895.             UnLock(DirLock);
  2896.         else
  2897.         {
  2898.                 /* Clone current directory lock. */
  2899.  
  2900.             DirLock = DupLock(ThisProcess-> pr_CurrentDir);
  2901.  
  2902.                 /* Create TERM: assignment referring to
  2903.                  * current directory.
  2904.                  */
  2905.  
  2906.             if(!AssignLock("TERM",DirLock))
  2907.                 UnLock(DirLock);
  2908.         }
  2909.  
  2910.         ThisProcess -> pr_WindowPtr = OldPtr;
  2911.         }
  2912.  
  2913.         /* Create proper path names. */
  2914.  
  2915.     if(ConfigFileName)
  2916.     {
  2917.         if(!GetFileSize(ConfigFileName))
  2918.             ConfigFileName = NULL;
  2919.     }
  2920.  
  2921.     if(!ConfigFileName)
  2922.     {
  2923.         strcpy(LastConfig,ConfigPath);
  2924.  
  2925.         AddPart(LastConfig,"term_preferences.iff",MAX_FILENAME_LENGTH);
  2926.  
  2927.         if(!GetFileSize(LastConfig))
  2928.         {
  2929.             strcpy(LastConfig,ConfigPath);
  2930.  
  2931.             AddPart(LastConfig,"term.prefs",MAX_FILENAME_LENGTH);
  2932.         }
  2933.     }
  2934.  
  2935.     strcpy(DefaultPubScreenName,"Workbench");
  2936.  
  2937.         /* Create both configuration buffers. */
  2938.  
  2939.     if(!(Config = CreateConfiguration(TRUE)))
  2940.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_PRIMARY_CONFIG_TXT));
  2941.  
  2942.     if(!(PrivateConfig = CreateConfiguration(TRUE)))
  2943.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_SECONDARY_CONFIG_TXT));
  2944.  
  2945.     ResetConfig(Config,ConfigPath);
  2946.  
  2947.         /* Read some more environment variables. */
  2948.  
  2949.     if(!WindowName[0])
  2950.     {
  2951.         if(!GetEnvDOS("TERMWINDOW",WindowName))
  2952.             strcpy(WindowName,"CON:0/11//100/term Output Window/CLOSE/SCREEN TERM");
  2953.     }
  2954.  
  2955.     GetEnvDOS("EDITOR",Config -> PathConfig -> Editor);
  2956.  
  2957.         /* Look for the default configuration file. */
  2958.  
  2959.     if(!ReadConfig(LastConfig,Config))
  2960.     {
  2961.         ResetConfig(Config,ConfigPath);
  2962.  
  2963.         Initializing = TRUE;
  2964.  
  2965.         LoadColours = TRUE;
  2966.     }
  2967.     else
  2968.     {
  2969.         switch(Config -> ScreenConfig -> ColourMode)
  2970.         {
  2971.             case COLOUR_EIGHT:
  2972.  
  2973.                 CopyMem(Config -> ScreenConfig -> Colours,ANSIColours,16 * sizeof(UWORD));
  2974.                 break;
  2975.  
  2976.             case COLOUR_SIXTEEN:
  2977.  
  2978.                 CopyMem(Config -> ScreenConfig -> Colours,EGAColours,16 * sizeof(UWORD));
  2979.                 break;
  2980.  
  2981.             case COLOUR_AMIGA:
  2982.  
  2983.                 CopyMem(Config -> ScreenConfig -> Colours,DefaultColours,16 * sizeof(UWORD));
  2984.                 break;
  2985.  
  2986.             case COLOUR_MONO:
  2987.  
  2988.                 CopyMem(Config -> ScreenConfig -> Colours,AtomicColours,16 * sizeof(UWORD));
  2989.                 break;
  2990.         }
  2991.  
  2992.         if(Config -> ScreenConfig -> ColourMode == COLOUR_AMIGA)
  2993.             Initializing = FALSE;
  2994.         else
  2995.             Initializing = TRUE;
  2996.     }
  2997.  
  2998.     if(UseNewDevice)
  2999.         strcpy(Config -> SerialConfig -> SerialDevice,NewDevice);
  3000.  
  3001.     if(UseNewUnit)
  3002.         Config -> SerialConfig -> UnitNumber = NewUnit;
  3003.  
  3004.     if(Config -> MiscConfig -> OpenFastMacroPanel)
  3005.         HadFastMacros = TRUE;
  3006.  
  3007.     strcpy(LastPhone,    Config -> PathConfig -> DefaultStorage);
  3008.     AddPart(LastPhone,    "term_phonebook.iff",MAX_FILENAME_LENGTH);
  3009.  
  3010.     if(!GetFileSize(LastPhone))
  3011.     {
  3012.         strcpy(LastPhone,    Config -> PathConfig -> DefaultStorage);
  3013.         AddPart(LastPhone,    "phonebook.prefs",MAX_FILENAME_LENGTH);
  3014.     }
  3015.  
  3016.     strcpy(LastKeys,    Config -> PathConfig -> DefaultStorage);
  3017.     AddPart(LastKeys,    "term_hotkeys.iff",MAX_FILENAME_LENGTH);
  3018.  
  3019.     if(!GetFileSize(LastKeys))
  3020.     {
  3021.         strcpy(LastKeys,    Config -> PathConfig -> DefaultStorage);
  3022.         AddPart(LastKeys,    "hotkeys.prefs",MAX_FILENAME_LENGTH);
  3023.     }
  3024.  
  3025.     strcpy(LastSpeech,    Config -> PathConfig -> DefaultStorage);
  3026.     AddPart(LastSpeech,    "term_speech.iff",MAX_FILENAME_LENGTH);
  3027.  
  3028.     if(!GetFileSize(LastSpeech))
  3029.     {
  3030.         strcpy(LastSpeech,    Config -> PathConfig -> DefaultStorage);
  3031.         AddPart(LastSpeech,    "speech.prefs",MAX_FILENAME_LENGTH);
  3032.     }
  3033.  
  3034.     strcpy(LastFastMacros,    Config -> PathConfig -> DefaultStorage);
  3035.     AddPart(LastFastMacros,    "term_fastmacros.iff",MAX_FILENAME_LENGTH);
  3036.  
  3037.     if(!GetFileSize(LastFastMacros))
  3038.     {
  3039.         strcpy(LastFastMacros,    Config -> PathConfig -> DefaultStorage);
  3040.         AddPart(LastFastMacros,    "fastmacros.prefs",MAX_FILENAME_LENGTH);
  3041.     }
  3042.  
  3043.     if(Config -> FileConfig -> MacroFileName[0])
  3044.         strcpy(LastMacros,Config -> FileConfig -> MacroFileName);
  3045.     else
  3046.     {
  3047.         strcpy(LastMacros,    Config -> PathConfig -> DefaultStorage);
  3048.         AddPart(LastMacros,    "term_macros.iff",MAX_FILENAME_LENGTH);
  3049.  
  3050.         if(!GetFileSize(LastMacros))
  3051.         {
  3052.             strcpy(LastMacros,    Config -> PathConfig -> DefaultStorage);
  3053.             AddPart(LastMacros,    "macros.prefs",MAX_FILENAME_LENGTH);
  3054.  
  3055.             if(!GetFileSize(LastMacros))
  3056.             {
  3057.                 strcpy(LastMacros,    Config -> PathConfig -> DefaultStorage);
  3058.                 AddPart(LastMacros,    "functionkeys.prefs",MAX_FILENAME_LENGTH);
  3059.             }
  3060.         }
  3061.     }
  3062.  
  3063.         /* Load the keyboard macros. */
  3064.  
  3065.     if(!LoadMacros(LastMacros,MacroKeys))
  3066.     {
  3067.         for(i = 0 ; i < 4 ; i++)
  3068.             strcpy(MacroKeys -> Keys[1][i],FunctionKeyCodes[i]);
  3069.     }
  3070.  
  3071.     if(Config -> FileConfig -> CursorFileName[0])
  3072.         strcpy(LastCursorKeys,Config -> FileConfig -> CursorFileName);
  3073.     else
  3074.     {
  3075.         strcpy(LastCursorKeys,    Config -> PathConfig -> DefaultStorage);
  3076.         AddPart(LastCursorKeys,    "cursorkeys.prefs",MAX_FILENAME_LENGTH);
  3077.     }
  3078.  
  3079.         /* Load the cursor keys. */
  3080.  
  3081.     if(!ReadIFFData(LastCursorKeys,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  3082.         ResetCursorKeys(CursorKeys);
  3083.  
  3084.         /* Are we to load the translation tables? */
  3085.  
  3086.     strcpy(LastTranslation,Config -> FileConfig -> TranslationFileName);
  3087.  
  3088.     if(Config -> FileConfig -> TranslationFileName[0])
  3089.     {
  3090.         if(SendTable = AllocTranslationTable())
  3091.         {
  3092.             if(ReceiveTable = AllocTranslationTable())
  3093.             {
  3094.                 if(!LoadTranslationTables(Config -> FileConfig -> TranslationFileName,SendTable,ReceiveTable))
  3095.                 {
  3096.                     FreeTranslationTable(SendTable);
  3097.  
  3098.                     SendTable = NULL;
  3099.  
  3100.                     FreeTranslationTable(ReceiveTable);
  3101.  
  3102.                     ReceiveTable = NULL;
  3103.                 }
  3104.                 else
  3105.                 {
  3106.                     if(IsStandardTable(SendTable) && IsStandardTable(ReceiveTable))
  3107.                     {
  3108.                         FreeTranslationTable(SendTable);
  3109.  
  3110.                         SendTable = NULL;
  3111.  
  3112.                         FreeTranslationTable(ReceiveTable);
  3113.  
  3114.                         ReceiveTable = NULL;
  3115.                     }
  3116.                 }
  3117.             }
  3118.             else
  3119.             {
  3120.                 FreeTranslationTable(SendTable);
  3121.  
  3122.                 SendTable = NULL;
  3123.             }
  3124.         }
  3125.     }
  3126.  
  3127.     ConOutputUpdate();
  3128.  
  3129.         /* Load the fast! macro settings. */
  3130.  
  3131.     LoadFastMacros(LastFastMacros);
  3132.  
  3133.         /* Load the speech settings. */
  3134.  
  3135.     if(!ReadIFFData(LastSpeech,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  3136.     {
  3137.         SpeechConfig . Rate        = DEFRATE;
  3138.         SpeechConfig . Pitch        = DEFPITCH;
  3139.         SpeechConfig . Frequency    = DEFFREQ;
  3140.         SpeechConfig . Volume        = DEFVOL;
  3141.         SpeechConfig . Sex        = DEFSEX;
  3142.         SpeechConfig . Enabled        = FALSE;
  3143.     }
  3144.  
  3145.         /* Load the hotkey settings. */
  3146.  
  3147.     if(!LoadHotkeys(LastKeys,&Hotkeys))
  3148.     {
  3149.         strcpy(Hotkeys . termScreenToFront,    "lshift rshift return");
  3150.         strcpy(Hotkeys . BufferScreenToFront,    "control rshift return");
  3151.         strcpy(Hotkeys . SkipDialEntry,        "control lshift rshift return");
  3152.         strcpy(Hotkeys . AbortARexx,        "lshift rshift escape");
  3153.  
  3154.         Hotkeys . CommodityPriority    = 0;
  3155.         Hotkeys . HotkeysEnabled    = TRUE;
  3156.     }
  3157.  
  3158.         /* Initialize the data flow parser. */
  3159.  
  3160.     FlowInit(TRUE);
  3161.  
  3162.         /* Set up parsing jump tables. */
  3163.  
  3164.     if(!(SpecialTable = (JUMP *)AllocVec(256 * sizeof(JUMP),MEMF_CLEAR | MEMF_ANY)))
  3165.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3166.  
  3167.     for(i = 0 ; i < sizeof(SpecialKeys) / sizeof(struct SpecialKey) ; i++)
  3168.         SpecialTable[SpecialKeys[i] . Key] = (JUMP)SpecialKeys[i] . Routine;
  3169.  
  3170.     if(!(AbortTable = (JUMP *)AllocVec(256 * sizeof(JUMP),MEMF_ANY)))
  3171.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3172.  
  3173.     for(i = 0 ; i < 256 ; i++)
  3174.     {
  3175.         switch(AbortMap[i])
  3176.         {
  3177.             case 0:    AbortTable[i] = (JUMP)ParseCode;
  3178.                 break;
  3179.  
  3180.             case 1:    AbortTable[i] = (JUMP)DoCancel;
  3181.                 break;
  3182.  
  3183.             case 2:    AbortTable[i] = (JUMP)DoNewEsc;
  3184.                 break;
  3185.  
  3186.             case 3:    AbortTable[i] = (JUMP)DoNewCsi;
  3187.                 break;
  3188.         }
  3189.     }
  3190.  
  3191.     for(i = GLIST_UPLOAD ; i <= GLIST_WAIT ; i++)
  3192.     {
  3193.         if(!(GenericListTable[i] = CreateGenericList()))
  3194.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3195.     }
  3196.  
  3197.         /* Set up the serial driver. */
  3198.  
  3199.     if(Error = CreateSerial())
  3200.     {
  3201.         MyEasyRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error);
  3202.  
  3203.         DeleteSerial();
  3204.     }
  3205.     else
  3206.     {
  3207.         if(SerialMessage)
  3208.         {
  3209.             MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  3210.  
  3211.             SerialMessage = NULL;
  3212.         }
  3213.     }
  3214.  
  3215.         /* Get two signal bits. */
  3216.  
  3217.     if((ClipBit = AllocSignal(-1)) == -1)
  3218.         return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_CLIP_SIGNAL_TXT));
  3219.  
  3220.     if((CheckBit = AllocSignal(-1)) == -1)
  3221.         return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_CHECK_SIGNAL_TXT));
  3222.  
  3223.         /* Load alternative beep sound if desired. */
  3224.  
  3225.     if(Config -> TerminalConfig -> BeepFileName[0])
  3226.         OpenSound(Config -> TerminalConfig -> BeepFileName);
  3227.  
  3228.     if(!CreateBeep())
  3229.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_AUDIO_DEVICE_TXT));
  3230.  
  3231.     if(!(TimePort = (struct MsgPort *)CreateMsgPort()))
  3232.         return(LocaleString(MSG_GLOBAL_FAILED_TO_CREATE_MSGPORT_TXT));
  3233.  
  3234.     if(!(TimeRequest = (struct timerequest *)CreateIORequest(TimePort,sizeof(struct timerequest))))
  3235.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_IOREQUEST_TXT));
  3236.  
  3237.     if(OpenDevice("timer.device",UNIT_VBLANK,TimeRequest,0))
  3238.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_TIMER_DEVICE_TXT));
  3239.  
  3240.     TimerBase = &TimeRequest -> tr_node . io_Device -> dd_Library;
  3241.  
  3242.         /* Start the clipboard serverprocess. */
  3243.  
  3244.     Forbid();
  3245.  
  3246.     if(ClipProcess = CreateNewProcTags(
  3247.         NP_Entry,    ClipServer,
  3248.         NP_Name,    "term clipboard process",
  3249.         NP_WindowPtr,    -1,
  3250.         NP_Priority,    20,
  3251.     TAG_DONE))
  3252.     {
  3253.         SetSignal(0,SIG_HANDSHAKE);
  3254.  
  3255.         Wait(SIG_HANDSHAKE);
  3256.     }
  3257.  
  3258.     Permit();
  3259.  
  3260.     if(!ClipProcess)
  3261.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_CLIPBOARD_SERVER_TXT));
  3262.  
  3263.         /* Add the global term port. */
  3264.  
  3265.     if(!TermPort)
  3266.     {
  3267.         if(!(TermPort = (struct TermPort *)AllocVec(sizeof(struct TermPort) + 11,MEMF_PUBLIC|MEMF_CLEAR)))
  3268.             return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_GLOBAL_PORT_TXT));
  3269.         else
  3270.         {
  3271.             NewList(&TermPort -> ExecNode . mp_MsgList);
  3272.  
  3273.             InitSemaphore(&TermPort -> OpenSemaphore);
  3274.  
  3275.             TermPort -> ExecNode . mp_Flags            = PA_IGNORE;
  3276.             TermPort -> ExecNode . mp_Node . ln_Name    = (char *)(TermPort + 1);
  3277.  
  3278.             strcpy(TermPort -> ExecNode . mp_Node . ln_Name,"term Port");
  3279.  
  3280.             AddPort(&TermPort -> ExecNode);
  3281.         }
  3282.     }
  3283.  
  3284.         /* Keep another term task from removing the port. */
  3285.  
  3286.     TermPort -> HoldIt = TRUE;
  3287.  
  3288.         /* Install a new term process. */
  3289.  
  3290.     ObtainSemaphore(&TermPort -> OpenSemaphore);
  3291.  
  3292.     TermPort -> OpenCount++;
  3293.  
  3294.     TermPort -> HoldIt = FALSE;
  3295.  
  3296.     TermID = TermPort -> ID++;
  3297.  
  3298.     ReleaseSemaphore(&TermPort -> OpenSemaphore);
  3299.  
  3300.         /* Set up the ID string. */
  3301.  
  3302.     if(TermID)
  3303.         SPrintf(TermIDString,"TERM.%ld",TermID);
  3304.     else
  3305.         strcpy(TermIDString,"TERM");
  3306.  
  3307.     if(RexxPortName[0])
  3308.     {
  3309.         WORD i;
  3310.  
  3311.         for(i = 0 ; i < strlen(RexxPortName) ; i++)
  3312.             RexxPortName[i] = ToUpper(RexxPortName[i]);
  3313.  
  3314.         if(FindPort(RexxPortName))
  3315.             RexxPortName[0] = 0;
  3316.     }
  3317.  
  3318.     if(!RexxPortName[0])
  3319.         strcpy(RexxPortName,TermIDString);
  3320.  
  3321.         /* Install the hotkey handler. */
  3322.  
  3323.     SetupCx();
  3324.  
  3325.         /* Allocate the first few lines for the display buffer. */
  3326.  
  3327.     if(!CreateBuffer())
  3328.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_VIEW_BUFFER_TXT));
  3329.  
  3330.     if(!(XprIO = (struct XPR_IO *)AllocVec(sizeof(struct XPR_IO),MEMF_ANY|MEMF_CLEAR)))
  3331.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_PROTOCOL_BUFFER_TXT));
  3332.  
  3333.         /* Set up the external emulation macro data. */
  3334.  
  3335.     if(!(XEM_MacroKeys = (struct XEmulatorMacroKey *)AllocVec((2 + 10 * 4) * sizeof(struct XEmulatorMacroKey),MEMF_ANY|MEMF_CLEAR)))
  3336.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACRO_KEY_DATA_TXT));
  3337.  
  3338.     strcpy(LastXprLibrary,Config -> FileConfig -> ProtocolFileName);
  3339.  
  3340.     ProtocolSetup();
  3341.  
  3342.         /* Load a keymap file if required. */
  3343.  
  3344.     if(Config -> TerminalConfig -> KeyMapFileName[0])
  3345.         KeyMap = LoadKeyMap(Config -> TerminalConfig -> KeyMapFileName);
  3346.  
  3347. #ifdef USE_AREXX
  3348.  
  3349.     if(!(TermRexxPort = (struct MsgPort *)CreateMsgPort()))
  3350.         return(LocaleString(MSG_GLOBAL_FAILED_TO_CREATE_MSGPORT_TXT));
  3351.  
  3352.         /* If rexxsyslib.library opens cleanly it's time for
  3353.          * us to create the background term Rexx server.
  3354.          */
  3355.  
  3356.     if(RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME,0))
  3357.     {
  3358.             /* Create a background process handling the
  3359.              * rexx messages asynchronously.
  3360.              */
  3361.  
  3362.         Forbid();
  3363.  
  3364.         if(RexxProcess = (struct Process *)CreateNewProcTags(
  3365.             NP_Entry,    RexxServer,
  3366.             NP_Name,    "term Rexx process",
  3367.             NP_Priority,    5,
  3368.             NP_StackSize,    8192,
  3369.             NP_WindowPtr,    -1,
  3370.         TAG_END))
  3371.         {
  3372.             SetSignal(0,SIG_HANDSHAKE);
  3373.  
  3374.             Wait(SIG_HANDSHAKE);
  3375.         }
  3376.  
  3377.         Permit();
  3378.  
  3379.         if(!RexxProcess)
  3380.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_AREXX_PROCESS_TXT));
  3381.     }
  3382.  
  3383. #endif    /* USE_AREXX */
  3384.  
  3385.         /* Start the input kludge handler if necessary. */
  3386.  
  3387.     CreateInputHandler();
  3388.  
  3389.         /* Install the public screen name, assumes that the user
  3390.          * wants the window to be opened on the screen, rather than
  3391.          * opening a custom screen.
  3392.          */
  3393.  
  3394.     if(SomePubScreenName[0])
  3395.     {
  3396.         strcpy(Config -> ScreenConfig -> PubScreenName,SomePubScreenName);
  3397.  
  3398.         Config -> ScreenConfig -> Blinking    = FALSE;
  3399.         Config -> ScreenConfig -> FasterLayout    = FALSE;
  3400.         Config -> ScreenConfig -> UseWorkbench    = TRUE;
  3401.  
  3402.         SomePubScreenName[0] = 0;
  3403.     }
  3404.  
  3405.         /* Create the whole display. */
  3406.  
  3407.     if(Result = CreateDisplay(TRUE))
  3408.         return(Result);
  3409.     else
  3410.     {
  3411.         PubScreenStuff();
  3412.  
  3413.         return(NULL);
  3414.     }
  3415. }
  3416.