diff --git a/src/main.cpp b/src/main.cpp index fffbef93..41ad539c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -136,6 +136,8 @@ int main(int argc, char *argv[]) // Ask another instance to open files passed in. if (!filePaths.isEmpty()) { guard.openExternalFiles(filePaths); + } else { + guard.showInstance(); } return 0; diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index be617715..459c2a79 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -1906,6 +1906,12 @@ void VMainWindow::checkSharedMemory() qDebug() << "shared memory fetch files" << files; openExternalFiles(files); + // Eliminate the signal. + m_guard->fetchAskedToShow(); + + showMainWindow(); + } else if (m_guard->fetchAskedToShow()) { + qDebug() << "shared memory asked to show up"; showMainWindow(); } } diff --git a/src/vsingleinstanceguard.cpp b/src/vsingleinstanceguard.cpp index 68bc3ae8..42437190 100644 --- a/src/vsingleinstanceguard.cpp +++ b/src/vsingleinstanceguard.cpp @@ -29,6 +29,7 @@ bool VSingleInstanceGuard::tryRun() SharedStruct *str = (SharedStruct *)m_sharedMemory.data(); str->m_magic = c_magic; str->m_filesBufIdx = 0; + str->m_askedToShow = false; m_sharedMemory.unlock(); return true; } else { @@ -130,3 +131,37 @@ QStringList VSingleInstanceGuard::fetchFilesToOpen() return files; } + +void VSingleInstanceGuard::showInstance() +{ + if (!m_sharedMemory.isAttached()) { + if (!m_sharedMemory.attach()) { + qDebug() << "fail to attach to the shared memory segment" + << (m_sharedMemory.error() ? m_sharedMemory.errorString() : ""); + return; + } + } + + m_sharedMemory.lock(); + SharedStruct *str = (SharedStruct *)m_sharedMemory.data(); + V_ASSERT(str->m_magic == c_magic); + str->m_askedToShow = true; + m_sharedMemory.unlock(); + + qDebug() << "try to request another instance to show up"; +} + +bool VSingleInstanceGuard::fetchAskedToShow() +{ + bool ret = false; + + Q_ASSERT(m_sharedMemory.isAttached()); + m_sharedMemory.lock(); + SharedStruct *str = (SharedStruct *)m_sharedMemory.data(); + Q_ASSERT(str->m_magic == c_magic); + ret = str->m_askedToShow; + str->m_askedToShow = false; + m_sharedMemory.unlock(); + + return ret; +} diff --git a/src/vsingleinstanceguard.h b/src/vsingleinstanceguard.h index 6cc442c1..750f3a3a 100644 --- a/src/vsingleinstanceguard.h +++ b/src/vsingleinstanceguard.h @@ -18,10 +18,16 @@ public: // via command line arguments. void openExternalFiles(const QStringList &p_files); + // Ask another instance to show itself. + void showInstance(); + // Fetch files from shared memory to open. // Will clear the shared memory. QStringList fetchFilesToOpen(); + // Whether this instance is asked to show itself. + bool fetchAskedToShow(); + private: // The count of the entries in the buffer to hold the path of the files to open. enum { FilesBufCount = 1024 }; @@ -38,6 +44,9 @@ private: // [size of file1][file1][size of file2][file 2] // Unicode representation of QString. ushort m_filesBuf[FilesBufCount]; + + // Whether other instances ask to show the legal instance. + bool m_askedToShow; }; // Append @p_file to the shared struct files buffer.