Skip to content

Commit 7c928d8

Browse files
authored
Merge pull request #4 from unix-streamdeck/handlerPackage
Handler package
2 parents a112126 + 510de49 commit 7c928d8

File tree

7 files changed

+81
-36
lines changed

7 files changed

+81
-36
lines changed

LICENSE

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2020, unix-streamdeck
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The actions you can have on a button are:
5858

5959
### D-Bus
6060

61-
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
61+
There is a D-Bus interface built into the daemon, the service name and interface for D-Bus are `com.unixstreamdeck.streamdeckd` and `com/unixstreamdeck/streamdeckd` respectively, and is made up of the following methods/signals
6262

6363
#### Methods
6464

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handlers
22

33
import (
44
"github.com/fogleman/gg"
@@ -8,28 +8,23 @@ import (
88
"strconv"
99
)
1010

11-
type CounterIconHandler struct {
12-
count int
13-
running bool
14-
}
15-
1611
func (c *CounterIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
17-
if c.running {
12+
if c.Running {
1813
img := gg.NewContext(72, 72)
1914
img.SetRGB(0, 0, 0)
2015
img.Clear()
2116
img.SetRGB(1, 1, 1)
2217
img.SetFontFace(inconsolata.Regular8x16)
23-
count := strconv.Itoa(c.count)
24-
img.DrawStringAnchored(count, 72/2, 72/2, 0.5, 0.5)
18+
Count := strconv.Itoa(c.Count)
19+
img.DrawStringAnchored(Count, 72/2, 72/2, 0.5, 0.5)
2520
img.Clip()
26-
SetImage(img.Image(), index, page, dev)
21+
c.OnSetImage(img.Image(), index, page, dev)
2722
key.Buff = img.Image()
2823
}
2924
}
3025

3126
func (c CounterIconHandler) Stop() {
32-
c.running = false
27+
c.Running = false
3328
}
3429

3530
type CounterKeyHandler struct{}
@@ -39,6 +34,6 @@ func (CounterKeyHandler) Key(page int, index int, key *api.Key, dev streamdeck.D
3934
return
4035
}
4136
handler := key.IconHandlerStruct.(*CounterIconHandler)
42-
handler.count += 1
37+
handler.Count += 1
4338
handler.Icon(page, index, key, dev)
4439
}

GifHandler.go handlers/gif.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
package main
1+
package handlers
22

33
import (
44
"github.com/unix-streamdeck/api"
55
"github.com/unix-streamdeck/driver"
6+
"github.com/nfnt/resize"
67
"image/gif"
78
"log"
89
"os"
910
"time"
1011
)
1112

12-
type GifIconHandler struct {
13-
running bool
14-
}
15-
1613
func (s *GifIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
17-
s.running = true
14+
s.Running = true
1815
f, err := os.Open(key.Icon)
1916
if err != nil {
2017
log.Println(err)
@@ -27,13 +24,13 @@ func (s *GifIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.
2724
}
2825

2926
func (s *GifIconHandler) Stop() {
30-
s.running = false
27+
s.Running = false
3128
}
3229

3330
func loop(gifs *gif.GIF, gifIndex int, timeDelay int, page int, index int, dev streamdeck.Device, key *api.Key, s *GifIconHandler) {
34-
for s.running {
35-
img := ResizeImage(gifs.Image[gifIndex])
36-
SetImage(img, index, page, dev)
31+
for s.Running {
32+
img := resize.Resize(dev.Pixels, dev.Pixels, gifs.Image[gifIndex], resize.Lanczos3)
33+
s.OnSetImage(img, index, page, dev)
3734
key.Buff = img
3835
gifIndex++
3936
if gifIndex >= len(gifs.Image) {

handlers/handlers.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package handlers
2+
3+
import (
4+
"image"
5+
"github.com/unix-streamdeck/driver"
6+
)
7+
8+
type CounterIconHandler struct {
9+
Count int
10+
Running bool
11+
OnSetImage func(img image.Image, i int, page int, dev streamdeck.Device)
12+
}
13+
14+
type GifIconHandler struct {
15+
Running bool
16+
OnSetImage func(img image.Image, i int, page int, dev streamdeck.Device)
17+
}
18+
19+
type TimeIconHandler struct{
20+
Running bool
21+
OnSetImage func(img image.Image, i int, page int, dev streamdeck.Device)
22+
}
23+

TimeHandler.go handlers/time.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handlers
22

33
import (
44
"github.com/fogleman/gg"
@@ -9,21 +9,18 @@ import (
99
"time"
1010
)
1111

12-
type TimeIconHandler struct{
13-
running bool
14-
}
1512

1613
func (t *TimeIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
17-
t.running = true
14+
t.Running = true
1815
go timeLoop(page, index, dev, key, t)
1916
}
2017

2118
func (t *TimeIconHandler) Stop() {
22-
t.running = false
19+
t.Running = false
2320
}
2421

2522
func timeLoop(page int, index int, dev streamdeck.Device, key *api.Key, handler *TimeIconHandler) {
26-
for handler.running {
23+
for handler.Running {
2724
img := gg.NewContext(72, 72)
2825
img.SetRGB(0, 0, 0)
2926
img.Clear()
@@ -33,7 +30,7 @@ func timeLoop(page int, index int, dev streamdeck.Device, key *api.Key, handler
3330
tString := t.Format("15:04:05")
3431
img.DrawStringAnchored(tString, 72/2, 72/2, 0.5, 0.5)
3532
img.Clip()
36-
SetImage(img.Image(), index, page, dev)
33+
handler.OnSetImage(img.Image(), index, page, dev)
3734
key.Buff = img.Image()
3835
time.Sleep(time.Second)
3936
}

interface.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/unix-streamdeck/api"
77
"github.com/unix-streamdeck/driver"
88
"golang.org/x/image/font/inconsolata"
9+
"github.com/unix-streamdeck/streamdeckd/handlers"
910
"image"
1011
"image/color"
1112
"image/draw"
@@ -50,6 +51,7 @@ func SetKey(currentKey *api.Key, i int) {
5051
img, err := LoadImage(currentKey.Icon)
5152
if err != nil {
5253
log.Println(err)
54+
return
5355
}
5456
currentKey.Buff = img
5557
}
@@ -62,7 +64,9 @@ func SetKey(currentKey *api.Key, i int) {
6264
currentKey.Buff = img.Image()
6365
}
6466
}
65-
SetImage(currentKey.Buff, i, p, dev)
67+
if currentKey.Buff != nil {
68+
SetImage(currentKey.Buff, i, p, dev)
69+
}
6670
}
6771

6872
func SetPage(config *api.Config, page int, dev streamdeck.Device) {
@@ -77,11 +81,11 @@ func SetPage(config *api.Config, page int, dev streamdeck.Device) {
7781
} else if currentKey.IconHandlerStruct == nil {
7882
var handler api.IconHandler
7983
if currentKey.IconHandler == "Gif" {
80-
handler = &GifIconHandler{true}
84+
handler = &handlers.GifIconHandler{Running:true, OnSetImage: SetImage}
8185
} else if currentKey.IconHandler == "Counter" {
82-
handler = &CounterIconHandler{0, true}
86+
handler = &handlers.CounterIconHandler{Count:0, Running: true, OnSetImage: SetImage}
8387
} else if currentKey.IconHandler == "Time" {
84-
handler = &TimeIconHandler{true}
88+
handler = &handlers.TimeIconHandler{Running:true, OnSetImage: SetImage}
8589
}
8690
if handler == nil {
8791
continue
@@ -117,7 +121,7 @@ func HandleInput(key *api.Key, page int, index int, dev streamdeck.Device) {
117121
if key.KeyHandlerStruct == nil {
118122
var handler api.KeyHandler
119123
if key.KeyHandler == "Counter" {
120-
handler = CounterKeyHandler{}
124+
handler = handlers.CounterKeyHandler{}
121125
}
122126
if handler == nil {
123127
return

0 commit comments

Comments
 (0)