]>
Commit | Line | Data |
---|---|---|
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 | 44 | extern "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 | ||
59 | class ScreensInfo | |
b1b3ddd8 | 60 | { |
ef1717a9 VZ |
61 | public: |
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 | |
76 | private: | |
77 | XineramaScreenInfo *m_screens; | |
78 | int m_num; | |
79 | }; | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // display and display factory classes | |
83 | // ---------------------------------------------------------------------------- | |
b1b3ddd8 | 84 | |
ef1717a9 | 85 | class WXDLLEXPORT wxDisplayImplX11 : public wxDisplayImpl |
b1b3ddd8 | 86 | { |
ef1717a9 | 87 | public: |
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 | ||
109 | private: | |
110 | wxRect m_rect; | |
111 | int m_depth; | |
112 | ||
113 | DECLARE_NO_COPY_CLASS(wxDisplayImplX11) | |
114 | }; | |
115 | ||
116 | class wxDisplayFactoryX11 : public wxDisplayFactory | |
b1b3ddd8 | 117 | { |
ef1717a9 | 118 | public: |
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 |
125 | protected: |
126 | DECLARE_NO_COPY_CLASS(wxDisplayFactoryX11) | |
127 | }; | |
128 | ||
129 | // ============================================================================ | |
130 | // wxDisplayFactoryX11 implementation | |
131 | // ============================================================================ | |
132 | ||
4e675101 | 133 | unsigned wxDisplayFactoryX11::GetCount() |
b1b3ddd8 | 134 | { |
ef1717a9 | 135 | return ScreensInfo().GetCount(); |
b1b3ddd8 JS |
136 | } |
137 | ||
ef1717a9 | 138 | int 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 | 156 | wxDisplayImpl *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 | 183 | wxArrayVideoModes 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 | 217 | wxVideoMode 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 | 227 | bool 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 | 276 | wxArrayVideoModes 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 | 297 | wxVideoMode wxDisplayImplX11::GetCurrentMode() const |
505c8ccd VS |
298 | { |
299 | // Not implemented | |
300 | return wxVideoMode(); | |
301 | } | |
302 | ||
d5a20ebb | 303 | bool 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 | ||
329 | #include <X11/Xatom.h> | |
330 | ||
331 | // TODO: make this a full-fledged class and move to a public header | |
332 | class wxX11Ptr | |
333 | { | |
334 | public: | |
335 | wxX11Ptr(void *ptr = NULL) : m_ptr(ptr) { } | |
336 | ~wxX11Ptr() { if ( m_ptr ) XFree(m_ptr); } | |
337 | ||
338 | private: | |
339 | void *m_ptr; | |
340 | ||
341 | DECLARE_NO_COPY_CLASS(wxX11Ptr) | |
342 | }; | |
343 | ||
344 | // NB: this function is implemented using X11 and not GDK calls as it's shared | |
345 | // by wxGTK[12], wxX11 and wxMotif ports | |
346 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) | |
347 | { | |
348 | Display * const dpy = wxGetX11Display(); | |
349 | wxCHECK_RET( dpy, _T("can't be called before initializing the GUI") ); | |
350 | ||
351 | const Atom atomWorkArea = XInternAtom(dpy, "_NET_WORKAREA", True); | |
352 | if ( atomWorkArea ) | |
353 | { | |
354 | long *workareas = NULL; | |
355 | unsigned long numItems; | |
356 | unsigned long bytesRemaining; | |
357 | Atom actualType; | |
358 | int format; | |
359 | ||
360 | if ( XGetWindowProperty | |
361 | ( | |
362 | dpy, | |
363 | XDefaultRootWindow(dpy), | |
364 | atomWorkArea, | |
365 | 0, // offset of data to retrieve | |
366 | 4, // number of items to retrieve | |
367 | False, // don't delete property | |
368 | XA_CARDINAL, // type of the items to get | |
369 | &actualType, | |
370 | &format, | |
371 | &numItems, | |
372 | &bytesRemaining, | |
373 | (unsigned char **)&workareas | |
374 | ) == Success && workareas ) | |
375 | { | |
376 | wxX11Ptr x11ptr(workareas); // ensure it will be freed | |
377 | ||
378 | if ( actualType != XA_CARDINAL || | |
379 | format != 32 || // FIXME: what is this 32? | |
380 | numItems != 4 ) | |
381 | { | |
382 | wxLogDebug(_T("XGetWindowProperty(\"_NET_WORKAREA\") failed")); | |
383 | return; | |
384 | } | |
385 | ||
386 | if ( x ) | |
387 | *x = workareas[0]; | |
388 | if ( y ) | |
389 | *y = workareas[1]; | |
390 | if ( width ) | |
391 | *width = workareas[2]; | |
392 | if ( height ) | |
393 | *height = workareas[3]; | |
394 | ||
395 | return; | |
396 | } | |
397 | } | |
398 | ||
399 | // if we get here, _NET_WORKAREA is not supported so return the entire | |
400 | // screen size as fall back | |
401 | if (x) | |
402 | *x = 0; | |
403 | if (y) | |
404 | *y = 0; | |
405 | wxDisplaySize(width, height); | |
406 | } | |
407 |