Added support for remote0

This commit is contained in:
mikx 2023-10-31 20:16:41 -04:00
commit 5c6e496688
88 changed files with 7833 additions and 0 deletions

32
.gitignore vendored Normal file
View File

@ -0,0 +1,32 @@
# Vendored libs
/vendor
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# VSCode launch settings
.vscode/launch.json
# GoLand
.idea
# Test graph.png
graph.png
# Debug
cpu.prof
mem.prof
cmd/hwinfostreamdeckplugin/debug
# dep cache (protoc)
.cache/

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.eol": "\n",
"editor.formatOnSave": true,
"go.useLanguageServer": true,
"taskExplorer.pathToMake": "make"
}

BIN
DejaVuSans-Bold.ttf Normal file

Binary file not shown.

40
Makefile Normal file
View File

@ -0,0 +1,40 @@
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
SDPLUGINDIR=./com.exension.hwinfo.sdPlugin
PROTOS=$(wildcard ./*/**/**/*.proto)
PROTOPB=$(PROTOS:.proto=.pb.go)
plugin: proto
$(GOBUILD) -o $(SDPLUGINDIR)/hwinfo.exe ./cmd/hwinfo_streamdeck_plugin
$(GOBUILD) -o $(SDPLUGINDIR)/hwinfo-plugin.exe ./cmd/hwinfo-plugin
cp ../go-hwinfo-hwservice-plugin/bin/hwinfo-plugin.exe $(SDPLUGINDIR)/hwinfo-plugin.exe
-@install-plugin.bat
proto: $(PROTOPB)
$(PROTOPB): $(PROTOS)
.cache/protoc/bin/protoc \
--go_out=Mgrpc/service_config/service_config.proto=/internal/proto/grpc_service_config:. \
--go-grpc_out=Mgrpc/service_config/service_config.proto=/internal/proto/grpc_service_config:. \
--go_opt=paths=source_relative \
--go-grpc_opt=paths=source_relative \
$(<)
# plugin:
# -@kill-streamdeck.bat
# @go build -o com.exension.hwinfo.sdPlugin\\hwinfo.exe github.com/shayne/hwinfo-streamdeck/cmd/hwinfo_streamdeck_plugin
# @xcopy com.exension.hwinfo.sdPlugin $(APPDATA)\\Elgato\\StreamDeck\\Plugins\\com.exension.hwinfo.sdPlugin\\ /E /Q /Y
# @start-streamdeck.bat
debug:
$(GOBUILD) -o $(SDPLUGINDIR)/hwinfo.exe ./cmd/hwinfo_debugger
cp ../go-grpc-hardware-service/bin/hwinfo-plugin.exe $(SDPLUGINDIR)/hwinfo-plugin.exe
-@install-plugin.bat
# @xcopy com.exension.hwinfo.sdPlugin $(APPDATA)\\Elgato\\StreamDeck\\Plugins\\com.exension.hwinfo.sdPlugin\\ /E /Q /Y
release:
-@rm build/com.exension.hwinfo.streamDeckPlugin
@DistributionTool.exe -b -i com.exension.hwinfo.sdPlugin -o build

78
README.md Normal file
View File

@ -0,0 +1,78 @@
# HWiNFO Stream Deck Plugin
## ⚠⚠ Major refactor landed in pre-release v2.0.0, plugin code open sourced, remote monitoring infrastructure support ⚠⚠
---
>## Thank you & Looking for Maintainers
>
>Thank you everyone who has used and enjoyed this plugin. It started as a passion project and I continue to use it day to day. I am happy to finally release the full source on GitHub. When I first built it, it was closed under agreement with the HWiNFO64 project. They have since opened up the shared memory interface and now the plugin is freely open.
>
>I haven't had the time to dedicate to this project in some time and appreciate everyone for hanging in there. I hope to work with some of you who are eager to take the project over. I am happy and ready to hand over the reigns. If there are development questions I'm happy to share my thoughts on the code and structure that exists.
>
>*-Shayne*
---
![alt text](images/demo.gif "HWiNFO64 Stream Deck Plugin Demo")
> NOTICE: HWiNFO64 must be run in Sensors-only mode for the plugin to work.
## Enabling Support in HWiNFO64
> NOTICE: It has been reported that running the "portable" version of HWiNFO64 doesn't work with this plugin. The recommendation is to run the version with the installer until I can figure out the issue.
1. Download and install HWiNFO64, if you haven't already
[HWiNFO Website](https://www.hwinfo.com)
2. Choose "Sensors-only" mode
![alt text](images/sensorsonly.png "HWiNFO64 Sensors Only")
3. Click "Settings"
![alt text](images/clicksettings.png "HWiNFO64 Click Settings")
4. Ensure "Shared Memory Support" is checked
![alt text](images/sharedmemory.png "HWiNFO64 Settings")
5. (Optional) Recommended launch settings
![alt text](images/recommendedsettings.png "Quit HWiNFO64")
6. Click "OK" then, "Run"
> If the plugin doesn't work immediately, you may have to quit and reopen HWiNFO64.
>
> From the system tray:
>
> ![alt text](images/contextquit.png "Quit HWiNFO64")
## Install and Setup the Plugin
1. Download the latest pre-compiled plugin
[Plugin Releases](../../releases)
> When upgrading, first uninstall: within the Stream Deck app choose "More Actions..." (bottom-right), locate "HWiNFO" and choose "Uninstall". Your tiles and settings will be preserved.
2. Double-click to install the plugin
3. Choose "Install" went prompted by Stream Deck
![alt text](images/streamdeckinstall.png "Stream Deck Plugin Installation")
4. Locate "HWiNFO" under "Custom" in the action list
![alt text](images/streamdeckactionlist.png "Stream Deck Action List")
5. Drag the "HWiNFO" action from the list to a tile in the canvas area
![alt text](images/dragaction.gif "Drag Action")
6. Configure the action to display the sensor reading you wish
![alt text](images/configureaction.gif "Configure Action")

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
build/images/demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

BIN
build/images/dragaction.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

31
cmd/hwinfo-plugin/main.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"log"
"github.com/hashicorp/go-plugin"
hwinfoplugin "github.com/shayne/hwinfo-streamdeck/internal/hwinfo/plugin"
hwsensorsservice "github.com/shayne/hwinfo-streamdeck/pkg/service"
)
func main() {
service := hwinfoplugin.StartService()
go func() {
for {
err := service.Recv()
if err != nil {
log.Printf("service recv failed: %v\n", err)
}
}
}()
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: hwsensorsservice.Handshake,
Plugins: map[string]plugin.Plugin{
"hwinfoplugin": &hwsensorsservice.HardwareServicePlugin{Impl: &hwinfoplugin.Plugin{Service: service}},
},
// A non-nil value here enables gRPC serving for this plugin...
GRPCServer: plugin.DefaultGRPCServer,
})
}

View File

@ -0,0 +1,46 @@
package main
import (
"encoding/json"
"flag"
"log"
"os"
"path/filepath"
)
var port = flag.String("port", "", "The port that should be used to create the WebSocket")
var pluginUUID = flag.String("pluginUUID", "", "A unique identifier string that should be used to register the plugin once the WebSocket is opened")
var registerEvent = flag.String("registerEvent", "", "Registration event")
var info = flag.String("info", "", "A stringified json containing the Stream Deck application information and devices information")
func main() {
appdata := os.Getenv("APPDATA")
logpath := filepath.Join(appdata, "Elgato/StreamDeck/Plugins/com.exension.hwinfo.sdPlugin/hwinfo.log")
f, err := os.OpenFile(logpath, os.O_RDWR|os.O_CREATE, 0666)
f.Truncate(0)
if err != nil {
log.Fatalf("OpenFile Log: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.SetFlags(0)
flag.Parse()
args := []string{
"-port",
*port,
"-pluginUUID",
*pluginUUID,
"-registerEvent",
*registerEvent,
"-info",
*info,
}
bytes, err := json.MarshalIndent(args, "", " ")
if err != nil {
log.Fatal("Failed to marshal args", err)
}
log.Println(string(bytes))
}

View File

@ -0,0 +1,69 @@
package main
import (
"flag"
"io/ioutil"
"log"
// "net/http"
// _ "net/http/pprof"
"os"
"path/filepath"
plugin "github.com/shayne/hwinfo-streamdeck/internal/app/hwinfostreamdeckplugin"
)
var port = flag.String("port", "", "The port that should be used to create the WebSocket")
var pluginUUID = flag.String("pluginUUID", "", "A unique identifier string that should be used to register the plugin once the WebSocket is opened")
var registerEvent = flag.String("registerEvent", "", "Registration event")
var info = flag.String("info", "", "A stringified json containing the Stream Deck application information and devices information")
func main() {
// go func() {
// log.Println(http.ListenAndServe("localhost:6060", nil))
// }()
// make sure files are read relative to exe
err := os.Chdir(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalf("Unable to chdir: %v", err)
}
// PRODUCTION
// LOGGING DISABLED:
//
log.SetOutput(ioutil.Discard)
// DEBUG LOGGING:
//
// appdata := os.Getenv("APPDATA")
// logpath := filepath.Join(appdata, "Elgato/StreamDeck/Plugins/com.exension.hwinfo.sdPlugin/hwinfo.log")
// f, err := os.OpenFile(logpath, os.O_RDWR|os.O_CREATE, 0666)
// if err != nil {
// log.Fatalf("OpenFile Log: %v", err)
// }
// err = f.Truncate(0)
// if err != nil {
// log.Fatalf("Truncate Log: %v", err)
// }
// defer func() {
// err := f.Close()
// if err != nil {
// log.Fatalf("File Close: %v", err)
// }
// }()
// log.SetOutput(f)
// log.SetFlags(0)
flag.Parse()
p, err := plugin.NewPlugin(*port, *pluginUUID, *registerEvent, *info)
if err != nil {
log.Fatal("NewPlugin failed:", err)
}
err = p.RunForever()
if err != nil {
log.Fatal("runForever", err)
}
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="6" viewBox="0 0 12 6">
<polygon fill="#8E8E92" fill-rule="evenodd" points="5 4 9 0 10 1 5 6 0 1 1 0"/>
</svg>

After

Width:  |  Height:  |  Size: 171 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 B

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="10" viewBox="0 0 12 10">
<polygon fill="#FFF" points="7.2 7.5 7.2 -1.3 8.7 -1.3 8.6 9.1 2.7 8.7 2.7 7.2" transform="rotate(37 5.718 3.896)"/>
</svg>

After

Width:  |  Height:  |  Size: 210 B

View File

@ -0,0 +1,24 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<g fill="none" fill-rule="evenodd">
<path fill="#9C9C9C" fill-rule="nonzero" d="M1,5 L1,14 L14,14 L14,5 L1,5 Z M0,1 L15,1 L15,15 L0,15 L0,1 Z M14,4 L14,2 L1,2 L1,4 L14,4 Z"/>
<rect width="1" height="1" x="2" fill="#9C9C9C" fill-rule="nonzero"/>
<rect width="1" height="1" x="12" fill="#9C9C9C" fill-rule="nonzero"/>
<g transform="translate(3 7)">
<rect width="1" height="1" x="2" fill="#9C9C9C"/>
<rect width="1" height="1" fill="#666"/>
<rect width="1" height="1" x="4" fill="#9C9C9C"/>
<rect width="1" height="1" x="6" fill="#9C9C9C"/>
<rect width="1" height="1" x="8" fill="#9C9C9C"/>
<rect width="1" height="1" y="2" fill="#9C9C9C"/>
<rect width="1" height="1" x="2" y="2" fill="#9C9C9C"/>
<rect width="1" height="1" x="4" y="2" fill="#9C9C9C"/>
<rect width="1" height="1" x="6" y="2" fill="#9C9C9C"/>
<rect width="1" height="1" x="8" y="2" fill="#9C9C9C"/>
<rect width="1" height="1" y="4" fill="#9C9C9C"/>
<rect width="1" height="1" x="2" y="4" fill="#9C9C9C"/>
<rect width="1" height="1" x="4" y="4" fill="#9C9C9C"/>
<rect width="1" height="1" x="6" y="4" fill="#9C9C9C"/>
<rect width="1" height="1" x="8" y="4" fill="#666"/>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -0,0 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<g fill="#9C9C9C">
<path d="M15,15 L1.77635684e-15,15 L1.77635684e-15,1 L15,1 L15,15 Z M5,7 L5,8 L6,8 L6,7 L5,7 Z M3,7 L3,8 L4,8 L4,7 L3,7 Z M7,7 L7,8 L8,8 L8,7 L7,7 Z M9,7 L9,8 L10,8 L10,7 L9,7 Z M11,7 L11,8 L12,8 L12,7 L11,7 Z M3,9 L3,10 L4,10 L4,9 L3,9 Z M5,9 L5,10 L6,10 L6,9 L5,9 Z M7,9 L7,10 L8,10 L8,9 L7,9 Z M9,9 L9,10 L10,10 L10,9 L9,9 Z M11,9 L11,10 L12,10 L12,9 L11,9 Z M3,11 L3,12 L4,12 L4,11 L3,11 Z M5,11 L5,12 L6,12 L6,11 L5,11 Z M7,11 L7,12 L8,12 L8,11 L7,11 Z M9,11 L9,12 L10,12 L10,11 L9,11 Z M11,11 L11,12 L12,12 L12,11 L11,11 Z M14,4 L14,2 L1,2 L1,4 L14,4 Z"/>
<rect width="1" height="1" x="2"/>
<rect width="1" height="1" x="12"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 764 B

View File

@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" viewBox="0 0 13 13">
<g fill="none">
<path fill="#9C9C9C" d="M13,13 L1.77635684e-15,13 L1.77635684e-15,1 L13,1 L13,13 Z M4,6 L4,7 L5,7 L5,6 L4,6 Z M6,6 L6,7 L7,7 L7,6 L6,6 Z M8,6 L8,7 L9,7 L9,6 L8,6 Z M10,6 L10,7 L11,7 L11,6 L10,6 Z M2,8 L2,9 L3,9 L3,8 L2,8 Z M4,8 L4,9 L5,9 L5,8 L4,8 Z M6,8 L6,9 L7,9 L7,8 L6,8 Z M8,8 L8,9 L9,9 L9,8 L8,8 Z M10,8 L10,9 L11,9 L11,8 L10,8 Z M2,10 L2,11 L3,11 L3,10 L2,10 Z M4,10 L4,11 L5,11 L5,10 L4,10 Z M6,10 L6,11 L7,11 L7,10 L6,10 Z M8,10 L8,11 L9,11 L9,10 L8,10 Z M12,4 L12,2 L1,2 L1,4 L12,4 Z"/>
<polygon fill="#3D3D3D" points="2 6 2 7 3 7 3 6"/>
<polygon fill="#3D3D3D" points="10 10 10 11 11 11 11 10"/>
<rect width="1" height="1" x="2" fill="#9C9C9C"/>
<rect width="1" height="1" x="10" fill="#9C9C9C"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 844 B

View File

@ -0,0 +1,18 @@
body,
.localbody {
height: 100%;
padding: 0;
overflow-x: hidden;
overflow-y: auto;
margin: 0;
-webkit-overflow-scrolling: touch;
}
.localbody {
width: 350px;
margin: 0 auto;
}
.hidden {
display: block;
}

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="9" height="9" viewBox="0 0 9 9">
<path fill="#D8D8D8" d="M4.5,0 C6.98528137,-4.56538782e-16 9,2.01471863 9,4.5 C9,6.98528137 6.98528137,9 4.5,9 C2.01471863,9 3.04359188e-16,6.98528137 0,4.5 C-3.04359188e-16,2.01471863 2.01471863,4.56538782e-16 4.5,0 Z M4,1 L4,6 L5,6 L5,1 L4,1 Z M4.5,8 C4.77614237,8 5,7.77614237 5,7.5 C5,7.22385763 4.77614237,7 4.5,7 C4.22385763,7 4,7.22385763 4,7.5 C4,7.77614237 4.22385763,8 4.5,8 Z"/>
</svg>

After

Width:  |  Height:  |  Size: 479 B

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="9" height="9" viewBox="0 0 9 9">
<polygon fill="#D8D8D8" points="5.2 1 6.2 1 6.2 7 3.2 7 3.2 6 5.2 6" transform="rotate(40 4.677 4)"/>
</svg>

After

Width:  |  Height:  |  Size: 191 B

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="6" height="6" viewBox="0 0 6 6">
<circle cx="3" cy="3" r="3" fill="#FFF"/>
</svg>

After

Width:  |  Height:  |  Size: 131 B

View File

@ -0,0 +1,43 @@
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote,
pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd,
q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt,
dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot,
thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption,
footer, header, hgroup, menu, nav, output, ruby, section, summary, time,
mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline
}
article, aside, details, figcaption, figure, footer, header, hgroup,
menu, nav, section {
display: block
}
body {
line-height: 1
}
ol, ul {
list-style: none
}
blockquote, q {
quotes: none
}
blockquote:before, blockquote:after, q:before, q:after {
content: '';
content: none
}
table {
border-collapse: collapse;
border-spacing: 0
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -0,0 +1,169 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport"
content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,minimal-ui,viewport-fit=cover" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>Property Inspector Samples PI</title>
<link rel="stylesheet" href="css/sdpi.css" />
<!--link rel="stylesheet"
media="screen and (max-width: 1025px)"
href="css/local.css" -->
<link rel="stylesheet" href="css/local.css" />
<style>
#error {
display: none;
}
#sensorSelect {
max-width: 226px;
padding-right: 25px;
font-family: monospace;
}
#sensorSelect option {
font-family: monospace;
}
#readingSelect {
max-width: 226px;
padding-right: 25px;
font-family: monospace;
}
#readingSelect option {
font-family: monospace;
}
</style>
</head>
<body>
<div id="error" class="sdpi-wrapper localbody hiddenx">
<div class="sdpi-heading">Plugin Error</div>
<div class="sdpi-item">
<details open class="message caution">
<summary>Unable To Communicate With HWiNFO64</summary>
<p>
The plugin is unable to communicate with HWiNFO64
</p>
<p>Make sure it's running and configured properly</p>
<p>
For help on how to properly setup HWiNFO64, refer to the
<a class="info" target="_blank"
href="https://github.com/exension/hwinfo-streamdeck/blob/master/README.md#hwinfo-stream-deck-plugin">
documentation
</a>
</p>
</details>
</div>
</div>
<div id="ui" class="sdpi-wrapper localbody hiddenx">
<div class="sdpi-heading">Font Sizes</div>
<div type="range" class="sdpi-item" id="titleFontSize">
<div class="sdpi-item-label">Title</div>
<div class="sdpi-item-value">
<span value="8">8</span>
<input type="range" min="8" max="20" step="0.5" value="10.5" list="steplist" />
<span value="20">20</span>
</div>
</div>
<div type="range" class="sdpi-item" id="valueFontSize">
<div class="sdpi-item-label">Value</div>
<div class="sdpi-item-value">
<span value="8">8</span>
<input type="range" min="8" max="20" step="0.5" value="10.5" />
<span value="20">20</span>
</div>
</div>
<div class="sdpi-heading">HWiNFO Sensors</div>
<div class="sdpi-item">
<div class="sdpi-item-label">Sensor</div>
<select class="sdpi-item-value select" id="sensorSelect" disabled="disabled">
<option>Loading...</option>
</select>
</div>
<div class="sdpi-item" id="readingSelectContainer">
<div class="sdpi-item-label">Reading</div>
<select class="sdpi-item-value select" id="readingSelect" disabled="disabled">
<option></option>
</select>
</div>
<div class="sdpi-heading">Value Params</div>
<div type="sdpi-item" class="sdpi-item" id="paramsgroup">
<div class="sdpi-item-label">Min/Max</div>
<div class="sdpi-item" id="minmax" style="margin-left: -4px">
<input class="sdpi-item-value" style="width:4em" placeholder="Min" inputmode="numeric" pattern="[0-9]*"
type="number" id="min" name="min" />
<input class="sdpi-item-value" style="margin-left: -7px; width:4em" placeholder=" Max" inputmode="numeric"
pattern="[0-9]*" type="number" id="max" name="max" />
</div>
</div>
<details id="advanced_details">
<summary>Advanced</summary>
<div class="sdpi-item" id="format">
<div class="sdpi-item-label">Format</div>
<input class="sdpi-item-value" placeholder="%.0f (no decimals)" type="text" name="format" value="" />
</div>
<div class="sdpi-item">
<details open class="message question noInnerMargins">
<summary>Help</summary>
<p>Format can be used to modify format of the value.</p>
<p>
For more information on how to use this field,
<a target="_BLANK" href="#"
onclick="openUrl('https://gobyexample.com/string-formatting'); return false;">click here</a>.
</p>
</details>
</div>
<div class="sdpi-item" id="divisor">
<div class="sdpi-item-label">Divisor</div>
<input class="sdpi-item-value" placeholder="e.g. 125 (for bytes to megabits)" inputmode="numeric"
pattern="[0-9]*" min="1" type="number" name="divisor" />
</div>
<div class="sdpi-item">
<details open class="message question noInnerMargins">
<summary>Help</summary>
<p>Divisor can be used to convert the value.</p>
<p>
For example, converting bytes to megabits, set the divisor to:
"125"
</p>
</details>
</div>
</details>
<div class="sdpi-heading">Graph Colors</div>
<div type="color" class="sdpi-item" id="colorselection">
<div class="sdpi-item-label">Background</div>
<input type="color" class="sdpi-item-value" id="background" value="#000000" />
<div class="sdpi-item-label">Value Text</div>
<input type="color" class="sdpi-item-value" id="valuetext" value="#ffffff" />
</div>
<div type="color" class="sdpi-item" id="colorselection">
<div class="sdpi-item-label">Foreground</div>
<input type="color" class="sdpi-item-value" id="foreground" value="#005128" />
<div class="sdpi-item-label">Highlight</div>
<input type="color" class="sdpi-item-value" id="highlight" value="#009e00" />
</div>
</div>
<!-- <script src="echomd.js"></script> -->
<script src="index_pi.js"></script>
</body>
</html>

View File

@ -0,0 +1,571 @@
// this is our global websocket, used to communicate from/to Stream Deck software
// and some info about our plugin, as sent by Stream Deck software
var websocket = null,
uuid = null,
actionInfo = {},
inInfo = {},
runningApps = [],
isQT = navigator.appVersion.includes("QtWebEngine"),
onchangeevt = "onchange"; // 'oninput'; // change this, if you want interactive elements act on any change, or while they're modified
function connectElgatoStreamDeckSocket(inPort, inUUID, inRegisterEvent, inInfo, inActionInfo) {
uuid = inUUID;
// please note: the incoming arguments are of type STRING, so
// in case of the inActionInfo, we must parse it into JSON first
actionInfo = JSON.parse(inActionInfo); // cache the info
inInfo = JSON.parse(inInfo);
websocket = new WebSocket("ws://localhost:" + inPort);
/** Since the PI doesn't have access to native settings
* Stream Deck sends some color settings to PI
* We use these to adjust some styles (e.g. highlight-colors for checkboxes)
*/
addDynamicStyles(inInfo.colors, "connectSocket");
initPropertyInspector(5);
// if connection was established, the websocket sends
// an 'onopen' event, where we need to register our PI
websocket.onopen = function () {
var json = {
event: inRegisterEvent,
uuid: inUUID,
};
// register property inspector to Stream Deck
websocket.send(JSON.stringify(json));
sendValueToPlugin("propertyInspectorConnected", "property_inspector");
};
websocket.onmessage = function (evt) {
// Received message from Stream Deck
var jsonObj = JSON.parse(evt.data);
var event = jsonObj["event"];
if (
"boolean" === typeof getPropFromString(jsonObj, "payload.error") &&
event === "sendToPropertyInspector"
) {
if (jsonObj.payload.error === true) {
document.querySelector("#ui").style = "display:none";
document.querySelector("#error").style = "display:block";
} else if (jsonObj.payload.message === "show_ui") {
document.querySelector("#ui").style = "display:block";
document.querySelector("#error").style = "display:none";
sendValueToPlugin("propertyInspectorConnected", "property_inspector");
}
}
if (
getPropFromString(jsonObj, "payload.sensors") &&
event === "sendToPropertyInspector"
) {
addSensors(
document.querySelector("#sensorSelect"),
jsonObj.payload.sensors,
jsonObj.payload.settings
);
}
if (
getPropFromString(jsonObj, "payload.readings") &&
event === "sendToPropertyInspector"
) {
addReadings(
document.querySelector("#readingSelect"),
jsonObj.payload.readings,
jsonObj.payload.settings
);
}
if (getPropFromString(jsonObj, "payload.settings")) {
var settings = jsonObj.payload.settings;
if (settings.min === 0 && settings.max === 0) {
// don't show 0, 0 min/max
} else {
document.querySelector("#min").value = settings.min;
document.querySelector("#max").value = settings.max;
}
document.querySelector("#format input").value = settings.format;
document.querySelector("#divisor input").value = settings.divisor || "";
if (
settings.format.length > 0 ||
(settings.divisor && settings.divisor.length > 0)
) {
var attr = document.createAttribute("open");
attr.value = "open";
document
.querySelector("#advanced_details")
.attributes.setNamedItem(attr);
}
if (settings.foregroundColor !== "") {
document.querySelector("#foreground").value = settings.foregroundColor;
}
if (settings.backgroundColor !== "") {
document.querySelector("#background").value = settings.backgroundColor;
}
if (settings.highlightColor !== "") {
document.querySelector("#highlight").value = settings.highlightColor;
}
if (settings.valueTextColor !== "") {
document.querySelector("#valuetext").value = settings.valueTextColor;
}
if (settings.titleFontSize !== "") {
document.querySelector("#titleFontSize input").value =
settings.titleFontSize || 10.5;
}
if (settings.valueFontSize !== "") {
document.querySelector("#valueFontSize input").value =
settings.valueFontSize || 10.5;
}
}
};
}
function sortBy(key) {
return function (a, b) {
if (a[key] > b[key]) return 1;
if (b[key] > a[key]) return -1;
return 0;
};
}
function addSensors(el, sensors, settings) {
var i;
for (i = el.options.length - 1; i >= 0; i--) {
el.remove(i);
}
el.removeAttribute("disabled");
var option = document.createElement("option");
option.text = "Choose a sensor";
option.disabled = true;
if (settings.isValid !== true) {
option.selected = true;
}
el.add(option);
var sortByName = sortBy("name");
sensors.sort(sortByName).forEach((s) => {
var option = document.createElement("option");
option.text = s.name;
option.value = s.uid;
if (settings.isValid === true && settings.sensorUid === s.uid) {
option.selected = true;
setTimeout(function () {
var event = new Event("change");
el.dispatchEvent(event);
}, 0);
}
el.add(option);
});
}
function addReadings(el, readings, settings) {
var i;
for (i = el.options.length - 1; i >= 0; i--) {
el.remove(i);
}
el.removeAttribute("disabled");
var option = document.createElement("option");
option.text = "Choose a reading";
option.disabled = true;
if (settings.isValid !== true) {
option.selected = true;
}
el.add(option);
var sortByLabel = sortBy("label");
var maxL = 0;
readings.sort(sortByLabel).forEach((r) => {
var l = r.prefix.length;
if (l > maxL) {
maxL = l;
}
});
readings.sort(sortByLabel).forEach((r) => {
var option = document.createElement("option");
option.style = "white-space: pre";
var spaces = "&nbsp;";
for (i = 0; i < maxL - r.prefix.length; ++i) {
spaces += "&nbsp;";
}
option.innerHTML = `${r.prefix}${spaces}${r.label}`;
option.value = r.id;
if (settings.isValid === true && settings.readingId === r.id) {
option.selected = true;
}
el.add(option);
});
}
function initPropertyInspector(initDelay) {
prepareDOMElements(document);
}
function revealSdpiWrapper() {
const el = document.querySelector(".sdpi-wrapper");
el && el.classList.remove("hidden");
}
// openUrl in default browser
function openUrl(url) {
if (websocket && websocket.readyState === 1) {
const json = {
event: "openUrl",
payload: {
url: url,
},
};
websocket.send(JSON.stringify(json));
}
}
// our method to pass values to the plugin
function sendValueToPlugin(value, param) {
if (websocket && websocket.readyState === 1) {
const json = {
action: actionInfo["action"],
event: "sendToPlugin",
context: uuid,
payload: {
[param]: value,
},
};
websocket.send(JSON.stringify(json));
}
}
if (!isQT) {
document.addEventListener("DOMContentLoaded", function () {
initPropertyInspector(100);
});
}
/** the beforeunload event is fired, right before the PI will remove all nodes */
window.addEventListener("beforeunload", function (e) {
e.preventDefault();
sendValueToPlugin("propertyInspectorWillDisappear", "property_inspector");
// Don't set a returnValue to the event, otherwise Chromium with throw an error. // e.returnValue = '';
});
/** the pagehide event is fired, when the view disappears */
/*
window.addEventListener('pagehide', function (event) {
console.log('%c%s','background: green; font-size: 22px; font-weight: bold;','window --->> pagehide.');
sendValueToPlugin('propertyInspectorPagehide', 'property_inspector');
});
*/
/** the unload event is fired, when the PI will finally disappear */
/*
window.addEventListener('unload', function (event) {
console.log('%c%s','background: orange; font-size: 22px; font-weight: bold;','window --->> onunload.');
sendValueToPlugin('propertyInspectorDisconnected', 'property_inspector');
});
*/
/** if you prefer, you can apply these listeners to PI's body, like so:
*
* <body onpagehide="sendValueToPlugin('propertyInspectorPagehide', 'property_inspector');">
*
* <body onunload="sendValueToPlugin('propertyInspectorDisconnected', 'property_inspector');">
*/
/** CREATE INTERACTIVE HTML-DOM
* where elements can be clicked or act on their 'change' event.
* Messages are then processed using the 'handleSdpiItemClick' method below.
*/
function prepareDOMElements(baseElement) {
baseElement = baseElement || document;
Array.from(baseElement.querySelectorAll(".sdpi-item-value")).forEach(
(el, i) => {
const elementsToClick = [
"BUTTON",
"OL",
"UL",
"TABLE",
"METER",
"PROGRESS",
"CANVAS",
].includes(el.tagName);
const evt = elementsToClick ? "onclick" : onchangeevt || "onchange";
// console.log(el.type, el.tagName, elementsToClick, el, evt);
/** Look for <input><span> combinations, where we consider the span as label for the input
* we don't use `labels` for that, because a range could have 2 labels.
*/
const inputGroup = el.querySelectorAll("input, span");
if (inputGroup.length === 2) {
const offs = inputGroup[0].tagName === "INPUT" ? 1 : 0;
inputGroup[offs].innerText = inputGroup[1 - offs].value;
inputGroup[1 - offs]["oninput"] = function () {
inputGroup[offs].innerText = inputGroup[1 - offs].value;
};
}
/** We look for elements which have an 'clickable' attribute
* we use these e.g. on an 'inputGroup' (<span><input type="range"><span>) to adjust the value of
* the corresponding range-control
*/
Array.from(el.querySelectorAll(".clickable")).forEach((subel, subi) => {
subel["onclick"] = function (e) {
handleSdpiItemClick(e.target, subi);
};
});
el[evt] = function (e) {
handleSdpiItemClick(e.target, i);
};
}
);
baseElement.querySelectorAll("textarea").forEach((e) => {
const maxl = e.getAttribute("maxlength");
e.targets = baseElement.querySelectorAll(`[for='${e.id}']`);
if (e.targets.length) {
let fn = () => {
for (let x of e.targets) {
x.innerText = maxl
? `${e.value.length}/${maxl}`
: `${e.value.length}`;
}
};
fn();
e.onkeyup = fn;
}
});
}
function handleSdpiItemClick(e, idx) {
/** Following items are containers, so we won't handle clicks on them */
if (["OL", "UL", "TABLE"].includes(e.tagName)) {
return;
}
// console.log('--- handleSdpiItemClick ---', e, `type: ${e.type}`, e.tagName, `inner: ${e.innerText}`);
/** SPANS are used inside a control as 'labels'
* If a SPAN element calls this function, it has a class of 'clickable' set and is thereby handled as
* clickable label.
*/
if (e.tagName === "SPAN") {
const inp = e.parentNode.querySelector("input");
if (e.getAttribute("value")) {
return inp && (inp.value = e.getAttribute("value"));
}
}
const selectedElements = [];
const isList = ["LI", "OL", "UL", "DL", "TD"].includes(e.tagName);
const sdpiItem = e.closest(".sdpi-item");
const sdpiItemGroup = e.closest(".sdpi-item-group");
let sdpiItemChildren = isList
? sdpiItem.querySelectorAll(e.tagName === "LI" ? "li" : "td")
: sdpiItem.querySelectorAll(".sdpi-item-child > input");
if (isList) {
const siv = e.closest(".sdpi-item-value");
if (!siv.classList.contains("multi-select")) {
for (let x of sdpiItemChildren) x.classList.remove("selected");
}
if (!siv.classList.contains("no-select")) {
e.classList.toggle("selected");
}
}
if (sdpiItemGroup && !sdpiItemChildren.length) {
for (let x of ["input", "meter", "progress"]) {
sdpiItemChildren = sdpiItemGroup.querySelectorAll(x);
if (sdpiItemChildren.length) break;
}
}
if (e.selectedIndex) {
idx = e.selectedIndex;
} else {
sdpiItemChildren.forEach((ec, i) => {
if (ec.classList.contains("selected")) {
selectedElements.push(ec.innerText);
}
if (ec === e) idx = i;
});
}
const returnValue = {
key: e.id || sdpiItem.id,
value: isList
? e.innerText
: e.value
? e.type === "file"
? decodeURIComponent(e.value.replace(/^C:\\fakepath\\/, ""))
: e.value
: e.getAttribute("value"),
group: sdpiItemGroup ? sdpiItemGroup.id : false,
index: idx,
selection: selectedElements,
checked: e.checked,
};
/** Just simulate the original file-selector:
* If there's an element of class '.sdpi-file-info'
* show the filename there
*/
if (e.type === "file") {
const info = sdpiItem.querySelector(".sdpi-file-info");
if (info) {
const s = returnValue.value.split("/").pop();
info.innerText =
s.length > 28
? s.substr(0, 10) + "..." + s.substr(s.length - 10, s.length)
: s;
}
}
sendValueToPlugin(returnValue, "sdpi_collection");
}
function updateKeyForDemoCanvas(cnv) {
sendValueToPlugin(
{
key: "your_canvas",
value: cnv.toDataURL(),
},
"sdpi_collection"
);
}
/** Stream Deck software passes system-highlight color information
* to Property Inspector. Here we 'inject' the CSS styles into the DOM
* when we receive this information. */
function addDynamicStyles(clrs, fromWhere) {
const node =
document.getElementById("#sdpi-dynamic-styles") ||
document.createElement("style");
if (!clrs.mouseDownColor)
clrs.mouseDownColor = fadeColor(clrs.highlightColor, -100);
const clr = clrs.highlightColor.slice(0, 7);
const clr1 = fadeColor(clr, 100);
const clr2 = fadeColor(clr, 60);
const metersActiveColor = fadeColor(clr, -60);
node.setAttribute("id", "sdpi-dynamic-styles");
node.innerHTML = `
input[type="radio"]:checked + label span,
input[type="checkbox"]:checked + label span {
background-color: ${clrs.highlightColor};
}
input[type="radio"]:active:checked + label span,
input[type="radio"]:active + label span,
input[type="checkbox"]:active:checked + label span,
input[type="checkbox"]:active + label span {
background-color: ${clrs.mouseDownColor};
}
input[type="radio"]:active + label span,
input[type="checkbox"]:active + label span {
background-color: ${clrs.buttonPressedBorderColor};
}
td.selected,
td.selected:hover,
li.selected:hover,
li.selected {
color: white;
background-color: ${clrs.highlightColor};
}
.sdpi-file-label > label:active,
.sdpi-file-label.file:active,
label.sdpi-file-label:active,
label.sdpi-file-info:active,
input[type="file"]::-webkit-file-upload-button:active,
button:active {
background-color: ${clrs.buttonPressedBackgroundColor};
color: ${clrs.buttonPressedTextColor};
border-color: ${clrs.buttonPressedBorderColor};
}
::-webkit-progress-value,
meter::-webkit-meter-optimum-value {
background: linear-gradient(${clr2}, ${clr1} 20%, ${clr} 45%, ${clr} 55%, ${clr2})
}
::-webkit-progress-value:active,
meter::-webkit-meter-optimum-value:active {
background: linear-gradient(${clr}, ${clr2} 20%, ${metersActiveColor} 45%, ${metersActiveColor} 55%, ${clr})
}
`;
document.body.appendChild(node);
}
/** UTILITIES */
/** Helper function to construct a list of running apps
* from a template string.
* -> information about running apps is received from the plugin
*/
function sdpiCreateList(el, obj, cb) {
if (el) {
el.style.display = obj.value.length ? "block" : "none";
Array.from(document.querySelectorAll(`.${el.id}`)).forEach((subel, i) => {
subel.style.display = obj.value.length ? "flex" : "none";
});
if (obj.value.length) {
el.innerHTML = `<div class="sdpi-item" ${obj.type ? `class="${obj.type}"` : ""
} id="${obj.id || window.btoa(new Date().getTime().toString()).substr(0, 8)
}">
<div class="sdpi-item-label">${obj.label || ""}</div>
<ul class="sdpi-item-value ${obj.selectionType ? obj.selectionType : ""
}">
${obj.value.map((e) => `<li>${e.name}</li>`).join("")}
</ul>
</div>`;
setTimeout(function () {
prepareDOMElements(el);
if (cb) cb();
}, 10);
return;
}
}
if (cb) cb();
}
/** get a JSON property from a (dot-separated) string
* Works on nested JSON, e.g.:
* jsn = {
* propA: 1,
* propB: 2,
* propC: {
* subA: 3,
* subB: {
* testA: 5,
* testB: 'Hello'
* }
* }
* }
* getPropFromString(jsn,'propC.subB.testB') will return 'Hello';
*/
const getPropFromString = (jsn, str, sep = ".") => {
const arr = str.split(sep);
return arr.reduce(
(obj, key) => (obj && obj.hasOwnProperty(key) ? obj[key] : undefined),
jsn
);
};
/*
Quick utility to lighten or darken a color (doesn't take color-drifting, etc. into account)
Usage:
fadeColor('#061261', 100); // will lighten the color
fadeColor('#200867'), -100); // will darken the color
*/
function fadeColor(col, amt) {
const min = Math.min,
max = Math.max;
const num = parseInt(col.replace(/#/g, ""), 16);
const r = min(255, max((num >> 16) + amt, 0));
const g = min(255, max((num & 0x0000ff) + amt, 0));
const b = min(255, max(((num >> 8) & 0x00ff) + amt, 0));
return "#" + (g | (b << 8) | (r << 16)).toString(16).padStart(6, 0);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -0,0 +1,41 @@
{
"SDKVersion": 2,
"Software": {
"MinimumVersion": "4.1"
},
"Actions": [
{
"Icon": "icon",
"Name": "HWiNFO",
"States": [
{
"Image": "defaultImage",
"TitleAlignment": "top",
"FontSize": "9",
"TitleColor": "#b7b7b7",
"ShowTitle": false
}
],
"SupportedInMultiActions": false,
"Tooltip": "Display sensor readings from HWiNFO",
"UUID": "com.exension.hwinfo.reading"
}
],
"Author": "shayne",
"CodePathWin": "hwinfo.exe",
"PropertyInspectorPath": "index_pi.html",
"Description": "Display sensor readings from HWiNFO64. This plugin is not affiliated with HWiNFO64, for more information and to download HWiNFO64 visit https://www.hwinfo.com",
"Name": "HWiNFO",
"Icon": "pluginIcon",
"URL": "https://github.com/shayne/hwinfo-streamdeck",
"Version": "2.0.5",
"ApplicationsToMonitor": {
"windows": ["HWiNFO64.EXE", "HWiNFO64.exe"]
},
"OS": [
{
"Platform": "windows",
"MinimumVersion": "10"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

Binary file not shown.

14
examples/bench/main.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
func main() {
_, filename, _, _ := runtime.Caller(0)
fmt.Println("Current test filename: " + filename)
os.Chdir(filepath.Dir(filename))
}

View File

@ -0,0 +1,61 @@
package main
import (
"fmt"
"image/color"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/shayne/hwinfo-streamdeck/pkg/graph"
)
func BenchmarkFoo(b *testing.B) {
_, filename, _, _ := runtime.Caller(0)
fmt.Println("Current test filename: " + filename)
os.Chdir(filepath.Dir(filename))
g := graph.NewGraph(72, 72, 0., 100.,
&color.RGBA{255, 255, 255, 255},
&color.RGBA{0, 0, 0, 255},
&color.RGBA{255, 255, 255, 255})
g.SetLabel(0, "CPU °C", 15, &color.RGBA{183, 183, 183, 255})
g.SetLabel(1, "5%", 40, &color.RGBA{255, 255, 255, 255})
data := []float64{
0., 0., 0., 0., 0.,
10., 10., 10., 10., 10.,
20., 20., 20., 20., 20.,
30., 30., 30., 30., 30.,
40., 40., 40., 40., 40.,
50., 50., 50., 50., 50.,
60., 60., 60., 60., 60.,
70., 70., 70., 70., 70.,
80., 80., 80., 80., 80.,
90., 90., 90., 90., 90.,
100., 100., 100., 100., 100.,
// 0., 0., 0., 0., 0.,
// 10., 10., 10., 10., 10.,
// 20., 20., 20., 20., 20.,
// 30., 30., 30., 30., 30.,
// 40., 40., 40., 40., 40.,
// 50., 50., 50., 50., 50.,
// 60., 60., 60., 60., 60.,
// 70., 70., 70., 70., 70.,
// 80., 80., 80., 80., 80.,
// 90., 90., 90., 90., 90.,
// 100., 100., 100., 100., 100.,
}
_ = data // FIXME
for i := 0; i < b.N; i++ {
// FIXME: updateChart does not exist
// for _, v := range data {
// g.UpdateChart(v)
// }
_, err := g.EncodePNG()
if err != nil {
b.Fatal("failed to encode png")
}
}
}

86
examples/graph/main.go Normal file
View File

@ -0,0 +1,86 @@
package main
import (
"fmt"
"image/color"
"io/ioutil"
"log"
"math/rand"
"time"
"github.com/shayne/hwinfo-streamdeck/pkg/graph"
)
const (
dev = 40
)
func main() {
g := graph.NewGraph(72, 72, 0., 100.,
&color.RGBA{255, 255, 255, 255},
&color.RGBA{0, 0, 0, 255},
&color.RGBA{255, 255, 255, 255})
g.SetLabel(0, "CPU °C", 15, &color.RGBA{183, 183, 183, 255})
g.SetLabel(1, "5%", 40, &color.RGBA{255, 255, 255, 255})
data := makeFakeData()
// data := []float64{
// 0., 0., 0., 0., 0.,
// 10., 10., 10., 10., 10.,
// 20., 20., 20., 20., 20.,
// 30., 30., 30., 30., 30.,
// 40., 40., 40., 40., 40.,
// 50., 50., 50., 50., 50.,
// 60., 60., 60., 60., 60.,
// 70., 70., 70., 70., 70.,
// 80., 80., 80., 80., 80.,
// 90., 90., 90., 90., 90.,
// 100., 100., 100., 100., 100.,
// }
for _, v := range data {
g.Update(v)
}
lastv := data[len(data)-1]
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
ndev := r.Intn(dev) - (dev / 2)
v := lastv + float64(ndev)
if v > 100 {
v = 100
} else if v < 0 {
v = 0
}
fmt.Println(v)
g.Update(v)
lastv = v
bts, err := g.EncodePNG()
if err != nil {
log.Fatal("failed to encode png")
}
err = ioutil.WriteFile("graph.png", bts, 0644)
if err != nil {
log.Fatal("failed to write png")
}
}
}
}
func makeFakeData() []float64 {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
data := make([]float64, 72)
v := r.Intn(100)
lastv := v
data[0] = float64(v)
for i := 1; i < 72; i++ {
ndev := r.Intn(dev) - (dev / 2)
v = lastv + ndev
data[i] = float64(v)
}
return data
}

28
go.mod Normal file
View File

@ -0,0 +1,28 @@
module github.com/shayne/hwinfo-streamdeck
go 1.19
require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/golang/protobuf v1.5.2
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-plugin v1.4.8
github.com/shayne/go-winpeg v0.0.0-20200807055429-803ae16a07c6
golang.org/x/image v0.3.0
golang.org/x/sys v0.4.0
golang.org/x/text v0.6.0
google.golang.org/grpc v1.52.0
google.golang.org/protobuf v1.28.1
)
require (
github.com/fatih/color v1.13.0 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/oklog/run v1.1.0 // indirect
golang.org/x/net v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5 // indirect
)

91
go.sum Normal file
View File

@ -0,0 +1,91 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/go-hclog v1.4.0 h1:ctuWFGrhFha8BnnzxqeRGidlEcQkDyL5u8J8t5eA11I=
github.com/hashicorp/go-hclog v1.4.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-plugin v1.4.8 h1:CHGwpxYDOttQOY7HOWgETU9dyVjOXzniXDqJcYJE1zM=
github.com/hashicorp/go-plugin v1.4.8/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shayne/go-winpeg v0.0.0-20200807055429-803ae16a07c6 h1:HKw6S9JJ7+Z4jc0ygiefl253IwWgk4/ohsQ/5tFWVs0=
github.com/shayne/go-winpeg v0.0.0-20200807055429-803ae16a07c6/go.mod h1:gkGydh7Q4gy2dbfmfQ5++JV6nn8jb1iRj62BWtNPQOg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.3.0 h1:HTDXbdK9bjfSWkPzDJIw89W8CAtfFGduujWs33NLLsg=
golang.org/x/image v0.3.0/go.mod h1:fXd9211C/0VTlYuAcOhW8dY/RtEJqODXOWBDpmYBf+A=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5 h1:wJT65XLOzhpSPCdAmmKfz94SlmnQzDzjm3Cj9k3fsXY=
google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/grpc v1.52.0 h1:kd48UiU7EHsV4rnLyOJRuP/Il/UHE7gdDAQ+SZI7nZk=
google.golang.org/grpc v1.52.0/go.mod h1:pu6fVzoFb+NBYNAvQL08ic+lvB2IojljRYuun5vorUY=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

BIN
images/clicksettings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
images/configureaction.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

BIN
images/contextquit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
images/demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

BIN
images/dragaction.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
images/sensorsonly.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

BIN
images/sharedmemory.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

4
install-plugin.bat Normal file
View File

@ -0,0 +1,4 @@
@echo off
CALL .\kill-streamdeck.bat
xcopy com.exension.hwinfo.sdPlugin %APPDATA%\\Elgato\\StreamDeck\\Plugins\\com.exension.hwinfo.sdPlugin\\ /E /Q /Y
CALL .\start-streamdeck.bat

View File

@ -0,0 +1,54 @@
package hwinfostreamdeckplugin
import (
"fmt"
"sync"
"time"
)
type actionManager struct {
mux sync.RWMutex
actions map[string]*actionData
}
func newActionManager() *actionManager {
return &actionManager{actions: make(map[string]*actionData)}
}
func (tm *actionManager) Run(updateTiles func(*actionData)) {
go func() {
ticker := time.NewTicker(time.Second)
for range ticker.C {
tm.mux.RLock()
for _, data := range tm.actions {
if data.settings.IsValid {
updateTiles(data)
}
}
tm.mux.RUnlock()
}
}()
}
func (tm *actionManager) SetAction(action, context string, settings *actionSettings) {
tm.mux.Lock()
tm.actions[context] = &actionData{action, context, settings}
tm.mux.Unlock()
}
func (tm *actionManager) RemoveAction(context string) {
tm.mux.Lock()
delete(tm.actions, context)
tm.mux.Unlock()
}
func (tm *actionManager) getSettings(context string) (actionSettings, error) {
tm.mux.RLock()
data, ok := tm.actions[context]
tm.mux.RUnlock()
if !ok {
return actionSettings{}, fmt.Errorf("getSettings invalid key: %s", context)
}
// return full copy of settings, not reference to stored settings
return *data.settings, nil
}

View File

@ -0,0 +1,219 @@
package hwinfostreamdeckplugin
import (
"encoding/json"
"image/color"
"log"
"github.com/gorilla/websocket"
"github.com/shayne/hwinfo-streamdeck/pkg/graph"
"github.com/shayne/hwinfo-streamdeck/pkg/streamdeck"
)
const (
tileWidth = 72
tileHeight = 72
)
// OnConnected event
func (p *Plugin) OnConnected(c *websocket.Conn) {
log.Println("OnConnected")
}
// OnWillAppear event
func (p *Plugin) OnWillAppear(event *streamdeck.EvWillAppear) {
var settings actionSettings
err := json.Unmarshal(*event.Payload.Settings, &settings)
if err != nil {
log.Println("OnWillAppear settings unmarshal", err)
}
tfSize := 10.5
vfSize := 10.5
var fgColor *color.RGBA
var bgColor *color.RGBA
var hlColor *color.RGBA
var tColor *color.RGBA
var vtColor *color.RGBA
if settings.TitleFontSize != 0 {
tfSize = settings.TitleFontSize
}
if settings.ValueFontSize != 0 {
vfSize = settings.ValueFontSize
}
if settings.ForegroundColor == "" {
fgColor = &color.RGBA{0, 81, 40, 255}
} else {
fgColor = hexToRGBA(settings.ForegroundColor)
}
if settings.BackgroundColor == "" {
bgColor = &color.RGBA{0, 0, 0, 255}
} else {
bgColor = hexToRGBA(settings.BackgroundColor)
}
if settings.HighlightColor == "" {
hlColor = &color.RGBA{0, 158, 0, 255}
} else {
hlColor = hexToRGBA(settings.HighlightColor)
}
if settings.TitleColor == "" {
tColor = &color.RGBA{183, 183, 183, 255}
} else {
tColor = hexToRGBA(settings.TitleColor)
}
if settings.ValueTextColor == "" {
vtColor = &color.RGBA{255, 255, 255, 255}
} else {
vtColor = hexToRGBA(settings.ValueTextColor)
}
g := graph.NewGraph(tileWidth, tileHeight, settings.Min, settings.Max, fgColor, bgColor, hlColor)
g.SetLabel(0, "", 19, tColor)
g.SetLabelFontSize(0, tfSize)
g.SetLabel(1, "", 44, vtColor)
g.SetLabelFontSize(1, vfSize)
p.graphs[event.Context] = g
p.am.SetAction(event.Action, event.Context, &settings)
}
// OnWillDisappear event
func (p *Plugin) OnWillDisappear(event *streamdeck.EvWillDisappear) {
var settings actionSettings
err := json.Unmarshal(*event.Payload.Settings, &settings)
if err != nil {
log.Println("OnWillAppear settings unmarshal", err)
}
delete(p.graphs, event.Context)
p.am.RemoveAction(event.Context)
}
// OnApplicationDidLaunch event
func (p *Plugin) OnApplicationDidLaunch(event *streamdeck.EvApplication) {
p.appLaunched = true
}
// OnApplicationDidTerminate event
func (p *Plugin) OnApplicationDidTerminate(event *streamdeck.EvApplication) {
p.appLaunched = false
}
// OnTitleParametersDidChange event
func (p *Plugin) OnTitleParametersDidChange(event *streamdeck.EvTitleParametersDidChange) {
var settings actionSettings
err := json.Unmarshal(*event.Payload.Settings, &settings)
if err != nil {
log.Println("OnWillAppear settings unmarshal", err)
}
g, ok := p.graphs[event.Context]
if !ok {
log.Printf("handleSetMax no graph for context: %s\n", event.Context)
return
}
g.SetLabelText(0, event.Payload.Title)
if event.Payload.TitleParameters.TitleColor != "" {
tClr := hexToRGBA(event.Payload.TitleParameters.TitleColor)
g.SetLabelColor(0, tClr)
}
settings.Title = event.Payload.Title
settings.TitleColor = event.Payload.TitleParameters.TitleColor
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
log.Printf("handleSetTitle SetSettings: %v\n", err)
return
}
p.am.SetAction(event.Action, event.Context, &settings)
}
// OnPropertyInspectorConnected event
func (p *Plugin) OnPropertyInspectorConnected(event *streamdeck.EvSendToPlugin) {
settings, err := p.am.getSettings(event.Context)
if err != nil {
log.Println("OnPropertyInspectorConnected getSettings", err)
}
sensors, err := p.hw.Sensors()
if err != nil {
log.Println("OnPropertyInspectorConnected Sensors", err)
payload := evStatus{Error: true, Message: "HWiNFO Unavailable"}
err := p.sd.SendToPropertyInspector(event.Action, event.Context, payload)
settings.InErrorState = true
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
log.Printf("OnPropertyInspectorConnected SetSettings: %v\n", err)
return
}
p.am.SetAction(event.Action, event.Context, &settings)
if err != nil {
log.Println("updateTiles SendToPropertyInspector", err)
}
return
}
evsensors := make([]*evSendSensorsPayloadSensor, 0, len(sensors))
for _, s := range sensors {
evsensors = append(evsensors, &evSendSensorsPayloadSensor{UID: s.ID(), Name: s.Name()})
}
payload := evSendSensorsPayload{Sensors: evsensors, Settings: &settings}
err = p.sd.SendToPropertyInspector(event.Action, event.Context, payload)
if err != nil {
log.Println("OnPropertyInspectorConnected SendToPropertyInspector", err)
}
}
// OnSendToPlugin event
func (p *Plugin) OnSendToPlugin(event *streamdeck.EvSendToPlugin) {
var payload map[string]*json.RawMessage
err := json.Unmarshal(*event.Payload, &payload)
if err != nil {
log.Println("OnSendToPlugin unmarshal", err)
}
if data, ok := payload["sdpi_collection"]; ok {
sdpi := evSdpiCollection{}
err = json.Unmarshal(*data, &sdpi)
if err != nil {
log.Println("SDPI unmarshal", err)
}
switch sdpi.Key {
case "sensorSelect":
err = p.handleSensorSelect(event, &sdpi)
if err != nil {
log.Println("handleSensorSelect", err)
}
case "readingSelect":
err = p.handleReadingSelect(event, &sdpi)
if err != nil {
log.Println("handleReadingSelect", err)
}
case "min":
err := p.handleSetMin(event, &sdpi)
if err != nil {
log.Println("handleSetMin", err)
}
case "max":
err := p.handleSetMax(event, &sdpi)
if err != nil {
log.Println("handleSetMax", err)
}
case "format":
err := p.handleSetFormat(event, &sdpi)
if err != nil {
log.Println("handleSetFormat", err)
}
case "divisor":
err := p.handleDivisor(event, &sdpi)
if err != nil {
log.Println("handleDivisor", err)
}
case "foreground", "background", "highlight", "valuetext":
err := p.handleColorChange(event, sdpi.Key, &sdpi)
if err != nil {
log.Println("handleColorChange", err)
}
case "titleFontSize", "valueFontSize":
err := p.handleSetFontSize(event, sdpi.Key, &sdpi)
if err != nil {
log.Println("handleSetTitleFontSize", err)
}
default:
log.Printf("Unknown sdpi key: %s\n", sdpi.Key)
}
return
}
}

View File

@ -0,0 +1,272 @@
package hwinfostreamdeckplugin
import (
"fmt"
"image/color"
"strconv"
hwsensorsservice "github.com/shayne/hwinfo-streamdeck/pkg/service"
"github.com/shayne/hwinfo-streamdeck/pkg/streamdeck"
)
func (p *Plugin) handleSensorSelect(event *streamdeck.EvSendToPlugin, sdpi *evSdpiCollection) error {
sensorid := sdpi.Value
readings, err := p.hw.ReadingsForSensorID(sensorid)
if err != nil {
return fmt.Errorf("handleSensorSelect ReadingsBySensor failed: %v", err)
}
evreadings := []*evSendReadingsPayloadReading{}
for _, r := range readings {
evreadings = append(evreadings, &evSendReadingsPayloadReading{ID: r.ID(), Label: r.Label(), Prefix: r.Unit()})
}
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("handleReadingSelect getSettings: %v", err)
}
// only update settings if SensorUID is changing
// this covers case where PI sends event when tile
// selected in SD UI
if settings.SensorUID != sensorid {
settings.SensorUID = sensorid
settings.ReadingID = 0
settings.IsValid = false
}
payload := evSendReadingsPayload{Readings: evreadings, Settings: &settings}
err = p.sd.SendToPropertyInspector(event.Action, event.Context, payload)
if err != nil {
return fmt.Errorf("sensorsSelect SendToPropertyInspector: %v", err)
}
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("handleSensorSelect SetSettings: %v", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}
func getDefaultMinMaxForReading(r hwsensorsservice.Reading) (int, int) {
switch r.Unit() {
case "%":
return 0, 100
case "Yes/No":
return 0, 1
}
min := r.ValueMin()
max := r.ValueMax()
min -= min * .2
if min <= 0 {
min = 0.
}
max += max * .2
return int(min), int(max)
}
func (p *Plugin) handleReadingSelect(event *streamdeck.EvSendToPlugin, sdpi *evSdpiCollection) error {
rid64, err := strconv.ParseInt(sdpi.Value, 10, 32)
if err != nil {
return fmt.Errorf("handleReadingSelect Atoi failed: %s, %v", sdpi.Value, err)
}
rid := int32(rid64)
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("handleReadingSelect getSettings: %v", err)
}
// no action if reading didn't change
if settings.ReadingID == rid {
return nil
}
settings.ReadingID = rid
// set default min/max
r, err := p.getReading(settings.SensorUID, settings.ReadingID)
if err != nil {
return fmt.Errorf("handleReadingSelect getReading: %v", err)
}
g, ok := p.graphs[event.Context]
if !ok {
return fmt.Errorf("handleReadingSelect no graph for context: %s", event.Context)
}
defaultMin, defaultMax := getDefaultMinMaxForReading(r)
settings.Min = defaultMin
g.SetMin(settings.Min)
settings.Max = defaultMax
g.SetMax(settings.Max)
settings.IsValid = true // set IsValid once we choose reading
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("handleReadingSelect SetSettings: %v", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}
func (p *Plugin) handleSetMin(event *streamdeck.EvSendToPlugin, sdpi *evSdpiCollection) error {
min, err := strconv.Atoi(sdpi.Value)
if err != nil {
return fmt.Errorf("handleSetMin strconv: %v", err)
}
g, ok := p.graphs[event.Context]
if !ok {
return fmt.Errorf("handleSetMax no graph for context: %s", event.Context)
}
g.SetMin(min)
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("handleSetMin getSettings: %v", err)
}
settings.Min = min
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("handleSetMin SetSettings: %v", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}
func (p *Plugin) handleSetMax(event *streamdeck.EvSendToPlugin, sdpi *evSdpiCollection) error {
max, err := strconv.Atoi(sdpi.Value)
if err != nil {
return fmt.Errorf("handleSetMax strconv: %v", err)
}
g, ok := p.graphs[event.Context]
if !ok {
return fmt.Errorf("handleSetMax no graph for context: %s", event.Context)
}
g.SetMax(max)
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("handleSetMax getSettings: %v", err)
}
settings.Max = max
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("handleSetMax SetSettings: %v", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}
func (p *Plugin) handleSetFormat(event *streamdeck.EvSendToPlugin, sdpi *evSdpiCollection) error {
format := sdpi.Value
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("handleSetFormat getSettings: %v", err)
}
settings.Format = format
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("handleSetFormat SetSettings: %v", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}
func (p *Plugin) handleDivisor(event *streamdeck.EvSendToPlugin, sdpi *evSdpiCollection) error {
divisor := sdpi.Value
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("handleDivisor getSettings: %v", err)
}
settings.Divisor = divisor
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("handleDivisor SetSettings: %v", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}
const (
hexFormat = "#%02x%02x%02x"
hexShortFormat = "#%1x%1x%1x"
hexToRGBFactor = 17
)
func hexToRGBA(hex string) *color.RGBA {
var r, g, b uint8
if len(hex) == 4 {
fmt.Sscanf(hex, hexShortFormat, &r, &g, &b)
r *= hexToRGBFactor
g *= hexToRGBFactor
b *= hexToRGBFactor
} else {
fmt.Sscanf(hex, hexFormat, &r, &g, &b)
}
return &color.RGBA{R: r, G: g, B: b, A: 255}
}
func (p *Plugin) handleColorChange(event *streamdeck.EvSendToPlugin, key string, sdpi *evSdpiCollection) error {
hex := sdpi.Value
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("handleDivisor getSettings: %v", err)
}
g, ok := p.graphs[event.Context]
if !ok {
return fmt.Errorf("handleSetMax no graph for context: %s", event.Context)
}
clr := hexToRGBA(hex)
switch key {
case "foreground":
settings.ForegroundColor = hex
g.SetForegroundColor(clr)
case "background":
settings.BackgroundColor = hex
g.SetBackgroundColor(clr)
case "highlight":
settings.HighlightColor = hex
g.SetHighlightColor(clr)
case "valuetext":
settings.ValueTextColor = hex
g.SetLabelColor(1, clr)
}
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("handleColorChange SetSettings: %v", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}
func (p *Plugin) handleSetFontSize(event *streamdeck.EvSendToPlugin, key string, sdpi *evSdpiCollection) error {
sv := sdpi.Value
size, err := strconv.ParseFloat(sv, 64)
if err != nil {
return fmt.Errorf("failed to convert value to float: %w", err)
}
settings, err := p.am.getSettings(event.Context)
if err != nil {
return fmt.Errorf("getSettings failed: %w", err)
}
g, ok := p.graphs[event.Context]
if !ok {
return fmt.Errorf("no graph for context: %s", event.Context)
}
switch key {
case "titleFontSize":
settings.TitleFontSize = size
g.SetLabelFontSize(0, size)
case "valueFontSize":
settings.ValueFontSize = size
g.SetLabelFontSize(1, size)
default:
return fmt.Errorf("invalid key: %s", sdpi.Key)
}
err = p.sd.SetSettings(event.Context, &settings)
if err != nil {
return fmt.Errorf("SetSettings failed: %w", err)
}
p.am.SetAction(event.Action, event.Context, &settings)
return nil
}

View File

@ -0,0 +1,231 @@
package hwinfostreamdeckplugin
import (
"fmt"
"io/ioutil"
"log"
"os/exec"
"strconv"
"time"
"github.com/hashicorp/go-plugin"
"github.com/shayne/go-winpeg"
"github.com/shayne/hwinfo-streamdeck/pkg/graph"
hwsensorsservice "github.com/shayne/hwinfo-streamdeck/pkg/service"
"github.com/shayne/hwinfo-streamdeck/pkg/streamdeck"
)
// Plugin handles information between HWiNFO and Stream Deck
type Plugin struct {
c *plugin.Client
peg winpeg.ProcessExitGroup
hw hwsensorsservice.HardwareService
sd *streamdeck.StreamDeck
am *actionManager
graphs map[string]*graph.Graph
appLaunched bool
}
func (p *Plugin) startClient() error {
cmd := exec.Command("./hwinfo-plugin.exe")
// We're a host. Start by launching the plugin process.
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: hwsensorsservice.Handshake,
Plugins: hwsensorsservice.PluginMap,
Cmd: cmd,
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
AutoMTLS: true,
})
// Connect via RPC
rpcClient, err := client.Client()
if err != nil {
return err
}
g, err := winpeg.NewProcessExitGroup()
if err != nil {
return err
}
if err := g.AddProcess(cmd.Process); err != nil {
return err
}
// Request the plugin
raw, err := rpcClient.Dispense("hwinfoplugin")
if err != nil {
return err
}
p.c = client
p.peg = g
p.hw = raw.(hwsensorsservice.HardwareService)
return nil
}
// NewPlugin creates an instance and initializes the plugin
func NewPlugin(port, uuid, event, info string) (*Plugin, error) {
// We don't want to see the plugin logs.
// log.SetOutput(ioutil.Discard)
p := &Plugin{
am: newActionManager(),
graphs: make(map[string]*graph.Graph),
}
p.startClient()
p.sd = streamdeck.NewStreamDeck(port, uuid, event, info)
return p, nil
}
// RunForever starts the plugin and waits for events, indefinitely
func (p *Plugin) RunForever() error {
defer func() {
p.c.Kill()
p.peg.Dispose()
}()
p.sd.SetDelegate(p)
p.am.Run(p.updateTiles)
go func() {
for {
if p.c.Exited() {
p.startClient()
}
time.Sleep(1 * time.Second)
}
}()
err := p.sd.Connect()
if err != nil {
return fmt.Errorf("StreamDeck Connect: %v", err)
}
defer p.sd.Close()
p.sd.ListenAndWait()
return nil
}
func (p *Plugin) getReading(suid string, rid int32) (hwsensorsservice.Reading, error) {
rbs, err := p.hw.ReadingsForSensorID(suid)
if err != nil {
return nil, fmt.Errorf("getReading ReadingsBySensor failed: %v", err)
}
for _, r := range rbs {
if r.ID() == rid {
return r, nil
}
}
return nil, fmt.Errorf("ReadingID does not exist: %s", suid)
}
func (p *Plugin) applyDefaultFormat(v float64, t hwsensorsservice.ReadingType, u string) string {
switch t {
case hwsensorsservice.ReadingTypeNone:
return fmt.Sprintf("%0.f %s", v, u)
case hwsensorsservice.ReadingTypeTemp:
return fmt.Sprintf("%.0f %s", v, u)
case hwsensorsservice.ReadingTypeVolt:
return fmt.Sprintf("%.0f %s", v, u)
case hwsensorsservice.ReadingTypeFan:
return fmt.Sprintf("%.0f %s", v, u)
case hwsensorsservice.ReadingTypeCurrent:
return fmt.Sprintf("%.0f %s", v, u)
case hwsensorsservice.ReadingTypePower:
return fmt.Sprintf("%0.f %s", v, u)
case hwsensorsservice.ReadingTypeClock:
return fmt.Sprintf("%.0f %s", v, u)
case hwsensorsservice.ReadingTypeUsage:
return fmt.Sprintf("%.0f%s", v, u)
case hwsensorsservice.ReadingTypeOther:
return fmt.Sprintf("%.0f %s", v, u)
}
return "Bad Format"
}
func (p *Plugin) updateTiles(data *actionData) {
if data.action != "com.exension.hwinfor0.reading" {
log.Printf("Unknown action updateTiles: %s\n", data.action)
return
}
g, ok := p.graphs[data.context]
if !ok {
log.Printf("Graph not found for context: %s\n", data.context)
return
}
if !p.appLaunched {
if !data.settings.InErrorState {
payload := evStatus{Error: true, Message: "HWiNFO Unavailable"}
err := p.sd.SendToPropertyInspector("com.exension.hwinfor0.reading", data.context, payload)
if err != nil {
log.Println("updateTiles SendToPropertyInspector", err)
}
data.settings.InErrorState = true
p.sd.SetSettings(data.context, &data.settings)
}
bts, err := ioutil.ReadFile("./launch-hwinfo.png")
if err != nil {
log.Printf("Failed to read launch-hwinfo.png: %v\n", err)
return
}
err = p.sd.SetImage(data.context, bts)
if err != nil {
log.Printf("Failed to setImage: %v\n", err)
return
}
return
}
// show ui on property inspector if in error state
if data.settings.InErrorState {
payload := evStatus{Error: false, Message: "show_ui"}
err := p.sd.SendToPropertyInspector("com.exension.hwinfor0.reading", data.context, payload)
if err != nil {
log.Println("updateTiles SendToPropertyInspector", err)
}
data.settings.InErrorState = false
p.sd.SetSettings(data.context, &data.settings)
}
s := data.settings
r, err := p.getReading(s.SensorUID, s.ReadingID)
if err != nil {
log.Printf("getReading failed: %v\n", err)
return
}
v := r.Value()
if s.Divisor != "" {
fdiv := 1.
fdiv, err := strconv.ParseFloat(s.Divisor, 64)
if err != nil {
log.Printf("Failed to parse float: %s\n", s.Divisor)
return
}
v = r.Value() / fdiv
}
g.Update(v)
var text string
if f := s.Format; f != "" {
text = fmt.Sprintf(f, v)
} else {
text = p.applyDefaultFormat(v, hwsensorsservice.ReadingType(r.TypeI()), r.Unit())
}
g.SetLabelText(1, text)
b, err := g.EncodePNG()
if err != nil {
log.Printf("Failed to encode graph: %v\n", err)
return
}
err = p.sd.SetImage(data.context, b)
if err != nil {
log.Printf("Failed to setImage: %v\n", err)
return
}
}

View File

@ -0,0 +1,60 @@
package hwinfostreamdeckplugin
type actionSettings struct {
SensorUID string `json:"sensorUid"`
ReadingID int32 `json:"readingId,string"`
Title string `json:"title"`
TitleFontSize float64 `json:"titleFontSize"`
ValueFontSize float64 `json:"valueFontSize"`
Min int `json:"min"`
Max int `json:"max"`
Format string `json:"format"`
Divisor string `json:"divisor"`
IsValid bool `json:"isValid"`
TitleColor string `json:"titleColor"`
ForegroundColor string `json:"foregroundColor"`
BackgroundColor string `json:"backgroundColor"`
HighlightColor string `json:"highlightColor"`
ValueTextColor string `json:"valueTextColor"`
InErrorState bool `json:"inErrorState"`
}
type actionData struct {
action string
context string
settings *actionSettings
}
type evStatus struct {
Error bool `json:"error"`
Message string `json:"message"`
}
type evSendSensorsPayloadSensor struct {
UID string `json:"uid"`
Name string `json:"name"`
}
type evSendSensorsPayload struct {
Sensors []*evSendSensorsPayloadSensor `json:"sensors"`
Settings *actionSettings `json:"settings"`
}
type evSendReadingsPayloadReading struct {
ID int32 `json:"id,string"`
Label string `json:"label"`
Prefix string `json:"prefix"`
}
type evSendReadingsPayload struct {
Readings []*evSendReadingsPayloadReading `json:"readings"`
Settings *actionSettings `json:"settings"`
}
type evSdpiCollection struct {
Group bool `json:"group"`
Index int `json:"index"`
Key string `json:"key"`
Selection []string `json:"selection"`
Value string `json:"value"`
}

163
internal/hwinfo/hwinfo.go Normal file
View File

@ -0,0 +1,163 @@
package hwinfo
/*
#include <windows.h>
#include "hwisenssm2.h"
*/
import "C"
import (
"fmt"
"log"
"time"
"unsafe"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo/shmem"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo/util"
)
// SharedMemory provides access to the HWiNFO shared memory
type SharedMemory struct {
data []byte
shmem C.PHWiNFO_SENSORS_SHARED_MEM2
}
// ReadSharedMem reads data from HWiNFO shared memory
// creating a copy of the data
func ReadSharedMem() (*SharedMemory, error) {
data, err := shmem.ReadBytes()
if err != nil {
return nil, err
}
return &SharedMemory{
data: append([]byte(nil), data...),
shmem: C.PHWiNFO_SENSORS_SHARED_MEM2(unsafe.Pointer(&data[0])),
}, nil
}
// Result for streamed shared memory updates
type Result struct {
Shmem *SharedMemory
Err error
}
func readAndSend(ch chan<- Result) {
shmem, err := ReadSharedMem()
ch <- Result{Shmem: shmem, Err: err}
}
// StreamSharedMem delivers shared memory hardware sensors updates
// over a channel
func StreamSharedMem() <-chan Result {
ch := make(chan Result)
go func() {
readAndSend(ch)
// TODO: don't use time.Tick, cancellable?
for range time.Tick(1 * time.Second) {
readAndSend(ch)
}
}()
return ch
}
// Signature "HWiS" if active, 'DEAD' when inactive
func (s *SharedMemory) Signature() string {
return util.DecodeCharPtr(unsafe.Pointer(&s.shmem.dwSignature), C.sizeof_DWORD)
}
// Version v1 is latest
func (s *SharedMemory) Version() int {
return int(s.shmem.dwVersion)
}
// Revision revision of version
func (s *SharedMemory) Revision() int {
return int(s.shmem.dwRevision)
}
// PollTime last polling time
func (s *SharedMemory) PollTime() uint64 {
addr := unsafe.Pointer(uintptr(unsafe.Pointer(&s.shmem.dwRevision)) + C.sizeof_DWORD)
return uint64(*(*C.__time64_t)(addr))
}
// OffsetOfSensorSection offset of the Sensor section from beginning of HWiNFO_SENSORS_SHARED_MEM2
func (s *SharedMemory) OffsetOfSensorSection() int {
return int(s.shmem.dwOffsetOfSensorSection)
}
// SizeOfSensorElement size of each sensor element = sizeof( HWiNFO_SENSORS_SENSOR_ELEMENT )
func (s *SharedMemory) SizeOfSensorElement() int {
return int(s.shmem.dwSizeOfSensorElement)
}
// NumSensorElements number of sensor elements
func (s *SharedMemory) NumSensorElements() int {
return int(s.shmem.dwNumSensorElements)
}
// OffsetOfReadingSection offset of the Reading section from beginning of HWiNFO_SENSORS_SHARED_MEM2
func (s *SharedMemory) OffsetOfReadingSection() int {
return int(s.shmem.dwOffsetOfReadingSection)
}
// SizeOfReadingElement size of each Reading element = sizeof( HWiNFO_SENSORS_READING_ELEMENT )
func (s *SharedMemory) SizeOfReadingElement() int {
return int(s.shmem.dwSizeOfReadingElement)
}
// NumReadingElements number of Reading elements
func (s *SharedMemory) NumReadingElements() int {
return int(s.shmem.dwNumReadingElements)
}
func (s *SharedMemory) dataForSensor(pos int) ([]byte, error) {
if pos >= s.NumSensorElements() {
return nil, fmt.Errorf("dataForSensor pos out of range, %d for size %d", pos, s.NumSensorElements())
}
start := s.OffsetOfSensorSection() + (pos * s.SizeOfSensorElement())
end := start + s.SizeOfSensorElement()
return s.data[start:end], nil
}
// IterSensors iterate over each sensor
func (s *SharedMemory) IterSensors() <-chan Sensor {
ch := make(chan Sensor)
go func() {
for i := 0; i < s.NumSensorElements(); i++ {
data, err := s.dataForSensor(i)
if err != nil {
log.Fatalf("TODO: failed to read dataForSensor: %v", err)
}
ch <- NewSensor(data)
}
close(ch)
}()
return ch
}
func (s *SharedMemory) dataForReading(pos int) ([]byte, error) {
if pos >= s.NumReadingElements() {
return nil, fmt.Errorf("dataForReading pos out of range, %d for size %d", pos, s.NumSensorElements())
}
start := s.OffsetOfReadingSection() + (pos * s.SizeOfReadingElement())
end := start + s.SizeOfReadingElement()
return s.data[start:end], nil
}
// IterReadings iterate over each sensor
func (s *SharedMemory) IterReadings() <-chan Reading {
ch := make(chan Reading)
go func() {
for i := 0; i < s.NumReadingElements(); i++ {
data, err := s.dataForReading(i)
if err != nil {
log.Fatalf("TODO: failed to read dataForReading: %v", err)
}
ch <- NewReading(data)
}
close(ch)
}()
return ch
}

View File

@ -0,0 +1,126 @@
#ifndef _HWISENSSM2_H_INCLUDED_
#define _HWISENSSM2_H_INCLUDED_
// Name of the file mapping object that needs to be opened using OpenFileMapping Function:
#define HWiNFO_SENSORS_MAP_FILE_NAME2 "Global\\HWiNFO_SENS_SM2"
// Name of the global mutex which is acquired when accessing the Shared Memory space. Release as quick as possible !
#define HWiNFO_SENSORS_SM2_MUTEX "Global\\HWiNFO_SM2_MUTEX"
#define HWiNFO_SENSORS_STRING_LEN2 128
#define HWiNFO_UNIT_STRING_LEN 16
enum SENSOR_READING_TYPE
{
SENSOR_TYPE_NONE = 0,
SENSOR_TYPE_TEMP,
SENSOR_TYPE_VOLT,
SENSOR_TYPE_FAN,
SENSOR_TYPE_CURRENT,
SENSOR_TYPE_POWER,
SENSOR_TYPE_CLOCK,
SENSOR_TYPE_USAGE,
SENSOR_TYPE_OTHER
};
typedef enum SENSOR_READING_TYPE SENSOR_READING_TYPE;
// No alignment of structure members
#pragma pack(1)
typedef struct _HWiNFO_SENSORS_READING_ELEMENT
{
SENSOR_READING_TYPE tReading; // Type of sensor reading
DWORD dwSensorIndex; // This is the index of sensor in the Sensors[] array to which this reading belongs to
DWORD dwReadingID; // A unique ID of the reading within a particular sensor
char szLabelOrig[HWiNFO_SENSORS_STRING_LEN2]; // Original label (e.g. "Chassis2 Fan")
char szLabelUser[HWiNFO_SENSORS_STRING_LEN2]; // Label displayed, which might have been renamed by user
char szUnit[HWiNFO_UNIT_STRING_LEN]; // e.g. "RPM"
double Value;
double ValueMin;
double ValueMax;
double ValueAvg;
} HWiNFO_SENSORS_READING_ELEMENT, *PHWiNFO_SENSORS_READING_ELEMENT;
typedef struct _HWiNFO_SENSORS_SENSOR_ELEMENT
{
DWORD dwSensorID; // A unique Sensor ID
DWORD dwSensorInst; // The instance of the sensor (together with dwSensorID forms a unique ID)
char szSensorNameOrig[HWiNFO_SENSORS_STRING_LEN2]; // Original sensor name
char szSensorNameUser[HWiNFO_SENSORS_STRING_LEN2]; // Sensor name displayed, which might have been renamed by user
} HWiNFO_SENSORS_SENSOR_ELEMENT, *PHWiNFO_SENSORS_SENSOR_ELEMENT;
typedef struct _HWiNFO_SENSORS_SHARED_MEM2
{
DWORD dwSignature; // "HWiS" if active, 'DEAD' when inactive
DWORD dwVersion; // v1 is latest
DWORD dwRevision; //
__time64_t poll_time; // last polling time
// descriptors for the Sensors section
DWORD dwOffsetOfSensorSection; // Offset of the Sensor section from beginning of HWiNFO_SENSORS_SHARED_MEM2
DWORD dwSizeOfSensorElement; // Size of each sensor element = sizeof( HWiNFO_SENSORS_SENSOR_ELEMENT )
DWORD dwNumSensorElements; // Number of sensor elements
// descriptors for the Readings section
DWORD dwOffsetOfReadingSection; // Offset of the Reading section from beginning of HWiNFO_SENSORS_SHARED_MEM2
DWORD dwSizeOfReadingElement; // Size of each Reading element = sizeof( HWiNFO_SENSORS_READING_ELEMENT )
DWORD dwNumReadingElements; // Number of Reading elements
} HWiNFO_SENSORS_SHARED_MEM2, *PHWiNFO_SENSORS_SHARED_MEM2;
#pragma pack()
#endif
// ***************************************************************************************************************
// HWiNFO Shared Memory Footprint
// ***************************************************************************************************************
//
// |-----------------------------|-----------------------------------|-----------------------------------|
// Content | HWiNFO_SENSORS_SHARED_MEM2 | HWiNFO_SENSORS_SENSOR_ELEMENT[] | HWiNFO_SENSORS_READING_ELEMENT[] |
// |-----------------------------|-----------------------------------|-----------------------------------|
// Pointer |<--0 |<--dwOffsetOfSensorSection |<--dwOffsetOfReadingSection |
// |-----------------------------|-----------------------------------|-----------------------------------|
// Size | dwOffsetOfSensorSection | dwSizeOfSensorElement | dwSizeOfReadingElement |
// | | * dwNumSensorElement | * dwNumReadingElement |
// |-----------------------------|-----------------------------------|-----------------------------------|
//
// ***************************************************************************************************************
// Code Example
// ***************************************************************************************************************
/*
HANDLE hHWiNFOMemory = OpenFileMapping( FILE_MAP_READ, FALSE, HWiNFO_SENSORS_MAP_FILE_NAME2 );
if (hHWiNFOMemory)
PHWiNFO_SENSORS_SHARED_MEM2 pHWiNFOMemory =
(PHWiNFO_SENSORS_SHARED_MEM2) MapViewOfFile( hHWiNFOMemory, FILE_MAP_READ, 0, 0, 0 );
// TODO: process signature, version, revision and poll time
// loop through all available sensors
for (DWORD dwSensor = 0; dwSensor < pHWiNFOMemory->dwNumSensorElements; dwSensor++)
{
PHWiNFO_SENSORS_SENSOR_ELEMENT sensor = (PHWiNFO_SENSORS_SENSOR_ELEMENT) ((BYTE*)pHWiNFOMemory +
pHWiNFOMemory->dwOffsetOfSensorSection +
(pHWiNFOMemory->dwSizeOfSensorElement * dwSensor));
// TODO: process sensor
}
// loop through all available readings
for (DWORD dwReading = 0; dwReading < pHWiNFOMemory->dwNumReadingElements; dwReading++)
{
PHWiNFO_SENSORS_READING_ELEMENT reading = (PHWiNFO_SENSORS_READING_ELEMENT) ((BYTE*)pHWiNFOMemory +
pHWiNFOMemory->dwOffsetOfReadingSection +
(pHWiNFOMemory->dwSizeOfReadingElement * dwReading));
// TODO: process reading
}
}
*/

View File

@ -0,0 +1,38 @@
package mutex
/*
#include <windows.h>
#include "../hwisenssm2.h"
*/
import "C"
import (
"fmt"
"sync"
"unsafe"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo/util"
)
var ghnd C.HANDLE
var imut = sync.Mutex{}
// Lock the global mutex
func Lock() error {
imut.Lock()
lpName := C.CString(C.HWiNFO_SENSORS_SM2_MUTEX)
defer C.free(unsafe.Pointer(lpName))
ghnd = C.OpenMutex(C.READ_CONTROL, C.FALSE, lpName)
if ghnd == C.HANDLE(C.NULL) {
errstr := util.HandleLastError(uint64(C.GetLastError()))
return fmt.Errorf("failed to lock global mutex: %w", errstr)
}
return nil
}
// Unlock the global mutex
func Unlock() {
defer imut.Unlock()
C.CloseHandle(ghnd)
}

View File

@ -0,0 +1,70 @@
package plugin
import (
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo"
hwsensorsservice "github.com/shayne/hwinfo-streamdeck/pkg/service"
)
// Plugin implementation
type Plugin struct {
Service *Service
}
// PollTime implementation for plugin
func (p *Plugin) PollTime() (uint64, error) {
shmem, err := p.Service.Shmem()
if err != nil {
return 0, err
}
return shmem.PollTime(), nil
}
// Sensors implementation for plugin
func (p *Plugin) Sensors() ([]hwsensorsservice.Sensor, error) {
shmem, err := p.Service.Shmem()
if err != nil {
return nil, err
}
var sensors []hwsensorsservice.Sensor
for s := range shmem.IterSensors() {
sensors = append(sensors, &sensor{s})
}
return sensors, nil
}
// ReadingsForSensorID implementation for plugin
func (p *Plugin) ReadingsForSensorID(id string) ([]hwsensorsservice.Reading, error) {
res, err := p.Service.ReadingsBySensorID(id)
if err != nil {
return nil, err
}
var readings []hwsensorsservice.Reading
for _, r := range res {
readings = append(readings, &reading{r})
}
return readings, nil
}
type sensor struct {
hwinfo.Sensor
}
func (s sensor) Name() string {
return s.NameOrig()
}
type reading struct {
hwinfo.Reading
}
func (r reading) Label() string {
return r.LabelOrig()
}
func (r reading) Type() string {
return r.Reading.Type().String()
}
func (r reading) TypeI() int32 {
return int32(r.Reading.Type())
}

View File

@ -0,0 +1,128 @@
package plugin
import (
"fmt"
"sync"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo"
)
// Service wraps hwinfo shared mem streaming
// and provides convenient methods for data access
type Service struct {
streamch <-chan hwinfo.Result
mu sync.RWMutex
sensorIDByIdx []string
readingsBySensorID map[string][]hwinfo.Reading
shmem *hwinfo.SharedMemory
readingsBuilt bool
}
// Start starts the service providing updating hardware info
func StartService() *Service {
return &Service{
streamch: hwinfo.StreamSharedMem(),
}
}
func (s *Service) recvShmem(shmem *hwinfo.SharedMemory) error {
if shmem == nil {
return fmt.Errorf("shmem nil")
}
s.mu.Lock()
defer s.mu.Unlock()
s.shmem = shmem
s.sensorIDByIdx = s.sensorIDByIdx[:0]
for k, v := range s.readingsBySensorID {
s.readingsBySensorID[k] = v[:0]
}
s.readingsBuilt = false
return nil
}
// Recv receives new hardware sensor updates
func (s *Service) Recv() error {
select {
case r := <-s.streamch:
if r.Err != nil {
return r.Err
}
return s.recvShmem(r.Shmem)
}
}
// Shmem provides access to underlying hwinfo shared memory
func (s *Service) Shmem() (*hwinfo.SharedMemory, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.shmem != nil {
return s.shmem, nil
}
return nil, fmt.Errorf("shmem nil")
}
// SensorIDByIdx returns ordered slice of sensor IDs
func (s *Service) SensorIDByIdx() ([]string, error) {
s.mu.RLock()
if len(s.sensorIDByIdx) > 0 {
defer s.mu.RUnlock()
return s.sensorIDByIdx, nil
}
s.mu.RUnlock()
s.mu.Lock()
defer s.mu.Unlock()
for sens := range s.shmem.IterSensors() {
s.sensorIDByIdx = append(s.sensorIDByIdx, sens.ID())
}
return s.sensorIDByIdx, nil
}
// ReadingsBySensorID returns slice of hwinfoReading for a given sensor ID
func (s *Service) ReadingsBySensorID(id string) ([]hwinfo.Reading, error) {
s.mu.RLock()
if s.readingsBySensorID != nil && s.readingsBuilt {
defer s.mu.RUnlock()
readings, ok := s.readingsBySensorID[id]
if !ok {
return nil, fmt.Errorf("readings for sensor id %s do not exist", id)
}
return readings, nil
}
s.mu.RUnlock()
sids, err := s.SensorIDByIdx()
if err != nil {
return nil, err
}
s.mu.Lock()
defer s.mu.Unlock()
if s.readingsBySensorID == nil {
s.readingsBySensorID = make(map[string][]hwinfo.Reading)
}
for r := range s.shmem.IterReadings() {
sidx := int(r.SensorIndex())
if sidx < len(sids) {
sid := sids[sidx]
s.readingsBySensorID[sid] = append(s.readingsBySensorID[sid], r)
} else {
return nil, fmt.Errorf("sensor at index %d out of range ", sidx)
}
}
s.readingsBuilt = true
readings, ok := s.readingsBySensorID[id]
if !ok {
return nil, fmt.Errorf("readings for sensor id %s do not exist", id)
}
return readings, nil
}

124
internal/hwinfo/reading.go Normal file
View File

@ -0,0 +1,124 @@
package hwinfo
/*
#include <windows.h>
#include "hwisenssm2.h"
*/
import "C"
import (
"unsafe"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo/util"
)
// ReadingType enum of value/unit type for reading
type ReadingType int
const (
// ReadingTypeNone no type
ReadingTypeNone ReadingType = iota
// ReadingTypeTemp temperature in celsius
ReadingTypeTemp
// ReadingTypeVolt voltage
ReadingTypeVolt
// ReadingTypeFan RPM
ReadingTypeFan
// ReadingTypeCurrent amps
ReadingTypeCurrent
// ReadingTypePower watts
ReadingTypePower
// ReadingTypeClock Mhz
ReadingTypeClock
// ReadingTypeUsage e.g. MBs
ReadingTypeUsage
// ReadingTypeOther other
ReadingTypeOther
)
func (t ReadingType) String() string {
return [...]string{"None", "Temp", "Volt", "Fan", "Current", "Power", "Clock", "Usage", "Other"}[t]
}
// Reading element (e.g. usage, power, mhz...)
type Reading struct {
cr C.PHWiNFO_SENSORS_READING_ELEMENT
}
// NewReading contructs a Reading
func NewReading(data []byte) Reading {
return Reading{
cr: C.PHWiNFO_SENSORS_READING_ELEMENT(unsafe.Pointer(&data[0])),
}
}
// ID unique ID of the reading within a particular sensor
func (r *Reading) ID() int32 {
return int32(r.cr.dwReadingID)
}
// Type of sensor reading
func (r *Reading) Type() ReadingType {
return ReadingType(r.cr.tReading)
}
// SensorIndex this is the index of sensor in the Sensors[] array to
// which this reading belongs to
func (r *Reading) SensorIndex() uint64 {
return uint64(r.cr.dwSensorIndex)
}
// ReadingID a unique ID of the reading within a particular sensor
func (r *Reading) ReadingID() uint64 {
return uint64(r.cr.dwReadingID)
}
// LabelOrig original label (e.g. "Chassis2 Fan")
func (r *Reading) LabelOrig() string {
return util.DecodeCharPtr(unsafe.Pointer(&r.cr.szLabelOrig), C.HWiNFO_SENSORS_STRING_LEN2)
}
// LabelUser label displayed, which might have been renamed by user
func (r *Reading) LabelUser() string {
return util.DecodeCharPtr(unsafe.Pointer(&r.cr.szLabelUser), C.HWiNFO_SENSORS_STRING_LEN2)
}
// Unit e.g. "RPM"
func (r *Reading) Unit() string {
return util.DecodeCharPtr(unsafe.Pointer(&r.cr.szUnit), C.HWiNFO_UNIT_STRING_LEN)
}
func (r *Reading) valuePtr() unsafe.Pointer {
return unsafe.Pointer(uintptr(unsafe.Pointer(&r.cr.szUnit)) + C.HWiNFO_UNIT_STRING_LEN)
}
// Value current value
func (r *Reading) Value() float64 {
return float64(*(*C.double)(r.valuePtr()))
}
func (r *Reading) valueMinPtr() unsafe.Pointer {
return unsafe.Pointer(uintptr(r.valuePtr()) + C.sizeof_double)
}
// ValueMin current value
func (r *Reading) ValueMin() float64 {
return float64(*(*C.double)(r.valueMinPtr()))
}
func (r *Reading) valueMaxPtr() unsafe.Pointer {
return unsafe.Pointer(uintptr(r.valueMinPtr()) + C.sizeof_double)
}
// ValueMax current value
func (r *Reading) ValueMax() float64 {
return float64(*(*C.double)(r.valueMaxPtr()))
}
func (r *Reading) valueAvgPtr() unsafe.Pointer {
return unsafe.Pointer(uintptr(r.valueMaxPtr()) + C.sizeof_double)
}
// ValueAvg current value
func (r *Reading) ValueAvg() float64 {
return float64(*(*C.double)(r.valueAvgPtr()))
}

52
internal/hwinfo/sensor.go Normal file
View File

@ -0,0 +1,52 @@
package hwinfo
/*
#include <windows.h>
#include "hwisenssm2.h"
*/
import "C"
import (
"strconv"
"unsafe"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo/util"
)
// Sensor element (e.g. motherboard, cpu, gpu...)
type Sensor struct {
cs C.PHWiNFO_SENSORS_SENSOR_ELEMENT
}
// NewSensor constructs a Sensor
func NewSensor(data []byte) Sensor {
return Sensor{
cs: C.PHWiNFO_SENSORS_SENSOR_ELEMENT(unsafe.Pointer(&data[0])),
}
}
// SensorID a unique Sensor ID
func (s *Sensor) SensorID() uint64 {
return uint64(s.cs.dwSensorID)
}
// SensorInst the instance of the sensor (together with SensorID forms a unique ID)
func (s *Sensor) SensorInst() uint64 {
return uint64(s.cs.dwSensorInst)
}
// ID a unique ID combining SensorID and SensorInst
func (s *Sensor) ID() string {
// keeping old method used in legacy steam deck plugin
return strconv.FormatUint(s.SensorID()*100+s.SensorInst(), 10)
}
// NameOrig original name of sensor
func (s *Sensor) NameOrig() string {
return util.DecodeCharPtr(unsafe.Pointer(&s.cs.szSensorNameOrig), C.HWiNFO_SENSORS_STRING_LEN2)
}
// NameUser sensor name displayed, which might have been renamed by user
func (s *Sensor) NameUser() string {
return util.DecodeCharPtr(unsafe.Pointer(&s.cs.szSensorNameUser), C.HWiNFO_SENSORS_STRING_LEN2)
}

View File

@ -0,0 +1,95 @@
package shmem
/*
#include <windows.h>
#include "../hwisenssm2.h"
*/
import "C"
import (
"fmt"
"reflect"
"syscall"
"unsafe"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo/mutex"
"github.com/shayne/hwinfo-streamdeck/internal/hwinfo/util"
"golang.org/x/sys/windows"
)
var buf = make([]byte, 200000)
func copyBytes(addr uintptr) []byte {
headerLen := C.sizeof_HWiNFO_SENSORS_SHARED_MEM2
var d []byte
dh := (*reflect.SliceHeader)(unsafe.Pointer(&d))
dh.Data = addr
dh.Len, dh.Cap = headerLen, headerLen
cheader := C.PHWiNFO_SENSORS_SHARED_MEM2(unsafe.Pointer(&d[0]))
fullLen := int(cheader.dwOffsetOfReadingSection + (cheader.dwSizeOfReadingElement * cheader.dwNumReadingElements))
if fullLen > cap(buf) {
buf = append(buf, make([]byte, fullLen-cap(buf))...)
}
dh.Len, dh.Cap = fullLen, fullLen
copy(buf, d)
return buf[:fullLen]
}
// ReadBytes copies bytes from global shared memory
func ReadBytes() ([]byte, error) {
err := mutex.Lock()
defer mutex.Unlock()
if err != nil {
return nil, err
}
hnd, err := openFileMapping()
if err != nil {
return nil, err
}
addr, err := mapViewOfFile(hnd)
if err != nil {
return nil, err
}
defer unmapViewOfFile(addr)
defer windows.CloseHandle(windows.Handle(unsafe.Pointer(hnd)))
return copyBytes(addr), nil
}
func openFileMapping() (C.HANDLE, error) {
lpName := C.CString("Global\\HWiNFO_SENS_SM2_REMOTE_0")
defer C.free(unsafe.Pointer(lpName))
hnd := C.OpenFileMapping(syscall.FILE_MAP_READ, 0, lpName)
if hnd == C.HANDLE(C.NULL) {
errstr := util.HandleLastError(uint64(C.GetLastError()))
return nil, fmt.Errorf("OpenFileMapping: %w", errstr)
}
return hnd, nil
}
func mapViewOfFile(hnd C.HANDLE) (uintptr, error) {
addr, err := windows.MapViewOfFile(windows.Handle(unsafe.Pointer(hnd)), C.FILE_MAP_READ, 0, 0, 0)
if err != nil {
return 0, fmt.Errorf("MapViewOfFile: %w", err)
}
return addr, nil
}
func unmapViewOfFile(ptr uintptr) error {
err := windows.UnmapViewOfFile(ptr)
if err != nil {
return fmt.Errorf("UnmapViewOfFile: %w", err)
}
return nil
}

View File

@ -0,0 +1,61 @@
package util
import "C"
import (
"errors"
"fmt"
"log"
"strings"
"unsafe"
"golang.org/x/text/encoding/charmap"
)
// ErrFileNotFound Windows error
var ErrFileNotFound = errors.New("file not found")
// ErrInvalidHandle Windows error
var ErrInvalidHandle = errors.New("invalid handle")
// UnknownError unhandled Windows error
type UnknownError struct {
Code uint64
}
func (e UnknownError) Error() string {
return fmt.Sprintf("unknown error code: %d", e.Code)
}
// HandleLastError converts C.GetLastError() to golang error
func HandleLastError(code uint64) error {
switch code {
case 2: // ERROR_FILE_NOT_FOUND
return ErrFileNotFound
case 6: // ERROR_INVALID_HANDLE
return ErrInvalidHandle
default:
return UnknownError{Code: code}
}
}
func goStringFromPtr(ptr unsafe.Pointer, len int) string {
s := C.GoStringN((*C.char)(ptr), C.int(len))
return s[:strings.IndexByte(s, 0)]
}
// DecodeCharPtr decodes ISO8859_1 string to UTF-8
func DecodeCharPtr(ptr unsafe.Pointer, len int) string {
s := goStringFromPtr(ptr, len)
ds, err := decodeISO8859_1(s)
if err != nil {
log.Fatalf("TODO: failed to decode: %v", err)
}
return ds
}
var isodecoder = charmap.ISO8859_1.NewDecoder()
func decodeISO8859_1(in string) (string, error) {
return isodecoder.String(in)
}

2
kill-streamdeck.bat Normal file
View File

@ -0,0 +1,2 @@
@echo off
taskkill /F /IM StreamDeck.exe /T

3
make-release.bat Normal file
View File

@ -0,0 +1,3 @@
@echo off
del build\com.exension.hwinfo.streamDeckPlugin
DistributionTool.exe com.exension.hwinfo.sdPlugin build

327
pkg/graph/graph.go Normal file
View File

@ -0,0 +1,327 @@
package graph
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math"
"regexp"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"image/color"
"image/png"
"sync"
)
// Label struct contains text, position and color information
type Label struct {
text string
y uint
fontSize float64
clr *color.RGBA
}
// Graph is used to display a histogram of data passed to Update
type Graph struct {
img *image.RGBA
lvay int
width int
height int
min int
max int
yvals []uint8
fgColor *color.RGBA
bgColor *color.RGBA
hlColor *color.RGBA
labels map[int]*Label
drawn bool
redraw bool
}
// FontFaceManager builds and caches fonts based on size
type FontFaceManager struct {
mux sync.Mutex
fontCache map[float64]font.Face
}
// NewFontFaceManager constructs new manager
func NewFontFaceManager() *FontFaceManager {
return &FontFaceManager{fontCache: make(map[float64]font.Face)}
}
func (f *FontFaceManager) newFace(size float64) font.Face {
b, err := ioutil.ReadFile("DejaVuSans-Bold.ttf")
if err != nil {
log.Fatal(err)
}
tt, err := truetype.Parse(b)
if err != nil {
log.Fatal("failed to parse font")
}
face := truetype.NewFace(tt, &truetype.Options{Size: size, DPI: 72})
return face
}
// GetFaceOfSize returns font face for given size
func (f *FontFaceManager) GetFaceOfSize(size float64) font.Face {
f.mux.Lock()
defer f.mux.Unlock()
if f, ok := f.fontCache[size]; ok {
return f
}
nf := f.newFace(size)
f.fontCache[size] = nf
return nf
}
type singleshared struct {
fontFaceManager *FontFaceManager
pngEnc *png.Encoder
pngBuf *bytes.Buffer
}
var sharedinstance *singleshared
var once sync.Once
func shared() *singleshared {
once.Do(func() {
sharedinstance = &singleshared{
pngEnc: &png.Encoder{
CompressionLevel: png.NoCompression,
},
pngBuf: bytes.NewBuffer(make([]byte, 0, 15697)),
}
sharedinstance.fontFaceManager = NewFontFaceManager()
})
return sharedinstance
}
// NewGraph initializes a new Graph for rendering
func NewGraph(width, height, min, max int, fgColor, bgColor, hlColor *color.RGBA) *Graph {
img := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
labels := make(map[int]*Label)
return &Graph{
img: img,
lvay: -1,
width: width,
height: height,
min: min,
max: max,
labels: labels,
yvals: make([]uint8, 0, width),
fgColor: fgColor,
bgColor: bgColor,
hlColor: hlColor,
}
}
// SetForegroundColor sets the foreground color of the graph
func (g *Graph) SetForegroundColor(clr *color.RGBA) {
g.fgColor = clr
g.redraw = true
}
// SetBackgroundColor sets the background color of the graph
func (g *Graph) SetBackgroundColor(clr *color.RGBA) {
g.bgColor = clr
g.redraw = true
}
// SetHighlightColor sets the highlight color of the graph
func (g *Graph) SetHighlightColor(clr *color.RGBA) {
g.hlColor = clr
g.redraw = true
}
// SetMin sets the min value for the graph scale
func (g *Graph) SetMin(min int) {
g.min = min
}
// SetMax sets the max value for the graph scale
func (g *Graph) SetMax(max int) {
g.max = max
}
// SetLabel given a key, set the initial text, position and color
func (g *Graph) SetLabel(key int, text string, y uint, clr *color.RGBA) {
l := &Label{text: text, y: y, clr: clr}
g.labels[key] = l
}
// SetLabelText given a key, update the text for a pre-set label
func (g *Graph) SetLabelText(key int, text string) error {
l, ok := g.labels[key]
if !ok {
return fmt.Errorf("Label with key (%d) does not exist", key)
}
l.text = text
return nil
}
// SetLabelFontSize given a key, update the text for a pre-set label
func (g *Graph) SetLabelFontSize(key int, size float64) error {
l, ok := g.labels[key]
if !ok {
return fmt.Errorf("Label with key (%d) does not exist", key)
}
l.fontSize = size
return nil
}
// SetLabelColor given a key and color, sets the color of the text
func (g *Graph) SetLabelColor(key int, clr *color.RGBA) error {
l, ok := g.labels[key]
if !ok {
return fmt.Errorf("Label with key (%d) does not exist", key)
}
l.clr = clr
return nil
}
func (g *Graph) drawGraph(x, vay, maxx int) {
var clr *color.RGBA
for ; x <= maxx; x++ {
for y := 0; y < g.height; y++ {
if y == vay {
clr = g.hlColor
} else if g.lvay != -1 && vay > g.lvay && vay >= y && y >= g.lvay {
clr = g.hlColor
} else if g.lvay != -1 && vay < g.lvay && vay <= y && y <= g.lvay {
clr = g.hlColor
} else if vay > y {
clr = g.fgColor
} else {
clr = g.bgColor
}
i := g.img.PixOffset(x, g.width-1-y)
g.img.Pix[i+0] = clr.R
g.img.Pix[i+1] = clr.G
g.img.Pix[i+2] = clr.B
g.img.Pix[i+3] = clr.A
}
g.lvay = vay
}
}
// Update given a value draws the graph, shifting contents left. Call EncodePNG to get a rendered PNG
func (g *Graph) Update(value float64) {
vay := vAsY(g.height-1, value, g.min, g.max)
if len(g.yvals) >= g.width {
_, a := g.yvals[0], g.yvals[1:]
g.yvals = a
}
g.yvals = append(g.yvals, uint8(vay))
if g.redraw {
g.lvay = -1
lyvals := len(g.yvals)
for idx := lyvals - 1; idx >= 0; idx-- {
x := g.width - lyvals + idx
maxx := x
if idx == 0 {
x = 0
}
v := int(g.yvals[idx])
g.drawGraph(x, v, maxx)
}
g.lvay = int(g.yvals[lyvals-1])
g.redraw = false
} else if g.drawn {
// shift the graph left 1px
for y := 0; y < g.height; y++ {
idx := g.img.PixOffset(0, y)
p1 := g.img.Pix[:idx]
p2 := g.img.Pix[idx+4 : idx+(g.width*4)]
p3 := g.img.Pix[idx+(g.width*4):]
g.img.Pix = append(p1, append(append(p2, []uint8{0, 0, 0, 0}...), p3...)...)
}
g.drawGraph(int(g.width)-1, int(vay), g.width-1)
} else {
g.drawGraph(0, vay, g.width-1)
g.drawn = true
}
}
// EncodePNG renders the current state of the graph
func (g *Graph) EncodePNG() ([]byte, error) {
bak := append(g.img.Pix[:0:0], g.img.Pix...)
for _, l := range g.labels {
g.drawLabel(l)
}
shared := shared()
err := shared.pngEnc.Encode(shared.pngBuf, g.img)
if err != nil {
return nil, err
}
g.img.Pix = bak
bts := shared.pngBuf.Bytes()
shared.pngBuf.Reset()
return bts, nil
}
func vAsY(maxY int, v float64, minV, maxV int) int {
r := maxV - minV
v1 := v - float64(minV)
yf := v1 / float64(r) * float64(maxY)
yi := int(math.Round(yf))
return yi
}
func unfix(x fixed.Int26_6) float64 {
const shift, mask = 6, 1<<6 - 1
if x >= 0 {
return float64(x>>shift) + float64(x&mask)/64
}
x = -x
if x >= 0 {
return -(float64(x>>shift) + float64(x&mask)/64)
}
return 0
}
var newlineRegex = regexp.MustCompile("(\n|\\\\n)+")
func (g *Graph) drawLabel(l *Label) {
shared := shared()
lines := newlineRegex.Split(l.text, -1)
face := shared.fontFaceManager.GetFaceOfSize(l.fontSize)
curY := l.y - uint(10.5-float64(face.Metrics().Height.Round()))
for _, line := range lines {
var lwidth float64
for _, x := range line {
awidth, ok := face.GlyphAdvance(rune(x))
if ok != true {
log.Println("drawLabel: Failed to GlyphAdvance")
return
}
lwidth += unfix(awidth)
}
lx := (float64(g.width) / 2.) - (lwidth / 2.)
point := fixed.Point26_6{X: fixed.Int26_6(lx * 64), Y: fixed.Int26_6(curY * 64)}
d := &font.Drawer{
Dst: g.img,
Src: image.NewUniform(l.clr),
Face: face,
Dot: point,
}
d.DrawString(line)
curY += 12
}
}

126
pkg/service/grpc.go Normal file
View File

@ -0,0 +1,126 @@
package hwsensorsservice
import (
"context"
"errors"
"io"
"github.com/golang/protobuf/ptypes/empty"
"github.com/shayne/hwinfo-streamdeck/pkg/service/proto"
)
// GRPCClient is an implementation of KV that talks over RPC.
type GRPCClient struct {
Client proto.HWServiceClient
}
// PollTime rpc call
func (c *GRPCClient) PollTime() (uint64, error) {
resp, err := c.Client.PollTime(context.Background(), &empty.Empty{})
if err != nil {
return 0, err
}
return resp.GetPollTime(), nil
}
// Sensors implementation
func (c *GRPCClient) Sensors() ([]Sensor, error) {
stream, err := c.Client.Sensors(context.Background(), &empty.Empty{})
if err != nil {
return nil, err
}
var sensors []Sensor
for {
s, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
sensors = append(sensors, &sensor{s})
}
return sensors, nil
}
// ReadingsForSensorID implementation
func (c *GRPCClient) ReadingsForSensorID(id string) ([]Reading, error) {
stream, err := c.Client.ReadingsForSensorID(context.Background(), &proto.SensorIDRequest{Id: id})
if err != nil {
return nil, err
}
var readings []Reading
for {
r, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
readings = append(readings, &reading{r})
}
return readings, nil
}
// GRPCServer is the gRPC server that GRPCClient talks to.
type GRPCServer struct {
// This is the real implementation
Impl HardwareService
proto.UnimplementedHWServiceServer
}
// PollTime gRPC wrapper
func (s *GRPCServer) PollTime(ctx context.Context, _ *empty.Empty) (*proto.PollTimeReply, error) {
v, err := s.Impl.PollTime()
return &proto.PollTimeReply{PollTime: v}, err
}
// Sensors gRPC wrapper
func (s *GRPCServer) Sensors(_ *empty.Empty, stream proto.HWService_SensorsServer) error {
sensors, err := s.Impl.Sensors()
if err != nil {
return err
}
for _, sensor := range sensors {
if err := stream.Send(&proto.Sensor{
ID: sensor.ID(),
Name: sensor.Name(),
}); err != nil {
return err
}
}
return nil
}
// ReadingsForSensorID gRPC wrapper
func (s *GRPCServer) ReadingsForSensorID(req *proto.SensorIDRequest, stream proto.HWService_ReadingsForSensorIDServer) error {
readings, err := s.Impl.ReadingsForSensorID(req.GetId())
if err != nil {
return err
}
for _, reading := range readings {
if err := stream.Send(&proto.Reading{
ID: reading.ID(),
TypeI: reading.TypeI(),
Type: reading.Type(),
Label: reading.Label(),
Unit: reading.Unit(),
Value: reading.Value(),
ValueMin: reading.ValueMin(),
ValueMax: reading.ValueMax(),
ValueAvg: reading.ValueAvg(),
}); err != nil {
return err
}
}
return nil
}

149
pkg/service/interface.go Normal file
View File

@ -0,0 +1,149 @@
package hwsensorsservice
import (
"context"
"google.golang.org/grpc"
"github.com/hashicorp/go-plugin"
"github.com/shayne/hwinfo-streamdeck/pkg/service/proto"
)
// Handshake is a common handshake that is shared by plugin and host.
var Handshake = plugin.HandshakeConfig{
// This isn't required when using VersionedPlugins
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
// PluginMap is the map of plugins we can dispense.
var PluginMap = map[string]plugin.Plugin{
"hwinfoplugin": &HardwareServicePlugin{},
}
// HardwareService is the interface that we're exposing as a plugin.
type HardwareService interface {
PollTime() (uint64, error)
Sensors() ([]Sensor, error)
ReadingsForSensorID(id string) ([]Reading, error)
}
// HardwareServicePlugin is the implementation of plugin.GRPCPlugin so we can serve/consume this.
type HardwareServicePlugin struct {
// GRPCPlugin must still implement the Plugin interface
plugin.Plugin
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
Impl HardwareService
}
// GRPCServer constructor
func (p *HardwareServicePlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
proto.RegisterHWServiceServer(s, &GRPCServer{Impl: p.Impl})
return nil
}
// GRPCClient constructor
func (p *HardwareServicePlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{Client: proto.NewHWServiceClient(c)}, nil
}
// Sensor is the common hardware interface for a sensor
type Sensor interface {
ID() string
Name() string
}
// ReadingType enum of value/unit type for reading
type ReadingType int
const (
// ReadingTypeNone no type
ReadingTypeNone ReadingType = iota
// ReadingTypeTemp temperature in celsius
ReadingTypeTemp
// ReadingTypeVolt voltage
ReadingTypeVolt
// ReadingTypeFan RPM
ReadingTypeFan
// ReadingTypeCurrent amps
ReadingTypeCurrent
// ReadingTypePower watts
ReadingTypePower
// ReadingTypeClock Mhz
ReadingTypeClock
// ReadingTypeUsage e.g. MBs
ReadingTypeUsage
// ReadingTypeOther other
ReadingTypeOther
)
func (t ReadingType) String() string {
return [...]string{"None", "Temp", "Volt", "Fan", "Current", "Power", "Clock", "Usage", "Other"}[t]
}
// Reading is the common hardware interface for a sensor's reading
type Reading interface {
ID() int32
TypeI() int32
Type() string
Label() string
Unit() string
Value() float64
ValueMin() float64
ValueMax() float64
ValueAvg() float64
}
type sensor struct {
*proto.Sensor
}
func (s sensor) ID() string {
return s.Sensor.GetID()
}
func (s sensor) Name() string {
return s.Sensor.GetName()
}
type reading struct {
*proto.Reading
}
func (r reading) ID() int32 {
return r.Reading.GetID()
}
func (r reading) Label() string {
return r.Reading.GetLabel()
}
func (r reading) Type() string {
return r.Reading.GetType()
}
func (r reading) TypeI() int32 {
return r.Reading.GetTypeI()
}
func (r reading) Unit() string {
return r.Reading.GetUnit()
}
func (r reading) Value() float64 {
return r.Reading.GetValue()
}
func (r reading) ValueMin() float64 {
return r.Reading.GetValueMin()
}
func (r reading) ValueMax() float64 {
return r.Reading.GetValueMax()
}
func (r reading) ValueAvg() float64 {
return r.Reading.GetValueAvg()
}

View File

@ -0,0 +1,439 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.21.12
// source: pkg/service/proto/hwservice.proto
package proto
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
emptypb "google.golang.org/protobuf/types/known/emptypb"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type PollTimeReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
PollTime uint64 `protobuf:"varint,1,opt,name=pollTime,proto3" json:"pollTime,omitempty"`
}
func (x *PollTimeReply) Reset() {
*x = PollTimeReply{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *PollTimeReply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*PollTimeReply) ProtoMessage() {}
func (x *PollTimeReply) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use PollTimeReply.ProtoReflect.Descriptor instead.
func (*PollTimeReply) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{0}
}
func (x *PollTimeReply) GetPollTime() uint64 {
if x != nil {
return x.PollTime
}
return 0
}
type Sensor struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
}
func (x *Sensor) Reset() {
*x = Sensor{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Sensor) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Sensor) ProtoMessage() {}
func (x *Sensor) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Sensor.ProtoReflect.Descriptor instead.
func (*Sensor) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{1}
}
func (x *Sensor) GetID() string {
if x != nil {
return x.ID
}
return ""
}
func (x *Sensor) GetName() string {
if x != nil {
return x.Name
}
return ""
}
type SensorIDRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
}
func (x *SensorIDRequest) Reset() {
*x = SensorIDRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SensorIDRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SensorIDRequest) ProtoMessage() {}
func (x *SensorIDRequest) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SensorIDRequest.ProtoReflect.Descriptor instead.
func (*SensorIDRequest) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{2}
}
func (x *SensorIDRequest) GetId() string {
if x != nil {
return x.Id
}
return ""
}
type Reading struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ID int32 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"`
TypeI int32 `protobuf:"varint,2,opt,name=typeI,proto3" json:"typeI,omitempty"`
Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"`
Label string `protobuf:"bytes,4,opt,name=label,proto3" json:"label,omitempty"`
Unit string `protobuf:"bytes,5,opt,name=unit,proto3" json:"unit,omitempty"`
Value float64 `protobuf:"fixed64,6,opt,name=value,proto3" json:"value,omitempty"`
ValueMin float64 `protobuf:"fixed64,7,opt,name=valueMin,proto3" json:"valueMin,omitempty"`
ValueMax float64 `protobuf:"fixed64,8,opt,name=valueMax,proto3" json:"valueMax,omitempty"`
ValueAvg float64 `protobuf:"fixed64,9,opt,name=valueAvg,proto3" json:"valueAvg,omitempty"`
}
func (x *Reading) Reset() {
*x = Reading{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Reading) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Reading) ProtoMessage() {}
func (x *Reading) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Reading.ProtoReflect.Descriptor instead.
func (*Reading) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{3}
}
func (x *Reading) GetID() int32 {
if x != nil {
return x.ID
}
return 0
}
func (x *Reading) GetTypeI() int32 {
if x != nil {
return x.TypeI
}
return 0
}
func (x *Reading) GetType() string {
if x != nil {
return x.Type
}
return ""
}
func (x *Reading) GetLabel() string {
if x != nil {
return x.Label
}
return ""
}
func (x *Reading) GetUnit() string {
if x != nil {
return x.Unit
}
return ""
}
func (x *Reading) GetValue() float64 {
if x != nil {
return x.Value
}
return 0
}
func (x *Reading) GetValueMin() float64 {
if x != nil {
return x.ValueMin
}
return 0
}
func (x *Reading) GetValueMax() float64 {
if x != nil {
return x.ValueMax
}
return 0
}
func (x *Reading) GetValueAvg() float64 {
if x != nil {
return x.ValueAvg
}
return 0
}
var File_pkg_service_proto_hwservice_proto protoreflect.FileDescriptor
var file_pkg_service_proto_hwservice_proto_rawDesc = []byte{
0x0a, 0x21, 0x70, 0x6b, 0x67, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2f, 0x68, 0x77, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67,
0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74,
0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2b, 0x0a, 0x0d, 0x50, 0x6f, 0x6c, 0x6c, 0x54,
0x69, 0x6d, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6f, 0x6c, 0x6c,
0x54, 0x69, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, 0x70, 0x6f, 0x6c, 0x6c,
0x54, 0x69, 0x6d, 0x65, 0x22, 0x2c, 0x0a, 0x06, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x12, 0x0e,
0x0a, 0x02, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x49, 0x44, 0x12, 0x12,
0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61,
0x6d, 0x65, 0x22, 0x21, 0x0a, 0x0f, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x49, 0x44, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0xd7, 0x01, 0x0a, 0x07, 0x52, 0x65, 0x61, 0x64, 0x69, 0x6e,
0x67, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49,
0x44, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x79, 0x70, 0x65, 0x49, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05,
0x52, 0x05, 0x74, 0x79, 0x70, 0x65, 0x49, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18,
0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6c,
0x61, 0x62, 0x65, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6c, 0x61, 0x62, 0x65,
0x6c, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x6e, 0x69, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x75, 0x6e, 0x69, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x06,
0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x76,
0x61, 0x6c, 0x75, 0x65, 0x4d, 0x69, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x76,
0x61, 0x6c, 0x75, 0x65, 0x4d, 0x69, 0x6e, 0x12, 0x1a, 0x0a, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x4d, 0x61, 0x78, 0x18, 0x08, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x4d, 0x61, 0x78, 0x12, 0x1a, 0x0a, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x76, 0x67, 0x18,
0x09, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x76, 0x67, 0x32,
0xc0, 0x01, 0x0a, 0x09, 0x48, 0x57, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3a, 0x0a,
0x08, 0x50, 0x6f, 0x6c, 0x6c, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67,
0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74,
0x79, 0x1a, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x6f, 0x6c, 0x6c, 0x54, 0x69,
0x6d, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x07, 0x53, 0x65, 0x6e,
0x73, 0x6f, 0x72, 0x73, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x0d, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x22, 0x00, 0x30, 0x01, 0x12,
0x41, 0x0a, 0x13, 0x52, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x73, 0x46, 0x6f, 0x72, 0x53, 0x65,
0x6e, 0x73, 0x6f, 0x72, 0x49, 0x44, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53,
0x65, 0x6e, 0x73, 0x6f, 0x72, 0x49, 0x44, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0e,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x22, 0x00,
0x30, 0x01, 0x42, 0x37, 0x5a, 0x35, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x73, 0x68, 0x61, 0x79, 0x6e, 0x65, 0x2f, 0x68, 0x77, 0x69, 0x6e, 0x66, 0x6f, 0x2d, 0x73,
0x74, 0x72, 0x65, 0x61, 0x6d, 0x64, 0x65, 0x63, 0x6b, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x33,
}
var (
file_pkg_service_proto_hwservice_proto_rawDescOnce sync.Once
file_pkg_service_proto_hwservice_proto_rawDescData = file_pkg_service_proto_hwservice_proto_rawDesc
)
func file_pkg_service_proto_hwservice_proto_rawDescGZIP() []byte {
file_pkg_service_proto_hwservice_proto_rawDescOnce.Do(func() {
file_pkg_service_proto_hwservice_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkg_service_proto_hwservice_proto_rawDescData)
})
return file_pkg_service_proto_hwservice_proto_rawDescData
}
var file_pkg_service_proto_hwservice_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_pkg_service_proto_hwservice_proto_goTypes = []interface{}{
(*PollTimeReply)(nil), // 0: proto.PollTimeReply
(*Sensor)(nil), // 1: proto.Sensor
(*SensorIDRequest)(nil), // 2: proto.SensorIDRequest
(*Reading)(nil), // 3: proto.Reading
(*emptypb.Empty)(nil), // 4: google.protobuf.Empty
}
var file_pkg_service_proto_hwservice_proto_depIdxs = []int32{
4, // 0: proto.HWService.PollTime:input_type -> google.protobuf.Empty
4, // 1: proto.HWService.Sensors:input_type -> google.protobuf.Empty
2, // 2: proto.HWService.ReadingsForSensorID:input_type -> proto.SensorIDRequest
0, // 3: proto.HWService.PollTime:output_type -> proto.PollTimeReply
1, // 4: proto.HWService.Sensors:output_type -> proto.Sensor
3, // 5: proto.HWService.ReadingsForSensorID:output_type -> proto.Reading
3, // [3:6] is the sub-list for method output_type
0, // [0:3] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_pkg_service_proto_hwservice_proto_init() }
func file_pkg_service_proto_hwservice_proto_init() {
if File_pkg_service_proto_hwservice_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_pkg_service_proto_hwservice_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*PollTimeReply); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_service_proto_hwservice_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Sensor); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_service_proto_hwservice_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SensorIDRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_service_proto_hwservice_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Reading); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_pkg_service_proto_hwservice_proto_rawDesc,
NumEnums: 0,
NumMessages: 4,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_pkg_service_proto_hwservice_proto_goTypes,
DependencyIndexes: file_pkg_service_proto_hwservice_proto_depIdxs,
MessageInfos: file_pkg_service_proto_hwservice_proto_msgTypes,
}.Build()
File_pkg_service_proto_hwservice_proto = out.File
file_pkg_service_proto_hwservice_proto_rawDesc = nil
file_pkg_service_proto_hwservice_proto_goTypes = nil
file_pkg_service_proto_hwservice_proto_depIdxs = nil
}

View File

@ -0,0 +1,34 @@
syntax = "proto3";
option go_package = "github.com/shayne/hwinfo-streamdeck/pkg/service/proto";
import "google/protobuf/empty.proto";
package proto;
service HWService {
rpc PollTime(google.protobuf.Empty) returns (PollTimeReply) {}
rpc Sensors(google.protobuf.Empty) returns (stream Sensor) {}
rpc ReadingsForSensorID(SensorIDRequest) returns (stream Reading) {}
}
message PollTimeReply { uint64 pollTime = 1; }
message Sensor {
string ID = 1;
string name = 2;
}
message SensorIDRequest { string id = 1; }
message Reading {
int32 ID = 1;
int32 typeI = 2;
string type = 3;
string label = 4;
string unit = 5;
double value = 6;
double valueMin = 7;
double valueMax = 8;
double valueAvg = 9;
}

View File

@ -0,0 +1,233 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.2.0
// - protoc v3.21.12
// source: pkg/service/proto/hwservice.proto
package proto
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
emptypb "google.golang.org/protobuf/types/known/emptypb"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
// HWServiceClient is the client API for HWService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type HWServiceClient interface {
PollTime(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PollTimeReply, error)
Sensors(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (HWService_SensorsClient, error)
ReadingsForSensorID(ctx context.Context, in *SensorIDRequest, opts ...grpc.CallOption) (HWService_ReadingsForSensorIDClient, error)
}
type hWServiceClient struct {
cc grpc.ClientConnInterface
}
func NewHWServiceClient(cc grpc.ClientConnInterface) HWServiceClient {
return &hWServiceClient{cc}
}
func (c *hWServiceClient) PollTime(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PollTimeReply, error) {
out := new(PollTimeReply)
err := c.cc.Invoke(ctx, "/proto.HWService/PollTime", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *hWServiceClient) Sensors(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (HWService_SensorsClient, error) {
stream, err := c.cc.NewStream(ctx, &HWService_ServiceDesc.Streams[0], "/proto.HWService/Sensors", opts...)
if err != nil {
return nil, err
}
x := &hWServiceSensorsClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type HWService_SensorsClient interface {
Recv() (*Sensor, error)
grpc.ClientStream
}
type hWServiceSensorsClient struct {
grpc.ClientStream
}
func (x *hWServiceSensorsClient) Recv() (*Sensor, error) {
m := new(Sensor)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *hWServiceClient) ReadingsForSensorID(ctx context.Context, in *SensorIDRequest, opts ...grpc.CallOption) (HWService_ReadingsForSensorIDClient, error) {
stream, err := c.cc.NewStream(ctx, &HWService_ServiceDesc.Streams[1], "/proto.HWService/ReadingsForSensorID", opts...)
if err != nil {
return nil, err
}
x := &hWServiceReadingsForSensorIDClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type HWService_ReadingsForSensorIDClient interface {
Recv() (*Reading, error)
grpc.ClientStream
}
type hWServiceReadingsForSensorIDClient struct {
grpc.ClientStream
}
func (x *hWServiceReadingsForSensorIDClient) Recv() (*Reading, error) {
m := new(Reading)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// HWServiceServer is the server API for HWService service.
// All implementations must embed UnimplementedHWServiceServer
// for forward compatibility
type HWServiceServer interface {
PollTime(context.Context, *emptypb.Empty) (*PollTimeReply, error)
Sensors(*emptypb.Empty, HWService_SensorsServer) error
ReadingsForSensorID(*SensorIDRequest, HWService_ReadingsForSensorIDServer) error
mustEmbedUnimplementedHWServiceServer()
}
// UnimplementedHWServiceServer must be embedded to have forward compatible implementations.
type UnimplementedHWServiceServer struct {
}
func (UnimplementedHWServiceServer) PollTime(context.Context, *emptypb.Empty) (*PollTimeReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method PollTime not implemented")
}
func (UnimplementedHWServiceServer) Sensors(*emptypb.Empty, HWService_SensorsServer) error {
return status.Errorf(codes.Unimplemented, "method Sensors not implemented")
}
func (UnimplementedHWServiceServer) ReadingsForSensorID(*SensorIDRequest, HWService_ReadingsForSensorIDServer) error {
return status.Errorf(codes.Unimplemented, "method ReadingsForSensorID not implemented")
}
func (UnimplementedHWServiceServer) mustEmbedUnimplementedHWServiceServer() {}
// UnsafeHWServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to HWServiceServer will
// result in compilation errors.
type UnsafeHWServiceServer interface {
mustEmbedUnimplementedHWServiceServer()
}
func RegisterHWServiceServer(s grpc.ServiceRegistrar, srv HWServiceServer) {
s.RegisterService(&HWService_ServiceDesc, srv)
}
func _HWService_PollTime_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(emptypb.Empty)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(HWServiceServer).PollTime(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/proto.HWService/PollTime",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(HWServiceServer).PollTime(ctx, req.(*emptypb.Empty))
}
return interceptor(ctx, in, info, handler)
}
func _HWService_Sensors_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(emptypb.Empty)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(HWServiceServer).Sensors(m, &hWServiceSensorsServer{stream})
}
type HWService_SensorsServer interface {
Send(*Sensor) error
grpc.ServerStream
}
type hWServiceSensorsServer struct {
grpc.ServerStream
}
func (x *hWServiceSensorsServer) Send(m *Sensor) error {
return x.ServerStream.SendMsg(m)
}
func _HWService_ReadingsForSensorID_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(SensorIDRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(HWServiceServer).ReadingsForSensorID(m, &hWServiceReadingsForSensorIDServer{stream})
}
type HWService_ReadingsForSensorIDServer interface {
Send(*Reading) error
grpc.ServerStream
}
type hWServiceReadingsForSensorIDServer struct {
grpc.ServerStream
}
func (x *hWServiceReadingsForSensorIDServer) Send(m *Reading) error {
return x.ServerStream.SendMsg(m)
}
// HWService_ServiceDesc is the grpc.ServiceDesc for HWService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var HWService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "proto.HWService",
HandlerType: (*HWServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "PollTime",
Handler: _HWService_PollTime_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "Sensors",
Handler: _HWService_Sensors_Handler,
ServerStreams: true,
},
{
StreamName: "ReadingsForSensorID",
Handler: _HWService_ReadingsForSensorID_Handler,
ServerStreams: true,
},
},
Metadata: "pkg/service/proto/hwservice.proto",
}

View File

@ -0,0 +1,295 @@
package streamdeck
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
// EventDelegate receives callbacks for Stream Deck SDK events
type EventDelegate interface {
OnConnected(*websocket.Conn)
OnWillAppear(*EvWillAppear)
OnTitleParametersDidChange(*EvTitleParametersDidChange)
OnPropertyInspectorConnected(*EvSendToPlugin)
OnSendToPlugin(*EvSendToPlugin)
OnApplicationDidLaunch(*EvApplication)
OnApplicationDidTerminate(*EvApplication)
}
// StreamDeck SDK APIs
type StreamDeck struct {
Port string
PluginUUID string
RegisterEvent string
Info string
delegate EventDelegate
conn *websocket.Conn
done chan struct{}
}
// NewStreamDeck prepares StreamDeck struct
func NewStreamDeck(port, pluginUUID, registerEvent, info string) *StreamDeck {
return &StreamDeck{
Port: port,
PluginUUID: pluginUUID,
RegisterEvent: registerEvent,
Info: info,
done: make(chan struct{}),
}
}
// SetDelegate sets the delegate for receiving Stream Deck SDK event callbacks
func (sd *StreamDeck) SetDelegate(ed EventDelegate) {
sd.delegate = ed
}
func (sd *StreamDeck) register() error {
reg := evRegister{Event: sd.RegisterEvent, UUID: sd.PluginUUID}
data, err := json.Marshal(reg)
log.Println(string(data))
if err != nil {
return err
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return err
}
return nil
}
// Connect establishes WebSocket connection to StreamDeck software
func (sd *StreamDeck) Connect() error {
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("127.0.0.1:%s", sd.Port)}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return err
}
sd.conn = c
err = sd.register()
if err != nil {
return fmt.Errorf("failed register: %v", err)
}
if sd.delegate != nil {
sd.delegate.OnConnected(sd.conn)
}
return nil
}
// Close closes the websocket connection, defer after Connect
func (sd *StreamDeck) Close() {
sd.conn.Close()
}
func (sd *StreamDeck) onPropertyInspectorMessage(value string, ev *EvSendToPlugin) error {
switch value {
case "propertyInspectorConnected":
if sd.delegate != nil {
sd.delegate.OnPropertyInspectorConnected(ev)
}
default:
log.Printf("Unknown property_inspector value: %s\n", value)
}
return nil
}
func (sd *StreamDeck) onSendToPlugin(ev *EvSendToPlugin) error {
payload := make(map[string]*json.RawMessage)
err := json.Unmarshal(*ev.Payload, &payload)
if err != nil {
return fmt.Errorf("onSendToPlugin payload unmarshal: %v", err)
}
if raw, ok := payload["property_inspector"]; ok {
var value string
err := json.Unmarshal(*raw, &value)
if err != nil {
return fmt.Errorf("onSendToPlugin unmarshal property_inspector value: %v", err)
}
err = sd.onPropertyInspectorMessage(value, ev)
if err != nil {
return fmt.Errorf("onPropertyInspectorMessage: %v", err)
}
return nil
}
if sd.delegate != nil {
sd.delegate.OnSendToPlugin(ev)
}
return nil
}
func (sd *StreamDeck) spawnMessageReader() {
defer close(sd.done)
for {
_, message, err := sd.conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
var objmap map[string]*json.RawMessage
err = json.Unmarshal(message, &objmap)
if err != nil {
log.Fatal("message unmarshal", err)
}
var event string
err = json.Unmarshal(*objmap["event"], &event)
if err != nil {
log.Fatal("event unmarshal", err)
}
switch event {
case "willAppear":
var ev EvWillAppear
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("willAppear unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnWillAppear(&ev)
}
case "titleParametersDidChange":
var ev EvTitleParametersDidChange
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("titleParametersDidChange unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnTitleParametersDidChange(&ev)
}
case "sendToPlugin":
var ev EvSendToPlugin
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("onSendToPlugin event unmarshal", err)
}
err = sd.onSendToPlugin(&ev)
if err != nil {
log.Fatal("onSendToPlugin", err)
}
case "applicationDidLaunch":
var ev EvApplication
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("applicationDidLaunch unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnApplicationDidLaunch(&ev)
}
case "applicationDidTerminate":
var ev EvApplication
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("applicationDidTerminate unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnApplicationDidTerminate(&ev)
}
default:
log.Printf("Unknown event: %s\n", event)
}
}
}
// ListenAndWait processes messages and waits until closed
func (sd *StreamDeck) ListenAndWait() {
go sd.spawnMessageReader()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
for {
select {
case <-sd.done:
return
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := sd.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-sd.done:
case <-time.After(time.Second):
}
}
}
}
// SendToPropertyInspector sends a payload to the Property Inspector
func (sd *StreamDeck) SendToPropertyInspector(action, context string, payload interface{}) error {
event := evSendToPropertyInspector{Action: action, Event: "sendToPropertyInspector",
Context: context, Payload: payload}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("sendToPropertyInspector: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setTitle write: %v", err)
}
return nil
}
// SetTitle dynamically changes the title displayed by an instance of an action
func (sd *StreamDeck) SetTitle(context, title string) error {
event := evSetTitle{Event: "setTitle", Context: context, Payload: evSetTitlePayload{
Title: title,
Target: 0,
}}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("setTitle: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setTitle write: %v", err)
}
return nil
}
// SetSettings saves persistent data for the action's instance
func (sd *StreamDeck) SetSettings(context string, payload interface{}) error {
event := evSetSettings{Event: "setSettings", Context: context, Payload: payload}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("setSettings: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setSettings write: %v", err)
}
return nil
}
// SetImage dynamically changes the image displayed by an instance of an action
func (sd *StreamDeck) SetImage(context string, bts []byte) error {
b64 := base64.StdEncoding.EncodeToString(bts)
event := evSetImage{Event: "setImage", Context: context, Payload: evSetImagePayload{
Image: fmt.Sprintf("data:image/png;base64, %s", b64),
Target: 0,
}}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("setImage: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setImage write: %v", err)
}
return nil
}

124
pkg/streamdeck/types.go Normal file
View File

@ -0,0 +1,124 @@
package streamdeck
import "encoding/json"
type evRegister struct {
Event string `json:"event"`
UUID string `json:"uuid"`
}
// EvCoordinates is the coordinates structure from events
type EvCoordinates struct {
Column int `json:"column"`
Row int `json:"row"`
}
// EvWillAppearPayload is the Payload structure from the willAppear event
type EvWillAppearPayload struct {
Settings *json.RawMessage `json:"settings"`
Coordinates EvCoordinates `json:"coordinates"`
Device string `json:"device"`
State int `json:"state"`
IsInMultiAction bool `json:"isInMultiAction"`
}
// EvWillAppear is the payload from the willAppear event
type EvWillAppear struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Device string `json:"device"`
Payload EvWillAppearPayload `json:"payload"`
}
// EvWillDisappearPayload is the Payload structure from willDisappear event
type EvWillDisappearPayload struct {
EvWillAppearPayload
}
// EvWillDisappear is the payload from the willDisappear event
type EvWillDisappear struct {
EvWillAppear
}
// EvApplicationPayload is the sub-strcture from the EvApplication struct
type EvApplicationPayload struct {
Application string `json:"application"`
}
// EvApplication is the payload from the applicatioDidLaunch/Terminate events
type EvApplication struct {
Payload EvApplicationPayload `json:"payload"`
}
// EvTitleParameters is sub-structure from EvTitleParametersDidChangePayload
type EvTitleParameters struct {
FontFamily string `json:"fontFamily"`
FontSize int `json:"fontSize"`
FontStyle string `json:"fontStyle"`
FontUnderline bool `json:"fontUnderline"`
ShowTitle bool `json:"showTitle"`
TitleAlignment string `json:"titleAlignment"`
TitleColor string `json:"titleColor"`
}
// EvTitleParametersDidChangePayload is the payload structure of EvTitleParametersDidChange
type EvTitleParametersDidChangePayload struct {
Coordinates EvCoordinates `json:"coordinates"`
Settings *json.RawMessage `json:"settings"`
TitleParameters EvTitleParameters `json:"titleParameters"`
Title string `json:"title"`
State int `json:"state"`
}
// EvTitleParametersDidChange is the payload from the titleParametersDidChange event
type EvTitleParametersDidChange struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Device string `json:"device"`
Payload EvTitleParametersDidChangePayload `json:"payload"`
}
// EvSendToPlugin is received from the Property Inspector
type EvSendToPlugin struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Payload *json.RawMessage `json:"payload"`
}
type evSendToPropertyInspector struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Payload interface{} `json:"payload"`
}
type evSetTitlePayload struct {
Title string `json:"title"`
Target int `json:"target"`
}
type evSetTitle struct {
Event string `json:"event"`
Context string `json:"context"`
Payload evSetTitlePayload `json:"payload"`
}
type evSetSettings struct {
Event string `json:"event"`
Context string `json:"context"`
Payload interface{} `json:"payload"`
}
type evSetImagePayload struct {
Image string `json:"image"`
Target int `json:"target"`
}
type evSetImage struct {
Event string `json:"event"`
Context string `json:"context"`
Payload evSetImagePayload `json:"payload"`
}

2
start-streamdeck.bat Normal file
View File

@ -0,0 +1,2 @@
@echo off
start "" /B "C:\Program Files\Elgato\StreamDeck\StreamDeck.exe"