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