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