2013-12-07 18:22:56 +01:00
|
|
|
/******************************************************************************
|
2023-05-19 02:37:26 +02:00
|
|
|
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
|
2013-12-07 18:22:56 +01:00
|
|
|
|
|
|
|
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 <sstream>
|
2016-01-26 02:09:59 +01:00
|
|
|
#include <dlfcn.h>
|
2014-01-09 01:17:30 +01:00
|
|
|
#include <util/base.h>
|
2022-07-14 19:22:21 +02:00
|
|
|
#include <util/threading.h>
|
2014-07-21 16:26:57 +02:00
|
|
|
#include <obs-config.h>
|
2013-12-07 18:22:56 +01:00
|
|
|
#include "platform.hpp"
|
2014-07-11 20:32:27 +02:00
|
|
|
#include "obs-app.hpp"
|
2013-12-07 18:22:56 +01:00
|
|
|
|
2013-12-12 11:44:17 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-12-16 16:04:51 +01:00
|
|
|
#import <AppKit/AppKit.h>
|
2022-07-14 19:22:21 +02:00
|
|
|
#import <CoreFoundation/CoreFoundation.h>
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
#import <ApplicationServices/ApplicationServices.h>
|
2013-12-16 16:04:51 +01:00
|
|
|
|
2014-01-09 01:17:30 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2019-10-14 04:46:08 +02:00
|
|
|
bool isInBundle()
|
|
|
|
{
|
|
|
|
NSRunningApplication *app = [NSRunningApplication currentApplication];
|
|
|
|
return [app bundleIdentifier] != nil;
|
|
|
|
}
|
|
|
|
|
2013-12-07 18:22:56 +01:00
|
|
|
bool GetDataFilePath(const char *data, string &output)
|
|
|
|
{
|
2023-04-02 18:56:44 +02:00
|
|
|
NSURL *bundleUrl = [[NSBundle mainBundle] bundleURL];
|
|
|
|
NSString *path = [[bundleUrl path] stringByAppendingFormat:@"/%@/%s", @"Contents/Resources", data];
|
|
|
|
output = path.UTF8String;
|
2019-10-14 04:46:08 +02:00
|
|
|
|
2013-12-12 11:44:17 +01:00
|
|
|
return !access(output.c_str(), R_OK);
|
2013-12-07 18:22:56 +01:00
|
|
|
}
|
2013-12-16 08:06:13 +01:00
|
|
|
|
2022-02-28 22:43:06 +01:00
|
|
|
void CheckIfAlreadyRunning(bool &already_running)
|
2020-10-05 14:48:22 +02:00
|
|
|
{
|
2023-04-02 18:56:44 +02:00
|
|
|
NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier];
|
2020-10-05 14:48:22 +02:00
|
|
|
|
2023-04-02 18:56:44 +02:00
|
|
|
NSUInteger appCount = [[NSRunningApplication runningApplicationsWithBundleIdentifier:bundleId] count];
|
2020-10-05 14:48:22 +02:00
|
|
|
|
2023-04-02 18:56:44 +02:00
|
|
|
already_running = appCount > 1;
|
2020-10-05 14:48:22 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 08:08:47 +02:00
|
|
|
string GetDefaultVideoSavePath()
|
|
|
|
{
|
2014-07-10 22:00:18 +02:00
|
|
|
NSFileManager *fm = [NSFileManager defaultManager];
|
|
|
|
NSURL *url = [fm URLForDirectory:NSMoviesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:true
|
|
|
|
error:nil];
|
2019-07-09 20:29:39 +02:00
|
|
|
|
2014-07-10 22:00:18 +02:00
|
|
|
if (!url)
|
|
|
|
return getenv("HOME");
|
|
|
|
|
2014-07-11 23:27:37 +02:00
|
|
|
return url.path.fileSystemRepresentation;
|
2014-05-21 08:08:47 +02:00
|
|
|
}
|
2014-07-11 20:32:27 +02:00
|
|
|
|
|
|
|
vector<string> GetPreferredLocales()
|
|
|
|
{
|
|
|
|
NSArray *preferred = [NSLocale preferredLanguages];
|
|
|
|
|
|
|
|
auto locales = GetLocaleNames();
|
|
|
|
auto lang_to_locale = [&locales](string lang) -> string {
|
|
|
|
string lang_match = "";
|
|
|
|
|
|
|
|
for (const auto &locale : locales) {
|
|
|
|
if (locale.first == lang.substr(0, locale.first.size()))
|
|
|
|
return locale.first;
|
|
|
|
|
2019-07-09 20:29:39 +02:00
|
|
|
if (!lang_match.size() && locale.first.substr(0, 2) == lang.substr(0, 2))
|
2014-07-11 20:32:27 +02:00
|
|
|
lang_match = locale.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lang_match;
|
|
|
|
};
|
|
|
|
|
|
|
|
vector<string> result;
|
|
|
|
result.reserve(preferred.count);
|
|
|
|
|
|
|
|
for (NSString *lang in preferred) {
|
|
|
|
string locale = lang_to_locale(lang.UTF8String);
|
|
|
|
if (!locale.size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (find(begin(result), end(result), locale) != end(result))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.emplace_back(locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2016-01-22 16:33:29 +01:00
|
|
|
|
2016-08-22 01:11:32 +02:00
|
|
|
bool IsAlwaysOnTop(QWidget *window)
|
2016-01-22 16:33:29 +01:00
|
|
|
{
|
|
|
|
return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
|
|
|
|
}
|
|
|
|
|
2020-10-01 18:45:13 +02:00
|
|
|
void disableColorSpaceConversion(QWidget *window)
|
|
|
|
{
|
|
|
|
NSView *view = (__bridge NSView *) reinterpret_cast<void *>(window->winId());
|
|
|
|
view.window.colorSpace = NSColorSpace.sRGBColorSpace;
|
|
|
|
}
|
|
|
|
|
2016-08-22 01:11:32 +02:00
|
|
|
void SetAlwaysOnTop(QWidget *window, bool enable)
|
2016-01-22 16:33:29 +01:00
|
|
|
{
|
|
|
|
Qt::WindowFlags flags = window->windowFlags();
|
|
|
|
|
2020-07-27 20:07:35 +02:00
|
|
|
if (enable) {
|
2023-04-02 18:56:44 +02:00
|
|
|
NSView *view = (__bridge NSView *) reinterpret_cast<void *>(window->winId());
|
|
|
|
|
|
|
|
[[view window] setLevel:NSScreenSaverWindowLevel];
|
2020-07-27 20:07:35 +02:00
|
|
|
|
2016-01-22 16:33:29 +01:00
|
|
|
flags |= Qt::WindowStaysOnTopHint;
|
2020-07-27 20:07:35 +02:00
|
|
|
} else {
|
2016-01-22 16:33:29 +01:00
|
|
|
flags &= ~Qt::WindowStaysOnTopHint;
|
2020-07-27 20:07:35 +02:00
|
|
|
}
|
2016-01-22 16:33:29 +01:00
|
|
|
|
|
|
|
window->setWindowFlags(flags);
|
|
|
|
window->show();
|
|
|
|
}
|
2016-01-26 02:09:59 +01:00
|
|
|
|
2020-06-27 02:31:08 +02:00
|
|
|
bool SetDisplayAffinitySupported(void)
|
|
|
|
{
|
|
|
|
// Not implemented yet
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-26 02:09:59 +01:00
|
|
|
typedef void (*set_int_t)(int);
|
|
|
|
|
|
|
|
void EnableOSXVSync(bool enable)
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
static bool valid = false;
|
|
|
|
static set_int_t set_debug_options = nullptr;
|
|
|
|
static set_int_t deferred_updates = nullptr;
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2016-01-26 02:09:59 +01:00
|
|
|
if (!initialized) {
|
|
|
|
void *quartzCore = dlopen("/System/Library/Frameworks/"
|
2019-07-09 20:29:39 +02:00
|
|
|
"QuartzCore.framework/QuartzCore",
|
|
|
|
RTLD_LAZY);
|
2016-01-26 02:09:59 +01:00
|
|
|
if (quartzCore) {
|
2019-07-09 20:29:39 +02:00
|
|
|
set_debug_options = (set_int_t) dlsym(quartzCore, "CGSSetDebugOptions");
|
|
|
|
deferred_updates = (set_int_t) dlsym(quartzCore, "CGSDeferredUpdates");
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2016-01-26 02:09:59 +01:00
|
|
|
valid = set_debug_options && deferred_updates;
|
|
|
|
}
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2016-01-26 02:09:59 +01:00
|
|
|
initialized = true;
|
|
|
|
}
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2016-01-26 02:09:59 +01:00
|
|
|
if (valid) {
|
|
|
|
set_debug_options(enable ? 0 : 0x08000000);
|
|
|
|
deferred_updates(enable ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 12:50:25 +02:00
|
|
|
|
|
|
|
void EnableOSXDockIcon(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
|
else
|
2019-07-09 20:29:39 +02:00
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
|
2018-08-17 12:50:25 +02:00
|
|
|
}
|
2020-02-09 09:00:07 +01:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
@interface DockView : NSView {
|
|
|
|
@private
|
2022-07-25 19:09:33 +02:00
|
|
|
QIcon _icon;
|
2022-03-01 13:43:29 +01:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DockView
|
2023-08-10 22:26:50 +02:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
- (id)initWithIcon:(QIcon)icon
|
|
|
|
{
|
|
|
|
self = [super init];
|
2022-07-25 19:09:33 +02:00
|
|
|
_icon = icon;
|
2022-03-01 13:43:29 +01:00
|
|
|
return self;
|
|
|
|
}
|
2023-08-10 22:26:50 +02:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
- (void)drawRect:(NSRect)dirtyRect
|
|
|
|
{
|
|
|
|
CGSize size = dirtyRect.size;
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
/* Draw regular app icon */
|
|
|
|
NSImage *appIcon = [[NSWorkspace sharedWorkspace] iconForFile:[[NSBundle mainBundle] bundlePath]];
|
|
|
|
[appIcon drawInRect:CGRectMake(0, 0, size.width, size.height)];
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
/* Draw small icon on top */
|
|
|
|
float iconSize = 0.45;
|
2022-07-25 19:09:33 +02:00
|
|
|
CGImageRef image = _icon.pixmap(size.width, size.height).toImage().toCGImage();
|
2022-03-01 13:43:29 +01:00
|
|
|
CGContextRef context = [[NSGraphicsContext currentContext] CGContext];
|
|
|
|
CGContextDrawImage(
|
|
|
|
context, CGRectMake(size.width * (1 - iconSize), 0, size.width * iconSize, size.height * iconSize), image);
|
|
|
|
CGImageRelease(image);
|
|
|
|
}
|
2023-08-10 22:26:50 +02:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
@end
|
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
MacPermissionStatus CheckPermissionWithPrompt(MacPermissionType type, bool prompt_for_permission)
|
|
|
|
{
|
|
|
|
__block MacPermissionStatus permissionResponse = kPermissionNotDetermined;
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
switch (type) {
|
|
|
|
case kAudioDeviceAccess: {
|
2022-07-28 19:02:22 +02:00
|
|
|
AVAuthorizationStatus audioStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-28 19:02:22 +02:00
|
|
|
if (audioStatus == AVAuthorizationStatusNotDetermined && prompt_for_permission) {
|
|
|
|
os_event_t *block_finished;
|
|
|
|
os_event_init(&block_finished, OS_EVENT_TYPE_MANUAL);
|
|
|
|
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio
|
|
|
|
completionHandler:^(BOOL granted __attribute((unused))) {
|
|
|
|
os_event_signal(block_finished);
|
|
|
|
}];
|
|
|
|
os_event_wait(block_finished);
|
|
|
|
os_event_destroy(block_finished);
|
2022-07-14 19:22:21 +02:00
|
|
|
audioStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
|
|
|
|
}
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-28 19:02:22 +02:00
|
|
|
permissionResponse = (MacPermissionStatus) audioStatus;
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
blog(LOG_INFO, "[macOS] Permission for audio device access %s.",
|
|
|
|
permissionResponse == kPermissionAuthorized ? "granted" : "denied");
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kVideoDeviceAccess: {
|
2022-07-28 19:02:22 +02:00
|
|
|
AVAuthorizationStatus videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-28 19:02:22 +02:00
|
|
|
if (videoStatus == AVAuthorizationStatusNotDetermined && prompt_for_permission) {
|
|
|
|
os_event_t *block_finished;
|
|
|
|
os_event_init(&block_finished, OS_EVENT_TYPE_MANUAL);
|
|
|
|
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
|
|
|
|
completionHandler:^(BOOL granted __attribute((unused))) {
|
|
|
|
os_event_signal(block_finished);
|
|
|
|
}];
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-28 19:02:22 +02:00
|
|
|
os_event_wait(block_finished);
|
|
|
|
os_event_destroy(block_finished);
|
2022-07-14 19:22:21 +02:00
|
|
|
videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
|
|
|
}
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-28 19:02:22 +02:00
|
|
|
permissionResponse = (MacPermissionStatus) videoStatus;
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
blog(LOG_INFO, "[macOS] Permission for video device access %s.",
|
|
|
|
permissionResponse == kPermissionAuthorized ? "granted" : "denied");
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kScreenCapture: {
|
2023-04-04 15:15:51 +02:00
|
|
|
permissionResponse = (CGPreflightScreenCaptureAccess() ? kPermissionAuthorized : kPermissionDenied);
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2023-04-04 15:15:51 +02:00
|
|
|
if (permissionResponse != kPermissionAuthorized && prompt_for_permission) {
|
|
|
|
permissionResponse = (CGRequestScreenCaptureAccess() ? kPermissionAuthorized : kPermissionDenied);
|
2022-07-14 19:22:21 +02:00
|
|
|
}
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
blog(LOG_INFO, "[macOS] Permission for screen capture %s.",
|
|
|
|
permissionResponse == kPermissionAuthorized ? "granted" : "denied");
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kAccessibility: {
|
|
|
|
permissionResponse = (AXIsProcessTrusted() ? kPermissionAuthorized : kPermissionDenied);
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
if (permissionResponse != kPermissionAuthorized && prompt_for_permission) {
|
|
|
|
NSDictionary *options = @{(__bridge id) kAXTrustedCheckOptionPrompt: @YES};
|
|
|
|
permissionResponse = (AXIsProcessTrustedWithOptions((CFDictionaryRef) options) ? kPermissionAuthorized
|
|
|
|
: kPermissionDenied);
|
|
|
|
}
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
blog(LOG_INFO, "[macOS] Permission for accessibility %s.",
|
|
|
|
permissionResponse == kPermissionAuthorized ? "granted" : "denied");
|
|
|
|
break;
|
2023-06-04 03:26:09 +02:00
|
|
|
}
|
2022-07-14 19:22:21 +02:00
|
|
|
}
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-07-14 19:22:21 +02:00
|
|
|
return permissionResponse;
|
|
|
|
}
|
|
|
|
|
2022-07-25 19:53:57 +02:00
|
|
|
void OpenMacOSPrivacyPreferences(const char *tab)
|
|
|
|
{
|
|
|
|
NSURL *url = [NSURL
|
|
|
|
URLWithString:[NSString
|
|
|
|
stringWithFormat:@"x-apple.systempreferences:com.apple.preference.security?Privacy_%s", tab]];
|
|
|
|
[[NSWorkspace sharedWorkspace] openURL:url];
|
|
|
|
}
|
|
|
|
|
2023-01-31 17:28:21 +01:00
|
|
|
void SetMacOSDarkMode(bool dark)
|
|
|
|
{
|
|
|
|
if (dark) {
|
|
|
|
NSApp.appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
|
|
|
|
} else {
|
|
|
|
NSApp.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:05:21 +02:00
|
|
|
void TaskbarOverlayInit() {}
|
2023-08-10 22:26:50 +02:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
void TaskbarOverlaySetStatus(TaskbarOverlayStatus status)
|
|
|
|
{
|
|
|
|
QIcon icon;
|
|
|
|
if (status == TaskbarOverlayStatusActive)
|
|
|
|
icon = QIcon::fromTheme("obs-active", QIcon(":/res/images/active_mac.png"));
|
|
|
|
else if (status == TaskbarOverlayStatusPaused)
|
|
|
|
icon = QIcon::fromTheme("obs-paused", QIcon(":/res/images/paused_mac.png"));
|
2023-06-04 03:26:09 +02:00
|
|
|
|
2022-03-01 13:43:29 +01:00
|
|
|
NSDockTile *tile = [NSApp dockTile];
|
|
|
|
[tile setContentView:[[DockView alloc] initWithIcon:icon]];
|
|
|
|
[tile display];
|
|
|
|
}
|
2022-04-23 18:05:21 +02:00
|
|
|
|
2020-02-09 09:00:07 +01:00
|
|
|
/*
|
|
|
|
* This custom NSApplication subclass makes the app compatible with CEF. Qt
|
|
|
|
* also has an NSApplication subclass, but it doesn't conflict thanks to Qt
|
|
|
|
* using arcane magic to hook into the NSApplication superclass itself if the
|
|
|
|
* program has its own NSApplication subclass.
|
|
|
|
*/
|
|
|
|
|
|
|
|
@protocol CrAppProtocol
|
|
|
|
- (BOOL)isHandlingSendEvent;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface OBSApplication : NSApplication <CrAppProtocol>
|
|
|
|
@property (nonatomic, getter=isHandlingSendEvent) BOOL handlingSendEvent;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OBSApplication
|
2023-08-10 22:26:50 +02:00
|
|
|
|
2020-02-09 09:00:07 +01:00
|
|
|
- (void)sendEvent:(NSEvent *)event
|
|
|
|
{
|
|
|
|
_handlingSendEvent = YES;
|
|
|
|
[super sendEvent:event];
|
|
|
|
_handlingSendEvent = NO;
|
|
|
|
}
|
2023-08-10 22:26:50 +02:00
|
|
|
|
2020-02-09 09:00:07 +01:00
|
|
|
@end
|
|
|
|
|
2022-07-21 18:35:44 +02:00
|
|
|
void InstallNSThreadLocks()
|
|
|
|
{
|
|
|
|
[[NSThread new] start];
|
|
|
|
|
|
|
|
if ([NSThread isMultiThreaded] != 1) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 09:00:07 +01:00
|
|
|
void InstallNSApplicationSubclass()
|
|
|
|
{
|
|
|
|
[OBSApplication sharedApplication];
|
|
|
|
}
|
2023-09-28 04:43:42 +02:00
|
|
|
|
|
|
|
bool HighContrastEnabled()
|
|
|
|
{
|
|
|
|
return [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldIncreaseContrast];
|
|
|
|
}
|