Debian Codegraph @

xbmc/input/WindowTranslator.cpp

raw
1 /*
2 * Copyright (C) 2017-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "WindowTranslator.h"
10
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "application/Application.h"
14 #include "application/ApplicationComponents.h"
15 #include "application/ApplicationPlayer.h"
16 #include "guilib/WindowIDs.h"
17 #include "pvr/PVRManager.h"
18 #include "pvr/guilib/PVRGUIActionsChannels.h"
19 #include "utils/StringUtils.h"
20 #include "utils/log.h"
21
22 #include <algorithm>
23 #include <cstring>
24 #include <stdlib.h>
25
26 const CWindowTranslator::WindowMapByName CWindowTranslator::WindowMappingByName = {
27 {"home", WINDOW_HOME},
28 {"programs", WINDOW_PROGRAMS},
29 {"pictures", WINDOW_PICTURES},
30 {"filemanager", WINDOW_FILES},
31 {"settings", WINDOW_SETTINGS_MENU},
32 {"music", WINDOW_MUSIC_NAV},
33 {"videos", WINDOW_VIDEO_NAV},
34 {"tvchannels", WINDOW_TV_CHANNELS},
35 {"tvrecordings", WINDOW_TV_RECORDINGS},
36 {"tvguide", WINDOW_TV_GUIDE},
37 {"tvtimers", WINDOW_TV_TIMERS},
38 {"tvsearch", WINDOW_TV_SEARCH},
39 {"radiochannels", WINDOW_RADIO_CHANNELS},
40 {"radiorecordings", WINDOW_RADIO_RECORDINGS},
41 {"radioguide", WINDOW_RADIO_GUIDE},
42 {"radiotimers", WINDOW_RADIO_TIMERS},
43 {"radiosearch", WINDOW_RADIO_SEARCH},
44 {"gamecontrollers", WINDOW_DIALOG_GAME_CONTROLLERS},
45 {"gameports", WINDOW_DIALOG_GAME_PORTS},
46 {"games", WINDOW_GAMES},
47 {"pvrguidecontrols", WINDOW_DIALOG_PVR_GUIDE_CONTROLS},
48 {"pvrguideinfo", WINDOW_DIALOG_PVR_GUIDE_INFO},
49 {"pvrrecordinginfo", WINDOW_DIALOG_PVR_RECORDING_INFO},
50 {"pvrradiordsinfo", WINDOW_DIALOG_PVR_RADIO_RDS_INFO},
51 {"pvrtimersetting", WINDOW_DIALOG_PVR_TIMER_SETTING},
52 {"pvrgroupmanager", WINDOW_DIALOG_PVR_GROUP_MANAGER},
53 {"pvrchannelmanager", WINDOW_DIALOG_PVR_CHANNEL_MANAGER},
54 {"pvrguidesearch", WINDOW_DIALOG_PVR_GUIDE_SEARCH},
55 {"pvrchannelscan", WINDOW_DIALOG_PVR_CHANNEL_SCAN},
56 {"pvrupdateprogress", WINDOW_DIALOG_PVR_UPDATE_PROGRESS},
57 {"pvrosdchannels", WINDOW_DIALOG_PVR_OSD_CHANNELS},
58 {"pvrchannelguide", WINDOW_DIALOG_PVR_CHANNEL_GUIDE},
59 {"pvrosdguide", WINDOW_DIALOG_PVR_CHANNEL_GUIDE}, // backward compatibility to v17
60 {"pvrosdteletext", WINDOW_DIALOG_OSD_TELETEXT},
61 {"systeminfo", WINDOW_SYSTEM_INFORMATION},
62 {"screencalibration", WINDOW_SCREEN_CALIBRATION},
63 {"systemsettings", WINDOW_SETTINGS_SYSTEM},
64 {"servicesettings", WINDOW_SETTINGS_SERVICE},
65 {"pvrsettings", WINDOW_SETTINGS_MYPVR},
66 {"playersettings", WINDOW_SETTINGS_PLAYER},
67 {"mediasettings", WINDOW_SETTINGS_MEDIA},
68 {"interfacesettings", WINDOW_SETTINGS_INTERFACE},
69 {"appearancesettings", WINDOW_SETTINGS_INTERFACE}, // backward compatibility to v16
70 {"gamesettings", WINDOW_SETTINGS_MYGAMES},
71 {"videoplaylist", WINDOW_VIDEO_PLAYLIST},
72 {"loginscreen", WINDOW_LOGIN_SCREEN},
73 {"profiles", WINDOW_SETTINGS_PROFILES},
74 {"skinsettings", WINDOW_SKIN_SETTINGS},
75 {"addonbrowser", WINDOW_ADDON_BROWSER},
76 {"yesnodialog", WINDOW_DIALOG_YES_NO},
77 {"progressdialog", WINDOW_DIALOG_PROGRESS},
78 {"virtualkeyboard", WINDOW_DIALOG_KEYBOARD},
79 {"volumebar", WINDOW_DIALOG_VOLUME_BAR},
80 {"submenu", WINDOW_DIALOG_SUB_MENU},
81 {"contextmenu", WINDOW_DIALOG_CONTEXT_MENU},
82 {"notification", WINDOW_DIALOG_KAI_TOAST},
83 {"numericinput", WINDOW_DIALOG_NUMERIC},
84 {"gamepadinput", WINDOW_DIALOG_GAMEPAD},
85 {"shutdownmenu", WINDOW_DIALOG_BUTTON_MENU},
86 {"playercontrols", WINDOW_DIALOG_PLAYER_CONTROLS},
87 {"playerprocessinfo", WINDOW_DIALOG_PLAYER_PROCESS_INFO},
88 {"seekbar", WINDOW_DIALOG_SEEK_BAR},
89 {"musicosd", WINDOW_DIALOG_MUSIC_OSD},
90 {"addonsettings", WINDOW_DIALOG_ADDON_SETTINGS},
91 {"visualisationpresetlist", WINDOW_DIALOG_VIS_PRESET_LIST},
92 {"osdcmssettings", WINDOW_DIALOG_CMS_OSD_SETTINGS},
93 {"osdvideosettings", WINDOW_DIALOG_VIDEO_OSD_SETTINGS},
94 {"osdaudiosettings", WINDOW_DIALOG_AUDIO_OSD_SETTINGS},
95 {"osdsubtitlesettings", WINDOW_DIALOG_SUBTITLE_OSD_SETTINGS},
96 {"videobookmarks", WINDOW_DIALOG_VIDEO_BOOKMARKS},
97 {"filebrowser", WINDOW_DIALOG_FILE_BROWSER},
98 {"networksetup", WINDOW_DIALOG_NETWORK_SETUP},
99 {"mediasource", WINDOW_DIALOG_MEDIA_SOURCE},
100 {"profilesettings", WINDOW_DIALOG_PROFILE_SETTINGS},
101 {"locksettings", WINDOW_DIALOG_LOCK_SETTINGS},
102 {"contentsettings", WINDOW_DIALOG_CONTENT_SETTINGS},
103 {"libexportsettings", WINDOW_DIALOG_LIBEXPORT_SETTINGS},
104 {"songinformation", WINDOW_DIALOG_SONG_INFO},
105 {"smartplaylisteditor", WINDOW_DIALOG_SMART_PLAYLIST_EDITOR},
106 {"smartplaylistrule", WINDOW_DIALOG_SMART_PLAYLIST_RULE},
107 {"busydialog", WINDOW_DIALOG_BUSY},
108 {"busydialognocancel", WINDOW_DIALOG_BUSY_NOCANCEL},
109 {"pictureinfo", WINDOW_DIALOG_PICTURE_INFO},
110 {"fullscreeninfo", WINDOW_DIALOG_FULLSCREEN_INFO},
111 {"sliderdialog", WINDOW_DIALOG_SLIDER},
112 {"addoninformation", WINDOW_DIALOG_ADDON_INFO},
113 {"subtitlesearch", WINDOW_DIALOG_SUBTITLES},
114 {"musicplaylist", WINDOW_MUSIC_PLAYLIST},
115 {"musicplaylisteditor", WINDOW_MUSIC_PLAYLIST_EDITOR},
116 {"infoprovidersettings", WINDOW_DIALOG_INFOPROVIDER_SETTINGS},
117 {"teletext", WINDOW_DIALOG_OSD_TELETEXT},
118 {"selectdialog", WINDOW_DIALOG_SELECT},
119 {"musicinformation", WINDOW_DIALOG_MUSIC_INFO},
120 {"okdialog", WINDOW_DIALOG_OK},
121 {"movieinformation", WINDOW_DIALOG_VIDEO_INFO},
122 {"managevideoversions", WINDOW_DIALOG_MANAGE_VIDEO_VERSIONS},
123 {"managevideoextras", WINDOW_DIALOG_MANAGE_VIDEO_EXTRAS},
124 {"selectvideoversion", WINDOW_DIALOG_SELECT_VIDEO_VERSION},
125 {"selectvideoextra", WINDOW_DIALOG_SELECT_VIDEO_EXTRA},
126 {"textviewer", WINDOW_DIALOG_TEXT_VIEWER},
127 {"fullscreenvideo", WINDOW_FULLSCREEN_VIDEO},
128 {"dialogcolorpicker", WINDOW_DIALOG_COLOR_PICKER},
129
130 // Virtual window for fullscreen radio, uses WINDOW_FULLSCREEN_VIDEO as
131 // fallback
132 {"fullscreenlivetv", WINDOW_FULLSCREEN_LIVETV},
133
134 // Live TV channel preview
135 {"fullscreenlivetvpreview", WINDOW_FULLSCREEN_LIVETV_PREVIEW},
136
137 // Live TV direct channel number input
138 {"fullscreenlivetvinput", WINDOW_FULLSCREEN_LIVETV_INPUT},
139
140 // Virtual window for fullscreen radio, uses WINDOW_VISUALISATION as fallback
141 {"fullscreenradio", WINDOW_FULLSCREEN_RADIO},
142
143 // PVR Radio channel preview
144 {"fullscreenradiopreview", WINDOW_FULLSCREEN_RADIO_PREVIEW},
145
146 // PVR radio direct channel number input
147 {"fullscreenradioinput", WINDOW_FULLSCREEN_RADIO_INPUT},
148
149 {"fullscreengame", WINDOW_FULLSCREEN_GAME},
150 {"visualisation", WINDOW_VISUALISATION},
151 {"slideshow", WINDOW_SLIDESHOW},
152 {"weather", WINDOW_WEATHER},
153 {"screensaver", WINDOW_SCREENSAVER},
154 {"videoosd", WINDOW_DIALOG_VIDEO_OSD},
155 {"videomenu", WINDOW_VIDEO_MENU},
156 {"videotimeseek", WINDOW_VIDEO_TIME_SEEK},
157 {"splash", WINDOW_SPLASH},
158 {"startwindow", WINDOW_START},
159 {"startup", WINDOW_STARTUP_ANIM},
160 {"peripherals", WINDOW_DIALOG_PERIPHERALS},
161 {"peripheralsettings", WINDOW_DIALOG_PERIPHERAL_SETTINGS},
162 {"extendedprogressdialog", WINDOW_DIALOG_EXT_PROGRESS},
163 {"mediafilter", WINDOW_DIALOG_MEDIA_FILTER},
164 {"addon", WINDOW_ADDON_START},
165 {"eventlog", WINDOW_EVENT_LOG},
166 {"favouritesbrowser", WINDOW_FAVOURITES},
167 {"tvtimerrules", WINDOW_TV_TIMER_RULES},
168 {"radiotimerrules", WINDOW_RADIO_TIMER_RULES},
169 {"gameosd", WINDOW_DIALOG_GAME_OSD},
170 {"gamevideofilter", WINDOW_DIALOG_GAME_VIDEO_FILTER},
171 {"gamestretchmode", WINDOW_DIALOG_GAME_STRETCH_MODE},
172 {"gamevolume", WINDOW_DIALOG_GAME_VOLUME},
173 {"gameadvancedsettings", WINDOW_DIALOG_GAME_ADVANCED_SETTINGS},
174 {"gamevideorotation", WINDOW_DIALOG_GAME_VIDEO_ROTATION},
175 {"ingamesaves", WINDOW_DIALOG_IN_GAME_SAVES},
176 {"gamesaves", WINDOW_DIALOG_GAME_SAVES},
177 {"gameagents", WINDOW_DIALOG_GAME_AGENTS},
178 };
179
180 namespace
181 {
182 struct FallbackWindowMapping
183 {
184 int origin;
185 int target;
186 };
187
188 static const std::vector<FallbackWindowMapping> FallbackWindows = {
189 {WINDOW_FULLSCREEN_LIVETV, WINDOW_FULLSCREEN_VIDEO},
190 {WINDOW_FULLSCREEN_LIVETV_INPUT, WINDOW_FULLSCREEN_LIVETV},
191 {WINDOW_FULLSCREEN_LIVETV_PREVIEW, WINDOW_FULLSCREEN_LIVETV},
192 {WINDOW_FULLSCREEN_RADIO, WINDOW_VISUALISATION},
193 {WINDOW_FULLSCREEN_RADIO_INPUT, WINDOW_FULLSCREEN_RADIO},
194 {WINDOW_FULLSCREEN_RADIO_PREVIEW, WINDOW_FULLSCREEN_RADIO},
195 };
196 } // anonymous namespace
197
198 bool CWindowTranslator::WindowNameCompare::operator()(const WindowMapItem& lhs,
199 const WindowMapItem& rhs) const
200 {
201 return std::strcmp(lhs.windowName, rhs.windowName) < 0;
202 }
203
204 bool CWindowTranslator::WindowIDCompare::operator()(const WindowMapItem& lhs,
205 const WindowMapItem& rhs) const
206 {
207 return lhs.windowId < rhs.windowId;
208 }
209
210 void CWindowTranslator::GetWindows(std::vector<std::string>& windowList)
211 {
212 windowList.clear();
213 windowList.reserve(WindowMappingByName.size());
214 for (auto itMapping : WindowMappingByName)
215 windowList.emplace_back(itMapping.windowName);
216 }
217
218 int CWindowTranslator::TranslateWindow(const std::string& window)
219 {
220 std::string strWindow(window);
221 if (strWindow.empty())
222 return WINDOW_INVALID;
223
224 StringUtils::ToLower(strWindow);
225
226 // Eliminate .xml
227 if (StringUtils::EndsWith(strWindow, ".xml"))
228 strWindow.resize(strWindow.size() - 4);
229
230 // window12345, for custom window to be keymapped
231 if (strWindow.length() > 6 && StringUtils::StartsWith(strWindow, "window"))
232 strWindow = strWindow.substr(6);
233
234 // Drop "my" prefix
235 if (StringUtils::StartsWith(strWindow, "my"))
236 strWindow = strWindow.substr(2);
237
238 if (StringUtils::IsNaturalNumber(strWindow))
239 {
240 // Allow a full window ID or a delta ID
241 int iWindow = atoi(strWindow.c_str());
242 if (iWindow > WINDOW_INVALID)
243 return iWindow;
244
245 return WINDOW_HOME + iWindow;
246 }
247
248 // Run through the window structure
249 auto it = WindowMappingByName.find({strWindow.c_str(), {}});
250 if (it != WindowMappingByName.end())
251 return it->windowId;
252
253 CLog::Log(LOGERROR, "Window Translator: Can't find window {}", window);
254
255 return WINDOW_INVALID;
256 }
257
258 std::string CWindowTranslator::TranslateWindow(int windowId)
259 {
260 static auto reverseWindowMapping = CreateWindowMappingByID();
261
262 windowId = GetVirtualWindow(windowId);
263
264 auto it = reverseWindowMapping.find(WindowMapItem{"", windowId});
265 if (it != reverseWindowMapping.end())
266 return it->windowName;
267
268 return "";
269 }
270
271 int CWindowTranslator::GetFallbackWindow(int windowId)
272 {
273 auto it = std::find_if(FallbackWindows.begin(), FallbackWindows.end(),
274 [windowId](const FallbackWindowMapping& mapping)
275 { return mapping.origin == windowId; });
276
277 if (it != FallbackWindows.end())
278 return it->target;
279
280 // For add-on windows use WINDOW_ADDON_START because ID is dynamic
281 if (WINDOW_ADDON_START < windowId && windowId <= WINDOW_ADDON_END)
282 return WINDOW_ADDON_START;
283
284 return -1;
285 }
286
287 CWindowTranslator::WindowMapByID CWindowTranslator::CreateWindowMappingByID()
288 {
289 WindowMapByID reverseWindowMapping;
290
291 reverseWindowMapping.insert(WindowMappingByName.begin(), WindowMappingByName.end());
292
293 return reverseWindowMapping;
294 }
295
296 int CWindowTranslator::GetVirtualWindow(int windowId)
297 {
298 if (windowId == WINDOW_FULLSCREEN_VIDEO)
299 {
300 if (g_application.CurrentFileItem().HasPVRChannelInfoTag())
301 {
302 // special casing for Live TV
303 if (CServiceBroker::GetPVRManager()
304 .Get<PVR::GUI::Channels>()
305 .GetChannelNumberInputHandler()
306 .HasChannelNumber())
307 return WINDOW_FULLSCREEN_LIVETV_INPUT;
308 else if (CServiceBroker::GetPVRManager()
309 .Get<PVR::GUI::Channels>()
310 .GetChannelNavigator()
311 .IsPreview())
312 return WINDOW_FULLSCREEN_LIVETV_PREVIEW;
313 else
314 return WINDOW_FULLSCREEN_LIVETV;
315 }
316 else
317 {
318 const auto& components = CServiceBroker::GetAppComponents();
319 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
320
321 // check if we're in a DVD menu
322 if (appPlayer->IsInMenu())
323 return WINDOW_VIDEO_MENU;
324 // special casing for numeric seek
325 else if (appPlayer->GetSeekHandler().HasTimeCode())
326 return WINDOW_VIDEO_TIME_SEEK;
327 }
328 }
329 else if (windowId == WINDOW_VISUALISATION)
330 {
331 if (g_application.CurrentFileItem().HasPVRChannelInfoTag())
332 {
333 // special casing for PVR radio
334 if (CServiceBroker::GetPVRManager()
335 .Get<PVR::GUI::Channels>()
336 .GetChannelNumberInputHandler()
337 .HasChannelNumber())
338 return WINDOW_FULLSCREEN_RADIO_INPUT;
339 else if (CServiceBroker::GetPVRManager()
340 .Get<PVR::GUI::Channels>()
341 .GetChannelNavigator()
342 .IsPreview())
343 return WINDOW_FULLSCREEN_RADIO_PREVIEW;
344 else
345 return WINDOW_FULLSCREEN_RADIO;
346 }
347 else
348 {
349 const auto& components = CServiceBroker::GetAppComponents();
350 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
351
352 // special casing for numeric seek
353 if (appPlayer->GetSeekHandler().HasTimeCode())
354 return WINDOW_VIDEO_TIME_SEEK;
355 }
356 }
357
358 return windowId;
359 }