]> git.saurik.com Git - wxWidgets.git/blame - src/unix/displayx11.cpp
check that the version of __sync_sub_and_fetch that returns a value is supported...
[wxWidgets.git] / src / unix / displayx11.cpp
CommitLineData
b1b3ddd8 1///////////////////////////////////////////////////////////////////////////
ef1717a9 2// Name: src/unix/displayx11.cpp
b1b3ddd8 3// Purpose: Unix/X11 implementation of wxDisplay class
ef1717a9 4// Author: Brian Victor, Vadim Zeitlin
b1b3ddd8
JS
5// Modified by:
6// Created: 12/05/02
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
b1b3ddd8
JS
10/////////////////////////////////////////////////////////////////////////////
11
ef1717a9
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
b1b3ddd8
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
ad9835c9 24 #pragma hdrstop
b1b3ddd8
JS
25#endif
26
ad9835c9 27#if wxUSE_DISPLAY
b1b3ddd8 28
88a7a4e1
WS
29#include "wx/display.h"
30
b1b3ddd8 31#ifndef WX_PRECOMP
ad9835c9
WS
32 #include "wx/dynarray.h"
33 #include "wx/gdicmn.h"
34 #include "wx/string.h"
35 #include "wx/utils.h"
88a7a4e1 36 #include "wx/intl.h"
e4db172a 37 #include "wx/log.h"
b1b3ddd8
JS
38#endif /* WX_PRECOMP */
39
ad9835c9 40#include "wx/display_impl.h"
b1b3ddd8
JS
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. */
ef1717a9 44extern "C"
b1b3ddd8 45{
ef1717a9
VZ
46 #include <X11/Xlib.h>
47 #include <X11/Xlibint.h>
b1b3ddd8 48
ef1717a9
VZ
49 #include <X11/extensions/Xinerama.h>
50 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
51 #include <X11/extensions/xf86vmode.h>
52 #endif
b1b3ddd8
JS
53}
54
ef1717a9
VZ
55// ----------------------------------------------------------------------------
56// helper class to automatically free XineramaQueryScreens() return value
57// ----------------------------------------------------------------------------
58
59class ScreensInfo
b1b3ddd8 60{
ef1717a9
VZ
61public:
62 ScreensInfo()
b1b3ddd8 63 {
ef1717a9 64 m_screens = XineramaQueryScreens((Display *)wxGetDisplay(), &m_num);
b1b3ddd8
JS
65 }
66
ef1717a9 67 ~ScreensInfo()
3cb98121 68 {
ef1717a9 69 XFree(m_screens);
3cb98121
VZ
70 }
71
ef1717a9
VZ
72 operator const XineramaScreenInfo *() const { return m_screens; }
73
4e675101 74 unsigned GetCount() const { return wx_static_cast(unsigned, m_num); }
ef1717a9
VZ
75
76private:
77 XineramaScreenInfo *m_screens;
78 int m_num;
79};
80
81// ----------------------------------------------------------------------------
82// display and display factory classes
83// ----------------------------------------------------------------------------
b1b3ddd8 84
ef1717a9 85class WXDLLEXPORT wxDisplayImplX11 : public wxDisplayImpl
b1b3ddd8 86{
ef1717a9 87public:
4e675101 88 wxDisplayImplX11(unsigned n, const XineramaScreenInfo& info)
547308b8
VZ
89 : wxDisplayImpl(n),
90 m_rect(info.x_org, info.y_org, info.width, info.height)
ef1717a9
VZ
91 {
92 }
b1b3ddd8 93
ef1717a9 94 virtual wxRect GetGeometry() const { return m_rect; }
676c8c1d
VZ
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
ef1717a9
VZ
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
109private:
110 wxRect m_rect;
111 int m_depth;
112
113 DECLARE_NO_COPY_CLASS(wxDisplayImplX11)
114};
115
116class wxDisplayFactoryX11 : public wxDisplayFactory
b1b3ddd8 117{
ef1717a9 118public:
1003169e 119 wxDisplayFactoryX11() { }
ef1717a9 120
4e675101
PC
121 virtual wxDisplayImpl *CreateDisplay(unsigned n);
122 virtual unsigned GetCount();
ef1717a9 123 virtual int GetFromPoint(const wxPoint& pt);
b1b3ddd8 124
ef1717a9
VZ
125protected:
126 DECLARE_NO_COPY_CLASS(wxDisplayFactoryX11)
127};
128
129// ============================================================================
130// wxDisplayFactoryX11 implementation
131// ============================================================================
132
4e675101 133unsigned wxDisplayFactoryX11::GetCount()
b1b3ddd8 134{
ef1717a9 135 return ScreensInfo().GetCount();
b1b3ddd8
JS
136}
137
ef1717a9 138int wxDisplayFactoryX11::GetFromPoint(const wxPoint& p)
b1b3ddd8 139{
ef1717a9
VZ
140 ScreensInfo screens;
141
4e675101
PC
142 const unsigned numscreens(screens.GetCount());
143 for ( unsigned i = 0; i < numscreens; ++i )
ef1717a9
VZ
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;
b1b3ddd8
JS
154}
155
4e675101 156wxDisplayImpl *wxDisplayFactoryX11::CreateDisplay(unsigned n)
b1b3ddd8 157{
ef1717a9
VZ
158 ScreensInfo screens;
159
547308b8 160 return n < screens.GetCount() ? new wxDisplayImplX11(n, screens[n]) : NULL;
b1b3ddd8
JS
161}
162
ef1717a9
VZ
163// ============================================================================
164// wxDisplayImplX11 implementation
165// ============================================================================
505c8ccd
VS
166
167#ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
168
73b89f70
RN
169//
170// See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
171// info about xf86 video mode extensions
172//
173
2980fc88
RN
174//free private data common to x (usually s3) servers
175#define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
176
a90bf709 177// Correct res rate from GLFW
2980fc88
RN
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)
73b89f70 182
0d5b6a40 183wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& mode) const
e07d33e7 184{
73b89f70
RN
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...
2980fc88 200 mode.Matches(wxCVM((*ppXModes[i]))) ) //...?
73b89f70 201 {
2980fc88 202 Modes.Add(wxCVM((*ppXModes[i])));
73b89f70
RN
203 }
204 wxClearXVM((*ppXModes[i]));
205 // XFree(ppXModes[i]); //supposed to free?
206 }
207 XFree(ppXModes);
208 }
209 else //OOPS!
210 {
428b6942 211 wxLogSysError(_("Failed to enumerate video modes"));
73b89f70
RN
212 }
213
214 return Modes;
e07d33e7
RN
215}
216
0d5b6a40 217wxVideoMode wxDisplayImplX11::GetCurrentMode() const
e07d33e7 218{
2980fc88
RN
219 XF86VidModeModeLine VM;
220 int nDotClock;
221 XF86VidModeGetModeLine((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
222 &nDotClock, &VM);
223 wxClearXVM(VM);
224 return wxCVM2(VM, nDotClock);
e07d33e7
RN
225}
226
0d5b6a40 227bool wxDisplayImplX11::ChangeMode(const wxVideoMode& mode)
e07d33e7 228{
a90bf709
VZ
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) )
73b89f70 233 {
a90bf709
VZ
234 wxLogSysError(_("Failed to change video mode"));
235 return false;
236 }
73b89f70 237
a90bf709
VZ
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)
73b89f70 245 {
a90bf709
VZ
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)
2980fc88 258 {
a90bf709 259 //switch!
2980fc88 260 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
a90bf709 261 ppXModes[i]) == TRUE;
2980fc88 262 }
a90bf709
VZ
263 wxClearXVM((*ppXModes[i]));
264 // XFree(ppXModes[i]); //supposed to free?
73b89f70
RN
265 }
266 }
a90bf709
VZ
267
268 XFree(ppXModes);
269
270 return bRet;
e5804ce7
RN
271}
272
273
274#else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
275
d5a20ebb 276wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& modeMatch) const
e5804ce7 277{
ef1717a9
VZ
278 int count_return;
279 int* depths = XListDepths((Display*)wxGetDisplay(), 0, &count_return);
280 wxArrayVideoModes modes;
281 if ( depths )
f2ebd959 282 {
ef1717a9
VZ
283 for ( int x = 0; x < count_return; ++x )
284 {
d5a20ebb
VZ
285 wxVideoMode mode(m_rect.GetWidth(), m_rect.GetHeight(), depths[x]);
286 if ( mode.Matches(modeMatch) )
287 {
288 modes.Add(modeMatch);
289 }
ef1717a9
VZ
290 }
291
292 XFree(depths);
f2ebd959 293 }
ef1717a9 294 return modes;
505c8ccd
VS
295}
296
0d5b6a40 297wxVideoMode wxDisplayImplX11::GetCurrentMode() const
505c8ccd
VS
298{
299 // Not implemented
300 return wxVideoMode();
301}
302
d5a20ebb 303bool wxDisplayImplX11::ChangeMode(const wxVideoMode& WXUNUSED(mode))
505c8ccd
VS
304{
305 // Not implemented
306 return false;
307}
308
309#endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
310
ef1717a9
VZ
311// ============================================================================
312// wxDisplay::CreateFactory()
313// ============================================================================
314
315/* static */ wxDisplayFactory *wxDisplay::CreateFactory()
316{
547308b8
VZ
317 if ( XineramaIsActive((Display*)wxGetDisplay()) )
318 {
319 return new wxDisplayFactoryX11;
320 }
ef1717a9 321
547308b8 322 return new wxDisplayFactorySingle;
ef1717a9
VZ
323}
324
505c8ccd 325#endif /* wxUSE_DISPLAY */
676c8c1d
VZ
326
327#include "wx/utils.h"
328
66d05130
VZ
329#if wxUSE_LIBHILDON
330
331void 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
562e60a0
PC
346#include "wx/log.h"
347
64bd3204 348#include <X11/Xlib.h>
676c8c1d
VZ
349#include <X11/Xatom.h>
350
351// TODO: make this a full-fledged class and move to a public header
352class wxX11Ptr
353{
354public:
355 wxX11Ptr(void *ptr = NULL) : m_ptr(ptr) { }
356 ~wxX11Ptr() { if ( m_ptr ) XFree(m_ptr); }
357
358private:
359 void *m_ptr;
360
361 DECLARE_NO_COPY_CLASS(wxX11Ptr)
362};
363
364// NB: this function is implemented using X11 and not GDK calls as it's shared
365// by wxGTK[12], wxX11 and wxMotif ports
366void wxClientDisplayRect(int *x, int *y, int *width, int *height)
367{
368 Display * const dpy = wxGetX11Display();
369 wxCHECK_RET( dpy, _T("can't be called before initializing the GUI") );
370
371 const Atom atomWorkArea = XInternAtom(dpy, "_NET_WORKAREA", True);
372 if ( atomWorkArea )
373 {
374 long *workareas = NULL;
375 unsigned long numItems;
376 unsigned long bytesRemaining;
377 Atom actualType;
378 int format;
379
380 if ( XGetWindowProperty
381 (
382 dpy,
383 XDefaultRootWindow(dpy),
384 atomWorkArea,
385 0, // offset of data to retrieve
386 4, // number of items to retrieve
387 False, // don't delete property
388 XA_CARDINAL, // type of the items to get
389 &actualType,
390 &format,
391 &numItems,
392 &bytesRemaining,
393 (unsigned char **)&workareas
394 ) == Success && workareas )
395 {
396 wxX11Ptr x11ptr(workareas); // ensure it will be freed
397
f2633e54
VZ
398 // check that we retrieved the property of the expected type and
399 // that we did get back 4 longs (32 is the format for long), as
400 // requested
676c8c1d 401 if ( actualType != XA_CARDINAL ||
f2633e54 402 format != 32 ||
676c8c1d
VZ
403 numItems != 4 )
404 {
405 wxLogDebug(_T("XGetWindowProperty(\"_NET_WORKAREA\") failed"));
406 return;
407 }
408
409 if ( x )
410 *x = workareas[0];
411 if ( y )
412 *y = workareas[1];
413 if ( width )
414 *width = workareas[2];
415 if ( height )
416 *height = workareas[3];
417
418 return;
419 }
420 }
421
422 // if we get here, _NET_WORKAREA is not supported so return the entire
423 // screen size as fall back
424 if (x)
425 *x = 0;
426 if (y)
427 *y = 0;
428 wxDisplaySize(width, height);
429}
430
66d05130 431#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON