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 <LibCore/ArgsParser.h>
  12. #include <LibCore/File.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/Menubar.h>
  15. #include <LibGUI/Notification.h>
  16. #include <LibGUI/Window.h>
  17. #include <fcntl.h>
  18. #include <spawn.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <unistd.h>
  23. using namespace HackStudio;
  24. static RefPtr<GUI::Window> s_window;
  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. s_window = GUI::Window::construct();
  37. s_window->resize(840, 600);
  38. s_window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png"));
  39. update_path_environment_variable();
  40. if (!make_is_available()) {
  41. notify_make_not_available();
  42. }
  43. const char* path_argument = nullptr;
  44. Core::ArgsParser args_parser;
  45. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  46. args_parser.parse(argc, argv);
  47. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  48. auto project_path = argument_absolute_path;
  49. if (argument_absolute_path.is_null())
  50. project_path = Core::File::real_path_for(".");
  51. s_hack_studio_widget = s_window->set_main_widget<HackStudioWidget>(project_path);
  52. s_window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name()));
  53. s_hack_studio_widget->initialize_menubar(*s_window);
  54. s_window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  55. s_hack_studio_widget->locator().close();
  56. if (s_hack_studio_widget->warn_unsaved_changes("There are unsaved changes, do you want to save before exiting?") == HackStudioWidget::ContinueDecision::Yes)
  57. return GUI::Window::CloseRequestDecision::Close;
  58. return GUI::Window::CloseRequestDecision::StayOpen;
  59. };
  60. s_window->show();
  61. s_hack_studio_widget->update_actions();
  62. return app->exec();
  63. }
  64. static bool make_is_available()
  65. {
  66. pid_t pid;
  67. const char* argv[] = { "make", "--version", nullptr };
  68. posix_spawn_file_actions_t action;
  69. posix_spawn_file_actions_init(&action);
  70. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  71. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  72. perror("posix_spawn");
  73. return false;
  74. }
  75. int wstatus;
  76. waitpid(pid, &wstatus, 0);
  77. posix_spawn_file_actions_destroy(&action);
  78. return WEXITSTATUS(wstatus) == 0;
  79. }
  80. static void notify_make_not_available()
  81. {
  82. auto notification = GUI::Notification::construct();
  83. notification->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/app-hack-studio.png"));
  84. notification->set_title("'make' Not Available");
  85. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  86. notification->show();
  87. }
  88. static void update_path_environment_variable()
  89. {
  90. StringBuilder path;
  91. path.append(getenv("PATH"));
  92. if (path.length())
  93. path.append(":");
  94. path.append("/bin:/usr/bin:/usr/local/bin");
  95. setenv("PATH", path.to_string().characters(), true);
  96. }
  97. namespace HackStudio {
  98. GUI::TextEditor& current_editor()
  99. {
  100. return s_hack_studio_widget->current_editor();
  101. }
  102. void open_file(const String& filename)
  103. {
  104. s_hack_studio_widget->open_file(filename);
  105. }
  106. void open_file(const String& filename, size_t line, size_t column)
  107. {
  108. s_hack_studio_widget->open_file(filename);
  109. s_hack_studio_widget->current_editor_wrapper().editor().set_cursor({ 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. }