1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <algorithm>
#include <sstream>
#include "obs-config.h"
#include "obs-app.hpp"
#include "qt-wrappers.hpp"
#include "platform.hpp"
#include <util/windows/win-version.h>
#include <util/platform.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <Dwmapi.h>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <util/windows/WinHandle.hpp>
#include <util/windows/HRError.hpp>
#include <util/windows/ComPtr.hpp>
using namespace std;
static inline bool check_path(const char *data, const char *path,
string &output)
{
ostringstream str;
str << path << data;
output = str.str();
blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
return os_file_exists(output.c_str());
}
bool GetDataFilePath(const char *data, string &output)
{
if (check_path(data, "data/obs-studio/", output))
return true;
return check_path(data, OBS_DATA_PATH "/obs-studio/", output);
}
string GetDefaultVideoSavePath()
{
wchar_t path_utf16[MAX_PATH];
char path_utf8[MAX_PATH] = {};
SHGetFolderPathW(NULL, CSIDL_MYVIDEO, NULL, SHGFP_TYPE_CURRENT,
path_utf16);
os_wcs_to_utf8(path_utf16, wcslen(path_utf16), path_utf8, MAX_PATH);
return string(path_utf8);
}
static vector<string> GetUserPreferredLocales()
{
vector<string> result;
ULONG num, length = 0;
if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num, nullptr,
&length))
return result;
vector<wchar_t> buffer(length);
if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &num,
&buffer.front(), &length))
return result;
result.reserve(num);
auto start = begin(buffer);
auto end_ = end(buffer);
decltype(start) separator;
while ((separator = find(start, end_, 0)) != end_) {
if (result.size() == num)
break;
char conv[MAX_PATH] = {};
os_wcs_to_utf8(&*start, separator - start, conv, MAX_PATH);
result.emplace_back(conv);
start = separator + 1;
}
return result;
}
vector<string> GetPreferredLocales()
{
vector<string> windows_locales = GetUserPreferredLocales();
auto obs_locales = GetLocaleNames();
auto windows_to_obs = [&obs_locales](string windows) {
string lang_match;
for (auto &locale_pair : obs_locales) {
auto &locale = locale_pair.first;
if (locale == windows.substr(0, locale.size()))
return locale;
if (lang_match.size())
continue;
if (locale.substr(0, 2) == windows.substr(0, 2))
lang_match = locale;
}
return lang_match;
};
vector<string> result;
result.reserve(obs_locales.size());
for (const string &locale : windows_locales) {
string match = windows_to_obs(locale);
if (!match.size())
continue;
if (find(begin(result), end(result), match) != end(result))
continue;
result.emplace_back(match);
}
return result;
}
uint32_t GetWindowsVersion()
{
static uint32_t ver = 0;
if (ver == 0) {
struct win_version_info ver_info;
get_win_ver(&ver_info);
ver = (ver_info.major << 8) | ver_info.minor;
}
return ver;
}
uint32_t GetWindowsBuild()
{
static uint32_t build = 0;
if (build == 0) {
struct win_version_info ver_info;
get_win_ver(&ver_info);
build = ver_info.build;
}
return build;
}
bool IsAlwaysOnTop(QWidget *window)
{
DWORD exStyle = GetWindowLong((HWND)window->winId(), GWL_EXSTYLE);
return (exStyle & WS_EX_TOPMOST) != 0;
}
void SetAlwaysOnTop(QWidget *window, bool enable)
{
HWND hwnd = (HWND)window->winId();
SetWindowPos(hwnd, enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
void SetProcessPriority(const char *priority)
{
if (!priority)
return;
if (strcmp(priority, "High") == 0)
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
else if (strcmp(priority, "AboveNormal") == 0)
SetPriorityClass(GetCurrentProcess(),
ABOVE_NORMAL_PRIORITY_CLASS);
else if (strcmp(priority, "Normal") == 0)
SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
else if (strcmp(priority, "BelowNormal") == 0)
SetPriorityClass(GetCurrentProcess(),
BELOW_NORMAL_PRIORITY_CLASS);
else if (strcmp(priority, "Idle") == 0)
SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
}
void SetWin32DropStyle(QWidget *window)
{
HWND hwnd = (HWND)window->winId();
LONG_PTR ex_style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
ex_style |= WS_EX_ACCEPTFILES;
SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_style);
}
bool SetDisplayAffinitySupported(void)
{
static bool checked = false;
static bool supported;
/* this has to be version gated as setting WDA_EXCLUDEFROMCAPTURE on
older Windows builds behaves like WDA_MONITOR (black box) */
if (!checked) {
if (GetWindowsVersion() > 0x0A00 ||
GetWindowsVersion() == 0x0A00 && GetWindowsBuild() >= 19041)
supported = true;
else
supported = false;
checked = true;
}
return supported;
}
bool DisableAudioDucking(bool disable)
{
ComPtr<IMMDeviceEnumerator> devEmum;
ComPtr<IMMDevice> device;
ComPtr<IAudioSessionManager2> sessionManager2;
ComPtr<IAudioSessionControl> sessionControl;
ComPtr<IAudioSessionControl2> sessionControl2;
HRESULT result = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr,
CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator),
(void **)&devEmum);
if (FAILED(result))
return false;
result = devEmum->GetDefaultAudioEndpoint(eRender, eConsole, &device);
if (FAILED(result))
return false;
result = device->Activate(__uuidof(IAudioSessionManager2),
CLSCTX_INPROC_SERVER, nullptr,
(void **)&sessionManager2);
if (FAILED(result))
return false;
result = sessionManager2->GetAudioSessionControl(nullptr, 0,
&sessionControl);
if (FAILED(result))
return false;
result = sessionControl->QueryInterface(&sessionControl2);
if (FAILED(result))
return false;
result = sessionControl2->SetDuckingPreference(disable);
return SUCCEEDED(result);
}
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 CheckIfAlreadyRunning(bool &already_running)
{
string name;
if (!portable_mode) {
name = "OBSStudioCore";
} else {
char path[500];
char absPath[512];
*path = 0;
*absPath = 0;
GetConfigPath(path, sizeof(path), "");
os_get_abs_path(path, absPath, sizeof(absPath));
name = "OBSStudioPortable";
name += absPath;
}
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;
}
struct MonitorData {
const wchar_t *id;
MONITORINFOEX info;
bool found;
};
static BOOL CALLBACK GetMonitorCallback(HMONITOR monitor, HDC, LPRECT,
LPARAM param)
{
MonitorData *data = (MonitorData *)param;
if (GetMonitorInfoW(monitor, &data->info)) {
if (wcscmp(data->info.szDevice, data->id) == 0) {
data->found = true;
return false;
}
}
return true;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
#define GENERIC_MONITOR_NAME QStringLiteral("Generic PnP Monitor")
QString GetMonitorName(const QString &id)
{
MonitorData data = {};
data.id = (const wchar_t *)id.utf16();
data.info.cbSize = sizeof(data.info);
EnumDisplayMonitors(nullptr, nullptr, GetMonitorCallback,
(LPARAM)&data);
if (!data.found) {
return GENERIC_MONITOR_NAME;
}
UINT32 numPath, numMode;
if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &numPath,
&numMode) != ERROR_SUCCESS) {
return GENERIC_MONITOR_NAME;
}
std::vector<DISPLAYCONFIG_PATH_INFO> paths(numPath);
std::vector<DISPLAYCONFIG_MODE_INFO> modes(numMode);
if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &numPath, paths.data(),
&numMode, modes.data(),
nullptr) != ERROR_SUCCESS) {
return GENERIC_MONITOR_NAME;
}
DISPLAYCONFIG_TARGET_DEVICE_NAME target;
bool found = false;
paths.resize(numPath);
for (size_t i = 0; i < numPath; ++i) {
const DISPLAYCONFIG_PATH_INFO &path = paths[i];
DISPLAYCONFIG_SOURCE_DEVICE_NAME s;
s.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
s.header.size = sizeof(s);
s.header.adapterId = path.sourceInfo.adapterId;
s.header.id = path.sourceInfo.id;
if (DisplayConfigGetDeviceInfo(&s.header) == ERROR_SUCCESS &&
wcscmp(data.info.szDevice, s.viewGdiDeviceName) == 0) {
target.header.type =
DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
target.header.size = sizeof(target);
target.header.adapterId = path.sourceInfo.adapterId;
target.header.id = path.targetInfo.id;
found = DisplayConfigGetDeviceInfo(&target.header) ==
ERROR_SUCCESS;
break;
}
}
if (!found) {
return GENERIC_MONITOR_NAME;
}
return QString::fromWCharArray(target.monitorFriendlyDeviceName);
}
#endif
/* Based on https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html */
typedef const char *(CDECL *WINEGETVERSION)(void);
bool IsRunningOnWine()
{
WINEGETVERSION func;
HMODULE nt;
nt = GetModuleHandleW(L"ntdll");
if (!nt)
return false;
func = (WINEGETVERSION)GetProcAddress(nt, "wine_get_version");
if (func) {
blog(LOG_WARNING, "Running on Wine version \"%s\"", func());
return true;
}
return false;
}
HWND hwnd;
void TaskbarOverlayInit()
{
hwnd = (HWND)App()->GetMainWindow()->winId();
}
void TaskbarOverlaySetStatus(TaskbarOverlayStatus status)
{
ITaskbarList4 *taskbarIcon;
auto hr = CoCreateInstance(CLSID_TaskbarList, NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&taskbarIcon));
if (FAILED(hr)) {
taskbarIcon->Release();
return;
}
hr = taskbarIcon->HrInit();
if (FAILED(hr)) {
taskbarIcon->Release();
return;
}
if (status != TaskbarOverlayStatusInactive) {
QIcon qicon;
switch (status) {
case TaskbarOverlayStatusActive:
qicon = QIcon::fromTheme(
"obs-active", QIcon(":/res/images/active.png"));
break;
case TaskbarOverlayStatusPaused:
qicon = QIcon::fromTheme(
"obs-paused", QIcon(":/res/images/paused.png"));
break;
}
HICON hicon = nullptr;
if (!qicon.isNull()) {
Q_GUI_EXPORT HICON qt_pixmapToWinHICON(
const QPixmap &p);
hicon = qt_pixmapToWinHICON(
qicon.pixmap(GetSystemMetrics(SM_CXSMICON)));
if (!hicon)
return;
}
taskbarIcon->SetOverlayIcon(hwnd, hicon, nullptr);
DestroyIcon(hicon);
} else {
taskbarIcon->SetOverlayIcon(hwnd, nullptr, nullptr);
}
taskbarIcon->Release();
}
|