main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 accept rpath cpath wpath proc exec unix fattr thread unix ptrace", nullptr) < 0) {
  39. perror("pledge");
  40. return 1;
  41. }
  42. auto app = GUI::Application::construct(argc, argv);
  43. if (pledge("stdio recvfd sendfd tty accept rpath cpath wpath proc exec fattr thread unix ptrace", nullptr) < 0) {
  44. perror("pledge");
  45. return 1;
  46. }
  47. s_window = GUI::Window::construct();
  48. s_window->resize(840, 600);
  49. s_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  50. update_path_environment_variable();
  51. if (!make_is_available()) {
  52. notify_make_not_available();
  53. }
  54. const char* path_argument = nullptr;
  55. Core::ArgsParser args_parser;
  56. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  57. args_parser.parse(argc, argv);
  58. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  59. auto project_path = argument_absolute_path;
  60. if (argument_absolute_path.is_null())
  61. project_path = Core::File::real_path_for(".");
  62. s_hack_studio_widget = s_window->set_main_widget<HackStudioWidget>(project_path);
  63. s_window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name()));
  64. auto menubar = GUI::Menubar::construct();
  65. s_hack_studio_widget->initialize_menubar(menubar);
  66. s_window->set_menubar(menubar);
  67. s_window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  68. if (s_hack_studio_widget->warn_unsaved_changes("There are unsaved changes, do you want to save before exiting?") == HackStudioWidget::ContinueDecision::Yes)
  69. return GUI::Window::CloseRequestDecision::Close;
  70. return GUI::Window::CloseRequestDecision::StayOpen;
  71. };
  72. s_window->show();
  73. s_hack_studio_widget->update_actions();
  74. return app->exec();
  75. }
  76. static bool make_is_available()
  77. {
  78. pid_t pid;
  79. const char* argv[] = { "make", "--version", nullptr };
  80. posix_spawn_file_actions_t action;
  81. posix_spawn_file_actions_init(&action);
  82. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  83. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  84. perror("posix_spawn");
  85. return false;
  86. }
  87. int wstatus;
  88. waitpid(pid, &wstatus, 0);
  89. posix_spawn_file_actions_destroy(&action);
  90. return WEXITSTATUS(wstatus) == 0;
  91. }
  92. static void notify_make_not_available()
  93. {
  94. auto notification = GUI::Notification::construct();
  95. notification->set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"));
  96. notification->set_title("'make' Not Available");
  97. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  98. notification->show();
  99. }
  100. static void update_path_environment_variable()
  101. {
  102. StringBuilder path;
  103. path.append(getenv("PATH"));
  104. if (path.length())
  105. path.append(":");
  106. path.append("/bin:/usr/bin:/usr/local/bin");
  107. setenv("PATH", path.to_string().characters(), true);
  108. }
  109. namespace HackStudio {
  110. GUI::TextEditor& current_editor()
  111. {
  112. return s_hack_studio_widget->current_editor();
  113. }
  114. void open_file(const String& filename)
  115. {
  116. s_hack_studio_widget->open_file(filename);
  117. }
  118. void open_file(const String& filename, size_t line, size_t column)
  119. {
  120. s_hack_studio_widget->open_file(filename);
  121. s_hack_studio_widget->current_editor_wrapper().editor().set_cursor({ line, column });
  122. }
  123. RefPtr<EditorWrapper> current_editor_wrapper()
  124. {
  125. if (!s_hack_studio_widget)
  126. return nullptr;
  127. return s_hack_studio_widget->current_editor_wrapper();
  128. }
  129. Project& project()
  130. {
  131. return s_hack_studio_widget->project();
  132. }
  133. String currently_open_file()
  134. {
  135. if (!s_hack_studio_widget)
  136. return {};
  137. return s_hack_studio_widget->active_file();
  138. }
  139. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  140. {
  141. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  142. }
  143. Locator& locator()
  144. {
  145. return s_hack_studio_widget->locator();
  146. }
  147. }