[Windows] Fix driver version detection of AMD RDNA+ GPU on Windows 10
[xbmc.git] / xbmc / AutoSwitch.cpp
blob0dd710f757165678ccbde5a02c7b608ae5282dc7
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "AutoSwitch.h"
11 #include "FileItem.h"
12 #include "FileItemList.h"
13 #include "ServiceBroker.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUIWindowManager.h"
16 #include "guilib/WindowIDs.h"
17 #include "settings/Settings.h"
18 #include "settings/SettingsComponent.h"
19 #include "view/ViewState.h"
21 #define METHOD_BYFOLDERS 0
22 #define METHOD_BYFILES 1
23 #define METHOD_BYTHUMBPERCENT 2
24 #define METHOD_BYFILECOUNT 3
25 #define METHOD_BYFOLDERTHUMBS 4
27 CAutoSwitch::CAutoSwitch(void) = default;
29 CAutoSwitch::~CAutoSwitch(void) = default;
31 /// \brief Generic function to add a layer of transparency to the calling window
32 /// \param vecItems Vector of FileItems passed from the calling window
33 int CAutoSwitch::GetView(const CFileItemList &vecItems)
35 int iSortMethod = -1;
36 int iPercent = 0;
37 int iCurrentWindow = CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow();
38 bool bHideParentFolderItems = !CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_FILELISTS_SHOWPARENTDIRITEMS);
40 switch (iCurrentWindow)
42 case WINDOW_PICTURES:
44 iSortMethod = METHOD_BYFILECOUNT;
46 break;
48 case WINDOW_PROGRAMS:
50 iSortMethod = METHOD_BYTHUMBPERCENT;
51 iPercent = 50; // 50% of thumbs -> use thumbs.
53 break;
55 default:
57 if (MetadataPercentage(vecItems) > 0.25f)
58 return DEFAULT_VIEW_INFO;
59 else
60 return DEFAULT_VIEW_LIST;
62 break;
65 bool bThumbs = false;
67 switch (iSortMethod)
69 case METHOD_BYFOLDERS:
70 bThumbs = ByFolders(vecItems);
71 break;
73 case METHOD_BYFILES:
74 bThumbs = ByFiles(bHideParentFolderItems, vecItems);
75 break;
77 case METHOD_BYTHUMBPERCENT:
78 bThumbs = ByThumbPercent(bHideParentFolderItems, iPercent, vecItems);
79 break;
80 case METHOD_BYFILECOUNT:
81 bThumbs = ByFileCount(vecItems);
82 break;
83 case METHOD_BYFOLDERTHUMBS:
84 bThumbs = ByFolderThumbPercentage(bHideParentFolderItems, iPercent, vecItems);
85 break;
88 // the GUIViewControl object will default down to small icons if a big icon
89 // view is not available.
90 return bThumbs ? DEFAULT_VIEW_BIG_ICONS : DEFAULT_VIEW_LIST;
93 /// \brief Auto Switch method based on the current directory \e containing ALL folders and \e atleast one non-default thumb
94 /// \param vecItems Vector of FileItems
95 bool CAutoSwitch::ByFolders(const CFileItemList& vecItems)
97 bool bThumbs = false;
98 // is the list all folders?
99 if (vecItems.GetFolderCount() == vecItems.Size())
101 // test for thumbs
102 for (int i = 0; i < vecItems.Size(); i++)
104 const CFileItemPtr pItem = vecItems[i];
105 if (pItem->HasArt("thumb"))
107 bThumbs = true;
108 break;
112 return bThumbs;
115 /// \brief Auto Switch method based on the current directory \e not containing ALL files and \e atleast one non-default thumb
116 /// \param bHideParentDirItems - are we not counting the ".." item?
117 /// \param vecItems Vector of FileItems
118 bool CAutoSwitch::ByFiles(bool bHideParentDirItems, const CFileItemList& vecItems)
120 bool bThumbs = false;
121 int iCompare = 0;
123 // parent directories are visible, increment
124 if (!bHideParentDirItems)
126 iCompare = 1;
129 // confirm the list is not just files and folderback
130 if (vecItems.GetFolderCount() > iCompare)
132 // test for thumbs
133 for (int i = 0; i < vecItems.Size(); i++)
135 const CFileItemPtr pItem = vecItems[i];
136 if (pItem->HasArt("thumb"))
138 bThumbs = true;
139 break;
143 return bThumbs;
147 /// \brief Auto Switch method based on the percentage of non-default thumbs \e in the current directory
148 /// \param iPercent Percent of non-default thumbs to autoswitch on
149 /// \param vecItems Vector of FileItems
150 bool CAutoSwitch::ByThumbPercent(bool bHideParentDirItems, int iPercent, const CFileItemList& vecItems)
152 bool bThumbs = false;
153 int iNumThumbs = 0;
154 int iNumItems = vecItems.Size();
155 if (!bHideParentDirItems)
157 iNumItems--;
160 if (iNumItems <= 0) return false;
162 for (int i = 0; i < vecItems.Size(); i++)
164 const CFileItemPtr pItem = vecItems[i];
165 if (pItem->HasArt("thumb"))
167 iNumThumbs++;
168 float fTempPercent = ( (float)iNumThumbs / (float)iNumItems ) * (float)100;
169 if (fTempPercent >= (float)iPercent)
171 bThumbs = true;
172 break;
177 return bThumbs;
180 /// \brief Auto Switch method based on whether there is more than 25% files.
181 /// \param iPercent Percent of non-default thumbs to autoswitch on
182 bool CAutoSwitch::ByFileCount(const CFileItemList& vecItems)
184 if (vecItems.Size() == 0) return false;
185 float fPercent = (float)vecItems.GetFileCount() / vecItems.Size();
186 return (fPercent > 0.25f);
189 // returns true if:
190 // 1. Have more than 75% folders and
191 // 2. Have more than percent folders with thumbs
192 bool CAutoSwitch::ByFolderThumbPercentage(bool hideParentDirItems, int percent, const CFileItemList &vecItems)
194 int numItems = vecItems.Size();
195 if (!hideParentDirItems)
196 numItems--;
197 if (numItems <= 0) return false;
199 int fileCount = vecItems.GetFileCount();
200 if (fileCount > 0.25f * numItems) return false;
202 int numThumbs = 0;
203 for (int i = 0; i < vecItems.Size(); i++)
205 const CFileItemPtr item = vecItems[i];
206 if (item->m_bIsFolder && item->HasArt("thumb"))
208 numThumbs++;
209 if (numThumbs >= 0.01f * percent * (numItems - fileCount))
210 return true;
214 return false;
217 float CAutoSwitch::MetadataPercentage(const CFileItemList &vecItems)
219 int count = 0;
220 int total = vecItems.Size();
221 for (int i = 0; i < vecItems.Size(); i++)
223 const CFileItemPtr item = vecItems[i];
224 if(item->HasMusicInfoTag()
225 || item->HasVideoInfoTag()
226 || item->HasPictureInfoTag()
227 || item->HasProperty("Addon.ID"))
228 count++;
229 if(item->IsParentFolder())
230 total--;
232 return (total != 0) ? ((float)count / total) : 0.0f;