MythTV  master
screensaver-dbus.cpp
Go to the documentation of this file.
1 #include "screensaver-dbus.h"
2 
3 #include <cstdint>
4 #include <string>
5 
6 #include <QDBusConnection>
7 #include <QDBusInterface>
8 #include <QDBusReply>
9 #include <QString>
10 
11 #include "mythlogging.h"
12 
13 #define LOC QString("ScreenSaverDBus: ")
14 
15 const std::string kApp = "MythTV";
16 const std::string kReason = "Watching TV";
17 const std::string kDbusInhibit = "Inhibit";
18 
19 #define NUM_DBUS_METHODS 4
20 // Thanks to vlc for the set of dbus services to use.
21 const std::array<const QString,NUM_DBUS_METHODS> kDbusService {
22  "org.freedesktop.ScreenSaver",
23  "org.freedesktop.PowerManagement.Inhibit",
24  "org.mate.SessionManager",
25  "org.gnome.SessionManager",
26 };
27 
28 const std::array<const QString,NUM_DBUS_METHODS> kDbusPath {
29  "/ScreenSaver",
30  "/org/freedesktop/PowerManagement",
31  "/org/mate/SessionManager",
32  "/org/gnome/SessionManager",
33 };
34 
35 // Service name is also the interface name in all cases
36 
37 const std::array<const QString,NUM_DBUS_METHODS> kDbusUnInhibit {
38  "UnInhibit",
39  "UnInhibit",
40  "Uninhibit",
41  "Uninhibit",
42 };
43 
45 {
46  friend class ScreenSaverDBus;
47 
48  public:
49  ScreenSaverDBusPrivate(const QString &dbusService, const QString &dbusPath,
50  const QString &dbusInterface, QDBusConnection *bus) :
51  m_bus(bus),
52  m_interface(new QDBusInterface(dbusService, dbusPath , dbusInterface, *m_bus)),
53  m_serviceUsed(dbusService)
54  {
55  if (!m_interface->isValid())
56  {
57  LOG(VB_GENERAL, LOG_DEBUG, LOC + "Could not connect to dbus: " +
58  m_interface->lastError().message());
59  }
60  else
61  {
62  LOG(VB_GENERAL, LOG_INFO, LOC + "Created for DBus service: " + dbusService);
63  }
64  }
66  {
67  delete m_interface;
68  }
69  void Inhibit(void)
70  {
71  if (m_interface->isValid())
72  {
73  // method uint org.freedesktop.ScreenSaver.Inhibit(QString application_name, QString reason_for_inhibit)
74  QDBusMessage msg = m_interface->call(QDBus::Block,
75  kDbusInhibit.c_str(),
76  kApp.c_str(), kReason.c_str());
77  if (msg.type() == QDBusMessage::ReplyMessage)
78  {
79  QList<QVariant> replylist = msg.arguments();
80  QVariant reply = replylist.first();
81  m_cookie = reply.toUInt();
82  m_inhibited = true;
83  LOG(VB_GENERAL, LOG_INFO, LOC +
84  QString("Successfully inhibited screensaver via %1. cookie %2. nom nom")
86  }
87  else // msg.type() == QDBusMessage::ErrorMessage
88  {
89  LOG(VB_GENERAL, LOG_WARNING, LOC + "Failed to disable screensaver: " +
90  msg.errorMessage());
91  }
92  }
93  }
94  void UnInhibit(void)
95  {
96  if (m_interface->isValid())
97  {
98  // Don't block waiting for the reply, there isn't one
99  // method void org.freedesktop.ScreenSaver.UnInhibit(uint cookie) (or equivalent)
100  if (m_cookie != 0) {
101  m_interface->call(QDBus::NoBlock, m_unInhibit , m_cookie);
102  m_cookie = 0;
103  m_inhibited = false;
104  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Screensaver uninhibited via %1")
105  .arg(m_serviceUsed));
106  }
107  }
108  }
109  void SetUnInhibit(const QString &method) { m_unInhibit = method; }
110 
111  protected:
112  bool m_inhibited {false};
113  uint32_t m_cookie {0};
114  QDBusConnection *m_bus {nullptr};
115  QDBusInterface *m_interface {nullptr};
116  private:
117  // Disable copying and assignment
118  Q_DISABLE_COPY(ScreenSaverDBusPrivate)
119  QString m_unInhibit;
120  QString m_serviceUsed;
121 };
122 
124  m_bus(QDBusConnection::sessionBus())
125 {
126  // service, path, interface, bus - note that interface = service, hence it is used twice
127  for (uint i=0; i < NUM_DBUS_METHODS; i++) {
128  auto *ssdbp =
130  ssdbp->SetUnInhibit(kDbusUnInhibit[i]);
131  m_dbusPrivateInterfaces.push_back(ssdbp);
132  }
133 }
134 
136 {
138  while (!m_dbusPrivateInterfaces.isEmpty()) {
140  delete ssdbp;
141  }
142 }
143 
145 {
146  QList<ScreenSaverDBusPrivate *>::iterator i;
147  for (i = m_dbusPrivateInterfaces.begin(); i != m_dbusPrivateInterfaces.end(); ++i) {
148  (*i)->Inhibit();
149  }
150 }
151 
153 {
154  QList<ScreenSaverDBusPrivate *>::iterator i;
155  for (i = m_dbusPrivateInterfaces.begin(); i != m_dbusPrivateInterfaces.end(); ++i) {
156  (*i)->UnInhibit();
157  }
158 }
159 
161 {
162  Restore();
163 }
164 
166 {
167  QList<ScreenSaverDBusPrivate *>::iterator i;
168  for (i = m_dbusPrivateInterfaces.begin(); i != m_dbusPrivateInterfaces.end(); ++i) {
169  if((*i)->m_inhibited) {
170  return true;
171  }
172  }
173  return false;
174 }
ScreenSaverDBusPrivate::m_inhibited
bool m_inhibited
Definition: screensaver-dbus.cpp:112
kDbusUnInhibit
const std::array< const QString, NUM_DBUS_METHODS > kDbusUnInhibit
Definition: screensaver-dbus.cpp:37
ScreenSaverDBusPrivate::m_unInhibit
QString m_unInhibit
Definition: screensaver-dbus.cpp:119
ScreenSaverDBusPrivate::m_serviceUsed
QString m_serviceUsed
Definition: screensaver-dbus.cpp:120
ScreenSaverDBusPrivate::SetUnInhibit
void SetUnInhibit(const QString &method)
Definition: screensaver-dbus.cpp:109
NUM_DBUS_METHODS
#define NUM_DBUS_METHODS
Definition: screensaver-dbus.cpp:19
arg
arg(title).arg(filename).arg(doDelete))
kDbusService
const std::array< const QString, NUM_DBUS_METHODS > kDbusService
KDE >= 4 and GNOME >= 3.10.
Definition: screensaver-dbus.cpp:21
kReason
const std::string kReason
Definition: screensaver-dbus.cpp:16
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
ScreenSaverDBusPrivate::Inhibit
void Inhibit(void)
Definition: screensaver-dbus.cpp:69
ScreenSaverDBusPrivate
Definition: screensaver-dbus.cpp:45
ScreenSaverDBusPrivate::ScreenSaverDBusPrivate
ScreenSaverDBusPrivate(const QString &dbusService, const QString &dbusPath, const QString &dbusInterface, QDBusConnection *bus)
Definition: screensaver-dbus.cpp:49
kDbusInhibit
const std::string kDbusInhibit
Definition: screensaver-dbus.cpp:17
ScreenSaverDBus
Definition: screensaver-dbus.h:8
mythlogging.h
ScreenSaverDBusPrivate::m_cookie
uint32_t m_cookie
Definition: screensaver-dbus.cpp:113
ScreenSaverDBus::Disable
void Disable(void) override
Definition: screensaver-dbus.cpp:144
ScreenSaverDBus::Asleep
bool Asleep(void) override
Definition: screensaver-dbus.cpp:165
uint
unsigned int uint
Definition: compat.h:140
ScreenSaverDBusPrivate::m_interface
QDBusInterface * m_interface
Definition: screensaver-dbus.cpp:115
LOC
#define LOC
Definition: screensaver-dbus.cpp:13
kDbusPath
const std::array< const QString, NUM_DBUS_METHODS > kDbusPath
Definition: screensaver-dbus.cpp:28
ScreenSaverDBusPrivate::~ScreenSaverDBusPrivate
~ScreenSaverDBusPrivate()
Definition: screensaver-dbus.cpp:65
screensaver-dbus.h
ScreenSaverDBus::~ScreenSaverDBus
~ScreenSaverDBus() override
Definition: screensaver-dbus.cpp:135
ScreenSaverDBusPrivate::UnInhibit
void UnInhibit(void)
Definition: screensaver-dbus.cpp:94
ScreenSaverDBus::m_dbusPrivateInterfaces
QList< ScreenSaverDBusPrivate * > m_dbusPrivateInterfaces
Definition: screensaver-dbus.h:21
ScreenSaverDBus::Reset
void Reset(void) override
Definition: screensaver-dbus.cpp:160
ScreenSaverDBus::Restore
void Restore(void) override
Definition: screensaver-dbus.cpp:152
ScreenSaverDBusPrivate::m_bus
QDBusConnection * m_bus
Definition: screensaver-dbus.cpp:114
kApp
const std::string kApp
Definition: screensaver-dbus.cpp:15