From 42a3027d7c4af4175681b27839fa677a8e0ef4ec Mon Sep 17 00:00:00 2001 From: laurentgom Date: Fri, 24 Jul 2009 12:56:04 +0000 Subject: [PATCH] The internal code of VideoMode now uses its own connection to the display instead of a global one (on Linux) git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1190 4e206d99-4929-0410-ac5d-dfc041789085 --- src/SFML/Window/Linux/VideoModeSupport.cpp | 157 +++++++++++++-------- 1 file changed, 95 insertions(+), 62 deletions(-) diff --git a/src/SFML/Window/Linux/VideoModeSupport.cpp b/src/SFML/Window/Linux/VideoModeSupport.cpp index 99c76ffe..311139ba 100644 --- a/src/SFML/Window/Linux/VideoModeSupport.cpp +++ b/src/SFML/Window/Linux/VideoModeSupport.cpp @@ -26,7 +26,6 @@ // Headers //////////////////////////////////////////////////////////// #include -#include #include #include #include @@ -45,58 +44,70 @@ void VideoModeSupport::GetSupportedVideoModes(std::vector& modes) // First, clear array to fill modes.clear(); - // Get an access to the display - DisplayRef disp; - int screen = DefaultScreen(disp.GetDisplay()); + // Open a connection with the X server + Display* disp = XOpenDisplay(NULL); + if (disp) + { + // Retrieve the default screen number + int screen = DefaultScreen(disp); - // Check if the XRandR extension is present - int version; - if (XQueryExtension(disp.GetDisplay(), "RANDR", &version, &version, &version)) - { - // Get the current configuration - XRRScreenConfiguration* config = XRRGetScreenInfo(disp.GetDisplay(), RootWindow(disp.GetDisplay(), screen)); - if (config) + // Check if the XRandR extension is present + int version; + if (XQueryExtension(disp, "RANDR", &version, &version, &version)) { - // Get the available screen sizes - int nbSizes; - XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes); - if (sizes && (nbSizes > 0)) + // Get the current configuration + XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen)); + if (config) { - // Get the list of supported depths - int nbDepths = 0; - int* depths = XListDepths(disp.GetDisplay(), screen, &nbDepths); - if (depths && (nbDepths > 0)) + // Get the available screen sizes + int nbSizes; + XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes); + if (sizes && (nbSizes > 0)) { - // Combine depths and sizes to fill the array of supported modes - for (int i = 0; i < nbDepths; ++i) + // Get the list of supported depths + int nbDepths = 0; + int* depths = XListDepths(disp, screen, &nbDepths); + if (depths && (nbDepths > 0)) { - for (int j = 0; j < nbSizes; ++j) + // Combine depths and sizes to fill the array of supported modes + for (int i = 0; i < nbDepths; ++i) { - // Convert to VideoMode - VideoMode mode(sizes[j].width, sizes[j].height, depths[i]); - - // Add it only if it is not already in the array - if (std::find(modes.begin(), modes.end(), mode) == modes.end()) - modes.push_back(mode); + for (int j = 0; j < nbSizes; ++j) + { + // Convert to VideoMode + VideoMode mode(sizes[j].width, sizes[j].height, depths[i]); + + // Add it only if it is not already in the array + if (std::find(modes.begin(), modes.end(), mode) == modes.end()) + modes.push_back(mode); + } } } } - } - // Free the configuration instance - XRRFreeScreenConfigInfo(config); + // Free the configuration instance + XRRFreeScreenConfigInfo(config); + } + else + { + // Failed to get the screen configuration + std::cerr << "Failed to retrieve the screen configuration while trying to get the supported video modes" << std::endl; + } } else { - // Failed to get the screen configuration - std::cerr << "Failed to get the list of available video modes" << std::endl; - } - } - else - { - // XRandr extension is not supported : we cannot get the video modes - std::cerr << "Failed to get the list of available video modes" << std::endl; - } + // XRandr extension is not supported : we cannot get the video modes + std::cerr << "Failed to use the XRandR extension while trying to get the supported video modes" << std::endl; + } + + // Close the connection with the X server + XCloseDisplay(disp); + } + else + { + // We couldn't connect to the X server + std::cerr << "Failed to connect to the X server while trying to get the supported video modes" << std::endl; + } } @@ -107,33 +118,55 @@ VideoMode VideoModeSupport::GetDesktopVideoMode() { VideoMode desktopMode; - // Get an access to the display - DisplayRef disp; - int screen = DefaultScreen(disp.GetDisplay()); + // Open a connection with the X server + Display* disp = XOpenDisplay(NULL); + if (disp) + { + // Retrieve the default screen number + int screen = DefaultScreen(disp); - // Check if the XRandR extension is present - int version; - if (XQueryExtension(disp.GetDisplay(), "RANDR", &version, &version, &version)) - { - // Get the current configuration - XRRScreenConfiguration* config = XRRGetScreenInfo(disp.GetDisplay(), RootWindow(disp.GetDisplay(), screen)); - if (config) + // Check if the XRandR extension is present + int version; + if (XQueryExtension(disp, "RANDR", &version, &version, &version)) { - // Get the current video mode - Rotation currentRotation; - int currentMode = XRRConfigCurrentConfiguration(config, ¤tRotation); + // Get the current configuration + XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen)); + if (config) + { + // Get the current video mode + Rotation currentRotation; + int currentMode = XRRConfigCurrentConfiguration(config, ¤tRotation); - // Get the available screen sizes - int nbSizes; - XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes); - if (sizes && (nbSizes > 0)) - desktopMode = VideoMode(sizes[currentMode].width, sizes[currentMode].height, DefaultDepth(disp.GetDisplay(), screen)); + // Get the available screen sizes + int nbSizes; + XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes); + if (sizes && (nbSizes > 0)) + desktopMode = VideoMode(sizes[currentMode].width, sizes[currentMode].height, DefaultDepth(disp, screen)); - // Free the configuration instance - XRRFreeScreenConfigInfo(config); + // Free the configuration instance + XRRFreeScreenConfigInfo(config); + } + else + { + // Failed to get the screen configuration + std::cerr << "Failed to retrieve the screen configuration while trying to get the desktop video modes" << std::endl; + } } - } - + else + { + // XRandr extension is not supported : we cannot get the video modes + std::cerr << "Failed to use the XRandR extension while trying to get the desktop video modes" << std::endl; + } + + // Close the connection with the X server + XCloseDisplay(disp); + } + else + { + // We couldn't connect to the X server + std::cerr << "Failed to connect to the X server while trying to get the desktop video modes" << std::endl; + } + return desktopMode; }