main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "HackStudio.h"
  27. #include "HackStudioWidget.h"
  28. #include "LanguageClients/Cpp/ServerConnection.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/Widget.h>
  39. #include <LibGUI/Window.h>
  40. #include <LibThread/Lock.h>
  41. #include <LibThread/Thread.h>
  42. #include <LibVT/TerminalWidget.h>
  43. #include <fcntl.h>
  44. #include <spawn.h>
  45. #include <stdio.h>
  46. #include <sys/types.h>
  47. #include <sys/wait.h>
  48. #include <unistd.h>
  49. using namespace HackStudio;
  50. static RefPtr<GUI::Window> s_window;
  51. static RefPtr<HackStudioWidget> s_hack_studio_widget;
  52. static RefPtr<LanguageClients::Cpp::ServerConnection> s_cpp_Language_server_connection;
  53. static bool make_is_available();
  54. static void update_path_environment_variable();
  55. static String path_to_project(const String& path_argument_absolute_path);
  56. static void open_default_project_file(const String& project_path);
  57. static void initialize_connections_to_language_servers(const String& project_path);
  58. int main(int argc, char** argv)
  59. {
  60. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec unix fattr thread unix", nullptr) < 0) {
  61. perror("pledge");
  62. return 1;
  63. }
  64. auto app = GUI::Application::construct(argc, argv);
  65. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec fattr thread unix", nullptr) < 0) {
  66. perror("pledge");
  67. return 1;
  68. }
  69. s_window = GUI::Window::construct();
  70. s_window->resize(840, 600);
  71. s_window->set_title("HackStudio");
  72. s_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  73. update_path_environment_variable();
  74. if (!make_is_available())
  75. GUI::MessageBox::show(s_window, "The 'make' command is not available. You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository.", "Error", GUI::MessageBox::Type::Error);
  76. const char* path_argument = nullptr;
  77. Core::ArgsParser args_parser;
  78. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  79. args_parser.parse(argc, argv);
  80. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  81. auto menubar = GUI::MenuBar::construct();
  82. auto project_path = path_to_project(argument_absolute_path);
  83. s_hack_studio_widget = s_window->set_main_widget<HackStudioWidget>(project_path);
  84. initialize_connections_to_language_servers(project_path);
  85. s_hack_studio_widget->initialize_menubar(menubar);
  86. app->set_menubar(menubar);
  87. s_window->show();
  88. open_default_project_file(argument_absolute_path);
  89. s_hack_studio_widget->update_actions();
  90. return app->exec();
  91. }
  92. static bool make_is_available()
  93. {
  94. pid_t pid;
  95. const char* argv[] = { "make", "--version", nullptr };
  96. posix_spawn_file_actions_t action;
  97. posix_spawn_file_actions_init(&action);
  98. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  99. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  100. perror("posix_spawn");
  101. return false;
  102. }
  103. int wstatus;
  104. waitpid(pid, &wstatus, 0);
  105. posix_spawn_file_actions_destroy(&action);
  106. return WEXITSTATUS(wstatus) == 0;
  107. }
  108. static void update_path_environment_variable()
  109. {
  110. StringBuilder path;
  111. path.append(getenv("PATH"));
  112. if (path.length())
  113. path.append(":");
  114. path.append("/bin:/usr/bin:/usr/local/bin");
  115. setenv("PATH", path.to_string().characters(), true);
  116. }
  117. static String path_to_project(const String& path_argument_absolute_path)
  118. {
  119. if (path_argument_absolute_path.ends_with(".hsp"))
  120. return path_argument_absolute_path;
  121. else
  122. return "/home/anon/Source/little/little.hsp";
  123. }
  124. static void open_default_project_file(const String& project_path)
  125. {
  126. if (!project_path.is_empty() && !project_path.ends_with(".hsp"))
  127. open_file(project_path);
  128. else
  129. open_file(s_hack_studio_widget->project().default_file());
  130. }
  131. static void initialize_connections_to_language_servers(const String& project_path)
  132. {
  133. LexicalPath project_root_dir(LexicalPath(project_path).dirname());
  134. s_cpp_Language_server_connection = LanguageClients::Cpp::ServerConnection::construct(project_root_dir.string());
  135. s_cpp_Language_server_connection->handshake();
  136. }
  137. namespace HackStudio {
  138. GUI::TextEditor& current_editor()
  139. {
  140. return s_hack_studio_widget->current_editor();
  141. }
  142. void open_file(const String& file_name)
  143. {
  144. return s_hack_studio_widget->open_file(file_name);
  145. }
  146. RefPtr<EditorWrapper> current_editor_wrapper()
  147. {
  148. if (!s_hack_studio_widget)
  149. return nullptr;
  150. return s_hack_studio_widget->current_editor_wrapper();
  151. }
  152. Project& project()
  153. {
  154. return s_hack_studio_widget->project();
  155. }
  156. String currently_open_file()
  157. {
  158. if (!s_hack_studio_widget)
  159. return {};
  160. return s_hack_studio_widget->currently_open_file();
  161. }
  162. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  163. {
  164. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  165. }
  166. LanguageClients::Cpp::ServerConnection& cpp_Language_server_connection()
  167. {
  168. ASSERT(s_cpp_Language_server_connection);
  169. return *s_cpp_Language_server_connection;
  170. }
  171. }