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
This commit is contained in:
laurentgom 2009-07-24 12:56:04 +00:00
parent 9fe4456e2c
commit 42a3027d7c

View File

@ -26,7 +26,6 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/Linux/VideoModeSupport.hpp> #include <SFML/Window/Linux/VideoModeSupport.hpp>
#include <SFML/Window/Linux/DisplayRef.hpp>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h> #include <X11/extensions/Xrandr.h>
#include <algorithm> #include <algorithm>
@ -45,16 +44,19 @@ void VideoModeSupport::GetSupportedVideoModes(std::vector<VideoMode>& modes)
// First, clear array to fill // First, clear array to fill
modes.clear(); modes.clear();
// Get an access to the display // Open a connection with the X server
DisplayRef disp; Display* disp = XOpenDisplay(NULL);
int screen = DefaultScreen(disp.GetDisplay()); if (disp)
{
// Retrieve the default screen number
int screen = DefaultScreen(disp);
// Check if the XRandR extension is present // Check if the XRandR extension is present
int version; int version;
if (XQueryExtension(disp.GetDisplay(), "RANDR", &version, &version, &version)) if (XQueryExtension(disp, "RANDR", &version, &version, &version))
{ {
// Get the current configuration // Get the current configuration
XRRScreenConfiguration* config = XRRGetScreenInfo(disp.GetDisplay(), RootWindow(disp.GetDisplay(), screen)); XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen));
if (config) if (config)
{ {
// Get the available screen sizes // Get the available screen sizes
@ -64,7 +66,7 @@ void VideoModeSupport::GetSupportedVideoModes(std::vector<VideoMode>& modes)
{ {
// Get the list of supported depths // Get the list of supported depths
int nbDepths = 0; int nbDepths = 0;
int* depths = XListDepths(disp.GetDisplay(), screen, &nbDepths); int* depths = XListDepths(disp, screen, &nbDepths);
if (depths && (nbDepths > 0)) if (depths && (nbDepths > 0))
{ {
// Combine depths and sizes to fill the array of supported modes // Combine depths and sizes to fill the array of supported modes
@ -89,13 +91,22 @@ void VideoModeSupport::GetSupportedVideoModes(std::vector<VideoMode>& modes)
else else
{ {
// Failed to get the screen configuration // Failed to get the screen configuration
std::cerr << "Failed to get the list of available video modes" << std::endl; std::cerr << "Failed to retrieve the screen configuration while trying to get the supported video modes" << std::endl;
} }
} }
else else
{ {
// XRandr extension is not supported : we cannot get the video modes // XRandr extension is not supported : we cannot get the video modes
std::cerr << "Failed to get the list of available video modes" << std::endl; 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,16 +118,19 @@ VideoMode VideoModeSupport::GetDesktopVideoMode()
{ {
VideoMode desktopMode; VideoMode desktopMode;
// Get an access to the display // Open a connection with the X server
DisplayRef disp; Display* disp = XOpenDisplay(NULL);
int screen = DefaultScreen(disp.GetDisplay()); if (disp)
{
// Retrieve the default screen number
int screen = DefaultScreen(disp);
// Check if the XRandR extension is present // Check if the XRandR extension is present
int version; int version;
if (XQueryExtension(disp.GetDisplay(), "RANDR", &version, &version, &version)) if (XQueryExtension(disp, "RANDR", &version, &version, &version))
{ {
// Get the current configuration // Get the current configuration
XRRScreenConfiguration* config = XRRGetScreenInfo(disp.GetDisplay(), RootWindow(disp.GetDisplay(), screen)); XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen));
if (config) if (config)
{ {
// Get the current video mode // Get the current video mode
@ -127,11 +141,30 @@ VideoMode VideoModeSupport::GetDesktopVideoMode()
int nbSizes; int nbSizes;
XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes); XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes);
if (sizes && (nbSizes > 0)) if (sizes && (nbSizes > 0))
desktopMode = VideoMode(sizes[currentMode].width, sizes[currentMode].height, DefaultDepth(disp.GetDisplay(), screen)); desktopMode = VideoMode(sizes[currentMode].width, sizes[currentMode].height, DefaultDepth(disp, screen));
// Free the configuration instance // Free the configuration instance
XRRFreeScreenConfigInfo(config); 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; return desktopMode;