2006-05-27 10:09:40 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: power.cpp
|
|
|
|
// Purpose: wxWidgets power management sample
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2006-05-27
|
2019-04-22 08:12:05 -04:00
|
|
|
// Copyright: (C) 2006 Vadim Zeitlin <vadim@wxwidgets.org>
|
2006-05-27 10:09:40 -04:00
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/frame.h"
|
2008-06-21 01:10:40 -04:00
|
|
|
#include "wx/log.h"
|
2006-05-27 10:09:40 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/textctrl.h"
|
|
|
|
#include "wx/msgdlg.h"
|
2014-08-29 19:22:15 -04:00
|
|
|
#include "wx/menu.h"
|
|
|
|
#include "wx/timer.h"
|
2006-05-27 10:09:40 -04:00
|
|
|
|
|
|
|
#include "wx/power.h"
|
|
|
|
|
2012-03-03 19:28:58 -05:00
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
2006-05-27 10:09:40 -04:00
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame()
|
|
|
|
: wxFrame(NULL, wxID_ANY, "wxWidgets Power Management Sample",
|
|
|
|
wxDefaultPosition, wxSize(500, 200))
|
|
|
|
{
|
2014-08-29 19:22:15 -04:00
|
|
|
m_powerResourceBlocker = NULL;
|
|
|
|
|
|
|
|
wxMenu *fileMenu = new wxMenu;
|
|
|
|
fileMenu->Append(wxID_NEW, "Start long running task\tCtrl-S");
|
|
|
|
fileMenu->Append(wxID_ABORT, "Stop long running task");
|
|
|
|
|
|
|
|
wxMenuBar* menuBar = new wxMenuBar();
|
|
|
|
menuBar->Append(fileMenu, "&Task");
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
|
2006-05-27 10:09:40 -04:00
|
|
|
wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, "",
|
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
wxTE_MULTILINE | wxTE_READONLY);
|
|
|
|
m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(text));
|
|
|
|
|
2006-05-27 10:29:30 -04:00
|
|
|
CreateStatusBar();
|
|
|
|
|
2006-05-27 10:09:40 -04:00
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
2006-05-27 10:29:30 -04:00
|
|
|
UpdatePowerSettings(wxPOWER_UNKNOWN, wxBATTERY_UNKNOWN_STATE);
|
|
|
|
|
2014-08-29 19:22:15 -04:00
|
|
|
StopLongTask();
|
|
|
|
Bind(wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
&MyFrame::OnStartTaskClicked, this, wxID_NEW);
|
|
|
|
Bind(wxEVT_COMMAND_MENU_SELECTED,
|
|
|
|
&MyFrame::OnStopTaskClicked, this, wxID_ABORT);
|
|
|
|
m_taskTimer.Bind(wxEVT_TIMER, &MyFrame::OnTaskTimer, this);
|
|
|
|
|
2006-05-27 10:09:40 -04:00
|
|
|
Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MyFrame()
|
|
|
|
{
|
2014-08-29 19:22:15 -04:00
|
|
|
delete m_powerResourceBlocker;
|
|
|
|
|
2006-05-27 10:09:40 -04:00
|
|
|
delete wxLog::SetActiveTarget(m_logOld);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2006-05-27 10:29:30 -04:00
|
|
|
void OnIdle(wxIdleEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
const wxPowerType powerType = wxGetPowerType();
|
|
|
|
const wxBatteryState batteryState = wxGetBatteryState();
|
|
|
|
if ( powerType != m_powerType || batteryState != m_batteryState )
|
|
|
|
{
|
|
|
|
UpdatePowerSettings(powerType, batteryState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef wxHAS_POWER_EVENTS
|
2006-05-27 10:09:40 -04:00
|
|
|
void OnSuspending(wxPowerEvent& event)
|
|
|
|
{
|
|
|
|
wxLogMessage("System suspend starting...");
|
|
|
|
if ( wxMessageBox("Veto suspend?", "Please answer",
|
|
|
|
wxYES_NO, this) == wxYES )
|
|
|
|
{
|
|
|
|
event.Veto();
|
|
|
|
wxLogMessage("Vetoed suspend.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnSuspended(wxPowerEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxLogMessage("System is going to suspend.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnSuspendCancel(wxPowerEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxLogMessage("System suspend was cancelled.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnResume(wxPowerEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxLogMessage("System resumed from suspend.");
|
|
|
|
}
|
2006-05-27 10:29:30 -04:00
|
|
|
#endif // wxHAS_POWER_EVENTS
|
|
|
|
|
|
|
|
|
|
|
|
void UpdatePowerSettings(wxPowerType powerType, wxBatteryState batteryState)
|
|
|
|
{
|
|
|
|
wxString powerStr;
|
|
|
|
switch ( m_powerType = powerType )
|
|
|
|
{
|
|
|
|
case wxPOWER_SOCKET:
|
|
|
|
powerStr = "wall";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxPOWER_BATTERY:
|
|
|
|
powerStr = "battery";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG("unknown wxPowerType value");
|
2018-11-25 14:22:44 -05:00
|
|
|
wxFALLTHROUGH;
|
2006-05-27 10:29:30 -04:00
|
|
|
|
|
|
|
case wxPOWER_UNKNOWN:
|
|
|
|
powerStr = "psychic";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString batteryStr;
|
|
|
|
switch ( m_batteryState = batteryState )
|
|
|
|
{
|
|
|
|
case wxBATTERY_NORMAL_STATE:
|
|
|
|
batteryStr = "charged";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxBATTERY_LOW_STATE:
|
|
|
|
batteryStr = "low";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxBATTERY_CRITICAL_STATE:
|
|
|
|
batteryStr = "critical";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxBATTERY_SHUTDOWN_STATE:
|
|
|
|
batteryStr = "empty";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG("unknown wxBatteryState value");
|
2018-11-25 14:22:44 -05:00
|
|
|
wxFALLTHROUGH;
|
2006-05-27 10:29:30 -04:00
|
|
|
|
|
|
|
case wxBATTERY_UNKNOWN_STATE:
|
|
|
|
batteryStr = "unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatusText(wxString::Format(
|
|
|
|
"System is on %s power, battery state is %s",
|
2018-11-01 19:32:02 -04:00
|
|
|
powerStr,
|
|
|
|
batteryStr));
|
2006-05-27 10:29:30 -04:00
|
|
|
}
|
2006-05-27 10:09:40 -04:00
|
|
|
|
2014-08-29 19:22:15 -04:00
|
|
|
void OnStartTaskClicked( wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
|
|
|
wxLogMessage("Starting long running task "
|
|
|
|
"(screen should keep powered on while running)...");
|
|
|
|
StartLongTask();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnStopTaskClicked(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
StopLongTask();
|
|
|
|
wxLogMessage("Stopped long running task");
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnTaskTimer(wxTimerEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
++m_taskProgress;
|
|
|
|
|
|
|
|
if ( m_taskProgress == 100 )
|
|
|
|
{
|
|
|
|
StopLongTask();
|
|
|
|
wxLogMessage("Long running task finished");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogMessage("Long running task at %d%%...", m_taskProgress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartLongTask()
|
|
|
|
{
|
|
|
|
m_taskProgress = 0;
|
|
|
|
m_taskTimer.Start(12000);
|
|
|
|
GetMenuBar()->Enable(wxID_NEW, false);
|
|
|
|
GetMenuBar()->Enable(wxID_ABORT, true);
|
|
|
|
|
|
|
|
m_powerResourceBlocker
|
|
|
|
= new wxPowerResourceBlocker(wxPOWER_RESOURCE_SYSTEM);
|
|
|
|
|
|
|
|
if ( !m_powerResourceBlocker->IsInEffect() )
|
|
|
|
{
|
|
|
|
wxLogMessage("Power resource could not be acquired, "
|
|
|
|
"user input is required to prevent system standby");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopLongTask()
|
|
|
|
{
|
|
|
|
GetMenuBar()->Enable(wxID_NEW, true);
|
|
|
|
GetMenuBar()->Enable(wxID_ABORT, false);
|
|
|
|
m_taskTimer.Stop();
|
|
|
|
|
|
|
|
wxDELETE(m_powerResourceBlocker);
|
|
|
|
}
|
|
|
|
|
2006-05-27 10:29:30 -04:00
|
|
|
wxPowerType m_powerType;
|
|
|
|
wxBatteryState m_batteryState;
|
2006-05-27 10:09:40 -04:00
|
|
|
|
|
|
|
wxLog *m_logOld;
|
2014-08-29 19:22:15 -04:00
|
|
|
wxTimer m_taskTimer;
|
|
|
|
wxPowerResourceBlocker *m_powerResourceBlocker;
|
|
|
|
int m_taskProgress;
|
2006-05-27 10:09:40 -04:00
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxDECLARE_EVENT_TABLE();
|
2006-05-27 10:09:40 -04:00
|
|
|
};
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2006-05-27 10:29:30 -04:00
|
|
|
EVT_IDLE(MyFrame::OnIdle)
|
|
|
|
|
|
|
|
#ifdef wxHAS_POWER_EVENTS
|
2006-05-27 10:09:40 -04:00
|
|
|
EVT_POWER_SUSPENDING(MyFrame::OnSuspending)
|
|
|
|
EVT_POWER_SUSPENDED(MyFrame::OnSuspended)
|
|
|
|
EVT_POWER_SUSPEND_CANCEL(MyFrame::OnSuspendCancel)
|
|
|
|
EVT_POWER_RESUME(MyFrame::OnResume)
|
2006-05-27 10:29:30 -04:00
|
|
|
#endif // wxHAS_POWER_EVENTS
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2006-05-27 10:09:40 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual bool OnInit() wxOVERRIDE
|
2006-05-27 10:09:40 -04:00
|
|
|
{
|
|
|
|
new MyFrame;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|