StumpWM: improved backlight changing functionality

This commit is contained in:
Marcel Kapfer 2018-09-03 08:28:07 +02:00
parent 7b7125538d
commit bc4d7e2a95
Signed by: mmk2410
GPG key ID: CADE6F0C09F21B09

View file

@ -182,26 +182,58 @@
(let ((max-backlight-file (concatenate 'string *backlight-sysfs* "max_brightness"))) (let ((max-backlight-file (concatenate 'string *backlight-sysfs* "max_brightness")))
(if (probe-file max-backlight-file) (if (probe-file max-backlight-file)
(with-open-file (in max-backlight-file) (with-open-file (in max-backlight-file)
(read-line in)) (parse-integer (read-line in))))))
nil)))
(defun set-backlight (value) (defun get-backlight ()
"Set backlight to given value" "Retrieve current backlight brightness from sysfs"
(let ((backlight-file (concatenate 'string *backlight-sysfs* "brightness"))) (let ((backlight-file (concatenate 'string *backlight-sysfs* "brightness")))
(if (probe-file backlight-file) (if (probe-file backlight-file)
(with-open-file (out backlight-file (with-open-file (in backlight-file)
(parse-integer (read-line in))))))
(defun get-backlight-rel ()
"Retrieve current backlight brightness relative to maximum"
(round (* 100
(float (/ (get-backlight)
(get-max-backlight))))))
(defun set-backlight (value)
"Set backlight brightness to given value using sysfs"
(let ((backlight-file (concatenate 'string *backlight-sysfs* "brightness")))
(if (probe-file backlight-file)
(progn (with-open-file (out backlight-file
:direction :output :direction :output
:if-exists :overwrite) :if-exists :overwrite)
(format out "~D" (truncate value)))))) (format out "~D" (truncate value)))
value))))
(defun set-backlight-rel (value)
"Set backlight brightness relative to maximum"
(let ((max-backlight (get-max-backlight)))
(if max-backlight
(set-backlight (* 0.01 value max-backlight)))))
;; set backlight
(defcommand change-backlight (value) (defcommand change-backlight (value)
((:number "Backlight in %: ")) ((:number "Backlight in %: "))
"Setting a new backlight value" "Setting a new backlight value"
(let ((max-backlight (get-max-backlight))) (unless (set-backlight-rel value)
(if max-backlight (echo "Can't set backlight")))
(set-backlight (* 0.01 value (parse-integer max-backlight)))
(echo "Can't set backlight")))) (defcommand increase-backlight ()
()
"Increase backlight by 5%"
(let ((current (get-backlight-rel)))
(if (> current 95)
(set-backlight-rel 100)
(set-backlight-rel (+ current 5)))))
(defcommand decrease-backlight ()
()
"Decrease backlight by 5%"
(let ((current (get-backlight-rel)))
(if (< current 6)
(set-backlight-rel 1)
(set-backlight-rel (- current 5)))))
;; tweet ;; tweet
(defcommand tweet (tweet) (defcommand tweet (tweet)
@ -211,6 +243,10 @@
(echo (concatenate 'string "Tweeted: " tweet))) (echo (concatenate 'string "Tweeted: " tweet)))
(define-key *root-map* (kbd "T") "tweet") (define-key *root-map* (kbd "T") "tweet")
(define-key *root-map* (kbd "B") "change-backlight")
(define-key *top-map* (kbd "XF86MonBrightnessUp") "increase-backlight")
(define-key *top-map* (kbd "XF86MonBrightnessDown") "decrease-backlight")
;; switch key layout ;; switch key layout
(defun switch-layout-to (new-lang) (defun switch-layout-to (new-lang)