summaryrefslogtreecommitdiffstats
path: root/glib/pygsource.c
blob: 298b928343446dcf80718796497ae2958520666c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/* -*- Mode: C; c-basic-offset: 4 -*-
 * pygtk- Python bindings for the GTK toolkit.
 * Copyright (C) 1998-2003  James Henstridge
 * Copyright (C) 2005       Oracle
 *
 * Author: Manish Singh <manish.singh@oracle.com>
 *
 *   pygsource.c: GSource wrapper
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <Python.h>
#include <pythread.h>
#include <structmember.h> /* for PyMemberDef */
#include "pyglib.h"
#include "pyglib-private.h"
#include "pygmaincontext.h"
#include "pygsource.h"

#define CHECK_DESTROYED(self, ret)			G_STMT_START {	\
    if ((self)->source == NULL) {					\
	PyErr_SetString(PyExc_RuntimeError, "source is destroyed");	\
	return (ret);							\
    }									\
} G_STMT_END


typedef struct {
    PyObject_HEAD
    GSource *source;
    PyObject *inst_dict;
    PyObject *weakreflist;
    gboolean python_source;
} PyGSource;

typedef struct
{
    GSource source;
    PyObject *obj;
} PyGRealSource;

/* glib.Source */

PYGLIB_DEFINE_TYPE("glib.Source", PyGSource_Type, PyGSource)

static PyObject *
source_repr(PyGSource *self, const char *type)
{
    gchar buf[256], *desc;
 
    if (self->source) {
	if (g_source_get_context(self->source))
	    desc = "attached";
	else
	    desc = "unattached";
    } else {
	desc = "destroyed";
    }

    if (type)
	g_snprintf(buf, sizeof(buf), "<%s glib %s source at 0x%lx>",
		   desc, type, (long) self);
    else
	g_snprintf(buf, sizeof(buf), "<%s glib source at 0x%lx>",
		   desc, (long) self);

    return _PyUnicode_FromString(buf);
}

static PyObject *
pyg_source_attach(PyGSource *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "context", NULL };
    PyGMainContext *py_context = NULL;
    GMainContext *context = NULL;
    guint id;

    if (!PyArg_ParseTupleAndKeywords (args, kwargs,
				      "|O!:attach", kwlist,
				      &PyGMainContext_Type, &py_context))
	return NULL;

    if (py_context)
	context = py_context->context;

    CHECK_DESTROYED(self, NULL);

    if (self->python_source) {
	PyGRealSource *pysource = (PyGRealSource *)self->source;
	Py_INCREF(pysource->obj);
    }

    id = g_source_attach(self->source, context);
    return _PyLong_FromLong(id);
}

static PyObject *
pyg_source_destroy(PyGSource *self)
{
    CHECK_DESTROYED(self, NULL);

    if (self->python_source && self->source->context) {
	PyGRealSource *pysource = (PyGRealSource *)self->source;
	Py_DECREF(pysource->obj);
    }

    g_source_destroy(self->source);
    self->source = NULL;

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pyg_source_set_callback(PyGSource *self, PyObject *args)
{
    PyObject *first, *callback, *cbargs = NULL, *data;
    gint len;

    CHECK_DESTROYED(self, NULL);

    len = PyTuple_Size (args);
    if (len < 1) {
	PyErr_SetString(PyExc_TypeError,
			"set_callback requires at least 1 argument");
	return NULL;
    }

    first = PySequence_GetSlice(args, 0, 1);
    if (!PyArg_ParseTuple(first, "O:set_callback", &callback)) {
	Py_DECREF (first);
	return NULL;
    }
    Py_DECREF(first);

    if (!PyCallable_Check(callback)) {
	PyErr_SetString(PyExc_TypeError, "first argument not callable");
	return NULL;
    }

    cbargs = PySequence_GetSlice(args, 1, len);
    if (cbargs == NULL)
	return NULL;

    data = Py_BuildValue("(ON)", callback, cbargs);
    if (data == NULL)
	return NULL;

    g_source_set_callback(self->source,
			  _pyglib_handler_marshal, data,
			  _pyglib_destroy_notify);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pyg_source_get_context(PyGSource *self)
{
    GMainContext *context;

    CHECK_DESTROYED(self, NULL);

    context = g_source_get_context(self->source);

    if (context) {
	return pyglib_main_context_new(context);
    } else {
	Py_INCREF(Py_None);
	return Py_None;
    }
}

static PyObject *
pyg_source_add_poll(PyGSource *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "fd", NULL };
    PyGPollFD *fd;

    if (!self->python_source) {
	PyErr_SetString(PyExc_TypeError,
			"add_poll can only be used with sources "
			"implemented in python");
	return NULL;
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "O!:add_poll", kwlist,
				     &PyGPollFD_Type, &fd))
	return NULL;

    CHECK_DESTROYED(self, NULL);

    g_source_add_poll(self->source, &fd->pollfd);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pyg_source_remove_poll(PyGSource *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "fd", NULL };
    PyGPollFD *fd;

    if (!self->python_source) {
	PyErr_SetString(PyExc_TypeError,
			"remove_poll can only be used with sources "
			"implemented in python");
	return NULL;
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "O!:remove_poll", kwlist,
				     &PyGPollFD_Type, &fd))
	return NULL;

    CHECK_DESTROYED(self, NULL);

    g_source_remove_poll(self->source, &fd->pollfd);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pyg_source_get_current_time(PyGSource *self)
{
    GTimeVal timeval;
    double   ret;

    CHECK_DESTROYED(self, NULL);

    g_source_get_current_time(self->source, &timeval);
    ret = (double)timeval.tv_sec + (double)timeval.tv_usec * 0.000001;
    return PyFloat_FromDouble(ret);
}

static PyMethodDef pyg_source_methods[] = {
    { "attach", (PyCFunction)pyg_source_attach, METH_KEYWORDS },
    { "destroy", (PyCFunction)pyg_source_destroy, METH_NOARGS },
    { "set_callback", (PyCFunction)pyg_source_set_callback, METH_VARARGS },
    { "get_context", (PyCFunction)pyg_source_get_context, METH_NOARGS },
    { "add_poll", (PyCFunction)pyg_source_add_poll, METH_KEYWORDS },
    { "remove_poll", (PyCFunction)pyg_source_remove_poll, METH_KEYWORDS },
    { "get_current_time", (PyCFunction)pyg_source_get_current_time, METH_NOARGS },
    { NULL, NULL, 0 }
};

static PyObject *
pyg_source_get_dict(PyGSource *self, void *closure)
{
    if (self->inst_dict == NULL) {
	self->inst_dict = PyDict_New();
	if (self->inst_dict == NULL)
	    return NULL;
    }

    Py_INCREF(self->inst_dict);
    return self->inst_dict;
}

static PyObject *
pyg_source_get_priority(PyGSource *self, void *closure)
{
    CHECK_DESTROYED(self, NULL);

    return _PyLong_FromLong(g_source_get_priority(self->source));
}

static int
pyg_source_set_priority(PyGSource *self, PyObject *value, void *closure)
{
    CHECK_DESTROYED(self, -1);

    if (value == NULL) {
	PyErr_SetString(PyExc_TypeError, "cannot delete priority");
	return -1;
    }

    if (!_PyLong_Check(value)) {
	PyErr_SetString(PyExc_TypeError, "type mismatch");
	return -1;
    }

    g_source_set_priority(self->source, _PyLong_AsLong(value));

    return 0;
}

static PyObject *
pyg_source_get_can_recurse(PyGSource *self, void *closure)
{
    CHECK_DESTROYED(self, NULL);

    return PyBool_FromLong(g_source_get_can_recurse(self->source));
}

static int
pyg_source_set_can_recurse(PyGSource *self, PyObject *value, void *closure)
{
    CHECK_DESTROYED(self, -1);

    if (value == NULL) {
	PyErr_SetString(PyExc_TypeError, "cannot delete can_recurse");
	return -1;
    }

    g_source_set_can_recurse(self->source, PyObject_IsTrue(value));

    return 0;
}

static PyObject *
pyg_source_get_id(PyGSource *self, void *closure)
{
    CHECK_DESTROYED(self, NULL);

    if (g_source_get_context(self->source) == NULL) {
	PyErr_SetString(PyExc_RuntimeError, "source is not attached");
	return NULL;
    }

    return _PyLong_FromLong(g_source_get_id(self->source));
}

static PyGetSetDef pyg_source_getsets[] = {
    { "__dict__", (getter)pyg_source_get_dict,  (setter)0 },
    {"priority", (getter)pyg_source_get_priority, (setter)pyg_source_set_priority },
    {"can_recurse", (getter)pyg_source_get_can_recurse, (setter)pyg_source_set_can_recurse },
    {"id", (getter)pyg_source_get_id, (setter)0 },
    {NULL, 0, 0}
};

static PyObject *
pyg_source_repr(PyGSource *self)
{
    return source_repr(self, NULL);
}

static int
pyg_source_traverse(PyGSource *self, visitproc visit, void *arg)
{
    int ret = 0;

    if (self->inst_dict) ret = visit(self->inst_dict, arg);
    if (ret != 0) return ret;

    return 0;
}

static int
pyg_source_clear(PyGSource *self)
{
    PyObject *tmp;

    tmp = self->inst_dict;
    self->inst_dict = NULL;
    Py_XDECREF(tmp);

    if (self->source) {
	g_source_unref(self->source);
	self->source = NULL;
    }

    return 0;
}

static void
pyg_source_dealloc(PyGSource *self)
{
    PyObject_ClearWeakRefs((PyObject *)self);

    PyObject_GC_UnTrack((PyObject *)self);

    pyg_source_clear(self);

    PyObject_GC_Del(self);
}

static gboolean
pyg_source_prepare(GSource *source, gint *timeout)
{
    PyGRealSource *pysource = (PyGRealSource *)source;
    PyObject *t;
    gboolean ret = FALSE;
    gboolean got_err = TRUE;
    PyGILState_STATE state;

    state = pyglib_gil_state_ensure();

    t = PyObject_CallMethod(pysource->obj, "prepare", NULL);

    if (t == NULL) {
	goto bail;
    } else if (!PyObject_IsTrue(t)) {
	got_err = FALSE;
	goto bail;
    } else if (!PyTuple_Check(t)) {
	PyErr_SetString(PyExc_TypeError,
			"source prepare function must return a tuple or False");
	goto bail;
    } else if (PyTuple_Size(t) != 2) {
	PyErr_SetString(PyExc_TypeError,
			"source prepare function return tuple must be exactly "
			"2 elements long");
	goto bail;
    }

    ret = PyObject_IsTrue(PyTuple_GET_ITEM(t, 0));
	*timeout = _PyLong_AsLong(PyTuple_GET_ITEM(t, 1));

	if (*timeout == -1 && PyErr_Occurred()) {
	    ret = FALSE;
	    goto bail;
	}

    got_err = FALSE;

bail:
    if (got_err)
	PyErr_Print();

    Py_XDECREF(t);

    pyglib_gil_state_release(state);

    return ret;
}

static gboolean
pyg_source_check(GSource *source)
{
    PyGRealSource *pysource = (PyGRealSource *)source;
    PyObject *t;
    gboolean ret;
    PyGILState_STATE state;

    state = pyglib_gil_state_ensure();

    t = PyObject_CallMethod(pysource->obj, "check", NULL);

    if (t == NULL) {
	PyErr_Print();
	ret = FALSE;
    } else {
	ret = PyObject_IsTrue(t);
	Py_DECREF(t);
    }

    pyglib_gil_state_release(state);

    return ret;
}

static gboolean
pyg_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{
    PyGRealSource *pysource = (PyGRealSource *)source;
    PyObject *func, *args, *tuple, *t;
    gboolean ret;
    PyGILState_STATE state;

    state = pyglib_gil_state_ensure();

    if (callback) {
	tuple = user_data;

	func = PyTuple_GetItem(tuple, 0);
        args = PyTuple_GetItem(tuple, 1);
    } else {
	func = Py_None;
	args = Py_None;
    }

    t = PyObject_CallMethod(pysource->obj, "dispatch", "OO", func, args);

    if (t == NULL) {
	PyErr_Print();
	ret = FALSE;
    } else {
	ret = PyObject_IsTrue(t);
	Py_DECREF(t);
    }

    pyglib_gil_state_release(state);

    return ret;
}

static void
pyg_source_finalize(GSource *source)
{
    PyGRealSource *pysource = (PyGRealSource *)source;
    PyObject *func, *t;
    PyGILState_STATE state;

    state = pyglib_gil_state_ensure();

    func = PyObject_GetAttrString(pysource->obj, "finalize");
    if (func) {
	t = PyObject_CallObject(func, NULL);
	Py_DECREF(func);

	if (t == NULL) {
	    PyErr_Print();
	} else {
	    Py_DECREF(t);
	}
    }

    pyglib_gil_state_release(state);
}

static GSourceFuncs pyg_source_funcs =
{
    pyg_source_prepare,
    pyg_source_check,
    pyg_source_dispatch,
    pyg_source_finalize
};

static int
pyg_source_init(PyGSource *self, PyObject *args, PyObject *kwargs)
{
    PyGRealSource *pysource;

    self->source = g_source_new(&pyg_source_funcs, sizeof(PyGRealSource));

    pysource = (PyGRealSource *)self->source;
    pysource->obj = (PyObject*)self;

    self->inst_dict = NULL;
    self->weakreflist = NULL;

    self->python_source = TRUE;

    return 0;
}

static void
pyg_source_free(PyObject *op)
{
    PyObject_GC_Del(op);
}

/* glib.Idle */

PYGLIB_DEFINE_TYPE("glib.Idle", PyGIdle_Type, PyGSource)

static PyObject *
pyg_idle_repr(PyGSource *self)
{
    return source_repr(self, "idle");
}

static int
pyg_idle_init(PyGSource *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "priority", NULL };
    gint priority = G_PRIORITY_DEFAULT_IDLE;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "|i:glib.Idle.__init__", kwlist,
				     &priority))
	return -1;

    self->source = g_idle_source_new ();

    if (priority != G_PRIORITY_DEFAULT_IDLE)
	g_source_set_priority(self->source, priority);

    self->inst_dict = NULL;
    self->weakreflist = NULL;

    self->python_source = FALSE;

    return 0;
}

/* glib.Timeout */

PYGLIB_DEFINE_TYPE("glib.Timeout", PyGTimeout_Type, PyGSource)

static PyObject *
pyg_timeout_repr(PyGSource *self)
{
    return source_repr(self, "timeout");
}

static int
pyg_timeout_init(PyGSource *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "interval", "priority", NULL };
    gint priority = G_PRIORITY_DEFAULT;
    guint interval;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "I|i:glib.Timeout.__init__", kwlist,
				     &interval, &priority))
	return -1;

    self->source = g_timeout_source_new(interval);

    if (priority != G_PRIORITY_DEFAULT)
	g_source_set_priority(self->source, priority);

    self->inst_dict = NULL;
    self->weakreflist = NULL;

    self->python_source = FALSE;

    return 0;
}

/* glib.PollFD */

PYGLIB_DEFINE_TYPE("glib.PollFD", PyGPollFD_Type, PyGPollFD)

static PyMemberDef pyg_poll_fd_members[] = {
    { "fd",      T_INT,    offsetof(PyGPollFD, pollfd.fd),      READONLY },
    { "events",  T_USHORT, offsetof(PyGPollFD, pollfd.events),  READONLY },
    { "revents", T_USHORT, offsetof(PyGPollFD, pollfd.revents), READONLY },
    { NULL, 0, 0, 0 }
};

static void
pyg_poll_fd_dealloc(PyGPollFD *self)
{
    Py_XDECREF(self->fd_obj);
    PyObject_DEL(self);
}

static PyObject *
pyg_poll_fd_repr(PyGPollFD *self)
{
    return _PyUnicode_FromFormat("<GPollFD %d (%d) at 0x%lx>",
				 self->pollfd.fd, self->pollfd.events,
				 (long)self);
}

static int
pyg_poll_fd_init(PyGPollFD *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "fd", "events", NULL };
    PyObject *o;
    gint fd;
    gushort events;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "OH:glib.PollFD.__init__", kwlist,
				     &o, &events))
	return -1;

    fd = PyObject_AsFileDescriptor(o);
    if (fd == -1)
	return -1;

    self->pollfd.fd = fd;
    self->pollfd.events = events;
    self->pollfd.revents = 0;

    Py_INCREF(o);
    self->fd_obj = o;

    return 0;
}

void
pyglib_source_register_types(PyObject *d)
{
    PyGSource_Type.tp_flags = (Py_TPFLAGS_DEFAULT |
			       Py_TPFLAGS_BASETYPE |
			       Py_TPFLAGS_HAVE_GC);
    PyGSource_Type.tp_init = (initproc)pyg_source_init;
    PyGSource_Type.tp_free = (freefunc)pyg_source_free;
    PyGSource_Type.tp_dealloc = (destructor)pyg_source_dealloc;
    PyGSource_Type.tp_methods = pyg_source_methods;
    PyGSource_Type.tp_repr = (reprfunc)pyg_source_repr;
    PyGSource_Type.tp_traverse = (traverseproc)pyg_source_traverse;
    PyGSource_Type.tp_clear = (inquiry)pyg_source_clear;
    PyGSource_Type.tp_getset = pyg_source_getsets;
    PyGSource_Type.tp_weaklistoffset = offsetof(PyGSource, weakreflist);
    PyGSource_Type.tp_dictoffset = offsetof(PyGSource, inst_dict);
    PYGLIB_REGISTER_TYPE(d, PyGSource_Type, "Source");

    PyGIdle_Type.tp_repr = (reprfunc)pyg_idle_repr;
    PyGIdle_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
    PyGIdle_Type.tp_base = (PyTypeObject *)&PyGSource_Type;
    PyGIdle_Type.tp_init = (initproc)pyg_idle_init;
    PYGLIB_REGISTER_TYPE(d, PyGIdle_Type, "Idle");

    PyGTimeout_Type.tp_repr = (reprfunc)pyg_timeout_repr;
    PyGTimeout_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
    PyGTimeout_Type.tp_base = (PyTypeObject *)&PyGSource_Type;
    PyGTimeout_Type.tp_init = (initproc)pyg_timeout_init;
    PYGLIB_REGISTER_TYPE(d, PyGTimeout_Type, "Timeout");

    PyGPollFD_Type.tp_dealloc = (destructor)pyg_poll_fd_dealloc;
    PyGPollFD_Type.tp_repr = (reprfunc)pyg_poll_fd_repr;
    PyGPollFD_Type.tp_flags = Py_TPFLAGS_DEFAULT;
    PyGPollFD_Type.tp_members = pyg_poll_fd_members;
    PyGPollFD_Type.tp_init = (initproc)pyg_poll_fd_init;
    PYGLIB_REGISTER_TYPE(d, PyGPollFD_Type, "PollFD");
}