summaryrefslogtreecommitdiff
path: root/hyprsunset.sh
blob: 332b027e017feb000d4871b6a57c21601ee13492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash

renice -n 19 $$
ionice -c 3 -p $$

# NIGHT_GAMMA=95
NIGHT_TEMP=5500
NORMAL_TEMP=6000
SUNRISE_HOUR=6
SUNSET_HOUR=20

set -E

trap '
CMD="$BASH_COMMAND"
hyprsunset &
$BASH_COMMAND' ERR

apply_settings() {
  current_hour=$(date +%H | sed 's/^0//')
  
  if [ $current_hour -ge $SUNRISE_HOUR -a $current_hour -lt $SUNSET_HOUR ]; then
    echo "Applying day settings"
    # hyprctl hyprsunset identity
    hyprctl hyprsunset temperature $NORMAL_TEMP
  else
    echo "Applying night settings"
    # hyprctl hyprsunset gamma $NIGHT_GAMMA
    hyprctl hyprsunset temperature $NIGHT_TEMP
  fi
}

calculate_sleep_time() {
  current_hour=$(date +%H | sed 's/^0//')
  current_min=$(date +%M | sed 's/^0//')
  
  if [ $current_hour -ge $SUNRISE_HOUR -a $current_hour -lt $SUNSET_HOUR ]; then
    hours_until_sunset=$((SUNSET_HOUR - current_hour - 1))
    mins_until_sunset=$((60 - current_min))
    sleep_seconds=$(( (hours_until_sunset * 3600) + (mins_until_sunset * 60) ))
  else
    if [ $current_hour -lt $SUNRISE_HOUR ]; then
      hours_until_sunrise=$(($SUNRISE_HOUR - $current_hour - 1))
      mins_until_sunrise=$((60 - $current_min))
    else
      hours_until_sunrise=$(($SUNRISE_HOUR + 24 - $current_hour - 1))
      mins_until_sunrise=$((60 - $current_min))
    fi
    sleep_seconds=$(( (hours_until_sunrise * 3600) + (mins_until_sunrise * 60) ))
  fi
  
  echo $sleep_seconds
}

while true; do
  if [ ! -S $XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.hyprsunset.sock ]; then
    echo "Hyprsunset not running, running it via hyprctl dispatch"
    hyprctl dispatch "hl.dsp.exec_raw('hyprsunset')"
  fi
  apply_settings
  sleep_time=$(calculate_sleep_time)
  
  printf "Sleeping for %d hours and %d minutes (%d seconds)\n" $((sleep_time / 3600)) $(((sleep_time % 3600) / 60)) $sleep_time
  
  sleep $sleep_time
done