if AllowMultiInstance, don't have second instance try to start IPC server

This commit is contained in:
Drew Weymouth
2024-06-12 18:39:07 -07:00
parent bfb869fadc
commit 8570bcd25a
+31 -35
View File
@@ -91,17 +91,17 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
a.bgrndCtx, a.cancel = context.WithCancel(context.Background()) a.bgrndCtx, a.cancel = context.WithCancel(context.Background())
a.readConfig() a.readConfig()
if haveCLI := HaveCommandLineOptions(); haveCLI || !a.Config.Application.AllowMultiInstance { cli, _ := ipc.Connect()
connected, err := a.checkCLIFlagsAndSendIPCMsg() if HaveCommandLineOptions() {
if err != nil { if err := a.checkFlagsAndSendIPCMsg(cli); err != nil {
if haveCLI { // we were supposed to control another instance and couldn't
// we were supposed to control another instance and couldn't log.Fatalf("error sending IPC message: %s", err.Error())
log.Fatalf("error sending IPC message: %s", err.Error())
}
}
if connected /* we reached the other instance at all */ {
return nil, ErrAnotherInstance
} }
return nil, ErrAnotherInstance
} else if cli != nil && !a.Config.Application.AllowMultiInstance {
log.Println("Another instance is running. Reactivating it...")
cli.Show()
return nil, ErrAnotherInstance
} }
log.Printf("Starting %s...", appName) log.Printf("Starting %s...", appName)
@@ -127,17 +127,17 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
_, _ = a.ImageManager.GetCoverThumbnail(coverID) _, _ = a.ImageManager.GetCoverThumbnail(coverID)
}) })
// Start IPC server // Start IPC server if another not already running in a different instance
if !a.lastWrittenCfg.Application.AllowMultiInstance { if cli == nil {
ipc.DestroyConn() // cleanup socket possibly orphaned by crashed process ipc.DestroyConn() // cleanup socket possibly orphaned by crashed process
} listener, err := ipc.Listen()
listener, err := ipc.Listen() if err == nil {
if err == nil { a.ipcServer = ipc.NewServer(a.PlaybackManager, a.callOnReactivate,
a.ipcServer = ipc.NewServer(a.PlaybackManager, a.callOnReactivate, func() { _ = a.callOnExit() })
func() { _ = a.callOnExit() }) go a.ipcServer.Serve(listener)
go a.ipcServer.Serve(listener) } else {
} else { log.Printf("error starting IPC server: %s", err.Error())
log.Printf("error starting IPC server: %s", err.Error()) }
} }
// OS media center integrations // OS media center integrations
@@ -379,32 +379,28 @@ func (a *App) SaveConfigFile() {
a.lastWrittenCfg = *a.Config a.lastWrittenCfg = *a.Config
} }
func (a *App) checkCLIFlagsAndSendIPCMsg() (connected bool, err error) { func (a *App) checkFlagsAndSendIPCMsg(cli *ipc.Client) error {
cli, err := ipc.Connect() if cli == nil {
if err != nil { return errors.New("no IPC connection")
return false, err
} }
switch { switch {
case *FlagPlay: case *FlagPlay:
err = cli.Play() return cli.Play()
case *FlagPause: case *FlagPause:
err = cli.Pause() return cli.Pause()
case *FlagPlayPause: case *FlagPlayPause:
err = cli.PlayPause() return cli.PlayPause()
case *FlagPrevious: case *FlagPrevious:
err = cli.SeekBackOrPrevious() return cli.SeekBackOrPrevious()
case *FlagNext: case *FlagNext:
err = cli.SeekNext() return cli.SeekNext()
case VolumeCLIArg >= 0: case VolumeCLIArg >= 0:
err = cli.SetVolume(VolumeCLIArg) return cli.SetVolume(VolumeCLIArg)
case SeekToCLIArg >= 0: case SeekToCLIArg >= 0:
err = cli.SeekSeconds(SeekToCLIArg) return cli.SeekSeconds(SeekToCLIArg)
default: default:
log.Println("Another instance is running. Reactivating it...") return nil
err = cli.Show()
} }
return true, err
} }
func (a *App) configFilePath() string { func (a *App) configFilePath() string {