obs-app.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. class OBSApp : public QApplication {
  58. Q_OBJECT
  59. private:
  60. std::string locale;
  61. std::string theme;
  62. QString defaultStyleSheet;
  63. bool themeDarkMode = true;
  64. ConfigFile globalConfig;
  65. TextLookup textLookup;
  66. QPointer<OBSMainWindow> mainWindow;
  67. profiler_name_store_t *profilerNameStore = nullptr;
  68. bool libobs_initialized = false;
  69. os_inhibit_t *sleepInhibitor = nullptr;
  70. int sleepInhibitRefs = 0;
  71. bool enableHotkeysInFocus = true;
  72. bool enableHotkeysOutOfFocus = true;
  73. std::deque<obs_frontend_translate_ui_cb> translatorHooks;
  74. bool UpdatePre22MultiviewLayout(const char *layout);
  75. bool InitGlobalConfig();
  76. bool InitGlobalConfigDefaults();
  77. bool InitLocale();
  78. bool InitTheme();
  79. inline void ResetHotkeyState(bool inFocus);
  80. QPalette defaultPalette;
  81. void ParseExtraThemeData(const char *path);
  82. void AddExtraThemeColor(QPalette &pal, int group, const char *name,
  83. uint32_t color);
  84. bool notify(QObject *receiver, QEvent *e) override;
  85. public:
  86. OBSApp(int &argc, char **argv, profiler_name_store_t *store);
  87. ~OBSApp();
  88. void AppInit();
  89. bool OBSInit();
  90. void UpdateHotkeyFocusSetting(bool reset = true);
  91. void DisableHotkeys();
  92. inline bool HotkeysEnabledInFocus() const
  93. {
  94. return enableHotkeysInFocus;
  95. }
  96. inline QMainWindow *GetMainWindow() const { return mainWindow.data(); }
  97. inline config_t *GlobalConfig() const { return globalConfig; }
  98. inline const char *GetLocale() const { return locale.c_str(); }
  99. inline const char *GetTheme() const { return theme.c_str(); }
  100. bool SetTheme(std::string name, std::string path = "");
  101. inline bool IsThemeDark() const { return themeDarkMode; };
  102. inline lookup_t *GetTextLookup() const { return textLookup; }
  103. inline const char *GetString(const char *lookupVal) const
  104. {
  105. return textLookup.GetString(lookupVal);
  106. }
  107. bool TranslateString(const char *lookupVal, const char **out) const;
  108. profiler_name_store_t *GetProfilerNameStore() const
  109. {
  110. return profilerNameStore;
  111. }
  112. const char *GetLastLog() const;
  113. const char *GetCurrentLog() const;
  114. const char *GetLastCrashLog() const;
  115. std::string GetVersionString() const;
  116. bool IsPortableMode();
  117. bool IsUpdaterDisabled();
  118. bool IsMissingFilesCheckDisabled();
  119. const char *InputAudioSource() const;
  120. const char *OutputAudioSource() const;
  121. const char *GetRenderModule() const;
  122. inline void IncrementSleepInhibition()
  123. {
  124. if (!sleepInhibitor)
  125. return;
  126. if (sleepInhibitRefs++ == 0)
  127. os_inhibit_sleep_set_active(sleepInhibitor, true);
  128. }
  129. inline void DecrementSleepInhibition()
  130. {
  131. if (!sleepInhibitor)
  132. return;
  133. if (sleepInhibitRefs == 0)
  134. return;
  135. if (--sleepInhibitRefs == 0)
  136. os_inhibit_sleep_set_active(sleepInhibitor, false);
  137. }
  138. inline void PushUITranslation(obs_frontend_translate_ui_cb cb)
  139. {
  140. translatorHooks.emplace_front(cb);
  141. }
  142. inline void PopUITranslation() { translatorHooks.pop_front(); }
  143. public slots:
  144. void Exec(VoidFunc func);
  145. signals:
  146. void StyleChanged();
  147. };
  148. int GetConfigPath(char *path, size_t size, const char *name);
  149. char *GetConfigPathPtr(const char *name);
  150. int GetProgramDataPath(char *path, size_t size, const char *name);
  151. char *GetProgramDataPathPtr(const char *name);
  152. inline OBSApp *App()
  153. {
  154. return static_cast<OBSApp *>(qApp);
  155. }
  156. inline config_t *GetGlobalConfig()
  157. {
  158. return App()->GlobalConfig();
  159. }
  160. std::vector<std::pair<std::string, std::string>> GetLocaleNames();
  161. inline const char *Str(const char *lookup)
  162. {
  163. return App()->GetString(lookup);
  164. }
  165. #define QTStr(lookupVal) QString::fromUtf8(Str(lookupVal))
  166. bool GetFileSafeName(const char *name, std::string &file);
  167. bool GetClosestUnusedFileName(std::string &path, const char *extension);
  168. bool GetUnusedSceneCollectionFile(std::string &name, std::string &file);
  169. bool WindowPositionValid(QRect rect);
  170. static inline int GetProfilePath(char *path, size_t size, const char *file)
  171. {
  172. OBSMainWindow *window =
  173. reinterpret_cast<OBSMainWindow *>(App()->GetMainWindow());
  174. return window->GetProfilePath(path, size, file);
  175. }
  176. extern bool portable_mode;
  177. extern bool opt_start_streaming;
  178. extern bool opt_start_recording;
  179. extern bool opt_start_replaybuffer;
  180. extern bool opt_start_virtualcam;
  181. extern bool opt_minimize_tray;
  182. extern bool opt_studio_mode;
  183. extern bool opt_allow_opengl;
  184. extern bool opt_always_on_top;
  185. extern bool opt_disable_high_dpi_scaling;
  186. extern std::string opt_starting_scene;
  187. extern bool restart;