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
27 #include "pygobject.h"
29 #include "pygi-private.h"
31 #include "pyglib-private.h"
32 #include "pygi-source.h"
41 pyg_source_prepare(GSource *source, gint *timeout)
43 PyGRealSource *pysource = (PyGRealSource *)source;
46 gboolean got_err = TRUE;
47 PyGILState_STATE state;
49 state = pyglib_gil_state_ensure();
51 t = PyObject_CallMethod(pysource->obj, "prepare", NULL);
55 } else if (!PyObject_IsTrue(t)) {
58 } else if (!PyTuple_Check(t)) {
59 PyErr_SetString(PyExc_TypeError,
60 "source prepare function must return a tuple or False");
62 } else if (PyTuple_Size(t) != 2) {
63 PyErr_SetString(PyExc_TypeError,
64 "source prepare function return tuple must be exactly "
69 ret = PyObject_IsTrue(PyTuple_GET_ITEM(t, 0));
70 *timeout = PYGLIB_PyLong_AsLong(PyTuple_GET_ITEM(t, 1));
72 if (*timeout == -1 && PyErr_Occurred()) {
85 pyglib_gil_state_release(state);
91 pyg_source_check(GSource *source)
93 PyGRealSource *pysource = (PyGRealSource *)source;
96 PyGILState_STATE state;
98 state = pyglib_gil_state_ensure();
100 t = PyObject_CallMethod(pysource->obj, "check", NULL);
106 ret = PyObject_IsTrue(t);
110 pyglib_gil_state_release(state);
116 pyg_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
118 PyGRealSource *pysource = (PyGRealSource *)source;
119 PyObject *func, *args, *tuple, *t;
121 PyGILState_STATE state;
123 state = pyglib_gil_state_ensure();
128 func = PyTuple_GetItem(tuple, 0);
129 args = PyTuple_GetItem(tuple, 1);
135 t = PyObject_CallMethod(pysource->obj, "dispatch", "OO", func, args);
141 ret = PyObject_IsTrue(t);
145 pyglib_gil_state_release(state);
151 pyg_source_finalize(GSource *source)
153 PyGRealSource *pysource = (PyGRealSource *)source;
155 PyGILState_STATE state;
157 state = pyglib_gil_state_ensure();
159 func = PyObject_GetAttrString(pysource->obj, "finalize");
161 t = PyObject_CallObject(func, NULL);
171 pyglib_gil_state_release(state);
174 static GSourceFuncs pyg_source_funcs =
183 pyg_source_set_callback(PyGObject *self_module, PyObject *args)
185 PyObject *self, *first, *callback, *cbargs = NULL, *data;
188 len = PyTuple_Size (args);
190 PyErr_SetString(PyExc_TypeError,
191 "set_callback requires at least 2 arguments");
195 first = PySequence_GetSlice(args, 0, 2);
196 if (!PyArg_ParseTuple(first, "OO:set_callback", &self, &callback)) {
202 if (!pyg_boxed_check (self, G_TYPE_SOURCE)) {
203 PyErr_SetString(PyExc_TypeError, "first argument is not a GLib.Source");
207 if (!PyCallable_Check(callback)) {
208 PyErr_SetString(PyExc_TypeError, "second argument not callable");
212 cbargs = PySequence_GetSlice(args, 2, len);
216 data = Py_BuildValue("(ON)", callback, cbargs);
220 g_source_set_callback(pyg_boxed_get (self, GSource),
221 _pyglib_handler_marshal, data,
222 _pyglib_destroy_notify);
231 * Wrap the un-bindable g_source_new() and provide wrapper callbacks in the
232 * GSourceFuncs which call back to Python.
235 pyg_source_new (void)
237 PyGRealSource *source = NULL;
240 source = (PyGRealSource*) g_source_new (&pyg_source_funcs, sizeof (PyGRealSource));
242 py_type = _pygi_type_import_by_name ("GLib", "Source");
243 /* g_source_new uses malloc, not slices */
244 source->obj = _pygi_boxed_new ( (PyTypeObject *) py_type, source, FALSE, 0);