on xenial help sign in
Add Power plugin to watch powerd signals
commit4f6ff6993ad4f17cac83e853d72cca868c223bf9 authorMichael Terry <michael.terry@canonical.com> dateFri 2 Aug 2013, 11:59 (13 years ago) parentd548a31
8 files changed+200−17
Shell.qml+36−17 plugins/CMakeLists.txt+1 plugins/Power/CMakeLists.txt+13 plugins/Power/Power.cpp+36 plugins/Power/Power.h+40 plugins/Power/plugin.cpp+36 plugins/Power/plugin.h+35 plugins/Power/qmldir+3
Shell.qml+36−17
@@ -19,6 +19,7 @@ import Ubuntu.Application 0.1
1919import Ubuntu.Components 0.1
2020import Ubuntu.Gestures 0.1
2121import LightDM 0.1 as LightDM
22+import Power 0.1
2223import "Dash"
2324import "Greeter"
2425import "Launcher"
@@ -420,9 +421,6 @@ FocusScope {
420421
421422 dragHandleWidth: shell.edgeSize
422423
423− property var previousMainApp: null
424− property var previousSideApp: null
425−
426424 onShownChanged: {
427425 if (shown) {
428426 lockscreen.reset();
@@ -432,20 +430,6 @@ FocusScope {
432430 LightDM.Greeter.authenticate(LightDM.Users.data(0, LightDM.UserRoles.NameRole));
433431 }
434432 greeter.forceActiveFocus();
435− // FIXME: *FocusedApplication are not updated when unfocused, hence the need to check whether
436− // the stage was actually shown
437− if (mainStage.fullyShown) greeter.previousMainApp = applicationManager.mainStageFocusedApplication;
438− if (sideStage.fullyShown) greeter.previousSideApp = applicationManager.sideStageFocusedApplication;
439− applicationManager.unfocusCurrentApplication();
440− } else {
441− if (greeter.previousMainApp) {
442− applicationManager.focusApplication(greeter.previousMainApp);
443− greeter.previousMainApp = null;
444− }
445− if (greeter.previousSideApp) {
446− applicationManager.focusApplication(greeter.previousSideApp);
447− greeter.previousSideApp = null;
448− }
449433 }
450434 }
451435
@@ -467,6 +451,41 @@ FocusScope {
467451 blockInput: greeter.shown || lockscreen.shown
468452 }
469453
454+ Connections {
455+ id: powerConnection
456+ target: Power
457+
458+ property var previousMainApp: null
459+ property var previousSideApp: null
460+
461+ function setFocused(focused) {
462+ if (!focused) {
463+ // FIXME: *FocusedApplication are not updated when unfocused, hence the need to check whether
464+ // the stage was actually shown
465+ if (mainStage.fullyShown) powerConnection.previousMainApp = applicationManager.mainStageFocusedApplication;
466+ if (sideStage.fullyShown) powerConnection.previousSideApp = applicationManager.sideStageFocusedApplication;
467+ applicationManager.unfocusCurrentApplication();
468+ } else {
469+ if (powerConnection.previousMainApp) {
470+ applicationManager.focusApplication(powerConnection.previousMainApp);
471+ powerConnection.previousMainApp = null;
472+ }
473+ if (powerConnection.previousSideApp) {
474+ applicationManager.focusApplication(powerConnection.previousSideApp);
475+ powerConnection.previousSideApp = null;
476+ }
477+ }
478+ }
479+
480+ onPowerStateChange: {
481+ if (state == 0) { // suspend
482+ powerConnection.setFocused(false);
483+ } else if (state == 1) { // active
484+ powerConnection.setFocused(true);
485+ }
486+ }
487+ }
488+
470489 Item {
471490 id: overlay
472491
plugins/CMakeLists.txt+1
@@ -4,3 +4,4 @@ add_subdirectory(Unity)
44add_subdirectory(HudClient)
55add_subdirectory(LightDM)
66add_subdirectory(ListViewWithPageHeader)
7+add_subdirectory(Power)
plugins/Power/CMakeLists.txtnew file+13
@@ -0,0 +1,13 @@
1+include(Plugins)
2+
3+add_definitions(-DSM_BUSNAME=systemBus)
4+
5+add_library(Power-qml MODULE
6+ plugin.cpp
7+ Power.cpp
8+ )
9+
10+qt5_use_modules(Power-qml DBus Qml)
11+
12+export_qmlfiles(Power Power)
13+export_qmlplugin(Power 0.1 Power TARGETS Power-qml)
plugins/Power/Power.cppnew file+36
@@ -0,0 +1,36 @@
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 "Power.h"
20+
21+Power::Power(QObject* parent)
22+ : QObject(parent),
23+ powerd(NULL)
24+{
25+ powerd = new QDBusInterface("com.canonical.powerd",
26+ "/com/canonical/powerd",
27+ "com.canonical.powerd",
28+ QDBusConnection::SM_BUSNAME(), this);
29+
30+ powerd->connection().connect("com.canonical.powerd",
31+ "/com/canonical/powerd",
32+ "com.canonical.powerd",
33+ "SysPowerStateChange",
34+ this,
35+ SIGNAL(powerStateChange(int)));
36+}
plugins/Power/Power.hnew file+40
@@ -0,0 +1,40 @@
1+/*
2+ * Copyright (C) 2012,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+ * Authors: Gerry Boland <gerry.boland@canonical.com>
17+ * Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#ifndef UNITY_POWER_H
21+#define UNITY_POWER_H
22+
23+#include <QtCore/QObject>
24+#include <QtDBus/QDBusInterface>
25+
26+class Power: public QObject
27+{
28+ Q_OBJECT
29+
30+public:
31+ explicit Power(QObject *parent = 0);
32+
33+Q_SIGNALS:
34+ void powerStateChange(int state);
35+
36+private:
37+ QDBusInterface *powerd;
38+};
39+
40+#endif
plugins/Power/plugin.cppnew file+36
@@ -0,0 +1,36 @@
1+/*
2+ * Copyright (C) 2012,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+ * Authors: Gerry Boland <gerry.boland@canonical.com>
17+ * Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#include "plugin.h"
21+#include "Power.h"
22+
23+#include <QtQml/QtQml>
24+
25+static QObject *power_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
26+{
27+ Q_UNUSED(engine)
28+ Q_UNUSED(scriptEngine)
29+ return new Power();
30+}
31+
32+void PowerPlugin::registerTypes(const char *uri)
33+{
34+ Q_ASSERT(uri == QLatin1String("Power"));
35+ qmlRegisterSingletonType<Power>(uri, 0, 1, "Power", power_provider);
36+}
plugins/Power/plugin.hnew file+35
@@ -0,0 +1,35 @@
1+/*
2+ * Copyright (C) 2012,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+ * Authors: Gerry Boland <gerry.boland@canonical.com>
17+ * Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#ifndef POWER_PLUGIN_H
21+#define POWER_PLUGIN_H
22+
23+#include <QtQml/QQmlEngine>
24+#include <QtQml/QQmlExtensionPlugin>
25+
26+class PowerPlugin : public QQmlExtensionPlugin
27+{
28+ Q_OBJECT
29+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
30+
31+public:
32+ void registerTypes(const char *uri);
33+};
34+
35+#endif
plugins/Power/qmldirnew file+3
@@ -0,0 +1,3 @@
1+module Power
2+plugin Power-qml
3+typeinfo plugin.qmltypes