Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Tomkoid <[email protected]>
  • Loading branch information
tomkoid committed May 6, 2024
1 parent f3db8ab commit a393392
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 54 deletions.
27 changes: 27 additions & 0 deletions internal/audio/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package audio

import (
"log"
"os/exec"

"codeberg.org/tomkoid/audiochanger/internal/config"
"codeberg.org/tomkoid/audiochanger/internal/tools"
)

func StopAudio(config *config.Config) {
log.Println("Stopping audio")
// if mpc binary exists on system
if _, err := exec.LookPath("mpc"); err == nil && config.Mpc {
err = tools.RunCommand("mpc", "pause")
if err != nil {
log.Println(err)
}
}

if _, err := exec.LookPath("playerctl"); err == nil && config.PlayerCtl {
err = tools.RunCommand("playerctl", "pause")
if err != nil {
log.Println(err)
}
}
}
3 changes: 0 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"errors"
"fmt"
"log"
"os"

Expand Down Expand Up @@ -59,7 +58,5 @@ func GetConfig() Config {
log.Fatal(err)
}

fmt.Printf("config: %+v\n", config)

return config
}
23 changes: 23 additions & 0 deletions internal/tools/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tools

import (
"os"
"os/signal"
"syscall"

"mrogalski.eu/go/pulseaudio"
)

func HandleCleanup(pulseClient *pulseaudio.Client) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

<-c

println("Shutting down...")

// Run Cleanup
pulseClient.Close()

os.Exit(1)
}
13 changes: 13 additions & 0 deletions internal/tools/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tools

import "os/exec"

func RunCommand(name string, arg ...string) error {
cmd := exec.Command(name, arg...)

if err := cmd.Run(); err != nil {
return err
}

return nil
}
56 changes: 5 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,25 @@ package main
import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"

"mrogalski.eu/go/pulseaudio"

"codeberg.org/tomkoid/audiochanger/internal/audio"
"codeberg.org/tomkoid/audiochanger/internal/config"
"codeberg.org/tomkoid/audiochanger/internal/tools"
)

func runCommand(name string, arg ...string) error {
cmd := exec.Command(name, arg...)

if err := cmd.Run(); err != nil {
return err
}

return nil
}

func stopAudio(config *config.Config) {
log.Println("Stopping audio")
// if mpc binary exists on system
if _, err := exec.LookPath("mpc"); err == nil && config.Mpc {
err = runCommand("mpc", "pause")
if err != nil {
log.Println(err)
}
}

if _, err := exec.LookPath("playerctl"); err == nil && config.PlayerCtl {
err = runCommand("playerctl", "pause")
if err != nil {
log.Println(err)
}
}
}

func handleCleanup(pulseClient *pulseaudio.Client) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

<-c

println("Shutting down...")

// Run Cleanup
pulseClient.Close()

os.Exit(1)
}

func main() {
config := config.GetConfig()

// test()

// Create a PulseAudio context
c, err := pulseaudio.NewClient()
if err != nil {
log.Fatal(err)
}

go handleCleanup(c)
// Handle program termination
go tools.HandleCleanup(c)

defer c.Close()

Expand All @@ -78,7 +33,6 @@ func main() {
// Get the default sink (audio source)
var defaultSink pulseaudio.Output = outputs[activeIndex]

// Store the initial sink name
var initialSinkName string = defaultSink.CardID

updateChan, err := c.Updates()
Expand Down Expand Up @@ -107,7 +61,7 @@ func main() {
)

// stop audio if mpc or playerctl is running
stopAudio(&config)
audio.StopAudio(&config)

initialSinkName = defaultSink.CardID
}
Expand Down

0 comments on commit a393392

Please sign in to comment.