bf3ec8a2609a1bd7780042fcfee40a5df16546e8
[platform/upstream/python-gobject.git] / gi / pygi-ccallback.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
5  *
6  *   pygi-boxed-closure.c: wrapper to handle GClosure box types with C callbacks.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #include "pygi-private.h"
25
26 #include <pygobject.h>
27 #include <girepository.h>
28 #include <pyglib-python-compat.h>
29
30
31 static PyObject *
32 _ccallback_call(PyGICCallback *self, PyObject *args, PyObject *kwargs)
33 {
34     PyObject *result;
35     GCallback *func;
36
37     if (self->cache == NULL) {
38         self->cache = _pygi_callable_cache_new (self->info, TRUE);
39         if (self->cache == NULL)
40             return NULL;
41     }
42
43     result = pygi_callable_info_invoke( (GIBaseInfo *) self->info,
44                                          args,
45                                          kwargs,
46                                          self->cache,
47                                          self->callback,
48                                          self->user_data);
49     return result;
50 }
51
52 PYGLIB_DEFINE_TYPE("gi.CCallback", PyGICCallback_Type, PyGICCallback);
53
54 PyObject *
55 _pygi_ccallback_new (GCallback callback,
56                      gpointer user_data,
57                      GIScopeType scope,
58                      GIFunctionInfo *info,
59                      GDestroyNotify destroy_notify)
60 {
61     PyGICCallback *self;
62
63     if (!callback) {
64         Py_RETURN_NONE;
65     }
66
67     self = (PyGICCallback *) PyGICCallback_Type.tp_alloc (&PyGICCallback_Type, 0);
68     if (self == NULL) {
69         return NULL;
70     }
71
72     self->callback = (GCallback) callback;
73     self->user_data = user_data;
74     self->scope = scope;
75     self->destroy_notify_func = destroy_notify;
76     self->info = g_base_info_ref( (GIBaseInfo *) info);
77
78     return (PyObject *) self;
79 }
80
81 static void
82 _ccallback_dealloc (PyGICCallback *self)
83 {
84     g_base_info_unref ( (GIBaseInfo *)self->info);
85 }
86
87 void
88 _pygi_ccallback_register_types (PyObject *m)
89 {
90     Py_TYPE(&PyGICCallback_Type) = &PyType_Type;
91     PyGICCallback_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
92     PyGICCallback_Type.tp_dealloc = (destructor) _ccallback_dealloc;
93     PyGICCallback_Type.tp_call = (ternaryfunc) _ccallback_call;
94
95
96     if (PyType_Ready (&PyGICCallback_Type))
97         return;
98     if (PyModule_AddObject (m, "CCallback", (PyObject *) &PyGICCallback_Type))
99         return;
100 }