main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "Project.h"
  29. #include <AK/StringBuilder.h>
  30. #include <LibCore/ArgsParser.h>
  31. #include <LibCore/Event.h>
  32. #include <LibCore/EventLoop.h>
  33. #include <LibCore/File.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/MenuBar.h>
  36. #include <LibGUI/MessageBox.h>
  37. #include <LibGUI/Widget.h>
  38. #include <LibGUI/Window.h>
  39. #include <LibThread/Lock.h>
  40. #include <LibThread/Thread.h>
  41. #include <LibVT/TerminalWidget.h>
  42. #include <fcntl.h>
  43. #include <spawn.h>
  44. #include <stdio.h>
  45. #include <sys/types.h>
  46. #include <sys/wait.h>
  47. #include <unistd.h>
  48. using namespace HackStudio;
  49. static RefPtr<GUI::Window> s_window;
  50. static RefPtr<HackStudioWidget> s_hack_studio_widget;
  51. static bool make_is_available();
  52. static void update_path_environment_variable();
  53. static String path_to_project(const String& path_argument_absolute_path);
  54. static void open_default_project_file(const String& project_path);
  55. int main(int argc, char** argv)
  56. {
  57. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec unix fattr thread unix sendfd", nullptr) < 0) {
  58. perror("pledge");
  59. return 1;
  60. }
  61. auto app = GUI::Application::construct(argc, argv);
  62. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec fattr thread unix sendfd", nullptr) < 0) {
  63. perror("pledge");
  64. return 1;
  65. }
  66. s_window = GUI::Window::construct();
  67. s_window->resize(840, 600);
  68. s_window->set_title("HackStudio");
  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. 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);
  73. const char* path_argument = nullptr;
  74. Core::ArgsParser args_parser;
  75. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  76. args_parser.parse(argc, argv);
  77. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  78. auto menubar = GUI::MenuBar::construct();
  79. auto project_path = path_to_project(argument_absolute_path);
  80. s_hack_studio_widget = s_window->set_main_widget<HackStudioWidget>(project_path);
  81. s_hack_studio_widget->initialize_menubar(menubar);
  82. app->set_menubar(menubar);
  83. s_window->show();
  84. open_default_project_file(argument_absolute_path);
  85. s_hack_studio_widget->update_actions();
  86. return app->exec();
  87. }
  88. static bool make_is_available()
  89. {
  90. pid_t pid;
  91. const char* argv[] = { "make", "--version", nullptr };
  92. posix_spawn_file_actions_t action;
  93. posix_spawn_file_actions_init(&action);
  94. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  95. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  96. perror("posix_spawn");
  97. return false;
  98. }
  99. int wstatus;
  100. waitpid(pid, &wstatus, 0);
  101. posix_spawn_file_actions_destroy(&action);
  102. return WEXITSTATUS(wstatus) == 0;
  103. }
  104. static void update_path_environment_variable()
  105. {
  106. StringBuilder path;
  107. path.append(getenv("PATH"));
  108. if (path.length())
  109. path.append(":");
  110. path.append("/bin:/usr/bin:/usr/local/bin");
  111. setenv("PATH", path.to_string().characters(), true);
  112. }
  113. static String path_to_project(const String& path_argument_absolute_path)
  114. {
  115. if (path_argument_absolute_path.ends_with(".hsp"))
  116. return path_argument_absolute_path;
  117. else
  118. return "/home/anon/Source/little/little.hsp";
  119. }
  120. static void open_default_project_file(const String& project_path)
  121. {
  122. if (!project_path.is_empty() && !project_path.ends_with(".hsp"))
  123. open_file(project_path);
  124. else
  125. open_file(s_hack_studio_widget->project().default_file());
  126. }
  127. namespace HackStudio {
  128. GUI::TextEditor& current_editor()
  129. {
  130. return s_hack_studio_widget->current_editor();
  131. }
  132. void open_file(const String& file_name)
  133. {
  134. return s_hack_studio_widget->open_file(file_name);
  135. }
  136. RefPtr<EditorWrapper> current_editor_wrapper()
  137. {
  138. if (!s_hack_studio_widget)
  139. return nullptr;
  140. return s_hack_studio_widget->current_editor_wrapper();
  141. }
  142. Project& project()
  143. {
  144. return s_hack_studio_widget->project();
  145. }
  146. String currently_open_file()
  147. {
  148. if (!s_hack_studio_widget)
  149. return {};
  150. return s_hack_studio_widget->currently_open_file();
  151. }
  152. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  153. {
  154. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  155. }
  156. }