1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
3 * Copyright (C) 1998-2003 James Henstridge
4 * Copyright (C) 2005 Oracle
5 * Copyright (c) 2012 Canonical Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "pygi-info.h"
27 #include "pygi-boxed.h"
28 #include "pygi-type.h"
31 #include "pygi-source.h"
40 pyg_source_prepare(GSource *source, gint *timeout)
42 PyGRealSource *pysource = (PyGRealSource *)source;
45 gboolean got_err = TRUE;
46 PyGILState_STATE state;
48 state = PyGILState_Ensure();
50 t = PyObject_CallMethod(pysource->obj, "prepare", NULL);
54 } else if (!PyObject_IsTrue(t)) {
57 } else if (!PyTuple_Check(t)) {
58 PyErr_SetString(PyExc_TypeError,
59 "source prepare function must return a tuple or False");
61 } else if (PyTuple_Size(t) != 2) {
62 PyErr_SetString(PyExc_TypeError,
63 "source prepare function return tuple must be exactly "
68 ret = PyObject_IsTrue(PyTuple_GET_ITEM(t, 0));
69 *timeout = PYGLIB_PyLong_AsLong(PyTuple_GET_ITEM(t, 1));
71 if (*timeout == -1 && PyErr_Occurred()) {
84 PyGILState_Release(state);
90 pyg_source_check(GSource *source)
92 PyGRealSource *pysource = (PyGRealSource *)source;
95 PyGILState_STATE state;
97 state = PyGILState_Ensure();
99 t = PyObject_CallMethod(pysource->obj, "check", NULL);
105 ret = PyObject_IsTrue(t);
109 PyGILState_Release(state);
115 pyg_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
117 PyGRealSource *pysource = (PyGRealSource *)source;
118 PyObject *func, *args, *tuple, *t;
120 PyGILState_STATE state;
122 state = PyGILState_Ensure();
127 func = PyTuple_GetItem(tuple, 0);
128 args = PyTuple_GetItem(tuple, 1);
134 t = PyObject_CallMethod(pysource->obj, "dispatch", "OO", func, args);
140 ret = PyObject_IsTrue(t);
144 PyGILState_Release(state);
150 pyg_source_finalize(GSource *source)
152 PyGRealSource *pysource = (PyGRealSource *)source;
154 PyGILState_STATE state;
156 state = PyGILState_Ensure();
158 func = PyObject_GetAttrString(pysource->obj, "finalize");
160 t = PyObject_CallObject(func, NULL);
172 PyGILState_Release(state);
175 static GSourceFuncs pyg_source_funcs =
184 pyg_source_set_callback(PyGObject *self_module, PyObject *args)
186 PyObject *self, *first, *callback, *cbargs = NULL, *data;
189 len = PyTuple_Size (args);
191 PyErr_SetString(PyExc_TypeError,
192 "set_callback requires at least 2 arguments");
196 first = PySequence_GetSlice(args, 0, 2);
197 if (!PyArg_ParseTuple(first, "OO:set_callback", &self, &callback)) {
203 if (!pyg_boxed_check (self, G_TYPE_SOURCE)) {
204 PyErr_SetString(PyExc_TypeError, "first argument is not a GLib.Source");
208 if (!PyCallable_Check(callback)) {
209 PyErr_SetString(PyExc_TypeError, "second argument not callable");
213 cbargs = PySequence_GetSlice(args, 2, len);
217 data = Py_BuildValue("(ON)", callback, cbargs);
221 g_source_set_callback(pyg_boxed_get (self, GSource),
222 _pyglib_handler_marshal, data,
223 _pyglib_destroy_notify);
232 * Wrap the un-bindable g_source_new() and provide wrapper callbacks in the
233 * GSourceFuncs which call back to Python.
236 pyg_source_new (void)
238 PyGRealSource *source = NULL;
241 source = (PyGRealSource*) g_source_new (&pyg_source_funcs, sizeof (PyGRealSource));
243 py_type = _pygi_type_import_by_name ("GLib", "Source");
244 /* Full ownership transfer of the source, this will be free'd with g_boxed_free. */
245 source->obj = _pygi_boxed_new ( (PyTypeObject *) py_type, source,
246 FALSE, /* copy_boxed */
247 0); /* slice_allocated */