]> git.saurik.com Git - wxWidgets.git/blob - src/unix/displayx11.cpp
cleanup
[wxWidgets.git] / src / unix / displayx11.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/displayx11.cpp
3 // Purpose: Unix/X11 implementation of wxDisplay class
4 // Author: Brian Victor, Vadim Zeitlin
5 // Modified by:
6 // Created: 12/05/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DISPLAY
28
29 #include "wx/display.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dynarray.h"
33 #include "wx/gdicmn.h"
34 #include "wx/string.h"
35 #include "wx/utils.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #endif /* WX_PRECOMP */
39
40 #include "wx/display_impl.h"
41
42 /* These must be included after the wx files. Otherwise the Data macro in
43 * Xlibint.h conflicts with a function declaration in wx/list.h. */
44 extern "C"
45 {
46 #include <X11/Xlib.h>
47 #include <X11/Xlibint.h>
48
49 #include <X11/extensions/Xinerama.h>
50 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
51 #include <X11/extensions/xf86vmode.h>
52 #endif
53 }
54
55 // ----------------------------------------------------------------------------
56 // helper class to automatically free XineramaQueryScreens() return value
57 // ----------------------------------------------------------------------------
58
59 class ScreensInfo
60 {
61 public:
62 ScreensInfo()
63 {
64 m_screens = XineramaQueryScreens((Display *)wxGetDisplay(), &m_num);
65 }
66
67 ~ScreensInfo()
68 {
69 XFree(m_screens);
70 }
71
72 operator const XineramaScreenInfo *() const { return m_screens; }
73
74 unsigned GetCount() const { return wx_static_cast(unsigned, m_num); }
75
76 private:
77 XineramaScreenInfo *m_screens;
78 int m_num;
79 };
80
81 // ----------------------------------------------------------------------------
82 // display and display factory classes
83 // ----------------------------------------------------------------------------
84
85 class WXDLLEXPORT wxDisplayImplX11 : public wxDisplayImpl
86 {
87 public:
88 wxDisplayImplX11(unsigned n, const XineramaScreenInfo& info)
89 : wxDisplayImpl(n),
90 m_rect(info.x_org, info.y_org, info.width, info.height)
91 {
92 }
93
94 virtual wxRect GetGeometry() const { return m_rect; }
95 virtual wxRect GetClientArea() const
96 {
97 // we intentionally don't cache the result here because the client
98 // display area may change (e.g. the user resized or hid a panel) and
99 // we don't currently react to its changes
100 return IsPrimary() ? wxGetClientDisplayRect() : m_rect;
101 }
102
103 virtual wxString GetName() const { return wxString(); }
104
105 virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const;
106 virtual wxVideoMode GetCurrentMode() const;
107 virtual bool ChangeMode(const wxVideoMode& mode);
108
109 private:
110 wxRect m_rect;
111 int m_depth;
112
113 DECLARE_NO_COPY_CLASS(wxDisplayImplX11)
114 };
115
116 class wxDisplayFactoryX11 : public wxDisplayFactory
117 {
118 public:
119 wxDisplayFactoryX11() { }
120
121 virtual wxDisplayImpl *CreateDisplay(unsigned n);
122 virtual unsigned GetCount();
123 virtual int GetFromPoint(const wxPoint& pt);
124
125 protected:
126 DECLARE_NO_COPY_CLASS(wxDisplayFactoryX11)
127 };
128
129 // ============================================================================
130 // wxDisplayFactoryX11 implementation
131 // ============================================================================
132
133 unsigned wxDisplayFactoryX11::GetCount()
134 {
135 return ScreensInfo().GetCount();
136 }
137
138 int wxDisplayFactoryX11::GetFromPoint(const wxPoint& p)
139 {
140 ScreensInfo screens;
141
142 const unsigned numscreens(screens.GetCount());
143 for ( unsigned i = 0; i < numscreens; ++i )
144 {
145 const XineramaScreenInfo& s = screens[i];
146 if ( p.x >= s.x_org && p.x < s.x_org + s.width &&
147 p.y >= s.y_org && p.y < s.y_org + s.height )
148 {
149 return i;
150 }
151 }
152
153 return wxNOT_FOUND;
154 }
155
156 wxDisplayImpl *wxDisplayFactoryX11::CreateDisplay(unsigned n)
157 {
158 ScreensInfo screens;
159
160 return n < screens.GetCount() ? new wxDisplayImplX11(n, screens[n]) : NULL;
161 }
162
163 // ============================================================================
164 // wxDisplayImplX11 implementation
165 // ============================================================================
166
167 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
168
169 //
170 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
171 // info about xf86 video mode extensions
172 //
173
174 //free private data common to x (usually s3) servers
175 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
176
177 // Correct res rate from GLFW
178 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
179 #define wxCRR(v) wxCRR2(v,v.dotclock)
180 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
181 #define wxCVM(v) wxCVM2(v, v.dotclock)
182
183 wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& mode) const
184 {
185 //Convenience...
186 Display* pDisplay = (Display*) wxGetDisplay(); //default display
187 int nScreen = DefaultScreen(pDisplay); //default screen of (default) display...
188
189 //Some variables..
190 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
191 int nNumModes; //Number of modes enumerated....
192
193 wxArrayVideoModes Modes; //modes to return...
194
195 if (XF86VidModeGetAllModeLines(pDisplay, nScreen, &nNumModes, &ppXModes) == TRUE)
196 {
197 for (int i = 0; i < nNumModes; ++i)
198 {
199 if (mode == wxDefaultVideoMode || //According to display.h All modes valid if dafault mode...
200 mode.Matches(wxCVM((*ppXModes[i]))) ) //...?
201 {
202 Modes.Add(wxCVM((*ppXModes[i])));
203 }
204 wxClearXVM((*ppXModes[i]));
205 // XFree(ppXModes[i]); //supposed to free?
206 }
207 XFree(ppXModes);
208 }
209 else //OOPS!
210 {
211 wxLogSysError(_("Failed to enumerate video modes"));
212 }
213
214 return Modes;
215 }
216
217 wxVideoMode wxDisplayImplX11::GetCurrentMode() const
218 {
219 XF86VidModeModeLine VM;
220 int nDotClock;
221 XF86VidModeGetModeLine((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
222 &nDotClock, &VM);
223 wxClearXVM(VM);
224 return wxCVM2(VM, nDotClock);
225 }
226
227 bool wxDisplayImplX11::ChangeMode(const wxVideoMode& mode)
228 {
229 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
230 int nNumModes; //Number of modes enumerated....
231
232 if( !XF86VidModeGetAllModeLines((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), &nNumModes, &ppXModes) )
233 {
234 wxLogSysError(_("Failed to change video mode"));
235 return false;
236 }
237
238 bool bRet = false;
239 if (mode == wxDefaultVideoMode)
240 {
241 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
242 ppXModes[0]) == TRUE;
243
244 for (int i = 0; i < nNumModes; ++i)
245 {
246 wxClearXVM((*ppXModes[i]));
247 // XFree(ppXModes[i]); //supposed to free?
248 }
249 }
250 else
251 {
252 for (int i = 0; i < nNumModes; ++i)
253 {
254 if (!bRet &&
255 ppXModes[i]->hdisplay == mode.w &&
256 ppXModes[i]->vdisplay == mode.h &&
257 wxCRR((*ppXModes[i])) == mode.refresh)
258 {
259 //switch!
260 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
261 ppXModes[i]) == TRUE;
262 }
263 wxClearXVM((*ppXModes[i]));
264 // XFree(ppXModes[i]); //supposed to free?
265 }
266 }
267
268 XFree(ppXModes);
269
270 return bRet;
271 }
272
273
274 #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
275
276 wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& modeMatch) const
277 {
278 int count_return;
279 int* depths = XListDepths((Display*)wxGetDisplay(), 0, &count_return);
280 wxArrayVideoModes modes;
281 if ( depths )
282 {
283 for ( int x = 0; x < count_return; ++x )
284 {
285 wxVideoMode mode(m_rect.GetWidth(), m_rect.GetHeight(), depths[x]);
286 if ( mode.Matches(modeMatch) )
287 {
288 modes.Add(modeMatch);
289 }
290 }
291
292 XFree(depths);
293 }
294 return modes;
295 }
296
297 wxVideoMode wxDisplayImplX11::GetCurrentMode() const
298 {
299 // Not implemented
300 return wxVideoMode();
301 }
302
303 bool wxDisplayImplX11::ChangeMode(const wxVideoMode& WXUNUSED(mode))
304 {
305 // Not implemented
306 return false;
307 }
308
309 #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
310
311 // ============================================================================
312 // wxDisplay::CreateFactory()
313 // ============================================================================
314
315 /* static */ wxDisplayFactory *wxDisplay::CreateFactory()
316 {
317 if ( XineramaIsActive((Display*)wxGetDisplay()) )
318 {
319 return new wxDisplayFactoryX11;
320 }
321
322 return new wxDisplayFactorySingle;
323 }
324
325 #endif /* wxUSE_DISPLAY */
326
327 #include "wx/utils.h"
328
329 #if wxUSE_LIBHILDON
330
331 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
332 {
333 // TODO: don't hardcode display size
334 if ( x )
335 *x = 0;
336 if ( y )
337 *y = 0;
338 if ( width )
339 *width = 672;
340 if ( height )
341 *height = 396;
342 }
343
344 #else // !wxUSE_LIBHILDON
345
346 #include <X11/Xlib.h>
347 #include <X11/Xatom.h>
348
349 // TODO: make this a full-fledged class and move to a public header
350 class wxX11Ptr
351 {
352 public:
353 wxX11Ptr(void *ptr = NULL) : m_ptr(ptr) { }
354 ~wxX11Ptr() { if ( m_ptr ) XFree(m_ptr); }
355
356 private:
357 void *m_ptr;
358
359 DECLARE_NO_COPY_CLASS(wxX11Ptr)
360 };
361
362 // NB: this function is implemented using X11 and not GDK calls as it's shared
363 // by wxGTK[12], wxX11 and wxMotif ports
364 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
365 {
366 Display * const dpy = wxGetX11Display();
367 wxCHECK_RET( dpy, _T("can't be called before initializing the GUI") );
368
369 const Atom atomWorkArea = XInternAtom(dpy, "_NET_WORKAREA", True);
370 if ( atomWorkArea )
371 {
372 long *workareas = NULL;
373 unsigned long numItems;
374 unsigned long bytesRemaining;
375 Atom actualType;
376 int format;
377
378 if ( XGetWindowProperty
379 (
380 dpy,
381 XDefaultRootWindow(dpy),
382 atomWorkArea,
383 0, // offset of data to retrieve
384 4, // number of items to retrieve
385 False, // don't delete property
386 XA_CARDINAL, // type of the items to get
387 &actualType,
388 &format,
389 &numItems,
390 &bytesRemaining,
391 (unsigned char **)&workareas
392 ) == Success && workareas )
393 {
394 wxX11Ptr x11ptr(workareas); // ensure it will be freed
395
396 // check that we retrieved the property of the expected type and
397 // that we did get back 4 longs (32 is the format for long), as
398 // requested
399 if ( actualType != XA_CARDINAL ||
400 format != 32 ||
401 numItems != 4 )
402 {
403 wxLogDebug(_T("XGetWindowProperty(\"_NET_WORKAREA\") failed"));
404 return;
405 }
406
407 if ( x )
408 *x = workareas[0];
409 if ( y )
410 *y = workareas[1];
411 if ( width )
412 *width = workareas[2];
413 if ( height )
414 *height = workareas[3];
415
416 return;
417 }
418 }
419
420 // if we get here, _NET_WORKAREA is not supported so return the entire
421 // screen size as fall back
422 if (x)
423 *x = 0;
424 if (y)
425 *y = 0;
426 wxDisplaySize(width, height);
427 }
428
429 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON