1 /*
2  *  Copyright (C) 2016-2018 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 "JoystickEasterEgg.h"
10 
11 #include "ServiceBroker.h"
12 #include "games/GameServices.h"
13 #include "games/GameSettings.h"
14 #include "games/controllers/ControllerIDs.h"
15 #include "guilib/GUIAudioManager.h"
16 #include "guilib/WindowIDs.h"
17 
18 using namespace KODI;
19 using namespace JOYSTICK;
20 
21 const std::map<std::string, std::vector<FeatureName>> CJoystickEasterEgg::m_sequence = {
22     {
23         DEFAULT_CONTROLLER_ID,
24         {
25             "up",
26             "up",
27             "down",
28             "down",
29             "left",
30             "right",
31             "left",
32             "right",
33             "b",
34             "a",
35         },
36     },
37     {
38         DEFAULT_REMOTE_ID,
39         {
40             "up",
41             "up",
42             "down",
43             "down",
44             "left",
45             "right",
46             "left",
47             "right",
48             "back",
49             "ok",
50         },
51     },
52 };
53 
CJoystickEasterEgg(const std::string & controllerId)54 CJoystickEasterEgg::CJoystickEasterEgg(const std::string& controllerId)
55   : m_controllerId(controllerId), m_state(0)
56 {
57 }
58 
OnButtonPress(const FeatureName & feature)59 bool CJoystickEasterEgg::OnButtonPress(const FeatureName& feature)
60 {
61   bool bHandled = false;
62 
63   auto it = m_sequence.find(m_controllerId);
64   if (it != m_sequence.end())
65   {
66     const auto& sequence = it->second;
67 
68     // Reset state if it previously finished
69     if (m_state >= sequence.size())
70       m_state = 0;
71 
72     if (feature == sequence[m_state])
73       m_state++;
74     else
75       m_state = 0;
76 
77     if (IsCapturing())
78     {
79       bHandled = true;
80 
81       if (m_state >= sequence.size())
82         OnFinish();
83     }
84   }
85 
86   return bHandled;
87 }
88 
IsCapturing()89 bool CJoystickEasterEgg::IsCapturing()
90 {
91   // Capture input when finished with arrows (2 x up/down/left/right)
92   return m_state > 8;
93 }
94 
OnFinish(void)95 void CJoystickEasterEgg::OnFinish(void)
96 {
97   GAME::CGameSettings& gameSettings = CServiceBroker::GetGameServices().GameSettings();
98   gameSettings.ToggleGames();
99 
100   WINDOW_SOUND sound = gameSettings.GamesEnabled() ? SOUND_INIT : SOUND_DEINIT;
101   CGUIComponent* gui = CServiceBroker::GetGUI();
102   if (gui)
103     gui->GetAudioManager().PlayWindowSound(WINDOW_DIALOG_KAI_TOAST, sound);
104 
105   //! @todo Shake screen
106 }
107