]> git.saurik.com Git - wxWidgets.git/blob - src/unix/displayx11.cpp
0a3a132c67353d5b5250a34c9105dba664354629
[wxWidgets.git] / src / unix / displayx11.cpp
1 ///////////////////////////////////////////////////////////////////////////
2 // Name: displayx11.cpp
3 // Purpose: Unix/X11 implementation of wxDisplay class
4 // Author: Brian Victor
5 // Modified by:
6 // Created: 12/05/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "display.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/display.h"
24 #include "wx/log.h"
25
26 #ifndef WX_PRECOMP
27 #include "wx/dynarray.h"
28 #include "wx/gdicmn.h"
29 #include "wx/string.h"
30 #include "wx/utils.h"
31 #endif /* WX_PRECOMP */
32
33 #if wxUSE_DISPLAY
34
35 /* These must be included after the wx files. Otherwise the Data macro in
36 * Xlibint.h conflicts with a function declaration in wx/list.h. */
37 extern "C" {
38 #include <X11/Xlib.h>
39 #include <X11/Xlibint.h>
40 #include <X11/extensions/Xinerama.h>
41 #include <X11/extensions/xf86vmode.h>
42 }
43
44 class wxDisplayUnixPriv
45 {
46 public:
47 wxRect m_rect;
48 int m_depth;
49 };
50
51 size_t wxDisplayBase::GetCount()
52 {
53 Display *disp = (Display*)wxGetDisplay();
54
55 if ( XineramaIsActive(disp) )
56 {
57 XineramaScreenInfo *screenarr;
58 int numscreens;
59 screenarr = XineramaQueryScreens(disp, &numscreens);
60 XFree(screenarr);
61 return numscreens;
62 }
63 else
64 {
65 return 1;
66 }
67 }
68
69 int wxDisplayBase::GetFromPoint(const wxPoint &p)
70 {
71 Display *disp = (Display*)wxGetDisplay();
72
73 if ( XineramaIsActive(disp) )
74 {
75 int which_screen = -1;
76 XineramaScreenInfo *screenarr;
77 int numscreens;
78 screenarr = XineramaQueryScreens(disp, &numscreens);
79
80 int i;
81 for (i = 0; i < numscreens; ++i)
82 {
83 if (p.x >= screenarr[i].x_org &&
84 p.x < screenarr[i].x_org + screenarr[i].width &&
85 p.y >= screenarr[i].y_org &&
86 p.y < screenarr[i].y_org + screenarr[i].height)
87 {
88 which_screen = i;
89 }
90 }
91
92 XFree(screenarr);
93 return which_screen;
94 }
95 else
96 {
97 wxSize size = wxGetDisplaySize();
98 if (p.x >= 0 &&
99 p.x < size.GetWidth() &&
100 p.y >= 0 &&
101 p.y < size.GetHeight())
102 {
103 return 0;
104 }
105
106 return -1;
107 }
108 }
109
110 wxDisplay::wxDisplay(size_t index) : wxDisplayBase ( index ), m_priv( new wxDisplayUnixPriv )
111 {
112 Display *disp = (Display*)wxGetDisplay();
113
114 if ( XineramaIsActive(disp) )
115 {
116 XineramaScreenInfo *screenarr;
117 int numscreens;
118 screenarr = XineramaQueryScreens(disp, &numscreens);
119 m_priv->m_rect = wxRect(screenarr[index].x_org, screenarr[index].y_org,
120 screenarr[index].width, screenarr[index].height);
121 m_priv->m_depth = DefaultDepth(disp, DefaultScreen(disp));
122 XFree(screenarr);
123 }
124 else
125 {
126 wxSize size = wxGetDisplaySize();
127 m_priv->m_rect = wxRect(0, 0, size.GetWidth(), size.GetHeight());
128 m_priv->m_depth = wxDisplayDepth();
129 }
130 }
131
132 wxDisplay::~wxDisplay()
133 {
134 delete m_priv;
135 }
136
137 wxRect wxDisplay::GetGeometry() const
138 {
139 return m_priv->m_rect;
140 }
141
142 int wxDisplay::GetDepth() const
143 {
144 return m_priv->m_depth;
145 }
146
147 wxString wxDisplay::GetName() const
148 {
149 return wxEmptyString;
150 }
151
152 //
153 // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more
154 // info about xf86 video mode extensions
155 //
156
157 //free private data common to x (usually s3) servers
158 #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private)
159
160 //Correct res rate from GLFW, which probably has the perfect license :)
161 #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f)
162 #define wxCRR(v) wxCRR2(v,v.dotclock)
163 #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc))
164 #define wxCVM(v) wxCVM2(v, v.dotclock)
165
166 wxArrayVideoModes wxDisplay::GetModes(const wxVideoMode& mode) const
167 {
168 //Convenience...
169 Display* pDisplay = (Display*) wxGetDisplay(); //default display
170 int nScreen = DefaultScreen(pDisplay); //default screen of (default) display...
171
172 //Some variables..
173 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
174 int nNumModes; //Number of modes enumerated....
175
176 wxArrayVideoModes Modes; //modes to return...
177
178 if (XF86VidModeGetAllModeLines(pDisplay, nScreen, &nNumModes, &ppXModes) == TRUE)
179 {
180 for (int i = 0; i < nNumModes; ++i)
181 {
182 if (mode == wxDefaultVideoMode || //According to display.h All modes valid if dafault mode...
183 mode.Matches(wxCVM((*ppXModes[i]))) ) //...?
184 {
185 Modes.Add(wxCVM((*ppXModes[i])));
186 }
187 wxClearXVM((*ppXModes[i]));
188 // XFree(ppXModes[i]); //supposed to free?
189 }
190 XFree(ppXModes);
191 }
192 else //OOPS!
193 {
194 wxLogSysError("XF86VidModeGetAllModeLines Failed in wxX11Display::GetModes()!");
195 }
196
197 return Modes;
198 }
199
200 wxVideoMode wxDisplay::GetCurrentMode() const
201 {
202 XF86VidModeModeLine VM;
203 int nDotClock;
204 XF86VidModeGetModeLine((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
205 &nDotClock, &VM);
206 wxClearXVM(VM);
207 return wxCVM2(VM, nDotClock);
208 }
209
210 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
211 {
212 //This gets kind of tricky AND complicated :) :\ :( :)
213 {
214 bool bRet = false;
215 //Some variables..
216 XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :))
217 int nNumModes; //Number of modes enumerated....
218
219 if(XF86VidModeGetAllModeLines((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), &nNumModes, &ppXModes) == TRUE)
220 {
221 if (mode == wxDefaultVideoMode)
222 {
223 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
224 ppXModes[0]) == TRUE;
225
226 for (int i = 0; i < nNumModes; ++i)
227 {
228 wxClearXVM((*ppXModes[i]));
229 // XFree(ppXModes[i]); //supposed to free?
230 }
231 XFree(ppXModes);
232
233 return bRet;
234 }
235 for (int i = 0; i < nNumModes; ++i)
236 {
237 if (!bRet &&
238 ppXModes[i]->hdisplay == mode.w &&
239 ppXModes[i]->vdisplay == mode.h &&
240 wxCRR((*ppXModes[i])) == mode.refresh)
241 {
242 //switch!
243 bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()),
244 ppXModes[i]) == TRUE;
245 }
246 wxClearXVM((*ppXModes[i]));
247 // XFree(ppXModes[i]); //supposed to free?
248 }
249 XFree(ppXModes);
250
251 return bRet;
252 }
253 else //OOPS!
254 {
255 wxLogSysError("XF86VidModeGetAllModeLines Failed in wxX11Display::ChangeMode()!");
256 return false;
257 }
258 }
259 /*
260 //Brian Victor's patch (X11 can't change bit depth yet), here for reference
261 Display *disp = (Display*)wxGetDisplay();
262 int count_return;
263 int* depths = XListDepths(disp, 0, &count_return);
264 wxArrayVideoModes modes;
265 if (depths)
266 {
267 int x;
268 for (x = 0; x < count_return; ++x)
269 {
270 modes.Add(wxVideoMode(m_priv->m_rect.GetWidth(), m_priv->m_rect.GetHeight(), depths[x]));
271 }
272 }
273 return modes;
274 */
275 }
276
277 #endif /* wxUSE_DISPLAY */
278