From 49e2eb410bc54147bd9817da6f1467a85776e9fb Mon Sep 17 00:00:00 2001 From: Nulo Date: Thu, 23 Dec 2021 11:49:01 -0300 Subject: [PATCH] Revert "Reimplement modifiers as bit operations" This reverts commit 112ee7b90a9fefb78660ec1f1bec1082033f807f. --- Makefile | 2 +- keycodes.scm | 7 ++++--- menelaus.scm | 37 ++++++++++++++++++++++--------------- usb_keyboard.c | 6 ------ 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 18991c7..45ee728 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ $(LAYOUT).s: $(LAYOUT).scm menelaus.scm keycodes.scm avr-gcc -mmcu=$(MCU) -o $(LAYOUT).elf $(LAYOUT).s usb_keyboard.s usb_keyboard.s: usb_keyboard.h usb_keyboard.c - avr-gcc -lm -std=gnu99 -S -D F_CPU=$(F_CPU)UL -mmcu=$(MCU) -c \ + avr-gcc -std=gnu99 -S -D F_CPU=$(F_CPU)UL -mmcu=$(MCU) -c \ -o usb_keyboard.s usb_keyboard.c udev: /etc/udev/rules.d/a-star.rules diff --git a/keycodes.scm b/keycodes.scm index 9ceaa39..39234d4 100644 --- a/keycodes.scm +++ b/keycodes.scm @@ -101,11 +101,12 @@ (define (combo modifier keycode) (list (car modifier) keycode)) (define (uncombo keycode) (and (= 2 (length keycode)) (car (cdr keycode)))) +;; We're treating these a little differently; they are not literal USB values. (define mod-ctrl (modify 1)) (define mod-shift (modify 2)) -(define mod-alt (modify 4)) -(define mod-super (modify 8)) -(define mod-altgr (modify 64)) +(define mod-alt (modify 3)) +(define mod-super (modify 4)) +(define mod-altgr (modify 5)) (define (sft keycode) (combo mod-shift keycode)) ; shorthand (define (altgr keycode) (combo mod-altgr keycode)) ; shorthand diff --git a/menelaus.scm b/menelaus.scm index 1476fbe..c43830d 100644 --- a/menelaus.scm +++ b/menelaus.scm @@ -245,13 +245,13 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Vectors to store keycodes for the USB frame we are preparing to send. -(define modifiers 0) -(define sticky-modifiers 0) +(define modifiers (vector 0 0 0 0 0)) +(define sticky-modifiers (vector 0 0 0 0 0)) (define keycodes-down (vector 0 0 0 0 0 0)) ;; For each element of the keycodes-down or modifiers vector, which physical ;; key caused it to be pressed? -(define keys-for-modifiers (vector #f #f #f #f #f #f #f #f)) +(define keys-for-modifiers (vector #f #f #f #f #f)) (define keys-for-frame (vector #f #f #f #f #f #f)) ;; Given a physical key index, what keycode does it map to in the layout? @@ -261,13 +261,13 @@ ;; Record that a given key resulted in a specific modifier press. (define (press-modifier keycode key) - (set! modifiers (| modifiers keycode)) - (vector-set! keys-for-modifiers (call-c-func "mathlog2" keycode) key)) + (vector-set! modifiers (- keycode 1) 1) + (vector-set! keys-for-modifiers (- keycode 1) key)) (define (press-sticky-modifier keycode) - (set! sticky-modifiers (| sticky-modifiers keycode))) + (vector-set! sticky-modifiers (- keycode 1) 1)) (define (release-sticky-modifier keycode) - (set! sticky-modifiers (^ sticky-modifiers keycode))) + (vector-set! sticky-modifiers (- keycode 1) 0)) ;; Record that a given key resulted in a specific non-modifier press. (define (press-normal-key keycode key) @@ -288,7 +288,13 @@ (press-modifier (unmodify keycode) key) (press-key-aux (uncombo keycode) key))) (press-modifier (unmodify keycode) key)) - (press-normal-key keycode key)))) + (let ((sticky (find sticky-modifiers 1))) + (if sticky + (begin + (press-modifier (+ sticky 1) key) + (release-sticky-modifier (+ sticky 1))) + #f) + (press-normal-key keycode key))))) ;; Record a key press in the modifiers/keycodes-down vectors for the layout. @@ -300,9 +306,9 @@ (define (release-modifier keycode key n) (if (= (or (vector-ref keys-for-modifiers n) (- 0 1)) key) (begin - (set! modifiers (^ modifiers (unmodify keycode))) + (vector-set! modifiers n 0) (vector-set! keys-for-modifiers n #f)) - (and (< n 7) (release-modifier keycode key (+ n 1))))) + (and (< n 4) (release-modifier keycode key (+ n 1))))) ;; Record a key release, clearing it out of the press tracking data. (define (release-key key) @@ -321,7 +327,7 @@ #f) (if modifier-slot (begin - (release-modifier keycode key 0) + (release-modifier modifier-slot key 0) (release-key key)) #f))))) @@ -343,8 +349,10 @@ ;; Actually send the USB frame. (define (usb-send m k0 k1 k2 k3 k4 k5) - ;; call-c-func is a special form and cannot be applied - (call-c-func "usb_send" (| m sticky-modifiers) k0 k1 k2 k3 k4 k5)) + (let ((mods (+ (vector-ref m 0) (* (vector-ref m 1) 2)))) ; + isn't variadic + (let ((mods (+ mods (+ (+ (* (vector-ref m 2) 4) (* (vector-ref m 3) 8)) (* (vector-ref m 4) 64))))) + ;; call-c-func is a special form and cannot be applied + (call-c-func "usb_send" mods k0 k1 k2 k3 k4 k5)))) ;; Scan the matrix, determine the appropriate keycodes, and send them. (define (loop) @@ -356,8 +364,7 @@ ;; macro in one fell swoop. Primitive, but effective. (free! (let ((keys-scanned (debounce-matrix))) (set-usb-frame (press/release-for keys-scanned)) - (apply usb-send (cons modifiers (vector->list keycodes-down))) - (if (> (vector-ref keycodes-down 0) 0) (set! sticky-modifiers 0) #f))) + (apply usb-send (cons modifiers (vector->list keycodes-down))))) (loop)) ;; Prepare the GPIO pins. diff --git a/usb_keyboard.c b/usb_keyboard.c index fd1d6ee..0f6e8e8 100644 --- a/usb_keyboard.c +++ b/usb_keyboard.c @@ -28,12 +28,6 @@ #include "usb_keyboard.h" #include -#include - -int16_t mathlog2(int x) { - return round(log(x)/log(2)); -} - /************************************************************************** * * Configurable Options