summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar liamwhite2022-12-16 11:57:16 -0500
committerGravatar GitHub2022-12-16 11:57:16 -0500
commit9bfd4d880ecc332b230c9977654849d7521c93fb (patch)
treed12654b62b1da9a29cc878f0aa3c7f26e275ed70
parentMerge pull request #9431 from liamwhite/sixty-five-oh-two (diff)
parentmain: Address review feedback (diff)
downloadyuzu-9bfd4d880ecc332b230c9977654849d7521c93fb.tar.gz
yuzu-9bfd4d880ecc332b230c9977654849d7521c93fb.tar.xz
yuzu-9bfd4d880ecc332b230c9977654849d7521c93fb.zip
Merge pull request #6769 from lat9nq/create-shortcut-2
yuzu qt, common: Add option to create game shortcuts on Linux
-rw-r--r--src/yuzu/game_list.cpp14
-rw-r--r--src/yuzu/game_list.h7
-rw-r--r--src/yuzu/main.cpp181
-rw-r--r--src/yuzu/main.h7
-rw-r--r--src/yuzu/uisettings.h1
5 files changed, 210 insertions, 0 deletions
diff --git a/src/yuzu/game_list.cpp b/src/yuzu/game_list.cpp
index 5c33c1b0f..22aa19c56 100644
--- a/src/yuzu/game_list.cpp
+++ b/src/yuzu/game_list.cpp
@@ -554,6 +554,12 @@ void GameList::AddGamePopup(QMenu& context_menu, u64 program_id, const std::stri
554 QAction* dump_romfs_sdmc = dump_romfs_menu->addAction(tr("Dump RomFS to SDMC")); 554 QAction* dump_romfs_sdmc = dump_romfs_menu->addAction(tr("Dump RomFS to SDMC"));
555 QAction* copy_tid = context_menu.addAction(tr("Copy Title ID to Clipboard")); 555 QAction* copy_tid = context_menu.addAction(tr("Copy Title ID to Clipboard"));
556 QAction* navigate_to_gamedb_entry = context_menu.addAction(tr("Navigate to GameDB entry")); 556 QAction* navigate_to_gamedb_entry = context_menu.addAction(tr("Navigate to GameDB entry"));
557#ifndef WIN32
558 QMenu* shortcut_menu = context_menu.addMenu(tr("Create Shortcut"));
559 QAction* create_desktop_shortcut = shortcut_menu->addAction(tr("Add to Desktop"));
560 QAction* create_applications_menu_shortcut =
561 shortcut_menu->addAction(tr("Add to Applications Menu"));
562#endif
557 context_menu.addSeparator(); 563 context_menu.addSeparator();
558 QAction* properties = context_menu.addAction(tr("Properties")); 564 QAction* properties = context_menu.addAction(tr("Properties"));
559 565
@@ -619,6 +625,14 @@ void GameList::AddGamePopup(QMenu& context_menu, u64 program_id, const std::stri
619 connect(navigate_to_gamedb_entry, &QAction::triggered, [this, program_id]() { 625 connect(navigate_to_gamedb_entry, &QAction::triggered, [this, program_id]() {
620 emit NavigateToGamedbEntryRequested(program_id, compatibility_list); 626 emit NavigateToGamedbEntryRequested(program_id, compatibility_list);
621 }); 627 });
628#ifndef WIN32
629 connect(create_desktop_shortcut, &QAction::triggered, [this, program_id, path]() {
630 emit CreateShortcut(program_id, path, GameListShortcutTarget::Desktop);
631 });
632 connect(create_applications_menu_shortcut, &QAction::triggered, [this, program_id, path]() {
633 emit CreateShortcut(program_id, path, GameListShortcutTarget::Applications);
634 });
635#endif
622 connect(properties, &QAction::triggered, 636 connect(properties, &QAction::triggered,
623 [this, path]() { emit OpenPerGameGeneralRequested(path); }); 637 [this, path]() { emit OpenPerGameGeneralRequested(path); });
624}; 638};
diff --git a/src/yuzu/game_list.h b/src/yuzu/game_list.h
index cdf085019..f7ff93ed9 100644
--- a/src/yuzu/game_list.h
+++ b/src/yuzu/game_list.h
@@ -52,6 +52,11 @@ enum class DumpRomFSTarget {
52 SDMC, 52 SDMC,
53}; 53};
54 54
55enum class GameListShortcutTarget {
56 Desktop,
57 Applications,
58};
59
55enum class InstalledEntryType { 60enum class InstalledEntryType {
56 Game, 61 Game,
57 Update, 62 Update,
@@ -108,6 +113,8 @@ signals:
108 const std::string& game_path); 113 const std::string& game_path);
109 void DumpRomFSRequested(u64 program_id, const std::string& game_path, DumpRomFSTarget target); 114 void DumpRomFSRequested(u64 program_id, const std::string& game_path, DumpRomFSTarget target);
110 void CopyTIDRequested(u64 program_id); 115 void CopyTIDRequested(u64 program_id);
116 void CreateShortcut(u64 program_id, const std::string& game_path,
117 GameListShortcutTarget target);
111 void NavigateToGamedbEntryRequested(u64 program_id, 118 void NavigateToGamedbEntryRequested(u64 program_id,
112 const CompatibilityList& compatibility_list); 119 const CompatibilityList& compatibility_list);
113 void OpenPerGameGeneralRequested(const std::string& file); 120 void OpenPerGameGeneralRequested(const std::string& file);
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index 885e24990..df7166d4b 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -4,6 +4,8 @@
4#include <cinttypes> 4#include <cinttypes>
5#include <clocale> 5#include <clocale>
6#include <cmath> 6#include <cmath>
7#include <fstream>
8#include <iostream>
7#include <memory> 9#include <memory>
8#include <thread> 10#include <thread>
9#ifdef __APPLE__ 11#ifdef __APPLE__
@@ -1249,6 +1251,7 @@ void GMainWindow::ConnectWidgetEvents() {
1249 connect(game_list, &GameList::CopyTIDRequested, this, &GMainWindow::OnGameListCopyTID); 1251 connect(game_list, &GameList::CopyTIDRequested, this, &GMainWindow::OnGameListCopyTID);
1250 connect(game_list, &GameList::NavigateToGamedbEntryRequested, this, 1252 connect(game_list, &GameList::NavigateToGamedbEntryRequested, this,
1251 &GMainWindow::OnGameListNavigateToGamedbEntry); 1253 &GMainWindow::OnGameListNavigateToGamedbEntry);
1254 connect(game_list, &GameList::CreateShortcut, this, &GMainWindow::OnGameListCreateShortcut);
1252 connect(game_list, &GameList::AddDirectory, this, &GMainWindow::OnGameListAddDirectory); 1255 connect(game_list, &GameList::AddDirectory, this, &GMainWindow::OnGameListAddDirectory);
1253 connect(game_list_placeholder, &GameListPlaceholder::AddDirectory, this, 1256 connect(game_list_placeholder, &GameListPlaceholder::AddDirectory, this,
1254 &GMainWindow::OnGameListAddDirectory); 1257 &GMainWindow::OnGameListAddDirectory);
@@ -2378,6 +2381,152 @@ void GMainWindow::OnGameListNavigateToGamedbEntry(u64 program_id,
2378 QDesktopServices::openUrl(QUrl(QStringLiteral("https://yuzu-emu.org/game/") + directory)); 2381 QDesktopServices::openUrl(QUrl(QStringLiteral("https://yuzu-emu.org/game/") + directory));
2379} 2382}
2380 2383
2384void GMainWindow::OnGameListCreateShortcut(u64 program_id, const std::string& game_path,
2385 GameListShortcutTarget target) {
2386 // Get path to yuzu executable
2387 const QStringList args = QApplication::arguments();
2388 std::filesystem::path yuzu_command = args[0].toStdString();
2389
2390#if defined(__linux__) || defined(__FreeBSD__)
2391 // If relative path, make it an absolute path
2392 if (yuzu_command.c_str()[0] == '.') {
2393 yuzu_command = Common::FS::GetCurrentDir() / yuzu_command;
2394 }
2395
2396#if defined(__linux__)
2397 // Warn once if we are making a shortcut to a volatile AppImage
2398 const std::string appimage_ending =
2399 std::string(Common::g_scm_rev).substr(0, 9).append(".AppImage");
2400 if (yuzu_command.string().ends_with(appimage_ending) &&
2401 !UISettings::values.shortcut_already_warned) {
2402 if (QMessageBox::warning(this, tr("Create Shortcut"),
2403 tr("This will create a shortcut to the current AppImage. This may "
2404 "not work well if you update. Continue?"),
2405 QMessageBox::StandardButton::Ok |
2406 QMessageBox::StandardButton::Cancel) ==
2407 QMessageBox::StandardButton::Cancel) {
2408 return;
2409 }
2410 UISettings::values.shortcut_already_warned = true;
2411 }
2412#endif // __linux__
2413#endif // __linux__ || __FreeBSD__
2414
2415 std::filesystem::path target_directory{};
2416 // Determine target directory for shortcut
2417#if defined(__linux__) || defined(__FreeBSD__)
2418 const char* home = std::getenv("HOME");
2419 const std::filesystem::path home_path = (home == nullptr ? "~" : home);
2420 const char* xdg_data_home = std::getenv("XDG_DATA_HOME");
2421
2422 if (target == GameListShortcutTarget::Desktop) {
2423 target_directory = home_path / "Desktop";
2424 if (!Common::FS::IsDir(target_directory)) {
2425 QMessageBox::critical(
2426 this, tr("Create Shortcut"),
2427 tr("Cannot create shortcut on desktop. Path \"%1\" does not exist.")
2428 .arg(QString::fromStdString(target_directory)),
2429 QMessageBox::StandardButton::Ok);
2430 return;
2431 }
2432 } else if (target == GameListShortcutTarget::Applications) {
2433 target_directory = (xdg_data_home == nullptr ? home_path / ".local/share" : xdg_data_home) /
2434 "applications";
2435 if (!Common::FS::CreateDirs(target_directory)) {
2436 QMessageBox::critical(this, tr("Create Shortcut"),
2437 tr("Cannot create shortcut in applications menu. Path \"%1\" "
2438 "does not exist and cannot be created.")
2439 .arg(QString::fromStdString(target_directory)),
2440 QMessageBox::StandardButton::Ok);
2441 return;
2442 }
2443 }
2444#endif
2445
2446 const std::string game_file_name = std::filesystem::path(game_path).filename().string();
2447 // Determine full paths for icon and shortcut
2448#if defined(__linux__) || defined(__FreeBSD__)
2449 std::filesystem::path system_icons_path =
2450 (xdg_data_home == nullptr ? home_path / ".local/share/" : xdg_data_home) /
2451 "icons/hicolor/256x256";
2452 if (!Common::FS::CreateDirs(system_icons_path)) {
2453 QMessageBox::critical(
2454 this, tr("Create Icon"),
2455 tr("Cannot create icon file. Path \"%1\" does not exist and cannot be created.")
2456 .arg(QString::fromStdString(system_icons_path)),
2457 QMessageBox::StandardButton::Ok);
2458 return;
2459 }
2460 std::filesystem::path icon_path =
2461 system_icons_path / (program_id == 0 ? fmt::format("yuzu-{}.png", game_file_name)
2462 : fmt::format("yuzu-{:016X}.png", program_id));
2463 const std::filesystem::path shortcut_path =
2464 target_directory / (program_id == 0 ? fmt::format("yuzu-{}.desktop", game_file_name)
2465 : fmt::format("yuzu-{:016X}.desktop", program_id));
2466#else
2467 const std::filesystem::path icon_path{};
2468 const std::filesystem::path shortcut_path{};
2469#endif
2470
2471 // Get title from game file
2472 const FileSys::PatchManager pm{program_id, system->GetFileSystemController(),
2473 system->GetContentProvider()};
2474 const auto control = pm.GetControlMetadata();
2475 const auto loader = Loader::GetLoader(*system, vfs->OpenFile(game_path, FileSys::Mode::Read));
2476
2477 std::string title{fmt::format("{:016X}", program_id)};
2478
2479 if (control.first != nullptr) {
2480 title = control.first->GetApplicationName();
2481 } else {
2482 loader->ReadTitle(title);
2483 }
2484
2485 // Get icon from game file
2486 std::vector<u8> icon_image_file{};
2487 if (control.second != nullptr) {
2488 icon_image_file = control.second->ReadAllBytes();
2489 } else if (loader->ReadIcon(icon_image_file) != Loader::ResultStatus::Success) {
2490 LOG_WARNING(Frontend, "Could not read icon from {:s}", game_path);
2491 }
2492
2493 QImage icon_jpeg =
2494 QImage::fromData(icon_image_file.data(), static_cast<int>(icon_image_file.size()));
2495#if defined(__linux__) || defined(__FreeBSD__)
2496 // Convert and write the icon as a PNG
2497 if (!icon_jpeg.save(QString::fromStdString(icon_path.string()))) {
2498 LOG_ERROR(Frontend, "Could not write icon as PNG to file");
2499 } else {
2500 LOG_INFO(Frontend, "Wrote an icon to {}", icon_path.string());
2501 }
2502#endif // __linux__
2503
2504#if defined(__linux__) || defined(__FreeBSD__)
2505 const std::string comment =
2506 tr("Start %1 with the yuzu Emulator").arg(QString::fromStdString(title)).toStdString();
2507 const std::string arguments = fmt::format("-g \"{:s}\"", game_path);
2508 const std::string categories = "Game;Emulator;Qt;";
2509 const std::string keywords = "Switch;Nintendo;";
2510#else
2511 const std::string comment{};
2512 const std::string arguments{};
2513 const std::string categories{};
2514 const std::string keywords{};
2515#endif
2516 if (!CreateShortcut(shortcut_path.string(), title, comment, icon_path.string(),
2517 yuzu_command.string(), arguments, categories, keywords)) {
2518 QMessageBox::critical(this, tr("Create Shortcut"),
2519 tr("Failed to create a shortcut at %1")
2520 .arg(QString::fromStdString(shortcut_path.string())));
2521 return;
2522 }
2523
2524 LOG_INFO(Frontend, "Wrote a shortcut to {}", shortcut_path.string());
2525 QMessageBox::information(
2526 this, tr("Create Shortcut"),
2527 tr("Successfully created a shortcut to %1").arg(QString::fromStdString(title)));
2528}
2529
2381void GMainWindow::OnGameListOpenDirectory(const QString& directory) { 2530void GMainWindow::OnGameListOpenDirectory(const QString& directory) {
2382 std::filesystem::path fs_path; 2531 std::filesystem::path fs_path;
2383 if (directory == QStringLiteral("SDMC")) { 2532 if (directory == QStringLiteral("SDMC")) {
@@ -3301,6 +3450,38 @@ void GMainWindow::OpenPerGameConfiguration(u64 title_id, const std::string& file
3301 } 3450 }
3302} 3451}
3303 3452
3453bool GMainWindow::CreateShortcut(const std::string& shortcut_path, const std::string& title,
3454 const std::string& comment, const std::string& icon_path,
3455 const std::string& command, const std::string& arguments,
3456 const std::string& categories, const std::string& keywords) {
3457#if defined(__linux__) || defined(__FreeBSD__)
3458 // This desktop file template was writting referencing
3459 // https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html
3460 std::string shortcut_contents{};
3461 shortcut_contents.append("[Desktop Entry]\n");
3462 shortcut_contents.append("Type=Application\n");
3463 shortcut_contents.append("Version=1.0\n");
3464 shortcut_contents.append(fmt::format("Name={:s}\n", title));
3465 shortcut_contents.append(fmt::format("Comment={:s}\n", comment));
3466 shortcut_contents.append(fmt::format("Icon={:s}\n", icon_path));
3467 shortcut_contents.append(fmt::format("TryExec={:s}\n", command));
3468 shortcut_contents.append(fmt::format("Exec={:s} {:s}\n", command, arguments));
3469 shortcut_contents.append(fmt::format("Categories={:s}\n", categories));
3470 shortcut_contents.append(fmt::format("Keywords={:s}\n", keywords));
3471
3472 std::ofstream shortcut_stream(shortcut_path);
3473 if (!shortcut_stream.is_open()) {
3474 LOG_WARNING(Common, "Failed to create file {:s}", shortcut_path);
3475 return false;
3476 }
3477 shortcut_stream << shortcut_contents;
3478 shortcut_stream.close();
3479
3480 return true;
3481#endif
3482 return false;
3483}
3484
3304void GMainWindow::OnLoadAmiibo() { 3485void GMainWindow::OnLoadAmiibo() {
3305 if (emu_thread == nullptr || !emu_thread->IsRunning()) { 3486 if (emu_thread == nullptr || !emu_thread->IsRunning()) {
3306 return; 3487 return;
diff --git a/src/yuzu/main.h b/src/yuzu/main.h
index 27644fae5..1047ba276 100644
--- a/src/yuzu/main.h
+++ b/src/yuzu/main.h
@@ -38,6 +38,7 @@ class QProgressDialog;
38class WaitTreeWidget; 38class WaitTreeWidget;
39enum class GameListOpenTarget; 39enum class GameListOpenTarget;
40enum class GameListRemoveTarget; 40enum class GameListRemoveTarget;
41enum class GameListShortcutTarget;
41enum class DumpRomFSTarget; 42enum class DumpRomFSTarget;
42enum class InstalledEntryType; 43enum class InstalledEntryType;
43class GameListPlaceholder; 44class GameListPlaceholder;
@@ -293,6 +294,8 @@ private slots:
293 void OnGameListCopyTID(u64 program_id); 294 void OnGameListCopyTID(u64 program_id);
294 void OnGameListNavigateToGamedbEntry(u64 program_id, 295 void OnGameListNavigateToGamedbEntry(u64 program_id,
295 const CompatibilityList& compatibility_list); 296 const CompatibilityList& compatibility_list);
297 void OnGameListCreateShortcut(u64 program_id, const std::string& game_path,
298 GameListShortcutTarget target);
296 void OnGameListOpenDirectory(const QString& directory); 299 void OnGameListOpenDirectory(const QString& directory);
297 void OnGameListAddDirectory(); 300 void OnGameListAddDirectory();
298 void OnGameListShowList(bool show); 301 void OnGameListShowList(bool show);
@@ -366,6 +369,10 @@ private:
366 bool CheckDarkMode(); 369 bool CheckDarkMode();
367 370
368 QString GetTasStateDescription() const; 371 QString GetTasStateDescription() const;
372 bool CreateShortcut(const std::string& shortcut_path, const std::string& title,
373 const std::string& comment, const std::string& icon_path,
374 const std::string& command, const std::string& arguments,
375 const std::string& categories, const std::string& keywords);
369 376
370 std::unique_ptr<Ui::MainWindow> ui; 377 std::unique_ptr<Ui::MainWindow> ui;
371 378
diff --git a/src/yuzu/uisettings.h b/src/yuzu/uisettings.h
index 452038cd9..2006b883e 100644
--- a/src/yuzu/uisettings.h
+++ b/src/yuzu/uisettings.h
@@ -138,6 +138,7 @@ struct Values {
138 138
139 bool configuration_applied; 139 bool configuration_applied;
140 bool reset_to_defaults; 140 bool reset_to_defaults;
141 bool shortcut_already_warned{false};
141 Settings::Setting<bool> disable_web_applet{true, "disable_web_applet"}; 142 Settings::Setting<bool> disable_web_applet{true, "disable_web_applet"};
142}; 143};
143 144