mirror of
https://github.com/obsproject/obs-studio.git
synced 2026-09-19 09:41:32 +08:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user