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

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 2.8 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* pState, enum t_statechange_notifications notification, const wxString& data, const void* data2)
53{
54        if (m_pMainFrame->GetQueue()->IsActive())
55        {
56                DoSetBusy();
57                return;
58        }
59
60        const std::vector<CState*> *states = CContextManager::Get()->GetAllStates();
61        for (std::vector<CState*>::const_iterator iter = states->begin(); iter != states->end(); ++iter)
62        {
63                if (!(*iter)->IsRemoteIdle())
64                {
65                        DoSetBusy();
66                        return;
67                }
68        }
69
70        DoSetIdle();
71}
72
73void CPowerManagement::DoSetBusy()
74{
75        if (m_busy)
76                return;
77
78        if (!COptions::Get()->GetOptionVal(OPTION_PREVENT_IDLESLEEP))
79                return;
80
81        m_busy = true;
82
83#ifdef __WXMSW__
84        SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
85#elif defined(WITH_LIBDBUS)
86        m_inhibitor->RequestBusy();
87#elif defined(__WXMAC__)
88        // >= 10.5 Required for Power Management
89        #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
90                IOReturn success = IOPMAssertionCreate(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &m_assertionID);
91                if (success != kIOReturnSuccess)
92                        m_busy = false;
93        #endif
94#endif
95}
96
97void CPowerManagement::DoSetIdle()
98{
99        if (!m_busy)
100                return;
101        m_busy = false;
102
103#ifdef __WXMSW__
104        SetThreadExecutionState(ES_CONTINUOUS);
105#elif defined(WITH_LIBDBUS)
106        m_inhibitor->RequestIdle();
107#elif defined(__WXMAC__)
108        // >= 10.5 Required for Power Management
109        #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
110                IOPMAssertionRelease(m_assertionID);
111        #endif
112#endif
113}
114
115bool CPowerManagement::IsSupported()
116{
117#ifdef __WXMSW__
118        return true;
119#endif
120#if defined(__WXMAC__) &&  MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
121        return true;
122#endif
123#ifdef WITH_LIBDBUS
124        return true;
125#endif
126
127        return false;
128}
Note: See TracBrowser for help on using the repository browser.