2018-04-23 19:32:43 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 Wolfgang Thaller.
|
|
|
|
|
|
|
|
This file is part of Retro68.
|
|
|
|
|
|
|
|
Retro68 is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Retro68 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Retro68. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Quickdraw.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <Menus.h>
|
|
|
|
#include <Fonts.h>
|
|
|
|
#include <Resources.h>
|
|
|
|
#include <TextEdit.h>
|
|
|
|
#include <TextUtils.h>
|
|
|
|
#include <Dialogs.h>
|
|
|
|
#include <Devices.h>
|
2018-05-01 23:47:33 +00:00
|
|
|
#include <Traps.h>
|
2018-05-05 21:45:59 +00:00
|
|
|
#include <LowMem.h>
|
2018-04-23 19:32:43 +00:00
|
|
|
|
|
|
|
#include "MacSerialStream.h"
|
2018-04-27 00:06:54 +00:00
|
|
|
#include "AppLauncher.h"
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
#include <ReliableStream.h>
|
2018-05-05 16:53:27 +00:00
|
|
|
#include "ServerProtocol.h"
|
2018-04-23 19:32:43 +00:00
|
|
|
#include <Processes.h>
|
|
|
|
#include <string.h>
|
2018-04-26 23:36:33 +00:00
|
|
|
#include <memory>
|
2018-04-23 19:32:43 +00:00
|
|
|
|
|
|
|
#include <UnreliableStream.h>
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kMenuApple = 128,
|
|
|
|
kMenuFile,
|
2018-04-24 20:06:32 +00:00
|
|
|
kMenuEdit,
|
|
|
|
kMenuSpeed
|
2018-04-23 19:32:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kItemAbout = 1,
|
|
|
|
|
|
|
|
kItemQuit = 1
|
|
|
|
};
|
|
|
|
|
2018-04-27 07:00:33 +00:00
|
|
|
struct Prefs
|
|
|
|
{
|
|
|
|
long baud = 19200;
|
|
|
|
bool inSubLaunch = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Prefs gPrefs;
|
|
|
|
bool gQuitting = false;
|
2018-04-24 20:06:32 +00:00
|
|
|
|
|
|
|
void SetBaud(long baud);
|
2018-04-23 19:32:43 +00:00
|
|
|
|
|
|
|
void ShowAboutBox()
|
|
|
|
{
|
|
|
|
WindowRef w = GetNewWindow(128, NULL, (WindowPtr) -1);
|
|
|
|
#if !TARGET_API_MAC_CARBON
|
|
|
|
MacMoveWindow(w,
|
|
|
|
qd.screenBits.bounds.right/2 - w->portRect.right/2,
|
|
|
|
qd.screenBits.bounds.bottom/2 - w->portRect.bottom/2,
|
|
|
|
false);
|
|
|
|
#endif
|
|
|
|
ShowWindow(w);
|
|
|
|
#if TARGET_API_MAC_CARBON
|
|
|
|
SetPortWindowPort(w);
|
|
|
|
#else
|
|
|
|
SetPort(w);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Handle h = GetResource('TEXT', 128);
|
|
|
|
HLock(h);
|
|
|
|
#if TARGET_API_MAC_CARBON
|
|
|
|
Rect r;
|
|
|
|
GetWindowPortBounds(w,&r);
|
|
|
|
#else
|
|
|
|
Rect r = w->portRect;
|
|
|
|
#endif
|
|
|
|
InsetRect(&r, 10,10);
|
|
|
|
TETextBox(*h, GetHandleSize(h), &r, teJustLeft);
|
|
|
|
|
|
|
|
ReleaseResource(h);
|
|
|
|
while(!Button())
|
|
|
|
;
|
|
|
|
while(Button())
|
|
|
|
;
|
|
|
|
FlushEvents(everyEvent, 0);
|
|
|
|
DisposeWindow(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateMenus()
|
|
|
|
{
|
|
|
|
MenuRef m = GetMenu(kMenuFile);
|
|
|
|
WindowRef w = FrontWindow();
|
|
|
|
|
|
|
|
#if TARGET_API_MAC_CARBON
|
|
|
|
#define EnableItem EnableMenuItem
|
|
|
|
#define DisableItem DisableMenuItem
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m = GetMenu(kMenuEdit);
|
|
|
|
if(w && GetWindowKind(w) < 0)
|
|
|
|
{
|
|
|
|
// Desk accessory in front: Enable edit menu items
|
|
|
|
EnableItem(m,1);
|
|
|
|
EnableItem(m,3);
|
|
|
|
EnableItem(m,4);
|
|
|
|
EnableItem(m,5);
|
|
|
|
EnableItem(m,6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Application window or nothing in front, disable edit menu
|
|
|
|
DisableItem(m,1);
|
|
|
|
DisableItem(m,3);
|
|
|
|
DisableItem(m,4);
|
|
|
|
DisableItem(m,5);
|
|
|
|
DisableItem(m,6);
|
|
|
|
}
|
2018-04-24 20:06:32 +00:00
|
|
|
|
|
|
|
m = GetMenu(kMenuSpeed);
|
|
|
|
for(int i = 1; i <= CountMenuItems(m); i++)
|
|
|
|
{
|
|
|
|
Str255 str;
|
|
|
|
long baud;
|
|
|
|
GetMenuItemText(m, i, str);
|
|
|
|
StringToNum(str, &baud);
|
2018-04-27 07:00:33 +00:00
|
|
|
CheckMenuItem(m, i, baud == gPrefs.baud);
|
2018-04-24 20:06:32 +00:00
|
|
|
}
|
2018-04-23 19:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoMenuCommand(long menuCommand)
|
|
|
|
{
|
|
|
|
Str255 str;
|
|
|
|
WindowRef w;
|
|
|
|
short menuID = menuCommand >> 16;
|
|
|
|
short menuItem = menuCommand & 0xFFFF;
|
|
|
|
if(menuID == kMenuApple)
|
|
|
|
{
|
|
|
|
if(menuItem == kItemAbout)
|
|
|
|
ShowAboutBox();
|
|
|
|
#if !TARGET_API_MAC_CARBON
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetMenuItemText(GetMenu(128), menuItem, str);
|
|
|
|
OpenDeskAcc(str);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if(menuID == kMenuFile)
|
|
|
|
{
|
|
|
|
switch(menuItem)
|
|
|
|
{
|
|
|
|
case kItemQuit:
|
2018-04-27 07:00:33 +00:00
|
|
|
gQuitting = true;
|
2018-04-23 19:32:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(menuID == kMenuEdit)
|
|
|
|
{
|
|
|
|
#if !TARGET_API_MAC_CARBON
|
|
|
|
if(!SystemEdit(menuItem - 1))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// edit command not handled by desk accessory
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 20:06:32 +00:00
|
|
|
else if(menuID == kMenuSpeed)
|
|
|
|
{
|
|
|
|
GetMenuItemText(GetMenu(menuID), menuItem, str);
|
2018-04-27 07:00:33 +00:00
|
|
|
StringToNum(str, &gPrefs.baud);
|
|
|
|
SetBaud(gPrefs.baud);
|
2018-04-24 20:06:32 +00:00
|
|
|
}
|
2018-04-23 19:32:43 +00:00
|
|
|
HiliteMenu(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WindowPtr statusWindow;
|
|
|
|
Str255 statusString = "\p";
|
|
|
|
int progressDone, progressTotal = 0;
|
|
|
|
|
|
|
|
void DoUpdate(WindowRef w)
|
|
|
|
{
|
|
|
|
if(w != statusWindow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if TARGET_API_MAC_CARBON
|
|
|
|
SetPortWindowPort(w);
|
|
|
|
#else
|
|
|
|
SetPort(w);
|
|
|
|
#endif
|
|
|
|
BeginUpdate(w);
|
|
|
|
EraseRect(&w->portRect);
|
|
|
|
|
|
|
|
MoveTo(10,20);
|
|
|
|
DrawString(statusString);
|
|
|
|
|
|
|
|
Rect r;
|
|
|
|
|
|
|
|
if(progressTotal)
|
|
|
|
{
|
|
|
|
SetRect(&r, 10, 40, w->portRect.right-10, 60);
|
|
|
|
FrameRect(&r);
|
|
|
|
SetRect(&r, 10, 40, 10 + (w->portRect.right-20) * progressDone / progressTotal, 60);
|
|
|
|
PaintRect(&r);
|
|
|
|
}
|
|
|
|
|
2018-05-01 19:17:39 +00:00
|
|
|
Str255 str;
|
|
|
|
NumToString(FreeMem(), str);
|
|
|
|
MoveTo(10,80);
|
|
|
|
DrawString(str); DrawString("\p / ");
|
|
|
|
NumToString(ApplicationZone()->bkLim - (Ptr)ApplicationZone(), str);
|
|
|
|
DrawString(str); DrawString("\p bytes free");
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
EndUpdate(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class AppStatus
|
|
|
|
{
|
|
|
|
ready = 1,
|
|
|
|
downloading = 2,
|
|
|
|
running = 3,
|
|
|
|
uploading = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
void SetStatus(AppStatus stat, int done = 0, int total = 0)
|
|
|
|
{
|
|
|
|
GetIndString(statusString,128,(short)stat);
|
|
|
|
|
|
|
|
progressTotal = total;
|
|
|
|
progressDone = done;
|
|
|
|
SetPort(statusWindow);
|
|
|
|
InvalRect(&statusWindow->portRect);
|
|
|
|
}
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
class LaunchServer : public StreamListener
|
2018-04-23 19:32:43 +00:00
|
|
|
{
|
2018-04-23 23:42:36 +00:00
|
|
|
Stream* stream;
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
uint32_t dataSize, rsrcSize;
|
|
|
|
uint32_t remainingSize;
|
|
|
|
short refNum;
|
|
|
|
public:
|
2018-04-23 23:42:36 +00:00
|
|
|
LaunchServer(Stream* stream) : stream(stream)
|
|
|
|
{
|
|
|
|
stream->setListener(this);
|
|
|
|
}
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
enum class State
|
|
|
|
{
|
2018-05-05 16:53:27 +00:00
|
|
|
command,
|
|
|
|
header,
|
2018-04-23 19:32:43 +00:00
|
|
|
data,
|
|
|
|
rsrc,
|
|
|
|
launch,
|
2018-04-23 23:42:36 +00:00
|
|
|
wait,
|
|
|
|
respond
|
2018-04-23 19:32:43 +00:00
|
|
|
};
|
2018-05-05 16:53:27 +00:00
|
|
|
State state = State::command;
|
|
|
|
RemoteCommand command;
|
2018-04-23 19:32:43 +00:00
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
void onReset()
|
|
|
|
{
|
|
|
|
SetStatus(AppStatus::ready, 0, 0);
|
2018-05-05 16:53:27 +00:00
|
|
|
state = State::command;
|
2018-04-23 23:42:36 +00:00
|
|
|
}
|
2018-04-23 19:32:43 +00:00
|
|
|
|
|
|
|
size_t onReceive(const uint8_t* p, size_t n)
|
|
|
|
{
|
|
|
|
switch(state)
|
|
|
|
{
|
2018-05-05 16:53:27 +00:00
|
|
|
case State::command:
|
2018-04-23 19:32:43 +00:00
|
|
|
{
|
2018-05-05 16:53:27 +00:00
|
|
|
if(n < 1)
|
2018-04-23 19:32:43 +00:00
|
|
|
return 0;
|
2018-05-05 16:53:27 +00:00
|
|
|
command = (RemoteCommand)p[0];
|
2018-05-05 21:45:59 +00:00
|
|
|
if(command == RemoteCommand::launchApp || command == RemoteCommand::upgradeLauncher)
|
2018-05-05 16:53:27 +00:00
|
|
|
state = State::header;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case State::header:
|
|
|
|
{
|
|
|
|
if(n < 16)
|
|
|
|
return 0;
|
|
|
|
OSType type = *(const OSType*)(p+0);
|
|
|
|
OSType creator = *(const OSType*)(p+4);
|
|
|
|
dataSize = *(const uint32_t*)(p+8);
|
|
|
|
rsrcSize = *(const uint32_t*)(p+12);
|
2018-04-23 19:32:43 +00:00
|
|
|
|
|
|
|
SetStatus(AppStatus::downloading, 0, dataSize + rsrcSize);
|
|
|
|
|
|
|
|
FSDelete("\pRetro68App", 0);
|
2018-05-05 16:53:27 +00:00
|
|
|
Create("\pRetro68App", 0, creator, type);
|
2018-04-23 19:32:43 +00:00
|
|
|
OpenDF("\pRetro68App", 0, &refNum);
|
2018-04-24 05:57:43 +00:00
|
|
|
FSDelete("\pout", 0);
|
|
|
|
Create("\pout", 0, 'ttxt', 'TEXT');
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
state = State::data;
|
|
|
|
remainingSize = dataSize;
|
2018-05-05 16:53:27 +00:00
|
|
|
return 16;
|
2018-04-23 19:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case State::data:
|
|
|
|
{
|
|
|
|
long count = n < remainingSize ? n : remainingSize;
|
|
|
|
|
|
|
|
FSWrite(refNum, &count, p);
|
|
|
|
remainingSize -= count;
|
|
|
|
|
|
|
|
SetStatus(AppStatus::downloading, dataSize - remainingSize, dataSize + rsrcSize);
|
|
|
|
|
|
|
|
if(remainingSize)
|
|
|
|
return count;
|
|
|
|
|
|
|
|
FSClose(refNum);
|
|
|
|
OpenRF("\pRetro68App", 0, &refNum);
|
|
|
|
state = State::rsrc;
|
|
|
|
remainingSize = rsrcSize;
|
|
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
case State::rsrc:
|
|
|
|
{
|
|
|
|
long count = n < remainingSize ? n : remainingSize;
|
|
|
|
|
|
|
|
FSWrite(refNum, &count, p);
|
|
|
|
remainingSize -= count;
|
|
|
|
|
|
|
|
SetStatus(AppStatus::downloading, dataSize + rsrcSize - remainingSize, dataSize + rsrcSize);
|
|
|
|
|
|
|
|
if(remainingSize)
|
|
|
|
return count;
|
|
|
|
|
|
|
|
FSClose(refNum);
|
|
|
|
|
|
|
|
SetStatus(AppStatus::running);
|
|
|
|
|
|
|
|
state = State::launch;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-24 05:57:43 +00:00
|
|
|
short outRefNum;
|
|
|
|
long outSize, outSizeRemaining;
|
2018-04-24 20:06:32 +00:00
|
|
|
MacSerialStream *gSerialStream;
|
2018-04-26 22:50:52 +00:00
|
|
|
int nullEventCounter = 0;
|
2018-04-24 20:06:32 +00:00
|
|
|
|
|
|
|
void SetBaud(long baud)
|
|
|
|
{
|
|
|
|
gSerialStream->setBaud(baud);
|
|
|
|
}
|
2018-04-24 05:57:43 +00:00
|
|
|
|
2018-04-27 07:00:33 +00:00
|
|
|
void StartResponding(LaunchServer& server, ReliableStream& rStream)
|
|
|
|
{
|
|
|
|
server.state = LaunchServer::State::respond;
|
|
|
|
uint32_t zero = 0;
|
|
|
|
rStream.write(&zero, 4);
|
|
|
|
|
|
|
|
OpenDF("\pout", 0, &outRefNum);
|
|
|
|
GetEOF(outRefNum, &outSize);
|
|
|
|
outSizeRemaining = outSize;
|
|
|
|
SetStatus(AppStatus::uploading, 0, outSize);
|
|
|
|
|
|
|
|
rStream.write(&outSize, 4);
|
|
|
|
rStream.flushWrite();
|
|
|
|
}
|
2018-04-26 23:36:33 +00:00
|
|
|
|
2018-04-27 07:00:33 +00:00
|
|
|
void WritePrefs()
|
|
|
|
{
|
|
|
|
short refNum;
|
|
|
|
Create("\pLaunchAPPLServer Preferences", 0, '????', 'LAPR');
|
|
|
|
if(OpenDF("\pLaunchAPPLServer Preferences", 0, &refNum) == noErr)
|
|
|
|
{
|
|
|
|
long count = sizeof(gPrefs);
|
|
|
|
FSWrite(refNum, &count, &gPrefs);
|
|
|
|
FSClose(refNum);
|
|
|
|
}
|
|
|
|
}
|
2018-04-26 23:36:33 +00:00
|
|
|
|
2018-05-05 21:59:38 +00:00
|
|
|
pascal OSErr aeRun (const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
|
|
|
|
{
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
pascal OSErr aeOpen (const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
|
|
|
|
{
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
pascal OSErr aePrint (const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
|
|
|
|
{
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
pascal OSErr aeQuit (const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
|
|
|
|
{
|
|
|
|
gQuitting = true;
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
int main()
|
|
|
|
{
|
2018-05-01 19:17:39 +00:00
|
|
|
// default stack size is 8KB on B&W macs
|
|
|
|
// and 24 KB on Color macs.
|
|
|
|
// 8KB seems to be not quite enough,
|
|
|
|
// so increase stack size by 8KB.
|
|
|
|
SetApplLimit(GetApplLimit() - 8192);
|
|
|
|
MaxApplZone();
|
2018-04-23 19:32:43 +00:00
|
|
|
#if !TARGET_API_MAC_CARBON
|
|
|
|
InitGraf(&qd.thePort);
|
|
|
|
InitFonts();
|
|
|
|
InitWindows();
|
|
|
|
InitMenus();
|
|
|
|
TEInit();
|
|
|
|
InitDialogs(NULL);
|
2018-05-05 21:59:38 +00:00
|
|
|
#endif
|
2018-05-01 19:17:39 +00:00
|
|
|
|
2018-05-05 21:59:38 +00:00
|
|
|
#if TARGET_CPU_68K && !TARGET_RT_MAC_CFM
|
2018-05-01 23:47:33 +00:00
|
|
|
short& ROM85 = *(short*) 0x028E;
|
|
|
|
Boolean is128KROM = (ROM85 > 0);
|
|
|
|
Boolean hasSysEnvirons = false;
|
|
|
|
Boolean hasWaitNextEvent = false;
|
2018-05-05 21:59:38 +00:00
|
|
|
Boolean hasAppleEvents = false;
|
2018-05-01 23:47:33 +00:00
|
|
|
if (is128KROM)
|
|
|
|
{
|
2018-05-05 21:59:38 +00:00
|
|
|
UniversalProcPtr trapUnimpl = GetToolTrapAddress(_Unimplemented);
|
2018-05-01 23:47:33 +00:00
|
|
|
UniversalProcPtr trapSysEnv = GetOSTrapAddress(_SysEnvirons);
|
|
|
|
UniversalProcPtr trapWaitNextEvent = GetToolTrapAddress(_WaitNextEvent);
|
2018-05-05 21:59:38 +00:00
|
|
|
UniversalProcPtr trapAppleEvents = GetToolTrapAddress(_Pack8);
|
2018-05-01 23:47:33 +00:00
|
|
|
|
|
|
|
hasSysEnvirons = (trapSysEnv != trapUnimpl);
|
|
|
|
hasWaitNextEvent = (trapWaitNextEvent != trapUnimpl);
|
2018-05-05 21:59:38 +00:00
|
|
|
hasAppleEvents = (trapAppleEvents != trapUnimpl);
|
2018-05-01 23:47:33 +00:00
|
|
|
}
|
2018-05-05 21:59:38 +00:00
|
|
|
#else
|
|
|
|
const Boolean hasSysEnvirons = true;
|
|
|
|
const Boolean hasWaitNextEvent = true;
|
|
|
|
const Boolean hasAppleEvents = true;
|
2018-05-01 23:47:33 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
SetMenuBar(GetNewMBar(128));
|
|
|
|
AppendResMenu(GetMenu(128), 'DRVR');
|
|
|
|
DrawMenuBar();
|
|
|
|
|
|
|
|
InitCursor();
|
|
|
|
|
2018-05-05 21:59:38 +00:00
|
|
|
if(hasAppleEvents)
|
|
|
|
{
|
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerUPP(&aeRun), 0, false);
|
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP(&aeOpen), 0, false);
|
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerUPP(&aePrint), 0, false);
|
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(&aeQuit), 0, false);
|
|
|
|
}
|
|
|
|
|
2018-04-23 19:32:43 +00:00
|
|
|
statusWindow = GetNewWindow(129, NULL, (WindowPtr) -1);
|
|
|
|
SetStatus(AppStatus::ready);
|
2018-04-27 07:00:33 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
short refNum;
|
|
|
|
if(OpenDF("\pLaunchAPPLServer Preferences", 0, &refNum) == noErr)
|
|
|
|
{
|
|
|
|
long count = sizeof(gPrefs);
|
|
|
|
FSRead(refNum, &count, &gPrefs);
|
|
|
|
FSClose(refNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MacSerialStream stream(gPrefs.baud);
|
2018-04-24 20:06:32 +00:00
|
|
|
gSerialStream = &stream;
|
2018-04-23 19:32:43 +00:00
|
|
|
|
|
|
|
//#define SIMULATE_ERRORS
|
|
|
|
#ifdef SIMULATE_ERRORS
|
|
|
|
UnreliableStream uStream(stream);
|
2018-04-23 23:42:36 +00:00
|
|
|
ReliableStream rStream(&uStream);
|
2018-04-23 19:32:43 +00:00
|
|
|
#else
|
2018-04-23 23:42:36 +00:00
|
|
|
ReliableStream rStream(&stream);
|
2018-04-23 19:32:43 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
LaunchServer server(&rStream);
|
2018-04-23 19:32:43 +00:00
|
|
|
|
2018-04-27 00:06:54 +00:00
|
|
|
std::unique_ptr<AppLauncher> appLauncher = CreateAppLauncher();
|
2018-04-23 19:32:43 +00:00
|
|
|
|
2018-04-27 07:00:33 +00:00
|
|
|
if(gPrefs.inSubLaunch)
|
|
|
|
{
|
|
|
|
gPrefs.inSubLaunch = false;
|
|
|
|
StartResponding(server, rStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!gQuitting)
|
2018-04-23 19:32:43 +00:00
|
|
|
{
|
|
|
|
EventRecord e;
|
|
|
|
WindowRef win;
|
2018-05-01 23:47:33 +00:00
|
|
|
Boolean hadEvent;
|
|
|
|
|
|
|
|
#if !TARGET_API_MAC_CARBON
|
|
|
|
if(!hasWaitNextEvent)
|
|
|
|
{
|
|
|
|
SystemTask();
|
|
|
|
hadEvent = GetNextEvent(everyEvent, &e);
|
|
|
|
}
|
|
|
|
else
|
2018-04-23 19:32:43 +00:00
|
|
|
#endif
|
2018-05-01 23:47:33 +00:00
|
|
|
{
|
|
|
|
hadEvent = WaitNextEvent(everyEvent, &e, 10, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hadEvent)
|
2018-04-23 19:32:43 +00:00
|
|
|
{
|
|
|
|
switch(e.what)
|
|
|
|
{
|
|
|
|
case keyDown:
|
|
|
|
if(e.modifiers & cmdKey)
|
|
|
|
{
|
|
|
|
UpdateMenus();
|
|
|
|
DoMenuCommand(MenuKey(e.message & charCodeMask));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case mouseDown:
|
|
|
|
switch(FindWindow(e.where, &win))
|
|
|
|
{
|
|
|
|
case inGoAway:
|
|
|
|
if(TrackGoAway(win, e.where))
|
|
|
|
DisposeWindow(win);
|
|
|
|
break;
|
|
|
|
case inDrag:
|
|
|
|
DragWindow(win, e.where, &qd.screenBits.bounds);
|
|
|
|
break;
|
|
|
|
case inMenuBar:
|
|
|
|
UpdateMenus();
|
|
|
|
DoMenuCommand( MenuSelect(e.where) );
|
|
|
|
break;
|
|
|
|
case inContent:
|
|
|
|
SelectWindow(win);
|
|
|
|
break;
|
|
|
|
#if !TARGET_API_MAC_CARBON
|
|
|
|
case inSysWindow:
|
|
|
|
SystemClick(&e, win);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case updateEvt:
|
|
|
|
DoUpdate((WindowRef)e.message);
|
|
|
|
break;
|
2018-05-05 21:59:38 +00:00
|
|
|
case kHighLevelEvent:
|
|
|
|
if(hasAppleEvents)
|
|
|
|
AEProcessAppleEvent(&e);
|
|
|
|
break;
|
2018-04-23 19:32:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-26 22:50:52 +00:00
|
|
|
else
|
|
|
|
nullEventCounter++;
|
2018-04-23 19:32:43 +00:00
|
|
|
|
|
|
|
stream.idle();
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
if(server.state == LaunchServer::State::launch)
|
2018-04-23 19:32:43 +00:00
|
|
|
{
|
2018-04-27 00:06:54 +00:00
|
|
|
gSerialStream->close();
|
2018-04-27 07:00:33 +00:00
|
|
|
gPrefs.inSubLaunch = true;
|
|
|
|
WritePrefs();
|
2018-05-05 21:45:59 +00:00
|
|
|
|
|
|
|
if(server.command == RemoteCommand::upgradeLauncher)
|
|
|
|
{
|
|
|
|
FSDelete("\pLaunchAPPLServer.old", 0);
|
|
|
|
Rename(LMGetCurApName(), 0, "\pLaunchAPPLServer.old");
|
|
|
|
Rename("\pRetro68App", 0, LMGetCurApName());
|
|
|
|
|
|
|
|
LaunchParamBlockRec lpb;
|
|
|
|
memset(&lpb, 0, sizeof(lpb));
|
|
|
|
lpb.reserved1 = (unsigned long) LMGetCurApName();
|
|
|
|
lpb.reserved2 = 0;
|
|
|
|
OSErr err = LaunchApplication(&lpb);
|
|
|
|
ExitToShell();
|
|
|
|
}
|
|
|
|
|
2018-04-27 00:06:54 +00:00
|
|
|
bool launched = appLauncher->Launch("\pRetro68App");
|
2018-04-27 07:00:33 +00:00
|
|
|
gPrefs.inSubLaunch = false;
|
|
|
|
WritePrefs();
|
|
|
|
|
2018-04-27 00:06:54 +00:00
|
|
|
if(launched)
|
2018-04-23 19:32:43 +00:00
|
|
|
{
|
2018-04-26 23:36:33 +00:00
|
|
|
server.state = LaunchServer::State::wait;
|
|
|
|
nullEventCounter = 0;
|
2018-04-26 22:50:52 +00:00
|
|
|
|
2018-04-26 23:36:33 +00:00
|
|
|
SetStatus(AppStatus::running, 0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-05 16:53:27 +00:00
|
|
|
server.state = LaunchServer::State::command;
|
2018-04-26 23:36:33 +00:00
|
|
|
SetStatus(AppStatus::ready, 0, 0);
|
2018-04-23 23:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-26 22:50:52 +00:00
|
|
|
else if(server.state == LaunchServer::State::wait && nullEventCounter > 3)
|
2018-04-23 23:42:36 +00:00
|
|
|
{
|
2018-04-26 23:36:33 +00:00
|
|
|
if(!appLauncher->IsRunning("\pRetro68App"))
|
2018-04-23 23:42:36 +00:00
|
|
|
{
|
2018-04-27 00:06:54 +00:00
|
|
|
gSerialStream->open();
|
2018-04-27 07:00:33 +00:00
|
|
|
StartResponding(server, rStream);
|
2018-04-23 23:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(server.state == LaunchServer::State::respond)
|
|
|
|
{
|
2018-04-24 05:57:43 +00:00
|
|
|
while(outSizeRemaining && rStream.readyToWrite())
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
long count = outSizeRemaining > 1024 ? 1024 : outSizeRemaining;
|
|
|
|
FSRead(outRefNum, &count, buf);
|
|
|
|
rStream.write(buf, count);
|
|
|
|
outSizeRemaining -= count;
|
|
|
|
}
|
|
|
|
SetStatus(AppStatus::uploading, outSize - outSizeRemaining, outSize);
|
|
|
|
|
|
|
|
if(outSizeRemaining == 0)
|
|
|
|
{
|
|
|
|
FSClose(outRefNum);
|
|
|
|
}
|
|
|
|
if(outSizeRemaining == 0 && rStream.allDataArrived())
|
2018-04-23 23:42:36 +00:00
|
|
|
{
|
2018-05-05 16:53:27 +00:00
|
|
|
server.state = LaunchServer::State::command;
|
2018-04-23 23:42:36 +00:00
|
|
|
SetStatus(AppStatus::ready, 0, 0);
|
|
|
|
rStream.reset(0);
|
2018-04-23 19:32:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 07:00:33 +00:00
|
|
|
|
|
|
|
WritePrefs();
|
2018-04-23 19:32:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|