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