1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"github.com/fogleman/gg"
5
6
"github.com/nfnt/resize"
6
7
"github.com/unix-streamdeck/api"
7
8
"github.com/unix-streamdeck/driver"
8
- "golang.org/x/image/font/inconsolata"
9
9
"github.com/unix-streamdeck/streamdeckd/handlers"
10
+ "golang.org/x/image/font/inconsolata"
11
+ "golang.org/x/sync/semaphore"
10
12
"image"
11
13
"image/color"
12
14
"image/draw"
@@ -15,6 +17,7 @@ import (
15
17
)
16
18
17
19
var p int
20
+ var sem = semaphore .NewWeighted (int64 (1 ))
18
21
19
22
func LoadImage (path string ) (image.Image , error ) {
20
23
f , err := os .Open (path )
@@ -36,12 +39,19 @@ func ResizeImage(img image.Image) image.Image {
36
39
}
37
40
38
41
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 )
39
49
if p == page {
40
50
dev .SetImage (uint8 (i ), img )
41
51
}
42
52
}
43
53
44
- func SetKey (currentKey * api.Key , i int ) {
54
+ func SetKeyImage (currentKey * api.Key , i int ) {
45
55
if currentKey .Buff == nil {
46
56
if currentKey .Icon == "" {
47
57
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) {
74
84
currentPage := config .Pages [page ]
75
85
for i := 0 ; i < len (currentPage ); i ++ {
76
86
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
+ }
80
91
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 }
95
105
}
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
98
111
}
112
+ } else {
113
+ SetImage (currentKey .Buff , i , p , dev )
99
114
}
100
- EmitPage (p )
101
115
}
102
116
103
117
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) {
112
126
SetPage (config , page , dev )
113
127
}
114
128
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
+ }
116
133
}
117
134
if key .Url != "" {
118
135
runCommand ("xdg-open " + key .Url )
0 commit comments