Skip to content

Commit

Permalink
add getprop for bool, use type-safe variants everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Nov 4, 2023
1 parent f8e4ec4 commit 50a0ac4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
22 changes: 6 additions & 16 deletions mpvplayer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,23 @@ func (p *Player) EventLoop() {
} else if evt.Event_Id == mpv.EVENT_PROPERTY_CHANGE {
// one of our observed properties changed. which one is probably extractable from evt.Data.. somehow.

position, err := p.instance.GetProperty("playback-time", mpv.FORMAT_INT64)
position, err := p.getPropertyInt64("playback-time")
if err != nil {
p.logger.Printf("mpv.EventLoop (%s): GetProperty %s -- %s", evt.Event_Id.String(), "playback-time", err.Error())
}
duration, err := p.instance.GetProperty("duration", mpv.FORMAT_INT64)
duration, err := p.getPropertyInt64("duration")
if err != nil {
p.logger.Printf("mpv.EventLoop (%s): GetProperty %s -- %s", evt.Event_Id.String(), "duration", err.Error())
}
volume, err := p.instance.GetProperty("volume", mpv.FORMAT_INT64)
volume, err := p.getPropertyInt64("volume")
if err != nil {
p.logger.Printf("mpv.EventLoop (%s): GetProperty %s -- %s", evt.Event_Id.String(), "volume", err.Error())
}

if position == nil {
position = int64(0)
}
if duration == nil {
duration = int64(0)
}
if volume == nil {
volume = int64(0)
}

statusData := StatusData{
Volume: volume.(int64),
Position: position.(int64),
Duration: duration.(int64),
Volume: volume,
Position: position,
Duration: duration,
}
p.remoteState.timePos = float64(statusData.Position)
p.sendGuiDataEvent(EventStatus, statusData)
Expand Down
27 changes: 27 additions & 0 deletions mpvplayer/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mpvplayer

import (
"errors"

"github.com/spezifisch/go-mpv"
)

func (p *Player) getPropertyInt64(name string) (int64, error) {
value, err := p.instance.GetProperty(name, mpv.FORMAT_INT64)
if err != nil {
return 0, err
} else if value == nil {
return 0, errors.New("nil value")
}
return value.(int64), err
}

func (p *Player) getPropertyBool(name string) (bool, error) {
value, err := p.instance.GetProperty(name, mpv.FORMAT_FLAG)
if err != nil {
return false, err
} else if value == nil {
return false, errors.New("nil value")
}
return value.(bool), err
}
26 changes: 8 additions & 18 deletions mpvplayer/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@ func (p *Player) mpvEngineEventHandler(instance *mpv.Mpv) {
}
}

func (p *Player) getPropertyInt64(name string) (int64, error) {
value, err := p.instance.GetProperty(name, mpv.FORMAT_INT64)
if err != nil {
return 0, err
} else if value == nil {
return 0, errors.New("nil value")
}
return value.(int64), err
}

func (p *Player) Quit() {
p.mpvEvents <- nil
p.instance.TerminateDestroy()
Expand Down Expand Up @@ -156,26 +146,26 @@ func (p *Player) temporaryStop() error {
}

func (p *Player) IsSongLoaded() (bool, error) {
idle, err := p.instance.GetProperty("idle-active", mpv.FORMAT_FLAG)
return !idle.(bool), err
idle, err := p.getPropertyBool("idle-active")
return !idle, err
}

func (p *Player) IsPaused() (bool, error) {
pause, err := p.instance.GetProperty("pause", mpv.FORMAT_FLAG)
return pause.(bool), err
pause, err := p.getPropertyBool("pause")
return pause, err
}

func (p *Player) IsPlaying() (playing bool, err error) {
if idle, err := p.instance.GetProperty("idle-active", mpv.FORMAT_FLAG); err != nil {
} else if paused, err := p.instance.GetProperty("pause", mpv.FORMAT_FLAG); err != nil {
if idle, err := p.getPropertyBool("idle-active"); err != nil {
} else if paused, err := p.getPropertyBool("pause"); err != nil {
} else {
playing = !idle.(bool) && !paused.(bool)
playing = !idle && !paused
}
return
}

func (p *Player) Test() {
res, err := p.instance.GetProperty("idle-active", mpv.FORMAT_FLAG)
res, err := p.getPropertyBool("idle-active")
p.logger.Printf("res %v err %v", res, err)
}

Expand Down

0 comments on commit 50a0ac4

Please sign in to comment.