home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / ProcessTools.cp < prev    next >
Encoding:
Text File  |  1994-02-20  |  4.9 KB  |  176 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // ProcessTools.cp
  3.  
  4. #include "ProcessTools.h"
  5.  
  6. #include <Errors.h>
  7.  
  8. #pragma segment MyTools
  9.  
  10. Boolean FindApplFromSignature(OSType sig, FSSpec &spec)
  11. {
  12.     // lidt modificeret udgave af LaunchBySignature i MacAppGlobals
  13.     ProcessSerialNumber psn;
  14.     OSErr err;
  15.     short sysVRefNum;
  16.     short vRefNum;
  17.     short index;
  18.     Boolean hasDesktopDB;
  19.  
  20.     // First see if it's already running:
  21.     err = FindProcessBySignature(sig, psn, &spec);
  22.     if (err == noErr)
  23.         return true;
  24.  
  25.     // Well, it's not running. Let's have a look around:
  26.  
  27.     FailOSErr(GetSysVolume(sysVRefNum));
  28.     vRefNum = sysVRefNum;                        // Start search with boot volume
  29.     index = 0;
  30.     do
  31.     {
  32.         if (index == 0 || vRefNum != sysVRefNum)
  33.         {
  34.             err = VolHasDesktopDB(vRefNum, hasDesktopDB);
  35.             if (err != noErr)
  36.                 continue;
  37.             if (hasDesktopDB)
  38.             {
  39.                 // If volume has a desktop DB,
  40.                 err = FindAppOnVolume(sig, vRefNum, spec);// ask it to find app
  41.                 if (err == noErr)
  42.                 {
  43.                     return true;
  44.                 }
  45.             }
  46.         }
  47.         err = GetIndVolume(++index, vRefNum);    // Else go to next volume
  48.     } while (err == noErr);                        // Keep going until we run out of vols
  49.     return false;
  50. }
  51.  
  52. void LaunchApplication(FSSpec &applSpec, Boolean toFront)
  53. {
  54.     LaunchFlags launchControlFlags = launchContinue + launchNoFileFlags + launchAllow24Bit + launchUseMinimum;
  55.     if (!toFront)
  56.         launchControlFlags |= launchDontSwitch;
  57.  
  58.     static LaunchParamBlockRec glaunchPB;
  59.     static FSSpec gLauchSpec = applSpec;
  60.  
  61.     glaunchPB.launchBlockID = extendedBlock;
  62.     glaunchPB.launchEPBLength = extendedBlockLen;
  63.     glaunchPB.launchFileFlags = launchNoFileFlags;
  64.     glaunchPB.launchControlFlags = launchControlFlags;
  65.     glaunchPB.launchAppSpec = &gLauchSpec;
  66.     glaunchPB.launchAppParameters = nil;
  67.  
  68.     FailOSErr(LaunchApplication(glaunchPB));
  69.  
  70.     gApplication->PollToolboxEvent(false);
  71.     gApplication->PollToolboxEvent(false);
  72.     gApplication->PollToolboxEvent(false);
  73.     gApplication->PollToolboxEvent(false);
  74. }
  75.  
  76.  
  77. void OpenApplicationDocument(FSSpec &applSpec, AliasHandle aliasH, Boolean toFront)
  78. {
  79.     static FSSpec gLauchSpec;
  80.     static LaunchParamBlockRec glaunchPB;
  81.     FailInfo fi;
  82.     if (fi.Try())
  83.     {
  84.         AppleEvent event;
  85.         AEDesc addr;
  86.         ProcessSerialNumber psn;
  87.  
  88. //@@
  89. #if 0
  90.         FailOSErr(LaunchBySignature(applSignature, psn, &gLauchSpec, nil, true,
  91.                 launchContinue + launchNoFileFlags + launchDontSwitch + launchAllow24Bit));
  92.         EventRecord toolEvent;
  93.         for (short i = 0; i <= 25; i++)
  94. //            EventAvail(everyEvent, toolEvent);
  95.             EventAvail(0, toolEvent);
  96.         if (FindProcessBySignature(applSignature, psn, &gLauchSpec) != noErr)
  97.         {
  98. #if qDebug
  99.             DebugStr("Editor is not running as expected (I just launched it!)");
  100. #endif
  101.             fi.Success();
  102.             return;
  103.         }
  104. #endif
  105. //@@
  106.  
  107.         FInfo info;
  108.         FailOSErr(FSpGetFInfo(applSpec, info));
  109.         Boolean applIsRunning = FindProcessBySignature(info.fdCreator, psn, &gLauchSpec) == noErr;
  110.  
  111. #if qDebugLauchEditor
  112.             fprintf(stderr, "ODOC: Appl is%s running\n", applIsRunning ? "" : " not");
  113. #endif
  114.         if (applIsRunning)
  115.             FailOSErr(AECreateDesc(typeProcessSerialNumber, Ptr(&psn), sizeof(ProcessSerialNumber), addr));
  116.         else
  117.         {
  118.                 addr.descriptorType = typeNull; // in LaunchWithDoc, addr is undefined
  119.                 addr.dataHandle = nil;
  120.         }
  121.         
  122.         FailOSErr(AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, addr, kAutoGenerateReturnID, kAnyTransactionID, event));
  123.     
  124.         AEDescList theList;
  125.         AEDesc docDesc;
  126.         FailOSErr(AECreateList(nil, 0, false, theList));
  127.         HLock(Handle(aliasH));
  128.         FailOSErr(AECreateDesc(typeAlias, Ptr(*aliasH), GetHandleSize(Handle(aliasH)), docDesc));
  129.         HUnlock(Handle(aliasH));
  130.  
  131.         FailOSErr(AEPutDesc(theList, 0, docDesc));
  132.         FailOSErr(AEPutParamDesc(event, keyDirectObject, theList));
  133.  
  134.         if (applIsRunning)
  135.         {
  136.             if (toFront)
  137.             {
  138.                 FailOSErr(SetFrontProcess(psn));
  139.                 gApplication->PollToolboxEvent(false);
  140.                 gApplication->PollToolboxEvent(false);
  141.                 gApplication->PollToolboxEvent(false);
  142.             }
  143.             // editor is running, throw the apple-event to it
  144.             AppleEvent dummyReply;
  145.             FailOSErr(AESend(event, dummyReply, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
  146.                 kAENormalPriority, 60, nil, nil));
  147.             fi.Success();
  148.             return;
  149.         }
  150.         // we must launch the appl with an apple-event
  151.         static LaunchParamBlockRec glaunchPB;
  152.         gLauchSpec = applSpec;
  153.         glaunchPB.launchAppSpec = &gLauchSpec;
  154.         AEDesc launchDesc;
  155.         AECoerceDesc(event, typeAppParameters, launchDesc);
  156.         HLock(Handle(event.dataHandle));
  157.         glaunchPB.launchAppParameters = (AppParametersPtr)*(launchDesc.dataHandle);
  158.         glaunchPB.launchBlockID = extendedBlock;
  159.         glaunchPB.launchEPBLength = extendedBlockLen;
  160.         glaunchPB.launchFileFlags = 0;
  161.         glaunchPB.launchControlFlags = launchContinue + launchNoFileFlags + launchAllow24Bit + launchUseMinimum;
  162.         if (!toFront)
  163.             glaunchPB.launchControlFlags += launchDontSwitch;
  164.         FailOSErr(LaunchApplication(glaunchPB));
  165.         gApplication->PollToolboxEvent(false);
  166.         gApplication->PollToolboxEvent(false);
  167.         gApplication->PollToolboxEvent(false);
  168.  
  169.         fi.Success();
  170.     }
  171.     else // fail
  172.     {
  173.         fi.ReSignal();
  174.     }            
  175. }
  176.