home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / term-source.lha / Init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  113.0 KB  |  5,246 lines

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