source: filezilla/trunk/fuentes/src/interface/power_management.cpp @ 3185

Last change on this file since 3185 was 3185, checked in by jrpelegrina, 2 years ago

Update new version: 3.15.02

File size: 2.7 KB
Line 
1#include <filezilla.h>
2#include "power_management.h"
3#include "Mainfrm.h"
4#include "Options.h"
5#include "queue.h"
6#ifdef WITH_LIBDBUS
7#include "../dbus/power_management_inhibitor.h"
8#endif
9#ifdef __WXMAC__
10        // >= 10.5 Required for Power Management
11        #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
12                #include <IOKit/pwr_mgt/IOPMLib.h>
13        #endif
14#endif
15
16CPowerManagement* CPowerManagement::m_pPowerManagement = 0;
17
18void CPowerManagement::Create(CMainFrame* pMainFrame)
19{
20        if (!m_pPowerManagement)
21                m_pPowerManagement = new CPowerManagement(pMainFrame);
22}
23
24void CPowerManagement::Destroy()
25{
26        delete m_pPowerManagement;
27        m_pPowerManagement = 0;
28}
29
30CPowerManagement::CPowerManagement(CMainFrame* pMainFrame)
31        : CStateEventHandler(0)
32{
33        m_pMainFrame = pMainFrame;
34
35        CContextManager::Get()->RegisterHandler(this, STATECHANGE_QUEUEPROCESSING, false);
36        CContextManager::Get()->RegisterHandler(this, STATECHANGE_REMOTE_IDLE, false);
37
38        m_busy = false;
39
40#ifdef WITH_LIBDBUS
41        m_inhibitor = new CPowerManagementInhibitor();
42#endif
43}
44
45CPowerManagement::~CPowerManagement()
46{
47#ifdef WITH_LIBDBUS
48        delete m_inhibitor;
49#endif
50}
51
52void CPowerManagement::OnStateChange(CState*, t_statechange_notifications, wxString const&, void const*)
53{
54        if (m_pMainFrame->GetQueue()->IsActive()) {
55                DoSetBusy();
56                return;
57        }
58
59        const std::vector<CState*> *states = CContextManager::Get()->GetAllStates();
60        for (std::vector<CState*>::const_iterator iter = states->begin(); iter != states->end(); ++iter) {
61                if (!(*iter)->IsRemoteIdle()) {
62                        DoSetBusy();
63                        return;
64                }
65        }
66
67        DoSetIdle();
68}
69
70void CPowerManagement::DoSetBusy()
71{
72        if (m_busy)
73                return;
74
75        if (!COptions::Get()->GetOptionVal(OPTION_PREVENT_IDLESLEEP))
76                return;
77
78        m_busy = true;
79
80#ifdef __WXMSW__
81        SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
82#elif defined(WITH_LIBDBUS)
83        m_inhibitor->RequestBusy();
84#elif defined(__WXMAC__)
85        // >= 10.5 Required for Power Management
86        #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
87                IOReturn success = IOPMAssertionCreate(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &m_assertionID);
88                if (success != kIOReturnSuccess)
89                        m_busy = false;
90        #endif
91#endif
92}
93
94void CPowerManagement::DoSetIdle()
95{
96        if (!m_busy)
97                return;
98        m_busy = false;
99
100#ifdef __WXMSW__
101        SetThreadExecutionState(ES_CONTINUOUS);
102#elif defined(WITH_LIBDBUS)
103        m_inhibitor->RequestIdle();
104#elif defined(__WXMAC__)
105        // >= 10.5 Required for Power Management
106        #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
107                IOPMAssertionRelease(m_assertionID);
108        #endif
109#endif
110}
111
112bool CPowerManagement::IsSupported()
113{
114#ifdef __WXMSW__
115        return true;
116#endif
117#if defined(__WXMAC__) &&  MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
118        return true;
119#endif
120#ifdef WITH_LIBDBUS
121        return true;
122#endif
123
124        return false;
125}
Note: See TracBrowser for help on using the repository browser.