From 582418bbeefb245499a282690284466c3bedef51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= Date: Wed, 27 Dec 2023 22:20:05 +0300 Subject: [PATCH] Dim screen on inactivity, then lock it --- .config/hypr/hyprland.conf | 16 ++++++++++++++-- scripts/dim-screen | 30 ++++++++++++++++++++++++++++++ scripts/lock-screen | 3 +++ scripts/undim-screen | 7 +++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100755 scripts/dim-screen create mode 100755 scripts/lock-screen create mode 100755 scripts/undim-screen diff --git a/.config/hypr/hyprland.conf b/.config/hypr/hyprland.conf index 60a284d..322adf9 100644 --- a/.config/hypr/hyprland.conf +++ b/.config/hypr/hyprland.conf @@ -32,6 +32,15 @@ monitor=HDMI-A-1,preferred,0x0,1.3 exec-once = dunst exec-once = /usr/libexec/kf5/polkit-kde-authentication-agent-1 +# Dim screen after 60 seconds of inactivity +exec-once = swayidle -w timeout 60 'dim-screen &' resume 'undim-screen' +# Lock screen after idle for 300s +exec-once = swayidle -w timeout 300 'lock-screen' +# Dim screen 5 seconds after screen is locked. no -w option here. +exec-once = swayidle timeout 5 'if pgrep -x swaylock; then dim-screen; fi' resume 'undim-screen' +# Turn monitors off 20 seconds after screen is locked +exec-once = swayidle -w timeout 20 'if pgrep -x swaylock; then hyprctl dispatch dpms off; fi' resume 'hyprctl dispatch dpms on' + # exec-once = /usr/libexec/polkit-gnome-authentication-agent-1 # Source a file (multi-file configs) @@ -219,14 +228,17 @@ bindm = $mainMod, mouse:273, resizewindow # screen -bindle=,XF86MonBrightnessUp, exec, light -A 5 -bindle=,XF86MonBrightnessDown, exec, light -U 5 +bindle=,XF86MonBrightnessUp, exec, light -A 3; light -O +bindle=,XF86MonBrightnessDown, exec, light -U 3; light -O # bindle=,XF86KbdBrightnessUp, exec, ags -r 'brightness.kbd++; indicator.kbd()' # bindle=,XF86KbdBrightnessDown, exec, ags -r 'brightness.kbd--; indicator.kbd()' # +# bindle=,XF86AudioRaiseVolume, exec, volume up bindle=,XF86AudioLowerVolume, exec, volume down bindle=,XF86AudioMute, exec, volume toggle-mute +bind = $mainMod SHIFT, q, exec, lock-screen +# bindl = $mainMod SHIFT, L, exec, sleep 1 && hyprctl dispatch dpms off windowrulev2 = stayfocused,class:(wofi) diff --git a/scripts/dim-screen b/scripts/dim-screen new file mode 100755 index 0000000..d58b3f8 --- /dev/null +++ b/scripts/dim-screen @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +# Gradually decrease brightness level. Thanks ChatGPT +import subprocess +import time + +def set_brightness(brightness): + subprocess.call(['light', '-S', str(brightness)]) + +def smooth_dimming(initial_brightness, final_brightness, duration, steps): + brightness_step = (initial_brightness - final_brightness) / steps + sleep_time = duration / steps + + for i in range(steps + 1): + current_brightness = round(initial_brightness - i * brightness_step,2) + set_brightness(current_brightness) + print(current_brightness) + time.sleep(sleep_time) + + set_brightness(final_brightness) + +# Save brighness value for later +subprocess.call(['light', '-O']) + +initial_brightness = float(subprocess.getoutput('light')) +final_brightness = 0 if initial_brightness < 20 else 5 +dimming_duration = 0.3 if initial_brightness < 20 else .5 +num_steps = 20 if initial_brightness < 20 else 50 + +smooth_dimming(initial_brightness, final_brightness, dimming_duration, num_steps) diff --git a/scripts/lock-screen b/scripts/lock-screen new file mode 100755 index 0000000..6bc120e --- /dev/null +++ b/scripts/lock-screen @@ -0,0 +1,3 @@ +#!/bin/bash + +swaylock -f diff --git a/scripts/undim-screen b/scripts/undim-screen new file mode 100755 index 0000000..db45e93 --- /dev/null +++ b/scripts/undim-screen @@ -0,0 +1,7 @@ +#!/bin/bash + +if pid=$(pgrep -x "dim-screen"); then + kill $pid +fi + +light -I