Blender V5.3
wm_keymap.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12#include <fmt/format.h>
13#include <fmt/ranges.h>
14
15#include "DNA_screen_types.h"
16#include "DNA_space_types.h"
17#include "DNA_userdef_types.h"
19#include "DNA_workspace_types.h"
20
21#include "CLG_log.h"
22#include "MEM_guardedalloc.h"
23
24#include "BLI_listbase.hh"
25#include "BLI_string.hh"
26#include "BLI_string_utf8.hh"
27#include "BLI_utildefines.hh"
28#include "BLI_vector.hh"
29
30#include "BLF_api.hh"
31
32#include "UI_interface.hh"
33
34#include "BKE_context.hh"
35#include "BKE_global.hh"
36#include "BKE_idprop.hh"
37#include "BKE_main.hh"
38#include "BKE_report.hh"
39#include "BKE_screen.hh"
40#include "BKE_workspace.hh"
41
42#include "BLT_translation.hh"
43
44#include "RNA_access.hh"
45#include "RNA_enum_types.hh"
46
47#include "WM_api.hh"
48#include "WM_keymap.hh"
49#include "WM_types.hh"
50#include "wm_event_system.hh"
51#include "wm_event_types.hh"
52
53#include "ED_screen.hh"
54
55namespace blender {
56
58 bool (*filter_fn)(const wmKeyMap *km, const wmKeyMapItem *kmi, void *user_data);
59 void *user_data;
60};
61
62/* -------------------------------------------------------------------- */
67
69{
70 wmKeyMapItem *kmin = MEM_dupalloc(kmi);
71
72 kmin->prev = kmin->next = nullptr;
73 kmin->flag &= ~KMI_UPDATE;
74
75 if (kmin->properties) {
76 kmin->ptr = MEM_new<PointerRNA>("UserKeyMapItemPtr");
78
79 /* Signal for no context, see #STRUCT_NO_CONTEXT_WITHOUT_OWNER_ID. */
80 kmin->ptr->owner_id = nullptr;
81
83 kmin->ptr->data = kmin->properties;
84 }
85 else {
86 kmin->properties = nullptr;
87 kmin->ptr = nullptr;
88 }
89
90 return kmin;
91}
92
94{
95 /* Not the `kmi` itself. */
96 if (kmi->ptr) {
98 MEM_delete(kmi->ptr);
99 kmi->ptr = nullptr;
100 kmi->properties = nullptr;
101 }
102 else if (kmi->properties) {
104 kmi->properties = nullptr;
105 }
106}
107
109{
110 IDProperty *properties = kmi->properties;
111 kmi->properties = nullptr;
112 if (kmi->ptr) {
113 kmi->ptr->data = nullptr;
114 }
116 kmi->properties = properties;
117}
118
120{
121 WM_operator_properties_alloc(&(kmi->ptr), &(kmi->properties), kmi->idname);
123
124 /* Signal for no context, see #STRUCT_NO_CONTEXT_WITHOUT_OWNER_ID. */
125 kmi->ptr->owner_id = nullptr;
126}
127
132static void wm_keymap_item_properties_update_ot(wmKeyMapItem *kmi, const bool keep_properties)
133{
134 if (kmi->idname[0] == 0) {
135 BLI_assert(kmi->ptr == nullptr);
136 return;
137 }
138
139 if (kmi->ptr == nullptr) {
141 }
142 else {
143 wmOperatorType *ot = WM_operatortype_find(kmi->idname, false);
144 if (ot) {
145 if (ot->srna != kmi->ptr->type) {
146 /* Matches #wm_keymap_item_properties_set but doesn't alloc new ptr. */
148 /* 'kmi->ptr->data' nullptr'd above, keep using existing properties.
149 * NOTE: the operators property types may have changed,
150 * we will need a more comprehensive sanitize function to support this properly.
151 */
152 if (kmi->properties) {
153 kmi->ptr->data = kmi->properties;
154 }
156
157 /* Signal for no context, see #STRUCT_NO_CONTEXT_WITHOUT_OWNER_ID. */
158 kmi->ptr->owner_id = nullptr;
159 }
160 }
161 else {
162 /* Zombie key-map item. */
163 if (keep_properties) {
165 }
166 else {
168 }
169 }
170 }
171}
172
173static void wm_keymap_item_properties_update_ot_from_list(ListBaseT<wmKeyMap> *km_lb,
174 const bool keep_properties)
175{
176 for (wmKeyMap &km : *km_lb) {
177 for (wmKeyMapItem &kmi : km.items) {
178 wm_keymap_item_properties_update_ot(&kmi, keep_properties);
179 }
180
181 for (wmKeyMapDiffItem &kmdi : km.diff_items) {
182 if (kmdi.add_item) {
183 wm_keymap_item_properties_update_ot(kmdi.add_item, keep_properties);
184 }
185 if (kmdi.remove_item) {
187 }
188 }
189 }
190}
191
193{
194 return (STREQ(a->idname, b->idname) &&
195 /* We do not really care about which Main we pass here, TBH. */
197 (a->flag & KMI_INACTIVE) == (b->flag & KMI_INACTIVE) && a->propvalue == b->propvalue);
198}
199
201{
202 return (wm_keymap_item_equals_result(a, b) && a->type == b->type && a->val == b->val &&
203 a->shift == b->shift && a->ctrl == b->ctrl && a->alt == b->alt && a->oskey == b->oskey &&
204 a->hyper == b->hyper && a->keymodifier == b->keymodifier && a->maptype == b->maptype &&
205 ((a->val != KM_PRESS_DRAG) || (a->direction == b->direction)) &&
206 ((ISKEYBOARD(a->type) == 0) ||
207 (a->flag & KMI_REPEAT_IGNORE) == (b->flag & KMI_REPEAT_IGNORE)));
208}
209
211{
212 if (kmi->ptr) [[likely]] {
214 MEM_delete(kmi->ptr);
215
216 kmi->ptr = nullptr;
217 }
218 else if (kmi->properties) {
220 }
221
222 kmi->properties = properties;
223
225}
226
228{
229 if (ISTIMER(kmi->type)) {
230 return KMI_TYPE_TIMER;
231 }
232 if (ISKEYBOARD(kmi->type)) {
233 return KMI_TYPE_KEYBOARD;
234 }
235 if (ISMOUSE(kmi->type)) {
236 return KMI_TYPE_MOUSE;
237 }
238 if (ISNDOF(kmi->type)) {
239 return KMI_TYPE_NDOF;
240 }
241 if (kmi->type == KM_TEXTINPUT) {
242 return KMI_TYPE_TEXTINPUT;
243 }
244 if (ELEM(kmi->type, TABLET_STYLUS, TABLET_ERASER)) {
245 return KMI_TYPE_MOUSE;
246 }
247 return KMI_TYPE_KEYBOARD;
248}
249
251
252/* -------------------------------------------------------------------- */
257
259{
260 wmKeyMapDiffItem *kmdin = MEM_dupalloc(kmdi);
261
262 kmdin->next = kmdin->prev = nullptr;
263 if (kmdi->add_item) {
264 kmdin->add_item = wm_keymap_item_copy(kmdi->add_item);
265 }
266 if (kmdi->remove_item) {
268 }
269
270 return kmdin;
271}
272
274{
275 if (kmdi->remove_item) {
277 MEM_delete(kmdi->remove_item);
278 }
279 if (kmdi->add_item) {
281 MEM_delete(kmdi->add_item);
282 }
283}
284
286
287/* -------------------------------------------------------------------- */
294
295wmKeyConfig *WM_keyconfig_new(wmWindowManager *wm, const char *idname, bool user_defined)
296{
297 BLI_assert(!BLI_findstring(&wm->runtime->keyconfigs, idname, offsetof(wmKeyConfig, idname)));
298 /* Create new configuration. */
299 wmKeyConfig *keyconf = MEM_new<wmKeyConfig>("wmKeyConfig");
300 STRNCPY_UTF8(keyconf->idname, idname);
301 BLI_addtail(&wm->runtime->keyconfigs, keyconf);
302
303 if (user_defined) {
304 keyconf->flag |= KEYCONF_USER;
305 }
306
307 return keyconf;
308}
309
310wmKeyConfig *WM_keyconfig_ensure(wmWindowManager *wm, const char *idname, bool user_defined)
311{
312 wmKeyConfig *keyconf = static_cast<wmKeyConfig *>(
313 BLI_findstring(&wm->runtime->keyconfigs, idname, offsetof(wmKeyConfig, idname)));
314 if (keyconf) {
315 if (keyconf == wm->runtime->defaultconf) {
316 /* For default configuration, we need to keep keymap
317 * modal items and poll functions intact. */
318 for (wmKeyMap &km : keyconf->keymaps) {
319 WM_keymap_clear(&km);
320 }
321 }
322 else {
323 /* For user defined key configuration, clear all keymaps. */
324 WM_keyconfig_clear(keyconf);
325 }
326
327 return keyconf;
328 }
329
330 return WM_keyconfig_new(wm, idname, user_defined);
331}
332
334{
335 BLI_assert(BLI_findindex(&wm->runtime->keyconfigs, keyconf) != -1);
336 if (STREQLEN(U.keyconfigstr, keyconf->idname, sizeof(U.keyconfigstr))) {
337 STRNCPY(U.keyconfigstr, wm->runtime->defaultconf->idname);
338 U.runtime.is_dirty = true;
339 WM_keyconfig_update_tag(nullptr, nullptr);
340 }
341
342 BLI_remlink(&wm->runtime->keyconfigs, keyconf);
343 WM_keyconfig_free(keyconf);
344
345 /* Clear pointers. */
346 wmKeyConfig **keyconf_arr_p[] = {WM_KEYCONFIG_ARRAY_P(wm)};
347 for (int i = 0; i < ARRAY_SIZE(keyconf_arr_p); i++) {
348 wmKeyConfig **kc_p = keyconf_arr_p[i];
349 if (*kc_p == keyconf) {
350 *kc_p = nullptr;
351 }
352 }
353}
354
356{
357 for (wmKeyMap &km : keyconf->keymaps.items_mutable()) {
358 WM_keymap_clear(&km);
359 MEM_delete(&km);
360 }
361
362 keyconf->keymaps.clear_no_delete();
363}
364
366{
367 WM_keyconfig_clear(keyconf);
368 MEM_delete(keyconf);
369}
370
372{
373 wmKeyConfig *keyconf;
374
375 /* First try from preset. */
376 keyconf = static_cast<wmKeyConfig *>(
377 BLI_findstring(&wm->runtime->keyconfigs, U.keyconfigstr, offsetof(wmKeyConfig, idname)));
378 if (keyconf) {
379 return keyconf;
380 }
381
382 /* Otherwise use default. */
383 return wm->runtime->defaultconf;
384}
385
387{
388 /* Setting a different key configuration as active:
389 * we ensure all is updated properly before and after making the change. */
390
392
393 STRNCPY(U.keyconfigstr, idname);
394 if (wm->init_flag & WM_INIT_FLAG_KEYCONFIG) {
395 U.runtime.is_dirty = true;
396 }
397
398 WM_keyconfig_update_tag(nullptr, nullptr);
400}
401
403
404/* -------------------------------------------------------------------- */
409
410static wmKeyMap *wm_keymap_new(const char *idname, int spaceid, int regionid)
411{
412 wmKeyMap *km = MEM_new<wmKeyMap>("keymap list");
413
414 STRNCPY_UTF8(km->idname, idname);
415 km->spaceid = spaceid;
416 km->regionid = regionid;
417
418 {
419 const char *owner_id = RNA_struct_state_owner_get();
420 if (owner_id) {
421 STRNCPY(km->owner_id, owner_id);
422 }
423 }
424 return km;
425}
426
427static void wm_keymap_copy_data(wmKeyMap *keymapn, wmKeyMap *keymap)
428{
429 /* Copy keymap data in place, preserving next/prev to keep its place in the list. */
430 wmKeyMap *prev = keymapn->prev;
431 wmKeyMap *next = keymapn->next;
432 *keymapn = *keymap;
433 keymapn->prev = prev;
434 keymapn->next = next;
435
436 keymapn->modal_items = keymap->modal_items;
437 keymapn->poll = keymap->poll;
438 keymapn->poll_modal_item = keymap->poll_modal_item;
439 keymapn->diff_items.clear_no_delete();
440 keymapn->items.clear_no_delete();
441 keymapn->flag &= ~(KEYMAP_UPDATE | KEYMAP_EXPANDED);
442
443 for (wmKeyMapDiffItem &kmdi : keymap->diff_items) {
445 BLI_addtail(&keymapn->items, kmdi_new);
446 }
447
448 for (wmKeyMapItem &kmi : keymap->items) {
449 wmKeyMapItem *kmi_new = wm_keymap_item_copy(&kmi);
450 BLI_addtail(&keymapn->items, kmi_new);
451 }
452}
453
455{
456 wmKeyMap *keymapn = MEM_new<wmKeyMap>(__func__);
457 wm_keymap_copy_data(keymapn, keymap);
458 return keymapn;
459}
460
462{
463 for (wmKeyMapDiffItem &kmdi : keymap->diff_items.items_mutable()) {
465 MEM_delete(&kmdi);
466 }
467
468 for (wmKeyMapItem &kmi : keymap->items.items_mutable()) {
470 MEM_delete(&kmi);
471 }
472
473 keymap->diff_items.clear_no_delete();
474 keymap->items.clear_no_delete();
475}
476
477void WM_keymap_remove(wmKeyConfig *keyconf, wmKeyMap *keymap)
478{
479 BLI_assert(BLI_findindex(&keyconf->keymaps, keymap) != -1);
480 WM_keymap_clear(keymap);
481 BLI_remlink(&keyconf->keymaps, keymap);
482 MEM_delete(keymap);
483}
484
486{
487 /* If we're tagged, only use compatible. */
488 if (keymap->owner_id[0] != '\0') {
489 const WorkSpace *workspace = CTX_wm_workspace(C);
490 if (BKE_workspace_owner_id_check(workspace, keymap->owner_id) == false) {
491 return false;
492 }
493 }
494
495 if (keymap->items.is_empty()) [[unlikely]] {
496 /* Empty key-maps may be missing more there may be a typo in the name.
497 * Warn early to avoid losing time investigating each case.
498 * When developing a customized Blender though you may want empty keymaps. */
499 if (!U.app_template[0] &&
500 /* Fallback key-maps may be intentionally empty, don't flood the output. */
501 !BLI_str_endswith(keymap->idname, " (fallback)") &&
502 /* This is an exception which may be empty.
503 * Longer term we might want a flag to indicate an empty key-map is intended. */
504 !STREQ(keymap->idname, "Node Tool: Tweak") &&
505 /* Another exception: Asset shelf keymap is meant for add-ons to use, it's empty by
506 * default. */
507 !STREQ(keymap->idname, "Asset Shelf") &&
508 /* Same for the asset browser keymap. */
509 !STREQ(keymap->idname, "Asset Browser Main"))
510 {
511 CLOG_WARN(WM_LOG_EVENTS, "empty keymap '%s'", keymap->idname);
512 }
513 }
514
515 if (keymap->poll != nullptr) {
516 return keymap->poll(C);
517 }
518 return true;
519}
520
521static bool wm_keymap_is_match(const wmKeyMap *km_a, const wmKeyMap *km_b)
522{
523 return ((km_a->spaceid == km_b->spaceid) && (km_a->regionid == km_b->regionid) &&
524 STREQ(km_a->idname, km_b->idname));
525}
526
527static void keymap_event_set(wmKeyMapItem *kmi, const KeyMapItem_Params *params)
528{
529 kmi->type = params->type;
530 kmi->val = params->value;
531 kmi->keymodifier = params->keymodifier;
532 kmi->direction = params->direction;
533
534 if (params->modifier == KM_ANY) {
535 kmi->shift = kmi->ctrl = kmi->alt = kmi->oskey = kmi->hyper = KM_ANY;
536 }
537 else {
538 const int8_t mod = params->modifier & 0xff;
539 const int8_t mod_any = KMI_PARAMS_MOD_FROM_ANY(params->modifier);
540
541 /* Only one of the flags should be set. */
542 BLI_assert((mod & mod_any) == 0);
543
544 kmi->shift = ((mod & KM_SHIFT) ? KM_MOD_HELD : ((mod_any & KM_SHIFT) ? KM_ANY : KM_NOTHING));
545 kmi->ctrl = ((mod & KM_CTRL) ? KM_MOD_HELD : ((mod_any & KM_CTRL) ? KM_ANY : KM_NOTHING));
546 kmi->alt = ((mod & KM_ALT) ? KM_MOD_HELD : ((mod_any & KM_ALT) ? KM_ANY : KM_NOTHING));
547 kmi->oskey = ((mod & KM_OSKEY) ? KM_MOD_HELD : ((mod_any & KM_OSKEY) ? KM_ANY : KM_NOTHING));
548 kmi->hyper = ((mod & KM_HYPER) ? KM_MOD_HELD : ((mod_any & KM_HYPER) ? KM_ANY : KM_NOTHING));
549 }
550}
551
552static void keymap_item_set_id(wmKeyMap *keymap, wmKeyMapItem *kmi)
553{
554 keymap->kmi_id++;
555 if ((keymap->flag & KEYMAP_USER) == 0) {
556 kmi->id = keymap->kmi_id;
557 }
558 else {
559 kmi->id = -keymap->kmi_id; /* User defined keymap entries have negative ids. */
560 }
561}
562
564 const char *idname,
565 const KeyMapItem_Params *params)
566{
567 wmKeyMapItem *kmi = MEM_new<wmKeyMapItem>("keymap entry");
568
569 BLI_addtail(&keymap->items, kmi);
570 STRNCPY(kmi->idname, idname);
571
572 keymap_event_set(kmi, params);
574
575 keymap_item_set_id(keymap, kmi);
576
577 WM_keyconfig_update_tag(keymap, kmi);
578
579 return kmi;
580}
581
583{
584 wmKeyMapItem *kmi_dst = wm_keymap_item_copy(kmi_src);
585
586 BLI_addtail(&keymap->items, kmi_dst);
587
588 keymap_item_set_id(keymap, kmi_dst);
589
590 WM_keyconfig_update_tag(keymap, kmi_dst);
591
592 return kmi_dst;
593}
594
596{
597 BLI_assert(BLI_findindex(&keymap->items, kmi) != -1);
598 if (kmi->ptr) {
600 MEM_delete(kmi->ptr);
601 }
602 else if (kmi->properties) {
604 }
605 BLI_freelinkN(&keymap->items, kmi);
606
607 WM_keyconfig_update_tag(keymap, nullptr);
608}
609
611
612/* -------------------------------------------------------------------- */
620
621static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
622{
623 for (wmKeyMapItem &kmi : addonmap->items) {
624 wmKeyMapItem *kmi_new = wm_keymap_item_copy(&kmi);
625 keymap_item_set_id(keymap, kmi_new);
626 BLI_addhead(&keymap->items, kmi_new);
627 }
628}
629
631{
632 for (wmKeyMapItem &kmi : km->items) {
633 if (wm_keymap_item_equals(&kmi, needle)) {
634 return &kmi;
635 }
636 }
637
638 return nullptr;
639}
640
642{
643 for (wmKeyMapItem &kmi : km->items) {
644 if (wm_keymap_item_equals_result(&kmi, needle)) {
645 return &kmi;
646 }
647 }
648
649 return nullptr;
650}
651
652static void wm_keymap_diff(
653 wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km, wmKeyMap *orig_km, wmKeyMap *addon_km)
654{
655 for (wmKeyMapItem &kmi : from_km->items) {
656 wmKeyMapItem *to_kmi = WM_keymap_item_find_id(to_km, kmi.id);
657
658 if (!to_kmi) {
659 /* Remove item. */
660 wmKeyMapDiffItem *kmdi = MEM_new<wmKeyMapDiffItem>("wmKeyMapDiffItem");
661 kmdi->remove_item = wm_keymap_item_copy(&kmi);
662 BLI_addtail(&diff_km->diff_items, kmdi);
663 }
664 else if (to_kmi && !wm_keymap_item_equals(&kmi, to_kmi)) {
665 /* Replace item. */
666 wmKeyMapDiffItem *kmdi = MEM_new<wmKeyMapDiffItem>("wmKeyMapDiffItem");
667 kmdi->remove_item = wm_keymap_item_copy(&kmi);
668 kmdi->add_item = wm_keymap_item_copy(to_kmi);
669 BLI_addtail(&diff_km->diff_items, kmdi);
670 }
671
672 /* Sync expanded flag back to original so we don't lose it on re-patch. */
673 if (to_kmi) {
674 wmKeyMapItem *orig_kmi = WM_keymap_item_find_id(orig_km, kmi.id);
675
676 if (!orig_kmi && addon_km) {
677 orig_kmi = wm_keymap_find_item_equals(addon_km, &kmi);
678 }
679
680 if (orig_kmi) {
681 orig_kmi->flag &= ~KMI_EXPANDED;
682 orig_kmi->flag |= (to_kmi->flag & KMI_EXPANDED);
683 }
684 }
685 }
686
687 for (wmKeyMapItem &kmi : to_km->items) {
688 if (kmi.id < 0) {
689 /* Add item. */
690 wmKeyMapDiffItem *kmdi = MEM_new<wmKeyMapDiffItem>("wmKeyMapDiffItem");
691 kmdi->add_item = wm_keymap_item_copy(&kmi);
692 BLI_addtail(&diff_km->diff_items, kmdi);
693 }
694 }
695}
696
697static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
698{
699 for (wmKeyMapDiffItem &kmdi : diff_km->diff_items) {
700 /* Find item to remove. */
701 wmKeyMapItem *kmi_remove = nullptr;
702 if (kmdi.remove_item) {
703 kmi_remove = wm_keymap_find_item_equals(km, kmdi.remove_item);
704 if (!kmi_remove) {
705 kmi_remove = wm_keymap_find_item_equals_result(km, kmdi.remove_item);
706 }
707 }
708
709 /* Add item. */
710 if (kmdi.add_item) {
711 /* Do not re-add an already existing keymap item! See #42088. */
712 /* We seek only for exact copy here! See #42137. */
714
715 /* If `kmi_add` is same as `kmi_remove` (can happen in some cases,
716 * typically when we got `kmi_remove` from #wm_keymap_find_item_equals_result()),
717 * no need to add or remove anything, see #45579. */
718
719 /* NOTE: This typically happens when we apply user-defined keymap diff to a base one that
720 * was exported with that customized keymap already. In that case:
721 *
722 * - `wm_keymap_find_item_equals(km, kmdi->remove_item)` finds nothing
723 * (because actual shortcut of current base does not match `kmdi->remove_item any more`).
724 * - `wm_keymap_find_item_equals_result(km, kmdi->remove_item)` finds the current `kmi` from
725 * base keymap (because it does exactly the same thing).
726 * - `wm_keymap_find_item_equals(km, kmdi->add_item)` finds the same `kmi`,
727 * since base keymap was exported with that user-defined shortcut already!
728 *
729 * Maybe we should rather keep user-defined keymaps specific to a given base one?
730 */
731 if (kmi_add != nullptr && kmi_add == kmi_remove) {
732 kmi_remove = nullptr;
733 }
734 /* Only if nothing to remove or item to remove found. */
735 else if (!kmi_add && (!kmdi.remove_item || kmi_remove)) {
736 kmi_add = wm_keymap_item_copy(kmdi.add_item);
737 kmi_add->flag |= KMI_USER_MODIFIED;
738
739 if (kmi_remove) {
740 kmi_add->flag &= ~KMI_EXPANDED;
741 kmi_add->flag |= (kmi_remove->flag & KMI_EXPANDED);
742 kmi_add->id = kmi_remove->id;
743 BLI_insertlinkbefore(&km->items, kmi_remove, kmi_add);
744 }
745 else {
746 keymap_item_set_id(km, kmi_add);
747 BLI_addtail(&km->items, kmi_add);
748 }
749 }
750 }
751
752 /* Remove item. */
753 if (kmi_remove) {
754 wm_keymap_item_free_data(kmi_remove);
755 BLI_freelinkN(&km->items, kmi_remove);
756 }
757 }
758}
759
760static wmKeyMap *wm_keymap_patch_update(ListBaseT<wmKeyMap> *lb,
761 wmKeyMap *defaultmap,
762 wmKeyMap *addonmap,
763 wmKeyMap *usermap)
764{
765 eKeyMap_Flag expanded = eKeyMap_Flag{};
766 Vector<short> expanded_item_ids;
767
768 /* Get previous keymap in list, we will update it in place to keep iterators valid. */
770 lb, defaultmap->idname, defaultmap->spaceid, defaultmap->regionid);
771 if (km) {
772 expanded = (km->flag & (KEYMAP_EXPANDED | KEYMAP_CHILDREN_EXPANDED));
773 for (wmKeyMapItem &kmi : km->items) {
774 if (kmi.flag & KMI_EXPANDED) {
775 expanded_item_ids.append(kmi.id);
776 }
777 }
778 WM_keymap_clear(km);
779 }
780 else {
781 km = MEM_new<wmKeyMap>(__func__);
782 BLI_addtail(lb, km);
783 }
784
785 /* Copy new keymap from an existing one. */
786 if (usermap && !(usermap->flag & KEYMAP_DIFF)) {
787 /* For compatibility with old user preferences with non-diff
788 * keymaps we override the original entirely. */
789 wm_keymap_copy_data(km, usermap);
790
791 /* Try to find corresponding id's for items. */
792 for (wmKeyMapItem &kmi : km->items) {
793 wmKeyMapItem *orig_kmi = wm_keymap_find_item_equals(defaultmap, &kmi);
794 if (!orig_kmi) {
795 orig_kmi = wm_keymap_find_item_equals_result(defaultmap, &kmi);
796 }
797
798 if (orig_kmi) {
799 kmi.id = orig_kmi->id;
800 }
801 else {
802 kmi.id = -(km->kmi_id++);
803 }
804 }
805
806 km->flag |= KEYMAP_UPDATE; /* Update again to create diff. */
807 }
808 else {
809 wm_keymap_copy_data(km, defaultmap);
810 }
811
812 /* Add addon keymap items. */
813 if (addonmap) {
814 wm_keymap_addon_add(km, addonmap);
815 }
816
817 /* Tag as being user edited. */
818 if (usermap) {
820 }
821 km->flag |= KEYMAP_USER | expanded;
822
823 /* Apply user changes of diff keymap. */
824 if (usermap && (usermap->flag & KEYMAP_DIFF)) {
825 wm_keymap_patch(km, usermap);
826 }
827
828 /* Rebuilding the keymap loses the expanded flag from items which aren't being edited,
829 * collapsing them shifts the UI layout, see: #162030. */
830 if (!expanded_item_ids.is_empty()) {
831 /* Items keep their order, so only the next ID needs to be checked. */
832 const short *id = expanded_item_ids.begin();
833 const short *id_end = expanded_item_ids.end();
834 for (wmKeyMapItem &kmi : km->items) {
835 if (kmi.id != *id) {
836 continue;
837 }
838 kmi.flag |= KMI_EXPANDED;
839 if (++id == id_end) {
840 break;
841 }
842 }
843 }
844
845 return km;
846}
847
848static void wm_keymap_diff_update(ListBaseT<wmKeyMap> *lb,
849 wmKeyMap *defaultmap,
850 wmKeyMap *addonmap,
851 wmKeyMap *km)
852{
853 /* Create temporary default + addon keymap for diff. */
854 wmKeyMap *origmap = defaultmap;
855
856 if (addonmap) {
857 defaultmap = wm_keymap_copy(defaultmap);
858 wm_keymap_addon_add(defaultmap, addonmap);
859 }
860
861 /* Remove previous diff keymap in list, we will replace it. */
862 wmKeyMap *prevmap = WM_keymap_list_find(lb, km->idname, km->spaceid, km->regionid);
863 if (prevmap) {
864 WM_keymap_clear(prevmap);
865 BLI_freelinkN(lb, prevmap);
866 }
867
868 /* Create diff keymap. */
869 wmKeyMap *diffmap = wm_keymap_new(km->idname, km->spaceid, km->regionid);
870 diffmap->flag |= KEYMAP_DIFF;
871 if (defaultmap->flag & KEYMAP_MODAL) {
872 diffmap->flag |= KEYMAP_MODAL;
873 }
874 wm_keymap_diff(diffmap, defaultmap, km, origmap, addonmap);
875
876 /* Add to list if not empty. */
877 if (diffmap->diff_items.first_) {
878 BLI_addtail(lb, diffmap);
879 }
880 else {
881 WM_keymap_clear(diffmap);
882 MEM_delete(diffmap);
883 }
884
885 /* Free temporary default map. */
886 if (addonmap) {
887 WM_keymap_clear(defaultmap);
888 MEM_delete(defaultmap);
889 }
890}
891
893
894/* -------------------------------------------------------------------- */
902
903wmKeyMap *WM_keymap_list_find(ListBaseT<wmKeyMap> *lb,
904 const char *idname,
905 int spaceid,
906 int regionid)
907{
908 for (wmKeyMap &km : *lb) {
909 if (km.spaceid == spaceid && km.regionid == regionid) {
910 if (STREQLEN(idname, km.idname, KMAP_MAX_NAME)) {
911 return &km;
912 }
913 }
914 }
915
916 return nullptr;
917}
918
920 const char *idname,
921 int spaceid,
922 int regionid)
923{
924 for (wmKeyMap &km : *lb) {
925 if (ELEM(km.spaceid, spaceid, SPACE_EMPTY) && km.regionid == regionid) {
926 if (STREQLEN(idname, km.idname, KMAP_MAX_NAME)) {
927 return &km;
928 }
929 }
930 }
931
932 return nullptr;
933}
934
935wmKeyMap *WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
936{
937 wmKeyMap *km = WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
938
939 if (km == nullptr) {
940 km = wm_keymap_new(idname, spaceid, regionid);
941 BLI_addtail(&keyconf->keymaps, km);
942
943 WM_keyconfig_update_tag(km, nullptr);
944 }
945
946 return km;
947}
948
949wmKeyMap *WM_keymap_find_all(wmWindowManager *wm, const char *idname, int spaceid, int regionid)
950{
951 return WM_keymap_list_find(&wm->runtime->userconf->keymaps, idname, spaceid, regionid);
952}
953
955 const char *idname,
956 int spaceid,
957 int regionid)
958{
960 &wm->runtime->userconf->keymaps, idname, spaceid, regionid);
961}
962
964
965/* -------------------------------------------------------------------- */
971
973 const char *idname,
974 const EnumPropertyItem *items)
975{
976 wmKeyMap *km = WM_keymap_ensure(keyconf, idname, SPACE_EMPTY, RGN_TYPE_WINDOW);
977 km->flag |= KEYMAP_MODAL;
978
979 /* Initialize modal items from default configuration. */
980 wmWindowManager *wm = G_MAIN->wm.first();
981 if (wm->runtime->defaultconf && wm->runtime->defaultconf != keyconf) {
982 wmKeyMap *defaultkm = WM_keymap_list_find(
983 &wm->runtime->defaultconf->keymaps, km->idname, SPACE_EMPTY, RGN_TYPE_WINDOW);
984
985 if (defaultkm) {
986 km->modal_items = defaultkm->modal_items;
987 km->poll = defaultkm->poll;
988 km->poll_modal_item = defaultkm->poll_modal_item;
989 }
990 }
991
992 if (items) {
993 km->modal_items = items;
994 }
995
996 return km;
997}
998
999wmKeyMap *WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
1000{
1001 for (wmKeyMap &km : keyconf->keymaps) {
1002 if (km.flag & KEYMAP_MODAL) {
1003 if (STREQLEN(idname, km.idname, KMAP_MAX_NAME)) {
1004 return &km;
1005 }
1006 }
1007 }
1008
1009 return nullptr;
1010}
1011
1013{
1014 wmKeyMapItem *kmi = MEM_new<wmKeyMapItem>("keymap entry");
1015
1016 BLI_addtail(&km->items, kmi);
1017 kmi->propvalue = value;
1018
1019 keymap_event_set(kmi, params);
1020
1021 keymap_item_set_id(km, kmi);
1022
1023 WM_keyconfig_update_tag(km, kmi);
1024
1025 return kmi;
1026}
1027
1029 const KeyMapItem_Params *params,
1030 const char *value)
1031{
1032 wmKeyMapItem *kmi = MEM_new<wmKeyMapItem>("keymap entry");
1033
1034 BLI_addtail(&km->items, kmi);
1035 STRNCPY(kmi->propvalue_str, value);
1036
1037 keymap_event_set(kmi, params);
1038
1039 keymap_item_set_id(km, kmi);
1040
1041 WM_keyconfig_update_tag(km, kmi);
1042
1043 return kmi;
1044}
1045
1047 const wmKeyMapItem *kmi,
1048 const int propvalue)
1049{
1050 if (km->flag & KEYMAP_MODAL) {
1051 kmi = kmi ? kmi->next : km->items.first();
1052 for (; kmi; kmi = kmi->next) {
1053 if (kmi->propvalue == propvalue) {
1054 return kmi;
1055 }
1056 }
1057 }
1058 else {
1059 BLI_assert_msg(0, "called with non modal keymap");
1060 }
1061
1062 return nullptr;
1063}
1064
1065const wmKeyMapItem *WM_modalkeymap_find_propvalue(const wmKeyMap *km, const int propvalue)
1066{
1067 return wm_modalkeymap_find_propvalue_iter(km, nullptr, propvalue);
1068}
1069
1070void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
1071{
1072 wmOperatorType *ot = WM_operatortype_find(opname, false);
1073
1074 if (ot) {
1075 ot->modalkeymap = km;
1076 }
1077 else {
1078 CLOG_ERROR(WM_LOG_OPERATORS, "unknown operator '%s' in modal keymap", opname);
1079 }
1080}
1081
1083{
1084 /* Here we convert propvalue string values delayed, due to python keymaps
1085 * being created before the actual modal keymaps, so no modal_items. */
1086
1087 if (km && (km->flag & KEYMAP_MODAL) && !km->modal_items) {
1088 if (wm->runtime->defaultconf == nullptr) {
1089 return;
1090 }
1091
1092 wmKeyMap *defaultkm = WM_keymap_list_find(
1093 &wm->runtime->defaultconf->keymaps, km->idname, SPACE_EMPTY, RGN_TYPE_WINDOW);
1094 if (!defaultkm) {
1095 return;
1096 }
1097
1098 km->modal_items = defaultkm->modal_items;
1099 km->poll = defaultkm->poll;
1100 km->poll_modal_item = defaultkm->poll_modal_item;
1101
1102 if (km->modal_items) {
1103 for (wmKeyMapItem &kmi : km->items) {
1104 if (kmi.propvalue_str[0]) {
1105 int propvalue;
1106 if (RNA_enum_value_from_id(static_cast<const EnumPropertyItem *>(km->modal_items),
1107 kmi.propvalue_str,
1108 &propvalue))
1109 {
1110 kmi.propvalue = propvalue;
1111 }
1112 kmi.propvalue_str[0] = '\0';
1113 }
1114 }
1115 }
1116 }
1117}
1118
1120
1121/* -------------------------------------------------------------------- */
1124
1125static const char *key_event_glyph_or_text(const int font_id,
1126 const char *text,
1127 const char *single_glyph)
1128{
1129 BLI_assert(single_glyph == nullptr || (BLI_strlen_utf8(single_glyph) == 1));
1130 return (single_glyph && BLF_has_glyph(font_id, BLI_str_utf8_as_unicode_or_error(single_glyph))) ?
1131 single_glyph :
1132 text;
1133}
1134
1135const char *WM_key_event_string(const short type, const bool compact)
1136{
1137 if (compact) {
1138 /* String storing a single unicode character or nullptr. */
1139 const char *single_glyph = nullptr;
1140 int font_id = BLF_default();
1141 const enum {
1142 UNIX,
1143 MACOS,
1144 MSWIN,
1145 } platform =
1146
1147#if defined(__APPLE__)
1148 MACOS
1149#elif defined(_WIN32)
1150 MSWIN
1151#else
1152 UNIX
1153#endif
1154 ;
1155
1156 switch (type) {
1157 case EVT_LEFTSHIFTKEY:
1158 case EVT_RIGHTSHIFTKEY: {
1159 if (platform == MACOS) {
1160 single_glyph = BLI_STR_UTF8_UPWARDS_WHITE_ARROW;
1161 }
1163 font_id, CTX_IFACE_(BLT_I18NCONTEXT_ID_WINDOWMANAGER, "Shift"), single_glyph);
1164 }
1165 case EVT_LEFTCTRLKEY:
1166 case EVT_RIGHTCTRLKEY:
1167 if (platform == MACOS) {
1169 }
1170 return IFACE_("Ctrl");
1171 case EVT_LEFTALTKEY:
1172 case EVT_RIGHTALTKEY: {
1173 if (platform == MACOS) {
1174 /* Option symbol on Mac keyboard. */
1175 single_glyph = BLI_STR_UTF8_OPTION_KEY;
1176 }
1177 return key_event_glyph_or_text(font_id, IFACE_("Alt"), single_glyph);
1178 }
1179 case EVT_OSKEY: {
1180 /* Keep these labels in sync with: `scripts/modules/rna_keymap_ui.py`. */
1181 if (platform == MACOS) {
1184 }
1185 if (platform == MSWIN) {
1188 }
1189 return IFACE_("OS");
1190 }
1191 case EVT_TABKEY:
1194 case EVT_BACKSPACEKEY:
1196 case EVT_ESCKEY:
1197 if (platform == MACOS) {
1199 }
1200 return key_event_glyph_or_text(font_id, IFACE_("Esc"), single_glyph);
1201 case EVT_RETKEY:
1203 case EVT_SPACEKEY:
1206 case EVT_LEFTARROWKEY:
1208 case EVT_UPARROWKEY:
1210 case EVT_RIGHTARROWKEY:
1212 case EVT_DOWNARROWKEY:
1214 }
1215 }
1216
1217 const EnumPropertyItem *it;
1218 const int i = RNA_enum_from_value(rna_enum_event_type_items, int(type));
1219
1220 if (i == -1) {
1221 return "";
1222 }
1224
1225 /* We first try enum items' description (abused as short-name here),
1226 * and fall back to usual name if empty. */
1227 if (compact && it->description[0]) {
1228 /* XXX No context for enum descriptions... In practice shall not be an issue though. */
1229 return IFACE_(it->description);
1230 }
1231
1233}
1234
1235std::optional<std::string> WM_keymap_item_raw_to_string(const int8_t shift,
1236 const int8_t ctrl,
1237 const int8_t alt,
1238 const int8_t oskey,
1239 const int8_t hyper,
1240 const short keymodifier,
1241 const short val,
1242 const short type,
1243 const bool compact)
1244{
1245 /* TODO: also support (some) value, like e.g. double-click? */
1246 Vector<std::string_view, 12> result_array;
1247
1248 const char *space = " ";
1249
1250 /* When a modifier is #KM_ANY, it isn't shown as this would end up being overly verbose. */
1251
1252 if (shift == KM_MOD_HELD) {
1253 result_array.append(WM_key_event_string(EVT_LEFTSHIFTKEY, true));
1254 result_array.append(space);
1255 }
1256 if (ctrl == KM_MOD_HELD) {
1257 result_array.append(WM_key_event_string(EVT_LEFTCTRLKEY, true));
1258 result_array.append(space);
1259 }
1260 if (alt == KM_MOD_HELD) {
1261 result_array.append(WM_key_event_string(EVT_LEFTALTKEY, true));
1262 result_array.append(space);
1263 }
1264 if (oskey == KM_MOD_HELD) {
1265 result_array.append(WM_key_event_string(EVT_OSKEY, true));
1266 result_array.append(space);
1267 }
1268 if (hyper == KM_MOD_HELD) {
1269 result_array.append(WM_key_event_string(EVT_HYPER, true));
1270 result_array.append(space);
1271 }
1272
1273 if (keymodifier) {
1274 result_array.append(WM_key_event_string(keymodifier, compact));
1275 result_array.append(space);
1276 }
1277
1278 if (type) {
1279 if (val == KM_DBL_CLICK) {
1280 result_array.append(IFACE_("dbl-"));
1281 }
1282 else if (val == KM_PRESS_DRAG) {
1283 result_array.append(IFACE_("drag-"));
1284 }
1285 result_array.append(WM_key_event_string(type, compact));
1286 }
1287
1288 if (!result_array.is_empty() && (result_array.last() == space)) {
1289 result_array.remove_last();
1290 }
1291
1292 return fmt::to_string(fmt::join(result_array, ""));
1293}
1294
1295std::optional<std::string> WM_keymap_item_to_string(const wmKeyMapItem *kmi, const bool compact)
1296{
1298 kmi->ctrl,
1299 kmi->alt,
1300 kmi->oskey,
1301 kmi->hyper,
1302 kmi->keymodifier,
1303 kmi->val,
1304 kmi->type,
1305 compact);
1306}
1307
1308std::optional<std::string> WM_modalkeymap_items_to_string(const wmKeyMap *km,
1309 const int propvalue,
1310 const bool compact)
1311{
1312 const wmKeyMapItem *kmi;
1313 if (km == nullptr || (kmi = WM_modalkeymap_find_propvalue(km, propvalue)) == nullptr) {
1314 return std::nullopt;
1315 }
1316
1317 std::string result;
1318 do {
1319 result += WM_keymap_item_to_string(kmi, compact).value_or("");
1320
1321 if ((kmi = wm_modalkeymap_find_propvalue_iter(km, kmi, propvalue)) == nullptr) {
1322 break;
1323 }
1324 result += '/';
1325 } while (true);
1326
1327 return result;
1328}
1329
1331 const int propvalue,
1332 const bool compact)
1333{
1334 wmWindowManager *wm = G_MAIN->wm.first();
1335 wmKeyMap *keymap = WM_keymap_active(wm, ot->modalkeymap);
1336 return WM_modalkeymap_items_to_string(keymap, propvalue, compact);
1337}
1338
1340
1341/* -------------------------------------------------------------------- */
1344
1346 const char *opname,
1347 const IDProperty *properties,
1348 const bool is_strict,
1349 const wmKeyMapItemFind_Params *params)
1350{
1351 for (wmKeyMapItem &kmi : keymap->items) {
1352 /* Skip disabled keymap items, see: #38447. */
1353 if (kmi.flag & KMI_INACTIVE) {
1354 continue;
1355 }
1356 if (!STREQ(kmi.idname, opname)) {
1357 continue;
1358 }
1359
1360 bool kmi_match = false;
1361 if (properties) {
1362/* Example of debugging keymaps. */
1363#if 0
1364 if (kmi->ptr) {
1365 if (STREQ("MESH_OT_rip_move", opname)) {
1366 printf("OPERATOR\n");
1367 IDP_print(properties);
1368 printf("KEYMAP\n");
1369 IDP_print(kmi->ptr->data);
1370 }
1371 }
1372#endif
1373
1374 if (kmi.ptr && IDP_EqualsProperties_ex(
1375 properties, static_cast<const IDProperty *>(kmi.ptr->data), is_strict))
1376 {
1377 kmi_match = true;
1378 }
1379 /* Debug only, helps spotting mismatches between menu entries and shortcuts! */
1380 else if (G.debug & G_DEBUG_WM) {
1381 if (is_strict && kmi.ptr) {
1382 wmOperatorType *ot = WM_operatortype_find(opname, true);
1383 if (ot) {
1384 /* Make a copy of the properties and set unset ones to their default values. */
1385 IDProperty *properties_default = IDP_CopyProperty(
1386 static_cast<const IDProperty *>(kmi.ptr->data));
1387
1388 PointerRNA opptr = RNA_pointer_create_discrete(nullptr, ot->srna, properties_default);
1389 WM_operator_properties_default(&opptr, true);
1390
1391 if (IDP_EqualsProperties_ex(properties, properties_default, is_strict)) {
1392 std::string kmi_str = WM_keymap_item_to_string(&kmi, false).value_or("");
1393 /* NOTE: given properties could come from other things than menu entry. */
1394 printf(
1395 "%s: Some set values in menu entry match default op values, "
1396 "this might not be desired!\n",
1397 opname);
1398 printf("\tkm: '%s', kmi: '%s'\n", keymap->idname, kmi_str.c_str());
1399#ifndef NDEBUG
1400# ifdef WITH_PYTHON
1401 printf("OPERATOR\n");
1402 IDP_print(properties);
1403 printf("KEYMAP\n");
1404 IDP_print(static_cast<IDProperty *>(kmi.ptr->data));
1405# endif
1406#endif
1407 printf("\n");
1408 }
1409
1410 IDP_FreeProperty(properties_default);
1411 }
1412 }
1413 }
1414 }
1415 else {
1416 kmi_match = true;
1417 }
1418
1419 if (kmi_match) {
1420 if ((params == nullptr) || params->filter_fn(keymap, &kmi, params->user_data)) {
1421 return &kmi;
1422 }
1423 }
1424 }
1425 return nullptr;
1426}
1427
1430 wmWindow *win,
1431 ListBaseT<wmEventHandler> *handlers,
1432 const char *opname,
1433 wm::OpCallContext /*opcontext*/,
1434 const IDProperty *properties,
1435 const bool is_strict,
1436 const wmKeyMapItemFind_Params *params,
1437 wmKeyMap **r_keymap)
1438{
1439 /* Find keymap item in handlers. */
1440 for (wmEventHandler &handler_base : *handlers) {
1441 if (handler_base.type == WM_HANDLER_TYPE_KEYMAP) {
1442 wmEventHandler_Keymap *handler = reinterpret_cast<wmEventHandler_Keymap *>(&handler_base);
1444 WM_event_get_keymaps_from_handler(wm, win, handler, &km_result);
1445 for (int km_index = 0; km_index < km_result.keymaps_len; km_index++) {
1446 wmKeyMap *keymap = km_result.keymaps[km_index];
1447 if (WM_keymap_poll(const_cast<bContext *>(C), keymap)) {
1449 keymap, opname, properties, is_strict, params);
1450 if (kmi != nullptr) {
1451 if (r_keymap) {
1452 *r_keymap = keymap;
1453 }
1454 return kmi;
1455 }
1456 }
1457 }
1458 }
1459 }
1460 /* Ensure un-initialized keymap is never used. */
1461 if (r_keymap) {
1462 *r_keymap = nullptr;
1463 }
1464 return nullptr;
1465}
1466
1468 const char *opname,
1469 wm::OpCallContext opcontext,
1470 const IDProperty *properties,
1471 const bool is_strict,
1472 const wmKeyMapItemFind_Params *params,
1473 wmKeyMap **r_keymap)
1474{
1476 wmWindow *win = CTX_wm_window(C);
1477 ScrArea *area = CTX_wm_area(C);
1478 ARegion *region = CTX_wm_region(C);
1479 wmKeyMapItem *found = nullptr;
1480
1481 /* Look into multiple handler lists to find the item. */
1482 if (win) {
1484 wm,
1485 win,
1486 &win->runtime->modalhandlers,
1487 opname,
1488 opcontext,
1489 properties,
1490 is_strict,
1491 params,
1492 r_keymap);
1493 if (found == nullptr) {
1495 wm,
1496 win,
1497 &win->runtime->handlers,
1498 opname,
1499 opcontext,
1500 properties,
1501 is_strict,
1502 params,
1503 r_keymap);
1504 }
1505 }
1506
1507 if (area && found == nullptr) {
1509 C, wm, win, &area->handlers, opname, opcontext, properties, is_strict, params, r_keymap);
1510 }
1511
1512 if (found == nullptr) {
1514 if (area) {
1515 if (!(region && region->regiontype == RGN_TYPE_WINDOW)) {
1517 }
1518
1519 if (region) {
1521 wm,
1522 win,
1523 &region->runtime->handlers,
1524 opname,
1525 opcontext,
1526 properties,
1527 is_strict,
1528 params,
1529 r_keymap);
1530 }
1531 }
1532 }
1533 else if (ELEM(opcontext,
1536 {
1537 if (!(region && region->regiontype == RGN_TYPE_CHANNELS)) {
1539 }
1540
1541 if (region) {
1543 wm,
1544 win,
1545 &region->runtime->handlers,
1546 opname,
1547 opcontext,
1548 properties,
1549 is_strict,
1550 params,
1551 r_keymap);
1552 }
1553 }
1554 else if (ELEM(opcontext,
1557 {
1558 if (!(region && region->regiontype == RGN_TYPE_PREVIEW)) {
1560 }
1561
1562 if (region) {
1564 wm,
1565 win,
1566 &region->runtime->handlers,
1567 opname,
1568 opcontext,
1569 properties,
1570 is_strict,
1571 params,
1572 r_keymap);
1573 }
1574 }
1575 else {
1576 if (region) {
1578 wm,
1579 win,
1580 &region->runtime->handlers,
1581 opname,
1582 opcontext,
1583 properties,
1584 is_strict,
1585 params,
1586 r_keymap);
1587 }
1588 }
1589 }
1590
1591 return found;
1592}
1593
1595 const char *opname,
1596 wm::OpCallContext opcontext,
1597 IDProperty *properties,
1598 bool is_strict,
1599 const wmKeyMapItemFind_Params *params,
1600 wmKeyMap **r_keymap)
1601{
1602 /* XXX Hack! Macro operators in menu entry have their whole props defined,
1603 * which is not the case for relevant keymap entries.
1604 * Could be good to check and harmonize this,
1605 * but for now always compare non-strict in this case. */
1606 wmOperatorType *ot = WM_operatortype_find(opname, true);
1607 if (ot) {
1608 is_strict = is_strict && ((ot->flag & OPTYPE_MACRO) == 0);
1609 }
1610
1612 C, opname, opcontext, properties, is_strict, params, r_keymap);
1613
1614 /* This block is *only* useful in one case: when op uses an enum menu in its prop member
1615 * (then, we want to rerun a comparison with that 'prop' unset). Note this remains brittle,
1616 * since now any enum prop may be used in UI (specified by name), ot->prop is not so much used...
1617 * Otherwise:
1618 * * If non-strict, unset properties always match set ones in IDP_EqualsProperties_ex.
1619 * * If strict, unset properties never match set ones in IDP_EqualsProperties_ex,
1620 * and we do not want that to change (else we get things like #41757)!
1621 * ...so in either case, re-running a comparison with unset props set to default is useless.
1622 */
1623 if (!found && properties) {
1624 if (ot && ot->prop) { /* XXX Shall we also check ot->prop is actually an enum? */
1625 /* Make a copy of the properties and unset the 'ot->prop' one if set. */
1626 IDProperty *properties_temp = IDP_CopyProperty(properties);
1627
1628 PointerRNA opptr = RNA_pointer_create_discrete(nullptr, ot->srna, properties_temp);
1629
1630 if (RNA_property_is_set(&opptr, ot->prop)) {
1631 /* For operator that has enum menu,
1632 * unset it so its value does not affect comparison result. */
1633 RNA_property_unset(&opptr, ot->prop);
1634
1636 C, opname, opcontext, properties_temp, is_strict, params, r_keymap);
1637 }
1638
1639 IDP_FreeProperty(properties_temp);
1640 }
1641 }
1642
1643 /* Debug only, helps spotting mismatches between menu entries and shortcuts! */
1644 if (G.debug & G_DEBUG_WM) {
1645 if (!found && is_strict && properties) {
1646 if (ot) {
1647 /* Make a copy of the properties and set unset ones to their default values. */
1648 IDProperty *properties_default = IDP_CopyProperty(properties);
1649
1650 PointerRNA opptr = RNA_pointer_create_discrete(nullptr, ot->srna, properties_default);
1651 WM_operator_properties_default(&opptr, true);
1652
1653 wmKeyMap *km;
1655 C, opname, opcontext, properties_default, is_strict, params, &km);
1656 if (kmi) {
1657 std::string kmi_str = WM_keymap_item_to_string(kmi, false).value_or("");
1658 printf(
1659 "%s: Some set values in keymap entry match default op values, "
1660 "this might not be desired!\n",
1661 opname);
1662 printf("\tkm: '%s', kmi: '%s'\n", km->idname, kmi_str.c_str());
1663#ifndef NDEBUG
1664# ifdef WITH_PYTHON
1665 printf("OPERATOR\n");
1666 IDP_print(properties);
1667 printf("KEYMAP\n");
1668 IDP_print(static_cast<IDProperty *>(kmi->ptr->data));
1669# endif
1670#endif
1671 printf("\n");
1672 }
1673
1674 IDP_FreeProperty(properties_default);
1675 }
1676 }
1677 }
1678
1679 return found;
1680}
1681
1682static bool kmi_filter_is_visible(const wmKeyMap * /*km*/,
1683 const wmKeyMapItem *kmi,
1684 void * /*user_data*/)
1685{
1686 return ((WM_key_event_string(kmi->type, false)[0] != '\0') &&
1687 (IS_EVENT_ACTIONZONE(kmi->type) == false));
1688}
1689
1690std::optional<std::string> WM_key_event_operator_string(const bContext *C,
1691 const char *opname,
1692 wm::OpCallContext opcontext,
1693 IDProperty *properties,
1694 const bool is_strict)
1695{
1696 wmKeyMapItemFind_Params params{};
1698 params.user_data = nullptr;
1700 C, opname, opcontext, properties, is_strict, &params, nullptr);
1701 if (kmi) {
1702 return WM_keymap_item_to_string(kmi, false);
1703 }
1704
1705 return std::nullopt;
1706}
1707
1709 const wmKeyMapItem *kmi,
1710 void *user_data)
1711{
1712 short *mask_pair = static_cast<short int *>(user_data);
1713 return ((WM_event_type_mask_test(kmi->type, eEventType_Mask(mask_pair[0])) == true) &&
1714 (WM_event_type_mask_test(kmi->type, eEventType_Mask(mask_pair[1])) == false) &&
1715 kmi_filter_is_visible(km, kmi, user_data));
1716}
1717
1719 const char *opname,
1720 wm::OpCallContext opcontext,
1721 IDProperty *properties,
1722 const short include_mask,
1723 const short exclude_mask,
1724 wmKeyMap **r_keymap)
1725{
1726 short user_data_mask[2] = {include_mask, exclude_mask};
1727 bool use_mask = (include_mask != EVT_TYPE_MASK_ALL) || (exclude_mask != 0);
1728 wmKeyMapItemFind_Params params{};
1730 params.user_data = use_mask ? user_data_mask : nullptr;
1731 return wm_keymap_item_find(C, opname, opcontext, properties, true, &params, r_keymap);
1732}
1733
1735 const char *opname,
1736 IDProperty *properties,
1737 const short include_mask,
1738 const short exclude_mask)
1739{
1740 short user_data_mask[2] = {include_mask, exclude_mask};
1741 bool use_mask = (include_mask != EVT_TYPE_MASK_ALL) || (exclude_mask != 0);
1742 wmKeyMapItemFind_Params params{};
1744 params.user_data = use_mask ? user_data_mask : nullptr;
1745
1746 return wm_keymap_item_find_in_keymap(keymap, opname, properties, true, &params);
1747}
1748
1750{
1751 if (k1->flag & KMI_INACTIVE || k2->flag & KMI_INACTIVE) {
1752 return false;
1753 }
1754
1755 /* Take event mapping into account. */
1756 int k1type = WM_userdef_event_map(k1->type);
1757 int k2type = WM_userdef_event_map(k2->type);
1758
1759 if (k1type != KM_ANY && k2type != KM_ANY && k1type != k2type) {
1760 return false;
1761 }
1762
1763 if (k1->val != KM_ANY && k2->val != KM_ANY) {
1764 /* Take click, press, release conflict into account. */
1765 if (k1->val == KM_CLICK && ELEM(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0) {
1766 return false;
1767 }
1768 if (k2->val == KM_CLICK && ELEM(k1->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0) {
1769 return false;
1770 }
1771 if (k1->val != k2->val) {
1772 return false;
1773 }
1774 if (k1->val == KM_PRESS_DRAG && (k1->direction != k2->direction)) {
1775 return false;
1776 }
1777 }
1778
1779 if (k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift) {
1780 return false;
1781 }
1782
1783 if (k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl) {
1784 return false;
1785 }
1786
1787 if (k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt) {
1788 return false;
1789 }
1790
1791 if (k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey) {
1792 return false;
1793 }
1794
1795 if (k1->hyper != KM_ANY && k2->hyper != KM_ANY && k1->hyper != k2->hyper) {
1796 return false;
1797 }
1798
1799 if (k1->keymodifier != k2->keymodifier) {
1800 return false;
1801 }
1802
1803 return true;
1804}
1805
1807
1808/* -------------------------------------------------------------------- */
1815
1816/* So operator removal can trigger update. */
1817enum {
1819
1820 /* Ensure all wmKeyMap have their operator types validated after removing an operator. */
1822
1824};
1825
1827
1829#ifndef NDEBUG
1831#endif
1832
1834{
1835 /* Quick tag to do delayed keymap updates. */
1837
1838 if (keymap) {
1839 keymap->flag |= KEYMAP_UPDATE;
1840 }
1841 if (kmi) {
1842 kmi->flag |= KMI_UPDATE;
1843 }
1844}
1845
1850
1851/* NOTE(@ideasman42): regarding suppressing updates.
1852 * If this becomes a common operation it would be better use something more general,
1853 * a key-map flag for example to signify that the key-map is stored outside of a #wmKeyConfig
1854 * and should not receive updates on modification. At the moment this has the down-side of
1855 * needing to be supported in quite a few places for something which isn't used much.
1856 * Since the use case for this is limited, add functions to begin/end suppression.
1857 * If these end up being used a lot we can consider alternatives. */
1858
1867
1877
1882
1887
1889{
1890 int update = (km->flag & KEYMAP_UPDATE);
1891 km->flag &= ~KEYMAP_UPDATE;
1892
1893 for (wmKeyMapItem &kmi : km->items) {
1894 update |= (kmi.flag & KMI_UPDATE);
1895 kmi.flag &= ~KMI_UPDATE;
1896 }
1897
1898 return (update != 0);
1899}
1900
1902{
1903 BLI_assert(keyconf == WM_keyconfig_active(wm));
1904 wmKeyMap *keymap = WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
1905 if (!keymap && wm->runtime->defaultconf) {
1906 keymap = WM_keymap_list_find(
1907 &wm->runtime->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
1908 }
1909
1910 return keymap;
1911}
1912
1914{
1915 /* Ignore #WM_KEYMAP_UPDATE_OPERATORTYPE flag on startup,
1916 * it's likely to be enabled because it's set when registering any operator
1917 * however running this is unnecessary since the key-map hasn't been initialized.
1918 * It's harmless but would add redundant initialization every startup. */
1920
1921 WM_keyconfig_update_ex(wm, false);
1922}
1923
1928
1929void WM_keyconfig_update_ex(wmWindowManager *wm, bool keep_properties)
1930{
1931 if (wm_keymap_update_flag == 0) {
1932 return;
1933 }
1934
1935 bool compat_update = false;
1936
1937 /* Update drop-boxes when the operators have been added or removed. While this isn't an ideal
1938 * place to update drop-boxes, they share characteristics with key-map items.
1939 * We could consider renaming this to API function so it's not only relating to keymaps. */
1940 bool dropbox_update = false;
1941
1942 /* Postpone update until after the key-map has been initialized
1943 * to ensure add-ons have been loaded, see: #113603. */
1945 return;
1946 }
1947
1949 /* One or more operator-types have been removed, this won't happen often
1950 * but when it does we have to check _every_ key-map item. */
1951 wm_keymap_item_properties_update_ot_from_list(&U.user_keymaps, keep_properties);
1952 for (wmKeyConfig &kc : wm->runtime->keyconfigs) {
1954 }
1955
1956 /* An operator has been removed, refresh. */
1957 dropbox_update = true;
1958
1960 }
1961
1963 wmKeyConfig *kc_active = WM_keyconfig_active(wm);
1964
1965 /* Update operator properties for non-modal user keymaps. */
1966 for (wmKeyMap &km : U.user_keymaps) {
1967 if ((km.flag & KEYMAP_MODAL) == 0) {
1968 for (wmKeyMapDiffItem &kmdi : km.diff_items) {
1969 if (kmdi.add_item) {
1971 }
1972 if (kmdi.remove_item) {
1974 }
1975 }
1976
1977 for (wmKeyMapItem &kmi : km.items) {
1979 }
1980 }
1981 }
1982
1983 /* Update `U.user_keymaps` with user key configuration changes. */
1984 for (wmKeyMap &km : wm->runtime->userconf->keymaps) {
1985 /* Only diff if the user keymap was modified. */
1987 /* Find keymaps. */
1988 wmKeyMap *defaultmap = wm_keymap_preset(wm, kc_active, &km);
1989 wmKeyMap *addonmap = WM_keymap_list_find(
1990 &wm->runtime->addonconf->keymaps, km.idname, km.spaceid, km.regionid);
1991
1992 /* Diff. */
1993 if (defaultmap) {
1994 wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, &km);
1995 }
1996 }
1997 }
1998
1999 /* Create user key configuration from preset + addon + user preferences. */
2000 for (wmKeyMap &km : wm->runtime->defaultconf->keymaps) {
2001 /* Find keymaps. */
2002 wmKeyMap *defaultmap = wm_keymap_preset(wm, kc_active, &km);
2003 wmKeyMap *addonmap = WM_keymap_list_find(
2004 &wm->runtime->addonconf->keymaps, km.idname, km.spaceid, km.regionid);
2005 wmKeyMap *usermap = WM_keymap_list_find(&U.user_keymaps, km.idname, km.spaceid, km.regionid);
2006
2007 /* For now only the default map defines modal key-maps,
2008 * if we support modal keymaps for 'addonmap', these will need to be enabled too. */
2010
2011 /* Add. */
2013 &wm->runtime->userconf->keymaps, defaultmap, addonmap, usermap);
2014
2015 if (kmn) {
2016 kmn->modal_items = km.modal_items;
2017 kmn->poll = km.poll;
2019 }
2020
2021 /* In case of old non-diff keymaps, force extra update to create diffs. */
2022 compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
2023 }
2024
2025 /* An operator may have been added, refresh. */
2026 dropbox_update = true;
2027
2029 }
2030
2032
2033 if (compat_update) {
2034 WM_keyconfig_update_tag(nullptr, nullptr);
2036 }
2037
2038 if (dropbox_update) {
2040 }
2041
2042 /* NOTE(@ideasman42): open preferences will contain "stale" #wmKeyMapItem data.
2043 *
2044 * The common case this solves is using Blender with the key-map editor open,
2045 * an action in the view-port for example may manipulate the key-map causing it to be rebuilt.
2046 * Later interaction with the key-map editor may then attempt to access freed data.
2047 *
2048 * Take care, this is _not_ fool proof because it's possible:
2049 * - Key-map options to be shown in any region (using scripts).
2050 * - Key-map re-generation could happen while the preferences is open,
2051 * where the data becomes stale before the UI has a chance to redraw.
2052 *
2053 * In practice both cases are quite unlikely though. */
2054 if (U.space_data.section_active == USER_SECTION_KEYMAP) {
2055 for (wmWindow &win : wm->windows) {
2056 bScreen *screen = WM_window_get_active_screen(&win);
2057 for (ScrArea &area : screen->areabase) {
2058 if (area.spacetype == SPACE_USERPREF) {
2059 ED_area_tag_redraw(&area);
2060 }
2061 }
2062 }
2063 }
2064}
2065
2067
2068/* -------------------------------------------------------------------- */
2074
2076{
2077 if (!keymap) {
2078 return nullptr;
2079 }
2080
2081 /* First user defined keymaps. */
2083 &wm->runtime->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
2084
2085 if (km) {
2086 return km;
2087 }
2088
2089 return keymap;
2090}
2091
2093
2094/* -------------------------------------------------------------------- */
2099
2101{
2102 if (!keymap) {
2103 return;
2104 }
2105
2106 /* Construct default keymap from preset + addons. */
2107 wmKeyConfig *kc_active = WM_keyconfig_active(wm);
2108 wmKeyMap *defaultmap = wm_keymap_preset(wm, kc_active, keymap);
2109 wmKeyMap *addonmap = WM_keymap_list_find(
2110 &wm->runtime->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
2111
2112 if (addonmap) {
2113 defaultmap = wm_keymap_copy(defaultmap);
2114 wm_keymap_addon_add(defaultmap, addonmap);
2115 }
2116
2117 /* Find original item. */
2118 wmKeyMapItem *orig = WM_keymap_item_find_id(defaultmap, kmi->id);
2119
2120 if (orig) {
2121 /* Restore to original. */
2122 if (!STREQ(orig->idname, kmi->idname)) {
2123 STRNCPY(kmi->idname, orig->idname);
2124 WM_keymap_item_properties_reset(kmi, nullptr);
2125 }
2126
2127 if (orig->properties) {
2128 if (kmi->properties) {
2130 kmi->properties = nullptr;
2131 }
2132
2134 if (kmi->ptr) {
2135 kmi->ptr->data = kmi->properties;
2136 }
2137 }
2138
2139 kmi->propvalue = orig->propvalue;
2140 kmi->type = orig->type;
2141 kmi->val = orig->val;
2142 kmi->shift = orig->shift;
2143 kmi->ctrl = orig->ctrl;
2144 kmi->alt = orig->alt;
2145 kmi->oskey = orig->oskey;
2146 kmi->hyper = orig->hyper;
2147 kmi->keymodifier = orig->keymodifier;
2148 kmi->maptype = orig->maptype;
2149 kmi->flag = (kmi->flag & ~(KMI_REPEAT_IGNORE | KMI_INACTIVE)) |
2150 (orig->flag & (KMI_REPEAT_IGNORE | KMI_INACTIVE));
2151
2152 WM_keyconfig_update_tag(keymap, kmi);
2153 }
2154
2155 /* Free temporary keymap. */
2156 if (addonmap) {
2157 WM_keymap_clear(defaultmap);
2158 MEM_delete(defaultmap);
2159 }
2160}
2161
2163{
2164 /* Remove keymap from U.user_keymaps and update. */
2165 wmKeyMap *usermap = WM_keymap_list_find(
2166 &U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
2167
2168 if (usermap) {
2169 WM_keymap_clear(usermap);
2170 BLI_freelinkN(&U.user_keymaps, usermap);
2171
2172 WM_keyconfig_update_tag(nullptr, nullptr);
2174 }
2175}
2176
2177const char *WM_bool_as_string(bool test)
2178{
2179 return test ? IFACE_("ON") : IFACE_("OFF");
2180}
2181
2183
2184/* -------------------------------------------------------------------- */
2187
2189{
2190 for (wmKeyMapItem &kmi : keymap->items) {
2191 if (kmi.id == id) {
2192 return &kmi;
2193 }
2194 }
2195
2196 return nullptr;
2197}
2198
2200 wmKeyMap *km_match,
2201 const wmKeyMapItem *kmi_match,
2202 ReportList *reports)
2203{
2204 /* NOTE: this is called by RNA, some of the reports in this function
2205 * would be asserts when called from C++. */
2206
2207 if (wm_keymap_update_flag != 0) {
2208 /* NOTE: this could be limited to the key-maps marked for updating.
2209 * However #KEYMAP_UPDATE is only cleared for `wm->runtime->userconf`
2210 * so only check the global flag for now.
2211 *
2212 * Use a warning not an error because scripts cannot prevent other scripts from manipulating
2213 * key-map items, so we won't want scripts to create exceptions in unrelated scripts.
2214 * Ideally we could detect which key-maps have been modified. */
2215 BKE_reportf(reports,
2217 "KeyMap item result may be incorrect since an update is pending, call "
2218 "`context.window_manager.keyconfigs.update()` to ensure matches can be found.");
2219 }
2220
2221 if (km_base == km_match) {
2222 /* We could also return `kmi_match` (it's technically correct)
2223 * however this is almost certainly API misuse (as it's a no-op). */
2224 BKE_report(reports, RPT_ERROR, "KeyMaps are equal");
2225 return nullptr;
2226 }
2227
2228 const char *idname = km_base->idname;
2229 const short spaceid = km_base->spaceid;
2230 const short regionid = km_base->regionid;
2231
2232 if (!wm_keymap_is_match(km_base, km_match)) {
2234 reports, RPT_ERROR, "KeyMap \"%s\" does not match \"%s\"", idname, km_match->idname);
2235 return nullptr;
2236 }
2237
2238 wmWindowManager *wm = G_MAIN->wm.first();
2239 wmKeyConfig *kc_active = WM_keyconfig_active(wm);
2240
2241 /* NOTE: the key-maps could store this, it would simplify checks here. */
2242 enum {
2243 KM_TYPE_UNKNOWN = 0,
2244 KM_TYPE_USER,
2245 KM_TYPE_ADDON,
2246 KM_TYPE_ACTIVE,
2247 /* No support yet for preferences. */
2248 } base_type = KM_TYPE_UNKNOWN,
2249 match_type = KM_TYPE_UNKNOWN;
2250
2251 if (km_base->flag & KEYMAP_USER) {
2252 if (km_base == WM_keymap_list_find(&wm->runtime->userconf->keymaps, idname, spaceid, regionid))
2253 {
2254 base_type = KM_TYPE_USER;
2255 }
2256 }
2257
2258 if ((km_match->flag & KEYMAP_USER) == 0) {
2259 if (km_match ==
2260 WM_keymap_list_find(&wm->runtime->addonconf->keymaps, idname, spaceid, regionid))
2261 {
2262 match_type = KM_TYPE_ADDON;
2263 }
2264 else if (km_match == WM_keymap_list_find(&kc_active->keymaps, idname, spaceid, regionid)) {
2265 match_type = KM_TYPE_ACTIVE;
2266 }
2267 }
2268
2269 if (base_type == KM_TYPE_UNKNOWN) {
2270 BKE_reportf(reports, RPT_ERROR, "KeyMap \"%s\" (base) must be a user keymap", idname);
2271 return nullptr;
2272 }
2273
2274 if (match_type == KM_TYPE_UNKNOWN) {
2276 reports, RPT_ERROR, "KeyMap \"%s\" (other) must be an add-on or active keymap", idname);
2277 return nullptr;
2278 }
2279
2280 const int kmi_index = BLI_findindex(&km_match->items, kmi_match);
2281 if (kmi_index == -1) {
2282 BKE_reportf(reports, RPT_ERROR, "KeyMap \"%s\" item not part of the keymap", idname);
2283 return nullptr;
2284 }
2285
2286 int kmi_id;
2287 if (match_type == KM_TYPE_ADDON) {
2288 /* Perform the following lookup that calculates the ID that *would* be used
2289 * if the user key-map was re-created, see: #WM_keymap_item_restore_to_default.
2290 *
2291 * Find the index of the key-map item and add this to the `defaultmap`'s key-map index
2292 * since this is how the "user" key-map ID's are generated.
2293 *
2294 * This is needed so add-ons can show the user key-map items in preferences. */
2295
2296 wmKeyMap *defaultmap = wm_keymap_preset(wm, kc_active, km_base);
2297 if (defaultmap == nullptr) {
2298 /* This should practically never fail, it could be caused by failure
2299 * to refresh the user key-map after manipulating the add-on key-map. */
2300 return nullptr;
2301 }
2302 kmi_id = defaultmap->kmi_id + kmi_index + 1;
2303 }
2304 else {
2305 kmi_id = kmi_match->id;
2306 }
2307
2308 /* Returning null here isn't an error because it's possible there is no match. */
2309 return WM_keymap_item_find_id(km_base, kmi_id);
2310}
2311
2313
2314} // namespace blender
#define G_MAIN
#define STRNCPY_UTF8(dst, src)
#define BLI_STR_UTF8_BROKEN_CIRCLE_WITH_NORTHWEST_ARROW
#define BLI_STR_UTF8_LEFTWARDS_ARROW
#define BLI_STR_UTF8_BLACK_DIAMOND_MINUS_WHITE_X
#define BLI_STR_UTF8_UPWARDS_WHITE_ARROW
#define BLI_STR_UTF8_UPWARDS_ARROW
#define BLI_STR_UTF8_DOWNWARDS_ARROW
#define BLI_STR_UTF8_OPEN_BOX
#define BLI_STR_UTF8_PLACE_OF_INTEREST_SIGN
#define BLI_STR_UTF8_UP_ARROWHEAD
#define BLI_STR_UTF8_HORIZONTAL_TAB_KEY
#define BLI_STR_UTF8_RIGHTWARDS_ARROW
#define BLI_STR_UTF8_ERASE_TO_THE_LEFT
#define BLI_STR_UTF8_RETURN_SYMBOL
#define BLI_STR_UTF8_OPTION_KEY
#define BLT_I18NCONTEXT_ID_WINDOWMANAGER
#define CTX_N_(context, msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_UI_EVENTS
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define KMAP_MAX_NAME
#define WM_KEYCONFIG_ARRAY_P(wm)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define KMI_PARAMS_MOD_FROM_ANY(mod)
Definition WM_keymap.hh:91
#define KM_MOD_HELD
Definition WM_types.hh:339
#define STREQLEN(a, b, n)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
void append(const T &value)
const T & last(const int64_t n=0) const
bool is_empty() const
#define offsetof(t, d)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
BMesh Iterators.
wmKeyConfig * WM_keyconfig_ensure(wmWindowManager *wm, const char *idname, bool user_defined)
Definition wm_keymap.cc:310
void WM_keymap_item_properties_reset(wmKeyMapItem *kmi, IDProperty *properties)
Definition wm_keymap.cc:210
static bool wm_keymap_test_and_clear_update(wmKeyMap *km)
UserDef U
Definition blender.cc:58
wmKeyMap * WM_keymap_active(const wmWindowManager *wm, wmKeyMap *keymap)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
wmKeyMap * WM_keymap_list_find_spaceid_or_empty(ListBaseT< wmKeyMap > *lb, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:919
void IDP_print(const IDProperty *prop)
static void keymap_item_set_id(wmKeyMap *keymap, wmKeyMapItem *kmi)
Definition wm_keymap.cc:552
const wmKeyMapItem * WM_modalkeymap_find_propvalue(const wmKeyMap *km, const int propvalue)
ScrArea * CTX_wm_area(const bContext *C)
wmKeyMap * WM_keymap_find_all(wmWindowManager *wm, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:949
char * STRNCPY(char(&dst)[N], const char *src)
@ RPT_WARNING
Definition BKE_report.hh:40
wmKeyMapItem * WM_keymap_item_find_id(wmKeyMap *keymap, const int id)
void WM_keyconfig_update_postpone_begin()
static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
Definition wm_keymap.cc:697
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
static bool wm_keymap_is_match(const wmKeyMap *km_a, const wmKeyMap *km_b)
Definition wm_keymap.cc:521
wmKeyMapItem * WM_modalkeymap_add_item(wmKeyMap *km, const KeyMapItem_Params *params, int value)
bool WM_keymap_poll(bContext *C, wmKeyMap *keymap)
Definition wm_keymap.cc:485
void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
static void wm_keymap_item_properties_update_ot_from_list(ListBaseT< wmKeyMap > *km_lb, const bool keep_properties)
Definition wm_keymap.cc:173
void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
bool WM_event_type_mask_test(const int event_type, const enum eEventType_Mask mask)
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
WorkSpace * CTX_wm_workspace(const bContext *C)
void WM_keymap_remove(wmKeyConfig *keyconf, wmKeyMap *keymap)
Definition wm_keymap.cc:477
static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
Definition wm_keymap.cc:621
static char wm_keymap_update_suppress_flag
static wmKeyMapItem * wm_keymap_item_find_props(const bContext *C, const char *opname, wm::OpCallContext opcontext, const IDProperty *properties, const bool is_strict, const wmKeyMapItemFind_Params *params, wmKeyMap **r_keymap)
int bool bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL(1
@ WM_KEYMAP_UPDATE_RECONFIGURE
@ WM_KEYMAP_UPDATE_OPERATORTYPE
@ WM_KEYMAP_UPDATE_POSTPONE
void WM_operator_properties_free(PointerRNA *ptr)
std::optional< std::string > WM_keymap_item_raw_to_string(const int8_t shift, const int8_t ctrl, const int8_t alt, const int8_t oskey, const int8_t hyper, const short keymodifier, const short val, const short type, const bool compact)
bool BKE_workspace_owner_id_check(const WorkSpace *workspace, const char *owner_id) ATTR_NONNULL()
Definition workspace.cc:607
static wmKeyConfig * WM_keyconfig_active(wmWindowManager *wm)
Definition wm_keymap.cc:371
wmKeyMapItem * WM_key_event_operator_from_keymap(wmKeyMap *keymap, const char *opname, IDProperty *properties, const short include_mask, const short exclude_mask)
const char * WM_bool_as_string(bool test)
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
static wmKeyMapDiffItem * wm_keymap_diff_item_copy(wmKeyMapDiffItem *kmdi)
Definition wm_keymap.cc:258
bool RNA_struct_equals(Main *bmain, PointerRNA *ptr_a, PointerRNA *ptr_b, eRNACompareMode mode)
void WM_keyconfig_update(wmWindowManager *wm)
const char * RNA_struct_state_owner_get()
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
static bool kmi_filter_is_visible(const wmKeyMap *, const wmKeyMapItem *kmi, void *)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
void WM_keyconfig_update_operatortype_tag()
static void wm_user_modal_keymap_set_items(wmWindowManager *wm, wmKeyMap *km)
CLG_LogRef * WM_LOG_EVENTS
wmWindow * CTX_wm_window(const bContext *C)
std::optional< std::string > WM_key_event_operator_string(const bContext *C, const char *opname, wm::OpCallContext opcontext, IDProperty *properties, const bool is_strict)
static wmKeyMapItem * wm_keymap_item_find(const bContext *C, const char *opname, wm::OpCallContext opcontext, IDProperty *properties, bool is_strict, const wmKeyMapItemFind_Params *params, wmKeyMap **r_keymap)
static wmKeyMapItem * wm_keymap_find_item_equals_result(wmKeyMap *km, const wmKeyMapItem *needle)
Definition wm_keymap.cc:641
void WM_keymap_clear(wmKeyMap *keymap)
Definition wm_keymap.cc:461
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
wmKeyMapItem * WM_keymap_add_item(wmKeyMap *keymap, const char *idname, const KeyMapItem_Params *params)
Definition wm_keymap.cc:563
static void keymap_event_set(wmKeyMapItem *kmi, const KeyMapItem_Params *params)
Definition wm_keymap.cc:527
wmKeyMapItem * WM_keymap_item_find_match(wmKeyMap *km_base, wmKeyMap *km_match, const wmKeyMapItem *kmi_match, ReportList *reports)
static wmKeyMap * wm_keymap_patch_update(ListBaseT< wmKeyMap > *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *usermap)
Definition wm_keymap.cc:760
static char wm_keymap_update_flag
@ RNA_EQ_UNSET_MATCH_NONE
const char * WM_key_event_string(const short type, const bool compact)
Global G
Definition blender.cc:57
bool RNA_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value)
PointerRNA WM_operator_properties_create(const char *opstring)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
static void wm_keymap_diff_item_free(wmKeyMapDiffItem *kmdi)
Definition wm_keymap.cc:273
static bool wm_keymap_item_equals_result(const wmKeyMapItem *a, const wmKeyMapItem *b)
Definition wm_keymap.cc:192
static wmKeyMap * wm_keymap_preset(wmWindowManager *wm, wmKeyConfig *keyconf, wmKeyMap *km)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
const EnumPropertyItem rna_enum_event_type_items[]
Definition rna_wm.cc:225
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool WM_keymap_item_compare(const wmKeyMapItem *k1, const wmKeyMapItem *k2)
void WM_keymap_item_restore_to_default(wmWindowManager *wm, wmKeyMap *keymap, wmKeyMapItem *kmi)
int BLF_default()
wmWindowManager * CTX_wm_manager(const bContext *C)
wmKeyMapItem * WM_keymap_add_item_copy(wmKeyMap *keymap, const wmKeyMapItem *kmi_src)
Definition wm_keymap.cc:582
static void wm_keymap_item_properties_set(wmKeyMapItem *kmi)
Definition wm_keymap.cc:119
void RNA_property_unset(PointerRNA *ptr, PropertyRNA *prop)
static wmKeyMapItem * wm_keymap_find_item_equals(wmKeyMap *km, const wmKeyMapItem *needle)
Definition wm_keymap.cc:630
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void WM_keyconfig_set_active(wmWindowManager *wm, const char *idname)
Definition wm_keymap.cc:386
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static wmKeyMapItem * wm_keymap_item_find_handlers(const bContext *C, wmWindowManager *wm, wmWindow *win, ListBaseT< wmEventHandler > *handlers, const char *opname, wm::OpCallContext, const IDProperty *properties, const bool is_strict, const wmKeyMapItemFind_Params *params, wmKeyMap **r_keymap)
void WM_keyconfig_update_on_startup(wmWindowManager *wm)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:935
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
int WM_userdef_event_map(int kmitype)
static void wm_keymap_item_clear_runtime(wmKeyMapItem *kmi)
Definition wm_keymap.cc:108
static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km, wmKeyMap *orig_km, wmKeyMap *addon_km)
Definition wm_keymap.cc:652
static const wmKeyMapItem * wm_modalkeymap_find_propvalue_iter(const wmKeyMap *km, const wmKeyMapItem *kmi, const int propvalue)
int WM_keymap_item_map_type_get(const wmKeyMapItem *kmi)
Definition wm_keymap.cc:227
void WM_dropbox_update_ot()
@ WM_HANDLER_TYPE_KEYMAP
void WM_keyconfig_update_suppress_begin()
bool BLF_has_glyph(int fontid, unsigned int unicode) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:153
CLG_LogRef * WM_LOG_OPERATORS
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
static bool kmi_filter_is_visible_type_mask(const wmKeyMap *km, const wmKeyMapItem *kmi, void *user_data)
wmKeyMapItem * WM_key_event_operator(const bContext *C, const char *opname, wm::OpCallContext opcontext, IDProperty *properties, const short include_mask, const short exclude_mask, wmKeyMap **r_keymap)
@ KM_TEXTINPUT
Definition WM_types.hh:317
std::optional< std::string > WM_keymap_item_to_string(const wmKeyMapItem *kmi, const bool compact)
void WM_keyconfig_free(wmKeyConfig *keyconf)
Definition wm_keymap.cc:365
static wmKeyMapItem * wm_keymap_item_find_in_keymap(wmKeyMap *keymap, const char *opname, const IDProperty *properties, const bool is_strict, const wmKeyMapItemFind_Params *params)
wmKeyMap * WM_keymap_list_find(ListBaseT< wmKeyMap > *lb, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:903
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
Definition wm_keymap.cc:333
void WM_keymap_restore_to_default(wmKeyMap *keymap, wmWindowManager *wm)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void wm_keymap_diff_update(ListBaseT< wmKeyMap > *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *km)
Definition wm_keymap.cc:848
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void WM_keyconfig_update_suppress_end()
static void wm_keymap_item_free_data(wmKeyMapItem *kmi)
Definition wm_keymap.cc:93
static wmKeyMap * wm_keymap_new(const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:410
@ KM_NOTHING
Definition WM_types.hh:323
@ KM_PRESS_DRAG
Definition WM_types.hh:332
@ KM_DBL_CLICK
Definition WM_types.hh:327
@ KM_RELEASE
Definition WM_types.hh:325
unsigned int BLI_str_utf8_as_unicode_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
static const char * key_event_glyph_or_text(const int font_id, const char *text, const char *single_glyph)
bool WM_operator_properties_default(PointerRNA *ptr, const bool do_update)
void WM_keyconfig_clear(wmKeyConfig *keyconf)
Definition wm_keymap.cc:355
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
void WM_event_get_keymaps_from_handler(wmWindowManager *wm, wmWindow *win, wmEventHandler_Keymap *handler, wmEventHandler_KeymapResult *km_result)
@ OPTYPE_MACRO
Definition WM_types.hh:193
static wmKeyMapItem * wm_keymap_item_copy(const wmKeyMapItem *kmi)
Definition wm_keymap.cc:68
static bool wm_keymap_item_equals(const wmKeyMapItem *a, const wmKeyMapItem *b)
Definition wm_keymap.cc:200
void WM_keyconfig_update_postpone_end()
static void wm_keymap_copy_data(wmKeyMap *keymapn, wmKeyMap *keymap)
Definition wm_keymap.cc:427
static void wm_keymap_item_properties_update_ot(wmKeyMapItem *kmi, const bool keep_properties)
Definition wm_keymap.cc:132
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void WM_keyconfig_update_tag(wmKeyMap *keymap, wmKeyMapItem *kmi)
std::optional< std::string > WM_modalkeymap_operator_items_to_string(wmOperatorType *ot, const int propvalue, const bool compact)
wmKeyMapItem * WM_modalkeymap_add_item_str(wmKeyMap *km, const KeyMapItem_Params *params, const char *value)
std::optional< std::string > WM_modalkeymap_items_to_string(const wmKeyMap *km, const int propvalue, const bool compact)
static int8_t wm_keymap_update_suppress_count
wmKeyConfig * WM_keyconfig_new(wmWindowManager *wm, const char *idname, bool user_defined)
Definition wm_keymap.cc:295
bScreen * WM_window_get_active_screen(const wmWindow *win)
ARegion * CTX_wm_region(const bContext *C)
void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
Definition wm_keymap.cc:595
void WM_keyconfig_update_ex(wmWindowManager *wm, bool keep_properties)
wmKeyMap * WM_keymap_find_all_spaceid_or_empty(wmWindowManager *wm, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:954
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
static wmKeyMap * wm_keymap_copy(wmKeyMap *keymap)
Definition wm_keymap.cc:454
signed char int8_t
Definition BKE_text.h:76
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
bool IDP_EqualsProperties_ex(const IDProperty *prop1, const IDProperty *prop2, bool is_strict) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:1036
bke::ARegionRuntime * runtime
eRegion_Type regiontype
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< struct wmEventHandler > handlers
ListBaseT< struct ScrArea > areabase
ListBaseT< wmEventHandler > handlers
ListBaseT< wmEventHandler > handlers
ListBaseT< wmEventHandler > modalhandlers
ListBaseT< wmKeyMap > keymaps
struct wmKeyMapDiffItem * next
struct wmKeyMapDiffItem * prev
bool(*) filter_fn(const wmKeyMap *km, const wmKeyMapItem *kmi, void *user_data)
Definition wm_keymap.cc:58
ListBaseT< wmKeyMapDiffItem > diff_items
bool(*) poll_modal_item(const struct wmOperator *op, int value)
bool(*) poll(struct bContext *)
ListBaseT< wmKeyMapItem > items
bke::WindowRuntime * runtime
#define ISTIMER(event_type)
#define IS_EVENT_ACTIONZONE(event_type)
#define ISKEYBOARD(event_type)
#define EVT_TYPE_MASK_ALL
#define ISNDOF(event_type)
#define ISMOUSE(event_type)