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