1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: MSW Implementation of wxDisplay class
4 // Author: Royce Mitchell III
5 // Modified by: VZ (resolutions enumeration/change support, DirectDraw, ...)
6 // Ryan Norton (IsPrimary override)
9 // Copyright: (c) wxWidgets team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
15 // ===========================================================================
17 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
32 #include "wx/dynarray.h"
36 #include "wx/dynload.h"
38 #include "wx/display.h"
40 // the following define is necessary to access the multi-monitor function
41 // declarations in a manner safe to use w/ Windows 95
42 #define COMPILE_MULTIMON_STUBS
44 // if you don't have multimon.h you can download the file from:
46 // http://www.microsoft.com/msj/0697/monitor/monitortextfigs.htm#fig4
50 // as (m)any standard header(s), this one doesn't compile without warnings
52 #pragma warning(disable:4706)
55 // with mingw32, we must include windows.h first and it doesn't hurt with other
62 #pragma warning(default:4706)
67 // we don't want to link with ddraw.lib which contains the real
68 // IID_IDirectDraw2 definition
69 const GUID wxIID_IDirectDraw2
=
70 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
77 #define WINFUNC(x) _T(#x) L"W"
79 #define WINFUNC(x) #x "A"
82 // ----------------------------------------------------------------------------
83 // typedefs for dynamically loaded Windows functions
84 // ----------------------------------------------------------------------------
86 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
92 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
93 LPTSTR driverDescription
,
98 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
102 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
103 LPDIRECTDRAW
*lplpDD
,
104 IUnknown
*pUnkOuter
);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
113 // handle of this monitor used by MonitorXXX() functions, never NULL
116 // IDirectDraw object used to control this display, may be NULL
117 IDirectDraw2
*m_pDD2
;
119 // DirectDraw GUID for this display, only valid when using DirectDraw
122 // the entire area of this monitor in virtual screen coordinates
125 // the display device name for this monitor, empty initially and retrieved
126 // on demand by DoGetName()
129 wxDisplayInfo() { m_hmon
= NULL
; m_pDD2
= NULL
; }
130 ~wxDisplayInfo() { if ( m_pDD2
) m_pDD2
->Release(); }
133 WX_DECLARE_OBJARRAY(wxDisplayInfo
, wxDisplayInfoArray
);
134 #include "wx/arrimpl.cpp"
135 WX_DEFINE_OBJARRAY(wxDisplayInfoArray
);
137 // this module is used to cleanup gs_displays array
138 class wxDisplayModule
: public wxModule
141 virtual bool OnInit() { return true; }
142 virtual void OnExit();
144 DECLARE_DYNAMIC_CLASS(wxDisplayModule
)
147 IMPLEMENT_DYNAMIC_CLASS(wxDisplayModule
, wxModule
)
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 // do we use DirectX?
154 static bool gs_useDirectX
= false;
156 // dynamically resolved DirectDrawCreate()
157 static DirectDrawCreate_t gs_DirectDrawCreate
= NULL
;
159 // this is not really MT-unsafe as wxDisplay is only going to be used from the
160 // main thread, i.e. we consider that it's a GUI class and so don't protect it
161 static wxDisplayInfoArray
*gs_displays
= NULL
;
163 // ===========================================================================
165 // ===========================================================================
167 // ----------------------------------------------------------------------------
168 // callbacks for monitor/modes enumeration stuff
169 // ----------------------------------------------------------------------------
171 static BOOL CALLBACK
wxmswMonitorEnumProc (
172 HMONITOR hMonitor
, // handle to display monitor
173 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
174 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
175 LPARAM
WXUNUSED(dwData
) // data passed from EnumDisplayMonitors (unused)
178 wxDisplayInfo
*info
= new wxDisplayInfo();
180 // we need hMonitor to be able to map display id to it which is needed for
181 // MonitorXXX() functions, in particular MonitorFromPoint()
182 info
->m_hmon
= hMonitor
;
184 // we also store the display geometry
185 info
->m_rect
.SetX ( lprcMonitor
->left
);
186 info
->m_rect
.SetY ( lprcMonitor
->top
);
187 info
->m_rect
.SetWidth ( lprcMonitor
->right
- lprcMonitor
->left
);
188 info
->m_rect
.SetHeight ( lprcMonitor
->bottom
- lprcMonitor
->top
);
190 // now add this monitor to the array
191 gs_displays
->Add(info
);
193 // continue the enumeration
198 wxDDEnumExCallback(GUID
*pGuid
,
199 LPTSTR
WXUNUSED(driverDescription
),
201 LPVOID
WXUNUSED(lpContext
),
206 wxDisplayInfo
*info
= new wxDisplayInfo();
209 info
->m_guid
= *pGuid
;
210 info
->m_devName
= driverName
;
212 gs_displays
->Add(info
);
214 //else: we're called for the primary monitor, skip it
216 // continue the enumeration
220 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
223 // we need at least the mode size
224 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
225 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
227 wxArrayVideoModes
* const modes
= (wxArrayVideoModes
*)lpContext
;
229 modes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
230 lpDDSurfaceDesc
->dwHeight
,
231 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
232 lpDDSurfaceDesc
->dwRefreshRate
));
235 // continue the enumeration
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 // initialize gs_displays using DirectX functions
244 static bool DoInitDirectX()
247 // suppress the errors if ddraw.dll is not found
248 wxLog::EnableLogging(false);
251 wxDynamicLibrary
dllDX(_T("ddraw.dll"));
254 wxLog::EnableLogging();
257 if ( !dllDX
.IsLoaded() )
260 DirectDrawEnumerateEx_t pDDEnumEx
= (DirectDrawEnumerateEx_t
)
261 dllDX
.GetSymbol(WINFUNC(DirectDrawEnumerateEx
));
265 // we'll also need DirectDrawCreate() later, resolve it right now
266 gs_DirectDrawCreate
= (DirectDrawCreate_t
)
267 dllDX
.GetSymbol(_T("DirectDrawCreate"));
268 if ( !gs_DirectDrawCreate
)
271 if ( (*pDDEnumEx
)(wxDDEnumExCallback
,
273 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
278 // ok, it seems like we're going to use DirectDraw and so we're going to
279 // need ddraw.dll all the time, don't unload it
285 // initialize gs_displays using the standard Windows functions
286 static void DoInitStdWindows()
288 // enumerate all displays
289 if ( !::EnumDisplayMonitors(NULL
, NULL
, wxmswMonitorEnumProc
, 0) )
291 wxLogLastError(wxT("EnumDisplayMonitors"));
293 // TODO: still create at least one (valid) entry in gs_displays for the
298 // this function must be called before accessing gs_displays array as it
299 // creates and initializes it
300 static void InitDisplays()
305 gs_displays
= new wxDisplayInfoArray();
307 if ( !gs_useDirectX
|| !DoInitDirectX() )
309 // either we were told not to try to use DirectX or fall back to std
310 // functions if DirectX method failed
311 gs_useDirectX
= false;
317 // convert a DEVMODE to our wxVideoMode
318 wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
320 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one" and
321 // although 0 is ok for us we don't want to return modes with 1hz refresh
322 return wxVideoMode(dm
.dmPelsWidth
,
325 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
328 // emulation of ChangeDisplaySettingsEx() for Win95
329 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
333 LPVOID
WXUNUSED(lParam
))
335 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 void wxDisplayModule::OnExit()
347 // ---------------------------------------------------------------------------
349 // ---------------------------------------------------------------------------
352 void wxDisplay::UseDirectX(bool useDX
)
354 wxCHECK_RET( !gs_displays
, _T("it is too late to call UseDirectX") );
356 gs_useDirectX
= useDX
;
359 // helper of GetFromPoint() and GetFromWindow()
360 static int DisplayFromHMONITOR(HMONITOR hmon
)
364 const size_t count
= wxDisplay::GetCount();
366 for ( size_t n
= 0; n
< count
; n
++ )
368 if ( hmon
== (*gs_displays
)[n
].m_hmon
)
377 size_t wxDisplayBase::GetCount()
381 //RN: FIXME: This is wrong - the display info array should reload after every call
382 //to GetCount() - otherwise it will not be accurate.
383 //The user can change the number of displays in display properties/settings
384 //after GetCount or similar is called and really mess this up...
385 //wxASSERT_MSG( gs_displays->GetCount() == (size_t)::GetSystemMetrics(SM_CMONITORS),
386 // _T("So how many displays does this system have?") );
388 return gs_displays
->GetCount();
392 int wxDisplayBase::GetFromPoint ( const wxPoint
& pt
)
398 return DisplayFromHMONITOR(::MonitorFromPoint(pt2
, MONITOR_DEFAULTTONULL
));
402 int wxDisplayBase::GetFromWindow(wxWindow
*window
)
404 return DisplayFromHMONITOR
406 ::MonitorFromWindow(GetHwndOf(window
), MONITOR_DEFAULTTONULL
)
410 // ----------------------------------------------------------------------------
411 // wxDisplay ctor/dtor
412 // ----------------------------------------------------------------------------
414 wxDisplay::wxDisplay ( size_t n
)
415 : wxDisplayBase ( n
)
417 // if we do this in ctor we won't have to call it from all the member
423 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[n
];
425 LPDIRECTDRAW2
& pDD2
= dpyInfo
.m_pDD2
;
428 if ( !gs_DirectDrawCreate
)
435 HRESULT hr
= (*gs_DirectDrawCreate
)(&dpyInfo
.m_guid
, &pDD
, NULL
);
437 if ( FAILED(hr
) || !pDD
)
440 wxLogApiError(_T("DirectDrawCreate"), hr
);
442 else // got IDirectDraw, we want IDirectDraw2
444 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&pDD2
);
445 if ( FAILED(hr
) || !pDD2
)
447 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr
);
453 //else: DirectDraw object corresponding to our display already exists
455 // increment its ref count to account for Release() in dtor
457 // NB: pDD2 will be only really Release()d when gs_displays is
458 // destroyed which is ok as we don't want to recreate DD objects
464 wxDisplay::~wxDisplay()
466 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
468 LPDIRECTDRAW2
& pDD2
= dpyInfo
.m_pDD2
;
475 // ----------------------------------------------------------------------------
476 // wxDisplay simple accessors
477 // ----------------------------------------------------------------------------
479 bool wxDisplay::IsOk() const
481 return m_index
< GetCount() &&
482 (!gs_useDirectX
|| (*gs_displays
)[m_index
].m_pDD2
);
485 wxRect
wxDisplay::GetGeometry() const
487 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
488 wxRect
& rect
= dpyInfo
.m_rect
;
492 wxZeroMemory(monInfo
);
493 monInfo
.cbSize
= sizeof(monInfo
);
495 if ( !::GetMonitorInfo(dpyInfo
.m_hmon
, &monInfo
) )
497 wxLogLastError(_T("GetMonitorInfo"));
501 wxCopyRECTToRect(monInfo
.rcMonitor
, rect
);
508 wxString
wxDisplay::GetName() const
510 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
511 if ( dpyInfo
.m_devName
.empty() )
513 MONITORINFOEX monInfo
;
514 wxZeroMemory(monInfo
);
515 monInfo
.cbSize
= sizeof(monInfo
);
517 // NB: Cast from MONITORINFOEX* to MONITORINFO* is done because
518 // Mingw headers - unlike the ones from Microsoft's Platform SDK -
519 // don't derive the former from the latter in C++ mode and so
520 // the pointer's type is not converted implicitly.
521 if ( !::GetMonitorInfo(dpyInfo
.m_hmon
, (LPMONITORINFO
)&monInfo
) )
523 wxLogLastError(_T("GetMonitorInfo"));
527 dpyInfo
.m_devName
= monInfo
.szDevice
;
531 return dpyInfo
.m_devName
;
534 wxString
wxDisplay::GetNameForEnumSettings() const
537 const bool isWin95
= wxGetOsVersion(&major
, &minor
) == wxWIN95
&&
538 major
== 4 && minor
== 0;
540 // the first parameter of EnumDisplaySettings() must be NULL under Win95
541 // according to MSDN but GetMonitorInfo() stub in multimon.h still returns
542 // something even in this case, so we have to correct this manually
550 // ----------------------------------------------------------------------------
551 // determine if this is the primary display
552 // ----------------------------------------------------------------------------
554 bool wxDisplay::IsPrimary() const
556 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
558 MONITORINFOEX monInfo
;
559 wxZeroMemory(monInfo
);
560 monInfo
.cbSize
= sizeof(monInfo
);
562 // NB: Cast from MONITORINFOEX* to MONITORINFO* is done because
563 // Mingw headers - unlike the ones from Microsoft's Platform SDK -
564 // don't derive the former from the latter in C++ mode and so
565 // the pointer's type is not converted implicitly.
566 if ( !::GetMonitorInfo(dpyInfo
.m_hmon
, (LPMONITORINFO
)&monInfo
) )
568 wxLogLastError(_T("GetMonitorInfo"));
571 return (monInfo
.dwFlags
& MONITORINFOF_PRIMARY
) == MONITORINFOF_PRIMARY
;
574 // ----------------------------------------------------------------------------
575 // video modes enumeration
576 // ----------------------------------------------------------------------------
579 wxDisplay::DoGetModesDirectX(const wxVideoMode
& WXUNUSED(modeMatch
)) const
581 wxArrayVideoModes modes
;
583 IDirectDraw2
*pDD
= (*gs_displays
)[m_index
].m_pDD2
;
587 HRESULT hr
= pDD
->EnumDisplayModes
590 NULL
, // all modes (TODO: use modeMatch!)
591 &modes
, // callback parameter
592 wxDDEnumModesCallback
597 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr
);
605 wxDisplay::DoGetModesWindows(const wxVideoMode
& modeMatch
) const
607 wxArrayVideoModes modes
;
609 const wxString name
= GetNameForEnumSettings();
611 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
614 for ( int iModeNum
= 0;
615 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
618 const wxVideoMode mode
= ConvertToVideoMode(dm
);
619 if ( mode
.Matches(modeMatch
) )
628 wxArrayVideoModes
wxDisplay::GetModes(const wxVideoMode
& modeMatch
) const
630 return gs_useDirectX
? DoGetModesDirectX(modeMatch
)
631 : DoGetModesWindows(modeMatch
);
634 wxVideoMode
wxDisplay::GetCurrentMode() const
638 const wxString name
= GetNameForEnumSettings();
641 if ( !::EnumDisplaySettings(name
.empty() ? NULL
: name
.c_str(),
642 ENUM_CURRENT_SETTINGS
,
645 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
649 mode
= ConvertToVideoMode(dm
);
655 // ----------------------------------------------------------------------------
656 // video mode switching
657 // ----------------------------------------------------------------------------
659 bool wxDisplay::DoChangeModeDirectX(const wxVideoMode
& mode
)
661 IDirectDraw2
*pDD
= (*gs_displays
)[m_index
].m_pDD2
;
665 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
666 wxCHECK_MSG( winTop
, false, _T("top level window required for DirectX") );
668 HRESULT hr
= pDD
->SetCooperativeLevel
671 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
675 wxLogApiError(_T("IDirectDraw::SetCooperativeLevel"), hr
);
680 hr
= pDD
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
683 wxLogApiError(_T("IDirectDraw::SetDisplayMode"), hr
);
692 bool wxDisplay::DoChangeModeWindows(const wxVideoMode
& mode
)
694 // prepare ChangeDisplaySettingsEx() parameters
699 if ( mode
== wxDefaultVideoMode
)
701 // reset the video mode to default
705 else // change to the given mode
707 wxCHECK_MSG( mode
.w
&& mode
.h
, false,
708 _T("at least the width and height must be specified") );
711 dm
.dmSize
= sizeof(dm
);
712 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
713 dm
.dmPelsWidth
= mode
.w
;
714 dm
.dmPelsHeight
= mode
.h
;
718 dm
.dmFields
|= DM_BITSPERPEL
;
719 dm
.dmBitsPerPel
= mode
.bpp
;
724 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
725 dm
.dmDisplayFrequency
= mode
.refresh
;
730 flags
= CDS_FULLSCREEN
;
734 // get pointer to the function dynamically
736 // we're only called from the main thread, so it's ok to use static
738 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
739 if ( !pfnChangeDisplaySettingsEx
)
741 wxDynamicLibrary
dllUser32(_T("user32.dll"));
742 if ( dllUser32
.IsLoaded() )
744 pfnChangeDisplaySettingsEx
= (ChangeDisplaySettingsEx_t
)
745 dllUser32
.GetSymbol(WINFUNC(ChangeDisplaySettingsEx
));
747 //else: huh, no user32.dll??
749 if ( !pfnChangeDisplaySettingsEx
)
751 // we must be under Win95 and so there is no multiple monitors
753 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
757 // do change the mode
758 switch ( pfnChangeDisplaySettingsEx
760 GetName(), // display name
761 pDevMode
, // dev mode or NULL to reset
764 NULL
// pointer to video parameters (not used)
767 case DISP_CHANGE_SUCCESSFUL
:
770 // If we have a top-level, full-screen frame, emulate
771 // the DirectX behavior and resize it. This makes this
772 // API quite a bit easier to use.
773 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
774 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
775 if (frameTop
&& frameTop
->IsFullScreen())
777 wxVideoMode current
= GetCurrentMode();
778 frameTop
->SetClientSize(current
.w
, current
.h
);
783 case DISP_CHANGE_BADMODE
:
784 // don't complain about this, this is the only "expected" error
788 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
794 bool wxDisplay::ChangeMode(const wxVideoMode
& mode
)
796 return gs_useDirectX
? DoChangeModeDirectX(mode
)
797 : DoChangeModeWindows(mode
);
800 #endif // wxUSE_DISPLAY