Skip to content

Commit

Permalink
Revert "Replace RunGuard with SingleApplication"
Browse files Browse the repository at this point in the history
This reverts commit d105ff6.
  • Loading branch information
HTRamsey committed Aug 29, 2024
1 parent 77c7b3d commit f453db4
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,11 @@ if(ANDROID)

target_include_directories(QGC PUBLIC ${CMAKE_SOURCE_DIR}/android/src)
else()
set(QAPPLICATION_CLASS QApplication CACHE STRING "Inheritance class for SingleApplication" FORCE)
FetchContent_Declare(SingleApplication
GIT_REPOSITORY https://github.com/itay-grudev/SingleApplication
GIT_TAG v3.5.1
GIT_SHALLOW TRUE
target_sources(QGC
PRIVATE
RunGuard.cc
RunGuard.h
)
FetchContent_MakeAvailable(SingleApplication)
target_link_libraries(QGC PUBLIC SingleApplication::SingleApplication)
endif()

target_precompile_headers(QGC PRIVATE ${CMAKE_SOURCE_DIR}/src/pch.h)
2 changes: 1 addition & 1 deletion src/QGCApplication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static QObject* shapeFileHelperSingletonFactory(QQmlEngine*, QJSEngine*)
}

QGCApplication::QGCApplication(int &argc, char* argv[], bool unitTesting)
: QGCAPPLICATION_CLASS(argc, argv)
: QApplication(argc, argv)
, _runningUnitTests(unitTesting)
{
_msecsElapsedTime.start();
Expand Down
9 changes: 1 addition & 8 deletions src/QGCApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
#include <QtCore/private/qthread_p.h>
#include <QtCore/private/qobject_p.h>

#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
#define QGCAPPLICATION_CLASS QApplication
#else
#include <SingleApplication>
#define QGCAPPLICATION_CLASS SingleApplication
#endif

// Work around circular header includes
class QQmlApplicationEngine;
class QGCToolbox;
Expand Down Expand Up @@ -63,7 +56,7 @@ class QGCApplication;
**/

// TODO: Use QtGraphs to convert to QGuiApplication
class QGCApplication : public QGCAPPLICATION_CLASS
class QGCApplication : public QApplication
{
Q_OBJECT
public:
Expand Down
87 changes: 87 additions & 0 deletions src/RunGuard.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/****************************************************************************
*
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/

#include "RunGuard.h"

#include <QtCore/QCryptographicHash>

namespace
{

QString generateKeyHash( const QString& key, const QString& salt )
{
QByteArray data;

data.append( key.toUtf8() );
data.append( salt.toUtf8() );
data = QCryptographicHash::hash( data, QCryptographicHash::Sha1 ).toHex();

return data;
}

}

RunGuard::RunGuard( const QString& key )
: key( key )
, memLockKey( generateKeyHash( key, "_memLockKey" ) )
, sharedmemKey( generateKeyHash( key, "_sharedmemKey" ) )
, sharedMem( sharedmemKey )
, memLock( memLockKey, 1 )
{
memLock.acquire();
{
QSharedMemory fix( sharedmemKey ); // Fix for *nix: http://habrahabr.ru/post/173281/
fix.attach();
}
memLock.release();
}

RunGuard::~RunGuard()
{
release();
}

bool RunGuard::isAnotherRunning()
{
if ( sharedMem.isAttached() )
return false;

memLock.acquire();
const bool isRunning = sharedMem.attach();
if ( isRunning )
sharedMem.detach();
memLock.release();

return isRunning;
}

bool RunGuard::tryToRun()
{
if ( isAnotherRunning() ) // Extra check
return false;

memLock.acquire();
const bool result = sharedMem.create( sizeof( quint64 ) );
memLock.release();
if ( !result )
{
release();
return false;
}

return true;
}

void RunGuard::release()
{
memLock.acquire();
if ( sharedMem.isAttached() )
sharedMem.detach();
memLock.release();
}
36 changes: 36 additions & 0 deletions src/RunGuard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/****************************************************************************
*
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/

#pragma once


#include <QtCore/QObject>
#include <QtCore/QSharedMemory>
#include <QtCore/QSystemSemaphore>

class RunGuard
{
public:
RunGuard( const QString& key );
~RunGuard();

bool isAnotherRunning();
bool tryToRun();
void release();

private:
const QString key;
const QString memLockKey;
const QString sharedmemKey;

QSharedMemory sharedMem;
QSystemSemaphore memLock;

Q_DISABLE_COPY( RunGuard )
};
22 changes: 22 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "QGC.h"
#include "AppMessages.h"

#ifndef __mobile__
#include "RunGuard.h"
#endif

#ifdef Q_OS_ANDROID
#include "AndroidInterface.h"
#endif
Expand Down Expand Up @@ -78,6 +82,24 @@ void sigHandler(int s)

int main(int argc, char *argv[])
{
#ifndef __mobile__
// We make the runguard key different for custom and non custom
// builds, so they can be executed together in the same device.
// Stable and Daily have same QGC_APP_NAME so they would
// not be able to run at the same time
const QString runguardString = QString("%1 RunGuardKey").arg(QGC_APP_NAME);

RunGuard guard(runguardString);
if (!guard.tryToRun()) {
// QApplication is necessary to use QMessageBox
QApplication errorApp(argc, argv);
QMessageBox::critical(nullptr, QObject::tr("Error"),
QObject::tr("A second instance of %1 is already running. Please close the other instance and try again.").arg(QGC_APP_NAME)
);
return -1;
}
#endif

#ifdef Q_OS_LINUX
#ifndef Q_OS_ANDROID
if (getuid() == 0) {
Expand Down

0 comments on commit f453db4

Please sign in to comment.