Skip to content

Commit

Permalink
put mpris handling into own package, rename mpv wrapper to mpvplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Oct 26, 2023
1 parent f389612 commit 5e489d5
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 85 deletions.
26 changes: 13 additions & 13 deletions event_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"time"

"github.com/wildeyedskies/stmp/mpv"
"github.com/wildeyedskies/stmp/mpvplayer"
)

type eventLoop struct {
Expand Down Expand Up @@ -51,30 +51,30 @@ func (ui *Ui) guiEventLoop() {
case mpvEvent := <-ui.mpvEvents:
// handle events from mpv wrapper
switch mpvEvent.Type {
case mpv.EventStatus:
case mpvplayer.EventStatus:
if mpvEvent.Data == nil {
continue
}
statusData := mpvEvent.Data.(mpv.StatusData) // TODO is this safe to access? maybe we need a copy
statusData := mpvEvent.Data.(mpvplayer.StatusData) // TODO is this safe to access? maybe we need a copy

ui.app.QueueUpdateDraw(func() {
ui.playerStatus.SetText(formatPlayerStatus(statusData.Volume, statusData.Position, statusData.Duration))
})

case mpv.EventStopped:
case mpvplayer.EventStopped:
ui.logger.Print("mpvEvent: stopped")
ui.app.QueueUpdateDraw(func() {
ui.startStopStatus.SetText("[::b]stmp: [red]Stopped")
updateQueueList(ui.player, ui.queueList, ui.starIdList)
})

case mpv.EventPlaying:
case mpvplayer.EventPlaying:
ui.logger.Print("mpvEvent: playing")
statusText := "[::b]stmp: [green]Playing"

var currentSong mpv.QueueItem
var currentSong mpvplayer.QueueItem
if mpvEvent.Data != nil {
currentSong = mpvEvent.Data.(mpv.QueueItem) // TODO is this safe to access? maybe we need a copy
currentSong = mpvEvent.Data.(mpvplayer.QueueItem) // TODO is this safe to access? maybe we need a copy
statusText += formatSongForStatusBar(&currentSong)

if ui.connection.Scrobble {
Expand Down Expand Up @@ -106,27 +106,27 @@ func (ui *Ui) guiEventLoop() {
updateQueueList(ui.player, ui.queueList, ui.starIdList)
})

case mpv.EventPaused:
case mpvplayer.EventPaused:
ui.logger.Print("mpvEvent: paused")
statusText := "[::b]stmp: [yellow]Paused"

var currentSong mpv.QueueItem
var currentSong mpvplayer.QueueItem
if mpvEvent.Data != nil {
currentSong = mpvEvent.Data.(mpv.QueueItem) // TODO is this safe to access? maybe we need a copy
currentSong = mpvEvent.Data.(mpvplayer.QueueItem) // TODO is this safe to access? maybe we need a copy
statusText += formatSongForStatusBar(&currentSong)
}

ui.app.QueueUpdateDraw(func() {
ui.startStopStatus.SetText(statusText)
})

case mpv.EventUnpaused:
case mpvplayer.EventUnpaused:
ui.logger.Print("mpvEvent: unpaused")
statusText := "[::b]stmp: [green]Playing"

var currentSong mpv.QueueItem
var currentSong mpvplayer.QueueItem
if mpvEvent.Data != nil {
currentSong = mpvEvent.Data.(mpv.QueueItem) // TODO is this safe to access? maybe we need a copy
currentSong = mpvEvent.Data.(mpvplayer.QueueItem) // TODO is this safe to access? maybe we need a copy
statusText += formatSongForStatusBar(&currentSong)
}

Expand Down
10 changes: 5 additions & 5 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/wildeyedskies/stmp/logger"
"github.com/wildeyedskies/stmp/mpv"
"github.com/wildeyedskies/stmp/mpvplayer"
"github.com/wildeyedskies/stmp/subsonic"
)

Expand Down Expand Up @@ -44,26 +44,26 @@ type Ui struct {
starIdList map[string]struct{}

eventLoop *eventLoop
mpvEvents chan mpv.UiEvent
mpvEvents chan mpvplayer.UiEvent

playlists []subsonic.SubsonicPlaylist
connection *subsonic.SubsonicConnection
player *mpv.Player
player *mpvplayer.Player
logger *logger.Logger
}

func InitGui(indexes *[]subsonic.SubsonicIndex,
playlists *[]subsonic.SubsonicPlaylist,
connection *subsonic.SubsonicConnection,
player *mpv.Player,
player *mpvplayer.Player,
logger *logger.Logger) (ui *Ui) {
ui = &Ui{
currentDirectory: &subsonic.SubsonicDirectory{},
artistIdList: []string{},
starIdList: map[string]struct{}{},

eventLoop: nil, // initialized by initEventLoops()
mpvEvents: make(chan mpv.UiEvent, 5),
mpvEvents: make(chan mpvplayer.UiEvent, 5),

playlists: *playlists,
connection: connection,
Expand Down
10 changes: 5 additions & 5 deletions gui_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/wildeyedskies/stmp/mpv"
"github.com/wildeyedskies/stmp/mpvplayer"
"github.com/wildeyedskies/stmp/subsonic"
)

Expand Down Expand Up @@ -409,7 +409,7 @@ func (ui *Ui) addSongToQueue(entity *subsonic.SubsonicEntity) {

var id = entity.Id

queueItem := &mpv.QueueItem{
queueItem := &mpvplayer.QueueItem{
Id: id,
Uri: uri,
Title: entity.GetSongTitle(),
Expand Down Expand Up @@ -452,7 +452,7 @@ func (ui *Ui) deletePlaylist(index int) {
}

func makeSongHandler(id string, uri string, title string, artist string, duration int,
player *mpv.Player, queueList *tview.List, starIdList map[string]struct{}) func() {
player *mpvplayer.Player, queueList *tview.List, starIdList map[string]struct{}) func() {
return func() {
player.Play(id, uri, title, artist, duration)
updateQueueList(player, queueList, starIdList)
Expand All @@ -465,7 +465,7 @@ func (ui *Ui) makeEntityHandler(directoryId string) func() {
}
}

func queueListTextFormat(queueItem mpv.QueueItem, starredItems map[string]struct{}) string {
func queueListTextFormat(queueItem mpvplayer.QueueItem, starredItems map[string]struct{}) string {
min, sec := iSecondsToMinAndSec(queueItem.Duration)
var star = ""
_, hasStar := starredItems[queueItem.Id]
Expand All @@ -480,7 +480,7 @@ func updateQueueListItem(queueList *tview.List, id int, text string) {
queueList.SetItemText(id, text, "")
}

func updateQueueList(player *mpv.Player, queueList *tview.List, starredItems map[string]struct{}) {
func updateQueueList(player *mpvplayer.Player, queueList *tview.List, starredItems map[string]struct{}) {
queueList.Clear()
for _, queueItem := range player.GetQueueCopy() { // TODO find a way without a deepcopy
queueList.AddItem(queueListTextFormat(queueItem, starredItems), "", 0, nil)
Expand Down
4 changes: 2 additions & 2 deletions gui_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/rivo/tview"
"github.com/wildeyedskies/stmp/mpv"
"github.com/wildeyedskies/stmp/mpvplayer"
)

func makeModal(p tview.Primitive, width, height int) tview.Primitive {
Expand All @@ -30,7 +30,7 @@ func formatPlayerStatus(volume int64, position float64, duration float64) string
positionMin, positionSec, durationMin, durationSec)
}

func formatSongForStatusBar(currentSong *mpv.QueueItem) (text string) {
func formatSongForStatusBar(currentSong *mpvplayer.QueueItem) (text string) {
if currentSong == nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions gui_mpvevents.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import "github.com/wildeyedskies/stmp/mpv"
import "github.com/wildeyedskies/stmp/mpvplayer"

func (ui *Ui) SendEvent(event mpv.UiEvent) {
func (ui *Ui) SendEvent(event mpvplayer.UiEvent) {
ui.mpvEvents <- event
}
2 changes: 1 addition & 1 deletion mpv/handler.go → mpvplayer/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mpv
package mpvplayer

import (
"github.com/wildeyedskies/go-mpv/mpv"
Expand Down
2 changes: 1 addition & 1 deletion mpv/interfaces.go → mpvplayer/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mpv
package mpvplayer

type UiEventType int

Expand Down
11 changes: 10 additions & 1 deletion mpv/player.go → mpvplayer/player.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mpv
package mpvplayer

import (
"errors"
Expand Down Expand Up @@ -117,6 +117,15 @@ func (p *Player) IsPaused() (bool, error) {
return pause.(bool), 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 {
} else {
playing = !idle.(bool) && !paused.(bool)
}
return
}

func (p *Player) Test() {
res, err := p.instance.GetProperty("idle-active", mpv.FORMAT_FLAG)
p.logger.Printf("res %v err %v", res, err)
Expand Down
2 changes: 1 addition & 1 deletion mpv/typed_shared.go → mpvplayer/typed_shared.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mpv
package mpvplayer

type QueueItem struct {
Id string
Expand Down
Loading

0 comments on commit 5e489d5

Please sign in to comment.