home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 1 / CD_Magazyn_EXEC_nr_1.iso / eXec / FAQ / ParM.lzx / ParM_V6.02 / ParM.c.bak < prev    next >
Text File  |  1989-05-26  |  23KB  |  871 lines

  1. /*
  2.  *    $VER:ParM.c - (02.07.95) 18:58:24 Copyright © 1990 by S.R. & P.C.
  3.  *
  4.  *      Created:        01 Jul 1990
  5.  *      Modified:    02 Jul 1995    18:58:24
  6.  *
  7.  *      Make>> smake
  8.  *
  9.  *    17/06/95:
  10.  *        start Add ScreenNotify support.
  11.  *    18/06/95:
  12.  *        clean up source: localise some var.
  13.  *    19/06/95:
  14.  *        remove MYMENU & CLI support. Is there somebody still using it ?. This make src difficult to understand
  15.  * 02/07/95:
  16.  *        ParM don't always reopen its window after screen change. try to fix that by try 10 time to open window
  17.  *        Now handle Ctrl-C and quit
  18.  * 02/16/95:
  19.  *        Now never reopen window, ScreenNotifyTest don't too...search for the pb...
  20.  *        Found the pb. This due of a bug of NewMode 3.6
  21.  */
  22.  
  23. #include "lib/ParMBase.h"
  24. #include <proto/ParM.h>
  25. #include <retina/retina.h>
  26. #include <proto/retina.h>
  27.  
  28. #include <Libraries/VMem.h>
  29. #include <proto/VMem.h>
  30.  
  31. #include "VMM_Stat.h"
  32. #include "pragmas/VMM_pragmas.h"
  33. #include "clib/VMM_protos.h"
  34.  
  35. #include <libraries/screennotify.h>
  36. #include <proto/ScreenNotify.h>
  37.  
  38. #include <clib.h>
  39.  
  40. #include "ParM.h"
  41.  
  42. #define CATCOMP_NUMBERS
  43. #include "Lib/CtlgStrings.h"
  44.  
  45. #define BASE_TIME    100000    /* the base time for all time operations: 10 time per seconds */
  46.  
  47. #define THE_END            0
  48. #define NO_MENUS        10
  49. #define NO_WIN            20
  50.  
  51. #define OPEN_ITEM        0
  52. #define UPDATE_ITEM        1
  53. #define STD_CFG_ITEM    2
  54. #define COMMAND_ITEM    4
  55. #define CHDIR_ITEM        6
  56. #define QUIT_ITEM        8
  57.  
  58. /* MakeDateTimeString( ) arg */
  59. #define DATE_STRING        1
  60. #define TIME_STRING        2
  61.  
  62. #define MAX_PATH_LEN    255
  63.  
  64. #define MEMF_PHYS MEMF_PUBLIC    /* workaround to lack of this flags */
  65.  
  66. /*****                global variables                    *****/
  67.  
  68. extern struct ParMConfig *ParMConfig;
  69. extern struct ParMOpt ParMOpt;
  70. extern struct NewWindow NWS;
  71. extern char DefaultConfigFile[ ];
  72. extern char CopyRight[ ];
  73.  
  74. static struct Process *ParMProcess;
  75. static BOOL DoSetRast = TRUE;    /* Clear window on Open */
  76. static BOOL DoNextSelect;
  77. static struct DateStamp Time;    /* the System Time */
  78. static struct TextAttr TextAttr;
  79. static struct TextFont *Font;
  80. static char InfoString[ 80];
  81. static char Dir[ MAX_PATH_LEN + 1];
  82. static char File[ 34];
  83. static char DateFmt[ 20];
  84.  
  85. struct RetinaBase *RetinaBase;
  86. struct Library *VMemBase;
  87.  
  88. static BOOL IsVisible( struct Window *Window)
  89. {
  90.     if (  Window->WScreen == IntuitionBase->ActiveScreen)
  91.         return TRUE;
  92.  
  93. //KPrintF(  "IntuitionBase->ActiveScreen->TopEdge:%ld, (  Window->TopEdge + Window->Height):%ld\n", IntuitionBase->ActiveScreen->TopEdge, (  Window->TopEdge + Window->Height));
  94.     if (  IntuitionBase->ActiveScreen->TopEdge > (  Window->TopEdge + Window->Height))
  95.         return TRUE;
  96.  
  97.     return FALSE;
  98. }
  99.  
  100.  
  101. static void CleanupMenus( APTR VisualInfo, struct Menu *Menu)
  102. {
  103.     LayoutMenus(  Menu, VisualInfo,
  104.         GTMN_NewLookMenus, ( ParMOpt.Flags & PF_NEWLOOKMENUS) || ( ParMOpt.ParMMode == MODE_MYMENU),
  105.         TAG_DONE);
  106. }
  107.  
  108. static struct Menu *CreateParMMenus(  APTR VisualInfo, short Color)
  109. {
  110.     static struct NewMenu NewMenus[ ] =
  111.     {
  112.         {NM_TITLE, "ParM", 0, 0, 0, 0},
  113.         {NM_ITEM, NULL, 0, 0, 0, 0},
  114.         {NM_ITEM, NULL, 0, 0, 0, 0},
  115.         {NM_ITEM, NULL, 0, 0, 0, 0},
  116.         {NM_ITEM, NM_BARLABEL, 0, 0, 0, 0},
  117.         {NM_ITEM, NULL, 0, 0, 0, 0},
  118.         {NM_ITEM, NM_BARLABEL, 0, 0, 0, 0},
  119.         {NM_ITEM, NULL, 0, 0, 0, 0},
  120.         {NM_ITEM, NM_BARLABEL, 0, 0, 0, 0},
  121.         {NM_ITEM, NULL, 0, 0, 0, 0},
  122.         {NM_END}
  123.     };
  124.     long StrID = ParM_MSG_ParM_Open_MENU;
  125.     struct NewMenu *NM;
  126.     char *s;
  127.     struct Menu *ParMMenu;
  128.  
  129.     NM = &NewMenus[ 1];
  130.     while ( NM->nm_Type != NM_END)
  131.     {
  132.         if ( NM->nm_Label != NM_BARLABEL)
  133.         {
  134.             s = GetParMCatalogStr( StrID++);
  135.             if ( *s == '_')
  136.             {
  137.                 NM->nm_CommKey = s + 1;
  138.                 s += 3;
  139.             }
  140.             NM->nm_Label = s;
  141.         }
  142.         NM++;
  143.     }
  144.     if ( ParMMenu = CreateMenus( NewMenus, GTMN_FrontPen, Color, TAG_DONE))
  145.         CleanupMenus( VisualInfo, ParMMenu);
  146.  
  147.     return ParMMenu;
  148. }
  149.  
  150. static void ClearMenus(  struct Window *ParMWindow)
  151. {
  152.     if ( ParMOpt.ParMMode == MODE_MYMENU)
  153.     {
  154.     }
  155.     else
  156.         ClearMenuStrip( ParMWindow);
  157. }
  158.  
  159. void UpDateMenus( APTR VisualInfo, struct Window *ParMWindow, struct Menu *ParMMenu)
  160. {
  161.     ClearMenus( ParMWindow);
  162.  
  163.     ParMConfig->LinkMenu = ParMMenu;
  164.     ParMConfig->MenuPen = ParMOpt.ParMenuColor;
  165.  
  166.     if ( ParseMenus( ParMConfig))
  167.         CleanupMenus( VisualInfo, ParMConfig->LinkMenu);
  168.  
  169.     if ( ParMOpt.ParMMode == MODE_MYMENU)
  170.         ;
  171.     else
  172.         SetMenuStrip( ParMWindow, ParMMenu);
  173. }
  174.  
  175.  
  176. void InitDateFmt( void)
  177. {
  178.     struct DateTime dt;
  179.     char Date[ 18];
  180.  
  181.     memset( &dt, 0, sizeof ( struct DateTime));
  182.  
  183.     dt.dat_StrDate = Date;
  184.     DateToStr( &dt);
  185.     SPrintf( DateFmt, "%%-%lds %%10.10s  %%s", strlen( Date));
  186. }
  187.  
  188.  
  189. void MakeDateTimeString( char *dest, short Type)
  190. {
  191.     struct DateTime dt;
  192.     char Date[ 18], Time[ 18], Day[ 18];
  193.  
  194.     memset( &dt, 0, sizeof ( struct DateTime));
  195.  
  196.     DateStamp( &dt.dat_Stamp);
  197.     if ( Type == DATE_STRING)
  198.     {
  199.         dt.dat_Flags = DTF_FUTURE;
  200.         dt.dat_StrDay = Day;
  201.         dt.dat_StrDate = Date;
  202.         dt.dat_StrTime = Time;
  203.     }
  204.     else
  205.         dt.dat_StrTime = dest;
  206.     DateToStr( &dt);
  207.     if ( Type == DATE_STRING)
  208.     {
  209.         /* show week day only if both mem & time options are on */
  210.         if ( ParMOpt.Flags & PF_SHOWTIME)
  211.             SPrintf( dest, DateFmt, Day, Date, Time);
  212.         else
  213.             SPrintf( dest, "%10.10s  %s", Date, Time);
  214.     }
  215. }
  216.  
  217. #define RETINA_MEM_FMT    "Retina:%6lD  "
  218. #define VMEM_MEM_FMT "  VMem:%7lD"    /* space at start because it was at last position */
  219.  
  220. static void MakeInfoString(  struct Window *ParMWindow)
  221. {
  222.     long chip_free, fast_free;
  223.     short i = 0;
  224.  
  225.     if (  ParMWindow == IntuitionBase->ActiveWindow && ( ParMOpt.Flags & PF_SHOWMEM))
  226.         MakeDateTimeString( InfoString, DATE_STRING);
  227.     else
  228.     {
  229.         if ( ParMOpt.Flags & PF_SHOWMEM)
  230.         {
  231.             chip_free = AvailMem(  MEMF_CHIP) >> 10;
  232.             fast_free = AvailMem(  MEMF_FAST | MEMF_PHYS) >> 10;
  233.             if ( RetinaBase)
  234.                 i = SPrintf( InfoString, RETINA_MEM_FMT, Retina_AvailMem( 0) >> 10);
  235.             i += SPrintf( &InfoString[ i], GetParMCatalogStr( ParM_MSG_Mem_Fmt), chip_free, fast_free);
  236.             if (  FindPort(  "VMM_Port"))    /* avoid starting of VMM caused by vmm.library function call */
  237.             {
  238.                 struct Library *VMMBase;
  239.  
  240.                 if (  VMMBase = OpenLibrary(  "vmm.library", 1L))    /* since VMM port exist, this imply that vmm.library was alredy opened so that this OpenLibrary( ) don't cause any disk access */
  241.                 {
  242.                     i += SPrintf(  &InfoString[  i], VMEM_MEM_FMT, AvailVMem(  MEMF_ANY) >> 10);
  243.                     CloseLibrary( VMMBase);
  244.                 }
  245.                 else
  246.                     i += SPrintf(  &InfoString[  i], VMEM_MEM_FMT, 0);
  247.             }
  248.             else if (  VMemBase)
  249.                 i += SPrintf(  &InfoString[  i], VMEM_MEM_FMT, VMAvailMem( MEMF_ANY, VMEMF_VIRTUAL) >> 10);
  250.         }
  251.         if ( ParMOpt.Flags & PF_SHOWTIME)
  252.         {
  253.             if ( ParMOpt.Flags & PF_SHOWMEM)
  254.             {
  255.                 InfoString[ i++] = ' ';
  256.                 InfoString[ i++] = ' ';
  257.             }
  258.             MakeDateTimeString( &InfoString[ i], TIME_STRING);
  259.         }
  260.     }
  261. }
  262.  
  263.  
  264. static void MakeInfoStringInit( void)
  265. {
  266.     struct DateTime dt;
  267.     char Time[ 18];
  268.     short i = 0;
  269.  
  270.     if ( ParMOpt.Flags & PF_SHOWMEM)
  271.     {
  272.         BOOL VMMInstalled = FALSE;
  273.  
  274.         if ( RetinaBase)
  275.             i = SPrintf( InfoString, RETINA_MEM_FMT, 8888L);
  276.         i += SPrintf( &InfoString[ i], GetParMCatalogStr( ParM_MSG_Mem_Fmt), 8888L, 88888L);
  277.         {
  278. //            struct Library *VMMBase;
  279. //            if (  VMMBase = OpenLibrary(  "vmm.library", 1L))
  280. //            {
  281. //                /* don't use any function or this start VMM */
  282. //                VMMInstalled = TRUE;
  283. //                CloseLibrary( VMMBase);
  284. //            }
  285.             BPTR VMMLock;
  286.  
  287.             /* I use Lock( ) because loading vmm.library start VMM. I just want to know if VMM is installed */
  288.             if (  VMMLock = Lock(  "Libs:VMM.library", ACCESS_READ))
  289.             {
  290.                 VMMInstalled = TRUE;
  291.                 UnLock(  VMMLock);
  292.             }
  293.         }
  294.         if (  VMemBase || VMMInstalled)
  295.             i += SPrintf( &InfoString[ i], VMEM_MEM_FMT, 888888L);
  296.     }
  297.     if ( ParMOpt.Flags & PF_SHOWTIME)
  298.     {
  299.         if ( ParMOpt.Flags & PF_SHOWMEM)
  300.         {
  301.             InfoString[ i++] = ' ';
  302.             InfoString[ i++] = ' ';
  303.         }
  304.         memset( &dt, 0, sizeof ( struct DateTime));
  305.  
  306.         dt.dat_StrTime = Time;
  307.         DateToStr( &dt);
  308.         strcpy( &InfoString[ i], Time);
  309.     }
  310. }
  311.  
  312.  
  313. static void RefreshInfoString(  struct Window *ParMWindow)
  314. {
  315.     struct RastPort *rp;
  316.  
  317.     if (  ParMWindow && ( ParMOpt.Flags & ( PF_SHOWMEM | PF_SHOWTIME)))
  318.     {
  319.         MakeInfoString(  ParMWindow);
  320.         rp = ParMWindow->RPort;
  321.         if ( DoSetRast)
  322.         {
  323.             SetRast( rp, ParMOpt.MTBlockPen);
  324.             DoSetRast = FALSE;
  325.         }
  326.         SetAPen( rp, ParMOpt.MTDetailPen);
  327.         Move( rp, 4, Font->tf_Baseline + 1);
  328.         Text( rp, InfoString, strlen( InfoString));
  329.         if ( rp->cp_x < ParMWindow->Width)
  330.         {
  331.             SetAPen( rp, ParMOpt.MTBlockPen);
  332.             RectFill( rp, rp->cp_x, 1, ParMWindow->Width, Font->tf_YSize + 1);
  333.         }
  334.     }
  335. }
  336.  
  337.  
  338. /* change parm current directory */
  339. static void ChangeDir( char *dir)
  340. {
  341.     BPTR NewLock, OldLock;
  342.  
  343.     if ( NewLock = Lock( dir, ACCESS_READ))
  344.     {
  345.         if ( OldLock = CurrentDir( NewLock))
  346.             UnLock( OldLock);
  347.     }
  348. }
  349.  
  350.  
  351. static BOOL GetFileDir( char *Path, struct TagItem *Tags)
  352. {
  353.     struct Library *AslBase;
  354.     struct FileRequester *FR;
  355.     BOOL Ok = FALSE;
  356.  
  357.     if ( !( AslBase = OpenLibrary( "asl.library", 37)))
  358.     {
  359.         SimpleReq( GetParMCatalogStr( ParM_MSG_RequiresAsl));
  360.         return FALSE;
  361.     }
  362.     if ( FR = ( struct FileRequester *)AllocAslRequest( ASL_FileRequest, Tags))
  363.     {
  364.         if ( Ok = AslRequestTags( FR, TAG_DONE))
  365.         {
  366.             strcpy( Path, ( char *)FR->fr_Drawer);
  367.             AddPart( Path, FR->fr_File, MAX_PATH_LEN);
  368.         }
  369.         FreeAslRequest( FR);
  370.     }
  371.     CloseLibrary( AslBase);
  372.     return Ok;
  373. }
  374.  
  375.  
  376. static struct TagItem CfgTags[ ] =
  377. {
  378.     {ASLFR_TitleText, NULL},
  379.     {ASLFR_Flags1, FRB_DOPATTERNS},
  380.     {ASLFR_InitialPattern, ( ULONG) "#?.cfg"},
  381.     {ASLFR_InitialDrawer, ( ULONG) Dir},
  382.     {ASLFR_InitialFile, ( ULONG) File},
  383.     {TAG_DONE}
  384. };
  385.  
  386.  
  387. static void OpenMenus( struct Window *ParMWindow)
  388. {
  389.     strcpy( Dir, ParMConfig->CurCfg);
  390.     strcpy( File, FilePart( Dir));
  391.     *PathPart( Dir) = '\0';    /* cut off filename from fr_Dir */
  392.     CfgTags[ 0].ti_Data = ( ULONG) GetParMCatalogStr( ParM_MSG1L_Open);
  393.     if ( !GetFileDir( Dir, CfgTags))
  394.         return;        /* quit if Open canceled */
  395.     if ( strlen( Dir) >= 80)
  396.     {
  397.         SimpleReq( GetParMCatalogStr( ParM_MSG_PathTooLong));
  398.         return;
  399.     }
  400.     strcpy( ParMConfig->CurCfg, Dir);
  401. }
  402.  
  403. static void GetNewDir( void)
  404. {
  405.     struct TagItem DirTags[ ] =
  406.     {
  407.         {ASLFR_TitleText, NULL},
  408.         {ASLFR_Flags2, FRB_DRAWERSONLY},
  409.         {ASLFR_InitialDrawer, ( ULONG) Dir},
  410.         {TAG_DONE}
  411.     };
  412.     NameFromLock( ParMProcess->pr_CurrentDir, Dir, MAX_PATH_LEN);
  413.     DirTags[ 0].ti_Data = ( ULONG) GetParMCatalogStr( ParM_MSG_EnterNewDir);
  414.     if ( GetFileDir( Dir, DirTags))
  415.         ChangeDir( Dir);
  416. }
  417.  
  418.  
  419. /* Print busy message in place of window title */
  420.  
  421. static void Busy(  struct Window *ParMWindow, BOOL status)
  422. {
  423.     if ( ParMOpt.ParMMode == MODE_WINDOW)
  424.     {
  425.         SetWindowTitles(  ParMWindow, ( status) ? GetParMCatalogStr( ParM_MSG_Busy) : "ParM", CopyRight);
  426.         if ( ParMOpt.Flags & ( PF_SHOWMEM | PF_SHOWTIME))
  427.         {
  428.             if ( status)    /* Busy */
  429.                 RefreshWindowFrame(  ParMWindow);
  430.             else
  431.                 SetRast(  ParMWindow->RPort, ParMOpt.MTBlockPen);
  432.         }
  433.     }
  434. }
  435.  
  436.  
  437. static void DoExtMenu( APTR VisualInfo, struct Window *ParMWindow, struct Menu *ParMMenu, UWORD MenuNum)
  438. {
  439.     struct MenuInfo *MI;
  440.     struct Extended_WBStartup *EWBS;
  441.  
  442.     MI = GTMENUITEM_USERDATA( ItemAddress( ParMMenu, MenuNum));
  443.     switch ( MI->mi_RunMode)
  444.     {
  445.         case RM_Run:
  446.         case RM_Shell:
  447.             RunTags( ParMConfig, &MI->mi_RunInfo, PR_Mode, MI->mi_RunMode, TAG_DONE);
  448.             break;
  449.         case RM_WorkBench:
  450.             if ( ( EWBS = MakeWBStartup( ParMConfig->ReqTitle, &MI->mi_RunInfo))
  451.                 && WBRunTags( ParMConfig->ReqTitle, EWBS, WBRun_Pri, MI->mi_RunInfo.ri_Pri, TAG_DONE)
  452.                 && ( MI->mi_RunInfo.ri_Flags & RIF_WBTOFRONT))
  453.             {
  454.                 WBenchToFront( );
  455.             }
  456.             break;
  457.         case RM_Cfg:        /* new cfg */
  458.             strcpy( ParMConfig->CurCfg, MI->mi_RunInfo.ri_Cmd);
  459.             UpDateMenus( VisualInfo, ParMWindow, ParMMenu);
  460.             DoNextSelect = FALSE;    /* Tell DoIntuiMsg not to execute next menu selection */
  461.             break;
  462.     }
  463. }
  464.  
  465. static struct Window *OpenParMWindow( STRPTR HostPubScreenName)
  466. {
  467.     struct IntuiText IT;
  468.     struct Window *ParMWindow;
  469.     struct NewWindow NewWindow = NWS;
  470.     struct Screen *HostPubScreenScreen;
  471.  
  472.     if ( HostPubScreenScreen = LockPubScreen( HostPubScreenName))
  473.     {
  474.         AskFont( &HostPubScreenScreen ->RastPort, &TextAttr);    /* Get screen font */
  475.         Font = OpenFont( &TextAttr);
  476.         IT.ITextFont = &TextAttr;
  477.  
  478.         /* Screen->BarHeight is one pixel less than true height */
  479.         NewWindow.Height = HostPubScreenScreen ->BarHeight;
  480.         if ( DOSBase->dl_lib.lib_Version < 39)
  481.             NewWindow.Height++;    /* In V39, do not hide screen's title bar bevel effect */
  482.  
  483.         /* calculate the real lenght of string ParM ( font can be proportional in 2.0) */
  484.         /* if the font is proportional tf_YSize chould contain max char size */
  485.         if ( ParMOpt.Flags & ( PF_SHOWMEM | PF_SHOWTIME))
  486.         {
  487.             MakeInfoStringInit( );
  488.             IT.IText = InfoString;
  489.         }
  490.         else
  491.         {
  492.             IT.IText = "ParM";
  493.             NewWindow.Width = 10;
  494.             if ( NewWindow.Flags & WFLG_DEPTHGADGET)
  495.                 NewWindow.Width += 51;    /* Depth gadget stay hidden when show Mem/Time */
  496.         }
  497.         NewWindow.Width += IntuiTextLength(  &IT) + 6;
  498.         if ( NewWindow.LeftEdge + NewWindow.Width > HostPubScreenScreen ->Width - 23)
  499.             NewWindow.LeftEdge = HostPubScreenScreen ->Width - NewWindow.Width - 23;
  500.  
  501.         if (  ParMWindow = OpenWindowTags(  &NewWindow, WA_AutoAdjust, TRUE, TAG_DONE))
  502.         {
  503.             UnlockPubScreen( NULL, HostPubScreenScreen);
  504.             SetWindowTitles(  ParMWindow, "ParM", CopyRight);
  505.             SetFont(  ParMWindow->RPort, Font);
  506.             return ParMWindow;
  507.         }
  508.         UnlockPubScreen( NULL, HostPubScreenScreen);
  509.     }
  510.  
  511.     return NULL;
  512. }
  513.  
  514. static struct Menu *StartParMMenu( APTR *VisualInfo, struct Window *ParMWindow)
  515. {
  516.     struct Menu *ParMMenu = NULL;
  517.  
  518.     ParMConfig->Win = ParMWindow;
  519.     *VisualInfo = GetVisualInfo( ParMWindow->WScreen, TAG_DONE);
  520.     if ( ParMOpt.ParMenuColor < 0)
  521.         ParMOpt.ParMenuColor = ParMWindow->DetailPen;
  522.     if ( ParMMenu = CreateParMMenus( *VisualInfo, ParMOpt.ParMenuColor))
  523.     {
  524.         ParMConfig->LinkMenu = ParMMenu;
  525.  
  526.         UpDateMenus( *VisualInfo, ParMWindow, ParMMenu);
  527.         SetBPen( ParMWindow->RPort, ParMOpt.MTBlockPen);
  528.         SetDrMd( ParMWindow->RPort, JAM2);
  529.         RefreshInfoString( ParMWindow);
  530.  
  531.         return ParMMenu;
  532.     }
  533.  
  534.     return NULL;
  535. }
  536.  
  537. static void EndParMMenu( APTR *VisualInfo, struct Window *ParMWindow, struct Menu *ParMMenu)
  538. {
  539.     ClearMenus( ParMWindow);
  540.     FreeParMMenus( ParMConfig);
  541.     FreeMenus( ParMMenu);
  542.     ParMMenu = NULL;
  543.     FreeVisualInfo( *VisualInfo);
  544.     *VisualInfo = NULL;
  545. }
  546.  
  547. static void CloseParMWindow( struct Window *ParMWindow)
  548. {
  549.     CloseWindow( ParMWindow);
  550.     if ( Font)
  551.     {
  552.         CloseFont( Font);
  553.         Font = NULL;
  554.     }
  555. }
  556.  
  557. long ParMMain( void)
  558. {
  559.     BOOL Quit = FALSE;
  560.  
  561.     ULONG MsgClass;
  562.     UWORD Code, Qual;
  563.     BPTR OldCIS, OldCOS, fh = 0;
  564.     APTR OldConsoleTask;
  565.  
  566.     /* for TopInfo String */
  567.     struct MsgPort *Timer_Port = NULL;
  568.     struct timerequest Time_Req;
  569.     long TimerOpen = 1;
  570.     struct MsgPort *IntuiPort = NULL;
  571.     ULONG IntuiSignal = 0;
  572.     ULONG TimerSignal = 0;
  573.     ULONG Signal = 0;
  574.     ULONG WaitMask = 0;
  575.  
  576.     struct Window *ParMWindow = NULL;
  577.     struct Menu *ParMMenu = NULL;
  578.     APTR VisualInfo = NULL;
  579.  
  580.     struct Library *ScreenNotifyBase = NULL;
  581.     struct MsgPort *ScreenNotifyPort = NULL;
  582.     ULONG ScreenNotifySignal = 0;
  583.     APTR ScreenNotifyHandle = NULL;
  584.  
  585.     RetinaBase = ( struct RetinaBase *)OpenLibrary( "retina.library", 2L);
  586.     VMemBase = OpenLibrary( "vmem.library", 1L);
  587.  
  588.     InitDateFmt( );
  589.     ParMProcess = ( struct Process *)SysBase->ThisTask;
  590.     switch ( ParMOpt.ParMMode)
  591.     {
  592.         case MODE_WINDOW:
  593.             if (  !(  ParMWindow = OpenParMWindow(  NULL)))
  594.                 goto Bye;
  595.             if ( !( ParMMenu = StartParMMenu( &VisualInfo, ParMWindow)))
  596.                 goto Bye;
  597.             IntuiPort = ParMWindow->UserPort;
  598.             IntuiSignal = 1L << IntuiPort->mp_SigBit;
  599.             WaitMask |= IntuiSignal;
  600.             if ( ( ParMOpt.Flags & ( PF_SHOWMEM | PF_SHOWTIME)) && ( Timer_Port = CreateMsgPort( )))
  601.             {
  602.                 if ( !( TimerOpen = OpenDevice( TIMERNAME, UNIT_VBLANK, ( struct IORequest *)&Time_Req, 0L)))
  603.                 {
  604.                     Time_Req.tr_node.io_Message.mn_ReplyPort = Timer_Port;
  605.                     Time_Req.tr_node.io_Command = TR_ADDREQUEST;
  606.                     Time_Req.tr_node.io_Flags = 0;
  607.                     Time_Req.tr_node.io_Error = 0;
  608.                     Time_Req.tr_time.tv_secs = 0;
  609.                     Time_Req.tr_time.tv_micro = BASE_TIME * ParMOpt.RefreshTime;
  610.                 }
  611.             }
  612.             if (  ScreenNotifyBase = OpenLibrary( SCREENNOTIFY_NAME, SCREENNOTIFY_VERSION))
  613.             {
  614.                 ScreenNotifyPort = CreateMsgPort( );
  615.                 ScreenNotifySignal = 1L << ScreenNotifyPort->mp_SigBit;
  616.                 WaitMask |= ScreenNotifySignal;
  617.                 ScreenNotifyHandle = AddWorkbenchClient(  ScreenNotifyPort , 0);
  618.             }
  619.  
  620.             break;
  621.         case MODE_CLI:
  622.             goto Bye;
  623.             break;
  624.         case MODE_MYMENU:
  625.             goto Bye;
  626.             break;
  627.     }
  628.  
  629.     OldCIS = ParMProcess->pr_CIS;
  630.     OldCOS = ParMProcess->pr_COS;
  631.     OldConsoleTask = ParMProcess->pr_ConsoleTask;
  632.     if ( ParMOpt.Flags & PF_USENULL && ( fh = Open( "NIL:", MODE_NEWFILE)))
  633.     {
  634.         ParMProcess->pr_CIS = fh;
  635.         ParMProcess->pr_COS = fh;
  636.         ParMProcess->pr_ConsoleTask = 0;
  637.     }
  638.     /* Monitor Menu Events */
  639.  
  640.     WaitMask |= IntuiSignal;
  641.     if ( Timer_Port)
  642.     {
  643.         TimerSignal = 1L << Timer_Port->mp_SigBit;
  644.         WaitMask |= TimerSignal;
  645.         SendIO( ( struct IORequest *)&Time_Req.tr_node);
  646.     }
  647.  
  648.     WaitMask |= SIGBREAKF_CTRL_C;
  649.     while ( !Quit)
  650.     {
  651.         struct IntuiMessage *IMsg;
  652.  
  653.         Signal = Wait( WaitMask);
  654.         if ( Signal & IntuiSignal)
  655.         {
  656.             while ( IMsg = ( struct IntuiMessage *)GetMsg( IntuiPort))
  657.             {
  658.                 MsgClass = IMsg->Class;
  659.                 Code = IMsg->Code;
  660.                 Qual = IMsg->Qualifier;
  661.                 if ( MsgClass == IDCMP_RAWKEY && !( Qual & IEQUALIFIER_REPEAT) && !( Code & IECODE_UP_PREFIX))
  662.                 {
  663.                     UWORD TmpCode;
  664.  
  665.                     if ( ( TmpCode = FindMenuHotKey( ParMConfig, ParMMenu, Code, Qual, IMsg->IAddress)) != MENUNULL)
  666.                     {
  667.                         Code = TmpCode;
  668.                         MsgClass = IDCMP_MENUPICK;
  669.                     }
  670.                     // Give nul Qualifier for RawKeyToAscii( ) to allow 'alt' keys as menu qualifier keys
  671.                     else if ( Code = RawKeyToAscii( Code, Qual & ~( IEQUALIFIER_CONTROL | IEQUALIFIER_LALT | IEQUALIFIER_RALT), IMsg->IAddress))
  672.                     {
  673.                         if ( Code >= 32 && Code <= 255)
  674.                         {
  675.                             ULONG const EXCLUDE_QUAL_MASK    = ( IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_CAPSLOCK);
  676.  
  677.                             if ( ( Qual & ~EXCLUDE_QUAL_MASK) == ( ParMConfig->ShortCutQual & ~EXCLUDE_QUAL_MASK))
  678.                             {
  679.                                 Code = MakeMenuShortCut( ParMMenu, Code);
  680.                                 MsgClass = IDCMP_MENUPICK;
  681.                             }
  682.                         }
  683.                     }
  684.                 }
  685.                 switch ( MsgClass)
  686.                 {
  687.                     case IDCMP_ACTIVEWINDOW:
  688.                     case IDCMP_INACTIVEWINDOW:
  689.                         if ( ParMOpt.ParMMode == MODE_WINDOW && ( ParMOpt.Flags & ( PF_SHOWMEM | PF_SHOWTIME)))
  690.                         {
  691.                             Delay( 6);    /* let intuition do its defered drawing */
  692.                             DoSetRast = TRUE;
  693.                         }
  694.                         break;
  695.                     case IDCMP_CLOSEWINDOW:
  696.                         Quit = TRUE;
  697.                         break;
  698.                     case IDCMP_MENUPICK:
  699.                         DoNextSelect = TRUE;
  700.                         if ( Code != MENUNULL)
  701.                         {
  702.                             ULONG const FirstParMMenuNum = 0;    // from removed MyMeny support
  703.  
  704.                             Busy( ParMWindow, TRUE);
  705.                             do
  706.                             {
  707.                                 /* Prevent extended selection of workbench menus */
  708.                                 if ( MENUNUM( Code) == FirstParMMenuNum)
  709.                                 {
  710.                                     switch ( ITEMNUM( Code))
  711.                                     {
  712.                                     case OPEN_ITEM:
  713.                                         OpenMenus( ParMWindow);
  714.                                         UpDateMenus( VisualInfo, ParMWindow, ParMMenu);
  715.                                         DoNextSelect = FALSE;
  716.                                         break;
  717.                                     case UPDATE_ITEM:
  718.                                         UpDateMenus( VisualInfo, ParMWindow, ParMMenu);
  719.                                         DoNextSelect = FALSE;
  720.                                         break;
  721.                                     case STD_CFG_ITEM:
  722.                                         strcpy( ParMConfig->CurCfg, DefaultConfigFile);
  723.                                         UpDateMenus( VisualInfo, ParMWindow, ParMMenu);
  724.                                         DoNextSelect = FALSE;
  725.                                         break;
  726.                                     case COMMAND_ITEM:
  727.                                         Command( ParMConfig);
  728.                                         break;
  729.                                     case CHDIR_ITEM:
  730.                                         GetNewDir( );
  731.                                         break;
  732.                                     case QUIT_ITEM:
  733.                                         Quit = TRUE;
  734.                                         DoNextSelect = FALSE;
  735.                                     }
  736.                                 }
  737.                                 else if ( MENUNUM( Code) > FirstParMMenuNum)    /* custom menus */
  738.                                     DoExtMenu( VisualInfo, ParMWindow, ParMMenu, Code);
  739.                             } while ( DoNextSelect && ( Code = ItemAddress( ParMMenu, Code)->NextSelect) != MENUNULL);
  740.                             Busy( ParMWindow, FALSE);
  741.                         }
  742.                         break;
  743.                 }
  744.                 ReplyMsg( IMsg);
  745.             }
  746.         }
  747.         if ( Signal & TimerSignal)
  748.         {
  749.             if ( ParMWindow && ( ParMOpt.Flags & PF_AUTOFRONT) && IsHidden( ParMWindow))
  750.                 WindowToFront( ParMWindow);
  751.             Time_Req.tr_time.tv_secs = 0;
  752.             Time_Req.tr_time.tv_micro = BASE_TIME * ParMOpt.RefreshTime;
  753.             SendIO( ( struct IORequest *)&Time_Req.tr_node);
  754.         }
  755.         if (  Signal & ScreenNotifySignal)
  756.         {
  757.             struct ScreenNotifyMessage *snm;
  758.  
  759.             /* Get message */
  760.             while (  snm = ( struct ScreenNotifyMessage *)GetMsg( ScreenNotifyPort))
  761.             {
  762.                 /* Workbench notification? */
  763.                 if (  snm->snm_Type == SCREENNOTIFY_TYPE_WORKBENCH)
  764.                 {
  765.                     /* Yes, Open/Close event? */
  766.                     switch (  ( ULONG)snm->snm_Value)
  767.                     {
  768.                         case FALSE:    /* WB Close -> close our window */
  769.                             if ( ParMWindow)
  770.                             {
  771.                                 if ( ParMMenu)
  772.                                 {
  773.                                     EndParMMenu( &VisualInfo, ParMWindow, ParMMenu);
  774.                                     ParMMenu = NULL;
  775.                                 }
  776.                                 if ( ParMWindow)
  777.                                 {
  778.                                     CloseParMWindow( ParMWindow);
  779.                                     ParMWindow = NULL;
  780.                                 }
  781.                                 WaitMask &= ~IntuiSignal;
  782.                                 IntuiSignal = 0;
  783.                                 IntuiPort = NULL;
  784.  
  785.                                 // workaround a bug of ScreenNotify library wich send WBOpen immediatly after WBClose even if 
  786.                                 // the WB haven't yet be closed and reopened
  787.                                 //Delay(  20);    // workaround a bug of
  788.                             }
  789.                         break;
  790.                         case TRUE:    /* WB Open -> reopen our window */
  791.                         {
  792.                             UBYTE i = 0;
  793.                             while ( (i < 10) && !( ParMWindow = OpenParMWindow( NULL)))
  794.                             {
  795.                                 i++;
  796.                                 Delay( 50L);
  797.                             }
  798.                             if ( ParMWindow)
  799.                             {
  800.                                 if ( ParMMenu = StartParMMenu( &VisualInfo, ParMWindow))
  801.                                 {
  802.                                     IntuiPort = ParMWindow->UserPort;
  803.                                     IntuiSignal = 1L << IntuiPort->mp_SigBit;
  804.                                     WaitMask |= IntuiSignal;
  805.                                 }
  806.                                 else
  807.                                     Quit = TRUE;
  808.                             }
  809.                             else
  810.                                 Quit = TRUE;
  811.                         }
  812.                         break;
  813.                     }
  814.                 }
  815.                 ReplyMsg( ( struct Message *)snm);
  816.             }
  817.         }
  818.         if ( Signal & SIGBREAKF_CTRL_C)
  819.             Quit = TRUE;
  820.         //if ( ParMWindow && ( ParMOpt.ParMMode == MODE_WINDOW) && IsVisible( ParMWindow))
  821.         if ( ParMWindow && ( ParMOpt.ParMMode == MODE_WINDOW))
  822.             RefreshInfoString( ParMWindow);
  823.         /* See if some wb messages have been replied, and free them */
  824.         FreeRepliedWBMessages( );
  825.     }
  826.  
  827.     if ( !TimerOpen)
  828.     {
  829.         AbortIO( ( struct IORequest *)&Time_Req);        /* wait for the last timer msg before exit */
  830.         CloseDevice( ( struct IORequest *)&Time_Req);
  831.     }
  832.     if ( Timer_Port)
  833.         DeleteMsgPort( Timer_Port);
  834.  
  835.     ParMProcess->pr_CIS = OldCIS;
  836.     ParMProcess->pr_COS = OldCOS;
  837.     ParMProcess->pr_ConsoleTask = OldConsoleTask;
  838.     if ( fh)
  839.         Close( fh);
  840.  
  841.     Bye:
  842.     if ( ParMMenu)
  843.     {
  844.         EndParMMenu( &VisualInfo, ParMWindow, ParMMenu);
  845.         ParMMenu = NULL;
  846.     }
  847.     if ( ParMWindow)
  848.     {
  849.         CloseParMWindow( ParMWindow);
  850.         ParMWindow = NULL;
  851.     }
  852.  
  853.     if (  ScreenNotifyHandle )
  854.     {
  855.         /* Unregister WB client */
  856.         while ( !RemWorkbenchClient( ScreenNotifyHandle))
  857.             Delay(  10);
  858.     }
  859.     if (  ScreenNotifyPort)
  860.         DeleteMsgPort(  ScreenNotifyPort);
  861.     if (  ScreenNotifyBase)
  862.         CloseLibrary(  ScreenNotifyBase);
  863.  
  864.     if ( RetinaBase)
  865.         CloseLibrary( RetinaBase);
  866.     if ( VMemBase)
  867.         CloseLibrary( VMemBase);
  868.  
  869.     return 0;
  870. }
  871.