mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-26 00:23:55 -07:00
Only enforce single instance if launched with arguments.
Took 5 minutes
This commit is contained in:
parent
edf90a9a4a
commit
d791e7d0ff
2 changed files with 43 additions and 41 deletions
|
|
@ -279,18 +279,31 @@ int main(int argc, char *argv[])
|
||||||
SpoilerBackgroundUpdater spoilerBackgroundUpdater;
|
SpoilerBackgroundUpdater spoilerBackgroundUpdater;
|
||||||
|
|
||||||
// --- Handle files or URLs passed at startup ---
|
// --- Handle files or URLs passed at startup ---
|
||||||
SingleInstanceManager instance;
|
|
||||||
QStringList startupFiles;
|
QStringList startupFiles;
|
||||||
|
|
||||||
// Collect command-line files/URLs
|
|
||||||
for (int i = 1; i < argc; ++i) {
|
for (int i = 1; i < argc; ++i) {
|
||||||
QString arg = QString::fromLocal8Bit(argv[i]);
|
startupFiles.append(QString::fromLocal8Bit(argv[i]));
|
||||||
startupFiles.append(arg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!instance.tryRun(startupFiles)) {
|
bool hasActivationFiles = !startupFiles.isEmpty();
|
||||||
// Another instance received our files, exit
|
|
||||||
return 0;
|
SingleInstanceManager instance;
|
||||||
|
|
||||||
|
if (hasActivationFiles) {
|
||||||
|
// Activation launch: try to forward
|
||||||
|
if (!instance.tryRun(startupFiles)) {
|
||||||
|
// Sent successfully → exit
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// No primary instance → become server
|
||||||
|
qInfo() << "No existing instance found, becoming primary instance";
|
||||||
|
} else {
|
||||||
|
// Plain launch: try to start server, but do not connect to any existing
|
||||||
|
if (!instance.tryRun(QStringList())) {
|
||||||
|
// Server already exists → just run independently
|
||||||
|
qInfo() << "Another instance exists, running independently";
|
||||||
|
} else {
|
||||||
|
qInfo() << "No existing instance found, starting server";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.show();
|
ui.show();
|
||||||
|
|
@ -330,18 +343,6 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
|
||||||
// macOS: handle files opened via Finder after startup
|
|
||||||
QObject::connect(&app, &QApplication::fileOpen, [&ui](const QString &filePath) {
|
|
||||||
qDebug() << "macOS opened file:" << filePath;
|
|
||||||
std::optional<LoadedDeck> deckOpt =
|
|
||||||
DeckLoader::loadFromFile(filePath, DeckFileFormat::getFormatFromName(filePath), true);
|
|
||||||
if (deckOpt) {
|
|
||||||
ui.getTabSupervisor()->openDeckInNewTab(deckOpt.value());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int ret = app.exec();
|
int ret = app.exec();
|
||||||
|
|
||||||
qCInfo(MainLog) << "Event loop finished, terminating...";
|
qCInfo(MainLog) << "Event loop finished, terminating...";
|
||||||
|
|
|
||||||
|
|
@ -4,35 +4,35 @@ SingleInstanceManager::SingleInstanceManager(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SingleInstanceManager::tryRun(const QStringList &initialFiles)
|
bool SingleInstanceManager::tryRun(const QStringList &filesToSend)
|
||||||
{
|
{
|
||||||
serverName = "CockatriceSingleInstance";
|
serverName = "CockatriceSingleInstance";
|
||||||
|
|
||||||
QLocalSocket socket;
|
// Attempt to connect only if we have files to send
|
||||||
socket.connectToServer(serverName);
|
if (!filesToSend.isEmpty()) {
|
||||||
|
QLocalSocket socket;
|
||||||
|
socket.connectToServer(serverName);
|
||||||
|
if (socket.waitForConnected(200)) {
|
||||||
|
// Serialize payload with length prefix
|
||||||
|
QByteArray payload;
|
||||||
|
QDataStream out(&payload, QIODevice::WriteOnly);
|
||||||
|
out << filesToSend;
|
||||||
|
|
||||||
if (socket.waitForConnected(200)) {
|
QByteArray message;
|
||||||
// Serialize into buffer first
|
QDataStream msgStream(&message, QIODevice::WriteOnly);
|
||||||
QByteArray payload;
|
msgStream << quint32(payload.size());
|
||||||
QDataStream out(&payload, QIODevice::WriteOnly);
|
message.append(payload);
|
||||||
out << initialFiles;
|
|
||||||
|
|
||||||
// Prefix with size
|
socket.write(message);
|
||||||
QByteArray message;
|
socket.flush();
|
||||||
QDataStream msgStream(&message, QIODevice::WriteOnly);
|
socket.waitForBytesWritten(1000);
|
||||||
msgStream << quint32(payload.size());
|
|
||||||
message.append(payload);
|
|
||||||
|
|
||||||
socket.write(message);
|
return false; // Sent successfully → exit
|
||||||
socket.flush();
|
}
|
||||||
socket.waitForBytesWritten(1000);
|
|
||||||
|
|
||||||
return false; // Another instance is running
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// No other instance → start server
|
// Otherwise, start server
|
||||||
server = new QLocalServer(this);
|
server = new QLocalServer(this);
|
||||||
|
|
||||||
connect(server, &QLocalServer::newConnection, this, &SingleInstanceManager::handleNewConnection);
|
connect(server, &QLocalServer::newConnection, this, &SingleInstanceManager::handleNewConnection);
|
||||||
|
|
||||||
if (!server->listen(serverName)) {
|
if (!server->listen(serverName)) {
|
||||||
|
|
@ -40,8 +40,9 @@ bool SingleInstanceManager::tryRun(const QStringList &initialFiles)
|
||||||
server->listen(serverName);
|
server->listen(serverName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true; // This process is now primary server
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleInstanceManager::handleNewConnection()
|
void SingleInstanceManager::handleNewConnection()
|
||||||
{
|
{
|
||||||
QLocalSocket *socket = server->nextPendingConnection();
|
QLocalSocket *socket = server->nextPendingConnection();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue