af6fc88097b7927302296a9150276f69e1d9c963
[blender.git] / source / blender / python / intern / bpy_rna_callback.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_rna_callback.c
24  *  \ingroup pythonintern
25  */
26
27
28 #include <Python.h>
29
30 #include "RNA_types.h"
31
32 #include "bpy_rna.h"
33 #include "bpy_rna_callback.h"
34 #include "bpy_util.h"
35
36 #include "BLI_utildefines.h"
37
38 #include "DNA_screen_types.h"
39
40 #include "RNA_access.h"
41
42 #include "BKE_context.h"
43 #include "ED_space_api.h"
44
45 /* use this to stop other capsules from being mis-used */
46 #define RNA_CAPSULE_ID "RNA_HANDLE"
47 #define RNA_CAPSULE_ID_INVALID "RNA_HANDLE_REMOVED"
48
49 static void cb_region_draw(const bContext *C, ARegion *UNUSED(ar), void *customdata)
50 {
51         PyObject *cb_func, *cb_args, *result;
52         PyGILState_STATE gilstate;
53
54         bpy_context_set((bContext *)C, &gilstate);
55
56         cb_func= PyTuple_GET_ITEM((PyObject *)customdata, 0);
57         cb_args= PyTuple_GET_ITEM((PyObject *)customdata, 1);
58         result= PyObject_CallObject(cb_func, cb_args);
59
60         if(result) {
61                 Py_DECREF(result);
62         }
63         else {
64                 PyErr_Print();
65                 PyErr_Clear();
66         }
67
68         bpy_context_clear((bContext *)C, &gilstate);
69 }
70
71 PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
72 {
73         void *handle;
74
75         PyObject *cb_func, *cb_args;
76         char *cb_event_str= NULL;
77         int cb_event;
78
79         if (!PyArg_ParseTuple(args, "OO!|s:bpy_struct.callback_add", &cb_func, &PyTuple_Type, &cb_args, &cb_event_str))
80                 return NULL;
81         
82         if(!PyCallable_Check(cb_func)) {
83                 PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable");
84                 return NULL;
85         }
86
87         if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
88                 if(cb_event_str) {
89                         static EnumPropertyItem region_draw_mode_items[]= {
90                                 {REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
91                                 {REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""},
92                                 {REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
93                                 {0, NULL, 0, NULL, NULL}};
94         
95                         if(pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") < 0)
96                                 return NULL;
97                 }
98                 else {
99                         cb_event= REGION_DRAW_POST_PIXEL;
100                 }
101
102                 handle= ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
103                 Py_INCREF(args);
104         }
105         else {
106                 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not suppport callbacks");
107                 return NULL;
108         }
109
110         return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
111 }
112
113 PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
114 {
115         PyObject *py_handle;
116         void *handle;
117         void *customdata;
118
119         if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle))
120                 return NULL;
121
122         handle= PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
123
124         if(handle==NULL) {
125                 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
126                 return NULL;
127         }
128
129         if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
130                 customdata= ED_region_draw_cb_customdata(handle);
131                 Py_DECREF((PyObject *)customdata);
132
133                 ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
134         }
135
136         /* dont allow reuse */
137         PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);
138
139         Py_RETURN_NONE;
140 }