BsLinuxVideoModeInfo.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. {
  33. XRRFreeCrtcInfo(crtcInfo);
  34. XRRFreeOutputInfo(outputInfo);
  35. continue;
  36. }
  37. VideoOutputInfo* output = bs_new<LinuxVideoOutputInfo>(display, i, outputInfo, crtcInfo, screenRes,
  38. screenRes->outputs[j], (UINT32)mOutputs.size());
  39. // Make sure the primary output is the first in the output list
  40. if(i == defaultScreen && screenRes->outputs[j] == primaryOutput)
  41. mOutputs.insert(mOutputs.begin(), output);
  42. else
  43. mOutputs.push_back(output);
  44. XRRFreeCrtcInfo(crtcInfo);
  45. XRRFreeOutputInfo(outputInfo);
  46. }
  47. XRRFreeScreenResources(screenRes);
  48. }
  49. LinuxPlatform::unlockX();
  50. }
  51. LinuxVideoOutputInfo::LinuxVideoOutputInfo(::Display* x11Display, INT32 screen, XRROutputInfo* outputInfo,
  52. XRRCrtcInfo* crtcInfo, XRRScreenResources* screenRes, RROutput outputID, UINT32 outputIdx)
  53. : mOutputID(outputID), mScreen(screen)
  54. {
  55. RRMode currentMode = crtcInfo->mode;
  56. // Parse output name
  57. Atom EDID = XInternAtom(x11Display, "EDID", False);
  58. INT32 numOutputProps;
  59. Atom* outputProps = XRRListOutputProperties(x11Display, mOutputID, &numOutputProps);
  60. for(INT32 k = 0; k < numOutputProps; k++)
  61. {
  62. if(outputProps[k] != EDID)
  63. continue;
  64. Atom actualType;
  65. unsigned long numItems, bytesAfter;
  66. INT32 actualFormat;
  67. UINT8* data;
  68. Status status = XRRGetOutputProperty(x11Display, mOutputID, outputProps[k], 0, 100, False,
  69. False, AnyPropertyType, &actualType, &actualFormat, &numItems, &bytesAfter, &data);
  70. if(status == Success)
  71. {
  72. // Decode EDID to get the name
  73. for(UINT32 l = 0; l < 4; l++)
  74. {
  75. INT32 idx = 0x36 + l * 18;
  76. if(data[idx] == 0 && data[idx + 1] == 0 && data[idx + 3] == 0xFC)
  77. {
  78. UINT8* nameSrc = &data[idx + 5];
  79. char name[13];
  80. for(UINT32 m = 0; m < 13; m++)
  81. {
  82. if(nameSrc[m] == 0x0a)
  83. {
  84. name[m] = '\0';
  85. break;
  86. }
  87. else if(nameSrc[m] == 0x00)
  88. name[m] = ' ';
  89. else
  90. name[m] = nameSrc[m];
  91. }
  92. mName = String(name);
  93. }
  94. }
  95. continue;
  96. }
  97. XFree(data);
  98. break;
  99. }
  100. XFree(outputProps);
  101. // Use the output name if display name cannot be found
  102. if(mName.empty())
  103. mName = outputInfo->name;
  104. // Enumerate all valid resolutions
  105. for(INT32 k = 0; k < screenRes->nmode; k++)
  106. {
  107. const XRRModeInfo& modeInfo = screenRes->modes[k];
  108. UINT32 width, height;
  109. if(crtcInfo->rotation & (XRANDR_ROTATION_LEFT | XRANDR_ROTATION_RIGHT))
  110. {
  111. width = modeInfo.height;
  112. height = modeInfo.width;
  113. }
  114. else
  115. {
  116. width = modeInfo.width;
  117. height = modeInfo.height;
  118. }
  119. float refreshRate;
  120. if(modeInfo.hTotal != 0 && modeInfo.vTotal != 0)
  121. refreshRate = (float)(modeInfo.dotClock / (double)(modeInfo.hTotal * modeInfo.vTotal));
  122. else
  123. refreshRate = 0.0f;
  124. LinuxVideoMode* videoMode = new (bs_alloc<LinuxVideoMode>())
  125. LinuxVideoMode(width, height, refreshRate, outputIdx, modeInfo.id);
  126. mVideoModes.push_back(videoMode);
  127. }
  128. // Save current desktop mode
  129. for(INT32 k = 0; k < screenRes->nmode; k++)
  130. {
  131. if(screenRes->modes[k].id == currentMode)
  132. {
  133. mDesktopVideoMode = new (bs_alloc<LinuxVideoMode>())
  134. LinuxVideoMode(mVideoModes[k]->getWidth(), mVideoModes[k]->getHeight(),
  135. mVideoModes[k]->getRefreshRate(), mVideoModes[k]->getOutputIdx(), currentMode);
  136. break;
  137. }
  138. }
  139. }
  140. LinuxVideoMode::LinuxVideoMode(UINT32 width, UINT32 height, float refreshRate, UINT32 outputIdx)
  141. :VideoMode(width, height, refreshRate, outputIdx), mModeID((RRMode)-1)
  142. { }
  143. LinuxVideoMode::LinuxVideoMode(UINT32 width, UINT32 height, float refreshRate, UINT32 outputIdx, RRMode modeID)
  144. :VideoMode(width, height, refreshRate, outputIdx), mModeID(modeID)
  145. {
  146. mIsCustom = false;
  147. }
  148. }}