![]() Power Management & The Energy Saver APIBy Vinnie MoscaritoloApple Developer Technical Support (DTS) |
CONTENTS |
In an effort to provide a consistent method of controlling the various power conservation hardware and software features implemented on the Macintosh platform, Apple has introduced the Energy Saver system. In addition to providing developers with a standard way to control power management features such as display sleep/dimming, hard drive spindown, idle shutdown or sleep, the Energy Services API also provides a consistent way to synchronize the Human Interfaces for multiple concurrent applications that need to access these features. This Note discusses how to communicate with the Macintosh Energy Saver Extension through the Energy Services API and is important for developers who wish to access Mac OS power management features from their applications. This Note also includes the public interfaces for the Energy Services API. |
Energy Saver OverviewEnergy Saver provides a consistent method of controlling the various power conservation hardware and software features implemented on the Macintosh platform. Although the features it supports vary depending on the machines' capabilities, Energy Saver typically attempts to reduce power consumption by controlling such operations as:
The Energy Services API also provides a way to synchronize the Human Interfaces for multiple concurrent applications that need to access the power management features.
Components of the Energy SaverEnergy Saver is actually composed of two separate components: a system extension and a control panel.
Figure 1 provides an overview of the Energy Saver components. FIGURE 1. Energy Saver Overview
|
Energy Services AccessApplications can safely use the same mechanism to manipulate the Energy Saver preference file as the Energy Saver control panel. This is accomplished through the Energy Services API. The purpose of this API is to provide homogenous support for high-level power management features that are not provided by the Power Manager, as well as provide standardized preference support for power manager functions that do not have persistent PRAM settings. The Energy Saver Extension provides auto power on/off features for all Macs with an Egret or Cuda microcontrollers. In addition, it will manage preferences for Screen dimming, Hard disk spin down, sleep mode, and options for all these features. There are calls that can be used to obtain and set the preferences, as well as calls to activate the settings in the preferences passed. Whenever possible, application developers should use this functionality instead of creating their own from scratch, at least to the extent of updating the preferences, so that other applications and control panels will reflect user choices made in all applications. |
Determining If Your Mac Supports the Energy Services APIThe proper way to determine if a particular Macintosh model supports the Energy Services API is to use the Gestalt function to check for the 'wnkl' selector, then check the response for non-nil value. This will indicate that the Energy Services API is loaded. |
Calling the Energy Services APIOnce you have established the existence of the Energy Services Manager you can access the energy services API via the gestalt selector 'wnkl' response value. The response is a Handle to a data structure whose first field is a pointer to the Main routine. The interface to this routine is:
To facilitate developer usage, the
Calling the
|
Energy Services API |
The ESGlobals StructureThe Energy Saver Extension maintains a set of preferences
globals. These globals are defined in the |
typedef struct { short version; // data structure version (1) long EnergySaverFeatures; // ES features (not used yet) // Idle Sleep & ShutDown Timing unsigned long dimIdleTime; // minutes before screen sleeps unsigned long spinDownIdleTime; // minutes before HD spins down // desktops must be >30 unsigned long sleepIdleTime; // minutes prior to system sleep short idleFlags; // Idle time features supported // 0001 = Enable dimIdleTime // 0002 = Enable spinDownIdleTime // 0008 = ShutDown instead of sleep // 0010 = Never Spindown disk // 0020 = Restart after powerfail short reserved1; short reserved2; // Schedule Sleep & Shut Down // These times are in minutes from midnight (actually 12:00:01 AM ) // these fileds are only used by the Energy Saver application HI. // your application should use the SDxxxTime and SWUxxxTime fields // to maniupulate the shutdown and Wakeup time unsigned long mainWUTime; // Time computer starts up unsigned long mainSDTime; // Time computer shuts down // bit fields telling which day of the week Wakeup or Shutdown is enabled short WUFields; // Startup short SDFields; // Shutdown // Monday = 0x0001 // Tuesday = 0x0002 // Wednesday = 0x0004 // Thursday = 0x0008 // Friday = 0x0010 // Saturday = 0x0020 // Sunday = 0x0040 // EveryDay = 0x007F // Enabled = 0x0080 // The following fields let you specify different times of the day // for startup and shutdown for each individual day. If you are not // going to allow specific daily schedules. then set all WU's to // mainWUTime and all SD fields to mainSDTime. // Shutdown Time unsigned long SDMonTime; unsigned long SDTueTime; unsigned long SDWedTime; unsigned long SDThuTime; unsigned long SDFriTime; unsigned long SDSatTime; unsigned long SDSunTime; // Wakeup Time unsigned long WUMonTime; unsigned long WUTueTime; unsigned long WUWedTime; unsigned long WUThuTime; unsigned long WUFriTime; unsigned long WUSatTime; unsigned long WUSunTime; // Sleep Prefs Info stuff short reserved3; short WUSoundResID; // ID of the 'snd ' to play on wakeup short reserved4; // Startup Prefs Info short reserved5; short AppFlags; // Always set to 0 in new pref file. short NonComplianceFlag; // Set if machine sleeps over 30 watts // Shutdown Prefs Info short SDIdleTime; short SDNotifyFlags; // Notification Options // 0020 = Notify on Shutdown // 0001 = Blinking icon // 0002 = Text message // 0004 = Play snd // 0008 = save files // 8008 = Dont save on sleep // 0400 = Play snd on Wakeup // 0800 = Wakeup on Ring detect // 1000 = Blink pwr light (portables) // 2000 = mute sounds when asleep short SDNotifyDelayTime; short SDSoundResID; // ID of the 'snd ' to play on Shutdown short reserved6; short reserved7; // Energy Saver General prefs info long reserved8; Boolean reserved9; } ESGlobals, *ESGlobalsPtr, **ESGlobalsHand; |
ESLoadPreferencesESLoadPreferences reloads the current settings from the preferences file and updates the Energy Services Globals. It also returns a copy of the settings for your use. PROTOTYPEOSErr ESLoadPreferences(ESGlobalsPtr thePrefs);
RESULT CODEFile Read Errors, Memory Errors, ES Errors IMPORTANTThis is called by the Energy Saver Extension at system startup time. Your application should never have to make this call unless you intend to revert back to the saved preference settings. |
ESGetPreferencesESGetPreferences returns a copy of the current settings for your use. This is the normal call an application will make to get the current user settings for all the energy services information. PROTOTYPEOSErr ESGetPreferences(ESGlobalsPtr thePrefs);
RESULT CODEMemory Errors, ES Errors |
ESSetPreferencesESSetPreferences copies the passed settings to the Energy Services globals. PROTOTYPEOSErr ESSetPreferences(ESGlobalsPtr thePrefs);
RESULT CODEMemory Errors, ES Errors IMPORTANT
ESSetPreferences will NOT call the Power Manager to make
the settings effective, nor will it set new startup and
shutdown times. After making this call, you need to call
|
ESRefreshSettingsESRefreshSettings will cause the current settings to be activated and will make all necessary power manager calls to cause them to be active. In addition, it will install whatever startup and shutdown tasks need to be installed to act on the current set of notifications. PROTOTYPEOSErr ESRefreshSettings(); RESULT CODEES Errors |
ESSavePreferencesESSavePreferences performs three operations: it calls ESSetPreferences() to transfer the passed settings, then ESRefreshSettings() to make them active, and lastly it saves the settings out to the preferences file so they remain consistent over boots. This is the normal call an application will make when it wants to make a persistent update to the Energy services settings. PROTOTYPEOSErr ESSavePreferences(ESGlobalsPtr thePrefs);
RESULT CODEFile Errors, Memory Errors, ES Errors IMPORTANTWhen specifying snd resources in the |
ESAddNoteProc
You can use the
PROTOTYPEOSErr ESAddNoteProc(ESNotifyProcPtr theProc, long data);
RESULT CODE
SEE ALSO
|
ESRemoveNoteProc
You can use the
PROTOTYPEOSErr ESRemoveNoteProc(ESNotifyProcPtr theProc);
RESULT CODE
SEE ALSO
|
Energy Saver Notification FunctionOnce installed by the PROTOTYPEpascal long MyESNotifyProc (short selector, ESGlobalsPtr theData);
DESCRIPTIONThe valid types of activity are: |
ESRestoreDefaults
PROTOTYPEOSErr ESRestoreDefaults(ESGlobalsPtr thePrefs);
RESULT CODE
|
ESGetUnsavedFolder
PROTOTYPEOSErr ESGetUnsavedFolder(Ptr theString);
RESULT CODE
|
ESSetUnsavedFolder
You can use the
PROTOTYPEOSErr ESSetUnsavedFolder(Ptr theString);
RESULT CODE
|
ESGetINITVersion
PROTOTYPEOSErr ESGetINITVersion(Handle* theVers);
RESULT CODE
|
SummaryAs the Macintosh platform continues to evolve, the Energy Saver API provides a consistent method of monitoring and controlling power management features. In addition, the Energy Saver API also presents a few interesting opportunities for third-party developer applications. Further ReferencesDownloadables
AcknowledgmentsSpecial thanks to Dave West, Nancy Zayed and Bun-Bun the Rabbit. |