obs-app.hpp 5.7 KB

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