Blender V5.3
wm_message_bus_rna.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10
11#include "CLG_log.h"
12#include "MEM_guardedalloc.h"
13
14#include "DNA_ID.h"
15
16#include "BLI_ghash.hh"
17#include "BLI_listbase.hh"
18#include "BLI_string.hh"
19
20#include "WM_message.hh"
21#include "WM_types.hh"
23
24#include "RNA_access.hh"
25#include "RNA_path.hh"
26
27namespace blender {
28
29/* -------------------------------------------------------------------- */
32
34{
35 size_t y = size_t(key) >> sizeof(void *);
36 return uint(y);
37}
38
39static uint wm_msg_rna_gset_hash(const void *key_p)
40{
41 const wmMsgSubscribeKey_RNA *key = static_cast<const wmMsgSubscribeKey_RNA *>(key_p);
42 const wmMsgParams_RNA *params = &key->msg.params;
43 // printf("%s\n", RNA_struct_identifier(params->ptr.type));
44 uint k = void_hash_uint(params->ptr.type);
45 k ^= void_hash_uint(params->ptr.data);
46 k ^= void_hash_uint(params->ptr.owner_id);
47 k ^= void_hash_uint(params->prop);
48 return k;
49}
50static bool wm_msg_rna_gset_cmp(const void *key_a_p, const void *key_b_p)
51{
52 const wmMsgParams_RNA *params_a =
53 &(static_cast<const wmMsgSubscribeKey_RNA *>(key_a_p))->msg.params;
54 const wmMsgParams_RNA *params_b =
55 &(static_cast<const wmMsgSubscribeKey_RNA *>(key_b_p))->msg.params;
56 return !((params_a->ptr.type == params_b->ptr.type) &&
57 (params_a->ptr.owner_id == params_b->ptr.owner_id) &&
58 (params_a->ptr.data == params_b->ptr.data) && (params_a->prop == params_b->prop));
59}
60
61static void *wm_msg_rna_gset_key_duplicate(const void *key_p)
62{
63 const wmMsgSubscribeKey_RNA *key_src = static_cast<const wmMsgSubscribeKey_RNA *>(key_p);
64 return MEM_new<wmMsgSubscribeKey_RNA>(__func__, *key_src);
65}
66static void wm_msg_rna_gset_key_free(void *key_p)
67{
68 wmMsgSubscribeKey_RNA *key = static_cast<wmMsgSubscribeKey_RNA *>(key_p);
69 wmMsgSubscribeValueLink *msg_lnk_next;
70 for (wmMsgSubscribeValueLink *msg_lnk = key->head.values.first(); msg_lnk;
71 msg_lnk = msg_lnk_next)
72 {
73 msg_lnk_next = msg_lnk->next;
74 wm_msg_subscribe_value_free(&key->head, msg_lnk);
75 }
76 if (key->msg.params.data_path != nullptr) {
77 MEM_delete(key->msg.params.data_path);
78 }
79 MEM_delete(key);
80}
81
82static void wm_msg_rna_repr(FILE *stream, const wmMsgSubscribeKey *msg_key)
83{
84 const wmMsgSubscribeKey_RNA *m = reinterpret_cast<wmMsgSubscribeKey_RNA *>(
85 const_cast<wmMsgSubscribeKey *>(msg_key));
86 const char *none = "<none>";
87 fprintf(stream,
88 "<wmMsg_RNA %p, "
89 "id='%s', "
90 "%s.%s values_len=%d\n",
91 m,
92 m->msg.head.id,
94 m->msg.params.prop ?
96 none,
97 m->head.values.count());
98}
99
100static void wm_msg_rna_update_by_id(wmMsgBus *mbus, ID *id_src, ID *id_dst)
101{
102 GSet *gs = mbus->messages_gset[WM_MSG_TYPE_RNA];
103 GSetIterator gs_iter;
104 BLI_gsetIterator_init(&gs_iter, gs);
105 while (BLI_gsetIterator_done(&gs_iter) == false) {
106 wmMsgSubscribeKey_RNA *key = static_cast<wmMsgSubscribeKey_RNA *>(
107 BLI_gsetIterator_getKey(&gs_iter));
108 BLI_gsetIterator_step(&gs_iter);
109 if (key->msg.params.ptr.owner_id == id_src) {
110
111 /* GSet always needs updating since the key changes. */
112 BLI_gset_remove(gs, key, nullptr);
113
114 /* Remove any non-persistent values, so a single persistent
115 * value doesn't modify behavior for the rest. */
116 for (wmMsgSubscribeValueLink *msg_lnk = key->head.values.first(), *msg_lnk_next; msg_lnk;
117 msg_lnk = msg_lnk_next)
118 {
119 msg_lnk_next = msg_lnk->next;
120 if (msg_lnk->params.is_persistent == false) {
121 if (msg_lnk->params.tag) {
122 mbus->messages_tag_count -= 1;
123 }
124 wm_msg_subscribe_value_free(&key->head, msg_lnk);
125 }
126 }
127
128 bool remove = true;
129
130 if (key->head.values.is_empty()) {
131 /* Remove, no reason to keep. */
132 }
133 else if (key->msg.params.ptr.data == key->msg.params.ptr.owner_id) {
134 /* Simple, just update the ID. */
135 key->msg.params.ptr.data = id_dst;
136 key->msg.params.ptr.owner_id = id_dst;
137 remove = false;
138 }
139 else {
140 /* We need to resolve this from the new ID pointer. */
141 PointerRNA idptr = RNA_id_pointer_create(id_dst);
142 PointerRNA ptr;
143 PropertyRNA *prop = nullptr;
144 if (RNA_path_resolve(&idptr, key->msg.params.data_path, &ptr, &prop) &&
145 (prop == nullptr) == (key->msg.params.prop == nullptr))
146 {
147 key->msg.params.ptr = ptr;
148 key->msg.params.prop = prop;
149 remove = false;
150 }
151 }
152
153 if (remove) {
154 for (wmMsgSubscribeValueLink *msg_lnk = key->head.values.first(), *msg_lnk_next; msg_lnk;
155 msg_lnk = msg_lnk_next)
156 {
157 msg_lnk_next = msg_lnk->next;
158 if (msg_lnk->params.is_persistent == false) {
159 if (msg_lnk->params.tag) {
160 mbus->messages_tag_count -= 1;
161 }
162 wm_msg_subscribe_value_free(&key->head, msg_lnk);
163 }
164 }
165 /* Failed to persist, remove the key. */
166 BLI_remlink(&mbus->messages, key);
168 }
169 else {
170 /* Note that it's not impossible this key exists, however it is very unlikely
171 * since a subscriber would need to register in the middle of an undo for eg.
172 * so assert for now. */
173 BLI_assert(!BLI_gset_haskey(gs, key));
174 BLI_gset_add(gs, key);
175 }
176 }
177 }
178}
179
180static void wm_msg_rna_remove_by_id(wmMsgBus *mbus, const ID *id)
181{
182 GSet *gs = mbus->messages_gset[WM_MSG_TYPE_RNA];
183 GSetIterator gs_iter;
184 BLI_gsetIterator_init(&gs_iter, gs);
185 while (BLI_gsetIterator_done(&gs_iter) == false) {
186 wmMsgSubscribeKey_RNA *key = static_cast<wmMsgSubscribeKey_RNA *>(
187 BLI_gsetIterator_getKey(&gs_iter));
188 BLI_gsetIterator_step(&gs_iter);
189 if (key->msg.params.ptr.owner_id == id) {
190 /* Clear here so we can decrement 'messages_tag_count'. */
191 for (wmMsgSubscribeValueLink *msg_lnk = key->head.values.first(), *msg_lnk_next; msg_lnk;
192 msg_lnk = msg_lnk_next)
193 {
194 msg_lnk_next = msg_lnk->next;
195 if (msg_lnk->params.tag) {
196 mbus->messages_tag_count -= 1;
197 }
198 wm_msg_subscribe_value_free(&key->head, msg_lnk);
199 }
200
201 BLI_remlink(&mbus->messages, key);
202 BLI_gset_remove(gs, key, nullptr);
204 }
205 }
206}
207
209{
210 msgtype_info->gset.hash_fn = wm_msg_rna_gset_hash;
211 msgtype_info->gset.cmp_fn = wm_msg_rna_gset_cmp;
212 msgtype_info->gset.key_duplicate_fn = wm_msg_rna_gset_key_duplicate;
213 msgtype_info->gset.key_free_fn = wm_msg_rna_gset_key_free;
214
215 msgtype_info->repr = wm_msg_rna_repr;
218}
219
221
222/* -------------------------------------------------------------------- */
225
227{
228 wmMsgSubscribeKey_RNA key_test;
229 key_test.msg.params = *msg_key_params;
230 return static_cast<wmMsgSubscribeKey_RNA *>(
232}
233
234void WM_msg_publish_rna_params(wmMsgBus *mbus, const wmMsgParams_RNA *msg_key_params)
235{
237
238 const char *none = "<none>";
241 "rna(id='%s', %s.%s)",
242 msg_key_params->ptr.has_owner_id() ? ((ID *)msg_key_params->ptr.owner_id)->name : none,
243 msg_key_params->ptr.has_type() ? RNA_struct_identifier(msg_key_params->ptr.type) : none,
244 msg_key_params->prop ? RNA_property_identifier((PropertyRNA *)msg_key_params->prop) : none);
245
246 if ((key = WM_msg_lookup_rna(mbus, msg_key_params))) {
247 WM_msg_publish_with_key(mbus, &key->head);
248 }
249
250 /* Support anonymous subscribers, this may be some extra overhead
251 * but we want to be able to be more ambiguous. */
252 if (msg_key_params->ptr.has_owner_id() || msg_key_params->ptr.has_data()) {
253 wmMsgParams_RNA msg_key_params_anon = *msg_key_params;
254
255 /* We might want to enable this later? */
256 if (msg_key_params_anon.prop != nullptr) {
257 /* All properties for this type. */
258 msg_key_params_anon.prop = nullptr;
259 if ((key = WM_msg_lookup_rna(mbus, &msg_key_params_anon))) {
260 WM_msg_publish_with_key(mbus, &key->head);
261 }
262 msg_key_params_anon.prop = msg_key_params->prop;
263 }
264
265 msg_key_params_anon.ptr.owner_id = nullptr;
266 msg_key_params_anon.ptr.data = nullptr;
267 if ((key = WM_msg_lookup_rna(mbus, &msg_key_params_anon))) {
268 WM_msg_publish_with_key(mbus, &key->head);
269 }
270
271 /* Support subscribers to a type. */
272 if (msg_key_params->prop) {
273 msg_key_params_anon.prop = nullptr;
274 if ((key = WM_msg_lookup_rna(mbus, &msg_key_params_anon))) {
275 WM_msg_publish_with_key(mbus, &key->head);
276 }
277 }
278 }
279}
280
282{
283 wmMsgParams_RNA params{};
284 params.ptr = *ptr;
285 params.prop = prop;
286 WM_msg_publish_rna_params(mbus, &params);
287}
288
290 const wmMsgParams_RNA *msg_key_params,
291 const wmMsgSubscribeValue *msg_val_params,
292 const char *id_repr)
293{
294 wmMsgSubscribeKey_RNA msg_key_test{};
295
296 /* Use when added. */
297 msg_key_test.msg.head.id = id_repr;
298 msg_key_test.msg.head.type = WM_MSG_TYPE_RNA;
299 /* For lookup. */
300 msg_key_test.msg.params = *msg_key_params;
301
302 const char *none = "<none>";
305 "rna(id='%s', %s.%s, info='%s')",
306 msg_key_params->ptr.has_owner_id() ? ((ID *)msg_key_params->ptr.owner_id)->name : none,
307 msg_key_params->ptr.has_type() ? RNA_struct_identifier(msg_key_params->ptr.type) : none,
308 msg_key_params->prop ? RNA_property_identifier((PropertyRNA *)msg_key_params->prop) : none,
309 id_repr);
310
311 wmMsgSubscribeKey_RNA *msg_key = reinterpret_cast<wmMsgSubscribeKey_RNA *>(
312 WM_msg_subscribe_with_key(mbus, &msg_key_test.head, msg_val_params));
313
314 if (msg_val_params->is_persistent) {
315 if (msg_key->msg.params.data_path == nullptr) {
316 if (msg_key->msg.params.ptr.data != msg_key->msg.params.ptr.owner_id) {
317 /* We assume prop type can't change. */
318 const std::optional<std::string> str = RNA_path_from_ID_to_struct(
319 &msg_key->msg.params.ptr);
320 msg_key->msg.params.data_path = str ? BLI_strdupn(str->c_str(), str->size()) : nullptr;
321 }
322 }
323 }
324}
325
327 PointerRNA *ptr,
328 const PropertyRNA *prop,
329 const wmMsgSubscribeValue *msg_val_params,
330 const char *id_repr)
331{
332 wmMsgParams_RNA params{};
333 params.ptr = *ptr;
334 params.prop = prop;
335 WM_msg_subscribe_rna_params(mbus, &params, msg_val_params, id_repr);
336}
337
339
340/* -------------------------------------------------------------------------- */
345
347 ID *id,
348 const wmMsgSubscribeValue *msg_val_params,
349 const char *id_repr)
350{
351 wmMsgParams_RNA msg_key_params = {{}};
352 msg_key_params.ptr = RNA_id_pointer_create(id);
353 WM_msg_subscribe_rna_params(mbus, &msg_key_params, msg_val_params, id_repr);
354}
355
357{
358 wmMsgParams_RNA msg_key_params = {{}};
359 msg_key_params.ptr = RNA_id_pointer_create(id);
360 WM_msg_publish_rna_params(mbus, &msg_key_params);
361}
362
364
365} // namespace blender
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
ID and Library types, which are fundamental for SDNA.
Read Blender memory allocation and freeing.
#define BLI_INLINE
#define str(s)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
CLG_LogRef * WM_LOG_MSGBUS_SUB
unsigned int uint
Definition BKE_text.h:55
static uint wm_msg_rna_gset_hash(const void *key_p)
bool RNA_path_resolve(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:529
void * BLI_gset_lookup(const GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT
PointerRNA RNA_id_pointer_create(ID *id)
const char * RNA_property_identifier(const PropertyRNA *prop)
std::optional< std::string > RNA_path_from_ID_to_struct(const PointerRNA *ptr)
Definition rna_path.cc:1078
BLI_INLINE bool BLI_gsetIterator_done(const GSetIterator *gsi)
Definition BLI_ghash.hh:466
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void WM_msg_subscribe_rna_params(wmMsgBus *mbus, const wmMsgParams_RNA *msg_key_params, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
BLI_INLINE uint void_hash_uint(const void *key)
static void wm_msg_rna_remove_by_id(wmMsgBus *mbus, const ID *id)
wmMsgSubscribeKey * WM_msg_subscribe_with_key(wmMsgBus *mbus, const wmMsgSubscribeKey *msg_key_test, const wmMsgSubscribeValue *msg_val_params)
static void wm_msg_rna_update_by_id(wmMsgBus *mbus, ID *id_src, ID *id_dst)
static bool wm_msg_rna_gset_cmp(const void *key_a_p, const void *key_b_p)
void wm_msg_subscribe_value_free(wmMsgSubscribeKey *msg_key, wmMsgSubscribeValueLink *msg_lnk)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BLI_INLINE void BLI_gsetIterator_init(GSetIterator *gsi, GSet *gs)
Definition BLI_ghash.hh:450
void WM_msg_publish_with_key(wmMsgBus *mbus, wmMsgSubscribeKey *msg_key)
void WM_msg_publish_rna(wmMsgBus *mbus, PointerRNA *ptr, PropertyRNA *prop)
static void wm_msg_rna_repr(FILE *stream, const wmMsgSubscribeKey *msg_key)
const char * RNA_struct_identifier(const StructRNA *type)
void WM_msg_subscribe_rna(wmMsgBus *mbus, PointerRNA *ptr, const PropertyRNA *prop, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
void WM_msg_publish_ID(wmMsgBus *mbus, ID *id)
void WM_msg_subscribe_ID(wmMsgBus *mbus, ID *id, const wmMsgSubscribeValue *msg_val_params, const char *id_repr)
wmMsgSubscribeKey_RNA * WM_msg_lookup_rna(wmMsgBus *mbus, const wmMsgParams_RNA *msg_key_params)
bool BLI_gset_remove(GSet *gs, const void *key, GSetKeyFreeFP keyfreefp)
BLI_INLINE void BLI_gsetIterator_step(GSetIterator *gsi)
Definition BLI_ghash.hh:462
static void wm_msg_rna_gset_key_free(void *key_p)
bool BLI_gset_haskey(const GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT
CLG_LogRef * WM_LOG_MSGBUS_PUB
static void * wm_msg_rna_gset_key_duplicate(const void *key_p)
void WM_msg_publish_rna_params(wmMsgBus *mbus, const wmMsgParams_RNA *msg_key_params)
void WM_msgtypeinfo_init_rna(wmMsgTypeInfo *msgtype_info)
bool BLI_gset_add(GSet *gs, void *key)
Definition BLI_ghash.cc:974
BLI_INLINE void * BLI_gsetIterator_getKey(GSetIterator *gsi)
Definition BLI_ghash.hh:458
bool has_type() const
Definition RNA_types.hh:180
StructRNA * type
Definition RNA_types.hh:63
bool has_data() const
Definition RNA_types.hh:169
bool has_owner_id() const
Definition RNA_types.hh:186
ListBaseT< wmMsgSubscribeKey > messages
GSet * messages_gset[WM_MSG_TYPE_NUM]
const PropertyRNA * prop
ListBaseT< wmMsgSubscribeValueLink > values
void(*) repr(FILE *stream, const wmMsgSubscribeKey *msg_key)
void(*) remove_by_id(wmMsgBus *mbus, const ID *id)
struct blender::wmMsgTypeInfo::@222252052342264033027352010177057107113002371022 gset
void(*) update_by_id(wmMsgBus *mbus, ID *id_src, ID *id_dst)
wmMsgParams_RNA params
unsigned int type
const char * id