/extlibs/SFML/src/SFML/Window/Linux/VideoModeImpl.cpp

https://bitbucket.org/hugoruscitti/pilascpp · C++ · 175 lines · 101 code · 20 blank · 54 comment · 16 complexity · e643ef375c1f5ff0081feb679216e18c MD5 · raw file

  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment
  16. // in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. ////////////////////////////////////////////////////////////
  25. // Headers
  26. ////////////////////////////////////////////////////////////
  27. #include <SFML/Window/VideoModeImpl.hpp>
  28. #include <SFML/System/Err.hpp>
  29. #include <X11/Xlib.h>
  30. #include <X11/extensions/Xrandr.h>
  31. #include <algorithm>
  32. namespace sf
  33. {
  34. namespace priv
  35. {
  36. ////////////////////////////////////////////////////////////
  37. std::vector<VideoMode> VideoModeImpl::GetFullscreenModes()
  38. {
  39. std::vector<VideoMode> modes;
  40. // Open a connection with the X server
  41. Display* disp = XOpenDisplay(NULL);
  42. if (disp)
  43. {
  44. // Retrieve the default screen number
  45. int screen = DefaultScreen(disp);
  46. // Check if the XRandR extension is present
  47. int version;
  48. if (XQueryExtension(disp, "RANDR", &version, &version, &version))
  49. {
  50. // Get the current configuration
  51. XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen));
  52. if (config)
  53. {
  54. // Get the available screen sizes
  55. int nbSizes;
  56. XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes);
  57. if (sizes && (nbSizes > 0))
  58. {
  59. // Get the list of supported depths
  60. int nbDepths = 0;
  61. int* depths = XListDepths(disp, screen, &nbDepths);
  62. if (depths && (nbDepths > 0))
  63. {
  64. // Combine depths and sizes to fill the array of supported modes
  65. for (int i = 0; i < nbDepths; ++i)
  66. {
  67. for (int j = 0; j < nbSizes; ++j)
  68. {
  69. // Convert to VideoMode
  70. VideoMode mode(sizes[j].width, sizes[j].height, depths[i]);
  71. // Add it only if it is not already in the array
  72. if (std::find(modes.begin(), modes.end(), mode) == modes.end())
  73. modes.push_back(mode);
  74. }
  75. }
  76. // Free the array of depths
  77. XFree(depths);
  78. }
  79. }
  80. // Free the configuration instance
  81. XRRFreeScreenConfigInfo(config);
  82. }
  83. else
  84. {
  85. // Failed to get the screen configuration
  86. Err() << "Failed to retrieve the screen configuration while trying to get the supported video modes" << std::endl;
  87. }
  88. }
  89. else
  90. {
  91. // XRandr extension is not supported : we cannot get the video modes
  92. Err() << "Failed to use the XRandR extension while trying to get the supported video modes" << std::endl;
  93. }
  94. // Close the connection with the X server
  95. XCloseDisplay(disp);
  96. }
  97. else
  98. {
  99. // We couldn't connect to the X server
  100. Err() << "Failed to connect to the X server while trying to get the supported video modes" << std::endl;
  101. }
  102. return modes;
  103. }
  104. ////////////////////////////////////////////////////////////
  105. VideoMode VideoModeImpl::GetDesktopMode()
  106. {
  107. VideoMode desktopMode;
  108. // Open a connection with the X server
  109. Display* disp = XOpenDisplay(NULL);
  110. if (disp)
  111. {
  112. // Retrieve the default screen number
  113. int screen = DefaultScreen(disp);
  114. // Check if the XRandR extension is present
  115. int version;
  116. if (XQueryExtension(disp, "RANDR", &version, &version, &version))
  117. {
  118. // Get the current configuration
  119. XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen));
  120. if (config)
  121. {
  122. // Get the current video mode
  123. Rotation currentRotation;
  124. int currentMode = XRRConfigCurrentConfiguration(config, &currentRotation);
  125. // Get the available screen sizes
  126. int nbSizes;
  127. XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes);
  128. if (sizes && (nbSizes > 0))
  129. desktopMode = VideoMode(sizes[currentMode].width, sizes[currentMode].height, DefaultDepth(disp, screen));
  130. // Free the configuration instance
  131. XRRFreeScreenConfigInfo(config);
  132. }
  133. else
  134. {
  135. // Failed to get the screen configuration
  136. Err() << "Failed to retrieve the screen configuration while trying to get the desktop video modes" << std::endl;
  137. }
  138. }
  139. else
  140. {
  141. // XRandr extension is not supported : we cannot get the video modes
  142. Err() << "Failed to use the XRandR extension while trying to get the desktop video modes" << std::endl;
  143. }
  144. // Close the connection with the X server
  145. XCloseDisplay(disp);
  146. }
  147. else
  148. {
  149. // We couldn't connect to the X server
  150. Err() << "Failed to connect to the X server while trying to get the desktop video modes" << std::endl;
  151. }
  152. return desktopMode;
  153. }
  154. } // namespace priv
  155. } // namespace sf