Unity 8
LegacyLights.cpp
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  * Copyright (C) 2021 UBports Foundation.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Renato Araujo Oliveira Filho <renato.filho@canonical.com>
18  */
19 
20 #include "LegacyLights.h"
21 #include <QtCore/QDebug>
22 
23 extern "C" {
24 #include "android-hardware.h"
25 #include <string.h>
26 }
27 
28 LegacyLights::~LegacyLights()
29 {
30  if (m_lightDevice) {
31  hw_device_t* device = (hw_device_t*) m_lightDevice;
32  device->close(device);
33  }
34 }
35 
36 bool LegacyLights::init()
37 {
38  if (m_lightDevice) {
39  return true;
40  }
41 
42  int err;
43  hw_module_t* module;
44 
45  err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
46  if (err == 0) {
47  hw_device_t* device;
48  err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
49  if (err == 0) {
50  m_lightDevice = (light_device_t*)device;
51  turnOff();
52  return true;
53  } else {
54  qWarning() << "Failed to access notification lights";
55  }
56  } else {
57  qWarning() << "Failed to initialize lights hardware.";
58  }
59  return false;
60 }
61 
62 void LegacyLights::turnOn()
63 {
64  // pulse
65  light_state_t state;
66  memset(&state, 0, sizeof(light_state_t));
67  state.color = m_color.rgba();
68  state.flashMode = LIGHT_FLASH_TIMED;
69  state.flashOnMS = m_onMs;
70  state.flashOffMS = m_offMs;
71  state.brightnessMode = BRIGHTNESS_MODE_USER;
72 
73  if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
74  qWarning() << "Failed to turn the light off";
75  }
76 }
77 
78 void LegacyLights::turnOff()
79 {
80  light_state_t state;
81  memset(&state, 0, sizeof(light_state_t));
82  state.color = 0x00000000;
83  state.flashMode = LIGHT_FLASH_NONE;
84  state.flashOnMS = 0;
85  state.flashOffMS = 0;
86  state.brightnessMode = 0;
87 
88  if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
89  qWarning() << "Failed to turn the light off";
90  }
91 }