obs-app.hpp 5.9 KB

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