2 * Copyright (C) 2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
24 #include "DisplaySettings.h"
25 #include "dialogs/GUIDialogYesNo.h"
26 #include "guilib/GraphicContext.h"
27 #include "guilib/gui3d.h"
28 #include "guilib/LocalizeStrings.h"
29 #include "guilib/StereoscopicsManager.h"
30 #include "settings/AdvancedSettings.h"
31 #include "settings/lib/Setting.h"
32 #include "settings/Settings.h"
33 #include "threads/SingleLock.h"
34 #include "utils/log.h"
35 #include "utils/StringUtils.h"
36 #include "utils/XMLUtils.h"
37 #include "windowing/WindowingFactory.h"
39 // 0.1 second increments
40 #define MAX_REFRESH_CHANGE_DELAY 200
44 static RESOLUTION_INFO EmptyResolution;
45 static RESOLUTION_INFO EmptyModifiableResolution;
47 float square_error(float x, float y)
49 float yonx = (x > 0) ? y / x : 0;
50 float xony = (y > 0) ? x / y : 0;
51 return std::max(yonx, xony);
54 static CStdString ModeFlagsToString(unsigned int flags, bool identifier)
57 if(flags & D3DPRESENTFLAG_INTERLACED)
65 if(flags & D3DPRESENTFLAG_MODE3DSBS)
67 else if(flags & D3DPRESENTFLAG_MODE3DTB)
74 CDisplaySettings::CDisplaySettings()
76 m_resolutions.insert(m_resolutions.begin(), RES_CUSTOM, RESOLUTION_INFO());
80 m_verticalShift = 0.0f;
81 m_nonLinearStretched = false;
82 m_resolutionChangeAborted = false;
85 CDisplaySettings::~CDisplaySettings()
88 CDisplaySettings& CDisplaySettings::Get()
90 static CDisplaySettings sDisplaySettings;
91 return sDisplaySettings;
94 bool CDisplaySettings::Load(const TiXmlNode *settings)
96 CSingleLock lock(m_critical);
97 m_calibrations.clear();
102 const TiXmlElement *pElement = settings->FirstChildElement("resolutions");
105 CLog::Log(LOGERROR, "CDisplaySettings: settings file doesn't contain <resolutions>");
109 const TiXmlElement *pResolution = pElement->FirstChildElement("resolution");
112 // get the data for this calibration
115 XMLUtils::GetString(pResolution, "description", cal.strMode);
116 XMLUtils::GetInt(pResolution, "subtitles", cal.iSubtitles);
117 XMLUtils::GetFloat(pResolution, "pixelratio", cal.fPixelRatio);
119 XMLUtils::GetFloat(pResolution, "refreshrate", cal.fRefreshRate);
120 XMLUtils::GetString(pResolution, "output", cal.strOutput);
121 XMLUtils::GetString(pResolution, "xrandrid", cal.strId);
124 const TiXmlElement *pOverscan = pResolution->FirstChildElement("overscan");
127 XMLUtils::GetInt(pOverscan, "left", cal.Overscan.left);
128 XMLUtils::GetInt(pOverscan, "top", cal.Overscan.top);
129 XMLUtils::GetInt(pOverscan, "right", cal.Overscan.right);
130 XMLUtils::GetInt(pOverscan, "bottom", cal.Overscan.bottom);
133 // mark calibration as not updated
134 // we must not delete those, resolution just might not be available
135 cal.iWidth = cal.iHeight = 0;
137 // store calibration, avoid adding duplicates
139 for (ResolutionInfos::const_iterator it = m_calibrations.begin(); it != m_calibrations.end(); ++it)
141 if (it->strMode.Equals(cal.strMode))
148 m_calibrations.push_back(cal);
151 pResolution = pResolution->NextSiblingElement("resolution");
158 bool CDisplaySettings::Save(TiXmlNode *settings) const
160 if (settings == NULL)
163 CSingleLock lock(m_critical);
164 TiXmlElement xmlRootElement("resolutions");
165 TiXmlNode *pRoot = settings->InsertEndChild(xmlRootElement);
170 for (ResolutionInfos::const_iterator it = m_calibrations.begin(); it != m_calibrations.end(); ++it)
172 // Write the resolution tag
173 TiXmlElement resElement("resolution");
174 TiXmlNode *pNode = pRoot->InsertEndChild(resElement);
178 // Now write each of the pieces of information we need...
179 XMLUtils::SetString(pNode, "description", it->strMode);
180 XMLUtils::SetInt(pNode, "subtitles", it->iSubtitles);
181 XMLUtils::SetFloat(pNode, "pixelratio", it->fPixelRatio);
183 XMLUtils::SetFloat(pNode, "refreshrate", it->fRefreshRate);
184 XMLUtils::SetString(pNode, "output", it->strOutput);
185 XMLUtils::SetString(pNode, "xrandrid", it->strId);
188 // create the overscan child
189 TiXmlElement overscanElement("overscan");
190 TiXmlNode *pOverscanNode = pNode->InsertEndChild(overscanElement);
191 if (pOverscanNode == NULL)
194 XMLUtils::SetInt(pOverscanNode, "left", it->Overscan.left);
195 XMLUtils::SetInt(pOverscanNode, "top", it->Overscan.top);
196 XMLUtils::SetInt(pOverscanNode, "right", it->Overscan.right);
197 XMLUtils::SetInt(pOverscanNode, "bottom", it->Overscan.bottom);
203 void CDisplaySettings::Clear()
205 CSingleLock lock(m_critical);
206 m_calibrations.clear();
207 m_resolutions.clear();
211 m_verticalShift = 0.0f;
212 m_nonLinearStretched = false;
215 bool CDisplaySettings::OnSettingChanging(const CSetting *setting)
220 const std::string &settingId = setting->GetId();
221 if (settingId == "videoscreen.resolution" ||
222 settingId == "videoscreen.screen")
224 RESOLUTION newRes = RES_DESKTOP;
225 if (settingId == "videoscreen.resolution")
226 newRes = (RESOLUTION)((CSettingInt*)setting)->GetValue();
227 else if (settingId == "videoscreen.screen")
228 newRes = GetResolutionForScreen();
230 string screenmode = GetStringFromResolution(newRes);
231 CSettings::Get().SetString("videoscreen.screenmode", screenmode);
233 if (settingId == "videoscreen.screenmode")
235 RESOLUTION oldRes = GetCurrentResolution();
236 RESOLUTION newRes = GetResolutionFromString(((CSettingString*)setting)->GetValue());
238 SetCurrentResolution(newRes, false);
239 g_graphicsContext.SetVideoResolution(newRes);
241 // check if the old or the new resolution was/is windowed
242 // in which case we don't show any prompt to the user
243 if (oldRes != RES_WINDOW && newRes != RES_WINDOW)
245 if (!m_resolutionChangeAborted)
247 bool cancelled = false;
248 if (!CGUIDialogYesNo::ShowAndGetInput(13110, 13111, 20022, 20022, -1, -1, cancelled, 10000))
250 m_resolutionChangeAborted = true;
255 m_resolutionChangeAborted = false;
262 bool CDisplaySettings::OnSettingUpdate(CSetting* &setting, const char *oldSettingId, const TiXmlNode *oldSettingNode)
267 const std::string &settingId = setting->GetId();
268 if (settingId == "videoscreen.screenmode")
270 CSettingString *screenmodeSetting = (CSettingString*)setting;
271 std::string screenmode = screenmodeSetting->GetValue();
272 // in Eden there was no character ("i" or "p") indicating interlaced/progressive
273 // at the end so we just add a "p" and assume progressive
274 // no 3d mode existed before, so just assume std modes
275 if (screenmode.size() == 20)
276 return screenmodeSetting->SetValue(screenmode + "pstd");
277 if (screenmode.size() == 21)
278 return screenmodeSetting->SetValue(screenmode + "std");
284 void CDisplaySettings::SetCurrentResolution(RESOLUTION resolution, bool save /* = false */)
288 string mode = GetStringFromResolution(resolution);
289 CSettings::Get().SetString("videoscreen.screenmode", mode.c_str());
292 m_currentResolution = resolution;
297 RESOLUTION CDisplaySettings::GetDisplayResolution() const
299 return GetResolutionFromString(CSettings::Get().GetString("videoscreen.screenmode"));
302 const RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(size_t index) const
304 CSingleLock lock(m_critical);
305 if (index >= m_resolutions.size())
306 return EmptyResolution;
308 return m_resolutions[index];
311 const RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(RESOLUTION resolution) const
313 if (resolution <= RES_INVALID)
314 return EmptyResolution;
316 return GetResolutionInfo((size_t)resolution);
319 RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(size_t index)
321 CSingleLock lock(m_critical);
322 if (index >= m_resolutions.size())
324 EmptyModifiableResolution = RESOLUTION_INFO();
325 return EmptyModifiableResolution;
328 return m_resolutions[index];
331 RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(RESOLUTION resolution)
333 if (resolution <= RES_INVALID)
335 EmptyModifiableResolution = RESOLUTION_INFO();
336 return EmptyModifiableResolution;
339 return GetResolutionInfo((size_t)resolution);
342 void CDisplaySettings::AddResolutionInfo(const RESOLUTION_INFO &resolution)
344 CSingleLock lock(m_critical);
345 RESOLUTION_INFO res(resolution);
347 if((res.dwFlags & D3DPRESENTFLAG_MODE3DTB) == 0)
349 /* add corrections for some special case modes frame packing modes */
351 if(res.iScreenWidth == 1920
352 && res.iScreenHeight == 2205)
355 res.dwFlags |= D3DPRESENTFLAG_MODE3DTB;
358 if(res.iScreenWidth == 1280
359 && res.iScreenHeight == 1470)
362 res.dwFlags |= D3DPRESENTFLAG_MODE3DTB;
365 m_resolutions.push_back(res);
368 void CDisplaySettings::ApplyCalibrations()
370 CSingleLock lock(m_critical);
371 // apply all calibrations to the resolutions
372 for (ResolutionInfos::const_iterator itCal = m_calibrations.begin(); itCal != m_calibrations.end(); ++itCal)
375 for (size_t res = 0; res < m_resolutions.size(); ++res)
377 if (res == RES_WINDOW)
379 if (itCal->strMode.Equals(m_resolutions[res].strMode))
382 m_resolutions[res].Overscan.left = itCal->Overscan.left;
383 if (m_resolutions[res].Overscan.left < -m_resolutions[res].iWidth/4)
384 m_resolutions[res].Overscan.left = -m_resolutions[res].iWidth/4;
385 if (m_resolutions[res].Overscan.left > m_resolutions[res].iWidth/4)
386 m_resolutions[res].Overscan.left = m_resolutions[res].iWidth/4;
388 m_resolutions[res].Overscan.top = itCal->Overscan.top;
389 if (m_resolutions[res].Overscan.top < -m_resolutions[res].iHeight/4)
390 m_resolutions[res].Overscan.top = -m_resolutions[res].iHeight/4;
391 if (m_resolutions[res].Overscan.top > m_resolutions[res].iHeight/4)
392 m_resolutions[res].Overscan.top = m_resolutions[res].iHeight/4;
394 m_resolutions[res].Overscan.right = itCal->Overscan.right;
395 if (m_resolutions[res].Overscan.right < m_resolutions[res].iWidth / 2)
396 m_resolutions[res].Overscan.right = m_resolutions[res].iWidth / 2;
397 if (m_resolutions[res].Overscan.right > m_resolutions[res].iWidth * 3/2)
398 m_resolutions[res].Overscan.right = m_resolutions[res].iWidth *3/2;
400 m_resolutions[res].Overscan.bottom = itCal->Overscan.bottom;
401 if (m_resolutions[res].Overscan.bottom < m_resolutions[res].iHeight / 2)
402 m_resolutions[res].Overscan.bottom = m_resolutions[res].iHeight / 2;
403 if (m_resolutions[res].Overscan.bottom > m_resolutions[res].iHeight * 3/2)
404 m_resolutions[res].Overscan.bottom = m_resolutions[res].iHeight * 3/2;
406 m_resolutions[res].iSubtitles = itCal->iSubtitles;
407 if (m_resolutions[res].iSubtitles < m_resolutions[res].iHeight / 2)
408 m_resolutions[res].iSubtitles = m_resolutions[res].iHeight / 2;
409 if (m_resolutions[res].iSubtitles > m_resolutions[res].iHeight* 5/4)
410 m_resolutions[res].iSubtitles = m_resolutions[res].iHeight* 5/4;
412 m_resolutions[res].fPixelRatio = itCal->fPixelRatio;
413 if (m_resolutions[res].fPixelRatio < 0.5f)
414 m_resolutions[res].fPixelRatio = 0.5f;
415 if (m_resolutions[res].fPixelRatio > 2.0f)
416 m_resolutions[res].fPixelRatio = 2.0f;
423 void CDisplaySettings::UpdateCalibrations()
425 CSingleLock lock(m_critical);
426 for (size_t res = RES_DESKTOP; res < m_resolutions.size(); ++res)
430 for (ResolutionInfos::iterator itCal = m_calibrations.begin(); itCal != m_calibrations.end(); ++itCal)
432 if (itCal->strMode.Equals(m_resolutions[res].strMode))
434 // TODO: erase calibrations with default values
435 *itCal = m_resolutions[res];
442 m_calibrations.push_back(m_resolutions[res]);
446 DisplayMode CDisplaySettings::GetCurrentDisplayMode() const
448 if (GetCurrentResolution() == RES_WINDOW)
451 return GetCurrentResolutionInfo().iScreen;
454 RESOLUTION CDisplaySettings::FindBestMatchingResolution(const std::map<RESOLUTION, RESOLUTION_INFO> &resolutionInfos, int screen, int width, int height, float refreshrate, unsigned flags)
456 // find the closest match to these in our res vector. If we have the screen, we score the res
457 RESOLUTION bestRes = RES_DESKTOP;
458 float bestScore = FLT_MAX;
459 flags &= D3DPRESENTFLAG_MODEMASK;
461 for (std::map<RESOLUTION, RESOLUTION_INFO>::const_iterator it = resolutionInfos.begin(); it != resolutionInfos.end(); ++it)
463 const RESOLUTION_INFO &info = it->second;
465 if ( info.iScreen != screen
466 || (info.dwFlags & D3DPRESENTFLAG_MODEMASK) != flags)
469 float score = 10 * (square_error((float)info.iScreenWidth, (float)width) +
470 square_error((float)info.iScreenHeight, (float)height) +
471 square_error(info.fRefreshRate, refreshrate));
472 if (score < bestScore)
482 RESOLUTION CDisplaySettings::GetResolutionFromString(const std::string &strResolution)
484 if (strResolution == "DESKTOP")
486 else if (strResolution == "WINDOW")
488 else if (strResolution.size() >= 21)
490 // format: SWWWWWHHHHHRRR.RRRRRP333, where S = screen, W = width, H = height, R = refresh, P = interlace, 3 = stereo mode
491 int screen = strtol(StringUtils::Mid(strResolution, 0,1).c_str(), NULL, 10);
492 int width = strtol(StringUtils::Mid(strResolution, 1,5).c_str(), NULL, 10);
493 int height = strtol(StringUtils::Mid(strResolution, 6,5).c_str(), NULL, 10);
494 float refresh = (float)strtod(StringUtils::Mid(strResolution, 11,9).c_str(), NULL);
497 // look for 'i' and treat everything else as progressive,
498 if(StringUtils::Mid(strResolution, 20,1) == "i")
499 flags |= D3DPRESENTFLAG_INTERLACED;
501 if(StringUtils::Mid(strResolution, 21,3) == "sbs")
502 flags |= D3DPRESENTFLAG_MODE3DSBS;
503 else if(StringUtils::Mid(strResolution, 21,3) == "tab")
504 flags |= D3DPRESENTFLAG_MODE3DTB;
506 std::map<RESOLUTION, RESOLUTION_INFO> resolutionInfos;
507 for (size_t resolution = RES_DESKTOP; resolution < CDisplaySettings::Get().ResolutionInfoSize(); resolution++)
508 resolutionInfos.insert(make_pair((RESOLUTION)resolution, CDisplaySettings::Get().GetResolutionInfo(resolution)));
510 return FindBestMatchingResolution(resolutionInfos, screen, width, height, refresh, flags);
516 std::string CDisplaySettings::GetStringFromResolution(RESOLUTION resolution, float refreshrate /* = 0.0f */)
518 if (resolution == RES_WINDOW)
521 if (resolution >= RES_DESKTOP && resolution < (RESOLUTION)CDisplaySettings::Get().ResolutionInfoSize())
523 const RESOLUTION_INFO &info = CDisplaySettings::Get().GetResolutionInfo(resolution);
524 // also handle RES_DESKTOP resolutions with non-default refresh rates
525 if (resolution != RES_DESKTOP || (refreshrate > 0.0f && refreshrate != info.fRefreshRate))
527 return StringUtils::Format("%1i%05i%05i%09.5f%s", info.iScreen,
528 info.iScreenWidth, info.iScreenHeight,
529 refreshrate > 0.0f ? refreshrate : info.fRefreshRate, ModeFlagsToString(info.dwFlags, true).c_str());
536 RESOLUTION CDisplaySettings::GetResolutionForScreen()
538 DisplayMode mode = CSettings::Get().GetInt("videoscreen.screen");
539 if (mode == DM_WINDOWED)
542 for (int idx=0; idx < g_Windowing.GetNumScreens(); idx++)
544 if (CDisplaySettings::Get().GetResolutionInfo(RES_DESKTOP + idx).iScreen == mode)
545 return (RESOLUTION)(RES_DESKTOP + idx);
551 void CDisplaySettings::SettingOptionsRefreshChangeDelaysFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
553 list.push_back(make_pair(g_localizeStrings.Get(13551), 0));
555 for (int i = 1; i <= MAX_REFRESH_CHANGE_DELAY; i++)
556 list.push_back(make_pair(StringUtils::Format(g_localizeStrings.Get(13553).c_str(), (double)i / 10.0), i));
559 void CDisplaySettings::SettingOptionsRefreshRatesFiller(const CSetting *setting, std::vector< std::pair<std::string, std::string> > &list, std::string ¤t)
561 // get the proper resolution
562 RESOLUTION res = CDisplaySettings::Get().GetDisplayResolution();
563 if (res < RES_WINDOW)
566 // only add "Windowed" if in windowed mode
567 if (res == RES_WINDOW)
570 list.push_back(make_pair(g_localizeStrings.Get(242), current));
574 RESOLUTION_INFO resInfo = CDisplaySettings::Get().GetResolutionInfo(res);
575 // The only meaningful parts of res here are iScreen, iScreenWidth, iScreenHeight
576 vector<REFRESHRATE> refreshrates = g_Windowing.RefreshRates(resInfo.iScreen, resInfo.iScreenWidth, resInfo.iScreenHeight, resInfo.dwFlags);
579 for (vector<REFRESHRATE>::const_iterator refreshrate = refreshrates.begin(); refreshrate != refreshrates.end(); ++refreshrate)
581 std::string screenmode = GetStringFromResolution((RESOLUTION)refreshrate->ResInfo_Index, refreshrate->RefreshRate);
582 if (!match && StringUtils::EqualsNoCase(((CSettingString*)setting)->GetValue(), screenmode))
584 list.push_back(make_pair(StringUtils::Format("%.02f", refreshrate->RefreshRate), screenmode));
588 current = GetStringFromResolution(res, g_Windowing.DefaultRefreshRate(resInfo.iScreen, refreshrates).RefreshRate);
591 void CDisplaySettings::SettingOptionsResolutionsFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
593 RESOLUTION res = CDisplaySettings::Get().GetDisplayResolution();
594 RESOLUTION_INFO info = CDisplaySettings::Get().GetResolutionInfo(res);
595 if (res == RES_WINDOW)
598 list.push_back(make_pair(g_localizeStrings.Get(242), res));
602 std::map<RESOLUTION, RESOLUTION_INFO> resolutionInfos;
603 vector<RESOLUTION_WHR> resolutions = g_Windowing.ScreenResolutions(info.iScreen, info.fRefreshRate);
604 for (vector<RESOLUTION_WHR>::const_iterator resolution = resolutions.begin(); resolution != resolutions.end(); ++resolution)
606 list.push_back(make_pair(
607 StringUtils::Format("%dx%d%s", resolution->width, resolution->height,
608 ModeFlagsToString(resolution->flags, false).c_str()),
609 resolution->ResInfo_Index));
611 resolutionInfos.insert(make_pair((RESOLUTION)resolution->ResInfo_Index, CDisplaySettings::Get().GetResolutionInfo(resolution->ResInfo_Index)));
614 current = FindBestMatchingResolution(resolutionInfos, info.iScreen,
615 info.iScreenWidth, info.iScreenHeight,
616 info.fRefreshRate, info.dwFlags);
620 void CDisplaySettings::SettingOptionsScreensFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
622 if (g_advancedSettings.m_canWindowed)
623 list.push_back(make_pair(g_localizeStrings.Get(242), DM_WINDOWED));
625 for (int idx = 0; idx < g_Windowing.GetNumScreens(); idx++)
627 int screen = CDisplaySettings::Get().GetResolutionInfo(RES_DESKTOP + idx).iScreen;
628 list.push_back(make_pair(StringUtils::Format(g_localizeStrings.Get(241), screen + 1), screen));
631 RESOLUTION res = CDisplaySettings::Get().GetDisplayResolution();
632 if (res == RES_WINDOW)
633 current = DM_WINDOWED;
636 RESOLUTION_INFO resInfo = CDisplaySettings::Get().GetResolutionInfo(res);
637 current = resInfo.iScreen;
641 void CDisplaySettings::SettingOptionsVerticalSyncsFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
643 #if defined(TARGET_POSIX) && !defined(TARGET_DARWIN)
644 list.push_back(make_pair(g_localizeStrings.Get(13101), VSYNC_DRIVER));
646 list.push_back(make_pair(g_localizeStrings.Get(13106), VSYNC_DISABLED));
647 list.push_back(make_pair(g_localizeStrings.Get(13107), VSYNC_VIDEO));
648 list.push_back(make_pair(g_localizeStrings.Get(13108), VSYNC_ALWAYS));
651 void CDisplaySettings::SettingOptionsStereoscopicModesFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
653 for (int i = RENDER_STEREO_MODE_OFF; i < RENDER_STEREO_MODE_COUNT; i++)
655 RENDER_STEREO_MODE mode = (RENDER_STEREO_MODE) i;
656 if (g_Windowing.SupportsStereo(mode))
657 list.push_back(make_pair(CStereoscopicsManager::Get().GetLabelForStereoMode(mode), mode));
661 void CDisplaySettings::SettingOptionsPreferredStereoscopicViewModesFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
663 SettingOptionsStereoscopicModesFiller(setting, list, current);
664 list.push_back(make_pair(g_localizeStrings.Get(36525), RENDER_STEREO_MODE_AUTO)); // option for autodetect