Skip to content

Commit

Permalink
Fixed #1: Implemented org.freedesktop.DBus.Introspectable
Browse files Browse the repository at this point in the history
  • Loading branch information
natsukagami committed Apr 18, 2018
1 parent 049443b commit 887a1fa
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 6 deletions.
12 changes: 10 additions & 2 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/godbus/dbus/introspect"

"github.com/godbus/dbus"
"github.com/godbus/dbus/prop"
"github.com/natsukagami/mpd-mpris/mpd"
Expand All @@ -23,9 +25,13 @@ func (ins *Instance) Close() {
ins.dbus.Close()
}

// Name returns the name of the instance.
func (ins *Instance) Name() string {
return fmt.Sprintf("org.mpris.MediaPlayer2.mpd.instance%d", os.Getpid())
}

// NewInstance creates a new instance that takes care of the specified mpd.
func NewInstance(mpd *mpd.Client) (ins *Instance, err error) {
// TODO: Unimplemented!
ins = &Instance{mpd: mpd}
if ins.dbus, err = dbus.SessionBus(); err != nil {
return nil, errors.WithStack(err)
Expand All @@ -37,12 +43,14 @@ func NewInstance(mpd *mpd.Client) (ins *Instance, err error) {
player := &Player{Instance: ins}
ins.dbus.Export(player, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player")

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

ins.props = prop.New(ins.dbus, "/org/mpris/MediaPlayer2", map[string]map[string]*prop.Prop{
"org.mpris.MediaPlayer2": mp2.properties(),
"org.mpris.MediaPlayer2.Player": player.properties(),
})

reply, err := ins.dbus.RequestName(fmt.Sprintf("org.mpris.MediaPlayer2.mpd.instance%d", os.Getpid()), dbus.NameFlagReplaceExisting)
reply, err := ins.dbus.RequestName(ins.Name(), dbus.NameFlagReplaceExisting)

if err != nil || reply != dbus.RequestNameReplyPrimaryOwner {
return nil, errors.WithStack(err)
Expand Down
194 changes: 194 additions & 0 deletions introspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package mpris

import (
introspect "github.com/godbus/dbus/introspect"
)

// IntrospectNode returns the root node of the library's introspection output.
func (i *Instance) IntrospectNode() *introspect.Node {
return &introspect.Node{
Name: i.Name(),
Interfaces: []introspect.Interface{
introspect.IntrospectData,
introspect.Interface{
Name: "org.mpris.MediaPlayer2",
Properties: []introspect.Property{
introspect.Property{
Name: "CanQuit",
Type: "b",
Access: "read",
},
introspect.Property{
Name: "CanRaise",
Type: "b",
Access: "read",
},
introspect.Property{
Name: "HasTrackList",
Type: "b",
Access: "read",
},
introspect.Property{
Name: "Identity",
Type: "s",
Access: "read",
},
introspect.Property{
Name: "SupportedUriSchemes",
Type: "as",
Access: "read",
},
introspect.Property{
Name: "SupportedMimeTypes",
Type: "as",
Access: "read",
},
},
Methods: []introspect.Method{
introspect.Method{
Name: "Raise",
},
introspect.Method{
Name: "Quit",
},
},
},
introspect.Interface{
Name: "org.mpris.MediaPlayer2.Player",
Properties: []introspect.Property{
introspect.Property{
Name: "PlaybackStatus",
Type: "s",
Access: "read",
},
introspect.Property{
Name: "LoopStatus",
Type: "s",
Access: "readwrite",
},
introspect.Property{
Name: "Rate",
Type: "d",
Access: "readwrite",
},
introspect.Property{
Name: "Shuffle",
Type: "b",
Access: "readwrite",
},
introspect.Property{
Name: "Metadata",
Type: "a{sv}",
Access: "read",
},
introspect.Property{
Name: "Volume",
Type: "d",
Access: "readwrite",
},
introspect.Property{
Name: "Position",
Type: "x",
Access: "read",
},
introspect.Property{
Name: "MinimumRate",
Type: "d",
Access: "read",
},
introspect.Property{
Name: "MaximumRate",
Type: "d",
Access: "read",
},
introspect.Property{
Name: "CanGoNext",
Type: "b",
Access: "read",
},
introspect.Property{
Name: "CanGoPrevious",
Type: "b",
Access: "read",
},
introspect.Property{
Name: "CanPlay",
Type: "b",
Access: "read",
},
introspect.Property{
Name: "CanSeek",
Type: "b",
Access: "read",
},
introspect.Property{
Name: "CanControl",
Type: "b",
Access: "read",
},
},
Signals: []introspect.Signal{
introspect.Signal{
Name: "Seeked",
Args: []introspect.Arg{
introspect.Arg{
Name: "Position",
Type: "x",
},
},
},
},
Methods: []introspect.Method{
introspect.Method{
Name: "Next",
},
introspect.Method{
Name: "Previous",
},
introspect.Method{
Name: "Pause",
},
introspect.Method{
Name: "PlayPause",
},
introspect.Method{
Name: "Stop",
},
introspect.Method{
Name: "Play",
},
introspect.Method{
Name: "Seek",
Args: []introspect.Arg{
introspect.Arg{
Name: "Offset",
Type: "x",
Direction: "in",
},
},
},
introspect.Method{
Name: "SetPosition",
Args: []introspect.Arg{
introspect.Arg{
Name: "TrackId",
Type: "o",
Direction: "in",
},
introspect.Arg{
Name: "Position",
Type: "x",
Direction: "in",
},
},
},
},
},
// TODO: This interface is not fully implemented.
// introspect.Interface{
// Name: "org.mpris.MediaPlayer2.TrackList",

// },
},
}
}
5 changes: 1 addition & 4 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ func (p *Player) updateStatus() *dbus.Error {
} else if err != nil {
return err
}
if err := p.Instance.props.Set("org.mpris.MediaPlayer2.Player", "Position", dbus.MakeVariant(UsFromDuration(status.Seek))); err != nil {
return err
}
return nil
return p.Instance.props.Set("org.mpris.MediaPlayer2.Player", "Position", dbus.MakeVariant(UsFromDuration(status.Seek)))
}

// OnLoopStatus handles LoopStatus change.
Expand Down

0 comments on commit 887a1fa

Please sign in to comment.