home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************/
- /* SMPTE Monitor */
- /*------------------------------------------------------------------------------*/
- /* */
- /*------------------------------------------------------------------------------*/
- /* © GRAME 1992, Yann Orlarey */
- /********************************************************************************/
-
- #include "MIDIShare.h"
-
- #include <Values.h>
- #include <Types.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <ToolUtils.h>
- #include <Memory.h>
- #include <SegLoad.h>
- #include <Files.h>
- #include <OSUtils.h>
- #include <OSEvents.h>
- #include <DiskInit.h>
- #include <Packages.h>
- #include <Traps.h>
-
- /* Quelques constantes Macintosh */
-
- #define nil 0
- #define osEvent app4Evt
- #define resumeMask 1
- #define suspendResumeMessage 1
- #define mouseMovedMessage 0xFA
- #define _Unimplemented 0xA89F /* Unimplemented trap */
- #define _WaitNextEvent 0xA860 /* WaitNextEvent trap */
-
-
- /* Une barre de menus minimale */
-
- #define mBarSize 3
- #define AppleMenu 0
- #define FileMenu 1
- #define EditMenu 2
-
- #define AppleID 128
- #define FileID 129
- #define EditID 130
-
-
- /* Les items du menu Edit */
-
- #define UndoI 1
- #define CutI 3
- #define CopyI 4
- #define PasteI 5
- #define ClearI 6
-
-
- /* La fenêtre de l'application qui est en fait un dialog */
-
- #define dialogID 129
-
-
- /* Pour les erreurs et le About */
-
- #define AlertID 500
- #define AboutID 128 /* Dialog définit dans les ressources de MidiShareI */
- #define AboutPictID 130
-
-
- /* les contrôleurs du dialog */
-
- #define MidiShareTime 18
- #define SmpteTime 19
- #define SyncLocked 20
-
-
- /* Variables globales d'une application Macintosh */
-
- WindowPtr myWindow; /* fenêtre */
-
- Boolean doneFlag; /* flag d'arrêt */
-
- SysEnvRec gMac; /* machine… */
- Boolean hasWNE; /* vrai si WaitNextEvent implémenté */
- Boolean foreGround; /* vrai si en foreGround */
-
- EventRecord myEvent; /* pour la main event loop */
- char theChar; /* pour la gestion des touches */
- Rect dragRect; /* rect de déplacement de fenêtre */
- MenuHandle myMenus[mBarSize]; /* les menus */
-
-
- /* Constantes pour paramétrer l'application */
-
- #define ApplName "\pSmpte Monitor"
-
-
- /* Variables globales pour MidiShare */
-
- short myRefNum; /* numéro d'appl. Midi */
- TSyncInfo gInfo;
-
- ControlHandle msTimeHandler;
- ControlHandle smpteTimeHandler;
- ControlHandle syncLockedHandler;
-
-
- pascal void DisplayState( long , short , long, long, long);
- void DrawSmpteInfo(WindowPtr wind);
-
-
- /********************************************************************************/
- /* Standard Macintosh Routines */
- /* */
- /********************************************************************************/
-
-
- /*================================== Utilities =================================*/
-
-
- void AlertUser( char *msg )
- {
- ParamText( ApplName, msg, "\p", "\p");
- Alert(AlertID,nil);
- ExitToShell();
- }
-
-
- Boolean TrapAvailable(short trapNum, short tType)
- {
- if ( (tType == ToolTrap) && /* Toolbox trap */
- (gMac.machineType > envMachUnknown) && /* …512KE, Plus, or */
- (gMac.machineType < envMacII) ) { /* …SE */
-
- trapNum &= 0x3FF; /* Traps numbers are 10 bits long */
-
- if (trapNum > 0x1FF) { /* Traps only go up to 0x1FF on */
- return(false); /* these machines */
- }
- }
- return NGetTrapAddress(trapNum, tType) != GetTrapAddress(_Unimplemented);
- }
-
-
- Boolean IsAppWindow (WindowPtr wind)
- {
- return wind ? ((WindowPeek)wind)->windowKind >= 0 : false ;
- }
-
-
- Boolean IsDAWindow (WindowPtr wind)
- {
- return wind ? ((WindowPeek)wind)->windowKind < 0 : false ;
- }
-
-
- void CloseWind (WindowPtr wind)
- {
- if ( IsDAWindow(wind) )
- CloseDeskAcc( ((WindowPeek)wind)->windowKind );
- else if ( IsAppWindow(wind) )
- DisposeWindow(wind);
- }
-
- void CloseAllWinds (WindowPtr wind)
- {
- if (wind) {
- CloseAllWinds( (WindowPtr) (((WindowPeek)wind)->nextWindow) );
- CloseWind(wind);
- }
- }
-
-
-
- /*================================== pascal strings =================================*/
-
-
- void InsertCh( Str255 s, short p, char c) /* Insert un caractère dans une chaine Pascal */
- { /* à partir de la position p (de 1 à n). */
- register short n;
- register char t;
-
- if (p >= s[0])
- s[s[0]] = c;
- else
- for (n= s[0]-p; n>=0; n--, p++) { t=c; c=s[p]; s[p]=t; }
- s[0]++;
- }
-
- void ShiftPStr( Str255 s, short n ) /* Décale une chaine Pascal de n caractères */
- { /* vers la droite. */
- register char *ps;
- register char *pd;
-
- ps = (char*)s + s[0];
- pd = ps + n;
- s[0] += n;
- for (; n>0; n--) *pd-- = *ps-- ;
- }
-
- void TransfPStr(Str255 s1, char* s2, short n) /* Ecrase le début de la chaine s1 avec s2 */
- {
- for (; n>0; n--) *++s1 = *++s2 ;
- }
-
- void AddFirst( Str255 s1, char * s2) /* Insere en début de la chaine s1 la chaine s2 */
- {
- ShiftPStr( s1, s2[0] );
- TransfPStr( s1, s2, s2[0] );
- }
-
-
-
- /*================================== Dialog Items =================================*/
-
-
- void WindSetItemText (WindowPtr wind, short theItem, Str255 str)
- {
- Rect theRect;
- short theType;
- Handle theItemH;
-
- GetDItem((DialogPtr)wind, theItem, &theType, &theItemH, &theRect);
- SetIText(theItemH, str);
- }
-
- void WindHiliteItem (WindowPtr wind, short theItem, short value)
- {
- Rect theRect;
- short theType;
- Handle theItemH;
-
- GetDItem((DialogPtr)wind, theItem, &theType, &theItemH, &theRect);
- HiliteControl((ControlHandle)theItemH, value);
- }
-
- void WindGetItemText (WindowPtr wind, short theItem, Str255 str)
- {
- Rect theRect;
- short theType;
- Handle theItemH;
-
- GetDItem((DialogPtr)wind, theItem, &theType, &theItemH, &theRect);
- GetIText(theItemH, str);
- }
-
- void WindSetItemNum (WindowPtr wind, short theItem, long num)
- {
- Str255 str;
-
- NumToString(num, str);
- WindSetItemText(wind, theItem, str);
- }
-
- void WindGetItemNum (WindowPtr wind, short theItem, long* num)
- {
- Str255 str;
-
- WindGetItemText(wind, theItem, str);
- StringToNum(str, num);
- }
-
- void TrackDialog(WindowPtr w, Point p)
- {
- short itemHit= 0;
-
- SetPort(w);
- GlobalToLocal(&p); /* conversion du point en local */
- if( DialogSelect( &myEvent, &w, &itemHit))
- {
- switch( itemHit)
- {
- case MidiShareTime:
- MidiSetSyncMode(0);
- DisplayState( nil, nil, nil, nil, nil);
- break;
- case SmpteTime:
- MidiSetSyncMode(MIDISyncExternal | MIDISyncAnyPort);
- DisplayState( nil, nil, nil, nil, nil);
- break;
- }
- }
- }
-
-
-
- /*================================== About Dialog =================================*/
-
-
- //_______________________________________________________________________________________
- void UpdateWindow( WindowPtr w)
- {
- if( IsAppWindow( w))
- {
- BeginUpdate( w);
- if ( !EmptyRgn( w->visRgn ))
- {
- SetPort( w);
- DrawDialog( w);
- }
- EndUpdate( w);
- }
- }
-
- void ShowAbout()
- {
- DialogPtr myDialog;
- Boolean done= false;
-
- myDialog = GetNewDialog(AboutID,nil,(WindowPtr) -1);
- if (myDialog) {
- ShowWindow( (WindowPtr)myWindow);
- while (!done) {
- if (hasWNE)
- WaitNextEvent(everyEvent, &myEvent, 0, nil);
- else {
- SystemTask();
- GetNextEvent(everyEvent, &myEvent);
- }
- switch( myEvent.what ) {
- case mouseDown:
- done= true;
- break;
- case osEvent:
- if( (myEvent.message >> 24) == suspendResumeMessage)
- if( !(foreGround = myEvent.message & resumeMask))
- done= true;
- break;
- case updateEvt:
- UpdateWindow( (WindowPtr)myEvent.message);
- break;
- }
- }
- DisposDialog( myDialog);
- }
- else SysBeep(0);
- }
-
-
-
- /*================================== Mac events =================================*/
-
-
- void AdjustMenus()
- {
- if ( IsAppWindow( FrontWindow() ) )
- {
- DisableItem( myMenus[EditMenu], UndoI );
- DisableItem( myMenus[EditMenu], CutI );
- DisableItem( myMenus[EditMenu], CopyI );
- DisableItem( myMenus[EditMenu], PasteI );
- DisableItem( myMenus[EditMenu], ClearI );
- }
- else if ( IsDAWindow( FrontWindow() ) )
- {
- EnableItem( myMenus[EditMenu], UndoI );
- EnableItem( myMenus[EditMenu], CutI );
- EnableItem( myMenus[EditMenu], CopyI );
- EnableItem( myMenus[EditMenu], PasteI );
- EnableItem( myMenus[EditMenu], ClearI );
- }
- }
-
-
- void AdjustCursor()
- {
- if (foreGround && IsAppWindow(FrontWindow()))
- SetCursor(&qd.arrow);
- }
-
-
- void DoCommand(long mResult)
- {
- short theItem;
- short theMenu;
- Str255 name;
-
- theItem = LoWord(mResult);
- theMenu = HiWord(mResult);
-
- switch ( theMenu )
- {
- case AppleID:
- if (theItem != 1)
- {
- GetItem(myMenus[AppleMenu], theItem, name);
- theItem = OpenDeskAcc(name);
- } else {
- ShowAbout();
- }
- break;
- case EditID:
- SystemEdit( theItem-1 );
- break;
- case FileID:
- doneFlag = true;
- break;
- }
- HiliteMenu(0);
- }
-
-
- void DoMouseDown()
- {
- WindowPtr whichWindow;
-
- switch ( FindWindow( myEvent.where, &whichWindow ) )
- {
- case inMenuBar:
- AdjustMenus();
- DoCommand(MenuSelect(myEvent.where));
- break;
- case inSysWindow:
- SystemClick(&myEvent, whichWindow);
- break;
- case inGoAway:
- doneFlag = TrackGoAway(whichWindow, myEvent.where);
- break;
- case inDrag:
- DragWindow(whichWindow, myEvent.where, &dragRect);
- break;
- case inContent:
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else if (whichWindow == myWindow)
- TrackDialog(myWindow, myEvent.where);
- break;
- }
- }
-
-
-
- /*================================== Initialize =================================*/
-
- pascal void displayLock( long , short , long, long, long)
- {
- if( gInfo.syncLocked) SetIText( (Handle)syncLockedHandler, "\pSync. locked");
- else SetIText( (Handle)syncLockedHandler, "\p");
- }
-
- pascal void displayTime( long , short , long, long, long)
- {
- short midi= 1, smpte= 0;
-
- if( gInfo.syncMode & MIDISyncExternal)
- {
- midi = 0;
- smpte= 1;
- }
- SetCtlValue( msTimeHandler, midi);
- SetCtlValue( smpteTimeHandler, smpte);
- }
-
- pascal void DisplayState( long , short , long, long, long)
- {
- MidiGetSyncInfo(&gInfo);
- displayLock( nil, nil, nil, nil, nil);
- displayTime( nil, nil, nil, nil, nil);
- }
-
-
- void SetUpWindows()
- {
- short type;
- Rect box;
-
- SetRect (&dragRect, qd.screenBits.bounds.left, qd.screenBits.bounds.top,
- qd.screenBits.bounds.right,qd.screenBits.bounds.bottom);
- InsetRect (&dragRect, 4, 4);
-
- myWindow = GetNewDialog (dialogID, nil, (WindowPtr) -1);
- if( myWindow)
- {
- GetDItem( myWindow, MidiShareTime, &type, (Handle *)&msTimeHandler, &box);
- GetDItem( myWindow, SmpteTime, &type, (Handle *)&smpteTimeHandler, &box);
- GetDItem( myWindow, SyncLocked, &type, (Handle *)&syncLockedHandler, &box);
- SetPort(myWindow);
- TextFont(systemFont); TextSize(12); TextMode(srcCopy); PenNormal();
- ShowWindow(myWindow);
- DisplayState( nil, nil, nil, nil, nil);
- }
- else AlertUser( "\pcan't find DLOG resource !");
- }
-
-
- void SetUpMenus()
- {
- short i;
-
- myMenus[AppleMenu] = GetMenu(AppleID);
- AddResMenu(myMenus[AppleMenu],'DRVR');
- myMenus[FileMenu] = GetMenu(FileID);
- myMenus[EditMenu] = GetMenu(EditID);
- for (i = AppleMenu; i <= EditMenu; i++)
- InsertMenu(myMenus[i], 0);
- DrawMenuBar();
- }
-
-
- void Initialize()
- {
- OSErr err;
-
- err = SysEnvirons(1, &gMac);
- if (gMac.machineType < 0) AlertUser( "\pneed at least 128K ROMs" );
- hasWNE = TrapAvailable(_WaitNextEvent, ToolTrap);
- foreGround = true;
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitDialogs(nil);
- InitMenus();
- TEInit();
- InitCursor();
-
- FlushEvents(everyEvent, 0);
-
- SetUpMenus();
- SetUpWindows();
- }
-
-
-
- /********************************************************************************/
- /* Do Idle */
- /********************************************************************************/
- void DoIdle()
- {
- register long n;
- register short r;
-
- DrawSmpteInfo( myWindow);
- r = myRefNum;
- for (n=MidiCountDTasks(r); n>0; --n) MidiExec1DTask(r);
- }
-
-
- /********************************************************************************/
- /* MidiShare specific */
- /* */
- /********************************************************************************/
-
-
- /*=================================== alarms ===================================*/
-
-
- pascal void ApplAlarm( short r, long code)
- {
- MidiGetSyncInfo(&gInfo);
- if( code == MIDISyncStart)
- MidiDTask( displayLock, MidiGetTime(), r, 0L,0L,0L);
- else if( code == MIDISyncStop )
- MidiDTask( displayLock, MidiGetTime(), r, 0L,0L,0L);
- else if( code == MIDIChangeSync)
- MidiDTask( DisplayState, MidiGetTime(), r, 0L,0L,0L);
- }
-
-
- pascal void IgnoreEvents (register short r)
- {
- // our receive alarm ignore all incoming events
-
- MidiFlushEvs(r);
- }
-
-
- /*=================================== Initialize ===================================*/
-
-
- void SetUpMidi()
- {
- // first check for MidiShare installed
- if (!MidiShare()) { AlertUser( "\pMidiShare not implemented" ); }
-
- // then start the MidiShare session and check for no errors
- myRefNum= MidiOpen(ApplName);
- if (myRefNum < 0) { AlertUser( "\pError opening the MidiShare session"); }
-
- // install our alarms
- MidiSetRcvAlarm(myRefNum, IgnoreEvents);
- MidiSetApplAlarm(myRefNum, ApplAlarm);
-
- // get the current synchronisation informations
- MidiGetSyncInfo(&gInfo);
- }
-
-
- /*=================================== smpte monitor ===================================*/
-
-
- long oldpo = -2;
- long oldbr = -1;
- long oldfo = -1;
- long oldsub = 0;
- long oldfr = 0;
- long oldss = 0;
- long oldmn = 0;
- long oldhh = 0;
- char* formatNames[] = {"\p24", "\p25", "\p30df", "\p30"};
-
- void DrawSmpteInfo(WindowPtr wind)
- {
- TSmpteLocation loc;
-
- if (gInfo.syncBreaks != oldbr) {oldbr = gInfo.syncBreaks; WindSetItemNum(wind, 8, gInfo.syncBreaks);}
- if (gInfo.syncLocked) {
- if (gInfo.syncPort != oldpo) {oldpo = gInfo.syncPort; WindSetItemNum(wind, 7, gInfo.syncPort);}
-
- MidiTime2Smpte(MidiGetExtTime(), gInfo.syncFormat, &loc);
-
- if (loc.hours != oldhh) {oldhh = loc.hours; WindSetItemNum(wind, 1, loc.hours);}
- if (loc.minutes != oldmn) {oldmn = loc.minutes; WindSetItemNum(wind, 2, loc.minutes);}
- if (loc.seconds != oldss) {oldss = loc.seconds; WindSetItemNum(wind, 3, loc.seconds);}
- if (loc.frames != oldfr) {oldfr = loc.frames; WindSetItemNum(wind, 4, loc.frames);}
- if (loc.fracs != oldsub) {oldsub = loc.fracs; WindSetItemNum(wind, 5, loc.fracs);}
- if (loc.format != oldfo) {oldfo = loc.format; WindSetItemText(wind, 6, formatNames[loc.format]);}
- }
- else if (-1 != oldpo){oldpo = -1; WindSetItemText(wind, 7, "\p--");}
- }
-
- /********************************************************************************/
- /* Main */
- /* */
- /********************************************************************************/
-
- void main()
- {
- Boolean b;
-
- MaxApplZone();
- Initialize();
- SetUpMidi();
-
- doneFlag = false;
- while (!doneFlag) { /* Main Loop */
- if (hasWNE)
- b = WaitNextEvent(everyEvent, &myEvent, 0, nil); /* no sleep, no mouseRgn */
- else {
- SystemTask();
- b = GetNextEvent(everyEvent, &myEvent);
- }
- AdjustCursor();
-
- switch( myEvent.what ) {
- case nullEvent:
- DoIdle();
- break;
- case osEvent:
- switch (myEvent.message >> 24) {
- case suspendResumeMessage:
- foreGround = myEvent.message & resumeMask;
- break;
- case mouseMovedMessage:
- DoIdle();
- break;
- }
- break;
- case keyDown:
- case autoKey:
- theChar = myEvent.message & charCodeMask;
- if (myEvent.modifiers & cmdKey ) {
- AdjustMenus();
- DoCommand(MenuKey(theChar));
- }
- break;
- case mouseDown:
- DoMouseDown();
- break;
- case updateEvt:
- if ( IsAppWindow( (WindowPtr)myEvent.message ) )
- UpdateWindow( (WindowPtr)myEvent.message);
- break;
- }
- }
- MidiClose(myRefNum);
- CloseAllWinds( FrontWindow() );
- }
-
-
-
-
-
-
-
-
-