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

Staging #6

Merged
merged 16 commits into from
Aug 28, 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
29 changes: 12 additions & 17 deletions dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import (
"encoding/json"
"errors"
"github.com/godbus/dbus/v5"
"github.com/unix-streamdeck/api"
"log"
)


var conn *dbus.Conn

var s *StreamDeckDBus
var sDbus *StreamDeckDBus
var sDInfo api.StreamDeckInfo

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) {
Expand All @@ -43,8 +40,8 @@ func (StreamDeckDBus) ReloadConfig() *dbus.Error {
return nil
}

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

Expand Down Expand Up @@ -73,13 +70,11 @@ func InitDBUS() error {
}
defer conn.Close()

s = &StreamDeckDBus{
Cols: int(dev.Columns),
Rows: int(dev.Rows),
IconSize: int(dev.Pixels),
sDbus = &StreamDeckDBus{}
sDInfo = api.StreamDeckInfo{
Page: p,
}
conn.ExportAll(s, "/com/unixstreamdeck/streamdeckd", "com.unixstreamdeck.streamdeckd")
conn.ExportAll(sDbus, "/com/unixstreamdeck/streamdeckd", "com.unixstreamdeck.streamdeckd")
reply, err := conn.RequestName("com.unixstreamdeck.streamdeckd",
dbus.NameFlagDoNotQueue)
if err != nil {
Expand All @@ -89,14 +84,14 @@ func InitDBUS() error {
if reply != dbus.RequestNameReplyPrimaryOwner {
return errors.New("DBus: Name already taken")
}
select {}
select {}
}

func EmitPage(page int) {
if conn != nil {
conn.Emit("/com/unixstreamdeck/streamdeckd", "com.unixstreamdeck.streamdeckd.Page", page)
}
if s != nil {
s.Page = page
if sDbus != nil {
sDInfo.Page = page
}
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ require (
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/api v0.0.0-20200818180846-6942d99617b2
github.com/unix-streamdeck/api v0.0.0-20200828000516-c734a9f6cec5
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
)
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ 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/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/api v0.0.0-20200828000516-c734a9f6cec5 h1:8WlgvTJMHvfxqmQYyTbfM9TL/BYIm/WfrbDhyA8ihQo=
github.com/unix-streamdeck/api v0.0.0-20200828000516-c734a9f6cec5/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=
Expand All @@ -115,6 +115,7 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
26 changes: 20 additions & 6 deletions handlers/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package handlers
import (
"github.com/fogleman/gg"
"github.com/unix-streamdeck/api"
"github.com/unix-streamdeck/driver"
"golang.org/x/image/font/inconsolata"
"image"
"strconv"
"time"
)

func (c *CounterIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
func (c *CounterIconHandler) Start(_ api.Key, _ api.StreamDeckInfo, callback func(image image.Image)) {
if c.Callback == nil {
c.Callback = callback
}
if c.Running {
img := gg.NewContext(72, 72)
img.SetRGB(0, 0, 0)
Expand All @@ -18,22 +22,32 @@ func (c *CounterIconHandler) Icon(page int, index int, key *api.Key, dev streamd
Count := strconv.Itoa(c.Count)
img.DrawStringAnchored(Count, 72/2, 72/2, 0.5, 0.5)
img.Clip()
c.OnSetImage(img.Image(), index, page, dev)
key.Buff = img.Image()
callback(img.Image())
time.Sleep(250 * time.Millisecond)
}
}

func (c *CounterIconHandler) IsRunning() bool {
return c.Running
}

func (c *CounterIconHandler) SetRunning(running bool) {
c.Running = running
}

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

type CounterKeyHandler struct{}

func (CounterKeyHandler) Key(page int, index int, key *api.Key, dev streamdeck.Device) {
func (CounterKeyHandler) Key(key api.Key, info api.StreamDeckInfo) {
if key.IconHandler != "Counter" {
return
}
handler := key.IconHandlerStruct.(*CounterIconHandler)
handler.Count += 1
handler.Icon(page, index, key, dev)
if handler.Callback != nil {
handler.Start(key, info, handler.Callback)
}
}
37 changes: 26 additions & 11 deletions handlers/gif.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
package handlers

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

func (s *GifIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
func (s *GifIconHandler) Start(key api.Key, info api.StreamDeckInfo, callback func(image image.Image)) {
s.Running = true
f, err := os.Open(key.Icon)
if err != nil {
log.Println(err)
return
}
gifs, err := gif.DecodeAll(f)
if err != nil {
log.Println(err)
return
}
timeDelay := gifs.Delay[0]
gifIndex := 0
go loop(gifs, gifIndex, timeDelay, page, index, dev, key, s)
frames := make([]image.Image, len(gifs.Image))
for i, frame := range gifs.Image {
frames[i] = resize.Resize(uint(info.IconSize), uint(info.IconSize), frame, resize.Lanczos3)
}
go loop(frames, timeDelay, callback, s)
}

func (s *GifIconHandler) IsRunning() bool {
return s.Running
}

func (s *GifIconHandler) SetRunning(running bool) {
s.Running = running
}

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) {
func loop(frames []image.Image, timeDelay int, callback func(image image.Image), s *GifIconHandler) {
gifIndex := 0
for s.Running {
img := resize.Resize(dev.Pixels, dev.Pixels, gifs.Image[gifIndex], resize.Lanczos3)
s.OnSetImage(img, index, page, dev)
key.Buff = img
img := frames[gifIndex]
callback(img)
gifIndex++
if gifIndex >= len(gifs.Image) {
if gifIndex >= len(frames) {
gifIndex = 0
}
time.Sleep(time.Duration(timeDelay * 10000000))
}
}
}
16 changes: 5 additions & 11 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package handlers

import (
"image"
"github.com/unix-streamdeck/driver"
)
import "image"

type CounterIconHandler struct {
Count int
Running bool
OnSetImage func(img image.Image, i int, page int, dev streamdeck.Device)
Count int
Running bool
Callback func(image image.Image)
}

type GifIconHandler struct {
Running bool
OnSetImage func(img image.Image, i int, page int, dev streamdeck.Device)
}

type TimeIconHandler struct{
type TimeIconHandler struct {
Running bool
OnSetImage func(img image.Image, i int, page int, dev streamdeck.Device)
}

32 changes: 15 additions & 17 deletions handlers/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,41 @@ package handlers
import (
"github.com/fogleman/gg"
"github.com/unix-streamdeck/api"
"github.com/unix-streamdeck/driver"
"golang.org/x/image/font/inconsolata"
"strconv"
"image"
"time"
)


func (t *TimeIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
func (t *TimeIconHandler) Start(_ api.Key, info api.StreamDeckInfo, callback func(image image.Image)) {
t.Running = true
go timeLoop(page, index, dev, key, t)
go timeLoop(info, callback, t)
}

func (t *TimeIconHandler) IsRunning() bool {
return t.Running
}

func (t *TimeIconHandler) SetRunning(running bool) {
t.Running = running
}

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

func timeLoop(page int, index int, dev streamdeck.Device, key *api.Key, handler *TimeIconHandler) {
func timeLoop(info api.StreamDeckInfo, callback func(image image.Image), handler *TimeIconHandler) {
for handler.Running {
img := gg.NewContext(72, 72)
img := gg.NewContext(info.IconSize, info.IconSize)
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.DrawStringAnchored(tString, float64(info.IconSize)/2, float64(info.IconSize)/2, 0.5, 0.5)
img.Clip()
handler.OnSetImage(img.Image(), index, page, dev)
key.Buff = img.Image()
callback(img.Image())
time.Sleep(time.Second)
}
}

func zeroes(i int) (string) {
if i < 10 {
return "0" + strconv.Itoa(i)
} else {
return strconv.Itoa(i)
}
}
Loading