Major restructure and added a few example plugins.
This commit is contained in:
21
examples/counter/Makefile
Normal file
21
examples/counter/Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
GO = go
|
||||
GOFLAGS =
|
||||
INSTALLDIR = "$(APPDATA)\Elgato\StreamDeck\Plugins\dev.samwho.streamdeck.counter.sdPlugin"
|
||||
LOGDIR = "$(APPDATA)\Elgato\StreamDeck\logs"
|
||||
|
||||
.PHONY: test install build logs
|
||||
|
||||
build:
|
||||
$(GO) build $(GOFLAGS)
|
||||
|
||||
test:
|
||||
$(GO) run $(GOFLAGS) main.go -port 12345 -pluginUUID 213 -registerEvent test -info "{\"application\":{\"language\":\"en\",\"platform\":\"mac\",\"version\":\"4.1.0\"},\"plugin\":{\"version\":\"1.1\"},\"devicePixelRatio\":2,\"devices\":[{\"id\":\"55F16B35884A859CCE4FFA1FC8D3DE5B\",\"name\":\"Device Name\",\"size\":{\"columns\":5,\"rows\":3},\"type\":0},{\"id\":\"B8F04425B95855CF417199BCB97CD2BB\",\"name\":\"Another Device\",\"size\":{\"columns\":3,\"rows\":2},\"type\":1}]}"
|
||||
|
||||
install: build
|
||||
rm -rf $(INSTALLDIR)
|
||||
mkdir $(INSTALLDIR)
|
||||
cp *.json $(INSTALLDIR)
|
||||
cp *.exe $(INSTALLDIR)
|
||||
|
||||
logs:
|
||||
tail -f $(LOGDIR)/counter.log
|
112
examples/counter/main.go
Normal file
112
examples/counter/main.go
Normal file
@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/samwho/streamdeck"
|
||||
"github.com/samwho/streamdeck/payload"
|
||||
)
|
||||
|
||||
const (
|
||||
logFile = "C:\\Users\\samwh\\AppData\\Roaming\\Elgato\\StreamDeck\\logs\\streamdeck-livesplit.log"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
Counter int `json:"counter"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("error opening file: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
log.SetOutput(f)
|
||||
|
||||
ctx := context.Background()
|
||||
if err := run(ctx); err != nil {
|
||||
log.Fatalf("%v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context) error {
|
||||
params, err := streamdeck.ParseRegistrationParams(os.Args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := streamdeck.NewClient(ctx, params)
|
||||
setupCounter(client)
|
||||
|
||||
return client.Run()
|
||||
}
|
||||
|
||||
func setupCounter(client *streamdeck.Client) {
|
||||
action := client.Action("dev.samwho.streamdeck.counter")
|
||||
settings := make(map[string]*Settings)
|
||||
|
||||
action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
||||
p := payload.WillAppear{}
|
||||
if err := json.Unmarshal(event.Payload, &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s, ok := settings[event.Context]
|
||||
if !ok {
|
||||
s = &Settings{}
|
||||
settings[event.Context] = s
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(p.Settings, s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bg, err := streamdeck.Image(background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := client.SetImage(ctx, bg, payload.HardwareAndSoftware); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return client.SetTitle(ctx, strconv.Itoa(s.Counter), payload.HardwareAndSoftware)
|
||||
})
|
||||
|
||||
action.RegisterHandler(streamdeck.WillDisappear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
||||
s, _ := settings[event.Context]
|
||||
s.Counter = 0
|
||||
return client.SetSettings(ctx, s)
|
||||
})
|
||||
|
||||
action.RegisterHandler(streamdeck.KeyDown, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
||||
s, ok := settings[event.Context]
|
||||
if !ok {
|
||||
return fmt.Errorf("couldn't find settings for context %v", event.Context)
|
||||
}
|
||||
|
||||
s.Counter++
|
||||
if err := client.SetSettings(ctx, s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return client.SetTitle(ctx, strconv.Itoa(s.Counter), payload.HardwareAndSoftware)
|
||||
})
|
||||
}
|
||||
|
||||
func background() image.Image {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 20, 20))
|
||||
for x := 0; x < 20; x++ {
|
||||
for y := 0; y < 20; y++ {
|
||||
img.Set(x, y, color.Black)
|
||||
}
|
||||
}
|
||||
return img
|
||||
}
|
37
examples/counter/manifest.json
Normal file
37
examples/counter/manifest.json
Normal file
@ -0,0 +1,37 @@
|
||||
{
|
||||
"Actions": [
|
||||
{
|
||||
"Name": "Counter",
|
||||
"States": [
|
||||
{
|
||||
"TitleAlignment": "middle",
|
||||
"FontSize": "24"
|
||||
}
|
||||
],
|
||||
"SupportedInMultiActions": false,
|
||||
"Tooltip": "Count something!",
|
||||
"UUID": "dev.samwho.streamdeck.counter"
|
||||
}
|
||||
],
|
||||
"SDKVersion": 2,
|
||||
"Author": "Sam Rose",
|
||||
"CodePath": "counter.exe",
|
||||
"Description": "Count something!",
|
||||
"Name": "Counter",
|
||||
"URL": "https://samwho.dev",
|
||||
"Version": "0.1",
|
||||
"OS": [
|
||||
{
|
||||
"Platform": "mac",
|
||||
"MinimumVersion" : "10.11"
|
||||
},
|
||||
{
|
||||
"Platform": "windows",
|
||||
"MinimumVersion" : "10"
|
||||
}
|
||||
],
|
||||
"Software":
|
||||
{
|
||||
"MinimumVersion" : "4.1"
|
||||
}
|
||||
}
|
21
examples/cpu/Makefile
Normal file
21
examples/cpu/Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
GO = go
|
||||
GOFLAGS =
|
||||
INSTALLDIR = "$(APPDATA)\Elgato\StreamDeck\Plugins\dev.samwho.streamdeck.cpu.sdPlugin"
|
||||
LOGDIR = "$(APPDATA)\Elgato\StreamDeck\logs"
|
||||
|
||||
.PHONY: test install build logs
|
||||
|
||||
build:
|
||||
$(GO) build $(GOFLAGS)
|
||||
|
||||
test:
|
||||
$(GO) run $(GOFLAGS) main.go -port 12345 -pluginUUID 213 -registerEvent test -info "{\"application\":{\"language\":\"en\",\"platform\":\"mac\",\"version\":\"4.1.0\"},\"plugin\":{\"version\":\"1.1\"},\"devicePixelRatio\":2,\"devices\":[{\"id\":\"55F16B35884A859CCE4FFA1FC8D3DE5B\",\"name\":\"Device Name\",\"size\":{\"columns\":5,\"rows\":3},\"type\":0},{\"id\":\"B8F04425B95855CF417199BCB97CD2BB\",\"name\":\"Another Device\",\"size\":{\"columns\":3,\"rows\":2},\"type\":1}]}"
|
||||
|
||||
install: build
|
||||
rm -rf $(INSTALLDIR)
|
||||
mkdir $(INSTALLDIR)
|
||||
cp *.json $(INSTALLDIR)
|
||||
cp *.exe $(INSTALLDIR)
|
||||
|
||||
logs:
|
||||
tail -f $(LOGDIR)/cpu.log
|
118
examples/cpu/main.go
Normal file
118
examples/cpu/main.go
Normal file
@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/samwho/streamdeck/payload"
|
||||
|
||||
"github.com/samwho/streamdeck"
|
||||
sdcontext "github.com/samwho/streamdeck/context"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
)
|
||||
|
||||
const (
|
||||
logFile = "C:\\Users\\samwh\\AppData\\Roaming\\Elgato\\StreamDeck\\logs\\cpu.log"
|
||||
|
||||
imgX = 72
|
||||
imgY = 72
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("error opening file: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
log.SetOutput(f)
|
||||
|
||||
ctx := context.Background()
|
||||
if err := run(ctx); err != nil {
|
||||
log.Fatalf("%v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context) error {
|
||||
params, err := streamdeck.ParseRegistrationParams(os.Args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := streamdeck.NewClient(ctx, params)
|
||||
setup(client)
|
||||
|
||||
return client.Run()
|
||||
}
|
||||
|
||||
func setup(client *streamdeck.Client) {
|
||||
action := client.Action("dev.samwho.streamdeck.cpu")
|
||||
|
||||
contexts := make(map[string]struct{})
|
||||
|
||||
action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
||||
contexts[event.Context] = struct{}{}
|
||||
return nil
|
||||
})
|
||||
|
||||
action.RegisterHandler(streamdeck.WillDisappear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
||||
delete(contexts, event.Context)
|
||||
return nil
|
||||
})
|
||||
|
||||
readings := make([]float64, imgX, imgX)
|
||||
|
||||
go func() {
|
||||
for range time.Tick(time.Second) {
|
||||
for i := 0; i < imgX-1; i++ {
|
||||
readings[i] = readings[i+1]
|
||||
}
|
||||
|
||||
r, err := cpu.Percent(0, false)
|
||||
if err != nil {
|
||||
log.Printf("error getting CPU reading: %v\n", err)
|
||||
}
|
||||
readings[imgX-1] = r[0]
|
||||
|
||||
for ctxStr := range contexts {
|
||||
ctx := context.Background()
|
||||
ctx = sdcontext.WithContext(ctx, ctxStr)
|
||||
|
||||
img, err := streamdeck.Image(graph(readings))
|
||||
if err != nil {
|
||||
log.Printf("error creating image: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := client.SetImage(ctx, img, payload.HardwareAndSoftware); err != nil {
|
||||
log.Printf("error setting image: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := client.SetTitle(ctx, fmt.Sprintf("CPU\n%d%%", int(r[0])), payload.HardwareAndSoftware); err != nil {
|
||||
log.Printf("error setting title: %v\n", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func graph(readings []float64) image.Image {
|
||||
img := image.NewRGBA(image.Rect(0, 0, imgX, imgY))
|
||||
for x := 0; x < imgX; x++ {
|
||||
reading := readings[x] / 100
|
||||
upto := int(float64(imgY) * reading)
|
||||
for y := 0; y < upto; y++ {
|
||||
img.Set(x, imgY-y, color.RGBA{R: 255, A: 255})
|
||||
}
|
||||
for y := upto; y < imgY; y++ {
|
||||
img.Set(x, imgY-y, color.Black)
|
||||
}
|
||||
}
|
||||
return img
|
||||
}
|
37
examples/cpu/manifest.json
Normal file
37
examples/cpu/manifest.json
Normal file
@ -0,0 +1,37 @@
|
||||
{
|
||||
"Actions": [
|
||||
{
|
||||
"Name": "CPU graph",
|
||||
"States": [
|
||||
{
|
||||
"TitleAlignment": "middle",
|
||||
"FontSize": "14"
|
||||
}
|
||||
],
|
||||
"SupportedInMultiActions": false,
|
||||
"Tooltip": "Show a pretty little CPU graph",
|
||||
"UUID": "dev.samwho.streamdeck.cpu"
|
||||
}
|
||||
],
|
||||
"SDKVersion": 2,
|
||||
"Author": "Sam Rose",
|
||||
"CodePath": "cpu.exe",
|
||||
"Description": "Show a pretty little CPU graph",
|
||||
"Name": "CPU graph",
|
||||
"URL": "https://samwho.dev",
|
||||
"Version": "0.1",
|
||||
"OS": [
|
||||
{
|
||||
"Platform": "mac",
|
||||
"MinimumVersion" : "10.11"
|
||||
},
|
||||
{
|
||||
"Platform": "windows",
|
||||
"MinimumVersion" : "10"
|
||||
}
|
||||
],
|
||||
"Software":
|
||||
{
|
||||
"MinimumVersion" : "4.1"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user