Merge branch 'master' into blender2.8
[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  * This file currently exposes callbacks for interface regions but may be
27  * extended later.
28  */
29
30
31 #include <Python.h>
32
33 #include "RNA_types.h"
34
35 #include "BLI_utildefines.h"
36
37 #include "bpy_rna.h"
38 #include "bpy_rna_callback.h"
39 #include "bpy_capi_utils.h"
40
41 #include "DNA_space_types.h"
42 #include "DNA_screen_types.h"
43
44 #include "RNA_access.h"
45 #include "RNA_enum_types.h"
46
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49
50 #include "ED_space_api.h"
51
52 /* use this to stop other capsules from being mis-used */
53 #define RNA_CAPSULE_ID "RNA_HANDLE"
54 #define RNA_CAPSULE_ID_INVALID "RNA_HANDLE_REMOVED"
55
56 static const EnumPropertyItem region_draw_mode_items[] = {
57         {REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
58         {REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""},
59         {REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
60         {0, NULL, 0, NULL, NULL}
61 };
62
63 static void cb_region_draw(const bContext *C, ARegion *UNUSED(ar), void *customdata)
64 {
65         PyObject *cb_func, *cb_args, *result;
66         PyGILState_STATE gilstate;
67
68         bpy_context_set((bContext *)C, &gilstate);
69
70         cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
71         cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
72         result = PyObject_CallObject(cb_func, cb_args);
73
74         if (result) {
75                 Py_DECREF(result);
76         }
77         else {
78                 PyErr_Print();
79                 PyErr_Clear();
80         }
81
82         bpy_context_clear((bContext *)C, &gilstate);
83 }
84
85 #if 0
86 PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
87 {
88         void *handle;
89
90         PyObject *cb_func, *cb_args;
91         char *cb_event_str = NULL;
92         int cb_event;
93
94         if (!PyArg_ParseTuple(args, "OO!|s:bpy_struct.callback_add", &cb_func, &PyTuple_Type, &cb_args, &cb_event_str))
95                 return NULL;
96
97         if (!PyCallable_Check(cb_func)) {
98                 PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable");
99                 return NULL;
100         }
101
102         if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
103                 if (cb_event_str) {
104                         if (pyrna_enum_value_from_id(
105                                     region_draw_mode_items, cb_event_str,
106                                     &cb_event, "bpy_struct.callback_add()") == -1)
107                         {
108                                 return NULL;
109                         }
110                 }
111                 else {
112                         cb_event = REGION_DRAW_POST_PIXEL;
113                 }
114
115                 handle = ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
116                 Py_INCREF(args);
117         }
118         else {
119                 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
120                 return NULL;
121         }
122
123         return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
124 }
125
126 PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
127 {
128         PyObject *py_handle;
129         void *handle;
130         void *customdata;
131
132         if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle))
133                 return NULL;
134
135         handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
136
137         if (handle == NULL) {
138                 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
139                 return NULL;
140         }
141
142         if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
143                 customdata = ED_region_draw_cb_customdata(handle);
144                 Py_DECREF((PyObject *)customdata);
145
146                 ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
147         }
148         else {
149                 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
150                 return NULL;
151         }
152
153         /* don't allow reuse */
154         PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);
155
156         Py_RETURN_NONE;
157 }
158 #endif
159
160 /* reverse of rna_Space_refine() */
161 static eSpace_Type rna_Space_refine_reverse(StructRNA *srna)
162 {
163         if (srna == &RNA_SpaceView3D)           return SPACE_VIEW3D;
164         if (srna == &RNA_SpaceGraphEditor)      return SPACE_IPO;
165         if (srna == &RNA_SpaceOutliner)         return SPACE_OUTLINER;
166         if (srna == &RNA_SpaceProperties)       return SPACE_BUTS;
167         if (srna == &RNA_SpaceFileBrowser)      return SPACE_FILE;
168         if (srna == &RNA_SpaceImageEditor)      return SPACE_IMAGE;
169         if (srna == &RNA_SpaceInfo)             return SPACE_INFO;
170         if (srna == &RNA_SpaceSequenceEditor)   return SPACE_SEQ;
171         if (srna == &RNA_SpaceTextEditor)       return SPACE_TEXT;
172         if (srna == &RNA_SpaceDopeSheetEditor)  return SPACE_ACTION;
173         if (srna == &RNA_SpaceNLA)              return SPACE_NLA;
174         if (srna == &RNA_SpaceNodeEditor)       return SPACE_NODE;
175         if (srna == &RNA_SpaceConsole)          return SPACE_CONSOLE;
176         if (srna == &RNA_SpaceUserPreferences)  return SPACE_USERPREF;
177         if (srna == &RNA_SpaceClipEditor)       return SPACE_CLIP;
178         return SPACE_EMPTY;
179 }
180
181 PyObject *pyrna_callback_classmethod_add(PyObject *UNUSED(self), PyObject *args)
182 {
183         void *handle;
184         PyObject *cls;
185         PyObject *cb_func, *cb_args;
186         const char *cb_regiontype_str;
187         const char *cb_event_str;
188         int cb_event;
189         int cb_regiontype;
190         StructRNA *srna;
191
192         if (PyTuple_GET_SIZE(args) < 2) {
193                 PyErr_SetString(PyExc_ValueError, "handler_add(handler): expected at least 2 args");
194                 return NULL;
195         }
196
197         cls = PyTuple_GET_ITEM(args, 0);
198         if (!(srna = pyrna_struct_as_srna(cls, false, "handler_add"))) {
199                 return NULL;
200         }
201         cb_func = PyTuple_GET_ITEM(args, 1);
202         if (!PyCallable_Check(cb_func)) {
203                 PyErr_SetString(PyExc_TypeError, "first argument isn't callable");
204                 return NULL;
205         }
206
207         /* class specific callbacks */
208         if (RNA_struct_is_a(srna, &RNA_Space)) {
209                 if (!PyArg_ParseTuple(args, "OOO!ss:Space.draw_handler_add",
210                                       &cls, &cb_func,  /* already assigned, no matter */
211                                       &PyTuple_Type, &cb_args, &cb_regiontype_str, &cb_event_str))
212                 {
213                         return NULL;
214                 }
215
216                 if (pyrna_enum_value_from_id(
217                             region_draw_mode_items, cb_event_str,
218                             &cb_event, "bpy_struct.callback_add()") == -1)
219                 {
220                         return NULL;
221                 }
222                 else if (pyrna_enum_value_from_id(
223                                  rna_enum_region_type_items, cb_regiontype_str,
224                                  &cb_regiontype, "bpy_struct.callback_add()") == -1)
225                 {
226                         return NULL;
227                 }
228                 else {
229                         const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
230                         if (spaceid == SPACE_EMPTY) {
231                                 PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
232                                 return NULL;
233                         }
234                         else {
235                                 SpaceType *st = BKE_spacetype_from_id(spaceid);
236                                 ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);
237                                 if (art == NULL) {
238                                         PyErr_Format(PyExc_TypeError, "region type '%.200s' not in space", cb_regiontype_str);
239                                         return NULL;
240                                 }
241                                 handle = ED_region_draw_cb_activate(art, cb_region_draw, (void *)args, cb_event);
242                                 Py_INCREF(args);
243                         }
244                 }
245         }
246         else {
247                 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
248                 return NULL;
249         }
250
251         return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
252 }
253
254 PyObject *pyrna_callback_classmethod_remove(PyObject *UNUSED(self), PyObject *args)
255 {
256         PyObject *cls;
257         PyObject *py_handle;
258         void *handle;
259         void *customdata;
260         StructRNA *srna;
261         const char *cb_regiontype_str;
262         int cb_regiontype;
263
264         if (PyTuple_GET_SIZE(args) < 2) {
265                 PyErr_SetString(PyExc_ValueError, "callback_remove(handler): expected at least 2 args");
266                 return NULL;
267         }
268
269         cls = PyTuple_GET_ITEM(args, 0);
270         if (!(srna = pyrna_struct_as_srna(cls, false, "callback_remove"))) {
271                 return NULL;
272         }
273         py_handle = PyTuple_GET_ITEM(args, 1);
274         handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
275         if (handle == NULL) {
276                 PyErr_SetString(PyExc_ValueError, "callback_remove(handler): NULL handler given, invalid or already removed");
277                 return NULL;
278         }
279
280         if (RNA_struct_is_a(srna, &RNA_Space)) {
281                 if (!PyArg_ParseTuple(args, "OO!s:Space.draw_handler_remove",
282                                       &cls, &PyCapsule_Type, &py_handle,  /* already assigned, no matter */
283                                       &cb_regiontype_str))
284                 {
285                         return NULL;
286                 }
287
288                 customdata = ED_region_draw_cb_customdata(handle);
289                 Py_DECREF((PyObject *)customdata);
290
291                 if (pyrna_enum_value_from_id(
292                             rna_enum_region_type_items, cb_regiontype_str,
293                             &cb_regiontype, "bpy_struct.callback_remove()") == -1)
294                 {
295                         return NULL;
296                 }
297                 else {
298                         const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
299                         if (spaceid == SPACE_EMPTY) {
300                                 PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
301                                 return NULL;
302                         }
303                         else {
304                                 SpaceType *st = BKE_spacetype_from_id(spaceid);
305                                 ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);
306                                 if (art == NULL) {
307                                         PyErr_Format(PyExc_TypeError, "region type '%.200s' not in space", cb_regiontype_str);
308                                         return NULL;
309                                 }
310                                 ED_region_draw_cb_exit(art, handle);
311                         }
312                 }
313         }
314         else {
315                 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
316                 return NULL;
317         }
318
319         /* don't allow reuse */
320         PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);
321
322         Py_RETURN_NONE;
323 }