1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup pythonintern
*
* This file currently exposes callbacks for interface regions but may be
* extended later.
*/
#include <Python.h>
#include "RNA_types.h"
#include "BLI_utildefines.h"
#include "bpy_capi_utils.h"
#include "bpy_rna.h"
#include "bpy_rna_callback.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "RNA_access.h"
#include "RNA_enum_types.h"
#include "BKE_context.h"
#include "BKE_screen.h"
#include "WM_api.h"
#include "ED_space_api.h"
#include "../generic/python_utildefines.h"
/* Use this to stop other capsules from being mis-used. */
static const char *rna_capsual_id = "RNA_HANDLE";
static const char *rna_capsual_id_invalid = "RNA_HANDLE_REMOVED";
static const EnumPropertyItem region_draw_mode_items[] = {
{REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
{REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""},
{REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
{REGION_DRAW_BACKDROP, "BACKDROP", 0, "Backdrop", ""},
{0, NULL, 0, NULL, NULL},
};
static void cb_region_draw(const bContext *C, ARegion *UNUSED(region), void *customdata)
{
PyObject *cb_func, *cb_args, *result;
PyGILState_STATE gilstate;
bpy_context_set((bContext *)C, &gilstate);
cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
result = PyObject_CallObject(cb_func, cb_args);
if (result) {
Py_DECREF(result);
}
else {
PyErr_Print();
PyErr_Clear();
}
bpy_context_clear((bContext *)C, &gilstate);
}
/* We could make generic utility */
static PyObject *PyC_Tuple_CopySized(PyObject *src, int len_dst)
{
PyObject *dst = PyTuple_New(len_dst);
int len_src = PyTuple_GET_SIZE(src);
BLI_assert(len_src <= len_dst);
for (int i = 0; i < len_src; i++) {
PyObject *item = PyTuple_GET_ITEM(src, i);
PyTuple_SET_ITEM(dst, i, item);
Py_INCREF(item);
}
return dst;
}
static void cb_wm_cursor_draw(bContext *C, int x, int y, void *customdata)
{
PyObject *cb_func, *cb_args, *result;
PyGILState_STATE gilstate;
bpy_context_set((bContext *)C, &gilstate);
cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
const int cb_args_len = PyTuple_GET_SIZE(cb_args);
PyObject *cb_args_xy = PyTuple_New(2);
PyTuple_SET_ITEMS(cb_args_xy, PyLong_FromLong(x), PyLong_FromLong(y));
PyObject *cb_args_with_xy = PyC_Tuple_CopySized(cb_args, cb_args_len + 1);
PyTuple_SET_ITEM(cb_args_with_xy, cb_args_len, cb_args_xy);
result = PyObject_CallObject(cb_func, cb_args_with_xy);
Py_DECREF(cb_args_with_xy);
if (result) {
Py_DECREF(result);
}
else {
PyErr_Print();
PyErr_Clear();
}
bpy_context_clear((bContext *)C, &gilstate);
}
#if 0
PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
{
void *handle;
PyObject *cb_func, *cb_args;
char *cb_event_str = NULL;
int cb_event;
if (!PyArg_ParseTuple(args,
"OO!|s:bpy_struct.callback_add",
&cb_func,
&PyTuple_Type,
&cb_args,
&cb_event_str)) {
return NULL;
}
if (!PyCallable_Check(cb_func)) {
PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable");
return NULL;
}
if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
if (cb_event_str) {
if (pyrna_enum_value_from_id(
region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") ==
-1) {
return NULL;
}
}
else {
cb_event = REGION_DRAW_POST_PIXEL;
}
handle = ED_region_draw_cb_activate(
((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
Py_INCREF(args);
}
else {
PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
return NULL;
}
return PyCapsule_New((void *)handle, rna_capsual_id, NULL);
}
PyObject *pyrna_callback_remove(BPy_StructRNA *self, PyObject *args)
{
PyObject *py_handle;
void *handle;
void *customdata;
if (!PyArg_ParseTuple(args, "O!:callback_remove", &PyCapsule_Type, &py_handle)) {
return NULL;
}
handle = PyCapsule_GetPointer(py_handle, rna_capsual_id);
if (handle == NULL) {
PyErr_SetString(PyExc_ValueError,
"callback_remove(handle): NULL handle given, invalid or already removed");
return NULL;
}
if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
customdata = ED_region_draw_cb_customdata(handle);
Py_DECREF((PyObject *)customdata);
ED_region_draw_cb_exit(((ARegion *)self->ptr.data)->type, handle);
}
else {
PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
return NULL;
}
/* don't allow reuse */
PyCapsule_SetName(py_handle, rna_capsual_id_invalid);
Py_RETURN_NONE;
}
#endif
/* reverse of rna_Space_refine() */
static eSpace_Type rna_Space_refine_reverse(StructRNA *srna)
{
if (srna == &RNA_SpaceView3D) {
return SPACE_VIEW3D;
}
if (srna == &RNA_SpaceGraphEditor) {
return SPACE_GRAPH;
}
if (srna == &RNA_SpaceOutliner) {
return SPACE_OUTLINER;
}
if (srna == &RNA_SpaceProperties) {
return SPACE_PROPERTIES;
}
if (srna == &RNA_SpaceFileBrowser) {
return SPACE_FILE;
}
if (srna == &RNA_SpaceImageEditor) {
return SPACE_IMAGE;
}
if (srna == &RNA_SpaceInfo) {
return SPACE_INFO;
}
if (srna == &RNA_SpaceSequenceEditor) {
return SPACE_SEQ;
}
if (srna == &RNA_SpaceTextEditor) {
return SPACE_TEXT;
}
if (srna == &RNA_SpaceDopeSheetEditor) {
return SPACE_ACTION;
}
if (srna == &RNA_SpaceNLA) {
return SPACE_NLA;
}
if (srna == &RNA_SpaceNodeEditor) {
return SPACE_NODE;
}
if (srna == &RNA_SpaceConsole) {
return SPACE_CONSOLE;
}
if (srna == &RNA_SpacePreferences) {
return SPACE_USERPREF;
}
if (srna == &RNA_SpaceClipEditor) {
return SPACE_CLIP;
}
return SPACE_EMPTY;
}
PyObject *pyrna_callback_classmethod_add(PyObject *UNUSED(self), PyObject *args)
{
void *handle;
PyObject *cls;
PyObject *cb_func, *cb_args;
StructRNA *srna;
if (PyTuple_GET_SIZE(args) < 2) {
PyErr_SetString(PyExc_ValueError, "handler_add(handler): expected at least 2 args");
return NULL;
}
cls = PyTuple_GET_ITEM(args, 0);
if (!(srna = pyrna_struct_as_srna(cls, false, "handler_add"))) {
return NULL;
}
cb_func = PyTuple_GET_ITEM(args, 1);
if (!PyCallable_Check(cb_func)) {
PyErr_SetString(PyExc_TypeError, "first argument isn't callable");
return NULL;
}
/* class specific callbacks */
if (srna == &RNA_WindowManager) {
const char *error_prefix = "WindowManager.draw_cursor_add";
struct {
const char *space_type_str;
const char *region_type_str;
int space_type;
int region_type;
} params = {
.space_type_str = NULL,
.region_type_str = NULL,
.space_type = SPACE_TYPE_ANY,
.region_type = RGN_TYPE_ANY,
};
if (!PyArg_ParseTuple(args,
"OOO!|ss:WindowManager.draw_cursor_add",
&cls,
&cb_func, /* already assigned, no matter */
&PyTuple_Type,
&cb_args,
¶ms.space_type_str,
¶ms.region_type_str)) {
return NULL;
}
if (params.space_type_str && pyrna_enum_value_from_id(rna_enum_space_type_items,
params.space_type_str,
¶ms.space_type,
error_prefix) == -1) {
return NULL;
}
if (params.region_type_str && pyrna_enum_value_from_id(rna_enum_region_type_items,
params.region_type_str,
¶ms.region_type,
error_prefix) == -1) {
return NULL;
}
handle = WM_paint_cursor_activate(
params.space_type, params.region_type, NULL, cb_wm_cursor_draw, (void *)args);
}
else if (RNA_struct_is_a(srna, &RNA_Space)) {
const char *error_prefix = "Space.draw_handler_add";
struct {
const char *region_type_str;
const char *event_str;
int region_type;
int event;
} params;
if (!PyArg_ParseTuple(args,
"OOO!ss:Space.draw_handler_add",
&cls,
&cb_func, /* already assigned, no matter */
&PyTuple_Type,
&cb_args,
¶ms.region_type_str,
¶ms.event_str)) {
return NULL;
}
if (pyrna_enum_value_from_id(
region_draw_mode_items, params.event_str, ¶ms.event, error_prefix) == -1) {
return NULL;
}
if (pyrna_enum_value_from_id(rna_enum_region_type_items,
params.region_type_str,
¶ms.region_type,
error_prefix) == -1) {
return NULL;
}
const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
if (spaceid == SPACE_EMPTY) {
PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
return NULL;
}
SpaceType *st = BKE_spacetype_from_id(spaceid);
ARegionType *art = BKE_regiontype_from_id(st, params.region_type);
if (art == NULL) {
PyErr_Format(PyExc_TypeError, "region type '%.200s' not in space", params.region_type_str);
return NULL;
}
handle = ED_region_draw_cb_activate(art, cb_region_draw, (void *)args, params.event);
}
else {
PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
return NULL;
}
PyObject *ret = PyCapsule_New((void *)handle, rna_capsual_id, NULL);
/* Store 'args' in context as well as the handler custom-data,
* because the handle may be freed by Blender (new file, new window... etc) */
PyCapsule_SetContext(ret, args);
Py_INCREF(args);
return ret;
}
PyObject *pyrna_callback_classmethod_remove(PyObject *UNUSED(self), PyObject *args)
{
PyObject *cls;
PyObject *py_handle;
void *handle;
StructRNA *srna;
bool capsule_clear = false;
if (PyTuple_GET_SIZE(args) < 2) {
PyErr_SetString(PyExc_ValueError, "callback_remove(handler): expected at least 2 args");
return NULL;
}
cls = PyTuple_GET_ITEM(args, 0);
if (!(srna = pyrna_struct_as_srna(cls, false, "callback_remove"))) {
return NULL;
}
py_handle = PyTuple_GET_ITEM(args, 1);
handle = PyCapsule_GetPointer(py_handle, rna_capsual_id);
if (handle == NULL) {
PyErr_SetString(PyExc_ValueError,
"callback_remove(handler): NULL handler given, invalid or already removed");
return NULL;
}
PyObject *handle_args = PyCapsule_GetContext(py_handle);
if (srna == &RNA_WindowManager) {
if (!PyArg_ParseTuple(
args, "OO!:WindowManager.draw_cursor_remove", &cls, &PyCapsule_Type, &py_handle)) {
return NULL;
}
WM_paint_cursor_end(handle);
capsule_clear = true;
}
else if (RNA_struct_is_a(srna, &RNA_Space)) {
const char *error_prefix = "Space.draw_handler_remove";
struct {
const char *region_type_str;
int region_type;
} params;
if (!PyArg_ParseTuple(args,
"OO!s:Space.draw_handler_remove",
&cls,
&PyCapsule_Type,
&py_handle, /* already assigned, no matter */
¶ms.region_type_str)) {
return NULL;
}
if (pyrna_enum_value_from_id(rna_enum_region_type_items,
params.region_type_str,
¶ms.region_type,
error_prefix) == -1) {
return NULL;
}
const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
if (spaceid == SPACE_EMPTY) {
PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
return NULL;
}
SpaceType *st = BKE_spacetype_from_id(spaceid);
ARegionType *art = BKE_regiontype_from_id(st, params.region_type);
if (art == NULL) {
PyErr_Format(PyExc_TypeError, "region type '%.200s' not in space", params.region_type_str);
return NULL;
}
ED_region_draw_cb_exit(art, handle);
capsule_clear = true;
}
else {
PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
return NULL;
}
/* don't allow reuse */
if (capsule_clear) {
Py_DECREF(handle_args);
PyCapsule_SetName(py_handle, rna_capsual_id_invalid);
}
Py_RETURN_NONE;
}
|