From ea652045bb38651d99bcc53972a524f602c3b306 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sat, 14 Jul 2018 11:46:12 +0800 Subject: Added a simple tool to create a fake sysfs backlight interface that invokes external commands. Some changes in the utility module to accomodate the new tool. Added an option to watch ALS readings. --- CMakeLists.txt | 3 ++- ChangeLog | 6 +++++ README.md | 3 +++ lightsd.conf | 6 ++++- main.cpp | 1 + sensor_als.cpp | 6 +++++ sensor_als.hpp | 2 ++ tools/fakebl.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.cpp | 13 +++++++---- 9 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 tools/fakebl.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b5e17e..f45cab6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,10 @@ cmake_minimum_required(VERSION 2.6) project(lightsd) set(SOURCES brightness_ctrl.cpp sensor_als.cpp sensors.cpp utils.cpp main.cpp) set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDART_REQUIRED ON) +set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Threads REQUIRED) add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(fakebl tools/fakebl.c) target_link_libraries(${PROJECT_NAME} stdc++fs) target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin) diff --git a/ChangeLog b/ChangeLog index 2c82fd3..debd3b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2018-07-11 0.0.5-r2 +Added a simple tool to create a fake sysfs backlight interface +that invokes external commands. +Some changes in the utility module to accomodate the new tool. +Added an option to watch ALS readings. + 2018-07-11 0.0.5-r1 Remove profanity. Reset the iio device buffer before using it. diff --git a/README.md b/README.md index 0916ea4..a5d1868 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,9 @@ code style. Ugly implementation (it works nevertheless). As this daemon manipulates sysfs, IT ONLY RUNS AS ROOT! +Do not use `lightsd` with GNOME. It has good chance to conflict with GNOME's +implementation `iio-sensor-proxy`. + # Dependencies - CMake - gcc 8.x or clang 6 with C++17 support diff --git a/lightsd.conf b/lightsd.conf index 7217a75..6564131 100644 --- a/lightsd.conf +++ b/lightsd.conf @@ -12,7 +12,7 @@ kbd_backlight_control=/sys/bus/platform/devices/thinkpad_acpi/leds/tpacpi::kbd_b #list of (*increasing* _integer_) threshold values from the ambient light sensor, separated by commas lcd_backlight_thresholds=10,20,50,100,500,2000,10000 -#list of (_integer_) brightnesses (percentage) corresponding to the thresholds +#list of (_integer_) brightness values (in percentage) corresponding to the thresholds #should have one more element than the threshold list lcd_backlight_values=3,5,10,15,30,60,80,100 @@ -37,4 +37,8 @@ kbd_backlight_trigger_range=2 #kbd_backlight_min_value=0 #where to create the fifo for remote control +#leave empty or comment out to disable command_fifo_path=/tmp/lightsd.cmd.fifo + +#set to true if you want the reading from als to be printed to stdout everytime it changes +als_print_value=false diff --git a/main.cpp b/main.cpp index e7f4e84..f05d3d2 100644 --- a/main.cpp +++ b/main.cpp @@ -70,6 +70,7 @@ void load_config() if(sv[0]=="kbd_backlight_trigger_range")kbd.set_trigrange(atoi(sv[1].c_str())); if(sv[0]=="kbd_backlight_min_value")kbd.set_minabr(atoi(sv[1].c_str())); if(sv[0]=="command_fifo_path")fifo_path=sv[1]; + if(sv[0]=="als_print_value")als.set_debug(sv[1]=="true"); } delete[] buf; } diff --git a/sensor_als.cpp b/sensor_als.cpp index 88c3efc..8edd41c 100644 --- a/sensor_als.cpp +++ b/sensor_als.cpp @@ -1,6 +1,11 @@ //Chris Xiong 2018 //3-Clause BSD License +#include #include "sensor_als.hpp" +void SensorALS::set_debug(bool d) +{ + debug=d; +} void SensorALS::enable_scan_elements() { enable_scan_element("both"); @@ -9,5 +14,6 @@ void SensorALS::update_values() { value=std::any_cast(dict["in_intensity_both_value"])* std::any_cast(dict["in_intensity_scale"]); + if(debug)printf("ALS reading: %.2f\n",value); } float SensorALS::get_value(){return value;} diff --git a/sensor_als.hpp b/sensor_als.hpp index a010f2d..891af55 100644 --- a/sensor_als.hpp +++ b/sensor_als.hpp @@ -7,9 +7,11 @@ class SensorALS:public SensorBase { private: float value; + bool debug=false; protected: void enable_scan_elements(); public: + void set_debug(bool d); void update_values(); float get_value(); }; diff --git a/tools/fakebl.c b/tools/fakebl.c new file mode 100644 index 0000000..dc2d9b9 --- /dev/null +++ b/tools/fakebl.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include +char *path; +char *brpath; +char *mbrpath; +char *cmd; +char *rcmd; +FILE *brf; +int setup_dir() +{ + int r=mkdir(path,0755); + brpath=malloc(strlen(path)+12); + mbrpath=malloc(strlen(path)+16); + strcpy(brpath,path); + strcpy(mbrpath,path); + strcat(brpath,"/brightness"); + strcat(mbrpath,"/max_brightness"); + r|=mkfifo(brpath,0666); + FILE* mbrf=fopen(mbrpath,"w"); + r|=(mbrf==NULL); + if(mbrf)fputs("100\n",mbrf); + fclose(mbrf); + return r; +} +void remove_dir() +{ + unlink(brpath); + unlink(mbrpath); + rmdir(path); + free(brpath); + free(mbrpath); +} +void usage(char* s) +{ + printf("usage: %s -c -p \n",s); + _exit(1); +} +int main(int argc,char **argv) +{ + if(argc<5)usage(argv[0]); + for(int i=1;i #include #include +#include +#include #include "utils.hpp" int readint(const char* path) { - FILE* f=fopen(path,"r"); - if(!f)return LOG('W',"failed to open %s for reading: %d",path,errno),0; + int fd=open(path,O_RDONLY); + if(!~fd)return LOG('W',"failed to open %s for reading: %d",path,errno),0; char buf[16]; - ignore_result(fgets(buf,16,f)); - buf[15]=0; - fclose(f); + int l=read(fd,buf,15); + if(!~l){close(fd);return 0;} + buf[l]=0; + close(fd); return atoi(buf); } float readfloat(const char* path) -- cgit v1.2.3