Skip to content

Commit 8849b09

Browse files
authored
Allow the user to configure the position update interval (#32)
Also updated readme with current help output
1 parent 1fb5298 commit 8849b09

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ Install scripts coming soon.
2020
# mpd-mpris --help
2121
Usage of mpd-mpris:
2222
-host string
23-
The MPD host. (default "localhost")
23+
The MPD host (default localhost)
24+
-interval duration
25+
How often to update the current song position. Set to 0 to never update the current song position. (default 1s)
26+
-network string
27+
The network used to dial to the mpd server. Check https://golang.org/pkg/net/#Dial for available values (most common are "tcp" and "unix") (default "tcp")
2428
-no-instance
2529
Set the MPDris's interface as 'org.mpris.MediaPlayer2.mpd' instead of 'org.mpris.MediaPlayer2.mpd.instance#'
2630
-port int
27-
The MPD port (default 6600)
31+
The MPD port. Only works if network is "tcp". If you use anything else, you should put the port inside addr yourself. (default 6600)
2832
-pwd string
2933
The MPD connection password. Leave empty for none.
3034
```

cmd/mpd-mpris/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10+
"time"
1011

1112
mpris "github.com/natsukagami/mpd-mpris"
1213
"github.com/natsukagami/mpd-mpris/mpd"
@@ -19,6 +20,7 @@ var (
1920
password string
2021

2122
noInstance bool
23+
interval time.Duration
2224
)
2325

2426
func init() {
@@ -27,6 +29,7 @@ func init() {
2729
flag.IntVar(&port, "port", 6600, "The MPD port. Only works if network is \"tcp\". If you use anything else, you should put the port inside addr yourself.")
2830
flag.StringVar(&password, "pwd", "", "The MPD connection password. Leave empty for none.")
2931
flag.BoolVar(&noInstance, "no-instance", false, "Set the MPDris's interface as 'org.mpris.MediaPlayer2.mpd' instead of 'org.mpris.MediaPlayer2.mpd.instance#'")
32+
flag.DurationVar(&interval, "interval", time.Second, "How often to update the current song position. Set to 0 to never update the current song position.")
3033
}
3134

3235
func detectLocalSocket() {
@@ -93,7 +96,7 @@ func main() {
9396
opts = append(opts, mpris.NoInstance())
9497
}
9598

96-
instance, err := mpris.NewInstance(c, opts...)
99+
instance, err := mpris.NewInstance(c, interval, opts...)
97100

98101
if err != nil {
99102
panic(err)

instance.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mpris
33
import (
44
"fmt"
55
"os"
6+
"time"
67

78
"github.com/godbus/dbus/v5/introspect"
89

@@ -33,7 +34,7 @@ func (ins *Instance) Name() string {
3334
}
3435

3536
// NewInstance creates a new instance that takes care of the specified mpd.
36-
func NewInstance(mpd *mpd.Client, opts ...Option) (ins *Instance, err error) {
37+
func NewInstance(mpd *mpd.Client, interval time.Duration, opts ...Option) (ins *Instance, err error) {
3738
ins = &Instance{
3839
mpd: mpd,
3940

@@ -52,7 +53,7 @@ func NewInstance(mpd *mpd.Client, opts ...Option) (ins *Instance, err error) {
5253
ins.dbus.Export(mp2, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2")
5354

5455
player := &Player{Instance: ins}
55-
player.createStatus()
56+
player.createStatus(interval)
5657
ins.dbus.Export(player, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player")
5758

5859
ins.dbus.Export(introspect.NewIntrospectable(ins.IntrospectNode()), "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Introspectable")

player.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (p *Player) OnShuffle(c *prop.Change) *dbus.Error {
221221
return p.transformErr(p.mpd.Random(c.Value.(bool)))
222222
}
223223

224-
func (p *Player) createStatus() {
224+
func (p *Player) createStatus(interval time.Duration) {
225225
status, err := p.mpd.Status()
226226
if err != nil {
227227
log.Fatalf("%+v", err)
@@ -261,15 +261,17 @@ func (p *Player) createStatus() {
261261
}
262262

263263
// Set up a position updater
264-
go func() {
265-
tick := time.NewTicker(time.Second / 10)
266-
defer tick.Stop()
267-
for range tick.C {
268-
if err := p.status.Update(p); err != nil {
269-
log.Printf("%+v\n", err)
264+
if interval > 0 {
265+
go func() {
266+
tick := time.NewTicker(interval)
267+
defer tick.Stop()
268+
for range tick.C {
269+
if err := p.status.Update(p); err != nil {
270+
log.Printf("%+v\n", err)
271+
}
270272
}
271-
}
272-
}()
273+
}()
274+
}
273275

274276
p.props = map[string]*prop.Prop{
275277
"PlaybackStatus": newProp(playStatus, nil),

0 commit comments

Comments
 (0)