platform-osx.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 GetDataFilePath(const char *data, string &output)
  24. {
  25. stringstream str;
  26. str << OBS_DATA_PATH "/obs-studio/" << data;
  27. output = str.str();
  28. return !access(output.c_str(), R_OK);
  29. }
  30. void GetMonitors(vector<MonitorInfo> &monitors)
  31. {
  32. monitors.clear();
  33. for(NSScreen *screen : [NSScreen screens])
  34. {
  35. NSRect frame = [screen convertRectToBacking:[screen frame]];
  36. monitors.emplace_back(frame.origin.x, frame.origin.y,
  37. frame.size.width, frame.size.height);
  38. }
  39. }
  40. bool InitApplicationBundle()
  41. {
  42. #ifdef OBS_OSX_BUNDLE
  43. static bool initialized = false;
  44. if (initialized)
  45. return true;
  46. try {
  47. NSBundle *bundle = [NSBundle mainBundle];
  48. if (!bundle)
  49. throw "Could not find main bundle";
  50. NSString *exe_path = [bundle executablePath];
  51. if (!exe_path)
  52. throw "Could not find executable path";
  53. NSString *path = [exe_path stringByDeletingLastPathComponent];
  54. if (chdir([path fileSystemRepresentation]))
  55. throw "Could not change working directory to "
  56. "bundle path";
  57. } catch (const char* error) {
  58. blog(LOG_ERROR, "InitBundle: %s", error);
  59. return false;
  60. }
  61. return initialized = true;
  62. #else
  63. return true;
  64. #endif
  65. }
  66. string GetDefaultVideoSavePath()
  67. {
  68. NSFileManager *fm = [NSFileManager defaultManager];
  69. NSURL *url = [fm URLForDirectory:NSMoviesDirectory
  70. inDomain:NSUserDomainMask
  71. appropriateForURL:nil
  72. create:true
  73. error:nil];
  74. if (!url)
  75. return getenv("HOME");
  76. return url.path.fileSystemRepresentation;
  77. }
  78. vector<string> GetPreferredLocales()
  79. {
  80. NSArray *preferred = [NSLocale preferredLanguages];
  81. auto locales = GetLocaleNames();
  82. auto lang_to_locale = [&locales](string lang) -> string {
  83. string lang_match = "";
  84. for (const auto &locale : locales) {
  85. if (locale.first == lang.substr(0, locale.first.size()))
  86. return locale.first;
  87. if (!lang_match.size() &&
  88. locale.first.substr(0, 2) == lang.substr(0, 2))
  89. lang_match = locale.first;
  90. }
  91. return lang_match;
  92. };
  93. vector<string> result;
  94. result.reserve(preferred.count);
  95. for (NSString *lang in preferred) {
  96. string locale = lang_to_locale(lang.UTF8String);
  97. if (!locale.size())
  98. continue;
  99. if (find(begin(result), end(result), locale) != end(result))
  100. continue;
  101. result.emplace_back(locale);
  102. }
  103. return result;
  104. }
  105. bool IsAlwaysOnTop(QMainWindow *window)
  106. {
  107. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  108. }
  109. void SetAlwaysOnTop(QMainWindow *window, bool enable)
  110. {
  111. Qt::WindowFlags flags = window->windowFlags();
  112. if (enable)
  113. flags |= Qt::WindowStaysOnTopHint;
  114. else
  115. flags &= ~Qt::WindowStaysOnTopHint;
  116. window->setWindowFlags(flags);
  117. window->show();
  118. }
  119. typedef void (*set_int_t)(int);
  120. void EnableOSXVSync(bool enable)
  121. {
  122. static bool initialized = false;
  123. static bool valid = false;
  124. static set_int_t set_debug_options = nullptr;
  125. static set_int_t deferred_updates = nullptr;
  126. if (!initialized) {
  127. void *quartzCore = dlopen("/System/Library/Frameworks/"
  128. "QuartzCore.framework/QuartzCore", RTLD_LAZY);
  129. if (quartzCore) {
  130. set_debug_options = (set_int_t)dlsym(quartzCore,
  131. "CGSSetDebugOptions");
  132. deferred_updates = (set_int_t)dlsym(quartzCore,
  133. "CGSDeferredUpdates");
  134. valid = set_debug_options && deferred_updates;
  135. }
  136. initialized = true;
  137. }
  138. if (valid) {
  139. set_debug_options(enable ? 0 : 0x08000000);
  140. deferred_updates(enable ? 1 : 0);
  141. }
  142. }