on master help sign in
unity8/plugins/Powerd/Powerd.cpp
90 lines 3.7 kB history raw
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 */
19#include "Powerd.h"
20#include <QDBusPendingCall>
22void autoBrightnessChanged(GSettings *settings, const gchar *key, QDBusInterface *unityScreen)
24 bool value = g_settings_get_boolean(settings, key);
25 unityScreen->asyncCall(QStringLiteral("userAutobrightnessEnable"), QVariant(value));
28void activityTimeoutChanged(GSettings *settings, const gchar *key, QDBusInterface *unityScreen)
30 int value = g_settings_get_uint(settings, key);
31 unityScreen->asyncCall(QStringLiteral("setInactivityTimeouts"), QVariant(value), QVariant(-1));
34void dimTimeoutChanged(GSettings *settings, const gchar *key, QDBusInterface *unityScreen)
36 int value = g_settings_get_uint(settings, key);
37 unityScreen->asyncCall(QStringLiteral("setInactivityTimeouts"), QVariant(-1), QVariant(value));
40Powerd::Powerd(QObject* parent)
41 : QObject(parent),
42 unityScreen(nullptr),
43 cachedStatus(Status::On)
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);
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)));
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);
66Powerd::~Powerd()
68 g_signal_handlers_disconnect_by_data(systemSettings, unityScreen);
69 g_object_unref(systemSettings);
72Powerd::Status Powerd::status() const
74 return cachedStatus;
77void Powerd::setStatus(Powerd::Status status, DisplayStateChangeReason reason)
79 unityScreen->asyncCall(QStringLiteral("setScreenPowerMode"),
80 status == Powerd::On ? "on" : "off",
81 static_cast<qint32>(reason));
84void Powerd::handleDisplayPowerStateChange(int status, int reason)
86 if (cachedStatus != (Status)status) {
87 cachedStatus = (Status)status;
88 Q_EMIT statusChanged((DisplayStateChangeReason)reason);
89 }