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 "settings/AdvancedSettings.h"
30 #include "settings/Setting.h"
31 #include "settings/Settings.h"
32 #include "threads/SingleLock.h"
33 #include "utils/log.h"
34 #include "utils/StringUtils.h"
35 #include "utils/XMLUtils.h"
36 #include "windowing/WindowingFactory.h"
38 // 0.1 second increments
39 #define MAX_REFRESH_CHANGE_DELAY 200
43 static RESOLUTION_INFO EmptyResolution;
44 static RESOLUTION_INFO EmptyModifiableResolution;
46 float square_error(float x, float y)
48 float yonx = (x > 0) ? y / x : 0;
49 float xony = (y > 0) ? x / y : 0;
50 return std::max(yonx, xony);
53 CDisplaySettings::CDisplaySettings()
55 m_resolutions.insert(m_resolutions.begin(), RES_CUSTOM, RESOLUTION_INFO());
59 m_verticalShift = 0.0f;
60 m_nonLinearStretched = false;
61 m_resolutionChangeAborted = false;
64 CDisplaySettings::~CDisplaySettings()
67 CDisplaySettings& CDisplaySettings::Get()
69 static CDisplaySettings sDisplaySettings;
70 return sDisplaySettings;
73 bool CDisplaySettings::Load(const TiXmlNode *settings)
75 CSingleLock lock(m_critical);
76 m_calibrations.clear();
81 const TiXmlElement *pElement = settings->FirstChildElement("resolutions");
84 CLog::Log(LOGERROR, "CDisplaySettings: settings file doesn't contain <resolutions>");
88 const TiXmlElement *pResolution = pElement->FirstChildElement("resolution");
91 // get the data for this calibration
94 XMLUtils::GetString(pResolution, "description", cal.strMode);
95 XMLUtils::GetInt(pResolution, "subtitles", cal.iSubtitles);
96 XMLUtils::GetFloat(pResolution, "pixelratio", cal.fPixelRatio);
98 XMLUtils::GetFloat(pResolution, "refreshrate", cal.fRefreshRate);
99 XMLUtils::GetString(pResolution, "output", cal.strOutput);
100 XMLUtils::GetString(pResolution, "xrandrid", cal.strId);
103 const TiXmlElement *pOverscan = pResolution->FirstChildElement("overscan");
106 XMLUtils::GetInt(pOverscan, "left", cal.Overscan.left);
107 XMLUtils::GetInt(pOverscan, "top", cal.Overscan.top);
108 XMLUtils::GetInt(pOverscan, "right", cal.Overscan.right);
109 XMLUtils::GetInt(pOverscan, "bottom", cal.Overscan.bottom);
112 // mark calibration as not updated
113 // we must not delete those, resolution just might not be available
114 cal.iWidth = cal.iHeight = 0;
116 // store calibration, avoid adding duplicates
118 for (ResolutionInfos::const_iterator it = m_calibrations.begin(); it != m_calibrations.end(); ++it)
120 if (it->strMode.Equals(cal.strMode))
127 m_calibrations.push_back(cal);
130 pResolution = pResolution->NextSiblingElement("resolution");
137 bool CDisplaySettings::Save(TiXmlNode *settings) const
139 if (settings == NULL)
142 CSingleLock lock(m_critical);
143 TiXmlElement xmlRootElement("resolutions");
144 TiXmlNode *pRoot = settings->InsertEndChild(xmlRootElement);
149 for (ResolutionInfos::const_iterator it = m_calibrations.begin(); it != m_calibrations.end(); ++it)
151 // Write the resolution tag
152 TiXmlElement resElement("resolution");
153 TiXmlNode *pNode = pRoot->InsertEndChild(resElement);
157 // Now write each of the pieces of information we need...
158 XMLUtils::SetString(pNode, "description", it->strMode);
159 XMLUtils::SetInt(pNode, "subtitles", it->iSubtitles);
160 XMLUtils::SetFloat(pNode, "pixelratio", it->fPixelRatio);
162 XMLUtils::SetFloat(pNode, "refreshrate", it->fRefreshRate);
163 XMLUtils::SetString(pNode, "output", it->strOutput);
164 XMLUtils::SetString(pNode, "xrandrid", it->strId);
167 // create the overscan child
168 TiXmlElement overscanElement("overscan");
169 TiXmlNode *pOverscanNode = pNode->InsertEndChild(overscanElement);
170 if (pOverscanNode == NULL)
173 XMLUtils::SetInt(pOverscanNode, "left", it->Overscan.left);
174 XMLUtils::SetInt(pOverscanNode, "top", it->Overscan.top);
175 XMLUtils::SetInt(pOverscanNode, "right", it->Overscan.right);
176 XMLUtils::SetInt(pOverscanNode, "bottom", it->Overscan.bottom);
182 void CDisplaySettings::Clear()
184 CSingleLock lock(m_critical);
185 m_calibrations.clear();
186 m_resolutions.clear();
190 m_verticalShift = 0.0f;
191 m_nonLinearStretched = false;
194 bool CDisplaySettings::OnSettingChanging(const CSetting *setting)
199 const std::string &settingId = setting->GetId();
200 if (settingId == "videoscreen.resolution" ||
201 settingId == "videoscreen.screen")
203 RESOLUTION newRes = RES_DESKTOP;
204 if (settingId == "videoscreen.resolution")
205 newRes = (RESOLUTION)((CSettingInt*)setting)->GetValue();
206 else if (settingId == "videoscreen.screen")
207 newRes = GetResolutionForScreen();
209 string screenmode = GetStringFromResolution(newRes);
210 CSettings::Get().SetString("videoscreen.screenmode", screenmode);
212 if (settingId == "videoscreen.screenmode")
214 RESOLUTION oldRes = GetCurrentResolution();
215 RESOLUTION newRes = GetResolutionFromString(((CSettingString*)setting)->GetValue());
217 SetCurrentResolution(newRes, false);
218 g_graphicsContext.SetVideoResolution(newRes);
220 // check if the old or the new resolution was/is windowed
221 // in which case we don't show any prompt to the user
222 if (oldRes != RES_WINDOW && newRes != RES_WINDOW)
224 if (!m_resolutionChangeAborted)
226 bool cancelled = false;
227 if (!CGUIDialogYesNo::ShowAndGetInput(13110, 13111, 20022, 20022, -1, -1, cancelled, 10000))
229 m_resolutionChangeAborted = true;
234 m_resolutionChangeAborted = false;
241 bool CDisplaySettings::OnSettingUpdate(CSetting* &setting, const char *oldSettingId, const TiXmlNode *oldSettingNode)
246 const std::string &settingId = setting->GetId();
247 if (settingId == "videoscreen.screenmode")
249 CSettingString *screenmodeSetting = (CSettingString*)setting;
250 std::string screenmode = screenmodeSetting->GetValue();
251 // in Eden there was no character ("i" or "p") indicating interlaced/progressive
252 // at the end so we just add a "p" and assume progressive
253 if (screenmode.size() == 20)
254 return screenmodeSetting->SetValue(screenmode + "p");
260 void CDisplaySettings::SetCurrentResolution(RESOLUTION resolution, bool save /* = false */)
264 string mode = GetStringFromResolution(resolution);
265 CSettings::Get().SetString("videoscreen.screenmode", mode.c_str());
268 m_currentResolution = resolution;
273 RESOLUTION CDisplaySettings::GetDisplayResolution() const
275 return GetResolutionFromString(CSettings::Get().GetString("videoscreen.screenmode"));
278 const RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(size_t index) const
280 CSingleLock lock(m_critical);
281 if (index >= m_resolutions.size())
282 return EmptyResolution;
284 return m_resolutions[index];
287 const RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(RESOLUTION resolution) const
289 if (resolution <= RES_INVALID)
290 return EmptyResolution;
292 return GetResolutionInfo((size_t)resolution);
295 RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(size_t index)
297 CSingleLock lock(m_critical);
298 if (index >= m_resolutions.size())
300 EmptyModifiableResolution = RESOLUTION_INFO();
301 return EmptyModifiableResolution;
304 return m_resolutions[index];
307 RESOLUTION_INFO& CDisplaySettings::GetResolutionInfo(RESOLUTION resolution)
309 if (resolution <= RES_INVALID)
311 EmptyModifiableResolution = RESOLUTION_INFO();
312 return EmptyModifiableResolution;
315 return GetResolutionInfo((size_t)resolution);
318 void CDisplaySettings::AddResolutionInfo(const RESOLUTION_INFO &resolution)
320 CSingleLock lock(m_critical);
321 m_resolutions.push_back(resolution);
324 void CDisplaySettings::ApplyCalibrations()
326 CSingleLock lock(m_critical);
327 // apply all calibrations to the resolutions
328 for (ResolutionInfos::const_iterator itCal = m_calibrations.begin(); itCal != m_calibrations.end(); ++itCal)
331 for (size_t res = 0; res < m_resolutions.size(); ++res)
333 if (res == RES_WINDOW)
335 if (itCal->strMode.Equals(m_resolutions[res].strMode))
338 m_resolutions[res].Overscan.left = itCal->Overscan.left;
339 if (m_resolutions[res].Overscan.left < -m_resolutions[res].iWidth/4)
340 m_resolutions[res].Overscan.left = -m_resolutions[res].iWidth/4;
341 if (m_resolutions[res].Overscan.left > m_resolutions[res].iWidth/4)
342 m_resolutions[res].Overscan.left = m_resolutions[res].iWidth/4;
344 m_resolutions[res].Overscan.top = itCal->Overscan.top;
345 if (m_resolutions[res].Overscan.top < -m_resolutions[res].iHeight/4)
346 m_resolutions[res].Overscan.top = -m_resolutions[res].iHeight/4;
347 if (m_resolutions[res].Overscan.top > m_resolutions[res].iHeight/4)
348 m_resolutions[res].Overscan.top = m_resolutions[res].iHeight/4;
350 m_resolutions[res].Overscan.right = itCal->Overscan.right;
351 if (m_resolutions[res].Overscan.right < m_resolutions[res].iWidth / 2)
352 m_resolutions[res].Overscan.right = m_resolutions[res].iWidth / 2;
353 if (m_resolutions[res].Overscan.right > m_resolutions[res].iWidth * 3/2)
354 m_resolutions[res].Overscan.right = m_resolutions[res].iWidth *3/2;
356 m_resolutions[res].Overscan.bottom = itCal->Overscan.bottom;
357 if (m_resolutions[res].Overscan.bottom < m_resolutions[res].iHeight / 2)
358 m_resolutions[res].Overscan.bottom = m_resolutions[res].iHeight / 2;
359 if (m_resolutions[res].Overscan.bottom > m_resolutions[res].iHeight * 3/2)
360 m_resolutions[res].Overscan.bottom = m_resolutions[res].iHeight * 3/2;
362 m_resolutions[res].iSubtitles = itCal->iSubtitles;
363 if (m_resolutions[res].iSubtitles < m_resolutions[res].iHeight / 2)
364 m_resolutions[res].iSubtitles = m_resolutions[res].iHeight / 2;
365 if (m_resolutions[res].iSubtitles > m_resolutions[res].iHeight* 5/4)
366 m_resolutions[res].iSubtitles = m_resolutions[res].iHeight* 5/4;
368 m_resolutions[res].fPixelRatio = itCal->fPixelRatio;
369 if (m_resolutions[res].fPixelRatio < 0.5f)
370 m_resolutions[res].fPixelRatio = 0.5f;
371 if (m_resolutions[res].fPixelRatio > 2.0f)
372 m_resolutions[res].fPixelRatio = 2.0f;
379 void CDisplaySettings::UpdateCalibrations()
381 CSingleLock lock(m_critical);
382 for (size_t res = RES_DESKTOP; res < m_resolutions.size(); ++res)
386 for (ResolutionInfos::iterator itCal = m_calibrations.begin(); itCal != m_calibrations.end(); ++itCal)
388 if (itCal->strMode.Equals(m_resolutions[res].strMode))
390 // TODO: erase calibrations with default values
391 *itCal = m_resolutions[res];
398 m_calibrations.push_back(m_resolutions[res]);
402 DisplayMode CDisplaySettings::GetCurrentDisplayMode() const
404 if (GetCurrentResolution() == RES_WINDOW)
407 return GetCurrentResolutionInfo().iScreen;
410 RESOLUTION CDisplaySettings::FindBestMatchingResolution(const std::map<RESOLUTION, RESOLUTION_INFO> &resolutionInfos, int screen, int width, int height, float refreshrate, bool interlaced)
412 int interlace = interlaced ? 100 : 200;
414 // find the closest match to these in our res vector. If we have the screen, we score the res
415 RESOLUTION bestRes = RES_DESKTOP;
416 float bestScore = FLT_MAX;
418 for (std::map<RESOLUTION, RESOLUTION_INFO>::const_iterator it = resolutionInfos.begin(); it != resolutionInfos.end(); ++it)
420 const RESOLUTION_INFO &info = it->second;
421 if (info.iScreen != screen)
423 float score = 10 * (square_error((float)info.iScreenWidth, (float)width) +
424 square_error((float)info.iScreenHeight, (float)height) +
425 square_error(info.fRefreshRate, refreshrate) +
426 square_error((float)((info.dwFlags & D3DPRESENTFLAG_INTERLACED) ? 100 : 200), (float)interlace));
427 if (score < bestScore)
437 RESOLUTION CDisplaySettings::GetResolutionFromString(const std::string &strResolution)
439 if (strResolution == "DESKTOP")
441 else if (strResolution == "WINDOW")
443 else if (strResolution.size() == 21)
445 // format: SWWWWWHHHHHRRR.RRRRRP, where S = screen, W = width, H = height, R = refresh, P = interlace
446 int screen = strtol(StringUtils::Mid(strResolution, 0,1).c_str(), NULL, 10);
447 int width = strtol(StringUtils::Mid(strResolution, 1,5).c_str(), NULL, 10);
448 int height = strtol(StringUtils::Mid(strResolution, 6,5).c_str(), NULL, 10);
449 float refresh = (float)strtod(StringUtils::Mid(strResolution, 11,9).c_str(), NULL);
450 // look for 'i' and treat everything else as progressive,
451 // and use 100/200 to get a nice square_error.
452 bool interlaced = StringUtils::EndsWith(strResolution, "i");
454 std::map<RESOLUTION, RESOLUTION_INFO> resolutionInfos;
455 for (size_t resolution = RES_DESKTOP; resolution < CDisplaySettings::Get().ResolutionInfoSize(); resolution++)
456 resolutionInfos.insert(make_pair((RESOLUTION)resolution, CDisplaySettings::Get().GetResolutionInfo(resolution)));
458 return FindBestMatchingResolution(resolutionInfos, screen, width, height, refresh, interlaced);
464 std::string CDisplaySettings::GetStringFromResolution(RESOLUTION resolution, float refreshrate /* = 0.0f */)
466 if (resolution == RES_WINDOW)
469 if (resolution >= RES_DESKTOP && resolution < (RESOLUTION)CDisplaySettings::Get().ResolutionInfoSize())
471 const RESOLUTION_INFO &info = CDisplaySettings::Get().GetResolutionInfo(resolution);
472 // also handle RES_DESKTOP resolutions with non-default refresh rates
473 if (resolution != RES_DESKTOP || (refreshrate > 0.0f && refreshrate != info.fRefreshRate))
475 return StringUtils::Format("%1i%05i%05i%09.5f%s", info.iScreen,
476 info.iScreenWidth, info.iScreenHeight,
477 refreshrate > 0.0f ? refreshrate : info.fRefreshRate,
478 (info.dwFlags & D3DPRESENTFLAG_INTERLACED) ? "i":"p");
485 RESOLUTION CDisplaySettings::GetResolutionForScreen()
487 DisplayMode mode = CSettings::Get().GetInt("videoscreen.screen");
488 if (mode == DM_WINDOWED)
491 for (int idx=0; idx < g_Windowing.GetNumScreens(); idx++)
493 if (CDisplaySettings::Get().GetResolutionInfo(RES_DESKTOP + idx).iScreen == mode)
494 return (RESOLUTION)(RES_DESKTOP + idx);
500 void CDisplaySettings::SettingOptionsRefreshChangeDelaysFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
502 list.push_back(make_pair(g_localizeStrings.Get(13551), 0));
504 for (int i = 1; i <= MAX_REFRESH_CHANGE_DELAY; i++)
505 list.push_back(make_pair(StringUtils::Format(g_localizeStrings.Get(13553).c_str(), (double)i / 10.0), i));
508 void CDisplaySettings::SettingOptionsRefreshRatesFiller(const CSetting *setting, std::vector< std::pair<std::string, std::string> > &list, std::string ¤t)
510 // get the proper resolution
511 RESOLUTION res = CDisplaySettings::Get().GetDisplayResolution();
512 if (res < RES_WINDOW)
515 // only add "Windowed" if in windowed mode
516 if (res == RES_WINDOW)
519 list.push_back(make_pair(g_localizeStrings.Get(242), current));
523 RESOLUTION_INFO resInfo = CDisplaySettings::Get().GetResolutionInfo(res);
524 // The only meaningful parts of res here are iScreen, iScreenWidth, iScreenHeight
525 vector<REFRESHRATE> refreshrates = g_Windowing.RefreshRates(resInfo.iScreen, resInfo.iScreenWidth, resInfo.iScreenHeight, resInfo.dwFlags);
528 for (vector<REFRESHRATE>::const_iterator refreshrate = refreshrates.begin(); refreshrate != refreshrates.end(); ++refreshrate)
530 std::string screenmode = GetStringFromResolution((RESOLUTION)refreshrate->ResInfo_Index, refreshrate->RefreshRate);
531 if (!match && StringUtils::EqualsNoCase(((CSettingString*)setting)->GetValue(), screenmode))
533 list.push_back(make_pair(StringUtils::Format("%.02f", refreshrate->RefreshRate), screenmode));
537 current = GetStringFromResolution(res, g_Windowing.DefaultRefreshRate(resInfo.iScreen, refreshrates).RefreshRate);
540 void CDisplaySettings::SettingOptionsResolutionsFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
542 RESOLUTION res = CDisplaySettings::Get().GetDisplayResolution();
543 RESOLUTION_INFO info = CDisplaySettings::Get().GetResolutionInfo(res);
544 if (res == RES_WINDOW)
547 list.push_back(make_pair(g_localizeStrings.Get(242), res));
551 std::map<RESOLUTION, RESOLUTION_INFO> resolutionInfos;
552 vector<RESOLUTION_WHR> resolutions = g_Windowing.ScreenResolutions(info.iScreen, info.fRefreshRate);
553 for (vector<RESOLUTION_WHR>::const_iterator resolution = resolutions.begin(); resolution != resolutions.end(); ++resolution)
555 list.push_back(make_pair(
556 StringUtils::Format("%dx%d%s", resolution->width, resolution->height,
557 (resolution->interlaced == D3DPRESENTFLAG_INTERLACED) ? "i" : "p"),
558 resolution->ResInfo_Index));
560 resolutionInfos.insert(make_pair((RESOLUTION)resolution->ResInfo_Index, CDisplaySettings::Get().GetResolutionInfo(resolution->ResInfo_Index)));
563 current = FindBestMatchingResolution(resolutionInfos, info.iScreen,
564 info.iScreenWidth, info.iScreenHeight,
565 info.fRefreshRate, info.dwFlags & D3DPRESENTFLAG_INTERLACED);
569 void CDisplaySettings::SettingOptionsScreensFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
571 if (g_advancedSettings.m_canWindowed)
572 list.push_back(make_pair(g_localizeStrings.Get(242), DM_WINDOWED));
574 for (int idx = 0; idx < g_Windowing.GetNumScreens(); idx++)
576 int screen = CDisplaySettings::Get().GetResolutionInfo(RES_DESKTOP + idx).iScreen;
577 list.push_back(make_pair(StringUtils::Format(g_localizeStrings.Get(241), screen + 1), screen));
580 RESOLUTION res = CDisplaySettings::Get().GetDisplayResolution();
581 if (res == RES_WINDOW)
582 current = DM_WINDOWED;
585 RESOLUTION_INFO resInfo = CDisplaySettings::Get().GetResolutionInfo(res);
586 current = resInfo.iScreen;
590 void CDisplaySettings::SettingOptionsVerticalSyncsFiller(const CSetting *setting, std::vector< std::pair<std::string, int> > &list, int ¤t)
592 #if defined(TARGET_POSIX) && !defined(TARGET_DARWIN)
593 list.push_back(make_pair(g_localizeStrings.Get(13101), VSYNC_DRIVER));
595 list.push_back(make_pair(g_localizeStrings.Get(13106), VSYNC_DISABLED));
596 list.push_back(make_pair(g_localizeStrings.Get(13107), VSYNC_VIDEO));
597 list.push_back(make_pair(g_localizeStrings.Get(13108), VSYNC_ALWAYS));