main.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 open_selected_action = GUI::Action::create("Open", [&](const GUI::Action&) {
  225. auto files = selected_file_names();
  226. for (auto& file : files)
  227. open_file(file);
  228. });
  229. open_selected_action->set_enabled(true);
  230. auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  231. auto result = GUI::FilePicker::get_open_filepath(g_window, "Add existing file to project");
  232. if (!result.has_value())
  233. return;
  234. auto& filename = result.value();
  235. if (!g_project->add_file(filename)) {
  236. GUI::MessageBox::show(g_window, String::format("Failed to add '%s' to project", filename.characters()), "Error", GUI::MessageBox::Type::Error);
  237. return;
  238. }
  239. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  240. open_file(filename);
  241. });
  242. auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action&) {
  243. auto files = selected_file_names();
  244. if (files.is_empty())
  245. return;
  246. String message;
  247. if (files.size() == 1) {
  248. message = String::format("Really remove %s from the project?", LexicalPath(files[0]).basename().characters());
  249. } else {
  250. message = String::format("Really remove %d files from the project?", files.size());
  251. }
  252. auto result = GUI::MessageBox::show(g_window,
  253. message,
  254. "Confirm deletion",
  255. GUI::MessageBox::Type::Warning,
  256. GUI::MessageBox::InputType::OKCancel);
  257. if (result == GUI::MessageBox::ExecCancel)
  258. return;
  259. for (auto& file : files) {
  260. if (!g_project->remove_file(file)) {
  261. GUI::MessageBox::show(g_window,
  262. String::format("Removing file %s from the project failed.", file.characters()),
  263. "Removal failed",
  264. GUI::MessageBox::Type::Error);
  265. break;
  266. }
  267. }
  268. });
  269. delete_action->set_enabled(false);
  270. auto project_tree_view_context_menu = GUI::Menu::construct("Project Files");
  271. project_tree_view_context_menu->add_action(open_selected_action);
  272. // TODO: Rename, cut, copy, duplicate with new name, show containing folder ...
  273. project_tree_view_context_menu->add_separator();
  274. project_tree_view_context_menu->add_action(new_action);
  275. project_tree_view_context_menu->add_action(add_existing_file_action);
  276. project_tree_view_context_menu->add_action(delete_action);
  277. auto& outer_splitter = widget.add<GUI::HorizontalSplitter>();
  278. g_project_tree_view = outer_splitter.add<GUI::TreeView>();
  279. g_project_tree_view->set_model(g_project->model());
  280. g_project_tree_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  281. g_project_tree_view->set_preferred_size(140, 0);
  282. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  283. g_project_tree_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  284. if (index.is_valid()) {
  285. project_tree_view_context_menu->popup(event.screen_position(), open_selected_action);
  286. }
  287. };
  288. g_project_tree_view->on_selection_change = [&] {
  289. open_selected_action->set_enabled(!g_project_tree_view->selection().is_empty());
  290. delete_action->set_enabled(!g_project_tree_view->selection().is_empty());
  291. };
  292. g_right_hand_stack = outer_splitter.add<GUI::StackWidget>();
  293. g_form_inner_container = g_right_hand_stack->add<GUI::Widget>();
  294. g_form_inner_container->set_layout<GUI::HorizontalBoxLayout>();
  295. auto& form_widgets_toolbar = g_form_inner_container->add<GUI::ToolBar>(Orientation::Vertical, 26);
  296. form_widgets_toolbar.set_preferred_size(38, 0);
  297. GUI::ActionGroup tool_actions;
  298. tool_actions.set_exclusive(true);
  299. auto cursor_tool_action = GUI::Action::create_checkable("Cursor", Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Cursor.png"), [&](auto&) {
  300. g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget));
  301. });
  302. cursor_tool_action->set_checked(true);
  303. tool_actions.add_action(cursor_tool_action);
  304. form_widgets_toolbar.add_action(cursor_tool_action);
  305. GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& reg) {
  306. auto icon_path = String::format("/res/icons/hackstudio/G%s.png", reg.class_name().characters());
  307. auto action = GUI::Action::create_checkable(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg](auto&) {
  308. g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg));
  309. auto widget = reg.construct();
  310. g_form_editor_widget->form_widget().add_child(widget);
  311. widget->set_relative_rect(30, 30, 30, 30);
  312. g_form_editor_widget->model().update();
  313. });
  314. action->set_checked(false);
  315. tool_actions.add_action(action);
  316. form_widgets_toolbar.add_action(move(action));
  317. });
  318. auto& form_editor_inner_splitter = g_form_inner_container->add<GUI::HorizontalSplitter>();
  319. g_form_editor_widget = form_editor_inner_splitter.add<FormEditorWidget>();
  320. auto& form_editing_pane_container = form_editor_inner_splitter.add<GUI::VerticalSplitter>();
  321. form_editing_pane_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  322. form_editing_pane_container.set_preferred_size(190, 0);
  323. form_editing_pane_container.set_layout<GUI::VerticalBoxLayout>();
  324. auto add_properties_pane = [&](auto& text, auto pane_widget) {
  325. auto& wrapper = form_editing_pane_container.add<GUI::Widget>();
  326. wrapper.set_layout<GUI::VerticalBoxLayout>();
  327. auto& label = wrapper.add<GUI::Label>(text);
  328. label.set_fill_with_background_color(true);
  329. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  330. label.set_font(Gfx::Font::default_bold_font());
  331. label.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  332. label.set_preferred_size(0, 16);
  333. wrapper.add_child(pane_widget);
  334. };
  335. auto form_widget_tree_view = GUI::TreeView::construct();
  336. form_widget_tree_view->set_model(g_form_editor_widget->model());
  337. form_widget_tree_view->on_selection_change = [&] {
  338. g_form_editor_widget->selection().disable_hooks();
  339. g_form_editor_widget->selection().clear();
  340. form_widget_tree_view->selection().for_each_index([&](auto& index) {
  341. // NOTE: Make sure we don't add the FormWidget itself to the selection,
  342. // since that would allow you to drag-move the FormWidget.
  343. if (index.internal_data() != &g_form_editor_widget->form_widget())
  344. g_form_editor_widget->selection().add(*(GUI::Widget*)index.internal_data());
  345. });
  346. g_form_editor_widget->update();
  347. g_form_editor_widget->selection().enable_hooks();
  348. };
  349. g_form_editor_widget->selection().on_add = [&](auto& widget) {
  350. form_widget_tree_view->selection().add(g_form_editor_widget->model().index_for_widget(widget));
  351. };
  352. g_form_editor_widget->selection().on_remove = [&](auto& widget) {
  353. form_widget_tree_view->selection().remove(g_form_editor_widget->model().index_for_widget(widget));
  354. };
  355. g_form_editor_widget->selection().on_clear = [&] {
  356. form_widget_tree_view->selection().clear();
  357. };
  358. add_properties_pane("Form widget tree:", form_widget_tree_view);
  359. add_properties_pane("Widget properties:", GUI::TableView::construct());
  360. g_text_inner_splitter = g_right_hand_stack->add<GUI::VerticalSplitter>();
  361. g_text_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
  362. add_new_editor(*g_text_inner_splitter);
  363. auto switch_to_next_editor = GUI::Action::create("Switch to next editor", { Mod_Ctrl, Key_E }, [&](auto&) {
  364. if (g_all_editor_wrappers.size() <= 1)
  365. return;
  366. Vector<EditorWrapper*> wrappers;
  367. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  368. wrappers.append(&child);
  369. return IterationDecision::Continue;
  370. });
  371. for (size_t i = 0; i < wrappers.size(); ++i) {
  372. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  373. if (i == wrappers.size() - 1)
  374. wrappers[0]->editor().set_focus(true);
  375. else
  376. wrappers[i + 1]->editor().set_focus(true);
  377. }
  378. }
  379. });
  380. auto switch_to_previous_editor = GUI::Action::create("Switch to previous editor", { Mod_Ctrl | Mod_Shift, Key_E }, [&](auto&) {
  381. if (g_all_editor_wrappers.size() <= 1)
  382. return;
  383. Vector<EditorWrapper*> wrappers;
  384. g_text_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
  385. wrappers.append(&child);
  386. return IterationDecision::Continue;
  387. });
  388. for (int i = wrappers.size() - 1; i >= 0; --i) {
  389. if (g_current_editor_wrapper.ptr() == wrappers[i]) {
  390. if (i == 0)
  391. wrappers.last()->editor().set_focus(true);
  392. else
  393. wrappers[i - 1]->editor().set_focus(true);
  394. }
  395. }
  396. });
  397. auto remove_current_editor_action = GUI::Action::create("Remove current editor", { Mod_Alt | Mod_Shift, Key_E }, [&](auto&) {
  398. if (g_all_editor_wrappers.size() <= 1)
  399. return;
  400. auto wrapper = g_current_editor_wrapper;
  401. switch_to_next_editor->activate();
  402. g_text_inner_splitter->remove_child(*wrapper);
  403. g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
  404. update_actions();
  405. });
  406. 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&) {
  407. auto open_path = GUI::FilePicker::get_open_filepath(g_window, "Open project");
  408. if (!open_path.has_value())
  409. return;
  410. open_project(open_path.value());
  411. open_file(g_project->default_file());
  412. update_actions();
  413. });
  414. auto save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) {
  415. if (g_currently_open_file.is_empty())
  416. return;
  417. current_editor().write_to_file(g_currently_open_file);
  418. });
  419. toolbar.add_action(new_action);
  420. toolbar.add_action(add_existing_file_action);
  421. toolbar.add_action(save_action);
  422. toolbar.add_action(delete_action);
  423. toolbar.add_separator();
  424. toolbar.add_action(GUI::CommonActions::make_cut_action([&](auto&) { current_editor().cut_action().activate(); }));
  425. toolbar.add_action(GUI::CommonActions::make_copy_action([&](auto&) { current_editor().copy_action().activate(); }));
  426. toolbar.add_action(GUI::CommonActions::make_paste_action([&](auto&) { current_editor().paste_action().activate(); }));
  427. toolbar.add_separator();
  428. toolbar.add_action(GUI::CommonActions::make_undo_action([&](auto&) { current_editor().undo_action().activate(); }));
  429. toolbar.add_action(GUI::CommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
  430. toolbar.add_separator();
  431. g_project_tree_view->on_activation = [&](auto& index) {
  432. auto filename = index.data(GUI::ModelRole::Custom).to_string();
  433. open_file(filename);
  434. };
  435. s_action_tab_widget = g_text_inner_splitter->add<GUI::TabWidget>();
  436. s_action_tab_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  437. s_action_tab_widget->set_preferred_size(0, 24);
  438. s_action_tab_widget->on_change = [&](auto&) { update_actions(); };
  439. auto reveal_action_tab = [&](auto& widget) {
  440. if (s_action_tab_widget->preferred_size().height() < 200)
  441. s_action_tab_widget->set_preferred_size(0, 200);
  442. s_action_tab_widget->set_active_widget(&widget);
  443. };
  444. auto hide_action_tabs = [&] {
  445. s_action_tab_widget->set_preferred_size(0, 24);
  446. };
  447. auto hide_action_tabs_action = GUI::Action::create("Hide action tabs", { Mod_Ctrl | Mod_Shift, Key_X }, [&](auto&) {
  448. hide_action_tabs();
  449. });
  450. auto add_editor_action = GUI::Action::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E },
  451. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-text-editor.png"),
  452. [&](auto&) {
  453. add_new_editor(*g_text_inner_splitter);
  454. update_actions();
  455. });
  456. auto add_terminal_action = GUI::Action::create("Add new Terminal", { Mod_Ctrl | Mod_Alt, Key_T },
  457. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"),
  458. [&](auto&) {
  459. auto& terminal = s_action_tab_widget->add_tab<TerminalWrapper>("Terminal");
  460. reveal_action_tab(terminal);
  461. update_actions();
  462. terminal.terminal()->set_focus(true);
  463. });
  464. auto remove_current_terminal_action = GUI::Action::create("Remove current Terminal", { Mod_Alt | Mod_Shift, Key_T }, [&](auto&) {
  465. auto widget = s_action_tab_widget->active_widget();
  466. if (!widget)
  467. return;
  468. if (strcmp(widget->class_name(), "TerminalWrapper") != 0)
  469. return;
  470. auto terminal = reinterpret_cast<TerminalWrapper*>(widget);
  471. if (!terminal->user_spawned())
  472. return;
  473. s_action_tab_widget->remove_tab(*terminal);
  474. update_actions();
  475. });
  476. auto& find_in_files_widget = s_action_tab_widget->add_tab<FindInFilesWidget>("Find in files");
  477. auto& terminal_wrapper = s_action_tab_widget->add_tab<TerminalWrapper>("Build", false);
  478. auto& debug_info_widget = s_action_tab_widget->add_tab<DebugInfoWidget>("Debug");
  479. auto& disassembly_widget = s_action_tab_widget->add_tab<DisassemblyWidget>("Disassembly");
  480. auto& locator = widget.add<Locator>();
  481. auto open_locator_action = GUI::Action::create("Open Locator...", { Mod_Ctrl, Key_K }, [&](auto&) {
  482. locator.open();
  483. });
  484. auto menubar = GUI::MenuBar::construct();
  485. auto& app_menu = menubar->add_menu("HackStudio");
  486. app_menu.add_action(open_action);
  487. app_menu.add_action(save_action);
  488. app_menu.add_separator();
  489. app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  490. app->quit();
  491. }));
  492. auto& project_menu = menubar->add_menu("Project");
  493. project_menu.add_action(new_action);
  494. project_menu.add_action(add_existing_file_action);
  495. auto& edit_menu = menubar->add_menu("Edit");
  496. 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&) {
  497. reveal_action_tab(find_in_files_widget);
  498. find_in_files_widget.focus_textbox_and_select_all();
  499. }));
  500. auto stop_action = GUI::Action::create("Stop", Gfx::Bitmap::load_from_file("/res/icons/16x16/program-stop.png"), [&](auto&) {
  501. terminal_wrapper.kill_running_command();
  502. });
  503. stop_action->set_enabled(false);
  504. terminal_wrapper.on_command_exit = [&] {
  505. stop_action->set_enabled(false);
  506. };
  507. auto build_action = GUI::Action::create("Build", { Mod_Ctrl, Key_B }, Gfx::Bitmap::load_from_file("/res/icons/16x16/build.png"), [&](auto&) {
  508. reveal_action_tab(terminal_wrapper);
  509. build(terminal_wrapper);
  510. stop_action->set_enabled(true);
  511. });
  512. toolbar.add_action(build_action);
  513. toolbar.add_separator();
  514. auto run_action = GUI::Action::create("Run", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/program-run.png"), [&](auto&) {
  515. reveal_action_tab(terminal_wrapper);
  516. run(terminal_wrapper);
  517. stop_action->set_enabled(true);
  518. });
  519. RefPtr<LibThread::Thread> debugger_thread;
  520. auto debug_action = GUI::Action::create("Debug", Gfx::Bitmap::load_from_file("/res/icons/16x16/debug-run.png"), [&](auto&) {
  521. if (g_project->type() != ProjectType::Cpp) {
  522. GUI::MessageBox::show(g_window, String::format("Cannot debug current project type", get_project_executable_path().characters()), "Error", GUI::MessageBox::Type::Error);
  523. return;
  524. }
  525. if (!GUI::FilePicker::file_exists(get_project_executable_path())) {
  526. 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);
  527. return;
  528. }
  529. if (Debugger::the().session()) {
  530. GUI::MessageBox::show(g_window, "Debugger is already running", "Error", GUI::MessageBox::Type::Error);
  531. return;
  532. }
  533. Debugger::the().set_executable_path(get_project_executable_path());
  534. debugger_thread = adopt(*new LibThread::Thread(Debugger::start_static));
  535. debugger_thread->start();
  536. });
  537. toolbar.add_separator();
  538. toolbar.add_action(debug_action);
  539. RefPtr<EditorWrapper> current_editor_in_execution;
  540. Debugger::initialize(
  541. [&](const PtraceRegisters& regs) {
  542. ASSERT(Debugger::the().session());
  543. const auto& debug_session = *Debugger::the().session();
  544. auto source_position = debug_session.debug_info().get_source_position(regs.eip);
  545. if (!source_position.has_value()) {
  546. dbg() << "Could not find source position for address: " << (void*)regs.eip;
  547. return Debugger::HasControlPassedToUser::No;
  548. }
  549. Core::EventLoop::main().post_event(
  550. *g_window,
  551. make<Core::DeferredInvocationEvent>(
  552. [&, source_position](auto&) {
  553. current_editor_in_execution = get_editor_of_file(source_position.value().file_path);
  554. current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1);
  555. debug_info_widget.update_state(*Debugger::the().session(), regs);
  556. debug_info_widget.set_debug_actions_enabled(true);
  557. disassembly_widget.update_state(*Debugger::the().session(), regs);
  558. reveal_action_tab(debug_info_widget);
  559. }));
  560. Core::EventLoop::wake();
  561. return Debugger::HasControlPassedToUser::Yes;
  562. },
  563. [&]() {
  564. Core::EventLoop::main().post_event(*g_window, make<Core::DeferredInvocationEvent>([&](auto&) {
  565. debug_info_widget.set_debug_actions_enabled(false);
  566. if (current_editor_in_execution) {
  567. current_editor_in_execution->editor().clear_execution_position();
  568. }
  569. }));
  570. Core::EventLoop::wake();
  571. },
  572. [&]() {
  573. Core::EventLoop::main().post_event(*g_window, make<Core::DeferredInvocationEvent>([&](auto&) {
  574. debug_info_widget.program_stopped();
  575. disassembly_widget.program_stopped();
  576. hide_action_tabs();
  577. GUI::MessageBox::show(g_window, "Program Exited", "Debugger", GUI::MessageBox::Type::Information);
  578. }));
  579. Core::EventLoop::wake();
  580. });
  581. auto& build_menu = menubar->add_menu("Build");
  582. build_menu.add_action(build_action);
  583. build_menu.add_separator();
  584. build_menu.add_action(run_action);
  585. build_menu.add_action(stop_action);
  586. build_menu.add_separator();
  587. build_menu.add_action(debug_action);
  588. auto& view_menu = menubar->add_menu("View");
  589. view_menu.add_action(hide_action_tabs_action);
  590. view_menu.add_action(open_locator_action);
  591. view_menu.add_separator();
  592. view_menu.add_action(add_editor_action);
  593. view_menu.add_action(remove_current_editor_action);
  594. view_menu.add_action(add_terminal_action);
  595. view_menu.add_action(remove_current_terminal_action);
  596. auto& help_menu = menubar->add_menu("Help");
  597. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  598. GUI::AboutDialog::show("HackStudio", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"), g_window);
  599. }));
  600. app->set_menubar(move(menubar));
  601. g_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  602. g_window->show();
  603. update_actions = [&]() {
  604. auto is_remove_terminal_enabled = []() {
  605. auto widget = s_action_tab_widget->active_widget();
  606. if (!widget)
  607. return false;
  608. if (strcmp(widget->class_name(), "TerminalWrapper") != 0)
  609. return false;
  610. if (!reinterpret_cast<TerminalWrapper*>(widget)->user_spawned())
  611. return false;
  612. return true;
  613. };
  614. remove_current_editor_action->set_enabled(g_all_editor_wrappers.size() > 1);
  615. remove_current_terminal_action->set_enabled(is_remove_terminal_enabled());
  616. };
  617. g_open_file = open_file;
  618. if (!argument_absolute_path.is_empty() && !argument_absolute_path.ends_with(".hsp"))
  619. open_file(argument_absolute_path);
  620. else
  621. open_file(g_project->default_file());
  622. update_actions();
  623. return app->exec();
  624. }
  625. void build(TerminalWrapper& wrapper)
  626. {
  627. if (g_project->type() == ProjectType::JavaScript && g_currently_open_file.ends_with(".js"))
  628. wrapper.run_command(String::format("js -A %s", g_currently_open_file.characters()));
  629. else
  630. wrapper.run_command("make");
  631. }
  632. void run(TerminalWrapper& wrapper)
  633. {
  634. if (g_project->type() == ProjectType::JavaScript && g_currently_open_file.ends_with(".js"))
  635. wrapper.run_command(String::format("js %s", g_currently_open_file.characters()));
  636. else
  637. wrapper.run_command("make run");
  638. }
  639. void open_project(String filename)
  640. {
  641. LexicalPath lexical_path(filename);
  642. if (chdir(lexical_path.dirname().characters()) < 0) {
  643. perror("chdir");
  644. exit(1);
  645. }
  646. g_project = Project::load_from_file(filename);
  647. ASSERT(g_project);
  648. if (g_project_tree_view) {
  649. g_project_tree_view->set_model(g_project->model());
  650. g_project_tree_view->toggle_index(g_project_tree_view->model()->index(0, 0));
  651. g_project_tree_view->update();
  652. }
  653. if (Debugger::is_initialized()) {
  654. Debugger::the().reset_breakpoints();
  655. }
  656. }
  657. void open_file(const String& filename)
  658. {
  659. auto project_file = g_project->get_file(filename);
  660. if (project_file) {
  661. current_editor().set_document(const_cast<GUI::TextDocument&>(project_file->document()));
  662. current_editor().set_mode(GUI::TextEditor::Editable);
  663. } else {
  664. auto external_file = ProjectFile::construct_with_name(filename);
  665. current_editor().set_document(const_cast<GUI::TextDocument&>(external_file->document()));
  666. current_editor().set_mode(GUI::TextEditor::ReadOnly);
  667. }
  668. if (filename.ends_with(".cpp") || filename.ends_with(".h"))
  669. current_editor().set_syntax_highlighter(make<GUI::CppSyntaxHighlighter>());
  670. else if (filename.ends_with(".js"))
  671. current_editor().set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  672. else if (filename.ends_with(".ini"))
  673. current_editor().set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  674. else
  675. current_editor().set_syntax_highlighter(nullptr);
  676. if (filename.ends_with(".frm")) {
  677. set_edit_mode(EditMode::Form);
  678. } else {
  679. set_edit_mode(EditMode::Text);
  680. }
  681. g_currently_open_file = filename;
  682. g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
  683. g_project_tree_view->update();
  684. current_editor_wrapper().filename_label().set_text(filename);
  685. current_editor().set_focus(true);
  686. }
  687. bool make_is_available()
  688. {
  689. pid_t pid;
  690. const char* argv[] = { "make", "--version", nullptr };
  691. posix_spawn_file_actions_t action;
  692. posix_spawn_file_actions_init(&action);
  693. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  694. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  695. perror("posix_spawn");
  696. return false;
  697. }
  698. int wstatus;
  699. waitpid(pid, &wstatus, 0);
  700. posix_spawn_file_actions_destroy(&action);
  701. return WEXITSTATUS(wstatus) == 0;
  702. }
  703. }
  704. int main(int argc, char** argv)
  705. {
  706. return main_impl(argc, argv);
  707. }