CContext.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <iostream>
  2. #include <X11/Xlib.h>
  3. #include <X11/extensions/Xrandr.h>
  4. #include <GL/glew.h>
  5. #include <GL/glx.h>
  6. #include "CContext.h"
  7. #include "CVideo.h"
  8. using namespace WallpaperEngine::Render;
  9. CContext::CContext (std::vector <std::string> screens) :
  10. m_wallpaper (nullptr),
  11. m_screens (std::move (screens)),
  12. m_isRootWindow (m_screens.empty () == false),
  13. m_defaultViewport ({0, 0, 1920, 1080})
  14. {
  15. this->initializeViewports ();
  16. }
  17. void CContext::initializeViewports ()
  18. {
  19. if (this->m_isRootWindow == false || this->m_screens.empty () == true)
  20. return;
  21. Display* display = XOpenDisplay (nullptr);
  22. int xrandr_result, xrandr_error;
  23. if (!XRRQueryExtension (display, &xrandr_result, &xrandr_error))
  24. {
  25. std::cerr << "XRandr is not present, cannot detect specified screens, running in window mode" << std::endl;
  26. return;
  27. }
  28. int fullWidth = DisplayWidth (display, DefaultScreen (display));
  29. int fullHeight = DisplayHeight (display, DefaultScreen (display));
  30. XRRScreenResources* screenResources = XRRGetScreenResources (display, DefaultRootWindow (display));
  31. // there are some situations where xrandr returns null (like screen not using the extension)
  32. if (screenResources == nullptr)
  33. return;
  34. for (int i = 0; i < screenResources->noutput; i ++)
  35. {
  36. XRROutputInfo* info = XRRGetOutputInfo (display, screenResources, screenResources->outputs [i]);
  37. // there are some situations where xrandr returns null (like screen not using the extension)
  38. if (info == nullptr)
  39. continue;
  40. auto cur = this->m_screens.begin ();
  41. auto end = this->m_screens.end ();
  42. for (; cur != end; cur ++)
  43. {
  44. if (info->connection == RR_Connected && strcmp (info->name, (*cur).c_str ()) == 0)
  45. {
  46. XRRCrtcInfo* crtc = XRRGetCrtcInfo (display, screenResources, info->crtc);
  47. std::cout << "Found requested screen: " << info->name << " -> " << crtc->x << "x" << crtc->y << ":" << crtc->width << "x" << crtc->height << std::endl;
  48. glm::vec4 viewport = {
  49. crtc->x, fullHeight - (crtc->y + crtc->height), crtc->width, crtc->height
  50. };
  51. this->m_viewports.push_back (viewport);
  52. XRRFreeCrtcInfo (crtc);
  53. }
  54. }
  55. XRRFreeOutputInfo (info);
  56. }
  57. XRRFreeScreenResources (screenResources);
  58. // set the
  59. glfwWindowHintPointer (GLFW_NATIVE_PARENT_HANDLE, reinterpret_cast <void*> (DefaultRootWindow (display)));
  60. }
  61. void CContext::render ()
  62. {
  63. if (this->m_wallpaper == nullptr)
  64. return;
  65. if (this->m_viewports.empty () == false)
  66. {
  67. bool firstFrame = true;
  68. auto cur = this->m_viewports.begin ();
  69. auto end = this->m_viewports.end ();
  70. for (; cur != end; cur ++)
  71. {
  72. this->m_wallpaper->render (*cur, firstFrame);
  73. // scenes need to render a new frame for each viewport as they produce different results
  74. // but videos should only be rendered once per group of viewports
  75. firstFrame = !this->m_wallpaper->is <CVideo> ();
  76. }
  77. }
  78. else
  79. this->m_wallpaper->render (this->m_defaultViewport);
  80. }
  81. void CContext::setWallpaper (CWallpaper* wallpaper)
  82. {
  83. this->m_wallpaper = wallpaper;
  84. }
  85. void CContext::setDefaultViewport (glm::vec4 defaultViewport)
  86. {
  87. this->m_defaultViewport = defaultViewport;
  88. }
  89. CMouseInput* CContext::getMouse () const
  90. {
  91. return this->m_mouse;
  92. }
  93. void CContext::setMouse (CMouseInput* mouse)
  94. {
  95. this->m_mouse = mouse;
  96. }