CWallpaperApplication.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "CWallpaperApplication.h"
  2. #include "Steam/FileSystem/FileSystem.h"
  3. #include "WallpaperEngine/Application/CApplicationState.h"
  4. #include "WallpaperEngine/Assets/CAssetLoadException.h"
  5. #include "WallpaperEngine/Assets/CDirectory.h"
  6. #include "WallpaperEngine/Assets/CVirtualContainer.h"
  7. #include "WallpaperEngine/Audio/Drivers/Detectors/CPulseAudioPlayingDetector.h"
  8. #include "WallpaperEngine/Core/Wallpapers/CVideo.h"
  9. #include "WallpaperEngine/Input/Drivers/CGLFWMouseInput.h"
  10. #include "WallpaperEngine/Logging/CLog.h"
  11. #include "WallpaperEngine/Render/CRenderContext.h"
  12. #include "WallpaperEngine/Input/Drivers/CWaylandMouseInput.h"
  13. #include "WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.h"
  14. #include "WallpaperEngine/Render/Drivers/Detectors/CX11FullScreenDetector.h"
  15. #define FULLSCREEN_CHECK_WAIT_TIME 250
  16. float g_Time;
  17. float g_TimeLast;
  18. float g_Daytime;
  19. namespace WallpaperEngine::Application {
  20. CWallpaperApplication::CWallpaperApplication (CApplicationContext& context,
  21. WallpaperEngine::WebBrowser::CWebBrowserContext& browserContext) :
  22. m_context (context),
  23. m_defaultBackground (nullptr),
  24. browserContext (browserContext) {
  25. this->loadBackgrounds ();
  26. this->setupProperties ();
  27. }
  28. CWallpaperApplication::~CWallpaperApplication () {
  29. delete context;
  30. delete videoDriver;
  31. delete audioContext;
  32. delete audioDriver;
  33. delete inputContext;
  34. }
  35. void CWallpaperApplication::setupContainer (CCombinedContainer& container, const std::string& bg) const {
  36. const std::filesystem::path basepath = bg;
  37. container.add (new CDirectory (basepath));
  38. container.addPkg (basepath / "scene.pkg");
  39. container.addPkg (basepath / "gifscene.pkg");
  40. container.add (new CDirectory (this->m_context.settings.general.assets));
  41. // add two possible patches directories to the container
  42. // hopefully one sticks
  43. bool relative = true;
  44. bool absolute = true;
  45. try {
  46. container.add (new CDirectory ("../share/"));
  47. } catch (CAssetLoadException&) {
  48. relative = false;
  49. }
  50. try {
  51. container.add (new CDirectory (DATADIR));
  52. } catch (CAssetLoadException&) {
  53. absolute = false;
  54. }
  55. if (!relative && !absolute)
  56. sLog.error ("WARNING: Shader patches directory cannot be found, this might make some backgrounds not work "
  57. "properly");
  58. // TODO: move this somewhere else?
  59. auto* virtualContainer = new CVirtualContainer ();
  60. //
  61. // Had to get a little creative with the effects to achieve the same bloom effect without any custom code
  62. // these virtual files are loaded by an image in the scene that takes current _rt_FullFrameBuffer and
  63. // applies the bloom effect to render it out to the screen
  64. //
  65. // add the effect file for screen bloom
  66. // add some model for the image element even if it's going to waste rendering cycles
  67. virtualContainer->add ("effects/wpenginelinux/bloomeffect.json",
  68. "{"
  69. "\t\"name\":\"camerabloom_wpengine_linux\","
  70. "\t\"group\":\"wpengine_linux_camera\","
  71. "\t\"dependencies\":[],"
  72. "\t\"passes\":"
  73. "\t["
  74. "\t\t{"
  75. "\t\t\t\"material\": \"materials/util/downsample_quarter_bloom.json\","
  76. "\t\t\t\"target\": \"_rt_4FrameBuffer\","
  77. "\t\t\t\"bind\":"
  78. "\t\t\t["
  79. "\t\t\t\t{"
  80. "\t\t\t\t\t\"name\": \"_rt_FullFrameBuffer\","
  81. "\t\t\t\t\t\"index\": 0"
  82. "\t\t\t\t}"
  83. "\t\t\t]"
  84. "\t\t},"
  85. "\t\t{"
  86. "\t\t\t\"material\": \"materials/util/downsample_eighth_blur_v.json\","
  87. "\t\t\t\"target\": \"_rt_8FrameBuffer\","
  88. "\t\t\t\"bind\":"
  89. "\t\t\t["
  90. "\t\t\t\t{"
  91. "\t\t\t\t\t\"name\": \"_rt_4FrameBuffer\","
  92. "\t\t\t\t\t\"index\": 0"
  93. "\t\t\t\t}"
  94. "\t\t\t]"
  95. "\t\t},"
  96. "\t\t{"
  97. "\t\t\t\"material\": \"materials/util/blur_h_bloom.json\","
  98. "\t\t\t\"target\": \"_rt_Bloom\","
  99. "\t\t\t\"bind\":"
  100. "\t\t\t["
  101. "\t\t\t\t{"
  102. "\t\t\t\t\t\"name\": \"_rt_8FrameBuffer\","
  103. "\t\t\t\t\t\"index\": 0"
  104. "\t\t\t\t}"
  105. "\t\t\t]"
  106. "\t\t},"
  107. "\t\t{"
  108. "\t\t\t\"material\": \"materials/util/combine.json\","
  109. "\t\t\t\"target\": \"_rt_FullFrameBuffer\","
  110. "\t\t\t\"bind\":"
  111. "\t\t\t["
  112. "\t\t\t\t{"
  113. "\t\t\t\t\t\"name\": \"_rt_imageLayerComposite_-1_a\","
  114. "\t\t\t\t\t\"index\": 0"
  115. "\t\t\t\t},"
  116. "\t\t\t\t{"
  117. "\t\t\t\t\t\"name\": \"_rt_Bloom\","
  118. "\t\t\t\t\t\"index\": 1"
  119. "\t\t\t\t}"
  120. "\t\t\t]"
  121. "\t\t}"
  122. "\t]"
  123. "}");
  124. virtualContainer->add ("models/wpenginelinux.json", "{"
  125. "\t\"material\":\"materials/wpenginelinux.json\""
  126. "}");
  127. // models require materials, so add that too
  128. virtualContainer->add ("materials/wpenginelinux.json", "{"
  129. "\t\"passes\":"
  130. "\t\t["
  131. "\t\t\t{"
  132. "\t\t\t\t\"blending\": \"normal\","
  133. "\t\t\t\t\"cullmode\": \"nocull\","
  134. "\t\t\t\t\"depthtest\": \"disabled\","
  135. "\t\t\t\t\"depthwrite\": \"disabled\","
  136. "\t\t\t\t\"shader\": \"genericimage2\","
  137. "\t\t\t\t\"textures\": [\"_rt_FullFrameBuffer\"]"
  138. "\t\t\t}"
  139. "\t\t]"
  140. "}");
  141. container.add (virtualContainer);
  142. }
  143. void CWallpaperApplication::loadBackgrounds () {
  144. // load default background if specified
  145. if (!this->m_context.settings.general.defaultBackground.empty ()) {
  146. this->m_defaultBackground = this->loadBackground (this->m_context.settings.general.defaultBackground);
  147. }
  148. for (const auto& [background, path] : this->m_context.settings.general.screenBackgrounds) {
  149. // screens with no background should use the default
  150. if (path.empty ()) {
  151. this->m_backgrounds [background] = this->m_defaultBackground;
  152. } else {
  153. this->m_backgrounds [background] = this->loadBackground (path);
  154. }
  155. }
  156. }
  157. Core::CProject* CWallpaperApplication::loadBackground (const std::string& bg) {
  158. auto* container = new CCombinedContainer ();
  159. this->setupContainer (*container, bg);
  160. return Core::CProject::fromFile ("project.json", container);
  161. }
  162. void CWallpaperApplication::setupPropertiesForProject (const Core::CProject* project) {
  163. // show properties if required
  164. for (const auto cur : project->getProperties ()) {
  165. // update the value of the property
  166. auto override = this->m_context.settings.general.properties.find (cur->getName ());
  167. if (override != this->m_context.settings.general.properties.end ()) {
  168. sLog.out ("Applying override value for ", cur->getName ());
  169. cur->update (override->second);
  170. }
  171. if (this->m_context.settings.general.onlyListProperties)
  172. sLog.out (cur->dump ());
  173. }
  174. }
  175. void CWallpaperApplication::setupProperties () {
  176. for (const auto& [backgrounc, info] : this->m_backgrounds)
  177. this->setupPropertiesForProject (info);
  178. if (this->m_defaultBackground != nullptr)
  179. this->setupPropertiesForProject (this->m_defaultBackground);
  180. }
  181. void CWallpaperApplication::takeScreenshot (const Render::CRenderContext& context,
  182. const std::filesystem::path& filename, FREE_IMAGE_FORMAT format) {
  183. // this should be getting called at the end of the frame, so the right thing should be bound already
  184. const int width = context.getOutput ().getFullWidth ();
  185. const int height = context.getOutput ().getFullHeight ();
  186. // build the output file with FreeImage
  187. static FIBITMAP* bitmap = FreeImage_Allocate (width, height, 24);
  188. RGBQUAD color;
  189. int xoffset = 0;
  190. for (const auto& [screen, viewport] : context.getOutput ().getViewports ()) {
  191. // activate opengl context so we can read from the framebuffer
  192. viewport->makeCurrent ();
  193. // make room for storing the pixel of this viewport
  194. auto* buffer = new uint8_t [viewport->viewport.z * viewport->viewport.w * sizeof (uint8_t) * 3];
  195. const uint8_t* pixel = buffer;
  196. // read the viewport data into the pixel buffer
  197. glReadPixels (viewport->viewport.x, viewport->viewport.y, viewport->viewport.z, viewport->viewport.w, GL_RGB,
  198. GL_UNSIGNED_BYTE, buffer);
  199. // now get access to the pixels
  200. for (int y = viewport->viewport.w; y > 0; y--) {
  201. for (int x = 0; x < viewport->viewport.z; x++) {
  202. color.rgbRed = *pixel++;
  203. color.rgbGreen = *pixel++;
  204. color.rgbBlue = *pixel++;
  205. // set the pixel in the destination
  206. FreeImage_SetPixelColor (bitmap, x + xoffset,
  207. context.getOutput ().renderVFlip () ? (viewport->viewport.w - y) : y, &color);
  208. }
  209. }
  210. if (viewport->single)
  211. xoffset += viewport->viewport.z;
  212. // free the buffer allocated for the viewport
  213. delete [] buffer;
  214. }
  215. // finally save the file
  216. FreeImage_Save (format, bitmap, filename.c_str (), 0);
  217. FreeImage_Unload (bitmap);
  218. }
  219. void CWallpaperApplication::show () {
  220. const char* XDG_SESSION_TYPE = getenv ("XDG_SESSION_TYPE");
  221. if (!XDG_SESSION_TYPE) {
  222. sLog.exception("Cannot read environment variable XDG_SESSION_TYPE, window server detection failed. Please ensure proper values are set");
  223. }
  224. #ifdef ENABLE_WAYLAND
  225. bool isWayland = strncmp("wayland", XDG_SESSION_TYPE, strlen("wayland")) == 0;
  226. #endif // ENABLE_WAYLAND
  227. #ifdef ENABLE_X11
  228. bool isX11 = strncmp("x11", XDG_SESSION_TYPE, strlen("x11")) == 0;
  229. #endif // ENABLE_X11
  230. if (this->m_context.settings.render.mode == CApplicationContext::DESKTOP_BACKGROUND) {
  231. #ifdef ENABLE_WAYLAND
  232. if (isWayland) {
  233. videoDriver = new WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver (this->m_context, *this);
  234. inputContext = new WallpaperEngine::Input::CInputContext (
  235. new WallpaperEngine::Input::Drivers::CWaylandMouseInput (reinterpret_cast<WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver*>(videoDriver)));
  236. this->fullScreenDetector = new WallpaperEngine::Render::Drivers::Detectors::CWaylandFullScreenDetector(this->m_context);
  237. }
  238. #endif // ENABLE_WAYLAND
  239. #ifdef ENABLE_X11
  240. #ifdef ENABLE_WAYLAND
  241. else
  242. #endif // ENABLE_WAYLAND
  243. if (isX11)
  244. {
  245. videoDriver = new WallpaperEngine::Render::Drivers::CGLFWOpenGLDriver ("wallpaperengine", this->m_context, *this);
  246. inputContext = new WallpaperEngine::Input::CInputContext (
  247. new WallpaperEngine::Input::Drivers::CGLFWMouseInput (reinterpret_cast <Render::Drivers::CGLFWOpenGLDriver*>(videoDriver)));
  248. this->fullScreenDetector = new WallpaperEngine::Render::Drivers::Detectors::CX11FullScreenDetector (this->m_context, *reinterpret_cast <Render::Drivers::CGLFWOpenGLDriver*>(videoDriver));
  249. }
  250. #endif // ENABLE_X11
  251. else {
  252. sLog.exception("Cannot run in background mode, window server could not be detected. XDG_SESSION_TYPE must be wayland or x11");
  253. }
  254. } else {
  255. videoDriver = new WallpaperEngine::Render::Drivers::CGLFWOpenGLDriver ("wallpaperengine", this->m_context, *this);
  256. #ifdef ENABLE_WAYLAND
  257. if (isWayland) {
  258. this->fullScreenDetector = new WallpaperEngine::Render::Drivers::Detectors::CWaylandFullScreenDetector(this->m_context);
  259. }
  260. #endif // ENABLE_WAYLAND
  261. #ifdef ENABLE_X11
  262. #ifdef ENABLE_WAYLAND
  263. else
  264. #endif // ENABLE_WAYLAND
  265. if(isX11)
  266. {
  267. this->fullScreenDetector = new WallpaperEngine::Render::Drivers::Detectors::CX11FullScreenDetector (this->m_context, *reinterpret_cast <Render::Drivers::CGLFWOpenGLDriver*>(videoDriver));
  268. }
  269. #endif // ENABLE_X11
  270. else {
  271. this->fullScreenDetector = new WallpaperEngine::Render::Drivers::Detectors::CFullScreenDetector (this->m_context);
  272. }
  273. inputContext = new WallpaperEngine::Input::CInputContext (
  274. new WallpaperEngine::Input::Drivers::CGLFWMouseInput (reinterpret_cast <Render::Drivers::CGLFWOpenGLDriver*>(videoDriver)));
  275. }
  276. if (this->m_context.settings.audio.audioprocessing) {
  277. this->audioRecorder = new WallpaperEngine::Audio::Drivers::Recorders::CPulseAudioPlaybackRecorder ();
  278. } else {
  279. this->audioRecorder = new WallpaperEngine::Audio::Drivers::Recorders::CPlaybackRecorder ();
  280. }
  281. // audio playing detector
  282. WallpaperEngine::Audio::Drivers::Detectors::CPulseAudioPlayingDetector audioDetector (
  283. this->m_context, *this->fullScreenDetector);
  284. // initialize sdl audio driver
  285. audioDriver =
  286. new WallpaperEngine::Audio::Drivers::CSDLAudioDriver (this->m_context, audioDetector, *this->audioRecorder);
  287. // initialize audio context
  288. audioContext = new WallpaperEngine::Audio::CAudioContext (*audioDriver);
  289. // initialize render context
  290. context = new WallpaperEngine::Render::CRenderContext (*videoDriver, *inputContext, *this);
  291. // create a new background for each screen
  292. // set all the specific wallpapers required
  293. for (const auto& [background, info] : this->m_backgrounds) {
  294. context->setWallpaper (background, WallpaperEngine::Render::CWallpaper::fromWallpaper (
  295. info->getWallpaper (), *context, *audioContext, browserContext,
  296. this->m_context.settings.general.screenScalings [background]));
  297. }
  298. // wallpapers are setup, free browsesr context if possible
  299. if (!this->browserContext.isUsed ()) {
  300. this->browserContext.stop ();
  301. }
  302. static time_t seconds;
  303. static struct tm* timeinfo;
  304. while (this->m_context.state.general.keepRunning && !videoDriver->closeRequested ()) {
  305. // update g_Daytime
  306. time (&seconds);
  307. timeinfo = localtime (&seconds);
  308. g_Daytime = ((timeinfo->tm_hour * 60) + timeinfo->tm_min) / (24.0 * 60.0);
  309. // keep track of the previous frame's time
  310. g_TimeLast = g_Time;
  311. // calculate the current time value
  312. g_Time = videoDriver->getRenderTime ();
  313. // update audio recorder
  314. audioDriver->update ();
  315. // update input information
  316. inputContext->update ();
  317. // process driver events
  318. videoDriver->dispatchEventQueue ();
  319. if (!this->m_context.settings.screenshot.take || videoDriver->getFrameCounter () < 5)
  320. continue;
  321. this->takeScreenshot (*context, this->m_context.settings.screenshot.path,
  322. this->m_context.settings.screenshot.format);
  323. this->m_context.settings.screenshot.take = false;
  324. }
  325. // ensure this is updated as sometimes it might not come from a signal
  326. this->m_context.state.general.keepRunning = false;
  327. sLog.out ("Stop requested");
  328. SDL_Quit ();
  329. }
  330. void CWallpaperApplication::update (Render::Drivers::Output::COutputViewport* viewport) {
  331. // check for fullscreen windows and wait until there's none fullscreen
  332. while (this->fullScreenDetector->anythingFullscreen () && this->m_context.state.general.keepRunning)
  333. usleep (FULLSCREEN_CHECK_WAIT_TIME);
  334. // render the scene
  335. context->render (viewport);
  336. }
  337. void CWallpaperApplication::signal (int signal) {
  338. this->m_context.state.general.keepRunning = false;
  339. }
  340. const std::map<std::string, Core::CProject*>& CWallpaperApplication::getBackgrounds () const {
  341. return this->m_backgrounds;
  342. }
  343. Core::CProject* CWallpaperApplication::getDefaultBackground () const {
  344. return this->m_defaultBackground;
  345. }
  346. CApplicationContext& CWallpaperApplication::getContext () const {
  347. return this->m_context;
  348. }
  349. const WallpaperEngine::Render::Drivers::Output::COutput& CWallpaperApplication::getOutput () const {
  350. return this->context->getOutput ();
  351. }
  352. } // namespace WallpaperEngine::Application