main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 WeakPtr<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. static Optional<String> last_opened_project_path();
  32. ErrorOr<int> serenity_main(Main::Arguments arguments)
  33. {
  34. TRY(Core::System::pledge("stdio recvfd sendfd tty rpath cpath wpath proc exec unix fattr thread ptrace"));
  35. auto app = TRY(GUI::Application::try_create(arguments));
  36. Config::pledge_domains({ "HackStudio", "Terminal" });
  37. auto window = GUI::Window::construct();
  38. window->resize(840, 600);
  39. window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png").release_value_but_fixme_should_propagate_errors());
  40. update_path_environment_variable();
  41. if (!make_is_available()) {
  42. notify_make_not_available();
  43. }
  44. char const* path_argument = nullptr;
  45. bool mode_coredump = false;
  46. Core::ArgsParser args_parser;
  47. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  48. args_parser.add_option(mode_coredump, "Debug a coredump in HackStudio", "coredump", 'c');
  49. args_parser.parse(arguments);
  50. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  51. auto project_path = Core::File::real_path_for(".");
  52. if (!mode_coredump) {
  53. if (!argument_absolute_path.is_null())
  54. project_path = argument_absolute_path;
  55. else if (auto path = last_opened_project_path(); path.has_value())
  56. project_path = path.release_value();
  57. }
  58. auto hack_studio_widget = TRY(window->try_set_main_widget<HackStudioWidget>(project_path));
  59. s_hack_studio_widget = hack_studio_widget;
  60. window->set_title(String::formatted("{} - Hack Studio", hack_studio_widget->project().name()));
  61. hack_studio_widget->initialize_menubar(*window);
  62. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  63. hack_studio_widget->locator().close();
  64. if (hack_studio_widget->warn_unsaved_changes("There are unsaved changes, do you want to save before exiting?") == HackStudioWidget::ContinueDecision::Yes)
  65. return GUI::Window::CloseRequestDecision::Close;
  66. return GUI::Window::CloseRequestDecision::StayOpen;
  67. };
  68. window->show();
  69. hack_studio_widget->update_actions();
  70. if (mode_coredump)
  71. hack_studio_widget->open_coredump(argument_absolute_path);
  72. return app->exec();
  73. }
  74. static bool make_is_available()
  75. {
  76. pid_t pid;
  77. char const* argv[] = { "make", "--version", nullptr };
  78. posix_spawn_file_actions_t action;
  79. posix_spawn_file_actions_init(&action);
  80. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  81. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  82. perror("posix_spawn");
  83. return false;
  84. }
  85. int wstatus;
  86. waitpid(pid, &wstatus, 0);
  87. posix_spawn_file_actions_destroy(&action);
  88. return WEXITSTATUS(wstatus) == 0;
  89. }
  90. static void notify_make_not_available()
  91. {
  92. auto notification = GUI::Notification::construct();
  93. notification->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/app-hack-studio.png").release_value_but_fixme_should_propagate_errors());
  94. notification->set_title("'make' Not Available");
  95. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  96. notification->show();
  97. }
  98. static void update_path_environment_variable()
  99. {
  100. StringBuilder path;
  101. auto const* path_env_ptr = getenv("PATH");
  102. if (path_env_ptr != NULL)
  103. path.append({ path_env_ptr, strlen(path_env_ptr) });
  104. if (path.length())
  105. path.append(":");
  106. path.append("/usr/local/sbin:/usr/local/bin:/usr/bin:/bin");
  107. setenv("PATH", path.to_string().characters(), true);
  108. }
  109. static Optional<String> last_opened_project_path()
  110. {
  111. auto projects = HackStudioWidget::read_recent_projects();
  112. if (projects.size() == 0)
  113. return {};
  114. if (!Core::File::exists(projects[0]))
  115. return {};
  116. return { projects[0] };
  117. }
  118. namespace HackStudio {
  119. GUI::TextEditor& current_editor()
  120. {
  121. return s_hack_studio_widget->current_editor();
  122. }
  123. void open_file(String const& filename)
  124. {
  125. s_hack_studio_widget->open_file(filename);
  126. }
  127. void open_file(String const& filename, size_t line, size_t column)
  128. {
  129. s_hack_studio_widget->open_file(filename, line, column);
  130. }
  131. RefPtr<EditorWrapper> current_editor_wrapper()
  132. {
  133. if (!s_hack_studio_widget)
  134. return nullptr;
  135. return s_hack_studio_widget->current_editor_wrapper();
  136. }
  137. Project& project()
  138. {
  139. return s_hack_studio_widget->project();
  140. }
  141. String currently_open_file()
  142. {
  143. if (!s_hack_studio_widget)
  144. return {};
  145. return s_hack_studio_widget->active_file();
  146. }
  147. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  148. {
  149. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  150. }
  151. Locator& locator()
  152. {
  153. return s_hack_studio_widget->locator();
  154. }
  155. void for_each_open_file(Function<void(ProjectFile const&)> func)
  156. {
  157. s_hack_studio_widget->for_each_open_file(move(func));
  158. }
  159. bool semantic_syntax_highlighting_is_enabled()
  160. {
  161. return s_hack_studio_widget->semantic_syntax_highlighting_is_enabled();
  162. }
  163. }