Skip to content

Commit

Permalink
feat: add MediaPlayer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
leberKleber committed Dec 25, 2023
1 parent fa8117a commit 18d016b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ https://specifications.freedesktop.org/mpris-spec/2.2/Media_Player.html

#### Methods

| method | library path | implemented |
|--------|-----------------------------|--------------------------|
| Raise | `mpris.MediaPlayer.Raise()` | :heavy_multiplication_x: |
| Quit | `mpris.MediaPlayer.Quit()` | :heavy_multiplication_x: |
| method | library path | implemented |
|--------|-----------------------------|--------------------|
| Raise | `mpris.MediaPlayer.Raise()` | :heavy_check_mark: |
| Quit | `mpris.MediaPlayer.Quit()` | :heavy_check_mark: |

#### Properties

Expand Down
35 changes: 30 additions & 5 deletions media-player.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"github.com/godbus/dbus/v5"
)

const (
mediaPlayerObjectPath = "/org/mpris/MediaPlayer2"
mediaPlayerInterface = "org.mpris.MediaPlayer2"
mediaPlayerRaiseMethod = mediaPlayerInterface + ".Raise"
mediaPlayerQuitMethod = mediaPlayerInterface + ".Quit"
)

// MediaPlayer is an implementation of dbus org.mpris.MediaPlayer2.
// See: https://specifications.freedesktop.org/mpris-spec/2.2/Media_Player.html.
// Use NewMediaPlayer to create a new instance with a connected session-bus via dbus.SessionBus.
Expand All @@ -16,21 +23,21 @@ type MediaPlayer struct {

// NewMediaPlayer returns a new MediaPlayer which is already connected to session-bus via dbus.SessionBus.
// Don't forget to MediaPlayer.Close() the MediaPlayer after use.
func NewMediaPlayer(name string) (Player, error) {
func NewMediaPlayer(name string) (MediaPlayer, error) {
connection, err := dbusSessionBus()
if err != nil {
return Player{}, fmt.Errorf("failed to connect to session-bus: %w", err)
return MediaPlayer{}, fmt.Errorf("failed to connect to session-bus: %w", err)
}

return NewPlayerWithConnection(name, connection), nil
return NewMediaPlayerWithConnection(name, connection), nil
}

// NewMediaPlayerWithConnection returns a new MediaPlayer with the given name and connection.
// Deprecated: NewMediaPlayerWithConnection will be removed in the future.
// Plain Struct initialization should be used instead.
// Private fields will be public.
func NewMediaPlayerWithConnection(name string, connection *dbus.Conn) Player {
return Player{
func NewMediaPlayerWithConnection(name string, connection *dbus.Conn) MediaPlayer {
return MediaPlayer{
name: name,
connection: &dbusConnWrapper{
conn: connection,
Expand All @@ -47,3 +54,21 @@ func (mp MediaPlayer) Close() error {

return nil
}

// Raise brings the media player's user interface to the front using any appropriate mechanism available.
// The media player may be unable to control how its user interface is displayed, or it may not have a graphical user interface at all.
// In this case, the CanRaise property is false and this method does nothing.
// see: https://specifications.freedesktop.org/mpris-spec/2.2/Media_Player.html#Method:Raise
func (mp MediaPlayer) Raise() {
mp.connection.Object(mp.name, mediaPlayerObjectPath).Call(mediaPlayerRaiseMethod, 0)
}

// Quit causes the media player to stop running.
// The media player may refuse to allow clients to shut it down. In this case, the CanQuit property is false and this method does nothing.
// Note: Media players which can be D-Bus activated, or for which there is no sensibly easy way to terminate a running instance (via the main interface or a notification area icon for example) should allow clients to use this method.
// Otherwise, it should not be needed.
// If the media player does not have a UI, this should be implemented.
// see: https://specifications.freedesktop.org/mpris-spec/2.2/Media_Player.html#Method:Quit
func (mp MediaPlayer) Quit() {
mp.connection.Object(mp.name, mediaPlayerObjectPath).Call(mediaPlayerQuitMethod, 0)
}
60 changes: 60 additions & 0 deletions media-player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,63 @@ func TestMediaPlayer_Close(t *testing.T) {
})
}
}

func TestMediaPlayer_Methods(t *testing.T) {
tests := []struct {
name string
givenName string
action func(p *MediaPlayer)
expectedDest string
expectedPath dbus.ObjectPath
expectedMethod string
expectedFlags dbus.Flags
expectedArgs []interface{}
}{
{
name: "Raise",
givenName: "raise",
action: func(p *MediaPlayer) {
p.Raise()
},
expectedDest: "raise",
expectedPath: "/org/mpris/MediaPlayer2",
expectedMethod: "org.mpris.MediaPlayer2.Raise",
expectedFlags: 0,
expectedArgs: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var givenDest string
var givenPath dbus.ObjectPath
var givenMethod string
var givenFlags dbus.Flags
var givenArgs []interface{}

tt.action(&MediaPlayer{
name: tt.givenName,
connection: &dbusConnMock{
ObjectFunc: func(dest string, path dbus.ObjectPath) dbusBusObject {
givenDest = dest
givenPath = path
return &dbusBusObjectMock{
CallFunc: func(method string, flags dbus.Flags, args ...interface{}) dbusCall {
givenMethod = method
givenFlags = flags
givenArgs = args
return nil
},
}
},
},
})

assert.Equal(t, tt.expectedDest, givenDest, "given dest is not as expected")
assert.Equal(t, tt.expectedPath, givenPath, "given path is not as expected")
assert.Equal(t, tt.expectedMethod, givenMethod, "given method is not as expected")
assert.Equal(t, tt.expectedFlags, givenFlags, "given flags is not as expected")
assert.EqualValues(t, tt.expectedArgs, givenArgs, "given args is not as expected")
})
}
}

0 comments on commit 18d016b

Please # to comment.