platform-osx.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. NSRunningApplication *app = [NSRunningApplication currentApplication];
  32. NSURL *bundleURL = [app bundleURL];
  33. NSString *path = [NSString
  34. stringWithFormat:@"Contents/Resources/%@",
  35. [NSString stringWithUTF8String:data]];
  36. NSURL *dataURL = [bundleURL URLByAppendingPathComponent:path];
  37. output = [[dataURL path] UTF8String];
  38. return !access(output.c_str(), R_OK);
  39. }
  40. void CheckIfAlreadyRunning(bool &already_running)
  41. {
  42. try {
  43. NSBundle *bundle = [NSBundle mainBundle];
  44. if (!bundle)
  45. throw "Could not find main bundle";
  46. NSString *bundleID = [bundle bundleIdentifier];
  47. if (!bundleID)
  48. throw "Could not find bundle identifier";
  49. int app_count =
  50. [NSRunningApplication
  51. runningApplicationsWithBundleIdentifier:bundleID]
  52. .count;
  53. already_running = app_count > 1;
  54. } catch (const char *error) {
  55. blog(LOG_ERROR, "CheckIfAlreadyRunning: %s", error);
  56. }
  57. }
  58. string GetDefaultVideoSavePath()
  59. {
  60. NSFileManager *fm = [NSFileManager defaultManager];
  61. NSURL *url = [fm URLForDirectory:NSMoviesDirectory
  62. inDomain:NSUserDomainMask
  63. appropriateForURL:nil
  64. create:true
  65. error:nil];
  66. if (!url)
  67. return getenv("HOME");
  68. return url.path.fileSystemRepresentation;
  69. }
  70. vector<string> GetPreferredLocales()
  71. {
  72. NSArray *preferred = [NSLocale preferredLanguages];
  73. auto locales = GetLocaleNames();
  74. auto lang_to_locale = [&locales](string lang) -> string {
  75. string lang_match = "";
  76. for (const auto &locale : locales) {
  77. if (locale.first == lang.substr(0, locale.first.size()))
  78. return locale.first;
  79. if (!lang_match.size() &&
  80. locale.first.substr(0, 2) == lang.substr(0, 2))
  81. lang_match = locale.first;
  82. }
  83. return lang_match;
  84. };
  85. vector<string> result;
  86. result.reserve(preferred.count);
  87. for (NSString *lang in preferred) {
  88. string locale = lang_to_locale(lang.UTF8String);
  89. if (!locale.size())
  90. continue;
  91. if (find(begin(result), end(result), locale) != end(result))
  92. continue;
  93. result.emplace_back(locale);
  94. }
  95. return result;
  96. }
  97. bool IsAlwaysOnTop(QWidget *window)
  98. {
  99. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  100. }
  101. void disableColorSpaceConversion(QWidget *window)
  102. {
  103. NSView *view =
  104. (__bridge NSView *)reinterpret_cast<void *>(window->winId());
  105. view.window.colorSpace = NSColorSpace.sRGBColorSpace;
  106. }
  107. void SetAlwaysOnTop(QWidget *window, bool enable)
  108. {
  109. Qt::WindowFlags flags = window->windowFlags();
  110. if (enable) {
  111. /* Force the level of the window high so it sits on top of
  112. * full-screen applications like Keynote */
  113. NSView *nsv = (__bridge NSView *)reinterpret_cast<void *>(
  114. window->winId());
  115. NSWindow *nsw = nsv.window;
  116. [nsw setLevel:1024];
  117. flags |= Qt::WindowStaysOnTopHint;
  118. } else {
  119. flags &= ~Qt::WindowStaysOnTopHint;
  120. }
  121. window->setWindowFlags(flags);
  122. window->show();
  123. }
  124. bool SetDisplayAffinitySupported(void)
  125. {
  126. // Not implemented yet
  127. return false;
  128. }
  129. typedef void (*set_int_t)(int);
  130. void EnableOSXVSync(bool enable)
  131. {
  132. static bool initialized = false;
  133. static bool valid = false;
  134. static set_int_t set_debug_options = nullptr;
  135. static set_int_t deferred_updates = nullptr;
  136. if (!initialized) {
  137. void *quartzCore = dlopen("/System/Library/Frameworks/"
  138. "QuartzCore.framework/QuartzCore",
  139. RTLD_LAZY);
  140. if (quartzCore) {
  141. set_debug_options = (set_int_t)dlsym(
  142. quartzCore, "CGSSetDebugOptions");
  143. deferred_updates = (set_int_t)dlsym(
  144. quartzCore, "CGSDeferredUpdates");
  145. valid = set_debug_options && deferred_updates;
  146. }
  147. initialized = true;
  148. }
  149. if (valid) {
  150. set_debug_options(enable ? 0 : 0x08000000);
  151. deferred_updates(enable ? 1 : 0);
  152. }
  153. }
  154. void EnableOSXDockIcon(bool enable)
  155. {
  156. if (enable)
  157. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  158. else
  159. [NSApp setActivationPolicy:
  160. NSApplicationActivationPolicyProhibited];
  161. }
  162. bool ProcessIsRosettaTranslated()
  163. {
  164. #ifdef __aarch64__
  165. return false;
  166. #else
  167. int ret = 0;
  168. size_t size = sizeof(ret);
  169. if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1)
  170. return false;
  171. return ret == 1;
  172. #endif
  173. }
  174. // Not implemented yet
  175. void TaskbarOverlayInit() {}
  176. void TaskbarOverlaySetStatus(TaskbarOverlayStatus) {}
  177. /*
  178. * This custom NSApplication subclass makes the app compatible with CEF. Qt
  179. * also has an NSApplication subclass, but it doesn't conflict thanks to Qt
  180. * using arcane magic to hook into the NSApplication superclass itself if the
  181. * program has its own NSApplication subclass.
  182. */
  183. @protocol CrAppProtocol
  184. - (BOOL)isHandlingSendEvent;
  185. @end
  186. @interface OBSApplication : NSApplication <CrAppProtocol>
  187. @property (nonatomic, getter=isHandlingSendEvent) BOOL handlingSendEvent;
  188. @end
  189. @implementation OBSApplication
  190. - (void)sendEvent:(NSEvent *)event
  191. {
  192. _handlingSendEvent = YES;
  193. [super sendEvent:event];
  194. _handlingSendEvent = NO;
  195. }
  196. @end
  197. void InstallNSApplicationSubclass()
  198. {
  199. [OBSApplication sharedApplication];
  200. }