2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: power.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxPowerEvent
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-12-27 15:47:12 -05:00
|
|
|
enum wxPowerType
|
|
|
|
{
|
|
|
|
wxPOWER_SOCKET,
|
|
|
|
wxPOWER_BATTERY,
|
|
|
|
wxPOWER_UNKNOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
enum wxBatteryState
|
|
|
|
{
|
|
|
|
wxBATTERY_NORMAL_STATE, // system is fully usable
|
|
|
|
wxBATTERY_LOW_STATE, // start to worry
|
|
|
|
wxBATTERY_CRITICAL_STATE, // save quickly
|
|
|
|
wxBATTERY_SHUTDOWN_STATE, // too late
|
|
|
|
wxBATTERY_UNKNOWN_STATE
|
|
|
|
};
|
|
|
|
|
2014-08-29 19:22:15 -04:00
|
|
|
/**
|
|
|
|
Possible power resources that can be locked by wxPowerResourceBlocker.
|
|
|
|
|
|
|
|
@since 3.1.0
|
|
|
|
*/
|
|
|
|
enum wxPowerResourceKind
|
|
|
|
{
|
|
|
|
/// Use to prevent automatic display power off.
|
|
|
|
wxPOWER_RESOURCE_SCREEN,
|
|
|
|
|
|
|
|
/// Use to prevent automatic system suspend.
|
|
|
|
wxPOWER_RESOURCE_SYSTEM
|
|
|
|
};
|
2010-12-27 15:47:12 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxPowerEvent
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
The power events are generated when the system power state changes, e.g. the
|
|
|
|
system is suspended, hibernated, plugged into or unplugged from the wall socket
|
2009-02-18 12:58:51 -05:00
|
|
|
and so on. wxPowerEvents are emitted by wxWindows.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Notice that currently only suspend and resume events are generated and only
|
|
|
|
under MS Windows platform. To avoid the need to change the code using this
|
2008-04-10 17:16:38 -04:00
|
|
|
event later when these events are implemented on the other platforms please
|
|
|
|
use the test <tt>ifdef wxHAS_POWER_EVENTS</tt> instead of directly testing for
|
2008-03-08 08:52:38 -05:00
|
|
|
the platform in your code: this symbol will be defined for all platforms
|
|
|
|
supporting the power events.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-10 17:16:38 -04:00
|
|
|
@beginEventTable{wxPowerEvent}
|
2009-02-18 12:58:51 -05:00
|
|
|
@event{EVT_POWER_SUSPENDING(func)}
|
2014-08-29 19:22:15 -04:00
|
|
|
@warning This event and the possibility to veto suspend was removed
|
|
|
|
from MSW systems starting from Windows Vista. wxPowerResourceBlocker
|
|
|
|
can be used to prevent the system from suspending under both XP and
|
|
|
|
later systems, use it instead of handling this event.
|
|
|
|
|
2008-04-10 17:16:38 -04:00
|
|
|
System is about to be suspended, this event can be vetoed to prevent
|
|
|
|
suspend from taking place.
|
2009-02-18 12:58:51 -05:00
|
|
|
@event{EVT_POWER_SUSPENDED(func)}
|
2008-04-10 17:16:38 -04:00
|
|
|
System is about to suspend: normally the application should quickly
|
|
|
|
(i.e. without user intervention) close all the open files and network
|
|
|
|
connections here, possibly remembering them to reopen them later when
|
|
|
|
the system is resumed.
|
2009-02-18 12:58:51 -05:00
|
|
|
@event{EVT_POWER_SUSPEND_CANCEL(func)}
|
2008-04-10 17:16:38 -04:00
|
|
|
System suspension was cancelled because some application vetoed it.
|
2009-02-18 12:58:51 -05:00
|
|
|
@event{EVT_POWER_RESUME(func)}
|
2008-04-10 17:16:38 -04:00
|
|
|
System resumed from suspend: normally the application should restore
|
|
|
|
the state in which it had been before the suspension.
|
|
|
|
@endEventTable
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
2008-04-10 17:16:38 -04:00
|
|
|
@category{events}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-10 17:16:38 -04:00
|
|
|
@see ::wxGetPowerType(), ::wxGetBatteryState()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
class wxPowerEvent : public wxEvent
|
|
|
|
{
|
|
|
|
public:
|
2012-05-19 02:21:25 -04:00
|
|
|
wxPowerEvent();
|
|
|
|
wxPowerEvent(wxEventType evtType);
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-04-10 17:16:38 -04:00
|
|
|
Call this to prevent suspend from taking place in @c wxEVT_POWER_SUSPENDING
|
|
|
|
handler (it is ignored for all the others).
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void Veto();
|
2012-05-19 02:21:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns whether Veto has been called.
|
|
|
|
*/
|
|
|
|
bool IsVetoed() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|
2012-05-19 02:21:25 -04:00
|
|
|
wxEventType wxEVT_POWER_SUSPENDING;
|
|
|
|
wxEventType wxEVT_POWER_SUSPENDED;
|
|
|
|
wxEventType wxEVT_POWER_SUSPEND_CANCEL;
|
|
|
|
wxEventType wxEVT_POWER_RESUME;
|
|
|
|
|
2014-08-29 19:22:15 -04:00
|
|
|
/**
|
|
|
|
Helper functions for acquiring and releasing the given power resource.
|
|
|
|
|
|
|
|
If an application performs a long running task without user interaction it
|
|
|
|
is often necessary to prevent the system from automatically suspending or
|
|
|
|
powering off the screen and Acquire() method can be used to do this.
|
|
|
|
|
|
|
|
Notice that currently this functionality is only implemented for MSW and
|
2015-07-22 08:36:26 -04:00
|
|
|
OS X.
|
2014-08-29 19:22:15 -04:00
|
|
|
|
|
|
|
If possible, use wxPowerResourceBlocker class to ensure that Release() is
|
|
|
|
called instead of calling it manually.
|
|
|
|
|
|
|
|
@since 3.1.0
|
|
|
|
@library{wxbase}
|
|
|
|
@category{misc}
|
|
|
|
|
|
|
|
@see wxPowerResourceBlocker
|
|
|
|
*/
|
|
|
|
class wxPowerResource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Acquire a power resource for the application.
|
|
|
|
|
|
|
|
If successful, the system will not automatically power of the screen or
|
|
|
|
suspend until Release() is called.
|
|
|
|
|
|
|
|
Every call to Acquire @b must be matched by a corresponding call to
|
|
|
|
Release() or the system will not suspend until the application ends, use
|
|
|
|
wxPowerResourceBlocker to ensure that this happens.
|
|
|
|
|
|
|
|
@param kind Power resource required, either ::wxPOWER_RESOURCE_SCREEN
|
|
|
|
or ::wxPOWER_RESOURCE_SYSTEM.
|
|
|
|
@param reason Optional reason may be specified which might be used on
|
|
|
|
some platforms to inform the user what is preventing power saving.
|
|
|
|
It should usually describe the operation requiring the resource and
|
|
|
|
specifying it is strongly recommended.
|
|
|
|
@return Returns true if the acquisition was successful.
|
|
|
|
|
|
|
|
@see Release()
|
|
|
|
*/
|
|
|
|
static bool Acquire(wxPowerResourceKind kind,
|
|
|
|
const wxString& reason = wxString());
|
|
|
|
|
|
|
|
/**
|
|
|
|
Release a previously acquired power resource.
|
|
|
|
|
|
|
|
Release @b must be called for every Acquire() call made to restore
|
|
|
|
normal power saving behaviour
|
|
|
|
|
|
|
|
@param kind Power resource to be released.
|
|
|
|
|
|
|
|
@see Acquire()
|
|
|
|
*/
|
|
|
|
static void Release(wxPowerResourceKind kind);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
Helper RAII class ensuring that power resources are released.
|
|
|
|
|
|
|
|
A wxPowerResourceBlocker object acquires a power resource in the
|
|
|
|
constructor and releases it in the destructor making it impossible to to
|
|
|
|
forget to release the power resource (which would prevent suspending or
|
|
|
|
screen power off until the application ends).
|
|
|
|
|
|
|
|
Example:
|
|
|
|
@code
|
|
|
|
void MyWindow::DoSomething()
|
|
|
|
{
|
|
|
|
wxPowerResourceBlocker
|
|
|
|
blocker(wxPOWER_RESOURCE_SYSTEM, "Downloading something important");
|
|
|
|
|
|
|
|
if ( !blocker.IsInEffect() )
|
|
|
|
{
|
|
|
|
// If the resource could not be acquired, tell the user that he has
|
|
|
|
// to keep the system alive
|
|
|
|
wxLogMessage("Warning: system may suspend while downloading.");
|
|
|
|
}
|
2012-05-19 02:21:25 -04:00
|
|
|
|
2014-08-29 19:22:15 -04:00
|
|
|
// Run an important download and the system will not suspend while downloading
|
|
|
|
for ( int i = 0; i < download.size(); ++i )
|
|
|
|
download.readByte();
|
|
|
|
|
|
|
|
// wxPOWER_RESOURCE_SYSTEM automatically released here.
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
@since 3.1.0
|
|
|
|
@library{wxbase}
|
|
|
|
@category{misc}
|
|
|
|
|
|
|
|
@see wxPowerResource
|
|
|
|
*/
|
|
|
|
class wxPowerResourceBlocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Acquires the power resource.
|
|
|
|
|
|
|
|
Uses the same parameters as wxPowerResource::Acquire().
|
|
|
|
*/
|
|
|
|
explicit wxPowerResourceBlocker(wxPowerResourceKind kind,
|
|
|
|
const wxString& reason = wxString());
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns whether the power resource could be acquired.
|
|
|
|
|
|
|
|
This can be used to inform the user that the application will not
|
|
|
|
prevent automatic suspending.
|
|
|
|
|
|
|
|
@see wxPowerResource::Acquire()
|
|
|
|
*/
|
|
|
|
bool IsInEffect() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Releases the power resource.
|
|
|
|
|
|
|
|
@see wxPowerResource::Release()
|
|
|
|
*/
|
|
|
|
~wxPowerResourceBlocker();
|
|
|
|
};
|