/
flashlight.cpp
99 lines (83 loc) · 2.66 KB
/
flashlight.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <QFile>
#include <QTextStream>
#include <QtDBus/QtDBus>
#include <QDebug>
#include "flashlight.h"
static const QString FLASHLIGHT_SERVICE = "com.jolla.settings.system.flashlight";
static const QString FLASHLIGHT_PATH = "/com/jolla/settings/system/flashlight";
static const QString FLASHLIGHT_INTERFACE = FLASHLIGHT_SERVICE;
FlashLight::FlashLight()
{
filenames << "/sys/class/leds/led:flash_torch/brightness"; // LG Nexus 5, Fairphone Fairphone 2
filenames << "/sys/class/leds/torch-flash/flash_light"; // Motorola Photon Q
filenames << "/sys/class/leds/torch-light0/brightness"; // Intex Aqua Fish
filenames << "/sys/kernel/debug/flash_adp1650/mode"; // Jolla
filenames << "/sys/class/leds/led:torch_0/brightness"; // Xperia X
current_state = false;
if (QDBusConnection::sessionBus().connect(FLASHLIGHT_SERVICE,
FLASHLIGHT_PATH,
FLASHLIGHT_INTERFACE,
"flashlightOnChanged",
this,
SLOT(flashlightOnChangedSlot(bool)))) {
qDebug() << "Connected to DBus flashlightOnChanged event";
}
}
void FlashLight::flashlightOnChangedSlot(bool state)
{
setstate(state);
}
void FlashLight::enable()
{
if (current_state != true)
toggle();
}
void FlashLight::disable()
{
if (current_state != false)
toggle();
}
void FlashLight::toggle()
{
if (!write_value(!current_state) && !toggleDBus()) {
emit failure();
}
}
bool FlashLight::toggleDBus() {
if (!QDBusConnection::sessionBus().isConnected()) {
return false;
}
QDBusInterface iface(FLASHLIGHT_SERVICE, FLASHLIGHT_PATH, FLASHLIGHT_INTERFACE, QDBusConnection::sessionBus());
if (iface.isValid()) {
QDBusMessage reply = iface.call("toggleFlashlight");
if (reply.type() == QDBusMessage::ReplyMessage) {
qDebug() << "Flashlight toggled through DBus";
return true;
}
}
return false;
}
bool FlashLight::state()
{
return current_state;
}
void FlashLight::setstate(bool state)
{
current_state = state;
emit stateChanged();
}
bool FlashLight::write_value(int value)
{
foreach (QString filename, filenames) {
QFile file(filename);
QTextStream out(&file);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
out << value;
file.close();
setstate(value);
qDebug() << "Flashlight toggled through" << filename;
return true;
}
}
return false;
}