Skip to content

Commit

Permalink
reactivate works through ipc now, at least on Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jun 12, 2024
1 parent e0974fd commit 8e815c6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 90 deletions.
101 changes: 28 additions & 73 deletions backend/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@ import (
"github.com/dweymouth/supersonic/backend/player"
"github.com/dweymouth/supersonic/backend/player/mpv"
"github.com/dweymouth/supersonic/backend/util"
"github.com/fsnotify/fsnotify"
"github.com/google/uuid"

"github.com/20after4/configdir"
"github.com/zalando/go-keyring"
)

const (
configFile = "config.toml"
portableDir = "supersonic_portable"
sessionDir = "session"
sessionLockFile = ".lock"
sessionActivateFile = ".activate"
savedQueueFile = "saved_queue.json"
themesDir = "themes"
configFile = "config.toml"
portableDir = "supersonic_portable"
savedQueueFile = "saved_queue.json"
themesDir = "themes"
)

var (
Expand All @@ -47,6 +43,7 @@ type App struct {
LocalPlayer *mpv.Player
UpdateChecker UpdateChecker
MPRISHandler *MPRISHandler
ipcServer ipc.IPCServer

// UI callbacks to be set in main
OnReactivate func()
Expand Down Expand Up @@ -91,24 +88,6 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
return nil, ErrAnotherInstance
}

/*
sessionPath := path.Join(confDir, sessionDir)
if _, err := os.Stat(path.Join(sessionPath, sessionLockFile)); err == nil {
log.Println("Another instance is running. Reactivating it...")
reactivateFile := path.Join(sessionPath, sessionActivateFile)
if f, err := os.Create(reactivateFile); err == nil {
f.Close()
}
time.Sleep(750 * time.Millisecond)
if _, err := os.Stat(reactivateFile); err == nil {
log.Println("No other instance responded. Starting as normal...")
os.RemoveAll(sessionPath)
} else {
return nil, ErrAnotherInstance
}
}
*/

log.Printf("Starting %s...", appName)
log.Printf("Using config dir: %s", confDir)
log.Printf("Using cache dir: %s", cacheDir)
Expand All @@ -124,19 +103,6 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
a.readConfig()
a.startConfigWriter(a.bgrndCtx)

/*
if !a.Config.Application.AllowMultiInstance {
log.Println("Creating session lock file")
os.MkdirAll(sessionPath, 0770)
if f, err := os.Create(path.Join(sessionPath, sessionLockFile)); err == nil {
f.Close()
} else {
log.Printf("error creating session file: %s", err.Error())
}
a.startSessionWatcher(sessionPath)
}
*/

a.UpdateChecker = NewUpdateChecker(appVersionTag, latestReleaseURL, &a.Config.Application.LastCheckedVersion)
a.UpdateChecker.Start(a.bgrndCtx, 24*time.Hour)

Expand All @@ -155,9 +121,14 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
a.ServerManager.SetPrefetchAlbumCoverCallback(func(coverID string) {
_, _ = a.ImageManager.GetCoverThumbnail(coverID)
})
listener, _ := ipc.Listen()
server := ipc.NewServer(a.PlaybackManager, nil)
go server.Serve(listener)

// Start IPC server
listener, err := ipc.Listen()
if err == nil {
a.ipcServer = ipc.NewServer(a.PlaybackManager, a.callOnReactivate,
func() { _ = a.callOnExit() })
go a.ipcServer.Serve(listener)
}

// OS media center integrations
a.setupMPRIS(displayAppName)
Expand Down Expand Up @@ -211,26 +182,6 @@ func (a *App) readConfig() {
a.Config = cfg
}

func (a *App) startSessionWatcher(sessionPath string) {
if sessionWatch, err := fsnotify.NewWatcher(); err == nil {
sessionWatch.Add(sessionPath)
go func() {
for {
select {
case <-a.bgrndCtx.Done():
return
case <-sessionWatch.Events:
activatePath := path.Join(sessionPath, sessionActivateFile)
if _, err := os.Stat(activatePath); err == nil {
os.Remove(path.Join(sessionPath, sessionActivateFile))
a.callOnReactivate()
}
}
}
}()
}
}

// periodically save config file so abnormal exit won't lose settings
func (a *App) startConfigWriter(ctx context.Context) {
tick := time.NewTicker(2 * time.Minute)
Expand All @@ -254,6 +205,17 @@ func (a *App) callOnReactivate() {
}
}

func (a *App) callOnExit() error {
if a.OnExit == nil {
return errors.New("no quit handler registered")
}
go func() {
time.Sleep(10 * time.Millisecond)
a.OnExit()
}()
return nil
}

func (a *App) initMPV() error {
p := mpv.NewWithClientName(a.appName)
c := a.Config.LocalPlayback
Expand Down Expand Up @@ -329,16 +291,7 @@ func (a *App) setupMPRIS(mprisAppName string) {
return a.ImageManager.GetCoverArtUrl(id)
}
a.MPRISHandler.OnRaise = func() error { a.callOnReactivate(); return nil }
a.MPRISHandler.OnQuit = func() error {
if a.OnExit == nil {
return errors.New("no quit handler registered")
}
go func() {
time.Sleep(10 * time.Millisecond)
a.OnExit()
}()
return nil
}
a.MPRISHandler.OnQuit = a.callOnExit
a.MPRISHandler.Start()
}

Expand All @@ -361,6 +314,9 @@ func (a *App) DeleteServerCacheDir(serverID uuid.UUID) error {
}

func (a *App) Shutdown() {
if a.ipcServer != nil {
a.ipcServer.Shutdown(a.bgrndCtx)
}
a.MPRISHandler.Shutdown()
a.PlaybackManager.DisableCallbacks()
if a.Config.Application.SavePlayQueue {
Expand All @@ -377,7 +333,6 @@ func (a *App) Shutdown() {
a.cancel()
a.LocalPlayer.Destroy()
a.Config.WriteConfigFile(a.configFilePath())
os.RemoveAll(path.Join(a.configDir, sessionDir))
}

func (a *App) LoadSavedPlayQueue() error {
Expand Down
7 changes: 1 addition & 6 deletions backend/ipc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ type Client struct {

// Connect attempts to connect to the IPC socket as client.
func Connect() (*Client, error) {
conn, err := Dial()
if err != nil {
log.Println("dial error")
return nil, err
}
client := &Client{httpC: http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return conn, nil
return Dial()
},
},
}}
Expand Down
9 changes: 8 additions & 1 deletion backend/ipc/conn_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

package ipc

import "net"
import (
"net"
"os"
)

func Dial() (net.Conn, error) {
// TODO - use XDG runtime dir, also handle portable mode
Expand All @@ -12,3 +15,7 @@ func Dial() (net.Conn, error) {
func Listen() (net.Listener, error) {
return net.Listen("unix", "/tmp/supersonic.sock")
}

func DestroyConn() error {
return os.Remove("/tmp/supersonic.sock")
}
5 changes: 5 additions & 0 deletions backend/ipc/conn_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ func Dial() (net.Conn, error) {
func Listen() (net.Listener, error) {
return winio.ListenPipe("supersonic", nil)
}

func DestroyConn() error {
// Windows named pipes automatically clean up
return nil
}
33 changes: 24 additions & 9 deletions backend/ipc/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ipc

import (
"context"
"encoding/json"
"net"
"net/http"
)

Expand All @@ -17,21 +19,34 @@ type PlaybackHandler interface {
SetVolume(int) error
}

type WindowHandler interface {
Show()
Quit()
type IPCServer interface {
Serve(net.Listener) error
Shutdown(context.Context) error
}

type serverImpl struct {
server *http.Server
pbHandler PlaybackHandler
wdHandler WindowHandler
showFn func()
quitFn func()
}

func NewServer(pbHandler PlaybackHandler, wdHandler WindowHandler) *http.Server {
s := serverImpl{pbHandler: pbHandler, wdHandler: wdHandler}
return &http.Server{
func NewServer(pbHandler PlaybackHandler, showFn, quitFn func()) IPCServer {
s := &serverImpl{pbHandler: pbHandler, showFn: showFn, quitFn: quitFn}
s.server = &http.Server{
Handler: s.createHandler(),
}
return s
}

func (s *serverImpl) Serve(listener net.Listener) error {
return s.server.Serve(listener)
}

func (s *serverImpl) Shutdown(ctx context.Context) error {
err := s.server.Shutdown(ctx)
DestroyConn()
return err
}

func (s *serverImpl) createHandler() http.Handler {
Expand All @@ -42,11 +57,11 @@ func (s *serverImpl) createHandler() http.Handler {
})
m.HandleFunc(PingPath, s.makeSimpleEndpointHandler(func() error { return nil }))
m.HandleFunc(ShowPath, s.makeSimpleEndpointHandler(func() error {
s.wdHandler.Show()
s.showFn()
return nil
}))
m.HandleFunc(QuitPath, s.makeSimpleEndpointHandler(func() error {
go s.wdHandler.Quit()
s.quitFn()
return nil
}))
m.HandleFunc(PlayPath, s.makeSimpleEndpointHandler(s.pbHandler.Continue))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645
github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee
github.com/dweymouth/go-subsonic v0.0.0-20240603150834-605046e7c78a
github.com/fsnotify/fsnotify v1.7.0
github.com/godbus/dbus/v5 v5.1.0
github.com/google/uuid v1.3.0
github.com/pelletier/go-toml/v2 v2.0.8
Expand All @@ -30,6 +29,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/fredbi/uri v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
github.com/fyne-io/glfw-js v0.0.0-20240101223322-6e1efdc71b7a // indirect
github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
Expand Down

0 comments on commit 8e815c6

Please sign in to comment.