obs-app.hpp 5.8 KB

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