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: displayx11.cpp 50143 2007-11-22 02:52:10Z PC $
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:
ScreensInfo()62     ScreensInfo()
63     {
64         m_screens = XineramaQueryScreens((Display *)wxGetDisplay(), &m_num);
65     }
66 
~ScreensInfo()67     ~ScreensInfo()
68     {
69         XFree(m_screens);
70     }
71 
operator const XineramaScreenInfo*() const72     operator const XineramaScreenInfo *() const { return m_screens; }
73 
GetCount() const74     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:
wxDisplayImplX11(unsigned n,const XineramaScreenInfo & info)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 
GetGeometry() const94     virtual wxRect GetGeometry() const { return m_rect; }
GetClientArea() const95     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 
GetName() const103     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:
wxDisplayFactoryX11()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 
GetCount()133 unsigned wxDisplayFactoryX11::GetCount()
134 {
135     return ScreensInfo().GetCount();
136 }
137 
GetFromPoint(const wxPoint & p)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 
CreateDisplay(unsigned n)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 
GetModes(const wxVideoMode & mode) const183 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 
GetCurrentMode() const217 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 
ChangeMode(const wxVideoMode & mode)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 
GetModes(const wxVideoMode & modeMatch) const276 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 
GetCurrentMode() const297 wxVideoMode wxDisplayImplX11::GetCurrentMode() const
298 {
299     // Not implemented
300     return wxVideoMode();
301 }
302 
ChangeMode(const wxVideoMode & WXUNUSED (mode))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 
CreateFactory()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 #if defined(__WXGTK__) || defined(__X__)
328 
329 #include "wx/utils.h"
330 #include "wx/log.h"
331 
332 #include <X11/Xlib.h>
333 #include <X11/Xatom.h>
334 
335 // TODO: make this a full-fledged class and move to a public header
336 class wxX11Ptr
337 {
338 public:
wxX11Ptr(void * ptr=NULL)339     wxX11Ptr(void *ptr = NULL) : m_ptr(ptr) { }
~wxX11Ptr()340     ~wxX11Ptr() { if ( m_ptr ) XFree(m_ptr); }
341 
342 private:
343     void *m_ptr;
344 
345     DECLARE_NO_COPY_CLASS(wxX11Ptr)
346 };
347 
348 // NB: this function is implemented using X11 and not GDK calls as it's shared
349 //     by wxGTK[12], wxX11 and wxMotif ports
wxClientDisplayRect(int * x,int * y,int * width,int * height)350 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
351 {
352     Display * const dpy = (Display *)wxGetDisplay();
353     wxCHECK_RET( dpy, _T("can't be called before initializing the GUI") );
354 
355     const Atom atomWorkArea = XInternAtom(dpy, "_NET_WORKAREA", True);
356     if ( atomWorkArea )
357     {
358         long *workareas = NULL;
359         unsigned long numItems;
360         unsigned long bytesRemaining;
361         Atom actualType;
362         int format;
363 
364         if ( XGetWindowProperty
365              (
366                 dpy,
367                 XDefaultRootWindow(dpy),
368                 atomWorkArea,
369                 0,                          // offset of data to retrieve
370                 4,                          // number of items to retrieve
371                 False,                      // don't delete property
372                 XA_CARDINAL,                // type of the items to get
373                 &actualType,
374                 &format,
375                 &numItems,
376                 &bytesRemaining,
377                 (unsigned char **)&workareas
378              ) == Success && workareas )
379         {
380             wxX11Ptr x11ptr(workareas); // ensure it will be freed
381 
382             // check that we retrieved the property of the expected type and
383             // that we did get back 4 longs (32 is the format for long), as
384             // requested
385             if ( actualType != XA_CARDINAL ||
386                     format != 32 ||
387                         numItems != 4 )
388             {
389                 wxLogDebug(_T("XGetWindowProperty(\"_NET_WORKAREA\") failed"));
390                 return;
391             }
392 
393             if ( x )
394                 *x = workareas[0];
395             if ( y )
396                 *y = workareas[1];
397             if ( width )
398                 *width = workareas[2];
399             if ( height )
400                 *height = workareas[3];
401 
402             return;
403         }
404     }
405 
406     // if we get here, _NET_WORKAREA is not supported so return the entire
407     // screen size as fall back
408     if (x)
409         *x = 0;
410     if (y)
411         *y = 0;
412     wxDisplaySize(width, height);
413 }
414 
415 #else // !(wxGTK or X)
416 
wxClientDisplayRect(int * x,int * y,int * width,int * height)417 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
418 {
419     if (x)
420         *x = 0;
421     if (y)
422         *y = 0;
423     wxDisplaySize(width, height);
424 }
425 
426 #endif // wxGTK or X
427