Fix #522: add -volume-adjust-pct CLI arg
This commit is contained in:
@@ -435,6 +435,8 @@ func (a *App) checkFlagsAndSendIPCMsg(cli *ipc.Client) error {
|
|||||||
return cli.SeekNext()
|
return cli.SeekNext()
|
||||||
case VolumeCLIArg >= 0:
|
case VolumeCLIArg >= 0:
|
||||||
return cli.SetVolume(VolumeCLIArg)
|
return cli.SetVolume(VolumeCLIArg)
|
||||||
|
case VolumePctCLIArg != 0:
|
||||||
|
return cli.AdjustVolumePct(VolumePctCLIArg)
|
||||||
case SeekToCLIArg >= 0:
|
case SeekToCLIArg >= 0:
|
||||||
return cli.SeekSeconds(SeekToCLIArg)
|
return cli.SeekSeconds(SeekToCLIArg)
|
||||||
case SeekByCLIArg != 0:
|
case SeekByCLIArg != 0:
|
||||||
|
|||||||
@@ -3,12 +3,14 @@ package backend
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
VolumeCLIArg int = -1
|
VolumeCLIArg int = -1
|
||||||
SeekToCLIArg float64 = -1
|
SeekToCLIArg float64 = -1
|
||||||
SeekByCLIArg float64 = 0
|
SeekByCLIArg float64 = 0
|
||||||
|
VolumePctCLIArg float64 = 0
|
||||||
|
|
||||||
FlagPlay = flag.Bool("play", false, "unpause or begin playback")
|
FlagPlay = flag.Bool("play", false, "unpause or begin playback")
|
||||||
FlagPause = flag.Bool("pause", false, "pause playback")
|
FlagPause = flag.Bool("pause", false, "pause playback")
|
||||||
@@ -36,6 +38,14 @@ func init() {
|
|||||||
SeekByCLIArg = v
|
SeekByCLIArg = v
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
flag.Func("volume-adjust-pct", "adjusts volume up or down by the given percentage (positive or negative)", func(s string) error {
|
||||||
|
if strings.HasSuffix(s, "%") {
|
||||||
|
s = s[:len(s)-1]
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseFloat(s, 64)
|
||||||
|
VolumePctCLIArg = v
|
||||||
|
return err
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func HaveCommandLineOptions() bool {
|
func HaveCommandLineOptions() bool {
|
||||||
|
|||||||
+17
-12
@@ -3,18 +3,19 @@ package ipc
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PingPath = "/ping"
|
PingPath = "/ping"
|
||||||
PlayPath = "/transport/play"
|
PlayPath = "/transport/play"
|
||||||
PlayPausePath = "/transport/playpause"
|
PlayPausePath = "/transport/playpause"
|
||||||
PausePath = "/transport/pause"
|
PausePath = "/transport/pause"
|
||||||
StopPath = "/transport/stop"
|
StopPath = "/transport/stop"
|
||||||
PreviousPath = "/transport/previous"
|
PreviousPath = "/transport/previous"
|
||||||
NextPath = "/transport/next"
|
NextPath = "/transport/next"
|
||||||
TimePosPath = "/transport/timepos" // ?s=<seconds>
|
TimePosPath = "/transport/timepos" // ?s=<seconds>
|
||||||
SeekByPath = "/transport/seek-by" // ?s=<+/- seconds>
|
SeekByPath = "/transport/seek-by" // ?s=<+/- seconds>
|
||||||
VolumePath = "/volume" // ?v=<vol>
|
VolumePath = "/volume" // ?v=<vol>
|
||||||
ShowPath = "/window/show"
|
VolumeAdjustPath = "/volume/adjust" // ?pct=<+/- percentage>
|
||||||
QuitPath = "/window/quit"
|
ShowPath = "/window/show"
|
||||||
|
QuitPath = "/window/quit"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
@@ -25,6 +26,10 @@ func SetVolumePath(vol int) string {
|
|||||||
return fmt.Sprintf("%s?v=%d", VolumePath, vol)
|
return fmt.Sprintf("%s?v=%d", VolumePath, vol)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AdjustVolumePctPath(pct float64) string {
|
||||||
|
return fmt.Sprintf("%s?pct=%0.2f", VolumeAdjustPath, pct)
|
||||||
|
}
|
||||||
|
|
||||||
func SeekToSecondsPath(secs float64) string {
|
func SeekToSecondsPath(secs float64) string {
|
||||||
return fmt.Sprintf("%s?s=%0.2f", TimePosPath, secs)
|
return fmt.Sprintf("%s?s=%0.2f", TimePosPath, secs)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,10 @@ func (c *Client) SetVolume(vol int) error {
|
|||||||
return c.sendRequest(SetVolumePath(vol))
|
return c.sendRequest(SetVolumePath(vol))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) AdjustVolumePct(pct float64) error {
|
||||||
|
return c.sendRequest(AdjustVolumePctPath(pct))
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) Show() error {
|
func (c *Client) Show() error {
|
||||||
return c.sendRequest(ShowPath)
|
return c.sendRequest(ShowPath)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,8 +70,8 @@ func (s *serverImpl) createHandler() http.Handler {
|
|||||||
m.HandleFunc(StopPath, s.makeSimpleEndpointHandler(s.pbHandler.Stop))
|
m.HandleFunc(StopPath, s.makeSimpleEndpointHandler(s.pbHandler.Stop))
|
||||||
m.HandleFunc(PreviousPath, s.makeSimpleEndpointHandler(s.pbHandler.SeekBackOrPrevious))
|
m.HandleFunc(PreviousPath, s.makeSimpleEndpointHandler(s.pbHandler.SeekBackOrPrevious))
|
||||||
m.HandleFunc(NextPath, s.makeSimpleEndpointHandler(s.pbHandler.SeekNext))
|
m.HandleFunc(NextPath, s.makeSimpleEndpointHandler(s.pbHandler.SeekNext))
|
||||||
m.HandleFunc(TimePosPath, s.makeFloatEndpointHandler(s.pbHandler.SeekSeconds, "s"))
|
m.HandleFunc(TimePosPath, s.makeFloatEndpointHandler("s", s.pbHandler.SeekSeconds))
|
||||||
m.HandleFunc(SeekByPath, s.makeFloatEndpointHandler(s.pbHandler.SeekBySeconds, "s"))
|
m.HandleFunc(SeekByPath, s.makeFloatEndpointHandler("s", s.pbHandler.SeekBySeconds))
|
||||||
m.HandleFunc(VolumePath, func(w http.ResponseWriter, r *http.Request) {
|
m.HandleFunc(VolumePath, func(w http.ResponseWriter, r *http.Request) {
|
||||||
v := r.URL.Query().Get("v")
|
v := r.URL.Query().Get("v")
|
||||||
if vol, err := strconv.Atoi(v); err == nil {
|
if vol, err := strconv.Atoi(v); err == nil {
|
||||||
@@ -81,6 +81,11 @@ func (s *serverImpl) createHandler() http.Handler {
|
|||||||
s.writeErr(w, err)
|
s.writeErr(w, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
m.HandleFunc(VolumeAdjustPath, s.makeFloatEndpointHandler("pct", func(pct float64) {
|
||||||
|
vol := s.pbHandler.Volume()
|
||||||
|
vol = vol + int(float64(vol)*(pct/100))
|
||||||
|
s.pbHandler.SetVolume(vol) // will clamp to range for us
|
||||||
|
}))
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +96,7 @@ func (s *serverImpl) makeSimpleEndpointHandler(f func()) func(http.ResponseWrite
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *serverImpl) makeFloatEndpointHandler(f func(float64), queryParam string) func(http.ResponseWriter, *http.Request) {
|
func (s *serverImpl) makeFloatEndpointHandler(queryParam string, f func(float64)) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
v := r.URL.Query().Get(queryParam)
|
v := r.URL.Query().Get(queryParam)
|
||||||
if val, err := strconv.ParseFloat(v, 64); err == nil {
|
if val, err := strconv.ParseFloat(v, 64); err == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user