OBSApp.hpp 7.5 KB

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