summaryrefslogtreecommitdiff
path: root/main.go
blob: 658f76b942fd920ae62b561b5c4258c40694b3ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main

import (
	"context"
	"image/color"
	"log"
	"net"
	"os"
	"sync"

	"gioui.org/app"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/text"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"

	"xengineering.eu/soundbox-go/soundbox"
)

func main() {
	config, err := loadConfig()
	if err != nil {
		log.Fatal(err)
	}

	ui := NewUi(config)
	go func() {
		err := ui.Run()
		if err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

type State struct {
	sync.Mutex
	Config          Config
	Theme           *material.Theme
	IsPlaying       bool
	Title           string
	SourceSelector  widget.Enum
	PlayPauseButton widget.Clickable
	PlayerContext   context.Context
	PlayerCancel    context.CancelFunc
}

type Ui struct {
	Window *app.Window
	State  State
}

func NewUi(config Config) *Ui {
	ui := Ui{}

	ui.State.Config = config

	ui.State.IsPlaying = false

	ui.State.Theme = material.NewTheme()

	ui.Window = new(app.Window)
	ui.Window.Option(app.Title("soundbox"))

	ui.State.Title = "soundbox"

	return &ui
}

func (ui *Ui) Cleanup() {
	ui.State.Lock()
	defer ui.State.Unlock()

	if ui.State.PlayerCancel != nil {
		done := ui.State.PlayerContext.Done()
		ui.State.PlayerCancel()
		<-done
	}
}

func (ui *Ui) Run() error {
	defer ui.Cleanup()
	var ops op.Ops
	for {
		switch e := ui.Window.Event().(type) {
		case app.DestroyEvent:
			return e.Err
		case app.FrameEvent:
			gtx := app.NewContext(&ops, e)
			ui.HandleInputs(gtx)
			ui.Layout(gtx)
			e.Frame(gtx.Ops)
		}
	}
}

func (ui *Ui) HandleInputs(gtx layout.Context) {
	ui.State.Lock()
	defer ui.State.Unlock()

	if ui.State.PlayPauseButton.Clicked(gtx) {
		if ui.State.IsPlaying {
			ui.State.PlayerCancel()
		} else {
			if ui.State.SourceSelector.Value == "" {
				log.Println("A URL has to be selected.")
			} else {
				ui.State.PlayerContext, ui.State.PlayerCancel = context.WithCancel(context.Background())
				var targets []net.HardwareAddr
				for _, entry := range ui.State.Config.Soundboxes {
					if entry.Enabled {
						targets = append(targets, net.HardwareAddr(entry.Mac))
					}
				}
				go play(ui.State.PlayerContext, ui.State.SourceSelector.Value, targets, ui)
			}
		}
	}
}

func (ui *Ui) Layout(gtx layout.Context) layout.Dimensions {
	ui.State.Lock()
	defer ui.State.Unlock()

	inset := layout.UniformInset(unit.Dp(10))

	flex := layout.Flex{
		Axis:    layout.Vertical,
		Spacing: layout.SpaceEnd,
	}

	h1 := material.H1(ui.State.Theme, ui.State.Title)
	h1.Color = color.NRGBA{R: 88, G: 88, B: 88, A: 255}
	h1.Alignment = text.Middle

	var playPauseButtonText string
	if ui.State.IsPlaying {
		playPauseButtonText = "Stop"
	} else {
		playPauseButtonText = "Play"
	}
	button := material.Button(ui.State.Theme, &ui.State.PlayPauseButton, playPauseButtonText)

	entries := []layout.FlexChild{
		layout.Rigid(h1.Layout),
		layout.Rigid(layout.Spacer{Height: unit.Dp(25)}.Layout),
	}

	entries = append(entries, layout.Rigid(material.RadioButton(
		ui.State.Theme, &ui.State.SourceSelector,
		"PipeWire", "This device (experimental)").Layout))
	for _, url := range ui.State.Config.URLs {
		entries = append(entries, layout.Rigid(material.RadioButton(
			ui.State.Theme, &ui.State.SourceSelector,
			url.Url, url.Name).Layout))
	}

	entries = append(entries, layout.Rigid(layout.Spacer{Height: unit.Dp(25)}.Layout))
	entries = append(entries, layout.Rigid(button.Layout))

	return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
		return flex.Layout(gtx, entries...)
	})
}

func play(ctx context.Context, url string, targets []net.HardwareAddr, ui *Ui) {
	setPlayingState := func(isPlaying bool) {
		ui.State.Lock()
		defer ui.Window.Invalidate()
		defer ui.State.Unlock()
		ui.State.IsPlaying = isPlaying
	}

	setPlayingState(true)
	defer setPlayingState(false)

	var err error
	if url == "PipeWire" {
		err = soundbox.StreamPipewireContext(ctx, targets)
	} else {
		err = soundbox.StreamURLContext(ctx, url, targets)
	}
	if err != nil {
		log.Println(err)
	}
}