aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2018-07-14 11:46:12 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2018-07-14 11:46:12 +0800
commitea652045bb38651d99bcc53972a524f602c3b306 (patch)
treeea7bf5f5b3f0a76faf07bfeb944b3a56f6afd527
parente5aac499a8a2026af5d5176f45be3ac2576d75cc (diff)
downloadlightsd-ea652045bb38651d99bcc53972a524f602c3b306.tar.xz
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.
-rw-r--r--CMakeLists.txt3
-rw-r--r--ChangeLog6
-rw-r--r--README.md3
-rw-r--r--lightsd.conf6
-rw-r--r--main.cpp1
-rw-r--r--sensor_als.cpp6
-rw-r--r--sensor_als.hpp2
-rw-r--r--tools/fakebl.c74
-rw-r--r--utils.cpp13
9 files changed, 107 insertions, 7 deletions
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 <cstdio>
#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<int>(dict["in_intensity_both_value"])*
std::any_cast<float>(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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+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 <cmd> -p <path>\n",s);
+ _exit(1);
+}
+int main(int argc,char **argv)
+{
+ if(argc<5)usage(argv[0]);
+ for(int i=1;i<argc-1;++i)
+ {
+ if(!strcmp(argv[i],"-c"))cmd=strdup(argv[i+1]);
+ if(!strcmp(argv[i],"-p"))path=strdup(argv[i+1]);
+ }
+ if(!cmd||!path)
+ {
+ if(cmd)free(cmd);
+ if(path)free(path);
+ usage(argv[0]);
+ }
+ if(setup_dir())return puts("error setting up fake brightness interface"),1;
+ rcmd=malloc(strlen(cmd)+16);
+ while((brf=fopen(brpath,"r")))
+ {
+ char dat[64],*eptr;
+ eptr=fgets(dat,64,brf);//unused return value
+ int v=strtol(dat,&eptr,10),r;
+ if(eptr==dat)break;
+ snprintf(rcmd,strlen(cmd)+16,cmd,v);
+ if((r=system(rcmd)))
+ printf("%s returned %d\n",rcmd,r);
+ fclose(brf);
+ }
+ remove_dir();
+ free(path);
+ free(cmd);
+ free(rcmd);
+ return 0;
+}
diff --git a/utils.cpp b/utils.cpp
index 1d26fc3..698c7eb 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -4,15 +4,18 @@
#include <cstring>
#include <cerrno>
#include <cctype>
+#include <unistd.h>
+#include <fcntl.h>
#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)