1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
4 * Copyright (C) 2005 Oracle
6 * Author: Manish Singh <manish.singh@oracle.com>
8 * pygsource.c: GSource wrapper
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
32 #include <structmember.h> /* for PyMemberDef */
34 #include "pyglib-private.h"
35 #include "pygmaincontext.h"
36 #include "pygsource.h"
38 #define CHECK_DESTROYED(self, ret) G_STMT_START { \
39 if ((self)->source == NULL) { \
40 PyErr_SetString(PyExc_RuntimeError, "source is destroyed"); \
50 PyObject *weakreflist;
51 gboolean python_source;
62 PYGLIB_DEFINE_TYPE("glib.Source", PyGSource_Type, PyGSource)
65 source_repr(PyGSource *self, const char *type)
67 gchar buf[256], *desc;
70 if (g_source_get_context(self->source))
79 g_snprintf(buf, sizeof(buf), "<%s glib %s source at 0x%lx>",
80 desc, type, (long) self);
82 g_snprintf(buf, sizeof(buf), "<%s glib source at 0x%lx>",
85 return _PyUnicode_FromString(buf);
89 pyg_source_attach(PyGSource *self, PyObject *args, PyObject *kwargs)
91 static char *kwlist[] = { "context", NULL };
92 PyGMainContext *py_context = NULL;
93 GMainContext *context = NULL;
96 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
98 &PyGMainContext_Type, &py_context))
102 context = py_context->context;
104 CHECK_DESTROYED(self, NULL);
106 if (self->python_source) {
107 PyGRealSource *pysource = (PyGRealSource *)self->source;
108 Py_INCREF(pysource->obj);
111 id = g_source_attach(self->source, context);
112 return _PyLong_FromLong(id);
116 pyg_source_destroy(PyGSource *self)
118 CHECK_DESTROYED(self, NULL);
120 if (self->python_source && self->source->context) {
121 PyGRealSource *pysource = (PyGRealSource *)self->source;
122 Py_DECREF(pysource->obj);
125 g_source_destroy(self->source);
133 pyg_source_set_callback(PyGSource *self, PyObject *args)
135 PyObject *first, *callback, *cbargs = NULL, *data;
138 CHECK_DESTROYED(self, NULL);
140 len = PyTuple_Size (args);
142 PyErr_SetString(PyExc_TypeError,
143 "set_callback requires at least 1 argument");
147 first = PySequence_GetSlice(args, 0, 1);
148 if (!PyArg_ParseTuple(first, "O:set_callback", &callback)) {
154 if (!PyCallable_Check(callback)) {
155 PyErr_SetString(PyExc_TypeError, "first argument not callable");
159 cbargs = PySequence_GetSlice(args, 1, len);
163 data = Py_BuildValue("(ON)", callback, cbargs);
167 g_source_set_callback(self->source,
168 _pyglib_handler_marshal, data,
169 _pyglib_destroy_notify);
176 pyg_source_get_context(PyGSource *self)
178 GMainContext *context;
180 CHECK_DESTROYED(self, NULL);
182 context = g_source_get_context(self->source);
185 return pyglib_main_context_new(context);
193 pyg_source_add_poll(PyGSource *self, PyObject *args, PyObject *kwargs)
195 static char *kwlist[] = { "fd", NULL };
198 if (!self->python_source) {
199 PyErr_SetString(PyExc_TypeError,
200 "add_poll can only be used with sources "
201 "implemented in python");
205 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
206 "O!:add_poll", kwlist,
207 &PyGPollFD_Type, &fd))
210 CHECK_DESTROYED(self, NULL);
212 g_source_add_poll(self->source, &fd->pollfd);
219 pyg_source_remove_poll(PyGSource *self, PyObject *args, PyObject *kwargs)
221 static char *kwlist[] = { "fd", NULL };
224 if (!self->python_source) {
225 PyErr_SetString(PyExc_TypeError,
226 "remove_poll can only be used with sources "
227 "implemented in python");
231 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
232 "O!:remove_poll", kwlist,
233 &PyGPollFD_Type, &fd))
236 CHECK_DESTROYED(self, NULL);
238 g_source_remove_poll(self->source, &fd->pollfd);
245 pyg_source_get_current_time(PyGSource *self)
250 CHECK_DESTROYED(self, NULL);
252 g_source_get_current_time(self->source, &timeval);
253 ret = (double)timeval.tv_sec + (double)timeval.tv_usec * 0.000001;
254 return PyFloat_FromDouble(ret);
257 static PyMethodDef pyg_source_methods[] = {
258 { "attach", (PyCFunction)pyg_source_attach, METH_KEYWORDS },
259 { "destroy", (PyCFunction)pyg_source_destroy, METH_NOARGS },
260 { "set_callback", (PyCFunction)pyg_source_set_callback, METH_VARARGS },
261 { "get_context", (PyCFunction)pyg_source_get_context, METH_NOARGS },
262 { "add_poll", (PyCFunction)pyg_source_add_poll, METH_KEYWORDS },
263 { "remove_poll", (PyCFunction)pyg_source_remove_poll, METH_KEYWORDS },
264 { "get_current_time", (PyCFunction)pyg_source_get_current_time, METH_NOARGS },
269 pyg_source_get_dict(PyGSource *self, void *closure)
271 if (self->inst_dict == NULL) {
272 self->inst_dict = PyDict_New();
273 if (self->inst_dict == NULL)
277 Py_INCREF(self->inst_dict);
278 return self->inst_dict;
282 pyg_source_get_priority(PyGSource *self, void *closure)
284 CHECK_DESTROYED(self, NULL);
286 return _PyLong_FromLong(g_source_get_priority(self->source));
290 pyg_source_set_priority(PyGSource *self, PyObject *value, void *closure)
292 CHECK_DESTROYED(self, -1);
295 PyErr_SetString(PyExc_TypeError, "cannot delete priority");
299 if (!_PyLong_Check(value)) {
300 PyErr_SetString(PyExc_TypeError, "type mismatch");
304 g_source_set_priority(self->source, _PyLong_AsLong(value));
310 pyg_source_get_can_recurse(PyGSource *self, void *closure)
312 CHECK_DESTROYED(self, NULL);
314 return PyBool_FromLong(g_source_get_can_recurse(self->source));
318 pyg_source_set_can_recurse(PyGSource *self, PyObject *value, void *closure)
320 CHECK_DESTROYED(self, -1);
323 PyErr_SetString(PyExc_TypeError, "cannot delete can_recurse");
327 g_source_set_can_recurse(self->source, PyObject_IsTrue(value));
333 pyg_source_get_id(PyGSource *self, void *closure)
335 CHECK_DESTROYED(self, NULL);
337 if (g_source_get_context(self->source) == NULL) {
338 PyErr_SetString(PyExc_RuntimeError, "source is not attached");
342 return _PyLong_FromLong(g_source_get_id(self->source));
345 static PyGetSetDef pyg_source_getsets[] = {
346 { "__dict__", (getter)pyg_source_get_dict, (setter)0 },
347 {"priority", (getter)pyg_source_get_priority, (setter)pyg_source_set_priority },
348 {"can_recurse", (getter)pyg_source_get_can_recurse, (setter)pyg_source_set_can_recurse },
349 {"id", (getter)pyg_source_get_id, (setter)0 },
354 pyg_source_repr(PyGSource *self)
356 return source_repr(self, NULL);
360 pyg_source_traverse(PyGSource *self, visitproc visit, void *arg)
364 if (self->inst_dict) ret = visit(self->inst_dict, arg);
365 if (ret != 0) return ret;
371 pyg_source_clear(PyGSource *self)
375 tmp = self->inst_dict;
376 self->inst_dict = NULL;
380 g_source_unref(self->source);
388 pyg_source_dealloc(PyGSource *self)
390 PyObject_ClearWeakRefs((PyObject *)self);
392 PyObject_GC_UnTrack((PyObject *)self);
394 pyg_source_clear(self);
396 PyObject_GC_Del(self);
400 pyg_source_prepare(GSource *source, gint *timeout)
402 PyGRealSource *pysource = (PyGRealSource *)source;
404 gboolean ret = FALSE;
405 gboolean got_err = TRUE;
406 PyGILState_STATE state;
408 state = pyglib_gil_state_ensure();
410 t = PyObject_CallMethod(pysource->obj, "prepare", NULL);
414 } else if (!PyObject_IsTrue(t)) {
417 } else if (!PyTuple_Check(t)) {
418 PyErr_SetString(PyExc_TypeError,
419 "source prepare function must return a tuple or False");
421 } else if (PyTuple_Size(t) != 2) {
422 PyErr_SetString(PyExc_TypeError,
423 "source prepare function return tuple must be exactly "
428 ret = PyObject_IsTrue(PyTuple_GET_ITEM(t, 0));
429 *timeout = _PyLong_AsLong(PyTuple_GET_ITEM(t, 1));
431 if (*timeout == -1 && PyErr_Occurred()) {
444 pyglib_gil_state_release(state);
450 pyg_source_check(GSource *source)
452 PyGRealSource *pysource = (PyGRealSource *)source;
455 PyGILState_STATE state;
457 state = pyglib_gil_state_ensure();
459 t = PyObject_CallMethod(pysource->obj, "check", NULL);
465 ret = PyObject_IsTrue(t);
469 pyglib_gil_state_release(state);
475 pyg_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
477 PyGRealSource *pysource = (PyGRealSource *)source;
478 PyObject *func, *args, *tuple, *t;
480 PyGILState_STATE state;
482 state = pyglib_gil_state_ensure();
487 func = PyTuple_GetItem(tuple, 0);
488 args = PyTuple_GetItem(tuple, 1);
494 t = PyObject_CallMethod(pysource->obj, "dispatch", "OO", func, args);
500 ret = PyObject_IsTrue(t);
504 pyglib_gil_state_release(state);
510 pyg_source_finalize(GSource *source)
512 PyGRealSource *pysource = (PyGRealSource *)source;
514 PyGILState_STATE state;
516 state = pyglib_gil_state_ensure();
518 func = PyObject_GetAttrString(pysource->obj, "finalize");
520 t = PyObject_CallObject(func, NULL);
530 pyglib_gil_state_release(state);
533 static GSourceFuncs pyg_source_funcs =
542 pyg_source_init(PyGSource *self, PyObject *args, PyObject *kwargs)
544 PyGRealSource *pysource;
546 self->source = g_source_new(&pyg_source_funcs, sizeof(PyGRealSource));
548 pysource = (PyGRealSource *)self->source;
549 pysource->obj = (PyObject*)self;
551 self->inst_dict = NULL;
552 self->weakreflist = NULL;
554 self->python_source = TRUE;
560 pyg_source_free(PyObject *op)
567 PYGLIB_DEFINE_TYPE("glib.Idle", PyGIdle_Type, PyGSource)
570 pyg_idle_repr(PyGSource *self)
572 return source_repr(self, "idle");
576 pyg_idle_init(PyGSource *self, PyObject *args, PyObject *kwargs)
578 static char *kwlist[] = { "priority", NULL };
579 gint priority = G_PRIORITY_DEFAULT_IDLE;
581 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
582 "|i:glib.Idle.__init__", kwlist,
586 self->source = g_idle_source_new ();
588 if (priority != G_PRIORITY_DEFAULT_IDLE)
589 g_source_set_priority(self->source, priority);
591 self->inst_dict = NULL;
592 self->weakreflist = NULL;
594 self->python_source = FALSE;
601 PYGLIB_DEFINE_TYPE("glib.Timeout", PyGTimeout_Type, PyGSource)
604 pyg_timeout_repr(PyGSource *self)
606 return source_repr(self, "timeout");
610 pyg_timeout_init(PyGSource *self, PyObject *args, PyObject *kwargs)
612 static char *kwlist[] = { "interval", "priority", NULL };
613 gint priority = G_PRIORITY_DEFAULT;
616 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
617 "I|i:glib.Timeout.__init__", kwlist,
618 &interval, &priority))
621 self->source = g_timeout_source_new(interval);
623 if (priority != G_PRIORITY_DEFAULT)
624 g_source_set_priority(self->source, priority);
626 self->inst_dict = NULL;
627 self->weakreflist = NULL;
629 self->python_source = FALSE;
636 PYGLIB_DEFINE_TYPE("glib.PollFD", PyGPollFD_Type, PyGPollFD)
638 static PyMemberDef pyg_poll_fd_members[] = {
639 { "fd", T_INT, offsetof(PyGPollFD, pollfd.fd), READONLY },
640 { "events", T_USHORT, offsetof(PyGPollFD, pollfd.events), READONLY },
641 { "revents", T_USHORT, offsetof(PyGPollFD, pollfd.revents), READONLY },
646 pyg_poll_fd_dealloc(PyGPollFD *self)
648 Py_XDECREF(self->fd_obj);
653 pyg_poll_fd_repr(PyGPollFD *self)
655 return _PyUnicode_FromFormat("<GPollFD %d (%d) at 0x%lx>",
656 self->pollfd.fd, self->pollfd.events,
661 pyg_poll_fd_init(PyGPollFD *self, PyObject *args, PyObject *kwargs)
663 static char *kwlist[] = { "fd", "events", NULL };
668 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
669 "OH:glib.PollFD.__init__", kwlist,
673 fd = PyObject_AsFileDescriptor(o);
677 self->pollfd.fd = fd;
678 self->pollfd.events = events;
679 self->pollfd.revents = 0;
688 pyglib_source_register_types(PyObject *d)
690 PyGSource_Type.tp_flags = (Py_TPFLAGS_DEFAULT |
691 Py_TPFLAGS_BASETYPE |
693 PyGSource_Type.tp_init = (initproc)pyg_source_init;
694 PyGSource_Type.tp_free = (freefunc)pyg_source_free;
695 PyGSource_Type.tp_dealloc = (destructor)pyg_source_dealloc;
696 PyGSource_Type.tp_methods = pyg_source_methods;
697 PyGSource_Type.tp_repr = (reprfunc)pyg_source_repr;
698 PyGSource_Type.tp_traverse = (traverseproc)pyg_source_traverse;
699 PyGSource_Type.tp_clear = (inquiry)pyg_source_clear;
700 PyGSource_Type.tp_getset = pyg_source_getsets;
701 PyGSource_Type.tp_weaklistoffset = offsetof(PyGSource, weakreflist);
702 PyGSource_Type.tp_dictoffset = offsetof(PyGSource, inst_dict);
703 PYGLIB_REGISTER_TYPE(d, PyGSource_Type, "Source");
705 PyGIdle_Type.tp_repr = (reprfunc)pyg_idle_repr;
706 PyGIdle_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
707 PyGIdle_Type.tp_base = (PyTypeObject *)&PyGSource_Type;
708 PyGIdle_Type.tp_init = (initproc)pyg_idle_init;
709 PYGLIB_REGISTER_TYPE(d, PyGIdle_Type, "Idle");
711 PyGTimeout_Type.tp_repr = (reprfunc)pyg_timeout_repr;
712 PyGTimeout_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
713 PyGTimeout_Type.tp_base = (PyTypeObject *)&PyGSource_Type;
714 PyGTimeout_Type.tp_init = (initproc)pyg_timeout_init;
715 PYGLIB_REGISTER_TYPE(d, PyGTimeout_Type, "Timeout");
717 PyGPollFD_Type.tp_dealloc = (destructor)pyg_poll_fd_dealloc;
718 PyGPollFD_Type.tp_repr = (reprfunc)pyg_poll_fd_repr;
719 PyGPollFD_Type.tp_flags = Py_TPFLAGS_DEFAULT;
720 PyGPollFD_Type.tp_members = pyg_poll_fd_members;
721 PyGPollFD_Type.tp_init = (initproc)pyg_poll_fd_init;
722 PYGLIB_REGISTER_TYPE(d, PyGPollFD_Type, "PollFD");