Skip to content

Commit

Permalink
feat: Allow unhiding app window when second instance of AntiMicroX is…
Browse files Browse the repository at this point in the history
… launched

Until now attempt of launching AntiMicroX in Linux when app was hidden doesn't do anything.
I added showing this window when app is launched once more to ensure seamless experience.
  • Loading branch information
pktiuk committed Oct 19, 2021
2 parents bc273b3 + d5da311 commit faf033a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion other/io.github.antimicrox.antimicrox.desktop
Expand Up @@ -8,7 +8,7 @@ Comment[fr]=Utilisez une manette de jeu pour commander un logiciel
Name[de]=AntiMicroX
Comment[de]=Nutze ein Gamepad, um Programme/Spiele zu steuern
Comment[uk]=Використовуйте ігровий маніпулятор для керування програмами
Exec=antimicrox %f
Exec=antimicrox --show %f
Icon=io.github.antimicrox.antimicrox
StartupNotify=true
Terminal=false
Expand Down
12 changes: 12 additions & 0 deletions src/commandlineutility.cpp
Expand Up @@ -37,6 +37,7 @@ CommandLineUtility::CommandLineUtility(QObject *parent)
profileLocation = "";
controllerNumber = 0;
hiddenRequest = false;
showRequest = false;
unloadProfile = false;
startSetNumber = 0;
daemonMode = false;
Expand Down Expand Up @@ -69,6 +70,8 @@ void CommandLineUtility::parseArguments(const QApplication &parsed_app)
{"no-tray", QCoreApplication::translate("main", "Launch program with the tray menu disabled")},
// An option with a value
{"hidden", QCoreApplication::translate("main", "Launch program without the main window displayed")},
{"show", QCoreApplication::translate(
"main", "Show app window when hidden. (Used for unhiding window of already running app instance).")},
{"profile",
QCoreApplication::translate("main", "Launch program with the configuration file selected as "
"the default for "
Expand Down Expand Up @@ -149,6 +152,11 @@ void CommandLineUtility::parseArguments(const QApplication &parsed_app)
hiddenRequest = true;
}

if (parser.isSet("show"))
{
showRequest = true;
}

if (parser.isSet("unload"))
{
parseArgsUnload(parser);
Expand Down Expand Up @@ -246,6 +254,8 @@ void CommandLineUtility::parseArguments(const QApplication &parsed_app)

i++;
}
if (showRequest && hiddenRequest)
throw std::runtime_error(QObject::tr("Specified contradicting flags: --show and --hidden").toStdString());
}

void CommandLineUtility::parseArgsProfile(const QCommandLineParser &parser)
Expand Down Expand Up @@ -434,6 +444,8 @@ int CommandLineUtility::getControllerNumber() { return controllerNumber; }

bool CommandLineUtility::isHiddenRequested() { return hiddenRequest; }

bool CommandLineUtility::isShowRequested() { return showRequest; }

bool CommandLineUtility::hasControllerID() { return !controllerIDString.isEmpty(); }

QString CommandLineUtility::getControllerID() { return controllerIDString; }
Expand Down
2 changes: 2 additions & 0 deletions src/commandlineutility.h
Expand Up @@ -99,6 +99,7 @@ class CommandLineUtility : public QObject
bool hasControllerNumber();
bool hasControllerID();
bool isHiddenRequested();
bool isShowRequested();
bool isUnloadRequested();
bool shouldListControllers();
bool shouldMapController();
Expand Down Expand Up @@ -126,6 +127,7 @@ class CommandLineUtility : public QObject
bool launchInTray;
bool hideTrayIcon;
bool hiddenRequest;
bool showRequest;
bool unloadProfile;
bool daemonMode;
bool listControllers;
Expand Down
1 change: 1 addition & 0 deletions src/common.h
Expand Up @@ -126,6 +126,7 @@ const int LATESTCONFIGFILEVERSION = 19;
// to be performed in order to be compatible with the latest version.
const int LATESTCONFIGMIGRATIONVERSION = 5;
const QString localSocketKey = "antimicroxSignalListener";
const QString unhideCommand = "unhideWindow";
const QString githubProjectPage = "https://github.com/AntiMicroX/antimicrox/";
const QString githubIssuesPage = "https://github.com/AntiMicroX/antimicrox/issues";
const QString wikiPage = QString("%1/wiki").arg(githubProjectPage);
Expand Down
19 changes: 19 additions & 0 deletions src/localantimicroserver.cpp
Expand Up @@ -73,6 +73,7 @@ void LocalAntiMicroServer::handleOutsideConnection()
qDebug() << "There is next pending connection: " << socket->socketDescriptor();
connect(socket, &QLocalSocket::disconnected, this, &LocalAntiMicroServer::handleSocketDisconnect);
connect(socket, &QLocalSocket::disconnected, socket, &QLocalSocket::deleteLater);
checkForMessages(socket);
} else
{
qDebug() << "There isn't next pending connection: ";
Expand All @@ -87,4 +88,22 @@ void LocalAntiMicroServer::handleSocketDisconnect() { emit clientdisconnect(); }

void LocalAntiMicroServer::close() { localServer->close(); }

void LocalAntiMicroServer::checkForMessages(QLocalSocket *socket)
{
DEBUG() << "Waiting for message";
socket->waitForConnected(50);
bool result = socket->waitForReadyRead(200);
DEBUG() << "Waiting for message ended with result: " << (result ? "true" : "false");
if (result)
{
QString msg = QString(socket->readLine(30));
DEBUG() << "Received external message:" << msg;
if (msg == PadderCommon::unhideCommand)
{
DEBUG() << "Showing hidden window because of external request";
emit showHiddenWindow();
}
}
}

QLocalServer *LocalAntiMicroServer::getLocalServer() const { return localServer; }
7 changes: 7 additions & 0 deletions src/localantimicroserver.h
Expand Up @@ -19,10 +19,14 @@
#ifndef LOCALANTIMICROSERVER_H
#define LOCALANTIMICROSERVER_H

#include <QLocalSocket>
#include <QObject>

class QLocalServer;

/**
* @brief Class used for checking presence of other AntiMicroX instances and communicating with them.
*/
class LocalAntiMicroServer : public QObject
{
Q_OBJECT
Expand All @@ -34,6 +38,7 @@ class LocalAntiMicroServer : public QObject

signals:
void clientdisconnect();
void showHiddenWindow();

public slots:
void startLocalServer();
Expand All @@ -42,6 +47,8 @@ class LocalAntiMicroServer : public QObject
void close();

private:
void checkForMessages(QLocalSocket *socket);

QLocalServer *localServer;
};

Expand Down
6 changes: 6 additions & 0 deletions src/main.cpp
Expand Up @@ -322,6 +322,11 @@ int main(int argc, char *argv[])
} else if (cmdutility.isUnloadRequested())
{
mainWindow.saveAppConfig();
} else if (cmdutility.isShowRequested())
{
INFO() << "Showing window if hidden.\n";
socket.write(PadderCommon::unhideCommand.toStdString().c_str());
socket.waitForBytesWritten(100);
}
qDebug() << "Closing this app instance";

Expand Down Expand Up @@ -766,6 +771,7 @@ int main(int argc, char *argv[])
QObject::connect(&antimicrox, &QApplication::aboutToQuit, &PadderCommon::mouseHelperObj, &MouseHelper::deleteDeskWid,
Qt::DirectConnection);

QObject::connect(localServer, &LocalAntiMicroServer::showHiddenWindow, mainWindow, &MainWindow::show);
QObject::connect(localServer, &LocalAntiMicroServer::clientdisconnect, mainWindow,
&MainWindow::handleInstanceDisconnect);
QObject::connect(mainWindow, &MainWindow::mappingUpdated, joypad_worker.data(), &InputDaemon::refreshMapping);
Expand Down

0 comments on commit faf033a

Please sign in to comment.