1
0
Fork 0
mirror of https://github.com/SinTan1729/random.git synced 2024-12-26 21:18:36 -06:00
random/brightness-by-daylight.sh

70 lines
2.8 KiB
Bash
Raw Normal View History

2022-06-26 19:22:50 -05:00
#!/bin/sh
2022-07-27 02:16:16 -05:00
# this is a script to set my external monitor's brightness
2022-06-26 19:22:50 -05:00
# to high (70%) or low (40%) automatically according to day/night.
2022-07-27 02:16:16 -05:00
# it uses ddcutil to access the monitor's settings and sunwait to know
2022-06-26 19:22:50 -05:00
# if it's day or night. It expects a file in the following format in
# the .config directory (the values should be according to location)
# ~/.config/latlong.toml
# latitude="0.000000N"
# longitude="0.000000E"
2022-07-10 21:56:21 -05:00
# scriptdir=<location-of-this-script>
2022-07-11 23:52:48 -05:00
# high=70
# low=40
2022-06-26 19:22:50 -05:00
2022-07-27 02:16:16 -05:00
# just run this script one manually to set it up to run perpetually (hopefully) using 'at'
# (some distros don't come with 'at' preinstalled, so you might need to install 'at' and enable the 'atd' service.)
2022-06-26 19:22:50 -05:00
2022-07-27 23:45:05 -05:00
# it might also be a good idea to add '<location-of-this-script> scheduler' to your DE's autostart list, or to '.profile' so that it launches on system login
2022-07-27 02:16:16 -05:00
# (sometimes I forget to turn on the monitor when I start my PC, so this is better than having a crontab)
2022-08-01 21:43:35 -05:00
# avoid running two concurrent processes
2022-08-01 21:47:07 -05:00
[ "${BRTNESSLOCKER}" != "running" ] && exec env BRTNESSLOCKER="running" flock -en "/tmp/brightness-by-daylight.lock" "$0" "$@" || :
2022-08-01 21:43:35 -05:00
# set location of the config file
2022-07-29 21:51:03 -05:00
[ -z "$XDG_CONFIG_HOME" ] && confdir="/home/sintan/.config" || confdir="$XDG_CONFIG_HOME"
2022-06-26 19:22:50 -05:00
2022-07-27 02:16:16 -05:00
# read from the config file
2022-06-26 19:22:50 -05:00
if test -f $confdir/latlong.toml ; then
source $confdir/latlong.toml
else
2022-07-27 23:46:16 -05:00
echo "No config file found!"
2022-06-26 21:53:58 -05:00
exit
2022-06-26 19:22:50 -05:00
fi
2022-07-27 02:16:16 -05:00
# get sun status
sun_status=$(sunwait poll $latitude $longitude)
2022-07-27 02:16:16 -05:00
# check if we want to access the scheduler. If yes, create a schedule using `at`
if [ "$1" == "scheduler" ]; then
2022-07-27 23:45:05 -05:00
# get the timings for twilight events and current time
sunrise_time=$(sunwait report 35.221050N 97.445938W | grep "Daylight:" | awk '{print $4}')
sunset_time=$(sunwait report 35.221050N 97.445938W | grep "Daylight:" | awk '{print $6}')
now_time=$(date "+%H:%M")
# try to figure out if it's twilight right now, then need to adjust sun_status
[ $sunrise_time == $now_time ] && sun_status="DAY"
[ $sunset_time == $now_time ] && sun_status="NIGHT"
# figure out the time of the next twilight
[ $sun_status == "DAY" ] && time_next=$sunset_time || time_next=$sunrise_time
# query all `at` tasks
task_list=$(atq | grep $time_next | awk '{print $1}')
2022-07-27 02:16:16 -05:00
# loop through all `at` entries to see if a task already exists
flag=true
for item in $task_list
do
2022-07-27 23:45:05 -05:00
[ "$(at -c $item | sed 'x;$!d')" == "$scriptdir/brightness-by-daylight.sh scheduler" ] && flag=false
done
2022-07-27 23:45:05 -05:00
# create the schedule
$flag && at -m $time_next <<< "$scriptdir/brightness-by-daylight.sh scheduler"
fi
# do the brightness adjustment using ddcutil
[ $sun_status == "DAY" ] && target=$high || target=$low
ddcutil setvcp 10 $target
echo "Monitor brightness set to $target%, since it's $(echo $sun_status | tr '[:upper:]' '[:lower:]') time"