2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02: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"
|
|
|
|
|
|
2023-08-10 22:40:27 +02:00
|
|
|
#include "RNA_access.hh"
|
2022-03-14 16:54:46 +01:00
|
|
|
#include "RNA_prototypes.h"
|
2023-08-10 22:40:27 +02:00
|
|
|
#include "RNA_types.hh"
|
2019-09-09 10:25:04 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
static ListBase callback_slots[BKE_CB_EVT_TOT] = {{nullptr}};
|
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.")
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
void BKE_callback_exec(Main *bmain, 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
void BKE_callback_exec_null(Main *bmain, eCbEvent evt)
|
2019-09-09 10:25:04 +02:00
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
BKE_callback_exec(bmain, nullptr, 0, evt);
|
2019-09-09 10:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
void BKE_callback_exec_id(Main *bmain, ID *id, eCbEvent evt)
|
2019-09-09 10:25:04 +02:00
|
|
|
{
|
2023-09-06 00:48:50 +02:00
|
|
|
PointerRNA id_ptr = RNA_id_pointer_create(id);
|
2019-09-09 10:25:04 +02:00
|
|
|
|
|
|
|
|
PointerRNA *pointers[1] = {&id_ptr};
|
|
|
|
|
|
|
|
|
|
BKE_callback_exec(bmain, pointers, 1, evt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
void BKE_callback_exec_id_depsgraph(Main *bmain, ID *id, Depsgraph *depsgraph, eCbEvent evt)
|
2019-09-09 10:25:04 +02:00
|
|
|
{
|
2023-09-06 00:48:50 +02:00
|
|
|
PointerRNA id_ptr = RNA_id_pointer_create(id);
|
2019-09-09 10:25:04 +02:00
|
|
|
|
2023-09-06 00:48:50 +02:00
|
|
|
PointerRNA depsgraph_ptr = RNA_pointer_create(nullptr, &RNA_Depsgraph, depsgraph);
|
2019-09-09 10:25:04 +02:00
|
|
|
|
|
|
|
|
PointerRNA *pointers[2] = {&id_ptr, &depsgraph_ptr};
|
|
|
|
|
|
|
|
|
|
BKE_callback_exec(bmain, pointers, 2, evt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
void BKE_callback_exec_string(Main *bmain, eCbEvent evt, const char *str)
|
2023-03-09 10:46:49 +11:00
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
PrimitiveStringRNA data = {nullptr};
|
2023-03-09 10:46:49 +11:00
|
|
|
data.value = str;
|
2023-09-06 00:48:50 +02:00
|
|
|
PointerRNA str_ptr = RNA_pointer_create(nullptr, &RNA_PrimitiveString, &data);
|
2023-03-09 10:46:49 +11:00
|
|
|
|
|
|
|
|
PointerRNA *pointers[1] = {&str_ptr};
|
|
|
|
|
|
|
|
|
|
BKE_callback_exec(bmain, pointers, 1, 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
void BKE_callback_global_init()
|
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
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
void BKE_callback_global_finalize()
|
2011-06-24 16:54:30 +00:00
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
for (int evt_i = 0; evt_i < BKE_CB_EVT_TOT; evt_i++) {
|
|
|
|
|
const eCbEvent evt = eCbEvent(evt_i);
|
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;
|
2023-07-17 10:46:26 +02:00
|
|
|
for (funcstore = static_cast<bCallbackFuncStore *>(lb->first); funcstore;
|
|
|
|
|
funcstore = funcstore_next)
|
|
|
|
|
{
|
2014-08-29 16:16:28 +10:00
|
|
|
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
|
|
|
}
|