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"
29 #include "wx/display.h"
32 #include "wx/dynarray.h"
33 #include "wx/gdicmn.h"
34 #include "wx/string.h"
38 #endif /* WX_PRECOMP */
40 #include "wx/display_impl.h"
46 // define the struct with the same fields as XineramaScreenInfo (except for
47 // screen number which we don't need) but which we can use without
48 // including Xinerama headers
56 #else // use raw Xinerama functions
57 /* These must be included after the wx files. Otherwise the Data macro in
58 * Xlibint.h conflicts with a function declaration in wx/list.h. */
62 #include <X11/Xlibint.h>
64 #include <X11/extensions/Xinerama.h>
65 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
66 #include <X11/extensions/xf86vmode.h>
70 typedef XineramaScreenInfo ScreenInfo
;
71 #endif // GTK+/Xinerama
73 // ----------------------------------------------------------------------------
74 // helper class storing information about all screens
75 // ----------------------------------------------------------------------------
77 // the base class provides access to ScreenInfo array, derived class
78 // initializes it using either GTK+ or Xinerama functions
82 operator const ScreenInfo
*() const { return m_screens
; }
84 unsigned GetCount() const { return static_cast<unsigned>(m_num
); }
87 ScreenInfo
*m_screens
;
93 class ScreensInfo
: public ScreensInfoBase
98 GdkScreen
* const screen
= gdk_screen_get_default();
100 m_num
= gdk_screen_get_n_monitors(screen
);
101 m_screens
= new ScreenInfo
[m_num
];
102 for ( int i
= 0; i
< m_num
; i
++ )
105 gdk_screen_get_monitor_geometry(screen
, i
, &rect
);
106 m_screens
[i
].x_org
= rect
.x
;
107 m_screens
[i
].y_org
= rect
.y
;
108 m_screens
[i
].width
= rect
.width
;
109 m_screens
[i
].height
= rect
.height
;
121 class ScreensInfo
: public ScreensInfoBase
126 m_screens
= XineramaQueryScreens((Display
*)wxGetDisplay(), &m_num
);
135 #endif // GTK+/Xinerama
137 // ----------------------------------------------------------------------------
138 // display and display factory classes
139 // ----------------------------------------------------------------------------
141 class WXDLLEXPORT wxDisplayImplX11
: public wxDisplayImpl
144 wxDisplayImplX11(unsigned n
, const ScreenInfo
& info
)
146 m_rect(info
.x_org
, info
.y_org
, info
.width
, info
.height
)
150 virtual wxRect
GetGeometry() const { return m_rect
; }
151 virtual wxRect
GetClientArea() const
153 // we intentionally don't cache the result here because the client
154 // display area may change (e.g. the user resized or hid a panel) and
155 // we don't currently react to its changes
156 return IsPrimary() ? wxGetClientDisplayRect() : m_rect
;
159 virtual wxString
GetName() const { return wxString(); }
161 virtual wxArrayVideoModes
GetModes(const wxVideoMode
& mode
) const;
162 virtual wxVideoMode
GetCurrentMode() const;
163 virtual bool ChangeMode(const wxVideoMode
& mode
);
169 wxDECLARE_NO_COPY_CLASS(wxDisplayImplX11
);
172 class wxDisplayFactoryX11
: public wxDisplayFactory
175 wxDisplayFactoryX11() { }
177 virtual wxDisplayImpl
*CreateDisplay(unsigned n
);
178 virtual unsigned GetCount();
179 virtual int GetFromPoint(const wxPoint
& pt
);
182 wxDECLARE_NO_COPY_CLASS(wxDisplayFactoryX11
);
185 // ============================================================================
186 // wxDisplayFactoryX11 implementation
187 // ============================================================================
189 unsigned wxDisplayFactoryX11::GetCount()
191 return ScreensInfo().GetCount();
194 int wxDisplayFactoryX11::GetFromPoint(const wxPoint
& p
)
198 const unsigned numscreens(screens
.GetCount());
199 for ( unsigned i
= 0; i
< numscreens
; ++i
)
201 const ScreenInfo
& s
= screens
[i
];
202 if ( p
.x
>= s
.x_org
&& p
.x
< s
.x_org
+ s
.width
&&
203 p
.y
>= s
.y_org
&& p
.y
< s
.y_org
+ s
.height
)
212 wxDisplayImpl
*wxDisplayFactoryX11::CreateDisplay(unsigned n
)
216 return n
< screens
.GetCount() ? new wxDisplayImplX11(n
, screens
[n
]) : NULL
;
219 // ============================================================================
220 // wxDisplayImplX11 implementation
221 // ============================================================================
223 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
226 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
227 // info about xf86 video mode extensions
230 //free private data common to x (usually s3) servers
231 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
233 // Correct res rate from GLFW
234 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
235 #define wxCRR(v) wxCRR2(v,v.dotclock)
236 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
237 #define wxCVM(v) wxCVM2(v, v.dotclock)
239 wxArrayVideoModes
wxDisplayImplX11::GetModes(const wxVideoMode
& mode
) const
242 Display
* pDisplay
= (Display
*) wxGetDisplay(); //default display
243 int nScreen
= DefaultScreen(pDisplay
); //default screen of (default) display...
246 XF86VidModeModeInfo
** ppXModes
; //Enumerated Modes (Don't forget XFree() :))
247 int nNumModes
; //Number of modes enumerated....
249 wxArrayVideoModes Modes
; //modes to return...
251 if (XF86VidModeGetAllModeLines(pDisplay
, nScreen
, &nNumModes
, &ppXModes
) == TRUE
)
253 for (int i
= 0; i
< nNumModes
; ++i
)
255 if (mode
== wxDefaultVideoMode
|| //According to display.h All modes valid if dafault mode...
256 mode
.Matches(wxCVM((*ppXModes
[i
]))) ) //...?
258 Modes
.Add(wxCVM((*ppXModes
[i
])));
260 wxClearXVM((*ppXModes
[i
]));
261 // XFree(ppXModes[i]); //supposed to free?
267 wxLogSysError(_("Failed to enumerate video modes"));
273 wxVideoMode
wxDisplayImplX11::GetCurrentMode() const
275 XF86VidModeModeLine VM
;
277 XF86VidModeGetModeLine((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
280 return wxCVM2(VM
, nDotClock
);
283 bool wxDisplayImplX11::ChangeMode(const wxVideoMode
& mode
)
285 XF86VidModeModeInfo
** ppXModes
; //Enumerated Modes (Don't forget XFree() :))
286 int nNumModes
; //Number of modes enumerated....
288 if( !XF86VidModeGetAllModeLines((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()), &nNumModes
, &ppXModes
) )
290 wxLogSysError(_("Failed to change video mode"));
295 if (mode
== wxDefaultVideoMode
)
297 bRet
= XF86VidModeSwitchToMode((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
298 ppXModes
[0]) == TRUE
;
300 for (int i
= 0; i
< nNumModes
; ++i
)
302 wxClearXVM((*ppXModes
[i
]));
303 // XFree(ppXModes[i]); //supposed to free?
308 for (int i
= 0; i
< nNumModes
; ++i
)
311 ppXModes
[i
]->hdisplay
== mode
.GetWidth() &&
312 ppXModes
[i
]->vdisplay
== mode
.GetHeight() &&
313 wxCRR((*ppXModes
[i
])) == mode
.GetRefresh())
316 bRet
= XF86VidModeSwitchToMode((Display
*)wxGetDisplay(), DefaultScreen((Display
*)wxGetDisplay()),
317 ppXModes
[i
]) == TRUE
;
319 wxClearXVM((*ppXModes
[i
]));
320 // XFree(ppXModes[i]); //supposed to free?
330 #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H
332 wxArrayVideoModes
wxDisplayImplX11::GetModes(const wxVideoMode
& modeMatch
) const
335 int* depths
= XListDepths((Display
*)wxGetDisplay(), 0, &count_return
);
336 wxArrayVideoModes modes
;
339 for ( int x
= 0; x
< count_return
; ++x
)
341 wxVideoMode
mode(m_rect
.GetWidth(), m_rect
.GetHeight(), depths
[x
]);
342 if ( mode
.Matches(modeMatch
) )
344 modes
.Add(modeMatch
);
353 wxVideoMode
wxDisplayImplX11::GetCurrentMode() const
356 return wxVideoMode();
359 bool wxDisplayImplX11::ChangeMode(const wxVideoMode
& WXUNUSED(mode
))
365 #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H
367 // ============================================================================
368 // wxDisplay::CreateFactory()
369 // ============================================================================
371 /* static */ wxDisplayFactory
*wxDisplay::CreateFactory()
373 // GTK+ screen functions are always available, no need to check for them
375 if ( !XineramaIsActive((Display
*)wxGetDisplay()) )
376 return new wxDisplayFactorySingle
;
379 return new wxDisplayFactoryX11
;
382 #endif /* wxUSE_DISPLAY */
384 #include "wx/utils.h"
386 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
388 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
390 // TODO: don't hardcode display size
401 #else // !wxUSE_LIBHILDON || !wxUSE_LIBHILDON2
405 #include <X11/Xlib.h>
406 #include <X11/Xatom.h>
408 // TODO: make this a full-fledged class and move to a public header
412 wxX11Ptr(void *ptr
= NULL
) : m_ptr(ptr
) { }
413 ~wxX11Ptr() { if ( m_ptr
) XFree(m_ptr
); }
418 wxDECLARE_NO_COPY_CLASS(wxX11Ptr
);
421 // NB: this function is implemented using X11 and not GDK calls as it's shared
422 // by wxGTK[12], wxX11 and wxMotif ports
423 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
425 Display
* const dpy
= wxGetX11Display();
426 wxCHECK_RET( dpy
, wxT("can't be called before initializing the GUI") );
428 const Atom atomWorkArea
= XInternAtom(dpy
, "_NET_WORKAREA", True
);
431 long *workareas
= NULL
;
432 unsigned long numItems
;
433 unsigned long bytesRemaining
;
437 if ( XGetWindowProperty
440 XDefaultRootWindow(dpy
),
442 0, // offset of data to retrieve
443 4, // number of items to retrieve
444 False
, // don't delete property
445 XA_CARDINAL
, // type of the items to get
450 (unsigned char **)&workareas
451 ) == Success
&& workareas
)
453 wxX11Ptr
x11ptr(workareas
); // ensure it will be freed
455 // check that we retrieved the property of the expected type and
456 // that we did get back 4 longs (32 is the format for long), as
458 if ( actualType
!= XA_CARDINAL
||
462 wxLogDebug(wxT("XGetWindowProperty(\"_NET_WORKAREA\") failed"));
471 *width
= workareas
[2];
473 *height
= workareas
[3];
479 // if we get here, _NET_WORKAREA is not supported so return the entire
480 // screen size as fall back
485 wxDisplaySize(width
, height
);
488 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON