26 #include <QDBusInterface>
27 #include <QDBusPendingCall>
28 #include <QDBusPendingCallWatcher>
29 #include <QDBusPendingReply>
31 #include <QWeakPointer>
36 #ifndef DISPLAY_RELEASE_INTERVAL
37 #define DISPLAY_RELEASE_INTERVAL 4000
45 namespace MediaHubService {
55 m_interface(QStringLiteral(
"com.canonical.Unity.Screen"),
56 QStringLiteral(
"/com/canonical/Unity/Screen"),
57 QStringLiteral(
"com.canonical.Unity.Screen"),
58 QDBusConnection::systemBus()),
66 if (m_requestId >= 0) {
67 MH_ERROR(
"Display ON was already requested!");
71 QDBusPendingCall call = m_interface.asyncCall(
"keepDisplayOn");
72 auto watcher =
new QDBusPendingCallWatcher(call);
73 QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
74 [
this, cb](QDBusPendingCallWatcher *watcher) {
75 QDBusPendingReply<int32_t> reply = *watcher;
76 if (reply.isError()) {
77 MH_ERROR() <<
"Error requesting display ON:" << reply.error().message();
79 m_requestId = reply.value();
82 watcher->deleteLater();
87 if (m_requestId < 0) {
92 QDBusPendingCall call = m_interface.asyncCall(
"removeDisplayOnRequest", m_requestId);
93 auto watcher =
new QDBusPendingCallWatcher(call);
94 QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
95 [
this, cb](QDBusPendingCallWatcher *watcher) {
96 QDBusPendingReply<void> reply = *watcher;
97 if (reply.isError()) {
98 MH_ERROR() <<
"Error releasing display ON:" << reply.error().message();
103 watcher->deleteLater();
108 QDBusInterface m_interface;
115 m_interface(QStringLiteral(
"com.lomiri.Repowerd"),
116 QStringLiteral(
"/com/lomiri/Repowerd"),
117 QStringLiteral(
"com.lomiri.Repowerd"),
118 QDBusConnection::systemBus())
129 if (state == media::power::SystemState::suspend) {
133 QDBusPendingCall call =
134 m_interface.asyncCall(
"requestSysState",
135 QStringLiteral(
"media-hub-playback_lock"),
136 static_cast<int32_t
>(state));
137 auto watcher =
new QDBusPendingCallWatcher(call);
138 QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
139 [
this, state, cb](QDBusPendingCallWatcher *watcher) {
140 QDBusPendingReply<QString> reply = *watcher;
141 if (reply.isError()) {
142 MH_ERROR() <<
"Error requesting system state:" << reply.error().message();
144 m_cookieStore.insert(state, reply.value());
147 watcher->deleteLater();
154 if (state == media::power::SystemState::suspend) {
157 const auto i = m_cookieStore.find(state);
158 if (i == m_cookieStore.end()) {
162 QDBusPendingCall call = m_interface.asyncCall(
"clearSysState", i.value());
163 auto watcher =
new QDBusPendingCallWatcher(call);
164 QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
165 [
this, state, cb](QDBusPendingCallWatcher *watcher) {
166 QDBusPendingReply<void> reply = *watcher;
167 if (reply.isError()) {
168 MH_ERROR() <<
"Error releasing system state:" << reply.error().message();
170 m_cookieStore.remove(state);
173 watcher->deleteLater();
178 QDBusInterface m_interface;
179 QHash<SystemState, QString> m_cookieStore;
189 int m_displayLockCount = 0;
191 int m_systemLockCount = 0;
192 QTimer m_displayReleaseTimer;
204 m_displayReleaseTimer.setSingleShot(
true);
205 m_displayReleaseTimer.setTimerType(Qt::VeryCoarseTimer);
207 m_displayReleaseTimer.callOnTimeout(q, [
this, q]() {
208 auto emitReleaseSignal = [q]() {
225 static QWeakPointer<CreatableStateController> weakRef;
227 QSharedPointer<CreatableStateController> strong = weakRef.toStrongRef();
229 strong = QSharedPointer<CreatableStateController>::create();
238 if (++d->m_displayLockCount == 1) {
239 MH_INFO(
"Requesting new display wakelock.");
240 d->m_display.requestDisplayOn([
this]() {
249 if (--d->m_displayLockCount == 0) {
250 MH_INFO(
"Clearing display wakelock.");
251 d->m_displayReleaseTimer.start();
258 if (++d->m_systemLockCount == 1) {
259 MH_INFO(
"Requesting new system wakelock.");
260 d->m_system.requestSystemState(state, [
this](
SystemState state) {
269 if (--d->m_systemLockCount == 0) {
270 MH_INFO(
"Clearing system wakelock.");
271 d->m_system.releaseSystemState(state, [
this](
SystemState state) {