Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handlers & DBus (gif, clock & counter as examples) #2

Merged
merged 8 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions CounterHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"github.com/fogleman/gg"
"github.com/unix-streamdeck/api"
"github.com/unix-streamdeck/driver"
"golang.org/x/image/font/inconsolata"
"strconv"
)

type CounterIconHandler struct {
count int
running bool
}

func (c *CounterIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
if c.running {
img := gg.NewContext(72, 72)
img.SetRGB(0, 0, 0)
img.Clear()
img.SetRGB(1, 1, 1)
img.SetFontFace(inconsolata.Regular8x16)
count := strconv.Itoa(c.count)
img.DrawStringAnchored(count, 72/2, 72/2, 0.5, 0.5)
img.Clip()
SetImage(img.Image(), index, page, dev)
key.Buff = img.Image()
}
}

func (c CounterIconHandler) Stop() {
c.running = false
}

type CounterKeyHandler struct{}

func (CounterKeyHandler) Key(page int, index int, key *api.Key, dev streamdeck.Device) {
if key.IconHandler != "Counter" {
return
}
handler := key.IconHandlerStruct.(*CounterIconHandler)
handler.count += 1
handler.Icon(page, index, key, dev)
}
44 changes: 44 additions & 0 deletions GifHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"github.com/unix-streamdeck/api"
"github.com/unix-streamdeck/driver"
"image/gif"
"log"
"os"
"time"
)

type GifIconHandler struct {
running bool
}

func (s *GifIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
s.running = true
f, err := os.Open(key.Icon)
if err != nil {
log.Println(err)
return
}
gifs, err := gif.DecodeAll(f)
timeDelay := gifs.Delay[0]
gifIndex := 0
go loop(gifs, gifIndex, timeDelay, page, index, dev, key, s)
}

func (s *GifIconHandler) Stop() {
s.running = false
}

func loop(gifs *gif.GIF, gifIndex int, timeDelay int, page int, index int, dev streamdeck.Device, key *api.Key, s *GifIconHandler) {
for s.running {
img := ResizeImage(gifs.Image[gifIndex])
SetImage(img, index, page, dev)
key.Buff = img
gifIndex++
if gifIndex >= len(gifs.Image) {
gifIndex = 0
}
time.Sleep(time.Duration(timeDelay * 10000000))
}
}
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,29 @@ The actions you can have on a button are:
- `url`: opens a url in your default browser via xdg
- `brightness`: set the brightness of the streamdeck as a percentage
- `switch_page`: change the active page on the streamdeck


### D-Bus

There is a D-Bus interface built into the daemon, the service name and interface for D-Bus are `com.thejonsey.streamdeck` and `com/thejonsey/streamdeck` respectively, and is made up of the following methods/signals

#### Methods

- GetConfig - returns the current running config
- SetConfig - sets the config, without saving to disk, takes in Stringified json, returns an error if anything breaks
- ReloadConfig - reloads the config from disk
- GetDeckInfo - Returns information about the active streamdeck in the format of
```json
{
"icon_size": 72,
"rows": 3,
"cols": 5,
"page": 0
}
```
- SetPage - Set the page on the streamdeck to the number passed to it, returns an error if anything breaks
- CommitConfig - Commits the currently active config to disk, returns an error if anything breaks

#### Signals

- Page - sends the number of the page switched to on the StreamDeck
48 changes: 48 additions & 0 deletions TimeHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"github.com/fogleman/gg"
"github.com/unix-streamdeck/api"
"github.com/unix-streamdeck/driver"
"golang.org/x/image/font/inconsolata"
"strconv"
"time"
)

type TimeIconHandler struct{
running bool
}

func (t *TimeIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
t.running = true
go timeLoop(page, index, dev, key, t)
}

func (t *TimeIconHandler) Stop() {
t.running = false
}

func timeLoop(page int, index int, dev streamdeck.Device, key *api.Key, handler *TimeIconHandler) {
for handler.running {
img := gg.NewContext(72, 72)
img.SetRGB(0, 0, 0)
img.Clear()
img.SetRGB(1, 1, 1)
img.SetFontFace(inconsolata.Regular8x16)
t := time.Now()
tString := t.Format("15:04:05")
img.DrawStringAnchored(tString, 72/2, 72/2, 0.5, 0.5)
img.Clip()
SetImage(img.Image(), index, page, dev)
key.Buff = img.Image()
time.Sleep(time.Second)
}
}

func zeroes(i int) (string) {
if i < 10 {
return "0" + strconv.Itoa(i)
} else {
return strconv.Itoa(i)
}
}
20 changes: 0 additions & 20 deletions config.go

This file was deleted.

102 changes: 102 additions & 0 deletions dbus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"encoding/json"
"errors"
"github.com/godbus/dbus/v5"
"log"
)


var conn *dbus.Conn

var s *StreamDeckDBus

type StreamDeckDBus struct {
Cols int `json:"cols,omitempty"`
Rows int `json:"rows,omitempty"`
IconSize int `json:"icon_size,omitempty"`
Page int `json:"page"`
}

func (s StreamDeckDBus) GetDeckInfo() (string, *dbus.Error) {
infoString, err := json.Marshal(s)
if err != nil {
return "", dbus.MakeFailedError(err)
}
return string(infoString), nil
}

func (StreamDeckDBus) GetConfig() (string, *dbus.Error) {
configString, err := json.Marshal(config)
if err != nil {
return "", dbus.MakeFailedError(err)
}
return string(configString), nil
}

func (StreamDeckDBus) ReloadConfig() *dbus.Error {
err := ReloadConfig()
if err != nil {
return dbus.MakeFailedError(err)
}
return nil
}

func (StreamDeckDBus) SetPage(page int) *dbus.Error {
SetPage(config, page, dev)
return nil
}

func (StreamDeckDBus) SetConfig(configString string) *dbus.Error {
err := SetConfig(configString)
if err != nil {
return dbus.MakeFailedError(err)
}
return nil
}

func (StreamDeckDBus) CommitConfig() *dbus.Error {
err := SaveConfig()
if err != nil {
return dbus.MakeFailedError(err)
}
return nil
}

func InitDBUS() error {
var err error
conn, err = dbus.SessionBus()
if err != nil {
log.Println(err)
return err
}
defer conn.Close()

s = &StreamDeckDBus{
Cols: int(dev.Columns),
Rows: int(dev.Rows),
IconSize: int(dev.Pixels),
Page: p,
}
conn.ExportAll(s, "/com/unixstreamdeck/streamdeckd", "com.unixstreamdeck.streamdeckd")
reply, err := conn.RequestName("com.unixstreamdeck.streamdeckd",
dbus.NameFlagDoNotQueue)
if err != nil {
log.Println(err)
return err
}
if reply != dbus.RequestNameReplyPrimaryOwner {
return errors.New("DBus: Name already taken")
}
select {}
}

func EmitPage(page int) {
if conn != nil {
conn.Emit("/com/unixstreamdeck/streamdeckd", "com.unixstreamdeck.streamdeckd.Page", page)
}
if s != nil {
s.Page = page
}
}
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module streamdeckd
module github.com/unix-streamdeck/streamdeckd

go 1.14

require (
github.com/fogleman/gg v1.3.0
github.com/godbus/dbus/v5 v5.0.4-0.20200513180336-df5ef3eb7cca
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/unix-streamdeck/streamdeck v0.0.0-20200727161137-d2d416a422d2
github.com/unix-streamdeck/api v0.0.0-20200818180846-6942d99617b2
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
)
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godbus/dbus/v5 v5.0.4-0.20200513180336-df5ef3eb7cca h1:ewc47M3S8MAZgSO1yEnPrbmHjtQz6caAhYWOQzPHBok=
github.com/godbus/dbus/v5 v5.0.4-0.20200513180336-df5ef3eb7cca/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
Expand All @@ -40,8 +42,6 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/h2non/bimg v1.1.2 h1:J75W2eM5FT0KjcwsL2aiy1Ilu0Xy0ENb0sU+HHUJAvw=
github.com/h2non/bimg v1.1.2/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down Expand Up @@ -91,12 +91,10 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ungerik/go-cairo v0.0.0-20191014050614-4a03f432a432 h1:luu+HbKrZKlIjiaclwrldreEEvVPYX/ujRbTGkKz61c=
github.com/ungerik/go-cairo v0.0.0-20191014050614-4a03f432a432/go.mod h1:0ErpLiOxxE1oY+R4stiKut6/DbUJHnOp6U+e4d8zcTs=
github.com/unix-streamdeck/streamdeck v0.0.0-20200727161137-d2d416a422d2 h1:rLVbYju4iaW8jP64Pafffu9T0wXitOOtu3+qCxzGV1c=
github.com/unix-streamdeck/streamdeck v0.0.0-20200727161137-d2d416a422d2/go.mod h1:Tm8jArLxpQy5vGBbHSreHpNRAIYtDV3J3U3UMGRWTXc=
github.com/unix-streamdeck/streamdeck-lib v0.0.0-20200720132322-072961fa1fdd h1:oX6jtZ+/gBcryYn49alkMPbCzqqwyo/7VA17mA4khrQ=
github.com/unix-streamdeck/streamdeck-lib v0.0.0-20200720132322-072961fa1fdd/go.mod h1:EPLAk+jRYZ0zGW7S7bJctr3CLc4bc+tgBrJN7+G1bvc=
github.com/unix-streamdeck/api v0.0.0-20200818180846-6942d99617b2 h1:du+OoTUOp1mf/VRdQ8xQMWdR6JrOGbZvDG1nb9BBxHM=
github.com/unix-streamdeck/api v0.0.0-20200818180846-6942d99617b2/go.mod h1:rweAXRgCWdCACCuVhmleidq7HnJEO38zBGDe8uQqZ0w=
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b h1:27gVti9+OevmBC2BnWlKC0dQ0eiIHh7PvYTWxt4vb6A=
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b/go.mod h1:i3Eg6kJBslgUk2VIPJ3Cclta2fpV1KJrOnOnR8gnVKY=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
Loading