summaryrefslogtreecommitdiffstats
path: root/xbmc/input/WindowTranslator.cpp
blob: cceb3f06f2d11e85f252794a3785087a701b9364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "WindowTranslator.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "application/Application.h"
#include "application/ApplicationComponents.h"
#include "application/ApplicationPlayer.h"
#include "guilib/WindowIDs.h"
#include "pvr/PVRManager.h"
#include "pvr/guilib/PVRGUIActionsChannels.h"
#include "utils/StringUtils.h"
#include "utils/log.h"

#include <algorithm>
#include <cstring>
#include <stdlib.h>

const CWindowTranslator::WindowMapByName CWindowTranslator::WindowMappingByName = {
    {"home", WINDOW_HOME},
    {"programs", WINDOW_PROGRAMS},
    {"pictures", WINDOW_PICTURES},
    {"filemanager", WINDOW_FILES},
    {"settings", WINDOW_SETTINGS_MENU},
    {"music", WINDOW_MUSIC_NAV},
    {"videos", WINDOW_VIDEO_NAV},
    {"tvchannels", WINDOW_TV_CHANNELS},
    {"tvrecordings", WINDOW_TV_RECORDINGS},
    {"tvguide", WINDOW_TV_GUIDE},
    {"tvtimers", WINDOW_TV_TIMERS},
    {"tvsearch", WINDOW_TV_SEARCH},
    {"radiochannels", WINDOW_RADIO_CHANNELS},
    {"radiorecordings", WINDOW_RADIO_RECORDINGS},
    {"radioguide", WINDOW_RADIO_GUIDE},
    {"radiotimers", WINDOW_RADIO_TIMERS},
    {"radiosearch", WINDOW_RADIO_SEARCH},
    {"gamecontrollers", WINDOW_DIALOG_GAME_CONTROLLERS},
    {"gameports", WINDOW_DIALOG_GAME_PORTS},
    {"games", WINDOW_GAMES},
    {"pvrguidecontrols", WINDOW_DIALOG_PVR_GUIDE_CONTROLS},
    {"pvrguideinfo", WINDOW_DIALOG_PVR_GUIDE_INFO},
    {"pvrrecordinginfo", WINDOW_DIALOG_PVR_RECORDING_INFO},
    {"pvrradiordsinfo", WINDOW_DIALOG_PVR_RADIO_RDS_INFO},
    {"pvrtimersetting", WINDOW_DIALOG_PVR_TIMER_SETTING},
    {"pvrgroupmanager", WINDOW_DIALOG_PVR_GROUP_MANAGER},
    {"pvrchannelmanager", WINDOW_DIALOG_PVR_CHANNEL_MANAGER},
    {"pvrguidesearch", WINDOW_DIALOG_PVR_GUIDE_SEARCH},
    {"pvrchannelscan", WINDOW_DIALOG_PVR_CHANNEL_SCAN},
    {"pvrupdateprogress", WINDOW_DIALOG_PVR_UPDATE_PROGRESS},
    {"pvrosdchannels", WINDOW_DIALOG_PVR_OSD_CHANNELS},
    {"pvrchannelguide", WINDOW_DIALOG_PVR_CHANNEL_GUIDE},
    {"pvrosdguide", WINDOW_DIALOG_PVR_CHANNEL_GUIDE}, // backward compatibility to v17
    {"pvrosdteletext", WINDOW_DIALOG_OSD_TELETEXT},
    {"systeminfo", WINDOW_SYSTEM_INFORMATION},
    {"screencalibration", WINDOW_SCREEN_CALIBRATION},
    {"systemsettings", WINDOW_SETTINGS_SYSTEM},
    {"servicesettings", WINDOW_SETTINGS_SERVICE},
    {"pvrsettings", WINDOW_SETTINGS_MYPVR},
    {"playersettings", WINDOW_SETTINGS_PLAYER},
    {"mediasettings", WINDOW_SETTINGS_MEDIA},
    {"interfacesettings", WINDOW_SETTINGS_INTERFACE},
    {"appearancesettings", WINDOW_SETTINGS_INTERFACE}, // backward compatibility to v16
    {"gamesettings", WINDOW_SETTINGS_MYGAMES},
    {"videoplaylist", WINDOW_VIDEO_PLAYLIST},
    {"loginscreen", WINDOW_LOGIN_SCREEN},
    {"profiles", WINDOW_SETTINGS_PROFILES},
    {"skinsettings", WINDOW_SKIN_SETTINGS},
    {"addonbrowser", WINDOW_ADDON_BROWSER},
    {"yesnodialog", WINDOW_DIALOG_YES_NO},
    {"progressdialog", WINDOW_DIALOG_PROGRESS},
    {"virtualkeyboard", WINDOW_DIALOG_KEYBOARD},
    {"volumebar", WINDOW_DIALOG_VOLUME_BAR},
    {"submenu", WINDOW_DIALOG_SUB_MENU},
    {"favourites", WINDOW_DIALOG_FAVOURITES},
    {"contextmenu", WINDOW_DIALOG_CONTEXT_MENU},
    {"notification", WINDOW_DIALOG_KAI_TOAST},
    {"numericinput", WINDOW_DIALOG_NUMERIC},
    {"gamepadinput", WINDOW_DIALOG_GAMEPAD},
    {"shutdownmenu", WINDOW_DIALOG_BUTTON_MENU},
    {"playercontrols", WINDOW_DIALOG_PLAYER_CONTROLS},
    {"playerprocessinfo", WINDOW_DIALOG_PLAYER_PROCESS_INFO},
    {"seekbar", WINDOW_DIALOG_SEEK_BAR},
    {"musicosd", WINDOW_DIALOG_MUSIC_OSD},
    {"addonsettings", WINDOW_DIALOG_ADDON_SETTINGS},
    {"visualisationpresetlist", WINDOW_DIALOG_VIS_PRESET_LIST},
    {"osdcmssettings", WINDOW_DIALOG_CMS_OSD_SETTINGS},
    {"osdvideosettings", WINDOW_DIALOG_VIDEO_OSD_SETTINGS},
    {"osdaudiosettings", WINDOW_DIALOG_AUDIO_OSD_SETTINGS},
    {"osdsubtitlesettings", WINDOW_DIALOG_SUBTITLE_OSD_SETTINGS},
    {"videobookmarks", WINDOW_DIALOG_VIDEO_BOOKMARKS},
    {"filebrowser", WINDOW_DIALOG_FILE_BROWSER},
    {"networksetup", WINDOW_DIALOG_NETWORK_SETUP},
    {"mediasource", WINDOW_DIALOG_MEDIA_SOURCE},
    {"profilesettings", WINDOW_DIALOG_PROFILE_SETTINGS},
    {"locksettings", WINDOW_DIALOG_LOCK_SETTINGS},
    {"contentsettings", WINDOW_DIALOG_CONTENT_SETTINGS},
    {"libexportsettings", WINDOW_DIALOG_LIBEXPORT_SETTINGS},
    {"songinformation", WINDOW_DIALOG_SONG_INFO},
    {"smartplaylisteditor", WINDOW_DIALOG_SMART_PLAYLIST_EDITOR},
    {"smartplaylistrule", WINDOW_DIALOG_SMART_PLAYLIST_RULE},
    {"busydialog", WINDOW_DIALOG_BUSY},
    {"busydialognocancel", WINDOW_DIALOG_BUSY_NOCANCEL},
    {"pictureinfo", WINDOW_DIALOG_PICTURE_INFO},
    {"fullscreeninfo", WINDOW_DIALOG_FULLSCREEN_INFO},
    {"sliderdialog", WINDOW_DIALOG_SLIDER},
    {"addoninformation", WINDOW_DIALOG_ADDON_INFO},
    {"subtitlesearch", WINDOW_DIALOG_SUBTITLES},
    {"musicplaylist", WINDOW_MUSIC_PLAYLIST},
    {"musicplaylisteditor", WINDOW_MUSIC_PLAYLIST_EDITOR},
    {"infoprovidersettings", WINDOW_DIALOG_INFOPROVIDER_SETTINGS},
    {"teletext", WINDOW_DIALOG_OSD_TELETEXT},
    {"selectdialog", WINDOW_DIALOG_SELECT},
    {"musicinformation", WINDOW_DIALOG_MUSIC_INFO},
    {"okdialog", WINDOW_DIALOG_OK},
    {"movieinformation", WINDOW_DIALOG_VIDEO_INFO},
    {"textviewer", WINDOW_DIALOG_TEXT_VIEWER},
    {"fullscreenvideo", WINDOW_FULLSCREEN_VIDEO},
    {"dialogcolorpicker", WINDOW_DIALOG_COLOR_PICKER},

    // Virtual window for fullscreen radio, uses WINDOW_FULLSCREEN_VIDEO as
    // fallback
    {"fullscreenlivetv", WINDOW_FULLSCREEN_LIVETV},

    // Live TV channel preview
    {"fullscreenlivetvpreview", WINDOW_FULLSCREEN_LIVETV_PREVIEW},

    // Live TV direct channel number input
    {"fullscreenlivetvinput", WINDOW_FULLSCREEN_LIVETV_INPUT},

    // Virtual window for fullscreen radio, uses WINDOW_VISUALISATION as fallback
    {"fullscreenradio", WINDOW_FULLSCREEN_RADIO},

    // PVR Radio channel preview
    {"fullscreenradiopreview", WINDOW_FULLSCREEN_RADIO_PREVIEW},

    // PVR radio direct channel number input
    {"fullscreenradioinput", WINDOW_FULLSCREEN_RADIO_INPUT},

    {"fullscreengame", WINDOW_FULLSCREEN_GAME},
    {"visualisation", WINDOW_VISUALISATION},
    {"slideshow", WINDOW_SLIDESHOW},
    {"weather", WINDOW_WEATHER},
    {"screensaver", WINDOW_SCREENSAVER},
    {"videoosd", WINDOW_DIALOG_VIDEO_OSD},
    {"videomenu", WINDOW_VIDEO_MENU},
    {"videotimeseek", WINDOW_VIDEO_TIME_SEEK},
    {"splash", WINDOW_SPLASH},
    {"startwindow", WINDOW_START},
    {"startup", WINDOW_STARTUP_ANIM},
    {"peripheralsettings", WINDOW_DIALOG_PERIPHERAL_SETTINGS},
    {"extendedprogressdialog", WINDOW_DIALOG_EXT_PROGRESS},
    {"mediafilter", WINDOW_DIALOG_MEDIA_FILTER},
    {"addon", WINDOW_ADDON_START},
    {"eventlog", WINDOW_EVENT_LOG},
    {"favouritesbrowser", WINDOW_FAVOURITES},
    {"tvtimerrules", WINDOW_TV_TIMER_RULES},
    {"radiotimerrules", WINDOW_RADIO_TIMER_RULES},
    {"gameosd", WINDOW_DIALOG_GAME_OSD},
    {"gamevideofilter", WINDOW_DIALOG_GAME_VIDEO_FILTER},
    {"gamestretchmode", WINDOW_DIALOG_GAME_STRETCH_MODE},
    {"gamevolume", WINDOW_DIALOG_GAME_VOLUME},
    {"gameadvancedsettings", WINDOW_DIALOG_GAME_ADVANCED_SETTINGS},
    {"gamevideorotation", WINDOW_DIALOG_GAME_VIDEO_ROTATION},
    {"ingamesaves", WINDOW_DIALOG_IN_GAME_SAVES},
    {"gamesaves", WINDOW_DIALOG_GAME_SAVES}};

namespace
{
struct FallbackWindowMapping
{
  int origin;
  int target;
};

static const std::vector<FallbackWindowMapping> FallbackWindows = {
    {WINDOW_FULLSCREEN_LIVETV, WINDOW_FULLSCREEN_VIDEO},
    {WINDOW_FULLSCREEN_LIVETV_INPUT, WINDOW_FULLSCREEN_LIVETV},
    {WINDOW_FULLSCREEN_LIVETV_PREVIEW, WINDOW_FULLSCREEN_LIVETV},
    {WINDOW_FULLSCREEN_RADIO, WINDOW_VISUALISATION},
    {WINDOW_FULLSCREEN_RADIO_INPUT, WINDOW_FULLSCREEN_RADIO},
    {WINDOW_FULLSCREEN_RADIO_PREVIEW, WINDOW_FULLSCREEN_RADIO},
};
} // anonymous namespace

bool CWindowTranslator::WindowNameCompare::operator()(const WindowMapItem& lhs,
                                                      const WindowMapItem& rhs) const
{
  return std::strcmp(lhs.windowName, rhs.windowName) < 0;
}

bool CWindowTranslator::WindowIDCompare::operator()(const WindowMapItem& lhs,
                                                    const WindowMapItem& rhs) const
{
  return lhs.windowId < rhs.windowId;
}

void CWindowTranslator::GetWindows(std::vector<std::string>& windowList)
{
  windowList.clear();
  windowList.reserve(WindowMappingByName.size());
  for (auto itMapping : WindowMappingByName)
    windowList.emplace_back(itMapping.windowName);
}

int CWindowTranslator::TranslateWindow(const std::string& window)
{
  std::string strWindow(window);
  if (strWindow.empty())
    return WINDOW_INVALID;

  StringUtils::ToLower(strWindow);

  // Eliminate .xml
  if (StringUtils::EndsWith(strWindow, ".xml"))
    strWindow = strWindow.substr(0, strWindow.size() - 4);

  // window12345, for custom window to be keymapped
  if (strWindow.length() > 6 && StringUtils::StartsWith(strWindow, "window"))
    strWindow = strWindow.substr(6);

  // Drop "my" prefix
  if (StringUtils::StartsWith(strWindow, "my"))
    strWindow = strWindow.substr(2);

  if (StringUtils::IsNaturalNumber(strWindow))
  {
    // Allow a full window ID or a delta ID
    int iWindow = atoi(strWindow.c_str());
    if (iWindow > WINDOW_INVALID)
      return iWindow;

    return WINDOW_HOME + iWindow;
  }

  // Run through the window structure
  auto it = WindowMappingByName.find({strWindow.c_str(), {}});
  if (it != WindowMappingByName.end())
    return it->windowId;

  CLog::Log(LOGERROR, "Window Translator: Can't find window {}", window);

  return WINDOW_INVALID;
}

std::string CWindowTranslator::TranslateWindow(int windowId)
{
  static auto reverseWindowMapping = CreateWindowMappingByID();

  windowId = GetVirtualWindow(windowId);

  auto it = reverseWindowMapping.find(WindowMapItem{"", windowId});
  if (it != reverseWindowMapping.end())
    return it->windowName;

  return "";
}

int CWindowTranslator::GetFallbackWindow(int windowId)
{
  auto it = std::find_if(
      FallbackWindows.begin(), FallbackWindows.end(),
      [windowId](const FallbackWindowMapping& mapping) { return mapping.origin == windowId; });

  if (it != FallbackWindows.end())
    return it->target;

  // For add-on windows use WINDOW_ADDON_START because ID is dynamic
  if (WINDOW_ADDON_START < windowId && windowId <= WINDOW_ADDON_END)
    return WINDOW_ADDON_START;

  return -1;
}

CWindowTranslator::WindowMapByID CWindowTranslator::CreateWindowMappingByID()
{
  WindowMapByID reverseWindowMapping;

  reverseWindowMapping.insert(WindowMappingByName.begin(), WindowMappingByName.end());

  return reverseWindowMapping;
}

int CWindowTranslator::GetVirtualWindow(int windowId)
{
  if (windowId == WINDOW_FULLSCREEN_VIDEO)
  {
    if (g_application.CurrentFileItem().HasPVRChannelInfoTag())
    {
      // special casing for Live TV
      if (CServiceBroker::GetPVRManager()
              .Get<PVR::GUI::Channels>()
              .GetChannelNumberInputHandler()
              .HasChannelNumber())
        return WINDOW_FULLSCREEN_LIVETV_INPUT;
      else if (CServiceBroker::GetPVRManager()
                   .Get<PVR::GUI::Channels>()
                   .GetChannelNavigator()
                   .IsPreview())
        return WINDOW_FULLSCREEN_LIVETV_PREVIEW;
      else
        return WINDOW_FULLSCREEN_LIVETV;
    }
    else
    {
      const auto& components = CServiceBroker::GetAppComponents();
      const auto appPlayer = components.GetComponent<CApplicationPlayer>();

      // check if we're in a DVD menu
      if (appPlayer->IsInMenu())
        return WINDOW_VIDEO_MENU;
      // special casing for numeric seek
      else if (appPlayer->GetSeekHandler().HasTimeCode())
        return WINDOW_VIDEO_TIME_SEEK;
    }
  }
  else if (windowId == WINDOW_VISUALISATION)
  {
    if (g_application.CurrentFileItem().HasPVRChannelInfoTag())
    {
      // special casing for PVR radio
      if (CServiceBroker::GetPVRManager()
              .Get<PVR::GUI::Channels>()
              .GetChannelNumberInputHandler()
              .HasChannelNumber())
        return WINDOW_FULLSCREEN_RADIO_INPUT;
      else if (CServiceBroker::GetPVRManager()
                   .Get<PVR::GUI::Channels>()
                   .GetChannelNavigator()
                   .IsPreview())
        return WINDOW_FULLSCREEN_RADIO_PREVIEW;
      else
        return WINDOW_FULLSCREEN_RADIO;
    }
    else
    {
      const auto& components = CServiceBroker::GetAppComponents();
      const auto appPlayer = components.GetComponent<CApplicationPlayer>();

      // special casing for numeric seek
      if (appPlayer->GetSeekHandler().HasTimeCode())
        return WINDOW_VIDEO_TIME_SEEK;
    }
  }

  return windowId;
}