single-instance: latter instances will ask the legal instance to show itself

This commit is contained in:
Le Tan 2017-07-25 22:03:02 +08:00
parent 60e65d1781
commit 8264ace8f8
4 changed files with 52 additions and 0 deletions

View File

@ -136,6 +136,8 @@ int main(int argc, char *argv[])
// Ask another instance to open files passed in. // Ask another instance to open files passed in.
if (!filePaths.isEmpty()) { if (!filePaths.isEmpty()) {
guard.openExternalFiles(filePaths); guard.openExternalFiles(filePaths);
} else {
guard.showInstance();
} }
return 0; return 0;

View File

@ -1906,6 +1906,12 @@ void VMainWindow::checkSharedMemory()
qDebug() << "shared memory fetch files" << files; qDebug() << "shared memory fetch files" << files;
openExternalFiles(files); openExternalFiles(files);
// Eliminate the signal.
m_guard->fetchAskedToShow();
showMainWindow();
} else if (m_guard->fetchAskedToShow()) {
qDebug() << "shared memory asked to show up";
showMainWindow(); showMainWindow();
} }
} }

View File

@ -29,6 +29,7 @@ bool VSingleInstanceGuard::tryRun()
SharedStruct *str = (SharedStruct *)m_sharedMemory.data(); SharedStruct *str = (SharedStruct *)m_sharedMemory.data();
str->m_magic = c_magic; str->m_magic = c_magic;
str->m_filesBufIdx = 0; str->m_filesBufIdx = 0;
str->m_askedToShow = false;
m_sharedMemory.unlock(); m_sharedMemory.unlock();
return true; return true;
} else { } else {
@ -130,3 +131,37 @@ QStringList VSingleInstanceGuard::fetchFilesToOpen()
return files; 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;
}

View File

@ -18,10 +18,16 @@ public:
// via command line arguments. // via command line arguments.
void openExternalFiles(const QStringList &p_files); void openExternalFiles(const QStringList &p_files);
// Ask another instance to show itself.
void showInstance();
// Fetch files from shared memory to open. // Fetch files from shared memory to open.
// Will clear the shared memory. // Will clear the shared memory.
QStringList fetchFilesToOpen(); QStringList fetchFilesToOpen();
// Whether this instance is asked to show itself.
bool fetchAskedToShow();
private: private:
// The count of the entries in the buffer to hold the path of the files to open. // The count of the entries in the buffer to hold the path of the files to open.
enum { FilesBufCount = 1024 }; enum { FilesBufCount = 1024 };
@ -38,6 +44,9 @@ private:
// [size of file1][file1][size of file2][file 2] // [size of file1][file1][size of file2][file 2]
// Unicode representation of QString. // Unicode representation of QString.
ushort m_filesBuf[FilesBufCount]; 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. // Append @p_file to the shared struct files buffer.