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