1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/displayx11.cpp
3 // Purpose: Unix/X11 implementation of wxDisplay class
4 // Author: Brian Victor, Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/display.h"
28 #include "wx/display_impl.h"
33 #include "wx/dynarray.h"
34 #include "wx/gdicmn.h"
35 #include "wx/string.h"
37 #endif /* WX_PRECOMP */
41 /* These must be included after the wx files. Otherwise the Data macro in
42 * Xlibint.h conflicts with a function declaration in wx/list.h. */
46 #include <X11/Xlibint.h>
48 #include <X11/extensions/Xinerama.h>
49 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
50 #include <X11/extensions/xf86vmode.h>
54 // ----------------------------------------------------------------------------
55 // helper class to automatically free XineramaQueryScreens() return value
56 // ----------------------------------------------------------------------------
63 m_screens
= XineramaQueryScreens((Display
*)wxGetDisplay(), &m_num
);
71 operator const XineramaScreenInfo
*() const { return m_screens
; }
73 size_t GetCount() const { return wx_static_cast(size_t, m_num
); }
76 XineramaScreenInfo
*m_screens
;
80 // ----------------------------------------------------------------------------
81 // display and display factory classes
82 // ----------------------------------------------------------------------------
84 class WXDLLEXPORT wxDisplayImplX11
: public wxDisplayImpl
87 wxDisplayImplX11(size_t n
, const XineramaScreenInfo
& info
)
89 m_rect(info
.x_org
, info
.y_org
, info
.width
, info
.height
)
93 virtual wxRect
GetGeometry() const { return m_rect
; }
94 virtual wxString
GetName() const { return wxString(); }
96 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
97 virtual wxVideoMode
GetCurrentMode() const;
98 virtual bool ChangeMode(const wxVideoMode
& mode
);
104 DECLARE_NO_COPY_CLASS(wxDisplayImplX11
)
107 class wxDisplayFactoryX11
: public wxDisplayFactory
110 wxDisplayFactoryX11();
112 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
113 virtual size_t GetCount();
114 virtual int GetFromPoint(const wxPoint
& pt
);
117 DECLARE_NO_COPY_CLASS(wxDisplayFactoryX11
)
120 // ============================================================================
121 // wxDisplayFactoryX11 implementation
122 // ============================================================================
124 size_t wxDisplayFactoryX11::GetCount()
126 return ScreensInfo().GetCount();
129 int wxDisplayFactoryX11::GetFromPoint(const wxPoint
& p
)
133 const size_t numscreens(screens
.GetCount());
134 for ( size_t i
= 0; i
< numscreens
; ++i
)
136 const XineramaScreenInfo
& s
= screens
[i
];
137 if ( p
.x
>= s
.x_org
&& p
.x
< s
.x_org
+ s
.width
&&
138 p
.y
>= s
.y_org
&& p
.y
< s
.y_org
+ s
.height
)
147 wxDisplayImpl
*wxDisplayFactoryX11::CreateDisplay(size_t n
)
151 return n
< screens
.GetCount() ? new wxDisplayImplX11(n
, screens
[n
]) : NULL
;
154 // ============================================================================
155 // wxDisplayImplX11 implementation
156 // ============================================================================
158 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
161 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
162 // info about xf86 video mode extensions
165 //free private data common to x (usually s3) servers
166 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
168 // Correct res rate from GLFW
169 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
170 #define wxCRR(v) wxCRR2(v,v.dotclock)
171 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
172 #define wxCVM(v) wxCVM2(v, v.dotclock)
174 wxArrayVideoModes
wxDisplayImplX11::GetModes(const wxVideoMode
& mode
) const
177 Display
* pDisplay
= (Display
*) wxGetDisplay(); //default display
178 int nScreen
= DefaultScreen(pDisplay
); //default screen of (default) display...
181 XF86VidModeModeInfo
** ppXModes
; //Enumerated Modes (Don't forget XFree() :))
182 int nNumModes
; //Number of modes enumerated....
184 wxArrayVideoModes Modes
; //modes to return...
186 if (XF86VidModeGetAllModeLines(pDisplay
, nScreen
, &nNumModes
, &ppXModes
) == TRUE
)
188 for (int i
= 0; i
< nNumModes
; ++i
)
190 if (mode
== wxDefaultVideoMode
|| //According to display.h All modes valid if dafault mode...
191 mode
.Matches(wxCVM((*ppXModes
[i
]))) ) //...?
193 Modes
.Add(wxCVM((*ppXModes
[i
])));
195 wxClearXVM((*ppXModes
[i
]));
196 // XFree(ppXModes[i]); //supposed to free?
202 wxLogSysError(_("Failed to enumerate video modes"));
208 wxVideoMode
wxDisplayImplX11::GetCurrentMode() const
210 XF86VidModeModeLine VM
;
212 XF86VidModeGetModeLine((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
215 return wxCVM2(VM
, nDotClock
);
218 bool wxDisplayImplX11::ChangeMode(const wxVideoMode
& mode
)
220 XF86VidModeModeInfo
** ppXModes
; //Enumerated Modes (Don't forget XFree() :))
221 int nNumModes
; //Number of modes enumerated....
223 if( !XF86VidModeGetAllModeLines((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()), &nNumModes
, &ppXModes
) )
225 wxLogSysError(_("Failed to change video mode"));
230 if (mode
== wxDefaultVideoMode
)
232 bRet
= XF86VidModeSwitchToMode((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
233 ppXModes
[0]) == TRUE
;
235 for (int i
= 0; i
< nNumModes
; ++i
)
237 wxClearXVM((*ppXModes
[i
]));
238 // XFree(ppXModes[i]); //supposed to free?
243 for (int i
= 0; i
< nNumModes
; ++i
)
246 ppXModes
[i
]->hdisplay
== mode
.w
&&
247 ppXModes
[i
]->vdisplay
== mode
.h
&&
248 wxCRR((*ppXModes
[i
])) == mode
.refresh
)
251 bRet
= XF86VidModeSwitchToMode((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
252 ppXModes
[i
]) == TRUE
;
254 wxClearXVM((*ppXModes
[i
]));
255 // XFree(ppXModes[i]); //supposed to free?
265 #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
267 wxArrayVideoModes
wxDisplayImplX11::GetModes(const wxVideoMode
& mode
) const
270 int* depths
= XListDepths((Display
*)wxGetDisplay(), 0, &count_return
);
271 wxArrayVideoModes modes
;
274 for ( int x
= 0; x
< count_return
; ++x
)
276 modes
.Add(wxVideoMode(m_rect
.GetWidth(), m_rect
.GetHeight(), depths
[x
]));
284 wxVideoMode
wxDisplayImplX11::GetCurrentMode() const
287 return wxVideoMode();
290 bool wxDisplayImplX11::ChangeMode(const wxVideoMode
& mode
)
296 #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
298 // ============================================================================
299 // wxDisplay::CreateFactory()
300 // ============================================================================
302 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
304 if ( XineramaIsActive((Display
*)wxGetDisplay()) )
306 return new wxDisplayFactoryX11
;
309 return new wxDisplayFactorySingle
;
312 #endif /* wxUSE_DISPLAY */