main.cpp 4.9 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 <LibCore/ArgsParser.h>
  12. #include <LibCore/Event.h>
  13. #include <LibCore/EventLoop.h>
  14. #include <LibCore/File.h>
  15. #include <LibGUI/Application.h>
  16. #include <LibGUI/Menubar.h>
  17. #include <LibGUI/MessageBox.h>
  18. #include <LibGUI/Notification.h>
  19. #include <LibGUI/Widget.h>
  20. #include <LibGUI/Window.h>
  21. #include <LibThread/Lock.h>
  22. #include <LibThread/Thread.h>
  23. #include <LibVT/TerminalWidget.h>
  24. #include <fcntl.h>
  25. #include <spawn.h>
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <unistd.h>
  30. using namespace HackStudio;
  31. static RefPtr<GUI::Window> s_window;
  32. static RefPtr<HackStudioWidget> s_hack_studio_widget;
  33. static bool make_is_available();
  34. static void notify_make_not_available();
  35. static void update_path_environment_variable();
  36. int main(int argc, char** argv)
  37. {
  38. if (pledge("stdio recvfd sendfd tty rpath cpath wpath proc exec unix thread ptrace", nullptr) < 0) {
  39. perror("pledge");
  40. return 1;
  41. }
  42. auto app = GUI::Application::construct(argc, argv);
  43. s_window = GUI::Window::construct();
  44. s_window->resize(840, 600);
  45. s_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  46. update_path_environment_variable();
  47. if (!make_is_available()) {
  48. notify_make_not_available();
  49. }
  50. const char* path_argument = nullptr;
  51. Core::ArgsParser args_parser;
  52. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  53. args_parser.parse(argc, argv);
  54. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  55. auto project_path = argument_absolute_path;
  56. if (argument_absolute_path.is_null())
  57. project_path = Core::File::real_path_for(".");
  58. s_hack_studio_widget = s_window->set_main_widget<HackStudioWidget>(project_path);
  59. s_window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name()));
  60. auto menubar = GUI::Menubar::construct();
  61. s_hack_studio_widget->initialize_menubar(menubar);
  62. s_window->set_menubar(menubar);
  63. s_window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  64. if (s_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. s_window->show();
  69. s_hack_studio_widget->update_actions();
  70. return app->exec();
  71. }
  72. static bool make_is_available()
  73. {
  74. pid_t pid;
  75. const char* argv[] = { "make", "--version", nullptr };
  76. posix_spawn_file_actions_t action;
  77. posix_spawn_file_actions_init(&action);
  78. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  79. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  80. perror("posix_spawn");
  81. return false;
  82. }
  83. int wstatus;
  84. waitpid(pid, &wstatus, 0);
  85. posix_spawn_file_actions_destroy(&action);
  86. return WEXITSTATUS(wstatus) == 0;
  87. }
  88. static void notify_make_not_available()
  89. {
  90. auto notification = GUI::Notification::construct();
  91. notification->set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"));
  92. notification->set_title("'make' Not Available");
  93. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  94. notification->show();
  95. }
  96. static void update_path_environment_variable()
  97. {
  98. StringBuilder path;
  99. path.append(getenv("PATH"));
  100. if (path.length())
  101. path.append(":");
  102. path.append("/bin:/usr/bin:/usr/local/bin");
  103. setenv("PATH", path.to_string().characters(), true);
  104. }
  105. namespace HackStudio {
  106. GUI::TextEditor& current_editor()
  107. {
  108. return s_hack_studio_widget->current_editor();
  109. }
  110. void open_file(const String& filename)
  111. {
  112. s_hack_studio_widget->open_file(filename);
  113. }
  114. void open_file(const String& filename, size_t line, size_t column)
  115. {
  116. s_hack_studio_widget->open_file(filename);
  117. s_hack_studio_widget->current_editor_wrapper().editor().set_cursor({ line, column });
  118. }
  119. RefPtr<EditorWrapper> current_editor_wrapper()
  120. {
  121. if (!s_hack_studio_widget)
  122. return nullptr;
  123. return s_hack_studio_widget->current_editor_wrapper();
  124. }
  125. Project& project()
  126. {
  127. return s_hack_studio_widget->project();
  128. }
  129. String currently_open_file()
  130. {
  131. if (!s_hack_studio_widget)
  132. return {};
  133. return s_hack_studio_widget->active_file();
  134. }
  135. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  136. {
  137. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  138. }
  139. Locator& locator()
  140. {
  141. return s_hack_studio_widget->locator();
  142. }
  143. }