home *** CD-ROM | disk | FTP | other *** search
- /*** Includes ***/
- #include "StrangeGlove.h"
-
- #include "SerialI/O.h" /* Needs to include serial routine prototypes */
- #include <Serial.h> /* For THINK C 5.0.2, was <SerialDvr.h> for 4.0.5*/
- #include "RecentADT.h"
- #include "Terminal.h"
- #include "Glove.h"
- #include "main.h"
-
- WindowPtr gTermWindow, gADTWindow, gDataWindow, gBallWindow;
- GrafPort dataOffScr, ballOffScr;
- Boolean gDone, gWNEImplemented, gUpdateSlide;
- Boolean gTerminal, gShowADT, gShowData, gContinuous, gBall;
- Boolean gSmooth;
- EventRecord gTheEvent;
- MenuHandle gAppleMenu, gFileMenu, gEditMenu, gGloveMenu, gTerminalMenu, gWindowMenu;
- MenuHandle gDataRateMenu, gDataBitsMenu, gStopBitsMenu, gParityMenu;
- Rect gDragRect;
-
- int gDataRate, gDataBits, gStopBits, gParity;
-
- char *gTermBuffer;
- GloveState gTheGlove;
-
-
- /***** main *****/
- main()
- {
- ToolBoxInit();
- SetUpDragRect();
- MenuBarInit();
- WindowInit();
- MyInit();
- MainLoop();
- }
-
-
- /***** ToolBoxInit *****/
- ToolBoxInit()
- {
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL_POINTER);
- InitCursor();
- }
-
-
- /***** SetUpDragRect *****/
- SetUpDragRect()
- {
- gDragRect = screenBits.bounds;
- gDragRect.left += DRAG_THRESHOLD;
- gDragRect.right -= DRAG_THRESHOLD;
- gDragRect.bottom -= DRAG_THRESHOLD;
- }
-
-
- /***** WindowInit *****/
- WindowInit()
- {
- gTermWindow = GetNewWindow(TERM_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
- gADTWindow = GetNewWindow(ADT_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
- gDataWindow = GetNewWindow(DATA_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
- MkPort(&dataOffScr, gDataWindow->portRect);
- gBallWindow = GetNewWindow(BALL_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
- MkPort(&ballOffScr, gBallWindow->portRect);
- }
-
-
- /*** MyInit ***/
- MyInit()
- {
- gTerminal = FALSE; /* Set Global Terminal Settings */
- gDataRate = DEF_RATE;
- gDataBits = DEF_DATA_BITS;
- gStopBits = DEF_STOP_BITS;
- gParity = DEF_PARITY;
- gShowADT = FALSE;
- gContinuous = FALSE;
- gUpdateSlide = FALSE;
- gShowData = FALSE;
- gSmooth = TRUE;
- gBall = FALSE;
- AdjustTerminalMenu(); /* Update Terminal Menu Settings */
- AdjustGloveMenu(); /* Update Glove Menu Settings */
-
- gTermBuffer = NewPtr(TERMSIZE); /* Create Terminal Buffer */
- }
-
-
- /***** MainLoop *****/
- MainLoop()
- {
- gDone = FALSE;
-
- InitRecent();
- OpenGlove(gDataRate+gDataBits+gStopBits+gParity);
-
- gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) !=
- NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
- while (gDone == FALSE)
- {
- DoEvent();
- }
-
- CloseGlove();
- DisposPtr(gTermBuffer); /* Dispose of Terminal Buffer */
- }
-
-
- /***** DoEvent *****/
- DoEvent()
- {
- GrafPtr oldPort;
-
- if (gWNEImplemented)
- WaitNextEvent(everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION);
- else
- {
- SystemTask();
- GetNextEvent(everyEvent, &gTheEvent);
- }
-
- switch (gTheEvent.what)
- {
- case nullEvent:
- DoNull();
- break;
- case mouseDown:
- DoMouseDown();
- break;
- case keyDown:
- case autoKey:
- DoKey();
- break;
- case updateEvt:
- if (!IsDAWindow ( (WindowPtr) gTheEvent.message))
- {
- GetPort(&oldPort);
- SetPort( (GrafPtr) gTheEvent.message);
- BeginUpdate( (WindowPtr) gTheEvent.message);
-
- if ((WindowPtr)gTheEvent.message == gDataWindow)
- DisplayGlove(&gTheGlove);
- if ((WindowPtr)gTheEvent.message == gBallWindow)
- DrawBall(gTheGlove.x + 128, 128 - gTheGlove.y, gTheGlove.z + 128);
- if ((WindowPtr)gTheEvent.message == gADTWindow)
- DumpRecent();
-
- EndUpdate( (WindowPtr) gTheEvent.message);
- SetPort(oldPort);
- }
- break;
- }
- }
-
-
- /***** HandelNull *****/
- DoNull()
- {
- int count, i, received;
-
- received = 0;
- if (count = GetSerial(gTermBuffer))
- {
- i = 0;
- while (i < count)
- {
- if (gTerminal)
- WriteTermWindow(gTermBuffer[i]);
- AddToRecent(gTermBuffer[i]);
- if (gTermBuffer[i] == CONT_FLAG)
- received = 1;
- i++;
- }
- }
- if (gShowADT && gUpdateSlide)
- DumpRecent();
- if (received)
- {
- RecentToGlove(&gTheGlove);
- DisplayGlove(&gTheGlove);
- if (gBall)
- DrawBall(gTheGlove.x + 128, 128 - gTheGlove.y, gTheGlove.z + 128);
- }
- }
-
-
- /***** DoMouseDown *****/
- DoMouseDown()
- {
- WindowPtr whichWindow;
- short int thePart;
- long int menuChoice, windSize;
-
- thePart = FindWindow(gTheEvent.where, &whichWindow);
- switch (thePart)
- {
- case inMenuBar:
- AdjustEditMenu();
- menuChoice = MenuSelect(gTheEvent.where);
- DoMenuChoice(menuChoice);
- break;
- case inSysWindow:
- SystemClick(&gTheEvent, whichWindow);
- break;
- case inDrag:
- DragWindow(whichWindow, gTheEvent.where, &gDragRect);
- break;
- case inGoAway:
- if (TrackGoAway(whichWindow, gTheEvent.where))
- DoGoAway(whichWindow);
- break;
- case inContent:
- SelectWindow(whichWindow);
- break;
- }
- }
-
-
- /***** IsDAWindow *****/
- IsDAWindow(whichWindow)
- WindowPtr whichWindow;
- {
- if (whichWindow == NIL_POINTER)
- return (FALSE);
- else /* DA windows have negative windowKinds */
- return (((WindowPeek)whichWindow)->windowKind < 0);
- }
-
-
- /***** DoGoAway *****/
- DoGoAway(whichWindow)
- WindowPtr whichWindow;
- {
- if (whichWindow == gTermWindow)
- StopTerm();
- else if (whichWindow == gADTWindow)
- HideADT();
- else if (whichWindow == gDataWindow)
- HideData();
- else if (whichWindow == gBallWindow)
- StopBall();
- }
-
-
- /***** DoKey *****/
- DoKey()
- {
- char theChar;
-
- theChar = gTheEvent.message & charCodeMask;
-
- if ((gTheEvent.modifiers & cmdKey) != 0)
- DoMenuChoice(MenuKey(theChar));
- else if (gTerminal)
- {
- PutSerial(&theChar,1);
- if (ECHO)
- WriteTermWindow(theChar);
- }
- }
-
-
- /***** ErrorHandler *****/
- ErrorHandler(stringNum)
- int stringNum;
- {
- StringHandle errorStringH;
-
- if ((errorStringH = GetString(stringNum)) == NIL_POINTER)
- ParamText(HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING);
- else
- {
- HLock(errorStringH);
- ParamText(*errorStringH, NIL_STRING, NIL_STRING, NIL_STRING);
- HUnlock(errorStringH);
- }
- StopAlert(ERROR_ALERT_ID, NIL_POINTER);
- ExitToShell();
- }
-
-
- /***** GetTextName *****/
- GetTextName(SFReply *replyPtr)
- {
- Point myPoint;
- SFTypeList typeList;
- int numTypes;
-
- myPoint.h = 100;
- myPoint.v = 100;
- typeList[0] = 'TEXT';
- numTypes = 1;
- SFGetFile(myPoint, IGNORED_STRING, NIL_FILE_FILTER, numTypes, &typeList,
- NIL_DIALOG_HOOK, replyPtr);
- }
-
-
- /***** SendText *****/
- SendText()
- {
- SFReply reply;
- int srcFile;
- long textSize;
- Ptr textPtr;
- Str255 myString;
- int i;
-
- GetTextName(&reply);
- if (reply.good) /* User didn't cancel dialog */
- {
- if (FSOpen(reply.fName, reply.vRefNum, &srcFile) != noErr)
- ErrorHandler(CANT_OPEN_FILE);
- if (GetEOF(srcFile, &textSize) != noErr)
- ErrorHandler(GET_EOF_ERROR);
-
- if ((textPtr = NewPtr(textSize)) == NIL_POINTER)
- ErrorHandler(OUT_OF_MEMORY);
- if (FSRead(srcFile, &textSize, textPtr) != noErr)
- ErrorHandler(CANT_READ_FILE);
-
- FSClose(srcFile);
-
- for (i = 0; i < textSize; i++)
- PutSerial(&textPtr[i],1);
- DisposPtr(textPtr);
- }
- }
-
-
- /***** ContinuePrompt *****/
- int ContinuePrompt(int stringNum)
- {
- StringHandle stringH;
-
- if ((stringH = GetString(stringNum)) == NIL_POINTER)
- ParamText(HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING);
- else
- {
- HLock(stringH);
- ParamText(*stringH, NIL_STRING, NIL_STRING, NIL_STRING);
- HUnlock(stringH);
- }
- return(Alert(CONT_ALERT, NIL_POINTER));
- }
-
-
- /***** DownloadSRecord *****/
- DownloadSRecord()
- {
- SFReply reply;
- int srcFile;
- long textSize;
- Ptr textPtr;
- Str255 myString;
- int i,j,count;
- Handle theTextHndl;
- char *c;
- char CR = 13;
- char FF = 0xFF;
-
- if ((theTextHndl = GetResource('BOOT', BOOTCODE_ID)) == NULL)
- ErrorHandler(RES_ERROR);
- else
- {
- if (ContinuePrompt(RESET_UC) == OK_BUTTON)
- {
- textSize = GetHandleSize(theTextHndl);
- HLock(theTextHndl);
-
- ChangeSettings(baud1200+data8+stop10+noParity);
- PutSerial(&FF,1); /* Send one byte to ready bootloader */
- c = *theTextHndl;
- for (i = 0; i < textSize; ++i)
- {
- PutSerial((c++),1);
- // WaitEcho((long)240); //DEBUG
- }
-
- HUnlock(theTextHndl);
-
- GetTextName(&reply);
- if (reply.good) /* User didn't cancel dialog */
- {
- if (FSOpen(reply.fName, reply.vRefNum, &srcFile) != noErr)
- ErrorHandler(CANT_OPEN_FILE);
- if (GetEOF(srcFile, &textSize) != noErr)
- ErrorHandler(GET_EOF_ERROR);
-
- if ((textPtr = NewPtr(textSize)) == NIL_POINTER)
- ErrorHandler(OUT_OF_MEMORY);
- if (FSRead(srcFile, &textSize, textPtr) != noErr)
- ErrorHandler(CANT_READ_FILE);
-
- FSClose(srcFile);
-
- ChangeSettings(baud9600+data8+stop10+noParity);
- GetSerial(gTermBuffer);
- PutSerial(&CR,1);
- WaitEcho((long)240, FALSE);
-
- PutSerial("I",1);
- WaitEcho((long)240, FALSE);
-
- for (i = 0; i < textSize; i++)
- {
- PutSerial(&textPtr[i],1);
- WaitEcho((long)240, TRUE);
- }
- DisposPtr(textPtr);
- }
- ChangeSettings(gDataRate+gDataBits+gStopBits+gParity);
- }
- }
- ReleaseResource(theTextHndl);
- }
-
-
- char WaitEcho(long ticks, int echo)
- /* Waits until the number of macintosh ticks (60th second) has passed.
- or a value is received, returns received value or NIL_POINTER if timeout */
- {
- int count,i;
-
- ticks += TickCount();
- while (TickCount() < ticks)
- {
- if ((count = GetSerial(gTermBuffer)) != 0)
- {
- i = 0;
- while (i < count)
- {
- if (gTerminal && echo)
- WriteTermWindow(gTermBuffer[i]);
- AddToRecent(gTermBuffer[i]);
- i++;
- }
- if (gShowADT && gUpdateSlide && echo) // Update Recent Data Window
- DumpRecent();
- return (gTermBuffer[0]);
- }
- }
- return (NIL_POINTER);
- }
-