home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / MUI / aloha.c next >
Encoding:
C/C++ Source or Header  |  1995-12-16  |  9.5 KB  |  386 lines

  1. #include "defs.h"
  2.  
  3. static char rcsid [] = "$Id: aloha.c,v 3.6 95/12/16 19:02:09 Martin_Apel Rel $";
  4.  
  5. /**********************************************************************/
  6.  
  7. struct NewBroker nb = 
  8.   {
  9.   NB_VERSION,
  10.   PROGNAME,
  11.   PROGNAME,
  12.   NULL,             /* stuffed in later */  
  13.   NBU_UNIQUE,
  14.   COF_SHOW_HIDE,
  15.   0,                /* stuffed in later */  
  16.   NULL,             /* stuffed in later */
  17.   0
  18.   };
  19.  
  20. /**********************************************************************/
  21.  
  22. static BOOL VMEnabled = TRUE;
  23.  
  24. BOOL InstallAsCommodity (void)
  25.  
  26. {
  27. char *portname;
  28. char PrefsDir [200];
  29. char PrefsName [80];
  30. BOOL success;
  31. struct Process *myself;
  32. LONG CxSignal;
  33.  
  34. /* Get the full path for loading this program */
  35. myself = (struct Process*) FindTask (NULL);
  36. if (!NameFromLock (myself->pr_HomeDir, PrefsDir, 200L))
  37.   return (FALSE);
  38.  
  39. if (GetProgramName (PrefsName, 80L))         /* only for CLI */
  40.   success = AddPart (PrefsDir, FilePart (PrefsName), 200L);
  41. else
  42.   success = AddPart (PrefsDir, myself->pr_Task.tc_Node.ln_Name, 200L);
  43.  
  44. if (!success)
  45.   return (FALSE);
  46.  
  47. /* PrefsDir now contains the complete path for loading the Prefs program */
  48.  
  49. if ((CxParams = AllocVec (sizeof (struct CxParams) + (ULONG)strlen (PrefsDir) + 3,
  50.                           MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
  51.   return (FALSE);
  52.  
  53. strcpy ((char*)(CxParams + 1), PrefsDir);
  54. CxParams->PrefsPath = (char*)(CxParams + 1);
  55. CxParams->ForceOverwrite = ForceOverwrite;
  56.  
  57. /* Don't use CreateMsgPort for this, because the SigTask and SigBit fields
  58.  * will be changed later.
  59.  * Directly behind the MsgPort there's a pointer to the preferences 
  60.  * process whenever the it is running.
  61.  */
  62.  
  63. if ((CxParams->ExtCxPort = AllocMem (sizeof (struct ExtPort), MEMF_PUBLIC)) == NULL)
  64.   {
  65.   printf ("Couldn't create commodities port\n");
  66.   return (FALSE);
  67.   }
  68.  
  69. ExtCxPort = CxParams->ExtCxPort;
  70. ExtCxPort->PrefsTask = NULL;
  71. ExtCxPort->ShowSignal = -1L;
  72.  
  73. if ((CxSignal = AllocSignal (-1L)) == -1L)
  74.   {
  75.   printf ("Ran out of signals\n");
  76.   return (FALSE);
  77.   }
  78.  
  79. ExtCxPort->CxPort.mp_SigBit = (UBYTE)CxSignal;
  80. ExtCxPort->CxPort.mp_SigTask = FindTask (NULL);
  81. ExtCxPort->CxPort.mp_Flags = PA_SIGNAL;
  82. ExtCxPort->CxPort.mp_Node.ln_Type = NT_MSGPORT;
  83. ExtCxPort->CxPort.mp_Node.ln_Pri = 0;
  84. NewList (&(ExtCxPort->CxPort.mp_MsgList));
  85. if ((portname = AllocVec ((ULONG)strlen (CXPORTNAME) + 1, MEMF_PUBLIC)) == NULL)
  86.   {
  87.   printf ("Not enough memory for portname\n");
  88.   return (FALSE);
  89.   }
  90.  
  91. strcpy (portname, CXPORTNAME);
  92.  
  93. ExtCxPort->CxPort.mp_Node.ln_Name = portname;
  94. AddPort (&(ExtCxPort->CxPort));
  95.  
  96. nb.nb_Pri = CXPri;
  97. nb.nb_Port = &(ExtCxPort->CxPort);
  98. nb.nb_Descr = GetVMMString (msgVMMDescr);
  99. if ((CxParams->Broker = CxBroker (&nb, NULL)) == NULL)
  100.   {
  101.   printf ("Couldn't create broker\n");
  102.   return (FALSE);
  103.   }
  104.  
  105.  
  106. CxParams->GUIFilter = HotKey (CXPopKey, &(ExtCxPort->CxPort), APPEAR_ID);
  107. if (CxParams->GUIFilter == NULL)
  108.   {
  109.   printf ("Couldn't create commodity hotkey\n");
  110.   return (FALSE);
  111.   }
  112.  
  113. AttachCxObj (CxParams->Broker, CxParams->GUIFilter);
  114.  
  115. CxParams->EnableFilter = HotKey (EnableVMHotkey, &(ExtCxPort->CxPort), ENABLE_ID);
  116. if (CxParams->EnableFilter != NULL)
  117.   AttachCxObj (CxParams->Broker, CxParams->EnableFilter);
  118. #ifdef DEBUG
  119. else if (EnableVMHotkey [0] != 0)
  120.   printf ("Couldn't install Enable hotkey\n");
  121. #endif
  122.  
  123. CxParams->DisableFilter = HotKey (DisableVMHotkey, &(ExtCxPort->CxPort), DISABLE_ID);
  124. if (CxParams->DisableFilter != NULL)
  125.   AttachCxObj (CxParams->Broker, CxParams->DisableFilter);
  126. #ifdef DEBUG
  127. else if (DisableVMHotkey [0] != 0)
  128.   printf ("Couldn't install Disable hotkey\n");
  129. #endif
  130.  
  131. ActivateCxObj (CxParams->Broker, TRUE);
  132.  
  133. return (TRUE);
  134. }
  135.  
  136. /*********************************************************************/
  137.  
  138. void UninstallAsCommodity (void)
  139.  
  140. {
  141. struct Message *Msg;
  142.  
  143. if (CxParams == NULL)
  144.   return;
  145.  
  146. if (CxParams->Broker != NULL)
  147.   DeleteCxObjAll (CxParams->Broker);
  148.  
  149. if (ExtCxPort != NULL)
  150.   {
  151.   RemPort (&(ExtCxPort->CxPort));
  152.   while ((Msg = GetMsg (&(ExtCxPort->CxPort))) != NULL)
  153.     ReplyMsg (Msg);
  154.  
  155.   FreeSignal ((LONG)ExtCxPort->CxPort.mp_SigBit);
  156.   FreeSignal (ExtCxPort->ShowSignal);
  157.  
  158.   FreeVec (ExtCxPort->CxPort.mp_Node.ln_Name);
  159.   FreeMem (ExtCxPort, sizeof (struct ExtPort));
  160.   }
  161.  
  162. FreeVec (CxParams);
  163. }
  164.  
  165. /**********************************************************************/
  166.  
  167. int HandleCxMsg (void)
  168.  
  169. /* VMM is not running when this function is called. Otherwise VMM itself
  170.  * would handle these messages.
  171.  */
  172. {
  173. CxMsg *MyMsg;
  174. int status = Q_DONTQUIT;
  175.  
  176. while ((CxParams != NULL) && 
  177.        (MyMsg = (CxMsg*)GetMsg (&(ExtCxPort->CxPort))) != NULL)
  178.   {
  179.   switch (CxMsgType (MyMsg))
  180.     {
  181.     case CXM_COMMAND:
  182.       switch (CxMsgID (MyMsg))
  183.         {
  184.         case CXCMD_DISABLE: 
  185.                ActivateCxObj (CxParams->Broker, FALSE);
  186.                break;
  187.  
  188.         case CXCMD_ENABLE: 
  189.                ActivateCxObj (CxParams->Broker, TRUE);
  190.                break;
  191.  
  192.         case CXCMD_APPEAR: 
  193.                set (Application, MUIA_Application_Iconified, FALSE);
  194.                break;
  195.  
  196.         case CXCMD_DISAPPEAR: 
  197.                StartVMM ();
  198.                status = Q_QUITGUI;
  199.                break;
  200.  
  201.         case CXCMD_KILL: 
  202.                status = Q_QUITBOTH;
  203.                break;
  204.         }
  205.       break;
  206.  
  207.     case CXM_IEVENT:
  208.       switch (CxMsgID (MyMsg))
  209.         {
  210.         case APPEAR_ID: set (Application, MUIA_Application_Iconified, FALSE);
  211.                         break;
  212.  
  213.         case ENABLE_ID: if (!VMEnabled)
  214.                           {
  215.                           VMEnabled = TRUE;
  216.                           DisplayBeep (NULL);
  217.                           }
  218.                         break;
  219.  
  220.         case DISABLE_ID:if (VMEnabled)
  221.                           {
  222.                           VMEnabled = FALSE;
  223.                           DisplayBeep (NULL);
  224.                           }
  225.                         break;
  226.         }
  227.     break;
  228.     }
  229.  
  230.   ReplyMsg ((struct Message*) MyMsg);
  231.   }
  232.  
  233. return (status);
  234. }
  235.  
  236. /**********************************************************************/
  237.  
  238. BOOL StartVMM (void)
  239.  
  240. {
  241. struct MsgPort *StarterPort;
  242. BPTR SegList;
  243. struct Process *VM_Manager;
  244. struct VMMsg *StartMsg;
  245. BOOL success;
  246. LONG CxSignal;
  247.  
  248. if (CxParams->EnableFilter == NULL)
  249.   {
  250.   CxParams->EnableFilter = HotKey (EnableVMHotkey, 
  251.                                    &(ExtCxPort->CxPort), ENABLE_ID);
  252.   if (CxParams->EnableFilter == NULL && EnableVMHotkey [0] != 0)
  253.     {
  254.     printf ("Invalid hotkey for VM enable\n");
  255.     return (FALSE);
  256.     }
  257.   AttachCxObj (CxParams->Broker, CxParams->EnableFilter);
  258.   }
  259. else
  260.   {
  261.   SetFilter (CxParams->EnableFilter, EnableVMHotkey);
  262.   if ((CxObjError (CxParams->EnableFilter) != NULL) && 
  263.       EnableVMHotkey [0] != 0)
  264.     {
  265.     printf ("Invalid hotkey for VM enable\n");
  266.     return (FALSE);
  267.     }
  268.   }
  269.  
  270. if (CxParams->DisableFilter == NULL)
  271.   {
  272.   CxParams->DisableFilter = HotKey (DisableVMHotkey, 
  273.                                    &(ExtCxPort->CxPort), DISABLE_ID);
  274.   if (CxParams->DisableFilter == NULL && DisableVMHotkey [0] != 0)
  275.     {
  276.     printf ("Invalid hotkey for VM disable\n");
  277.     return (FALSE);
  278.     }
  279.   AttachCxObj (CxParams->Broker, CxParams->DisableFilter);
  280.   }
  281. else
  282.   {
  283.   SetFilter (CxParams->DisableFilter, DisableVMHotkey);
  284.   if ((CxObjError (CxParams->DisableFilter) != NULL) &&
  285.       DisableVMHotkey [0] != 0)
  286.     {
  287.     printf ("Invalid hotkey for VM disable\n");
  288.     return (FALSE);
  289.     }
  290.   }
  291.  
  292. if ((StartMsg = AllocMem (sizeof (struct VMMsg), MEMF_PUBLIC)) == NULL)
  293.   {
  294.   printf (GetVMMString (msgOutOfMem));
  295.   return (FALSE);
  296.   }
  297.  
  298. if ((StarterPort = CreateMsgPort ()) == NULL)
  299.   {
  300.   printf ("Couldn't create temporary port\n");
  301.   FreeMem (StartMsg, sizeof (struct VMMsg));
  302.   return (FALSE);
  303.   }
  304.  
  305. StarterPort->mp_Node.ln_Name = STARTER_PORT_STD;
  306. StarterPort->mp_Node.ln_Pri  = 0;
  307. AddPort (StarterPort);
  308.  
  309. SegList = NewLoadSeg (PROGPATH, NULL);
  310. if (SegList == NULL)
  311.   {
  312.   printf ("Couldn't load VMM executable\n");
  313.   RemPort (StarterPort);
  314.   DeleteMsgPort (StarterPort);
  315.   FreeMem (StartMsg, sizeof (struct VMMsg));
  316.   return (FALSE);
  317.   }
  318.  
  319. if ((VM_Manager = CreateNewProcTags (NP_Seglist, (ULONG)SegList,
  320.                                      NP_FreeSeglist, TRUE,
  321.                                      NP_Name, (ULONG)VM_MANAGER_NAME,
  322.                                      NP_StackSize, 4000L,
  323.                                      TAG_DONE, 0L)) == NULL)
  324.   {       
  325.   printf ("Couldn't create VMM_Manager process\n");
  326.   UnLoadSeg (SegList);
  327.   RemPort (StarterPort);
  328.   DeleteMsgPort (StarterPort);
  329.   FreeMem (StartMsg, sizeof (struct VMMsg));
  330.   return (FALSE);
  331.   }
  332.  
  333. /* Send startup message */
  334. CxParams->VMEnable = VMEnabled;
  335. CxParams->ConfigPath = CfgName;
  336. StartMsg->StartupParams = CxParams;
  337. StartMsg->VMCommand = VMCMD_Startup;
  338. StartMsg->ReplySignal = 0;
  339.  
  340. CxSignal = ExtCxPort->CxPort.mp_SigBit;
  341. CxParams = NULL;
  342. FreeSignal (CxSignal);
  343. PutMsg (&(VM_Manager->pr_MsgPort), (struct Message*) StartMsg);
  344.  
  345. /* Wait for initialization message */
  346. WaitPort (StarterPort);
  347. StartMsg = (struct VMMsg*)GetMsg (StarterPort);
  348.  
  349. success = (StartMsg->VMCommand == VMCMD_InitReady);
  350.  
  351. RemPort (StarterPort);
  352. DeleteMsgPort (StarterPort);
  353. FreeMem (StartMsg, sizeof (struct VMMsg));
  354.  
  355. return (success);
  356. }
  357.  
  358. /**********************************************************************/
  359.  
  360. BOOL StopVMM (void)
  361.  
  362. /* success only means the quit message could be posted. */
  363. {
  364. struct VMMsg *StopMsg;
  365. struct MsgPort *VMPort;
  366. BOOL success = FALSE;
  367.  
  368. if ((StopMsg = AllocMem (sizeof (struct VMMsg), MEMF_PUBLIC)) != NULL)
  369.   {
  370.   StopMsg->VMCommand = VMCMD_QuitAll;
  371.   StopMsg->ReplySignal = NULL;
  372.  
  373.   Forbid ();
  374.   if ((VMPort = FindPort (VMPORTNAME)) != NULL)
  375.     {
  376.     success = TRUE;
  377.     PutMsg (VMPort, (struct Message*)StopMsg);
  378.     }
  379.   else
  380.     FreeMem (StopMsg, sizeof (struct VMMsg));
  381.   Permit ();
  382.   }
  383.  
  384. return (success);
  385. }
  386.