Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor process execution #377

Merged
merged 4 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 49 additions & 32 deletions src/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "eventhandlerfactory.h"
#include "globalvariables.h"
#include "joybutton.h"
#include "logger.h"

#if defined(Q_OS_UNIX)
#if defined(WITH_X11)
Expand Down Expand Up @@ -95,38 +96,9 @@ void fakeAbsMouseCoordinates(double springX, double springY, int width, int heig
finaly = (screenMidheight + (springY * destMidHeight) + deskRect.y());
}

// Create the event used by the operating system.
void sendevent(JoyButtonSlot *slot, bool pressed)
{
JoyButtonSlot::JoySlotInputAction device = slot->getSlotMode();

if (device == JoyButtonSlot::JoyKeyboard)
{
EventHandlerFactory::getInstance()->handler()->sendKeyboardEvent(slot, pressed);
} else if (device == JoyButtonSlot::JoyMouseButton)
{
EventHandlerFactory::getInstance()->handler()->sendMouseButtonEvent(slot, pressed);
} else if ((device == JoyButtonSlot::JoyTextEntry) && pressed && !slot->getTextData().isEmpty())
{
EventHandlerFactory::getInstance()->handler()->sendTextEntryEvent(slot->getTextData());
} else if ((device == JoyButtonSlot::JoyExecute) && pressed && !slot->getTextData().isEmpty())
{
QString argumentsString = "";
if (slot->getExtraData().canConvert<QString>())
{
argumentsString = slot->getExtraData().toString();
// QStringList argumentsTempList(PadderCommon::parseArgumentsString(argumentsString));
}

QString launched_command =
QString("%1 %2 %3").arg(detectedScriptExt(slot->getTextData())).arg(slot->getTextData()).arg(argumentsString);
qInfo() << "Executing command: " << launched_command;
bool success = QProcess::startDetached(launched_command);
if (!success)
qWarning() << "Command cannot be executed";
}
}

/**
* @brief detects executor for selected file (for .py files python, for .exe "" etc)
*/
QString detectedScriptExt(QString file)
{
QFileInfo fileinfo(file);
Expand Down Expand Up @@ -165,6 +137,51 @@ QString detectedScriptExt(QString file)
return "";
}

// Create the event used by the operating system.
void sendevent(JoyButtonSlot *slot, bool pressed)
{
JoyButtonSlot::JoySlotInputAction device = slot->getSlotMode();

if (device == JoyButtonSlot::JoyKeyboard)
{
EventHandlerFactory::getInstance()->handler()->sendKeyboardEvent(slot, pressed);
} else if (device == JoyButtonSlot::JoyMouseButton)
{
EventHandlerFactory::getInstance()->handler()->sendMouseButtonEvent(slot, pressed);
} else if ((device == JoyButtonSlot::JoyTextEntry) && pressed && !slot->getTextData().isEmpty())
{
EventHandlerFactory::getInstance()->handler()->sendTextEntryEvent(slot->getTextData());
} else if ((device == JoyButtonSlot::JoyExecute) && pressed && !slot->getTextData().isEmpty())
{
QStringList argumentsTempList = {};
QString argumentsString = slot->getExtraData().toString();
if (slot->getExtraData().canConvert<QString>())
{
argumentsTempList = PadderCommon::parseArgumentsString(argumentsString);
}

QProcess process;
QString process_executor = detectedScriptExt(slot->getTextData());
if (process_executor.isEmpty())
process.setProgram(slot->getTextData());
else
{
process.setProgram(process_executor);
argumentsTempList.prepend(slot->getTextData());
}
process.setArguments(argumentsTempList);

process.setWorkingDirectory(QFileInfo(slot->getTextData()).absoluteDir().path());
qint64 pid = 0;
bool success = process.startDetached(&pid);
if (success)
qInfo() << "Command: " << slot->getTextData() << " " << argumentsString
<< " executed successfully with pid: " << pid;
else
qWarning() << "Command " << slot->getTextData() << " " << argumentsString << " cannot be executed, pid: " << pid;
}
}

// Create the relative mouse event used by the operating system.
void sendevent(int code1, int code2) { EventHandlerFactory::getInstance()->handler()->sendMouseEvent(code1, code2); }

Expand Down
6 changes: 0 additions & 6 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@
void sendevent(JoyButtonSlot *slot, bool pressed = true);
void sendevent(int code1, int code2);
void sendKeybEvent(JoyButtonSlot *slot, bool pressed = true);
void sendSpringEventRefactor(PadderCommon::springModeInfo *fullSpring, PadderCommon::springModeInfo *relativeSpring = 0,
int *const mousePosX = 0, int *const mousePos = 0);

void sendSpringEvent(PadderCommon::springModeInfo *fullSpring, PadderCommon::springModeInfo *relativeSpring = 0,
int *const mousePosX = 0, int *const mousePos = 0);

void fakeAbsMouseCoordinates(double springX, double springY, int width, int height, int &finalx, int &finaly,
int screen = -1);

QString detectedScriptExt(QString file);
int X11KeySymToKeycode(QString key);
QString keycodeToKeyString(int keycode, int alias = 0);
int X11KeyCodeToX11KeySym(int keycode);
Expand Down
2 changes: 1 addition & 1 deletion src/joybutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void JoyButton::vdpadPassEvent(bool pressed, bool ignoresets)

void JoyButton::joyEvent(bool pressed, bool ignoresets)
{
if (Logger::isDebugLevel())
if (Logger::isDebugEnabled())
DEBUG() << "Processing joyEvent for: " << getName();

if ((m_vdpad != nullptr) && !pendingEvent)
Expand Down
11 changes: 10 additions & 1 deletion src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,20 @@ Logger *Logger::createInstance(QTextStream *stream, LogLevel outputLevel, QObjec
return instance;
}

bool Logger::isDebugLevel()
bool Logger::isDebugEnabled()
{
if (instance != nullptr)
{
return instance->outputLevel == LogLevel::LOG_DEBUG;
}
return false;
}

QString Logger::getCurrentLogFile()
{
Q_ASSERT(instance != nullptr);
if (instance->outputFile.exists())
return instance->outputFile.fileName();
else
return "";
}
3 changes: 2 additions & 1 deletion src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ class Logger : public QObject

static void setLogLevel(LogLevel level);
LogLevel getCurrentLogLevel();
static bool isDebugLevel();
static bool isDebugEnabled();

static void setCurrentStream(QTextStream *stream);
static void setCurrentLogFile(QString filename);
static QString getCurrentLogFile();
bool isWritingToFile();
static QTextStream *getCurrentStream();

Expand Down