obs-app.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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 <QApplication>
  16. #include <QTranslator>
  17. #include <QPointer>
  18. #include <QFileSystemWatcher>
  19. #ifndef _WIN32
  20. #include <QSocketNotifier>
  21. #else
  22. #include <QSessionManager>
  23. #endif
  24. #include <obs.hpp>
  25. #include <util/lexer.h>
  26. #include <util/profiler.h>
  27. #include <util/util.hpp>
  28. #include <util/platform.h>
  29. #include <obs-frontend-api.h>
  30. #include <functional>
  31. #include <string>
  32. #include <memory>
  33. #include <vector>
  34. #include <deque>
  35. #include <filesystem>
  36. #include "window-main.hpp"
  37. #include "obs-app-theming.hpp"
  38. std::string CurrentTimeString();
  39. std::string CurrentDateTimeString();
  40. std::string GenerateTimeDateFilename(const char *extension, bool noSpace = false);
  41. std::string GenerateSpecifiedFilename(const char *extension, bool noSpace, const char *format);
  42. std::string GetFormatString(const char *format, const char *prefix, const char *suffix);
  43. std::string GetFormatExt(const char *container);
  44. std::string GetOutputFilename(const char *path, const char *container, bool noSpace, bool overwrite,
  45. const char *format);
  46. QObject *CreateShortcutFilter();
  47. struct BaseLexer {
  48. lexer lex;
  49. public:
  50. inline BaseLexer() { lexer_init(&lex); }
  51. inline ~BaseLexer() { lexer_free(&lex); }
  52. operator lexer *() { return &lex; }
  53. };
  54. class OBSTranslator : public QTranslator {
  55. Q_OBJECT
  56. public:
  57. virtual bool isEmpty() const override { return false; }
  58. virtual QString translate(const char *context, const char *sourceText, const char *disambiguation,
  59. int n) const override;
  60. };
  61. typedef std::function<void()> VoidFunc;
  62. struct UpdateBranch {
  63. QString name;
  64. QString display_name;
  65. QString description;
  66. bool is_enabled;
  67. bool is_visible;
  68. };
  69. class OBSApp : public QApplication {
  70. Q_OBJECT
  71. private:
  72. std::string locale;
  73. ConfigFile appConfig;
  74. ConfigFile userConfig;
  75. TextLookup textLookup;
  76. QPointer<OBSMainWindow> mainWindow;
  77. profiler_name_store_t *profilerNameStore = nullptr;
  78. std::vector<UpdateBranch> updateBranches;
  79. bool branches_loaded = false;
  80. bool libobs_initialized = false;
  81. os_inhibit_t *sleepInhibitor = nullptr;
  82. int sleepInhibitRefs = 0;
  83. bool enableHotkeysInFocus = true;
  84. bool enableHotkeysOutOfFocus = true;
  85. std::deque<obs_frontend_translate_ui_cb> translatorHooks;
  86. bool UpdatePre22MultiviewLayout(const char *layout);
  87. bool InitGlobalConfig();
  88. bool InitGlobalConfigDefaults();
  89. bool InitGlobalLocationDefaults();
  90. bool MigrateGlobalSettings();
  91. void MigrateLegacySettings(uint32_t lastVersion);
  92. bool InitUserConfig(std::filesystem::path &userConfigLocation, uint32_t lastVersion);
  93. void InitUserConfigDefaults();
  94. bool InitLocale();
  95. bool InitTheme();
  96. inline void ResetHotkeyState(bool inFocus);
  97. QPalette defaultPalette;
  98. OBSTheme *currentTheme = nullptr;
  99. QHash<QString, OBSTheme> themes;
  100. QPointer<QFileSystemWatcher> themeWatcher;
  101. void FindThemes();
  102. bool notify(QObject *receiver, QEvent *e) override;
  103. #ifndef _WIN32
  104. static int sigintFd[2];
  105. QSocketNotifier *snInt = nullptr;
  106. #else
  107. private slots:
  108. void commitData(QSessionManager &manager);
  109. #endif
  110. private slots:
  111. void themeFileChanged(const QString &);
  112. public:
  113. OBSApp(int &argc, char **argv, profiler_name_store_t *store);
  114. ~OBSApp();
  115. void AppInit();
  116. bool OBSInit();
  117. void UpdateHotkeyFocusSetting(bool reset = true);
  118. void DisableHotkeys();
  119. inline bool HotkeysEnabledInFocus() const { return enableHotkeysInFocus; }
  120. inline QMainWindow *GetMainWindow() const { return mainWindow.data(); }
  121. inline config_t *GetAppConfig() const { return appConfig; }
  122. inline config_t *GetUserConfig() const { return userConfig; }
  123. std::filesystem::path userConfigLocation;
  124. std::filesystem::path userScenesLocation;
  125. std::filesystem::path userProfilesLocation;
  126. inline const char *GetLocale() const { return locale.c_str(); }
  127. OBSTheme *GetTheme() const { return currentTheme; }
  128. QList<OBSTheme> GetThemes() const { return themes.values(); }
  129. OBSTheme *GetTheme(const QString &name);
  130. bool SetTheme(const QString &name);
  131. bool IsThemeDark() const { return currentTheme ? currentTheme->isDark : false; }
  132. void SetBranchData(const std::string &data);
  133. std::vector<UpdateBranch> GetBranches();
  134. inline lookup_t *GetTextLookup() const { return textLookup; }
  135. inline const char *GetString(const char *lookupVal) const { return textLookup.GetString(lookupVal); }
  136. bool TranslateString(const char *lookupVal, const char **out) const;
  137. profiler_name_store_t *GetProfilerNameStore() const { return profilerNameStore; }
  138. const char *GetLastLog() const;
  139. const char *GetCurrentLog() const;
  140. const char *GetLastCrashLog() const;
  141. std::string GetVersionString(bool platform = true) const;
  142. bool IsPortableMode();
  143. bool IsUpdaterDisabled();
  144. bool IsMissingFilesCheckDisabled();
  145. const char *InputAudioSource() const;
  146. const char *OutputAudioSource() const;
  147. const char *GetRenderModule() const;
  148. inline void IncrementSleepInhibition()
  149. {
  150. if (!sleepInhibitor)
  151. return;
  152. if (sleepInhibitRefs++ == 0)
  153. os_inhibit_sleep_set_active(sleepInhibitor, true);
  154. }
  155. inline void DecrementSleepInhibition()
  156. {
  157. if (!sleepInhibitor)
  158. return;
  159. if (sleepInhibitRefs == 0)
  160. return;
  161. if (--sleepInhibitRefs == 0)
  162. os_inhibit_sleep_set_active(sleepInhibitor, false);
  163. }
  164. inline void PushUITranslation(obs_frontend_translate_ui_cb cb) { translatorHooks.emplace_front(cb); }
  165. inline void PopUITranslation() { translatorHooks.pop_front(); }
  166. #ifndef _WIN32
  167. static void SigIntSignalHandler(int);
  168. #endif
  169. public slots:
  170. void Exec(VoidFunc func);
  171. void ProcessSigInt();
  172. signals:
  173. void StyleChanged();
  174. };
  175. int GetAppConfigPath(char *path, size_t size, const char *name);
  176. char *GetAppConfigPathPtr(const char *name);
  177. int GetProgramDataPath(char *path, size_t size, const char *name);
  178. char *GetProgramDataPathPtr(const char *name);
  179. inline OBSApp *App()
  180. {
  181. return static_cast<OBSApp *>(qApp);
  182. }
  183. std::vector<std::pair<std::string, std::string>> GetLocaleNames();
  184. inline const char *Str(const char *lookup)
  185. {
  186. return App()->GetString(lookup);
  187. }
  188. inline QString QTStr(const char *lookupVal)
  189. {
  190. return QString::fromUtf8(Str(lookupVal));
  191. }
  192. bool GetFileSafeName(const char *name, std::string &file);
  193. bool GetClosestUnusedFileName(std::string &path, const char *extension);
  194. bool GetUnusedSceneCollectionFile(std::string &name, std::string &file);
  195. bool WindowPositionValid(QRect rect);
  196. extern bool portable_mode;
  197. extern bool steam;
  198. extern bool safe_mode;
  199. extern bool disable_3p_plugins;
  200. extern bool opt_start_streaming;
  201. extern bool opt_start_recording;
  202. extern bool opt_start_replaybuffer;
  203. extern bool opt_start_virtualcam;
  204. extern bool opt_minimize_tray;
  205. extern bool opt_studio_mode;
  206. extern bool opt_allow_opengl;
  207. extern bool opt_always_on_top;
  208. extern std::string opt_starting_scene;
  209. extern bool restart;
  210. extern bool restart_safe;
  211. #ifdef _WIN32
  212. extern "C" void install_dll_blocklist_hook(void);
  213. extern "C" void log_blocked_dlls(void);
  214. #endif