[comp.sys.mac.programmer] Device Drivers and Think C

tj@kona.cs.ucla.edu (Tom Johnson) (10/24/90)

I hope someone can help me out on this quick!  I'm writing (or at least trying 
to write) a device driver in Think C.  The manual says that using Think C
makes writing device drivers 'easy' since, among other reasons, you can
have global variables.  Well, my globals don't seem to be getting allocated.
If I look at the dCtlStorage field after the drvr is open it is always nil.
Whenever the drvr is opened, it simply SysBeeps and closes the driver,
indicating that dCtlStorage is nil. (See the main routine below).
Is this correct?  Here is the code I'm using:(I swiped the skeleton
from somewhere else and made a few changes.) What am I doing wrong??
If you don't want to wade through all this stuff, you could just
send me a working piece of code :-).

Thanks--

Tom Johnson
tj@cs.ucla.edu
-------Cut Here for code-------

#include <DeviceMgr>

/* Globals */
int 		already_open = 0;	/*  1 -> DA is already open  	*/
DCtlPtr		dce;	/*  device control entry  		*/
short		global1;
long		global2;
/* Implementation */
short
main(
	CntrlParam		*p,
	DCtlPtr			d,
	short			n )
{
	short			result = 0;
	/*  if dCtlStorage is nil then our storage wasn't allocated...	*/
	if (d->dCtlStorage == 0L) {	/*  we must abort	*/
		
		if (n == 0) {		/*  if request is "open"    */
			SysBeep(3);	/*	beep, then close the DRVR */
			CloseDriver(d->dCtlRefNum);
		}
		return(0);			/* 	goodbye */
	}
	dce = d;			/* copy the DCE ptr into our globals */
	d->dCtlFlags &= ~dCtlEnable;		/* we are not re-entrant */
	switch( n ) {
		case OPEN:
			doOpen(d);
			break;
		case PRIME:
			doPrime(d);
			break;
		case CONTROL:
			doControl(p->csCode, p->csParam);
			break;
		case STATUS:
			doStatus();
			break;
		case CLOSE:
			doClose();
			break;
	}
	d->dCtlFlags |= dCtlEnable;						/* enable control calls once more */
	return( result );
}

doOpen( DCtlPtr	d )
{
	OSErr	theErr;
/*  every time ...  */
	dce->dCtlFlags |= dNeedLock|dNeedTime;
	if (already_open) {
		return;
	}
/*  first time only ...  */
/*		do some global initialization */
	already_open = 1;
}

doPrime( DCtlPtr d )
{
}

doControl(code, parm)
short code;
short *parm;
{
	switch( code ) {
		case accRun:
			doIdle();
			break;
		case goodBye:
			doClose();
			break;
	}
}

doStatus( void )
{
}

doIdle( void )
{
	Debugger();
}

doClose( void )
{
}


------End of code----
-- 
Tom Johnson      UCLA Computer Science Department 
			3413 Boelter Hall, Los Angeles CA 90024 (213)825-2145
			Internet:  tj@cs.ucla.edu