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(const XineramaScreenInfo
& info
)
88 : m_rect(info
.x_org
, info
.y_org
, info
.width
, info
.height
)
92 virtual wxRect
GetGeometry() const { return m_rect
; }
93 virtual wxString
GetName() const { return wxString(); }
95 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
96 virtual wxVideoMode
GetCurrentMode() const;
97 virtual bool ChangeMode(const wxVideoMode
& mode
);
103 DECLARE_NO_COPY_CLASS(wxDisplayImplX11
)
106 class wxDisplayFactoryX11
: public wxDisplayFactory
109 wxDisplayFactoryX11();
111 virtual wxDisplayImpl
*CreateDisplay(size_t n
);
112 virtual size_t GetCount();
113 virtual int GetFromPoint(const wxPoint
& pt
);
116 DECLARE_NO_COPY_CLASS(wxDisplayFactoryX11
)
119 // ============================================================================
120 // wxDisplayFactoryX11 implementation
121 // ============================================================================
123 size_t wxDisplayFactoryX11::GetCount()
125 return ScreensInfo().GetCount();
128 int wxDisplayFactoryX11::GetFromPoint(const wxPoint
& p
)
132 const size_t numscreens(screens
.GetCount());
133 for ( size_t i
= 0; i
< numscreens
; ++i
)
135 const XineramaScreenInfo
& s
= screens
[i
];
136 if ( p
.x
>= s
.x_org
&& p
.x
< s
.x_org
+ s
.width
&&
137 p
.y
>= s
.y_org
&& p
.y
< s
.y_org
+ s
.height
)
146 wxDisplayImpl
*wxDisplayFactoryX11::CreateDisplay(size_t n
)
150 return n
< screens
.GetCount() ? new wxDisplayImplX11(screens
[n
]) : NULL
;
153 // ============================================================================
154 // wxDisplayImplX11 implementation
155 // ============================================================================
157 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
160 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
161 // info about xf86 video mode extensions
164 //free private data common to x (usually s3) servers
165 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
167 // Correct res rate from GLFW
168 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
169 #define wxCRR(v) wxCRR2(v,v.dotclock)
170 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
171 #define wxCVM(v) wxCVM2(v, v.dotclock)
173 wxArrayVideoModes
wxDisplay::GetModes(const wxVideoMode
& mode
) const
176 Display
* pDisplay
= (Display
*) wxGetDisplay(); //default display
177 int nScreen
= DefaultScreen(pDisplay
); //default screen of (default) display...
180 XF86VidModeModeInfo
** ppXModes
; //Enumerated Modes (Don't forget XFree() :))
181 int nNumModes
; //Number of modes enumerated....
183 wxArrayVideoModes Modes
; //modes to return...
185 if (XF86VidModeGetAllModeLines(pDisplay
, nScreen
, &nNumModes
, &ppXModes
) == TRUE
)
187 for (int i
= 0; i
< nNumModes
; ++i
)
189 if (mode
== wxDefaultVideoMode
|| //According to display.h All modes valid if dafault mode...
190 mode
.Matches(wxCVM((*ppXModes
[i
]))) ) //...?
192 Modes
.Add(wxCVM((*ppXModes
[i
])));
194 wxClearXVM((*ppXModes
[i
]));
195 // XFree(ppXModes[i]); //supposed to free?
201 wxLogSysError(_("Failed to enumerate video modes"));
207 wxVideoMode
wxDisplay::GetCurrentMode() const
209 XF86VidModeModeLine VM
;
211 XF86VidModeGetModeLine((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
214 return wxCVM2(VM
, nDotClock
);
217 bool wxDisplay::ChangeMode(const wxVideoMode
& mode
)
219 XF86VidModeModeInfo
** ppXModes
; //Enumerated Modes (Don't forget XFree() :))
220 int nNumModes
; //Number of modes enumerated....
222 if( !XF86VidModeGetAllModeLines((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()), &nNumModes
, &ppXModes
) )
224 wxLogSysError(_("Failed to change video mode"));
229 if (mode
== wxDefaultVideoMode
)
231 bRet
= XF86VidModeSwitchToMode((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
232 ppXModes
[0]) == TRUE
;
234 for (int i
= 0; i
< nNumModes
; ++i
)
236 wxClearXVM((*ppXModes
[i
]));
237 // XFree(ppXModes[i]); //supposed to free?
242 for (int i
= 0; i
< nNumModes
; ++i
)
245 ppXModes
[i
]->hdisplay
== mode
.w
&&
246 ppXModes
[i
]->vdisplay
== mode
.h
&&
247 wxCRR((*ppXModes
[i
])) == mode
.refresh
)
250 bRet
= XF86VidModeSwitchToMode((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
251 ppXModes
[i
]) == TRUE
;
253 wxClearXVM((*ppXModes
[i
]));
254 // XFree(ppXModes[i]); //supposed to free?
264 #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
266 wxArrayVideoModes
wxDisplay::GetModes(const wxVideoMode
& mode
) const
269 int* depths
= XListDepths((Display
*)wxGetDisplay(), 0, &count_return
);
270 wxArrayVideoModes modes
;
273 for ( int x
= 0; x
< count_return
; ++x
)
275 modes
.Add(wxVideoMode(m_rect
.GetWidth(), m_rect
.GetHeight(), depths
[x
]));
283 wxVideoMode
wxDisplay::GetCurrentMode() const
286 return wxVideoMode();
289 bool wxDisplay::ChangeMode(const wxVideoMode
& mode
)
295 #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
297 // ============================================================================
298 // wxDisplay::CreateFactory()
299 // ============================================================================
301 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
303 Display
*disp
= (Display
*)wxGetDisplay();
305 return XineramaIsActive(disp
) ? new wxDisplayFactoryX11
306 : new wxDisplayFactorySingle
;
309 #endif /* wxUSE_DISPLAY */