LaunchAPPLServer: move AppLauncher to separate file

This commit is contained in:
Wolfgang Thaller 2018-04-27 02:06:54 +02:00
parent 797ab906f5
commit d7ac3a94a0
6 changed files with 164 additions and 137 deletions

View File

@ -0,0 +1,122 @@
#include "AppLauncher.h"
#include <Processes.h>
#include <TextUtils.h>
#include <string.h>
class AppLauncherSingle : public AppLauncher
{
public:
virtual bool Launch(ConstStr255Param name)
{
LaunchParamBlockRec lpb;
memset(&lpb, 0, sizeof(lpb));
lpb.reserved1 = (unsigned long) name;
lpb.reserved2 = 0;
lpb.launchBlockID = extendedBlock;
lpb.launchEPBLength = 6;
lpb.launchFileFlags = 0;
lpb.launchControlFlags = 0xC000;
LaunchApplication(&lpb);
return false;
}
virtual bool IsRunning(ConstStr255Param name)
{
return false;
}
};
class AppLauncher7 : public AppLauncher
{
ProcessSerialNumber psn;
public:
virtual bool Launch(ConstStr255Param name)
{
LaunchParamBlockRec lpb;
memset(&lpb, 0, sizeof(lpb));
FSSpec spec;
FSMakeFSSpec(0,0,name,&spec);
lpb.launchBlockID = extendedBlock;
lpb.launchEPBLength = extendedBlockLen;
lpb.launchFileFlags = 0;
lpb.launchControlFlags = launchContinue;
lpb.launchAppSpec = &spec;
OSErr err = LaunchApplication(&lpb);
psn = lpb.launchProcessSN;
return err >= 0;
}
virtual bool IsRunning(ConstStr255Param name)
{
ProcessInfoRec info;
memset(&info, 0, sizeof(info));
info.processInfoLength = sizeof(info);
OSErr err = GetProcessInformation(&psn,&info);
return err == noErr;
}
};
class AppLauncherMultiFinder : public AppLauncher
{
public:
virtual bool Launch(ConstStr255Param name)
{
LaunchParamBlockRec lpb;
memset(&lpb, 0, sizeof(lpb));
lpb.reserved1 = (unsigned long) name;
lpb.reserved2 = 0;
lpb.launchBlockID = extendedBlock;
lpb.launchEPBLength = 6;
lpb.launchFileFlags = 0;
lpb.launchControlFlags = 0xC000;
OSErr err = LaunchApplication(&lpb);
return err >= 0;
}
virtual bool IsRunning(ConstStr255Param name)
{
uint8_t *fcbs = *(uint8_t**)0x34E; // FCBSPtr;
uint16_t bufSize = * (uint16_t*) fcbs;
uint8_t *end = fcbs + bufSize;
for(uint8_t *fcb = fcbs + 2; fcb < end; fcb += 94)
{
if(*(uint32_t*) fcb == 0)
continue;
if(*(OSType*) (fcb + 0x32) != 'APPL')
continue;
if(EqualString(fcb + 0x3E, "\pRetro68App", true, true))
return true;
}
return false;
}
};
std::unique_ptr<AppLauncher> CreateAppLauncher()
{
#if TARGET_API_MAC_CARBON || TARGET_RT_MAC_CFM
return std::make_unique<AppLauncher7>();
#else
SysEnvRec environ;
SysEnvirons(curSysEnvVers, &environ);
if(environ.systemVersion >= 0x0700)
return std::make_unique<AppLauncher7>();
else
{
uint32_t& Twitcher2 = *(uint32_t*) 0xB7C;
if(Twitcher2 == 0 || Twitcher2 == 0xFFFFFFFF)
return std::make_unique<AppLauncherSingle>();
else
return std::make_unique<AppLauncherMultiFinder>();
}
#endif
}

View File

@ -0,0 +1,11 @@
#include <memory>
#include <MacTypes.h>
class AppLauncher
{
public:
virtual bool Launch(ConstStr255Param name) = 0;
virtual bool IsRunning(ConstStr255Param name) = 0;
};
std::unique_ptr<AppLauncher> CreateAppLauncher();

View File

@ -2,7 +2,9 @@ add_application(LaunchAPPLServer
LaunchAPPLServer.r
LaunchAPPLServer.cc
MacSerialStream.h
MacSerialStream.cc)
MacSerialStream.cc
AppLauncher.h
AppLauncher.cc)
target_link_libraries(LaunchAPPLServer LaunchAPPLCommon)
set_target_properties(LaunchAPPLServer PROPERTIES

View File

@ -29,6 +29,8 @@
#include "MacSerialStream.h"
#include "AppLauncher.h"
#include <ReliableStream.h>
#include <Processes.h>
#include <string.h>
@ -341,111 +343,6 @@ void SetBaud(long baud)
gSerialStream->setBaud(baud);
}
class AppLauncher
{
public:
virtual bool Launch(ConstStr255Param name) = 0;
virtual bool IsRunning(ConstStr255Param name) = 0;
};
class AppLauncherSingle : public AppLauncher
{
public:
virtual bool Launch(ConstStr255Param name)
{
gSerialStream->close();
LaunchParamBlockRec lpb;
memset(&lpb, 0, sizeof(lpb));
lpb.reserved1 = (unsigned long) name;
lpb.reserved2 = 0;
lpb.launchBlockID = extendedBlock;
lpb.launchEPBLength = 6;
lpb.launchFileFlags = 0;
lpb.launchControlFlags = 0xC000;
LaunchApplication(&lpb);
return false;
}
virtual bool IsRunning(ConstStr255Param name)
{
return false;
}
};
class AppLauncher7 : public AppLauncher
{
ProcessSerialNumber psn;
public:
virtual bool Launch(ConstStr255Param name)
{
LaunchParamBlockRec lpb;
memset(&lpb, 0, sizeof(lpb));
FSSpec spec;
FSMakeFSSpec(0,0,name,&spec);
lpb.launchBlockID = extendedBlock;
lpb.launchEPBLength = extendedBlockLen;
lpb.launchFileFlags = 0;
lpb.launchControlFlags = launchContinue;
lpb.launchAppSpec = &spec;
OSErr err = LaunchApplication(&lpb);
psn = lpb.launchProcessSN;
return err >= 0;
}
virtual bool IsRunning(ConstStr255Param name)
{
ProcessInfoRec info;
memset(&info, 0, sizeof(info));
info.processInfoLength = sizeof(info);
OSErr err = GetProcessInformation(&psn,&info);
return err == noErr;
}
};
class AppLauncherMultiFinder : public AppLauncher
{
public:
virtual bool Launch(ConstStr255Param name)
{
LaunchParamBlockRec lpb;
memset(&lpb, 0, sizeof(lpb));
lpb.reserved1 = (unsigned long) name;
lpb.reserved2 = 0;
lpb.launchBlockID = extendedBlock;
lpb.launchEPBLength = 6;
lpb.launchFileFlags = 0;
lpb.launchControlFlags = 0xC000;
OSErr err = LaunchApplication(&lpb);
return err >= 0;
}
virtual bool IsRunning(ConstStr255Param name)
{
uint8_t *fcbs = *(uint8_t**)0x34E; // FCBSPtr;
uint16_t bufSize = * (uint16_t*) fcbs;
uint8_t *end = fcbs + bufSize;
for(uint8_t *fcb = fcbs + 2; fcb < end; fcb += 94)
{
if(*(uint32_t*) fcb == 0)
continue;
if(*(OSType*) (fcb + 0x32) != 'APPL')
continue;
if(EqualString(fcb + 0x3E, "\pRetro68App", true, true))
return true;
}
return false;
}
};
int main()
@ -480,24 +377,7 @@ int main()
LaunchServer server(&rStream);
std::unique_ptr<AppLauncher> appLauncher;
#if TARGET_API_MAC_CARBON || TARGET_RT_MAC_CFM
appLauncher = std::make_unique<AppLauncher7>();
#else
SysEnvRec environ;
SysEnvirons(curSysEnvVers, &environ);
if(environ.systemVersion >= 0x0700)
appLauncher = std::make_unique<AppLauncher7>();
else
{
uint32_t& Twitcher2 = *(uint32_t*) 0xB7C;
if(Twitcher2 == 0 || Twitcher2 == 0xFFFFFFFF)
appLauncher = std::make_unique<AppLauncherSingle>();
else
appLauncher = std::make_unique<AppLauncherMultiFinder>();
}
#endif
std::unique_ptr<AppLauncher> appLauncher = CreateAppLauncher();
for(;;)
{
@ -558,7 +438,9 @@ int main()
if(server.state == LaunchServer::State::launch)
{
if(appLauncher->Launch("\pRetro68App"))
gSerialStream->close();
bool launched = appLauncher->Launch("\pRetro68App");
if(launched)
{
server.state = LaunchServer::State::wait;
nullEventCounter = 0;
@ -575,6 +457,7 @@ int main()
{
if(!appLauncher->IsRunning("\pRetro68App"))
{
gSerialStream->open();
server.state = LaunchServer::State::respond;
uint32_t zero = 0;
rStream.write(&zero, 4);

View File

@ -7,18 +7,8 @@
MacSerialStream::MacSerialStream()
{
OSErr err;
err = OpenDriver("\p.AOut", &outRefNum);
err = OpenDriver("\p.AIn", &inRefNum);
SerSetBuf(inRefNum, inputBuffer, kInputBufferSize);
SerShk shk;
memset(&shk, 0, sizeof(shk));
shk.fCTS = true;
Control(outRefNum, kSERDHandshake, &shk);
SerReset(outRefNum, baud19200 | data8 | noParity | stop10);
curBaud = 19200;
open();
}
void MacSerialStream::close()
@ -32,6 +22,22 @@ void MacSerialStream::close()
inRefNum = outRefNum = 0;
}
void MacSerialStream::open()
{
OSErr err;
err = OpenDriver("\p.AOut", &outRefNum);
err = OpenDriver("\p.AIn", &inRefNum);
SerSetBuf(inRefNum, inputBuffer, kInputBufferSize);
SerShk shk;
memset(&shk, 0, sizeof(shk));
shk.fCTS = true;
Control(outRefNum, kSERDHandshake, &shk);
setBaud(curBaud);
}
MacSerialStream::~MacSerialStream()
{
close();
@ -70,6 +76,7 @@ void MacSerialStream::idle()
void MacSerialStream::setBaud(int baud)
{
curBaud = baud;
short baudval = 0;
switch(baud)
{

View File

@ -11,6 +11,7 @@ class MacSerialStream : public Stream
char readBuffer[kReadBufferSize];
short outRefNum, inRefNum;
int curBaud;
public:
virtual void write(const void* p, size_t n) override;
@ -20,6 +21,7 @@ public:
~MacSerialStream();
void close();
void open();
void setBaud(int baud);
};