main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Editor.h"
  7. #include "HackStudio.h"
  8. #include "HackStudioWidget.h"
  9. #include "Project.h"
  10. #include <AK/StringBuilder.h>
  11. #include <LibConfig/Client.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/System.h>
  15. #include <LibGUI/Application.h>
  16. #include <LibGUI/Menubar.h>
  17. #include <LibGUI/Notification.h>
  18. #include <LibGUI/Window.h>
  19. #include <LibMain/Main.h>
  20. #include <fcntl.h>
  21. #include <spawn.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. using namespace HackStudio;
  27. static RefPtr<HackStudioWidget> s_hack_studio_widget;
  28. static bool make_is_available();
  29. static void notify_make_not_available();
  30. static void update_path_environment_variable();
  31. ErrorOr<int> serenity_main(Main::Arguments arguments)
  32. {
  33. TRY(Core::System::pledge("stdio recvfd sendfd tty rpath cpath wpath proc exec unix fattr thread ptrace"));
  34. auto app = GUI::Application::construct(arguments.argc, arguments.argv);
  35. Config::pledge_domains({ "HackStudio", "Terminal" });
  36. auto window = GUI::Window::construct();
  37. window->resize(840, 600);
  38. window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png").release_value_but_fixme_should_propagate_errors());
  39. update_path_environment_variable();
  40. if (!make_is_available()) {
  41. notify_make_not_available();
  42. }
  43. const char* path_argument = nullptr;
  44. bool mode_coredump = false;
  45. Core::ArgsParser args_parser;
  46. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  47. args_parser.add_option(mode_coredump, "Debug a coredump in HackStudio", "coredump", 'c');
  48. args_parser.parse(arguments);
  49. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  50. auto project_path = argument_absolute_path;
  51. if (argument_absolute_path.is_null() || mode_coredump)
  52. project_path = Core::File::real_path_for(".");
  53. s_hack_studio_widget = TRY(window->try_set_main_widget<HackStudioWidget>(project_path));
  54. window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name()));
  55. s_hack_studio_widget->initialize_menubar(*window);
  56. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  57. s_hack_studio_widget->locator().close();
  58. if (s_hack_studio_widget->warn_unsaved_changes("There are unsaved changes, do you want to save before exiting?") == HackStudioWidget::ContinueDecision::Yes)
  59. return GUI::Window::CloseRequestDecision::Close;
  60. return GUI::Window::CloseRequestDecision::StayOpen;
  61. };
  62. window->show();
  63. s_hack_studio_widget->update_actions();
  64. if (mode_coredump)
  65. s_hack_studio_widget->open_coredump(argument_absolute_path);
  66. return app->exec();
  67. }
  68. static bool make_is_available()
  69. {
  70. pid_t pid;
  71. const char* argv[] = { "make", "--version", nullptr };
  72. posix_spawn_file_actions_t action;
  73. posix_spawn_file_actions_init(&action);
  74. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  75. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  76. perror("posix_spawn");
  77. return false;
  78. }
  79. int wstatus;
  80. waitpid(pid, &wstatus, 0);
  81. posix_spawn_file_actions_destroy(&action);
  82. return WEXITSTATUS(wstatus) == 0;
  83. }
  84. static void notify_make_not_available()
  85. {
  86. auto notification = GUI::Notification::construct();
  87. notification->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/app-hack-studio.png").release_value_but_fixme_should_propagate_errors());
  88. notification->set_title("'make' Not Available");
  89. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  90. notification->show();
  91. }
  92. static void update_path_environment_variable()
  93. {
  94. StringBuilder path;
  95. path.append(getenv("PATH"));
  96. if (path.length())
  97. path.append(":");
  98. path.append("/usr/local/bin:/usr/bin:/bin");
  99. setenv("PATH", path.to_string().characters(), true);
  100. }
  101. namespace HackStudio {
  102. GUI::TextEditor& current_editor()
  103. {
  104. return s_hack_studio_widget->current_editor();
  105. }
  106. void open_file(const String& filename)
  107. {
  108. s_hack_studio_widget->open_file(filename);
  109. }
  110. void open_file(const String& filename, size_t line, size_t column)
  111. {
  112. s_hack_studio_widget->open_file(filename, line, column);
  113. }
  114. RefPtr<EditorWrapper> current_editor_wrapper()
  115. {
  116. if (!s_hack_studio_widget)
  117. return nullptr;
  118. return s_hack_studio_widget->current_editor_wrapper();
  119. }
  120. Project& project()
  121. {
  122. return s_hack_studio_widget->project();
  123. }
  124. String currently_open_file()
  125. {
  126. if (!s_hack_studio_widget)
  127. return {};
  128. return s_hack_studio_widget->active_file();
  129. }
  130. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  131. {
  132. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  133. }
  134. Locator& locator()
  135. {
  136. return s_hack_studio_widget->locator();
  137. }
  138. void for_each_open_file(Function<void(ProjectFile const&)> func)
  139. {
  140. s_hack_studio_widget->for_each_open_file(move(func));
  141. }
  142. }