@@ -0,0 +1,90 @@
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 "LegacyLights.h"
20+#include <QtCore/QDebug>
21+
22+extern "C" {
23+#include "android-hardware.h"
24+#include <string.h>
25+}
26+
27+LegacyLights::~LegacyLights()
28+{
29+ if (m_lightDevice) {
30+ hw_device_t* device = (hw_device_t*) m_lightDevice;
31+ device->close(device);
32+ }
33+}
34+
35+bool LegacyLights::init()
36+{
37+ if (m_lightDevice) {
38+ return true;
39+ }
40+
41+ int err;
42+ hw_module_t* module;
43+
44+ err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
45+ if (err == 0) {
46+ hw_device_t* device;
47+ err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
48+ if (err == 0) {
49+ m_lightDevice = (light_device_t*)device;
50+ turnOff();
51+ return true;
52+ } else {
53+ qWarning() << "Failed to access notification lights";
54+ }
55+ } else {
56+ qWarning() << "Failed to initialize lights hardware.";
57+ }
58+ return false;
59+}
60+
61+void LegacyLights::turnOn()
62+{
63+ // pulse
64+ light_state_t state;
65+ memset(&state, 0, sizeof(light_state_t));
66+ state.color = m_color.rgba();
67+ state.flashMode = LIGHT_FLASH_TIMED;
68+ state.flashOnMS = m_onMs;
69+ state.flashOffMS = m_offMs;
70+ state.brightnessMode = BRIGHTNESS_MODE_USER;
71+
72+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
73+ qWarning() << "Failed to turn the light off";
74+ }
75+}
76+
77+void LegacyLights::turnOff()
78+{
79+ light_state_t state;
80+ memset(&state, 0, sizeof(light_state_t));
81+ state.color = 0x00000000;
82+ state.flashMode = LIGHT_FLASH_NONE;
83+ state.flashOnMS = 0;
84+ state.flashOffMS = 0;
85+ state.brightnessMode = 0;
86+
87+ if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
88+ qWarning() << "Failed to turn the light off";
89+ }
90+}