main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * main.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "main.h"
  12. #include "mainwindow_moc.h"
  13. #include "prepare.h"
  14. #include "../lib/VCMIDirs.h"
  15. #include <QApplication>
  16. // Conan workaround https://github.com/conan-io/conan-center-index/issues/13332
  17. #ifdef VCMI_IOS
  18. # if __has_include("QIOSIntegrationPlugin.h")
  19. # include "QIOSIntegrationPlugin.h"
  20. # endif
  21. int argcForClient;
  22. char ** argvForClient;
  23. #elif defined(VCMI_ANDROID)
  24. # include <QAndroidJniObject>
  25. # include <QtAndroid>
  26. #else
  27. # include <QMessageBox>
  28. # include <QProcess>
  29. #endif // VCMI_IOS
  30. // android must export main explicitly to make it visible in the shared library
  31. #ifdef VCMI_ANDROID
  32. # define MAIN_EXPORT ELF_VISIBILITY
  33. #else
  34. # define MAIN_EXPORT
  35. #endif // VCMI_ANDROID
  36. int MAIN_EXPORT main(int argc, char * argv[])
  37. {
  38. int result;
  39. #ifdef VCMI_IOS
  40. {
  41. #endif
  42. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  43. QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  44. #endif
  45. QApplication vcmilauncher(argc, argv);
  46. launcher::prepare();
  47. MainWindow mainWindow;
  48. mainWindow.show();
  49. #ifdef VCMI_ANDROID
  50. // changing language causes window to increase size over the bounds, force it back to proper value
  51. // TODO: check in Qt 6 if the hack is still needed
  52. auto appWindow = vcmilauncher.focusWindow();
  53. auto resizeWindowToScreen = [appWindow]{
  54. appWindow->resize(appWindow->screen()->availableSize());
  55. };
  56. QObject::connect(appWindow, &QWindow::widthChanged, resizeWindowToScreen);
  57. QObject::connect(appWindow, &QWindow::heightChanged, resizeWindowToScreen);
  58. #endif
  59. result = vcmilauncher.exec();
  60. #ifdef VCMI_IOS
  61. }
  62. if (result == 0)
  63. launchGame(argcForClient, argvForClient);
  64. #endif
  65. return result;
  66. }
  67. void startGame(const QStringList & args)
  68. {
  69. logGlobal->warn("Starting game with the arguments: %s", args.join(" ").toStdString());
  70. #ifdef VCMI_IOS
  71. static const char clientName[] = "vcmiclient";
  72. argcForClient = args.size() + 1; //first argument is omitted
  73. argvForClient = new char*[argcForClient];
  74. argvForClient[0] = new char[strlen(clientName)+1];
  75. strcpy(argvForClient[0], clientName);
  76. for(int i = 1; i < argcForClient; ++i)
  77. {
  78. std::string s = args.at(i - 1).toStdString();
  79. argvForClient[i] = new char[s.size() + 1];
  80. strcpy(argvForClient[i], s.c_str());
  81. }
  82. qApp->quit();
  83. #elif defined(VCMI_ANDROID)
  84. QtAndroid::androidActivity().callMethod<void>("onLaunchGameBtnPressed");
  85. #else
  86. startExecutable(pathToQString(VCMIDirs::get().clientPath()), args);
  87. #endif
  88. }
  89. void startEditor(const QStringList & args)
  90. {
  91. #ifdef ENABLE_EDITOR
  92. startExecutable(pathToQString(VCMIDirs::get().mapEditorPath()), args);
  93. #endif
  94. }
  95. #ifndef VCMI_MOBILE
  96. void startExecutable(QString name, const QStringList & args)
  97. {
  98. QProcess process;
  99. auto showError = [&] {
  100. QMessageBox::critical(qApp->activeWindow(),
  101. QObject::tr("Error starting executable"),
  102. QObject::tr("Failed to start %1\nReason: %2").arg(name, process.errorString()));
  103. };
  104. #if defined(VCMI_MAC) || defined(VCMI_WINDOWS)
  105. if(process.startDetached(name, args))
  106. {
  107. qApp->quit();
  108. }
  109. else
  110. {
  111. showError();
  112. }
  113. #else // Linux
  114. // Start vcmiclient and vcmieditor with QProcess::start() instead of QProcess::startDetached()
  115. // since startDetached() results in a missing terminal prompt after quitting vcmiclient.
  116. // QProcess::start() causes the launcher window to freeze while the child process is running, so we hide it in
  117. // MainWindow::on_startGameButton_clicked() and MainWindow::on_startEditorButton_clicked()
  118. process.setProcessChannelMode(QProcess::ForwardedChannels);
  119. process.start(name, args);
  120. process.waitForFinished(-1);
  121. if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
  122. showError();
  123. }
  124. qApp->quit();
  125. #endif
  126. }
  127. #endif