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

View File

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

View File

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

View File

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

View File

@ -23,6 +23,7 @@
#include "uic_win_main.h" #include "uic_win_main.h"
#include "global.h" #include "global.h"
#include "ipc.h"
#include "model_status.h" #include "model_status.h"
#include "model_jobList.h" #include "model_jobList.h"
#include "model_options.h" #include "model_options.h"
@ -72,9 +73,10 @@ const char *tpl_last = "<LAST_USED>";
/* /*
* Constructor * Constructor
*/ */
MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures) MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures, IPC *ipc)
: :
m_cpuFeatures(cpuFeatures), m_cpuFeatures(cpuFeatures),
m_ipc(ipc),
m_appDir(QApplication::applicationDirPath()), m_appDir(QApplication::applicationDirPath()),
m_options(NULL), m_options(NULL),
m_jobList(NULL), m_jobList(NULL),
@ -108,10 +110,6 @@ MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures)
m_options = new OptionsModel(); m_options = new OptionsModel();
OptionsModel::loadTemplate(m_options, QString::fromLatin1(tpl_last)); 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 //Freeze minimum size
setMinimumSize(size()); setMinimumSize(size());
ui->splitter->setSizes(QList<int>() << 16 << 196); 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_preferences);
X264_DELETE(m_recentlyUsed); X264_DELETE(m_recentlyUsed);
VapourSynthCheckThread::unload(); VapourSynthCheckThread::unload();
@ -718,24 +715,12 @@ void MainWindow::init(void)
updateLabelPos(); updateLabelPos();
//Check for a running instance //Create the IPC listener thread
/* if(m_ipc->isInitialized())
bool firstInstance = false;
if(m_ipcThread->initialize(&firstInstance))
{ {
m_ipcThread->start(); connect(m_ipc, SIGNAL(receivedCommand(int,QStringList)), this, SLOT(handleCommand(int,QStringList)), Qt::QueuedConnection);
if(!firstInstance) m_ipc->startListening();
{
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();
}
} }
*/
//Check all binaries //Check all binaries
while(!binaries.isEmpty()) while(!binaries.isEmpty())
@ -953,14 +938,64 @@ void MainWindow::handleDroppedFiles(void)
qDebug("Leave from MainWindow::handleDroppedFiles!"); 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); 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) void MainWindow::checkUpdates(void)
@ -1383,15 +1418,7 @@ void MainWindow::parseCommandLineArgs(void)
{ {
if(!args.isEmpty()) if(!args.isEmpty())
{ {
current = args.takeFirst(); handleCommand(IPC::IPC_OPCODE_ADD_FILE, QStringList() << args.takeFirst());
if(QFileInfo(current).exists() && QFileInfo(current).isFile())
{
files << QFileInfo(current).canonicalFilePath();
}
else
{
qWarning("File '%s' not found!", current.toUtf8().constData());
}
} }
else else
{ {
@ -1402,25 +1429,12 @@ void MainWindow::parseCommandLineArgs(void)
{ {
if(args.size() >= 3) if(args.size() >= 3)
{ {
const QString fileSrc = args.takeFirst(); QStringList lst;
const QString fileOut = args.takeFirst(); for(int i = 0; i < 3; i++)
const QString templId = args.takeFirst();
if(QFileInfo(fileSrc).exists() && QFileInfo(fileSrc).isFile())
{ {
OptionsModel options; lst << args.takeFirst();
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());
} }
handleCommand(IPC::IPC_OPCODE_ADD_JOB, lst);
} }
else 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 "global.h"
#include <QMainWindow> #include <QMainWindow>
class IPC;
class JobListModel; class JobListModel;
class OptionsModel; class OptionsModel;
class QFile; class QFile;
@ -44,7 +45,7 @@ class MainWindow: public QMainWindow
Q_OBJECT Q_OBJECT
public: public:
MainWindow(const x264_cpu_t *const cpuFeatures); MainWindow(const x264_cpu_t *const cpuFeatures, IPC *ipc);
~MainWindow(void); ~MainWindow(void);
protected: protected:
@ -65,7 +66,7 @@ private:
bool m_initialized; bool m_initialized;
QLabel *m_label; QLabel *m_label;
//IPCThread *m_ipcThread; IPC *const m_ipc;
JobListModel *m_jobList; JobListModel *m_jobList;
OptionsModel *m_options; OptionsModel *m_options;
@ -101,7 +102,7 @@ private slots:
void checkUpdates(void); void checkUpdates(void);
void handleDroppedFiles(void); void handleDroppedFiles(void);
void init(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 jobSelected(const QModelIndex &current, const QModelIndex &previous);
void jobChangedData(const QModelIndex &top, const QModelIndex &bottom); void jobChangedData(const QModelIndex &top, const QModelIndex &bottom);
void jobLogExtended(const QModelIndex & parent, int start, int end); void jobLogExtended(const QModelIndex & parent, int start, int end);