Comment....
[wxWidgets.git] / src / unix / displayx11.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 // Name: displayx11.cpp
3 // Purpose: Unix/X11 implementation of wxDisplay class
4 // Author: Brian Victor
5 // Modified by:
6 // Created: 12/05/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "display.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/display.h"
24 #include "wx/intl.h"
25 #include "wx/log.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/dynarray.h"
29 #include "wx/gdicmn.h"
30 #include "wx/string.h"
31 #include "wx/utils.h"
32 #endif /* WX_PRECOMP */
33
34 #if wxUSE_DISPLAY
35
36 /* These must be included after the wx files. Otherwise the Data macro in
37 * Xlibint.h conflicts with a function declaration in wx/list.h. */
38 extern "C" {
39 #include <X11/Xlib.h>
40 #include <X11/Xlibint.h>
41 #include <X11/extensions/Xinerama.h>
42 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
43 #include <X11/extensions/xf86vmode.h>
44 #endif
45 }
46
47 class wxDisplayUnixPriv
48 {
49 public:
50 wxRect m_rect;
51 int m_depth;
52 };
53
54 size_t wxDisplayBase::GetCount()
55 {
56 Display *disp = (Display*)wxGetDisplay();
57
58 if ( XineramaIsActive(disp) )
59 {
60 XineramaScreenInfo *screenarr;
61 int numscreens;
62 screenarr = XineramaQueryScreens(disp, &numscreens);
63 XFree(screenarr);
64 return numscreens;
65 }
66 else
67 {
68 return 1;
69 }
70 }
71
72 int wxDisplayBase::GetFromPoint(const wxPoint &p)
73 {
74 Display *disp = (Display*)wxGetDisplay();
75
76 if ( XineramaIsActive(disp) )
77 {
78 int which_screen = -1;
79 XineramaScreenInfo *screenarr;
80 int numscreens;
81 screenarr = XineramaQueryScreens(disp, &numscreens);
82
83 int i;
84 for (i = 0; i < numscreens; ++i)
85 {
86 if (p.x >= screenarr[i].x_org &&
87 p.x < screenarr[i].x_org + screenarr[i].width &&
88 p.y >= screenarr[i].y_org &&
89 p.y < screenarr[i].y_org + screenarr[i].height)
90 {
91 which_screen = i;
92 }
93 }
94
95 XFree(screenarr);
96 return which_screen;
97 }
98 else
99 {
100 wxSize size = wxGetDisplaySize();
101 if (p.x >= 0 &&
102 p.x < size.GetWidth() &&
103 p.y >= 0 &&
104 p.y < size.GetHeight())
105 {
106 return 0;
107 }
108
109 return -1;
110 }
111 }
112
113 wxDisplay::wxDisplay(size_t index) : wxDisplayBase ( index ), m_priv( new wxDisplayUnixPriv )
114 {
115 Display *disp = (Display*)wxGetDisplay();
116
117 if ( XineramaIsActive(disp) )
118 {
119 XineramaScreenInfo *screenarr;
120 int numscreens;
121 screenarr = XineramaQueryScreens(disp, &numscreens);
122
123 //Make sure index is a valid display number...
124 wxASSERT(index < numscreens)
125
126 m_priv->m_rect = wxRect(screenarr[index].x_org, screenarr[index].y_org,
127 screenarr[index].width, screenarr[index].height);
128 m_priv->m_depth = DefaultDepth(disp, DefaultScreen(disp));
129 XFree(screenarr);
130 }
131 else
132 {
133 wxSize size = wxGetDisplaySize();
134 m_priv->m_rect = wxRect(0, 0, size.GetWidth(), size.GetHeight());
135 m_priv->m_depth = wxDisplayDepth();
136 }
137 }
138
139 wxDisplay::~wxDisplay()
140 {
141 delete m_priv;
142 }
143
144 wxRect wxDisplay::GetGeometry() const
145 {
146 return m_priv->m_rect;
147 }
148
149 int wxDisplay::GetDepth() const
150 {
151 return m_priv->m_depth;
152 }
153
154 wxString wxDisplay::GetName() const
155 {
156 return wxEmptyString;
157 }
158
159
160 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
161
162 //
163 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
164 // info about xf86 video mode extensions
165 //
166
167 //free private data common to x (usually s3) servers
168 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
169
170 //Correct res rate from GLFW, which probably has the perfect license :)
171 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
172 #define wxCRR(v) wxCRR2(v,v.dotclock)
173 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
174 #define wxCVM(v) wxCVM2(v, v.dotclock)
175
176 wxArrayVideoModes wxDisplay::GetModes(const wxVideoMode& mode) const
177 {
178 //Convenience...
179 Display* pDisplay = (Display*) wxGetDisplay(); //default display
180 int nScreen = DefaultScreen(pDisplay); //default screen of (default) display...
181
182 //Some variables..
183 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
184 int nNumModes; //Number of modes enumerated....
185
186 wxArrayVideoModes Modes; //modes to return...
187
188 if (XF86VidModeGetAllModeLines(pDisplay, nScreen, &nNumModes, &ppXModes) == TRUE)
189 {
190 for (int i = 0; i < nNumModes; ++i)
191 {
192 if (mode == wxDefaultVideoMode || //According to display.h All modes valid if dafault mode...
193 mode.Matches(wxCVM((*ppXModes[i]))) ) //...?
194 {
195 Modes.Add(wxCVM((*ppXModes[i])));
196 }
197 wxClearXVM((*ppXModes[i]));
198 // XFree(ppXModes[i]); //supposed to free?
199 }
200 XFree(ppXModes);
201 }
202 else //OOPS!
203 {
204 wxLogSysError(_("Failed to enumerate video modes"));
205 }
206
207 return Modes;
208 }
209
210 wxVideoMode wxDisplay::GetCurrentMode() const
211 {
212 XF86VidModeModeLine VM;
213 int nDotClock;
214 XF86VidModeGetModeLine((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
215 &nDotClock, &VM);
216 wxClearXVM(VM);
217 return wxCVM2(VM, nDotClock);
218 }
219
220 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
221 {
222 //This gets kind of tricky AND complicated :) :\ :( :)
223 {
224 bool bRet = false;
225 //Some variables..
226 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
227 int nNumModes; //Number of modes enumerated....
228
229 if(XF86VidModeGetAllModeLines((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), &nNumModes, &ppXModes) == TRUE)
230 {
231 if (mode == wxDefaultVideoMode)
232 {
233 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
234 ppXModes[0]) == TRUE;
235
236 for (int i = 0; i < nNumModes; ++i)
237 {
238 wxClearXVM((*ppXModes[i]));
239 // XFree(ppXModes[i]); //supposed to free?
240 }
241 XFree(ppXModes);
242
243 return bRet;
244 }
245 for (int i = 0; i < nNumModes; ++i)
246 {
247 if (!bRet &&
248 ppXModes[i]->hdisplay == mode.w &&
249 ppXModes[i]->vdisplay == mode.h &&
250 wxCRR((*ppXModes[i])) == mode.refresh)
251 {
252 //switch!
253 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
254 ppXModes[i]) == TRUE;
255 }
256 wxClearXVM((*ppXModes[i]));
257 // XFree(ppXModes[i]); //supposed to free?
258 }
259 XFree(ppXModes);
260
261 return bRet;
262 }
263 else //OOPS!
264 {
265 wxLogSysError(_("Failed to change video mode"));
266 return false;
267 }
268 }
269 }
270
271
272 #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
273
274 wxArrayVideoModes wxDisplay::GetModes(const wxVideoMode& mode) const
275 {
276 Display *disp = (Display*)wxGetDisplay();
277 int count_return;
278 int* depths = XListDepths(disp, 0, &count_return);
279 wxArrayVideoModes modes;
280 if (depths)
281 {
282 int x;
283 for (x = 0; x < count_return; ++x)
284 {
285 modes.Add(wxVideoMode(m_priv->m_rect.GetWidth(), m_priv->m_rect.GetHeight(), depths[x]));
286 }
287 }
288 return modes;
289 }
290
291 wxVideoMode wxDisplay::GetCurrentMode() const
292 {
293 // Not implemented
294 return wxVideoMode();
295 }
296
297 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
298 {
299 // Not implemented
300 return false;
301 }
302
303 #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
304
305 #endif /* wxUSE_DISPLAY */