citra.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <iostream>
  5. #include <memory>
  6. #include <string>
  7. #include <thread>
  8. // This needs to be included before getopt.h because the latter #defines symbols used by it
  9. #include "common/microprofile.h"
  10. #ifdef _MSC_VER
  11. #include <getopt.h>
  12. #else
  13. #include <getopt.h>
  14. #include <unistd.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <Windows.h>
  18. #endif
  19. #include "citra/config.h"
  20. #include "citra/emu_window/emu_window_sdl2.h"
  21. #include "common/logging/backend.h"
  22. #include "common/logging/filter.h"
  23. #include "common/logging/log.h"
  24. #include "common/scm_rev.h"
  25. #include "common/scope_exit.h"
  26. #include "common/string_util.h"
  27. #include "core/core.h"
  28. #include "core/gdbstub/gdbstub.h"
  29. #include "core/loader/loader.h"
  30. #include "core/settings.h"
  31. #include "core/system.h"
  32. #include "video_core/video_core.h"
  33. static void PrintHelp(const char* argv0) {
  34. std::cout << "Usage: " << argv0
  35. << " [options] <filename>\n"
  36. "-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n"
  37. "-h, --help Display this help and exit\n"
  38. "-v, --version Output version information and exit\n";
  39. }
  40. static void PrintVersion() {
  41. std::cout << "Citra " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  42. }
  43. /// Application entry point
  44. int main(int argc, char** argv) {
  45. Config config;
  46. int option_index = 0;
  47. bool use_gdbstub = Settings::values.use_gdbstub;
  48. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  49. char* endarg;
  50. #ifdef _WIN32
  51. int argc_w;
  52. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  53. if (argv_w == nullptr) {
  54. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  55. return -1;
  56. }
  57. #endif
  58. std::string boot_filename;
  59. static struct option long_options[] = {
  60. {"gdbport", required_argument, 0, 'g'},
  61. {"help", no_argument, 0, 'h'},
  62. {"version", no_argument, 0, 'v'},
  63. {0, 0, 0, 0},
  64. };
  65. while (optind < argc) {
  66. char arg = getopt_long(argc, argv, "g:hv", long_options, &option_index);
  67. if (arg != -1) {
  68. switch (arg) {
  69. case 'g':
  70. errno = 0;
  71. gdb_port = strtoul(optarg, &endarg, 0);
  72. use_gdbstub = true;
  73. if (endarg == optarg)
  74. errno = EINVAL;
  75. if (errno != 0) {
  76. perror("--gdbport");
  77. exit(1);
  78. }
  79. break;
  80. case 'h':
  81. PrintHelp(argv[0]);
  82. return 0;
  83. case 'v':
  84. PrintVersion();
  85. return 0;
  86. }
  87. } else {
  88. #ifdef _WIN32
  89. boot_filename = Common::UTF16ToUTF8(argv_w[optind]);
  90. #else
  91. boot_filename = argv[optind];
  92. #endif
  93. optind++;
  94. }
  95. }
  96. #ifdef _WIN32
  97. LocalFree(argv_w);
  98. #endif
  99. Log::Filter log_filter(Log::Level::Debug);
  100. Log::SetFilter(&log_filter);
  101. MicroProfileOnThreadCreate("EmuThread");
  102. SCOPE_EXIT({ MicroProfileShutdown(); });
  103. if (boot_filename.empty()) {
  104. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  105. return -1;
  106. }
  107. log_filter.ParseFilterString(Settings::values.log_filter);
  108. // Apply the command line arguments
  109. Settings::values.gdbstub_port = gdb_port;
  110. Settings::values.use_gdbstub = use_gdbstub;
  111. Settings::Apply();
  112. std::unique_ptr<EmuWindow_SDL2> emu_window = std::make_unique<EmuWindow_SDL2>();
  113. System::Init(emu_window.get());
  114. SCOPE_EXIT({ System::Shutdown(); });
  115. std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(boot_filename);
  116. if (!loader) {
  117. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", boot_filename.c_str());
  118. return -1;
  119. }
  120. Loader::ResultStatus load_result = loader->Load();
  121. if (Loader::ResultStatus::Success != load_result) {
  122. LOG_CRITICAL(Frontend, "Failed to load ROM (Error %i)!", load_result);
  123. return -1;
  124. }
  125. while (emu_window->IsOpen()) {
  126. Core::RunLoop();
  127. }
  128. return 0;
  129. }