/* gstreamermm - a C++ wrapper for gstreamer
 *
 * Copyright 2008 The gstreamermm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gstreamermm/mixeroptions.h>
#include <glibmm/vectorutils.h>

namespace Gst
{

MessageMixerMuteToggled::MessageMixerMuteToggled(GstMessage* castitem)
: Message(castitem)
{}

void MessageMixerMuteToggled::parse(Glib::RefPtr<Gst::MixerTrack>& track,
  bool& mute) const
{
  GstMixerTrack* gst_track = 0;
  gboolean gst_mute = 0;

  gst_mixer_message_parse_mute_toggled(const_cast<GstMessage*>(gobj()),
    &gst_track, &gst_mute);
  track = Glib::wrap(gst_track);
  mute = gst_mute;
}

Glib::RefPtr<Gst::MixerTrack> MessageMixerMuteToggled::parse()
{
  GstMixerTrack* gst_track = 0;
  gst_mixer_message_parse_mute_toggled(gobj(), &gst_track, 0);
  return Glib::wrap(gst_track);
}

Glib::RefPtr<const Gst::MixerTrack> MessageMixerMuteToggled::parse() const
{
  return parse();
}

bool MessageMixerMuteToggled::parse_mute() const
{
  gboolean gst_mute = 0;
  gst_mixer_message_parse_mute_toggled(const_cast<GstMessage*>(gobj()),
    0, &gst_mute);
  return gst_mute;
}

MessageMixerRecordToggled::MessageMixerRecordToggled(GstMessage* castitem)
: Message(castitem)
{}

void MessageMixerRecordToggled::parse(Glib::RefPtr<Gst::MixerTrack>& track,
  bool& record) const
{
  GstMixerTrack* gst_track = 0;
  gboolean gst_record = 0;

  gst_mixer_message_parse_record_toggled(const_cast<GstMessage*>(gobj()),
    &gst_track, &gst_record);
  track = Glib::wrap(gst_track);
  record = gst_record;
}

Glib::RefPtr<Gst::MixerTrack> MessageMixerRecordToggled::parse()
{
  GstMixerTrack* gst_track = 0;
  gst_mixer_message_parse_record_toggled(gobj(), &gst_track, 0);
  return Glib::wrap(gst_track);
}

Glib::RefPtr<const Gst::MixerTrack> MessageMixerRecordToggled::parse() const
{
  return parse();
}

bool MessageMixerRecordToggled::parse_record() const
{
  gboolean gst_record = 0;
  gst_mixer_message_parse_record_toggled(const_cast<GstMessage*>(gobj()),
    0, &gst_record);
  return gst_record;
}

MessageMixerVolumeChanged::MessageMixerVolumeChanged(GstMessage* castitem)
: Message(castitem)
{}

void MessageMixerVolumeChanged::parse(Glib::RefPtr<Gst::MixerTrack>& track,
  std::vector<int>& volumes) const
{
  GstMixerTrack* gst_track = 0;
  gint* gst_volumes = 0;
  gint gst_num_channels = 0;

  gst_mixer_message_parse_volume_changed(const_cast<GstMessage*>(gobj()),
    &gst_track, &gst_volumes, &gst_num_channels);

  track = Glib::wrap(gst_track);

  volumes = Glib::ArrayHandler<int>::array_to_vector(gst_volumes,
    gst_num_channels, Glib::OWNERSHIP_DEEP);
}

Glib::RefPtr<Gst::MixerTrack> MessageMixerVolumeChanged::parse()
{
  GstMixerTrack* gst_track = 0;
  gst_mixer_message_parse_volume_changed(gobj(), &gst_track, 0, 0);
  return Glib::wrap(gst_track);
}

Glib::RefPtr<const Gst::MixerTrack> MessageMixerVolumeChanged::parse() const
{
  return parse();
}

std::vector<int> MessageMixerVolumeChanged::parse_volumes() const
{
  gint* gst_volumes = 0;
  gint gst_num_channels = 0;

  gst_mixer_message_parse_volume_changed(const_cast<GstMessage*>(gobj()),
    0, &gst_volumes, &gst_num_channels);

  return Glib::ArrayHandle<int>(gst_volumes, gst_num_channels,
    Glib::OWNERSHIP_DEEP);
}

MessageMixerOptionChanged::MessageMixerOptionChanged(GstMessage* castitem)
: Message(castitem)
{}

void MessageMixerOptionChanged::parse(Glib::RefPtr<Gst::MixerOptions>& options,
  Glib::ustring& value) const
{
  GstMixerOptions* gst_options = 0;
  const gchar* gst_value = 0;

  gst_mixer_message_parse_option_changed(const_cast<GstMessage*>(gobj()),
    &gst_options, &gst_value);
  options = Glib::wrap(gst_options);
  value = gst_value;
}

Glib::RefPtr<Gst::MixerOptions> MessageMixerOptionChanged::parse()
{
  GstMixerOptions* gst_options = 0;
  gst_mixer_message_parse_option_changed(gobj(), &gst_options, 0);
  return Glib::wrap(gst_options);
}

Glib::RefPtr<const Gst::MixerOptions> MessageMixerOptionChanged::parse() const
{
  return parse();
}

Glib::ustring MessageMixerOptionChanged::parse_value() const
{
  const gchar* gst_value = 0;
  gst_mixer_message_parse_option_changed(const_cast<GstMessage*>(gobj()),
    0, &gst_value);
  return gst_value;
}

MessageMixerOptionsListChanged::MessageMixerOptionsListChanged(GstMessage* castitem)
: Message(castitem)
{}

Glib::RefPtr<Gst::MixerOptions> MessageMixerOptionsListChanged::parse()
{
  GstMixerOptions* gst_options = 0;
  gst_mixer_message_parse_options_list_changed(gobj(), &gst_options);
  return Glib::wrap(gst_options);
}

Glib::RefPtr<const Gst::MixerOptions> MessageMixerOptionsListChanged::parse()
  const
{
  return parse();
}

MessageMixerChanged::MessageMixerChanged(GstMessage* castitem)
: Message(castitem)
{}

std::vector<int> Mixer::get_volume(const Glib::RefPtr<const Gst::MixerTrack>& track) const
{
  const guint32 num_channels = track->get_num_channels();
  int* volumes = g_new(int, num_channels);

  gst_mixer_get_volume(const_cast<GstMixer*>(gobj()),
    const_cast<GstMixerTrack*>(Glib::unwrap(track)), volumes);

  return Glib::ArrayHandle<int>(volumes, num_channels, Glib::OWNERSHIP_DEEP);
}

// This vfunc wrapper is manually written, because we want the C++ vfunc to
// return the result instead of passing in a pointer in which to store it.

void Mixer_Class::get_volume_vfunc_callback(GstMixer* self, GstMixerTrack* track, gint* volumes)
{
  CppObjectType *const obj = dynamic_cast<CppObjectType*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj && obj->is_derived_())
  {
    try // Trap C++ exceptions which would normally be lost because this is a C callback.
    {
      // Call the virtual member method, which derived classes might override.
      std::vector<int> result = obj->get_volume_vfunc(Glib::wrap(track, true));
      for(guint i = 0; i < result.size(); i++)
      {
        volumes[i] = result[i];
      }
    }
    catch(...)
    {
      Glib::exception_handlers_invoke();
    }
  }
  else
  {
    BaseClassType *const base = static_cast<BaseClassType*>(
        g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
)    );

    // Call the original underlying C function:
    if(base && base->get_volume)
      return (*base->get_volume)(self, track, volumes);
  }
}
std::vector<int> Gst::Mixer::get_volume_vfunc(const Glib::RefPtr<const Gst::MixerTrack>& track) const
{
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
)  );

  if(base && base->get_volume)
  {
    const guint32 num_channels = track->get_num_channels();

    int* volumes = new int[num_channels];
    (*base->get_volume)(const_cast<GstMixer*>(gobj()),
      const_cast<GstMixerTrack*>(Glib::unwrap(track)), volumes);

    return Glib::ArrayHandle<int>(volumes, num_channels, Glib::OWNERSHIP_DEEP);
  }

  typedef Glib::ArrayHandle<int> RType;
  return RType(0, 0, Glib::OWNERSHIP_DEEP);
}
const GList* Mixer_Class::list_tracks_vfunc_callback(GstMixer* self)
{
  Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj_base && obj_base->is_derived_())
  {
    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Call the virtual member method, which derived classes might override.
        return
          Glib::ListHandler< Glib::RefPtr<Gst::MixerTrack> >::vector_to_list(
          obj->list_tracks_vfunc()).data();
      }
      catch(...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }
  
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
)  );

  // Call the original underlying C function:
  if(base && base->list_tracks)
    return (*base->list_tracks)(self);


  typedef const GList* RType;
  return RType();
}
std::vector< Glib::RefPtr<Gst::MixerTrack> > Gst::Mixer::list_tracks_vfunc()
{
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
)  );

  if(base && base->list_tracks)
    return Glib::ListHandle< Glib::RefPtr<Gst::MixerTrack> >(const_cast<GList*>((*base->list_tracks)(const_cast<GstMixer*>(gobj()))), Glib::OWNERSHIP_NONE);

  typedef Glib::ListHandle< Glib::RefPtr<Gst::MixerTrack> > RType;
  return RType(0, Glib::OWNERSHIP_NONE);
}

} // namespace Gst
