nyaa
This commit is contained in:
parent
0ab206e48a
commit
16e4f955db
56
battery.scm
Normal file
56
battery.scm
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
;;; Battery monitoring daemon script.
|
||||||
|
|
||||||
|
(import shell)
|
||||||
|
|
||||||
|
(import srfi-1)
|
||||||
|
|
||||||
|
;;; Constant values.
|
||||||
|
|
||||||
|
;;; Battery level control interval, in seconds.
|
||||||
|
|
||||||
|
(define +check-interval+ 5)
|
||||||
|
|
||||||
|
;;; Path to the virtual file containing the battery percentage number.
|
||||||
|
|
||||||
|
(define +capacity-path+ "/sys/class/power_supply/BAT1/capacity")
|
||||||
|
|
||||||
|
;;; Association list of threshold levels and formatting functions.
|
||||||
|
|
||||||
|
(define +thresholds+
|
||||||
|
`((20 . ,(lambda (battery-level)
|
||||||
|
(string-append "Battery level low: " (number->string battery-level) "%.")))
|
||||||
|
(5 . ,(lambda (battery-level)
|
||||||
|
(string-append "Battery level critical: " (number->string battery-level) "%.")))))
|
||||||
|
|
||||||
|
;;; Utility functions.
|
||||||
|
|
||||||
|
;;; Get the battery level, return it as a number.
|
||||||
|
|
||||||
|
(define (get-battery-level)
|
||||||
|
(with-input-from-file +capacity-path+ read))
|
||||||
|
|
||||||
|
;;; Method to display a notification dialog.
|
||||||
|
|
||||||
|
(define (user-notify message)
|
||||||
|
(run ("notify-send '" ,message "'")))
|
||||||
|
|
||||||
|
;;; Core daemon loop.
|
||||||
|
|
||||||
|
(define (core previous-level)
|
||||||
|
(let ((current-level (get-battery-level)))
|
||||||
|
(let ((crossed-thresholds
|
||||||
|
(filter (lambda (n)
|
||||||
|
(and (>= n current-level)
|
||||||
|
(< n previous-level)))
|
||||||
|
(map car +thresholds+))))
|
||||||
|
(if (not (null? crossed-thresholds))
|
||||||
|
(user-notify ((cdr (assoc (last crossed-thresholds) +thresholds+)) current-level))))
|
||||||
|
(sleep +check-interval+)
|
||||||
|
(core current-level)))
|
||||||
|
|
||||||
|
;;; Entry point of the program.
|
||||||
|
|
||||||
|
(define (main)
|
||||||
|
(core 100))
|
||||||
|
|
||||||
|
(main)
|
Loading…
Reference in New Issue
Block a user