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