/************************************************************************************* * * File Functions.c * * This file contains the miscellaneous functions for the module. * *************************************************************************************/ #include "AcquireInterface.h" #include "LG-3 Module.h" #include extern TGlobals myGlobals; extern TMInfo myTMInfo; extern int myTaskDone; /* The following routine searches for the LG-3 and returns the slot it is in */ int FindBoard (void) { int error; SpBlock mySpBlock; SpBlockPtr mySpBlockPtr; mySpBlockPtr = &mySpBlock; mySpBlockPtr->spSlot = 0; mySpBlockPtr->spExtDev = 0; mySpBlockPtr->spTBMask = 0; mySpBlockPtr->spID = SlotResourceID; mySpBlockPtr->spCategory = SlotCategory; mySpBlockPtr->spCType = SlotCType; mySpBlockPtr->spDrvrSW = SlotDrvrSW; mySpBlockPtr->spDrvrHW = SlotDrvrHW; error = SNextTypeSRsrc(mySpBlockPtr); if (error) return 0; return mySpBlockPtr->spSlot; } /* The following routine checks if this is the first time the module is called */ int FirstTime (long *dataPtr) { if (*dataPtr) return 0; else return 1; } /* The following routine initializes the permanent variables */ void InitGlobals (long *dataPtr) { Handle myPrefHandle; myPrefHandle = GetResource ('PREF', PrefResourceID); if (!myPrefHandle) { myGlobals.width = 256; myGlobals.height = 256; myGlobals.top = 155; myGlobals.bottom = 50; myGlobals.source = 2; } else BlockMove (*myPrefHandle, (Ptr) &myGlobals, (long) sizeof (TGlobals)); *dataPtr = (long) NewHandle ((long) sizeof (TGlobals)); } /* The following routine store the global variables in the preferences resource */ void StorePrefs () { Handle myPrefHandle; myPrefHandle = GetResource ('PREF', PrefResourceID); if (myPrefHandle) { BlockMove ((Ptr) &myGlobals, *myPrefHandle, (long) sizeof (TGlobals)); ChangedResource (myPrefHandle); WriteResource (myPrefHandle); } } /* The following routine restores the global variables */ void RestoreGlobals (long *dataPtr) { BlockMove (*((Handle) *dataPtr), (Ptr) &myGlobals, (long) sizeof (TGlobals)); } /* The following routine stores the global variables */ void StoreGlobals (long *dataPtr) { if (*dataPtr) BlockMove ((Ptr) &myGlobals, *((Handle) *dataPtr), (long) sizeof (TGlobals)); } /* The following routine sets the board in preparation for capture */ void ResetBoard (long base, int mode) { unsigned char *regPtr, *lutPtr; int i; regPtr = (unsigned char*) (base + 0xc0000); lutPtr = (unsigned char*) (base + 0x80000); for (i = 0; i < 7; i++) *(regPtr + i * 4) = 0; *(regPtr + 8) = myGlobals.source * 80; *(regPtr + 12) = myGlobals.top; *(regPtr + 16) = myGlobals.bottom; for (i = 0; i < 256; i++) { if (mode) *lutPtr = i; else *lutPtr = 255 - i; lutPtr += 4; } return; } /* The following routine captures a frame */ int GetFrame (long base) { unsigned char *regPtr; long startTime; regPtr = (unsigned char*) (base + 0xc0000); *regPtr = 128; startTime = TickCount (); while (*regPtr < 128) if (TickCount () > startTime + 60) { *regPtr = 0; return 0; } *regPtr = 0; return 1; } /* The following routine captures and copies a frame to the dialog box */ void CopyFrame (DialogPtr myDialogPtr) { int slot, grabType; long base; Rect grabBox, sRect, dRect, tRect; Handle grabHandle; PixMapHandle myPixMapHandle; GDHandle myGDHandle; slot = FindBoard (); if (!slot) return; base = 0xf0000000 + (slot * 0x01100000); ResetBoard (base, 0); GetFrame (base); myPixMapHandle = NewPixMap (); if (!myPixMapHandle) return; (**myPixMapHandle).baseAddr = (Ptr) base; (**myPixMapHandle).rowBytes = 1024 | 0x8000; (**myPixMapHandle).bounds.top = 0; (**myPixMapHandle).bounds.left = 0; (**myPixMapHandle).bounds.bottom = 480; (**myPixMapHandle).bounds.right = 640; (**myPixMapHandle).pmVersion = 4; (**myPixMapHandle).pixelType = 0; (**myPixMapHandle).pixelSize = 8; (**myPixMapHandle).cmpCount = 1; (**myPixMapHandle).cmpSize = 8; (**myPixMapHandle).planeBytes = 0; DisposCTable ((**myPixMapHandle).pmTable); (**myPixMapHandle).pmTable = GetCTable (CTableID); if (!(**myPixMapHandle).pmTable) return; GetDItem (myDialogPtr, GrabID, &grabType, &grabHandle, &grabBox); myGDHandle = GetMainDevice (); sRect.top = 120; sRect.left = 160; sRect.bottom = 360; sRect.right = 480; tRect = (**(**myGDHandle).gdPMap).bounds; dRect.top = tRect.top + (tRect.bottom - tRect.top) / 2 - 140; dRect.left = tRect.left + (tRect.right - tRect.left) / 2 - 210; dRect.bottom = dRect.top + 240; dRect.right = dRect.left + 320; HLock (myPixMapHandle); HLock ((**myGDHandle).gdPMap); CopyBits (*myPixMapHandle, *(**myGDHandle).gdPMap, &sRect, &dRect, srcCopy, 0); HUnlock (myPixMapHandle); HUnlock ((**myGDHandle).gdPMap); DisposPixMap (myPixMapHandle); } /* The following converts a string to a number and checks for validity */ int ConvertString (Str255 s, int *n, int mode) { int j; long x; for (j = 1; j <= *((unsigned char*) s); j++) if (s[j] < '0' || s[j] > '9') return 0; StringToNum (s, &x); if ((mode == 0) && (x < 1 || x > 640)) return 0; if ((mode == 1) && (x < 1 || x > 480)) return 0; *n = x; return 1; } /* The following routine centers a dialog box in the middle of the main screen */ void CenterDialog (DialogTHndl myDialogTHndl) { int width, height; Rect myRect; GDHandle myGDHandle; myGDHandle = GetMainDevice (); width = (**myGDHandle).gdRect.right - (**myGDHandle).gdRect.left; height = (**myGDHandle).gdRect.bottom - (**myGDHandle).gdRect.top; myRect = (**myDialogTHndl).boundsRect; OffsetRect (&myRect, -myRect.left, -myRect.top); OffsetRect (&myRect, (width - myRect.right) / 2, (height - myRect.bottom - 20) / 2 + 20); (**myDialogTHndl).boundsRect = myRect; return; } /* The following routine outlines the OK button in a dialog box */ pascal void OutlineOK (DialogPtr myDialogPtr, int item) { int OKType; Rect OKBox; Handle OKItem; GetDItem (myDialogPtr, OK, &OKType, &OKItem, &OKBox); PenNormal (); PenSize (3,3); InsetRect (&OKBox, -4, -4); FrameRoundRect (&OKBox, 16, 16); PenNormal (); return; } /* The following routine is the event filter for the video dialog box */ pascal Boolean MyEventFilter (DialogPtr myDialogPtr, EventRecord *theEvent, int *item) { int part, topType, bottomType, top, bottom; Point thePoint; Rect topBox, bottomBox; WindowPtr theWindow; ControlHandle theControl, topScroll, bottomScroll; if (myTaskDone) { CopyFrame (myDialogPtr); myTaskDone = 0; PrimeTime (&myTMInfo, 100L); } if ((*theEvent).what != mouseDown) return (0); part = FindWindow ((*theEvent).where, &theWindow); if (theWindow != myDialogPtr) return (0); GetDItem (myDialogPtr, TopID, &topType, &topScroll, &topBox); GetDItem (myDialogPtr, BottomID, &bottomType, &bottomScroll, &bottomBox); top = 255 - myGlobals.top; bottom = 255 - myGlobals.bottom; SetPort(myDialogPtr); thePoint = (*theEvent).where; GlobalToLocal (&thePoint); part = FindControl (thePoint, myDialogPtr, &theControl); if (theControl == topScroll) { part = TrackControl(theControl, thePoint, 0); switch (part) { case inUpButton: top -= 1; break; case inDownButton: top += 1; break; case inPageUp: top -= 10; break; case inPageDown: top += 10; break; case inThumb: top = GetCtlValue (topScroll); break; } if (top < 0) top = 0; if (top > 255) top = 255; if (top > bottom) top = bottom; SetCtlValue (topScroll, top); myGlobals.top = 255 - top; *item = 6; return (1); } if (theControl == bottomScroll) { part = TrackControl(theControl, thePoint, 0); switch (part) { case inUpButton: bottom -= 1; break; case inDownButton: bottom += 1; break; case inPageUp: bottom -= 10; break; case inPageDown: bottom += 10; break; case inThumb: bottom = GetCtlValue (bottomScroll); break; } if (bottom < 0) bottom = 0; if (bottom > 255) bottom = 255; if (bottom < top) bottom = top; SetCtlValue(bottomScroll, bottom); myGlobals.bottom = 255 - bottom; *item = 7; return (1); } return (0); } /* The following routine installs and activates a Time Manager task */ void InstallTMTask () { myTMInfo.myTMTask.tmAddr = (TimerProcPtr) MyTMTask; myTMInfo.myTMTask.tmWakeUp = 0; myTMInfo.myTMTask.tmReserved = 0; asm { move .l a4, myTMInfo.tmRefCon; } InsTime (&myTMInfo); PrimeTime (&myTMInfo, 250L); } /* The following routine removes a Time Manger task */ void RemoveTMTask () { RmvTime (&myTMInfo); } /* The following routine is the Time Manger task */ void MyTMTask () { TMInfoPtr myTMInfoPtr; asm { move.l a1, myTMInfoPtr; move.l a4, -(sp); move.l TMInfo.tmRefCon (a1), a4; } myTaskDone = 1; asm { move.l (sp)+, a4; } }