Skip to content

Commit

Permalink
Close the program if mpv crashes
Browse files Browse the repository at this point in the history
If mpv exits unexpectedly for any reason, close the program without
waiting for output from the mpv communication channel.
  • Loading branch information
ejv2 committed Aug 8, 2024
1 parent 787b781 commit f7a4b03
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
TrayMessage
PlayerChanged
DownloadChanged
RequestShutdown
)
15 changes: 11 additions & 4 deletions sound/sound.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func endWait(u chan int) {
Plr.NowPodcast = ""

// Set state to finished
data.Q.Range(func(i int, item *data.QueueItem) bool {
data.Q.Range(func(_ int, item *data.QueueItem) bool {
if item.Path == Plr.Now.Path {
item.State = data.StateFinished
item.Date = time.Now().Unix()
Expand Down Expand Up @@ -169,11 +169,20 @@ func (p *Player) start() {
p.proc = exec.Command(PlayerName, PlayerArgs...)
p.proc.Start()

go p.procWatcher()

for err := p.connect(); err != nil; {
err = p.connect()
}
}

func (p *Player) procWatcher() {
p.proc.Wait()
// Should mpv exit unexpectedly, we need to close the program
// gracefully.
p.hndl.Post(ev.RequestShutdown)
}

func (p *Player) load(filename string) {
if p.proc == nil || p.ctrl == nil {
p.start()
Expand Down Expand Up @@ -214,8 +223,6 @@ func (p *Player) play(q *data.QueueItem) {
// Stop ends playback of the current audio track, but does not
// destroy the sound mainloop. This will usually result in the
// next podcast playing.
//
// TODO: Add some way of stopping the sound mainloop.
func (p *Player) Stop() {
p.act <- actStop
}
Expand Down Expand Up @@ -416,7 +423,7 @@ func Mainloop() {
wait = endWait

// Set status to played
data.Q.Range(func(i int, item *data.QueueItem) bool {
data.Q.Range(func(_ int, item *data.QueueItem) bool {
if item.Path == elem.Path {
item.State = data.StatePlayed
item.Date = time.Now().Unix()
Expand Down
2 changes: 1 addition & 1 deletion ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func InputLoop(exit chan struct{}) {
PassKeystroke(c)
}

events <- ev.Keystroke
eventsHndl.Post(ev.Keystroke)
case <-exit:
return
}
Expand Down
5 changes: 5 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ func RenderLoop() {
if !ok {
return
}
if event == ev.RequestShutdown {
close(exitChan)
return
}

if event == ev.Resize {
UpdateDimensions(root)
renderMenu()
Expand Down

0 comments on commit f7a4b03

Please sign in to comment.