mirror of
https://github.com/Luzifer/streamdeck.git
synced 2024-12-20 17:51:21 +00:00
Initial version of tooling
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
ea8d38c642
commit
f2244140ae
9 changed files with 377 additions and 0 deletions
1
cmd/streamdeck/.gitignore
vendored
Normal file
1
cmd/streamdeck/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
config.yml
|
27
cmd/streamdeck/action_toggleDisplay.go
Normal file
27
cmd/streamdeck/action_toggleDisplay.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/pkg/errors"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registerAction("toggle_display", actionToggleDisplay{})
|
||||||
|
}
|
||||||
|
|
||||||
|
var actionToggleDisplayPreviousBrightness int
|
||||||
|
|
||||||
|
type actionToggleDisplay struct{}
|
||||||
|
|
||||||
|
func (actionToggleDisplay) Execute(attributes map[string]interface{}) error {
|
||||||
|
var newB int
|
||||||
|
if currentBrightness > 0 {
|
||||||
|
actionToggleDisplayPreviousBrightness = currentBrightness
|
||||||
|
} else {
|
||||||
|
newB = actionToggleDisplayPreviousBrightness
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := sd.SetBrightness(newB); err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to set brightness")
|
||||||
|
}
|
||||||
|
currentBrightness = newB
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
22
cmd/streamdeck/config.go
Normal file
22
cmd/streamdeck/config.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
DefaultBrightness int `yaml:"default_brightness"`
|
||||||
|
DefaultPage string `yaml:"default_page"`
|
||||||
|
Pages map[string]page `yaml:"pages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type page struct {
|
||||||
|
Keys []keyDefinition `yaml:"keys"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type keyDefinition struct {
|
||||||
|
Display dynamicElement `yaml:"display"`
|
||||||
|
Action dynamicElement `yaml:"action"`
|
||||||
|
On string `yaml:"on"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type dynamicElement struct {
|
||||||
|
Type string `yaml:"type"`
|
||||||
|
Attributes map[string]interface{} `yaml:"attributes"`
|
||||||
|
}
|
41
cmd/streamdeck/display_color.go
Normal file
41
cmd/streamdeck/display_color.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registerDisplayElement("color", displayElementColor{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type displayElementColor struct{}
|
||||||
|
|
||||||
|
func (d displayElementColor) Display(idx int, attributes map[string]interface{}) error {
|
||||||
|
if name, ok := attributes["color"].(string); ok {
|
||||||
|
return d.displayPredefinedColor(idx, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rgba, ok := attributes["rgba"].([]interface{}); ok {
|
||||||
|
if len(rgba) != 4 {
|
||||||
|
return errors.New("RGBA color definition needs 4 hex values")
|
||||||
|
}
|
||||||
|
|
||||||
|
return sd.FillColor(idx, color.RGBA{uint8(rgba[0].(int)), uint8(rgba[1].(int)), uint8(rgba[2].(int)), uint8(rgba[3].(int))})
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("No valid attributes specified for type color")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (displayElementColor) displayPredefinedColor(idx int, name string) error {
|
||||||
|
c, ok := map[string]color.RGBA{
|
||||||
|
"blue": {0x0, 0x0, 0xff, 0xff},
|
||||||
|
}[name]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return errors.Errorf("Unknown color %q", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sd.FillColor(idx, c)
|
||||||
|
}
|
36
cmd/streamdeck/display_image.go
Normal file
36
cmd/streamdeck/display_image.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
_ "image/jpeg"
|
||||||
|
_ "image/png"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registerDisplayElement("image", displayElementImage{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type displayElementImage struct{}
|
||||||
|
|
||||||
|
func (d displayElementImage) Display(idx int, attributes map[string]interface{}) error {
|
||||||
|
filename, ok := attributes["path"].(string)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("No path attribute specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to open image")
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
img, _, err := image.Decode(f)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Umable to decode image")
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.Wrap(sd.FillImage(idx, img), "Unable to set image")
|
||||||
|
}
|
17
cmd/streamdeck/go.mod
Normal file
17
cmd/streamdeck/go.mod
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
module github.com/Luzifer/streamdeck/cmd/streamdeck
|
||||||
|
|
||||||
|
go 1.13
|
||||||
|
|
||||||
|
replace github.com/Luzifer/streamdeck => ../../
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/Luzifer/rconfig/v2 v2.2.1
|
||||||
|
github.com/Luzifer/streamdeck v0.0.0-20191120013804-40fbd11001d0
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||||
|
github.com/pkg/errors v0.8.1
|
||||||
|
github.com/sirupsen/logrus v1.4.2
|
||||||
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e // indirect
|
||||||
|
gopkg.in/validator.v2 v2.0.0-20191107172027-c3144fdedc21 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.2.7
|
||||||
|
)
|
41
cmd/streamdeck/go.sum
Normal file
41
cmd/streamdeck/go.sum
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
github.com/Luzifer/rconfig v2.2.0+incompatible h1:Kle3+rshPM7LxciOheaR4EfHUzibkDDGws04sefQ5m8=
|
||||||
|
github.com/Luzifer/rconfig/v2 v2.2.1 h1:zcDdLQlnlzwcBJ8E0WFzOkQE1pCMn3EbX0dFYkeTczg=
|
||||||
|
github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZbW37w+sqBw=
|
||||||
|
github.com/Luzifer/streamdeck v0.0.0-20191120013804-40fbd11001d0 h1:5wPz++ed8CFOu1yRVCFtmVrEJVzHWAzyu6z3u1B53R0=
|
||||||
|
github.com/Luzifer/streamdeck v0.0.0-20191120013804-40fbd11001d0/go.mod h1:uyylIY6mN/z+sBmkZfmlQN12G4gAPTZI4SKmqUfByTQ=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||||
|
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||||
|
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||||
|
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
||||||
|
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||||
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/sstallion/go-hid v0.0.0-20190621001400-1cf4630be9f4 h1:hczfXYN39SDj4FcN5J7sgHBtJm4U7ef2nvlonn6NvVU=
|
||||||
|
github.com/sstallion/go-hid v0.0.0-20190621001400-1cf4630be9f4/go.mod h1:JwBz6izP5UGcbYDU5VGeLkqpRIpSBDPtCB5/XnVXz9Q=
|
||||||
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
||||||
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
|
||||||
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA=
|
||||||
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 h1:WB265cn5OpO+hK3pikC9hpP1zI/KTwmyMFKloW9eOVc=
|
||||||
|
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
|
||||||
|
gopkg.in/validator.v2 v2.0.0-20191107172027-c3144fdedc21 h1:2QQcyaEBdpfjjYkF0MXc69jZbHb4IOYuXz2UwsmVM8k=
|
||||||
|
gopkg.in/validator.v2 v2.0.0-20191107172027-c3144fdedc21/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
|
||||||
|
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
133
cmd/streamdeck/main.go
Normal file
133
cmd/streamdeck/main.go
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/Luzifer/rconfig/v2"
|
||||||
|
"github.com/Luzifer/streamdeck"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
cfg = struct {
|
||||||
|
Config string `flag:"config,c" default:"config.yml" description:"Configuration with page / key definitions"`
|
||||||
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
||||||
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
currentBrightness int
|
||||||
|
|
||||||
|
userConfig config
|
||||||
|
activePage page
|
||||||
|
|
||||||
|
sd *streamdeck.Client
|
||||||
|
|
||||||
|
version = "dev"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
||||||
|
log.Fatalf("Unable to parse commandline options: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.VersionAndExit {
|
||||||
|
fmt.Printf("streamdeck %s\n", version)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to parse log level")
|
||||||
|
} else {
|
||||||
|
log.SetLevel(l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Load config
|
||||||
|
userConfFile, err := os.Open(cfg.Config)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to open config")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = yaml.NewDecoder(userConfFile).Decode(&userConfig); err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to parse config")
|
||||||
|
}
|
||||||
|
|
||||||
|
userConfFile.Close()
|
||||||
|
|
||||||
|
// Initialize device
|
||||||
|
sd, err = streamdeck.New(streamdeck.StreamDeckOriginalV2)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to open StreamDeck connection")
|
||||||
|
}
|
||||||
|
defer sd.Close()
|
||||||
|
|
||||||
|
serial, err := sd.Serial()
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to read serial")
|
||||||
|
}
|
||||||
|
|
||||||
|
firmware, err := sd.GetFimwareVersion()
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to read firmware")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"firmware": firmware,
|
||||||
|
"serial": serial,
|
||||||
|
}).Info("Found StreamDeck")
|
||||||
|
|
||||||
|
// Initial setup
|
||||||
|
|
||||||
|
if err = sd.SetBrightness(userConfig.DefaultBrightness); err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to set brightness")
|
||||||
|
}
|
||||||
|
currentBrightness = userConfig.DefaultBrightness
|
||||||
|
|
||||||
|
if err = togglePage(userConfig.DefaultPage); err != nil {
|
||||||
|
log.WithError(err).Error("Unable to load default page")
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case evt := <-sd.Subscribe():
|
||||||
|
if evt.Key >= len(activePage.Keys) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
kd := activePage.Keys[evt.Key]
|
||||||
|
if kd.On == "down" && evt.Type == streamdeck.EventTypeDown || kd.On == "up" && evt.Type == streamdeck.EventTypeUp || kd.On == "both" {
|
||||||
|
if err := triggerAction(kd); err != nil {
|
||||||
|
log.WithError(err).Error("Unable to execute action")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func togglePage(page string) error {
|
||||||
|
activePage = userConfig.Pages[page]
|
||||||
|
sd.ClearAllKeys()
|
||||||
|
|
||||||
|
for idx, kd := range activePage.Keys {
|
||||||
|
if kd.Display.Type != "" {
|
||||||
|
if err := callDisplayElement(idx, kd); err != nil {
|
||||||
|
return errors.Wrapf(err, "Unable to execute display element on key %d", idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func triggerAction(kd keyDefinition) error {
|
||||||
|
if kd.Action.Type != "" {
|
||||||
|
return errors.Wrap(callAction(kd), "Unable to execute action")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
59
cmd/streamdeck/registry.go
Normal file
59
cmd/streamdeck/registry.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type action interface {
|
||||||
|
Execute(attributes map[string]interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type displayElement interface {
|
||||||
|
Display(idx int, attributes map[string]interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
registeredActions = map[string]reflect.Type{}
|
||||||
|
registeredActionsLock sync.Mutex
|
||||||
|
registeredDisplayElements = map[string]reflect.Type{}
|
||||||
|
registeredDisplayElementsLock sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func registerAction(name string, handler action) {
|
||||||
|
registeredActionsLock.Lock()
|
||||||
|
defer registeredActionsLock.Unlock()
|
||||||
|
|
||||||
|
registeredActions[name] = reflect.TypeOf(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerDisplayElement(name string, handler displayElement) {
|
||||||
|
registeredDisplayElementsLock.Lock()
|
||||||
|
defer registeredDisplayElementsLock.Unlock()
|
||||||
|
|
||||||
|
registeredDisplayElements[name] = reflect.TypeOf(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func callAction(kd keyDefinition) error {
|
||||||
|
t, ok := registeredActions[kd.Action.Type]
|
||||||
|
if !ok {
|
||||||
|
return errors.Errorf("Unknown action type %q", kd.Action.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
inst := reflect.New(t).Interface().(action)
|
||||||
|
|
||||||
|
return inst.Execute(kd.Action.Attributes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func callDisplayElement(idx int, kd keyDefinition) error {
|
||||||
|
t, ok := registeredDisplayElements[kd.Display.Type]
|
||||||
|
if !ok {
|
||||||
|
return errors.Errorf("Unknown display type %q", kd.Display.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
inst := reflect.New(t).Interface().(displayElement)
|
||||||
|
|
||||||
|
return inst.Display(idx, kd.Display.Attributes)
|
||||||
|
}
|
Loading…
Reference in a new issue