OBSApp.hpp 7.0 KB

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