Back to index

libsfml  1.6+dfsg2
VideoModeSupport.cpp
Go to the documentation of this file.
00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Window/Linux/VideoModeSupport.hpp>
00029 #include <SFML/Window/Linux/WindowImplX11.hpp>
00030 #include <X11/Xlib.h>
00031 #include <X11/extensions/Xrandr.h>
00032 #include <algorithm>
00033 #include <iostream>
00034 
00035 
00036 namespace sf
00037 {
00038 namespace priv
00039 {
00043 void VideoModeSupport::GetSupportedVideoModes(std::vector<VideoMode>& Modes)
00044 {
00045     // First, clear array to fill
00046     Modes.clear();
00047 
00048     // Get the display and screen from WindowImplUnix
00049     WindowImplX11::OpenDisplay(false);
00050     Display* Disp   = WindowImplX11::ourDisplay;
00051     int      Screen = WindowImplX11::ourScreen;
00052 
00053     // Check if the XRandR extension is present
00054     int Version;
00055     if (XQueryExtension(Disp, "RANDR", &Version, &Version, &Version))
00056     {
00057         // Get the current configuration
00058         XRRScreenConfiguration* Config = XRRGetScreenInfo(Disp, RootWindow(Disp, Screen));
00059         if (Config)
00060         {
00061             // Get the available screen sizes
00062             int NbSizes;
00063             XRRScreenSize* Sizes = XRRConfigSizes(Config, &NbSizes);
00064             if (Sizes && (NbSizes > 0))
00065             {
00066                 // Get the list of supported depths
00067                 int NbDepths = 0;
00068                 int* Depths = XListDepths(Disp, Screen, &NbDepths);
00069                 if (Depths && (NbDepths > 0))
00070                 {
00071                     // Combine depths and sizes to fill the array of supported modes
00072                     for (int i = 0; i < NbDepths; ++i)
00073                     {
00074                         for (int j = 0; j < NbSizes; ++j)
00075                         {
00076                             // Convert to VideoMode
00077                             VideoMode Mode(Sizes[j].width, Sizes[j].height, Depths[i]);
00078         
00079                             // Add it only if it is not already in the array
00080                             if (std::find(Modes.begin(), Modes.end(), Mode) == Modes.end())
00081                                 Modes.push_back(Mode);
00082                         }
00083                     }
00084                 }
00085             }
00086 
00087             // Free the configuration instance
00088             XRRFreeScreenConfigInfo(Config);
00089         }
00090         else
00091         {
00092             // Failed to get the screen configuration
00093             std::cerr << "Failed to get the list of available video modes" << std::endl;
00094         }
00095     }
00096     else
00097     {
00098         // XRandr extension is not supported : we cannot get the video modes
00099         std::cerr << "Failed to get the list of available video modes" << std::endl;
00100     }
00101 }
00102 
00103 
00107 VideoMode VideoModeSupport::GetDesktopVideoMode()
00108 {
00109     VideoMode DesktopMode;
00110 
00111     // Get the display and screen from WindowImplUnix
00112     WindowImplX11::OpenDisplay(false);
00113     Display* Disp   = WindowImplX11::ourDisplay;
00114     int      Screen = WindowImplX11::ourScreen;
00115 
00116     // Check if the XRandR extension is present
00117     int Version;
00118     if (XQueryExtension(Disp, "RANDR", &Version, &Version, &Version))
00119     {
00120         // Get the current configuration
00121         XRRScreenConfiguration* Config = XRRGetScreenInfo(Disp, RootWindow(Disp, Screen));
00122         if (Config)
00123         {
00124             // Get the current video mode
00125             Rotation CurrentRotation;
00126             int CurrentMode = XRRConfigCurrentConfiguration(Config, &CurrentRotation);
00127 
00128             // Get the available screen sizes
00129             int NbSizes;
00130             XRRScreenSize* Sizes = XRRConfigSizes(Config, &NbSizes);
00131             if (Sizes && (NbSizes > 0))
00132                 DesktopMode = VideoMode(Sizes[CurrentMode].width, Sizes[CurrentMode].height, DefaultDepth(Disp, Screen));
00133 
00134             // Free the configuration instance
00135             XRRFreeScreenConfigInfo(Config);
00136         }
00137     }
00138 
00139     return DesktopMode;
00140 }
00141 
00142 } // namespace priv
00143 
00144 } // namespace sf