2023-08-09 10:12:38 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-08-09 20:35:04 -06:00
|
|
|
"fmt"
|
2023-08-09 10:12:38 -06:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"macvolumecontrol/logging"
|
2023-08-09 20:35:04 -06:00
|
|
|
"macvolumecontrol/volume"
|
2023-08-09 10:12:38 -06:00
|
|
|
|
|
|
|
"code.encyclopediaofdaniel.com/dlprows/streamdeck-sdk"
|
2023-08-09 20:35:04 -06:00
|
|
|
sdcontext "code.encyclopediaofdaniel.com/dlprows/streamdeck-sdk/context"
|
2023-08-09 10:12:38 -06:00
|
|
|
)
|
|
|
|
|
2023-08-09 20:35:04 -06:00
|
|
|
var _currentSettings *volume.VolumeSettings = &volume.VolumeSettings{}
|
|
|
|
|
2023-08-09 10:12:38 -06:00
|
|
|
func main() {
|
|
|
|
logging.Enable()
|
|
|
|
log.Println("Starting plugin")
|
|
|
|
|
|
|
|
params, err := streamdeck.ParseRegistrationParams(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error parsing registration params: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sd := streamdeck.NewClient(context.Background(), params)
|
|
|
|
defer sd.Close()
|
|
|
|
|
|
|
|
setup(sd)
|
|
|
|
|
|
|
|
if err := sd.Run(); err != nil {
|
|
|
|
log.Fatalf("error running streamdeck client: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(client *streamdeck.Client) {
|
|
|
|
log.Println("Registering actions")
|
|
|
|
action := client.Action("com.dlprows.macvolumecontrol.dialaction")
|
|
|
|
|
|
|
|
contexts := make(map[string]struct{})
|
|
|
|
|
|
|
|
action.RegisterHandler(streamdeck.DialRotate, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
|
|
|
log.Println("dial rotate")
|
|
|
|
|
|
|
|
p := streamdeck.DialRotatePayload[any]{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(event.Payload, &p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-09 20:35:04 -06:00
|
|
|
//volume.ChangeVolumeWithKeyboard(p.Ticks)
|
|
|
|
newSettings, err := volume.ChangeVolume(p.Ticks)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-08-09 10:12:38 -06:00
|
|
|
}
|
|
|
|
|
2023-08-09 20:35:04 -06:00
|
|
|
return setFeedbackIfNeeded(ctx, client, newSettings)
|
2023-08-09 10:12:38 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
action.RegisterHandler(streamdeck.DialDown, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
|
|
|
log.Println("dial down")
|
2023-08-09 20:35:04 -06:00
|
|
|
|
|
|
|
newSettings, err := volume.ToggleMute()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return setFeedbackIfNeeded(ctx, client, newSettings)
|
|
|
|
})
|
|
|
|
|
|
|
|
action.RegisterHandler(streamdeck.TouchTap, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
|
|
|
log.Println("touch tap")
|
|
|
|
|
|
|
|
newSettings, err := volume.ToggleMute()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return setFeedbackIfNeeded(ctx, client, newSettings)
|
2023-08-09 10:12:38 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
|
|
|
log.Println("Will Appear")
|
|
|
|
contexts[event.Context] = struct{}{}
|
2023-08-09 20:35:04 -06:00
|
|
|
|
|
|
|
newSettings, err := volume.GetVolumeSettings()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return setFeedbackIfNeeded(ctx, client, newSettings)
|
2023-08-09 10:12:38 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
action.RegisterHandler(streamdeck.WillDisappear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
|
|
|
|
log.Println("Will Disappear")
|
|
|
|
delete(contexts, event.Context)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
//start background thread to keep the display up to date if changed outside the stream deck
|
2023-08-09 20:35:04 -06:00
|
|
|
go func() {
|
|
|
|
for range time.Tick(time.Second * 1) {
|
|
|
|
newSettings, err := volume.GetVolumeSettings()
|
2023-08-09 10:12:38 -06:00
|
|
|
|
2023-08-09 20:35:04 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-08-09 10:12:38 -06:00
|
|
|
|
2023-08-09 20:35:04 -06:00
|
|
|
for ctxStr := range contexts {
|
|
|
|
//for each context
|
|
|
|
//build a new context that can be used to perform outbound requests
|
2023-08-09 10:12:38 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = sdcontext.WithContext(ctx, ctxStr)
|
|
|
|
|
2023-08-09 20:35:04 -06:00
|
|
|
setFeedbackIfNeeded(ctx, client, newSettings)
|
2023-08-09 10:12:38 -06:00
|
|
|
}
|
2023-08-09 20:35:04 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFeedbackIfNeeded(ctx context.Context, client *streamdeck.Client, newSettings *volume.VolumeSettings) error {
|
|
|
|
|
|
|
|
if _currentSettings.OutputVolume == newSettings.OutputVolume && _currentSettings.OutputMuted == newSettings.OutputMuted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := FeedbackPayload{}
|
|
|
|
|
|
|
|
opacity := 1.0
|
|
|
|
|
|
|
|
if newSettings.OutputMuted {
|
|
|
|
opacity = 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Value = ValueWithOpacity[string]{
|
|
|
|
fmt.Sprintf("%d%%", newSettings.OutputVolume),
|
|
|
|
opacity,
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Indicator = ValueWithOpacity[int]{
|
|
|
|
newSettings.OutputVolume,
|
|
|
|
opacity,
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Icon = ValueWithOpacity[any]{nil, opacity}
|
|
|
|
|
|
|
|
_currentSettings = newSettings
|
|
|
|
return client.SetFeedback(ctx, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
type FeedbackPayload struct {
|
|
|
|
Value ValueWithOpacity[string] `json:"value"`
|
|
|
|
Indicator ValueWithOpacity[int] `json:"indicator"`
|
|
|
|
Icon ValueWithOpacity[any] `json:"icon"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ValueWithOpacity[T any] struct {
|
|
|
|
Value T `json:"value,omitempty"`
|
|
|
|
Opacity float64 `json:"opacity"`
|
|
|
|
}
|