UI: Warn user if multiple instances of the UI are open

Uses a named mutex to detect if multiple instances of the program are
open, and if so warns the user.  When running in portable mode, uses a
separate unique mutex name mapped to the user's config directory to
ensure that no two portable builds use the same config directory.  This
way, portable builds do not conflict with normal builds or other
separate portable builds.
This commit is contained in:
jp9000
2017-05-14 15:25:34 -07:00
parent 99f5ae3059
commit 96ce9633e0
5 changed files with 126 additions and 1 deletions
+63
View File
@@ -34,6 +34,7 @@ using namespace std;
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <util/windows/WinHandle.hpp>
#include <util/windows/HRError.hpp>
#include <util/windows/ComPtr.hpp>
@@ -271,3 +272,65 @@ uint64_t CurrentMemoryUsage()
return (uint64_t)pmc.WorkingSetSize;
}
struct RunOnceMutexData {
WinHandle handle;
inline RunOnceMutexData(HANDLE h) : handle(h) {}
};
RunOnceMutex::RunOnceMutex(RunOnceMutex &&rom)
{
delete data;
data = rom.data;
rom.data = nullptr;
}
RunOnceMutex::~RunOnceMutex()
{
delete data;
}
RunOnceMutex &RunOnceMutex::operator=(RunOnceMutex &&rom)
{
delete data;
data = rom.data;
rom.data = nullptr;
return *this;
}
RunOnceMutex GetRunOnceMutex(bool &already_running)
{
string name;
if (!portable_mode) {
name = "OBSStudioCore";
} else {
char path[500];
*path = 0;
GetConfigPath(path, sizeof(path), "");
name = "OBSStudioPortable";
name += path;
}
BPtr<wchar_t> wname;
os_utf8_to_wcs_ptr(name.c_str(), name.size(), &wname);
if (wname) {
wchar_t *temp = wname;
while (*temp) {
if (!iswalnum(*temp))
*temp = L'_';
temp++;
}
}
HANDLE h = OpenMutexW(SYNCHRONIZE, false, wname.Get());
already_running = !!h;
if (!already_running)
h = CreateMutexW(nullptr, false, wname.Get());
RunOnceMutex rom(h ? new RunOnceMutexData(h) : nullptr);
return rom;
}