@@ -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+