Commit 49686010 authored by Brandon Invergo's avatar Brandon Invergo

Support sending keypress events from rel/abs controls. Implements #2.

parent 2f971624
......@@ -3,6 +3,8 @@
* guile/librekontrol/device.scm
(remap-button,make-remap-button-toggle): Support modifier keys for
remap-button callbacks.
(rel-knob-to-button,make-abs-knob-to-button): Support sending
keypress events from rel/abs controls.
2019-03-27 Brandon Invergo <brandon@invergo.net>
......
......@@ -29,6 +29,8 @@
make-remap-button-toggle
system-button
make-system-button-toggle
rel-knob-to-button
make-abs-knob-to-button
system-abs-knob
system-rel-knob
make-abs-to-rel-system-knob
......@@ -106,11 +108,12 @@ INPUT-NAME. Set up the device further through optional arguments:
(let ((device (open-device alsa-name input-name #t #t)))
(when device
(if remap-events
(for-each (lambda (event)
(remap-enable-event device
(car event) (cdr event)))
remap-events)
(finalize-remap-dev device))
(begin
(for-each (lambda (event)
(remap-enable-event device
(car event) (cdr event)))
remap-events)
(finalize-remap-dev device)))
(if controls
(for-each (lambda (control-def)
(apply (lambda* (control callback #:rest r)
......@@ -206,7 +209,7 @@ the LED on or off).
Optional keyword arguments:
#:event-type -- Event type (default: #f)
#:event-code -- Event code (default: #f)
#:mod-keys -- List of modifier keys to apply (e.g. key-alt, key-ctrl,
#:mod-keys -- List of modifier keys to apply (e.g. key-alt, key-leftctrl,
etc.)
#:on-value -- Event value for when the button is pressed (default: 1)
#:off-value -- Event value for when the button is released (default: 0)"
......@@ -240,7 +243,7 @@ open ctl, toggle on/off the ctl.
Optional keyword arguments:
#:event-type -- Event type (default: #f)
#:event-code -- Event code (default: #f)
#:mod-keys -- List of modifier keys to apply (e.g. key-alt, key-ctrl,
#:mod-keys -- List of modifier keys to apply (e.g. key-alt, key-leftctrl,
etc.)
#:on-value -- Event value for when the toggle is turned on (default: 1)
#:off-value -- Event value for when the toggle is turned off (default: 0)"
......@@ -317,8 +320,119 @@ Optional keyword arguments:
(set! state #f)))
(toggle-ctl alsa-ctl)))))))
(define (system-abs-knob device input-event input-value alsa-ctl options)
"Send a system event when a knob is changed. The input value is
(define (rel-knob-to-button device input-event input-value alsa-ctl options)
"Remap a relative-position event to a button/key event. Different
events can be sent for negative and positive values. If ALSA-CTL is
not #f and its descriptor points to an open ctl, turn on the
ctl (i.e. turn the LED on).
Optional keyword arguments:
#:neg-event-code -- Event code for negative values (default: #f)
#:pos-event-code -- Event code for positive values (default: #f)
#:neg-mod-keys -- List of modifier keys to apply for negative values
(e.g. key-alt, key-leftctrl, etc.)
#:pos-mod-keys -- List of modifier keys to apply for negative values
(e.g. key-alt, key-leftctrl, etc.)
#:repeat -- Send multiple key events based on the magnitude of
INPUT-VALUE (default: #f"
(let-keywords options #f ((neg-event-code #f)
(pos-event-code #f)
(neg-mod-keys #f)
(pos-mod-keys #f)
(repeat #f))
(let ((mod-keys (if (< input-value 0)
neg-mod-keys pos-mod-keys))
(event-code (if (< input-value 0)
neg-event-code pos-event-code)))
(begin
(set-ctl alsa-ctl #t)
(when mod-keys
(for-each (lambda (mod-key)
(send-remap-event device ev-key mod-key 1))
mod-keys))
(if repeat
(do ((i 1 (1+ i)))
((> i (abs input-value)))
(begin
(send-remap-event device ev-key event-code 1)
(send-remap-event device ev-key event-code 0)))
(begin
(send-remap-event device ev-key event-code 1)
(send-remap-event device ev-key event-code 0)))
(when mod-keys
(for-each (lambda (mod-key)
(send-remap-event device ev-key mod-key 0))
mod-keys))
(set-ctl alsa-ctl #f)))))
(define (make-abs-knob-to-button)
"Return a callback function that remaps an absolute-position event
to a button event. The absolute position is first converted to a
relative position. Different events can be sent for negative and
positive values. If ALSA-CTL is not #f and its descriptor points to
an open ctl, turn on the ctl (i.e. turn the LED on).
Optional keyword arguments:
#:neg-event-code -- Event code for negative values (default: #f)
#:pos-event-code -- Event code for positive values (default: #f)
#:neg-mod-keys -- List of modifier keys to apply for negative values
(e.g. key-alt, key-leftctrl, etc.)
#:pos-mod-keys -- List of modifier keys to apply for negative values
(e.g. key-alt, key-leftctrl, etc.)
#:repeat -- Send multiple key events based on the magnitude of
INPUT-VALUE (default: #f)
#:knob-max -- Knob maximum value (default: 127)
#:wrap-tol -- Relative-position tolerance before deciding that the absolute
position has wrapped on an \"infinite-rotary\"-style knob (i.e.
the physical knob has no start or end position and can be
rotated completely in either direction). Expressed as a
fraction of KNOB-MAX (default: 0.01)
#:invert -- Whether to invert the sign of the relative position (set if
the sign is not what you expect for the direction of rotation)
(default: #f)"
(let ((prev-value #f))
(lambda (device input-event input-value alsa-ctl options)
(let-keywords options #f ((neg-event-code #f)
(pos-event-code #f)
(neg-mod-keys #f)
(pos-mod-keys #f)
(repeat #f)
(knob-max 127)
(wrap-tol 0.01)
(invert #f))
(let* ((value (if (not prev-value)
0
(if invert
(- prev-value input-value)
(- input-value prev-value))))
(mod-keys (if (< value 0)
neg-mod-keys pos-mod-keys))
(event-code (if (< value 0)
neg-event-code pos-event-code)))
(begin
(set-ctl alsa-ctl #t)
(when mod-keys
(for-each (lambda (mod-key)
(send-remap-event device ev-key mod-key 1))
mod-keys))
(if repeat
(do ((i 1 (1+ i)))
((> i (abs value)))
(begin
(send-remap-event device ev-key event-code 1)
(send-remap-event device ev-key event-code 0)))
(begin
(send-remap-event device ev-key event-code 1)
(send-remap-event device ev-key event-code 0)))
(when mod-keys
(for-each (lambda (mod-key)
(send-remap-event device ev-key mod-key 0))
mod-keys))
(set! prev-value input-value)
(set-ctl alsa-ctl #f)))))))
(define (system-abs-knob device input-event input-value alsa-ctl options)
"Send a system event when a knob is changed. The input value is
added to the system command via standard text formatting. If ALSA-CTL
is not #f and its descriptor points to an open ctl, toggle the
ctl (i.e. turn the LED on or off). This procedure applies to knobs
......@@ -329,13 +443,13 @@ Optional keyword arguments:
#:cmd -- Command (default: \"printf '%d\n' ~A\")
#:max-value -- Maximum value to send to the command (default: 127)
#:knob-max -- Knob maximum value (default: 127)"
(let-keywords options #f ((cmd ("printf '%d\n' ~A"))
(max-value 127)
(knob-max 127))
(when (eq? (car input-event) ev-abs)
(let* ((value (scale-value input-value knob-max max-value)))
(system (simple-format #f cmd value)))
(set-ctl alsa-ctl #t))))
(let-keywords options #f ((cmd ("printf '%d\n' ~A"))
(max-value 127)
(knob-max 127))
(when (eq? (car input-event) ev-abs)
(let* ((value (scale-value input-value knob-max max-value)))
(system (simple-format #f cmd value)))
(set-ctl alsa-ctl #t))))
(define (system-rel-knob device input-event input-value alsa-ctl options)
"Send a system event when a knob is changed. The input value is
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment