summaryrefslogtreecommitdiffstats
path: root/src/pysource.c
blob: 7dca6c0fc46e6a8c7879280486a6a16b652a022f (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
/* 
    irssi-python

    Copyright (C) 2006 Christopher Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <Python.h>
#include "pyirssi.h"
#include "pysource.h"

typedef struct _PY_SOURCE_REC
{
    int tag;
    GSList **tag_list;
    int fd;
    PyObject *func;
    PyObject *data;
} PY_SOURCE_REC;

static PY_SOURCE_REC *py_source_rec_new(GSList **tag_list, int fd, PyObject *func, PyObject *data)
{
    PY_SOURCE_REC *rec;

    rec = g_new0(PY_SOURCE_REC, 1);
    rec->tag_list = tag_list;
    rec->fd = fd;
    rec->func = func;
    rec->data = data;

    Py_INCREF(func);
    Py_XINCREF(data);

    return rec;
}

static int py_remove_tag(GSList **list, int handle)
{
    GSList *node;

    node = g_slist_find(*list, GINT_TO_POINTER(handle));
    if (!node)
        return 0;

    *list = g_slist_delete_link(*list, node);

    return 1;
}

static void py_source_destroy(PY_SOURCE_REC *rec)
{
    g_return_if_fail(py_remove_tag(rec->tag_list, rec->tag) == 1);
    Py_DECREF(rec->func);
    Py_XDECREF(rec->data);
    g_free(rec);
}

static int py_handle_ret(PyObject *ret)
{
    int res;

    if (!ret)
    {
        PyErr_Print();
        res = FALSE; 
    }
    else
    {
        res = PyObject_IsTrue(ret);
        Py_DECREF(ret);
    }

    return res;
}

static int py_timeout_proxy(PY_SOURCE_REC *rec)
{
    PyObject *ret;

    g_return_val_if_fail(rec != NULL, FALSE);
    
    if (rec->data)
        ret = PyObject_CallFunction(rec->func, "O", rec->data);
    else
        ret = PyObject_CallFunction(rec->func, "");

    return py_handle_ret(ret);
}

static int py_io_proxy(GIOChannel *src, GIOCondition condition, PY_SOURCE_REC *rec)
{
    PyObject *ret;

    g_return_val_if_fail(rec != NULL, FALSE);

    if (rec->data)
        ret = PyObject_CallFunction(rec->func, "iiO", rec->fd, condition, rec->data);
    else
        ret = PyObject_CallFunction(rec->func, "ii", rec->fd, condition);

    return py_handle_ret(ret);
}

int pysource_timeout_add_list(GSList **list, int msecs, PyObject *func, PyObject *data)
{
    PY_SOURCE_REC *rec;

    g_return_val_if_fail(func != NULL, -1);

    rec = py_source_rec_new(list, -1, func, data);
    rec->tag = g_timeout_add_full(G_PRIORITY_DEFAULT, msecs, 
            (GSourceFunc)py_timeout_proxy, rec, 
            (GDestroyNotify)py_source_destroy);
    
    *list = g_slist_append(*list, GINT_TO_POINTER(rec->tag));
    
    return rec->tag;
}

int pysource_io_add_watch_list(GSList **list, int fd, int cond, PyObject *func, PyObject *data)
{
    PY_SOURCE_REC *rec;
    GIOChannel *channel;

    g_return_val_if_fail(func != NULL, 1);

    rec = py_source_rec_new(list, fd, func, data);
    channel = g_io_channel_unix_new(fd);
    rec->tag = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, 
            (GIOFunc)py_io_proxy, rec,
            (GDestroyNotify)py_source_destroy);
    g_io_channel_unref(channel);
   
    *list = g_slist_append(*list, GINT_TO_POINTER(rec->tag));
    
    return rec->tag;
}