obs-app.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh 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 <QApplication>
  16. #include <QTranslator>
  17. #include <QPointer>
  18. #include <obs.hpp>
  19. #include <util/lexer.h>
  20. #include <util/profiler.h>
  21. #include <util/util.hpp>
  22. #include <util/platform.h>
  23. #include <obs-frontend-api.h>
  24. #include <functional>
  25. #include <string>
  26. #include <memory>
  27. #include <vector>
  28. #include <deque>
  29. #include "window-main.hpp"
  30. std::string CurrentTimeString();
  31. std::string CurrentDateTimeString();
  32. std::string GenerateTimeDateFilename(const char *extension,
  33. bool noSpace = false);
  34. std::string GenerateSpecifiedFilename(const char *extension, bool noSpace,
  35. const char *format);
  36. std::string GetFormatString(const char *format, const char *prefix,
  37. const char *suffix);
  38. std::string GetOutputFilename(const char *path, const char *ext, bool noSpace,
  39. bool overwrite, const char *format);
  40. QObject *CreateShortcutFilter();
  41. struct BaseLexer {
  42. lexer lex;
  43. public:
  44. inline BaseLexer() { lexer_init(&lex); }
  45. inline ~BaseLexer() { lexer_free(&lex); }
  46. operator lexer *() { return &lex; }
  47. };
  48. class OBSTranslator : public QTranslator {
  49. Q_OBJECT
  50. public:
  51. virtual bool isEmpty() const override { return false; }
  52. virtual QString translate(const char *context, const char *sourceText,
  53. const char *disambiguation,
  54. int n) const override;
  55. };
  56. typedef std::function<void()> VoidFunc;
  57. struct OBSThemeMeta {
  58. bool dark;
  59. std::string parent;
  60. std::string author;
  61. };
  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. std::string theme;
  74. bool themeDarkMode = true;
  75. ConfigFile globalConfig;
  76. TextLookup textLookup;
  77. QPointer<OBSMainWindow> mainWindow;
  78. profiler_name_store_t *profilerNameStore = nullptr;
  79. std::vector<UpdateBranch> updateBranches;
  80. bool branches_loaded = false;
  81. bool libobs_initialized = false;
  82. os_inhibit_t *sleepInhibitor = nullptr;
  83. int sleepInhibitRefs = 0;
  84. bool enableHotkeysInFocus = true;
  85. bool enableHotkeysOutOfFocus = true;
  86. std::deque<obs_frontend_translate_ui_cb> translatorHooks;
  87. bool UpdatePre22MultiviewLayout(const char *layout);
  88. bool InitGlobalConfig();
  89. bool InitGlobalConfigDefaults();
  90. bool InitLocale();
  91. bool InitTheme();
  92. inline void ResetHotkeyState(bool inFocus);
  93. QPalette defaultPalette;
  94. void ParseExtraThemeData(const char *path);
  95. static OBSThemeMeta *ParseThemeMeta(const char *path);
  96. void AddExtraThemeColor(QPalette &pal, int group, const char *name,
  97. uint32_t color);
  98. bool notify(QObject *receiver, QEvent *e) override;
  99. public:
  100. OBSApp(int &argc, char **argv, profiler_name_store_t *store);
  101. ~OBSApp();
  102. void AppInit();
  103. bool OBSInit();
  104. void UpdateHotkeyFocusSetting(bool reset = true);
  105. void DisableHotkeys();
  106. inline bool HotkeysEnabledInFocus() const
  107. {
  108. return enableHotkeysInFocus;
  109. }
  110. inline QMainWindow *GetMainWindow() const { return mainWindow.data(); }
  111. inline config_t *GlobalConfig() const { return globalConfig; }
  112. inline const char *GetLocale() const { return locale.c_str(); }
  113. inline const char *GetTheme() const { return theme.c_str(); }
  114. std::string GetTheme(std::string name, std::string path);
  115. std::string SetParentTheme(std::string name);
  116. bool SetTheme(std::string name, std::string path = "");
  117. inline bool IsThemeDark() const { return themeDarkMode; };
  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
  122. {
  123. return textLookup.GetString(lookupVal);
  124. }
  125. bool TranslateString(const char *lookupVal, const char **out) const;
  126. profiler_name_store_t *GetProfilerNameStore() const
  127. {
  128. return profilerNameStore;
  129. }
  130. const char *GetLastLog() const;
  131. const char *GetCurrentLog() const;
  132. const char *GetLastCrashLog() const;
  133. std::string GetVersionString() const;
  134. bool IsPortableMode();
  135. bool IsUpdaterDisabled();
  136. bool IsMissingFilesCheckDisabled();
  137. const char *InputAudioSource() const;
  138. const char *OutputAudioSource() const;
  139. const char *GetRenderModule() const;
  140. inline void IncrementSleepInhibition()
  141. {
  142. if (!sleepInhibitor)
  143. return;
  144. if (sleepInhibitRefs++ == 0)
  145. os_inhibit_sleep_set_active(sleepInhibitor, true);
  146. }
  147. inline void DecrementSleepInhibition()
  148. {
  149. if (!sleepInhibitor)
  150. return;
  151. if (sleepInhibitRefs == 0)
  152. return;
  153. if (--sleepInhibitRefs == 0)
  154. os_inhibit_sleep_set_active(sleepInhibitor, false);
  155. }
  156. inline void PushUITranslation(obs_frontend_translate_ui_cb cb)
  157. {
  158. translatorHooks.emplace_front(cb);
  159. }
  160. inline void PopUITranslation() { translatorHooks.pop_front(); }
  161. public slots:
  162. void Exec(VoidFunc func);
  163. signals:
  164. void StyleChanged();
  165. };
  166. int GetConfigPath(char *path, size_t size, const char *name);
  167. char *GetConfigPathPtr(const char *name);
  168. int GetProgramDataPath(char *path, size_t size, const char *name);
  169. char *GetProgramDataPathPtr(const char *name);
  170. inline OBSApp *App()
  171. {
  172. return static_cast<OBSApp *>(qApp);
  173. }
  174. inline config_t *GetGlobalConfig()
  175. {
  176. return App()->GlobalConfig();
  177. }
  178. std::vector<std::pair<std::string, std::string>> GetLocaleNames();
  179. inline const char *Str(const char *lookup)
  180. {
  181. return App()->GetString(lookup);
  182. }
  183. #define QTStr(lookupVal) QString::fromUtf8(Str(lookupVal))
  184. bool GetFileSafeName(const char *name, std::string &file);
  185. bool GetClosestUnusedFileName(std::string &path, const char *extension);
  186. bool GetUnusedSceneCollectionFile(std::string &name, std::string &file);
  187. bool WindowPositionValid(QRect rect);
  188. static inline int GetProfilePath(char *path, size_t size, const char *file)
  189. {
  190. OBSMainWindow *window =
  191. reinterpret_cast<OBSMainWindow *>(App()->GetMainWindow());
  192. return window->GetProfilePath(path, size, file);
  193. }
  194. extern bool portable_mode;
  195. extern bool steam;
  196. extern bool opt_start_streaming;
  197. extern bool opt_start_recording;
  198. extern bool opt_start_replaybuffer;
  199. extern bool opt_start_virtualcam;
  200. extern bool opt_minimize_tray;
  201. extern bool opt_studio_mode;
  202. extern bool opt_allow_opengl;
  203. extern bool opt_always_on_top;
  204. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  205. extern bool opt_disable_high_dpi_scaling;
  206. #endif
  207. extern std::string opt_starting_scene;
  208. extern bool restart;