Skip to content

Commit 75706a2

Browse files
authoredAug 27, 2020
Merge pull request #3 from unix-streamdeck/feat/optimizations
Feat/optimizations
2 parents 894e23f + d38dfa9 commit 75706a2

File tree

5 files changed

+59
-31
lines changed

5 files changed

+59
-31
lines changed
 

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ require (
1010
github.com/unix-streamdeck/api v0.0.0-20200818180846-6942d99617b2
1111
github.com/unix-streamdeck/driver v0.0.0-20200817173808-cdaf123c076b
1212
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
13+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4
1314
)

‎go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
115115
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
116116
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
117117
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
118+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
118119
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
119120
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
120121
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

‎handlers/gif.go

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

33
import (
4+
"github.com/nfnt/resize"
45
"github.com/unix-streamdeck/api"
56
"github.com/unix-streamdeck/driver"
6-
"github.com/nfnt/resize"
7+
"image"
78
"image/gif"
89
"log"
910
"os"
@@ -18,24 +19,32 @@ func (s *GifIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.
1819
return
1920
}
2021
gifs, err := gif.DecodeAll(f)
22+
if err != nil {
23+
log.Println(err)
24+
return
25+
}
2126
timeDelay := gifs.Delay[0]
22-
gifIndex := 0
23-
go loop(gifs, gifIndex, timeDelay, page, index, dev, key, s)
27+
frames := make([]image.Image, len(gifs.Image))
28+
for i, frame := range gifs.Image {
29+
frames[i] = resize.Resize(dev.Pixels, dev.Pixels, frame, resize.Lanczos3)
30+
}
31+
go loop(frames, timeDelay, page, index, dev, key, s)
2432
}
2533

2634
func (s *GifIconHandler) Stop() {
2735
s.Running = false
2836
}
2937

30-
func loop(gifs *gif.GIF, gifIndex int, timeDelay int, page int, index int, dev streamdeck.Device, key *api.Key, s *GifIconHandler) {
38+
func loop(frames []image.Image, timeDelay int, page int, index int, dev streamdeck.Device, key *api.Key, s *GifIconHandler) {
39+
gifIndex := 0
3140
for s.Running {
32-
img := resize.Resize(dev.Pixels, dev.Pixels, gifs.Image[gifIndex], resize.Lanczos3)
41+
img := frames[gifIndex]
3342
s.OnSetImage(img, index, page, dev)
3443
key.Buff = img
3544
gifIndex++
36-
if gifIndex >= len(gifs.Image) {
45+
if gifIndex >= len(frames) {
3746
gifIndex = 0
3847
}
3948
time.Sleep(time.Duration(timeDelay * 10000000))
4049
}
41-
}
50+
}

‎interface.go

+40-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package main
22

33
import (
4+
"context"
45
"github.com/fogleman/gg"
56
"github.com/nfnt/resize"
67
"github.com/unix-streamdeck/api"
78
"github.com/unix-streamdeck/driver"
8-
"golang.org/x/image/font/inconsolata"
99
"github.com/unix-streamdeck/streamdeckd/handlers"
10+
"golang.org/x/image/font/inconsolata"
11+
"golang.org/x/sync/semaphore"
1012
"image"
1113
"image/color"
1214
"image/draw"
@@ -15,6 +17,7 @@ import (
1517
)
1618

1719
var p int
20+
var sem = semaphore.NewWeighted(int64(1))
1821

1922
func LoadImage(path string) (image.Image, error) {
2023
f, err := os.Open(path)
@@ -36,12 +39,19 @@ func ResizeImage(img image.Image) image.Image {
3639
}
3740

3841
func SetImage(img image.Image, i int, page int, dev streamdeck.Device) {
42+
ctx := context.Background()
43+
err := sem.Acquire(ctx, 1)
44+
if err != nil {
45+
log.Println(err)
46+
return
47+
}
48+
defer sem.Release(1)
3949
if p == page {
4050
dev.SetImage(uint8(i), img)
4151
}
4252
}
4353

44-
func SetKey(currentKey *api.Key, i int) {
54+
func SetKeyImage(currentKey *api.Key, i int) {
4555
if currentKey.Buff == nil {
4656
if currentKey.Icon == "" {
4757
img := image.NewRGBA(image.Rect(0, 0, int(dev.Pixels), int(dev.Pixels)))
@@ -74,30 +84,34 @@ func SetPage(config *api.Config, page int, dev streamdeck.Device) {
7484
currentPage := config.Pages[page]
7585
for i := 0; i < len(currentPage); i++ {
7686
currentKey := &currentPage[i]
77-
if currentKey.Buff == nil {
78-
if currentKey.IconHandler == "" {
79-
SetKey(currentKey, i)
87+
go SetKey(currentKey, i, page, dev)
88+
}
89+
EmitPage(p)
90+
}
8091

81-
} else if currentKey.IconHandlerStruct == nil {
82-
var handler api.IconHandler
83-
if currentKey.IconHandler == "Gif" {
84-
handler = &handlers.GifIconHandler{Running:true, OnSetImage: SetImage}
85-
} else if currentKey.IconHandler == "Counter" {
86-
handler = &handlers.CounterIconHandler{Count:0, Running: true, OnSetImage: SetImage}
87-
} else if currentKey.IconHandler == "Time" {
88-
handler = &handlers.TimeIconHandler{Running:true, OnSetImage: SetImage}
89-
}
90-
if handler == nil {
91-
continue
92-
}
93-
handler.Icon(page, i, currentKey, dev)
94-
currentKey.IconHandlerStruct = handler
92+
func SetKey(currentKey *api.Key, i int, page int, dev streamdeck.Device) {
93+
if currentKey.Buff == nil {
94+
if currentKey.IconHandler == "" {
95+
SetKeyImage(currentKey, i)
96+
97+
} else if currentKey.IconHandlerStruct == nil {
98+
var handler api.IconHandler
99+
if currentKey.IconHandler == "Gif" {
100+
handler = &handlers.GifIconHandler{Running:true, OnSetImage: SetImage}
101+
} else if currentKey.IconHandler == "Counter" {
102+
handler = &handlers.CounterIconHandler{Count:0, Running: true, OnSetImage: SetImage}
103+
} else if currentKey.IconHandler == "Time" {
104+
handler = &handlers.TimeIconHandler{Running:true, OnSetImage: SetImage}
95105
}
96-
} else {
97-
SetImage(currentKey.Buff, i, p, dev)
106+
if handler == nil {
107+
return
108+
}
109+
handler.Icon(page, i, currentKey, dev)
110+
currentKey.IconHandlerStruct = handler
98111
}
112+
} else {
113+
SetImage(currentKey.Buff, i, p, dev)
99114
}
100-
EmitPage(p)
101115
}
102116

103117
func HandleInput(key *api.Key, page int, index int, dev streamdeck.Device) {
@@ -112,7 +126,10 @@ func HandleInput(key *api.Key, page int, index int, dev streamdeck.Device) {
112126
SetPage(config, page, dev)
113127
}
114128
if key.Brightness != 0 {
115-
_ = dev.SetBrightness(uint8(key.Brightness))
129+
err := dev.SetBrightness(uint8(key.Brightness))
130+
if err != nil {
131+
log.Println(err)
132+
}
116133
}
117134
if key.Url != "" {
118135
runCommand("xdg-open " + key.Url)

‎main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22

33
import (
44
"encoding/json"
5-
"github.com/unix-streamdeck/driver"
65
"github.com/unix-streamdeck/api"
6+
"github.com/unix-streamdeck/driver"
77
_ "image/gif"
88
_ "image/jpeg"
99
_ "image/png"

0 commit comments

Comments
 (0)
Please sign in to comment.