Merge pull request #4095 from vkosh/load-timezone
[vuplus_xbmc] / xbmc / video / PlayerController.cpp
1 /*
2  *      Copyright (C) 2012-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "PlayerController.h"
22 #include "utils/StdString.h"
23 #include "settings/AdvancedSettings.h"
24 #include "settings/DisplaySettings.h"
25 #include "settings/MediaSettings.h"
26 #include "settings/Settings.h"
27 #include "cores/IPlayer.h"
28 #include "guilib/Key.h"
29 #include "guilib/LocalizeStrings.h"
30 #include "guilib/GUISliderControl.h"
31 #include "dialogs/GUIDialogKaiToast.h"
32 #include "video/dialogs/GUIDialogAudioSubtitleSettings.h"
33 #include "video/windows/GUIWindowFullScreen.h"
34 #ifdef HAS_VIDEO_PLAYBACK
35 #include "cores/VideoRenderers/RenderManager.h"
36 #include "cores/VideoRenderers/OverlayRendererGUI.h"
37 #endif
38 #include "Application.h"
39 #include "utils/LangCodeExpander.h"
40 #include "utils/StringUtils.h"
41
42 CPlayerController::CPlayerController()
43 {
44   m_sliderAction = 0;
45 }
46
47 CPlayerController::~CPlayerController()
48 {
49 }
50
51 bool CPlayerController::OnAction(const CAction &action)
52 {
53   const unsigned int MsgTime = 300;
54   const unsigned int DisplTime = 2000;
55
56   if (g_application.m_pPlayer->IsPlayingVideo())
57   {
58     switch (action.GetID())
59     {
60       case ACTION_SHOW_SUBTITLES:
61       {
62         if (g_application.m_pPlayer->GetSubtitleCount() == 0)
63           return true;
64
65         CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn = !CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn;
66         g_application.m_pPlayer->SetSubtitleVisible(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn);
67         CStdString sub, lang;
68         if (CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn)
69         {
70           SPlayerSubtitleStreamInfo info;
71           g_application.m_pPlayer->GetSubtitleStreamInfo(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream, info);
72           if (!g_LangCodeExpander.Lookup(lang, info.language))
73             lang = g_localizeStrings.Get(13205); // Unknown
74
75           if (info.name.length() == 0)
76             sub = lang;
77           else
78             sub = StringUtils::Format("%s - %s", lang.c_str(), info.name.c_str());
79         }
80         else
81           sub = g_localizeStrings.Get(1223);
82         CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info,
83                                               g_localizeStrings.Get(287), sub, DisplTime, false, MsgTime);
84         return true;
85       }
86
87       case ACTION_NEXT_SUBTITLE:
88       {
89         if (g_application.m_pPlayer->GetSubtitleCount() == 0)
90           return true;
91
92         if(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream < 0)
93           CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream = g_application.m_pPlayer->GetSubtitle();
94
95         if (CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn)
96         {
97           CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream++;
98           if (CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream >= g_application.m_pPlayer->GetSubtitleCount())
99           {
100             CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream = 0;
101             CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn = false;
102             g_application.m_pPlayer->SetSubtitleVisible(false);
103           }
104           g_application.m_pPlayer->SetSubtitle(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream);
105         }
106         else
107         {
108           CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn = true;
109           g_application.m_pPlayer->SetSubtitleVisible(true);
110         }
111
112         CStdString sub, lang;
113         if (CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn)
114         {
115           SPlayerSubtitleStreamInfo info;
116           g_application.m_pPlayer->GetSubtitleStreamInfo(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream, info);
117           if (!g_LangCodeExpander.Lookup(lang, info.language))
118             lang = g_localizeStrings.Get(13205); // Unknown
119
120           if (info.name.length() == 0)
121             sub = lang;
122           else
123             sub = StringUtils::Format("%s - %s", lang.c_str(), info.name.c_str());
124         }
125         else
126           sub = g_localizeStrings.Get(1223);
127         CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(287), sub, DisplTime, false, MsgTime);
128         return true;
129       }
130
131       case ACTION_SUBTITLE_DELAY_MIN:
132       {
133         CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay -= 0.1f;
134         if (CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay < -g_advancedSettings.m_videoSubsDelayRange)
135           CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay = -g_advancedSettings.m_videoSubsDelayRange;
136         g_application.m_pPlayer->SetSubTitleDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay);
137
138         ShowSlider(action.GetID(), 22006, CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay,
139                                           -g_advancedSettings.m_videoSubsDelayRange, 0.1f,
140                                            g_advancedSettings.m_videoSubsDelayRange);
141         return true;
142       }
143
144       case ACTION_SUBTITLE_DELAY_PLUS:
145       {
146         CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay += 0.1f;
147         if (CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay > g_advancedSettings.m_videoSubsDelayRange)
148           CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay = g_advancedSettings.m_videoSubsDelayRange;
149         g_application.m_pPlayer->SetSubTitleDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay);
150
151         ShowSlider(action.GetID(), 22006, CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay,
152                                           -g_advancedSettings.m_videoSubsDelayRange, 0.1f,
153                                            g_advancedSettings.m_videoSubsDelayRange);
154         return true;
155       }
156
157       case ACTION_SUBTITLE_DELAY:
158       {
159         ShowSlider(action.GetID(), 22006, CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay,
160                                           -g_advancedSettings.m_videoSubsDelayRange, 0.1f,
161                                            g_advancedSettings.m_videoSubsDelayRange, true);
162         return true;
163       }
164
165       case ACTION_AUDIO_DELAY:
166       {
167         ShowSlider(action.GetID(), 297, CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay,
168                                         -g_advancedSettings.m_videoAudioDelayRange, 0.025f,
169                                          g_advancedSettings.m_videoAudioDelayRange, true);
170         return true;
171       }
172
173       case ACTION_AUDIO_DELAY_MIN:
174       {
175         CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay -= 0.025f;
176         if (CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay < -g_advancedSettings.m_videoAudioDelayRange)
177           CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay = -g_advancedSettings.m_videoAudioDelayRange;
178         g_application.m_pPlayer->SetAVDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay);
179
180         ShowSlider(action.GetID(), 297, CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay,
181                                         -g_advancedSettings.m_videoAudioDelayRange, 0.025f,
182                                          g_advancedSettings.m_videoAudioDelayRange);
183         return true;
184       }
185
186       case ACTION_AUDIO_DELAY_PLUS:
187       {
188         CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay += 0.025f;
189         if (CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay > g_advancedSettings.m_videoAudioDelayRange)
190           CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay = g_advancedSettings.m_videoAudioDelayRange;
191         g_application.m_pPlayer->SetAVDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay);
192
193         ShowSlider(action.GetID(), 297, CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay,
194                                         -g_advancedSettings.m_videoAudioDelayRange, 0.025f,
195                                          g_advancedSettings.m_videoAudioDelayRange);
196         return true;
197       }
198
199       case ACTION_AUDIO_NEXT_LANGUAGE:
200       {
201         if (g_application.m_pPlayer->GetAudioStreamCount() == 1)
202           return true;
203
204         if(CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream < 0)
205           CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream = g_application.m_pPlayer->GetAudioStream();
206
207         CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream++;
208         if (CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream >= g_application.m_pPlayer->GetAudioStreamCount())
209           CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream = 0;
210         g_application.m_pPlayer->SetAudioStream(CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream);    // Set the audio stream to the one selected
211         CStdString aud;
212         CStdString lan;
213         SPlayerAudioStreamInfo info;
214         g_application.m_pPlayer->GetAudioStreamInfo(CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream, info);
215         if (!g_LangCodeExpander.Lookup(lan, info.language))
216           lan = g_localizeStrings.Get(13205); // Unknown
217         if (info.name.empty())
218           aud = lan;
219         else
220           aud = StringUtils::Format("%s - %s", lan.c_str(), info.name.c_str());
221         CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(460), aud, DisplTime, false, MsgTime);
222         return true;
223       }
224
225       case ACTION_ZOOM_IN:
226       {
227         CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount += 0.01f;
228         if (CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount > 2.f)
229           CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount = 2.f;
230         CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode = ViewModeCustom;
231         g_renderManager.SetViewMode(ViewModeCustom);
232         ShowSlider(action.GetID(), 216, CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount, 0.5f, 0.1f, 2.0f);
233         return true;
234       }
235
236       case ACTION_ZOOM_OUT:
237       {
238         CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount -= 0.01f;
239         if (CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount < 0.5f)
240           CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount = 0.5f;
241         CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode = ViewModeCustom;
242         g_renderManager.SetViewMode(ViewModeCustom);
243         ShowSlider(action.GetID(), 216, CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount, 0.5f, 0.1f, 2.0f);
244         return true;
245       }
246
247       case ACTION_INCREASE_PAR:
248       {
249         CMediaSettings::Get().GetCurrentVideoSettings().m_CustomPixelRatio += 0.01f;
250         if (CMediaSettings::Get().GetCurrentVideoSettings().m_CustomPixelRatio > 2.f)
251           CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount = 2.f;
252         CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode = ViewModeCustom;
253         g_renderManager.SetViewMode(ViewModeCustom);
254         ShowSlider(action.GetID(), 217, CMediaSettings::Get().GetCurrentVideoSettings().m_CustomPixelRatio, 0.5f, 0.1f, 2.0f);
255         return true;
256       }
257
258       case ACTION_DECREASE_PAR:
259       {
260         CMediaSettings::Get().GetCurrentVideoSettings().m_CustomPixelRatio -= 0.01f;
261         if (CMediaSettings::Get().GetCurrentVideoSettings().m_CustomZoomAmount < 0.5f)
262           CMediaSettings::Get().GetCurrentVideoSettings().m_CustomPixelRatio = 0.5f;
263         CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode = ViewModeCustom;
264         g_renderManager.SetViewMode(ViewModeCustom);
265         ShowSlider(action.GetID(), 217, CMediaSettings::Get().GetCurrentVideoSettings().m_CustomPixelRatio, 0.5f, 0.1f, 2.0f);
266         return true;
267       }
268
269       case ACTION_VSHIFT_UP:
270       {
271         CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift -= 0.01f;
272         if (CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift < -2.0f)
273           CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift = -2.0f;
274         CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode = ViewModeCustom;
275         g_renderManager.SetViewMode(ViewModeCustom);
276         ShowSlider(action.GetID(), 225, CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift, -2.0f, 0.1f, 2.0f);
277         return true;
278       }
279
280       case ACTION_VSHIFT_DOWN:
281       {
282         CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift += 0.01f;
283         if (CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift > 2.0f)
284           CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift = 2.0f;
285         CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode = ViewModeCustom;
286         g_renderManager.SetViewMode(ViewModeCustom);
287         ShowSlider(action.GetID(), 225, CMediaSettings::Get().GetCurrentVideoSettings().m_CustomVerticalShift, -2.0f, 0.1f, 2.0f);
288         return true;
289       }
290
291       case ACTION_SUBTITLE_VSHIFT_UP:
292       {
293         RESOLUTION_INFO res_info = g_graphicsContext.GetResInfo();
294         int subalign = CSettings::Get().GetInt("subtitles.align");
295         if ((subalign == SUBTITLE_ALIGN_BOTTOM_OUTSIDE) || (subalign == SUBTITLE_ALIGN_TOP_INSIDE))
296         {
297           res_info.iSubtitles ++;
298           if (res_info.iSubtitles >= res_info.iHeight)
299             res_info.iSubtitles = res_info.iHeight - 1;
300
301           ShowSlider(action.GetID(), 274, (float) res_info.iHeight - res_info.iSubtitles, 0.0f, 1.0f, (float) res_info.iHeight);
302         }
303         else
304         {
305           res_info.iSubtitles --;
306           if (res_info.iSubtitles < 0)
307             res_info.iSubtitles = 0;
308
309           if (subalign == SUBTITLE_ALIGN_MANUAL)
310             ShowSlider(action.GetID(), 274, (float) res_info.iSubtitles, 0.0f, 1.0f, (float) res_info.iHeight);
311           else
312             ShowSlider(action.GetID(), 274, (float) res_info.iSubtitles - res_info.iHeight, (float) -res_info.iHeight, -1.0f, 0.0f);
313         }
314         g_graphicsContext.SetResInfo(g_graphicsContext.GetVideoResolution(), res_info);
315         return true;
316       }
317
318       case ACTION_SUBTITLE_VSHIFT_DOWN:
319       {
320         RESOLUTION_INFO res_info =  g_graphicsContext.GetResInfo();
321         int subalign = CSettings::Get().GetInt("subtitles.align");
322         if ((subalign == SUBTITLE_ALIGN_BOTTOM_OUTSIDE) || (subalign == SUBTITLE_ALIGN_TOP_INSIDE))
323         {
324           res_info.iSubtitles--;
325           if (res_info.iSubtitles < 0)
326             res_info.iSubtitles = 0;
327
328           ShowSlider(action.GetID(), 274, (float) res_info.iHeight - res_info.iSubtitles, 0.0f, 1.0f, (float) res_info.iHeight);
329         }
330         else
331         {
332           res_info.iSubtitles++;
333           if (res_info.iSubtitles >= res_info.iHeight)
334             res_info.iSubtitles = res_info.iHeight - 1;
335
336           if (subalign == SUBTITLE_ALIGN_MANUAL)
337             ShowSlider(action.GetID(), 274, (float) res_info.iSubtitles, 0.0f, 1.0f, (float) res_info.iHeight);
338           else
339             ShowSlider(action.GetID(), 274, (float) res_info.iSubtitles - res_info.iHeight, (float) -res_info.iHeight, -1.0f, 0.0f);
340         }
341         g_graphicsContext.SetResInfo(g_graphicsContext.GetVideoResolution(), res_info);
342         return true;
343       }
344
345       case ACTION_SUBTITLE_ALIGN:
346       {
347         RESOLUTION_INFO res_info = g_graphicsContext.GetResInfo();
348         int subalign = CSettings::Get().GetInt("subtitles.align");
349
350         subalign++;
351         if (subalign > SUBTITLE_ALIGN_TOP_OUTSIDE)
352           subalign = SUBTITLE_ALIGN_MANUAL;
353
354         res_info.iSubtitles = res_info.iHeight - 1;
355
356         CSettings::Get().SetInt("subtitles.align", subalign);
357         CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info,
358                                               g_localizeStrings.Get(21460),
359                                               g_localizeStrings.Get(21461 + subalign), 
360                                               TOAST_DISPLAY_TIME, false);
361         g_graphicsContext.SetResInfo(g_graphicsContext.GetVideoResolution(), res_info);
362         return true;
363       }
364
365       case ACTION_VOLAMP_UP:
366       case ACTION_VOLAMP_DOWN:
367       {
368         // Don't allow change with passthrough audio
369         if (g_application.m_pPlayer->IsPassthrough())
370         {
371           CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning,
372                                                 g_localizeStrings.Get(660),
373                                                 g_localizeStrings.Get(29802),
374                                                 TOAST_DISPLAY_TIME, false);
375           return false;
376         }
377
378         float sliderMax = VOLUME_DRC_MAXIMUM / 100.0f;
379         float sliderMin = VOLUME_DRC_MINIMUM / 100.0f;
380
381         if (action.GetID() == ACTION_VOLAMP_UP)
382           CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification += 1.0f;
383         else
384           CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification -= 1.0f;
385
386         CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification =
387           std::max(std::min(CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification, sliderMax), sliderMin);
388
389         g_application.m_pPlayer->SetDynamicRangeCompression((long)(CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification * 100));
390
391         ShowSlider(action.GetID(), 660, CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification, sliderMin, 1.0f, sliderMax);
392         return true;
393       }
394
395       default:
396         break;
397     }
398   }
399   return false;
400 }
401
402 void CPlayerController::ShowSlider(int action, int label, float value, float min, float delta, float max, bool modal)
403 {
404   m_sliderAction = action;
405   if (modal)
406     CGUIDialogSlider::ShowAndGetInput(g_localizeStrings.Get(label), value, min, delta, max, this);
407   else
408     CGUIDialogSlider::Display(label, value, min, delta, max, this);
409 }
410
411 void CPlayerController::OnSliderChange(void *data, CGUISliderControl *slider)
412 {
413   if (!slider)
414     return;
415
416   if (m_sliderAction == ACTION_ZOOM_OUT || m_sliderAction == ACTION_ZOOM_IN ||
417       m_sliderAction == ACTION_INCREASE_PAR || m_sliderAction == ACTION_DECREASE_PAR ||
418       m_sliderAction == ACTION_VSHIFT_UP || m_sliderAction == ACTION_VSHIFT_DOWN ||
419       m_sliderAction == ACTION_SUBTITLE_VSHIFT_UP || m_sliderAction == ACTION_SUBTITLE_VSHIFT_DOWN)
420   {
421     CStdString strValue = StringUtils::Format("%1.2f",slider->GetFloatValue());
422     slider->SetTextValue(strValue);
423   }
424   else if (m_sliderAction == ACTION_VOLAMP_UP || m_sliderAction == ACTION_VOLAMP_DOWN)
425     slider->SetTextValue(CGUIDialogAudioSubtitleSettings::FormatDecibel(slider->GetFloatValue(), 1.0f));
426   else
427     slider->SetTextValue(CGUIDialogAudioSubtitleSettings::FormatDelay(slider->GetFloatValue(), 0.025f));
428
429   if (g_application.m_pPlayer->HasPlayer())
430   {
431     if (m_sliderAction == ACTION_AUDIO_DELAY)
432     {
433       CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay = slider->GetFloatValue();
434       g_application.m_pPlayer->SetAVDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay);
435     }
436     else if (m_sliderAction == ACTION_SUBTITLE_DELAY)
437     {
438       CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay = slider->GetFloatValue();
439       g_application.m_pPlayer->SetSubTitleDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay);
440     }
441   }
442 }