/* -*- Mode: C; ; c-basic-offset: 4 -*- */
/* gst-python
 * Copyright (C) 2005 Edward Hervey
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 * 
 * Author: Johan Dahlin <johan@gnome.org>
 */
%%
ignore
  gst_controller_new
  gst_controller_*_valist
  gst_controller_remove_properties_list

%%
override gst_controller_set args
static PyObject *
_wrap_gst_controller_set (PyGObject *self, PyObject *args)
{
  GstController	*controller = (GstController *) self->obj;
  gchar		*param_name;
  GParamSpec	*pspec = NULL;
  GstClockTime	timestamp;
  GValue	value = { 0, };
  PyObject	*pvalue;
  gboolean	res;

  if (!PyArg_ParseTuple(args, "sLO:GstController.set",
			&param_name, &timestamp, &pvalue))
    return NULL;

  if (!(pspec = g_object_class_find_property(G_OBJECT_GET_CLASS (controller->object),
					     (const gchar*) param_name))) {
      PyErr_SetString (PyExc_TypeError,
		       "The controlled object doesn't have the given property");
      return NULL;
  }

  g_value_init (&value, pspec->value_type);

  if (pyg_value_from_pyobject (&value, pvalue)) {
    PyErr_SetString (PyExc_TypeError,
		     "Couldn't convert the given value to the good type");
    return NULL;
  }

  res = gst_controller_set (controller, param_name, timestamp, &value);
  if (res) {
    Py_INCREF (Py_True);
    return Py_True;
  }
  Py_INCREF (Py_False);
  return Py_False;
}
%%
override gst_controller_get kwargs
static PyObject *
_wrap_gst_controller_get (PyGObject *self, PyObject *args, PyObject *kwargs)
{
    GstController	*controller = (GstController *) self->obj;
    static char		*kwlist[] = { "propertyname", "timestamp", NULL };
    gchar	*propertyname;
    GstClockTime	timestamp;
    GValue	*value = NULL;
    PyObject	*pyvalue;

    if (!PyArg_ParseTupleAndKeywords (args, kwargs,
				      "sL:GstController.get",
				      kwlist, &propertyname, &timestamp))
	return NULL;

    value = gst_controller_get (controller, propertyname, timestamp);
    if (value) {
	pyvalue = pyg_value_as_pyobject (value, FALSE);
	return pyvalue;
    }
    Py_INCREF (Py_None);
    return Py_None;
}
%%
override gst_controller_new_list args
static int
_wrap_gst_controller_new_list(PyGObject *self, PyObject *args)
{
    PyObject    *target_pyobj;
    PyGObject	*target;
    gint	len;
    GList	*list = NULL;

    if ((len = PyTuple_Size(args)) < 1) {
	PyErr_SetString(PyExc_TypeError, "Controller requires at least a target object");
	return -1;
    }

    target_pyobj = PyTuple_GetItem(args, 0);
    if (!PyObject_TypeCheck(target_pyobj, &PyGObject_Type)) {
	PyErr_Format(PyExc_TypeError,
		     "argument 1 must be %s, not %s",
		     PyGObject_Type.tp_name,
		     target_pyobj == Py_None ? "None" : target_pyobj->ob_type->tp_name);
	return -1;
    }

    target = (PyGObject *) target_pyobj;
    
    if (len > 1)
	while (len-- > 1) {
	    PyObject	*temp;
	    gchar	*str;

	    temp = PyTuple_GetItem(args, len);
	    str = PyString_AsString(temp);
	    if (str == NULL) {
		g_list_free (list);
		return -1;
	    }
	    GST_INFO("prepending %s [%d]", str, len);
	    list = g_list_prepend(list, str);
	}

    self->obj = (GObject *) gst_controller_new_list(target->obj, list);
    g_list_free (list);

    if (!self->obj) {
	PyErr_SetString(PyExc_RuntimeError, "could not create GstController object");
	return -1;
    }
    
    pygobject_register_wrapper((PyObject *) self);
    return 0;
}
%%
override gst_controller_remove_properties args
static PyObject *
_wrap_gst_controller_remove_properties (PyGObject *self, PyObject *args)
{
    GstController *controller = (GstController *) self->obj;
    gint	len;
    GList	*list = NULL;
    gboolean	res = FALSE;
    PyObject	*pret;

    if ((len = PyTuple_Size(args)) < 1) {
	PyErr_SetString(PyExc_TypeError, "Please give at least one property name to remove");
	return NULL;
    }

    while (len--) {
	PyObject	*temp;
	gchar	*str;
	
	temp = PyTuple_GetItem(args, len);
	str = PyString_AsString(temp);
	if (str == NULL) {
	    g_list_free (list);
	    return NULL;
	}
	GST_INFO("prepending %s [%d]", str, len);
	list = g_list_prepend(list, str);
    }
    
    res = gst_controller_remove_properties_list(controller, list);
    g_list_free (list);

    if (res)
	pret = Py_True;
    else
	pret = Py_False;

    Py_INCREF (pret);

    return pret;
}
%%
override gst_controller_set_from_list args
static PyObject *
_wrap_gst_controller_set_from_list (PyGObject *self, PyObject *args)
{
    GstController *controller = (GstController *) self->obj;
    PyObject	*temp;
    gint	len;
    gchar	*pname;
    GSList	*list = NULL;
    GParamSpec	*pspec = NULL;
    gboolean	res = FALSE;
    PyObject	*pret;

    if ((len = PyTuple_Size(args)) < 2) {
	PyErr_SetString(PyExc_TypeError, "Please give a property name and a tuple of (time,value)");
	return NULL;
    }

    temp = PyTuple_GetItem(args, 0);
    if (!PyString_Check (temp)) {
	PyErr_SetString(PyExc_TypeError, "First argument must be a string");
	return NULL;
    }
    pname = PyString_AsString(temp);

    if (!(pspec = g_object_class_find_property(G_OBJECT_GET_CLASS (controller->object),
					       (const gchar*) pname)))
	goto error;

    while (len-- > 1) {
	PyObject	*temp2;
	GstTimedValue	*tval;
	
	temp2 = PyTuple_GetItem(args, len);
	if (!PyTuple_Check (temp2)) {
	    PyErr_SetString (PyExc_TypeError, "Tuple doesn't contain tuples !");
	    goto error;
	}
	tval = g_new0(GstTimedValue, 1);
	tval->timestamp = PyLong_AsUnsignedLongLong(PyTuple_GetItem(temp2, 0));
	g_value_init (&tval->value, pspec->value_type);
	if ((pyg_value_from_pyobject (&tval->value, PyTuple_GetItem (temp2, 1))) < 0) {
	    PyErr_SetString (PyExc_TypeError, "Couldn't convert value to correct type");
	    goto error;
	};

	list = g_slist_prepend(list, tval);
    }
    
    res = gst_controller_set_from_list(controller, pname, list);
    g_slist_free (list);

    if (res)
	pret = Py_True;
    else
	pret = Py_False;

    Py_INCREF (pret);

    return pret;

 error:
    while (list) {
	g_free(list->data);
	list = g_slist_next(list);
    }
    g_slist_free (list);
    return NULL;
}
%%
override gst_dp_header_payload_length kwargs
static PyObject *
_wrap_gst_dp_header_payload_length(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header", NULL };
    char *header;
    gulong ret;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:dp_header_payload_length", kwlist, &header))
        return NULL;
    ret = gst_dp_header_payload_length((guint8*)header);
    return PyLong_FromUnsignedLong(ret);
}
%%
overrid gst_dp_header_payload_type kwargs
static PyObject *
_wrap_gst_dp_header_payload_type(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header", NULL };
    char *header;
    gint ret;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:dp_header_payload_type", kwlist, &header))
        return NULL;
    ret = gst_dp_header_payload_type((guint8*)header);
    return pyg_enum_from_gtype(G_TYPE_NONE, ret);
}
%%
override gst_dp_buffer_from_header kwargs
static PyObject *
_wrap_gst_dp_buffer_from_header(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header_length", "header", NULL };
    char *header;
    GstBuffer *ret;
    guint header_length;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Is:dp_buffer_from_header", kwlist, &header_length, &header))
        return NULL;
    ret = gst_dp_buffer_from_header(header_length, (guint8*)header);
    /* pygobject_new handles NULL checking */
    return pygstminiobject_new((GstMiniObject *)ret);
}
%%
override gst_dp_caps_from_packet kwargs
static PyObject *
_wrap_gst_dp_caps_from_packet(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header_length", "header", "payload", NULL };
    char *header, *payload;
    GstCaps *ret;
    guint header_length;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Iss:dp_caps_from_packet", kwlist, &header_length, &header, &payload))
        return NULL;
    ret = gst_dp_caps_from_packet(header_length, 
				  (guint8*) header,
				  (guint8*) payload);
    return pyg_boxed_new (GST_TYPE_CAPS, ret, FALSE, TRUE);
}
%%
override gst_dp_event_from_packet kwargs
static PyObject *
_wrap_gst_dp_event_from_packet(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header_length", "header", "payload", NULL };
    char *header, *payload;
    GstEvent *ret;
    guint header_length;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Iss:dp_event_from_packet", kwlist, &header_length, &header, &payload))
        return NULL;
    ret = gst_dp_event_from_packet(header_length, 
				   (guint8*) header,
				   (guint8*) payload);
    /* pygobject_new handles NULL checking */
    return pygstminiobject_new((GstMiniObject *)ret);
}
%%
override gst_dp_validate_header kwargs
static PyObject *
_wrap_gst_dp_validate_header(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header_length", "header", NULL };
    char *header;
    int ret;
    guint header_length;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Is:dp_validate_header", kwlist, &header_length, &header))
        return NULL;
    ret = gst_dp_validate_header(header_length, (guint8*) header);
    return PyBool_FromLong(ret);

}
%%
override gst_dp_validate_payload kwargs
static PyObject *
_wrap_gst_dp_validate_payload(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header_length", "header", "payload", NULL };
    char *header, *payload;
    int ret;
    guint header_length;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Iss:dp_validate_payload", kwlist, &header_length, &header, &payload))
        return NULL;
    ret = gst_dp_validate_payload(header_length,
				  (guint8*) header,
				  (guint8*) payload);
    return PyBool_FromLong(ret);

}
%%
override gst_dp_validate_packet kwargs
static PyObject *
_wrap_gst_dp_validate_packet(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header_length", "header", "payload", NULL };
    char *header, *payload;
    int ret;
    guint header_length;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Iss:dp_validate_packet", kwlist, &header_length, &header, &payload))
        return NULL;
    ret = gst_dp_validate_packet(header_length, 
				 (guint8*) header,
				 (guint8*) payload);
    return PyBool_FromLong(ret);

}
%%
override gst_dp_header_payload_type kwargs
static PyObject *
_wrap_gst_dp_header_payload_type(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "header", NULL };
    char *header;
    gint ret;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:dp_header_payload_type", kwlist, &header))
        return NULL;
    ret = gst_dp_header_payload_type((guint8*) header);
    return pyg_enum_from_gtype(G_TYPE_NONE, ret);
}