2022-03-17 04:07:29 +00:00
|
|
|
/*
|
2022-03-20 18:35:57 +00:00
|
|
|
* SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
|
2022-03-17 04:07:29 +00:00
|
|
|
* SPDX-FileCopyrightText: 2022 by Devin Lin <devin@kde.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "flashlightutil.h"
|
|
|
|
|
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
#include <cstring>
|
2022-03-17 04:07:29 +00:00
|
|
|
#include <fcntl.h>
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
#include <libudev.h>
|
2022-03-17 04:07:29 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
2022-07-06 23:15:01 +00:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
#define TORCH_SUBSYSTEM "leds"
|
2022-03-17 04:07:29 +00:00
|
|
|
|
|
|
|
|
FlashlightUtil::FlashlightUtil(QObject *parent)
|
|
|
|
|
: QObject{parent}
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
, m_device{nullptr}
|
|
|
|
|
, m_isAvailable{false}
|
2022-03-17 04:07:29 +00:00
|
|
|
{
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
findTorchDevice();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FlashlightUtil::~FlashlightUtil()
|
|
|
|
|
{
|
|
|
|
|
if (m_device != nullptr) {
|
|
|
|
|
udev_device_unref(m_device);
|
|
|
|
|
}
|
2022-03-17 04:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlashlightUtil::toggleTorch()
|
|
|
|
|
{
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
if (!isAvailable()) {
|
|
|
|
|
qWarning() << "Flashlight not available";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-17 04:07:29 +00:00
|
|
|
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
int ret = udev_device_set_sysattr_value(m_device, "brightness", const_cast<char *>(m_torchEnabled ? "0" : m_maxBrightness));
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
qWarning() << "Flashlight can't be toggled";
|
2022-03-17 04:07:29 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_torchEnabled = !m_torchEnabled;
|
|
|
|
|
Q_EMIT torchChanged(m_torchEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlashlightUtil::torchEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_torchEnabled;
|
|
|
|
|
}
|
2022-07-06 23:15:01 +00:00
|
|
|
|
|
|
|
|
bool FlashlightUtil::isAvailable() const
|
|
|
|
|
{
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
return m_isAvailable;
|
2022-07-06 23:15:01 +00:00
|
|
|
}
|
Flashlight : Use udev instead of hardcoded pinephone device file
# Goal of this patch
Use udev to change or detect flash device instead of hardcoded pinephone device file.
Inspired by phosh flash manager because it works in many devices. https://gitlab.gnome.org/World/Phosh/phosh/-/blob/main/src/torch-manager.c?ref_type=heads#L168-198
# Remaining work
- [x] Minimal project for testing
- [x] Try to build for pmOS to test in my device (OP6)
- [x] Need udev rules to work (Require write permission in brightness)
# Stabilization
- [x] read permission removed in max_brightness to check if no crash
> "Failed to read max_brightness from udev device" in log
- [x] read permission removed in brightness to check if no crash
> "Failed to read brightness from udev device" in log
- [x] "Break" match in my side to check if no crash occured when no device found
> "No flashlight found" in log
# Minimal project
It run in my device perfectly, it find device, get current value and max value and toggle flashlight. It require **root** permissions to write in device file.
```cpp
#include <iostream>
#include <cstring>
#include <libudev.h>
#define TORCH_SUBSYSTEM "leds"
int main() {
struct udev* udev = udev_new();
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
// Use to find all devices in subsystem "leds"
// And use match sysname to filter only flash or torch
// Example:
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/white:flash On OP6
// - /sys/devices/platform/soc@0/c440000.spmi/spmi-0/0-03/c440000.spmi:pmic@3:led-controller@d300/leds/yellow:flash On OP6
// - /sys/devices/platform/led-controller/leds/white:flash/brightness On pinephone
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
udev_enumerate_add_match_sysname(enumerate, "*:torch");
udev_enumerate_add_match_sysname(enumerate, "*:flash");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
if (entry == nullptr) {
std::cout << "No flashlight found" << std::endl;
return 1;
}
const char *path = udev_list_entry_get_name(entry);
struct udev_device *torch = udev_device_new_from_syspath(udev, path);
const char *max_brightness = udev_device_get_sysattr_value(torch, "max_brightness");
const char *brightness = udev_device_get_sysattr_value(torch, "brightness");
bool enabled = std::strcmp(brightness, "0") != 0;
udev_device_set_sysattr_value(torch, "brightness", const_cast<char*>(enabled ? "0" : max_brightness));
udev_device_unref(torch);
udev_enumerate_unref(enumerate);
udev_unref(udev);
return 0;
}
```
Build with `g++ torch.cpp -ludev -o torch`
Run with `sudo ./torch`
# Important info
- **udev** dependencies added
- destructor for flashlightutil added to unref udev_device
# Require udev rules
Files `/etc/udev/rules.d/99-flashlight.rules`
```bash
# Allow everyone to set brightness of flashlight (Required for plasma-mobile flashlightplugin)
SUBSYSTEMS=="leds", KERNEL=="*:flash|*:torch", RUN+="/bin/chmod 666 %S%p/brightness"
```
pmOS : See to add depends `eudev` and makedepends `eudev-dev`
2023-12-31 17:11:28 +00:00
|
|
|
|
|
|
|
|
void FlashlightUtil::findTorchDevice()
|
|
|
|
|
{
|
|
|
|
|
if (m_device != nullptr) {
|
|
|
|
|
udev_device_unref(m_device);
|
|
|
|
|
}
|
|
|
|
|
m_device = nullptr;
|
|
|
|
|
m_isAvailable = false;
|
|
|
|
|
|
|
|
|
|
struct udev *udev = udev_new();
|
|
|
|
|
struct udev_enumerate *enumerate = udev_enumerate_new(udev);
|
|
|
|
|
|
|
|
|
|
udev_enumerate_add_match_subsystem(enumerate, TORCH_SUBSYSTEM);
|
|
|
|
|
udev_enumerate_add_match_sysname(enumerate, "*:torch");
|
|
|
|
|
udev_enumerate_add_match_sysname(enumerate, "*:flash");
|
|
|
|
|
udev_enumerate_scan_devices(enumerate);
|
|
|
|
|
|
|
|
|
|
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
|
|
|
|
|
struct udev_list_entry *entry = udev_list_entry_get_next(devices);
|
|
|
|
|
|
|
|
|
|
if (entry == nullptr) {
|
|
|
|
|
qWarning() << "No flashlight found";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *path = udev_list_entry_get_name(entry);
|
|
|
|
|
|
|
|
|
|
if (path == nullptr) {
|
|
|
|
|
qWarning() << "Failed to get path from udev entry";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct udev_device *device = udev_device_new_from_syspath(udev, path);
|
|
|
|
|
|
|
|
|
|
if (device == nullptr) {
|
|
|
|
|
qWarning() << "Failed to get udev device";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *maxBrightness = udev_device_get_sysattr_value(device, "max_brightness");
|
|
|
|
|
|
|
|
|
|
if (maxBrightness == nullptr) {
|
|
|
|
|
qWarning() << "Failed to read max_brightness from udev device";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *brightness = udev_device_get_sysattr_value(device, "brightness");
|
|
|
|
|
|
|
|
|
|
if (brightness == nullptr) {
|
|
|
|
|
qWarning() << "Failed to read brightness from udev device";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_maxBrightness = maxBrightness;
|
|
|
|
|
m_device = device;
|
|
|
|
|
m_isAvailable = true;
|
|
|
|
|
m_torchEnabled = std::strcmp(brightness, "0") != 0;
|
|
|
|
|
|
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
|
udev_unref(udev);
|
|
|
|
|
}
|