BsLinuxVideoModeInfo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, outputInfo, crtcInfo, screenRes, j,
  34. (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, XRROutputInfo* outputInfo, XRRCrtcInfo* crtcInfo,
  48. XRRScreenResources* screenRes, UINT32 resIdx, UINT32 outputIdx)
  49. {
  50. RRMode currentMode = crtcInfo->mode;
  51. // Parse output name
  52. Atom EDID = XInternAtom(x11Display, "EDID", False);
  53. INT32 numOutputProps;
  54. Atom* outputProps = XRRListOutputProperties(x11Display, screenRes->outputs[resIdx], &numOutputProps);
  55. for(INT32 k = 0; k < numOutputProps; k++)
  56. {
  57. if(outputProps[k] != EDID)
  58. continue;
  59. Atom actualType;
  60. unsigned long numItems, bytesAfter;
  61. INT32 actualFormat;
  62. UINT8* data;
  63. Status status = XRRGetOutputProperty(x11Display, screenRes->outputs[resIdx], outputProps[k], 0, 100, False,
  64. False, AnyPropertyType, &actualType, &actualFormat, &numItems,
  65. &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. mVideoModes.push_back(bs_new<LinuxVideoMode>(width, height, refreshRate, outputIdx));
  121. }
  122. // Save current desktop mode
  123. for(INT32 k = 0; k < screenRes->nmode; k++)
  124. {
  125. if(screenRes->modes[k].id == currentMode)
  126. {
  127. mDesktopVideoMode = bs_new<LinuxVideoMode>(mVideoModes[k]->getWidth(), mVideoModes[k]->getHeight(),
  128. mVideoModes[k]->getRefreshRate(), mVideoModes[k]->getOutputIdx());
  129. break;
  130. }
  131. }
  132. }
  133. LinuxVideoMode::LinuxVideoMode(UINT32 width, UINT32 height, float refreshRate, UINT32 outputIdx)
  134. :VideoMode(width, height, refreshRate, outputIdx)
  135. { }
  136. }}