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

Handler package #4

Merged
merged 7 commits into from
Aug 27, 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: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2020, unix-streamdeck
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The actions you can have on a button are:

### D-Bus

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
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

#### Methods

Expand Down
19 changes: 7 additions & 12 deletions CounterHandler.go → handlers/counter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package handlers

import (
"github.com/fogleman/gg"
Expand All @@ -8,28 +8,23 @@ import (
"strconv"
)

type CounterIconHandler struct {
count int
running bool
}

func (c *CounterIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
if c.running {
if c.Running {
img := gg.NewContext(72, 72)
img.SetRGB(0, 0, 0)
img.Clear()
img.SetRGB(1, 1, 1)
img.SetFontFace(inconsolata.Regular8x16)
count := strconv.Itoa(c.count)
img.DrawStringAnchored(count, 72/2, 72/2, 0.5, 0.5)
Count := strconv.Itoa(c.Count)
img.DrawStringAnchored(Count, 72/2, 72/2, 0.5, 0.5)
img.Clip()
SetImage(img.Image(), index, page, dev)
c.OnSetImage(img.Image(), index, page, dev)
key.Buff = img.Image()
}
}

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

type CounterKeyHandler struct{}
Expand All @@ -39,6 +34,6 @@ func (CounterKeyHandler) Key(page int, index int, key *api.Key, dev streamdeck.D
return
}
handler := key.IconHandlerStruct.(*CounterIconHandler)
handler.count += 1
handler.Count += 1
handler.Icon(page, index, key, dev)
}
17 changes: 7 additions & 10 deletions GifHandler.go → handlers/gif.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package main
package handlers

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

type GifIconHandler struct {
running bool
}

func (s *GifIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
s.running = true
s.Running = true
f, err := os.Open(key.Icon)
if err != nil {
log.Println(err)
Expand All @@ -27,13 +24,13 @@ func (s *GifIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.
}

func (s *GifIconHandler) Stop() {
s.running = false
s.Running = false
}

func loop(gifs *gif.GIF, gifIndex int, timeDelay int, page int, index int, dev streamdeck.Device, key *api.Key, s *GifIconHandler) {
for s.running {
img := ResizeImage(gifs.Image[gifIndex])
SetImage(img, index, page, dev)
for s.Running {
img := resize.Resize(dev.Pixels, dev.Pixels, gifs.Image[gifIndex], resize.Lanczos3)
s.OnSetImage(img, index, page, dev)
key.Buff = img
gifIndex++
if gifIndex >= len(gifs.Image) {
Expand Down
23 changes: 23 additions & 0 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package handlers

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

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

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

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

13 changes: 5 additions & 8 deletions TimeHandler.go → handlers/time.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package handlers

import (
"github.com/fogleman/gg"
Expand All @@ -9,21 +9,18 @@ import (
"time"
)

type TimeIconHandler struct{
running bool
}

func (t *TimeIconHandler) Icon(page int, index int, key *api.Key, dev streamdeck.Device) {
t.running = true
t.Running = true
go timeLoop(page, index, dev, key, t)
}

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

func timeLoop(page int, index int, dev streamdeck.Device, key *api.Key, handler *TimeIconHandler) {
for handler.running {
for handler.Running {
img := gg.NewContext(72, 72)
img.SetRGB(0, 0, 0)
img.Clear()
Expand All @@ -33,7 +30,7 @@ func timeLoop(page int, index int, dev streamdeck.Device, key *api.Key, handler
tString := t.Format("15:04:05")
img.DrawStringAnchored(tString, 72/2, 72/2, 0.5, 0.5)
img.Clip()
SetImage(img.Image(), index, page, dev)
handler.OnSetImage(img.Image(), index, page, dev)
key.Buff = img.Image()
time.Sleep(time.Second)
}
Expand Down
14 changes: 9 additions & 5 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/unix-streamdeck/api"
"github.com/unix-streamdeck/driver"
"golang.org/x/image/font/inconsolata"
"github.com/unix-streamdeck/streamdeckd/handlers"
"image"
"image/color"
"image/draw"
Expand Down Expand Up @@ -50,6 +51,7 @@ func SetKey(currentKey *api.Key, i int) {
img, err := LoadImage(currentKey.Icon)
if err != nil {
log.Println(err)
return
}
currentKey.Buff = img
}
Expand All @@ -62,7 +64,9 @@ func SetKey(currentKey *api.Key, i int) {
currentKey.Buff = img.Image()
}
}
SetImage(currentKey.Buff, i, p, dev)
if currentKey.Buff != nil {
SetImage(currentKey.Buff, i, p, dev)
}
}

func SetPage(config *api.Config, page int, dev streamdeck.Device) {
Expand All @@ -77,11 +81,11 @@ func SetPage(config *api.Config, page int, dev streamdeck.Device) {
} else if currentKey.IconHandlerStruct == nil {
var handler api.IconHandler
if currentKey.IconHandler == "Gif" {
handler = &GifIconHandler{true}
handler = &handlers.GifIconHandler{Running:true, OnSetImage: SetImage}
} else if currentKey.IconHandler == "Counter" {
handler = &CounterIconHandler{0, true}
handler = &handlers.CounterIconHandler{Count:0, Running: true, OnSetImage: SetImage}
} else if currentKey.IconHandler == "Time" {
handler = &TimeIconHandler{true}
handler = &handlers.TimeIconHandler{Running:true, OnSetImage: SetImage}
}
if handler == nil {
continue
Expand Down Expand Up @@ -117,7 +121,7 @@ func HandleInput(key *api.Key, page int, index int, dev streamdeck.Device) {
if key.KeyHandlerStruct == nil {
var handler api.KeyHandler
if key.KeyHandler == "Counter" {
handler = CounterKeyHandler{}
handler = handlers.CounterKeyHandler{}
}
if handler == nil {
return
Expand Down