------------------------------------------------------------------------------
-- Sound Package Parser                                                     --
--                                                                          --
-- Part of SparForte                                                        --
------------------------------------------------------------------------------
--                                                                          --
--            Copyright (C) 2001-2024 Free Software Foundation              --
--                                                                          --
-- This is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion.  This is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with this;  see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- This is maintained at http://www.pegasoft.ca                             --
--                                                                          --
------------------------------------------------------------------------------

with text_io;use text_io;
with gnat.source_info,
    ada.strings.unbounded,
    spar_os.sound,
    world,
    scanner,
    scanner.communications,
    parser,
    builtins;
use ada.strings.unbounded,
    spar_os,
    spar_os.sound,
    world,
    scanner,
    scanner.communications,
    parser,
    builtins;

package body parser_sound is

alreadyStarted : boolean := false;

procedure ParsePlay is
  expr_val  : unbounded_string;
  expr_type : identifier;
  pri_val   : unbounded_string := to_unbounded_string( integer'image( integer'last ) );
  pri_type  : identifier;
begin
  expect( sound_play_t );
  expect( symbol_t, "(" );
  ParseExpression( expr_val, expr_type );
  if uniTypesOK( expr_type, uni_string_t ) then
     if token = symbol_t and identifiers( token ).value.all = "," then
        getNextToken;
        ParseExpression( pri_val, pri_type );
        if uniTypesOK( pri_type, uni_numeric_t ) then
           null;
        end if;
     end if;
  end if;
  expect( symbol_t, ")" );
  if isExecutingCommand then
#if SOUND
     -- play must have full path (gstreamer requirement)
     if length( expr_val ) > 0 then
        if element( expr_val, 1 ) /= '/' then
           expr_val := current_working_directory & "/" & expr_val;
        end if;
     end if;
     declare
       path : constant string := to_string( expr_val );
       msg : unbounded_string;
       f : file_type;
       res : integer;
     begin
       -- the error checking isn't very good on gstreamer.  It's best to
       -- check for things like file existence ourselves.
       open( f, in_file, path );
       close( f );
       begin
         -- gstreamer uses a URI to play
         res := play_uri( "file://" & path & ASCII.NUL );
         if res /= 1 then
            msg := to_unbounded_string( "Sound error: " );
            for i in gst_error'range loop
                exit when gst_error(i) = ASCII.NUL;
                msg := msg & gst_error(i);
            end loop;
            err( unb_pl( msg ) );
         end if;
       exception when others =>
         err_exception_raised;
       end;
     exception
       when NAME_ERROR =>
         err( +"file " & em( path ) & pl( " does not exist or is not readable" ) );
       when STATUS_ERROR =>
         err( +"file " & em( path ) & pl( " may be locked" ) );
       when MODE_ERROR =>
         err( pl( gnat.source_info.source_location & ": internal error: mode error on file open" ) );
       when others =>
         err( +"exception when opening sound file " & em( path ) );
     end;
#else
      err( +"sound support not installed" );
#end if;
  end if;
end ParsePlay;

procedure ParsePlayCD is
  dev_name : unbounded_string := null_unbounded_string;
  dev_kind : identifier;
begin
  expect( sound_playcd_t );
  if token = symbol_t and identifiers( token ).value.all = "(" then
     expect( symbol_t, "(" );
     ParseExpression( dev_name, dev_kind );
     if baseTypesOK( dev_kind, string_t ) then
        expect( symbol_t, ")" );
     end if;
  end if;
  if isExecutingCommand then
#if SOUND
     PlayCD( dev_name );
#else
      err( +"sound support not installed" );
#end if;
  end if;
end ParsePlayCD;

procedure ParseStopCD is
begin
  expect( sound_stopcd_t );
  if isExecutingCommand then
#if SOUND
     StopCD;
#else
      err( +"sound support not installed" );
#end if;
  end if;
end ParseStopCD;

--procedure ParseMute is
--begin
--  expect( sound_mute_t );
--  if isExecutingCommand then
--#if SOUND
--     Mute;
--#else
--      +err( "sound support not installed" );
--#end if;
--  end if;
--end ParseMute;
--
--procedure ParseUnmute is
--begin
--  expect( sound_unmute_t );
--  if isExecutingCommand then
--#if SOUND
--     Unmute;
--#else
--      +err( "sound support not installed" );
--#end if;
--  end if;
--end ParseUnmute;

procedure StartupSound is
begin
#if SOUND
  if not alreadyStarted then
     startup_gstreamer;
     alreadyStarted := false;
  end if;
#end if;
  declareNamespace( "sound" );
  declareProcedure( sound_play_t, "sound.play", ParsePlay'access );
  declareProcedure( sound_playcd_t, "sound.playcd", ParsePlayCD'access );
  declareProcedure( sound_stopcd_t, "sound.stopcd", ParseStopCD'access );
  --declareProcedure( sound_mute_t, "sound.mute" );
  --declareProcedure( sound_unmute_t, "sound.unmute" );
  declareNamespaceClosed( "sound" );
end StartupSound;

procedure ShutdownSound is
begin
  -- shutdown_gstreamer may be alright to call but I'm not sure.  Like SDL,
  -- we'd like to initialize it once and leave it running.
  null; -- shutdown_gstreamer;
end ShutdownSound;

end parser_sound;
