Handle incoming IPC commands in the "main" window.

This commit is contained in:
LoRd_MuldeR 2014-01-27 21:54:24 +01:00
parent 3cb8f41b0e
commit 8d68357907
6 changed files with 120 additions and 76 deletions

View File

@ -257,6 +257,9 @@ bool IPC::pushCommand(const int &command, const QStringList *args)
bool IPC::popCommand(int &command, QStringList &args)
{
command = -1;
args.clear();
if(m_initialized < 0)
{
qWarning("Error: IPC not initialized yet!");
@ -276,7 +279,6 @@ bool IPC::popCommand(int &command, QStringList &args)
}
bool success = true;
args.clear();
try
{
@ -316,6 +318,12 @@ bool IPC::popCommand(int &command, QStringList &args)
bool IPC::sendAsync(const int &command, const QStringList &args, const int timeout)
{
if(m_initialized < 0)
{
qWarning("Error: IPC not initialized yet!");
return false;
}
IPCSendThread sendThread(this, command, args);
sendThread.start();
@ -330,12 +338,18 @@ bool IPC::sendAsync(const int &command, const QStringList &args, const int timeo
return sendThread.result();
}
void IPC::startListening(void)
bool IPC::startListening(void)
{
if(m_initialized < 0)
{
qWarning("Error: IPC not initialized yet!");
return false;
}
if(!m_recvThread)
{
m_recvThread = new IPCReceiveThread(this);
connect(m_recvThread, SIGNAL(receivedStr(QString)), this, SIGNAL(receivedStr(QString)), Qt::QueuedConnection);
connect(m_recvThread, SIGNAL(receivedCommand(int,QStringList)), this, SIGNAL(receivedCommand(int,QStringList)), Qt::QueuedConnection);
}
if(!m_recvThread->isRunning())
@ -347,10 +361,17 @@ void IPC::startListening(void)
qWarning("Receive thread was already running!");
}
return true;
}
void IPC::stopListening(void)
bool IPC::stopListening(void)
{
if(m_initialized < 0)
{
qWarning("Error: IPC not initialized yet!");
return false;
}
if(m_recvThread && m_recvThread->isRunning())
{
m_recvThread->stop();
@ -367,4 +388,6 @@ void IPC::stopListening(void)
{
qWarning("Receive thread was not running!");
}
return true;
}

View File

@ -46,13 +46,15 @@ public:
bool initialize(bool &firstInstance);
bool sendAsync(const int &command, const QStringList &args, const int timeout = 5000);
inline bool isInitialized(void) { return (m_initialized >= 0); }
public slots:
void startListening(void);
void stopListening(void);
bool startListening(void);
bool stopListening(void);
signals:
void receivedStr(const QString &str);
void receivedCommand(const int &command, const QStringList &args);
protected:
bool popCommand(int &command, QStringList &args);

View File

@ -82,6 +82,7 @@ static int x264_main(int argc, char* argv[])
{
if(!firstInstance)
{
qDebug("This is *not* the fist instance -> sending all CLI commands to first instance!");
handleMultipleInstances(arguments, ipc);
X264_DELETE(ipc);
return 0;
@ -114,7 +115,7 @@ static int x264_main(int argc, char* argv[])
}
//Create Main Window
MainWindow *mainWin = new MainWindow(&cpuFeatures);
MainWindow *mainWin = new MainWindow(&cpuFeatures, ipc);
mainWin->show();
//Run application
@ -137,15 +138,18 @@ void handleMultipleInstances(QStringList args, IPC *ipc)
{
bool commandSent = false;
//Skip the program file name
args.takeFirst();
//Process all command-line arguments
while(!args.isEmpty())
{
const QString current = args.takeFirst();
if(X264_STRCMP(current, "--add") || X264_STRCMP(current, "--add-file"))
{
commandSent = true;
if(!args.isEmpty())
{
commandSent = true;
if(!ipc->sendAsync(IPC::IPC_OPCODE_ADD_FILE, QStringList() << args.takeFirst()))
{
break;
@ -158,10 +162,15 @@ void handleMultipleInstances(QStringList args, IPC *ipc)
}
else if(X264_STRCMP(current, "--add-job"))
{
commandSent = true;
if(args.size() >= 3)
{
commandSent = true;
if(!ipc->sendAsync(IPC::IPC_OPCODE_ADD_JOB, QStringList() << args.takeFirst() << args.takeFirst() << args.takeFirst()))
QStringList lst;
for(int i = 0; i < 3; i++)
{
lst << args.takeFirst();
}
if(!ipc->sendAsync(IPC::IPC_OPCODE_ADD_JOB, lst))
{
break;
}

View File

@ -24,9 +24,9 @@
#endif
#define VER_X264_MAJOR 2
#define VER_X264_MINOR 2
#define VER_X264_PATCH 9
#define VER_X264_BUILD 727
#define VER_X264_MINOR 3
#define VER_X264_PATCH 0
#define VER_X264_BUILD 730
#define VER_X264_MINIMUM_REV 2363
#define VER_X264_CURRENT_API 140

View File

@ -23,6 +23,7 @@
#include "uic_win_main.h"
#include "global.h"
#include "ipc.h"
#include "model_status.h"
#include "model_jobList.h"
#include "model_options.h"
@ -72,9 +73,10 @@ const char *tpl_last = "<LAST_USED>";
/*
* Constructor
*/
MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures)
MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures, IPC *ipc)
:
m_cpuFeatures(cpuFeatures),
m_ipc(ipc),
m_appDir(QApplication::applicationDirPath()),
m_options(NULL),
m_jobList(NULL),
@ -108,10 +110,6 @@ MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures)
m_options = new OptionsModel();
OptionsModel::loadTemplate(m_options, QString::fromLatin1(tpl_last));
//Create IPC thread object
//m_ipcThread = new IPCThread();
//connect(m_ipcThread, SIGNAL(instanceCreated(unsigned int)), this, SLOT(instanceCreated(unsigned int)), Qt::QueuedConnection);
//Freeze minimum size
setMinimumSize(size());
ui->splitter->setSizes(QList<int>() << 16 << 196);
@ -224,7 +222,6 @@ MainWindow::~MainWindow(void)
}
*/
//X264_DELETE(m_ipcThread);
X264_DELETE(m_preferences);
X264_DELETE(m_recentlyUsed);
VapourSynthCheckThread::unload();
@ -718,24 +715,12 @@ void MainWindow::init(void)
updateLabelPos();
//Check for a running instance
/*
bool firstInstance = false;
if(m_ipcThread->initialize(&firstInstance))
//Create the IPC listener thread
if(m_ipc->isInitialized())
{
m_ipcThread->start();
if(!firstInstance)
{
if(!m_ipcThread->wait(5000))
{
QMessageBox::warning(this, tr("Not Responding"), tr("<nobr>Another instance of this application is already running, but did not respond in time.<br>If the problem persists, please kill the running instance from the task manager!</nobr>"), tr("Quit"));
m_ipcThread->terminate();
m_ipcThread->wait();
}
INIT_ERROR_EXIT();
}
connect(m_ipc, SIGNAL(receivedCommand(int,QStringList)), this, SLOT(handleCommand(int,QStringList)), Qt::QueuedConnection);
m_ipc->startListening();
}
*/
//Check all binaries
while(!binaries.isEmpty())
@ -953,14 +938,64 @@ void MainWindow::handleDroppedFiles(void)
qDebug("Leave from MainWindow::handleDroppedFiles!");
}
void MainWindow::instanceCreated(unsigned int pid)
void MainWindow::handleCommand(const int &command, const QStringList &args)
{
qDebug("Notification from other instance (PID=0x%X) received!", pid);
x264_blink_window(this, 5, 125);
x264_bring_to_front(this);
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
x264_blink_window(this, 5, 125);
if(true)
{
qDebug("\n---------- IPC ----------");
qDebug("CommandId: %d", command);
for(QStringList::ConstIterator iter = args.constBegin(); iter != args.constEnd(); iter++)
{
qDebug("Arguments: %s", iter->toUtf8().constData());
}
qDebug("---------- IPC ----------\n");
}
switch(command)
{
case IPC::IPC_OPCODE_PING:
qDebug("Received a PING request from another instance!");
x264_blink_window(this, 5, 125);
break;
case IPC::IPC_OPCODE_ADD_FILE:
if(!args.isEmpty())
{
if(QFileInfo(args[0]).exists() && QFileInfo(args[0]).isFile())
{
createJobMultiple(QStringList() << QFileInfo(args[0]).canonicalFilePath());
}
else
{
qWarning("File '%s' not found!", args[0].toUtf8().constData());
}
}
break;
case IPC::IPC_OPCODE_ADD_JOB:
if(args.size() >= 3)
{
if(QFileInfo(args[0]).exists() && QFileInfo(args[0]).isFile())
{
OptionsModel options;
if(!(args[2].isEmpty() || X264_STRCMP(args[2], "-")))
{
if(!OptionsModel::loadTemplate(&options, args[2].trimmed()))
{
qWarning("Template '%s' could not be found -> using defaults!", args[2].trimmed().toUtf8().constData());
}
}
appendJob(args[0], args[1], &options, true);
}
else
{
qWarning("Source file '%s' not found!", args[0].toUtf8().constData());
}
}
break;
default:
throw std::exception("Unknown command received!");
}
}
void MainWindow::checkUpdates(void)
@ -1383,15 +1418,7 @@ void MainWindow::parseCommandLineArgs(void)
{
if(!args.isEmpty())
{
current = args.takeFirst();
if(QFileInfo(current).exists() && QFileInfo(current).isFile())
{
files << QFileInfo(current).canonicalFilePath();
}
else
{
qWarning("File '%s' not found!", current.toUtf8().constData());
}
handleCommand(IPC::IPC_OPCODE_ADD_FILE, QStringList() << args.takeFirst());
}
else
{
@ -1402,25 +1429,12 @@ void MainWindow::parseCommandLineArgs(void)
{
if(args.size() >= 3)
{
const QString fileSrc = args.takeFirst();
const QString fileOut = args.takeFirst();
const QString templId = args.takeFirst();
if(QFileInfo(fileSrc).exists() && QFileInfo(fileSrc).isFile())
QStringList lst;
for(int i = 0; i < 3; i++)
{
OptionsModel options;
if(!(templId.isEmpty() || (templId.compare("-", Qt::CaseInsensitive) == 0)))
{
if(!OptionsModel::loadTemplate(&options, templId.trimmed()))
{
qWarning("Template '%s' could not be found -> using defaults!", templId.trimmed().toUtf8().constData());
}
}
appendJob(fileSrc, fileOut, &options, true);
}
else
{
qWarning("Source file '%s' not found!", fileSrc.toUtf8().constData());
lst << args.takeFirst();
}
handleCommand(IPC::IPC_OPCODE_ADD_JOB, lst);
}
else
{
@ -1437,9 +1451,4 @@ void MainWindow::parseCommandLineArgs(void)
}
}
}
if(files.count() > 0)
{
createJobMultiple(files);
}
}

View File

@ -24,6 +24,7 @@
#include "global.h"
#include <QMainWindow>
class IPC;
class JobListModel;
class OptionsModel;
class QFile;
@ -44,7 +45,7 @@ class MainWindow: public QMainWindow
Q_OBJECT
public:
MainWindow(const x264_cpu_t *const cpuFeatures);
MainWindow(const x264_cpu_t *const cpuFeatures, IPC *ipc);
~MainWindow(void);
protected:
@ -65,7 +66,7 @@ private:
bool m_initialized;
QLabel *m_label;
//IPCThread *m_ipcThread;
IPC *const m_ipc;
JobListModel *m_jobList;
OptionsModel *m_options;
@ -101,7 +102,7 @@ private slots:
void checkUpdates(void);
void handleDroppedFiles(void);
void init(void);
void instanceCreated(unsigned int pid);
void handleCommand(const int &command, const QStringList &args);
void jobSelected(const QModelIndex &current, const QModelIndex &previous);
void jobChangedData(const QModelIndex &top, const QModelIndex &bottom);
void jobLogExtended(const QModelIndex & parent, int start, int end);