/
squeezelitegui.cpp
515 lines (445 loc) · 19.1 KB
/
squeezelitegui.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include <QtGui/QGuiApplication>
#include <QQmlContext>
#include <QSettings>
#include <QNetworkInterface>
#include <QList>
#include <QListIterator>
#include "squeezelitegui.h"
#ifdef SQUEEZELITEGUI_DEBUG
#define DEBUGF(...) qDebug() << this->objectName() << Q_FUNC_INFO << __VA_ARGS__;
#else
#define DEBUGF(...)
#endif
squeezeLiteGui::squeezeLiteGui(QWindow *parent) :
QQuickView(parent)
{
setObjectName("squeezeLiteGui");
m_interfaceState = INTERFACE_UNINITIALIZED;
}
squeezeLiteGui::~squeezeLiteGui(void)
{
m_cliThread->exit();
cli->deleteLater();
m_cliThread->deleteLater();
m_playerInfoThread->exit();
m_playerInfo->deleteLater();
m_playerInfoThread->deleteLater();
m_interfaceState = INTERFACE_CLOSED;
QHashIterator<QString,ControlListModel*> i(controlHierarchy);
while(i.hasNext())
i.value()->deleteLater();
Close();
}
void squeezeLiteGui::Init(void)
{
qmlRegisterType<ControlListModel>("net.galtfamily.controlListModel",1,0,"ControlListModel");
// qmlRegisterType<ControlListItem>("net.galtfamily.controllistitem",1,0,"ControlListItem");
setSource(QUrl::fromLocalFile("qml/squeezelitegui/squeezeliteguimain.qml"));
show();
QSettings *mySettings = new QSettings("squeezelitegui", "squeezelitegui");
if( !mySettings->contains("Version") || mySettings->value("Version")!=DATAVERSION) { // no settings file, so create one
mySettings->setValue("Version", DATAVERSION);
mySettings->setValue("Server/Address","127.0.0.1");
// mySettings->setValue("Server/Address","10.6.67.54");
mySettings->setValue("Server/AudioPort","3483");
mySettings->setValue("Server/CLIPort", "9090");
mySettings->setValue("Server/HttpPort", "9000");
mySettings->setValue("Server/Username", "");
mySettings->setValue("Server/Password", "");
mySettings->setValue("Audio/Device","default");
mySettings->setValue("Player/Name","squeezelite");
mySettings->sync();
}
lmsUsername = mySettings->value("Server/Username","").toString();
lmsPassword = mySettings->value("Server/Password","").toString();
SqueezeBoxServerAddress = mySettings->value("Server/Address","127.0.0.1").toString();
SqueezeBoxServerAudioPort = mySettings->value("Server/AudioPort","3483").toString();
SqueezeBoxServerCLIPort = mySettings->value("Server/CLIPort", "9090").toString();
SqueezeBoxServerHttpPort = mySettings->value("Server/HttpPort", "9000").toString();
AudioDevice = mySettings->value("Audio/Device","").toString();
PlayerName = mySettings->value("Player/Name", "squeezelite").toString();
getplayerMACAddress(); // get the MAC address we are going to use
player = new QProcess(this);
#ifdef Q_OS_LINUX
QString program = "squeezelite";
program += (QString(" -m " + QString( MacAddress ))) + (QString(" -n " + PlayerName)) + (QString(" -o " + AudioDevice)) + " " + SqueezeBoxServerAddress;
#else
QString program = QString('"')+QString("c:\\program files (x86)\\squeezelite\\squeezelite-win")+QString('"');
program += (QString(" -m " + QString( MacAddress ))) + (QString(" -n " + PlayerName)) + (QString(" -o " + AudioDevice)) + " " + SqueezeBoxServerAddress;
#endif
DEBUGF( "player command " << program);
player->start( program );
DEBUGF("ERROR STRING IF ANY" << player->error() << player->errorString() );
// player->waitForStarted(2000);
// // initialize the CLI interface. Make sure that you've set the appropriate server address and port
cli = new SlimCLI(0, SqueezeBoxServerAddress, encodedMacAddress, SqueezeBoxServerCLIPort.toInt());
m_cliThread = new cliThread(cli);
cli->moveToThread(m_cliThread);
connect(cli,SIGNAL(isConnected()), this, SLOT(CliReady()));
connect(cli,SIGNAL(cliError(QString)),this,SLOT(ErrorMessageReceiver(QString)));
m_cliThread->start();
m_interfaceState = INTERFACE_CLI_STARTED;
}
void squeezeLiteGui::Close(void)
{
DEBUGF("");
if(player) {
DEBUGF("CLOSING PLAYER");
player->close();
player->waitForFinished(5000);
player->deleteLater();
}
}
void squeezeLiteGui::ErrorMessageReceiver(QString s)
{
qDebug() << "SQUEEZELITE ERROR MESSAGE:" << s;
}
void squeezeLiteGui::CliReady(void)
{
DEBUGF("cliConnected Slot");
m_playerInfo = new playerInfo(cli,encodedMacAddress);
m_playerInfoThread = new playerInfoThread(m_playerInfo);
m_playerInfo->moveToThread(m_playerInfoThread);
// Error message processing
connect(m_playerInfo,SIGNAL(playerInfoError(QString)),
this,SLOT(ErrorMessageReceiver(QString)));
connect(cli,SIGNAL(cliError(QString)),
this,SLOT(ErrorMessageReceiver(QString)));
// interface command issuing
connect(this,SIGNAL(issueStandardCommand(CliCommand)),
cli,SLOT(SendStandardCommand(CliCommand)));
connect(this,SIGNAL(issueCommand(QByteArray)),
cli,SLOT(SendCommand(QByteArray))); // so device can send messages
connect(m_playerInfo,SIGNAL(PlayerInfoFilled()),
this,SLOT(playerInfoReady()));
connect(m_playerInfo,SIGNAL(PlayerStatus(PlayerState)),
this,SLOT(PlayerStatus(PlayerState)));
m_playerInfoThread->start();
// connect(this,SIGNAL(deviceStatusReady()),this,SLOT(initInterfaceConnections()));
}
void squeezeLiteGui::PlayerStatus(PlayerState s)
{
switch(s){
case UNINITIALIZED:
break;
case INITIALIZED:
emit issueStandardCommand(C_SUBSCRIBE);
emit issueStandardCommand(C_GETSTATUS);
break;
case DATAREADY:
break;
case NEWPLAYLIST:
loadNowPlayingScreen();
break;
case ENDING:
break;
case MAX_PLAYERSTATE_MODES:
break;
}
}
void squeezeLiteGui::playerInfoReady(void)
{
/* the playerInfo has been filled -- but only with information about the player, not playlist
* so establish the interface and allow user interaction. We are now filling the playlist,
* if any, and will load that screen when ready
*/
setupInterfaceConnections();
refreshScreenSettings();
loadHomeScreen();
}
void squeezeLiteGui::setupInterfaceConnections(void)
{
DEBUGF("Initialize Interface Connections");
QQuickItem *v = rootObject();
// interface signals to application
connect(v,SIGNAL(play(int)), this,SLOT(playState(int)));
connect(v,SIGNAL(nextTrack()), this,SLOT(nextTrackClicked()));
connect(v,SIGNAL(prevTrack()), this,SLOT(prevTrackClicked()));
connect(v,SIGNAL(volUp()), this,SLOT(volUp()));
connect(v,SIGNAL(volDown()), this,SLOT(volDown()));
connect(v,SIGNAL(setVolume(int)), this,SLOT(setVolume(int)));
connect(v,SIGNAL(controlClicked(QString)), this,SLOT(controlViewClicked(QString)));
connect(v,SIGNAL(shuffle(int)), this,SLOT(shuffleState(int)));
connect(v,SIGNAL(repeat(int)), this,SLOT(repeatState(int)));
// connect(v,SIGNAL(playButtonClicked()),this,SLOT(playPauseToggle()));
// application signals to interface
connect(this,SIGNAL(playlistIndexChange(QVariant)), v, SLOT(setControlViewListIndex(QVariant)));
connect(this,SIGNAL(updateAlbumCover(QVariant)), v,SLOT(updateAlbumCover(QVariant)));
connect(this,SIGNAL(playStatus(QVariant)), v, SLOT(updatePlayMode(QVariant)));
connect(this,SIGNAL(VolumeChange(QVariant)), v,SLOT(setMainVolume(QVariant)));
connect(this,SIGNAL(songDuration(QVariant)), v,SLOT(setSongDuration(QVariant)));
connect(this,SIGNAL(progress(QVariant)), v,SLOT(updateProgress(QVariant)));
connect(m_playerInfo,SIGNAL(PlayingTime(QVariant,QVariant)),
v,SLOT(setupSongTimes(QVariant,QVariant)));
connect(m_playerInfo,SIGNAL(TimeText(QVariant)),v,SLOT(setTimeText(QVariant)));
connect(m_playerInfo,SIGNAL(NewSong()),this,SLOT(NewSong()));
connect(&m_tick,SIGNAL(timeout()),m_playerInfo,SLOT(tick()));
m_tick.start(1000);
/*
* messages from device that need to be connected to slots
void playlistIndexChange(QVariant newidx);
void NewSong(int newPlayListIndex);
void NewPlaylist(void);
void Mute(bool);
void VolumeChange(int);
void ModeChange(QString);
*/
}
void squeezeLiteGui::refreshScreenSettings(void)
{
emit VolumeChange(QVariant(m_playerInfo->GetDeviceVolume()));
emit playStatus(QVariant(m_playerInfo->GetDeviceMode()));
emit shuffleStatus(QVariant(m_playerInfo->GetShuffleMode()));
emit repeatStatus(QVariant(m_playerInfo->GetRepeatMode()));
emit songDuration(QVariant(m_playerInfo->GetTrackDuration()));
emit progress(QVariant(m_playerInfo->GetTrackPlaytime()));
}
void squeezeLiteGui::loadHomeScreen(void)
{
DEBUGF(QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/artists_40x40.png"));
if( !controlHierarchy.contains("Home")) {
ControlListModel *model = new ControlListModel(this);
model->appendRow(new ControlListItem("Music",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/artists_40x40.png"), model));
model->appendRow(new ControlListItem("Internet Radio",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/plugins/cache/icons/radiomusic_40x40.png"), model));
model->appendRow(new ControlListItem("My Apps",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/plugins/AppGallery/html/images/icon_40x40.png"), model));
model->appendRow(new ControlListItem("Favorites",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/favorites_40x40.png"), model));
model->appendRow(new ControlListItem("Extras",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/alarm_40x40.png"), model));
controlHierarchy.insert("Home", model);
rootContext()->setContextProperty("controlListModel", model);
}
else {
rootContext()->setContextProperty("controlListModel", controlHierarchy["Home"]);
}
}
void squeezeLiteGui::loadMusicScreen(void)
{
if( !controlHierarchy.contains("MusicScreen")) {
ControlListModel *model = new ControlListModel(this);
model->appendRow(new ControlListItem("Artists",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/artists_40x40.png"), model));
model->appendRow(new ControlListItem("Albums",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/albums_40x40.png"), model));
model->appendRow(new ControlListItem("Genres",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/genres_40x40.png"), model));
model->appendRow(new ControlListItem("Years",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/years_40x40.png"), model));
model->appendRow(new ControlListItem("New Music",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/newmusic_40x40.png"), model));
model->appendRow(new ControlListItem("Random Mix",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/plugins/RandomPlay/html/images/icon_40x40.png"), model));
model->appendRow(new ControlListItem("Music Folder",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/musicfolder_40x40.png"), model));
model->appendRow(new ControlListItem("Playlists",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/playlists_40x40.png"), model));
model->appendRow(new ControlListItem("Search",
QString("http://")+SqueezeBoxServerAddress+QString(":")+SqueezeBoxServerHttpPort+QString("/html/images/search_40x40.png"), model));
controlHierarchy.insert("MusicScreen", model);
rootContext()->setContextProperty("controlListModel", model);
}
else {
rootContext()->setContextProperty("controlListModel", controlHierarchy["MusicScreen"]);
}
}
void squeezeLiteGui::updateNowPlayingScreen(void)
{
ControlListModel *model;
int rowCounter = 0;
if( controlHierarchy.contains("NowPlaying")) {
model = controlHierarchy.value("NowPlaying");
model->clear();
}
else {
model = new ControlListModel(this);
}
m_playerInfo->LockMutex();
QListIterator<TrackData> i = QListIterator<TrackData>(m_playerInfo->GetCurrentPlaylistNoMutex());
while(i.hasNext()) {
DEBUGF("CAPTURING TRACK" << rowCounter++);
TrackData track = i.next();
QString urlString;
if(track.coverid.isEmpty()) {
urlString = QString("http://%1:%2/%3")
.arg(SqueezeBoxServerAddress)
.arg(SqueezeBoxServerHttpPort)
.arg(QString("html/images/artists_40x40.png"));
}
else {
urlString = QString("http://%1:%2/music/%3/%4")
.arg(SqueezeBoxServerAddress)
.arg(SqueezeBoxServerHttpPort)
.arg(QString(track.coverid))
.arg(QString("cover_40x40"));
}
model->appendRow(new ControlListItem(QString(track.title +" - "+track.artist),urlString,QString(track.song_id)));
}
m_playerInfo->UnlockMutex();
controlHierarchy.insert("NowPlaying", model);
rootContext()->setContextProperty("controlListModel", model);
// rootContext()->setContextProperty("controlListModel", m_playerInfo->GetCurrentPlaylistModel());
}
void squeezeLiteGui::loadNowPlayingScreen(void)
{
DEBUGF("LOAD NOW PLAYING SCREEN");
if( !controlHierarchy.contains("NowPlaying")) {
updateNowPlayingScreen();
}
else {
rootContext()->setContextProperty("controlListModel", controlHierarchy["NowPlaying"]);
}
NewSong();
}
void squeezeLiteGui::controlViewClicked(int idx)
{
}
void squeezeLiteGui::controlViewClicked(QString itemClicked)
{
DEBUGF("control view clicked with name:" << itemClicked);
if(itemClicked=="Music") {
loadMusicScreen();
}
else if (itemClicked=="Home") {
loadHomeScreen();
}
else if(itemClicked=="NowPlaying") {
loadNowPlayingScreen();
}
}
void squeezeLiteGui::ModeChange(QString)
{
}
void squeezeLiteGui::shuffleState(int state)
{
switch(state) {
case SHUFFLE_BY_SONG:
issueStandardCommand(C_SHUFFLE_BY_SONG);
break;
case SHUFFLE_BY_ALBUM:
issueStandardCommand(C_SHUFFLE_BY_PLAYLIST);
break;
case SHUFFLE_OFF:
default:
issueStandardCommand(C_SHUFFLE_OFF);
break;
}
}
void squeezeLiteGui::repeatState(int state)
{
switch(state){
case REPEAT_TRACK:
issueStandardCommand(C_REPEAT_SONG);
break;
case REPEAT_PLAYLIST:
issueStandardCommand(C_REPEAT_PLAYLIST);
break;
default:
case REPEAT_OFF:
issueStandardCommand(C_REPEAT_OFF);
break;
}
}
void squeezeLiteGui::nextTrackClicked(void)
{
issueStandardCommand(C_NEXTTRACK);
}
void squeezeLiteGui::prevTrackClicked(void)
{
issueStandardCommand(C_PREVIOUSTRACK);
}
void squeezeLiteGui::playState(int state)
{
if(state==PLAY) {
issueStandardCommand(C_PLAY);
m_playerInfo->SetDeviceMode(PLAY);
}
else {
issueStandardCommand(C_PAUSE);
m_playerInfo->SetDeviceMode(PAUSE);
}
DEBUGF("DEVICE MODE IS NOW:" << m_playerInfo->GetDeviceMode());
}
void squeezeLiteGui::playPauseToggle(void)
{
if(m_playerInfo->GetDeviceMode()==PLAY) {
issueStandardCommand(C_PAUSE);
m_playerInfo->SetDeviceMode(PAUSE);
}
else {
issueStandardCommand(C_PLAY);
m_playerInfo->SetDeviceMode(PLAY);
}
}
void squeezeLiteGui::volUp(void)
{
}
void squeezeLiteGui::volDown(void)
{
}
void squeezeLiteGui::NewSong()
{
int currentTrackIndex = m_playerInfo->GetPlaylistIndex();
DEBUGF("CONTAINS NOWPLAYING" << controlHierarchy.contains("NowPlaying"));
DEBUGF("current track index" << currentTrackIndex);
DEBUGF("NowPlaying row count =" << controlHierarchy.value("NowPlaying")->rowCount());
if(controlHierarchy.contains("NowPlaying") &&
controlHierarchy.value("NowPlaying")->rowCount() >= currentTrackIndex) {
emit playlistIndexChange(QVariant(currentTrackIndex));
DEBUGF("emit playlistIndexChange to" << currentTrackIndex);
TrackData track = m_playerInfo->GetCurrentPlaylist().at(currentTrackIndex);
QString imageURL;
if(track.coverid.isEmpty()) {
imageURL = QString("http://%1:%2/%3")
.arg(SqueezeBoxServerAddress)
.arg(SqueezeBoxServerHttpPort)
.arg(QString("html/images/artists_300x300.png"));
}
else {
imageURL = QString("http://%1:%2/music/%3/%4")
.arg(SqueezeBoxServerAddress)
.arg(SqueezeBoxServerHttpPort)
.arg(QString(track.coverid))
.arg(QString("cover_300x300"));
}
emit updateAlbumCover(QVariant(imageURL));
}
}
void squeezeLiteGui::NewPlaylist(void)
{
DEBUGF("New Playlist");
if(controlHierarchy.contains("NowPlaying")) {
ControlListModel *model = controlHierarchy.value("NowPlaying");
model->deleteLater();
controlHierarchy.remove("NowPlaying"); // delete current playlist
}
}
void squeezeLiteGui::Mute(bool)
{
}
void squeezeLiteGui::setVolume(int vol)
{
QString cmd = QString("mixer volume %1 \n").arg(vol);
emit issueCommand(cmd.toLatin1());
}
void squeezeLiteGui::getplayerMACAddress(void)
{
DEBUGF("");
MacAddress = QByteArray( "00:00:00:00:00:04" );
QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
QListIterator<QNetworkInterface> i( netList );
QNetworkInterface t;
while( i.hasNext() ) { // note: this grabs the first functional, non-loopback address there is. It may not the be interface on which you really connect to the slimserver
t = i.next();
if( !t.flags().testFlag( QNetworkInterface::IsLoopBack ) &&
t.flags().testFlag( QNetworkInterface::IsUp ) &&
t.flags().testFlag( QNetworkInterface::IsRunning ) ) {
MacAddress = t.hardwareAddress().toLatin1().toLower();
if(!MacAddress.contains("%3A")) // not escape encoded
encodedMacAddress = QString(MacAddress).toLatin1().toPercentEncoding();
return;
}
}
}