Fix various compiler warnings.
[blender-staging.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_util.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 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(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") == -1) {
105                                 return NULL;
106                         }
107                 }
108                 else {
109                         cb_event = REGION_DRAW_POST_PIXEL;
110                 }
111
112                 handle = ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
113                 Py_INCREF(args);
114         }
115         else {
116                 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
117                 return NULL;
118         }
119
120         return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
121 }
122
123 PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
124 {
125         PyObject *py_handle;
126         void *handle;
127         void *customdata;
128
129         if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle))
130                 return NULL;
131
132         handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
133
134         if (handle == NULL) {
135                 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
136                 return NULL;
137         }
138
139         if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
140                 customdata = ED_region_draw_cb_customdata(handle);
141                 Py_DECREF((PyObject *)customdata);
142
143                 ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
144         }
145         else {
146                 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
147                 return NULL;
148         }
149
150         /* don't allow reuse */
151         PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);
152
153         Py_RETURN_NONE;
154 }
155 #endif
156
157 /* reverse of rna_Space_refine() */
158 static eSpace_Type rna_Space_refine_reverse(StructRNA *srna)
159 {
160         if (srna == &RNA_SpaceView3D)           return SPACE_VIEW3D;
161         if (srna == &RNA_SpaceGraphEditor)      return SPACE_IPO;
162         if (srna == &RNA_SpaceOutliner)         return SPACE_OUTLINER;
163         if (srna == &RNA_SpaceProperties)       return SPACE_BUTS;
164         if (srna == &RNA_SpaceFileBrowser)      return SPACE_FILE;
165         if (srna == &RNA_SpaceImageEditor)      return SPACE_IMAGE;
166         if (srna == &RNA_SpaceInfo)             return SPACE_INFO;
167         if (srna == &RNA_SpaceSequenceEditor)   return SPACE_SEQ;
168         if (srna == &RNA_SpaceTextEditor)       return SPACE_TEXT;
169         if (srna == &RNA_SpaceDopeSheetEditor)  return SPACE_ACTION;
170         if (srna == &RNA_SpaceNLA)              return SPACE_NLA;
171         if (srna == &RNA_SpaceTimeline)         return SPACE_TIME;
172         if (srna == &RNA_SpaceNodeEditor)       return SPACE_NODE;
173         if (srna == &RNA_SpaceLogicEditor)      return SPACE_LOGIC;
174         if (srna == &RNA_SpaceConsole)          return SPACE_CONSOLE;
175         if (srna == &RNA_SpaceUserPreferences)  return SPACE_USERPREF;
176         if (srna == &RNA_SpaceClipEditor)       return SPACE_CLIP;
177         return SPACE_EMPTY;
178 }
179
180 PyObject *pyrna_callback_classmethod_add(PyObject *UNUSED(self), PyObject *args)
181 {
182         void *handle;
183         PyObject *cls;
184         PyObject *cb_func, *cb_args;
185         const char *cb_regiontype_str;
186         const char *cb_event_str;
187         int cb_event;
188         int cb_regiontype;
189         StructRNA *srna;
190
191         if (PyTuple_GET_SIZE(args) < 2) {
192                 PyErr_SetString(PyExc_ValueError, "handler_add(handle): expected at least 2 args");
193                 return NULL;
194         }
195
196         cls = PyTuple_GET_ITEM(args, 0);
197         if (!(srna = pyrna_struct_as_srna(cls, false, "handler_add"))) {
198                 return NULL;
199         }
200         cb_func = PyTuple_GET_ITEM(args, 1);
201         if (!PyCallable_Check(cb_func)) {
202                 PyErr_SetString(PyExc_TypeError, "first argument isn't callable");
203                 return NULL;
204         }
205
206         /* class specific callbacks */
207         if (RNA_struct_is_a(srna, &RNA_Space)) {
208                 if (!PyArg_ParseTuple(args, "OOO!ss:Space.draw_handler_add",
209                                       &cls, &cb_func,  /* already assigned, no matter */
210                                       &PyTuple_Type, &cb_args, &cb_regiontype_str, &cb_event_str))
211                 {
212                         return NULL;
213                 }
214
215                 if (pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") == -1) {
216                         return NULL;
217                 }
218                 else if (pyrna_enum_value_from_id(region_type_items, cb_regiontype_str, &cb_regiontype, "bpy_struct.callback_add()") == -1) {
219                         return NULL;
220                 }
221                 else {
222                         const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
223                         if (spaceid == SPACE_EMPTY) {
224                                 PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
225                                 return NULL;
226                         }
227                         else {
228                                 SpaceType *st = BKE_spacetype_from_id(spaceid);
229                                 ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);
230
231                                 handle = ED_region_draw_cb_activate(art, cb_region_draw, (void *)args, cb_event);
232                                 Py_INCREF(args);
233                         }
234                 }
235         }
236         else {
237                 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
238                 return NULL;
239         }
240
241         return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
242 }
243
244 PyObject *pyrna_callback_classmethod_remove(PyObject *UNUSED(self), PyObject *args)
245 {
246         PyObject *cls;
247         PyObject *py_handle;
248         void *handle;
249         void *customdata;
250         StructRNA *srna;
251         const char *cb_regiontype_str;
252         int cb_regiontype;
253
254         if (PyTuple_GET_SIZE(args) < 2) {
255                 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): expected at least 2 args");
256                 return NULL;
257         }
258
259         cls = PyTuple_GET_ITEM(args, 0);
260         if (!(srna = pyrna_struct_as_srna(cls, false, "callback_remove"))) {
261                 return NULL;
262         }
263         py_handle = PyTuple_GET_ITEM(args, 1);
264         handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
265         if (handle == NULL) {
266                 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
267                 return NULL;
268         }
269
270         if (RNA_struct_is_a(srna, &RNA_Space)) {
271                 if (!PyArg_ParseTuple(args, "OO!s:Space.draw_handler_remove",
272                                       &cls, &PyCapsule_Type, &py_handle,  /* already assigned, no matter */
273                                       &cb_regiontype_str))
274                 {
275                         return NULL;
276                 }
277
278                 customdata = ED_region_draw_cb_customdata(handle);
279                 Py_DECREF((PyObject *)customdata);
280
281                 if (pyrna_enum_value_from_id(region_type_items, cb_regiontype_str, &cb_regiontype, "bpy_struct.callback_remove()") == -1) {
282                         return NULL;
283                 }
284                 else {
285                         const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
286                         if (spaceid == SPACE_EMPTY) {
287                                 PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
288                                 return NULL;
289                         }
290                         else {
291                                 SpaceType *st = BKE_spacetype_from_id(spaceid);
292                                 ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);
293
294                                 ED_region_draw_cb_exit(art, handle);
295                         }
296                 }
297         }
298         else {
299                 PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
300                 return NULL;
301         }
302
303         /* don't allow reuse */
304         PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);
305
306         Py_RETURN_NONE;
307 }