Make stump-backlight work correctly on multiple monitors.

This commit is contained in:
Florian Margaine 2021-01-21 09:45:42 +01:00 committed by Javier Olaechea
parent 6dc5bf91f9
commit 2fa5814111
3 changed files with 37 additions and 23 deletions

View file

@ -22,12 +22,15 @@ stump-backlight exposes a few things:
- The =*scale*= variable: defaults to 10. It defines the scale of the
backlight. If you want more granularity than 10 steps, increase this variable.
- The =*current-percent*= variable: defaults to 50. It defines the current
- The =*default-percent*= variable: defaults to 50. It defines the default
percentage of the backlight. Due to underlying technology issues, the
percentage is managed separately from the real one, so this lets you set it at
startup.
- The =update= function: it takes the values from the variables and applies the
backlight.
- The =(update output)= function: it takes the values from the variables and
applies the backlight. It takes one argument: the output (aka monitor) to
apply the update on.
- The =(current-output)= function: it returns the output (aka monitor) of the
currently focused window. Useful to pass to the previous function.
- 2 commands: =backlight-increase= and =backlight-decrease=. They increase or
decrease based on the scale and apply the backlight.

View file

@ -3,38 +3,48 @@
(defvar *scale* 10
"The backlight scale. Increase if you want more granularity.")
(defvar *current-percent* 50
"CLX does not let us query the existing backlight, so we need to keep track of it.")
(defvar *default-percent* 50
"Default value for an output's percentage.")
(defvar *current-percent* (make-hash-table)
"CLX does not let us query the existing backlight, so we need to keep track of
it manually.")
(defun current-output ()
(xlib:rr-get-output-primary (stumpwm:window-xwin (stumpwm:current-window))))
(stumpwm:defcommand backlight-increase () ()
(when (< *current-percent* 100)
(setf *current-percent* (* (1+ (/ *current-percent* *scale*)) *scale*))
(update)))
(let* ((output (current-output))
(current-percent (or (gethash output *current-percent*) *default-percent*)))
(when (< current-percent 100)
(setf (gethash output *current-percent*) (* (1+ (/ current-percent *scale*)) *scale*))
(update output))))
(stumpwm:defcommand backlight-decrease () ()
(when (> *current-percent* 0)
(setf *current-percent* (* (1- (/ *current-percent* *scale*)) *scale*))
(update)))
(let* ((output (current-output))
(current-percent (or (gethash output *current-percent*) *default-percent*)))
(when (> current-percent 0)
(setf (gethash output *current-percent*) (* (1- (/ current-percent *scale*)) *scale*))
(update output))))
(defun update ()
(let* ((window (stumpwm:window-xwin (stumpwm:current-window)))
(output (xlib:rr-get-output-primary window))
(backlight-limits
(multiple-value-list
(xlib:rr-query-output-property stumpwm:*display* output :backlight)))
(backlight-type (xlib:rr-get-output-property stumpwm:*display* output :backlight)))
(defun update (output)
(let ((backlight-limits
(multiple-value-list
(xlib:rr-query-output-property stumpwm:*display* output :backlight)))
(backlight-type (xlib:rr-get-output-property stumpwm:*display* output :backlight)))
(destructuring-bind (min max) (fourth backlight-limits)
(xlib:rr-change-output-property
stumpwm:*display*
output
:backlight
0 ; replace
(vector (scaled-current (1+ min) (1- max))) ; max is non-inclusive in X
; API, but inclusive in ours.
(vector (scaled-current output (1+ min) (1- max))) ; max is non-inclusive
; in X11 API, but
; inclusive in ours.
backlight-type))))
(defun scaled-current (min max)
(truncate (/ (* *current-percent* (- max min)) 100)))
(defun scaled-current (output min max)
(truncate (/ (* (gethash output *current-percent*) (- max min)) 100)))
;; Opinionated but this one should be fair.
(stumpwm:define-key stumpwm:*top-map*

View file

@ -1,7 +1,8 @@
(defpackage #:stump-backlight
(:use #:cl)
(:export #:*current-percent*
(:export #:*default-percent*
#:*scale*
#:backlight-increase
#:backlight-decrease
#:current-output
#:update))