2020-01-18 09:38:21 +01:00
/*
2021-08-25 19:39:57 +02:00
* Copyright ( c ) 2018 - 2021 , Andreas Kling < kling @ serenityos . org >
2020-01-18 09:38:21 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 09:38:21 +01:00
*/
2022-03-12 20:40:32 +00:00
# include <AK/FixedArray.h>
2021-05-28 21:26:39 +02:00
# include <AK/QuickSort.h>
2021-01-03 08:39:46 +00:00
# include <AK/URL.h>
2021-08-25 19:39:57 +02:00
# include <LibConfig/Client.h>
2021-11-21 01:01:21 +01:00
# include <LibConfig/Listener.h>
2020-02-06 15:04:03 +01:00
# include <LibCore/ArgsParser.h>
2021-05-28 21:26:39 +02:00
# include <LibCore/DirIterator.h>
2022-08-20 17:01:53 +02:00
# include <LibCore/File.h>
2021-11-23 10:59:50 +01:00
# include <LibCore/System.h>
2021-01-03 08:39:46 +00:00
# include <LibDesktop/Launcher.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/Action.h>
# include <LibGUI/ActionGroup.h>
# include <LibGUI/Application.h>
# include <LibGUI/BoxLayout.h>
2020-12-28 11:30:19 +02:00
# include <LibGUI/Button.h>
# include <LibGUI/CheckBox.h>
2021-05-28 21:26:39 +02:00
# include <LibGUI/ComboBox.h>
2020-12-28 11:30:19 +02:00
# include <LibGUI/Event.h>
2020-11-02 19:30:17 +00:00
# include <LibGUI/Icon.h>
2021-05-28 21:26:39 +02:00
# include <LibGUI/ItemListModel.h>
2020-02-15 01:56:30 +01:00
# include <LibGUI/Menu.h>
2021-04-13 16:18:20 +02:00
# include <LibGUI/Menubar.h>
2022-05-04 16:28:43 +01:00
# include <LibGUI/MessageBox.h>
2022-05-11 19:41:33 +01:00
# include <LibGUI/Process.h>
2020-12-28 11:30:19 +02:00
# include <LibGUI/TextBox.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/Widget.h>
# include <LibGUI/Window.h>
2022-04-09 09:28:38 +02:00
# include <LibGfx/Font/FontDatabase.h>
2020-02-15 01:18:12 +01:00
# include <LibGfx/Palette.h>
2021-11-22 16:12:40 +01:00
# include <LibMain/Main.h>
2019-10-21 19:50:07 +02:00
# include <LibVT/TerminalWidget.h>
2019-06-07 11:48:03 +02:00
# include <assert.h>
# include <errno.h>
2021-04-30 03:40:38 +02:00
# include <pty.h>
2019-06-07 11:48:03 +02:00
# include <pwd.h>
2019-12-02 16:50:10 +01:00
# include <signal.h>
2019-06-07 11:48:03 +02:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <sys/ioctl.h>
2020-11-29 21:03:42 -07:00
# include <sys/wait.h>
2019-06-07 11:48:03 +02:00
# include <unistd.h>
2019-01-15 04:30:55 +01:00
2021-11-21 01:01:21 +01:00
class TerminalChangeListener : public Config : : Listener {
public :
TerminalChangeListener ( VT : : TerminalWidget & parent_terminal )
: m_parent_terminal ( parent_terminal )
{
}
2022-01-14 20:47:43 -08:00
virtual void config_bool_did_change ( String const & domain , String const & group , String const & key , bool value ) override
{
VERIFY ( domain = = " Terminal " ) ;
2022-05-04 21:36:07 +01:00
if ( group = = " Terminal " ) {
if ( key = = " ShowScrollBar " )
m_parent_terminal . set_show_scrollbar ( value ) ;
else if ( key = = " ConfirmClose " & & on_confirm_close_changed )
on_confirm_close_changed ( value ) ;
2022-05-12 23:08:51 +02:00
} else if ( group = = " Cursor " & & key = = " Blinking " ) {
m_parent_terminal . set_cursor_blinking ( value ) ;
2022-01-14 20:47:43 -08:00
}
}
2021-12-04 10:09:09 +01:00
virtual void config_string_did_change ( String const & domain , String const & group , String const & key , String const & value ) override
2021-11-21 01:01:21 +01:00
{
VERIFY ( domain = = " Terminal " ) ;
if ( group = = " Window " ) {
if ( key = = " Bell " ) {
auto bell_mode = VT : : TerminalWidget : : BellMode : : Visible ;
if ( value = = " AudibleBeep " )
bell_mode = VT : : TerminalWidget : : BellMode : : AudibleBeep ;
if ( value = = " Visible " )
bell_mode = VT : : TerminalWidget : : BellMode : : Visible ;
if ( value = = " Disabled " )
bell_mode = VT : : TerminalWidget : : BellMode : : Disabled ;
m_parent_terminal . set_bell_mode ( bell_mode ) ;
} else if ( key = = " ColorScheme " ) {
m_parent_terminal . set_color_scheme ( value ) ;
}
} else if ( group = = " Text " & & key = = " Font " ) {
auto font = Gfx : : FontDatabase : : the ( ) . get_by_name ( value ) ;
if ( font . is_null ( ) )
font = Gfx : : FontDatabase : : default_fixed_width_font ( ) ;
m_parent_terminal . set_font_and_resize_to_fit ( * font ) ;
2022-08-14 19:22:37 +02:00
m_parent_terminal . apply_size_increments_to_window ( * m_parent_terminal . window ( ) ) ;
2021-11-21 01:01:21 +01:00
m_parent_terminal . window ( ) - > resize ( m_parent_terminal . size ( ) ) ;
2022-05-12 23:08:51 +02:00
} else if ( group = = " Cursor " & & key = = " Shape " ) {
auto cursor_shape = VT : : TerminalWidget : : parse_cursor_shape ( value ) . value_or ( VT : : CursorShape : : Block ) ;
m_parent_terminal . set_cursor_shape ( cursor_shape ) ;
2021-11-21 01:01:21 +01:00
}
}
2021-12-04 10:09:09 +01:00
virtual void config_i32_did_change ( String const & domain , String const & group , String const & key , i32 value ) override
2021-11-21 01:01:21 +01:00
{
VERIFY ( domain = = " Terminal " ) ;
if ( group = = " Terminal " & & key = = " MaxHistorySize " ) {
m_parent_terminal . set_max_history_size ( value ) ;
} else if ( group = = " Window " & & key = = " Opacity " ) {
m_parent_terminal . set_opacity ( value ) ;
}
}
2022-05-04 21:36:07 +01:00
Function < void ( bool ) > on_confirm_close_changed ;
2021-11-21 01:01:21 +01:00
private :
VT : : TerminalWidget & m_parent_terminal ;
} ;
2021-11-24 23:25:03 +01:00
static void utmp_update ( String const & tty , pid_t pid , bool create )
2020-09-06 16:14:46 +02:00
{
int utmpupdate_pid = fork ( ) ;
if ( utmpupdate_pid < 0 ) {
perror ( " fork " ) ;
return ;
}
2020-11-29 21:03:42 -07:00
if ( utmpupdate_pid = = 0 ) {
// Be careful here! Because fork() only clones one thread it's
// possible that we deadlock on anything involving a mutex,
// including the heap! So resort to low-level APIs
char pid_str [ 32 ] ;
snprintf ( pid_str , sizeof ( pid_str ) , " %d " , pid ) ;
2021-11-24 23:25:03 +01:00
execl ( " /bin/utmpupdate " , " /bin/utmpupdate " , " -f " , " Terminal " , " -p " , pid_str , ( create ? " -c " : " -d " ) , tty . characters ( ) , nullptr ) ;
2020-11-29 21:03:42 -07:00
} else {
wait_again :
int status = 0 ;
if ( waitpid ( utmpupdate_pid , & status , 0 ) < 0 ) {
int err = errno ;
if ( err = = EINTR )
goto wait_again ;
perror ( " waitpid " ) ;
return ;
}
if ( WIFEXITED ( status ) & & WEXITSTATUS ( status ) ! = 0 )
dbgln ( " Terminal: utmpupdate exited with status {} " , WEXITSTATUS ( status ) ) ;
else if ( WIFSIGNALED ( status ) )
dbgln ( " Terminal: utmpupdate exited due to unhandled signal {} " , WTERMSIG ( status ) ) ;
}
2020-09-06 16:14:46 +02:00
}
2022-03-12 20:40:32 +00:00
static ErrorOr < void > run_command ( String command , bool keep_open )
2019-01-15 06:30:19 +01:00
{
2021-04-30 03:40:38 +02:00
String shell = " /bin/Shell " ;
auto * pw = getpwuid ( getuid ( ) ) ;
if ( pw & & pw - > pw_shell ) {
shell = pw - > pw_shell ;
2020-05-25 17:04:57 +04:30
}
2021-04-30 03:40:38 +02:00
endpwent ( ) ;
2020-05-25 17:04:57 +04:30
2022-03-12 20:40:32 +00:00
Vector < StringView > arguments ;
arguments . append ( shell ) ;
2021-04-30 03:40:38 +02:00
if ( ! command . is_empty ( ) ) {
2021-07-04 10:59:40 +02:00
if ( keep_open )
2022-07-11 17:32:29 +00:00
arguments . append ( " --keep-open " sv ) ;
arguments . append ( " -c " sv ) ;
2022-03-12 20:40:32 +00:00
arguments . append ( command ) ;
2021-04-30 03:40:38 +02:00
}
2022-08-20 17:01:53 +02:00
auto env = TRY ( FixedArray < StringView > : : try_create ( { " TERM=xterm " sv , " PAGER=more " sv , " PATH= " sv DEFAULT_PATH_SV } ) ) ;
2022-03-12 20:40:32 +00:00
TRY ( Core : : System : : exec ( shell , arguments , Core : : System : : SearchInPath : : No , env . span ( ) ) ) ;
2021-04-30 03:40:38 +02:00
VERIFY_NOT_REACHED ( ) ;
2019-01-15 06:30:19 +01:00
}
2019-01-15 04:30:55 +01:00
2021-11-24 23:05:25 +01:00
static ErrorOr < NonnullRefPtr < GUI : : Window > > create_find_window ( VT : : TerminalWidget & terminal )
2020-12-28 11:30:19 +02:00
{
2022-08-22 12:52:21 -04:00
auto window = TRY ( GUI : : Window : : try_create ( & terminal ) ) ;
window - > set_window_mode ( GUI : : WindowMode : : RenderAbove ) ;
2020-12-28 11:30:19 +02:00
window - > set_title ( " Find in Terminal " ) ;
window - > set_resizable ( false ) ;
window - > resize ( 300 , 90 ) ;
2021-11-24 23:05:25 +01:00
auto main_widget = TRY ( window - > try_set_main_widget < GUI : : Widget > ( ) ) ;
main_widget - > set_fill_with_background_color ( true ) ;
main_widget - > set_background_role ( ColorRole : : Button ) ;
2021-12-03 09:53:24 +00:00
( void ) TRY ( main_widget - > try_set_layout < GUI : : VerticalBoxLayout > ( ) ) ;
2021-11-24 23:05:25 +01:00
main_widget - > layout ( ) - > set_margins ( 4 ) ;
2020-12-28 11:30:19 +02:00
2021-11-24 23:05:25 +01:00
auto find = TRY ( main_widget - > try_add < GUI : : Widget > ( ) ) ;
2021-12-03 09:53:24 +00:00
( void ) TRY ( find - > try_set_layout < GUI : : HorizontalBoxLayout > ( ) ) ;
2021-11-24 23:05:25 +01:00
find - > layout ( ) - > set_margins ( 4 ) ;
find - > set_fixed_height ( 30 ) ;
2020-12-28 11:30:19 +02:00
2021-11-24 23:05:25 +01:00
auto find_textbox = TRY ( find - > try_add < GUI : : TextBox > ( ) ) ;
find_textbox - > set_fixed_width ( 230 ) ;
find_textbox - > set_focus ( true ) ;
2021-09-11 02:15:44 +03:00
if ( terminal . has_selection ( ) )
2022-07-11 17:32:29 +00:00
find_textbox - > set_text ( terminal . selected_text ( ) . replace ( " \n " sv , " " sv , ReplaceMode : : All ) ) ;
2021-11-24 23:05:25 +01:00
auto find_backwards = TRY ( find - > try_add < GUI : : Button > ( ) ) ;
find_backwards - > set_fixed_width ( 25 ) ;
2022-08-06 02:50:22 +02:00
find_backwards - > set_icon ( TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/upward-triangle.png " sv ) ) ) ;
2021-11-24 23:05:25 +01:00
auto find_forwards = TRY ( find - > try_add < GUI : : Button > ( ) ) ;
find_forwards - > set_fixed_width ( 25 ) ;
2022-08-06 02:50:22 +02:00
find_forwards - > set_icon ( TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/downward-triangle.png " sv ) ) ) ;
2021-11-24 23:05:25 +01:00
find_textbox - > on_return_pressed = [ find_backwards ] ( ) mutable {
find_backwards - > click ( ) ;
2020-12-28 11:30:19 +02:00
} ;
2021-11-24 23:05:25 +01:00
find_textbox - > on_shift_return_pressed = [ find_forwards ] ( ) mutable {
find_forwards - > click ( ) ;
2021-06-02 13:21:17 +02:00
} ;
2021-11-24 23:05:25 +01:00
auto match_case = TRY ( main_widget - > try_add < GUI : : CheckBox > ( " Case sensitive " ) ) ;
auto wrap_around = TRY ( main_widget - > try_add < GUI : : CheckBox > ( " Wrap around " ) ) ;
2020-12-28 11:30:19 +02:00
2021-11-24 23:05:25 +01:00
find_backwards - > on_click = [ & terminal , find_textbox , match_case , wrap_around ] ( auto ) mutable {
auto needle = find_textbox - > text ( ) ;
2020-12-28 11:30:19 +02:00
if ( needle . is_empty ( ) ) {
return ;
}
2021-11-24 23:05:25 +01:00
auto found_range = terminal . find_previous ( needle , terminal . normalized_selection ( ) . start ( ) , match_case - > is_checked ( ) , wrap_around - > is_checked ( ) ) ;
2020-12-28 11:30:19 +02:00
if ( found_range . is_valid ( ) ) {
terminal . scroll_to_row ( found_range . start ( ) . row ( ) ) ;
terminal . set_selection ( found_range ) ;
}
} ;
2021-11-24 23:32:43 +01:00
find_forwards - > on_click = [ & terminal , find_textbox , match_case , wrap_around ] ( auto ) {
2021-11-24 23:05:25 +01:00
auto needle = find_textbox - > text ( ) ;
2020-12-28 11:30:19 +02:00
if ( needle . is_empty ( ) ) {
return ;
}
2021-11-24 23:05:25 +01:00
auto found_range = terminal . find_next ( needle , terminal . normalized_selection ( ) . end ( ) , match_case - > is_checked ( ) , wrap_around - > is_checked ( ) ) ;
2020-12-28 11:30:19 +02:00
if ( found_range . is_valid ( ) ) {
terminal . scroll_to_row ( found_range . start ( ) . row ( ) ) ;
terminal . set_selection ( found_range ) ;
}
} ;
return window ;
}
2021-11-22 16:12:40 +01:00
ErrorOr < int > serenity_main ( Main : : Arguments arguments )
2019-01-16 12:57:07 +01:00
{
2021-11-27 14:26:34 -08:00
TRY ( Core : : System : : pledge ( " stdio tty rpath cpath wpath recvfd sendfd proc exec unix sigaction " ) ) ;
2020-01-11 21:07:18 +01:00
2019-12-02 16:50:10 +01:00
struct sigaction act ;
memset ( & act , 0 , sizeof ( act ) ) ;
act . sa_flags = SA_NOCLDWAIT ;
act . sa_handler = SIG_IGN ;
2021-11-23 10:59:50 +01:00
TRY ( Core : : System : : sigaction ( SIGCHLD , & act , nullptr ) ) ;
2019-02-11 14:56:23 +01:00
2021-11-24 22:54:49 +01:00
auto app = TRY ( GUI : : Application : : try_create ( arguments ) ) ;
2021-11-22 16:12:40 +01:00
2021-11-27 14:26:34 -08:00
TRY ( Core : : System : : pledge ( " stdio tty rpath cpath wpath recvfd sendfd proc exec unix " ) ) ;
2020-01-11 21:07:18 +01:00
2022-02-11 17:33:09 +01:00
Config : : pledge_domain ( " Terminal " ) ;
2021-08-25 20:06:54 +02:00
2022-04-01 20:58:27 +03:00
char const * command_to_execute = nullptr ;
2021-07-04 10:59:40 +02:00
bool keep_open = false ;
2019-09-02 15:28:52 +10:00
2020-02-02 12:34:39 +01:00
Core : : ArgsParser args_parser ;
2020-01-27 20:25:36 +03:00
args_parser . add_option ( command_to_execute , " Execute this command inside the terminal " , nullptr , ' e ' , " command " ) ;
2021-07-04 10:59:40 +02:00
args_parser . add_option ( keep_open , " Keep the terminal open after the command has finished executing " , nullptr , ' k ' ) ;
2020-03-15 18:20:19 +02:00
2021-11-22 19:03:05 -05:00
args_parser . parse ( arguments ) ;
2019-09-02 15:28:52 +10:00
2021-07-04 10:59:40 +02:00
if ( keep_open & & ! command_to_execute ) {
warnln ( " Option -k can only be used in combination with -e. " ) ;
return 1 ;
}
2021-05-06 13:52:46 +01:00
int ptm_fd ;
pid_t shell_pid = forkpty ( & ptm_fd , nullptr , nullptr , nullptr ) ;
2021-04-30 03:40:38 +02:00
if ( shell_pid < 0 ) {
perror ( " forkpty " ) ;
return 1 ;
}
if ( shell_pid = = 0 ) {
close ( ptm_fd ) ;
if ( command_to_execute )
2022-03-12 20:40:32 +00:00
TRY ( run_command ( command_to_execute , keep_open ) ) ;
2021-04-30 03:40:38 +02:00
else
2022-07-11 17:32:29 +00:00
TRY ( run_command ( Config : : read_string ( " Terminal " sv , " Startup " sv , " Command " sv , " " sv ) , false ) ) ;
2021-04-30 03:40:38 +02:00
VERIFY_NOT_REACHED ( ) ;
}
2020-09-06 16:14:46 +02:00
2021-11-24 23:25:03 +01:00
auto ptsname = TRY ( Core : : System : : ptsname ( ptm_fd ) ) ;
utmp_update ( ptsname , shell_pid , true ) ;
2019-01-15 06:30:19 +01:00
2022-07-11 17:32:29 +00:00
auto app_icon = GUI : : Icon : : default_icon ( " app-terminal " sv ) ;
2020-11-02 19:30:17 +00:00
2021-11-24 22:54:49 +01:00
auto window = TRY ( GUI : : Window : : try_create ( ) ) ;
2019-05-16 20:13:41 +02:00
window - > set_title ( " Terminal " ) ;
2022-06-29 03:18:47 +02:00
window - > set_obey_widget_min_size ( false ) ;
2019-01-15 04:30:55 +01:00
2021-11-24 22:54:49 +01:00
auto terminal = TRY ( window - > try_set_main_widget < VT : : TerminalWidget > ( ptm_fd , true ) ) ;
terminal - > on_command_exit = [ & ] {
2020-07-04 14:05:19 +02:00
app - > quit ( 0 ) ;
2019-10-22 21:57:53 +02:00
} ;
2021-11-24 22:54:49 +01:00
terminal - > on_title_change = [ & ] ( auto title ) {
2019-10-21 22:07:59 +02:00
window - > set_title ( title ) ;
} ;
2021-11-24 22:54:49 +01:00
terminal - > on_terminal_size_change = [ & ] ( auto & size ) {
2021-02-20 16:57:31 +01:00
window - > resize ( size ) ;
} ;
2021-11-24 22:54:49 +01:00
terminal - > apply_size_increments_to_window ( * window ) ;
2020-11-02 19:30:17 +00:00
window - > set_icon ( app_icon . bitmap_for_size ( 16 ) ) ;
2020-12-19 21:47:45 +00:00
2021-11-21 01:01:21 +01:00
Config : : monitor_domain ( " Terminal " ) ;
2022-07-11 17:32:29 +00:00
auto should_confirm_close = Config : : read_bool ( " Terminal " sv , " Terminal " sv , " ConfirmClose " sv , true ) ;
2021-11-21 01:01:21 +01:00
TerminalChangeListener listener { terminal } ;
2022-07-11 17:32:29 +00:00
auto bell = Config : : read_string ( " Terminal " sv , " Window " sv , " Bell " sv , " Visible " sv ) ;
2020-12-19 21:47:45 +00:00
if ( bell = = " AudibleBeep " ) {
2021-11-24 22:54:49 +01:00
terminal - > set_bell_mode ( VT : : TerminalWidget : : BellMode : : AudibleBeep ) ;
2020-12-19 21:47:45 +00:00
} else if ( bell = = " Disabled " ) {
2021-11-24 22:54:49 +01:00
terminal - > set_bell_mode ( VT : : TerminalWidget : : BellMode : : Disabled ) ;
2020-12-19 21:47:45 +00:00
} else {
2021-11-24 22:54:49 +01:00
terminal - > set_bell_mode ( VT : : TerminalWidget : : BellMode : : Visible ) ;
2020-12-19 21:47:45 +00:00
}
2019-02-10 14:28:39 +01:00
2022-07-11 17:32:29 +00:00
auto cursor_shape = VT : : TerminalWidget : : parse_cursor_shape ( Config : : read_string ( " Terminal " sv , " Cursor " sv , " Shape " sv , " Block " sv ) ) . value_or ( VT : : CursorShape : : Block ) ;
2022-05-12 23:08:51 +02:00
terminal - > set_cursor_shape ( cursor_shape ) ;
2022-07-11 17:32:29 +00:00
auto cursor_blinking = Config : : read_bool ( " Terminal " sv , " Cursor " sv , " Blinking " sv , true ) ;
2022-05-12 23:08:51 +02:00
terminal - > set_cursor_blinking ( cursor_blinking ) ;
2021-11-24 23:05:25 +01:00
auto find_window = TRY ( create_find_window ( terminal ) ) ;
2019-05-31 12:43:58 -07:00
2022-07-11 17:32:29 +00:00
auto new_opacity = Config : : read_i32 ( " Terminal " sv , " Window " sv , " Opacity " sv , 255 ) ;
2021-11-24 22:54:49 +01:00
terminal - > set_opacity ( new_opacity ) ;
2019-08-15 15:19:54 +02:00
window - > set_has_alpha_channel ( new_opacity < 255 ) ;
2019-05-25 16:43:15 -07:00
2022-07-11 17:32:29 +00:00
auto new_scrollback_size = Config : : read_i32 ( " Terminal " sv , " Terminal " sv , " MaxHistorySize " sv , terminal - > max_history_size ( ) ) ;
2021-11-24 22:54:49 +01:00
terminal - > set_max_history_size ( new_scrollback_size ) ;
2020-11-29 18:19:45 +03:30
2022-07-11 17:32:29 +00:00
auto show_scroll_bar = Config : : read_bool ( " Terminal " sv , " Terminal " sv , " ShowScrollBar " sv , true ) ;
2022-01-14 20:47:43 -08:00
terminal - > set_show_scrollbar ( show_scroll_bar ) ;
2022-08-14 19:16:49 +02:00
auto open_settings_action = GUI : : Action : : create ( " Terminal &Settings " , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/settings.png " sv ) ) ,
2020-12-30 17:52:19 +01:00
[ & ] ( auto & ) {
2022-07-11 17:32:29 +00:00
GUI : : Process : : spawn_or_show_error ( window , " /bin/TerminalSettings " sv ) ;
2020-12-30 17:52:19 +01:00
} ) ;
2021-11-24 22:54:49 +01:00
TRY ( terminal - > context_menu ( ) . try_add_separator ( ) ) ;
TRY ( terminal - > context_menu ( ) . try_add_action ( open_settings_action ) ) ;
2020-12-30 13:42:16 +01:00
2021-11-24 22:54:49 +01:00
auto file_menu = TRY ( window - > try_add_menu ( " &File " ) ) ;
2022-08-06 02:50:22 +02:00
TRY ( file_menu - > try_add_action ( GUI : : Action : : create ( " Open New &Terminal " , { Mod_Ctrl | Mod_Shift , Key_N } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/app-terminal.png " sv ) ) , [ & ] ( auto & ) {
2022-07-11 17:32:29 +00:00
GUI : : Process : : spawn_or_show_error ( window , " /bin/Terminal " sv ) ;
2021-11-24 22:54:49 +01:00
} ) ) ) ;
2020-12-30 13:42:16 +01:00
2021-11-24 22:54:49 +01:00
TRY ( file_menu - > try_add_action ( open_settings_action ) ) ;
TRY ( file_menu - > try_add_separator ( ) ) ;
2022-05-04 16:28:43 +01:00
auto tty_has_foreground_process = [ & ] {
pid_t fg_pid = tcgetpgrp ( ptm_fd ) ;
return fg_pid ! = - 1 & & fg_pid ! = shell_pid ;
} ;
auto shell_child_process_count = [ & ] {
Core : : DirIterator iterator ( String : : formatted ( " /proc/{}/children " , shell_pid ) , Core : : DirIterator : : Flags : : SkipParentAndBaseDir ) ;
int background_process_count = 0 ;
while ( iterator . has_next ( ) ) {
+ + background_process_count ;
( void ) iterator . next_path ( ) ;
}
return background_process_count ;
} ;
2022-05-13 13:10:27 +01:00
auto check_terminal_quit = [ & ] ( ) - > GUI : : Dialog : : ExecResult {
2022-05-04 21:36:07 +01:00
if ( ! should_confirm_close )
2022-05-13 13:10:27 +01:00
return GUI : : MessageBox : : ExecResult : : OK ;
2022-05-04 16:28:43 +01:00
Optional < String > close_message ;
if ( tty_has_foreground_process ( ) ) {
close_message = " There is still a process running in this terminal. Closing the terminal will kill it. " ;
} else {
auto child_process_count = shell_child_process_count ( ) ;
if ( child_process_count > 1 )
close_message = String : : formatted ( " There are {} background processes running in this terminal. Closing the terminal may kill them. " , child_process_count ) ;
else if ( child_process_count = = 1 )
close_message = " There is a background process running in this terminal. Closing the terminal may kill it. " ;
}
if ( close_message . has_value ( ) )
2022-07-11 17:32:29 +00:00
return GUI : : MessageBox : : show ( window , * close_message , " Close this terminal? " sv , GUI : : MessageBox : : Type : : Warning , GUI : : MessageBox : : InputType : : OKCancel ) ;
2022-05-13 13:10:27 +01:00
return GUI : : MessageBox : : ExecResult : : OK ;
2022-05-04 16:28:43 +01:00
} ;
TRY ( file_menu - > try_add_action ( GUI : : CommonActions : : make_quit_action ( [ & ] ( auto & ) {
2020-10-06 19:12:42 +02:00
dbgln ( " Terminal: Quit menu activated! " ) ;
2022-05-13 13:10:27 +01:00
if ( check_terminal_quit ( ) = = GUI : : MessageBox : : ExecResult : : OK )
2022-05-04 16:28:43 +01:00
GUI : : Application : : the ( ) - > quit ( ) ;
2021-11-24 22:54:49 +01:00
} ) ) ) ;
2019-02-11 15:37:12 +01:00
2021-11-24 22:54:49 +01:00
auto edit_menu = TRY ( window - > try_add_menu ( " &Edit " ) ) ;
TRY ( edit_menu - > try_add_action ( terminal - > copy_action ( ) ) ) ;
TRY ( edit_menu - > try_add_action ( terminal - > paste_action ( ) ) ) ;
TRY ( edit_menu - > try_add_separator ( ) ) ;
2022-08-06 02:50:22 +02:00
TRY ( edit_menu - > try_add_action ( GUI : : Action : : create ( " &Find... " , { Mod_Ctrl | Mod_Shift , Key_F } , TRY ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/find.png " sv ) ) ,
2020-12-28 11:30:19 +02:00
[ & ] ( auto & ) {
find_window - > show ( ) ;
find_window - > move_to_front ( ) ;
2021-11-24 22:54:49 +01:00
} ) ) ) ;
2019-11-20 21:33:23 +01:00
2021-11-24 22:54:49 +01:00
auto view_menu = TRY ( window - > try_add_menu ( " &View " ) ) ;
TRY ( view_menu - > try_add_action ( GUI : : CommonActions : : make_fullscreen_action ( [ & ] ( auto & ) {
2021-03-13 03:49:39 +00:00
window - > set_fullscreen ( ! window - > is_fullscreen ( ) ) ;
2021-11-24 22:54:49 +01:00
} ) ) ) ;
TRY ( view_menu - > try_add_action ( terminal - > clear_including_history_action ( ) ) ) ;
2019-02-12 08:39:19 +01:00
2021-11-24 22:54:49 +01:00
auto help_menu = TRY ( window - > try_add_menu ( " &Help " ) ) ;
TRY ( help_menu - > try_add_action ( GUI : : CommonActions : : make_help_action ( [ ] ( auto & ) {
2022-09-29 01:30:58 +02:00
Desktop : : Launcher : : open ( URL : : create_with_file_scheme ( " /usr/share/man/man1/Terminal.md " ) , " /bin/Help " ) ;
2021-11-24 22:54:49 +01:00
} ) ) ) ;
TRY ( help_menu - > try_add_action ( GUI : : CommonActions : : make_about_action ( " Terminal " , app_icon , window ) ) ) ;
2019-02-11 15:37:12 +01:00
2022-05-04 16:28:43 +01:00
window - > on_close_request = [ & ] ( ) - > GUI : : Window : : CloseRequestDecision {
2022-05-13 13:10:27 +01:00
if ( check_terminal_quit ( ) = = GUI : : MessageBox : : ExecResult : : OK )
2022-05-04 16:28:43 +01:00
return GUI : : Window : : CloseRequestDecision : : Close ;
return GUI : : Window : : CloseRequestDecision : : StayOpen ;
} ;
2022-09-06 00:04:06 -06:00
TRY ( Core : : System : : unveil ( " /proc/all " , " r " ) ) ;
2021-11-23 10:59:50 +01:00
TRY ( Core : : System : : unveil ( " /res " , " r " ) ) ;
TRY ( Core : : System : : unveil ( " /bin " , " r " ) ) ;
2022-05-04 16:28:43 +01:00
TRY ( Core : : System : : unveil ( " /proc " , " r " ) ) ;
2021-11-23 10:59:50 +01:00
TRY ( Core : : System : : unveil ( " /bin/Terminal " , " x " ) ) ;
2021-11-21 01:01:21 +01:00
TRY ( Core : : System : : unveil ( " /bin/TerminalSettings " , " x " ) ) ;
2021-11-23 10:59:50 +01:00
TRY ( Core : : System : : unveil ( " /bin/utmpupdate " , " x " ) ) ;
TRY ( Core : : System : : unveil ( " /etc/FileIconProvider.ini " , " r " ) ) ;
2022-09-06 00:04:06 -06:00
TRY ( Core : : System : : unveil ( " /tmp/session/%sid/portal/launch " , " rw " ) ) ;
TRY ( Core : : System : : unveil ( " /tmp/session/%sid/portal/config " , " rw " ) ) ;
2021-11-23 10:59:50 +01:00
TRY ( Core : : System : : unveil ( nullptr , nullptr ) ) ;
2020-01-21 12:12:15 +01:00
2022-05-04 16:28:43 +01:00
auto modified_state_check_timer = Core : : Timer : : create_repeating ( 500 , [ & ] {
window - > set_modified ( tty_has_foreground_process ( ) | | shell_child_process_count ( ) > 0 ) ;
} ) ;
2022-05-04 21:36:07 +01:00
listener . on_confirm_close_changed = [ & ] ( bool confirm_close ) {
if ( confirm_close ) {
modified_state_check_timer - > start ( ) ;
} else {
modified_state_check_timer - > stop ( ) ;
window - > set_modified ( false ) ;
}
should_confirm_close = confirm_close ;
} ;
2021-06-12 11:59:00 +02:00
window - > show ( ) ;
2022-05-04 21:36:07 +01:00
if ( should_confirm_close )
modified_state_check_timer - > start ( ) ;
2020-09-06 16:14:46 +02:00
int result = app - > exec ( ) ;
2020-10-06 19:12:42 +02:00
dbgln ( " Exiting terminal, updating utmp " ) ;
2021-11-24 23:25:03 +01:00
utmp_update ( ptsname , 0 , false ) ;
2020-09-06 16:14:46 +02:00
return result ;
2019-01-15 04:30:55 +01:00
}