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 and screen is off.

commita0d22446fcc2ae29b2b722a2f172015f389665a1 authorRenato Araujo Oliveira Filho <renato.filho@canonical.com> dateThu 3 Jul 2014, 14:47 (12 years ago) parenta77daa1dd4acef
11 files changed+397
debian/control+2 debian/unity8-private.install+1 plugins/CMakeLists.txt+1 plugins/Lights/CMakeLists.txt+18 plugins/Lights/Lights.cpp+182 plugins/Lights/Lights.h+75 plugins/Lights/plugin.cpp+35 plugins/Lights/plugin.h+34 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 libpay1-dev,
1819 libpulse-dev,
@@ -160,6 +161,7 @@ Architecture: any
160161Multi-Arch: same
161162Pre-Depends: ${misc:Pre-Depends},
162163Depends: gsettings-ubuntu-schemas,
164+ libhardware2,
163165 libunity-core-6.0-9,
164166 pay-service,
165167 ${misc: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 Gui)
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+182
@@ -0,0 +1,182 @@
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_lightDevice(0),
31+ m_color("blue"),
32+ m_state(Lights::Off),
33+ m_onMs(1000),
34+ m_offMs(3000)
35+{
36+}
37+
38+Lights::~Lights()
39+{
40+ if (m_lightDevice) {
41+ hw_device_t* device = (hw_device_t*) m_lightDevice;
42+ device->close(device);
43+ }
44+}
45+
46+void Lights::setState(Lights::State newState)
47+{
48+ if (m_state != newState) {
49+ if (newState == Lights::On) {
50+ turnOn();
51+ } else {
52+ turnOff();
53+ }
54+ }
55+}
56+
57+Lights::State Lights::state() const
58+{
59+ return m_state;
60+}
61+
62+void Lights::setColor(const QColor &color)
63+{
64+ if (m_color != color) {
65+ m_color = color;
66+ Q_EMIT colorChanged(m_color);
67+ // FIXME: update the collor if the light is already on
68+ }
69+}
70+
71+QColor Lights::color() const
72+{
73+ return m_color;
74+}
75+
76+int Lights::onMillisec() const
77+{
78+ return m_onMs;
79+}
80+
81+void Lights::setOnMillisec(int onMs)
82+{
83+ if (m_onMs != onMs) {
84+ m_onMs = onMs;
85+ Q_EMIT onMillisecChanged(m_onMs);
86+ // FIXME: update the property if the light is already on
87+ }
88+}
89+
90+int Lights::offMillisec() const
91+{
92+ return m_offMs;
93+}
94+
95+void Lights::setOffMillisec(int offMs)
96+{
97+ if (m_offMs != offMs) {
98+ m_offMs = offMs;
99+ Q_EMIT offMillisecChanged(m_offMs);
100+ // FIXME: update the property if the light is already on
101+ }
102+}
103+
104+bool Lights::init()
105+{
106+ if (m_lightDevice) {
107+ return true;
108+ }
109+
110+ int err;
111+ hw_module_t* module;
112+
113+ err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
114+ if (err == 0) {
115+ hw_device_t* device;
116+ err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
117+ if (err == 0) {
118+ m_lightDevice = (light_device_t*)device;
119+ return true;
120+ } else {
121+ qWarning() << "Failed to access notification lights";
122+ }
123+ } else {
124+ qWarning() << "Failed to initialize lights hardware.";
125+ }
126+ return false;
127+}
128+
129+void Lights::turnOn()
130+{
131+ if (!init()) {
132+ qWarning() << "No lights device";
133+ return;
134+ }
135+
136+ if (m_state == Lights::On) {
137+ return;
138+ }
139+
140+ // pulse
141+ light_state_t state;
142+ memset(&state, 0, sizeof(light_state_t));
143+ state.color = m_color.rgba();
144+ state.flashMode = LIGHT_FLASH_TIMED;
145+ state.flashOnMS = m_onMs;
146+ state.flashOffMS = m_offMs;
147+ state.brightnessMode = BRIGHTNESS_MODE_USER;
148+
149+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
150+ qWarning() << "Failed to turn the light off";
151+ } else {
152+ m_state = Lights::On;
153+ Q_EMIT stateChanged(m_state);
154+ }
155+}
156+
157+void Lights::turnOff()
158+{
159+ if (!init()) {
160+ qWarning() << "No lights device";
161+ return;
162+ }
163+
164+ if (m_state == Lights::Off) {
165+ return;
166+ }
167+
168+ light_state_t state;
169+ memset(&state, 0, sizeof(light_state_t));
170+ state.color = 0x00000000;
171+ state.flashMode = LIGHT_FLASH_NONE;
172+ state.flashOnMS = 0;
173+ state.flashOffMS = 0;
174+ state.brightnessMode = 0;
175+
176+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
177+ qWarning() << "Failed to turn the light off";
178+ } else {
179+ m_state = Lights::Off;
180+ Q_EMIT stateChanged(m_state);
181+ }
182+}
plugins/Lights/Lights.hnew file+75
@@ -0,0 +1,75 @@
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+#include <QtGui/QColor>
24+
25+struct light_device_t;
26+
27+class Lights: public QObject
28+{
29+ Q_OBJECT
30+ Q_ENUMS(State)
31+ Q_PROPERTY(State state READ state WRITE setState NOTIFY stateChanged)
32+ Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
33+ Q_PROPERTY(int onMillisec READ onMillisec WRITE setOnMillisec NOTIFY onMillisecChanged)
34+ Q_PROPERTY(int offMillisec READ offMillisec WRITE setOffMillisec NOTIFY offMillisecChanged)
35+
36+public:
37+ enum State {
38+ Off,
39+ On,
40+ };
41+
42+ explicit Lights(QObject *parent = 0);
43+ ~Lights();
44+
45+ void setState(State newState);
46+ State state() const;
47+
48+ void setColor(const QColor &color);
49+ QColor color() const;
50+
51+ int onMillisec() const;
52+ void setOnMillisec(int onMs);
53+
54+ int offMillisec() const;
55+ void setOffMillisec(int offMs);
56+
57+Q_SIGNALS:
58+ void stateChanged(State newState);
59+ void colorChanged(const QColor &color);
60+ void onMillisecChanged(int onMs);
61+ void offMillisecChanged(int offMs);
62+
63+private:
64+ light_device_t* m_lightDevice;
65+ QColor m_color;
66+ State m_state;
67+ int m_onMs;
68+ int m_offMs;
69+
70+ bool init();
71+ void turnOff();
72+ void turnOn();
73+};
74+
75+#endif
plugins/Lights/plugin.cppnew file+35
@@ -0,0 +1,35 @@
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 "plugin.h"
20+#include "Lights.h"
21+
22+#include <QtQml/qqml.h>
23+
24+static QObject *lights_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
25+{
26+ Q_UNUSED(engine)
27+ Q_UNUSED(scriptEngine)
28+ return new Lights();
29+}
30+
31+void LightsPlugin::registerTypes(const char *uri)
32+{
33+ Q_ASSERT(uri == QLatin1String("Lights"));
34+ qmlRegisterSingletonType<Lights>(uri, 0, 1, "Lights", lights_provider);
35+}
plugins/Lights/plugin.hnew file+34
@@ -0,0 +1,34 @@
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 LIGHTS_PLUGIN_H
20+#define LIGHTS_PLUGIN_H
21+
22+#include <QtQml/QQmlEngine>
23+#include <QtQml/QQmlExtensionPlugin>
24+
25+class LightsPlugin : public QQmlExtensionPlugin
26+{
27+ Q_OBJECT
28+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
29+
30+public:
31+ void registerTypes(const char *uri);
32+};
33+
34+#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 @@ IndicatorBase {
3232 width: itemRow.width
3333 enabled: false
3434
35+ // FIXME: 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 objectName: "itemRow"
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+}