TopInformations.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "TopInformations.hpp"
  2. #include "AssetManager.hpp"
  3. #include "DownloadQueue.hpp"
  4. #include "Notification.hpp"
  5. #include "Theme.hpp"
  6. #include "Util.hpp"
  7. #include "Config.hpp"
  8. #include "States/StateIdentifiers.hpp"
  9. #include "States/DialogState.hpp"
  10. #include "States/SyncState.hpp"
  11. #include <cpp3ds/System/I18n.hpp>
  12. #include <cpp3ds/System/FileSystem.hpp>
  13. #include <TweenEngine/Tween.h>
  14. #include <time.h>
  15. #include <stdlib.h>
  16. namespace FreeShop {
  17. TopInformations::TopInformations()
  18. : m_batteryPercent(-1)
  19. , m_textClockMode(1)
  20. , m_isCollapsed(true)
  21. , m_isTransitioning(false)
  22. , m_canTransition(false)
  23. , m_lowBatteryNotified(false)
  24. {
  25. //Start the clock
  26. m_switchClock.restart();
  27. m_updateClock.restart();
  28. //Get the time to show it in the top part of the App List
  29. time_t t = time(NULL);
  30. struct tm * timeinfo;
  31. timeinfo = localtime(&t);
  32. char timeTextFmt[12];
  33. char tempSec[3];
  34. strftime(tempSec, 3, "%S", timeinfo);
  35. strftime(timeTextFmt, 12, "%H %M", timeinfo);
  36. m_textClock.setString(timeTextFmt);
  37. m_textClock.useSystemFont();
  38. m_textClock.setCharacterSize(14);
  39. if (Theme::isTextThemed)
  40. m_textClock.setFillColor(Theme::primaryTextColor);
  41. else
  42. m_textClock.setFillColor(cpp3ds::Color(80, 80, 80, 255));
  43. //Battery icon
  44. if (fopen(FREESHOP_DIR "/theme/images/battery0.png", "rb"))
  45. m_textureBattery.loadFromFile(FREESHOP_DIR "/theme/images/battery0.png");
  46. else
  47. m_textureBattery.loadFromFile("images/battery0.png");
  48. m_batteryIcon.setPosition(370.f - m_textureBattery.getSize().x, 5.f);
  49. m_batteryIcon.setTexture(m_textureBattery, true);
  50. //Signal icon
  51. if (fopen(FREESHOP_DIR "/theme/images/wifi0.png", "rb"))
  52. m_textureSignal.loadFromFile(FREESHOP_DIR "/theme/images/wifi0.png");
  53. else
  54. m_textureSignal.loadFromFile("images/wifi0.png");
  55. m_signalIcon.setPosition(-50.f, 5.f);
  56. m_signalIcon.setTexture(m_textureSignal, true);
  57. //Define texts position
  58. m_textClock.setPosition(308.f - (m_textureBattery.getSize().x + m_textClock.getLocalBounds().width), -50.f);
  59. //Two points in clock
  60. m_textTwoPoints = m_textClock;
  61. m_textTwoPoints.setString(":");
  62. m_textTwoPoints.setCharacterSize(14);
  63. m_textTwoPoints.setPosition((m_textClock.getPosition().x + (m_textClock.getLocalBounds().width / 2)) - 3, m_textClock.getPosition().y);
  64. #define TWEEN_IN(obj, posY) \
  65. TweenEngine::Tween::to(obj, obj.POSITION_Y, 0.6f).target(posY).delay(0.5f).start(m_tweenManager);
  66. #define TWEEN_IN_NOWAIT(obj, posY) \
  67. TweenEngine::Tween::to(obj, obj.POSITION_Y, 0.6f).target(posY).start(m_tweenManager);
  68. #define TWEEN_IN_X(obj, posX) \
  69. TweenEngine::Tween::to(obj, obj.POSITION_X, 0.6f).target(posX).delay(0.5f).start(m_tweenManager);
  70. #define TWEEN_IN_X_NOWAIT(obj, posX) \
  71. TweenEngine::Tween::to(obj, obj.POSITION_X, 0.6f).target(posX).start(m_tweenManager);
  72. TweenEngine::Tween::to(m_textClock, m_textClock.POSITION_Y, 0.6f).target(5.f).delay(0.5f).setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween* source) {m_isCollapsed = false;}).start(m_tweenManager);
  73. TWEEN_IN(m_textTwoPoints, 5.f);
  74. TWEEN_IN_X(m_batteryIcon, 318.f - m_textureBattery.getSize().x);
  75. TWEEN_IN_X(m_signalIcon, 2.f);
  76. //Two points animation
  77. TweenEngine::Tween::to(m_textTwoPoints, util3ds::TweenText::FILL_COLOR_ALPHA, 1.f).target(0).repeatYoyo(-1, 0).start(m_tweenManager);
  78. }
  79. TopInformations::~TopInformations()
  80. {
  81. }
  82. void TopInformations::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  83. {
  84. states.transform *= getTransform();
  85. //Draw clock
  86. target.draw(m_textClock);
  87. //Draw only the two points if the clock text is in clock mode
  88. if (m_textClockMode == 1 && !m_isTransitioning)
  89. target.draw(m_textTwoPoints);
  90. //Draw battery & text
  91. target.draw(m_batteryIcon);
  92. //Draw signal
  93. target.draw(m_signalIcon);
  94. }
  95. void TopInformations::update(float delta)
  96. {
  97. //Update the time to show it in the top part of the App List
  98. time_t t = time(NULL);
  99. struct tm * timeinfo;
  100. timeinfo = localtime(&t);
  101. char timeTextFmt[12];
  102. char tempSec[3];
  103. strftime(tempSec, 3, "%S", timeinfo);
  104. strftime(timeTextFmt, 12, "%H %M", timeinfo);
  105. m_tweenManager.update(delta);
  106. //Update battery and signal icons
  107. if (m_updateClock.getElapsedTime() >= cpp3ds::seconds(2)) {
  108. m_updateClock.restart();
  109. updateIcons(std::string(timeTextFmt));
  110. }
  111. }
  112. void TopInformations::setCollapsed(bool collapsed)
  113. {
  114. if (collapsed)
  115. m_isCollapsed = collapsed;
  116. if (collapsed) {
  117. TweenEngine::Tween::to(m_textClock, m_textClock.POSITION_Y, 0.6f).target(-50.f).setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween* source) {m_isCollapsed = true;}).start(m_tweenManager);
  118. TWEEN_IN_NOWAIT(m_textTwoPoints, -50.f);
  119. TWEEN_IN_X_NOWAIT(m_batteryIcon, 370.f - m_textureBattery.getSize().x);
  120. TWEEN_IN_X_NOWAIT(m_signalIcon, -50.f);
  121. } else {
  122. TweenEngine::Tween::to(m_textClock, m_textClock.POSITION_Y, 0.6f).target(5.f).delay(0.5f).setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween* source) {m_isCollapsed = false;}).start(m_tweenManager);
  123. TWEEN_IN(m_textTwoPoints, 5.f);
  124. TWEEN_IN_X(m_batteryIcon, 318.f - m_textureBattery.getSize().x);
  125. TWEEN_IN_X(m_signalIcon, 2.f);
  126. }
  127. }
  128. void TopInformations::updateIcons(std::string timeTextFmt)
  129. {
  130. #ifndef EMULATION
  131. //Update battery icon and percentage
  132. cpp3ds::Uint8 batteryChargeState = 0;
  133. cpp3ds::Uint8 isAdapterPlugged = 0;
  134. cpp3ds::Uint8 batteryLevel = 0;
  135. cpp3ds::Uint8 batteryPercentHolder = 0;
  136. std::string batteryPath;
  137. if (R_SUCCEEDED(PTMU_GetBatteryChargeState(&batteryChargeState)) && batteryChargeState) {
  138. batteryPath = "battery_charging.png";
  139. m_lowBatteryNotified = false;
  140. } else if (R_SUCCEEDED(PTMU_GetAdapterState(&isAdapterPlugged)) && isAdapterPlugged) {
  141. batteryPath = "battery_charging_full.png";
  142. m_lowBatteryNotified = false;
  143. } else if (R_SUCCEEDED(PTMU_GetBatteryLevel(&batteryLevel))) {
  144. batteryPath = "battery" + std::to_string(batteryLevel - 1) + ".png";
  145. if (batteryLevel - 1 == 1 && !m_lowBatteryNotified) {
  146. Notification::spawn(_("Battery power is running low"));
  147. m_lowBatteryNotified = true;
  148. }
  149. }
  150. else
  151. batteryPath = "battery0.png";
  152. //Update battery percentage
  153. if (R_SUCCEEDED(MCUHWC_GetBatteryLevel(&batteryPercentHolder)))
  154. m_batteryPercent = static_cast<int>(batteryPercentHolder);
  155. std::string themedBatteryPath = cpp3ds::FileSystem::getFilePath(FREESHOP_DIR "/theme/images/" + batteryPath);
  156. if (pathExists(themedBatteryPath.c_str(), false))
  157. m_textureBattery.loadFromFile(themedBatteryPath);
  158. else
  159. m_textureBattery.loadFromFile("images/" + batteryPath);
  160. //Update signal icon
  161. uint32_t wifiStatus = 0;
  162. std::string signalPath;
  163. if (R_SUCCEEDED(ACU_GetWifiStatus(&wifiStatus)) && wifiStatus)
  164. signalPath = "wifi" + std::to_string(osGetWifiStrength()) + ".png";
  165. else
  166. signalPath = "wifi_disconnected.png";
  167. std::string themedSignalPath = cpp3ds::FileSystem::getFilePath(FREESHOP_DIR "/theme/images/" + signalPath);
  168. if (pathExists(themedSignalPath.c_str(), false))
  169. m_textureSignal.loadFromFile(themedSignalPath);
  170. else
  171. m_textureSignal.loadFromFile("images/" + signalPath);
  172. #else
  173. //Update battery icon
  174. std::string batteryPath = "battery" + std::to_string(rand() % 5) + ".png";
  175. std::string themedBatteryPath = cpp3ds::FileSystem::getFilePath(FREESHOP_DIR "/theme/images/" + batteryPath);
  176. m_batteryPercent = rand() % 101;
  177. if (pathExists(themedBatteryPath.c_str(), false))
  178. m_textureBattery.loadFromFile(themedBatteryPath);
  179. else
  180. m_textureBattery.loadFromFile("images/" + batteryPath);
  181. //Update signal icon
  182. std::string signalPath = "wifi" + std::to_string(rand() % 4) + ".png";
  183. std::string themedSignalPath = cpp3ds::FileSystem::getFilePath(FREESHOP_DIR "/theme/images/" + signalPath);
  184. if (pathExists(themedSignalPath.c_str(), false))
  185. m_textureSignal.loadFromFile(themedSignalPath);
  186. else
  187. m_textureSignal.loadFromFile("images/" + signalPath);
  188. #endif
  189. //Change the mode of the clock if enough time passed
  190. if (m_switchClock.getElapsedTime() >= cpp3ds::seconds(10)) {
  191. //Reset the clock
  192. m_switchClock.restart();
  193. m_isTransitioning = true;
  194. //Switch mode
  195. if (m_textClockMode == 1 && Config::get(Config::ShowBattery).GetBool() && m_canTransition) {
  196. //Battery percentage mode
  197. m_textClockMode = 2;
  198. //Fade out the old text
  199. TweenEngine::Tween::to(m_textClock, m_textClock.FILL_COLOR_ALPHA, 0.2f)
  200. .target(0.f)
  201. .setCallback(TweenEngine::TweenCallback::COMPLETE, [=](TweenEngine::BaseTween* source) {
  202. m_textClock.setString(std::to_string(m_batteryPercent) + "%");
  203. if (!m_isCollapsed)
  204. m_textClock.setPosition(308.f - (m_textureBattery.getSize().x + m_textClock.getLocalBounds().width), 5.f);
  205. })
  206. .start(m_tweenManager);
  207. TweenEngine::Tween::to(m_textClock, m_textClock.SCALE_Y, 0.2f)
  208. .target(.5f)
  209. .start(m_tweenManager);
  210. //Fade in the new text
  211. TweenEngine::Tween::to(m_textClock, m_textClock.FILL_COLOR_ALPHA, 0.2f)
  212. .target(255.f)
  213. .delay(0.3f)
  214. .setCallback(TweenEngine::TweenCallback::COMPLETE, [=](TweenEngine::BaseTween* source) {
  215. m_isTransitioning = false;
  216. })
  217. .start(m_tweenManager);
  218. TweenEngine::Tween::to(m_textClock, m_textClock.SCALE_Y, 0.2f)
  219. .target(1.f)
  220. .delay(0.3f)
  221. .start(m_tweenManager);
  222. } else if (m_textClockMode != 1) {
  223. //Clock mode
  224. m_textClockMode = 1;
  225. //Fade out old text
  226. TweenEngine::Tween::to(m_textClock, m_textClock.FILL_COLOR_ALPHA, 0.2f)
  227. .target(0.f)
  228. .setCallback(TweenEngine::TweenCallback::COMPLETE, [=](TweenEngine::BaseTween* source) {
  229. m_textClock.setString(timeTextFmt);
  230. if (!m_isCollapsed)
  231. m_textClock.setPosition(308.f - (m_textureBattery.getSize().x + m_textClock.getLocalBounds().width), 5.f);
  232. })
  233. .start(m_tweenManager);
  234. TweenEngine::Tween::to(m_textClock, m_textClock.SCALE_Y, 0.2f)
  235. .target(.5f)
  236. .start(m_tweenManager);
  237. //Fade in new text
  238. TweenEngine::Tween::to(m_textClock, m_textClock.FILL_COLOR_ALPHA, 0.2f)
  239. .target(255.f)
  240. .delay(0.3f)
  241. .setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween* source) {
  242. m_isTransitioning = false;
  243. })
  244. .start(m_tweenManager);
  245. TweenEngine::Tween::to(m_textClock, m_textClock.SCALE_Y, 0.2f)
  246. .target(1.f)
  247. .delay(0.3f)
  248. .start(m_tweenManager);
  249. } else {
  250. m_isTransitioning = false;
  251. }
  252. }
  253. //Don't forget to update the text at each update (except if a transition is occuring)
  254. if (!m_isTransitioning) {
  255. switch (m_textClockMode) {
  256. case 1:
  257. //Clock mode
  258. m_textClock.setString(timeTextFmt);
  259. break;
  260. case 2:
  261. //Battery mode
  262. m_textClock.setString(std::to_string(m_batteryPercent) + "%");
  263. break;
  264. default:
  265. //We should never be here
  266. break;
  267. }
  268. if (!m_isCollapsed)
  269. m_textClock.setPosition(308.f - (m_textureBattery.getSize().x + m_textClock.getLocalBounds().width), 5.f);
  270. }
  271. }
  272. void TopInformations::setTextMode(int newMode)
  273. {
  274. //Update the time to show it in the top part of the App List
  275. time_t t = time(NULL);
  276. struct tm * timeinfo;
  277. timeinfo = localtime(&t);
  278. char timeTextFmt[12];
  279. char tempSec[3];
  280. strftime(tempSec, 3, "%S", timeinfo);
  281. strftime(timeTextFmt, 12, "%H %M", timeinfo);
  282. m_textClockMode = newMode;
  283. updateIcons(timeTextFmt);
  284. }
  285. void TopInformations::setModeChangeEnabled(bool newMode)
  286. {
  287. m_canTransition = newMode;
  288. }
  289. void TopInformations::resetModeTimer()
  290. {
  291. m_switchClock.restart();
  292. }
  293. #ifndef EMULATION
  294. Result TopInformations::PTMU_GetAdapterState(u8 *out)
  295. {
  296. Handle serviceHandle = 0;
  297. Result result = srvGetServiceHandle(&serviceHandle, "ptm:u");
  298. u32* ipc = getThreadCommandBuffer();
  299. ipc[0] = 0x50000;
  300. Result ret = svcSendSyncRequest(serviceHandle);
  301. svcCloseHandle(serviceHandle);
  302. if(ret < 0) return ret;
  303. *out = ipc[2];
  304. return ipc[1];
  305. }
  306. // Saw on the awesome 3DShell file explorer homebrew
  307. Result TopInformations::MCUHWC_GetBatteryLevel(u8 *out)
  308. {
  309. Handle serviceHandle = 0;
  310. Result result = srvGetServiceHandle(&serviceHandle, "mcu::HWC");
  311. u32* ipc = getThreadCommandBuffer();
  312. ipc[0] = 0x50000;
  313. Result ret = svcSendSyncRequest(serviceHandle);
  314. svcCloseHandle(serviceHandle);
  315. if(ret < 0) return ret;
  316. *out = ipc[2];
  317. return ipc[1];
  318. }
  319. #endif
  320. } // namespace FreeShop