obs-app.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <string>
  24. #include <memory>
  25. #include <vector>
  26. #include "window-main.hpp"
  27. std::string CurrentTimeString();
  28. std::string CurrentDateTimeString();
  29. std::string GenerateTimeDateFilename(const char *extension, bool noSpace=false);
  30. std::string GenerateSpecifiedFilename(const char *extension, bool noSpace,
  31. const char *format);
  32. QObject *CreateShortcutFilter();
  33. struct BaseLexer {
  34. lexer lex;
  35. public:
  36. inline BaseLexer() {lexer_init(&lex);}
  37. inline ~BaseLexer() {lexer_free(&lex);}
  38. operator lexer*() {return &lex;}
  39. };
  40. class OBSTranslator : public QTranslator {
  41. Q_OBJECT
  42. public:
  43. virtual bool isEmpty() const override {return false;}
  44. virtual QString translate(const char *context, const char *sourceText,
  45. const char *disambiguation, int n) const override;
  46. };
  47. class OBSApp : public QApplication {
  48. Q_OBJECT
  49. private:
  50. std::string locale;
  51. std::string theme;
  52. ConfigFile globalConfig;
  53. TextLookup textLookup;
  54. OBSContext obsContext;
  55. QPointer<OBSMainWindow> mainWindow;
  56. profiler_name_store_t *profilerNameStore = nullptr;
  57. os_inhibit_t *sleepInhibitor = nullptr;
  58. int sleepInhibitRefs = 0;
  59. bool InitGlobalConfig();
  60. bool InitGlobalConfigDefaults();
  61. bool InitLocale();
  62. bool InitTheme();
  63. public:
  64. OBSApp(int &argc, char **argv, profiler_name_store_t *store);
  65. ~OBSApp();
  66. void AppInit();
  67. bool OBSInit();
  68. inline QMainWindow *GetMainWindow() const {return mainWindow.data();}
  69. inline config_t *GlobalConfig() const {return globalConfig;}
  70. inline const char *GetLocale() const
  71. {
  72. return locale.c_str();
  73. }
  74. inline const char *GetTheme() const {return theme.c_str();}
  75. bool SetTheme(std::string name, std::string path = "");
  76. inline lookup_t *GetTextLookup() const {return textLookup;}
  77. inline const char *GetString(const char *lookupVal) const
  78. {
  79. return textLookup.GetString(lookupVal);
  80. }
  81. profiler_name_store_t *GetProfilerNameStore() const
  82. {
  83. return profilerNameStore;
  84. }
  85. const char *GetLastLog() const;
  86. const char *GetCurrentLog() const;
  87. std::string GetVersionString() const;
  88. const char *InputAudioSource() const;
  89. const char *OutputAudioSource() const;
  90. const char *GetRenderModule() const;
  91. inline void IncrementSleepInhibition()
  92. {
  93. if (!sleepInhibitor) return;
  94. if (sleepInhibitRefs++ == 0)
  95. os_inhibit_sleep_set_active(sleepInhibitor, true);
  96. }
  97. inline void DecrementSleepInhibition()
  98. {
  99. if (!sleepInhibitor) return;
  100. if (sleepInhibitRefs == 0) return;
  101. if (--sleepInhibitRefs == 0)
  102. os_inhibit_sleep_set_active(sleepInhibitor, false);
  103. }
  104. };
  105. int GetConfigPath(char *path, size_t size, const char *name);
  106. char *GetConfigPathPtr(const char *name);
  107. inline OBSApp *App() {return static_cast<OBSApp*>(qApp);}
  108. inline config_t *GetGlobalConfig() {return App()->GlobalConfig();}
  109. std::vector<std::pair<std::string, std::string>> GetLocaleNames();
  110. inline const char *Str(const char *lookup) {return App()->GetString(lookup);}
  111. #define QTStr(lookupVal) QString::fromUtf8(Str(lookupVal))
  112. bool GetFileSafeName(const char *name, std::string &file);
  113. bool GetClosestUnusedFileName(std::string &path, const char *extension);
  114. static inline int GetProfilePath(char *path, size_t size, const char *file)
  115. {
  116. OBSMainWindow *window = reinterpret_cast<OBSMainWindow*>(
  117. App()->GetMainWindow());
  118. return window->GetProfilePath(path, size, file);
  119. }