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