home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / misc / dr.str / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-07  |  9.3 KB  |  451 lines  |  [TEXT/KAHL]

  1. /*** Includes ***/
  2. #include "StrangeGlove.h"
  3.  
  4. #include "SerialI/O.h"            /* Needs to include serial routine prototypes */
  5. #include <Serial.h>                /* For THINK C 5.0.2, was <SerialDvr.h> for 4.0.5*/
  6. #include "RecentADT.h"
  7. #include "Terminal.h"
  8. #include "Glove.h"
  9. #include "main.h"
  10.  
  11. WindowPtr        gTermWindow, gADTWindow, gDataWindow, gBallWindow;
  12. GrafPort        dataOffScr, ballOffScr;
  13. Boolean            gDone, gWNEImplemented, gUpdateSlide;
  14. Boolean            gTerminal, gShowADT, gShowData, gContinuous, gBall;
  15. Boolean            gSmooth;
  16. EventRecord        gTheEvent;
  17. MenuHandle        gAppleMenu, gFileMenu, gEditMenu, gGloveMenu, gTerminalMenu, gWindowMenu;
  18. MenuHandle        gDataRateMenu, gDataBitsMenu, gStopBitsMenu, gParityMenu;
  19. Rect            gDragRect;
  20.  
  21. int                gDataRate, gDataBits, gStopBits, gParity;
  22.  
  23. char            *gTermBuffer;
  24. GloveState        gTheGlove;
  25.  
  26.  
  27. /***** main *****/
  28. main()
  29. {
  30.     ToolBoxInit();
  31.     SetUpDragRect();
  32.     MenuBarInit();
  33.     WindowInit();
  34.     MyInit();
  35.     MainLoop();
  36. }
  37.  
  38.  
  39. /***** ToolBoxInit *****/
  40. ToolBoxInit()
  41. {
  42.     InitGraf(&thePort);
  43.     InitFonts();
  44.     FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(NIL_POINTER);
  49.     InitCursor();
  50. }
  51.  
  52.  
  53. /***** SetUpDragRect *****/
  54. SetUpDragRect()
  55. {
  56.     gDragRect = screenBits.bounds;
  57.     gDragRect.left += DRAG_THRESHOLD;
  58.     gDragRect.right -= DRAG_THRESHOLD;
  59.     gDragRect.bottom -= DRAG_THRESHOLD;
  60. }
  61.  
  62.  
  63. /***** WindowInit *****/
  64. WindowInit()
  65. {
  66.     gTermWindow = GetNewWindow(TERM_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
  67.     gADTWindow = GetNewWindow(ADT_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
  68.     gDataWindow = GetNewWindow(DATA_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
  69.     MkPort(&dataOffScr, gDataWindow->portRect);
  70.     gBallWindow = GetNewWindow(BALL_WIND_ID, NIL_POINTER, (WindowPtr) MOVE_TO_FRONT);
  71.     MkPort(&ballOffScr, gBallWindow->portRect);
  72. }
  73.  
  74.  
  75. /*** MyInit ***/
  76. MyInit()
  77. {
  78.     gTerminal = FALSE;                    /* Set Global Terminal Settings */
  79.     gDataRate = DEF_RATE;
  80.     gDataBits = DEF_DATA_BITS;
  81.     gStopBits = DEF_STOP_BITS;
  82.     gParity = DEF_PARITY;
  83.     gShowADT = FALSE;
  84.     gContinuous = FALSE;
  85.     gUpdateSlide = FALSE;
  86.     gShowData = FALSE;
  87.     gSmooth = TRUE;
  88.     gBall = FALSE;
  89.     AdjustTerminalMenu();                /* Update Terminal Menu Settings */
  90.     AdjustGloveMenu();                    /* Update Glove Menu Settings */
  91.     
  92.     gTermBuffer = NewPtr(TERMSIZE);        /* Create Terminal Buffer */
  93. }    
  94.  
  95.  
  96. /***** MainLoop *****/
  97. MainLoop()
  98. {
  99.     gDone = FALSE;
  100.  
  101.     InitRecent();
  102.     OpenGlove(gDataRate+gDataBits+gStopBits+gParity);
  103.     
  104.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) !=
  105.                         NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
  106.     while (gDone == FALSE)
  107.     {
  108.         DoEvent();
  109.     }
  110.     
  111.     CloseGlove();
  112.     DisposPtr(gTermBuffer);                /* Dispose of Terminal Buffer */
  113. }
  114.  
  115.  
  116. /***** DoEvent *****/
  117. DoEvent()
  118. {
  119.     GrafPtr    oldPort;
  120.  
  121.     if (gWNEImplemented)
  122.         WaitNextEvent(everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION);
  123.     else
  124.     {
  125.         SystemTask();
  126.         GetNextEvent(everyEvent, &gTheEvent);
  127.     }
  128.     
  129.     switch (gTheEvent.what)
  130.     {
  131.         case nullEvent:
  132.             DoNull();
  133.             break;
  134.         case mouseDown:
  135.             DoMouseDown();
  136.             break;
  137.         case keyDown:
  138.         case autoKey:
  139.             DoKey();
  140.             break;
  141.         case updateEvt:
  142.             if (!IsDAWindow ( (WindowPtr) gTheEvent.message))
  143.             {
  144.                 GetPort(&oldPort);
  145.                 SetPort( (GrafPtr) gTheEvent.message);
  146.                 BeginUpdate( (WindowPtr) gTheEvent.message);
  147.                 
  148.                 if ((WindowPtr)gTheEvent.message == gDataWindow)
  149.                     DisplayGlove(&gTheGlove);
  150.                 if ((WindowPtr)gTheEvent.message == gBallWindow)
  151.                     DrawBall(gTheGlove.x + 128, 128 - gTheGlove.y, gTheGlove.z + 128);
  152.                 if ((WindowPtr)gTheEvent.message == gADTWindow)
  153.                     DumpRecent();
  154.                 
  155.                 EndUpdate( (WindowPtr) gTheEvent.message);
  156.                 SetPort(oldPort);
  157.             }
  158.             break;
  159.     }
  160. }
  161.  
  162.  
  163. /***** HandelNull *****/
  164. DoNull()
  165. {
  166.     int count, i, received;
  167.  
  168.     received = 0;
  169.     if (count = GetSerial(gTermBuffer))
  170.     {
  171.         i = 0;
  172.         while (i < count)
  173.         {
  174.             if (gTerminal)
  175.                 WriteTermWindow(gTermBuffer[i]);
  176.             AddToRecent(gTermBuffer[i]);
  177.             if (gTermBuffer[i] == CONT_FLAG)
  178.                 received = 1;
  179.             i++;
  180.         }
  181.     }
  182.     if (gShowADT && gUpdateSlide)
  183.         DumpRecent();
  184.     if (received)
  185.     {
  186.         RecentToGlove(&gTheGlove);
  187.         DisplayGlove(&gTheGlove);
  188.         if (gBall)
  189.             DrawBall(gTheGlove.x + 128, 128 - gTheGlove.y, gTheGlove.z + 128);
  190.     }
  191. }
  192.  
  193.  
  194. /***** DoMouseDown *****/
  195. DoMouseDown()
  196. {
  197.     WindowPtr    whichWindow;
  198.     short int    thePart;
  199.     long int    menuChoice, windSize;
  200.     
  201.     thePart = FindWindow(gTheEvent.where, &whichWindow);
  202.     switch (thePart)
  203.     {
  204.         case inMenuBar:
  205.             AdjustEditMenu();
  206.             menuChoice = MenuSelect(gTheEvent.where);
  207.             DoMenuChoice(menuChoice);
  208.             break;
  209.         case inSysWindow:
  210.             SystemClick(&gTheEvent, whichWindow);
  211.             break;
  212.         case inDrag:
  213.             DragWindow(whichWindow, gTheEvent.where, &gDragRect);
  214.             break;
  215.         case inGoAway:
  216.             if (TrackGoAway(whichWindow, gTheEvent.where))
  217.                 DoGoAway(whichWindow);
  218.             break;
  219.         case inContent:
  220.             SelectWindow(whichWindow);
  221.             break;
  222.     }
  223. }
  224.  
  225.  
  226. /***** IsDAWindow *****/
  227. IsDAWindow(whichWindow)
  228. WindowPtr whichWindow;
  229. {
  230.     if (whichWindow == NIL_POINTER)
  231.         return (FALSE);
  232.     else    /* DA windows have negative windowKinds */
  233.         return (((WindowPeek)whichWindow)->windowKind < 0);
  234. }
  235.  
  236.  
  237. /***** DoGoAway *****/
  238. DoGoAway(whichWindow)
  239. WindowPtr whichWindow;
  240. {
  241.     if (whichWindow == gTermWindow)
  242.         StopTerm();
  243.     else if (whichWindow == gADTWindow)
  244.         HideADT();
  245.     else if (whichWindow == gDataWindow)
  246.         HideData();
  247.     else if (whichWindow == gBallWindow)
  248.         StopBall();
  249. }
  250.  
  251.  
  252. /***** DoKey *****/
  253. DoKey()
  254. {
  255.     char theChar;
  256.     
  257.     theChar = gTheEvent.message & charCodeMask;
  258.     
  259.     if ((gTheEvent.modifiers & cmdKey) != 0)
  260.         DoMenuChoice(MenuKey(theChar));
  261.     else if (gTerminal)
  262.     {
  263.         PutSerial(&theChar,1);
  264.         if (ECHO)
  265.             WriteTermWindow(theChar);
  266.     }
  267. }
  268.  
  269.  
  270. /***** ErrorHandler *****/
  271. ErrorHandler(stringNum)
  272. int stringNum;
  273. {
  274.     StringHandle    errorStringH;
  275.     
  276.     if ((errorStringH = GetString(stringNum)) == NIL_POINTER)
  277.         ParamText(HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING);
  278.     else
  279.     {
  280.         HLock(errorStringH);
  281.         ParamText(*errorStringH, NIL_STRING, NIL_STRING, NIL_STRING);
  282.         HUnlock(errorStringH);
  283.     }
  284.     StopAlert(ERROR_ALERT_ID, NIL_POINTER);
  285.     ExitToShell();
  286. }
  287.  
  288.  
  289. /***** GetTextName *****/
  290. GetTextName(SFReply *replyPtr)
  291. {
  292.     Point        myPoint;
  293.     SFTypeList    typeList;
  294.     int            numTypes;
  295.     
  296.     myPoint.h = 100;
  297.     myPoint.v = 100;
  298.     typeList[0] = 'TEXT';
  299.     numTypes = 1;
  300.     SFGetFile(myPoint, IGNORED_STRING, NIL_FILE_FILTER, numTypes, &typeList,
  301.         NIL_DIALOG_HOOK, replyPtr);
  302. }
  303.  
  304.  
  305. /***** SendText *****/
  306. SendText()
  307. {
  308.     SFReply    reply;
  309.     int        srcFile;
  310.     long    textSize;
  311.     Ptr        textPtr;
  312.     Str255    myString;
  313.     int        i;
  314.     
  315.     GetTextName(&reply);
  316.     if (reply.good)            /* User didn't cancel dialog */
  317.     {
  318.         if (FSOpen(reply.fName, reply.vRefNum, &srcFile) != noErr)
  319.             ErrorHandler(CANT_OPEN_FILE);
  320.         if (GetEOF(srcFile, &textSize) != noErr)
  321.             ErrorHandler(GET_EOF_ERROR);
  322.         
  323.         if ((textPtr = NewPtr(textSize)) == NIL_POINTER)
  324.             ErrorHandler(OUT_OF_MEMORY);
  325.         if (FSRead(srcFile, &textSize, textPtr) != noErr)
  326.             ErrorHandler(CANT_READ_FILE);
  327.         
  328.         FSClose(srcFile);
  329.         
  330.         for (i = 0; i < textSize; i++)
  331.             PutSerial(&textPtr[i],1);
  332.         DisposPtr(textPtr);
  333.     }
  334. }
  335.  
  336.  
  337. /***** ContinuePrompt *****/
  338. int    ContinuePrompt(int stringNum)
  339. {
  340.     StringHandle    stringH;
  341.     
  342.     if ((stringH = GetString(stringNum)) == NIL_POINTER)
  343.         ParamText(HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING);
  344.     else
  345.     {
  346.         HLock(stringH);
  347.         ParamText(*stringH, NIL_STRING, NIL_STRING, NIL_STRING);
  348.         HUnlock(stringH);
  349.     }
  350.     return(Alert(CONT_ALERT, NIL_POINTER));
  351. }
  352.  
  353.  
  354. /***** DownloadSRecord *****/
  355. DownloadSRecord()
  356. {
  357.     SFReply    reply;
  358.     int        srcFile;
  359.     long    textSize;
  360.     Ptr        textPtr;
  361.     Str255    myString;
  362.     int        i,j,count;
  363.     Handle    theTextHndl;
  364.     char    *c;
  365.     char    CR    =    13;
  366.     char    FF    =    0xFF;
  367.     
  368.     if ((theTextHndl = GetResource('BOOT', BOOTCODE_ID)) == NULL)
  369.         ErrorHandler(RES_ERROR);
  370.     else
  371.     {
  372.         if (ContinuePrompt(RESET_UC) == OK_BUTTON)
  373.         {
  374.             textSize = GetHandleSize(theTextHndl);
  375.             HLock(theTextHndl);
  376.             
  377.             ChangeSettings(baud1200+data8+stop10+noParity);
  378.             PutSerial(&FF,1);        /* Send one byte to ready bootloader */
  379.             c = *theTextHndl;
  380.             for (i = 0; i < textSize; ++i)
  381.             {
  382.                 PutSerial((c++),1);
  383.             //    WaitEcho((long)240);    //DEBUG
  384.             }    
  385.  
  386.             HUnlock(theTextHndl);
  387.             
  388.             GetTextName(&reply);
  389.             if (reply.good)            /* User didn't cancel dialog */
  390.             {
  391.                 if (FSOpen(reply.fName, reply.vRefNum, &srcFile) != noErr)
  392.                     ErrorHandler(CANT_OPEN_FILE);
  393.                 if (GetEOF(srcFile, &textSize) != noErr)
  394.                     ErrorHandler(GET_EOF_ERROR);
  395.                 
  396.                 if ((textPtr = NewPtr(textSize)) == NIL_POINTER)
  397.                     ErrorHandler(OUT_OF_MEMORY);
  398.                 if (FSRead(srcFile, &textSize, textPtr) != noErr)
  399.                     ErrorHandler(CANT_READ_FILE);
  400.                 
  401.                 FSClose(srcFile);
  402.                 
  403.                 ChangeSettings(baud9600+data8+stop10+noParity);
  404.                 GetSerial(gTermBuffer);
  405.                 PutSerial(&CR,1);
  406.                 WaitEcho((long)240, FALSE);
  407.             
  408.                 PutSerial("I",1);
  409.                 WaitEcho((long)240, FALSE);
  410.                         
  411.                 for (i = 0; i < textSize; i++)
  412.                 {
  413.                     PutSerial(&textPtr[i],1);
  414.                     WaitEcho((long)240, TRUE);
  415.                 }
  416.                 DisposPtr(textPtr);
  417.             }
  418.             ChangeSettings(gDataRate+gDataBits+gStopBits+gParity);
  419.         }
  420.     }
  421.     ReleaseResource(theTextHndl);
  422. }
  423.  
  424.  
  425. char WaitEcho(long ticks, int echo)
  426.     /* Waits until the number of macintosh ticks (60th second) has passed. 
  427.         or a value is received, returns received value or NIL_POINTER if timeout */
  428. {
  429.     int    count,i;
  430.     
  431.     ticks += TickCount();
  432.     while (TickCount() < ticks)
  433.     {
  434.         if ((count = GetSerial(gTermBuffer)) != 0)
  435.         {
  436.             i = 0;
  437.             while (i < count)
  438.             {
  439.                 if (gTerminal && echo)
  440.                     WriteTermWindow(gTermBuffer[i]);
  441.                 AddToRecent(gTermBuffer[i]);
  442.                 i++;
  443.             }
  444.             if (gShowADT && gUpdateSlide && echo)        // Update Recent Data Window
  445.                 DumpRecent();
  446.             return (gTermBuffer[0]);
  447.         }
  448.     }
  449.     return (NIL_POINTER);
  450. }
  451.