PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/dialogs/GUIDialogGamepad.cpp

https://gitlab.com/sandihidayat/kodi
C++ | 315 lines | 217 code | 35 blank | 63 comment | 39 complexity | 32c4f755b44ef4cb4ae4909aa6fcb848 MD5 | raw file
  1. /*
  2. * Copyright (C) 2005-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. #include "GUIDialogGamepad.h"
  21. #include "utils/md5.h"
  22. #include "utils/StringUtils.h"
  23. #include "guilib/GUIAudioManager.h"
  24. #include "guilib/GUIWindowManager.h"
  25. #include "GUIDialogOK.h"
  26. #include "input/Key.h"
  27. #include "guilib/LocalizeStrings.h"
  28. CGUIDialogGamepad::CGUIDialogGamepad(void)
  29. : CGUIDialogBoxBase(WINDOW_DIALOG_GAMEPAD, "DialogGamepad.xml")
  30. {
  31. m_bCanceled = false;
  32. m_iRetries = 0;
  33. m_bUserInputCleanup = true;
  34. m_bHideInputChars = true;
  35. m_cHideInputChar = '*';
  36. }
  37. CGUIDialogGamepad::~CGUIDialogGamepad(void)
  38. {}
  39. bool CGUIDialogGamepad::OnAction(const CAction &action)
  40. {
  41. if ((action.GetButtonCode() >= KEY_BUTTON_A &&
  42. action.GetButtonCode() <= KEY_BUTTON_RIGHT_TRIGGER) ||
  43. (action.GetButtonCode() >= KEY_BUTTON_DPAD_UP &&
  44. action.GetButtonCode() <= KEY_BUTTON_DPAD_RIGHT) ||
  45. (action.GetID() >= ACTION_MOVE_LEFT &&
  46. action.GetID() <= ACTION_MOVE_DOWN) ||
  47. action.GetID() == ACTION_PLAYER_PLAY
  48. )
  49. {
  50. switch (action.GetButtonCode())
  51. {
  52. case KEY_BUTTON_A : m_strUserInput += "A"; break;
  53. case KEY_BUTTON_B : m_strUserInput += "B"; break;
  54. case KEY_BUTTON_X : m_strUserInput += "X"; break;
  55. case KEY_BUTTON_Y : m_strUserInput += "Y"; break;
  56. case KEY_BUTTON_BLACK : m_strUserInput += "K"; break;
  57. case KEY_BUTTON_WHITE : m_strUserInput += "W"; break;
  58. case KEY_BUTTON_LEFT_TRIGGER : m_strUserInput += "("; break;
  59. case KEY_BUTTON_RIGHT_TRIGGER : m_strUserInput += ")"; break;
  60. case KEY_BUTTON_DPAD_UP : m_strUserInput += "U"; break;
  61. case KEY_BUTTON_DPAD_DOWN : m_strUserInput += "D"; break;
  62. case KEY_BUTTON_DPAD_LEFT : m_strUserInput += "L"; break;
  63. case KEY_BUTTON_DPAD_RIGHT : m_strUserInput += "R"; break;
  64. default:
  65. switch (action.GetID())
  66. {
  67. case ACTION_MOVE_LEFT: m_strUserInput += "L"; break;
  68. case ACTION_MOVE_RIGHT: m_strUserInput += "R"; break;
  69. case ACTION_MOVE_UP: m_strUserInput += "U"; break;
  70. case ACTION_MOVE_DOWN: m_strUserInput += "D"; break;
  71. case ACTION_PLAYER_PLAY: m_strUserInput += "P"; break;
  72. default:
  73. return true;
  74. }
  75. break;
  76. }
  77. std::string strHiddenInput(m_strUserInput);
  78. for (int i = 0; i < (int)strHiddenInput.size(); i++)
  79. {
  80. strHiddenInput[i] = m_cHideInputChar;
  81. }
  82. SetLine(2, strHiddenInput);
  83. return true;
  84. }
  85. else if (action.GetButtonCode() == KEY_BUTTON_BACK || action.GetID() == ACTION_PREVIOUS_MENU || action.GetID() == ACTION_NAV_BACK)
  86. {
  87. m_bConfirmed = false;
  88. m_bCanceled = true;
  89. m_strUserInput = "";
  90. m_bHideInputChars = true;
  91. Close();
  92. return true;
  93. }
  94. else if (action.GetButtonCode() == KEY_BUTTON_START || action.GetID() == ACTION_SELECT_ITEM)
  95. {
  96. m_bConfirmed = false;
  97. m_bCanceled = false;
  98. std::string md5pword2 = XBMC::XBMC_MD5::GetMD5(m_strUserInput);
  99. if (!StringUtils::EqualsNoCase(m_strPassword, md5pword2))
  100. {
  101. // incorrect password entered
  102. m_iRetries--;
  103. // don't clean up if the calling code wants the bad user input
  104. if (m_bUserInputCleanup)
  105. m_strUserInput = "";
  106. else
  107. m_bUserInputCleanup = true;
  108. m_bHideInputChars = true;
  109. Close();
  110. return true;
  111. }
  112. // correct password entered
  113. m_bConfirmed = true;
  114. m_iRetries = 0;
  115. m_strUserInput = "";
  116. m_bHideInputChars = true;
  117. Close();
  118. return true;
  119. }
  120. else if (action.GetID() >= REMOTE_0 && action.GetID() <= REMOTE_9)
  121. {
  122. return true; // unhandled
  123. }
  124. else
  125. {
  126. return CGUIDialog::OnAction(action);
  127. }
  128. }
  129. bool CGUIDialogGamepad::OnMessage(CGUIMessage& message)
  130. {
  131. switch ( message.GetMessage() )
  132. {
  133. case GUI_MSG_WINDOW_INIT:
  134. {
  135. m_bConfirmed = false;
  136. m_bCanceled = false;
  137. m_cHideInputChar = g_localizeStrings.Get(12322).c_str()[0];
  138. CGUIDialog::OnMessage(message);
  139. return true;
  140. }
  141. break;
  142. case GUI_MSG_CLICKED:
  143. {
  144. m_bConfirmed = false;
  145. m_bCanceled = false;
  146. }
  147. break;
  148. }
  149. return CGUIDialogBoxBase::OnMessage(message);
  150. }
  151. // \brief Show gamepad keypad and replace aTextString with user input.
  152. // \param aTextString String to preload into the keyboard accumulator. Overwritten with user input if return=true.
  153. // \param dlgHeading String shown on dialog title. Converts to localized string if contains a positive integer.
  154. // \param bHideUserInput Masks user input as asterisks if set as true. Currently not yet implemented.
  155. // \return true if successful display and user input. false if unsuccessful display, no user input, or canceled editing.
  156. bool CGUIDialogGamepad::ShowAndGetInput(std::string& aTextString, const std::string &dlgHeading, bool bHideUserInput)
  157. {
  158. // Prompt user for input
  159. std::string strUserInput;
  160. if (ShowAndVerifyInput(strUserInput, dlgHeading, aTextString, "", "", true, bHideUserInput))
  161. {
  162. // user entry was blank
  163. return false;
  164. }
  165. if (strUserInput.empty())
  166. // user canceled out
  167. return false;
  168. // We should have a string to return
  169. aTextString = strUserInput;
  170. return true;
  171. }
  172. // \brief Show gamepad keypad twice to get and confirm a user-entered password string.
  173. // \param strNewPassword String to preload into the keyboard accumulator. Overwritten with user input if return=true.
  174. // \return true if successful display and user input entry/re-entry. false if unsuccessful display, no user input, or canceled editing.
  175. bool CGUIDialogGamepad::ShowAndVerifyNewPassword(std::string& strNewPassword)
  176. {
  177. // Prompt user for password input
  178. std::string strUserInput;
  179. if (ShowAndVerifyInput(strUserInput, "12340", "12330", "12331", "", true, true))
  180. {
  181. // TODO: Show error to user saying the password entry was blank
  182. CGUIDialogOK::ShowAndGetInput(12357, 12358); // Password is empty/blank
  183. return false;
  184. }
  185. if (strUserInput.empty())
  186. // user canceled out
  187. return false;
  188. // Prompt again for password input, this time sending previous input as the password to verify
  189. if (!ShowAndVerifyInput(strUserInput, "12341", "12330", "12331", "", false, true))
  190. {
  191. // TODO: Show error to user saying the password re-entry failed
  192. CGUIDialogOK::ShowAndGetInput(12357, 12344); // Password do not match
  193. return false;
  194. }
  195. // password entry and re-entry succeeded
  196. strNewPassword = strUserInput;
  197. return true;
  198. }
  199. // \brief Show gamepad keypad and verify user input against strPassword.
  200. // \param strPassword Value to compare against user input.
  201. // \param dlgHeading String shown on dialog title. Converts to localized string if contains a positive integer.
  202. // \param iRetries If greater than 0, shows "Incorrect password, %d retries left" on dialog line 2, else line 2 is blank.
  203. // \return 0 if successful display and user input. 1 if unsuccessful input. -1 if no user input or canceled editing.
  204. int CGUIDialogGamepad::ShowAndVerifyPassword(std::string& strPassword, const std::string& dlgHeading, int iRetries)
  205. {
  206. std::string strLine2;
  207. if (0 < iRetries)
  208. {
  209. // Show a string telling user they have iRetries retries left
  210. strLine2 = StringUtils::Format("%s %i %s", g_localizeStrings.Get(12342).c_str(), iRetries, g_localizeStrings.Get(12343).c_str());
  211. }
  212. // make a copy of strPassword to prevent from overwriting it later
  213. std::string strPassTemp = strPassword;
  214. if (ShowAndVerifyInput(strPassTemp, dlgHeading, g_localizeStrings.Get(12330), g_localizeStrings.Get(12331), strLine2, true, true))
  215. {
  216. // user entered correct password
  217. return 0;
  218. }
  219. if (strPassTemp.empty())
  220. // user canceled out
  221. return -1;
  222. // user must have entered an incorrect password
  223. return 1;
  224. }
  225. // \brief Show gamepad keypad and verify user input against strToVerify.
  226. // \param strToVerify Value to compare against user input.
  227. // \param dlgHeading String shown on dialog title. Converts to localized string if contains a positive integer.
  228. // \param dlgLine0 String shown on dialog line 0. Converts to localized string if contains a positive integer.
  229. // \param dlgLine1 String shown on dialog line 1. Converts to localized string if contains a positive integer.
  230. // \param dlgLine2 String shown on dialog line 2. Converts to localized string if contains a positive integer.
  231. // \param bGetUserInput If set as true and return=true, strToVerify is overwritten with user input string.
  232. // \param bHideInputChars Masks user input as asterisks if set as true. Currently not yet implemented.
  233. // \return true if successful display and user input. false if unsuccessful display, no user input, or canceled editing.
  234. bool CGUIDialogGamepad::ShowAndVerifyInput(std::string& strToVerify, const std::string& dlgHeading,
  235. const std::string& dlgLine0, const std::string& dlgLine1,
  236. const std::string& dlgLine2, bool bGetUserInput, bool bHideInputChars)
  237. {
  238. // Prompt user for password input
  239. CGUIDialogGamepad *pDialog = (CGUIDialogGamepad *)g_windowManager.GetWindow(WINDOW_DIALOG_GAMEPAD);
  240. pDialog->m_strPassword = strToVerify;
  241. pDialog->m_bUserInputCleanup = !bGetUserInput;
  242. pDialog->m_bHideInputChars = bHideInputChars;
  243. // HACK: This won't work if the label specified is actually a positive numeric value, but that's very unlikely
  244. if (!StringUtils::IsNaturalNumber(dlgHeading))
  245. pDialog->SetHeading( dlgHeading );
  246. else
  247. pDialog->SetHeading( atoi(dlgHeading.c_str()) );
  248. if (!StringUtils::IsNaturalNumber(dlgLine0))
  249. pDialog->SetLine( 0, dlgLine0 );
  250. else
  251. pDialog->SetLine( 0, atoi(dlgLine0.c_str()) );
  252. if (!StringUtils::IsNaturalNumber(dlgLine1))
  253. pDialog->SetLine( 1, dlgLine1 );
  254. else
  255. pDialog->SetLine( 1, atoi(dlgLine1.c_str()) );
  256. if (!StringUtils::IsNaturalNumber(dlgLine2))
  257. pDialog->SetLine( 2, dlgLine2 );
  258. else
  259. pDialog->SetLine( 2, atoi(dlgLine2.c_str()) );
  260. g_audioManager.Enable(false); // dont do sounds during pwd input
  261. pDialog->DoModal();
  262. g_audioManager.Enable(true);
  263. if (bGetUserInput && !pDialog->IsCanceled())
  264. {
  265. strToVerify = XBMC::XBMC_MD5::GetMD5(pDialog->m_strUserInput);
  266. StringUtils::ToLower(strToVerify);
  267. pDialog->m_strUserInput = "";
  268. }
  269. if (!pDialog->IsConfirmed() || pDialog->IsCanceled())
  270. {
  271. // user canceled out or entered an incorrect password
  272. return false;
  273. }
  274. // user entered correct password
  275. return true;
  276. }
  277. bool CGUIDialogGamepad::IsCanceled() const
  278. {
  279. return m_bCanceled;
  280. }