Make macOS screen shape detection code more robust

This commit is contained in:
Andrew Nelless 2018-03-14 13:35:42 +00:00 committed by Steve Williams
parent 82edfe087c
commit 35fb8c3389
2 changed files with 19 additions and 14 deletions

View File

@ -109,8 +109,8 @@ protected:
virtual IKeyState* getKeyState() const; virtual IKeyState* getKeyState() const;
private: private:
void updateScreenShape(); bool updateScreenShape();
void updateScreenShape(const CGDirectDisplayID, const CGDisplayChangeSummaryFlags); bool updateScreenShape(const CGDirectDisplayID, const CGDisplayChangeSummaryFlags);
void postMouseEvent(CGPoint&) const; void postMouseEvent(CGPoint&) const;
// convenience function to send events // convenience function to send events

View File

@ -105,9 +105,12 @@ OSXScreen::OSXScreen(IEventQueue* events, bool isPrimary, bool autoShowHideCurso
m_getDropTargetThread(NULL), m_getDropTargetThread(NULL),
m_impl(NULL) m_impl(NULL)
{ {
try {
m_displayID = CGMainDisplayID(); m_displayID = CGMainDisplayID();
updateScreenShape(m_displayID, 0); if (!updateScreenShape(m_displayID, 0)) {
throw std::runtime_error ("failed to initialize screen shape");
}
try {
m_screensaver = new OSXScreenSaver(m_events, getEventTarget()); m_screensaver = new OSXScreenSaver(m_events, getEventTarget());
m_keyState = new OSXKeyState(m_events); m_keyState = new OSXKeyState(m_events);
@ -1212,9 +1215,10 @@ OSXScreen::displayReconfigurationCallback(CGDirectDisplayID displayID, CGDisplay
LOG((CLOG_DEBUG1 "event: display was reconfigured: %x %x %x", flags, mask, flags & mask)); LOG((CLOG_DEBUG1 "event: display was reconfigured: %x %x %x", flags, mask, flags & mask));
if (flags & mask) { /* Something actually did change */ if (flags & mask) { /* Something actually did change */
LOG((CLOG_DEBUG1 "event: screen changed shape; refreshing dimensions")); LOG((CLOG_DEBUG1 "event: screen changed shape; refreshing dimensions"));
screen->updateScreenShape(displayID, flags); if (!screen->updateScreenShape(displayID, flags)) {
LOG((CLOG_ERR "failed to update screen shape during display reconfiguration"));
}
} }
} }
@ -1518,35 +1522,34 @@ OSXScreen::getKeyState() const
return m_keyState; return m_keyState;
} }
void bool OSXScreen::updateScreenShape(const CGDirectDisplayID, const CGDisplayChangeSummaryFlags flags)
OSXScreen::updateScreenShape(const CGDirectDisplayID, const CGDisplayChangeSummaryFlags flags)
{ {
updateScreenShape(); return updateScreenShape();
} }
void bool
OSXScreen::updateScreenShape() OSXScreen::updateScreenShape()
{ {
// get info for each display // get info for each display
CGDisplayCount displayCount = 0; CGDisplayCount displayCount = 0;
if (CGGetActiveDisplayList(0, NULL, &displayCount) != CGDisplayNoErr) { if (CGGetActiveDisplayList(0, NULL, &displayCount) != CGDisplayNoErr) {
return; return false;
} }
if (displayCount == 0) { if (displayCount == 0) {
return; return false;
} }
CGDirectDisplayID* displays = new CGDirectDisplayID[displayCount]; CGDirectDisplayID* displays = new CGDirectDisplayID[displayCount];
if (displays == NULL) { if (displays == NULL) {
return; return false;
} }
if (CGGetActiveDisplayList(displayCount, if (CGGetActiveDisplayList(displayCount,
displays, &displayCount) != CGDisplayNoErr) { displays, &displayCount) != CGDisplayNoErr) {
delete[] displays; delete[] displays;
return; return false;
} }
// get smallest rect enclosing all display rects // get smallest rect enclosing all display rects
@ -1575,6 +1578,8 @@ OSXScreen::updateScreenShape()
LOG((CLOG_DEBUG "screen shape: center=%d,%d size=%dx%d on %u %s", LOG((CLOG_DEBUG "screen shape: center=%d,%d size=%dx%d on %u %s",
m_x, m_y, m_w, m_h, displayCount, m_x, m_y, m_w, m_h, displayCount,
(displayCount == 1) ? "display" : "displays")); (displayCount == 1) ? "display" : "displays"));
return true;
} }
#pragma mark - #pragma mark -