4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2007 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/windowmanager/intern/wm_keymap.c
36 #include "DNA_object_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_userdef_types.h"
40 #include "DNA_windowmanager_types.h"
42 #include "MEM_guardedalloc.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
47 #include "BKE_blender.h"
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
52 #include "BKE_screen.h"
55 #include "RNA_access.h"
56 #include "RNA_enum_types.h"
60 #include "wm_window.h"
61 #include "wm_event_system.h"
62 #include "wm_event_types.h"
64 /******************************* Keymap Item **********************************
65 * Item in a keymap, that maps from an event to an operator or modal map item */
67 static wmKeyMapItem *wm_keymap_item_copy(wmKeyMapItem *kmi)
69 wmKeyMapItem *kmin = MEM_dupallocN(kmi);
71 kmin->prev= kmin->next= NULL;
72 kmin->flag &= ~KMI_UPDATE;
74 if(kmin->properties) {
75 kmin->ptr= MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
76 WM_operator_properties_create(kmin->ptr, kmin->idname);
78 kmin->properties= IDP_CopyProperty(kmin->properties);
79 kmin->ptr->data= kmin->properties;
85 static void wm_keymap_item_free(wmKeyMapItem *kmi)
89 WM_operator_properties_free(kmi->ptr);
94 static void wm_keymap_item_properties_set(wmKeyMapItem *kmi)
96 WM_operator_properties_alloc(&(kmi->ptr), &(kmi->properties), kmi->idname);
97 WM_operator_properties_sanitize(kmi->ptr, 1);
100 static int wm_keymap_item_equals_result(wmKeyMapItem *a, wmKeyMapItem *b)
102 if(strcmp(a->idname, b->idname) != 0)
105 if(!((a->ptr==NULL && b->ptr==NULL) ||
106 (a->ptr && b->ptr && IDP_EqualsProperties(a->ptr->data, b->ptr->data))))
109 if((a->flag & KMI_INACTIVE) != (b->flag & KMI_INACTIVE))
112 return (a->propvalue == b->propvalue);
115 static int wm_keymap_item_equals(wmKeyMapItem *a, wmKeyMapItem *b)
117 return (wm_keymap_item_equals_result(a, b) &&
118 a->type == b->type &&
120 a->shift == b->shift &&
121 a->ctrl == b->ctrl &&
123 a->oskey == b->oskey &&
124 a->keymodifier == b->keymodifier &&
125 a->maptype == b->maptype);
128 /* properties can be NULL, otherwise the arg passed is used and ownership is given to the kmi */
129 void WM_keymap_properties_reset(wmKeyMapItem *kmi, struct IDProperty *properties)
131 WM_operator_properties_free(kmi->ptr);
135 kmi->properties = properties;
137 wm_keymap_item_properties_set(kmi);
140 /**************************** Keymap Diff Item *********************************
141 * Item in a diff keymap, used for saving diff of keymaps in user preferences */
143 static wmKeyMapDiffItem *wm_keymap_diff_item_copy(wmKeyMapDiffItem *kmdi)
145 wmKeyMapDiffItem *kmdin = MEM_dupallocN(kmdi);
147 kmdin->next = kmdin->prev = NULL;
149 kmdin->add_item = wm_keymap_item_copy(kmdi->add_item);
150 if(kmdi->remove_item)
151 kmdin->remove_item = wm_keymap_item_copy(kmdi->remove_item);
156 static void wm_keymap_diff_item_free(wmKeyMapDiffItem *kmdi)
158 if(kmdi->remove_item) {
159 wm_keymap_item_free(kmdi->remove_item);
160 MEM_freeN(kmdi->remove_item);
163 wm_keymap_item_free(kmdi->add_item);
164 MEM_freeN(kmdi->add_item);
168 /***************************** Key Configuration ******************************
169 * List of keymaps for all editors, modes, ... . There is a builtin default key
170 * configuration, a user key configuration, and other preset configurations. */
172 wmKeyConfig *WM_keyconfig_new(wmWindowManager *wm, const char *idname)
174 wmKeyConfig *keyconf;
176 keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
177 BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
178 BLI_addtail(&wm->keyconfigs, keyconf);
183 wmKeyConfig *WM_keyconfig_new_user(wmWindowManager *wm, const char *idname)
185 wmKeyConfig *keyconf = WM_keyconfig_new(wm, idname);
187 keyconf->flag |= KEYCONF_USER;
192 void WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
195 if (strncmp(U.keyconfigstr, keyconf->idname, sizeof(U.keyconfigstr)) == 0) {
196 BLI_strncpy(U.keyconfigstr, wm->defaultconf->idname, sizeof(U.keyconfigstr));
197 WM_keyconfig_update_tag(NULL, NULL);
200 BLI_remlink(&wm->keyconfigs, keyconf);
201 WM_keyconfig_free(keyconf);
205 void WM_keyconfig_free(wmKeyConfig *keyconf)
209 while((km= keyconf->keymaps.first)) {
211 BLI_freelinkN(&keyconf->keymaps, km);
217 static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
221 for(kc= lb->first; kc; kc= kc->next)
222 if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
228 wmKeyConfig *WM_keyconfig_active(wmWindowManager *wm)
230 wmKeyConfig *keyconf;
232 /* first try from preset */
233 keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
237 /* otherwise use default */
238 return wm->defaultconf;
241 void WM_keyconfig_set_active(wmWindowManager *wm, const char *idname)
243 /* setting a different key configuration as active: we ensure all is
244 updated properly before and after making the change */
246 WM_keyconfig_update(wm);
248 BLI_strncpy(U.keyconfigstr, idname, sizeof(U.keyconfigstr));
250 WM_keyconfig_update_tag(NULL, NULL);
251 WM_keyconfig_update(wm);
254 /********************************** Keymap *************************************
255 * List of keymap items for one editor, mode, modal operator, ... */
257 static wmKeyMap *wm_keymap_new(const char *idname, int spaceid, int regionid)
259 wmKeyMap *km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
261 BLI_strncpy(km->idname, idname, KMAP_MAX_NAME);
262 km->spaceid= spaceid;
263 km->regionid= regionid;
268 static wmKeyMap *wm_keymap_copy(wmKeyMap *keymap)
270 wmKeyMap *keymapn = MEM_dupallocN(keymap);
271 wmKeyMapItem *kmi, *kmin;
272 wmKeyMapDiffItem *kmdi, *kmdin;
274 keymapn->modal_items= keymap->modal_items;
275 keymapn->poll= keymap->poll;
276 keymapn->items.first= keymapn->items.last= NULL;
277 keymapn->flag &= ~(KEYMAP_UPDATE|KEYMAP_EXPANDED);
279 for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
280 kmdin= wm_keymap_diff_item_copy(kmdi);
281 BLI_addtail(&keymapn->items, kmdin);
284 for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
285 kmin= wm_keymap_item_copy(kmi);
286 BLI_addtail(&keymapn->items, kmin);
292 void WM_keymap_free(wmKeyMap *keymap)
295 wmKeyMapDiffItem *kmdi;
297 for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next)
298 wm_keymap_diff_item_free(kmdi);
300 for(kmi=keymap->items.first; kmi; kmi=kmi->next)
301 wm_keymap_item_free(kmi);
303 BLI_freelistN(&keymap->diff_items);
304 BLI_freelistN(&keymap->items);
307 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
311 kmi->keymodifier= keymodifier;
313 if(modifier == KM_ANY) {
314 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
318 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
321 if(modifier & KM_SHIFT)
323 else if(modifier & KM_SHIFT2)
325 if(modifier & KM_CTRL)
327 else if(modifier & KM_CTRL2)
329 if(modifier & KM_ALT)
331 else if(modifier & KM_ALT2)
333 if(modifier & KM_OSKEY)
335 else if(modifier & KM_OSKEY2)
340 static void keymap_item_set_id(wmKeyMap *keymap, wmKeyMapItem *kmi)
343 if ((keymap->flag & KEYMAP_USER) == 0) {
344 kmi->id = keymap->kmi_id;
346 kmi->id = -keymap->kmi_id; // User defined keymap entries have negative ids
350 /* if item was added, then bail out */
351 wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
355 for(kmi= keymap->items.first; kmi; kmi= kmi->next)
356 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
359 kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
361 BLI_addtail(&keymap->items, kmi);
362 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
364 keymap_item_set_id(keymap, kmi);
366 keymap_event_set(kmi, type, val, modifier, keymodifier);
367 wm_keymap_item_properties_set(kmi);
372 /* always add item */
373 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
375 wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
377 BLI_addtail(&keymap->items, kmi);
378 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
380 keymap_event_set(kmi, type, val, modifier, keymodifier);
381 wm_keymap_item_properties_set(kmi);
383 keymap_item_set_id(keymap, kmi);
385 WM_keyconfig_update_tag(keymap, kmi);
390 /* menu wrapper for WM_keymap_add_item */
391 wmKeyMapItem *WM_keymap_add_menu(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
393 wmKeyMapItem *kmi= WM_keymap_add_item(keymap, "WM_OT_call_menu", type, val, modifier, keymodifier);
394 RNA_string_set(kmi->ptr, "name", idname);
398 void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
400 if(BLI_findindex(&keymap->items, kmi) != -1) {
402 WM_operator_properties_free(kmi->ptr);
405 BLI_freelinkN(&keymap->items, kmi);
407 WM_keyconfig_update_tag(keymap, kmi);
411 /************************** Keymap Diff and Patch ****************************
412 * Rather than saving the entire keymap for user preferences, we only save a
413 * diff so that changes in the defaults get synced. This system is not perfect
414 * but works better than overriding the keymap entirely when only few items
417 static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
419 wmKeyMapItem *kmi, *kmin;
421 for(kmi=addonmap->items.first; kmi; kmi=kmi->next) {
422 kmin = wm_keymap_item_copy(kmi);
423 keymap_item_set_id(keymap, kmin);
424 BLI_addhead(&keymap->items, kmin);
428 static wmKeyMapItem *wm_keymap_find_item_equals(wmKeyMap *km, wmKeyMapItem *needle)
432 for(kmi=km->items.first; kmi; kmi=kmi->next)
433 if(wm_keymap_item_equals(kmi, needle))
439 static wmKeyMapItem *wm_keymap_find_item_equals_result(wmKeyMap *km, wmKeyMapItem *needle)
443 for(kmi=km->items.first; kmi; kmi=kmi->next)
444 if(wm_keymap_item_equals_result(kmi, needle))
450 static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km, wmKeyMap *orig_km, wmKeyMap *addon_km)
452 wmKeyMapItem *kmi, *to_kmi, *orig_kmi;
453 wmKeyMapDiffItem *kmdi;
455 for(kmi=from_km->items.first; kmi; kmi=kmi->next) {
456 to_kmi = WM_keymap_item_find_id(to_km, kmi->id);
460 kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
461 kmdi->remove_item = wm_keymap_item_copy(kmi);
462 BLI_addtail(&diff_km->diff_items, kmdi);
464 else if(to_kmi && !wm_keymap_item_equals(kmi, to_kmi)) {
466 kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
467 kmdi->remove_item = wm_keymap_item_copy(kmi);
468 kmdi->add_item = wm_keymap_item_copy(to_kmi);
469 BLI_addtail(&diff_km->diff_items, kmdi);
472 /* sync expanded flag back to original so we don't loose it on repatch */
474 orig_kmi = WM_keymap_item_find_id(orig_km, kmi->id);
477 orig_kmi = wm_keymap_find_item_equals(addon_km, kmi);
480 orig_kmi->flag &= ~KMI_EXPANDED;
481 orig_kmi->flag |= (to_kmi->flag & KMI_EXPANDED);
486 for(kmi=to_km->items.first; kmi; kmi=kmi->next) {
489 kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
490 kmdi->add_item = wm_keymap_item_copy(kmi);
491 BLI_addtail(&diff_km->diff_items, kmdi);
496 static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
498 wmKeyMapDiffItem *kmdi;
499 wmKeyMapItem *kmi_remove, *kmi_add;
501 for(kmdi=diff_km->diff_items.first; kmdi; kmdi=kmdi->next) {
502 /* find item to remove */
504 if(kmdi->remove_item) {
505 kmi_remove = wm_keymap_find_item_equals(km, kmdi->remove_item);
507 kmi_remove = wm_keymap_find_item_equals_result(km, kmdi->remove_item);
512 /* only if nothing to remove or item to remove found */
513 if(!kmdi->remove_item || kmi_remove) {
514 kmi_add = wm_keymap_item_copy(kmdi->add_item);
515 kmi_add->flag |= KMI_USER_MODIFIED;
518 kmi_add->flag &= ~KMI_EXPANDED;
519 kmi_add->flag |= (kmi_remove->flag & KMI_EXPANDED);
520 kmi_add->id = kmi_remove->id;
521 BLI_insertlinkbefore(&km->items, kmi_remove, kmi_add);
524 keymap_item_set_id(km, kmi_add);
525 BLI_addtail(&km->items, kmi_add);
532 wm_keymap_item_free(kmi_remove);
533 BLI_freelinkN(&km->items, kmi_remove);
538 static wmKeyMap *wm_keymap_patch_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *usermap)
543 /* remove previous keymap in list, we will replace it */
544 km = WM_keymap_list_find(lb, defaultmap->idname, defaultmap->spaceid, defaultmap->regionid);
546 expanded = (km->flag & (KEYMAP_EXPANDED|KEYMAP_CHILDREN_EXPANDED));
548 BLI_freelinkN(lb, km);
551 /* copy new keymap from an existing one */
552 if(usermap && !(usermap->flag & KEYMAP_DIFF)) {
553 /* for compatibiltiy with old user preferences with non-diff
554 keymaps we override the original entirely */
555 wmKeyMapItem *kmi, *orig_kmi;
557 km = wm_keymap_copy(usermap);
559 /* try to find corresponding id's for items */
560 for(kmi=km->items.first; kmi; kmi=kmi->next) {
561 orig_kmi = wm_keymap_find_item_equals(defaultmap, kmi);
563 orig_kmi = wm_keymap_find_item_equals_result(defaultmap, kmi);
566 kmi->id = orig_kmi->id;
568 kmi->id = -(km->kmi_id++);
571 km->flag |= KEYMAP_UPDATE; /* update again to create diff */
574 km = wm_keymap_copy(defaultmap);
576 /* add addon keymap items */
578 wm_keymap_addon_add(km, addonmap);
580 /* tag as being user edited */
582 km->flag |= KEYMAP_USER_MODIFIED;
583 km->flag |= KEYMAP_USER|expanded;
585 /* apply user changes of diff keymap */
586 if(usermap && (usermap->flag & KEYMAP_DIFF))
587 wm_keymap_patch(km, usermap);
595 static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *km)
597 wmKeyMap *diffmap, *prevmap, *origmap;
599 /* create temporary default + addon keymap for diff */
600 origmap = defaultmap;
603 defaultmap = wm_keymap_copy(defaultmap);
604 wm_keymap_addon_add(defaultmap, addonmap);
607 /* remove previous diff keymap in list, we will replace it */
608 prevmap = WM_keymap_list_find(lb, km->idname, km->spaceid, km->regionid);
610 WM_keymap_free(prevmap);
611 BLI_freelinkN(lb, prevmap);
614 /* create diff keymap */
615 diffmap= wm_keymap_new(km->idname, km->spaceid, km->regionid);
616 diffmap->flag |= KEYMAP_DIFF;
617 wm_keymap_diff(diffmap, defaultmap, km, origmap, addonmap);
619 /* add to list if not empty */
620 if(diffmap->diff_items.first) {
621 BLI_addtail(lb, diffmap);
624 WM_keymap_free(diffmap);
628 /* free temporary default map */
630 WM_keymap_free(defaultmap);
631 MEM_freeN(defaultmap);
635 /* ****************** storage in WM ************ */
637 /* name id's are for storing general or multiple keymaps,
638 space/region ids are same as DNA_space_types.h */
639 /* gets free'd in wm.c */
641 wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int regionid)
645 for(km= lb->first; km; km= km->next)
646 if(km->spaceid==spaceid && km->regionid==regionid)
647 if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
653 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
655 wmKeyMap *km= WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
658 km= wm_keymap_new(idname, spaceid, regionid);
659 BLI_addtail(&keyconf->keymaps, km);
661 WM_keyconfig_update_tag(km, NULL);
667 wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid, int regionid)
669 wmWindowManager *wm= CTX_wm_manager(C);
671 return WM_keymap_list_find(&wm->userconf->keymaps, idname, spaceid, regionid);
674 /* ****************** modal keymaps ************ */
676 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
678 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, const char *idname, EnumPropertyItem *items)
680 wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
681 km->flag |= KEYMAP_MODAL;
682 km->modal_items= items;
687 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, const char *idname)
691 for(km= keyconf->keymaps.first; km; km= km->next)
692 if(km->flag & KEYMAP_MODAL)
693 if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
700 wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
702 wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
704 BLI_addtail(&km->items, kmi);
705 kmi->propvalue= value;
707 keymap_event_set(kmi, type, val, modifier, keymodifier);
709 keymap_item_set_id(km, kmi);
711 WM_keyconfig_update_tag(km, kmi);
716 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
718 wmOperatorType *ot= WM_operatortype_find(opname, 0);
723 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
726 /* ***************** get string from key events **************** */
728 const char *WM_key_event_string(short type)
730 const char *name= NULL;
731 if(RNA_enum_name(event_type_items, (int)type, &name))
737 char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
743 if (kmi->shift == KM_ANY &&
744 kmi->ctrl == KM_ANY &&
745 kmi->alt == KM_ANY &&
746 kmi->oskey == KM_ANY) {
751 strcat(buf, "Shift ");
754 strcat(buf, "Ctrl ");
763 if(kmi->keymodifier) {
764 strcat(buf, WM_key_event_string(kmi->keymodifier));
768 strcat(buf, WM_key_event_string(kmi->type));
769 BLI_strncpy(str, buf, len);
774 static wmKeyMapItem *wm_keymap_item_find_handlers(const bContext *C, ListBase *handlers, const char *opname, int UNUSED(opcontext), IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
776 wmWindowManager *wm= CTX_wm_manager(C);
777 wmEventHandler *handler;
781 /* find keymap item in handlers */
782 for(handler=handlers->first; handler; handler=handler->next) {
783 keymap= WM_keymap_active(wm, handler->keymap);
785 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
786 for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
788 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
790 if (!ISHOTKEY(kmi->type))
794 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
795 if(keymap_r) *keymap_r= keymap;
800 if(keymap_r) *keymap_r= keymap;
808 /* ensure un-initialized keymap is never used */
809 if(keymap_r) *keymap_r= NULL;
813 static wmKeyMapItem *wm_keymap_item_find_props(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
815 wmWindow *win= CTX_wm_window(C);
816 ScrArea *sa= CTX_wm_area(C);
817 ARegion *ar= CTX_wm_region(C);
818 wmKeyMapItem *found= NULL;
820 /* look into multiple handler lists to find the item */
822 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
825 if(sa && found==NULL)
826 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
829 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
831 if (!(ar && ar->regiontype == RGN_TYPE_WINDOW))
832 ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
835 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
838 else if(ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
839 if (!(ar && ar->regiontype == RGN_TYPE_CHANNELS))
840 ar= BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
843 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
845 else if(ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
846 if (!(ar && ar->regiontype == RGN_TYPE_PREVIEW))
847 ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
850 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
854 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
861 static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
863 wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, hotkey, keymap_r);
866 found= wm_keymap_item_find_props(C, opname, opcontext, NULL, 0, hotkey, keymap_r);
871 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
873 wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, 0, NULL);
876 WM_keymap_item_to_string(kmi, str, len);
883 int WM_key_event_operator_id(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
885 wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, hotkey, keymap_r);
893 int WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
897 if (k1->flag & KMI_INACTIVE || k2->flag & KMI_INACTIVE)
900 /* take event mapping into account */
901 k1type = WM_userdef_event_map(k1->type);
902 k2type = WM_userdef_event_map(k2->type);
904 if(k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
907 if(k1->val != KM_ANY && k2->val != KM_ANY) {
908 /* take click, press, release conflict into account */
909 if (k1->val == KM_CLICK && ELEM3(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
911 if (k2->val == KM_CLICK && ELEM3(k1->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
913 if (k1->val != k2->val)
917 if(k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
920 if(k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
923 if(k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
926 if(k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
929 if(k1->keymodifier != k2->keymodifier)
935 /************************* Update Final Configuration *************************
936 * On load or other changes, the final user key configuration is rebuilt from
937 * the preset, addon and user preferences keymaps. We also test if the final
938 * configuration changed and write the changes to the user preferences. */
940 static int WM_KEYMAP_UPDATE = 0;
942 void WM_keyconfig_update_tag(wmKeyMap *km, wmKeyMapItem *kmi)
944 /* quick tag to do delayed keymap updates */
948 km->flag |= KEYMAP_UPDATE;
950 kmi->flag |= KMI_UPDATE;
953 static int wm_keymap_test_and_clear_update(wmKeyMap *km)
958 update= (km->flag & KEYMAP_UPDATE);
959 km->flag &= ~KEYMAP_UPDATE;
961 for(kmi=km->items.first; kmi; kmi=kmi->next) {
962 update= update || (kmi->flag & KMI_UPDATE);
963 kmi->flag &= ~KMI_UPDATE;
969 static wmKeyMap *wm_keymap_preset(wmWindowManager *wm, wmKeyMap *km)
971 wmKeyConfig *keyconf= WM_keyconfig_active(wm);
974 keymap= WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
976 keymap= WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
981 void WM_keyconfig_update(wmWindowManager *wm)
983 wmKeyMap *km, *defaultmap, *addonmap, *usermap, *kmn;
985 wmKeyMapDiffItem *kmdi;
986 int compat_update = 0;
988 if(!WM_KEYMAP_UPDATE)
991 /* update operator properties for non-modal user keymaps */
992 for(km=U.user_keymaps.first; km; km=km->next) {
993 if((km->flag & KEYMAP_MODAL) == 0) {
994 for(kmdi=km->diff_items.first; kmdi; kmdi=kmdi->next) {
996 wm_keymap_item_properties_set(kmdi->add_item);
997 if(kmdi->remove_item)
998 wm_keymap_item_properties_set(kmdi->remove_item);
1001 for(kmi=km->items.first; kmi; kmi=kmi->next)
1002 wm_keymap_item_properties_set(kmi);
1006 /* update U.user_keymaps with user key configuration changes */
1007 for(km=wm->userconf->keymaps.first; km; km=km->next) {
1008 /* only diff if the user keymap was modified */
1009 if(wm_keymap_test_and_clear_update(km)) {
1011 defaultmap= wm_keymap_preset(wm, km);
1012 addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1016 wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, km);
1020 /* create user key configuration from preset + addon + user preferences */
1021 for(km=wm->defaultconf->keymaps.first; km; km=km->next) {
1023 defaultmap= wm_keymap_preset(wm, km);
1024 addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1025 usermap= WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
1028 kmn= wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
1031 kmn->modal_items= km->modal_items;
1032 kmn->poll= km->poll;
1035 /* in case of old non-diff keymaps, force extra update to create diffs */
1036 compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
1039 WM_KEYMAP_UPDATE= 0;
1042 WM_keyconfig_update_tag(NULL, NULL);
1043 WM_keyconfig_update(wm);
1047 /********************************* Event Handling *****************************
1048 * Handlers have pointers to the keymap in the default configuration. During
1049 * event handling this function is called to get the keymap from the final
1052 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
1059 /* first user defined keymaps */
1060 km= WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1068 /******************************* Keymap Editor ********************************
1069 * In the keymap editor the user key configuration is edited. */
1071 void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
1073 wmWindowManager *wm = CTX_wm_manager(C);
1074 wmKeyMap *defaultmap, *addonmap;
1080 /* construct default keymap from preset + addons */
1081 defaultmap= wm_keymap_preset(wm, keymap);
1082 addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1085 defaultmap = wm_keymap_copy(defaultmap);
1086 wm_keymap_addon_add(defaultmap, addonmap);
1089 /* find original item */
1090 orig = WM_keymap_item_find_id(defaultmap, kmi->id);
1093 /* restore to original */
1094 if(strcmp(orig->idname, kmi->idname) != 0) {
1095 BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
1096 WM_keymap_properties_reset(kmi, NULL);
1099 if (orig->properties) {
1100 if(kmi->properties) {
1101 IDP_FreeProperty(kmi->properties);
1102 MEM_freeN(kmi->properties);
1103 kmi->properties= NULL;
1106 kmi->properties= IDP_CopyProperty(orig->properties);
1107 kmi->ptr->data= kmi->properties;
1110 kmi->propvalue = orig->propvalue;
1111 kmi->type = orig->type;
1112 kmi->val = orig->val;
1113 kmi->shift = orig->shift;
1114 kmi->ctrl = orig->ctrl;
1115 kmi->alt = orig->alt;
1116 kmi->oskey = orig->oskey;
1117 kmi->keymodifier = orig->keymodifier;
1118 kmi->maptype = orig->maptype;
1120 WM_keyconfig_update_tag(keymap, kmi);
1123 /* free temporary keymap */
1125 WM_keymap_free(defaultmap);
1126 MEM_freeN(defaultmap);
1130 void WM_keymap_restore_to_default(wmKeyMap *keymap, bContext *C)
1132 wmWindowManager *wm = CTX_wm_manager(C);
1135 /* remove keymap from U.user_keymaps and update */
1136 usermap= WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1139 WM_keymap_free(usermap);
1140 BLI_freelinkN(&U.user_keymaps, usermap);
1142 WM_keyconfig_update_tag(NULL, NULL);
1143 WM_keyconfig_update(wm);
1147 wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
1151 for (kmi=keymap->items.first; kmi; kmi=kmi->next) {
1152 if (kmi->id == id) {
1160 /* Guess an appropriate keymap from the operator name */
1161 /* Needs to be kept up to date with Keymap and Operator naming */
1162 wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
1165 SpaceLink *sl = CTX_wm_space_data(C);
1168 if (strstr(opname, "WM_OT")) {
1169 km = WM_keymap_find_all(C, "Window", 0, 0);
1172 else if (strstr(opname, "SCREEN_OT")) {
1173 km = WM_keymap_find_all(C, "Screen", 0, 0);
1176 else if (strstr(opname, "GPENCIL_OT")) {
1177 km = WM_keymap_find_all(C, "Grease Pencil", 0, 0);
1180 else if (strstr(opname, "MARKER_OT")) {
1181 km = WM_keymap_find_all(C, "Markers", 0, 0);
1186 else if (strstr(opname, "VIEW3D_OT")) {
1187 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1189 else if (strstr(opname, "OBJECT_OT")) {
1190 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1195 else if (strstr(opname, "MESH_OT")) {
1196 km = WM_keymap_find_all(C, "Mesh", 0, 0);
1198 /* some mesh operators are active in object mode too, like add-prim */
1199 if(km && km->poll && km->poll((bContext *)C)==0) {
1200 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1203 else if (strstr(opname, "CURVE_OT")) {
1204 km = WM_keymap_find_all(C, "Curve", 0, 0);
1206 /* some curve operators are active in object mode too, like add-prim */
1207 if(km && km->poll && km->poll((bContext *)C)==0) {
1208 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1211 else if (strstr(opname, "ARMATURE_OT")) {
1212 km = WM_keymap_find_all(C, "Armature", 0, 0);
1214 else if (strstr(opname, "POSE_OT")) {
1215 km = WM_keymap_find_all(C, "Pose", 0, 0);
1217 else if (strstr(opname, "SCULPT_OT")) {
1218 km = WM_keymap_find_all(C, "Sculpt", 0, 0);
1220 else if (strstr(opname, "MBALL_OT")) {
1221 km = WM_keymap_find_all(C, "Metaball", 0, 0);
1223 /* some mball operators are active in object mode too, like add-prim */
1224 if(km && km->poll && km->poll((bContext *)C)==0) {
1225 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1228 else if (strstr(opname, "LATTICE_OT")) {
1229 km = WM_keymap_find_all(C, "Lattice", 0, 0);
1231 else if (strstr(opname, "PARTICLE_OT")) {
1232 km = WM_keymap_find_all(C, "Particle", 0, 0);
1234 else if (strstr(opname, "FONT_OT")) {
1235 km = WM_keymap_find_all(C, "Font", 0, 0);
1237 else if (strstr(opname, "PAINT_OT")) {
1239 /* check for relevant mode */
1240 switch(CTX_data_mode_enum(C)) {
1241 case OB_MODE_WEIGHT_PAINT:
1242 km = WM_keymap_find_all(C, "Weight Paint", 0, 0);
1244 case OB_MODE_VERTEX_PAINT:
1245 km = WM_keymap_find_all(C, "Vertex Paint", 0, 0);
1247 case OB_MODE_TEXTURE_PAINT:
1248 km = WM_keymap_find_all(C, "Image Paint", 0, 0);
1252 /* Paint Face Mask */
1253 else if (strstr(opname, "PAINT_OT_face_select")) {
1254 km = WM_keymap_find_all(C, "Face Mask", sl->spacetype, 0);
1257 else if (strstr(opname, "TIME_OT")) {
1258 km = WM_keymap_find_all(C, "Timeline", sl->spacetype, 0);
1261 else if (strstr(opname, "IMAGE_OT")) {
1262 km = WM_keymap_find_all(C, "Image", sl->spacetype, 0);
1265 else if (strstr(opname, "UV_OT")) {
1266 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1269 else if (strstr(opname, "NODE_OT")) {
1270 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1272 /* Animation Editor Channels */
1273 else if (strstr(opname, "ANIM_OT_channels")) {
1274 km = WM_keymap_find_all(C, "Animation Channels", sl->spacetype, 0);
1276 /* Animation Generic - after channels */
1277 else if (strstr(opname, "ANIM_OT")) {
1278 km = WM_keymap_find_all(C, "Animation", 0, 0);
1281 else if (strstr(opname, "GRAPH_OT")) {
1282 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1284 /* Dopesheet Editor */
1285 else if (strstr(opname, "ACTION_OT")) {
1286 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1289 else if (strstr(opname, "NLA_OT")) {
1290 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1293 else if (strstr(opname, "SCRIPT_OT")) {
1294 km = WM_keymap_find_all(C, "Script", sl->spacetype, 0);
1297 else if (strstr(opname, "TEXT_OT")) {
1298 km = WM_keymap_find_all(C, "Text", sl->spacetype, 0);
1301 else if (strstr(opname, "SEQUENCER_OT")) {
1302 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1305 else if (strstr(opname, "CONSOLE_OT")) {
1306 km = WM_keymap_find_all(C, "Console", sl->spacetype, 0);
1309 else if (strstr(opname, "INFO_OT")) {
1310 km = WM_keymap_find_all(C, "Info", sl->spacetype, 0);
1314 else if (strstr(opname, "TRANSFORM_OT")) {
1316 /* check for relevant editor */
1317 switch(sl->spacetype) {
1319 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1322 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1325 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1328 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1331 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1334 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1337 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);