main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Editor.h"
  27. #include "HackStudio.h"
  28. #include "HackStudioWidget.h"
  29. #include "Project.h"
  30. #include <AK/StringBuilder.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCore/Event.h>
  33. #include <LibCore/EventLoop.h>
  34. #include <LibCore/File.h>
  35. #include <LibGUI/Application.h>
  36. #include <LibGUI/Menubar.h>
  37. #include <LibGUI/MessageBox.h>
  38. #include <LibGUI/Notification.h>
  39. #include <LibGUI/Widget.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibThread/Lock.h>
  42. #include <LibThread/Thread.h>
  43. #include <LibVT/TerminalWidget.h>
  44. #include <fcntl.h>
  45. #include <spawn.h>
  46. #include <stdio.h>
  47. #include <sys/types.h>
  48. #include <sys/wait.h>
  49. #include <unistd.h>
  50. using namespace HackStudio;
  51. static RefPtr<GUI::Window> s_window;
  52. static RefPtr<HackStudioWidget> s_hack_studio_widget;
  53. static bool make_is_available();
  54. static void notify_make_not_available();
  55. static void update_path_environment_variable();
  56. int main(int argc, char** argv)
  57. {
  58. if (pledge("stdio recvfd sendfd tty accept rpath cpath wpath proc exec unix fattr thread unix ptrace", nullptr) < 0) {
  59. perror("pledge");
  60. return 1;
  61. }
  62. auto app = GUI::Application::construct(argc, argv);
  63. if (pledge("stdio recvfd sendfd tty accept rpath cpath wpath proc exec fattr thread unix ptrace", nullptr) < 0) {
  64. perror("pledge");
  65. return 1;
  66. }
  67. s_window = GUI::Window::construct();
  68. s_window->resize(840, 600);
  69. s_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  70. update_path_environment_variable();
  71. if (!make_is_available()) {
  72. notify_make_not_available();
  73. }
  74. const char* path_argument = nullptr;
  75. Core::ArgsParser args_parser;
  76. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  77. args_parser.parse(argc, argv);
  78. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  79. auto project_path = argument_absolute_path;
  80. if (argument_absolute_path.is_null())
  81. project_path = Core::File::real_path_for(".");
  82. s_hack_studio_widget = s_window->set_main_widget<HackStudioWidget>(project_path);
  83. s_window->set_title(String::formatted("{} - Hack Studio", s_hack_studio_widget->project().name()));
  84. auto menubar = GUI::Menubar::construct();
  85. s_hack_studio_widget->initialize_menubar(menubar);
  86. s_window->set_menubar(menubar);
  87. s_window->show();
  88. s_hack_studio_widget->update_actions();
  89. return app->exec();
  90. }
  91. static bool make_is_available()
  92. {
  93. pid_t pid;
  94. const char* argv[] = { "make", "--version", nullptr };
  95. posix_spawn_file_actions_t action;
  96. posix_spawn_file_actions_init(&action);
  97. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  98. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  99. perror("posix_spawn");
  100. return false;
  101. }
  102. int wstatus;
  103. waitpid(pid, &wstatus, 0);
  104. posix_spawn_file_actions_destroy(&action);
  105. return WEXITSTATUS(wstatus) == 0;
  106. }
  107. static void notify_make_not_available()
  108. {
  109. auto notification = GUI::Notification::construct();
  110. notification->set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"));
  111. notification->set_title("'make' Not Available");
  112. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  113. notification->show();
  114. }
  115. static void update_path_environment_variable()
  116. {
  117. StringBuilder path;
  118. path.append(getenv("PATH"));
  119. if (path.length())
  120. path.append(":");
  121. path.append("/bin:/usr/bin:/usr/local/bin");
  122. setenv("PATH", path.to_string().characters(), true);
  123. }
  124. namespace HackStudio {
  125. GUI::TextEditor& current_editor()
  126. {
  127. return s_hack_studio_widget->current_editor();
  128. }
  129. void open_file(const String& file_name)
  130. {
  131. s_hack_studio_widget->open_file(file_name);
  132. }
  133. void open_file(const String& file_name, size_t line, size_t column)
  134. {
  135. s_hack_studio_widget->open_file(file_name);
  136. s_hack_studio_widget->current_editor_wrapper().editor().set_cursor({ line, column });
  137. }
  138. RefPtr<EditorWrapper> current_editor_wrapper()
  139. {
  140. if (!s_hack_studio_widget)
  141. return nullptr;
  142. return s_hack_studio_widget->current_editor_wrapper();
  143. }
  144. Project& project()
  145. {
  146. return s_hack_studio_widget->project();
  147. }
  148. String currently_open_file()
  149. {
  150. if (!s_hack_studio_widget)
  151. return {};
  152. return s_hack_studio_widget->currently_open_file();
  153. }
  154. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  155. {
  156. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  157. }
  158. Locator& locator()
  159. {
  160. return s_hack_studio_widget->locator();
  161. }
  162. }