File indexing completed on 2024-05-05 17:42:30

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
0003  * SPDX-FileCopyrightText: 2022 by Devin Lin <devin@kde.org>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "flashlightutil.h"
0009 
0010 #include <fcntl.h>
0011 #include <unistd.h>
0012 
0013 #include <QDebug>
0014 #include <QFileInfo>
0015 
0016 // FIXME this is hardcoded to the PinePhone for now
0017 static const char *FLASH_SYSFS_PATH = "/sys/devices/platform/led-controller/leds/white:flash/brightness";
0018 
0019 FlashlightUtil::FlashlightUtil(QObject *parent)
0020     : QObject{parent}
0021     , m_torchEnabled{false}
0022 {
0023 }
0024 
0025 void FlashlightUtil::toggleTorch()
0026 {
0027     int fd = open(FLASH_SYSFS_PATH, O_WRONLY);
0028 
0029     if (fd < 0) {
0030         qWarning() << "Unable to open file %s" << FLASH_SYSFS_PATH;
0031         return;
0032     }
0033 
0034     write(fd, m_torchEnabled ? "0" : "1", 1);
0035     close(fd);
0036     m_torchEnabled = !m_torchEnabled;
0037     Q_EMIT torchChanged(m_torchEnabled);
0038 }
0039 
0040 bool FlashlightUtil::torchEnabled() const
0041 {
0042     return m_torchEnabled;
0043 }
0044 
0045 bool FlashlightUtil::isAvailable() const
0046 {
0047     return QFileInfo::exists(FLASH_SYSFS_PATH);
0048 }