main.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 "CursorTool.h"
  27. #include "Debugger/DebugInfoWidget.h"
  28. #include "Debugger/Debugger.h"
  29. #include "Debugger/DisassemblyWidget.h"
  30. #include "Editor.h"
  31. #include "EditorWrapper.h"
  32. #include "FindInFilesWidget.h"
  33. #include "FormEditorWidget.h"
  34. #include "FormWidget.h"
  35. #include "HackStudio.h"
  36. #include "Locator.h"
  37. #include "Project.h"
  38. #include "TerminalWrapper.h"
  39. #include "WidgetTool.h"
  40. #include "WidgetTreeModel.h"
  41. #include <AK/StringBuilder.h>
  42. #include <LibCore/ArgsParser.h>
  43. #include <LibCore/Event.h>
  44. #include <LibCore/EventLoop.h>
  45. #include <LibCore/File.h>
  46. #include <LibDebug/DebugSession.h>
  47. #include <LibGUI/AboutDialog.h>
  48. #include <LibGUI/Action.h>
  49. #include <LibGUI/ActionGroup.h>
  50. #include <LibGUI/Application.h>
  51. #include <LibGUI/BoxLayout.h>
  52. #include <LibGUI/Button.h>
  53. #include <LibGUI/CppSyntaxHighlighter.h>
  54. #include <LibGUI/FilePicker.h>
  55. #include <LibGUI/INISyntaxHighlighter.h>
  56. #include <LibGUI/InputBox.h>
  57. #include <LibGUI/JSSyntaxHighlighter.h>
  58. #include <LibGUI/Label.h>
  59. #include <LibGUI/Menu.h>
  60. #include <LibGUI/MenuBar.h>
  61. #include <LibGUI/MessageBox.h>
  62. #include <LibGUI/Splitter.h>
  63. #include <LibGUI/StackWidget.h>
  64. #include <LibGUI/TabWidget.h>
  65. #include <LibGUI/TableView.h>
  66. #include <LibGUI/TextBox.h>
  67. #include <LibGUI/TextEditor.h>
  68. #include <LibGUI/ToolBar.h>
  69. #include <LibGUI/ToolBarContainer.h>
  70. #include <LibGUI/TreeView.h>
  71. #include <LibGUI/Widget.h>
  72. #include <LibGUI/Window.h>
  73. #include <LibThread/Lock.h>
  74. #include <LibThread/Thread.h>
  75. #include <LibVT/TerminalWidget.h>
  76. #include <fcntl.h>
  77. #include <spawn.h>
  78. #include <stdio.h>
  79. #include <sys/types.h>
  80. #include <sys/wait.h>
  81. #include <unistd.h>
  82. using namespace HackStudio;
  83. namespace HackStudio {
  84. NonnullRefPtrVector<EditorWrapper> g_all_editor_wrappers;
  85. RefPtr<EditorWrapper> g_current_editor_wrapper;
  86. Function<void(String)> g_open_file;
  87. String g_currently_open_file;
  88. OwnPtr<Project> g_project;
  89. RefPtr<GUI::Window> g_window;
  90. RefPtr<GUI::TreeView> g_project_tree_view;
  91. RefPtr<GUI::StackWidget> g_right_hand_stack;
  92. RefPtr<GUI::Splitter> g_text_inner_splitter;
  93. RefPtr<GUI::Widget> g_form_inner_container;
  94. RefPtr<FormEditorWidget> g_form_editor_widget;
  95. static RefPtr<GUI::TabWidget> s_action_tab_widget;
  96. static void add_new_editor(GUI::Widget& parent)
  97. {
  98. auto wrapper = EditorWrapper::construct(Debugger::on_breakpoint_change);
  99. if (s_action_tab_widget) {
  100. parent.insert_child_before(wrapper, *s_action_tab_widget);
  101. } else {
  102. parent.add_child(wrapper);
  103. }
  104. g_current_editor_wrapper = wrapper;
  105. g_all_editor_wrappers.append(wrapper);
  106. wrapper->editor().set_focus(true);
  107. }
  108. enum class EditMode {
  109. Text,
  110. Form,
  111. };
  112. static void set_edit_mode(EditMode mode)
  113. {
  114. if (mode == EditMode::Text) {
  115. g_right_hand_stack->set_active_widget(g_text_inner_splitter);
  116. } else if (mode == EditMode::Form) {
  117. g_right_hand_stack->set_active_widget(g_form_inner_container);
  118. }
  119. }
  120. static void build(TerminalWrapper&);
  121. static void run(TerminalWrapper&);
  122. void open_project(String);
  123. void open_file(const String&);
  124. bool make_is_available();
  125. static EditorWrapper& current_editor_wrapper()
  126. {
  127. ASSERT(g_current_editor_wrapper);
  128. return *g_current_editor_wrapper;
  129. }
  130. GUI::TextEditor& current_editor()
  131. {
  132. return current_editor_wrapper().editor();
  133. }
  134. static String get_full_path_of_serenity_source(const String& file)
  135. {
  136. auto path_parts = LexicalPath(file).parts();
  137. ASSERT(path_parts[0] == "..");
  138. path_parts.remove(0);
  139. StringBuilder relative_path_builder;
  140. relative_path_builder.join("/", path_parts);
  141. constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity";
  142. LexicalPath serenity_sources_base(SERENITY_LIBS_PREFIX);
  143. return String::format("%s/%s", serenity_sources_base.string().characters(), relative_path_builder.to_string().characters());
  144. }
  145. static NonnullRefPtr<EditorWrapper> get_editor_of_file(const String& file)
  146. {
  147. String file_path = file;
  148. // TODO: We can probably do a more specific condition here, something like
  149. // "if (file.starts_with("../Libraries/") || file.starts_with("../AK/"))"
  150. if (file.starts_with("../")) {
  151. file_path = get_full_path_of_serenity_source(file);
  152. }
  153. open_file(file_path);
  154. return current_editor_wrapper();
  155. }
  156. static String get_project_executable_path()
  157. {
  158. // e.g /my/project.hsp => /my/project
  159. // TODO: Perhaps a Makefile rule for getting the value of $(PROGRAM) would be better?
  160. return g_project->path().substring(0, g_project->path().index_of(".").value());
  161. }
  162. static int main_impl(int argc, char** argv)
  163. {
  164. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec unix fattr thread", nullptr) < 0) {
  165. perror("pledge");
  166. return 1;
  167. }
  168. auto app = GUI::Application::construct(argc, argv);
  169. if (pledge("stdio tty accept rpath cpath wpath shared_buffer proc exec fattr thread", nullptr) < 0) {
  170. perror("pledge");
  171. return 1;
  172. }
  173. Function<void()> update_actions;
  174. g_window = GUI::Window::construct();
  175. g_window->resize(840, 600);
  176. g_window->set_title("HackStudio");
  177. auto& widget = g_window->set_main_widget<GUI::Widget>();
  178. widget.set_fill_with_background_color(true);
  179. widget.set_layout<GUI::VerticalBoxLayout>();
  180. widget.layout()->set_spacing(2);
  181. StringBuilder path;
  182. path.append(getenv("PATH"));
  183. if (path.length())
  184. path.append(":");
  185. path.append("/bin:/usr/bin:/usr/local/bin");
  186. setenv("PATH", path.to_string().characters(), true);
  187. if (!make_is_available())
  188. GUI::MessageBox::show(g_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);
  189. const char* path_argument = nullptr;
  190. Core::ArgsParser args_parser;
  191. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  192. args_parser.parse(argc, argv);
  193. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  194. if (argument_absolute_path.ends_with(".hsp"))
  195. open_project(argument_absolute_path);
  196. else
  197. open_project("/home/anon/Source/little/little.hsp");
  198. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  199. auto& toolbar = toolbar_container.add<GUI::ToolBar>();
  200. auto selected_file_names = [&] {
  201. Vector<String> files;
  202. g_project_tree_view->selection().for_each_index([&](const GUI::ModelIndex& index) {
  203. files.append(index.data().as_string());
  204. });
  205. return files;
  206. };
  207. auto new_action = GUI::Action::create("Add new file to project...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
  208. String filename;
  209. if (GUI::InputBox::show(filename, g_window, "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
  210. return;
  211. auto file = Core::File::construct(filename);
  212. if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
  213. GUI::MessageBox::show(g_window, String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  214. return;
  215. }
  216. if (!g_project->add_file(filename)) {
  217. GUI::MessageBox::show(g_window, String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  218. // FIXME: Should we unlink the file here maybe?
  219. return;
  220. }
  221. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  222. open_file(filename);
  223. });
  224. auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  225. auto result = GUI::FilePicker::get_open_filepath(g_window, "Add existing file to project");
  226. if (!result.has_value())
  227. return;
  228. auto& filename = result.value();
  229. if (!g_project->add_file(filename)) {
  230. GUI::MessageBox::show(g_window, String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  231. return;
  232. }
  233. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  234. open_file(filename);
  235. });
  236. auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action& action) {
  237. (void)action;
  238. auto files = selected_file_names();
  239. if (files.is_empty())
  240. return;
  241. String message;
  242. if (files.size() == 1) {
  243. message = String::format("Really remove %s from the project?", LexicalPath(files[0]).basename().characters());
  244. } else {
  245. message = String::format("Really remove %d files from the project?", files.size());
  246. }
  247. auto result = GUI::MessageBox::show(g_window,
  248. message,
  249. "Confirm deletion",
  250. GUI::MessageBox::Type::Warning,
  251. GUI::MessageBox::InputType::OKCancel);
  252. if (result == GUI::MessageBox::ExecCancel)
  253. return;
  254. for (auto& file : files) {
  255. if (!g_project->remove_file(file)) {
  256. GUI::MessageBox::show(g_window,
  257. String::format("Removing file %s from the project failed.", file.characters()),
  258. "Removal failed",
  259. GUI::MessageBox::Type::Error);
  260. break;
  261. }
  262. }
  263. });
  264. delete_action->set_enabled(false);
  265. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  266. project_tree_view_context_menu->add_action(new_action);
  267. project_tree_view_context_menu->add_action(add_existing_file_action);
  268. project_tree_view_context_menu->add_action(delete_action);
  269. auto& outer_splitter = widget.add<GUI::HorizontalSplitter>();
  270. g_project_tree_view = outer_splitter.add<GUI::TreeView>();
  271. g_project_tree_view->set_model(g_project->model());
  272. g_project_tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  273. g_project_tree_view->set_preferred_size(140, 0);
  274. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  275. g_project_tree_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  276. if (index.is_valid()) {
  277. project_tree_view_context_menu->popup(event.screen_position());
  278. }
  279. };
  280. g_project_tree_view->on_selection_change = [&] {
  281. delete_action->set_enabled(!g_project_tree_view->selection().is_empty());
  282. };
  283. g_right_hand_stack = outer_splitter.add<GUI::StackWidget>();
  284. g_form_inner_container = g_right_hand_stack->add<GUI::Widget>();
  285. g_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  286. auto& form_widgets_toolbar = g_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  287. form_widgets_toolbar.set_preferred_size(38, 0);
  288. GUI::ActionGroup tool_actions;
  289. tool_actions.set_exclusive(true);
  290. auto cursor_tool_action = GUI::Action::create_checkable("Cursor", Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Cursor.png"), [&](auto&) {
  291. g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
  292. });
  293. cursor_tool_action->set_checked(true);
  294. tool_actions.add_action(cursor_tool_action);
  295. form_widgets_toolbar.add_action(cursor_tool_action);
  296. GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) {
  297. auto icon_path = String::format("/res/icons/hackstudio/G%s.png", reg.class_name().characters());
  298. auto action = GUI::Action::create_checkable(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) {
  299. g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
  300. auto widget = reg.construct();
  301. g_form_editor_widget->form_widget().add_child(widget);
  302. widget->set_relative_rect(30, 30, 30, 30);
  303. g_form_editor_widget->model().update();
  304. });
  305. action->set_checked(false);
  306. tool_actions.add_action(action);
  307. form_widgets_toolbar.add_action(move(action));
  308. });
  309. auto& form_editor_inner_splitter = g_form_inner_container->add<GUI::HorizontalSplitter>();
  310. g_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
  311. auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
  312. form_editing_pane_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  313. form_editing_pane_container.set_preferred_size(190, 0);
  314. form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
  315. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  316. auto& wrapper = form_editing_pane_container.add<GUI::Widget>();
  317. wrapper.set_layout<GUI::VerticalBoxLayout>();
  318. auto& label = wrapper.add<GUI::Label>(text);
  319. label.set_fill_with_background_color(true);
  320. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  321. label.set_font(Gfx::Font::default_bold_font());
  322. label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  323. label.set_preferred_size(0, 16);
  324. wrapper.add_child(pane_widget);
  325. };
  326. auto form_widget_tree_view = GUI::TreeView::construct();
  327. form_widget_tree_view->set_model(g_form_editor_widget->model());
  328. form_widget_tree_view->on_selection_change = [&] {
  329. g_form_editor_widget->selection().disable_hooks();
  330. g_form_editor_widget->selection().clear();
  331. form_widget_tree_view->selection().for_each_index([&](auto& index) {
  332. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  333. // since that would allow you to drag-move the FormWidget.
  334. if (index.internal_data() != &g_form_editor_widget->form_widget())
  335. g_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  336. });
  337. g_form_editor_widget->update();
  338. g_form_editor_widget->selection().enable_hooks();
  339. };
  340. g_form_editor_widget->selection().on_add = [&](auto& widget) {
  341. form_widget_tree_view->selection().add(g_form_editor_widget->model().index_for_widget(widget));
  342. };
  343. g_form_editor_widget->selection().on_remove = [&](auto& widget) {
  344. form_widget_tree_view->selection().remove(g_form_editor_widget->model().index_for_widget(widget));
  345. };
  346. g_form_editor_widget->selection().on_clear = [&] {
  347. form_widget_tree_view->selection().clear();
  348. };
  349. add_properties_pane("Form widget tree:", form_widget_tree_view);
  350. add_properties_pane("Widget properties:", GUI::TableView::construct());
  351. g_text_inner_splitter = g_right_hand_stack->add<GUI::VerticalSplitter>();
  352. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  353. add_new_editor(*g_text_inner_splitter);
  354. auto switch_to_next_editor = GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  355. if (g_all_editor_wrappers.size() <= 1)
  356. return;
  357. Vector<EditorWrapper*> wrappers;
  358. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  359. wrappers.append(&child);
  360. return IterationDecision::Continue;
  361. });
  362. for (size_t i = 0; i < wrappers.size(); ++i) {
  363. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  364. if (i == wrappers.size() - 1)
  365. wrappers[0]->editor().set_focus(true);
  366. else
  367. wrappers[i + 1]->editor().set_focus(true);
  368. }
  369. }
  370. });
  371. auto switch_to_previous_editor = GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  372. if (g_all_editor_wrappers.size() <= 1)
  373. return;
  374. Vector<EditorWrapper*> wrappers;
  375. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  376. wrappers.append(&child);
  377. return IterationDecision::Continue;
  378. });
  379. for (int i = wrappers.size() - 1; i >= 0; --i) {
  380. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  381. if (i == 0)
  382. wrappers.last()->editor().set_focus(true);
  383. else
  384. wrappers[i - 1]->editor().set_focus(true);
  385. }
  386. }
  387. });
  388. auto remove_current_editor_action = GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  389. if (g_all_editor_wrappers.size() <= 1)
  390. return;
  391. auto wrapper = g_current_editor_wrapper;
  392. switch_to_next_editor->activate();
  393. g_text_inner_splitter->remove_child(*wrapper);
  394. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  395. update_actions();
  396. });
  397. auto open_action = GUI::Action::create("Open project...", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  398. auto open_path = GUI::FilePicker::get_open_filepath(g_window, "Open project");
  399. if (!open_path.has_value())
  400. return;
  401. open_project(open_path.value());
  402. open_file(g_project->default_file());
  403. update_actions();
  404. });
  405. auto save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  406. if (g_currently_open_file.is_empty())
  407. return;
  408. current_editor().write_to_file(g_currently_open_file);
  409. });
  410. toolbar.add_action(new_action);
  411. toolbar.add_action(add_existing_file_action);
  412. toolbar.add_action(save_action);
  413. toolbar.add_action(delete_action);
  414. toolbar.add_separator();
  415. toolbar.add_action(GUI::CommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  416. toolbar.add_action(GUI::CommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  417. toolbar.add_action(GUI::CommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  418. toolbar.add_separator();
  419. toolbar.add_action(GUI::CommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  420. toolbar.add_action(GUI::CommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  421. toolbar.add_separator();
  422. g_project_tree_view->on_activation = [&](auto& index) {
  423. auto filename = index.data(GUI::ModelRole::Custom).to_string();
  424. open_file(filename);
  425. };
  426. s_action_tab_widget = g_text_inner_splitter->add<GUI::TabWidget>();
  427. s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  428. s_action_tab_widget->set_preferred_size(0, 24);
  429. s_action_tab_widget->on_change = [&](auto&) { update_actions(); };
  430. auto reveal_action_tab = [&](auto& widget) {
  431. if (s_action_tab_widget->preferred_size().height() < 200)
  432. s_action_tab_widget->set_preferred_size(0, 200);
  433. s_action_tab_widget->set_active_widget(&widget);
  434. };
  435. auto hide_action_tabs = [&] {
  436. s_action_tab_widget->set_preferred_size(0, 24);
  437. };
  438. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  439. hide_action_tabs();
  440. });
  441. auto add_editor_action = GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E },
  442. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-text-editor.png"),
  443. [&](auto&) {
  444. add_new_editor(*g_text_inner_splitter);
  445. update_actions();
  446. });
  447. auto add_terminal_action = GUI::Action::create("Add new Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  448. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"),
  449. [&](auto&) {
  450. auto& terminal = s_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  451. reveal_action_tab(terminal);
  452. update_actions();
  453. terminal.terminal()->set_focus(true);
  454. });
  455. auto remove_current_terminal_action = GUI::Action::create("Remove current Terminal", { Mod_Alt | Mod_Shift, Key_T }, [&](auto&) {
  456. auto widget = s_action_tab_widget->active_widget();
  457. if (!widget)
  458. return;
  459. if (strcmp(widget->class_name(), "TerminalWrapper") != 0)
  460. return;
  461. auto terminal = reinterpret_cast<TerminalWrapper*>(widget);
  462. if (!terminal->user_spawned())
  463. return;
  464. s_action_tab_widget->remove_tab(*terminal);
  465. update_actions();
  466. });
  467. auto& find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  468. auto& terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
  469. auto& debug_info_widget = s_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  470. auto& disassembly_widget = s_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  471. auto& locator = widget.add<Locator>();
  472. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  473. locator.open();
  474. });
  475. auto menubar = GUI::MenuBar::construct();
  476. auto& app_menu = menubar->add_menu("HackStudio");
  477. app_menu.add_action(open_action);
  478. app_menu.add_action(save_action);
  479. app_menu.add_separator();
  480. app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  481. app->quit();
  482. }));
  483. auto& project_menu = menubar->add_menu("Project");
  484. project_menu.add_action(new_action);
  485. project_menu.add_action(add_existing_file_action);
  486. auto& edit_menu = menubar->add_menu("Edit");
  487. edit_menu.add_action(GUI::Action::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [&](auto&) {
  488. reveal_action_tab(find_in_files_widget);
  489. find_in_files_widget.focus_textbox_and_select_all();
  490. }));
  491. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/program-stop.png"), [&](auto&) {
  492. terminal_wrapper.kill_running_command();
  493. });
  494. stop_action->set_enabled(false);
  495. terminal_wrapper.on_command_exit = [&] {
  496. stop_action->set_enabled(false);
  497. };
  498. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  499. reveal_action_tab(terminal_wrapper);
  500. build(terminal_wrapper);
  501. stop_action->set_enabled(true);
  502. });
  503. toolbar.add_action(build_action);
  504. toolbar.add_separator();
  505. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/program-run.png"), [&](auto&) {
  506. reveal_action_tab(terminal_wrapper);
  507. run(terminal_wrapper);
  508. stop_action->set_enabled(true);
  509. });
  510. RefPtr<LibThread::Thread> debugger_thread;
  511. auto debug_action = GUI::Action::create("Debug", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-run.png"), [&](auto&) {
  512. if (g_project->type() != ProjectType::Cpp) {
  513. GUI::MessageBox::show(g_window, String::format("Cannot debug current project type", get_project_executable_path().characters()), "Error", GUI::MessageBox::Type::Error);
  514. return;
  515. }
  516. if (!GUI::FilePicker::file_exists(get_project_executable_path())) {
  517. GUI::MessageBox::show(g_window, String::format("Could not find file: %s. (did you build the project?)", get_project_executable_path().characters()), "Error", GUI::MessageBox::Type::Error);
  518. return;
  519. }
  520. if (Debugger::the().session()) {
  521. GUI::MessageBox::show(g_window, "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  522. return;
  523. }
  524. Debugger::the().set_executable_path(get_project_executable_path());
  525. debugger_thread = adopt(*new LibThread::Thread(Debugger::start_static));
  526. debugger_thread->start();
  527. });
  528. toolbar.add_separator();
  529. toolbar.add_action(debug_action);
  530. RefPtr<EditorWrapper> current_editor_in_execution;
  531. Debugger::initialize(
  532. [&](const PtraceRegisters& regs) {
  533. ASSERT(Debugger::the().session());
  534. const auto& debug_session = *Debugger::the().session();
  535. auto source_position = debug_session.debug_info().get_source_position(regs.eip);
  536. if (!source_position.has_value()) {
  537. dbg() << "Could not find source position for address: " << (void*)regs.eip;
  538. return Debugger::HasControlPassedToUser::No;
  539. }
  540. Core::EventLoop::main().post_event(
  541. *g_window,
  542. make<Core::DeferredInvocationEvent>(
  543. [&, source_position](auto&) {
  544. current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  545. current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  546. debug_info_widget.update_state(*Debugger::the().session(), regs);
  547. debug_info_widget.set_debug_actions_enabled(true);
  548. disassembly_widget.update_state(*Debugger::the().session(), regs);
  549. reveal_action_tab(debug_info_widget);
  550. }));
  551. Core::EventLoop::wake();
  552. return Debugger::HasControlPassedToUser::Yes;
  553. },
  554. [&]() {
  555. Core::EventLoop::main().post_event(*g_window, make<Core::DeferredInvocationEvent>([&](auto&) {
  556. debug_info_widget.set_debug_actions_enabled(false);
  557. if (current_editor_in_execution) {
  558. current_editor_in_execution->editor().clear_execution_position();
  559. }
  560. }));
  561. Core::EventLoop::wake();
  562. },
  563. [&]() {
  564. Core::EventLoop::main().post_event(*g_window, make<Core::DeferredInvocationEvent>([&](auto&) {
  565. debug_info_widget.program_stopped();
  566. disassembly_widget.program_stopped();
  567. hide_action_tabs();
  568. GUI::MessageBox::show(g_window, "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  569. }));
  570. Core::EventLoop::wake();
  571. });
  572. auto& build_menu = menubar->add_menu("Build");
  573. build_menu.add_action(build_action);
  574. build_menu.add_separator();
  575. build_menu.add_action(run_action);
  576. build_menu.add_action(stop_action);
  577. build_menu.add_separator();
  578. build_menu.add_action(debug_action);
  579. auto& view_menu = menubar->add_menu("View");
  580. view_menu.add_action(hide_action_tabs_action);
  581. view_menu.add_action(open_locator_action);
  582. view_menu.add_separator();
  583. view_menu.add_action(add_editor_action);
  584. view_menu.add_action(remove_current_editor_action);
  585. view_menu.add_action(add_terminal_action);
  586. view_menu.add_action(remove_current_terminal_action);
  587. auto& help_menu = menubar->add_menu("Help");
  588. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  589. GUI::AboutDialog::show("HackStudio", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"), g_window);
  590. }));
  591. app->set_menubar(move(menubar));
  592. g_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  593. g_window->show();
  594. update_actions = [&]() {
  595. auto is_remove_terminal_enabled = []() {
  596. auto widget = s_action_tab_widget->active_widget();
  597. if (!widget)
  598. return false;
  599. if (strcmp(widget->class_name(), "TerminalWrapper") != 0)
  600. return false;
  601. if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned())
  602. return false;
  603. return true;
  604. };
  605. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  606. remove_current_terminal_action->set_enabled(is_remove_terminal_enabled());
  607. };
  608. g_open_file = open_file;
  609. if (!argument_absolute_path.is_empty() && !argument_absolute_path.ends_with(".hsp"))
  610. open_file(argument_absolute_path);
  611. else
  612. open_file(g_project->default_file());
  613. update_actions();
  614. return app->exec();
  615. }
  616. void build(TerminalWrapper& wrapper)
  617. {
  618. if (g_project->type() == ProjectType::JavaScript && g_currently_open_file.ends_with(".js"))
  619. wrapper.run_command(String::format("js -A %s", g_currently_open_file.characters()));
  620. else
  621. wrapper.run_command("make");
  622. }
  623. void run(TerminalWrapper& wrapper)
  624. {
  625. if (g_project->type() == ProjectType::JavaScript && g_currently_open_file.ends_with(".js"))
  626. wrapper.run_command(String::format("js %s", g_currently_open_file.characters()));
  627. else
  628. wrapper.run_command("make run");
  629. }
  630. void open_project(String filename)
  631. {
  632. LexicalPath lexical_path(filename);
  633. if (chdir(lexical_path.dirname().characters()) < 0) {
  634. perror("chdir");
  635. exit(1);
  636. }
  637. g_project = Project::load_from_file(filename);
  638. ASSERT(g_project);
  639. if (g_project_tree_view) {
  640. g_project_tree_view->set_model(g_project->model());
  641. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  642. g_project_tree_view->update();
  643. }
  644. if (Debugger::is_initialized()) {
  645. Debugger::the().reset_breakpoints();
  646. }
  647. }
  648. void open_file(const String& filename)
  649. {
  650. auto project_file = g_project->get_file(filename);
  651. if (project_file) {
  652. current_editor().set_document(const_cast<GUI::TextDocument&>(project_file->document()));
  653. current_editor().set_mode(GUI::TextEditor::Editable);
  654. } else {
  655. auto external_file = ProjectFile::construct_with_name(filename);
  656. current_editor().set_document(const_cast<GUI::TextDocument&>(external_file->document()));
  657. current_editor().set_mode(GUI::TextEditor::ReadOnly);
  658. }
  659. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  660. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  661. else if (filename.ends_with(".js"))
  662. current_editor().set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  663. else if (filename.ends_with(".ini"))
  664. current_editor().set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  665. else
  666. current_editor().set_syntax_highlighter(nullptr);
  667. if (filename.ends_with(".frm")) {
  668. set_edit_mode(EditMode::Form);
  669. } else {
  670. set_edit_mode(EditMode::Text);
  671. }
  672. g_currently_open_file = filename;
  673. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  674. g_project_tree_view->update();
  675. current_editor_wrapper().filename_label().set_text(filename);
  676. current_editor().set_focus(true);
  677. }
  678. bool make_is_available()
  679. {
  680. pid_t pid;
  681. const char* argv[] = { "make", "--version", nullptr };
  682. posix_spawn_file_actions_t action;
  683. posix_spawn_file_actions_init(&action);
  684. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  685. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  686. perror("posix_spawn");
  687. return false;
  688. }
  689. int wstatus;
  690. waitpid(pid, &wstatus, 0);
  691. posix_spawn_file_actions_destroy(&action);
  692. return WEXITSTATUS(wstatus) == 0;
  693. }
  694. }
  695. int main(int argc, char** argv)
  696. {
  697. return main_impl(argc, argv);
  698. }