1
0
Fork 0
mirror of https://github.com/Luzifer/streamdeck.git synced 2024-12-20 09:41:19 +00:00

Add support for short / long press actions

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-10-19 23:36:23 +02:00
parent d9671f1e4c
commit b77ac561ce
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 62 additions and 32 deletions

View file

@ -1,9 +1,15 @@
package main package main
import ( import (
"os"
"time" "time"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
) )
const defaultLongPressDuration = 500 * time.Millisecond
type config struct { type config struct {
AutoReload bool `yaml:"auto_reload"` AutoReload bool `yaml:"auto_reload"`
CaptionBorder int `yaml:"caption_border"` CaptionBorder int `yaml:"caption_border"`
@ -14,6 +20,7 @@ type config struct {
DefaultBrightness int `yaml:"default_brightness"` DefaultBrightness int `yaml:"default_brightness"`
DefaultPage string `yaml:"default_page"` DefaultPage string `yaml:"default_page"`
DisplayOffTime time.Duration `yaml:"display_off_time"` DisplayOffTime time.Duration `yaml:"display_off_time"`
LongPressDuration time.Duration `yaml:"long_press_duration"`
Pages map[string]page `yaml:"pages"` Pages map[string]page `yaml:"pages"`
RenderFont string `yaml:"render_font"` RenderFont string `yaml:"render_font"`
} }
@ -27,17 +34,18 @@ type page struct {
type keyDefinition struct { type keyDefinition struct {
Display dynamicElement `yaml:"display"` Display dynamicElement `yaml:"display"`
Actions []dynamicElement `yaml:"actions"` Actions []dynamicElement `yaml:"actions"`
On string `yaml:"on"`
} }
type dynamicElement struct { type dynamicElement struct {
Type string `yaml:"type"` Type string `yaml:"type"`
LongPress bool `yaml:"long_press"`
Attributes map[string]interface{} `yaml:"attributes"` Attributes map[string]interface{} `yaml:"attributes"`
} }
func newConfig() config { func newConfig() config {
return config{ return config{
AutoReload: true, AutoReload: true,
LongPressDuration: defaultLongPressDuration,
} }
} }
@ -47,3 +55,22 @@ const (
captionPositionBottom = "bottom" captionPositionBottom = "bottom"
captionPositionTop = "top" captionPositionTop = "top"
) )
func loadConfig() error {
userConfFile, err := os.Open(cfg.Config)
if err != nil {
return errors.Wrap(err, "Unable to open config")
}
defer userConfFile.Close()
tempConf := newConfig()
if err = yaml.NewDecoder(userConfFile).Decode(&tempConf); err != nil {
return errors.Wrap(err, "Unable to parse config")
}
applySystemPages(&tempConf)
userConfig = tempConf
return nil
}

View file

@ -15,7 +15,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sashko/go-uinput" "github.com/sashko/go-uinput"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
) )
var ( var (
@ -65,25 +64,6 @@ func init() {
} }
} }
func loadConfig() error {
userConfFile, err := os.Open(cfg.Config)
if err != nil {
return errors.Wrap(err, "Unable to open config")
}
defer userConfFile.Close()
var tempConf config
if err = yaml.NewDecoder(userConfFile).Decode(&tempConf); err != nil {
return errors.Wrap(err, "Unable to parse config")
}
applySystemPages(&tempConf)
userConfig = tempConf
return nil
}
func main() { func main() {
if cfg.List { if cfg.List {
listAndQuit() listAndQuit()
@ -160,6 +140,11 @@ func main() {
} }
} }
var (
actor *int
actStart time.Time
)
for { for {
select { select {
case evt := <-sd.Subscribe(): case evt := <-sd.Subscribe():
@ -167,15 +152,25 @@ func main() {
offTimer.Reset(userConfig.DisplayOffTime) offTimer.Reset(userConfig.DisplayOffTime)
} }
kd, ok := activePage.GetKeyDefinitions(userConfig)[evt.Key] if evt.Type == streamdeck.EventTypeDown {
actor = &evt.Key
actStart = time.Now()
continue
}
if evt.Key != *actor {
continue
}
kd, ok := activePage.GetKeyDefinitions(userConfig)[*actor]
if !ok { if !ok {
continue continue
} }
if kd.On == "down" && evt.Type == streamdeck.EventTypeDown || (kd.On == "up" || kd.On == "") && evt.Type == streamdeck.EventTypeUp || kd.On == "both" { isLongPress := time.Since(actStart) > userConfig.LongPressDuration
if err := triggerAction(kd); err != nil {
log.WithError(err).Error("Unable to execute action") if err := triggerAction(kd, isLongPress); err != nil {
} log.WithError(err).Error("Unable to execute action")
} }
case <-offTimer.C: case <-offTimer.C:
@ -253,12 +248,20 @@ func togglePage(page string) error {
return nil return nil
} }
func triggerAction(kd keyDefinition) error { func triggerAction(kd keyDefinition, isLongPress bool) error {
for _, a := range kd.Actions { for _, a := range kd.Actions {
if a.Type != "" { if a.Type == "" {
if err := callAction(a); err != nil { // No type on that action: Invalid
return err continue
} }
if isLongPress != a.LongPress {
// press duration does not match requirement
continue
}
if err := callAction(a); err != nil {
return err
} }
} }