summaryrefslogtreecommitdiff
path: root/src/graphics/X11Display.cpp
blob: b6a35100d0903906002979b49a618d57f137366d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "X11Display.h"

#include "../base/Exception.h"
#include "../base/Logger.h"

#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>

#include <boost/math/special_functions/fpclassify.hpp>


namespace avg {

X11Display::X11Display()
{
}

X11Display::~X11Display()
{
}
 
float X11Display::queryPPMM()
{
    ::Display * pDisplay = XOpenDisplay(0);
    float ppmm = getScreenResolution().x/float(DisplayWidthMM(pDisplay, 0));
    XCloseDisplay(pDisplay);
    return ppmm;
}

IntPoint X11Display::queryScreenResolution()
{
    IntPoint size;
    // Xinerama query has been removed from here in order to fix #431
    ::Display * pDisplay = XOpenDisplay(0);

    Screen* pScreen = DefaultScreenOfDisplay(pDisplay);
    AVG_ASSERT(pScreen);
    size = IntPoint(pScreen->width, pScreen->height);

    XCloseDisplay(pDisplay);
    return size;
}

float X11Display::queryRefreshRate()
{
    ::Display * pDisplay = XOpenDisplay(0);
    int pixelClock;
    XF86VidModeModeLine modeLine;
    bool bOK = XF86VidModeGetModeLine(pDisplay, DefaultScreen(pDisplay), 
            &pixelClock, &modeLine);
    if (!bOK) {
        AVG_LOG_WARNING(
                "Could not get current refresh rate (XF86VidModeGetModeLine failed).");
        AVG_LOG_WARNING("Defaulting to 60 Hz refresh rate.");
        return 60;
    }
    float hSyncRate = (pixelClock * 1000.0) / modeLine.htotal;
    float refreshRate = hSyncRate / modeLine.vtotal;
    XCloseDisplay(pDisplay);
    if ( refreshRate < 20 || refreshRate > 200 || !(boost::math::isnormal(refreshRate))){
        AVG_LOG_WARNING("Could not get valid refresh rate");
        AVG_LOG_WARNING("Defaulting to 60 Hz refresh rate.");
        return 60;
    }
    return refreshRate;
}

::Display* getX11Display(const SDL_SysWMinfo* pSDLWMInfo)
{
    ::Display* pDisplay;
    if (pSDLWMInfo) {
        // SDL window exists, use it.
        pDisplay = pSDLWMInfo->info.x11.display;
    } else {
        pDisplay = XOpenDisplay(0);
    }
    if (!pDisplay) {
        throw Exception(AVG_ERR_VIDEO_GENERAL, "No X windows display available.");
    }
    return pDisplay;
}

Window createChildWindow(const SDL_SysWMinfo* pSDLWMInfo, XVisualInfo* pVisualInfo,
        const IntPoint& windowSize, Colormap& colormap)

{
    // Create a child window with the required attributes to render into.
    XSetWindowAttributes swa;
    ::Display* pDisplay = pSDLWMInfo->info.x11.display;
    colormap = XCreateColormap(pDisplay, RootWindow(pDisplay, pVisualInfo->screen),
            pVisualInfo->visual, AllocNone);
    swa.colormap = colormap;
    swa.background_pixmap = None;
    swa.event_mask = StructureNotifyMask; 
    Window win = XCreateWindow(pDisplay, pSDLWMInfo->info.x11.window, 
            0, 0, windowSize.x, windowSize.y, 0, pVisualInfo->depth, InputOutput, 
            pVisualInfo->visual, CWColormap|CWEventMask, &swa);
    AVG_ASSERT(win);
    XMapWindow(pDisplay, win);
    return win;
}

}