1 #include "common/common_pch.h"
2 
3 #include "mkvtoolnix-gui/util/sleep_inhibitor.h"
4 #include "mkvtoolnix-gui/util/sleep_inhibitor_p.h"
5 #if defined(SYS_WINDOWS)
6 # include "mkvtoolnix-gui/util/sleep_inhibitor/windows.h"
7 #elif defined(SYS_APPLE)
8 # include "mkvtoolnix-gui/util/sleep_inhibitor/macos.h"
9 #elif defined(SYS_LINUX)
10 # include "mkvtoolnix-gui/util/sleep_inhibitor/linux_logind.h"
11 #endif
12 
13 namespace mtx::gui::Util {
14 
15 debugging_option_c BasicSleepInhibitorPrivate::ms_debug{"sleep_inhibitor"};
16 
BasicSleepInhibitor()17 BasicSleepInhibitor::BasicSleepInhibitor()
18   : p_ptr{new BasicSleepInhibitorPrivate}
19 {
20 }
21 
BasicSleepInhibitor(BasicSleepInhibitorPrivate & p)22 BasicSleepInhibitor::BasicSleepInhibitor(BasicSleepInhibitorPrivate &p)
23   : p_ptr{&p}
24 {
25 }
26 
~BasicSleepInhibitor()27 BasicSleepInhibitor::~BasicSleepInhibitor() {
28   uninhibit();
29 }
30 
31 bool
inhibit()32 BasicSleepInhibitor::inhibit() {
33   for (auto const &inhibitor : p_func()->m_inhibitors)
34     if (inhibitor->inhibit())
35       return true;
36 
37   return false;
38 }
39 
40 void
uninhibit()41 BasicSleepInhibitor::uninhibit() {
42   for (auto const &inhibitor : p_func()->m_inhibitors)
43     inhibitor->uninhibit();
44 }
45 
46 bool
isInhibited() const47 BasicSleepInhibitor::isInhibited()
48   const {
49   for (auto const &inhibitor : p_func()->m_inhibitors)
50     if (inhibitor->isInhibited())
51       return true;
52 
53   return false;
54 }
55 
56 void
addInhibitor(std::shared_ptr<BasicSleepInhibitor> const & inhibitor)57 BasicSleepInhibitor::addInhibitor(std::shared_ptr<BasicSleepInhibitor> const &inhibitor) {
58   p_func()->m_inhibitors.emplace_back(inhibitor);
59 }
60 
61 std::unique_ptr<BasicSleepInhibitor>
create()62 BasicSleepInhibitor::create() {
63   auto inhibitor = std::make_unique<BasicSleepInhibitor>();
64 
65 #if defined(SYS_WINDOWS)
66   inhibitor->addInhibitor(std::make_shared<WindowsSleepInhibitor>());
67 #elif defined(SYS_APPLE)
68   inhibitor->addInhibitor(std::make_shared<MacOSSleepInhibitor>());
69 #elif defined(SYS_LINUX)
70   inhibitor->addInhibitor(std::make_shared<LogindSleepInhibitor>());
71 #endif
72 
73  return inhibitor;
74 }
75 
76 }
77