MacVolumeControl/src/main.go

129 lines
3.0 KiB
Go
Raw Normal View History

package main
import (
"context"
"encoding/json"
"log"
"os"
"time"
"macvolumecontrol/keyboard"
"macvolumecontrol/logging"
"code.encyclopediaofdaniel.com/dlprows/streamdeck-sdk"
)
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")
debounce := time.Now()
contexts := make(map[string]struct{})
action.RegisterHandler(streamdeck.DialRotate, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
log.Println("dial rotate")
t := time.Now()
elapsed := t.Sub(debounce)
if elapsed.Milliseconds() < 50 {
log.Println("dial rotate skipped")
return nil
}
debounce = t
p := streamdeck.DialRotatePayload[any]{}
if err := json.Unmarshal(event.Payload, &p); err != nil {
return err
}
if p.Ticks > 0 {
keyboard.PressMediaKey(keyboard.VOLUMEUP)
} else {
keyboard.PressMediaKey(keyboard.VOLUMEDOWN)
}
return nil
})
action.RegisterHandler(streamdeck.DialDown, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
log.Println("dial down")
keyboard.PressMediaKey(keyboard.MUTE)
return nil
})
action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
log.Println("Will Appear")
contexts[event.Context] = struct{}{}
return nil
})
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
//go func() {
//}()
}
/*
osascript -e 'output volume of (get volume settings)'
osascript -e 'set volume output volume 50'
osascript -e 'output muted of (get volume settings)'
osascript -e 'set volume output muted true'
*/
/*
for ctxStr := range contexts {
//for each context
//build a new context that can be used to perform outbound requests
ctx := context.Background()
ctx = sdcontext.WithContext(ctx, ctxStr)
img, err := streamdeck.Image(graph(readings))
if err != nil {
log.Printf("error creating image: %v\n", err)
continue
}
if err := client.SetImage(ctx, img, streamdeck.HardwareAndSoftware); err != nil {
log.Printf("error setting image: %v\n", err)
continue
}
title := ""
if pi.ShowText {
title = fmt.Sprintf("CPU\n%d%%", int(r[0]))
}
if err := client.SetTitle(ctx, title, streamdeck.HardwareAndSoftware); err != nil {
log.Printf("error setting title: %v\n", err)
continue
}
}
*/