~ubuntu-branches/ubuntu/precise/gst0.10-python/precise

« back to all changes in this revision

Viewing changes to gst/gstbus.override

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier
  • Date: 2006-06-25 19:37:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060625193745-9yeg0wq56r24n57x
Tags: upstream-0.10.4
ImportĀ upstreamĀ versionĀ 0.10.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; c-basic-offset: 4 -*-
 
2
 * vi:si:et:sw=4:sts=4:ts=4
 
3
 *
 
4
 * gst-python
 
5
 * Copyright (C) 2005 Edward Hervey
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 * 
 
22
 * Author: Edward Hervey <edward@fluendo.com>
 
23
 */
 
24
%%
 
25
ignore
 
26
  gst_bus_create_watch
 
27
  gst_bus_sync_signal_handler
 
28
  gst_bus_async_signal_func
 
29
%%
 
30
headers
 
31
static GstBusSyncReply
 
32
bus_sync_handler (GstBus *bus, GstMessage *message, gpointer user_data)
 
33
{
 
34
    PyGILState_STATE state;
 
35
    GstBusSyncReply    res;
 
36
    PyObject    *py_userdata;
 
37
    PyObject    *py_msg;
 
38
    PyObject    *callback, *args;
 
39
    PyObject    *ret;
 
40
    gint        i, len;
 
41
 
 
42
    g_return_val_if_fail (user_data != NULL, GST_BUS_PASS);
 
43
 
 
44
    state = pyg_gil_state_ensure ();
 
45
 
 
46
    py_userdata = (PyObject *) user_data;
 
47
    py_msg = pygstminiobject_new (GST_MINI_OBJECT (message));
 
48
    callback = PyTuple_GetItem (py_userdata, 0);
 
49
 
 
50
    /* Using N we give away our references to the args tuple */
 
51
    args = Py_BuildValue ("(NN)",
 
52
                          pygstobject_new (G_OBJECT (bus)),
 
53
                          py_msg);
 
54
 
 
55
    /* add all *args to the args tuple object */
 
56
    len = PyTuple_Size (py_userdata);
 
57
    for (i = 1; i < len; ++i) {
 
58
        PyObject *tuple = args;
 
59
        args = PySequence_Concat (tuple, PyTuple_GetItem (py_userdata, i));
 
60
        Py_DECREF (tuple);
 
61
    }
 
62
    ret = PyObject_CallObject (callback, args);
 
63
 
 
64
    if (!ret) {
 
65
        PyErr_Print ();
 
66
        res = GST_BUS_PASS;
 
67
    } else {
 
68
        if (ret == Py_None) {
 
69
            PyErr_SetString (PyExc_TypeError,
 
70
                "callback should return a BusSyncReply");
 
71
            PyErr_Print ();
 
72
            res = GST_BUS_PASS;    
 
73
        } else if (pyg_enum_get_value (GST_TYPE_BUS_SYNC_REPLY, ret,
 
74
                                       (gint *) &res))
 
75
            res = GST_BUS_PASS;
 
76
 
 
77
        Py_DECREF (ret);
 
78
    }
 
79
    Py_DECREF (args);
 
80
 
 
81
    pyg_gil_state_release (state);
 
82
 
 
83
    return res;
 
84
}
 
85
 
 
86
static gboolean
 
87
bus_func (GstBus *bus, GstMessage *message, gpointer user_data)
 
88
{
 
89
    PyGILState_STATE state;
 
90
    gboolean    res;
 
91
    PyObject    *py_userdata;
 
92
    PyObject    *py_msg;
 
93
    PyObject    *callback, *args;
 
94
    PyObject    *ret;
 
95
    gint        i, len;
 
96
 
 
97
    g_return_val_if_fail (user_data != NULL, TRUE);
 
98
 
 
99
    GST_DEBUG_OBJECT (bus, "dispatching message %p", message);
 
100
 
 
101
    state = pyg_gil_state_ensure ();
 
102
 
 
103
    py_userdata = (PyObject *) user_data;
 
104
    g_assert (PyTuple_Check (py_userdata));
 
105
 
 
106
    py_msg = pygstminiobject_new (GST_MINI_OBJECT (message));
 
107
    callback = PyTuple_GetItem (py_userdata, 0);
 
108
 
 
109
    /* Using N we give away our references to the args tuple */
 
110
    args = Py_BuildValue ("(NN)",
 
111
                          pygstobject_new (G_OBJECT (bus)),
 
112
                          py_msg);
 
113
    g_assert (args);
 
114
 
 
115
    /* add all *args to the args tuple object */
 
116
    len = PyTuple_Size (py_userdata);
 
117
    for (i = 1; i < len; ++i) {
 
118
        PyObject *item;
 
119
        PyObject *tuple = args;
 
120
 
 
121
        item = PyTuple_GetItem (py_userdata, i);
 
122
        g_assert (item);
 
123
 
 
124
        args = PySequence_Concat (tuple, item);
 
125
        g_assert (args);
 
126
 
 
127
        Py_DECREF (tuple);
 
128
    }
 
129
    ret = PyObject_CallObject(callback, args);
 
130
 
 
131
    if (!ret) {
 
132
        PyErr_Print ();
 
133
        res = TRUE;
 
134
    } else {
 
135
        if (ret == Py_None) {
 
136
            PyErr_SetString (PyExc_TypeError,
 
137
                "callback should return True or False");
 
138
            PyErr_Print ();
 
139
            res = TRUE;
 
140
        } else 
 
141
            res = PyObject_IsTrue (ret);
 
142
        Py_DECREF (ret);
 
143
    }
 
144
    Py_DECREF (args);
 
145
 
 
146
    pyg_gil_state_release (state);
 
147
 
 
148
    GST_DEBUG_OBJECT (bus, "dispatched message %p", message);
 
149
 
 
150
    return res;
 
151
}
 
152
 
 
153
%%
 
154
override gst_bus_set_sync_handler args
 
155
static PyObject *
 
156
_wrap_gst_bus_set_sync_handler (PyGObject *self, PyObject *args)
 
157
{
 
158
    PyObject *callback, *cbargs = NULL, *data;
 
159
    gint len;
 
160
 
 
161
    len = PyTuple_Size(args);
 
162
 
 
163
    if (len < 1) {
 
164
    PyErr_SetString(PyExc_TypeError, "Bus requires at least 1 arg");
 
165
    return NULL;
 
166
    }
 
167
    callback = PySequence_GetItem(args, 0);
 
168
    if (!PyCallable_Check(callback)) {
 
169
    PyErr_SetString(PyExc_TypeError, "callback is not callable");
 
170
    return NULL;
 
171
    }
 
172
    cbargs = PySequence_GetSlice(args, 1, len);
 
173
    if (cbargs == NULL)
 
174
    return NULL;
 
175
    data = Py_BuildValue("(ON)", callback, cbargs);
 
176
    if (data == NULL)
 
177
    return NULL;
 
178
    gst_bus_set_sync_handler (GST_BUS (self->obj),
 
179
                      (GstBusSyncHandler) bus_sync_handler, 
 
180
                      data);
 
181
 
 
182
    Py_INCREF(Py_None);
 
183
    return Py_None;
 
184
}
 
185
%%
 
186
override gst_bus_add_watch args
 
187
static PyObject *
 
188
_wrap_gst_bus_add_watch (PyGObject *self, PyObject *args)
 
189
{
 
190
    PyObject *callback, *cbargs = NULL, *data;
 
191
    guint sigid;
 
192
    guint len;
 
193
 
 
194
    len = PyTuple_Size(args);
 
195
 
 
196
    if (len < 1) {
 
197
        PyErr_SetString(PyExc_TypeError, "Bus.add_watch requires at least 1 argument");
 
198
        return NULL;
 
199
    }
 
200
 
 
201
    callback = PySequence_GetItem(args, 0);
 
202
    if (!PyCallable_Check(callback)) {
 
203
        PyErr_SetString(PyExc_TypeError, "callback is not callable");
 
204
        return NULL;
 
205
    }
 
206
    cbargs = PySequence_GetSlice(args, 1, len);
 
207
    if (cbargs == NULL)
 
208
        return NULL;
 
209
    /* FIXME: thomas: I'm pretty sure the second N needs to be O */
 
210
    data = Py_BuildValue("(ON)", callback, cbargs);
 
211
    if (data == NULL)
 
212
        return NULL;
 
213
 
 
214
    sigid = gst_bus_add_watch_full (GST_BUS (self->obj), G_PRIORITY_DEFAULT,
 
215
        (GstBusFunc) bus_func, data, (GDestroyNotify)pyg_destroy_notify);
 
216
 
 
217
    return PyInt_FromLong(sigid);
 
218
}
 
219
%%
 
220
override gst_bus_add_signal_watch kwargs
 
221
static PyObject *
 
222
_wrap_gst_bus_add_signal_watch(PyGObject *self, PyObject *args, PyObject *kwargs)
 
223
{
 
224
    static char *kwlist[] = { "priority", NULL };
 
225
    int priority = G_PRIORITY_DEFAULT;
 
226
 
 
227
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:GstBus.add_signal_watch", kwlist, &priority))
 
228
        return NULL;
 
229
    pyg_begin_allow_threads;
 
230
    gst_bus_add_signal_watch_full(GST_BUS(self->obj), priority);
 
231
    pyg_end_allow_threads;
 
232
    Py_INCREF(Py_None);
 
233
    return Py_None;
 
234
}