diff --git a/backend/app.go b/backend/app.go index 440e579d..cd915abb 100644 --- a/backend/app.go +++ b/backend/app.go @@ -12,6 +12,7 @@ import ( "slices" "time" + "github.com/dweymouth/supersonic/backend/ipc" "github.com/dweymouth/supersonic/backend/mediaprovider" "github.com/dweymouth/supersonic/backend/player" "github.com/dweymouth/supersonic/backend/player/mpv" @@ -83,22 +84,31 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string) configdir.MakePath(confDir) configdir.MakePath(cacheDir) - sessionPath := path.Join(confDir, sessionDir) - if _, err := os.Stat(path.Join(sessionPath, sessionLockFile)); err == nil { + cli, err := ipc.Connect() + if 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 - } + cli.Show() + 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) @@ -114,16 +124,18 @@ 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()) + /* + 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.startSessionWatcher(sessionPath) - } + */ a.UpdateChecker = NewUpdateChecker(appVersionTag, latestReleaseURL, &a.Config.Application.LastCheckedVersion) a.UpdateChecker.Start(a.bgrndCtx, 24*time.Hour) @@ -143,6 +155,9 @@ 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) // OS media center integrations a.setupMPRIS(displayAppName) diff --git a/backend/ipc/client.go b/backend/ipc/client.go index 134ed4f7..d03eeb6f 100644 --- a/backend/ipc/client.go +++ b/backend/ipc/client.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "log" "net" "net/http" ) @@ -18,6 +19,7 @@ type Client struct { func Connect() (*Client, error) { conn, err := Dial() if err != nil { + log.Println("dial error") return nil, err } client := &Client{httpC: http.Client{ @@ -28,6 +30,7 @@ func Connect() (*Client, error) { }, }} if err := client.Ping(); err != nil { + log.Println("ping error") return nil, err } return client, nil @@ -60,17 +63,26 @@ func (c *Client) SeekBackOrPrevious() error { return c.makeSimpleRequest(http.MethodPost, NextPath) } +func (c *Client) Show() error { + return c.makeSimpleRequest(http.MethodPost, ShowPath) +} + +func (c *Client) Quit() error { + return c.makeSimpleRequest(http.MethodPost, QuitPath) +} + func (c *Client) makeSimpleRequest(method string, path string) error { var resp *http.Response var err error switch method { case http.MethodGet: - resp, err = c.httpC.Get(path) + resp, err = c.httpC.Get("http://supersonic/" + path) case http.MethodPost: - resp, err = c.httpC.Post(path, "application/json", nil) + resp, err = c.httpC.Post("http://supersonic/"+path, "application/json", nil) } if err != nil { + log.Printf("http err: %v\n", err) return err } defer resp.Body.Close()