OBSApp.hpp 6.4 KB

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