Committing patch "[#27676] Change window size/resolution in realtime" by me.
[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 "bpy_rna.h"
36 #include "bpy_rna_callback.h"
37 #include "bpy_util.h"
38
39 #include "BLI_utildefines.h"
40
41 #include "DNA_screen_types.h"
42
43 #include "RNA_access.h"
44
45 #include "BKE_context.h"
46 #include "ED_space_api.h"
47
48 /* use this to stop other capsules from being mis-used */
49 #define RNA_CAPSULE_ID "RNA_HANDLE"
50 #define RNA_CAPSULE_ID_INVALID "RNA_HANDLE_REMOVED"
51
52 static void cb_region_draw(const bContext *C, ARegion *UNUSED(ar), void *customdata)
53 {
54         PyObject *cb_func, *cb_args, *result;
55         PyGILState_STATE gilstate;
56
57         bpy_context_set((bContext *)C, &gilstate);
58
59         cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 0);
60         cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 1);
61         result = PyObject_CallObject(cb_func, cb_args);
62
63         if (result) {
64                 Py_DECREF(result);
65         }
66         else {
67                 PyErr_Print();
68                 PyErr_Clear();
69         }
70
71         bpy_context_clear((bContext *)C, &gilstate);
72 }
73
74 PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
75 {
76         void *handle;
77
78         PyObject *cb_func, *cb_args;
79         char *cb_event_str = NULL;
80         int cb_event;
81
82         if (!PyArg_ParseTuple(args, "OO!|s:bpy_struct.callback_add", &cb_func, &PyTuple_Type, &cb_args, &cb_event_str))
83                 return NULL;
84         
85         if (!PyCallable_Check(cb_func)) {
86                 PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable");
87                 return NULL;
88         }
89
90         if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
91                 if (cb_event_str) {
92                         static EnumPropertyItem region_draw_mode_items[] = {
93                                 {REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
94                                 {REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""},
95                                 {REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
96                                 {0, NULL, 0, NULL, NULL}};
97         
98                         if (pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") < 0) {
99                                 return NULL;
100                         }
101                 }
102                 else {
103                         cb_event = REGION_DRAW_POST_PIXEL;
104                 }
105
106                 handle = ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
107                 Py_INCREF(args);
108         }
109         else {
110                 PyErr_SetString(PyExc_TypeError, "callback_add(): type does not suppport callbacks");
111                 return NULL;
112         }
113
114         return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
115 }
116
117 PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
118 {
119         PyObject *py_handle;
120         void *handle;
121         void *customdata;
122
123         if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle))
124                 return NULL;
125
126         handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
127
128         if (handle == NULL) {
129                 PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
130                 return NULL;
131         }
132
133         if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
134                 customdata = ED_region_draw_cb_customdata(handle);
135                 Py_DECREF((PyObject *)customdata);
136
137                 ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
138         }
139
140         /* dont allow reuse */
141         PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);
142
143         Py_RETURN_NONE;
144 }