forked from spezifisch/stmps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_handlers.go
244 lines (211 loc) · 5.63 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
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2023 The STMPS Authors
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"log"
"time"
"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("")
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 'c':
ui.logger.Printf("info: starting server scan")
ui.scanning = true
if err := ui.connection.StartScan(); err != nil {
ui.logger.PrintError("startScan:", err)
return event
}
go func() {
for {
if pl, err := ui.player.IsPlaying(); err != nil || pl {
return
}
if ss, err := ui.connection.ScanStatus(); err != nil {
return
} else {
ui.scanning = ss.Scanning
}
item, err := ui.player.GetPlayingTrack()
if err != nil {
item = mpvplayer.QueueItem{}
}
ui.app.QueueUpdateDraw(func() {
txt := formatPlayerStatus(ui.scanning, ui.player.GetVolume(), int64(ui.player.GetTimePos()), int64(item.Duration))
ui.playerStatus.SetText(txt)
})
// If we're not scanning, this poller is not needed
if !ui.scanning {
return
}
// We could do this with a timer channel, but this is simpler
time.Sleep(1 * time.Second)
}
}()
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)
if name == PageSearch {
ui.searchPage.aproposFocus()
}
}
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) {
ui.addRandomSongsToQueue(id)
ui.queuePage.UpdateQueue()
}
func (ui *Ui) addRandomSongsToQueue(id string) {
entities, err := ui.connection.GetRandomSongs(id)
if err != nil {
ui.logger.Printf("addRandomSongsToQueue %s", err.Error())
}
for _, e := range entities {
ui.addSongToQueue(e)
}
}
// make sure to call ui.QueuePage.UpdateQueue() after this
func (ui *Ui) addSongToQueue(entity subsonic.Entity) {
uri := ui.connection.GetPlayUrl(entity)
album, err := ui.connection.GetAlbum(entity.Parent)
albumName := ""
if err != nil {
ui.logger.PrintError("addSongToQueue", err)
} else {
switch {
case album.Name != "":
albumName = album.Name
case album.Title != "":
albumName = album.Title
case album.Album != "":
albumName = album.Album
}
}
// Populate the genre, by hook or crook
genre := entity.Genre
if genre == "" {
genre = album.Genre
}
if genre == "" && len(album.Genres) > 0 {
genre = album.Genres[0].Name
}
queueItem := &mpvplayer.QueueItem{
Id: entity.Id,
Uri: uri,
Title: entity.GetSongTitle(),
Artist: entity.Artist,
Duration: entity.Duration,
Album: albumName,
TrackNumber: entity.Track,
CoverArtId: entity.CoverArtId,
DiscNumber: entity.DiscNumber,
Year: entity.Year,
Genre: genre,
}
ui.player.AddToQueue(queueItem)
}
func (ui *Ui) makeSongHandler(entity subsonic.Entity) func() {
return func() {
uri := ui.connection.GetPlayUrl(entity)
if err := ui.player.PlayUri(uri, entity.CoverArtId, entity); err != nil {
ui.logger.PrintError("SongHandler Play", err)
return
}
ui.queuePage.UpdateQueue()
}
}