From: justin@tasman.cc.utas.edu.au (Justin Ridge)
Subject: Time Manager & INIT questions (4)
Keywords: time manager, INIT, startup, icon, think C
Organization: University of Tasmania, Australia.
Date: Fri, 1 Jan 1993 04:35:18 GMT

G'day to all comp.sys.mac.programmers!

A little while back, I started programming on the Mac, and soon got the
hang of resource management, dialogs, and other aspects of simple
applications.  Over the Christmas break, I decided to branch out and look
at things like system extensions (INITs), and soon struck some problems,
which are detailed below.

I apologise for the verbosity of these problem descriptions, however if you
bear with me I think the answers are in fact rather simple, and have most
certainly been dealt with in the past by others.
I'd appreciate it if you can help with even one of the topics!!!
Please send responses directly to me (address below).

Problems: 1. Icons at startup
          2. Delayed execution of an INIT
          3. Delayed execution of an application
          4. Notifying an INIT of events

1. ICONS AT STARTUP
-------------------
How do I get an INIT to display its icon at startup time when it executes?
Do I use routines like PlotIcon()?  If so, how do I know where to draw it
along the bottom of the screen?
Couldn't find much about this in IM so I am really lost, but I imagine it
would only be half a dozen lines of code.

2. DELAYED EXECUTION OF AN INIT
-------------------------------
Say I wish to have an INIT execute not at startup time, but 1 minute after
startup.
How is this accomplished?  I understand I must use the Time Manager...
consider the following routine:
~~~~~
pascal void delayedRoutine(void);

main() 
{       
  TMTask       theDel;
  long         myDelay = 60000;
  TimerProcPtr temp;

  InitGraf(&thePort);
  FlushEvents(everyEvent, 0);
  InitCursor();
  MaxApplZone();

  temp = delayedRoutine;
  theDel.tmAddr = temp;
  theDel.tmWakeUp = 0;
  theDel.tmReserved = 0;
  InsXTime(&theDel);
  PrimeTime(&theDel, myDelay);
}

pascal void delayedRoutine(void)
{
  /* do something */
}
~~~~~
This is written in THINK C 5 under System 7, so I have had to declare the
routine delayedRoutine() as a pascal procedure, as described on pp.213-214
of the THINK C User Manual.  Yet this doesn't work!  Why?
I also understand that the routine must be in memory when execution time
comes round, but doesn't an INIT remain in memory indefinately after it is
loaded? (If not, how do I modify the above code to get it to do so?)

3. DELAYED EXECUTION OF AN APPLICATION
--------------------------------------
Say the routine in (2) was changed from an INIT into an application, which
merely starts up, schedules delayedRoutine() for sometime in the future,
then quits.  Then delayedRoutine() would be removed from memory, and
couldn't be found at  execution time.  To avoid this, I build
delayedRoutine() into a code resource which is loaded into the system heap,
as suggested in IM VI 23-10:

delayedRoutineHandle = GetResource('CODE', 0);

OK, so I have loaded the routine, and I have a handle to it.
Now how can I execute that routine?
How can I get something of type TimerProcPtr and have the Time Manager call
it after an arbitrary delay? i.e. what are the steps between GetResource
and InsXTime?

4. NOTIFYING AN INIT OF EVENTS
------------------------------
This is the last problem (phew) and a fairly general one.  I have no
problems writing an INIT which loads at startup time, performs a task and
then quits.  My next job will be to write an INIT which performs ongoing
tasks after startup.
Say I want to write an INIT similar to CapsLock, which detects when a
certain key is pressed, then draws a symbol.
How do I get events such as mouseDown, key events etc. always to be sent to
my INIT  after startup, so that I can examine them and take the desired
action?

CONCLUSION
----------
If anyone can give me any vague pointers as to how to go about these tasks,
I would be most grateful.  If you can actually suggest some replacement
code to correct any of my mistakes then that is a bonus!
Also, if you have written routines yourself which do similar tasks, please be
encouraged to send extracts as I will probably learn a lot more from actual
examples than anything else!

Finally, please send any responses DIRECTLY TO ME, as this newsgroup is
likely to scroll before I get a chance to read it again.

Thanks a lot for any tips,
Regards,
Justin Ridge.

justin@tasman.cc.utas.edu.au

--------------------------------------------------------------------------
Justin Ridge,             |    Phone:  +61 02 202811
Computing Centre          |    Fax  :  +61 02 231772
University of Tasmania    |    Email:  Justin.Ridge@cc.utas.edu.au
--------------------------------------------------------------------------

Xref: sparky comp.sys.mac.programmer:20532 comp.os.ms-windows.programmer.tools:1847