OBSApp.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. #pragma once
  15. #include <utility/OBSTheme.hpp>
  16. #include <utility/NativeEventFilter.hpp>
  17. #include <widgets/OBSMainWindow.hpp>
  18. #include <obs-frontend-api.h>
  19. #include <util/platform.h>
  20. #include <util/profiler.hpp>
  21. #include <util/util.hpp>
  22. #include <QAbstractNativeEventFilter>
  23. #include <QApplication>
  24. #include <QPalette>
  25. #include <QPointer>
  26. #include <QUuid>
  27. #include <deque>
  28. #include <functional>
  29. #include <string>
  30. #include <vector>
  31. typedef std::function<void()> VoidFunc;
  32. Q_DECLARE_METATYPE(VoidFunc)
  33. class QFileSystemWatcher;
  34. class QSocketNotifier;
  35. namespace OBS {
  36. class CrashHandler;
  37. enum class LogFileType { NoType, CurrentAppLog, LastAppLog, CrashLog };
  38. enum class LogFileState { NoState, New, Uploaded };
  39. class PluginManager;
  40. } // namespace OBS
  41. struct UpdateBranch {
  42. QString name;
  43. QString display_name;
  44. QString description;
  45. bool is_enabled;
  46. bool is_visible;
  47. };
  48. class OBSApp : public QApplication {
  49. Q_OBJECT
  50. friend class OBS::NativeEventFilter;
  51. private:
  52. QUuid appLaunchUUID_;
  53. std::unique_ptr<OBS::CrashHandler> crashHandler_;
  54. std::string locale;
  55. ConfigFile appConfig;
  56. ConfigFile userConfig;
  57. TextLookup textLookup;
  58. QPointer<OBSMainWindow> mainWindow;
  59. profiler_name_store_t *profilerNameStore = nullptr;
  60. std::vector<UpdateBranch> updateBranches;
  61. bool branches_loaded = false;
  62. bool libobs_initialized = false;
  63. os_inhibit_t *sleepInhibitor = nullptr;
  64. int sleepInhibitRefs = 0;
  65. bool enableHotkeysInFocus = true;
  66. bool enableHotkeysOutOfFocus = true;
  67. std::deque<obs_frontend_translate_ui_cb> translatorHooks;
  68. std::unique_ptr<OBS::PluginManager> pluginManager_;
  69. bool UpdatePre22MultiviewLayout(const char *layout);
  70. bool InitGlobalConfig();
  71. bool InitGlobalConfigDefaults();
  72. bool InitGlobalLocationDefaults();
  73. bool MigrateGlobalSettings();
  74. void MigrateLegacySettings(uint32_t lastVersion);
  75. bool InitUserConfig(std::filesystem::path &userConfigLocation, uint32_t lastVersion);
  76. void InitUserConfigDefaults();
  77. bool InitLocale();
  78. bool InitTheme();
  79. inline void ResetHotkeyState(bool inFocus);
  80. QPalette defaultPalette;
  81. OBSTheme *currentTheme = nullptr;
  82. QHash<QString, OBSTheme> themes;
  83. QPointer<QFileSystemWatcher> themeWatcher;
  84. std::unique_ptr<QStyle> invisibleCursorStyle;
  85. void FindThemes();
  86. bool notify(QObject *receiver, QEvent *e) override;
  87. #ifndef _WIN32
  88. static int sigintFd[2];
  89. QSocketNotifier *snInt = nullptr;
  90. static int sigtermFd[2];
  91. QSocketNotifier *snTerm = nullptr;
  92. #endif
  93. private slots:
  94. void commitData(QSessionManager &manager);
  95. void addLogLine(int logLevel, const QString &message);
  96. void themeFileChanged(const QString &);
  97. void applicationShutdown() noexcept;
  98. public:
  99. OBSApp(int &argc, char **argv, profiler_name_store_t *store);
  100. ~OBSApp();
  101. void AppInit();
  102. void checkForUncleanShutdown();
  103. bool OBSInit();
  104. void UpdateHotkeyFocusSetting(bool reset = true);
  105. void DisableHotkeys();
  106. inline bool HotkeysEnabledInFocus() const { return enableHotkeysInFocus; }
  107. inline QMainWindow *GetMainWindow() const { return mainWindow.data(); }
  108. inline config_t *GetAppConfig() const { return appConfig; }
  109. inline config_t *GetUserConfig() const { return userConfig; }
  110. std::filesystem::path userConfigLocation;
  111. std::filesystem::path userScenesLocation;
  112. std::filesystem::path userProfilesLocation;
  113. std::filesystem::path userPluginManagerSettingsLocation;
  114. inline const char *GetLocale() const { return locale.c_str(); }
  115. OBSTheme *GetTheme() const { return currentTheme; }
  116. QList<OBSTheme> GetThemes() const { return themes.values(); }
  117. OBSTheme *GetTheme(const QString &name);
  118. bool SetTheme(const QString &name);
  119. bool IsThemeDark() const { return currentTheme ? currentTheme->isDark : false; }
  120. QStyle *GetInvisibleCursorStyle();
  121. void SetBranchData(const std::string &data);
  122. std::vector<UpdateBranch> GetBranches();
  123. inline lookup_t *GetTextLookup() const { return textLookup; }
  124. inline const char *GetString(const char *lookupVal) const { return textLookup.GetString(lookupVal); }
  125. bool TranslateString(const char *lookupVal, const char **out) const;
  126. profiler_name_store_t *GetProfilerNameStore() const { return profilerNameStore; }
  127. const char *GetLastLog() const;
  128. const char *GetCurrentLog() const;
  129. void openCrashLogDirectory() const;
  130. void uploadLastAppLog() const;
  131. void uploadCurrentAppLog() const;
  132. void uploadLastCrashLog();
  133. OBS::LogFileState getLogFileState(OBS::LogFileType type) const;
  134. std::string GetVersionString(bool platform = true) const;
  135. bool IsPortableMode();
  136. bool IsUpdaterDisabled();
  137. bool IsMissingFilesCheckDisabled();
  138. const char *InputAudioSource() const;
  139. const char *OutputAudioSource() const;
  140. const char *GetRenderModule() const;
  141. inline void IncrementSleepInhibition()
  142. {
  143. if (!sleepInhibitor)
  144. return;
  145. if (sleepInhibitRefs++ == 0)
  146. os_inhibit_sleep_set_active(sleepInhibitor, true);
  147. }
  148. inline void DecrementSleepInhibition()
  149. {
  150. if (!sleepInhibitor)
  151. return;
  152. if (sleepInhibitRefs == 0)
  153. return;
  154. if (--sleepInhibitRefs == 0)
  155. os_inhibit_sleep_set_active(sleepInhibitor, false);
  156. }
  157. inline void PushUITranslation(obs_frontend_translate_ui_cb cb) { translatorHooks.emplace_front(cb); }
  158. inline void PopUITranslation() { translatorHooks.pop_front(); }
  159. #ifndef _WIN32
  160. static void SigIntSignalHandler(int);
  161. static void SigTermSignalHandler(int);
  162. #endif
  163. void loadAppModules(struct obs_module_failure_info &mfi);
  164. // Plugin Manager Accessors
  165. void pluginManagerOpenDialog();
  166. public slots:
  167. void Exec(VoidFunc func);
  168. void ProcessSigInt();
  169. void ProcessSigTerm();
  170. signals:
  171. void logLineAdded(int logLevel, const QString &message);
  172. void StyleChanged();
  173. void logUploadFinished(OBS::LogFileType, const QString &fileUrl);
  174. void logUploadFailed(OBS::LogFileType, const QString &errorMessage);
  175. };
  176. int GetAppConfigPath(char *path, size_t size, const char *name);
  177. char *GetAppConfigPathPtr(const char *name);
  178. inline OBSApp *App()
  179. {
  180. return static_cast<OBSApp *>(qApp);
  181. }
  182. std::vector<std::pair<std::string, std::string>> GetLocaleNames();
  183. inline const char *Str(const char *lookup)
  184. {
  185. return App()->GetString(lookup);
  186. }
  187. inline QString QTStr(const char *lookupVal)
  188. {
  189. return QString::fromUtf8(Str(lookupVal));
  190. }
  191. int GetProgramDataPath(char *path, size_t size, const char *name);
  192. char *GetProgramDataPathPtr(const char *name);
  193. bool GetFileSafeName(const char *name, std::string &file);
  194. bool GetClosestUnusedFileName(std::string &path, const char *extension);
  195. bool WindowPositionValid(QRect rect);
  196. #ifdef _WIN32
  197. extern "C" void install_dll_blocklist_hook(void);
  198. extern "C" void log_blocked_dlls(void);
  199. #endif
  200. std::string CurrentDateTimeString();
  201. std::string GetFormatString(const char *format, const char *prefix, const char *suffix);
  202. std::string GenerateTimeDateFilename(const char *extension, bool noSpace = false);
  203. std::string GetFormatExt(const char *container);
  204. std::string GetOutputFilename(const char *path, const char *container, bool noSpace, bool overwrite,
  205. const char *format);
  206. QObject *CreateShortcutFilter();