2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-06-24 16:54:30 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
2019-09-05 15:52:38 +02:00
|
|
|
* \ingroup bke
|
2013-09-12 03:02:50 +00:00
|
|
|
*/
|
|
|
|
|
|
2011-06-24 16:54:30 +00:00
|
|
|
#include "BLI_listbase.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2019-09-05 15:52:38 +02:00
|
|
|
|
|
|
|
|
#include "BKE_callbacks.h"
|
2011-06-24 16:54:30 +00:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2019-09-09 10:25:04 +02:00
|
|
|
#include "RNA_access.h"
|
2022-03-14 16:54:46 +01:00
|
|
|
#include "RNA_prototypes.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "RNA_types.h"
|
2019-09-09 10:25:04 +02:00
|
|
|
|
2019-09-05 15:52:38 +02:00
|
|
|
static ListBase callback_slots[BKE_CB_EVT_TOT] = {{NULL}};
|
2011-06-24 16:54:30 +00:00
|
|
|
|
2021-10-25 12:07:37 +02:00
|
|
|
static bool callbacks_initialized = false;
|
|
|
|
|
|
|
|
|
|
#define ASSERT_CALLBACKS_INITIALIZED() \
|
|
|
|
|
BLI_assert_msg(callbacks_initialized, \
|
|
|
|
|
"Callbacks should be initialized with BKE_callback_global_init() before using " \
|
|
|
|
|
"the callback system.")
|
|
|
|
|
|
2019-09-09 10:25:04 +02:00
|
|
|
void BKE_callback_exec(struct Main *bmain,
|
|
|
|
|
struct PointerRNA **pointers,
|
|
|
|
|
const int num_pointers,
|
|
|
|
|
eCbEvent evt)
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
2021-10-25 12:07:37 +02:00
|
|
|
ASSERT_CALLBACKS_INITIALIZED();
|
|
|
|
|
|
2021-10-19 13:20:48 +11:00
|
|
|
/* Use mutable iteration so handlers are able to remove themselves. */
|
2012-05-12 15:13:06 +00:00
|
|
|
ListBase *lb = &callback_slots[evt];
|
2021-10-19 13:20:48 +11:00
|
|
|
LISTBASE_FOREACH_MUTABLE (bCallbackFuncStore *, funcstore, lb) {
|
2019-09-09 10:25:04 +02:00
|
|
|
funcstore->func(bmain, pointers, num_pointers, funcstore->arg);
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 10:25:04 +02:00
|
|
|
void BKE_callback_exec_null(struct Main *bmain, eCbEvent evt)
|
|
|
|
|
{
|
|
|
|
|
BKE_callback_exec(bmain, NULL, 0, evt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_callback_exec_id(struct Main *bmain, struct ID *id, eCbEvent evt)
|
|
|
|
|
{
|
|
|
|
|
PointerRNA id_ptr;
|
|
|
|
|
RNA_id_pointer_create(id, &id_ptr);
|
|
|
|
|
|
|
|
|
|
PointerRNA *pointers[1] = {&id_ptr};
|
|
|
|
|
|
|
|
|
|
BKE_callback_exec(bmain, pointers, 1, evt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_callback_exec_id_depsgraph(struct Main *bmain,
|
|
|
|
|
struct ID *id,
|
|
|
|
|
struct Depsgraph *depsgraph,
|
|
|
|
|
eCbEvent evt)
|
|
|
|
|
{
|
|
|
|
|
PointerRNA id_ptr;
|
|
|
|
|
RNA_id_pointer_create(id, &id_ptr);
|
|
|
|
|
|
|
|
|
|
PointerRNA depsgraph_ptr;
|
|
|
|
|
RNA_pointer_create(NULL, &RNA_Depsgraph, depsgraph, &depsgraph_ptr);
|
|
|
|
|
|
|
|
|
|
PointerRNA *pointers[2] = {&id_ptr, &depsgraph_ptr};
|
|
|
|
|
|
|
|
|
|
BKE_callback_exec(bmain, pointers, 2, evt);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 15:52:38 +02:00
|
|
|
void BKE_callback_add(bCallbackFuncStore *funcstore, eCbEvent evt)
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
2021-10-25 12:07:37 +02:00
|
|
|
ASSERT_CALLBACKS_INITIALIZED();
|
2012-05-12 15:13:06 +00:00
|
|
|
ListBase *lb = &callback_slots[evt];
|
2011-06-24 16:54:30 +00:00
|
|
|
BLI_addtail(lb, funcstore);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 16:34:13 +02:00
|
|
|
void BKE_callback_remove(bCallbackFuncStore *funcstore, eCbEvent evt)
|
|
|
|
|
{
|
2021-10-25 12:07:37 +02:00
|
|
|
/* The callback may have already been removed by BKE_callback_global_finalize(), for
|
2021-10-19 15:48:12 +02:00
|
|
|
* example when removing callbacks in response to a BKE_blender_atexit_register callback
|
|
|
|
|
* function. `BKE_blender_atexit()` runs after `BKE_callback_global_finalize()`. */
|
2021-10-25 12:07:37 +02:00
|
|
|
if (!callbacks_initialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ListBase *lb = &callback_slots[evt];
|
|
|
|
|
|
|
|
|
|
/* Be noisy about potential programming errors. */
|
|
|
|
|
BLI_assert_msg(BLI_findindex(lb, funcstore) != -1, "To-be-removed callback not found");
|
|
|
|
|
|
|
|
|
|
BLI_remlink(lb, funcstore);
|
2021-10-19 15:48:12 +02:00
|
|
|
|
2021-09-24 16:34:13 +02:00
|
|
|
if (funcstore->alloc) {
|
|
|
|
|
MEM_freeN(funcstore);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 15:52:38 +02:00
|
|
|
void BKE_callback_global_init(void)
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
2021-10-25 12:07:37 +02:00
|
|
|
callbacks_initialized = true;
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-05 15:52:38 +02:00
|
|
|
void BKE_callback_global_finalize(void)
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
|
|
|
|
eCbEvent evt;
|
2019-09-05 15:52:38 +02:00
|
|
|
for (evt = 0; evt < BKE_CB_EVT_TOT; evt++) {
|
2012-05-12 15:13:06 +00:00
|
|
|
ListBase *lb = &callback_slots[evt];
|
2011-06-24 16:54:30 +00:00
|
|
|
bCallbackFuncStore *funcstore;
|
|
|
|
|
bCallbackFuncStore *funcstore_next;
|
2014-08-29 16:16:28 +10:00
|
|
|
for (funcstore = lb->first; funcstore; funcstore = funcstore_next) {
|
|
|
|
|
funcstore_next = funcstore->next;
|
2021-09-24 16:34:13 +02:00
|
|
|
BKE_callback_remove(funcstore, evt);
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-25 12:07:37 +02:00
|
|
|
|
|
|
|
|
callbacks_initialized = false;
|
2011-06-24 16:54:30 +00:00
|
|
|
}
|