diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..742bd2c --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md index b365e90..51468f7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/CounterHandler.go b/handlers/counter.go similarity index 74% rename from CounterHandler.go rename to handlers/counter.go index da7ab23..d1e543e 100644 --- a/CounterHandler.go +++ b/handlers/counter.go @@ -1,4 +1,4 @@ -package main +package handlers import ( "github.com/fogleman/gg" @@ -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{} @@ -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) } diff --git a/GifHandler.go b/handlers/gif.go similarity index 76% rename from GifHandler.go rename to handlers/gif.go index 820241c..c264fef 100644 --- a/GifHandler.go +++ b/handlers/gif.go @@ -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) @@ -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) { diff --git a/handlers/handlers.go b/handlers/handlers.go new file mode 100644 index 0000000..567ec13 --- /dev/null +++ b/handlers/handlers.go @@ -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) +} + diff --git a/TimeHandler.go b/handlers/time.go similarity index 84% rename from TimeHandler.go rename to handlers/time.go index 84d91bd..81e9180 100644 --- a/TimeHandler.go +++ b/handlers/time.go @@ -1,4 +1,4 @@ -package main +package handlers import ( "github.com/fogleman/gg" @@ -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() @@ -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) } diff --git a/interface.go b/interface.go index 893eacf..21b08c0 100644 --- a/interface.go +++ b/interface.go @@ -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" @@ -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 } @@ -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) { @@ -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 @@ -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