caps_manager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <sstream>
  4. #include "common/fs/file.h"
  5. #include "common/fs/path_util.h"
  6. #include "common/logging/log.h"
  7. #include "common/stb.h"
  8. #include "core/core.h"
  9. #include "core/hle/service/caps/caps_manager.h"
  10. #include "core/hle/service/caps/caps_result.h"
  11. #include "core/hle/service/time/time_manager.h"
  12. #include "core/hle/service/time/time_zone_content_manager.h"
  13. namespace Service::Capture {
  14. AlbumManager::AlbumManager(Core::System& system_) : system{system_} {}
  15. AlbumManager::~AlbumManager() = default;
  16. Result AlbumManager::DeleteAlbumFile(const AlbumFileId& file_id) {
  17. if (file_id.storage > AlbumStorage::Sd) {
  18. return ResultInvalidStorage;
  19. }
  20. if (!is_mounted) {
  21. return ResultIsNotMounted;
  22. }
  23. std::filesystem::path path;
  24. const auto result = GetFile(path, file_id);
  25. if (result.IsError()) {
  26. return result;
  27. }
  28. if (!Common::FS::RemoveFile(path)) {
  29. return ResultFileNotFound;
  30. }
  31. return ResultSuccess;
  32. }
  33. Result AlbumManager::IsAlbumMounted(AlbumStorage storage) {
  34. if (storage > AlbumStorage::Sd) {
  35. return ResultInvalidStorage;
  36. }
  37. is_mounted = true;
  38. if (storage == AlbumStorage::Sd) {
  39. FindScreenshots();
  40. }
  41. return is_mounted ? ResultSuccess : ResultIsNotMounted;
  42. }
  43. Result AlbumManager::GetAlbumFileList(std::vector<AlbumEntry>& out_entries, AlbumStorage storage,
  44. u8 flags) const {
  45. if (storage > AlbumStorage::Sd) {
  46. return ResultInvalidStorage;
  47. }
  48. if (!is_mounted) {
  49. return ResultIsNotMounted;
  50. }
  51. for (auto& [file_id, path] : album_files) {
  52. if (file_id.storage != storage) {
  53. continue;
  54. }
  55. if (out_entries.size() >= SdAlbumFileLimit) {
  56. break;
  57. }
  58. const auto entry_size = Common::FS::GetSize(path);
  59. out_entries.push_back({
  60. .entry_size = entry_size,
  61. .file_id = file_id,
  62. });
  63. }
  64. return ResultSuccess;
  65. }
  66. Result AlbumManager::GetAlbumFileList(std::vector<ApplicationAlbumFileEntry>& out_entries,
  67. ContentType content_type, s64 start_posix_time,
  68. s64 end_posix_time, u64 aruid) const {
  69. if (!is_mounted) {
  70. return ResultIsNotMounted;
  71. }
  72. std::vector<ApplicationAlbumEntry> album_entries;
  73. const auto start_date = ConvertToAlbumDateTime(start_posix_time);
  74. const auto end_date = ConvertToAlbumDateTime(end_posix_time);
  75. const auto result = GetAlbumFileList(album_entries, content_type, start_date, end_date, aruid);
  76. if (result.IsError()) {
  77. return result;
  78. }
  79. for (const auto& album_entry : album_entries) {
  80. ApplicationAlbumFileEntry entry{
  81. .entry = album_entry,
  82. .datetime = album_entry.datetime,
  83. .unknown = {},
  84. };
  85. out_entries.push_back(entry);
  86. }
  87. return ResultSuccess;
  88. }
  89. Result AlbumManager::GetAlbumFileList(std::vector<ApplicationAlbumEntry>& out_entries,
  90. ContentType content_type, AlbumFileDateTime start_date,
  91. AlbumFileDateTime end_date, u64 aruid) const {
  92. if (!is_mounted) {
  93. return ResultIsNotMounted;
  94. }
  95. for (auto& [file_id, path] : album_files) {
  96. if (file_id.type != content_type) {
  97. continue;
  98. }
  99. if (file_id.date > start_date) {
  100. continue;
  101. }
  102. if (file_id.date < end_date) {
  103. continue;
  104. }
  105. if (out_entries.size() >= SdAlbumFileLimit) {
  106. break;
  107. }
  108. const auto entry_size = Common::FS::GetSize(path);
  109. ApplicationAlbumEntry entry{
  110. .size = entry_size,
  111. .hash{},
  112. .datetime = file_id.date,
  113. .storage = file_id.storage,
  114. .content = content_type,
  115. .unknown = 1,
  116. };
  117. out_entries.push_back(entry);
  118. }
  119. return ResultSuccess;
  120. }
  121. Result AlbumManager::GetAutoSavingStorage(bool& out_is_autosaving) const {
  122. out_is_autosaving = false;
  123. return ResultSuccess;
  124. }
  125. Result AlbumManager::LoadAlbumScreenShotImage(LoadAlbumScreenShotImageOutput& out_image_output,
  126. std::vector<u8>& out_image,
  127. const AlbumFileId& file_id,
  128. const ScreenShotDecodeOption& decoder_options) const {
  129. if (file_id.storage > AlbumStorage::Sd) {
  130. return ResultInvalidStorage;
  131. }
  132. if (!is_mounted) {
  133. return ResultIsNotMounted;
  134. }
  135. out_image_output = {
  136. .width = 1280,
  137. .height = 720,
  138. .attribute =
  139. {
  140. .unknown_0{},
  141. .orientation = AlbumImageOrientation::None,
  142. .unknown_1{},
  143. .unknown_2{},
  144. },
  145. };
  146. std::filesystem::path path;
  147. const auto result = GetFile(path, file_id);
  148. if (result.IsError()) {
  149. return result;
  150. }
  151. out_image.resize(out_image_output.height * out_image_output.width * STBI_rgb_alpha);
  152. return LoadImage(out_image, path, static_cast<int>(out_image_output.width),
  153. +static_cast<int>(out_image_output.height), decoder_options.flags);
  154. }
  155. Result AlbumManager::LoadAlbumScreenShotThumbnail(
  156. LoadAlbumScreenShotImageOutput& out_image_output, std::vector<u8>& out_image,
  157. const AlbumFileId& file_id, const ScreenShotDecodeOption& decoder_options) const {
  158. if (file_id.storage > AlbumStorage::Sd) {
  159. return ResultInvalidStorage;
  160. }
  161. if (!is_mounted) {
  162. return ResultIsNotMounted;
  163. }
  164. out_image_output = {
  165. .width = 320,
  166. .height = 180,
  167. .attribute =
  168. {
  169. .unknown_0{},
  170. .orientation = AlbumImageOrientation::None,
  171. .unknown_1{},
  172. .unknown_2{},
  173. },
  174. };
  175. std::filesystem::path path;
  176. const auto result = GetFile(path, file_id);
  177. if (result.IsError()) {
  178. return result;
  179. }
  180. out_image.resize(out_image_output.height * out_image_output.width * STBI_rgb_alpha);
  181. return LoadImage(out_image, path, static_cast<int>(out_image_output.width),
  182. +static_cast<int>(out_image_output.height), decoder_options.flags);
  183. }
  184. Result AlbumManager::SaveScreenShot(ApplicationAlbumEntry& out_entry,
  185. const ScreenShotAttribute& attribute,
  186. AlbumReportOption report_option, std::span<const u8> image_data,
  187. u64 aruid) {
  188. return SaveScreenShot(out_entry, attribute, report_option, {}, image_data, aruid);
  189. }
  190. Result AlbumManager::SaveScreenShot(ApplicationAlbumEntry& out_entry,
  191. const ScreenShotAttribute& attribute,
  192. AlbumReportOption report_option,
  193. const ApplicationData& app_data, std::span<const u8> image_data,
  194. u64 aruid) {
  195. const u64 title_id = system.GetApplicationProcessProgramID();
  196. const auto& user_clock = system.GetTimeManager().GetStandardUserSystemClockCore();
  197. s64 posix_time{};
  198. Result result = user_clock.GetCurrentTime(system, posix_time);
  199. if (result.IsError()) {
  200. return result;
  201. }
  202. const auto date = ConvertToAlbumDateTime(posix_time);
  203. return SaveImage(out_entry, image_data, title_id, date);
  204. }
  205. Result AlbumManager::SaveEditedScreenShot(ApplicationAlbumEntry& out_entry,
  206. const ScreenShotAttribute& attribute,
  207. const AlbumFileId& file_id,
  208. std::span<const u8> image_data) {
  209. const auto& user_clock = system.GetTimeManager().GetStandardUserSystemClockCore();
  210. s64 posix_time{};
  211. Result result = user_clock.GetCurrentTime(system, posix_time);
  212. if (result.IsError()) {
  213. return result;
  214. }
  215. const auto date = ConvertToAlbumDateTime(posix_time);
  216. return SaveImage(out_entry, image_data, file_id.application_id, date);
  217. }
  218. Result AlbumManager::GetFile(std::filesystem::path& out_path, const AlbumFileId& file_id) const {
  219. const auto file = album_files.find(file_id);
  220. if (file == album_files.end()) {
  221. return ResultFileNotFound;
  222. }
  223. out_path = file->second;
  224. return ResultSuccess;
  225. }
  226. void AlbumManager::FindScreenshots() {
  227. is_mounted = false;
  228. album_files.clear();
  229. // TODO: Swap this with a blocking operation.
  230. const auto screenshots_dir = Common::FS::GetYuzuPath(Common::FS::YuzuPath::ScreenshotsDir);
  231. Common::FS::IterateDirEntries(
  232. screenshots_dir,
  233. [this](const std::filesystem::path& full_path) {
  234. AlbumEntry entry;
  235. if (GetAlbumEntry(entry, full_path).IsError()) {
  236. return true;
  237. }
  238. while (album_files.contains(entry.file_id)) {
  239. if (++entry.file_id.date.unique_id == 0) {
  240. break;
  241. }
  242. }
  243. album_files[entry.file_id] = full_path;
  244. return true;
  245. },
  246. Common::FS::DirEntryFilter::File);
  247. is_mounted = true;
  248. }
  249. Result AlbumManager::GetAlbumEntry(AlbumEntry& out_entry, const std::filesystem::path& path) const {
  250. std::istringstream line_stream(path.filename().string());
  251. std::string date;
  252. std::string application;
  253. std::string time;
  254. // Parse filename to obtain entry properties
  255. std::getline(line_stream, application, '_');
  256. std::getline(line_stream, date, '_');
  257. std::getline(line_stream, time, '_');
  258. std::istringstream date_stream(date);
  259. std::istringstream time_stream(time);
  260. std::string year;
  261. std::string month;
  262. std::string day;
  263. std::string hour;
  264. std::string minute;
  265. std::string second;
  266. std::getline(date_stream, year, '-');
  267. std::getline(date_stream, month, '-');
  268. std::getline(date_stream, day, '-');
  269. std::getline(time_stream, hour, '-');
  270. std::getline(time_stream, minute, '-');
  271. std::getline(time_stream, second, '-');
  272. try {
  273. out_entry = {
  274. .entry_size = 1,
  275. .file_id{
  276. .application_id = static_cast<u64>(std::stoll(application, 0, 16)),
  277. .date =
  278. {
  279. .year = static_cast<s16>(std::stoi(year)),
  280. .month = static_cast<s8>(std::stoi(month)),
  281. .day = static_cast<s8>(std::stoi(day)),
  282. .hour = static_cast<s8>(std::stoi(hour)),
  283. .minute = static_cast<s8>(std::stoi(minute)),
  284. .second = static_cast<s8>(std::stoi(second)),
  285. .unique_id = 0,
  286. },
  287. .storage = AlbumStorage::Sd,
  288. .type = ContentType::Screenshot,
  289. .unknown = 1,
  290. },
  291. };
  292. } catch (const std::invalid_argument&) {
  293. return ResultUnknown;
  294. } catch (const std::out_of_range&) {
  295. return ResultUnknown;
  296. } catch (const std::exception&) {
  297. return ResultUnknown;
  298. }
  299. return ResultSuccess;
  300. }
  301. Result AlbumManager::LoadImage(std::span<u8> out_image, const std::filesystem::path& path,
  302. int width, int height, ScreenShotDecoderFlag flag) const {
  303. if (out_image.size() != static_cast<std::size_t>(width * height * STBI_rgb_alpha)) {
  304. return ResultUnknown;
  305. }
  306. const Common::FS::IOFile db_file{path, Common::FS::FileAccessMode::Read,
  307. Common::FS::FileType::BinaryFile};
  308. std::vector<u8> raw_file(db_file.GetSize());
  309. if (db_file.Read(raw_file) != raw_file.size()) {
  310. return ResultUnknown;
  311. }
  312. int filter_flag = STBIR_FILTER_DEFAULT;
  313. int original_width, original_height, color_channels;
  314. const auto dbi_image =
  315. stbi_load_from_memory(raw_file.data(), static_cast<int>(raw_file.size()), &original_width,
  316. &original_height, &color_channels, STBI_rgb_alpha);
  317. if (dbi_image == nullptr) {
  318. return ResultUnknown;
  319. }
  320. switch (flag) {
  321. case ScreenShotDecoderFlag::EnableFancyUpsampling:
  322. filter_flag = STBIR_FILTER_TRIANGLE;
  323. break;
  324. case ScreenShotDecoderFlag::EnableBlockSmoothing:
  325. filter_flag = STBIR_FILTER_BOX;
  326. break;
  327. default:
  328. filter_flag = STBIR_FILTER_DEFAULT;
  329. break;
  330. }
  331. stbir_resize_uint8_srgb(dbi_image, original_width, original_height, 0, out_image.data(), width,
  332. height, 0, STBI_rgb_alpha, 3, filter_flag);
  333. return ResultSuccess;
  334. }
  335. void AlbumManager::FlipVerticallyOnWrite(bool flip) {
  336. stbi_flip_vertically_on_write(flip);
  337. }
  338. static void PNGToMemory(void* context, void* data, int len) {
  339. std::vector<u8>* png_image = static_cast<std::vector<u8>*>(context);
  340. unsigned char* png = static_cast<unsigned char*>(data);
  341. png_image->insert(png_image->end(), png, png + len);
  342. }
  343. Result AlbumManager::SaveImage(ApplicationAlbumEntry& out_entry, std::span<const u8> image,
  344. u64 title_id, const AlbumFileDateTime& date) const {
  345. const auto screenshot_path =
  346. Common::FS::GetYuzuPathString(Common::FS::YuzuPath::ScreenshotsDir);
  347. const std::string formatted_date =
  348. fmt::format("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}-{:03}", date.year, date.month, date.day,
  349. date.hour, date.minute, date.second, 0);
  350. const std::string file_path =
  351. fmt::format("{}/{:016x}_{}.png", screenshot_path, title_id, formatted_date);
  352. const Common::FS::IOFile db_file{file_path, Common::FS::FileAccessMode::Write,
  353. Common::FS::FileType::BinaryFile};
  354. std::vector<u8> png_image;
  355. if (!stbi_write_png_to_func(PNGToMemory, &png_image, 1280, 720, STBI_rgb_alpha, image.data(),
  356. 0)) {
  357. return ResultFileCountLimit;
  358. }
  359. if (db_file.Write(png_image) != png_image.size()) {
  360. return ResultFileCountLimit;
  361. }
  362. out_entry = {
  363. .size = png_image.size(),
  364. .hash = {},
  365. .datetime = date,
  366. .storage = AlbumStorage::Sd,
  367. .content = ContentType::Screenshot,
  368. .unknown = 1,
  369. };
  370. return ResultSuccess;
  371. }
  372. AlbumFileDateTime AlbumManager::ConvertToAlbumDateTime(u64 posix_time) const {
  373. Time::TimeZone::CalendarInfo calendar_date{};
  374. const auto& time_zone_manager =
  375. system.GetTimeManager().GetTimeZoneContentManager().GetTimeZoneManager();
  376. time_zone_manager.ToCalendarTimeWithMyRules(posix_time, calendar_date);
  377. return {
  378. .year = calendar_date.time.year,
  379. .month = calendar_date.time.month,
  380. .day = calendar_date.time.day,
  381. .hour = calendar_date.time.hour,
  382. .minute = calendar_date.time.minute,
  383. .second = calendar_date.time.second,
  384. .unique_id = 0,
  385. };
  386. }
  387. } // namespace Service::Capture