Idle Call

From FM Plugin Wikipedia
Jump to: navigation, search

You can have FileMaker call your plugin during the standard system idle call. During this time, you can execute call backs to FileMaker, including Script and/or SQL callbacks.

NOTE : Be careful that your idle code doesn't take too much time - it's called frequently and any time it takes reduces the overall performance of FileMaker.


Step One - Enable Idle Time Flag

Set the 'Want idle time' flag in the plugin Option String to be 'Y' (the third to last flag)


#define IDMA_Plugin_OptionString		"IDMA1nnYYnn"
//						 ^--^ || | |---- // No Win32s support
//						 |    || |------ // Want idle time 
//						 |    ||-------- // No old external functions
//						 |    |--------- // Has preferences dialog
//						 |-------------- // 'IDMA' plug-in ID



Step Two - FMExternCallProc

This is the key function that FileMaker calls to initialise, shutdown, get name or perform during idle time.

It shouldn't be necessary to edit this function.

void FMX_ENTRYPT FMExternCallProc(FMX_ExternCallPtr pb)
{
	// Setup global defined in FMxExtern.h
	gFMX_ExternCallPtr = pb;

	// Message dispatcher
	switch (gFMX_ExternCallPtr->whichCall) 
	{
		case kFMXT_Init:
		{
			//	Msg	=	Parm1				Parm2				Parm3
			//	kFMXT_Init	FMX_Application value		App vers unicode c str* 	[unused]
			gFMX_ExternCallPtr->result = Do_PluginInit(gFMX_ExternCallPtr->extnVersion, gFMX_ExternCallPtr->parm1 );
		}
		break;
			
		case kFMXT_Idle:
		{
			Do_PluginIdle(gFMX_ExternCallPtr->parm1);		// This calls our 'idle' function, passing the current 'idleLevel'
		}
		break;
			
		case kFMXT_Shutdown:
		{
			Do_PluginShutdown();
		}
		break;
			
		case kFMXT_DoAppPreferences:
		{
			Do_PluginPrefs();
		}
		break;

		case kFMXT_GetString:
		{
			Do_GetString(gFMX_ExternCallPtr->parm1, gFMX_ExternCallPtr->parm2, gFMX_ExternCallPtr->parm3, reinterpret_cast<FMX_Unichar*>(gFMX_ExternCallPtr->result));
		}
		break;

	}// switch whichCall

} // FMExternCallProc



Step Three - Do_PluginIdle function

In your main file, you will find the 'Do_PluginIdle' function.


static void Do_PluginIdle(FMX_IdleLevel idleLevel) 
{
	if (pluginIsRunningOnFMPro)		// Only execute if running on client or runtime  (ie. don't call when on server)
	{					// BOOL pluginIsRunningOnFMPro  set during plugin init
		// Check idle state
		switch (idleLevel)
		{
			case kFMXT_UserIdle:
			{
				// The user hasn't done anything for 30 seconds or more.  (mouse movement don't reset this)
			}
			break;
				
 			case kFMXT_UserNotIdle:
			{
				// The user has done something within the last 30 seconds
			}
			break;

			case kFMXT_ScriptPaused:
			{
				// The user is running a script that is paused
			}
			break;

			case kFMXT_ScriptRunning:
			{
				// The user is running a script
			}
			break;
				
			case kFMXT_Unsafe:
			{
				// The plug-in should wait and do nothing at this time.
			}
			break;
		}// switch idleLevel

	}
} // Do_PluginIdle



Example : Execute queued SQL

The following example shows how to first get the current environment and then call a function 'sqlProcessQueue'. This is done during 'safe' idle calls (UserIdle, NotIdle, ScriptPaused and ScriptRunning)

Depending on what you are wanting to do, you may or may not want to include one or more of the 'idleLevel's


void pluginIdle(FMX_IdleLevel idleLevel)
{
   fmx::errcode fmError;
   fmx::ExprEnvAutoPtr currEnv;
   
   switch (idleLevel)
   {
       case kFMXT_UserIdle       :		// The user hasn't done anything for 30 seconds or more.
       case kFMXT_UserNotIdle    :		// The user has done something within 30 seconds.
       case kFMXT_ScriptPaused   :		// The user is running a script that is paused.
       case kFMXT_ScriptRunning  :		// The user is running a script.
           fmError = FMX_SetToCurrentEnv(&(*currEnv));
           if (!fmError)
               sqlProcessQueue(*currEnv);
           break;
           
       case kFMXT_Unsafe         : // Same as if unsafeCalls is set to true.
           // do nothing
           break; // case kFMXT_Unsafe :

   } // switch (idleLevel)
}