An alarm is set in an active application under development.  The alarm
functions as intended while the application remains active.  However, if
the app is not active the alarm does not function.  No alarm triggered
launch command gets to the application.  It is as if by quiting the app
the alarm has been turned off.

Attached is a code fragment from the pilotmain, appstart, appstop &
alarmtriggered routines.

Thanks

Greg McGraw
/***********************************************************************/
static DWord XXXPilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
{
        Err                                     error;


        error = RomVersionCompatible (ourMinVersion, launchFlags);
        if (error) return (error);


        switch (cmd)
                {
                case sysAppLaunchCmdNormalLaunch:
                        error = AppStart();
                        if (error) 
                                return error;                   
                        FrmGotoForm(MainForm);
                        AppEventLoop();
                        AppStop();
                        break;

                case sysAppLaunchCmdAlarmTriggered:
                        AlarmTriggered((SysAlarmTriggeredParamType *)cmdPBP, 
launchFlags);
                        break;

                default:
                        break;

                }
        
        return 0;
}


/***********************************************************************/
static Err AppStart(void)
{
    Err                                                         error;
        UInt                                                    cardNo;
        LocalID                                                 dbID;
        UInt                                                    dbAttrs;
        UInt                                                    mode;   
        VoidHand                                                h;
        Ptr                                                             p;
        UInt                                                    index;
    Boolean                                                     recordFound = false;
    LocalID                                                     appInfoID;
    UInt                                                        version;
    Word                                                        prefsSize;
        XXXAppInfoPtr                                   appInfoP;

        // Open application's database
        mode = dmModeReadWrite | dmModeShowSecret;
        XXXDB = DmOpenDatabaseByTypeCreator(XXXDBType, appFileCreator, mode);
        if (!XXXDB)
                {
                // No database, create one
                error = DmCreateDatabase(0, XXXDBName, appFileCreator, XXXDBType, 
false);
                ErrFatalDisplayIf(error, "AppStart - Can't create database");
                if (error)
                        return true;
                XXXDB = DmOpenDatabaseByTypeCreator(XXXDBType, appFileCreator, mode);
                error = DmOpenDatabaseInfo(XXXDB, &dbID, NULL, NULL, &cardNo, NULL);
                ErrFatalDisplayIf(error, "AppStart - Can't get database info");
                error = DmDatabaseInfo(0, dbID, NULL, &dbAttrs, 
                        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
                ErrFatalDisplayIf(error, "AppStart - Can't get database info");
                dbAttrs = dbAttrs | dmHdrAttrBackup;
                error = DmSetDatabaseInfo(0, dbID, NULL, NULL, &version,
                        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
                ErrFatalDisplayIf(error, "AppStart - Can't set database info");

                // Create a new first record in the database and copy in default city
                h = DmNewRecord(XXXDB, &index, sizeof(XXXCityType));
                p = MemHandleLock(h);
                error = DmWrite (p, 0, &DefaultCity, sizeof(XXXCityType));
                ErrFatalDisplayIf(error, "AppStart - Could not write new record.");
                MemPtrUnlock(p);
                DmReleaseRecord(XXXDB, index, true);

                // Remember the index of the current record.
                CurrentRecord = index;
                
                if (AppInfoInit(XXXDB))
                        return true;

                }
        else
                {
                error = DmOpenDatabaseInfo(XXXDB, &dbID, NULL, NULL, &cardNo, NULL);
                ErrFatalDisplayIf(error, "AppStart - Can't get database info");
                error = DmDatabaseInfo(0, dbID, NULL, &dbAttrs, NULL, NULL, NULL, 
NULL, 
                                                                                NULL, 
&appInfoID, NULL, NULL, NULL);
                ErrFatalDisplayIf(error, "AppStart - Can't get database info");
                if (appInfoID == NULL)
                        if (AppInfoInit(XXXDB))
                                return true;
                }
                
        error = DmDatabaseInfo(0, dbID, NULL, &dbAttrs, NULL, NULL, NULL, NULL, 
                                                                        NULL, 
&appInfoID, NULL, NULL, NULL);
        ErrFatalDisplayIf(error, "AppStart - Can't get database info");
        
        PrefGetPreferences(&sysPrefs);

        // Read the saved preferences
        prefsSize = sizeof(XXXPreferenceType);
        if (PrefGetAppPreferences(appFileCreator, appPrefID, &prefs, &prefsSize, true) 
== 
                appVersionNum)
                {
                        TopVisibleRecord = prefs.topVisibleRecord;
                        CurrentRecord = prefs.currentRecord;
                        CurrentRecordID = prefs.currentRecordID;
                }
        
        // Initialize current app info  
        appInfoP = MemLocalIDToLockedPtr(appInfoID, cardNo);
        aiP = &CurrentAppInfo;
        MemMove(aiP, appInfoP, sizeof(XXXAppInfoType));
        MemPtrUnlock(appInfoP);
        
        // Load the far call jump table.
        FarCalls.ListRecords = ListRecords;
        FarCalls.FindRecord = FindRecord;
                
#ifdef EMUL8
        // Open the MathLib             
        error = SysLibFind(MathLibName, &MathLibRef);
        if (error)
                error = SysLibLoad(LibType, MathLibCreator, &MathLibRef);
        ErrFatalDisplayIf(error, "AppStart - Can't find MathLib");
        error = MathLibOpen(MathLibRef, MathLibVersion);
        ErrFatalDisplayIf(error, "AppStart - Can't open MathLib");
#endif

   return 0;
}


/***********************************************************************/
static void AppStop(void)
{
    XXXPreferenceType                           prefs;
    LocalID                                                     appInfoID;
        LocalID                                                 dbID;
        UInt                                                    dbAttrs;
        UInt                                                    cardNo;
    UInt                                                        usecount;
    Err                                                         error;
        XXXAppInfoPtr                                   appInfoP;
   
    // Close all forms
    FrmCloseAllForms();
   
        // Save current app info
        error = DmOpenDatabaseInfo(XXXDB, &dbID, NULL, NULL, &cardNo, NULL);
        ErrFatalDisplayIf(error, "AppStop - Can't get open database info");
        error = DmDatabaseInfo(0, dbID, NULL, &dbAttrs, NULL, NULL, NULL, NULL, 
                                                                        NULL, 
&appInfoID, NULL, NULL, NULL);
        ErrFatalDisplayIf(error, "AppStop - Can't get database info");
        appInfoP = MemLocalIDToLockedPtr(appInfoID, cardNo);
        DmWrite (appInfoP, 0, &CurrentAppInfo, sizeof(XXXAppInfoType));
        MemPtrUnlock(appInfoP);

        // Copy state information to prefs
        prefs.topVisibleRecord = TopVisibleRecord;
        prefs.currentRecord = CurrentRecord;
        if (DmQueryRecord (XXXDB, CurrentRecord) != 0)
                {
                DmRecordInfo (XXXDB, CurrentRecord, NULL, &prefs.currentRecordID, 
NULL);
                }
        else
                prefs.currentRecordID = noRecordSelectedID;

        // Write the state information
        PrefSetAppPreferences (appFileCreator, appPrefID, appPrefVersionNum, 
                &prefs, sizeof (prefs), true);

        // Close the application's database.
        DmCloseDatabase(XXXDB);

#ifdef EMUL8
        // Close the MathLib
        error = MathLibClose(MathLibRef, &usecount);
        ErrFatalDisplayIf(error, "AppStop - Can't close MathLib");
        if (usecount == 0)
                SysLibRemove(MathLibRef); 
#endif   

}


/***********************************************************************/
static void AlarmTriggered(SysAlarmTriggeredParamType *cmdPBP, Word launchFlags)
{

        // Sound Alarm
        SndPlaySystemSound(sndAlarm);
        cmdPBP->purgeAlarm = false;
                        
}


Reply via email to