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
|
/***************************************************************************
* Copyright (C) 2006-2018 by Ilya Kotov *
* forkotov02@ya.ru *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QApplication>
#include <QDir>
#include <QLocalServer>
#include <QLocalSocket>
#include <QSettings>
#include <QIcon>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <qmmp/qmmp.h>
#include <qmmpui/commandlinemanager.h>
#include <qmmpui/mediaplayer.h>
#include <qmmpui/playlistparser.h>
#include <qmmpui/uihelper.h>
#include <qmmpui/uiloader.h>
#include <qmmpui/qmmpuisettings.h>
#include "qmmpstarter.h"
#include "builtincommandlineoption.h"
#ifdef Q_OS_WIN
#include <sstream>
#include <QMessageBox>
#else
#include <sys/stat.h>
#endif
#ifdef Q_OS_WIN
#define UDS_PATH QString("qmmp")
#else
#define UDS_PATH QString("/tmp/qmmp.sock.%1").arg(getuid()).toLatin1().constData()
#endif
using namespace std;
QMMPStarter::QMMPStarter() : QObject()
{
m_player = nullptr;
m_core = nullptr;
m_ui = nullptr;
m_finished = false;
m_exit_code = EXIT_SUCCESS;
#ifndef QT_NO_SESSIONMANAGER
connect(qApp, SIGNAL(commitDataRequest(QSessionManager&)), SLOT(commitData(QSessionManager&)), Qt::DirectConnection);
#endif
#ifdef Q_OS_WIN
m_named_mutex = 0;
#endif
m_option_manager = new BuiltinCommandLineOption(this);
QStringList tmp = qApp->arguments().mid(1);
argString = tmp.join("|||");
QHash <QString, QStringList> commands = m_option_manager->splitArgs(tmp);
if(commands.keys().contains("--help") || commands.keys().contains("-h"))
{
printUsage();
m_finished = true;
return;
}
if(commands.keys().contains("--version") || commands.keys().contains("-v"))
{
printVersion();
m_finished = true;
return;
}
if(commands.keys().contains("--ui-list"))
{
printUserInterfaces();
m_finished = true;
return;
}
if(commands.keys().contains("--ui"))
{
QStringList args = commands.value("--ui");
if(args.size() == 1)
UiLoader::select(args.first());
}
if(!commands.isEmpty())
{
foreach(QString key, commands.keys())
{
CommandLineHandler::OptionFlags flags;
if(!m_option_manager->identify(key) &&
!CommandLineManager::hasOption(key, &flags) &&
key != "--no-start" &&
key != "--ui")
{
cout << qPrintable(tr("Unknown command")) << endl;
m_exit_code = EXIT_FAILURE;
m_finished = true;
return;
}
if(flags & CommandLineHandler::NO_START)
{
m_exit_code = EXIT_SUCCESS;
m_finished = true;
//show dialog with command line documentation under ms windows
#ifdef Q_OS_WIN
stringstream tmp_stream;
tmp_stream.copyfmt(cout);
streambuf* old_stream = cout.rdbuf(tmp_stream.rdbuf());
#endif
cout << qPrintable(CommandLineManager::executeCommand(key, commands.value(key)).trimmed()) << endl;
#ifdef Q_OS_WIN
string text = tmp_stream.str();
QMessageBox::information(0, tr("Command Line Help"), QString::fromLocal8Bit(text.c_str()));
cout.rdbuf(old_stream); //restore old stream buffer
#endif
return;
}
}
}
m_server = new QLocalServer(this);
m_socket = new QLocalSocket(this);
bool noStart = commands.keys().contains("--no-start") || commands.keys().contains("--quit");
#ifdef Q_OS_WIN
//Windows IPC implementation (named mutex and named pipe)
m_named_mutex = CreateMutexA(NULL, TRUE, "QMMP-403cd318-cc7b-4622-8dfd-df18d1e70057");
if(GetLastError() == NO_ERROR && !noStart)
{
m_server->listen (UDS_PATH);
startPlayer();
}
else
{
m_socket->connectToServer(UDS_PATH); //connecting
m_socket->waitForConnected();
if(!m_socket->isValid()) //invalid connection
{
qWarning("QMMPStarter: unable to connect to server");
m_exit_code = EXIT_FAILURE;
m_finished = true;
return;
}
writeCommand();
}
#else
if(!noStart && m_server->listen (UDS_PATH)) //trying to create server
{
#ifndef Q_OS_WIN
chmod(UDS_PATH, S_IRUSR | S_IWUSR);
#endif
startPlayer();
}
else if(QFile::exists(UDS_PATH))
{
m_socket->connectToServer(UDS_PATH); //connecting
m_socket->waitForConnected();
if(!m_socket->isValid()) //invalid connection
{
if(!QLocalServer::removeServer(UDS_PATH))
{
qWarning("QMMPStarter: unable to remove invalid socket file");
m_exit_code = EXIT_FAILURE;
m_finished = true;
return;
}
qWarning("QMMPStarter: removed invalid socket file");
if(noStart)
{
m_exit_code = EXIT_FAILURE;
m_finished = true;
return;
}
else if(m_server->listen (UDS_PATH))
{
#ifndef Q_OS_WIN
chmod(UDS_PATH, S_IRUSR | S_IWUSR);
#endif
startPlayer();
}
else
{
qWarning("QMMPStarter: server error: %s", qPrintable(m_server->errorString()));
m_exit_code = EXIT_FAILURE;
m_finished = true;
return;
}
}
else
writeCommand();
}
else
m_finished = true;
#endif
}
QMMPStarter::~QMMPStarter()
{
if (m_ui)
delete m_ui;
#ifdef Q_OS_WIN
if(m_named_mutex)
ReleaseMutex(m_named_mutex);
#endif
}
bool QMMPStarter::isFinished() const
{
return m_finished;
}
int QMMPStarter::exitCode() const
{
return m_exit_code;
}
void QMMPStarter::startPlayer()
{
connect(m_server, SIGNAL(newConnection()), SLOT(readCommand()));
QStringList args = argString.split("|||", QString::SkipEmptyParts);
#ifdef Q_OS_WIN
QIcon::setThemeSearchPaths(QStringList() << qApp->applicationDirPath() + "/themes/");
QIcon::setThemeName("oxygen");
#else
//add extra theme path;
QStringList theme_paths = QIcon::themeSearchPaths();
QString share_path = qgetenv("XDG_DATA_HOME");
if(share_path.isEmpty())
share_path = QDir::homePath() + "/.local/share";
theme_paths << share_path + "/icons";
theme_paths.removeDuplicates();
QIcon::setThemeSearchPaths(theme_paths);
#endif
//prepare libqmmp and libqmmpui libraries for usage
m_player = new MediaPlayer(this);
m_core = SoundCore::instance();
//additional featuries
new UiHelper(this);
//interface
UiFactory *factory = UiLoader::selected();
if(factory)
m_ui = factory->create();
else
{
qWarning("QMMPStarter: no user interface found");
m_finished = true;
m_exit_code = EXIT_FAILURE;
return;
}
connect(qApp, SIGNAL(aboutToQuit()), SLOT(savePosition()));
processCommandArgs(args, QDir::currentPath());
if(args.isEmpty())
{
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
settings.beginGroup("General");
if(settings.value("resume_playback", false).toBool())
{
qint64 pos = settings.value("resume_playback_time").toLongLong();
m_player->play(pos);
}
}
}
void QMMPStarter::savePosition()
{
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
settings.beginGroup("General");
settings.setValue("resume_playback",m_core->state() == Qmmp::Playing &&
QmmpUiSettings::instance()->resumeOnStartup());
settings.setValue("resume_playback_time", m_core->duration() > 0 ? m_core->elapsed() : 0);
settings.endGroup();
m_core->stop();
}
void QMMPStarter::commitData(QSessionManager &manager)
{
if(UiHelper::instance())
UiHelper::instance()->exit();
#ifndef QT_NO_SESSIONMANAGER
manager.release();
#endif
}
void QMMPStarter::writeCommand()
{
QString workingDir = QDir::currentPath() + "|||";
QByteArray barray;
barray.append(workingDir.toUtf8 ());
barray.append(argString.isEmpty() ? "--show-mw" : argString.toUtf8 ());
while(!barray.isEmpty())
{
qint64 size = m_socket->write(barray);
barray.remove(0, size);
}
m_socket->flush();
//reading answer
while(m_socket->waitForReadyRead(1500))
cout << m_socket->readAll().data();
#ifndef Q_OS_WIN
if (argString.isEmpty())
printUsage();
#endif
m_finished = true;
}
void QMMPStarter::readCommand()
{
QLocalSocket *socket = m_server->nextPendingConnection();
socket->waitForReadyRead();
QByteArray inputArray = socket->readAll();
if(inputArray.isEmpty())
{
socket->deleteLater();
return;
}
QStringList slist = QString::fromUtf8(inputArray.data()).split("|||",QString::SkipEmptyParts);
QString cwd = slist.takeAt(0);
QString out = processCommandArgs(slist, cwd);
if(!out.isEmpty())
{
//writing answer
socket->write(out.toLocal8Bit());
while(socket->waitForBytesWritten())
socket->flush();
}
socket->deleteLater();
}
QString QMMPStarter::processCommandArgs(const QStringList &slist, const QString& cwd)
{
if(slist.isEmpty())
return QString();
QStringList paths;
foreach(QString arg, slist) //detect file/directory paths
{
if(arg.startsWith("-"))
break;
paths.append(arg);
}
if(!paths.isEmpty())
{
return m_option_manager->executeCommand(QString(), paths, cwd); //add paths only
}
QHash<QString, QStringList> commands = m_option_manager->splitArgs(slist);
if(commands.isEmpty())
return QString();
QString out;
foreach(QString key, commands.keys())
{
if(key == "--no-start" || key == "--ui")
continue;
if (CommandLineManager::hasOption(key))
return CommandLineManager::executeCommand(key, commands.value(key));
else if (m_option_manager->identify(key))
out += m_option_manager->executeCommand(key, commands.value(key), cwd);
else
return QString();
}
return out;
}
void QMMPStarter::printUsage()
{
//show dialog with command line documentation under ms windows
#ifdef Q_OS_WIN
stringstream tmp_stream;
tmp_stream.copyfmt(cout);
streambuf* old_stream = cout.rdbuf(tmp_stream.rdbuf());
#endif
cout << qPrintable(tr("Usage: qmmp [options] [files]")) << endl;
cout << qPrintable(tr("Options:")) << endl;
cout << "--------" << endl;
foreach (QString line, m_option_manager->helpString())
cout << qPrintable(CommandLineManager::formatHelpString(line) ) << endl;
CommandLineManager::printUsage();
QStringList extraHelp;
extraHelp << QString("--ui <name>") + "||" + tr("Start qmmp with the specified user interface");
extraHelp << QString("--ui-list") + "||" + tr("List all available user interfaces");
extraHelp << QString("--no-start") + "||" + tr("Don't start the application");
extraHelp << QString("-h, --help") + "||" + tr("Display this text and exit");
extraHelp << QString("-v, --version") + "||" + tr("Print version number and exit");
extraHelp << "";
extraHelp << tr("Home page: %1").arg("http://qmmp.ylsoftware.com");
extraHelp << tr("Development page: %1").arg("https://sourceforge.net/p/qmmp-dev");
extraHelp << tr("Bug tracker: %1").arg("https://sourceforge.net/p/qmmp-dev/tickets");
foreach (QString line, extraHelp)
cout << qPrintable(CommandLineManager::formatHelpString(line)) << endl;
#ifdef Q_OS_WIN
string text = tmp_stream.str();
QMessageBox::information(0, tr("Command Line Help"), QString::fromLocal8Bit(text.c_str()));
cout.rdbuf(old_stream); //restore old stream buffer
#endif
}
void QMMPStarter::printVersion()
{
//show dialog with qmmp version under ms windows
#ifdef Q_OS_WIN
stringstream tmp_stream;
tmp_stream.copyfmt(cout);
streambuf* old_stream = cout.rdbuf(tmp_stream.rdbuf());
#endif
cout << qPrintable(tr("QMMP version: %1").arg(Qmmp::strVersion())) << endl;
cout << qPrintable(tr("Compiled with Qt version: %1").arg(QT_VERSION_STR)) << endl;
cout << qPrintable(tr("Using Qt version: %1").arg(qVersion())) << endl;
#ifdef Q_OS_WIN
string text = tmp_stream.str();
QMessageBox::information(0, tr("Qmmp Version"), QString::fromLocal8Bit(text.c_str()));
cout.rdbuf(old_stream); //restore old stream buffer
#endif
}
void QMMPStarter::printUserInterfaces()
{
//show dialog with qmmp version under ms windows
#ifdef Q_OS_WIN
stringstream tmp_stream;
tmp_stream.copyfmt(cout);
streambuf* old_stream = cout.rdbuf(tmp_stream.rdbuf());
#endif
foreach (QString name, UiLoader::names())
cout << qPrintable(name) << endl;
#ifdef Q_OS_WIN
string text = tmp_stream.str();
QMessageBox::information(0, tr("User Interfaces"), QString::fromLocal8Bit(text.c_str()));
cout.rdbuf(old_stream); //restore old stream buffer
#endif
}
|