appletidle.c

Go to the documentation of this file.
00001 
00002 /*  $Id: appletidle.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /* copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
00005 
00006 
00007 #include <GestaltEqu.h>
00008 #include <applet.h>
00009 #include <appletmsg.h>
00010 #include <appletmain.h>
00011 #include <appletmsg.h>
00012 #include "appletidle.h"
00013 
00014 
00015 
00016 static boolean flinterruptedbyuser = false;
00017 
00018 static long lasteventtickcount = 0;
00019 
00020 
00021 
00022 
00023 static boolean callgestalt (long *ticks) {
00024     
00025     if (Gestalt ('idle', ticks) == noErr) /*the init is installed*/
00026         return (true);
00027         
00028     *ticks = TickCount () - lasteventtickcount;
00029     
00030     return (true);
00031     } /*callgestalt*/
00032     
00033 
00034 boolean getidletime (long *ticks) {
00035     
00036     if (!callgestalt (ticks))
00037         return (false);
00038         
00039     *ticks -= (**app.appwindow).idleatreset;
00040     
00041     return (true);
00042     } /*getidletime*/
00043     
00044 
00045 static boolean resetsystemidlevisit (hdlappwindow appwindow) {
00046     
00047     long idleticks;
00048     
00049     assert (appwindow != nil);
00050     
00051     callgestalt (&idleticks);
00052     
00053     (**appwindow).idleatreset = idleticks;
00054     
00055     (**appwindow).systemidlecomplete = false;
00056     
00057     return (true);
00058     } /*resetsystemidlevisit*/
00059     
00060     
00061 void appresetsystemidle (EventRecord *ev) {
00062     
00063     boolean allwindows = false;
00064     WindowPtr w;
00065     
00066     if ((*ev).what != nullEvent) { /*DW 4/28/95: support init-less installs*/
00067         
00068         /*appsetmessage (app.appwindow, "\pxxxxxx");*/
00069     
00070         lasteventtickcount = TickCount ();
00071         }
00072     
00073     switch ((*ev).what) {
00074         
00075         case keyDown: case autoKey: 
00076             w = FrontWindow ();
00077             
00078             break;
00079             
00080         case mouseDown: case mouseUp: {
00081         
00082             switch (FindWindow ((*ev).where, &w)) {
00083                 
00084                 case inContent: case inDrag: case inGrow:
00085                 case inGoAway: case inZoomIn: case inZoomOut:
00086                     break; /*fall thru*/
00087                     
00088                 default:
00089                     return; /*avoid*/
00090                 } /*switch*/
00091             
00092             break;
00093             }
00094         
00095         case osEvt:
00096             if (((*ev).message & resumeFlag) == 0) /*we're being switched out, not in*/
00097                 return;
00098                 
00099             allwindows = true;
00100             
00101             break;
00102             
00103         case diskEvt:
00104             allwindows = true;
00105             
00106             break;
00107             
00108         default:
00109             return;
00110         } /*switch*/
00111 
00112     flinterruptedbyuser = false;
00113     
00114     if (allwindows)
00115         visitappwindows (&resetsystemidlevisit);
00116     else {
00117         hdlappwindow appwindow;
00118         
00119         if (getappwindow (w, &appwindow))
00120             resetsystemidlevisit (appwindow);
00121         }
00122     } /*appresetsystemidle*/
00123 
00124 
00125 boolean appuseractive (void) {
00126     
00127     /*
00128     call this from your system idle handler, we return true if
00129     the user did something after your handler started. you 
00130     should break out of your loop and return asap.
00131     */
00132     
00133     long idleticks, shouldbe;
00134     EventRecord ev;
00135     
00136     if (flinterruptedbyuser)
00137         return (true);
00138         
00139     if (EventAvail (everyEvent, &ev)) {
00140     
00141         flinterruptedbyuser = true;
00142         
00143         return (true);
00144         }
00145     
00146     if (!getidletime (&idleticks)) /*the system idle traps aren't patched*/
00147         return (false);
00148     
00149     shouldbe = TickCount () - (**app.appwindow).lastsystemidle;
00150     
00151     if (idleticks < shouldbe) { 
00152         
00153         flinterruptedbyuser = true;
00154         
00155         return (true);
00156         }
00157     
00158     return (false);
00159     } /*appuseractive*/
00160     
00161     
00162 static boolean systemidlevisit (hdlappwindow appwindow) {
00163     
00164     /*
00165     DW 8/26/93: I couldn't understand why CB's idle routine would start
00166     up while I was holding down the right arrow key, just after doing
00167     the first movement, and before doing the second. then I figured it
00168     out -- the system waits "KeyThresh" ticks before sending the second
00169     keystroke. just long enough if systemidleticks is set to 20, to sneak
00170     another call to the idle routine. made a jerky start even jerkier.
00171     changed cb's systemidleticks value to 0, and added a check here that
00172     effectively ignores systemidleticks values that are less than
00173     KeyThresh.
00174     */
00175     
00176     hdlappwindow ha = appwindow;
00177     long idleticks;
00178     EventRecord ev;
00179     
00180     if ((**ha).systemidlecallback == nil) /*no callback installed*/
00181         return (true);
00182         
00183     if (EventAvail (everyEvent, &ev)) /*we've got more important things to do*/
00184         return (true);
00185     
00186     if (!getidletime (&idleticks)) /*the system idle traps aren't patched*/
00187         return (true);
00188         
00189     if (idleticks <= LMGetKeyThresh ()) /*see comment at head of routine*/
00190         return (true);
00191         
00192     if (idleticks < (**ha).systemidleticks) /*haven't idled long enough*/
00193         return (true);
00194         
00195     if ((**ha).systemidlecomplete) /*the idle task completed since the user did anything*/
00196         return (true);
00197     
00198     (**app.appwindow).lastsystemidle = TickCount () - idleticks;
00199     
00200     flinterruptedbyuser = false;
00201     
00202     setappwindow (ha);
00203 
00204     appprecallback ();
00205 
00206     (*(**ha).systemidlecallback) ((**ha).systemidlerefcon);
00207 
00208     (*app.preupdatecallback) ();
00209 
00210     apppostcallback (); 
00211             
00212     return (true); /*keep traversing*/
00213     } /*systemidlevisit*/
00214     
00215     
00216 void appsystemidle (void) {
00217 
00218     visitappwindows (&systemidlevisit);
00219     } /*appsystemidle*/
00220     
00221     

Generated on Wed May 31 18:19:42 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6