Skip to content

Commit 5976b9d

Browse files
authored
Merge pull request #7 from unix-streamdeck/feat/use-api-text-methods
Refactored to use api text methods
2 parents 1e95bbd + afd5913 commit 5976b9d

File tree

8 files changed

+83
-80
lines changed

8 files changed

+83
-80
lines changed

go.mod

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ module github.com/unix-streamdeck/streamdeckd
33
go 1.14
44

55
require (
6-
github.com/fogleman/gg v1.3.0
76
github.com/godbus/dbus/v5 v5.0.4-0.20200513180336-df5ef3eb7cca
8-
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
9-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
10-
github.com/unix-streamdeck/api v0.0.0-20200828000516-c734a9f6cec5
7+
github.com/unix-streamdeck/api v0.0.0-20200907181211-2f2cfdad366e
118
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b
12-
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
139
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
1410
)

go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
9191
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
9292
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
9393
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
94-
github.com/unix-streamdeck/api v0.0.0-20200828000516-c734a9f6cec5 h1:8WlgvTJMHvfxqmQYyTbfM9TL/BYIm/WfrbDhyA8ihQo=
95-
github.com/unix-streamdeck/api v0.0.0-20200828000516-c734a9f6cec5/go.mod h1:rweAXRgCWdCACCuVhmleidq7HnJEO38zBGDe8uQqZ0w=
94+
github.com/unix-streamdeck/api v0.0.0-20200907181211-2f2cfdad366e h1:/stJJT3mpg42pmda9mJocaeWIPc2RHAYe5no8yNrzz4=
95+
github.com/unix-streamdeck/api v0.0.0-20200907181211-2f2cfdad366e/go.mod h1:Z8bzDHQnWv/2hx9wQXp0/qw6Fp4ty5pFRsgaBG5WYAI=
9696
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b h1:27gVti9+OevmBC2BnWlKC0dQ0eiIHh7PvYTWxt4vb6A=
9797
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b/go.mod h1:i3Eg6kJBslgUk2VIPJ3Cclta2fpV1KJrOnOnR8gnVKY=
9898
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
@@ -105,6 +105,8 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
105105
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
106106
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 h1:5h3ngYt7+vXCDZCup/HkCQgW5XwmSvR/nA2JmJ0RErg=
107107
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
108+
golang.org/x/image v0.0.0-20200801110659-972c09e46d76 h1:U7GPaoQyQmX+CBRWXKrvRzWTbd+slqeSh8uARsIyhAw=
109+
golang.org/x/image v0.0.0-20200801110659-972c09e46d76/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
108110
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
109111
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
110112
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

handlers/counter.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
package handlers
22

33
import (
4-
"github.com/fogleman/gg"
54
"github.com/unix-streamdeck/api"
6-
"golang.org/x/image/font/inconsolata"
75
"image"
6+
"image/draw"
7+
"log"
88
"strconv"
9-
"time"
109
)
1110

12-
func (c *CounterIconHandler) Start(_ api.Key, _ api.StreamDeckInfo, callback func(image image.Image)) {
11+
func (c *CounterIconHandler) Start(_ api.Key, info api.StreamDeckInfo, callback func(image image.Image)) {
1312
if c.Callback == nil {
1413
c.Callback = callback
1514
}
1615
if c.Running {
17-
img := gg.NewContext(72, 72)
18-
img.SetRGB(0, 0, 0)
19-
img.Clear()
20-
img.SetRGB(1, 1, 1)
21-
img.SetFontFace(inconsolata.Regular8x16)
16+
img := image.NewRGBA(image.Rect(0, 0, info.IconSize, info.IconSize))
17+
draw.Draw(img, img.Bounds(), image.Black, image.ZP, draw.Src)
2218
Count := strconv.Itoa(c.Count)
23-
img.DrawStringAnchored(Count, 72/2, 72/2, 0.5, 0.5)
24-
img.Clip()
25-
callback(img.Image())
26-
time.Sleep(250 * time.Millisecond)
19+
imgParsed, err := api.DrawText(img, Count)
20+
if err != nil {
21+
log.Println(err)
22+
} else {
23+
callback(imgParsed)
24+
}
2725
}
2826
}
2927

handlers/gif.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handlers
22

33
import (
4-
"github.com/nfnt/resize"
54
"github.com/unix-streamdeck/api"
65
"image"
76
"image/gif"
@@ -25,7 +24,14 @@ func (s *GifIconHandler) Start(key api.Key, info api.StreamDeckInfo, callback fu
2524
timeDelay := gifs.Delay[0]
2625
frames := make([]image.Image, len(gifs.Image))
2726
for i, frame := range gifs.Image {
28-
frames[i] = resize.Resize(uint(info.IconSize), uint(info.IconSize), frame, resize.Lanczos3)
27+
img := api.ResizeImage(frame, info.IconSize)
28+
if key.Text != "" {
29+
img, err = api.DrawText(img, key.Text)
30+
if err != nil {
31+
log.Println(err)
32+
}
33+
}
34+
frames[i] = img
2935
}
3036
go loop(frames, timeDelay, callback, s)
3137
}

handlers/handlers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ type GifIconHandler struct {
1414

1515
type TimeIconHandler struct {
1616
Running bool
17-
}
17+
}

handlers/time.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package handlers
22

33
import (
4-
"github.com/fogleman/gg"
54
"github.com/unix-streamdeck/api"
6-
"golang.org/x/image/font/inconsolata"
75
"image"
6+
"image/draw"
7+
"log"
88
"time"
99
)
1010

@@ -28,16 +28,16 @@ func (t *TimeIconHandler) Stop() {
2828

2929
func timeLoop(info api.StreamDeckInfo, callback func(image image.Image), handler *TimeIconHandler) {
3030
for handler.Running {
31-
img := gg.NewContext(info.IconSize, info.IconSize)
32-
img.SetRGB(0, 0, 0)
33-
img.Clear()
34-
img.SetRGB(1, 1, 1)
35-
img.SetFontFace(inconsolata.Regular8x16)
31+
img := image.NewRGBA(image.Rect(0, 0, info.IconSize, info.IconSize))
32+
draw.Draw(img, img.Bounds(), image.Black, image.ZP, draw.Src)
3633
t := time.Now()
3734
tString := t.Format("15:04:05")
38-
img.DrawStringAnchored(tString, float64(info.IconSize)/2, float64(info.IconSize)/2, 0.5, 0.5)
39-
img.Clip()
40-
callback(img.Image())
35+
imgParsed, err := api.DrawText(img, tString)
36+
if err != nil {
37+
log.Println(err)
38+
} else {
39+
callback(imgParsed)
40+
}
4141
time.Sleep(time.Second)
4242
}
4343
}

interface.go

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ package main
22

33
import (
44
"context"
5-
"github.com/fogleman/gg"
6-
"github.com/nfnt/resize"
75
"github.com/unix-streamdeck/api"
86
"github.com/unix-streamdeck/streamdeckd/handlers"
9-
"golang.org/x/image/font/inconsolata"
107
"golang.org/x/sync/semaphore"
118
"image"
12-
"image/color"
139
"image/draw"
1410
"log"
1511
"os"
@@ -31,11 +27,7 @@ func LoadImage(path string) (image.Image, error) {
3127
return nil, err
3228
}
3329

34-
return ResizeImage(img), nil
35-
}
36-
37-
func ResizeImage(img image.Image) image.Image {
38-
return resize.Resize(dev.Pixels, dev.Pixels, img, resize.Lanczos3)
30+
return api.ResizeImage(img, sDInfo.IconSize), nil
3931
}
4032

4133
func SetImage(img image.Image, i int, page int) {
@@ -62,7 +54,7 @@ func SetKeyImage(currentKey *api.Key, i int) {
6254
if currentKey.Buff == nil {
6355
if currentKey.Icon == "" {
6456
img := image.NewRGBA(image.Rect(0, 0, int(dev.Pixels), int(dev.Pixels)))
65-
draw.Draw(img, img.Bounds(), image.NewUniform(color.RGBA{0, 0, 0, 255}), image.ZP, draw.Src)
57+
draw.Draw(img, img.Bounds(), image.Black, image.ZP, draw.Src)
6658
currentKey.Buff = img
6759
} else {
6860
img, err := LoadImage(currentKey.Icon)
@@ -73,12 +65,12 @@ func SetKeyImage(currentKey *api.Key, i int) {
7365
currentKey.Buff = img
7466
}
7567
if currentKey.Text != "" {
76-
img := gg.NewContextForImage(currentKey.Buff)
77-
img.SetRGB(1, 1, 1)
78-
img.SetFontFace(inconsolata.Regular8x16)
79-
img.DrawStringAnchored(currentKey.Text, 72/2, 72/2, 0.5, 0.5)
80-
img.Clip()
81-
currentKey.Buff = img.Image()
68+
img, err := api.DrawText(currentKey.Buff, currentKey.Text)
69+
if err != nil {
70+
log.Println(err)
71+
} else {
72+
currentKey.Buff = img
73+
}
8274
}
8375
}
8476
if currentKey.Buff != nil {
@@ -114,6 +106,9 @@ func SetKey(currentKey *api.Key, i int, page int) {
114106
return
115107
}
116108
handler.Start(*currentKey, sDInfo, func(image image.Image) {
109+
if image.Bounds().Max.X != 72 || image.Bounds().Max.Y != 72 {
110+
image = api.ResizeImage(image, sDInfo.IconSize)
111+
}
117112
SetImage(image, i, page)
118113
currentKey.Buff = image
119114
})
@@ -125,6 +120,9 @@ func SetKey(currentKey *api.Key, i int, page int) {
125120
if currentKey.IconHandlerStruct != nil && !currentKey.IconHandlerStruct.IsRunning() {
126121
currentKey.IconHandlerStruct.SetRunning(true)
127122
currentKey.IconHandlerStruct.Start(*currentKey, sDInfo, func(image image.Image) {
123+
if image.Bounds().Max.X != 72 || image.Bounds().Max.Y != 72 {
124+
image = api.ResizeImage(image, sDInfo.IconSize)
125+
}
128126
SetImage(image, i, page)
129127
currentKey.Buff = image
130128
})

main.go

+34-31
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,11 @@ var connectSem = semaphore.NewWeighted(1)
2929
var basicConfig = api.Config{
3030
Pages: []api.Page{
3131
{
32-
api.Key{},
3332
},
3433
},
3534
}
3635

3736
func main() {
38-
var err error
39-
config, err = readConfig()
40-
if err != nil && !os.IsNotExist(err) {
41-
log.Println(err)
42-
} else if os.IsNotExist(err) {
43-
file, err := os.Create(configPath)
44-
if err != nil {
45-
log.Println(err)
46-
}
47-
err = file.Close()
48-
if err != nil {
49-
log.Println(err)
50-
}
51-
config = &basicConfig
52-
err = SaveConfig()
53-
if err != nil {
54-
log.Println(err)
55-
}
56-
}
57-
if len(config.Pages) == 0 {
58-
config.Pages = append(config.Pages, api.Page{})
59-
}
6037
cleanupHook()
6138
go InitDBUS()
6239
attemptConnection()
@@ -66,6 +43,9 @@ func attemptConnection() {
6643
for !isOpen {
6744
_ = openDevice()
6845
if isOpen {
46+
if config == nil {
47+
loadConfig()
48+
}
6949
SetPage(config, p)
7050
if sDbus != nil {
7151
sDInfo.IconSize = int(dev.Pixels)
@@ -117,6 +97,36 @@ func openDevice() error {
11797
return nil
11898
}
11999

100+
func loadConfig() {
101+
var err error
102+
config, err = readConfig()
103+
if err != nil && !os.IsNotExist(err) {
104+
log.Println(err)
105+
} else if os.IsNotExist(err) {
106+
file, err := os.Create(configPath)
107+
if err != nil {
108+
log.Println(err)
109+
}
110+
err = file.Close()
111+
if err != nil {
112+
log.Println(err)
113+
}
114+
config = &basicConfig
115+
page := config.Pages[0]
116+
for i := 0; i < int(dev.Rows)*int(dev.Columns); i++ {
117+
page = append(page, api.Key{})
118+
}
119+
config.Pages[0] = page
120+
err = SaveConfig()
121+
if err != nil {
122+
log.Println(err)
123+
}
124+
}
125+
if len(config.Pages) == 0 {
126+
config.Pages = append(config.Pages, api.Page{})
127+
}
128+
}
129+
120130
func readConfig() (*api.Config, error) {
121131
data, err := ioutil.ReadFile(configPath)
122132
if err != nil {
@@ -169,14 +179,7 @@ func SetConfig(configString string) error {
169179

170180
func ReloadConfig() error {
171181
unmountHandlers()
172-
var err error
173-
config, err = readConfig()
174-
if err != nil && !os.IsNotExist(err) {
175-
return err
176-
}
177-
if len(config.Pages) == 0 {
178-
config.Pages = append(config.Pages, api.Page{})
179-
}
182+
loadConfig()
180183
SetPage(config, p)
181184
return nil
182185
}

0 commit comments

Comments
 (0)