diff --git a/scripts/brightness.rkt b/scripts/brightness.rkt new file mode 100755 index 0000000..68ae6aa --- /dev/null +++ b/scripts/brightness.rkt @@ -0,0 +1,37 @@ +#!/usr/bin/env racket +#lang racket + +(current-print void) + +(define sysfs (make-parameter #t)) +(define ddc (make-parameter #t)) +(define brightness + (command-line #:program "brightness" + #:usage-help "Sets the backlight brightness of connected monitors" + #:once-each + [("-s" "--no-sysfs") "Don't set brightness via sysfs" (sysfs #f)] + [("-d" "--no-ddc") "Don't set brightness via DDC/CI" (ddc #f)] + #:args (brightness-arg) + (let ([brightness-parsed (string->number brightness-arg)]) + (unless ((and/c (>=/c 0) (<=/c 100)) brightness-parsed) + (raise-user-error "brightness argument is invalid!")) + brightness-parsed))) + +(when (sysfs) + (for ([dir (directory-list "/sys/class/backlight")]) + (printf "sysfs: ~a\n" dir) + (let* ([max-brightness (string->number (file->string (build-path dir "max_brightness")))] + [rel-brightness (exact-round (* brightness (/ max-brightness 100)))] + [brightness-path (build-path dir "brightness")]) + (display-to-file rel-brightness brightness-path #:exists 'truncate)))) + +(match* ((ddc) (find-executable-path "ddcutil")) + [(#f _) #f] + [(_ #f) #f] + [(_ ddcutil-exe) + (for ([dpy (regexp-match* #px"Display (\\d+)" + (car (process* ddcutil-exe "detect")) + #:match-select (λ (m) (string->number + (bytes->string/utf-8 (cadr m)))))]) + (printf "ddc: #~a\n" dpy) + (system* ddcutil-exe "setvcp" "10" (format "--display=~a" dpy) (number->string brightness)))]) diff --git a/setup/commands/install-scripts.rkt b/setup/commands/install-scripts.rkt index c0f5cb6..61960ad 100644 --- a/setup/commands/install-scripts.rkt +++ b/setup/commands/install-scripts.rkt @@ -9,6 +9,7 @@ (generate-cgopt-json) ;; Symlink interpreted scripts + (install-link "scripts/brightness.rkt" (bin-path "brightness")) (install-link "scripts/map-touch-display.rkt" (bin-path "map-touch-display")) (install-link "scripts/startriver.sh" (bin-path "startriver")) (install-link "scripts/sysupdate.rkt" (bin-path "sysupdate")) @@ -16,7 +17,7 @@ (install-link "scripts/use-country-mirrors.sh" (bin-path "use-country-mirrors")) (install-link "scripts/videos-duration.sh" (bin-path "videos-duration")) - ;; Compile scripts + ;; Compiled scripts (install-zig "scripts/alecor") (install-zig "scripts/hyprtool") (install-rust "scripts/i3status")