2020-01-18 05:38:21 -03:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 05:38:21 -03:00
*/
2021-02-27 04:50:02 -03:00
# include "Editor.h"
2020-08-10 17:28:38 -03:00
# include "HackStudio.h"
2020-09-19 11:25:05 -03:00
# include "HackStudioWidget.h"
2019-10-21 13:46:55 -03:00
# include "Project.h"
2020-01-15 23:42:54 -03:00
# include <AK/StringBuilder.h>
2020-08-12 18:23:45 -03:00
# include <LibCore/ArgsParser.h>
2020-04-24 18:01:19 -03:00
# include <LibCore/Event.h>
# include <LibCore/EventLoop.h>
2020-02-06 11:04:03 -03:00
# include <LibCore/File.h>
2020-02-06 16:33:02 -03:00
# include <LibGUI/Application.h>
2021-04-13 11:18:20 -03:00
# include <LibGUI/Menubar.h>
2020-02-06 16:33:02 -03:00
# include <LibGUI/MessageBox.h>
2021-02-10 15:02:43 -03:00
# include <LibGUI/Notification.h>
2020-02-06 16:33:02 -03:00
# include <LibGUI/Widget.h>
# include <LibGUI/Window.h>
2020-04-24 18:01:19 -03:00
# include <LibThread/Lock.h>
# include <LibThread/Thread.h>
2020-04-05 19:03:15 -03:00
# include <LibVT/TerminalWidget.h>
2020-08-12 19:25:37 -03:00
# include <fcntl.h>
2020-06-28 14:40:10 -03:00
# include <spawn.h>
2019-10-21 13:46:55 -03:00
# include <stdio.h>
2020-08-12 19:25:37 -03:00
# include <sys/types.h>
2019-12-28 19:25:50 -03:00
# include <sys/wait.h>
2019-10-21 13:46:55 -03:00
# include <unistd.h>
2020-08-17 10:22:30 -03:00
using namespace HackStudio ;
2020-09-19 11:25:05 -03:00
static RefPtr < GUI : : Window > s_window ;
static RefPtr < HackStudioWidget > s_hack_studio_widget ;
2020-08-15 04:58:16 -03:00
2020-09-19 11:25:05 -03:00
static bool make_is_available ( ) ;
2021-02-10 15:02:43 -03:00
static void notify_make_not_available ( ) ;
2020-09-19 11:25:05 -03:00
static void update_path_environment_variable ( ) ;
2019-10-27 08:55:10 -03:00
2020-09-19 11:25:05 -03:00
int main ( int argc , char * * argv )
2019-10-21 13:46:55 -03:00
{
2021-01-17 04:18:11 -03:00
if ( pledge ( " stdio recvfd sendfd tty accept rpath cpath wpath proc exec unix fattr thread unix ptrace " , nullptr ) < 0 ) {
2020-01-11 17:17:56 -03:00
perror ( " pledge " ) ;
return 1 ;
}
2020-07-04 09:05:19 -03:00
auto app = GUI : : Application : : construct ( argc , argv ) ;
2019-10-21 13:46:55 -03:00
2021-01-17 04:18:11 -03:00
if ( pledge ( " stdio recvfd sendfd tty accept rpath cpath wpath proc exec fattr thread unix ptrace " , nullptr ) < 0 ) {
2020-01-12 07:59:11 -03:00
perror ( " pledge " ) ;
return 1 ;
}
2020-09-19 11:25:05 -03:00
s_window = GUI : : Window : : construct ( ) ;
s_window - > resize ( 840 , 600 ) ;
s_window - > set_icon ( Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-hack-studio.png " ) ) ;
2019-11-05 17:07:39 -03:00
2020-09-19 11:25:05 -03:00
update_path_environment_variable ( ) ;
2020-01-15 23:42:54 -03:00
2021-02-10 15:02:43 -03:00
if ( ! make_is_available ( ) ) {
notify_make_not_available ( ) ;
}
2019-12-28 19:25:50 -03:00
2020-08-12 19:25:37 -03:00
const char * path_argument = nullptr ;
2020-08-12 18:23:45 -03:00
Core : : ArgsParser args_parser ;
2020-08-12 19:25:37 -03:00
args_parser . add_positional_argument ( path_argument , " Path to a workspace or a file " , " path " , Core : : ArgsParser : : Required : : No ) ;
2020-08-12 18:23:45 -03:00
args_parser . parse ( argc , argv ) ;
2020-08-05 11:39:55 -03:00
2020-08-12 19:25:37 -03:00
auto argument_absolute_path = Core : : File : : real_path_for ( path_argument ) ;
2019-10-28 14:48:53 -03:00
2020-12-10 14:59:03 -03:00
auto project_path = argument_absolute_path ;
if ( argument_absolute_path . is_null ( ) )
project_path = Core : : File : : real_path_for ( " . " ) ;
2020-09-26 18:11:15 -03:00
s_hack_studio_widget = s_window - > set_main_widget < HackStudioWidget > ( project_path ) ;
2021-01-04 19:59:22 -03:00
s_window - > set_title ( String : : formatted ( " {} - Hack Studio " , s_hack_studio_widget - > project ( ) . name ( ) ) ) ;
2020-12-10 14:59:03 -03:00
2021-04-13 11:18:20 -03:00
auto menubar = GUI : : Menubar : : construct ( ) ;
2020-09-19 11:25:05 -03:00
s_hack_studio_widget - > initialize_menubar ( menubar ) ;
2021-03-25 17:41:39 -03:00
s_window - > set_menubar ( menubar ) ;
2020-04-04 07:18:40 -03:00
2021-05-01 07:42:07 -03:00
s_window - > on_close_request = [ & ] ( ) - > GUI : : Window : : CloseRequestDecision {
if ( s_hack_studio_widget - > warn_unsaved_changes ( " There are unsaved changes, do you want to save before exiting? " ) = = HackStudioWidget : : ContinueDecision : : Yes )
return GUI : : Window : : CloseRequestDecision : : Close ;
return GUI : : Window : : CloseRequestDecision : : StayOpen ;
} ;
2020-09-19 11:25:05 -03:00
s_window - > show ( ) ;
2019-10-21 17:13:20 -03:00
2020-09-19 11:25:05 -03:00
s_hack_studio_widget - > update_actions ( ) ;
2019-10-21 17:13:20 -03:00
2020-07-04 09:05:19 -03:00
return app - > exec ( ) ;
2019-10-21 13:46:55 -03:00
}
2019-10-22 17:15:43 -03:00
2020-09-19 11:25:05 -03:00
static bool make_is_available ( )
2019-10-22 17:15:43 -03:00
{
2020-09-19 11:25:05 -03:00
pid_t pid ;
const char * argv [ ] = { " make " , " --version " , nullptr } ;
posix_spawn_file_actions_t action ;
posix_spawn_file_actions_init ( & action ) ;
posix_spawn_file_actions_addopen ( & action , STDOUT_FILENO , " /dev/null " , O_WRONLY , 0 ) ;
if ( ( errno = posix_spawnp ( & pid , " make " , & action , nullptr , const_cast < char * * > ( argv ) , environ ) ) ) {
perror ( " posix_spawn " ) ;
return false ;
}
int wstatus ;
waitpid ( pid , & wstatus , 0 ) ;
posix_spawn_file_actions_destroy ( & action ) ;
return WEXITSTATUS ( wstatus ) = = 0 ;
2019-10-22 17:15:43 -03:00
}
2019-10-22 17:18:46 -03:00
2021-02-10 15:02:43 -03:00
static void notify_make_not_available ( )
{
auto notification = GUI : : Notification : : construct ( ) ;
2021-02-13 05:18:06 -03:00
notification - > set_icon ( Gfx : : Bitmap : : load_from_file ( " /res/icons/32x32/app-hack-studio.png " ) ) ;
2021-02-10 15:02:43 -03:00
notification - > set_title ( " 'make' Not Available " ) ;
notification - > set_text ( " You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository " ) ;
notification - > show ( ) ;
}
2020-09-19 11:25:05 -03:00
static void update_path_environment_variable ( )
2019-10-22 17:18:46 -03:00
{
2020-09-19 11:25:05 -03:00
StringBuilder path ;
path . append ( getenv ( " PATH " ) ) ;
if ( path . length ( ) )
path . append ( " : " ) ;
path . append ( " /bin:/usr/bin:/usr/local/bin " ) ;
setenv ( " PATH " , path . to_string ( ) . characters ( ) , true ) ;
2020-03-13 10:38:21 -03:00
}
2020-09-19 11:25:05 -03:00
namespace HackStudio {
2019-10-26 16:45:29 -03:00
2020-09-19 11:25:05 -03:00
GUI : : TextEditor & current_editor ( )
{
return s_hack_studio_widget - > current_editor ( ) ;
}
2019-10-27 08:55:10 -03:00
2021-04-29 16:46:15 -03:00
void open_file ( const String & filename )
2020-09-19 11:25:05 -03:00
{
2021-04-29 16:46:15 -03:00
s_hack_studio_widget - > open_file ( filename ) ;
2021-02-27 04:50:02 -03:00
}
2021-04-29 16:46:15 -03:00
void open_file ( const String & filename , size_t line , size_t column )
2021-02-27 04:50:02 -03:00
{
2021-04-29 16:46:15 -03:00
s_hack_studio_widget - > open_file ( filename ) ;
2021-02-27 04:50:02 -03:00
s_hack_studio_widget - > current_editor_wrapper ( ) . editor ( ) . set_cursor ( { line , column } ) ;
2019-10-23 15:54:41 -03:00
}
2019-12-28 19:25:50 -03:00
2020-09-19 11:25:05 -03:00
RefPtr < EditorWrapper > current_editor_wrapper ( )
2019-12-28 19:25:50 -03:00
{
2020-09-19 11:25:05 -03:00
if ( ! s_hack_studio_widget )
return nullptr ;
return s_hack_studio_widget - > current_editor_wrapper ( ) ;
}
2020-08-12 19:25:37 -03:00
2020-09-19 11:25:05 -03:00
Project & project ( )
{
return s_hack_studio_widget - > project ( ) ;
2019-12-28 19:25:50 -03:00
}
2020-08-17 10:22:30 -03:00
2020-09-19 11:25:05 -03:00
String currently_open_file ( )
{
if ( ! s_hack_studio_widget )
return { } ;
2021-05-01 07:04:19 -03:00
return s_hack_studio_widget - > active_file ( ) ;
2020-08-17 10:22:30 -03:00
}
2020-09-19 11:25:05 -03:00
void set_current_editor_wrapper ( RefPtr < EditorWrapper > wrapper )
2020-08-17 10:22:30 -03:00
{
2020-09-19 11:25:05 -03:00
s_hack_studio_widget - > set_current_editor_wrapper ( wrapper ) ;
}
2021-02-27 04:50:02 -03:00
Locator & locator ( )
{
return s_hack_studio_widget - > locator ( ) ;
}
2020-08-17 10:22:30 -03:00
}