BsLinuxVideoModeInfo.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Linux/BsLinuxVideoModeInfo.h"
  4. #include "Linux/BsLinuxPlatform.h"
  5. #include <X11/extensions/Xrandr.h>
  6. #define XRANDR_ROTATION_LEFT (1 << 1)
  7. #define XRANDR_ROTATION_RIGHT (1 << 3)
  8. namespace bs { namespace ct
  9. {
  10. LinuxVideoModeInfo::LinuxVideoModeInfo()
  11. {
  12. LinuxPlatform::lockX();
  13. ::Display* display = LinuxPlatform::getXDisplay();
  14. INT32 minor, major;
  15. XRRQueryVersion(display, &minor, &major);
  16. INT32 defaultScreen = XDefaultScreen(display);
  17. RROutput primaryOutput = XRRGetOutputPrimary(display, RootWindow(display, defaultScreen));
  18. INT32 screenCount = XScreenCount(display);
  19. for(INT32 i = 0; i < screenCount; i++)
  20. {
  21. XRRScreenResources* screenRes = XRRGetScreenResources(display, RootWindow(display, i));
  22. for(INT32 j = 0; j < screenRes->noutput; j++)
  23. {
  24. XRROutputInfo* outputInfo = XRRGetOutputInfo(display, screenRes, screenRes->outputs[j]);
  25. if(outputInfo == nullptr || outputInfo->crtc == 0 || outputInfo->connection == RR_Disconnected)
  26. {
  27. XRRFreeOutputInfo(outputInfo);
  28. continue;
  29. }
  30. XRRCrtcInfo* crtcInfo = XRRGetCrtcInfo(display, screenRes, outputInfo->crtc);
  31. if(crtcInfo == nullptr)
  32. continue;
  33. VideoOutputInfo* output = bs_new<LinuxVideoOutputInfo>(display, i, outputInfo, crtcInfo, screenRes,
  34. screenRes->outputs[j], (UINT32)mOutputs.size());
  35. // Make sure the primary output is the first in the output list
  36. if(i == defaultScreen && screenRes->outputs[j] == primaryOutput)
  37. mOutputs.insert(mOutputs.begin(), output);
  38. else
  39. mOutputs.push_back(output);
  40. XRRFreeCrtcInfo(crtcInfo);
  41. XRRFreeOutputInfo(outputInfo);
  42. }
  43. XRRFreeScreenResources(screenRes);
  44. }
  45. LinuxPlatform::unlockX();
  46. }
  47. LinuxVideoOutputInfo::LinuxVideoOutputInfo(::Display* x11Display, INT32 screen, XRROutputInfo* outputInfo,
  48. XRRCrtcInfo* crtcInfo, XRRScreenResources* screenRes, RROutput outputID, UINT32 outputIdx)
  49. : mOutputID(outputID), mScreen(screen)
  50. {
  51. RRMode currentMode = crtcInfo->mode;
  52. // Parse output name
  53. Atom EDID = XInternAtom(x11Display, "EDID", False);
  54. INT32 numOutputProps;
  55. Atom* outputProps = XRRListOutputProperties(x11Display, mOutputID, &numOutputProps);
  56. for(INT32 k = 0; k < numOutputProps; k++)
  57. {
  58. if(outputProps[k] != EDID)
  59. continue;
  60. Atom actualType;
  61. unsigned long numItems, bytesAfter;
  62. INT32 actualFormat;
  63. UINT8* data;
  64. Status status = XRRGetOutputProperty(x11Display, mOutputID, outputProps[k], 0, 100, False,
  65. False, AnyPropertyType, &actualType, &actualFormat, &numItems, &bytesAfter, &data);
  66. if(status == Success)
  67. {
  68. // Decode EDID to get the name
  69. for(UINT32 l = 0; l < 4; l++)
  70. {
  71. INT32 idx = 0x36 + l * 18;
  72. if(data[idx] == 0 && data[idx + 1] == 0 && data[idx + 3] == 0xFC)
  73. {
  74. UINT8* nameSrc = &data[idx + 5];
  75. char name[13];
  76. for(UINT32 m = 0; m < 13; m++)
  77. {
  78. if(nameSrc[m] == 0x0a)
  79. {
  80. name[m] = '\0';
  81. break;
  82. }
  83. else if(nameSrc[m] == 0x00)
  84. name[m] = ' ';
  85. else
  86. name[m] = nameSrc[m];
  87. }
  88. mName = String(name);
  89. }
  90. }
  91. continue;
  92. }
  93. XFree(data);
  94. break;
  95. }
  96. XFree(outputProps);
  97. // Use the output name if display name cannot be found
  98. if(mName.empty())
  99. mName = outputInfo->name;
  100. // Enumerate all valid resolutions
  101. for(INT32 k = 0; k < screenRes->nmode; k++)
  102. {
  103. const XRRModeInfo& modeInfo = screenRes->modes[k];
  104. UINT32 width, height;
  105. if(crtcInfo->rotation & (XRANDR_ROTATION_LEFT | XRANDR_ROTATION_RIGHT))
  106. {
  107. width = modeInfo.height;
  108. height = modeInfo.width;
  109. }
  110. else
  111. {
  112. width = modeInfo.width;
  113. height = modeInfo.height;
  114. }
  115. float refreshRate;
  116. if(modeInfo.hTotal != 0 && modeInfo.vTotal != 0)
  117. refreshRate = (float)(modeInfo.dotClock / (double)(modeInfo.hTotal * modeInfo.vTotal));
  118. else
  119. refreshRate = 0.0f;
  120. LinuxVideoMode* videoMode = new (bs_alloc<LinuxVideoMode>())
  121. LinuxVideoMode(width, height, refreshRate, outputIdx, modeInfo.id);
  122. mVideoModes.push_back(videoMode);
  123. }
  124. // Save current desktop mode
  125. for(INT32 k = 0; k < screenRes->nmode; k++)
  126. {
  127. if(screenRes->modes[k].id == currentMode)
  128. {
  129. mDesktopVideoMode = new (bs_alloc<LinuxVideoMode>())
  130. LinuxVideoMode(mVideoModes[k]->getWidth(), mVideoModes[k]->getHeight(),
  131. mVideoModes[k]->getRefreshRate(), mVideoModes[k]->getOutputIdx(), currentMode);
  132. break;
  133. }
  134. }
  135. }
  136. LinuxVideoMode::LinuxVideoMode(UINT32 width, UINT32 height, float refreshRate, UINT32 outputIdx)
  137. :VideoMode(width, height, refreshRate, outputIdx), mModeID((RRMode)-1)
  138. { }
  139. LinuxVideoMode::LinuxVideoMode(UINT32 width, UINT32 height, float refreshRate, UINT32 outputIdx, RRMode modeID)
  140. :VideoMode(width, height, refreshRate, outputIdx), mModeID(modeID)
  141. {
  142. mIsCustom = false;
  143. }
  144. }}