platform-osx.mm 6.4 KB

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