forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gui_handlers.go
232 lines (198 loc) · 5.58 KB
/
gui_handlers.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2023 The STMPS Authors
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"log"
"github.com/gdamore/tcell/v2"
"github.com/spezifisch/stmps/mpvplayer"
"github.com/spezifisch/stmps/subsonic"
)
func (ui *Ui) handlePageInput(event *tcell.EventKey) *tcell.EventKey {
// we don't want any of these firing if we're trying to add a new playlist
focused := ui.app.GetFocus()
if ui.playlistPage.IsNewPlaylistInputFocused(focused) || ui.browserPage.IsSearchFocused(focused) || focused == ui.searchPage.searchField || ui.selectPlaylistWidget.visible {
return event
}
switch event.Rune() {
case '1':
ui.ShowPage(PageBrowser)
case '2':
ui.ShowPage(PageQueue)
case '3':
ui.ShowPage(PagePlaylists)
case '4':
ui.ShowPage(PageSearch)
case '5':
ui.ShowPage(PageLog)
case '?':
ui.ShowHelp()
case 'Q':
ui.Quit()
case 'r':
// add random songs to queue
ui.handleAddRandomSongs("", "random")
case 'D':
// clear queue and stop playing
ui.player.ClearQueue()
ui.queuePage.UpdateQueue()
case 'p':
// toggle playing/pause
err := ui.player.Pause()
if err != nil {
ui.logger.PrintError("handlePageInput: Pause", err)
}
case 'P':
// stop playing without changes to queue
ui.logger.Print("key stop")
err := ui.player.Stop()
if err != nil {
ui.logger.PrintError("handlePageInput: Stop", err)
}
case 'X':
// debug stuff
ui.logger.Print("test")
//ui.player.Test()
ui.showMessageBox("foo bar")
case '-':
// volume-
if err := ui.player.AdjustVolume(-5); err != nil {
ui.logger.PrintError("handlePageInput: AdjustVolume-", err)
}
case '+', '=':
// volume+
if err := ui.player.AdjustVolume(5); err != nil {
ui.logger.PrintError("handlePageInput: AdjustVolume+", err)
}
case '.':
// <<
if err := ui.player.Seek(10); err != nil {
ui.logger.PrintError("handlePageInput: Seek+", err)
}
case ',':
// >>
if err := ui.player.Seek(-10); err != nil {
ui.logger.PrintError("handlePageInput: Seek-", err)
}
case '>':
// skip to next track
if err := ui.player.PlayNextTrack(); err != nil {
ui.logger.PrintError("handlePageInput: Next", err)
}
ui.queuePage.UpdateQueue()
case 's':
if err := ui.connection.StartScan(); err != nil {
ui.logger.PrintError("startScan:", err)
}
default:
return event
}
return nil
}
func (ui *Ui) ShowPage(name string) {
ui.pages.SwitchToPage(name)
ui.menuWidget.SetActivePage(name)
_, prim := ui.pages.GetFrontPage()
ui.app.SetFocus(prim)
}
func (ui *Ui) Quit() {
if len(ui.queuePage.queueData.playerQueue) > 0 {
ids := make([]string, len(ui.queuePage.queueData.playerQueue))
for i, it := range ui.queuePage.queueData.playerQueue {
ids[i] = it.Id
}
// stmps always only ever plays the first song in the queue
pos := ui.player.GetTimePos()
if err := ui.connection.SavePlayQueue(ids, ids[0], int(pos)); err != nil {
log.Printf("error stashing play queue: %s", err)
}
} else {
// The only way to purge a saved play queue is to force an error by providing
// bad data. Therefore, we ignore errors.
_ = ui.connection.SavePlayQueue([]string{"XXX"}, "XXX", 0)
}
ui.player.Quit()
ui.app.Stop()
}
func (ui *Ui) handleAddRandomSongs(Id string, randomType string) {
ui.addRandomSongsToQueue(Id, randomType)
ui.queuePage.UpdateQueue()
}
func (ui *Ui) addRandomSongsToQueue(Id string, randomType string) {
response, err := ui.connection.GetRandomSongs(Id, randomType)
if err != nil {
ui.logger.Printf("addRandomSongsToQueue %s", err.Error())
}
switch randomType {
case "random":
for _, e := range response.RandomSongs.Song {
ui.addSongToQueue(&e)
}
case "similar":
for _, e := range response.SimilarSongs.Song {
ui.addSongToQueue(&e)
}
}
}
// make sure to call ui.QueuePage.UpdateQueue() after this
func (ui *Ui) addSongToQueue(entity *subsonic.SubsonicEntity) {
uri := ui.connection.GetPlayUrl(entity)
response, err := ui.connection.GetAlbum(entity.Parent)
album := ""
if err != nil {
ui.logger.PrintError("addSongToQueue", err)
} else {
switch {
case response.Album.Name != "":
album = response.Album.Name
case response.Album.Title != "":
album = response.Album.Title
case response.Album.Album != "":
album = response.Album.Album
}
}
queueItem := &mpvplayer.QueueItem{
Id: entity.Id,
Uri: uri,
Title: entity.GetSongTitle(),
Artist: entity.Artist,
Duration: entity.Duration,
Album: album,
TrackNumber: entity.Track,
CoverArtId: entity.CoverArtId,
DiscNumber: entity.DiscNumber,
}
ui.player.AddToQueue(queueItem)
}
func makeSongHandler(entity *subsonic.SubsonicEntity, ui *Ui, fallbackArtist string) func() {
// make copy of values so this function can be used inside a loop iterating over entities
id := entity.Id
// TODO: Why aren't we doing all of this _inside_ the returned func?
uri := ui.connection.GetPlayUrl(entity)
title := entity.Title
artist := stringOr(entity.Artist, fallbackArtist)
duration := entity.Duration
track := entity.Track
coverArtId := entity.CoverArtId
disc := entity.DiscNumber
response, err := ui.connection.GetAlbum(entity.Parent)
album := ""
if err != nil {
ui.logger.PrintError("makeSongHandler", err)
} else {
switch {
case response.Album.Name != "":
album = response.Album.Name
case response.Album.Title != "":
album = response.Album.Title
case response.Album.Album != "":
album = response.Album.Album
}
}
return func() {
if err := ui.player.PlayUri(id, uri, title, artist, album, duration, track, disc, coverArtId); err != nil {
ui.logger.PrintError("SongHandler Play", err)
return
}
ui.queuePage.UpdateQueue()
}
}