Padre

 view release on metacpan or  search on metacpan

lib/Padre/Startup.pm  view on Meta::CPAN


=pod

=head1 NAME

Padre::Startup - Padre start-up related configuration settings

=head1 DESCRIPTION

Padre stores host-related data in a combination of an easily transportable
YAML file for personal settings and a powerful and robust SQLite-based
configuration database for host settings and state data.

Unfortunately, fully loading and validating these configurations can be
relatively expensive and may take some time. A limited number of these
settings need to be available extremely early in the Padre bootstrapping
process.

The F<startup.yml> file is automatically written at the same time as the
regular configuration files, and is read without validating during early start-up.

L<Padre::Startup::Config> is a small convenience module for reading and
writing the F<startup.yml> file.

=head1 FUNCTIONS

=cut

use 5.008005;
use strict;
use warnings;
use File::Spec      ();
use Padre::Constant ();

our $VERSION = '1.02';

my $SPLASH = undef;





#####################################################################
# Main Startup Procedure

# Runs the (as light as possible) startup process for Padre.
# Returns true if we should continue with the startup.
# Returns false if we should abort the startup and exit.
sub startup {

	# Start with the default settings
	my %setting = (
		main_singleinstance      => Padre::Constant::DEFAULT_SINGLEINSTANCE,
		main_singleinstance_port => Padre::Constant::DEFAULT_SINGLEINSTANCE_PORT,
		threads                  => 1,
		threads_stacksize        => 0,
		startup_splash           => 0,
		VERSION                  => 0,
	);

	# Load and overlay the startup.yml file
	if ( -f Padre::Constant::CONFIG_STARTUP ) {
		%setting = ( %setting, startup_config() );
	}

	# Attempt to connect to the single instance server
	if ( $setting{main_singleinstance} ) {

		# This blocks for about 1 second
		require IO::Socket;
		my $socket = IO::Socket::INET->new(
			PeerAddr => '127.0.0.1',
			PeerPort => $setting{main_singleinstance_port},
			Proto    => 'tcp',
			Type     => IO::Socket::SOCK_STREAM(),
		);
		if ($socket) {
			if (Padre::Constant::WIN32) {
				my $pid = '';
				my $read = $socket->sysread( $pid, 10 );
				if ( defined $read and $read == 10 ) {

					# Got the single instance PID
					$pid =~ s/\s+\s//;
					require Padre::Util::Win32;
					Padre::Util::Win32::AllowSetForegroundWindow($pid);
				}
			}
			foreach my $file (@ARGV) {
				my $path = File::Spec->rel2abs($file);
				$socket->print("open $path\n");
			}
			$socket->print("focus\n");
			$socket->close;
			return 0;
		}
	}

	if ( $setting{threads} ) {

		# Load a limited subset of Wx early so that we can be sure that
		# the Wx::PlThreadEvent works in child threads. The thread
		# modules must be loaded before Wx so that threading in Wx works
		require threads;
		require threads::shared;
		require Wx;

		# Allowing custom tuning of the stack size
		my $size = $setting{threads_stacksize};
		threads->set_stack_size($size) if $size;

		# Second-generation version of the threading optimisation, with
		# worker threads spawned of a single initial early spawned
		# "slave master" thread. This dramatically reduces the overhead
		# of spawning a thread, because it doesn't need to copy all the
		# stuff in the parent thread.
		require Padre::Wx::App;
		require Padre::TaskWorker;
		Padre::Wx::App->new;
		Padre::TaskWorker->master;
	}



( run in 1.511 second using v1.01-cache-2.11-cpan-99c4e6809bf )