platform-osx.mm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <util/base.h>
  16. #include <obs-config.h>
  17. #include "platform.hpp"
  18. #include "obs-app.hpp"
  19. #include <unistd.h>
  20. #import <AppKit/AppKit.h>
  21. using namespace std;
  22. bool GetDataFilePath(const char *data, string &output)
  23. {
  24. stringstream str;
  25. str << OBS_DATA_PATH "/obs-studio/" << data;
  26. output = str.str();
  27. return !access(output.c_str(), R_OK);
  28. }
  29. void GetMonitors(vector<MonitorInfo> &monitors)
  30. {
  31. monitors.clear();
  32. for(NSScreen *screen : [NSScreen screens])
  33. {
  34. NSRect frame = [screen convertRectToBacking:[screen frame]];
  35. monitors.emplace_back(frame.origin.x, frame.origin.y,
  36. frame.size.width, frame.size.height);
  37. }
  38. }
  39. bool InitApplicationBundle()
  40. {
  41. #ifdef OBS_OSX_BUNDLE
  42. static bool initialized = false;
  43. if (initialized)
  44. return true;
  45. try {
  46. NSBundle *bundle = [NSBundle mainBundle];
  47. if (!bundle)
  48. throw "Could not find main bundle";
  49. NSString *exe_path = [bundle executablePath];
  50. if (!exe_path)
  51. throw "Could not find executable path";
  52. NSString *path = [exe_path stringByDeletingLastPathComponent];
  53. if (chdir([path fileSystemRepresentation]))
  54. throw "Could not change working directory to "
  55. "bundle path";
  56. } catch (const char* error) {
  57. blog(LOG_ERROR, "InitBundle: %s", error);
  58. return false;
  59. }
  60. return initialized = true;
  61. #else
  62. return true;
  63. #endif
  64. }
  65. string GetDefaultVideoSavePath()
  66. {
  67. NSFileManager *fm = [NSFileManager defaultManager];
  68. NSURL *url = [fm URLForDirectory:NSMoviesDirectory
  69. inDomain:NSUserDomainMask
  70. appropriateForURL:nil
  71. create:true
  72. error:nil];
  73. if (!url)
  74. return getenv("HOME");
  75. return url.path.fileSystemRepresentation;
  76. }
  77. vector<string> GetPreferredLocales()
  78. {
  79. NSArray *preferred = [NSLocale preferredLanguages];
  80. auto locales = GetLocaleNames();
  81. auto lang_to_locale = [&locales](string lang) -> string {
  82. string lang_match = "";
  83. for (const auto &locale : locales) {
  84. if (locale.first == lang.substr(0, locale.first.size()))
  85. return locale.first;
  86. if (!lang_match.size() &&
  87. locale.first.substr(0, 2) == lang.substr(0, 2))
  88. lang_match = locale.first;
  89. }
  90. return lang_match;
  91. };
  92. vector<string> result;
  93. result.reserve(preferred.count);
  94. for (NSString *lang in preferred) {
  95. string locale = lang_to_locale(lang.UTF8String);
  96. if (!locale.size())
  97. continue;
  98. if (find(begin(result), end(result), locale) != end(result))
  99. continue;
  100. result.emplace_back(locale);
  101. }
  102. return result;
  103. }
  104. bool IsAlwaysOnTop(QMainWindow *window)
  105. {
  106. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  107. }
  108. void SetAlwaysOnTop(QMainWindow *window, bool enable)
  109. {
  110. Qt::WindowFlags flags = window->windowFlags();
  111. if (enable)
  112. flags |= Qt::WindowStaysOnTopHint;
  113. else
  114. flags &= ~Qt::WindowStaysOnTopHint;
  115. window->setWindowFlags(flags);
  116. window->show();
  117. }