1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/display.cpp
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
57 #include "wx/msw/wrapwin.h"
61 #endif // !__WXWINCE__
64 #pragma warning(default:4706)
69 // we don't want to link with ddraw.lib which contains the real
70 // IID_IDirectDraw2 definition
71 const GUID wxIID_IDirectDraw2
=
72 { 0xB3A6F3E0, 0x2B43, 0x11CF, { 0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 } };
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
79 #define WINFUNC(x) _T(#x) L"W"
81 #define WINFUNC(x) #x "A"
84 // ----------------------------------------------------------------------------
85 // typedefs for dynamically loaded Windows functions
86 // ----------------------------------------------------------------------------
88 typedef LONG (WINAPI
*ChangeDisplaySettingsEx_t
)(LPCTSTR lpszDeviceName
,
94 typedef BOOL (PASCAL
*DDEnumExCallback_t
)(GUID
*pGuid
,
95 LPTSTR driverDescription
,
100 typedef HRESULT (WINAPI
*DirectDrawEnumerateEx_t
)(DDEnumExCallback_t lpCallback
,
104 typedef HRESULT (WINAPI
*DirectDrawCreate_t
)(GUID
*lpGUID
,
105 LPDIRECTDRAW
*lplpDD
,
106 IUnknown
*pUnkOuter
);
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
115 // handle of this monitor used by MonitorXXX() functions, never NULL
118 // IDirectDraw object used to control this display, may be NULL
119 IDirectDraw2
*m_pDD2
;
121 // DirectDraw GUID for this display, only valid when using DirectDraw
124 // the entire area of this monitor in virtual screen coordinates
127 // the display device name for this monitor, empty initially and retrieved
128 // on demand by DoGetName()
131 wxDisplayInfo() { m_hmon
= NULL
; m_pDD2
= NULL
; }
132 ~wxDisplayInfo() { if ( m_pDD2
) m_pDD2
->Release(); }
135 WX_DECLARE_OBJARRAY(wxDisplayInfo
, wxDisplayInfoArray
);
136 #include "wx/arrimpl.cpp"
137 WX_DEFINE_OBJARRAY(wxDisplayInfoArray
);
139 // this module is used to cleanup gs_displays array
140 class wxDisplayModule
: public wxModule
143 virtual bool OnInit() { return true; }
144 virtual void OnExit();
146 DECLARE_DYNAMIC_CLASS(wxDisplayModule
)
149 IMPLEMENT_DYNAMIC_CLASS(wxDisplayModule
, wxModule
)
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 // do we use DirectX?
156 static bool gs_useDirectX
= false;
158 // dynamically resolved DirectDrawCreate()
159 static DirectDrawCreate_t gs_DirectDrawCreate
= NULL
;
161 // this is not really MT-unsafe as wxDisplay is only going to be used from the
162 // main thread, i.e. we consider that it's a GUI class and so don't protect it
163 static wxDisplayInfoArray
*gs_displays
= NULL
;
165 // ===========================================================================
167 // ===========================================================================
169 // ----------------------------------------------------------------------------
170 // callbacks for monitor/modes enumeration stuff
171 // ----------------------------------------------------------------------------
173 static BOOL CALLBACK
wxmswMonitorEnumProc (
174 HMONITOR hMonitor
, // handle to display monitor
175 HDC
WXUNUSED(hdcMonitor
), // handle to monitor-appropriate device context
176 LPRECT lprcMonitor
, // pointer to monitor intersection rectangle
177 LPARAM
WXUNUSED(dwData
) // data passed from EnumDisplayMonitors (unused)
180 wxDisplayInfo
*info
= new wxDisplayInfo();
182 // we need hMonitor to be able to map display id to it which is needed for
183 // MonitorXXX() functions, in particular MonitorFromPoint()
184 info
->m_hmon
= hMonitor
;
186 // we also store the display geometry
187 info
->m_rect
.SetX ( lprcMonitor
->left
);
188 info
->m_rect
.SetY ( lprcMonitor
->top
);
189 info
->m_rect
.SetWidth ( lprcMonitor
->right
- lprcMonitor
->left
);
190 info
->m_rect
.SetHeight ( lprcMonitor
->bottom
- lprcMonitor
->top
);
192 // now add this monitor to the array
193 gs_displays
->Add(info
);
195 // continue the enumeration
200 wxDDEnumExCallback(GUID
*pGuid
,
201 LPTSTR
WXUNUSED(driverDescription
),
203 LPVOID
WXUNUSED(lpContext
),
208 wxDisplayInfo
*info
= new wxDisplayInfo();
211 info
->m_guid
= *pGuid
;
212 info
->m_devName
= driverName
;
214 gs_displays
->Add(info
);
216 //else: we're called for the primary monitor, skip it
218 // continue the enumeration
222 HRESULT WINAPI
wxDDEnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc
,
225 // we need at least the mode size
226 static const DWORD FLAGS_REQUIRED
= DDSD_HEIGHT
| DDSD_WIDTH
;
227 if ( (lpDDSurfaceDesc
->dwFlags
& FLAGS_REQUIRED
) == FLAGS_REQUIRED
)
229 wxArrayVideoModes
* const modes
= (wxArrayVideoModes
*)lpContext
;
231 modes
->Add(wxVideoMode(lpDDSurfaceDesc
->dwWidth
,
232 lpDDSurfaceDesc
->dwHeight
,
233 lpDDSurfaceDesc
->ddpfPixelFormat
.dwRGBBitCount
,
234 lpDDSurfaceDesc
->dwRefreshRate
));
237 // continue the enumeration
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 // initialize gs_displays using DirectX functions
246 static bool DoInitDirectX()
249 // suppress the errors if ddraw.dll is not found
250 wxLog::EnableLogging(false);
253 wxDynamicLibrary
dllDX(_T("ddraw.dll"));
256 wxLog::EnableLogging();
259 if ( !dllDX
.IsLoaded() )
262 DirectDrawEnumerateEx_t pDDEnumEx
= (DirectDrawEnumerateEx_t
)
263 dllDX
.GetSymbol(WINFUNC(DirectDrawEnumerateEx
));
267 // we'll also need DirectDrawCreate() later, resolve it right now
268 gs_DirectDrawCreate
= (DirectDrawCreate_t
)
269 dllDX
.GetSymbol(_T("DirectDrawCreate"));
270 if ( !gs_DirectDrawCreate
)
273 if ( (*pDDEnumEx
)(wxDDEnumExCallback
,
275 DDENUM_ATTACHEDSECONDARYDEVICES
) != DD_OK
)
280 // ok, it seems like we're going to use DirectDraw and so we're going to
281 // need ddraw.dll all the time, don't unload it
287 // initialize gs_displays using the standard Windows functions
288 static void DoInitStdWindows()
290 // enumerate all displays
291 if ( !::EnumDisplayMonitors(NULL
, NULL
, wxmswMonitorEnumProc
, 0) )
293 wxLogLastError(wxT("EnumDisplayMonitors"));
295 // TODO: still create at least one (valid) entry in gs_displays for the
300 // this function must be called before accessing gs_displays array as it
301 // creates and initializes it
302 static void InitDisplays()
307 gs_displays
= new wxDisplayInfoArray();
309 if ( !gs_useDirectX
|| !DoInitDirectX() )
311 // either we were told not to try to use DirectX or fall back to std
312 // functions if DirectX method failed
313 gs_useDirectX
= false;
319 // convert a DEVMODE to our wxVideoMode
320 wxVideoMode
ConvertToVideoMode(const DEVMODE
& dm
)
322 // note that dmDisplayFrequency may be 0 or 1 meaning "standard one" and
323 // although 0 is ok for us we don't want to return modes with 1hz refresh
324 return wxVideoMode(dm
.dmPelsWidth
,
327 dm
.dmDisplayFrequency
> 1 ? dm
.dmDisplayFrequency
: 0);
331 // emulation of ChangeDisplaySettingsEx() for Win95
332 LONG WINAPI
ChangeDisplaySettingsExForWin95(LPCTSTR
WXUNUSED(lpszDeviceName
),
336 LPVOID
WXUNUSED(lParam
))
338 return ::ChangeDisplaySettings(lpDevMode
, dwFlags
);
340 #endif // !__WXWINCE__
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
346 void wxDisplayModule::OnExit()
351 // ---------------------------------------------------------------------------
353 // ---------------------------------------------------------------------------
356 void wxDisplay::UseDirectX(bool useDX
)
358 wxCHECK_RET( !gs_displays
, _T("it is too late to call UseDirectX") );
360 gs_useDirectX
= useDX
;
363 // helper of GetFromPoint() and GetFromWindow()
364 static int DisplayFromHMONITOR(HMONITOR hmon
)
368 const size_t count
= wxDisplay::GetCount();
370 for ( size_t n
= 0; n
< count
; n
++ )
372 if ( hmon
== (*gs_displays
)[n
].m_hmon
)
381 size_t wxDisplayBase::GetCount()
385 //RN: FIXME: This is wrong - the display info array should reload after every call
386 //to GetCount() - otherwise it will not be accurate.
387 //The user can change the number of displays in display properties/settings
388 //after GetCount or similar is called and really mess this up...
389 //wxASSERT_MSG( gs_displays->GetCount() == (size_t)::GetSystemMetrics(SM_CMONITORS),
390 // _T("So how many displays does this system have?") );
392 return gs_displays
->GetCount();
396 int wxDisplayBase::GetFromPoint ( const wxPoint
& pt
)
402 return DisplayFromHMONITOR(::MonitorFromPoint(pt2
, MONITOR_DEFAULTTONULL
));
406 int wxDisplayBase::GetFromWindow(wxWindow
*window
)
408 return DisplayFromHMONITOR
410 ::MonitorFromWindow(GetHwndOf(window
), MONITOR_DEFAULTTONULL
)
414 // ----------------------------------------------------------------------------
415 // wxDisplay ctor/dtor
416 // ----------------------------------------------------------------------------
418 wxDisplay::wxDisplay ( size_t n
)
419 : wxDisplayBase ( n
)
421 // if we do this in ctor we won't have to call it from all the member
427 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[n
];
429 LPDIRECTDRAW2
& pDD2
= dpyInfo
.m_pDD2
;
432 if ( !gs_DirectDrawCreate
)
439 HRESULT hr
= (*gs_DirectDrawCreate
)(&dpyInfo
.m_guid
, &pDD
, NULL
);
441 if ( FAILED(hr
) || !pDD
)
444 wxLogApiError(_T("DirectDrawCreate"), hr
);
446 else // got IDirectDraw, we want IDirectDraw2
448 hr
= pDD
->QueryInterface(wxIID_IDirectDraw2
, (void **)&pDD2
);
449 if ( FAILED(hr
) || !pDD2
)
451 wxLogApiError(_T("IDirectDraw::QueryInterface(IDD2)"), hr
);
457 //else: DirectDraw object corresponding to our display already exists
459 // increment its ref count to account for Release() in dtor
461 // NB: pDD2 will be only really Release()d when gs_displays is
462 // destroyed which is ok as we don't want to recreate DD objects
468 wxDisplay::~wxDisplay()
470 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
472 LPDIRECTDRAW2
& pDD2
= dpyInfo
.m_pDD2
;
479 // ----------------------------------------------------------------------------
480 // wxDisplay simple accessors
481 // ----------------------------------------------------------------------------
483 bool wxDisplay::IsOk() const
485 return m_index
< GetCount() &&
486 (!gs_useDirectX
|| (*gs_displays
)[m_index
].m_pDD2
);
489 wxRect
wxDisplay::GetGeometry() const
491 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
492 wxRect
& rect
= dpyInfo
.m_rect
;
496 wxZeroMemory(monInfo
);
497 monInfo
.cbSize
= sizeof(monInfo
);
499 if ( !::GetMonitorInfo(dpyInfo
.m_hmon
, &monInfo
) )
501 wxLogLastError(_T("GetMonitorInfo"));
505 wxCopyRECTToRect(monInfo
.rcMonitor
, rect
);
512 wxString
wxDisplay::GetName() const
514 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
515 if ( dpyInfo
.m_devName
.empty() )
517 MONITORINFOEX monInfo
;
518 wxZeroMemory(monInfo
);
519 monInfo
.cbSize
= sizeof(monInfo
);
521 // NB: Cast from MONITORINFOEX* to MONITORINFO* is done because
522 // Mingw headers - unlike the ones from Microsoft's Platform SDK -
523 // don't derive the former from the latter in C++ mode and so
524 // the pointer's type is not converted implicitly.
525 if ( !::GetMonitorInfo(dpyInfo
.m_hmon
, (LPMONITORINFO
)&monInfo
) )
527 wxLogLastError(_T("GetMonitorInfo"));
531 dpyInfo
.m_devName
= monInfo
.szDevice
;
535 return dpyInfo
.m_devName
;
538 wxString
wxDisplay::GetNameForEnumSettings() const
541 const bool isWin95
= wxGetOsVersion(&major
, &minor
) == wxWIN95
&&
542 major
== 4 && minor
== 0;
544 // the first parameter of EnumDisplaySettings() must be NULL under Win95
545 // according to MSDN but GetMonitorInfo() stub in multimon.h still returns
546 // something even in this case, so we have to correct this manually
554 // ----------------------------------------------------------------------------
555 // determine if this is the primary display
556 // ----------------------------------------------------------------------------
558 bool wxDisplay::IsPrimary() const
560 wxDisplayInfo
& dpyInfo
= (*gs_displays
)[m_index
];
562 MONITORINFOEX monInfo
;
563 wxZeroMemory(monInfo
);
564 monInfo
.cbSize
= sizeof(monInfo
);
566 // NB: Cast from MONITORINFOEX* to MONITORINFO* is done because
567 // Mingw headers - unlike the ones from Microsoft's Platform SDK -
568 // don't derive the former from the latter in C++ mode and so
569 // the pointer's type is not converted implicitly.
570 if ( !::GetMonitorInfo(dpyInfo
.m_hmon
, (LPMONITORINFO
)&monInfo
) )
572 wxLogLastError(_T("GetMonitorInfo"));
575 return (monInfo
.dwFlags
& MONITORINFOF_PRIMARY
) == MONITORINFOF_PRIMARY
;
578 // ----------------------------------------------------------------------------
579 // video modes enumeration
580 // ----------------------------------------------------------------------------
583 wxDisplay::DoGetModesDirectX(const wxVideoMode
& WXUNUSED(modeMatch
)) const
585 wxArrayVideoModes modes
;
587 IDirectDraw2
*pDD
= (*gs_displays
)[m_index
].m_pDD2
;
591 HRESULT hr
= pDD
->EnumDisplayModes
594 NULL
, // all modes (TODO: use modeMatch!)
595 &modes
, // callback parameter
596 wxDDEnumModesCallback
601 wxLogApiError(_T("IDirectDraw::EnumDisplayModes"), hr
);
609 wxDisplay::DoGetModesWindows(const wxVideoMode
& modeMatch
) const
611 wxArrayVideoModes modes
;
613 const wxString name
= GetNameForEnumSettings();
615 const wxChar
* const deviceName
= name
.empty() ? NULL
: name
.c_str();
618 for ( int iModeNum
= 0;
619 ::EnumDisplaySettings(deviceName
, iModeNum
, &dm
);
622 const wxVideoMode mode
= ConvertToVideoMode(dm
);
623 if ( mode
.Matches(modeMatch
) )
632 wxArrayVideoModes
wxDisplay::GetModes(const wxVideoMode
& modeMatch
) const
634 return gs_useDirectX
? DoGetModesDirectX(modeMatch
)
635 : DoGetModesWindows(modeMatch
);
638 wxVideoMode
wxDisplay::GetCurrentMode() const
642 const wxString name
= GetNameForEnumSettings();
645 if ( !::EnumDisplaySettings(name
.empty() ? NULL
: name
.c_str(),
646 ENUM_CURRENT_SETTINGS
,
649 wxLogLastError(_T("EnumDisplaySettings(ENUM_CURRENT_SETTINGS)"));
653 mode
= ConvertToVideoMode(dm
);
659 // ----------------------------------------------------------------------------
660 // video mode switching
661 // ----------------------------------------------------------------------------
663 bool wxDisplay::DoChangeModeDirectX(const wxVideoMode
& mode
)
665 IDirectDraw2
*pDD
= (*gs_displays
)[m_index
].m_pDD2
;
669 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
670 wxCHECK_MSG( winTop
, false, _T("top level window required for DirectX") );
672 HRESULT hr
= pDD
->SetCooperativeLevel
675 DDSCL_EXCLUSIVE
| DDSCL_FULLSCREEN
679 wxLogApiError(_T("IDirectDraw::SetCooperativeLevel"), hr
);
684 hr
= pDD
->SetDisplayMode(mode
.w
, mode
.h
, mode
.bpp
, mode
.refresh
, 0);
687 wxLogApiError(_T("IDirectDraw::SetDisplayMode"), hr
);
696 bool wxDisplay::DoChangeModeWindows(const wxVideoMode
& mode
)
698 // prepare ChangeDisplaySettingsEx() parameters
703 if ( mode
== wxDefaultVideoMode
)
705 // reset the video mode to default
709 else // change to the given mode
711 wxCHECK_MSG( mode
.w
&& mode
.h
, false,
712 _T("at least the width and height must be specified") );
715 dm
.dmSize
= sizeof(dm
);
716 dm
.dmFields
= DM_PELSWIDTH
| DM_PELSHEIGHT
;
717 dm
.dmPelsWidth
= mode
.w
;
718 dm
.dmPelsHeight
= mode
.h
;
722 dm
.dmFields
|= DM_BITSPERPEL
;
723 dm
.dmBitsPerPel
= mode
.bpp
;
728 dm
.dmFields
|= DM_DISPLAYFREQUENCY
;
729 dm
.dmDisplayFrequency
= mode
.refresh
;
736 #else // !__WXWINCE__
737 flags
= CDS_FULLSCREEN
;
738 #endif // __WXWINCE__/!__WXWINCE__
742 // get pointer to the function dynamically
744 // we're only called from the main thread, so it's ok to use static
746 static ChangeDisplaySettingsEx_t pfnChangeDisplaySettingsEx
= NULL
;
747 if ( !pfnChangeDisplaySettingsEx
)
749 wxDynamicLibrary
dllUser32(_T("user32.dll"));
750 if ( dllUser32
.IsLoaded() )
752 pfnChangeDisplaySettingsEx
= (ChangeDisplaySettingsEx_t
)
753 dllUser32
.GetSymbol(WINFUNC(ChangeDisplaySettingsEx
));
755 //else: huh, no user32.dll??
758 if ( !pfnChangeDisplaySettingsEx
)
760 // we must be under Win95 and so there is no multiple monitors
762 pfnChangeDisplaySettingsEx
= ChangeDisplaySettingsExForWin95
;
764 #endif // !__WXWINCE__
767 // do change the mode
768 switch ( pfnChangeDisplaySettingsEx
770 GetName(), // display name
771 pDevMode
, // dev mode or NULL to reset
774 NULL
// pointer to video parameters (not used)
777 case DISP_CHANGE_SUCCESSFUL
:
780 // If we have a top-level, full-screen frame, emulate
781 // the DirectX behavior and resize it. This makes this
782 // API quite a bit easier to use.
783 wxWindow
*winTop
= wxTheApp
->GetTopWindow();
784 wxFrame
*frameTop
= wxDynamicCast(winTop
, wxFrame
);
785 if (frameTop
&& frameTop
->IsFullScreen())
787 wxVideoMode current
= GetCurrentMode();
788 frameTop
->SetClientSize(current
.w
, current
.h
);
793 case DISP_CHANGE_BADMODE
:
794 // don't complain about this, this is the only "expected" error
798 wxFAIL_MSG( _T("unexpected ChangeDisplaySettingsEx() return value") );
804 bool wxDisplay::ChangeMode(const wxVideoMode
& mode
)
806 return gs_useDirectX
? DoChangeModeDirectX(mode
)
807 : DoChangeModeWindows(mode
);
810 #endif // wxUSE_DISPLAY