Unity 8
Powerd.cpp
1 /*
2  * Copyright (C) 2013 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Author: Michael Terry <michael.terry@canonical.com>
17  */
18 
19 #include "Powerd.h"
20 #include <QDBusPendingCall>
21 
22 void autoBrightnessChanged(GSettings *settings, const gchar *key, QDBusInterface *unityScreen)
23 {
24  bool value = g_settings_get_boolean(settings, key);
25  unityScreen->asyncCall(QStringLiteral("userAutobrightnessEnable"), QVariant(value));
26 }
27 
28 void activityTimeoutChanged(GSettings *settings, const gchar *key, QDBusInterface *unityScreen)
29 {
30  int value = g_settings_get_uint(settings, key);
31  unityScreen->asyncCall(QStringLiteral("setInactivityTimeouts"), QVariant(value), QVariant(-1));
32 }
33 
34 void dimTimeoutChanged(GSettings *settings, const gchar *key, QDBusInterface *unityScreen)
35 {
36  int value = g_settings_get_uint(settings, key);
37  unityScreen->asyncCall(QStringLiteral("setInactivityTimeouts"), QVariant(-1), QVariant(value));
38 }
39 
40 Powerd::Powerd(QObject* parent)
41  : QObject(parent),
42  unityScreen(nullptr),
43  cachedStatus(Status::On)
44 {
45  unityScreen = new QDBusInterface(QStringLiteral("com.canonical.Unity.Screen"),
46  QStringLiteral("/com/canonical/Unity/Screen"),
47  QStringLiteral("com.canonical.Unity.Screen"),
48  QDBusConnection::SM_BUSNAME(), this);
49 
50  unityScreen->connection().connect(QStringLiteral("com.canonical.Unity.Screen"),
51  QStringLiteral("/com/canonical/Unity/Screen"),
52  QStringLiteral("com.canonical.Unity.Screen"),
53  QStringLiteral("DisplayPowerStateChange"),
54  this,
55  SLOT(handleDisplayPowerStateChange(int, int)));
56 
57  systemSettings = g_settings_new("com.ubuntu.touch.system");
58  g_signal_connect(systemSettings, "changed::auto-brightness", G_CALLBACK(autoBrightnessChanged), unityScreen);
59  g_signal_connect(systemSettings, "changed::activity-timeout", G_CALLBACK(activityTimeoutChanged), unityScreen);
60  g_signal_connect(systemSettings, "changed::dim-timeout", G_CALLBACK(dimTimeoutChanged), unityScreen);
61  autoBrightnessChanged(systemSettings, "auto-brightness", unityScreen);
62  activityTimeoutChanged(systemSettings, "activity-timeout", unityScreen);
63  dimTimeoutChanged(systemSettings, "dim-timeout", unityScreen);
64 }
65 
66 Powerd::~Powerd()
67 {
68  g_signal_handlers_disconnect_by_data(systemSettings, unityScreen);
69  g_object_unref(systemSettings);
70 }
71 
72 Powerd::Status Powerd::status() const
73 {
74  return cachedStatus;
75 }
76 
77 void Powerd::setStatus(Powerd::Status status, DisplayStateChangeReason reason)
78 {
79  unityScreen->asyncCall(QStringLiteral("setScreenPowerMode"),
80  status == Powerd::On ? "on" : "off",
81  static_cast<qint32>(reason));
82 }
83 
84 void Powerd::handleDisplayPowerStateChange(int status, int reason)
85 {
86  if (cachedStatus != (Status)status) {
87  cachedStatus = (Status)status;
88  Q_EMIT statusChanged((DisplayStateChangeReason)reason);
89  }
90 }