App-Pimpd

 view release on metacpan or  search on metacpan

lib/App/Pimpd/Player.pm  view on Meta::CPAN

  exit(0) if($PID); #parent
  exit(1) if(!defined($PID)); # out of resources

  setsid();
  $PID = fork();
  exit(1) if(!defined($PID));

  if($PID) { # parent
    open(my $fh, '>', $pidfile_pimpd) or confess($!);
    print $fh $$;
    close($fh);

    waitpid($PID, 0);
    #unlink($pidfile); # remove the lock when child have confessd

    # Child have confessd/returned.
    # This means that MPD is in a state where it's not sending any data
    # We try to reconnect 20 times with a delay, and if the stream is still
    # down, we exit. See player_init()

    player_init();
    exit(0);
  }
  elsif($PID == 0) { # child
    open(my $fh, '>', "$pidfile_player") or confess("pidfile $pidfile_player: $!");
    print $fh $$;
    close($fh);
    open(STDOUT, '>>',  $daemon_log) unless $ENV{DEBUG};
    open(STDERR, '>', '/dev/null')   unless $ENV{DEBUG};
    open(STDIN,  '<', '/dev/null')   unless $ENV{DEBUG};
  }
  return 0;
}

sub play {
  player_destruct(); # FIXME
  $mpd->play;
  player_init(@_);

  #if(player_init() == 1) {
  #  $mpd->play;
  #}
  return;
}

sub stop {
  $mpd->stop;
  #unlink($player_tmp_log);
  player_destruct();

  return;
}

sub player_destruct {
  open(my $fh, '<', $pidfile_pimpd) or return 1; # for now
  my $pimpd_player = <$fh>;
  close($fh);

  if(kill(9, $pimpd_player)) {
    unlink($player_tmp_log);
    #printf("%s %s\n", fg('bold', $pimpd_player), 'terminated');
  }

  open(my $fh, '<', $pidfile_player) or confess($!);
  my $pimpd_target = <$fh>;
  close($fh);

  if(kill(9, $pimpd_target)) {
    #printf("%s %s\n", fg('bold', $pimpd_target, 'terminated'));
  }

  if(kill(9, $pimpd_target+1)) {
    #printf("%s %s\n", fg('bold', $pimpd_target + 1), 'terminated');
  }
  return 0;
}


1;

__END__

=pod

=head1 NAME

App::Pimpd::Player - Package exporting functions that helps with local playback

=head1 SYNOPSIS

    use App::Pimpd;
    use App::Pimpd::Player;

    if($play_music) {
      play();
    }
    elsif($time_to_sleep) {
      player_destruct();
    }

=head1 DESCRIPTION

App::Pimpd::Player provides functions that allows for local playback of music
playing on a remote MPD server.

=head1 EXPORTS

=over

=item play()

Starts remote and local playback.

=item stop()

Stops remote and local playback.

=back

=head1 SEE ALSO

App::Pimpd

=head1 AUTHOR

  Magnus Woldrich
  CPAN ID: WOLDRICH
  magnus@trapd00r.se
  http://japh.se

=head1 COPYRIGHT

Copyright (C) 2010, 2011 Magnus Woldrich. All right reserved.



( run in 1.272 second using v1.01-cache-2.11-cpan-39bf76dae61 )