PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/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////////////////////////////////////////////////////////////
 26// Headers
 27////////////////////////////////////////////////////////////
 28#include <SFML/Window/VideoModeImpl.hpp>
 29#include <SFML/System/Err.hpp>
 30#include <X11/Xlib.h>
 31#include <X11/extensions/Xrandr.h>
 32#include <algorithm>
 33
 34
 35namespace sf
 36{
 37namespace priv
 38{
 39////////////////////////////////////////////////////////////
 40std::vector<VideoMode> VideoModeImpl::GetFullscreenModes()
 41{
 42    std::vector<VideoMode> modes;
 43
 44    // Open a connection with the X server
 45    Display* disp = XOpenDisplay(NULL);
 46    if (disp)
 47    {
 48        // Retrieve the default screen number
 49        int screen = DefaultScreen(disp);
 50
 51        // Check if the XRandR extension is present
 52        int version;
 53        if (XQueryExtension(disp, "RANDR", &version, &version, &version))
 54        {
 55            // Get the current configuration
 56            XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen));
 57            if (config)
 58            {
 59                // Get the available screen sizes
 60                int nbSizes;
 61                XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes);
 62                if (sizes && (nbSizes > 0))
 63                {
 64                    // Get the list of supported depths
 65                    int nbDepths = 0;
 66                    int* depths = XListDepths(disp, screen, &nbDepths);
 67                    if (depths && (nbDepths > 0))
 68                    {
 69                        // Combine depths and sizes to fill the array of supported modes
 70                        for (int i = 0; i < nbDepths; ++i)
 71                        {
 72                            for (int j = 0; j < nbSizes; ++j)
 73                            {
 74                                // Convert to VideoMode
 75                                VideoMode mode(sizes[j].width, sizes[j].height, depths[i]);
 76            
 77                                // Add it only if it is not already in the array
 78                                if (std::find(modes.begin(), modes.end(), mode) == modes.end())
 79                                    modes.push_back(mode);
 80                            }
 81                        }
 82
 83                        // Free the array of depths
 84                        XFree(depths);
 85                    }
 86                }
 87
 88                // Free the configuration instance
 89                XRRFreeScreenConfigInfo(config);
 90            }
 91            else
 92            {
 93                // Failed to get the screen configuration
 94                Err() << "Failed to retrieve the screen configuration while trying to get the supported video modes" << std::endl;
 95            }
 96        }
 97        else
 98        {
 99            // XRandr extension is not supported : we cannot get the video modes
100            Err() << "Failed to use the XRandR extension while trying to get the supported video modes" << std::endl;
101        }
102
103        // Close the connection with the X server
104        XCloseDisplay(disp);
105    }
106    else
107    {
108        // We couldn't connect to the X server
109        Err() << "Failed to connect to the X server while trying to get the supported video modes" << std::endl;
110    }
111
112    return modes;
113}
114
115
116////////////////////////////////////////////////////////////
117VideoMode VideoModeImpl::GetDesktopMode()
118{
119    VideoMode desktopMode;
120
121    // Open a connection with the X server
122    Display* disp = XOpenDisplay(NULL);
123    if (disp)
124    {
125        // Retrieve the default screen number
126        int screen = DefaultScreen(disp);
127
128        // Check if the XRandR extension is present
129        int version;
130        if (XQueryExtension(disp, "RANDR", &version, &version, &version))
131        {
132            // Get the current configuration
133            XRRScreenConfiguration* config = XRRGetScreenInfo(disp, RootWindow(disp, screen));
134            if (config)
135            {
136                // Get the current video mode
137                Rotation currentRotation;
138                int currentMode = XRRConfigCurrentConfiguration(config, &currentRotation);
139
140                // Get the available screen sizes
141                int nbSizes;
142                XRRScreenSize* sizes = XRRConfigSizes(config, &nbSizes);
143                if (sizes && (nbSizes > 0))
144                    desktopMode = VideoMode(sizes[currentMode].width, sizes[currentMode].height, DefaultDepth(disp, screen));
145
146                // Free the configuration instance
147                XRRFreeScreenConfigInfo(config);
148            }
149            else
150            {
151                // Failed to get the screen configuration
152                Err() << "Failed to retrieve the screen configuration while trying to get the desktop video modes" << std::endl;
153            }
154        }
155        else
156        {
157            // XRandr extension is not supported : we cannot get the video modes
158            Err() << "Failed to use the XRandR extension while trying to get the desktop video modes" << std::endl;
159        }
160
161        // Close the connection with the X server
162        XCloseDisplay(disp);
163    }
164    else
165    {
166        // We couldn't connect to the X server
167        Err() << "Failed to connect to the X server while trying to get the desktop video modes" << std::endl;
168    }
169
170    return desktopMode;
171}
172
173} // namespace priv
174
175} // namespace sf