2020-01-18 09:38:21 +01:00
/*
2021-02-16 17:44:40 +01:00
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
2020-01-18 09:38:21 +01:00
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
*
* 1. Redistributions of source code must retain the above copyright notice , this
* list of conditions and the following disclaimer .
*
* 2. Redistributions in binary form must reproduce the above copyright notice ,
* this list of conditions and the following disclaimer in the documentation
* and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
* AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL
* DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
* OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2021-01-03 08:39:46 +00:00
# include <AK/URL.h>
2020-12-31 23:38:39 +01:00
# include <Applications/Terminal/TerminalSettingsWindowGML.h>
2020-02-06 15:04:03 +01:00
# include <LibCore/ArgsParser.h>
2021-03-13 15:34:02 +01:00
# include <LibCore/ConfigFile.h>
# include <LibCore/File.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>
# include <LibGUI/Event.h>
2020-12-30 17:52:19 +01:00
# include <LibGUI/FontPicker.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/GroupBox.h>
2020-11-02 19:30:17 +00:00
# include <LibGUI/Icon.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>
2020-12-31 11:57:58 +01:00
# include <LibGUI/OpacitySlider.h>
2020-02-06 20:33:02 +01:00
# include <LibGUI/RadioButton.h>
2020-06-29 22:28:18 +02:00
# include <LibGUI/SpinBox.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>
2020-02-14 23:53:11 +01:00
# include <LibGfx/Font.h>
2020-02-15 01:18:12 +01:00
# include <LibGfx/Palette.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>
# include <fcntl.h>
# include <pwd.h>
2020-08-04 14:23:28 +02:00
# include <serenity.h>
2019-12-02 16:50:10 +01:00
# include <signal.h>
2020-06-28 13:40:10 -04:00
# include <spawn.h>
2019-06-07 11:48:03 +02:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <sys/ioctl.h>
# include <sys/select.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
2020-09-06 16:14:46 +02:00
static void utmp_update ( const char * tty , pid_t pid , bool create )
{
if ( ! tty )
return ;
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 ) ;
execl ( " /bin/utmpupdate " , " /bin/utmpupdate " , " -f " , " Terminal " , " -p " , pid_str , ( create ? " -c " : " -d " ) , tty , nullptr ) ;
} 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
}
static pid_t run_command ( int ptm_fd , String command )
2019-01-15 06:30:19 +01:00
{
pid_t pid = fork ( ) ;
2020-05-25 17:04:57 +04:30
if ( pid < 0 ) {
perror ( " fork " ) ;
2020-10-06 19:12:42 +02:00
dbgln ( " run_command: could not fork to run '{}' " , command ) ;
2020-09-06 16:14:46 +02:00
return pid ;
2020-05-25 17:04:57 +04:30
}
2019-01-15 06:30:19 +01:00
if ( pid = = 0 ) {
const char * tty_name = ptsname ( ptm_fd ) ;
if ( ! tty_name ) {
perror ( " ptsname " ) ;
exit ( 1 ) ;
}
close ( ptm_fd ) ;
int pts_fd = open ( tty_name , O_RDWR ) ;
2019-02-12 11:25:25 +01:00
if ( pts_fd < 0 ) {
perror ( " open " ) ;
2019-01-15 08:49:24 +01:00
exit ( 1 ) ;
}
2019-02-12 11:25:25 +01:00
2020-01-25 14:52:06 +01:00
if ( setsid ( ) < 0 ) {
perror ( " setsid " ) ;
}
2019-02-12 11:25:25 +01:00
2019-01-15 06:30:19 +01:00
close ( 0 ) ;
close ( 1 ) ;
close ( 2 ) ;
2019-02-12 11:25:25 +01:00
int rc = dup2 ( pts_fd , 0 ) ;
if ( rc < 0 ) {
perror ( " dup2 " ) ;
exit ( 1 ) ;
}
rc = dup2 ( pts_fd , 1 ) ;
if ( rc < 0 ) {
perror ( " dup2 " ) ;
exit ( 1 ) ;
}
rc = dup2 ( pts_fd , 2 ) ;
if ( rc < 0 ) {
perror ( " dup2 " ) ;
exit ( 1 ) ;
}
rc = close ( pts_fd ) ;
if ( rc < 0 ) {
perror ( " close " ) ;
exit ( 1 ) ;
}
2019-01-15 08:49:24 +01:00
rc = ioctl ( 0 , TIOCSCTTY ) ;
if ( rc < 0 ) {
perror ( " ioctl(TIOCSCTTY) " ) ;
exit ( 1 ) ;
}
2020-01-25 12:25:43 +01:00
String shell = " /bin/Shell " ;
auto * pw = getpwuid ( getuid ( ) ) ;
if ( pw & & pw - > pw_shell ) {
shell = pw - > pw_shell ;
}
endpwent ( ) ;
const char * args [ 4 ] = { shell . characters ( ) , nullptr , nullptr , nullptr } ;
2019-09-02 15:28:52 +10:00
if ( ! command . is_empty ( ) ) {
args [ 1 ] = " -c " ;
args [ 2 ] = command . characters ( ) ;
}
2021-03-03 20:36:31 +01:00
const char * envs [ ] = { " TERM=xterm " , " PAGER=more " , " PATH=/bin:/usr/bin:/usr/local/bin " , nullptr } ;
2020-01-25 12:25:43 +01:00
rc = execve ( shell . characters ( ) , const_cast < char * * > ( args ) , const_cast < char * * > ( envs ) ) ;
2019-01-15 06:30:19 +01:00
if ( rc < 0 ) {
perror ( " execve " ) ;
exit ( 1 ) ;
}
2021-02-23 20:42:32 +01:00
VERIFY_NOT_REACHED ( ) ;
2019-01-15 06:30:19 +01:00
}
2020-09-06 16:14:46 +02:00
return pid ;
2019-01-15 06:30:19 +01:00
}
2019-01-15 04:30:55 +01:00
2021-02-27 17:49:08 +01:00
static RefPtr < GUI : : Window > create_settings_window ( VT : : TerminalWidget & terminal )
2019-05-31 13:28:47 -07:00
{
2021-02-16 16:20:39 +01:00
auto window = GUI : : Window : : construct ( ) ;
window - > set_window_type ( GUI : : WindowType : : ToolWindow ) ;
2020-12-30 13:42:16 +01:00
window - > set_title ( " Terminal settings " ) ;
2020-03-10 22:08:25 +01:00
window - > set_resizable ( false ) ;
2020-12-30 13:15:23 +01:00
window - > resize ( 200 , 210 ) ;
2021-02-16 16:20:39 +01:00
window - > center_within ( * terminal . window ( ) ) ;
2019-05-31 14:51:06 -07:00
2020-03-04 09:46:23 +01:00
auto & settings = window - > set_main_widget < GUI : : Widget > ( ) ;
2020-12-31 23:38:39 +01:00
settings . load_from_gml ( terminal_settings_window_gml ) ;
2020-03-04 19:07:55 +01:00
2021-01-01 00:57:48 -07:00
auto & beep_bell_radio = * settings . find_descendant_of_type_named < GUI : : RadioButton > ( " beep_bell_radio " ) ;
auto & visual_bell_radio = * settings . find_descendant_of_type_named < GUI : : RadioButton > ( " visual_bell_radio " ) ;
auto & no_bell_radio = * settings . find_descendant_of_type_named < GUI : : RadioButton > ( " no_bell_radio " ) ;
2020-12-19 21:47:45 +00:00
switch ( terminal . bell_mode ( ) ) {
2021-02-27 17:49:08 +01:00
case VT : : TerminalWidget : : BellMode : : Visible :
2020-12-31 23:38:39 +01:00
visual_bell_radio . set_checked ( true ) ;
2020-12-19 21:47:45 +00:00
break ;
2021-02-27 17:49:08 +01:00
case VT : : TerminalWidget : : BellMode : : AudibleBeep :
2020-12-31 23:38:39 +01:00
beep_bell_radio . set_checked ( true ) ;
2020-12-19 21:47:45 +00:00
break ;
2021-02-27 17:49:08 +01:00
case VT : : TerminalWidget : : BellMode : : Disabled :
2020-12-31 23:38:39 +01:00
no_bell_radio . set_checked ( true ) ;
2020-12-19 21:47:45 +00:00
break ;
}
2020-12-31 23:38:39 +01:00
beep_bell_radio . on_checked = [ & terminal ] ( bool ) {
2021-02-27 17:49:08 +01:00
terminal . set_bell_mode ( VT : : TerminalWidget : : BellMode : : AudibleBeep ) ;
2020-12-19 21:47:45 +00:00
} ;
2020-12-31 23:38:39 +01:00
visual_bell_radio . on_checked = [ & terminal ] ( bool ) {
2021-02-27 17:49:08 +01:00
terminal . set_bell_mode ( VT : : TerminalWidget : : BellMode : : Visible ) ;
2020-12-19 21:47:45 +00:00
} ;
2020-12-31 23:38:39 +01:00
no_bell_radio . on_checked = [ & terminal ] ( bool ) {
2021-02-27 17:49:08 +01:00
terminal . set_bell_mode ( VT : : TerminalWidget : : BellMode : : Disabled ) ;
2019-06-07 11:48:03 +02:00
} ;
2019-05-31 13:28:47 -07:00
2021-01-01 00:57:48 -07:00
auto & slider = * settings . find_descendant_of_type_named < GUI : : OpacitySlider > ( " background_opacity_slider " ) ;
2020-12-30 15:11:46 +01:00
slider . on_change = [ & terminal ] ( int value ) {
2019-06-28 21:41:13 +02:00
terminal . set_opacity ( value ) ;
2019-06-07 11:48:03 +02:00
} ;
2020-03-04 19:07:55 +01:00
slider . set_value ( terminal . opacity ( ) ) ;
2019-05-31 13:28:47 -07:00
2021-01-01 00:57:48 -07:00
auto & history_size_spinbox = * settings . find_descendant_of_type_named < GUI : : SpinBox > ( " history_size_spinbox " ) ;
2020-11-29 18:19:45 +03:30
history_size_spinbox . set_value ( terminal . max_history_size ( ) ) ;
history_size_spinbox . on_change = [ & terminal ] ( int value ) {
terminal . set_max_history_size ( value ) ;
} ;
2019-05-31 14:51:06 -07:00
return window ;
2019-05-31 13:28:47 -07:00
}
2021-02-27 17:49:08 +01:00
static RefPtr < GUI : : Window > create_find_window ( VT : : TerminalWidget & terminal )
2020-12-28 11:30:19 +02:00
{
auto window = GUI : : Window : : construct ( ) ;
2021-02-16 16:20:39 +01:00
window - > set_window_type ( GUI : : WindowType : : ToolWindow ) ;
2020-12-28 11:30:19 +02:00
window - > set_title ( " Find in Terminal " ) ;
window - > set_resizable ( false ) ;
window - > resize ( 300 , 90 ) ;
auto & search = window - > set_main_widget < GUI : : Widget > ( ) ;
search . set_fill_with_background_color ( true ) ;
search . set_background_role ( ColorRole : : Button ) ;
search . set_layout < GUI : : VerticalBoxLayout > ( ) ;
search . layout ( ) - > set_margins ( { 4 , 4 , 4 , 4 } ) ;
auto & find = search . add < GUI : : Widget > ( ) ;
find . set_layout < GUI : : HorizontalBoxLayout > ( ) ;
find . layout ( ) - > set_margins ( { 4 , 4 , 4 , 4 } ) ;
2020-12-30 01:23:32 +01:00
find . set_fixed_height ( 30 ) ;
2020-12-28 11:30:19 +02:00
auto & find_textbox = find . add < GUI : : TextBox > ( ) ;
2020-12-30 01:23:32 +01:00
find_textbox . set_fixed_width ( 230 ) ;
2020-12-28 11:30:19 +02:00
find_textbox . set_focus ( true ) ;
if ( terminal . has_selection ( ) ) {
String selected_text = terminal . selected_text ( ) ;
selected_text . replace ( " \n " , " " , true ) ;
find_textbox . set_text ( selected_text ) ;
}
auto & find_backwards = find . add < GUI : : Button > ( ) ;
2020-12-30 01:23:32 +01:00
find_backwards . set_fixed_width ( 25 ) ;
2020-12-28 11:30:19 +02:00
find_backwards . set_icon ( Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/upward-triangle.png " ) ) ;
auto & find_forwards = find . add < GUI : : Button > ( ) ;
2020-12-30 01:23:32 +01:00
find_forwards . set_fixed_width ( 25 ) ;
2020-12-28 11:30:19 +02:00
find_forwards . set_icon ( Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/downward-triangle.png " ) ) ;
find_textbox . on_return_pressed = [ & ] ( ) {
find_backwards . click ( ) ;
} ;
auto & match_case = search . add < GUI : : CheckBox > ( " Case sensitive " ) ;
auto & wrap_around = search . add < GUI : : CheckBox > ( " Wrap around " ) ;
find_backwards . on_click = [ & ] ( auto ) {
auto needle = find_textbox . text ( ) ;
if ( needle . is_empty ( ) ) {
return ;
}
auto found_range = terminal . find_previous ( needle , terminal . normalized_selection ( ) . start ( ) , match_case . is_checked ( ) , wrap_around . is_checked ( ) ) ;
if ( found_range . is_valid ( ) ) {
terminal . scroll_to_row ( found_range . start ( ) . row ( ) ) ;
terminal . set_selection ( found_range ) ;
}
} ;
find_forwards . on_click = [ & ] ( auto ) {
auto needle = find_textbox . text ( ) ;
if ( needle . is_empty ( ) ) {
return ;
}
auto found_range = terminal . find_next ( needle , terminal . normalized_selection ( ) . end ( ) , match_case . is_checked ( ) , wrap_around . is_checked ( ) ) ;
if ( found_range . is_valid ( ) ) {
terminal . scroll_to_row ( found_range . start ( ) . row ( ) ) ;
terminal . set_selection ( found_range ) ;
}
} ;
return window ;
}
2019-02-11 14:56:23 +01:00
int main ( int argc , char * * argv )
2019-01-16 12:57:07 +01:00
{
2021-01-16 17:42:31 +01:00
if ( pledge ( " stdio tty rpath accept cpath wpath recvfd sendfd proc exec unix fattr sigaction " , nullptr ) < 0 ) {
2020-01-11 21:07:18 +01:00
perror ( " pledge " ) ;
return 1 ;
}
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 ;
int rc = sigaction ( SIGCHLD , & act , nullptr ) ;
if ( rc < 0 ) {
perror ( " sigaction " ) ;
return 1 ;
}
2020-07-04 14:05:19 +02:00
auto app = GUI : : Application : : construct ( argc , argv ) ;
2019-02-11 14:56:23 +01:00
2021-01-16 17:42:31 +01:00
if ( pledge ( " stdio tty rpath accept cpath wpath recvfd sendfd proc exec unix " , nullptr ) < 0 ) {
2020-01-11 21:07:18 +01:00
perror ( " pledge " ) ;
return 1 ;
}
2020-02-03 20:42:46 -06:00
const char * command_to_execute = nullptr ;
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 " ) ;
2020-03-15 18:20:19 +02:00
2020-01-27 20:25:36 +03:00
args_parser . parse ( argc , argv ) ;
2019-09-02 15:28:52 +10:00
2020-02-05 21:17:41 +01:00
int ptm_fd = posix_openpt ( O_RDWR | O_CLOEXEC ) ;
2019-01-16 13:36:10 +01:00
if ( ptm_fd < 0 ) {
2020-02-05 21:17:41 +01:00
perror ( " posix_openpt " ) ;
return 1 ;
}
if ( grantpt ( ptm_fd ) < 0 ) {
perror ( " grantpt " ) ;
return 1 ;
}
if ( unlockpt ( ptm_fd ) < 0 ) {
perror ( " unlockpt " ) ;
2019-01-16 13:36:10 +01:00
return 1 ;
}
2019-01-15 06:30:19 +01:00
2020-05-02 21:28:26 +12:00
RefPtr < Core : : ConfigFile > config = Core : : ConfigFile : : get_for_app ( " Terminal " ) ;
2021-03-13 15:34:02 +01:00
Core : : File : : ensure_parent_directories ( config - > file_name ( ) ) ;
2020-05-02 21:28:26 +12:00
2020-09-06 16:14:46 +02:00
pid_t shell_pid = 0 ;
2020-05-02 21:28:26 +12:00
if ( command_to_execute )
2020-09-06 16:14:46 +02:00
shell_pid = run_command ( ptm_fd , command_to_execute ) ;
2020-05-02 21:28:26 +12:00
else
2020-09-06 16:14:46 +02:00
shell_pid = run_command ( ptm_fd , config - > read_entry ( " Startup " , " Command " , " " ) ) ;
auto * pts_name = ptsname ( ptm_fd ) ;
utmp_update ( pts_name , shell_pid , true ) ;
2019-01-15 06:30:19 +01:00
2020-11-02 19:30:17 +00:00
auto app_icon = GUI : : Icon : : default_icon ( " app-terminal " ) ;
2020-02-02 15:07:41 +01:00
auto window = GUI : : Window : : construct ( ) ;
2019-05-16 20:13:41 +02:00
window - > set_title ( " Terminal " ) ;
2019-04-10 14:29:47 +02:00
window - > set_background_color ( Color : : Black ) ;
2019-03-17 04:23:54 +01:00
window - > set_double_buffering_enabled ( false ) ;
2019-01-15 04:30:55 +01:00
2021-02-27 17:49:08 +01:00
auto & terminal = window - > set_main_widget < VT : : TerminalWidget > ( ptm_fd , true , config ) ;
2020-03-04 09:46:23 +01:00
terminal . on_command_exit = [ & ] {
2020-07-04 14:05:19 +02:00
app - > quit ( 0 ) ;
2019-10-22 21:57:53 +02:00
} ;
2020-03-04 09:46:23 +01:00
terminal . on_title_change = [ & ] ( auto & title ) {
2019-10-21 22:07:59 +02:00
window - > set_title ( title ) ;
} ;
2021-02-20 16:57:31 +01:00
terminal . on_terminal_size_change = [ & ] ( auto & size ) {
window - > resize ( size ) ;
} ;
2020-03-04 09:46:23 +01:00
terminal . apply_size_increments_to_window ( * window ) ;
2019-02-10 14:28:39 +01:00
window - > show ( ) ;
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
auto bell = config - > read_entry ( " Window " , " Bell " , " Visible " ) ;
if ( bell = = " AudibleBeep " ) {
2021-02-27 17:49:08 +01:00
terminal . set_bell_mode ( VT : : TerminalWidget : : BellMode : : AudibleBeep ) ;
2020-12-19 21:47:45 +00:00
} else if ( bell = = " Disabled " ) {
2021-02-27 17:49:08 +01:00
terminal . set_bell_mode ( VT : : TerminalWidget : : BellMode : : Disabled ) ;
2020-12-19 21:47:45 +00:00
} else {
2021-02-27 17:49:08 +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
2020-02-02 15:07:41 +01:00
RefPtr < GUI : : Window > settings_window ;
2020-12-28 11:30:19 +02:00
RefPtr < GUI : : Window > find_window ;
2019-05-31 12:43:58 -07:00
2019-05-25 16:43:15 -07:00
auto new_opacity = config - > read_num_entry ( " Window " , " Opacity " , 255 ) ;
2020-03-04 09:46:23 +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
2020-11-29 18:19:45 +03:30
auto new_scrollback_size = config - > read_num_entry ( " Terminal " , " MaxHistorySize " , terminal . max_history_size ( ) ) ;
terminal . set_max_history_size ( new_scrollback_size ) ;
2021-04-21 19:03:55 +02:00
auto open_settings_action = GUI : : Action : : create ( " &Settings " , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/settings.png " ) ,
2020-12-30 13:42:16 +01:00
[ & ] ( const GUI : : Action & ) {
2021-02-16 16:20:39 +01:00
if ( ! settings_window )
2020-12-30 13:42:16 +01:00
settings_window = create_settings_window ( terminal ) ;
2021-02-16 16:20:39 +01:00
settings_window - > show ( ) ;
2020-12-30 13:42:16 +01:00
settings_window - > move_to_front ( ) ;
} ) ;
2020-12-30 17:52:19 +01:00
terminal . context_menu ( ) . add_separator ( ) ;
2021-04-09 15:55:28 +02:00
auto pick_font_action = GUI : : Action : : create ( " Terminal &Font... " , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-font-editor.png " ) ,
2020-12-30 17:52:19 +01:00
[ & ] ( auto & ) {
auto picker = GUI : : FontPicker : : construct ( window , & terminal . font ( ) , true ) ;
if ( picker - > exec ( ) = = GUI : : Dialog : : ExecOK ) {
2021-01-09 13:46:22 +01:00
terminal . set_font_and_resize_to_fit ( * picker - > font ( ) ) ;
window - > resize ( terminal . size ( ) ) ;
2020-12-30 17:52:19 +01:00
config - > write_entry ( " Text " , " Font " , picker - > font ( ) - > qualified_name ( ) ) ;
config - > sync ( ) ;
}
} ) ;
terminal . context_menu ( ) . add_action ( pick_font_action ) ;
2020-12-30 13:42:16 +01:00
terminal . context_menu ( ) . add_separator ( ) ;
terminal . context_menu ( ) . add_action ( open_settings_action ) ;
2021-04-13 16:18:20 +02:00
auto menubar = GUI : : Menubar : : construct ( ) ;
2019-02-11 15:37:12 +01:00
2021-04-05 23:06:46 +02:00
auto & app_menu = menubar - > add_menu ( " &File " ) ;
2021-04-09 15:55:28 +02:00
app_menu . add_action ( GUI : : Action : : create ( " Open New &Terminal " , { Mod_Ctrl | Mod_Shift , Key_N } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/app-terminal.png " ) , [ & ] ( auto & ) {
2020-06-28 13:40:10 -04:00
pid_t child ;
const char * argv [ ] = { " Terminal " , nullptr } ;
2020-08-04 14:23:28 +02:00
if ( ( errno = posix_spawn ( & child , " /bin/Terminal " , nullptr , nullptr , const_cast < char * * > ( argv ) , environ ) ) ) {
perror ( " posix_spawn " ) ;
} else {
if ( disown ( child ) < 0 )
perror ( " disown " ) ;
}
2019-11-20 21:19:17 +01:00
} ) ) ;
2020-12-30 13:42:16 +01:00
app_menu . add_action ( open_settings_action ) ;
2020-04-04 12:18:40 +02:00
app_menu . add_separator ( ) ;
app_menu . add_action ( GUI : : CommonActions : : make_quit_action ( [ ] ( auto & ) {
2020-10-06 19:12:42 +02:00
dbgln ( " Terminal: Quit menu activated! " ) ;
2020-07-04 16:52:01 +02:00
GUI : : Application : : the ( ) - > quit ( ) ;
2019-02-12 14:09:48 +01:00
} ) ) ;
2019-02-11 15:37:12 +01:00
2021-04-05 23:06:46 +02:00
auto & edit_menu = menubar - > add_menu ( " &Edit " ) ;
2020-04-04 12:18:40 +02:00
edit_menu . add_action ( terminal . copy_action ( ) ) ;
edit_menu . add_action ( terminal . paste_action ( ) ) ;
2020-12-28 11:30:19 +02:00
edit_menu . add_separator ( ) ;
2021-04-09 15:55:28 +02:00
edit_menu . add_action ( GUI : : Action : : create ( " &Find... " , { Mod_Ctrl | Mod_Shift , Key_F } , Gfx : : Bitmap : : load_from_file ( " /res/icons/16x16/find.png " ) ,
2020-12-28 11:30:19 +02:00
[ & ] ( auto & ) {
2021-02-16 16:20:39 +01:00
if ( ! find_window )
2020-12-28 11:30:19 +02:00
find_window = create_find_window ( terminal ) ;
find_window - > show ( ) ;
find_window - > move_to_front ( ) ;
} ) ) ;
2019-11-20 21:33:23 +01:00
2021-04-05 23:06:46 +02:00
auto & view_menu = menubar - > add_menu ( " &View " ) ;
2021-03-13 03:49:39 +00:00
view_menu . add_action ( GUI : : CommonActions : : make_fullscreen_action ( [ & ] ( auto & ) {
window - > set_fullscreen ( ! window - > is_fullscreen ( ) ) ;
} ) ) ;
2020-07-05 23:34:02 +02:00
view_menu . add_action ( terminal . clear_including_history_action ( ) ) ;
2020-12-30 17:52:19 +01:00
view_menu . add_separator ( ) ;
view_menu . add_action ( pick_font_action ) ;
2019-02-12 08:39:19 +01:00
2021-04-05 23:06:46 +02:00
auto & help_menu = menubar - > add_menu ( " &Help " ) ;
2021-01-03 08:39:46 +00:00
help_menu . add_action ( GUI : : CommonActions : : make_help_action ( [ ] ( auto & ) {
Desktop : : Launcher : : open ( URL : : create_with_file_protocol ( " /usr/share/man/man1/Terminal.md " ) , " /bin/Help " ) ;
} ) ) ;
2021-01-04 23:51:49 +01:00
help_menu . add_action ( GUI : : CommonActions : : make_about_action ( " Terminal " , app_icon , window ) ) ;
2019-02-11 15:37:12 +01:00
2021-03-25 21:41:39 +01:00
window - > set_menubar ( menubar ) ;
2019-02-11 15:37:12 +01:00
2020-01-21 12:12:15 +01:00
if ( unveil ( " /res " , " r " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
2020-12-24 16:10:33 +01:00
if ( unveil ( " /bin " , " r " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
2020-01-21 12:12:15 +01:00
if ( unveil ( " /bin/Terminal " , " x " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
2020-09-06 16:14:46 +02:00
if ( unveil ( " /bin/utmpupdate " , " x " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
2020-12-27 19:13:57 +01:00
if ( unveil ( " /etc/FileIconProvider.ini " , " r " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
2020-05-09 16:16:16 +02:00
if ( unveil ( " /tmp/portal/launch " , " rw " ) < 0 ) {
perror ( " unveil " ) ;
return 1 ;
}
2021-03-13 15:34:02 +01:00
if ( unveil ( config - > file_name ( ) . characters ( ) , " rwc " ) < 0 ) {
2020-01-21 13:08:22 +01:00
perror ( " unveil " ) ;
return 1 ;
}
2020-01-21 12:12:15 +01:00
unveil ( nullptr , nullptr ) ;
2019-05-25 16:43:15 -07:00
config - > sync ( ) ;
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 " ) ;
2020-09-06 16:14:46 +02:00
utmp_update ( pts_name , 0 , false ) ;
return result ;
2019-01-15 04:30:55 +01:00
}