| 1 |
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (C) 1998-2003 James Henstridge |
| 4 |
* Copyright (C) 2005 Oracle |
| 5 |
* Copyright (c) 2012 Canonical Ltd. |
| 6 |
* |
| 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: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 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 |
| 23 |
* IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "pygi-type.h" |
| 27 |
|
| 28 |
#include "pygboxed.h" |
| 29 |
#include "pygi-basictype.h" |
| 30 |
#include "pygi-boxed.h" |
| 31 |
#include "pygi-info.h" |
| 32 |
#include "pygi-util.h" |
| 33 |
#include "pygi-source.h" |
| 34 |
|
| 35 |
typedef struct { |
| 36 |
GSource source; |
| 37 |
PyObject *obj; |
| 38 |
} PyGRealSource; |
| 39 |
|
| 40 |
static gboolean |
| 41 |
source_prepare (GSource *source, gint *timeout) |
| 42 |
{ |
| 43 |
PyGRealSource *pysource = (PyGRealSource *)source; |
| 44 |
PyObject *t; |
| 45 |
gboolean ret = FALSE; |
| 46 |
gboolean got_err = TRUE; |
| 47 |
PyGILState_STATE state; |
| 48 |
|
| 49 |
state = PyGILState_Ensure (); |
| 50 |
|
| 51 |
t = PyObject_CallMethod (pysource->obj, "prepare", NULL); |
| 52 |
|
| 53 |
if (t == NULL) { |
| 54 |
goto bail; |
| 55 |
} else if (!PyObject_IsTrue (t)) { |
| 56 |
got_err = FALSE; |
| 57 |
goto bail; |
| 58 |
} else if (!PyTuple_Check (t)) { |
| 59 |
PyErr_SetString ( |
| 60 |
PyExc_TypeError, |
| 61 |
"source prepare function must return a tuple or False"); |
| 62 |
goto bail; |
| 63 |
} else if (PyTuple_Size (t) != 2) { |
| 64 |
PyErr_SetString ( |
| 65 |
PyExc_TypeError, |
| 66 |
"source prepare function return tuple must be exactly " |
| 67 |
"2 elements long"); |
| 68 |
goto bail; |
| 69 |
} |
| 70 |
|
| 71 |
if (!pygi_gboolean_from_py (PyTuple_GET_ITEM (t, 0), &ret)) { |
| 72 |
ret = FALSE; |
| 73 |
goto bail; |
| 74 |
} |
| 75 |
|
| 76 |
if (!pygi_gint_from_py (PyTuple_GET_ITEM (t, 1), timeout)) { |
| 77 |
ret = FALSE; |
| 78 |
goto bail; |
| 79 |
} |
| 80 |
|
| 81 |
got_err = FALSE; |
| 82 |
|
| 83 |
bail: |
| 84 |
if (got_err) PyErr_Print (); |
| 85 |
|
| 86 |
Py_XDECREF (t); |
| 87 |
|
| 88 |
PyGILState_Release (state); |
| 89 |
|
| 90 |
return ret; |
| 91 |
} |
| 92 |
|
| 93 |
static gboolean |
| 94 |
source_check (GSource *source) |
| 95 |
{ |
| 96 |
PyGRealSource *pysource = (PyGRealSource *)source; |
| 97 |
PyObject *t; |
| 98 |
gboolean ret; |
| 99 |
PyGILState_STATE state; |
| 100 |
|
| 101 |
state = PyGILState_Ensure (); |
| 102 |
|
| 103 |
t = PyObject_CallMethod (pysource->obj, "check", NULL); |
| 104 |
|
| 105 |
if (t == NULL) { |
| 106 |
PyErr_Print (); |
| 107 |
ret = FALSE; |
| 108 |
} else { |
| 109 |
ret = PyObject_IsTrue (t); |
| 110 |
Py_DECREF (t); |
| 111 |
} |
| 112 |
|
| 113 |
PyGILState_Release (state); |
| 114 |
|
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
static gboolean |
| 119 |
source_dispatch (GSource *source, GSourceFunc callback, gpointer user_data) |
| 120 |
{ |
| 121 |
PyGRealSource *pysource = (PyGRealSource *)source; |
| 122 |
PyObject *func, *args, *tuple, *t; |
| 123 |
gboolean ret; |
| 124 |
PyGILState_STATE state; |
| 125 |
|
| 126 |
state = PyGILState_Ensure (); |
| 127 |
|
| 128 |
if (callback) { |
| 129 |
tuple = user_data; |
| 130 |
|
| 131 |
func = PyTuple_GetItem (tuple, 0); |
| 132 |
args = PyTuple_GetItem (tuple, 1); |
| 133 |
} else { |
| 134 |
func = Py_None; |
| 135 |
args = Py_None; |
| 136 |
} |
| 137 |
|
| 138 |
t = PyObject_CallMethod (pysource->obj, "dispatch", "OO", func, args); |
| 139 |
|
| 140 |
if (t == NULL) { |
| 141 |
PyErr_Print (); |
| 142 |
ret = FALSE; |
| 143 |
} else { |
| 144 |
ret = PyObject_IsTrue (t); |
| 145 |
Py_DECREF (t); |
| 146 |
} |
| 147 |
|
| 148 |
PyGILState_Release (state); |
| 149 |
|
| 150 |
return ret; |
| 151 |
} |
| 152 |
|
| 153 |
static GSourceFuncs pyg_source_funcs = { |
| 154 |
source_prepare, |
| 155 |
source_check, |
| 156 |
source_dispatch, |
| 157 |
NULL, |
| 158 |
}; |
| 159 |
|
| 160 |
static gboolean |
| 161 |
handler_marshal (gpointer user_data) |
| 162 |
{ |
| 163 |
PyObject *tuple, *ret; |
| 164 |
gboolean res; |
| 165 |
PyGILState_STATE state; |
| 166 |
|
| 167 |
g_return_val_if_fail (user_data != NULL, FALSE); |
| 168 |
|
| 169 |
state = PyGILState_Ensure (); |
| 170 |
|
| 171 |
tuple = (PyObject *)user_data; |
| 172 |
ret = PyObject_CallObject (PyTuple_GetItem (tuple, 0), |
| 173 |
PyTuple_GetItem (tuple, 1)); |
| 174 |
if (!ret) { |
| 175 |
PyErr_Print (); |
| 176 |
res = FALSE; |
| 177 |
} else { |
| 178 |
res = PyObject_IsTrue (ret); |
| 179 |
Py_DECREF (ret); |
| 180 |
} |
| 181 |
|
| 182 |
PyGILState_Release (state); |
| 183 |
|
| 184 |
return res; |
| 185 |
} |
| 186 |
|
| 187 |
PyObject * |
| 188 |
pygi_source_set_callback (PyGObject *self_module, PyObject *args) |
| 189 |
{ |
| 190 |
PyObject *self, *first, *callback, *cbargs = NULL, *data; |
| 191 |
Py_ssize_t len; |
| 192 |
|
| 193 |
len = PyTuple_Size (args); |
| 194 |
if (len < 2) { |
| 195 |
PyErr_SetString (PyExc_TypeError, |
| 196 |
"set_callback requires at least 2 arguments"); |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
|
| 200 |
first = PySequence_GetSlice (args, 0, 2); |
| 201 |
if (!PyArg_ParseTuple (first, "OO:set_callback", &self, &callback)) { |
| 202 |
Py_DECREF (first); |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
Py_DECREF (first); |
| 206 |
|
| 207 |
if (!pyg_boxed_check (self, G_TYPE_SOURCE)) { |
| 208 |
PyErr_SetString (PyExc_TypeError, |
| 209 |
"first argument is not a GLib.Source"); |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
|
| 213 |
if (!PyCallable_Check (callback)) { |
| 214 |
PyErr_SetString (PyExc_TypeError, "second argument not callable"); |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
cbargs = PySequence_GetSlice (args, 2, len); |
| 219 |
if (cbargs == NULL) return NULL; |
| 220 |
|
| 221 |
data = Py_BuildValue ("(ON)", callback, cbargs); |
| 222 |
if (data == NULL) return NULL; |
| 223 |
|
| 224 |
g_source_set_callback (pyg_boxed_get (self, GSource), handler_marshal, |
| 225 |
data, pyg_destroy_notify); |
| 226 |
|
| 227 |
Py_RETURN_NONE; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* pygi_source_new: |
| 232 |
* |
| 233 |
* Wrap the un-bindable g_source_new() and provide wrapper callbacks in the |
| 234 |
* GSourceFuncs which call back to Python. |
| 235 |
* |
| 236 |
* Returns NULL on error and sets an exception. |
| 237 |
*/ |
| 238 |
PyObject * |
| 239 |
pygi_source_new (PyObject *self, PyObject *args) |
| 240 |
{ |
| 241 |
PyGRealSource *source; |
| 242 |
PyObject *py_type, *boxed; |
| 243 |
|
| 244 |
g_assert (args == NULL); |
| 245 |
|
| 246 |
py_type = pygi_type_import_by_name ("GLib", "Source"); |
| 247 |
if (!py_type) return NULL; |
| 248 |
|
| 249 |
source = (PyGRealSource *)g_source_new (&pyg_source_funcs, |
| 250 |
sizeof (PyGRealSource)); |
| 251 |
/* g_source_new uses malloc, not slices */ |
| 252 |
boxed = pygi_boxed_new ((PyTypeObject *)py_type, source, TRUE, 0); |
| 253 |
Py_DECREF (py_type); |
| 254 |
if (!boxed) { |
| 255 |
g_source_unref ((GSource *)source); |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
source->obj = boxed; |
| 259 |
|
| 260 |
return source->obj; |
| 261 |
} |