]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/display.cpp
wxTinderbox build fix.
[wxWidgets.git] / src / mac / carbon / display.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: display.cpp
3 // Purpose: Mac implementation of wxDisplay class
4 // Author: Ryan Norton & Brian Victor
5 // Modified by: Royce Mitchell III
6 // Created: 06/21/02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_DISPLAY
19
20 #ifndef WX_PRECOMP
21 #include "wx/dynarray.h"
22 #include "wx/log.h"
23 #include "wx/msgdlg.h"
24 #endif
25
26 #ifdef __DARWIN__
27 #include <Carbon/Carbon.h>
28 #else
29 #include <Gestalt.h>
30 #include <Displays.h>
31 #include <Quickdraw.h>
32 #include <Video.h> // for VDSwitchInfoRec
33 #include <FixMath.h>
34 #include <Debugging.h>
35 #endif
36
37 #include "wx/display.h"
38 #include "wx/gdicmn.h"
39 #include "wx/string.h"
40
41 // ----------------------------------------------------------------------------
42 // private classes
43 // ----------------------------------------------------------------------------
44
45 #ifdef __WXMAC_OSX__
46
47 class wxDisplayMacPriv
48 {
49 public:
50 CGDirectDisplayID m_id;
51 };
52
53 size_t wxDisplayBase::GetCount()
54 {
55 CGDisplayCount count;
56 CGDisplayErr err = CGGetActiveDisplayList(0, NULL, &count);
57
58 wxASSERT(err == CGDisplayNoErr);
59
60 return count;
61 }
62
63 int wxDisplayBase::GetFromPoint(const wxPoint &p)
64 {
65 CGPoint thePoint = {(float)p.x, (float)p.y};
66 CGDirectDisplayID theID;
67 CGDisplayCount theCount;
68 CGDisplayErr err = CGGetDisplaysWithPoint(thePoint, 1, &theID, &theCount);
69 wxASSERT(err == CGDisplayNoErr);
70
71 int nWhich = -1;
72
73 if (theCount)
74 {
75 theCount = GetCount();
76 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
77 err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
78 wxASSERT(err == CGDisplayNoErr);
79
80 for (nWhich = 0; nWhich < (int) theCount; ++nWhich)
81 {
82 if (theIDs[nWhich] == theID)
83 break;
84 }
85
86 delete [] theIDs;
87
88 if (nWhich == (int) theCount)
89 {
90 wxFAIL_MSG(wxT("Failed to find display in display list"));
91 nWhich = -1;
92 }
93 }
94
95 return nWhich;
96 }
97
98 wxDisplay::wxDisplay(size_t index)
99 : wxDisplayBase( index ) ,
100 m_priv( new wxDisplayMacPriv() )
101 {
102 CGDisplayCount theCount = GetCount();
103 CGDirectDisplayID* theIDs = new CGDirectDisplayID[theCount];
104
105 CGDisplayErr err = CGGetActiveDisplayList(theCount, theIDs, &theCount);
106
107 wxASSERT( err == CGDisplayNoErr );
108 wxASSERT( index < theCount );
109
110 m_priv->m_id = theIDs[index];
111
112 delete [] theIDs;
113 }
114
115 wxRect wxDisplay::GetGeometry() const
116 {
117 CGRect theRect = CGDisplayBounds(m_priv->m_id);
118 return wxRect( (int)theRect.origin.x,
119 (int)theRect.origin.y,
120 (int)theRect.size.width,
121 (int)theRect.size.height ); //floats
122 }
123
124 int wxDisplay::GetDepth() const
125 {
126 return (int) CGDisplayBitsPerPixel( m_priv->m_id ); //size_t
127 }
128
129 wxString wxDisplay::GetName() const
130 {
131 // Macs don't name their displays...
132 return wxEmptyString;
133 }
134
135 static int wxCFDictKeyToInt( CFDictionaryRef desc, CFStringRef key )
136 {
137 CFNumberRef value = (CFNumberRef) CFDictionaryGetValue( desc, key );
138 if (value == NULL)
139 return 0;
140
141 int num = 0;
142 CFNumberGetValue( value, kCFNumberIntType, &num );
143
144 return num;
145 }
146
147 wxArrayVideoModes
148 wxDisplay::GetModes(const wxVideoMode& mode) const
149 {
150 wxArrayVideoModes resultModes;
151
152 CFArrayRef theArray = CGDisplayAvailableModes( m_priv->m_id );
153
154 for (CFIndex i = 0; i < CFArrayGetCount(theArray); ++i)
155 {
156 CFDictionaryRef theValue = (CFDictionaryRef) CFArrayGetValueAtIndex( theArray, i );
157
158 wxVideoMode theMode(
159 wxCFDictKeyToInt( theValue, kCGDisplayWidth ),
160 wxCFDictKeyToInt( theValue, kCGDisplayHeight ),
161 wxCFDictKeyToInt( theValue, kCGDisplayBitsPerPixel ),
162 wxCFDictKeyToInt( theValue, kCGDisplayRefreshRate ));
163
164 if (theMode.Matches( mode ))
165 resultModes.Add( theMode );
166 }
167
168 return resultModes;
169 }
170
171 wxVideoMode wxDisplay::GetCurrentMode() const
172 {
173 CFDictionaryRef theValue = CGDisplayCurrentMode( m_priv->m_id );
174
175 return wxVideoMode(
176 wxCFDictKeyToInt( theValue, kCGDisplayWidth ),
177 wxCFDictKeyToInt( theValue, kCGDisplayHeight ),
178 wxCFDictKeyToInt( theValue, kCGDisplayBitsPerPixel ),
179 wxCFDictKeyToInt( theValue, kCGDisplayRefreshRate ));
180 }
181
182 bool wxDisplay::ChangeMode( const wxVideoMode& mode )
183 {
184 // Changing to default mode (wxDefaultVideoMode) doesn't
185 // work because we don't have access to the system's 'scrn'
186 // resource which holds the user's mode which the system
187 // will return to after this app is done
188 boolean_t bExactMatch;
189 CFDictionaryRef theCGMode = CGDisplayBestModeForParametersAndRefreshRate(
190 m_priv->m_id,
191 (size_t)mode.bpp,
192 (size_t)mode.w,
193 (size_t)mode.h,
194 (double)mode.refresh,
195 &bExactMatch );
196
197 bool bOK = bExactMatch;
198
199 if (bOK)
200 bOK = CGDisplaySwitchToMode( m_priv->m_id, theCGMode ) == CGDisplayNoErr;
201
202 return bOK;
203 }
204
205 wxDisplay::~wxDisplay()
206 {
207 if ( m_priv )
208 {
209 delete m_priv;
210 m_priv = 0;
211 }
212 }
213
214 #else
215
216 class wxDisplayMacPriv
217 {
218 public:
219 GDHandle m_hndl;
220 };
221
222 size_t wxDisplayBase::GetCount()
223 {
224 GDHandle hndl;
225 size_t num = 0;
226 hndl = DMGetFirstScreenDevice(true);
227 while (hndl)
228 {
229 num++;
230 hndl = DMGetNextScreenDevice(hndl, true);
231 }
232
233 return num;
234 }
235
236 int wxDisplayBase::GetFromPoint(const wxPoint &p)
237 {
238 GDHandle hndl;
239 size_t num = 0;
240 hndl = DMGetFirstScreenDevice(true);
241
242 while (hndl)
243 {
244 Rect screenrect = (*hndl)->gdRect;
245 if (p.x >= screenrect.left &&
246 p.x <= screenrect.right &&
247 p.y >= screenrect.top &&
248 p.y <= screenrect.bottom)
249 {
250 return num;
251 }
252
253 num++;
254 hndl = DMGetNextScreenDevice(hndl, true);
255 }
256
257 return -1;
258 }
259
260 wxDisplay::wxDisplay( size_t index )
261 : wxDisplayBase( index ),
262 m_priv( new wxDisplayMacPriv() )
263 {
264 GDHandle hndl;
265 hndl = DMGetFirstScreenDevice(true);
266 m_priv->m_hndl = NULL;
267
268 while (hndl)
269 {
270 if (index == 0)
271 m_priv->m_hndl = hndl;
272
273 index--;
274 hndl = DMGetNextScreenDevice(hndl, true);
275 }
276 }
277
278 wxRect wxDisplay::GetGeometry() const
279 {
280 if ((m_priv == NULL) || (m_priv->m_hndl == NULL))
281 return wxRect(0, 0, 0, 0);
282
283 Rect screenrect = (*(m_priv->m_hndl))->gdRect;
284 return wxRect(
285 screenrect.left, screenrect.top,
286 screenrect.right - screenrect.left,
287 screenrect.bottom - screenrect.top );
288 }
289
290 int wxDisplay::GetDepth() const
291 {
292 if ((m_priv == NULL) || (m_priv->m_hndl == NULL))
293 return 0;
294
295 // This cryptic looking code is based on Apple's sample code:
296 // http://developer.apple.com/samplecode/Sample_Code/Graphics_2D/GDevVideo/Gen.cp.htm
297
298 // RN - according to the docs
299 // gdPMap is a bitmap-type representation of the GDevice, and all
300 // 0x0000FFFF does is get the lower 16 bits of pixelSize. However,
301 // since pixelSize is only 16 bits (a short)...
302 return ((*(*(m_priv->m_hndl))->gdPMap)->pixelSize) & 0x0000FFFF;
303 }
304
305 wxString wxDisplay::GetName() const
306 {
307 // Macs don't name their displays...
308 return wxEmptyString;
309 }
310
311 struct DMModeIteratorRec
312 {
313 wxArrayVideoModes* pModes;
314 const wxVideoMode* pMatchMode;
315 };
316
317 pascal void DMModeListIteratorProc(
318 void* pData,
319 DMListIndexType nIndex,
320 DMDisplayModeListEntryPtr pInfo)
321 {
322 DMModeIteratorRec* pInfoData = (DMModeIteratorRec*) pData;
323
324 // Note that in testing the refresh rate is always 0 on my ibook - RN
325 int refresh = (int) Fix2Long(pInfo->displayModeResolutionInfo->csRefreshRate);
326
327 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
328
329 for (unsigned long i = 0; i < pInfo->displayModeDepthBlockInfo->depthBlockCount; ++i)
330 {
331 if (wxVideoMode( (int) pInfo->displayModeResolutionInfo->csHorizontalPixels,
332 (int) pInfo->displayModeResolutionInfo->csVerticalLines,
333 (int) pDBI->vpPixelSize,
334 refresh).Matches(*pInfoData->pMatchMode) )
335 {
336 pInfoData->pModes->Add(
337 wxVideoMode(
338 (int) pInfo->displayModeResolutionInfo->csHorizontalPixels,
339 (int) pInfo->displayModeResolutionInfo->csVerticalLines,
340 (int) pDBI->vpPixelSize,
341 refresh ) );
342 }
343 }
344
345 #undef pDBI
346 }
347
348 struct DMModeInfoRec
349 {
350 const wxVideoMode* pMode;
351 VDSwitchInfoRec sMode;
352 bool bMatched;
353 };
354
355 pascal void DMModeInfoProc(
356 void* pData,
357 DMListIndexType nIndex,
358 DMDisplayModeListEntryPtr pInfo )
359 {
360 DMModeInfoRec* pInfoData = (DMModeInfoRec*) pData;
361 Fixed refresh = Long2Fix(pInfoData->pMode->refresh);
362
363 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
364
365 for (unsigned long i = 0; i < pInfo->displayModeDepthBlockInfo->depthBlockCount; ++i)
366 {
367 if (pInfoData->pMode->w == (int&) pInfo->displayModeResolutionInfo->csHorizontalPixels &&
368 pInfoData->pMode->h == (int&) pInfo->displayModeResolutionInfo->csVerticalLines &&
369 pInfoData->pMode->bpp == (int) pDBI->vpPixelSize &&
370 refresh == pInfo->displayModeResolutionInfo->csRefreshRate)
371 {
372 memcpy(
373 &pInfoData->sMode,
374 pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthSwitchInfo,
375 sizeof(VDSwitchInfoRec));
376 pInfoData->sMode.csMode = pDBI->vpPixelSize;
377 pInfoData->bMatched = true;
378 break;
379 }
380 }
381
382 #undef pDBI
383 }
384
385 struct DMModeTransRec
386 {
387 wxVideoMode Mode;
388 const VDSwitchInfoRec* psMode;
389 bool bMatched;
390 };
391
392 pascal void DMModeTransProc(
393 void* pData,
394 DMListIndexType nIndex,
395 DMDisplayModeListEntryPtr pInfo)
396 {
397 DMModeTransRec* pInfoData = (DMModeTransRec*) pData;
398
399 #define pDBI pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthVPBlock
400
401 for (unsigned long i = 0; i < pInfo->displayModeDepthBlockInfo->depthBlockCount; ++i)
402 {
403 if (pInfoData->psMode->csData == pInfo->displayModeDepthBlockInfo->depthVPBlock[i].depthSwitchInfo->csData)
404 {
405 pInfoData->Mode = wxVideoMode(
406 (int) pInfo->displayModeResolutionInfo->csHorizontalPixels,
407 (int) pInfo->displayModeResolutionInfo->csVerticalLines,
408 (int) pDBI->vpPixelSize,
409 (int) Fix2Long(pInfo->displayModeResolutionInfo->csRefreshRate) );
410 pInfoData->bMatched = true;
411 break;
412 }
413 }
414
415 #undef pDBI
416 }
417
418 wxArrayVideoModes
419 wxDisplay::GetModes(const wxVideoMode& mode) const
420 {
421 wxArrayVideoModes Modes;
422 unsigned long dwDMVer;
423
424 // Check DM version == 2
425 // (for backward compatibility only - 7.5.3+ use 2.0)
426 Gestalt( gestaltDisplayMgrVers, (long*) &dwDMVer );
427 if (dwDMVer >= 0x020000)
428 {
429 DMListIndexType nNumModes;
430 DMListType pModes;
431 DMDisplayModeListIteratorUPP uppMLI;
432 DisplayIDType nDisplayID;
433 OSErr err;
434
435 err = DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false);
436 verify_noerr( err );
437
438 // Create a new list...
439 err = DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes);
440 wxASSERT_MSG( err == noErr, wxT("Could not create a new display mode list") );
441
442 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeListIteratorProc);
443 wxASSERT( uppMLI );
444
445 DMModeIteratorRec sModeInfo;
446 sModeInfo.pModes = &Modes;
447 sModeInfo.pMatchMode = &mode;
448
449 for (DMListIndexType i = 0; i < nNumModes; ++i)
450 {
451 err = DMGetIndexedDisplayModeFromList(pModes, i, NULL, uppMLI, &sModeInfo);
452 verify_noerr( err );
453 }
454
455 DisposeDMDisplayModeListIteratorUPP(uppMLI);
456 err = DMDisposeList(pModes);
457 verify_noerr( err );
458 }
459 else // DM 1.0, 1.2, 1.x
460 {
461 wxLogSysError(
462 wxString::Format(
463 wxT("Display Manager Version %u Not Supported! Present? %s"),
464 (unsigned int) dwDMVer / 0x10000,
465 (dwDMVer & (1 << gestaltDisplayMgrPresent) ? wxT("Yes") : wxT("No")) ) );
466 }
467
468 return Modes;
469 }
470
471 wxVideoMode wxDisplay::GetCurrentMode() const
472 {
473 unsigned long dwDMVer;
474 wxVideoMode RetMode;
475
476 // Check DM version == 2
477 // (for backward compatibility only - 7.5.3+ use 2.0)
478 Gestalt( gestaltDisplayMgrVers, (long*) &dwDMVer );
479 if (dwDMVer >= 0x020000)
480 {
481 VDSwitchInfoRec sMode; // Note: csMode member also contains the bit depth
482 OSErr err;
483
484 err = DMGetDisplayMode( m_priv->m_hndl, &sMode );
485 if (err == noErr)
486 {
487 DMListIndexType nNumModes;
488 DMListType pModes;
489 DMDisplayModeListIteratorUPP uppMLI;
490 DisplayIDType nDisplayID;
491
492 err = DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false);
493 verify_noerr( err );
494
495 // Create a new list...
496 err = DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes);
497 wxASSERT_MSG( err == noErr, wxT("Could not create a new display mode list") );
498
499 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeTransProc);
500 wxASSERT( uppMLI );
501
502 DMModeTransRec sModeInfo;
503 sModeInfo.bMatched = false;
504 sModeInfo.psMode = &sMode;
505 for (DMListIndexType i = 0; i < nNumModes; ++i)
506 {
507 err = DMGetIndexedDisplayModeFromList(pModes, i, NULL, uppMLI, &sModeInfo);
508 verify_noerr( err );
509
510 if ( sModeInfo.bMatched )
511 {
512 RetMode = sModeInfo.Mode;
513 break;
514 }
515 }
516
517 DisposeDMDisplayModeListIteratorUPP(uppMLI);
518 err = DMDisposeList(pModes);
519 verify_noerr( err );
520 }
521 else // Can't get current mode?
522 {
523 wxLogSysError(
524 wxString::Format(
525 wxT("Couldn't obtain current display mode!!!\ndwDMVer:%u"),
526 (unsigned int) dwDMVer));
527 }
528 }
529 else // DM ver 1
530 {
531 wxLogSysError(
532 wxString::Format(
533 wxT("Display Manager Version %u Not Supported! Present? %s"),
534 (unsigned int) dwDMVer / 0x10000,
535 (dwDMVer & (1 << gestaltDisplayMgrPresent) ? wxT("Yes") : wxT("No")) ) );
536 }
537
538 return RetMode;
539 }
540
541 bool wxDisplay::ChangeMode(const wxVideoMode& mode)
542 {
543 unsigned long dwDMVer;
544
545 Gestalt( gestaltDisplayMgrVers, (long*)&dwDMVer );
546 if (GetCount() == 1 || dwDMVer >= 0x020000)
547 {
548 if (mode == wxDefaultVideoMode)
549 {
550 return true;
551
552 #if 0
553 //#ifndef __DARWIN__
554 // Handle hDisplayState;
555 // if (DMBeginConfigureDisplays(&hDisplayState) != noErr)
556 // {
557 // wxLogSysError(wxT("Could not lock display for display mode changing!"));
558 // return false;
559 // }
560 //
561 // wxASSERT( DMUseScreenPrefs(true, hDisplayState) == noErr);
562 // DMEndConfigureDisplays(hDisplayState);
563 // return true;
564 //#else
565 // hmmmmm....
566 // return true;
567 //#endif
568 #endif
569 }
570
571 //0 & NULL for params 2 & 3 of DMSetVideoMode signal it to use defaults (current mode)
572 //DM 2.0+ doesn't use params 2 & 3 of DMSetDisplayMode
573 //so we have to use this icky structure
574 VDSwitchInfoRec sMode;
575 memset( &sMode, 0, sizeof(VDSwitchInfoRec) );
576
577 DMListIndexType nNumModes;
578 DMListType pModes;
579 DMDisplayModeListIteratorUPP uppMLI;
580 DisplayIDType nDisplayID;
581 OSErr err;
582
583 err = DMGetDisplayIDByGDevice(m_priv->m_hndl, &nDisplayID, false);
584 verify_noerr( err );
585
586 // Create a new list...
587 err = DMNewDisplayModeList(nDisplayID, NULL, NULL, &nNumModes, &pModes);
588 wxASSERT_MSG(err == noErr, wxT("Could not create a new display mode list") );
589
590 uppMLI = NewDMDisplayModeListIteratorUPP(DMModeInfoProc);
591 wxASSERT(uppMLI);
592
593 DMModeInfoRec sModeInfo;
594 sModeInfo.bMatched = false;
595 sModeInfo.pMode = &mode;
596 unsigned int i;
597
598 for (i = 0; i < nNumModes; ++i)
599 {
600 err = DMGetIndexedDisplayModeFromList(pModes, i, NULL, uppMLI, &sModeInfo);
601 verify_noerr( err );
602
603 if (sModeInfo.bMatched)
604 {
605 sMode = sModeInfo.sMode;
606 break;
607 }
608 }
609
610 if (i == nNumModes)
611 return false;
612
613 DisposeDMDisplayModeListIteratorUPP(uppMLI);
614
615 err = DMDisposeList(pModes);
616 verify_noerr( err );
617
618 // For the really paranoid -
619 // unsigned long flags;
620 // Boolean bok;
621 // wxASSERT(noErr == DMCheckDisplayMode(m_priv->m_hndl, sMode.csData,
622 // sMode.csMode, &flags, NULL, &bok));
623 // wxASSERT(bok);
624
625 Handle hDisplayState;
626 if (DMBeginConfigureDisplays(&hDisplayState) != noErr)
627 {
628 wxLogSysError(wxT("Could not lock display for display mode changing!"));
629
630 return false;
631 }
632
633 unsigned long dwBPP = (unsigned long) mode.bpp;
634 err = DMSetDisplayMode(
635 m_priv->m_hndl, sMode.csData,
636 (unsigned long*) &(dwBPP),
637 NULL, //(unsigned long) &sMode
638 hDisplayState );
639
640 if (err != noErr)
641 {
642 DMEndConfigureDisplays(hDisplayState);
643 wxMessageBox( wxString::Format(wxT("Could not set the display mode")) );
644
645 return false;
646 }
647
648 DMEndConfigureDisplays(hDisplayState);
649 }
650 else // DM 1.0, 1.2, 1.x
651 {
652 wxLogSysError(
653 wxString::Format(
654 wxT("Monitor gravitation not supported yet. dwDMVer:%u"),
655 (unsigned int) dwDMVer));
656
657 return false;
658 }
659
660 return true;
661 }
662
663 wxDisplay::~wxDisplay()
664 {
665 if ( m_priv )
666 {
667 delete m_priv;
668 m_priv = 0;
669 }
670 }
671
672 #endif // !OSX
673
674 #endif // wxUSE_DISPLAY