Blender  V2.93
callbacks.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BLI_listbase.h"
22 #include "BLI_utildefines.h"
23 
24 #include "BKE_callbacks.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "RNA_access.h"
29 #include "RNA_types.h"
30 
32 
33 void BKE_callback_exec(struct Main *bmain,
34  struct PointerRNA **pointers,
35  const int num_pointers,
36  eCbEvent evt)
37 {
38  ListBase *lb = &callback_slots[evt];
39  bCallbackFuncStore *funcstore;
40 
41  for (funcstore = lb->first; funcstore; funcstore = funcstore->next) {
42  funcstore->func(bmain, pointers, num_pointers, funcstore->arg);
43  }
44 }
45 
46 void BKE_callback_exec_null(struct Main *bmain, eCbEvent evt)
47 {
48  BKE_callback_exec(bmain, NULL, 0, evt);
49 }
50 
51 void BKE_callback_exec_id(struct Main *bmain, struct ID *id, eCbEvent evt)
52 {
53  PointerRNA id_ptr;
54  RNA_id_pointer_create(id, &id_ptr);
55 
56  PointerRNA *pointers[1] = {&id_ptr};
57 
58  BKE_callback_exec(bmain, pointers, 1, evt);
59 }
60 
62  struct ID *id,
63  struct Depsgraph *depsgraph,
64  eCbEvent evt)
65 {
66  PointerRNA id_ptr;
67  RNA_id_pointer_create(id, &id_ptr);
68 
69  PointerRNA depsgraph_ptr;
70  RNA_pointer_create(NULL, &RNA_Depsgraph, depsgraph, &depsgraph_ptr);
71 
72  PointerRNA *pointers[2] = {&id_ptr, &depsgraph_ptr};
73 
74  BKE_callback_exec(bmain, pointers, 2, evt);
75 }
76 
78 {
79  ListBase *lb = &callback_slots[evt];
80  BLI_addtail(lb, funcstore);
81 }
82 
84 {
85  /* do nothing */
86 }
87 
88 /* call on application exit */
90 {
91  eCbEvent evt;
92  for (evt = 0; evt < BKE_CB_EVT_TOT; evt++) {
93  ListBase *lb = &callback_slots[evt];
94  bCallbackFuncStore *funcstore;
95  bCallbackFuncStore *funcstore_next;
96  for (funcstore = lb->first; funcstore; funcstore = funcstore_next) {
97  funcstore_next = funcstore->next;
98  BLI_remlink(lb, funcstore);
99  if (funcstore->alloc) {
100  MEM_freeN(funcstore);
101  }
102  }
103  }
104 }
eCbEvent
Definition: BKE_callbacks.h:39
@ BKE_CB_EVT_TOT
Definition: BKE_callbacks.h:62
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
Read Guarded memory(de)allocation.
StructRNA RNA_Depsgraph
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
Definition: callbacks.c:77
void BKE_callback_exec_id_depsgraph(struct Main *bmain, struct ID *id, struct Depsgraph *depsgraph, eCbEvent evt)
Definition: callbacks.c:61
static ListBase callback_slots[BKE_CB_EVT_TOT]
Definition: callbacks.c:31
void BKE_callback_global_finalize(void)
Definition: callbacks.c:89
void BKE_callback_exec_null(struct Main *bmain, eCbEvent evt)
Definition: callbacks.c:46
void BKE_callback_exec_id(struct Main *bmain, struct ID *id, eCbEvent evt)
Definition: callbacks.c:51
void BKE_callback_exec(struct Main *bmain, struct PointerRNA **pointers, const int num_pointers, eCbEvent evt)
Definition: callbacks.c:33
void BKE_callback_global_init(void)
Definition: callbacks.c:83
const Depsgraph * depsgraph
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:122
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
void(* func)(struct Main *, struct PointerRNA **, const int num_pointers, void *arg)
Definition: BKE_callbacks.h:67
struct bCallbackFuncStore * next
Definition: BKE_callbacks.h:66