obs-app.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifndef _WIN32
  19. #include <QSocketNotifier>
  20. #endif
  21. #include <obs.hpp>
  22. #include <util/lexer.h>
  23. #include <util/profiler.h>
  24. #include <util/util.hpp>
  25. #include <util/platform.h>
  26. #include <obs-frontend-api.h>
  27. #include <functional>
  28. #include <string>
  29. #include <memory>
  30. #include <vector>
  31. #include <deque>
  32. #include "window-main.hpp"
  33. std::string CurrentTimeString();
  34. std::string CurrentDateTimeString();
  35. std::string GenerateTimeDateFilename(const char *extension,
  36. bool noSpace = false);
  37. std::string GenerateSpecifiedFilename(const char *extension, bool noSpace,
  38. const char *format);
  39. std::string GetFormatString(const char *format, const char *prefix,
  40. const char *suffix);
  41. std::string GetFormatExt(const char *container);
  42. std::string GetOutputFilename(const char *path, const char *container,
  43. bool noSpace, bool overwrite, const char *format);
  44. QObject *CreateShortcutFilter();
  45. struct BaseLexer {
  46. lexer lex;
  47. public:
  48. inline BaseLexer() { lexer_init(&lex); }
  49. inline ~BaseLexer() { lexer_free(&lex); }
  50. operator lexer *() { return &lex; }
  51. };
  52. class OBSTranslator : public QTranslator {
  53. Q_OBJECT
  54. public:
  55. virtual bool isEmpty() const override { return false; }
  56. virtual QString translate(const char *context, const char *sourceText,
  57. const char *disambiguation,
  58. int n) const override;
  59. };
  60. typedef std::function<void()> VoidFunc;
  61. struct OBSThemeMeta {
  62. bool dark;
  63. std::string parent;
  64. std::string author;
  65. };
  66. struct UpdateBranch {
  67. QString name;
  68. QString display_name;
  69. QString description;
  70. bool is_enabled;
  71. bool is_visible;
  72. };
  73. class OBSApp : public QApplication {
  74. Q_OBJECT
  75. private:
  76. std::string locale;
  77. std::string theme;
  78. bool themeDarkMode = true;
  79. ConfigFile globalConfig;
  80. TextLookup textLookup;
  81. QPointer<OBSMainWindow> mainWindow;
  82. profiler_name_store_t *profilerNameStore = nullptr;
  83. std::vector<UpdateBranch> updateBranches;
  84. bool branches_loaded = false;
  85. bool libobs_initialized = false;
  86. os_inhibit_t *sleepInhibitor = nullptr;
  87. int sleepInhibitRefs = 0;
  88. bool enableHotkeysInFocus = true;
  89. bool enableHotkeysOutOfFocus = true;
  90. std::deque<obs_frontend_translate_ui_cb> translatorHooks;
  91. bool UpdatePre22MultiviewLayout(const char *layout);
  92. bool InitGlobalConfig();
  93. bool InitGlobalConfigDefaults();
  94. bool InitLocale();
  95. bool InitTheme();
  96. inline void ResetHotkeyState(bool inFocus);
  97. QPalette defaultPalette;
  98. void ParseExtraThemeData(const char *path);
  99. static OBSThemeMeta *ParseThemeMeta(const char *path);
  100. void AddExtraThemeColor(QPalette &pal, int group, const char *name,
  101. uint32_t color);
  102. bool notify(QObject *receiver, QEvent *e) override;
  103. #ifndef _WIN32
  104. static int sigintFd[2];
  105. QSocketNotifier *snInt = nullptr;
  106. #endif
  107. public:
  108. OBSApp(int &argc, char **argv, profiler_name_store_t *store);
  109. ~OBSApp();
  110. void AppInit();
  111. bool OBSInit();
  112. void UpdateHotkeyFocusSetting(bool reset = true);
  113. void DisableHotkeys();
  114. inline bool HotkeysEnabledInFocus() const
  115. {
  116. return enableHotkeysInFocus;
  117. }
  118. inline QMainWindow *GetMainWindow() const { return mainWindow.data(); }
  119. inline config_t *GlobalConfig() const { return globalConfig; }
  120. inline const char *GetLocale() const { return locale.c_str(); }
  121. inline const char *GetTheme() const { return theme.c_str(); }
  122. std::string GetTheme(std::string name, std::string path);
  123. std::string SetParentTheme(std::string name);
  124. bool SetTheme(std::string name, std::string path = "");
  125. inline bool IsThemeDark() const { return themeDarkMode; };
  126. void SetBranchData(const std::string &data);
  127. std::vector<UpdateBranch> GetBranches();
  128. inline lookup_t *GetTextLookup() const { return textLookup; }
  129. inline const char *GetString(const char *lookupVal) const
  130. {
  131. return textLookup.GetString(lookupVal);
  132. }
  133. bool TranslateString(const char *lookupVal, const char **out) const;
  134. profiler_name_store_t *GetProfilerNameStore() const
  135. {
  136. return profilerNameStore;
  137. }
  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)
  165. {
  166. translatorHooks.emplace_front(cb);
  167. }
  168. inline void PopUITranslation() { translatorHooks.pop_front(); }
  169. #ifndef _WIN32
  170. static void SigIntSignalHandler(int);
  171. #endif
  172. public slots:
  173. void Exec(VoidFunc func);
  174. void ProcessSigInt();
  175. signals:
  176. void StyleChanged();
  177. };
  178. int GetConfigPath(char *path, size_t size, const char *name);
  179. char *GetConfigPathPtr(const char *name);
  180. int GetProgramDataPath(char *path, size_t size, const char *name);
  181. char *GetProgramDataPathPtr(const char *name);
  182. inline OBSApp *App()
  183. {
  184. return static_cast<OBSApp *>(qApp);
  185. }
  186. inline config_t *GetGlobalConfig()
  187. {
  188. return App()->GlobalConfig();
  189. }
  190. std::vector<std::pair<std::string, std::string>> GetLocaleNames();
  191. inline const char *Str(const char *lookup)
  192. {
  193. return App()->GetString(lookup);
  194. }
  195. #define QTStr(lookupVal) QString::fromUtf8(Str(lookupVal))
  196. bool GetFileSafeName(const char *name, std::string &file);
  197. bool GetClosestUnusedFileName(std::string &path, const char *extension);
  198. bool GetUnusedSceneCollectionFile(std::string &name, std::string &file);
  199. bool WindowPositionValid(QRect rect);
  200. static inline int GetProfilePath(char *path, size_t size, const char *file)
  201. {
  202. OBSMainWindow *window =
  203. reinterpret_cast<OBSMainWindow *>(App()->GetMainWindow());
  204. return window->GetProfilePath(path, size, file);
  205. }
  206. extern bool portable_mode;
  207. extern bool steam;
  208. extern bool opt_start_streaming;
  209. extern bool opt_start_recording;
  210. extern bool opt_start_replaybuffer;
  211. extern bool opt_start_virtualcam;
  212. extern bool opt_minimize_tray;
  213. extern bool opt_studio_mode;
  214. extern bool opt_allow_opengl;
  215. extern bool opt_always_on_top;
  216. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  217. extern bool opt_disable_high_dpi_scaling;
  218. #endif
  219. extern std::string opt_starting_scene;
  220. extern bool restart;
  221. #ifdef _WIN32
  222. extern "C" void install_dll_blocklist_hook(void);
  223. extern "C" void log_blocked_dlls(void);
  224. #endif