platform-osx.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <sstream>
  15. #include <dlfcn.h>
  16. #include <util/base.h>
  17. #include <obs-config.h>
  18. #include "platform.hpp"
  19. #include "obs-app.hpp"
  20. #include <unistd.h>
  21. #include <sys/sysctl.h>
  22. #import <AppKit/AppKit.h>
  23. using namespace std;
  24. bool isInBundle()
  25. {
  26. NSRunningApplication *app = [NSRunningApplication currentApplication];
  27. return [app bundleIdentifier] != nil;
  28. }
  29. bool GetDataFilePath(const char *data, string &output)
  30. {
  31. if (isInBundle()) {
  32. NSRunningApplication *app =
  33. [NSRunningApplication currentApplication];
  34. NSURL *bundleURL = [app bundleURL];
  35. NSString *path = [NSString
  36. stringWithFormat:@"Contents/Resources/data/obs-studio/%@",
  37. [NSString stringWithUTF8String:data]];
  38. NSURL *dataURL = [bundleURL URLByAppendingPathComponent:path];
  39. output = [[dataURL path] UTF8String];
  40. } else {
  41. stringstream str;
  42. str << OBS_DATA_PATH "/obs-studio/" << data;
  43. output = str.str();
  44. }
  45. return !access(output.c_str(), R_OK);
  46. }
  47. bool InitApplicationBundle()
  48. {
  49. #ifdef OBS_OSX_BUNDLE
  50. static bool initialized = false;
  51. if (initialized)
  52. return true;
  53. try {
  54. NSBundle *bundle = [NSBundle mainBundle];
  55. if (!bundle)
  56. throw "Could not find main bundle";
  57. NSString *exe_path = [bundle executablePath];
  58. if (!exe_path)
  59. throw "Could not find executable path";
  60. NSString *path = [exe_path stringByDeletingLastPathComponent];
  61. if (chdir([path fileSystemRepresentation]))
  62. throw "Could not change working directory to "
  63. "bundle path";
  64. } catch (const char *error) {
  65. blog(LOG_ERROR, "InitBundle: %s", error);
  66. return false;
  67. }
  68. return initialized = true;
  69. #else
  70. return true;
  71. #endif
  72. }
  73. void CheckAppWithSameBundleID(bool &already_running)
  74. {
  75. try {
  76. NSBundle *bundle = [NSBundle mainBundle];
  77. if (!bundle)
  78. throw "Could not find main bundle";
  79. NSString *bundleID = [bundle bundleIdentifier];
  80. if (!bundleID)
  81. throw "Could not find bundle identifier";
  82. int app_count =
  83. [NSRunningApplication
  84. runningApplicationsWithBundleIdentifier:bundleID]
  85. .count;
  86. already_running = app_count > 1;
  87. } catch (const char *error) {
  88. blog(LOG_ERROR, "CheckAppWithSameBundleID: %s", error);
  89. }
  90. }
  91. string GetDefaultVideoSavePath()
  92. {
  93. NSFileManager *fm = [NSFileManager defaultManager];
  94. NSURL *url = [fm URLForDirectory:NSMoviesDirectory
  95. inDomain:NSUserDomainMask
  96. appropriateForURL:nil
  97. create:true
  98. error:nil];
  99. if (!url)
  100. return getenv("HOME");
  101. return url.path.fileSystemRepresentation;
  102. }
  103. vector<string> GetPreferredLocales()
  104. {
  105. NSArray *preferred = [NSLocale preferredLanguages];
  106. auto locales = GetLocaleNames();
  107. auto lang_to_locale = [&locales](string lang) -> string {
  108. string lang_match = "";
  109. for (const auto &locale : locales) {
  110. if (locale.first == lang.substr(0, locale.first.size()))
  111. return locale.first;
  112. if (!lang_match.size() &&
  113. locale.first.substr(0, 2) == lang.substr(0, 2))
  114. lang_match = locale.first;
  115. }
  116. return lang_match;
  117. };
  118. vector<string> result;
  119. result.reserve(preferred.count);
  120. for (NSString *lang in preferred) {
  121. string locale = lang_to_locale(lang.UTF8String);
  122. if (!locale.size())
  123. continue;
  124. if (find(begin(result), end(result), locale) != end(result))
  125. continue;
  126. result.emplace_back(locale);
  127. }
  128. return result;
  129. }
  130. bool IsAlwaysOnTop(QWidget *window)
  131. {
  132. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  133. }
  134. void disableColorSpaceConversion(QWidget *window)
  135. {
  136. NSView *view =
  137. (__bridge NSView *)reinterpret_cast<void *>(window->winId());
  138. view.window.colorSpace = NSColorSpace.sRGBColorSpace;
  139. }
  140. void SetAlwaysOnTop(QWidget *window, bool enable)
  141. {
  142. Qt::WindowFlags flags = window->windowFlags();
  143. if (enable) {
  144. /* Force the level of the window high so it sits on top of
  145. * full-screen applications like Keynote */
  146. NSView *nsv = (__bridge NSView *)reinterpret_cast<void *>(
  147. window->winId());
  148. NSWindow *nsw = nsv.window;
  149. [nsw setLevel:1024];
  150. flags |= Qt::WindowStaysOnTopHint;
  151. } else {
  152. flags &= ~Qt::WindowStaysOnTopHint;
  153. }
  154. window->setWindowFlags(flags);
  155. window->show();
  156. }
  157. typedef void (*set_int_t)(int);
  158. void EnableOSXVSync(bool enable)
  159. {
  160. static bool initialized = false;
  161. static bool valid = false;
  162. static set_int_t set_debug_options = nullptr;
  163. static set_int_t deferred_updates = nullptr;
  164. if (!initialized) {
  165. void *quartzCore = dlopen("/System/Library/Frameworks/"
  166. "QuartzCore.framework/QuartzCore",
  167. RTLD_LAZY);
  168. if (quartzCore) {
  169. set_debug_options = (set_int_t)dlsym(
  170. quartzCore, "CGSSetDebugOptions");
  171. deferred_updates = (set_int_t)dlsym(
  172. quartzCore, "CGSDeferredUpdates");
  173. valid = set_debug_options && deferred_updates;
  174. }
  175. initialized = true;
  176. }
  177. if (valid) {
  178. set_debug_options(enable ? 0 : 0x08000000);
  179. deferred_updates(enable ? 1 : 0);
  180. }
  181. }
  182. void EnableOSXDockIcon(bool enable)
  183. {
  184. if (enable)
  185. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  186. else
  187. [NSApp setActivationPolicy:
  188. NSApplicationActivationPolicyProhibited];
  189. }
  190. bool ProcessIsRosettaTranslated()
  191. {
  192. #ifdef __aarch64__
  193. return false;
  194. #else
  195. int ret = 0;
  196. size_t size = sizeof(ret);
  197. if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1)
  198. return false;
  199. return ret == 1;
  200. #endif
  201. }
  202. /*
  203. * This custom NSApplication subclass makes the app compatible with CEF. Qt
  204. * also has an NSApplication subclass, but it doesn't conflict thanks to Qt
  205. * using arcane magic to hook into the NSApplication superclass itself if the
  206. * program has its own NSApplication subclass.
  207. */
  208. @protocol CrAppProtocol
  209. - (BOOL)isHandlingSendEvent;
  210. @end
  211. @interface OBSApplication : NSApplication <CrAppProtocol>
  212. @property (nonatomic, getter=isHandlingSendEvent) BOOL handlingSendEvent;
  213. @end
  214. @implementation OBSApplication
  215. - (void)sendEvent:(NSEvent *)event
  216. {
  217. _handlingSendEvent = YES;
  218. [super sendEvent:event];
  219. _handlingSendEvent = NO;
  220. }
  221. @end
  222. void InstallNSApplicationSubclass()
  223. {
  224. [OBSApplication sharedApplication];
  225. }