main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <LibGUI/Application.h>
  15. #include <LibGUI/Menubar.h>
  16. #include <LibGUI/Notification.h>
  17. #include <LibGUI/Window.h>
  18. #include <fcntl.h>
  19. #include <spawn.h>
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <unistd.h>
  24. using namespace HackStudio;
  25. static RefPtr<HackStudioWidget> s_hack_studio_widget;
  26. static bool make_is_available();
  27. static void notify_make_not_available();
  28. static void update_path_environment_variable();
  29. int main(int argc, char** argv)
  30. {
  31. if (pledge("stdio recvfd sendfd tty rpath cpath wpath proc exec unix fattr thread ptrace", nullptr) < 0) {
  32. perror("pledge");
  33. return 1;
  34. }
  35. auto app = GUI::Application::construct(argc, argv);
  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. const char* path_argument = nullptr;
  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.parse(argc, argv);
  48. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  49. auto project_path = argument_absolute_path;
  50. if (argument_absolute_path.is_null())
  51. project_path = Core::File::real_path_for(".");
  52. s_hack_studio_widget = window->set_main_widget<HackStudioWidget>(project_path);
  53. window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name()));
  54. s_hack_studio_widget->initialize_menubar(*window);
  55. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  56. s_hack_studio_widget->locator().close();
  57. if (s_hack_studio_widget->warn_unsaved_changes("There are unsaved changes, do you want to save before exiting?") == HackStudioWidget::ContinueDecision::Yes)
  58. return GUI::Window::CloseRequestDecision::Close;
  59. return GUI::Window::CloseRequestDecision::StayOpen;
  60. };
  61. window->show();
  62. s_hack_studio_widget->update_actions();
  63. return app->exec();
  64. }
  65. static bool make_is_available()
  66. {
  67. pid_t pid;
  68. const char* argv[] = { "make", "--version", nullptr };
  69. posix_spawn_file_actions_t action;
  70. posix_spawn_file_actions_init(&action);
  71. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  72. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  73. perror("posix_spawn");
  74. return false;
  75. }
  76. int wstatus;
  77. waitpid(pid, &wstatus, 0);
  78. posix_spawn_file_actions_destroy(&action);
  79. return WEXITSTATUS(wstatus) == 0;
  80. }
  81. static void notify_make_not_available()
  82. {
  83. auto notification = GUI::Notification::construct();
  84. notification->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/app-hack-studio.png").release_value_but_fixme_should_propagate_errors());
  85. notification->set_title("'make' Not Available");
  86. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  87. notification->show();
  88. }
  89. static void update_path_environment_variable()
  90. {
  91. StringBuilder path;
  92. path.append(getenv("PATH"));
  93. if (path.length())
  94. path.append(":");
  95. path.append("/usr/local/bin:/usr/bin:/bin");
  96. setenv("PATH", path.to_string().characters(), true);
  97. }
  98. namespace HackStudio {
  99. GUI::TextEditor& current_editor()
  100. {
  101. return s_hack_studio_widget->current_editor();
  102. }
  103. void open_file(const String& filename)
  104. {
  105. s_hack_studio_widget->open_file(filename);
  106. }
  107. void open_file(const String& filename, size_t line, size_t column)
  108. {
  109. s_hack_studio_widget->open_file(filename, line, column);
  110. }
  111. RefPtr<EditorWrapper> current_editor_wrapper()
  112. {
  113. if (!s_hack_studio_widget)
  114. return nullptr;
  115. return s_hack_studio_widget->current_editor_wrapper();
  116. }
  117. Project& project()
  118. {
  119. return s_hack_studio_widget->project();
  120. }
  121. String currently_open_file()
  122. {
  123. if (!s_hack_studio_widget)
  124. return {};
  125. return s_hack_studio_widget->active_file();
  126. }
  127. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  128. {
  129. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  130. }
  131. Locator& locator()
  132. {
  133. return s_hack_studio_widget->locator();
  134. }
  135. }