obs-app.hpp 4.3 KB

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