on xenial help sign in
Create IndicatorsLight.qml component used to control indicator led.

A blue led will pulse if the message indicator is blue.

commit5b8c50f13ac02f7f6a02a8064ccfa8e48bfe9664 authorRenato Araujo Oliveira Filho <renato.filho@canonical.com> dateFri 27 Jun 2014, 17:48 (12 years ago) parent9ecb67c
11 files changed+339
debian/control+2 debian/unity8-private.install+1 plugins/CMakeLists.txt+1 plugins/Lights/CMakeLists.txt+18 plugins/Lights/Lights.cpp+141 plugins/Lights/Lights.h+56 plugins/Lights/plugin.cpp+36 plugins/Lights/plugin.h+35 plugins/Lights/qmldir+3 qml/Panel/Indicators/DefaultIndicatorWidget.qml+8 qml/Panel/Indicators/IndicatorsLight.qml+38
debian/control+2
@@ -13,6 +13,7 @@ Build-Depends: cmake,
1313 libgles2-mesa-dev[armhf],
1414 libglib2.0-dev,
1515 libgsettings-qt-dev,
16+ libhardware-dev,
1617 libhud-client2-dev,
1718 libpulse-dev,
1819 libqmenumodel-dev (>= 0.2.7),
@@ -159,6 +160,7 @@ Architecture: any
159160Multi-Arch: same
160161Pre-Depends: ${misc:Pre-Depends},
161162Depends: gsettings-ubuntu-schemas,
163+ libhardware2,
162164 libunity-core-6.0-9,
163165 ${misc:Depends},
164166 ${shlibs:Depends},
debian/unity8-private.install+1
@@ -2,6 +2,7 @@ usr/lib/*/unity8/qml/AccountsService
22usr/lib/*/unity8/qml/Dash
33usr/lib/*/unity8/qml/HudClient
44usr/lib/*/unity8/qml/LightDM
5+usr/lib/*/unity8/qml/Lights
56usr/lib/*/unity8/qml/Powerd
67usr/lib/*/unity8/qml/SessionBroadcast
78usr/lib/*/unity8/qml/Ubuntu
plugins/CMakeLists.txt+1
@@ -14,6 +14,7 @@ endmacro()
1414add_subdirectory(AccountsService)
1515add_subdirectory(HudClient)
1616add_subdirectory(LightDM)
17+add_subdirectory(Lights)
1718add_subdirectory(Dash)
1819add_subdirectory(Powerd)
1920add_subdirectory(SessionBroadcast)
plugins/Lights/CMakeLists.txtnew file+18
@@ -0,0 +1,18 @@
1+include(FindPkgConfig)
2+
3+pkg_check_modules(ANDROID_HEADERS REQUIRED android-headers)
4+pkg_check_modules(ANDROID_HARDWARE REQUIRED libhardware)
5+
6+include_directories(
7+ ${ANDROID_HEADERS_INCLUDE_DIRS}
8+)
9+
10+add_library(Lights-qml MODULE
11+ plugin.cpp
12+ Lights.cpp
13+ )
14+
15+qt5_use_modules(Lights-qml Qml)
16+target_link_libraries(Lights-qml ${ANDROID_HARDWARE_LIBRARIES})
17+
18+add_unity8_plugin(Lights 0.1 Lights TARGETS Lights-qml)
plugins/Lights/Lights.cppnew file+141
@@ -0,0 +1,141 @@
1+/*
2+ * Copyright (C) 2014 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: Renato Araujo Oliveira Filho <renato.filho@canonical.com>
17+ */
18+
19+#include "Lights.h"
20+#include <QtCore/QDebug>
21+
22+extern "C" {
23+#include <hardware/hardware.h>
24+#include <hardware/lights.h>
25+#include <string.h>
26+}
27+
28+Lights::Lights(QObject* parent)
29+ : QObject(parent),
30+ m_state(Lights::Off),
31+ m_lightDevice(0)
32+{
33+}
34+
35+
36+Lights::~Lights()
37+{
38+ if (m_lightDevice) {
39+ hw_device_t* device = (hw_device_t*) m_lightDevice;
40+ device->close(device);
41+ }
42+}
43+
44+void Lights::setState(Lights::State newState)
45+{
46+ if (m_state != newState) {
47+ if (newState == Lights::On) {
48+ turnOn();
49+ } else {
50+ turnOff();
51+ }
52+ }
53+}
54+
55+Lights::State Lights::state() const
56+{
57+ return m_state;
58+}
59+
60+bool Lights::init()
61+{
62+ if (m_lightDevice) {
63+ return true;
64+ }
65+
66+ int err;
67+ hw_module_t* module;
68+
69+ err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
70+ if (err == 0) {
71+ hw_device_t* device;
72+ err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
73+ if (err == 0) {
74+ m_lightDevice = (light_device_t*)device;
75+ return true;
76+ } else {
77+ qWarning() << "Failt to access notification lights";
78+ }
79+ } else {
80+ qWarning() << "Failt to initialize lights hardware.";
81+ }
82+ return false;
83+}
84+
85+void Lights::turnOn()
86+{
87+ if (!init()) {
88+ qWarning() << "No lights device";
89+ return;
90+ }
91+
92+ if (m_state == Lights::On) {
93+ return;
94+ }
95+
96+ // pulse
97+ light_state_t state;
98+ memset(&state, 0, sizeof(light_state_t));
99+ state.color = 0xff0000ff; // blue
100+ state.flashMode = LIGHT_FLASH_TIMED;
101+ state.flashOnMS = 1000;
102+ state.flashOffMS = 1000;
103+ state.brightnessMode = BRIGHTNESS_MODE_USER;
104+
105+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
106+ qWarning() << "Fail to turn the light off";
107+ } else {
108+ m_state = Lights::On;
109+ Q_EMIT stateChanged(m_state);
110+ }
111+}
112+
113+void Lights::turnOff()
114+{
115+ if (!init()) {
116+ qWarning() << "No lights device";
117+ return;
118+ }
119+
120+ if (m_state == Lights::Off) {
121+ return;
122+ }
123+
124+ light_state_t state;
125+ memset(&state, 0, sizeof(light_state_t));
126+ state.color = 0x00000000;
127+ state.flashMode = LIGHT_FLASH_NONE;
128+ state.flashOnMS = 0;
129+ state.flashOffMS = 0;
130+ state.brightnessMode = 0;
131+
132+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
133+ qWarning() << "Fail to turn the light off";
134+ } else {
135+ m_state = Lights::Off;
136+ Q_EMIT stateChanged(m_state);
137+ }
138+}
139+
140+
141+
plugins/Lights/Lights.hnew file+56
@@ -0,0 +1,56 @@
1+/*
2+ * Copyright (C) 2014 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: Renato Araujo Oliveira Filho <renato.filho@canonical.com>
17+ */
18+
19+#ifndef UNITY_LIGHTS_H
20+#define UNITY_LIGHTS_H
21+
22+#include <QtCore/QObject>
23+
24+struct light_device_t;
25+
26+class Lights: public QObject
27+{
28+ Q_OBJECT
29+ Q_ENUMS(State)
30+ Q_PROPERTY(State state READ state WRITE setState NOTIFY stateChanged)
31+
32+public:
33+ enum State {
34+ Off,
35+ On,
36+ };
37+
38+ explicit Lights(QObject *parent = 0);
39+ ~Lights();
40+
41+ void setState(State newState);
42+ State state() const;
43+
44+Q_SIGNALS:
45+ void stateChanged(State newState);
46+
47+private:
48+ State m_state;
49+ light_device_t* m_lightDevice;
50+
51+ bool init();
52+ void turnOff();
53+ void turnOn();
54+};
55+
56+#endif
plugins/Lights/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 "Lights.h"
22+
23+#include <QtQml/qqml.h>
24+
25+static QObject *lights_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
26+{
27+ Q_UNUSED(engine)
28+ Q_UNUSED(scriptEngine)
29+ return new Lights();
30+}
31+
32+void LightsPlugin::registerTypes(const char *uri)
33+{
34+ Q_ASSERT(uri == QLatin1String("Lights"));
35+ qmlRegisterSingletonType<Lights>(uri, 0, 1, "Lights", lights_provider);
36+}
plugins/Lights/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 LIGHTS_PLUGIN_H
21+#define LIGHTS_PLUGIN_H
22+
23+#include <QtQml/QQmlEngine>
24+#include <QtQml/QQmlExtensionPlugin>
25+
26+class LightsPlugin : 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/Lights/qmldirnew file+3
@@ -0,0 +1,3 @@
1+module Lights
2+plugin Lights-qml
3+typeinfo Lights.qmltypes
qml/Panel/Indicators/DefaultIndicatorWidget.qml+8
@@ -32,6 +32,14 @@ Indicators.IndicatorBase {
3232 width: itemRow.width
3333 enabled: false
3434
35+ // WORKAROUND: For now we will enable led indicator support only for messaging indicator
36+ // in the future we should export a led API insted of doing that,
37+ Loader {
38+ id: indicatorLed
39+ // only load source Component if the icons contains the new message icon
40+ source: (indicatorWidget.icons && (String(indicatorWidget.icons).indexOf("indicator-messages-new") != -1)) ? Qt.resolvedUrl("IndicatorsLight.qml") : ""
41+ }
42+
3543 Row {
3644 id: itemRow
3745 width: childrenRect.width
qml/Panel/Indicators/IndicatorsLight.qmlnew file+38
@@ -0,0 +1,38 @@
1+/*
2+ * Copyright 2014 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Renato Araujo Oliveira Filho <renato@canonical.com>
18+ */
19+
20+import QtQuick 2.0
21+import Powerd 0.1
22+import Lights 0.1
23+
24+QtObject {
25+ id: root
26+
27+ Component.onDestruction: Lights.state = Lights.Off
28+
29+ // QtObject does not have children
30+ property var _connections: Connections {
31+ id: powerConnection
32+
33+ target: Powerd
34+ onDisplayPowerStateChange: {
35+ Lights.state = (status === Powerd.Off) ? Lights.On : Lights.Off
36+ }
37+ }
38+}