Fix action-zones showing up as shortcuts
[blender.git] / source / blender / windowmanager / intern / wm_keymap.c
1 /*
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2007 Blender Foundation.
20  * All rights reserved.
21  *
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/windowmanager/intern/wm_keymap.c
29  *  \ingroup wm
30  *
31  * Configurable key-maps - add/remove/find/compare/patch...
32  */
33
34 #include <string.h>
35
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"
41 #include "DNA_workspace_types.h"
42
43 #include "MEM_guardedalloc.h"
44 #include "CLG_log.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_math.h"
49
50 #include "BKE_context.h"
51 #include "BKE_idprop.h"
52 #include "BKE_global.h"
53 #include "BKE_main.h"
54 #include "BKE_screen.h"
55 #include "BKE_workspace.h"
56
57 #include "BLT_translation.h"
58
59 #include "RNA_access.h"
60 #include "RNA_enum_types.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64 #include "wm_event_system.h"
65 #include "wm_event_types.h"
66
67
68 struct wmKeyMapItemFind_Params {
69         bool (*filter_fn)(const wmKeyMap *km, const wmKeyMapItem *kmi, void *user_data);
70         void *user_data;
71 };
72
73 /******************************* Keymap Item **********************************
74  * Item in a keymap, that maps from an event to an operator or modal map item */
75
76 static wmKeyMapItem *wm_keymap_item_copy(wmKeyMapItem *kmi)
77 {
78         wmKeyMapItem *kmin = MEM_dupallocN(kmi);
79
80         kmin->prev = kmin->next = NULL;
81         kmin->flag &= ~KMI_UPDATE;
82
83         if (kmin->properties) {
84                 kmin->ptr = MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
85                 WM_operator_properties_create(kmin->ptr, kmin->idname);
86
87                 kmin->properties = IDP_CopyProperty(kmin->properties);
88                 kmin->ptr->data = kmin->properties;
89         }
90         else {
91                 kmin->properties = NULL;
92                 kmin->ptr = NULL;
93         }
94
95         return kmin;
96 }
97
98 static void wm_keymap_item_free(wmKeyMapItem *kmi)
99 {
100         /* not kmi itself */
101         if (kmi->ptr) {
102                 WM_operator_properties_free(kmi->ptr);
103                 MEM_freeN(kmi->ptr);
104                 kmi->ptr = NULL;
105                 kmi->properties = NULL;
106         }
107 }
108
109 static void wm_keymap_item_properties_set(wmKeyMapItem *kmi)
110 {
111         WM_operator_properties_alloc(&(kmi->ptr), &(kmi->properties), kmi->idname);
112         WM_operator_properties_sanitize(kmi->ptr, 1);
113 }
114
115 /**
116  * Similar to #wm_keymap_item_properties_set but checks for the wmOperatorType having changed, see [#38042]
117  */
118 static void wm_keymap_item_properties_update_ot(wmKeyMapItem *kmi)
119 {
120         if (kmi->idname[0] == 0) {
121                 BLI_assert(kmi->ptr == NULL);
122                 return;
123         }
124
125         if (kmi->ptr == NULL) {
126                 wm_keymap_item_properties_set(kmi);
127         }
128         else {
129                 wmOperatorType *ot = WM_operatortype_find(kmi->idname, 0);
130                 if (ot) {
131                         if (ot->srna != kmi->ptr->type) {
132                                 /* matches wm_keymap_item_properties_set but doesn't alloc new ptr */
133                                 WM_operator_properties_create_ptr(kmi->ptr, ot);
134                                 /* 'kmi->ptr->data' NULL'd above, keep using existing properties.
135                                  * Note: the operators property types may have changed,
136                                  * we will need a more comprehensive sanitize function to support this properly.
137                                  */
138                                 if (kmi->properties) {
139                                         kmi->ptr->data = kmi->properties;
140                                 }
141                                 WM_operator_properties_sanitize(kmi->ptr, 1);
142                         }
143                 }
144                 else {
145                         /* zombie keymap item */
146                         wm_keymap_item_free(kmi);
147                 }
148         }
149 }
150
151 static void wm_keyconfig_properties_update_ot(ListBase *km_lb)
152 {
153         wmKeyMap *km;
154         wmKeyMapItem *kmi;
155
156         for (km = km_lb->first; km; km = km->next) {
157                 wmKeyMapDiffItem *kmdi;
158
159                 for (kmi = km->items.first; kmi; kmi = kmi->next) {
160                         wm_keymap_item_properties_update_ot(kmi);
161                 }
162
163                 for (kmdi = km->diff_items.first; kmdi; kmdi = kmdi->next) {
164                         if (kmdi->add_item)
165                                 wm_keymap_item_properties_update_ot(kmdi->add_item);
166                         if (kmdi->remove_item)
167                                 wm_keymap_item_properties_update_ot(kmdi->remove_item);
168                 }
169         }
170 }
171
172 static bool wm_keymap_item_equals_result(wmKeyMapItem *a, wmKeyMapItem *b)
173 {
174         return (STREQ(a->idname, b->idname) &&
175                 /* We do not really care about which Main we pass here, tbh. */
176                 RNA_struct_equals(G_MAIN, a->ptr, b->ptr, RNA_EQ_UNSET_MATCH_NONE) &&
177                 (a->flag & KMI_INACTIVE) == (b->flag & KMI_INACTIVE) &&
178                 a->propvalue == b->propvalue);
179 }
180
181 static bool wm_keymap_item_equals(wmKeyMapItem *a, wmKeyMapItem *b)
182 {
183         return (wm_keymap_item_equals_result(a, b) &&
184                 a->type == b->type &&
185                 a->val == b->val &&
186                 a->shift == b->shift &&
187                 a->ctrl == b->ctrl &&
188                 a->alt == b->alt &&
189                 a->oskey == b->oskey &&
190                 a->keymodifier == b->keymodifier &&
191                 a->maptype == b->maptype);
192 }
193
194 /* properties can be NULL, otherwise the arg passed is used and ownership is given to the kmi */
195 void WM_keymap_properties_reset(wmKeyMapItem *kmi, struct IDProperty *properties)
196 {
197         if (LIKELY(kmi->ptr)) {
198                 WM_operator_properties_free(kmi->ptr);
199                 MEM_freeN(kmi->ptr);
200
201                 kmi->ptr = NULL;
202         }
203
204         kmi->properties = properties;
205
206         wm_keymap_item_properties_set(kmi);
207 }
208
209 int WM_keymap_map_type_get(wmKeyMapItem *kmi)
210 {
211         if (ISTIMER(kmi->type)) {
212                 return KMI_TYPE_TIMER;
213         }
214         if (ISKEYBOARD(kmi->type)) {
215                 return KMI_TYPE_KEYBOARD;
216         }
217         if (ISTWEAK(kmi->type)) {
218                 return KMI_TYPE_TWEAK;
219         }
220         if (ISMOUSE(kmi->type)) {
221                 return KMI_TYPE_MOUSE;
222         }
223         if (ISNDOF(kmi->type)) {
224                 return KMI_TYPE_NDOF;
225         }
226         if (kmi->type == KM_TEXTINPUT) {
227                 return KMI_TYPE_TEXTINPUT;
228         }
229         if (ELEM(kmi->type, TABLET_STYLUS, TABLET_ERASER)) {
230                 return KMI_TYPE_MOUSE;
231         }
232         return KMI_TYPE_KEYBOARD;
233 }
234
235
236 /**************************** Keymap Diff Item *********************************
237  * Item in a diff keymap, used for saving diff of keymaps in user preferences */
238
239 static wmKeyMapDiffItem *wm_keymap_diff_item_copy(wmKeyMapDiffItem *kmdi)
240 {
241         wmKeyMapDiffItem *kmdin = MEM_dupallocN(kmdi);
242
243         kmdin->next = kmdin->prev = NULL;
244         if (kmdi->add_item)
245                 kmdin->add_item = wm_keymap_item_copy(kmdi->add_item);
246         if (kmdi->remove_item)
247                 kmdin->remove_item = wm_keymap_item_copy(kmdi->remove_item);
248
249         return kmdin;
250 }
251
252 static void wm_keymap_diff_item_free(wmKeyMapDiffItem *kmdi)
253 {
254         if (kmdi->remove_item) {
255                 wm_keymap_item_free(kmdi->remove_item);
256                 MEM_freeN(kmdi->remove_item);
257         }
258         if (kmdi->add_item) {
259                 wm_keymap_item_free(kmdi->add_item);
260                 MEM_freeN(kmdi->add_item);
261         }
262 }
263
264 /***************************** Key Configuration ******************************
265  * List of keymaps for all editors, modes, ... . There is a builtin default key
266  * configuration, a user key configuration, and other preset configurations. */
267
268 wmKeyConfig *WM_keyconfig_new(wmWindowManager *wm, const char *idname, bool user_defined)
269 {
270         wmKeyConfig *keyconf = BLI_findstring(&wm->keyconfigs, idname, offsetof(wmKeyConfig, idname));
271         if (keyconf) {
272                 if (keyconf == wm->defaultconf) {
273                         /* For default configuration, we need to keep keymap
274                          * modal items and poll functions intact. */
275                         for (wmKeyMap *km = keyconf->keymaps.first; km; km = km->next) {
276                                 WM_keymap_clear(km);
277                         }
278                 }
279                 else {
280                         /* For user defined key configuration, clear all keymaps. */
281                         WM_keyconfig_clear(keyconf);
282                 }
283
284                 return keyconf;
285         }
286
287         /* Create new configuration. */
288         keyconf = MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
289         BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
290         BLI_addtail(&wm->keyconfigs, keyconf);
291
292         if (user_defined) {
293                 keyconf->flag |= KEYCONF_USER;
294         }
295
296         return keyconf;
297 }
298
299 wmKeyConfig *WM_keyconfig_new_user(wmWindowManager *wm, const char *idname)
300 {
301         return WM_keyconfig_new(wm, idname, true);
302 }
303
304 bool WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
305 {
306         if (BLI_findindex(&wm->keyconfigs, keyconf) != -1) {
307                 if (STREQLEN(U.keyconfigstr, keyconf->idname, sizeof(U.keyconfigstr))) {
308                         BLI_strncpy(U.keyconfigstr, wm->defaultconf->idname, sizeof(U.keyconfigstr));
309                         WM_keyconfig_update_tag(NULL, NULL);
310                 }
311
312                 BLI_remlink(&wm->keyconfigs, keyconf);
313                 WM_keyconfig_free(keyconf);
314
315                 return true;
316         }
317         else {
318                 return false;
319         }
320 }
321
322 void WM_keyconfig_clear(wmKeyConfig *keyconf)
323 {
324         for (wmKeyMap *km = keyconf->keymaps.first; km; km = km->next) {
325                 WM_keymap_clear(km);
326         }
327
328         BLI_freelistN(&keyconf->keymaps);
329 }
330
331 void WM_keyconfig_free(wmKeyConfig *keyconf)
332 {
333         WM_keyconfig_clear(keyconf);
334         MEM_freeN(keyconf);
335 }
336
337 static wmKeyConfig *WM_keyconfig_active(wmWindowManager *wm)
338 {
339         wmKeyConfig *keyconf;
340
341         /* first try from preset */
342         keyconf = BLI_findstring(&wm->keyconfigs, U.keyconfigstr, offsetof(wmKeyConfig, idname));
343         if (keyconf)
344                 return keyconf;
345
346         /* otherwise use default */
347         return wm->defaultconf;
348 }
349
350 void WM_keyconfig_set_active(wmWindowManager *wm, const char *idname)
351 {
352         /* setting a different key configuration as active: we ensure all is
353          * updated properly before and after making the change */
354
355         WM_keyconfig_update(wm);
356
357         BLI_strncpy(U.keyconfigstr, idname, sizeof(U.keyconfigstr));
358
359         WM_keyconfig_update_tag(NULL, NULL);
360         WM_keyconfig_update(wm);
361 }
362
363 /********************************** Keymap *************************************
364  * List of keymap items for one editor, mode, modal operator, ... */
365
366 static wmKeyMap *wm_keymap_new(const char *idname, int spaceid, int regionid)
367 {
368         wmKeyMap *km = MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
369
370         BLI_strncpy(km->idname, idname, KMAP_MAX_NAME);
371         km->spaceid = spaceid;
372         km->regionid = regionid;
373
374         {
375                 const char *owner_id = RNA_struct_state_owner_get();
376                 if (owner_id) {
377                         BLI_strncpy(km->owner_id, owner_id, sizeof(km->owner_id));
378                 }
379         }
380         return km;
381 }
382
383 static wmKeyMap *wm_keymap_copy(wmKeyMap *keymap)
384 {
385         wmKeyMap *keymapn = MEM_dupallocN(keymap);
386         wmKeyMapItem *kmi, *kmin;
387         wmKeyMapDiffItem *kmdi, *kmdin;
388
389         keymapn->modal_items = keymap->modal_items;
390         keymapn->poll = keymap->poll;
391         BLI_listbase_clear(&keymapn->items);
392         keymapn->flag &= ~(KEYMAP_UPDATE | KEYMAP_EXPANDED);
393
394         for (kmdi = keymap->diff_items.first; kmdi; kmdi = kmdi->next) {
395                 kmdin = wm_keymap_diff_item_copy(kmdi);
396                 BLI_addtail(&keymapn->items, kmdin);
397         }
398
399         for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
400                 kmin = wm_keymap_item_copy(kmi);
401                 BLI_addtail(&keymapn->items, kmin);
402         }
403
404         return keymapn;
405 }
406
407 void WM_keymap_clear(wmKeyMap *keymap)
408 {
409         wmKeyMapItem *kmi;
410         wmKeyMapDiffItem *kmdi;
411
412         for (kmdi = keymap->diff_items.first; kmdi; kmdi = kmdi->next)
413                 wm_keymap_diff_item_free(kmdi);
414
415         for (kmi = keymap->items.first; kmi; kmi = kmi->next)
416                 wm_keymap_item_free(kmi);
417
418         BLI_freelistN(&keymap->diff_items);
419         BLI_freelistN(&keymap->items);
420 }
421
422 bool WM_keymap_remove(wmKeyConfig *keyconf, wmKeyMap *keymap)
423 {
424         if (BLI_findindex(&keyconf->keymaps, keymap) != -1) {
425
426                 WM_keymap_clear(keymap);
427                 BLI_remlink(&keyconf->keymaps, keymap);
428                 MEM_freeN(keymap);
429
430                 return true;
431         }
432         else {
433                 return false;
434         }
435 }
436
437
438 bool WM_keymap_poll(bContext *C, wmKeyMap *keymap)
439 {
440         /* If we're tagged, only use compatible. */
441         if (keymap->owner_id[0] != '\0') {
442                 const WorkSpace *workspace = CTX_wm_workspace(C);
443                 if (BKE_workspace_owner_id_check(workspace, keymap->owner_id) == false) {
444                         return false;
445                 }
446         }
447
448         if (keymap->poll != NULL) {
449                 return keymap->poll(C);
450         }
451         return true;
452 }
453
454 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
455 {
456         kmi->type = type;
457         kmi->val = val;
458         kmi->keymodifier = keymodifier;
459
460         if (modifier == KM_ANY) {
461                 kmi->shift = kmi->ctrl = kmi->alt = kmi->oskey = KM_ANY;
462         }
463         else {
464                 kmi->shift = (modifier & KM_SHIFT) ? KM_MOD_FIRST : ((modifier & KM_SHIFT2) ? KM_MOD_SECOND : false);
465                 kmi->ctrl =  (modifier & KM_CTRL)  ? KM_MOD_FIRST : ((modifier & KM_CTRL2)  ? KM_MOD_SECOND : false);
466                 kmi->alt =   (modifier & KM_ALT)   ? KM_MOD_FIRST : ((modifier & KM_ALT2)   ? KM_MOD_SECOND : false);
467                 kmi->oskey = (modifier & KM_OSKEY) ? KM_MOD_FIRST : ((modifier & KM_OSKEY2) ? KM_MOD_SECOND : false);
468         }
469 }
470
471 static void keymap_item_set_id(wmKeyMap *keymap, wmKeyMapItem *kmi)
472 {
473         keymap->kmi_id++;
474         if ((keymap->flag & KEYMAP_USER) == 0) {
475                 kmi->id = keymap->kmi_id;
476         }
477         else {
478                 kmi->id = -keymap->kmi_id; /* User defined keymap entries have negative ids */
479         }
480 }
481
482 /* if item was added, then bail out */
483 wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
484 {
485         wmKeyMapItem *kmi;
486
487         for (kmi = keymap->items.first; kmi; kmi = kmi->next)
488                 if (STREQLEN(kmi->idname, idname, OP_MAX_TYPENAME))
489                         break;
490         if (kmi == NULL) {
491                 kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
492
493                 BLI_addtail(&keymap->items, kmi);
494                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
495
496                 keymap_item_set_id(keymap, kmi);
497
498                 keymap_event_set(kmi, type, val, modifier, keymodifier);
499                 wm_keymap_item_properties_set(kmi);
500         }
501         return kmi;
502 }
503
504 /* always add item */
505 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
506 {
507         wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
508
509         BLI_addtail(&keymap->items, kmi);
510         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
511
512         keymap_event_set(kmi, type, val, modifier, keymodifier);
513         wm_keymap_item_properties_set(kmi);
514
515         keymap_item_set_id(keymap, kmi);
516
517         WM_keyconfig_update_tag(keymap, kmi);
518
519         return kmi;
520 }
521
522 wmKeyMapItem *WM_keymap_add_item_copy(
523         struct wmKeyMap *keymap, wmKeyMapItem *kmi_src)
524 {
525         wmKeyMapItem *kmi_dst = wm_keymap_item_copy(kmi_src);
526
527         BLI_addtail(&keymap->items, kmi_dst);
528
529         keymap_item_set_id(keymap, kmi_dst);
530
531         WM_keyconfig_update_tag(keymap, kmi_dst);
532
533         return kmi_dst;
534 }
535
536 bool WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
537 {
538         if (BLI_findindex(&keymap->items, kmi) != -1) {
539                 if (kmi->ptr) {
540                         WM_operator_properties_free(kmi->ptr);
541                         MEM_freeN(kmi->ptr);
542                 }
543                 BLI_freelinkN(&keymap->items, kmi);
544
545                 WM_keyconfig_update_tag(keymap, NULL);
546                 return true;
547         }
548         else {
549                 return false;
550         }
551 }
552
553 /************************** Keymap Diff and Patch ****************************
554  * Rather than saving the entire keymap for user preferences, we only save a
555  * diff so that changes in the defaults get synced. This system is not perfect
556  * but works better than overriding the keymap entirely when only few items
557  * are changed. */
558
559 static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
560 {
561         wmKeyMapItem *kmi, *kmin;
562
563         for (kmi = addonmap->items.first; kmi; kmi = kmi->next) {
564                 kmin = wm_keymap_item_copy(kmi);
565                 keymap_item_set_id(keymap, kmin);
566                 BLI_addhead(&keymap->items, kmin);
567         }
568 }
569
570 static wmKeyMapItem *wm_keymap_find_item_equals(wmKeyMap *km, wmKeyMapItem *needle)
571 {
572         wmKeyMapItem *kmi;
573
574         for (kmi = km->items.first; kmi; kmi = kmi->next)
575                 if (wm_keymap_item_equals(kmi, needle))
576                         return kmi;
577
578         return NULL;
579 }
580
581 static wmKeyMapItem *wm_keymap_find_item_equals_result(wmKeyMap *km, wmKeyMapItem *needle)
582 {
583         wmKeyMapItem *kmi;
584
585         for (kmi = km->items.first; kmi; kmi = kmi->next)
586                 if (wm_keymap_item_equals_result(kmi, needle))
587                         return kmi;
588
589         return NULL;
590 }
591
592 static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km, wmKeyMap *orig_km, wmKeyMap *addon_km)
593 {
594         wmKeyMapItem *kmi, *to_kmi, *orig_kmi;
595         wmKeyMapDiffItem *kmdi;
596
597         for (kmi = from_km->items.first; kmi; kmi = kmi->next) {
598                 to_kmi = WM_keymap_item_find_id(to_km, kmi->id);
599
600                 if (!to_kmi) {
601                         /* remove item */
602                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
603                         kmdi->remove_item = wm_keymap_item_copy(kmi);
604                         BLI_addtail(&diff_km->diff_items, kmdi);
605                 }
606                 else if (to_kmi && !wm_keymap_item_equals(kmi, to_kmi)) {
607                         /* replace item */
608                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
609                         kmdi->remove_item = wm_keymap_item_copy(kmi);
610                         kmdi->add_item = wm_keymap_item_copy(to_kmi);
611                         BLI_addtail(&diff_km->diff_items, kmdi);
612                 }
613
614                 /* sync expanded flag back to original so we don't loose it on repatch */
615                 if (to_kmi) {
616                         orig_kmi = WM_keymap_item_find_id(orig_km, kmi->id);
617
618                         if (!orig_kmi && addon_km)
619                                 orig_kmi = wm_keymap_find_item_equals(addon_km, kmi);
620
621                         if (orig_kmi) {
622                                 orig_kmi->flag &= ~KMI_EXPANDED;
623                                 orig_kmi->flag |= (to_kmi->flag & KMI_EXPANDED);
624                         }
625                 }
626         }
627
628         for (kmi = to_km->items.first; kmi; kmi = kmi->next) {
629                 if (kmi->id < 0) {
630                         /* add item */
631                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
632                         kmdi->add_item = wm_keymap_item_copy(kmi);
633                         BLI_addtail(&diff_km->diff_items, kmdi);
634                 }
635         }
636 }
637
638 static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
639 {
640         wmKeyMapDiffItem *kmdi;
641         wmKeyMapItem *kmi_remove, *kmi_add;
642
643         for (kmdi = diff_km->diff_items.first; kmdi; kmdi = kmdi->next) {
644                 /* find item to remove */
645                 kmi_remove = NULL;
646                 if (kmdi->remove_item) {
647                         kmi_remove = wm_keymap_find_item_equals(km, kmdi->remove_item);
648                         if (!kmi_remove)
649                                 kmi_remove = wm_keymap_find_item_equals_result(km, kmdi->remove_item);
650                 }
651
652                 /* add item */
653                 if (kmdi->add_item) {
654                         /* Do not re-add an already existing keymap item! See T42088. */
655                         /* We seek only for exact copy here! See T42137. */
656                         kmi_add = wm_keymap_find_item_equals(km, kmdi->add_item);
657
658                         /* If kmi_add is same as kmi_remove (can happen in some cases, typically when we got kmi_remove
659                          * from wm_keymap_find_item_equals_result()), no need to add or remove anything, see T45579. */
660                         /* Note: This typically happens when we apply user-defined keymap diff to a base one that was exported
661                          *       with that customized keymap already. In that case:
662                          *         - wm_keymap_find_item_equals(km, kmdi->remove_item) finds nothing (because actual shortcut of
663                          *           current base does not match kmdi->remove_item any more).
664                          *         - wm_keymap_find_item_equals_result(km, kmdi->remove_item) finds the current kmi from
665                          *           base keymap (because it does exactly the same thing).
666                          *         - wm_keymap_find_item_equals(km, kmdi->add_item) finds the same kmi, since base keymap was
667                          *           exported with that user-defined shortcut already!
668                          *       Maybe we should rather keep user-defined keymaps specific to a given base one? */
669                         if (kmi_add != NULL && kmi_add == kmi_remove) {
670                                 kmi_remove = NULL;
671                         }
672                         /* only if nothing to remove or item to remove found */
673                         else if (!kmi_add && (!kmdi->remove_item || kmi_remove)) {
674                                 kmi_add = wm_keymap_item_copy(kmdi->add_item);
675                                 kmi_add->flag |= KMI_USER_MODIFIED;
676
677                                 if (kmi_remove) {
678                                         kmi_add->flag &= ~KMI_EXPANDED;
679                                         kmi_add->flag |= (kmi_remove->flag & KMI_EXPANDED);
680                                         kmi_add->id = kmi_remove->id;
681                                         BLI_insertlinkbefore(&km->items, kmi_remove, kmi_add);
682                                 }
683                                 else {
684                                         keymap_item_set_id(km, kmi_add);
685                                         BLI_addtail(&km->items, kmi_add);
686                                 }
687                         }
688                 }
689
690                 /* remove item */
691                 if (kmi_remove) {
692                         wm_keymap_item_free(kmi_remove);
693                         BLI_freelinkN(&km->items, kmi_remove);
694                 }
695         }
696 }
697
698 static wmKeyMap *wm_keymap_patch_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *usermap)
699 {
700         wmKeyMap *km;
701         int expanded = 0;
702
703         /* remove previous keymap in list, we will replace it */
704         km = WM_keymap_list_find(lb, defaultmap->idname, defaultmap->spaceid, defaultmap->regionid);
705         if (km) {
706                 expanded = (km->flag & (KEYMAP_EXPANDED | KEYMAP_CHILDREN_EXPANDED));
707                 WM_keymap_clear(km);
708                 BLI_freelinkN(lb, km);
709         }
710
711         /* copy new keymap from an existing one */
712         if (usermap && !(usermap->flag & KEYMAP_DIFF)) {
713                 /* for compatibility with old user preferences with non-diff
714                  * keymaps we override the original entirely */
715                 wmKeyMapItem *kmi, *orig_kmi;
716
717                 km = wm_keymap_copy(usermap);
718
719                 /* try to find corresponding id's for items */
720                 for (kmi = km->items.first; kmi; kmi = kmi->next) {
721                         orig_kmi = wm_keymap_find_item_equals(defaultmap, kmi);
722                         if (!orig_kmi)
723                                 orig_kmi = wm_keymap_find_item_equals_result(defaultmap, kmi);
724
725                         if (orig_kmi)
726                                 kmi->id = orig_kmi->id;
727                         else
728                                 kmi->id = -(km->kmi_id++);
729                 }
730
731                 km->flag |= KEYMAP_UPDATE; /* update again to create diff */
732         }
733         else
734                 km = wm_keymap_copy(defaultmap);
735
736         /* add addon keymap items */
737         if (addonmap)
738                 wm_keymap_addon_add(km, addonmap);
739
740         /* tag as being user edited */
741         if (usermap)
742                 km->flag |= KEYMAP_USER_MODIFIED;
743         km->flag |= KEYMAP_USER | expanded;
744
745         /* apply user changes of diff keymap */
746         if (usermap && (usermap->flag & KEYMAP_DIFF))
747                 wm_keymap_patch(km, usermap);
748
749         /* add to list */
750         BLI_addtail(lb, km);
751
752         return km;
753 }
754
755 static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *km)
756 {
757         wmKeyMap *diffmap, *prevmap, *origmap;
758
759         /* create temporary default + addon keymap for diff */
760         origmap = defaultmap;
761
762         if (addonmap) {
763                 defaultmap = wm_keymap_copy(defaultmap);
764                 wm_keymap_addon_add(defaultmap, addonmap);
765         }
766
767         /* remove previous diff keymap in list, we will replace it */
768         prevmap = WM_keymap_list_find(lb, km->idname, km->spaceid, km->regionid);
769         if (prevmap) {
770                 WM_keymap_clear(prevmap);
771                 BLI_freelinkN(lb, prevmap);
772         }
773
774         /* create diff keymap */
775         diffmap = wm_keymap_new(km->idname, km->spaceid, km->regionid);
776         diffmap->flag |= KEYMAP_DIFF;
777         if (defaultmap->flag & KEYMAP_MODAL)
778                 diffmap->flag |= KEYMAP_MODAL;
779         wm_keymap_diff(diffmap, defaultmap, km, origmap, addonmap);
780
781         /* add to list if not empty */
782         if (diffmap->diff_items.first) {
783                 BLI_addtail(lb, diffmap);
784         }
785         else {
786                 WM_keymap_clear(diffmap);
787                 MEM_freeN(diffmap);
788         }
789
790         /* free temporary default map */
791         if (addonmap) {
792                 WM_keymap_clear(defaultmap);
793                 MEM_freeN(defaultmap);
794         }
795 }
796
797 /* ****************** storage in WM ************ */
798
799 /* name id's are for storing general or multiple keymaps,
800  * space/region ids are same as DNA_space_types.h */
801 /* gets freed in wm.c */
802
803 wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int regionid)
804 {
805         wmKeyMap *km;
806
807         for (km = lb->first; km; km = km->next)
808                 if (km->spaceid == spaceid && km->regionid == regionid)
809                         if (STREQLEN(idname, km->idname, KMAP_MAX_NAME))
810                                 return km;
811
812         return NULL;
813 }
814
815 wmKeyMap *WM_keymap_list_find_spaceid_or_empty(ListBase *lb, const char *idname, int spaceid, int regionid)
816 {
817         wmKeyMap *km;
818
819         for (km = lb->first; km; km = km->next)
820                 if (ELEM(km->spaceid, spaceid, SPACE_EMPTY) && km->regionid == regionid)
821                         if (STREQLEN(idname, km->idname, KMAP_MAX_NAME))
822                                 return km;
823
824         return NULL;
825 }
826
827 wmKeyMap *WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
828 {
829         wmKeyMap *km = WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
830
831         if (km == NULL) {
832                 km = wm_keymap_new(idname, spaceid, regionid);
833                 BLI_addtail(&keyconf->keymaps, km);
834
835                 WM_keyconfig_update_tag(km, NULL);
836         }
837
838         return km;
839 }
840
841 wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid, int regionid)
842 {
843         wmWindowManager *wm = CTX_wm_manager(C);
844
845         return WM_keymap_list_find(&wm->userconf->keymaps, idname, spaceid, regionid);
846 }
847
848 wmKeyMap *WM_keymap_find_all_spaceid_or_empty(const bContext *C, const char *idname, int spaceid, int regionid)
849 {
850         wmWindowManager *wm = CTX_wm_manager(C);
851
852         return WM_keymap_list_find_spaceid_or_empty(&wm->userconf->keymaps, idname, spaceid, regionid);
853 }
854
855 /* ****************** modal keymaps ************ */
856
857 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
858
859 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
860 {
861         wmKeyMap *km = WM_keymap_ensure(keyconf, idname, 0, 0);
862         km->flag |= KEYMAP_MODAL;
863
864         /* init modal items from default config */
865         wmWindowManager *wm = G_MAIN->wm.first;
866         if (wm->defaultconf && wm->defaultconf != keyconf) {
867                 wmKeyMap *defaultkm = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
868
869                 if (defaultkm) {
870                         km->modal_items = defaultkm->modal_items;
871                         km->poll = defaultkm->poll;
872                 }
873         }
874
875         if (items) {
876                 km->modal_items = items;
877         }
878
879         return km;
880 }
881
882 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, const char *idname)
883 {
884         wmKeyMap *km;
885
886         for (km = keyconf->keymaps.first; km; km = km->next)
887                 if (km->flag & KEYMAP_MODAL)
888                         if (STREQLEN(idname, km->idname, KMAP_MAX_NAME))
889                                 break;
890
891         return km;
892 }
893
894
895 wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
896 {
897         wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
898
899         BLI_addtail(&km->items, kmi);
900         kmi->propvalue = value;
901
902         keymap_event_set(kmi, type, val, modifier, keymodifier);
903
904         keymap_item_set_id(km, kmi);
905
906         WM_keyconfig_update_tag(km, kmi);
907
908         return kmi;
909 }
910
911 wmKeyMapItem *WM_modalkeymap_add_item_str(wmKeyMap *km, int type, int val, int modifier, int keymodifier, const char *value)
912 {
913         wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
914
915         BLI_addtail(&km->items, kmi);
916         BLI_strncpy(kmi->propvalue_str, value, sizeof(kmi->propvalue_str));
917
918         keymap_event_set(kmi, type, val, modifier, keymodifier);
919
920         keymap_item_set_id(km, kmi);
921
922         WM_keyconfig_update_tag(km, kmi);
923
924         return kmi;
925 }
926
927 static wmKeyMapItem *wm_modalkeymap_find_propvalue_iter(wmKeyMap *km, wmKeyMapItem *kmi, const int propvalue)
928 {
929         if (km->flag & KEYMAP_MODAL) {
930                 kmi = kmi ? kmi->next : km->items.first;
931                 for (; kmi; kmi = kmi->next) {
932                         if (kmi->propvalue == propvalue) {
933                                 return kmi;
934                         }
935                 }
936         }
937         else {
938                 BLI_assert(!"called with non modal keymap");
939         }
940
941         return NULL;
942 }
943
944 wmKeyMapItem *WM_modalkeymap_find_propvalue(wmKeyMap *km, const int propvalue)
945 {
946         return wm_modalkeymap_find_propvalue_iter(km, NULL, propvalue);
947 }
948
949 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
950 {
951         wmOperatorType *ot = WM_operatortype_find(opname, 0);
952
953         if (ot) {
954                 ot->modalkeymap = km;
955         }
956         else {
957                 CLOG_ERROR(WM_LOG_KEYMAPS, "unknown operator '%s'", opname);
958         }
959 }
960
961 static void wm_user_modal_keymap_set_items(wmWindowManager *wm, wmKeyMap *km)
962 {
963         /* here we convert propvalue string values delayed, due to python keymaps
964          * being created before the actual modal keymaps, so no modal_items */
965         wmKeyMap *defaultkm;
966         wmKeyMapItem *kmi;
967         int propvalue;
968
969         if (km && (km->flag & KEYMAP_MODAL) && !km->modal_items) {
970                 if (wm->defaultconf == NULL) {
971                         return;
972                 }
973
974                 defaultkm = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
975
976                 if (!defaultkm)
977                         return;
978
979                 km->modal_items = defaultkm->modal_items;
980                 km->poll = defaultkm->poll;
981
982                 if (km->modal_items) {
983                         for (kmi = km->items.first; kmi; kmi = kmi->next) {
984                                 if (kmi->propvalue_str[0]) {
985                                         if (RNA_enum_value_from_id(km->modal_items, kmi->propvalue_str, &propvalue))
986                                                 kmi->propvalue = propvalue;
987                                         kmi->propvalue_str[0] = '\0';
988                                 }
989                         }
990                 }
991         }
992 }
993
994 /* ***************** get string from key events **************** */
995
996 const char *WM_key_event_string(const short type, const bool compact)
997 {
998         const EnumPropertyItem *it;
999         const int i = RNA_enum_from_value(rna_enum_event_type_items, (int)type);
1000
1001         if (i == -1) {
1002                 return "";
1003         }
1004         it = &rna_enum_event_type_items[i];
1005
1006         /* We first try enum items' description (abused as shortname here), and fall back to usual name if empty. */
1007         if (compact && it->description[0]) {
1008                 /* XXX No context for enum descriptions... In practice shall not be an issue though. */
1009                 return IFACE_(it->description);
1010         }
1011
1012         return CTX_IFACE_(BLT_I18NCONTEXT_UI_EVENTS, it->name);
1013 }
1014
1015 /* TODO: also support (some) value, like e.g. double-click? */
1016 int WM_keymap_item_raw_to_string(
1017         const short shift, const short ctrl, const short alt, const short oskey,
1018         const short keymodifier, const short val, const short type, const bool compact,
1019         char *result, const int result_len)
1020 {
1021 #define ADD_SEP if (p != buf) *p++ = ' '; (void)0
1022
1023         char buf[128];
1024         char *p = buf;
1025
1026         buf[0] = '\0';
1027
1028         /* TODO: support order (KM_SHIFT vs. KM_SHIFT2) ? */
1029         if (shift == KM_ANY &&
1030             ctrl == KM_ANY &&
1031             alt == KM_ANY &&
1032             oskey == KM_ANY)
1033         {
1034                 /* Don't show anything for any mapping. */
1035         }
1036         else {
1037                 if (shift) {
1038                         ADD_SEP;
1039                         p += BLI_strcpy_rlen(p, IFACE_("Shift"));
1040                 }
1041
1042                 if (ctrl) {
1043                         ADD_SEP;
1044                         p += BLI_strcpy_rlen(p, IFACE_("Ctrl"));
1045                 }
1046
1047                 if (alt) {
1048                         ADD_SEP;
1049                         p += BLI_strcpy_rlen(p, IFACE_("Alt"));
1050                 }
1051
1052                 if (oskey) {
1053                         ADD_SEP;
1054                         p += BLI_strcpy_rlen(p, IFACE_("Cmd"));
1055                 }
1056         }
1057
1058         if (keymodifier) {
1059                 ADD_SEP;
1060                 p += BLI_strcpy_rlen(p, WM_key_event_string(keymodifier, compact));
1061         }
1062
1063         if (type) {
1064                 ADD_SEP;
1065                 if (val == KM_DBL_CLICK) {
1066                         p += BLI_strcpy_rlen(p, IFACE_("dbl-"));
1067                 }
1068                 p += BLI_strcpy_rlen(p, WM_key_event_string(type, compact));
1069         }
1070
1071         /* We assume size of buf is enough to always store any possible shortcut, but let's add a debug check about it! */
1072         BLI_assert(p - buf < sizeof(buf));
1073
1074         /* We need utf8 here, otherwise we may 'cut' some unicode chars like arrows... */
1075         return BLI_strncpy_utf8_rlen(result, buf, result_len);
1076
1077 #undef ADD_SEP
1078 }
1079
1080 int WM_keymap_item_to_string(
1081         wmKeyMapItem *kmi, const bool compact,
1082         char *result, const int result_len)
1083 {
1084         return WM_keymap_item_raw_to_string(
1085                 kmi->shift, kmi->ctrl, kmi->alt, kmi->oskey, kmi->keymodifier, kmi->val, kmi->type,
1086                 compact, result, result_len);
1087 }
1088
1089 int WM_modalkeymap_items_to_string(
1090         wmKeyMap *km, const int propvalue, const bool compact,
1091         char *result, const int result_len)
1092 {
1093         int totlen = 0;
1094         bool add_sep = false;
1095
1096         if (km) {
1097                 wmKeyMapItem *kmi;
1098
1099                 /* Find all shortcuts related to that propvalue! */
1100                 for (kmi = WM_modalkeymap_find_propvalue(km, propvalue);
1101                      kmi && totlen < (result_len - 2);
1102                      kmi = wm_modalkeymap_find_propvalue_iter(km, kmi, propvalue))
1103                 {
1104                         if (add_sep) {
1105                                 result[totlen++] = '/';
1106                                 result[totlen] = '\0';
1107                         }
1108                         else {
1109                                 add_sep = true;
1110                         }
1111                         totlen += WM_keymap_item_to_string(kmi, compact, &result[totlen], result_len - totlen);
1112                 }
1113         }
1114
1115         return totlen;
1116 }
1117
1118 int WM_modalkeymap_operator_items_to_string(
1119         wmOperatorType *ot, const int propvalue, const bool compact,
1120         char *result, const int result_len)
1121 {
1122         return WM_modalkeymap_items_to_string(ot->modalkeymap, propvalue, compact, result, result_len);
1123 }
1124
1125 char *WM_modalkeymap_operator_items_to_string_buf(
1126         wmOperatorType *ot, const int propvalue, const bool compact,
1127         const int max_len, int *r_available_len, char **r_result)
1128 {
1129         char *ret = *r_result;
1130
1131         if (*r_available_len > 1) {
1132                 int used_len = WM_modalkeymap_operator_items_to_string(
1133                         ot, propvalue, compact, ret, min_ii(*r_available_len, max_len)) + 1;
1134
1135                 *r_available_len -= used_len;
1136                 *r_result += used_len;
1137                 if (*r_available_len == 0) {
1138                         (*r_result)--;  /* So that *result keeps pointing on a valid char, we'll stay on it anyway. */
1139                 }
1140         }
1141         else {
1142                 *ret = '\0';
1143         }
1144
1145         return ret;
1146 }
1147
1148 static wmKeyMapItem *wm_keymap_item_find_handlers(
1149         const bContext *C, ListBase *handlers, const char *opname, int UNUSED(opcontext),
1150         IDProperty *properties, const bool is_strict,
1151         const struct wmKeyMapItemFind_Params *params,
1152         wmKeyMap **r_keymap)
1153 {
1154         wmWindowManager *wm = CTX_wm_manager(C);
1155         wmEventHandler *handler;
1156         wmKeyMap *keymap;
1157         wmKeyMapItem *kmi;
1158
1159         /* find keymap item in handlers */
1160         for (handler = handlers->first; handler; handler = handler->next) {
1161                 keymap = WM_keymap_active(wm, handler->keymap);
1162
1163                 if (keymap && WM_keymap_poll((bContext *)C, keymap)) {
1164                         for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1165                                 /* skip disabled keymap items [T38447] */
1166                                 if (kmi->flag & KMI_INACTIVE) {
1167                                         continue;
1168                                 }
1169
1170                                 bool kmi_match = false;
1171
1172                                 if (STREQ(kmi->idname, opname)) {
1173                                         if (properties) {
1174                                                 /* example of debugging keymaps */
1175 #if 0
1176                                                 if (kmi->ptr) {
1177                                                         if (STREQ("MESH_OT_rip_move", opname)) {
1178                                                                 printf("OPERATOR\n");
1179                                                                 IDP_print(properties);
1180                                                                 printf("KEYMAP\n");
1181                                                                 IDP_print(kmi->ptr->data);
1182                                                         }
1183                                                 }
1184 #endif
1185
1186                                                 if (kmi->ptr && IDP_EqualsProperties_ex(properties, kmi->ptr->data, is_strict)) {
1187                                                         kmi_match = true;
1188                                                 }
1189                                                 /* Debug only, helps spotting mismatches between menu entries and shortcuts! */
1190                                                 else if (G.debug & G_DEBUG_WM) {
1191                                                         if (is_strict && kmi->ptr) {
1192                                                                 wmOperatorType *ot = WM_operatortype_find(opname, true);
1193                                                                 if (ot) {
1194                                                                         /* make a copy of the properties and set unset ones to their default values. */
1195                                                                         PointerRNA opptr;
1196                                                                         IDProperty *properties_default = IDP_CopyProperty(kmi->ptr->data);
1197
1198                                                                         RNA_pointer_create(NULL, ot->srna, properties_default, &opptr);
1199                                                                         WM_operator_properties_default(&opptr, true);
1200
1201                                                                         if (IDP_EqualsProperties_ex(properties, properties_default, is_strict)) {
1202                                                                                 char kmi_str[128];
1203                                                                                 WM_keymap_item_to_string(kmi, false, kmi_str, sizeof(kmi_str));
1204                                                                                 /* Note gievn properties could come from other things than menu entry... */
1205                                                                                 printf("%s: Some set values in menu entry match default op values, "
1206                                                                                        "this might not be desired!\n", opname);
1207                                                                                 printf("\tkm: '%s', kmi: '%s'\n", keymap->idname, kmi_str);
1208 #ifndef NDEBUG
1209 #ifdef WITH_PYTHON
1210                                                                                 printf("OPERATOR\n");
1211                                                                                 IDP_print(properties);
1212                                                                                 printf("KEYMAP\n");
1213                                                                                 IDP_print(kmi->ptr->data);
1214 #endif
1215 #endif
1216                                                                                 printf("\n");
1217                                                                         }
1218
1219                                                                         IDP_FreeProperty(properties_default);
1220                                                                         MEM_freeN(properties_default);
1221                                                                 }
1222                                                         }
1223                                                 }
1224                                         }
1225                                         else {
1226                                                 kmi_match = true;
1227                                         }
1228
1229                                         if (kmi_match) {
1230                                                 if ((params == NULL) || params->filter_fn(keymap, kmi, params->user_data)) {
1231                                                         if (r_keymap) {
1232                                                                 *r_keymap = keymap;
1233                                                         }
1234                                                         return kmi;
1235                                                 }
1236                                         }
1237                                 }
1238                         }
1239                 }
1240         }
1241
1242         /* ensure un-initialized keymap is never used */
1243         if (r_keymap) *r_keymap = NULL;
1244         return NULL;
1245 }
1246
1247 static wmKeyMapItem *wm_keymap_item_find_props(
1248         const bContext *C, const char *opname, int opcontext,
1249         IDProperty *properties, const bool is_strict,
1250         const struct wmKeyMapItemFind_Params *params,
1251         wmKeyMap **r_keymap)
1252 {
1253         wmWindow *win = CTX_wm_window(C);
1254         ScrArea *sa = CTX_wm_area(C);
1255         ARegion *ar = CTX_wm_region(C);
1256         wmKeyMapItem *found = NULL;
1257
1258         /* look into multiple handler lists to find the item */
1259         if (win) {
1260                 found = wm_keymap_item_find_handlers(
1261                         C, &win->modalhandlers, opname, opcontext, properties, is_strict, params, r_keymap);
1262                 if (found == NULL) {
1263                         found = wm_keymap_item_find_handlers(
1264                                 C, &win->handlers, opname, opcontext, properties, is_strict, params, r_keymap);
1265                 }
1266         }
1267
1268         if (sa && found == NULL)
1269                 found = wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, is_strict, params, r_keymap);
1270
1271         if (found == NULL) {
1272                 if (ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
1273                         if (sa) {
1274                                 if (!(ar && ar->regiontype == RGN_TYPE_WINDOW))
1275                                         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
1276
1277                                 if (ar)
1278                                         found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, params, r_keymap);
1279                         }
1280                 }
1281                 else if (ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
1282                         if (!(ar && ar->regiontype == RGN_TYPE_CHANNELS))
1283                                 ar = BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
1284
1285                         if (ar)
1286                                 found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, params, r_keymap);
1287                 }
1288                 else if (ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
1289                         if (!(ar && ar->regiontype == RGN_TYPE_PREVIEW))
1290                                 ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
1291
1292                         if (ar)
1293                                 found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, params, r_keymap);
1294                 }
1295                 else {
1296                         if (ar)
1297                                 found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, params, r_keymap);
1298                 }
1299         }
1300
1301         return found;
1302 }
1303
1304 static wmKeyMapItem *wm_keymap_item_find(
1305         const bContext *C, const char *opname, int opcontext,
1306         IDProperty *properties, bool is_strict,
1307         const struct wmKeyMapItemFind_Params *params,
1308         wmKeyMap **r_keymap)
1309 {
1310         wmKeyMapItem *found;
1311
1312         /* XXX Hack! Macro operators in menu entry have their whole props defined, which is not the case for
1313          *     relevant keymap entries. Could be good to check and harmonize this, but for now always
1314          *     compare non-strict in this case.
1315          */
1316         wmOperatorType *ot = WM_operatortype_find(opname, true);
1317         if (ot) {
1318                 is_strict = is_strict && ((ot->flag & OPTYPE_MACRO) == 0);
1319         }
1320
1321         found = wm_keymap_item_find_props(C, opname, opcontext, properties, is_strict, params, r_keymap);
1322
1323         /* This block is *only* useful in one case: when op uses an enum menu in its prop member
1324          * (then, we want to rerun a comparison with that 'prop' unset). Note this remains brittle,
1325          * since now any enum prop may be used in UI (specified by name), ot->prop is not so much used...
1326          * Otherwise:
1327          *     * If non-strict, unset properties always match set ones in IDP_EqualsProperties_ex.
1328          *     * If strict, unset properties never match set ones in IDP_EqualsProperties_ex,
1329          *       and we do not want that to change (else we get things like T41757)!
1330          * ...so in either case, re-running a comparison with unset props set to default is useless.
1331          */
1332         if (!found && properties) {
1333                 if (ot && ot->prop) {  /* XXX Shall we also check ot->prop is actually an enum? */
1334                         /* make a copy of the properties and unset the 'ot->prop' one if set. */
1335                         PointerRNA opptr;
1336                         IDProperty *properties_temp = IDP_CopyProperty(properties);
1337
1338                         RNA_pointer_create(NULL, ot->srna, properties_temp, &opptr);
1339
1340                         if (RNA_property_is_set(&opptr, ot->prop)) {
1341                                 /* for operator that has enum menu, unset it so its value does not affect comparison result */
1342                                 RNA_property_unset(&opptr, ot->prop);
1343
1344                                 found = wm_keymap_item_find_props(C, opname, opcontext, properties_temp, is_strict, params, r_keymap);
1345                         }
1346
1347                         IDP_FreeProperty(properties_temp);
1348                         MEM_freeN(properties_temp);
1349                 }
1350         }
1351
1352         /* Debug only, helps spotting mismatches between menu entries and shortcuts! */
1353         if (G.debug & G_DEBUG_WM) {
1354                 if (!found && is_strict && properties) {
1355                         wmKeyMap *km;
1356                         wmKeyMapItem *kmi;
1357                         if (ot) {
1358                                 /* make a copy of the properties and set unset ones to their default values. */
1359                                 PointerRNA opptr;
1360                                 IDProperty *properties_default = IDP_CopyProperty(properties);
1361
1362                                 RNA_pointer_create(NULL, ot->srna, properties_default, &opptr);
1363                                 WM_operator_properties_default(&opptr, true);
1364
1365                                 kmi = wm_keymap_item_find_props(C, opname, opcontext, properties_default, is_strict, params, &km);
1366                                 if (kmi) {
1367                                         char kmi_str[128];
1368                                         WM_keymap_item_to_string(kmi, false, kmi_str, sizeof(kmi_str));
1369                                         printf("%s: Some set values in keymap entry match default op values, "
1370                                                "this might not be desired!\n", opname);
1371                                         printf("\tkm: '%s', kmi: '%s'\n", km->idname, kmi_str);
1372 #ifndef NDEBUG
1373 #ifdef WITH_PYTHON
1374                                         printf("OPERATOR\n");
1375                                         IDP_print(properties);
1376                                         printf("KEYMAP\n");
1377                                         IDP_print(kmi->ptr->data);
1378 #endif
1379 #endif
1380                                         printf("\n");
1381                                 }
1382
1383                                 IDP_FreeProperty(properties_default);
1384                                 MEM_freeN(properties_default);
1385                         }
1386                 }
1387         }
1388
1389         return found;
1390 }
1391
1392 static bool kmi_filter_is_visible(const wmKeyMap *UNUSED(km), const wmKeyMapItem *kmi, void *UNUSED(user_data))
1393 {
1394         return ((WM_key_event_string(kmi->type, false)[0] != '\0') &&
1395                 (IS_EVENT_ACTIONZONE(kmi->type) == false));
1396 }
1397
1398 char *WM_key_event_operator_string(
1399         const bContext *C, const char *opname, int opcontext,
1400         IDProperty *properties, const bool is_strict,
1401         char *result, const int result_len)
1402 {
1403         wmKeyMapItem *kmi = wm_keymap_item_find(
1404                 C, opname, opcontext, properties, is_strict,
1405                 &(struct wmKeyMapItemFind_Params){
1406                     .filter_fn = kmi_filter_is_visible,
1407                     .user_data = NULL,
1408                 },
1409                 NULL);
1410         if (kmi) {
1411                 WM_keymap_item_to_string(kmi, false, result, result_len);
1412                 return result;
1413         }
1414
1415         return NULL;
1416 }
1417
1418 static bool kmi_filter_is_visible_hotkey(const wmKeyMap *km, const wmKeyMapItem *kmi, void *user_data)
1419 {
1420         return (ISHOTKEY(kmi->type) && kmi_filter_is_visible(km, kmi, user_data));
1421 }
1422
1423 wmKeyMapItem *WM_key_event_operator(
1424         const bContext *C, const char *opname, int opcontext,
1425         IDProperty *properties, const bool is_hotkey,
1426         wmKeyMap **r_keymap)
1427 {
1428         return wm_keymap_item_find(
1429                 C, opname, opcontext, properties, true,
1430                 &(struct wmKeyMapItemFind_Params){
1431                     .filter_fn = is_hotkey ? kmi_filter_is_visible_hotkey : kmi_filter_is_visible,
1432                     .user_data = NULL,
1433                 },
1434                 r_keymap);
1435 }
1436
1437 bool WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
1438 {
1439         int k1type, k2type;
1440
1441         if (k1->flag & KMI_INACTIVE || k2->flag & KMI_INACTIVE)
1442                 return 0;
1443
1444         /* take event mapping into account */
1445         k1type = WM_userdef_event_map(k1->type);
1446         k2type = WM_userdef_event_map(k2->type);
1447
1448         if (k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
1449                 return 0;
1450
1451         if (k1->val != KM_ANY && k2->val != KM_ANY) {
1452                 /* take click, press, release conflict into account */
1453                 if (k1->val == KM_CLICK && ELEM(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
1454                         return 0;
1455                 if (k2->val == KM_CLICK && ELEM(k1->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
1456                         return 0;
1457                 if (k1->val != k2->val)
1458                         return 0;
1459         }
1460
1461         if (k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
1462                 return 0;
1463
1464         if (k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
1465                 return 0;
1466
1467         if (k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
1468                 return 0;
1469
1470         if (k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
1471                 return 0;
1472
1473         if (k1->keymodifier != k2->keymodifier)
1474                 return 0;
1475
1476         return 1;
1477 }
1478
1479 /************************* Update Final Configuration *************************
1480  * On load or other changes, the final user key configuration is rebuilt from
1481  * the preset, addon and user preferences keymaps. We also test if the final
1482  * configuration changed and write the changes to the user preferences. */
1483
1484 /* so operator removal can trigger update */
1485 enum {
1486         WM_KEYMAP_UPDATE_RECONFIGURE    = (1 << 0),
1487
1488         /* ensure all wmKeyMap have their operator types validated after removing an operator */
1489         WM_KEYMAP_UPDATE_OPERATORTYPE   = (1 << 1),
1490 };
1491
1492 static char wm_keymap_update_flag = 0;
1493
1494 void WM_keyconfig_update_tag(wmKeyMap *km, wmKeyMapItem *kmi)
1495 {
1496         /* quick tag to do delayed keymap updates */
1497         wm_keymap_update_flag |= WM_KEYMAP_UPDATE_RECONFIGURE;
1498
1499         if (km)
1500                 km->flag |= KEYMAP_UPDATE;
1501         if (kmi)
1502                 kmi->flag |= KMI_UPDATE;
1503 }
1504
1505 void WM_keyconfig_update_operatortype(void)
1506 {
1507         wm_keymap_update_flag |= WM_KEYMAP_UPDATE_OPERATORTYPE;
1508 }
1509
1510 static bool wm_keymap_test_and_clear_update(wmKeyMap *km)
1511 {
1512         wmKeyMapItem *kmi;
1513         int update;
1514
1515         update = (km->flag & KEYMAP_UPDATE);
1516         km->flag &= ~KEYMAP_UPDATE;
1517
1518         for (kmi = km->items.first; kmi; kmi = kmi->next) {
1519                 update = update || (kmi->flag & KMI_UPDATE);
1520                 kmi->flag &= ~KMI_UPDATE;
1521         }
1522
1523         return (update != 0);
1524 }
1525
1526 static wmKeyMap *wm_keymap_preset(wmWindowManager *wm, wmKeyMap *km)
1527 {
1528         wmKeyConfig *keyconf = WM_keyconfig_active(wm);
1529         wmKeyMap *keymap;
1530
1531         keymap = WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
1532         if (!keymap && wm->defaultconf)
1533                 keymap = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
1534
1535         return keymap;
1536 }
1537
1538 void WM_keyconfig_update(wmWindowManager *wm)
1539 {
1540         wmKeyMap *km, *defaultmap, *addonmap, *usermap, *kmn;
1541         wmKeyMapItem *kmi;
1542         wmKeyMapDiffItem *kmdi;
1543         bool compat_update = false;
1544
1545         if (G.background)
1546                 return;
1547
1548         if (wm_keymap_update_flag == 0)
1549                 return;
1550
1551         if (wm_keymap_update_flag & WM_KEYMAP_UPDATE_OPERATORTYPE) {
1552                 /* an operatortype has been removed, this wont happen often
1553                  * but when it does we have to check _every_ keymap item */
1554                 wmKeyConfig *kc;
1555
1556                 ListBase *keymaps_lb[] = {
1557                     &U.user_keymaps,
1558                     &wm->userconf->keymaps,
1559                     &wm->defaultconf->keymaps,
1560                     &wm->addonconf->keymaps,
1561                     NULL};
1562
1563                 int i;
1564
1565                 for (i = 0; keymaps_lb[i]; i++) {
1566                         wm_keyconfig_properties_update_ot(keymaps_lb[i]);
1567                 }
1568
1569                 for (kc = wm->keyconfigs.first; kc; kc = kc->next) {
1570                         wm_keyconfig_properties_update_ot(&kc->keymaps);
1571                 }
1572
1573                 wm_keymap_update_flag &= ~WM_KEYMAP_UPDATE_OPERATORTYPE;
1574         }
1575
1576
1577         if (wm_keymap_update_flag == 0)
1578                 return;
1579
1580
1581         /* update operator properties for non-modal user keymaps */
1582         for (km = U.user_keymaps.first; km; km = km->next) {
1583                 if ((km->flag & KEYMAP_MODAL) == 0) {
1584                         for (kmdi = km->diff_items.first; kmdi; kmdi = kmdi->next) {
1585                                 if (kmdi->add_item)
1586                                         wm_keymap_item_properties_set(kmdi->add_item);
1587                                 if (kmdi->remove_item)
1588                                         wm_keymap_item_properties_set(kmdi->remove_item);
1589                         }
1590
1591                         for (kmi = km->items.first; kmi; kmi = kmi->next)
1592                                 wm_keymap_item_properties_set(kmi);
1593                 }
1594         }
1595
1596         /* update U.user_keymaps with user key configuration changes */
1597         for (km = wm->userconf->keymaps.first; km; km = km->next) {
1598                 /* only diff if the user keymap was modified */
1599                 if (wm_keymap_test_and_clear_update(km)) {
1600                         /* find keymaps */
1601                         defaultmap = wm_keymap_preset(wm, km);
1602                         addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1603
1604                         /* diff */
1605                         if (defaultmap)
1606                                 wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, km);
1607                 }
1608         }
1609
1610         /* create user key configuration from preset + addon + user preferences */
1611         for (km = wm->defaultconf->keymaps.first; km; km = km->next) {
1612                 /* find keymaps */
1613                 defaultmap = wm_keymap_preset(wm, km);
1614                 addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1615                 usermap = WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
1616
1617                 wm_user_modal_keymap_set_items(wm, defaultmap);
1618
1619                 /* add */
1620                 kmn = wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
1621
1622                 if (kmn) {
1623                         kmn->modal_items = km->modal_items;
1624                         kmn->poll = km->poll;
1625                 }
1626
1627                 /* in case of old non-diff keymaps, force extra update to create diffs */
1628                 compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
1629
1630         }
1631
1632         wm_keymap_update_flag &= ~WM_KEYMAP_UPDATE_RECONFIGURE;
1633
1634         BLI_assert(wm_keymap_update_flag == 0);
1635
1636         if (compat_update) {
1637                 WM_keyconfig_update_tag(NULL, NULL);
1638                 WM_keyconfig_update(wm);
1639         }
1640 }
1641
1642 /********************************* Event Handling *****************************
1643  * Handlers have pointers to the keymap in the default configuration. During
1644  * event handling this function is called to get the keymap from the final
1645  * configuration. */
1646
1647 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
1648 {
1649         wmKeyMap *km;
1650
1651         if (!keymap)
1652                 return NULL;
1653
1654         /* first user defined keymaps */
1655         km = WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1656
1657         if (km)
1658                 return km;
1659
1660         return keymap;
1661 }
1662
1663 /******************************* Keymap Editor ********************************
1664  * In the keymap editor the user key configuration is edited. */
1665
1666 void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
1667 {
1668         wmWindowManager *wm = CTX_wm_manager(C);
1669         wmKeyMap *defaultmap, *addonmap;
1670         wmKeyMapItem *orig;
1671
1672         if (!keymap)
1673                 return;
1674
1675         /* construct default keymap from preset + addons */
1676         defaultmap = wm_keymap_preset(wm, keymap);
1677         addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1678
1679         if (addonmap) {
1680                 defaultmap = wm_keymap_copy(defaultmap);
1681                 wm_keymap_addon_add(defaultmap, addonmap);
1682         }
1683
1684         /* find original item */
1685         orig = WM_keymap_item_find_id(defaultmap, kmi->id);
1686
1687         if (orig) {
1688                 /* restore to original */
1689                 if (!STREQ(orig->idname, kmi->idname)) {
1690                         BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
1691                         WM_keymap_properties_reset(kmi, NULL);
1692                 }
1693
1694                 if (orig->properties) {
1695                         if (kmi->properties) {
1696                                 IDP_FreeProperty(kmi->properties);
1697                                 MEM_freeN(kmi->properties);
1698                                 kmi->properties = NULL;
1699                         }
1700
1701                         kmi->properties = IDP_CopyProperty(orig->properties);
1702                         kmi->ptr->data = kmi->properties;
1703                 }
1704
1705                 kmi->propvalue = orig->propvalue;
1706                 kmi->type = orig->type;
1707                 kmi->val = orig->val;
1708                 kmi->shift = orig->shift;
1709                 kmi->ctrl = orig->ctrl;
1710                 kmi->alt = orig->alt;
1711                 kmi->oskey = orig->oskey;
1712                 kmi->keymodifier = orig->keymodifier;
1713                 kmi->maptype = orig->maptype;
1714
1715                 WM_keyconfig_update_tag(keymap, kmi);
1716         }
1717
1718         /* free temporary keymap */
1719         if (addonmap) {
1720                 WM_keymap_clear(defaultmap);
1721                 MEM_freeN(defaultmap);
1722         }
1723 }
1724
1725 void WM_keymap_restore_to_default(wmKeyMap *keymap, bContext *C)
1726 {
1727         wmWindowManager *wm = CTX_wm_manager(C);
1728         wmKeyMap *usermap;
1729
1730         /* remove keymap from U.user_keymaps and update */
1731         usermap = WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1732
1733         if (usermap) {
1734                 WM_keymap_clear(usermap);
1735                 BLI_freelinkN(&U.user_keymaps, usermap);
1736
1737                 WM_keyconfig_update_tag(NULL, NULL);
1738                 WM_keyconfig_update(wm);
1739         }
1740 }
1741
1742 wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
1743 {
1744         wmKeyMapItem *kmi;
1745
1746         for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1747                 if (kmi->id == id) {
1748                         return kmi;
1749                 }
1750         }
1751
1752         return NULL;
1753 }
1754
1755 const char *WM_bool_as_string(bool test)
1756 {
1757         return test ? IFACE_("ON") : IFACE_("OFF");
1758 }