Files

54 lines
1.4 KiB
Go

// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
package input
import (
"fmt"
"time"
"git.stormux.org/storm/fenrir/internal/command"
)
const DefaultMultiPressTimeout = 200 * time.Millisecond
// Set_multi_press_timeout changes the window between consecutive fresh presses.
// It preserves bindings and physical key ownership, but starts a new sequence.
func (r *Router) Set_multi_press_timeout(timeout time.Duration) error {
if timeout < time.Millisecond || timeout > 5*time.Second {
return fmt.Errorf("multi-press timeout must be between 1 and 5000 milliseconds")
}
r.multiPressTimeout = timeout
r.lastPress = time.Time{}
return nil
}
func (r *Router) multi_press(device int, combo chord, id command.ID, scriptChord bool) command.ID {
if scriptChord || id == "" || r.doubleBindings[combo] == "" {
r.lastPress = time.Time{}
return id
}
now := time.Now()
if r.now != nil {
now = r.now()
}
elapsed := now.Sub(r.lastPress)
if r.lastPress.IsZero() || r.lastDevice != device || r.lastChord != combo || elapsed < 0 || elapsed > r.multiPressTimeout {
r.pressCount = 1
} else {
r.pressCount++
}
r.lastChord, r.lastDevice, r.lastPress = combo, device, now
switch r.pressCount {
case 2:
id = r.doubleBindings[combo]
if r.tripleBindings[combo] == "" {
r.lastPress = time.Time{}
}
case 3:
id = r.tripleBindings[combo]
r.lastPress = time.Time{}
}
return id
}