obs-app.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <QApplication>
  16. #include <QTranslator>
  17. #include <QPointer>
  18. #ifndef _WIN32
  19. #include <QSocketNotifier>
  20. #else
  21. #include <QSessionManager>
  22. #endif
  23. #include <obs.hpp>
  24. #include <util/lexer.h>
  25. #include <util/profiler.h>
  26. #include <util/util.hpp>
  27. #include <util/platform.h>
  28. #include <obs-frontend-api.h>
  29. #include <functional>
  30. #include <string>
  31. #include <memory>
  32. #include <vector>
  33. #include <deque>
  34. #include "window-main.hpp"
  35. std::string CurrentTimeString();
  36. std::string CurrentDateTimeString();
  37. std::string GenerateTimeDateFilename(const char *extension,
  38. bool noSpace = false);
  39. std::string GenerateSpecifiedFilename(const char *extension, bool noSpace,
  40. const char *format);
  41. std::string GetFormatString(const char *format, const char *prefix,
  42. const char *suffix);
  43. std::string GetFormatExt(const char *container);
  44. std::string GetOutputFilename(const char *path, const char *container,
  45. bool noSpace, bool overwrite, 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,
  59. const char *disambiguation,
  60. int n) const override;
  61. };
  62. typedef std::function<void()> VoidFunc;
  63. struct OBSThemeMeta {
  64. bool dark;
  65. std::string parent;
  66. std::string author;
  67. };
  68. struct UpdateBranch {
  69. QString name;
  70. QString display_name;
  71. QString description;
  72. bool is_enabled;
  73. bool is_visible;
  74. };
  75. class OBSApp : public QApplication {
  76. Q_OBJECT
  77. private:
  78. std::string locale;
  79. std::string theme;
  80. bool themeDarkMode = true;
  81. ConfigFile globalConfig;
  82. TextLookup textLookup;
  83. QPointer<OBSMainWindow> mainWindow;
  84. profiler_name_store_t *profilerNameStore = nullptr;
  85. std::vector<UpdateBranch> updateBranches;
  86. bool branches_loaded = false;
  87. bool libobs_initialized = false;
  88. os_inhibit_t *sleepInhibitor = nullptr;
  89. int sleepInhibitRefs = 0;
  90. bool enableHotkeysInFocus = true;
  91. bool enableHotkeysOutOfFocus = true;
  92. std::deque<obs_frontend_translate_ui_cb> translatorHooks;
  93. bool UpdatePre22MultiviewLayout(const char *layout);
  94. bool InitGlobalConfig();
  95. bool InitGlobalConfigDefaults();
  96. bool InitLocale();
  97. bool InitTheme();
  98. inline void ResetHotkeyState(bool inFocus);
  99. QPalette defaultPalette;
  100. void ParseExtraThemeData(const char *path);
  101. static OBSThemeMeta *ParseThemeMeta(const char *path);
  102. void AddExtraThemeColor(QPalette &pal, int group, const char *name,
  103. uint32_t color);
  104. bool notify(QObject *receiver, QEvent *e) override;
  105. #ifndef _WIN32
  106. static int sigintFd[2];
  107. QSocketNotifier *snInt = nullptr;
  108. #else
  109. private slots:
  110. void commitData(QSessionManager &manager);
  111. #endif
  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
  120. {
  121. return enableHotkeysInFocus;
  122. }
  123. inline QMainWindow *GetMainWindow() const { return mainWindow.data(); }
  124. inline config_t *GlobalConfig() const { return globalConfig; }
  125. inline const char *GetLocale() const { return locale.c_str(); }
  126. inline const char *GetTheme() const { return theme.c_str(); }
  127. std::string GetTheme(std::string name, std::string path);
  128. std::string SetParentTheme(std::string name);
  129. bool SetTheme(std::string name, std::string path = "");
  130. inline bool IsThemeDark() const { return themeDarkMode; };
  131. void SetBranchData(const std::string &data);
  132. std::vector<UpdateBranch> GetBranches();
  133. inline lookup_t *GetTextLookup() const { return textLookup; }
  134. inline const char *GetString(const char *lookupVal) const
  135. {
  136. return textLookup.GetString(lookupVal);
  137. }
  138. bool TranslateString(const char *lookupVal, const char **out) const;
  139. profiler_name_store_t *GetProfilerNameStore() const
  140. {
  141. return profilerNameStore;
  142. }
  143. const char *GetLastLog() const;
  144. const char *GetCurrentLog() const;
  145. const char *GetLastCrashLog() const;
  146. std::string GetVersionString(bool platform = true) const;
  147. bool IsPortableMode();
  148. bool IsUpdaterDisabled();
  149. bool IsMissingFilesCheckDisabled();
  150. const char *InputAudioSource() const;
  151. const char *OutputAudioSource() const;
  152. const char *GetRenderModule() const;
  153. inline void IncrementSleepInhibition()
  154. {
  155. if (!sleepInhibitor)
  156. return;
  157. if (sleepInhibitRefs++ == 0)
  158. os_inhibit_sleep_set_active(sleepInhibitor, true);
  159. }
  160. inline void DecrementSleepInhibition()
  161. {
  162. if (!sleepInhibitor)
  163. return;
  164. if (sleepInhibitRefs == 0)
  165. return;
  166. if (--sleepInhibitRefs == 0)
  167. os_inhibit_sleep_set_active(sleepInhibitor, false);
  168. }
  169. inline void PushUITranslation(obs_frontend_translate_ui_cb cb)
  170. {
  171. translatorHooks.emplace_front(cb);
  172. }
  173. inline void PopUITranslation() { translatorHooks.pop_front(); }
  174. #ifndef _WIN32
  175. static void SigIntSignalHandler(int);
  176. #endif
  177. public slots:
  178. void Exec(VoidFunc func);
  179. void ProcessSigInt();
  180. signals:
  181. void StyleChanged();
  182. };
  183. int GetConfigPath(char *path, size_t size, const char *name);
  184. char *GetConfigPathPtr(const char *name);
  185. int GetProgramDataPath(char *path, size_t size, const char *name);
  186. char *GetProgramDataPathPtr(const char *name);
  187. inline OBSApp *App()
  188. {
  189. return static_cast<OBSApp *>(qApp);
  190. }
  191. inline config_t *GetGlobalConfig()
  192. {
  193. return App()->GlobalConfig();
  194. }
  195. std::vector<std::pair<std::string, std::string>> GetLocaleNames();
  196. inline const char *Str(const char *lookup)
  197. {
  198. return App()->GetString(lookup);
  199. }
  200. inline QString QTStr(const char *lookupVal)
  201. {
  202. return QString::fromUtf8(Str(lookupVal));
  203. }
  204. bool GetFileSafeName(const char *name, std::string &file);
  205. bool GetClosestUnusedFileName(std::string &path, const char *extension);
  206. bool GetUnusedSceneCollectionFile(std::string &name, std::string &file);
  207. bool WindowPositionValid(QRect rect);
  208. static inline int GetProfilePath(char *path, size_t size, const char *file)
  209. {
  210. OBSMainWindow *window =
  211. reinterpret_cast<OBSMainWindow *>(App()->GetMainWindow());
  212. return window->GetProfilePath(path, size, file);
  213. }
  214. extern bool portable_mode;
  215. extern bool steam;
  216. extern bool safe_mode;
  217. extern bool disable_3p_plugins;
  218. extern bool opt_start_streaming;
  219. extern bool opt_start_recording;
  220. extern bool opt_start_replaybuffer;
  221. extern bool opt_start_virtualcam;
  222. extern bool opt_minimize_tray;
  223. extern bool opt_studio_mode;
  224. extern bool opt_allow_opengl;
  225. extern bool opt_always_on_top;
  226. extern std::string opt_starting_scene;
  227. extern bool restart;
  228. extern bool restart_safe;
  229. #ifdef _WIN32
  230. extern "C" void install_dll_blocklist_hook(void);
  231. extern "C" void log_blocked_dlls(void);
  232. #endif