aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2018-04-14 22:12:57 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2018-04-14 22:12:57 +0800
commitef269036a9c21ee8ec5966f0662f4b6b328226ec (patch)
treeed83ff0af7c48b8d576fd0fcfaf9f45102f6cae5
parentdd96de7379d68659eed8b4c795da1e4b57d3e734 (diff)
downloadlightsd-ef269036a9c21ee8ec5966f0662f4b6b328226ec.tar.xz
Added forced adjustment and minimum brightness.
Documentation(?)
-rw-r--r--ChangeLog4
-rw-r--r--README.md40
-rw-r--r--brightness_ctrl.cpp10
-rw-r--r--brightness_ctrl.hpp4
-rw-r--r--lightsd.conf6
-rw-r--r--main.cpp7
-rw-r--r--sensors.cpp2
7 files changed, 65 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index b32a3f7..e2d76a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2018-04-14 0.0.3
+Added forced adjustment and minimum brightness.
+Documentation(?)
+
2018-04-10 0.0.2
Hopefully fixed some segmentation faults.
Added init script for OpenRC.
diff --git a/README.md b/README.md
index 35d6ae2..368e5d6 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,9 @@
# lightsd
`lightsd` is a small daemon to make your ~~(actually, my)~~ ambient
-light sensor on your laptop useful in a Linux <sup>interjection</sup>
-desktop without using a full desktop environment (or `systemd`).
+light sensor on your laptop useful in Linux <sup>interjection</sup>
+without using a full desktop environment (or `systemd`). It even works
+in a tty!
This service watches the readings from the ambient light sensor and
control the backlight of the screen and keyboard. It also creates a
@@ -21,11 +22,21 @@ at any time. The author uses Gentoo. _Very_ shitty code.
As this daemon manipulates sysfs, IT ONLY RUNS AS ROOT!
+# Dependencies
+ - CMake
+ - Reasonable new gcc or clang release that support C++17
+ (Don't use trunk though as I'm still using `std::experimental::filesystem::v1`)
+
# Building
-Building _requires_ C++17. Just `mkdir build && cd build && cmake .. && make`.
+Just `mkdir build && cd build && cmake .. && make`.
+
+# Installing
+`sudo make install`. If you are using `OpenRC`, an init script is also installed.
+Sorry to `systemd` users but my only computer running `systemd` does not have
+iio sensors.
# Documentation
-None. The code documentes itself.
+None. The code documents itself.
## fifo usage
- `u [x=5,0<x<=100]`
@@ -33,10 +44,27 @@ Makes lcd x% brighter.
- `d [x=5,0<x<=100]`
Makes lcd x% darker.
- `s <x,-100<=x<=100>`
-Set relative brightness of lcd.
+Sets relative brightness of lcd.
- `r`
-Reset relative brightness of lcd, equivalent to `s 0`.
+Resets relative brightness of lcd, equivalent to `s 0`.
+- `f`
+Forces an adjustment to be made. You may want to call this when the lid
+is being opened.
The fifo is owned by `root:video` and has permission `0620` so that
everyone in the video group could potentially mess with your brightness.
Surprise!
+
+# Tested on
+ - Lenovo ThinkPad X1 Yoga 1st gen.
+
+# To-do
+ - Less segmentation faults
+ - Less data races
+ - Input sanitation
+ - Actual, _real_ logging: not printf'ing to `stdout`.
+ - More fifo commands: disabling auto adjustment, set absolute brightness etc.
+ - use iio triggers instead?
+ - auto orientation using accelerometer?
+ - hogging cpu and battery?
+ - ability to order pizza?
diff --git a/brightness_ctrl.cpp b/brightness_ctrl.cpp
index 7ad2277..97f0149 100644
--- a/brightness_ctrl.cpp
+++ b/brightness_ctrl.cpp
@@ -5,10 +5,12 @@
#define log10_n(x) ((x)<1?0:log10(x))
void BrightnessControl::_brightness_slide(int p)
{
+ //TODO: mutual exclusion
p+=offset;
if(p>100)p=100;
if(p<0)p=0;
int pbr=maxbr*p/100;
+ if(pbr<minabr)pbr=minabr;
printf("brightness adjust: %d->%d/%d\n",br,pbr,maxbr);
int d=1;if(pbr<br)d=-1;double dd=1;
while(d>0&&br+round(d*dd)<=pbr||d<0&&br+round(d*dd)>=pbr)
@@ -39,6 +41,7 @@ void BrightnessControl::set_thresh(std::vector<int> _th){thresh=_th;}
void BrightnessControl::set_value(std::vector<int> _v){value=_v;}
void BrightnessControl::set_delay(int _d){delay=_d;}
void BrightnessControl::set_trigrange(int _tr){tr=_tr;}
+void BrightnessControl::set_minabr(int _mbr){minabr=_mbr;}
void BrightnessControl::set_offset(int rel,int off)
{
@@ -48,6 +51,12 @@ void BrightnessControl::set_offset(int rel,int off)
brightness_slide(value[cur]);
}
+void BrightnessControl::force_adjust()
+{
+ cur=std::upper_bound(thresh.begin(),thresh.end(),(int)roundf(als->get_value()))
+ -thresh.begin();
+ brightness_slide(value[cur]);
+}
void BrightnessControl::on_sensor_report(float v)
{
int lb=cur>0?thresh[cur-1]:0;
@@ -73,6 +82,7 @@ void BrightnessControl::on_sensor_report(float v)
}
void BrightnessControl::brightness_slide(int p)
{
+ if(cpath.empty())return;
std::thread brth(&BrightnessControl::_brightness_slide,std::ref(*this),p);
brth.detach();
}
diff --git a/brightness_ctrl.hpp b/brightness_ctrl.hpp
index 4a94e0b..57bcffd 100644
--- a/brightness_ctrl.hpp
+++ b/brightness_ctrl.hpp
@@ -12,7 +12,7 @@ class BrightnessControl
private:
filesystem::path cpath,brpath,maxbrpath;
std::vector<int> thresh,value;
- int delay,direction,br,maxbr,tr,offset;
+ int delay,direction,br,maxbr,minabr,tr,offset;
size_t cur;
SensorALS *als;
std::mutex interrupt_m,threshnotify_m;
@@ -25,9 +25,11 @@ public:
void set_value(std::vector<int> _v);
void set_delay(int _d);
void set_trigrange(int _tr);
+ void set_minabr(int _mbr);
void set_offset(int rel,int off);
+ void force_adjust();
void on_sensor_report(float v);
void brightness_slide(int p);
void worker();
diff --git a/lightsd.conf b/lightsd.conf
index ca47344..6b39c3f 100644
--- a/lightsd.conf
+++ b/lightsd.conf
@@ -6,12 +6,14 @@ lcd_backlight_control=/sys/class/backlight/intel_backlight
#path to keyboard backlight control in sysfs
#expected files are max_brightness and brightness
+#leave empty or comment out if your laptop does not have keyboard backlight
kbd_backlight_control=/sys/bus/platform/devices/thinkpad_acpi/leds/tpacpi::kbd_backlight
#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
+#should have one more element than the threshold list
lcd_backlight_values=3,5,10,15,30,60,80,100
#seconds before a brightness change really takes place
@@ -24,11 +26,15 @@ lcd_backlight_control_delay=3
#threshold before using
lcd_backlight_trigger_range=2
+#minimum _absolute_ value for lcd backlight
+lcd_backlight_min_value=1
+
#same as their lcd conterpart
kbd_backlight_thresholds=100
kbd_backlight_values=50,0
kbd_backlight_control_delay=5
kbd_backlight_trigger_range=2
+#kbd_backlight_min_value=0
#where to create the fifo for remote control
command_fifo_path=/tmp/lightsd.cmd.fifo
diff --git a/main.cpp b/main.cpp
index 4f20be5..d46a73a 100644
--- a/main.cpp
+++ b/main.cpp
@@ -66,6 +66,7 @@ void load_config()
}
if(sv[0]=="lcd_backlight_control_delay")lcd.set_delay(atoi(sv[1].c_str()));
if(sv[0]=="lcd_backlight_trigger_range")lcd.set_trigrange(atoi(sv[1].c_str()));
+ if(sv[0]=="lcd_backlight_min_value")lcd.set_minabr(atoi(sv[1].c_str()));
if(sv[0]=="kbd_backlight_thresholds")
{
std::vector<std::string> vals;
@@ -84,6 +85,7 @@ void load_config()
}
if(sv[0]=="kbd_backlight_control_delay")kbd.set_delay(atoi(sv[1].c_str()));
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];
}
delete[] buf;
@@ -144,6 +146,11 @@ void command_thread()
}
if(cav[0]=="s")if(cav.size()>1)lcd.set_offset(0,atoi(cav[1].c_str()));
if(cav[0]=="r")lcd.set_offset(0,0);
+ if(cav[0]=="f")
+ {
+ lcd.force_adjust();
+ kbd.force_adjust();
+ }
}
fclose(fifo_f);
fifo_f=fopen(fifo_path.c_str(),"r");
diff --git a/sensors.cpp b/sensors.cpp
index 3b6b1e0..d2166be 100644
--- a/sensors.cpp
+++ b/sensors.cpp
@@ -99,7 +99,7 @@ void SensorBase::enable_scan_element(std::string elem)
enabled_scan_elem.insert(
std::upper_bound(enabled_scan_elem.begin(),enabled_scan_elem.end(),
std::make_tuple(idx,elem_base,st),
- [](const auto&a,const auto&b)->bool{return std::get<0>(a)<std::get<0>(b);}
+ [](const auto&a,const auto&b){return std::get<0>(a)<std::get<0>(b);}
),std::make_tuple(idx,elem_base,st)
);
}