| 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$ |
| 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: |
| 62 | ScreensInfo() |
| 63 | { |
| 64 | m_screens = XineramaQueryScreens((Display *)wxGetDisplay(), &m_num); |
| 65 | } |
| 66 | |
| 67 | ~ScreensInfo() |
| 68 | { |
| 69 | XFree(m_screens); |
| 70 | } |
| 71 | |
| 72 | operator const XineramaScreenInfo *() const { return m_screens; } |
| 73 | |
| 74 | size_t GetCount() const { return wx_static_cast(size_t, 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: |
| 88 | wxDisplayImplX11(size_t n, const XineramaScreenInfo& info) |
| 89 | : wxDisplayImpl(n), |
| 90 | m_rect(info.x_org, info.y_org, info.width, info.height) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | virtual wxRect GetGeometry() const { return m_rect; } |
| 95 | virtual wxString GetName() const { return wxString(); } |
| 96 | |
| 97 | virtual wxArrayVideoModes GetModes(const wxVideoMode& mode) const; |
| 98 | virtual wxVideoMode GetCurrentMode() const; |
| 99 | virtual bool ChangeMode(const wxVideoMode& mode); |
| 100 | |
| 101 | private: |
| 102 | wxRect m_rect; |
| 103 | int m_depth; |
| 104 | |
| 105 | DECLARE_NO_COPY_CLASS(wxDisplayImplX11) |
| 106 | }; |
| 107 | |
| 108 | class wxDisplayFactoryX11 : public wxDisplayFactory |
| 109 | { |
| 110 | public: |
| 111 | wxDisplayFactoryX11() { } |
| 112 | |
| 113 | virtual wxDisplayImpl *CreateDisplay(size_t n); |
| 114 | virtual size_t GetCount(); |
| 115 | virtual int GetFromPoint(const wxPoint& pt); |
| 116 | |
| 117 | protected: |
| 118 | DECLARE_NO_COPY_CLASS(wxDisplayFactoryX11) |
| 119 | }; |
| 120 | |
| 121 | // ============================================================================ |
| 122 | // wxDisplayFactoryX11 implementation |
| 123 | // ============================================================================ |
| 124 | |
| 125 | size_t wxDisplayFactoryX11::GetCount() |
| 126 | { |
| 127 | return ScreensInfo().GetCount(); |
| 128 | } |
| 129 | |
| 130 | int wxDisplayFactoryX11::GetFromPoint(const wxPoint& p) |
| 131 | { |
| 132 | ScreensInfo screens; |
| 133 | |
| 134 | const size_t numscreens(screens.GetCount()); |
| 135 | for ( size_t i = 0; i < numscreens; ++i ) |
| 136 | { |
| 137 | const XineramaScreenInfo& s = screens[i]; |
| 138 | if ( p.x >= s.x_org && p.x < s.x_org + s.width && |
| 139 | p.y >= s.y_org && p.y < s.y_org + s.height ) |
| 140 | { |
| 141 | return i; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return wxNOT_FOUND; |
| 146 | } |
| 147 | |
| 148 | wxDisplayImpl *wxDisplayFactoryX11::CreateDisplay(size_t n) |
| 149 | { |
| 150 | ScreensInfo screens; |
| 151 | |
| 152 | return n < screens.GetCount() ? new wxDisplayImplX11(n, screens[n]) : NULL; |
| 153 | } |
| 154 | |
| 155 | // ============================================================================ |
| 156 | // wxDisplayImplX11 implementation |
| 157 | // ============================================================================ |
| 158 | |
| 159 | #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H |
| 160 | |
| 161 | // |
| 162 | // See (http://www.xfree86.org/4.2.0/XF86VidModeDeleteModeLine.3.html) for more |
| 163 | // info about xf86 video mode extensions |
| 164 | // |
| 165 | |
| 166 | //free private data common to x (usually s3) servers |
| 167 | #define wxClearXVM(vm) if(vm.privsize) XFree(vm.c_private) |
| 168 | |
| 169 | // Correct res rate from GLFW |
| 170 | #define wxCRR2(v,dc) (int) (((1000.0f * (float) dc) /*PIXELS PER SECOND */) / ((float) v.htotal * v.vtotal /*PIXELS PER FRAME*/) + 0.5f) |
| 171 | #define wxCRR(v) wxCRR2(v,v.dotclock) |
| 172 | #define wxCVM2(v, dc) wxVideoMode(v.hdisplay, v.vdisplay, DefaultDepth((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay())), wxCRR2(v,dc)) |
| 173 | #define wxCVM(v) wxCVM2(v, v.dotclock) |
| 174 | |
| 175 | wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& mode) const |
| 176 | { |
| 177 | //Convenience... |
| 178 | Display* pDisplay = (Display*) wxGetDisplay(); //default display |
| 179 | int nScreen = DefaultScreen(pDisplay); //default screen of (default) display... |
| 180 | |
| 181 | //Some variables.. |
| 182 | XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :)) |
| 183 | int nNumModes; //Number of modes enumerated.... |
| 184 | |
| 185 | wxArrayVideoModes Modes; //modes to return... |
| 186 | |
| 187 | if (XF86VidModeGetAllModeLines(pDisplay, nScreen, &nNumModes, &ppXModes) == TRUE) |
| 188 | { |
| 189 | for (int i = 0; i < nNumModes; ++i) |
| 190 | { |
| 191 | if (mode == wxDefaultVideoMode || //According to display.h All modes valid if dafault mode... |
| 192 | mode.Matches(wxCVM((*ppXModes[i]))) ) //...? |
| 193 | { |
| 194 | Modes.Add(wxCVM((*ppXModes[i]))); |
| 195 | } |
| 196 | wxClearXVM((*ppXModes[i])); |
| 197 | // XFree(ppXModes[i]); //supposed to free? |
| 198 | } |
| 199 | XFree(ppXModes); |
| 200 | } |
| 201 | else //OOPS! |
| 202 | { |
| 203 | wxLogSysError(_("Failed to enumerate video modes")); |
| 204 | } |
| 205 | |
| 206 | return Modes; |
| 207 | } |
| 208 | |
| 209 | wxVideoMode wxDisplayImplX11::GetCurrentMode() const |
| 210 | { |
| 211 | XF86VidModeModeLine VM; |
| 212 | int nDotClock; |
| 213 | XF86VidModeGetModeLine((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), |
| 214 | &nDotClock, &VM); |
| 215 | wxClearXVM(VM); |
| 216 | return wxCVM2(VM, nDotClock); |
| 217 | } |
| 218 | |
| 219 | bool wxDisplayImplX11::ChangeMode(const wxVideoMode& mode) |
| 220 | { |
| 221 | XF86VidModeModeInfo** ppXModes; //Enumerated Modes (Don't forget XFree() :)) |
| 222 | int nNumModes; //Number of modes enumerated.... |
| 223 | |
| 224 | if( !XF86VidModeGetAllModeLines((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), &nNumModes, &ppXModes) ) |
| 225 | { |
| 226 | wxLogSysError(_("Failed to change video mode")); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | bool bRet = false; |
| 231 | if (mode == wxDefaultVideoMode) |
| 232 | { |
| 233 | bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), |
| 234 | ppXModes[0]) == TRUE; |
| 235 | |
| 236 | for (int i = 0; i < nNumModes; ++i) |
| 237 | { |
| 238 | wxClearXVM((*ppXModes[i])); |
| 239 | // XFree(ppXModes[i]); //supposed to free? |
| 240 | } |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | for (int i = 0; i < nNumModes; ++i) |
| 245 | { |
| 246 | if (!bRet && |
| 247 | ppXModes[i]->hdisplay == mode.w && |
| 248 | ppXModes[i]->vdisplay == mode.h && |
| 249 | wxCRR((*ppXModes[i])) == mode.refresh) |
| 250 | { |
| 251 | //switch! |
| 252 | bRet = XF86VidModeSwitchToMode((Display*)wxGetDisplay(), DefaultScreen((Display*)wxGetDisplay()), |
| 253 | ppXModes[i]) == TRUE; |
| 254 | } |
| 255 | wxClearXVM((*ppXModes[i])); |
| 256 | // XFree(ppXModes[i]); //supposed to free? |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | XFree(ppXModes); |
| 261 | |
| 262 | return bRet; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | #else // !HAVE_X11_EXTENSIONS_XF86VMODE_H |
| 267 | |
| 268 | wxArrayVideoModes wxDisplayImplX11::GetModes(const wxVideoMode& mode) const |
| 269 | { |
| 270 | int count_return; |
| 271 | int* depths = XListDepths((Display*)wxGetDisplay(), 0, &count_return); |
| 272 | wxArrayVideoModes modes; |
| 273 | if ( depths ) |
| 274 | { |
| 275 | for ( int x = 0; x < count_return; ++x ) |
| 276 | { |
| 277 | modes.Add(wxVideoMode(m_rect.GetWidth(), m_rect.GetHeight(), depths[x])); |
| 278 | } |
| 279 | |
| 280 | XFree(depths); |
| 281 | } |
| 282 | return modes; |
| 283 | } |
| 284 | |
| 285 | wxVideoMode wxDisplayImplX11::GetCurrentMode() const |
| 286 | { |
| 287 | // Not implemented |
| 288 | return wxVideoMode(); |
| 289 | } |
| 290 | |
| 291 | bool wxDisplayImplX11::ChangeMode(const wxVideoMode& mode) |
| 292 | { |
| 293 | // Not implemented |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | #endif // !HAVE_X11_EXTENSIONS_XF86VMODE_H |
| 298 | |
| 299 | // ============================================================================ |
| 300 | // wxDisplay::CreateFactory() |
| 301 | // ============================================================================ |
| 302 | |
| 303 | /* static */ wxDisplayFactory *wxDisplay::CreateFactory() |
| 304 | { |
| 305 | if ( XineramaIsActive((Display*)wxGetDisplay()) ) |
| 306 | { |
| 307 | return new wxDisplayFactoryX11; |
| 308 | } |
| 309 | |
| 310 | return new wxDisplayFactorySingle; |
| 311 | } |
| 312 | |
| 313 | #endif /* wxUSE_DISPLAY */ |