ccf06155f258724db3b423ccffd1b31cd233ef2c
[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 /* menu wrapper for WM_keymap_add_item */
481 wmKeyMapItem *WM_keymap_add_menu(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
482 {
483         wmKeyMapItem *kmi = WM_keymap_add_item(keymap, "WM_OT_call_menu", type, val, modifier, keymodifier);
484         RNA_string_set(kmi->ptr, "name", idname);
485         return kmi;
486 }
487
488 wmKeyMapItem *WM_keymap_add_menu_pie(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
489 {
490         wmKeyMapItem *kmi = WM_keymap_add_item(keymap, "WM_OT_call_menu_pie", type, val, modifier, keymodifier);
491         RNA_string_set(kmi->ptr, "name", idname);
492         return kmi;
493 }
494
495 bool WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
496 {
497         if (BLI_findindex(&keymap->items, kmi) != -1) {
498                 if (kmi->ptr) {
499                         WM_operator_properties_free(kmi->ptr);
500                         MEM_freeN(kmi->ptr);
501                 }
502                 BLI_freelinkN(&keymap->items, kmi);
503
504                 WM_keyconfig_update_tag(keymap, NULL);
505                 return true;
506         }
507         else {
508                 return false;
509         }
510 }
511
512 /************************** Keymap Diff and Patch ****************************
513  * Rather than saving the entire keymap for user preferences, we only save a
514  * diff so that changes in the defaults get synced. This system is not perfect
515  * but works better than overriding the keymap entirely when only few items
516  * are changed. */
517
518 static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
519 {
520         wmKeyMapItem *kmi, *kmin;
521
522         for (kmi = addonmap->items.first; kmi; kmi = kmi->next) {
523                 kmin = wm_keymap_item_copy(kmi);
524                 keymap_item_set_id(keymap, kmin);
525                 BLI_addhead(&keymap->items, kmin);
526         }
527 }
528
529 static wmKeyMapItem *wm_keymap_find_item_equals(wmKeyMap *km, wmKeyMapItem *needle)
530 {
531         wmKeyMapItem *kmi;
532
533         for (kmi = km->items.first; kmi; kmi = kmi->next)
534                 if (wm_keymap_item_equals(kmi, needle))
535                         return kmi;
536
537         return NULL;
538 }
539
540 static wmKeyMapItem *wm_keymap_find_item_equals_result(wmKeyMap *km, wmKeyMapItem *needle)
541 {
542         wmKeyMapItem *kmi;
543
544         for (kmi = km->items.first; kmi; kmi = kmi->next)
545                 if (wm_keymap_item_equals_result(kmi, needle))
546                         return kmi;
547
548         return NULL;
549 }
550
551 static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km, wmKeyMap *orig_km, wmKeyMap *addon_km)
552 {
553         wmKeyMapItem *kmi, *to_kmi, *orig_kmi;
554         wmKeyMapDiffItem *kmdi;
555
556         for (kmi = from_km->items.first; kmi; kmi = kmi->next) {
557                 to_kmi = WM_keymap_item_find_id(to_km, kmi->id);
558
559                 if (!to_kmi) {
560                         /* remove item */
561                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
562                         kmdi->remove_item = wm_keymap_item_copy(kmi);
563                         BLI_addtail(&diff_km->diff_items, kmdi);
564                 }
565                 else if (to_kmi && !wm_keymap_item_equals(kmi, to_kmi)) {
566                         /* replace item */
567                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
568                         kmdi->remove_item = wm_keymap_item_copy(kmi);
569                         kmdi->add_item = wm_keymap_item_copy(to_kmi);
570                         BLI_addtail(&diff_km->diff_items, kmdi);
571                 }
572
573                 /* sync expanded flag back to original so we don't loose it on repatch */
574                 if (to_kmi) {
575                         orig_kmi = WM_keymap_item_find_id(orig_km, kmi->id);
576
577                         if (!orig_kmi && addon_km)
578                                 orig_kmi = wm_keymap_find_item_equals(addon_km, kmi);
579
580                         if (orig_kmi) {
581                                 orig_kmi->flag &= ~KMI_EXPANDED;
582                                 orig_kmi->flag |= (to_kmi->flag & KMI_EXPANDED);
583                         }
584                 }
585         }
586
587         for (kmi = to_km->items.first; kmi; kmi = kmi->next) {
588                 if (kmi->id < 0) {
589                         /* add item */
590                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
591                         kmdi->add_item = wm_keymap_item_copy(kmi);
592                         BLI_addtail(&diff_km->diff_items, kmdi);
593                 }
594         }
595 }
596
597 static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
598 {
599         wmKeyMapDiffItem *kmdi;
600         wmKeyMapItem *kmi_remove, *kmi_add;
601
602         for (kmdi = diff_km->diff_items.first; kmdi; kmdi = kmdi->next) {
603                 /* find item to remove */
604                 kmi_remove = NULL;
605                 if (kmdi->remove_item) {
606                         kmi_remove = wm_keymap_find_item_equals(km, kmdi->remove_item);
607                         if (!kmi_remove)
608                                 kmi_remove = wm_keymap_find_item_equals_result(km, kmdi->remove_item);
609                 }
610
611                 /* add item */
612                 if (kmdi->add_item) {
613                         /* Do not re-add an already existing keymap item! See T42088. */
614                         /* We seek only for exact copy here! See T42137. */
615                         kmi_add = wm_keymap_find_item_equals(km, kmdi->add_item);
616
617                         /* If kmi_add is same as kmi_remove (can happen in some cases, typically when we got kmi_remove
618                          * from wm_keymap_find_item_equals_result()), no need to add or remove anything, see T45579. */
619                         /* Note: This typically happens when we apply user-defined keymap diff to a base one that was exported
620                          *       with that customized keymap already. In that case:
621                          *         - wm_keymap_find_item_equals(km, kmdi->remove_item) finds nothing (because actual shortcut of
622                          *           current base does not match kmdi->remove_item any more).
623                          *         - wm_keymap_find_item_equals_result(km, kmdi->remove_item) finds the current kmi from
624                          *           base keymap (because it does exactly the same thing).
625                          *         - wm_keymap_find_item_equals(km, kmdi->add_item) finds the same kmi, since base keymap was
626                          *           exported with that user-defined shortcut already!
627                          *       Maybe we should rather keep user-defined keymaps specific to a given base one? */
628                         if (kmi_add != NULL && kmi_add == kmi_remove) {
629                                 kmi_remove = NULL;
630                         }
631                         /* only if nothing to remove or item to remove found */
632                         else if (!kmi_add && (!kmdi->remove_item || kmi_remove)) {
633                                 kmi_add = wm_keymap_item_copy(kmdi->add_item);
634                                 kmi_add->flag |= KMI_USER_MODIFIED;
635
636                                 if (kmi_remove) {
637                                         kmi_add->flag &= ~KMI_EXPANDED;
638                                         kmi_add->flag |= (kmi_remove->flag & KMI_EXPANDED);
639                                         kmi_add->id = kmi_remove->id;
640                                         BLI_insertlinkbefore(&km->items, kmi_remove, kmi_add);
641                                 }
642                                 else {
643                                         keymap_item_set_id(km, kmi_add);
644                                         BLI_addtail(&km->items, kmi_add);
645                                 }
646                         }
647                 }
648
649                 /* remove item */
650                 if (kmi_remove) {
651                         wm_keymap_item_free(kmi_remove);
652                         BLI_freelinkN(&km->items, kmi_remove);
653                 }
654         }
655 }
656
657 static wmKeyMap *wm_keymap_patch_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *usermap)
658 {
659         wmKeyMap *km;
660         int expanded = 0;
661
662         /* remove previous keymap in list, we will replace it */
663         km = WM_keymap_list_find(lb, defaultmap->idname, defaultmap->spaceid, defaultmap->regionid);
664         if (km) {
665                 expanded = (km->flag & (KEYMAP_EXPANDED | KEYMAP_CHILDREN_EXPANDED));
666                 WM_keymap_free(km);
667                 BLI_freelinkN(lb, km);
668         }
669
670         /* copy new keymap from an existing one */
671         if (usermap && !(usermap->flag & KEYMAP_DIFF)) {
672                 /* for compatibility with old user preferences with non-diff
673                  * keymaps we override the original entirely */
674                 wmKeyMapItem *kmi, *orig_kmi;
675
676                 km = wm_keymap_copy(usermap);
677
678                 /* try to find corresponding id's for items */
679                 for (kmi = km->items.first; kmi; kmi = kmi->next) {
680                         orig_kmi = wm_keymap_find_item_equals(defaultmap, kmi);
681                         if (!orig_kmi)
682                                 orig_kmi = wm_keymap_find_item_equals_result(defaultmap, kmi);
683
684                         if (orig_kmi)
685                                 kmi->id = orig_kmi->id;
686                         else
687                                 kmi->id = -(km->kmi_id++);
688                 }
689
690                 km->flag |= KEYMAP_UPDATE; /* update again to create diff */
691         }
692         else
693                 km = wm_keymap_copy(defaultmap);
694
695         /* add addon keymap items */
696         if (addonmap)
697                 wm_keymap_addon_add(km, addonmap);
698
699         /* tag as being user edited */
700         if (usermap)
701                 km->flag |= KEYMAP_USER_MODIFIED;
702         km->flag |= KEYMAP_USER | expanded;
703
704         /* apply user changes of diff keymap */
705         if (usermap && (usermap->flag & KEYMAP_DIFF))
706                 wm_keymap_patch(km, usermap);
707
708         /* add to list */
709         BLI_addtail(lb, km);
710
711         return km;
712 }
713
714 static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *km)
715 {
716         wmKeyMap *diffmap, *prevmap, *origmap;
717
718         /* create temporary default + addon keymap for diff */
719         origmap = defaultmap;
720
721         if (addonmap) {
722                 defaultmap = wm_keymap_copy(defaultmap);
723                 wm_keymap_addon_add(defaultmap, addonmap);
724         }
725
726         /* remove previous diff keymap in list, we will replace it */
727         prevmap = WM_keymap_list_find(lb, km->idname, km->spaceid, km->regionid);
728         if (prevmap) {
729                 WM_keymap_free(prevmap);
730                 BLI_freelinkN(lb, prevmap);
731         }
732
733         /* create diff keymap */
734         diffmap = wm_keymap_new(km->idname, km->spaceid, km->regionid);
735         diffmap->flag |= KEYMAP_DIFF;
736         if (defaultmap->flag & KEYMAP_MODAL)
737                 diffmap->flag |= KEYMAP_MODAL;
738         wm_keymap_diff(diffmap, defaultmap, km, origmap, addonmap);
739
740         /* add to list if not empty */
741         if (diffmap->diff_items.first) {
742                 BLI_addtail(lb, diffmap);
743         }
744         else {
745                 WM_keymap_free(diffmap);
746                 MEM_freeN(diffmap);
747         }
748
749         /* free temporary default map */
750         if (addonmap) {
751                 WM_keymap_free(defaultmap);
752                 MEM_freeN(defaultmap);
753         }
754 }
755
756 /* ****************** storage in WM ************ */
757
758 /* name id's are for storing general or multiple keymaps,
759  * space/region ids are same as DNA_space_types.h */
760 /* gets freed in wm.c */
761
762 wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int regionid)
763 {
764         wmKeyMap *km;
765
766         for (km = lb->first; km; km = km->next)
767                 if (km->spaceid == spaceid && km->regionid == regionid)
768                         if (STREQLEN(idname, km->idname, KMAP_MAX_NAME))
769                                 return km;
770
771         return NULL;
772 }
773
774 wmKeyMap *WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
775 {
776         wmKeyMap *km = WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
777
778         if (km == NULL) {
779                 km = wm_keymap_new(idname, spaceid, regionid);
780                 BLI_addtail(&keyconf->keymaps, km);
781
782                 WM_keyconfig_update_tag(km, NULL);
783         }
784
785         return km;
786 }
787
788 wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid, int regionid)
789 {
790         wmWindowManager *wm = CTX_wm_manager(C);
791
792         return WM_keymap_list_find(&wm->userconf->keymaps, idname, spaceid, regionid);
793 }
794
795 /* ****************** modal keymaps ************ */
796
797 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
798
799 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
800 {
801         wmKeyMap *km = WM_keymap_ensure(keyconf, idname, 0, 0);
802         km->flag |= KEYMAP_MODAL;
803         km->modal_items = items;
804
805         if (!items) {
806                 /* init modal items from default config */
807                 wmWindowManager *wm = G_MAIN->wm.first;
808                 if (wm->defaultconf) {
809                         wmKeyMap *defaultkm = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
810
811                         if (defaultkm) {
812                                 km->modal_items = defaultkm->modal_items;
813                                 km->poll = defaultkm->poll;
814                         }
815                 }
816         }
817
818         return km;
819 }
820
821 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, const char *idname)
822 {
823         wmKeyMap *km;
824
825         for (km = keyconf->keymaps.first; km; km = km->next)
826                 if (km->flag & KEYMAP_MODAL)
827                         if (STREQLEN(idname, km->idname, KMAP_MAX_NAME))
828                                 break;
829
830         return km;
831 }
832
833
834 wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
835 {
836         wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
837
838         BLI_addtail(&km->items, kmi);
839         kmi->propvalue = value;
840
841         keymap_event_set(kmi, type, val, modifier, keymodifier);
842
843         keymap_item_set_id(km, kmi);
844
845         WM_keyconfig_update_tag(km, kmi);
846
847         return kmi;
848 }
849
850 wmKeyMapItem *WM_modalkeymap_add_item_str(wmKeyMap *km, int type, int val, int modifier, int keymodifier, const char *value)
851 {
852         wmKeyMapItem *kmi = MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
853
854         BLI_addtail(&km->items, kmi);
855         BLI_strncpy(kmi->propvalue_str, value, sizeof(kmi->propvalue_str));
856
857         keymap_event_set(kmi, type, val, modifier, keymodifier);
858
859         keymap_item_set_id(km, kmi);
860
861         WM_keyconfig_update_tag(km, kmi);
862
863         return kmi;
864 }
865
866 static wmKeyMapItem *wm_modalkeymap_find_propvalue_iter(wmKeyMap *km, wmKeyMapItem *kmi, const int propvalue)
867 {
868         if (km->flag & KEYMAP_MODAL) {
869                 kmi = kmi ? kmi->next : km->items.first;
870                 for (; kmi; kmi = kmi->next) {
871                         if (kmi->propvalue == propvalue) {
872                                 return kmi;
873                         }
874                 }
875         }
876         else {
877                 BLI_assert(!"called with non modal keymap");
878         }
879
880         return NULL;
881 }
882
883 wmKeyMapItem *WM_modalkeymap_find_propvalue(wmKeyMap *km, const int propvalue)
884 {
885         return wm_modalkeymap_find_propvalue_iter(km, NULL, propvalue);
886 }
887
888 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
889 {
890         wmOperatorType *ot = WM_operatortype_find(opname, 0);
891
892         if (ot) {
893                 ot->modalkeymap = km;
894         }
895         else {
896                 CLOG_ERROR(WM_LOG_KEYMAPS, "unknown operator '%s'", opname);
897         }
898 }
899
900 static void wm_user_modal_keymap_set_items(wmWindowManager *wm, wmKeyMap *km)
901 {
902         /* here we convert propvalue string values delayed, due to python keymaps
903          * being created before the actual modal keymaps, so no modal_items */
904         wmKeyMap *defaultkm;
905         wmKeyMapItem *kmi;
906         int propvalue;
907
908         if (km && (km->flag & KEYMAP_MODAL) && !km->modal_items) {
909                 if (wm->defaultconf == NULL) {
910                         return;
911                 }
912
913                 defaultkm = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
914
915                 if (!defaultkm)
916                         return;
917
918                 km->modal_items = defaultkm->modal_items;
919                 km->poll = defaultkm->poll;
920
921                 for (kmi = km->items.first; kmi; kmi = kmi->next) {
922                         if (kmi->propvalue_str[0]) {
923                                 if (RNA_enum_value_from_id(km->modal_items, kmi->propvalue_str, &propvalue))
924                                         kmi->propvalue = propvalue;
925                                 kmi->propvalue_str[0] = '\0';
926                         }
927                 }
928         }
929 }
930
931 /* ***************** get string from key events **************** */
932
933 const char *WM_key_event_string(const short type, const bool compact)
934 {
935         const EnumPropertyItem *it;
936         const int i = RNA_enum_from_value(rna_enum_event_type_items, (int)type);
937
938         if (i == -1) {
939                 return "";
940         }
941         it = &rna_enum_event_type_items[i];
942
943         /* We first try enum items' description (abused as shortname here), and fall back to usual name if empty. */
944         if (compact && it->description[0]) {
945                 /* XXX No context for enum descriptions... In practice shall not be an issue though. */
946                 return IFACE_(it->description);
947         }
948
949         return CTX_IFACE_(BLT_I18NCONTEXT_UI_EVENTS, it->name);
950 }
951
952 /* TODO: also support (some) value, like e.g. double-click? */
953 int WM_keymap_item_raw_to_string(
954         const short shift, const short ctrl, const short alt, const short oskey,
955         const short keymodifier, const short val, const short type, const bool compact,
956         char *result, const int result_len)
957 {
958 #define ADD_SEP if (p != buf) *p++ = ' '; (void)0
959
960         char buf[128];
961         char *p = buf;
962
963         buf[0] = '\0';
964
965         /* TODO: support order (KM_SHIFT vs. KM_SHIFT2) ? */
966         if (shift == KM_ANY &&
967             ctrl == KM_ANY &&
968             alt == KM_ANY &&
969             oskey == KM_ANY)
970         {
971                 /* make it implicit in case of compact result expected. */
972                 if (!compact) {
973                         ADD_SEP;
974                         p += BLI_strcpy_rlen(p, IFACE_("Any"));
975                 }
976         }
977         else {
978                 if (shift) {
979                         ADD_SEP;
980                         p += BLI_strcpy_rlen(p, IFACE_("Shift"));
981                 }
982
983                 if (ctrl) {
984                         ADD_SEP;
985                         p += BLI_strcpy_rlen(p, IFACE_("Ctrl"));
986                 }
987
988                 if (alt) {
989                         ADD_SEP;
990                         p += BLI_strcpy_rlen(p, IFACE_("Alt"));
991                 }
992
993                 if (oskey) {
994                         ADD_SEP;
995                         p += BLI_strcpy_rlen(p, IFACE_("Cmd"));
996                 }
997         }
998
999         if (keymodifier) {
1000                 ADD_SEP;
1001                 p += BLI_strcpy_rlen(p, WM_key_event_string(keymodifier, compact));
1002         }
1003
1004         if (type) {
1005                 ADD_SEP;
1006                 if (val == KM_DBL_CLICK) {
1007                         p += BLI_strcpy_rlen(p, IFACE_("dbl-"));
1008                 }
1009                 p += BLI_strcpy_rlen(p, WM_key_event_string(type, compact));
1010         }
1011
1012         /* We assume size of buf is enough to always store any possible shortcut, but let's add a debug check about it! */
1013         BLI_assert(p - buf < sizeof(buf));
1014
1015         /* We need utf8 here, otherwise we may 'cut' some unicode chars like arrows... */
1016         return BLI_strncpy_utf8_rlen(result, buf, result_len);
1017
1018 #undef ADD_SEP
1019 }
1020
1021 int WM_keymap_item_to_string(
1022         wmKeyMapItem *kmi, const bool compact,
1023         char *result, const int result_len)
1024 {
1025         return WM_keymap_item_raw_to_string(
1026                 kmi->shift, kmi->ctrl, kmi->alt, kmi->oskey, kmi->keymodifier, kmi->val, kmi->type,
1027                 compact, result, result_len);
1028 }
1029
1030 int WM_modalkeymap_items_to_string(
1031         wmKeyMap *km, const int propvalue, const bool compact,
1032         char *result, const int result_len)
1033 {
1034         int totlen = 0;
1035         bool add_sep = false;
1036
1037         if (km) {
1038                 wmKeyMapItem *kmi;
1039
1040                 /* Find all shortcuts related to that propvalue! */
1041                 for (kmi = WM_modalkeymap_find_propvalue(km, propvalue);
1042                      kmi && totlen < (result_len - 2);
1043                      kmi = wm_modalkeymap_find_propvalue_iter(km, kmi, propvalue))
1044                 {
1045                         if (add_sep) {
1046                                 result[totlen++] = '/';
1047                                 result[totlen] = '\0';
1048                         }
1049                         else {
1050                                 add_sep = true;
1051                         }
1052                         totlen += WM_keymap_item_to_string(kmi, compact, &result[totlen], result_len - totlen);
1053                 }
1054         }
1055
1056         return totlen;
1057 }
1058
1059 int WM_modalkeymap_operator_items_to_string(
1060         wmOperatorType *ot, const int propvalue, const bool compact,
1061         char *result, const int result_len)
1062 {
1063         return WM_modalkeymap_items_to_string(ot->modalkeymap, propvalue, compact, result, result_len);
1064 }
1065
1066 char *WM_modalkeymap_operator_items_to_string_buf(
1067         wmOperatorType *ot, const int propvalue, const bool compact,
1068         const int max_len, int *r_available_len, char **r_result)
1069 {
1070         char *ret = *r_result;
1071
1072         if (*r_available_len > 1) {
1073                 int used_len = WM_modalkeymap_operator_items_to_string(
1074                         ot, propvalue, compact, ret, min_ii(*r_available_len, max_len)) + 1;
1075
1076                 *r_available_len -= used_len;
1077                 *r_result += used_len;
1078                 if (*r_available_len == 0) {
1079                         (*r_result)--;  /* So that *result keeps pointing on a valid char, we'll stay on it anyway. */
1080                 }
1081         }
1082         else {
1083                 *ret = '\0';
1084         }
1085
1086         return ret;
1087 }
1088
1089 static wmKeyMapItem *wm_keymap_item_find_handlers(
1090         const bContext *C, ListBase *handlers, const char *opname, int UNUSED(opcontext),
1091         IDProperty *properties, const bool is_strict, const bool is_hotkey,
1092         wmKeyMap **r_keymap)
1093 {
1094         wmWindowManager *wm = CTX_wm_manager(C);
1095         wmEventHandler *handler;
1096         wmKeyMap *keymap;
1097         wmKeyMapItem *kmi;
1098
1099         /* find keymap item in handlers */
1100         for (handler = handlers->first; handler; handler = handler->next) {
1101                 keymap = WM_keymap_active(wm, handler->keymap);
1102
1103                 if (keymap && WM_keymap_poll((bContext *)C, keymap)) {
1104                         for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1105                                 /* skip disabled keymap items [T38447] */
1106                                 if (kmi->flag & KMI_INACTIVE)
1107                                         continue;
1108
1109                                 if (STREQ(kmi->idname, opname) && WM_key_event_string(kmi->type, false)[0]) {
1110                                         if (is_hotkey) {
1111                                                 if (!ISHOTKEY(kmi->type))
1112                                                         continue;
1113                                         }
1114
1115                                         if (properties) {
1116                                                 /* example of debugging keymaps */
1117 #if 0
1118                                                 if (kmi->ptr) {
1119                                                         if (STREQ("MESH_OT_rip_move", opname)) {
1120                                                                 printf("OPERATOR\n");
1121                                                                 IDP_print(properties);
1122                                                                 printf("KEYMAP\n");
1123                                                                 IDP_print(kmi->ptr->data);
1124                                                         }
1125                                                 }
1126 #endif
1127
1128                                                 if (kmi->ptr && IDP_EqualsProperties_ex(properties, kmi->ptr->data, is_strict)) {
1129                                                         if (r_keymap) *r_keymap = keymap;
1130                                                         return kmi;
1131                                                 }
1132                                                 /* Debug only, helps spotting mismatches between menu entries and shortcuts! */
1133                                                 else if (G.debug & G_DEBUG_WM) {
1134                                                         if (is_strict && kmi->ptr) {
1135                                                                 wmOperatorType *ot = WM_operatortype_find(opname, true);
1136                                                                 if (ot) {
1137                                                                         /* make a copy of the properties and set unset ones to their default values. */
1138                                                                         PointerRNA opptr;
1139                                                                         IDProperty *properties_default = IDP_CopyProperty(kmi->ptr->data);
1140
1141                                                                         RNA_pointer_create(NULL, ot->srna, properties_default, &opptr);
1142                                                                         WM_operator_properties_default(&opptr, true);
1143
1144                                                                         if (IDP_EqualsProperties_ex(properties, properties_default, is_strict)) {
1145                                                                                 char kmi_str[128];
1146                                                                                 WM_keymap_item_to_string(kmi, false, kmi_str, sizeof(kmi_str));
1147                                                                                 /* Note gievn properties could come from other things than menu entry... */
1148                                                                                 printf("%s: Some set values in menu entry match default op values, "
1149                                                                                        "this might not be desired!\n", opname);
1150                                                                                 printf("\tkm: '%s', kmi: '%s'\n", keymap->idname, kmi_str);
1151 #ifndef NDEBUG
1152 #ifdef WITH_PYTHON
1153                                                                                 printf("OPERATOR\n");
1154                                                                                 IDP_print(properties);
1155                                                                                 printf("KEYMAP\n");
1156                                                                                 IDP_print(kmi->ptr->data);
1157 #endif
1158 #endif
1159                                                                                 printf("\n");
1160                                                                         }
1161
1162                                                                         IDP_FreeProperty(properties_default);
1163                                                                         MEM_freeN(properties_default);
1164                                                                 }
1165                                                         }
1166                                                 }
1167                                         }
1168                                         else {
1169                                                 if (r_keymap) *r_keymap = keymap;
1170                                                 return kmi;
1171                                         }
1172                                 }
1173                         }
1174                 }
1175         }
1176
1177         /* ensure un-initialized keymap is never used */
1178         if (r_keymap) *r_keymap = NULL;
1179         return NULL;
1180 }
1181
1182 static wmKeyMapItem *wm_keymap_item_find_props(
1183         const bContext *C, const char *opname, int opcontext,
1184         IDProperty *properties, const bool is_strict, const bool is_hotkey,
1185         wmKeyMap **r_keymap)
1186 {
1187         wmWindow *win = CTX_wm_window(C);
1188         ScrArea *sa = CTX_wm_area(C);
1189         ARegion *ar = CTX_wm_region(C);
1190         wmKeyMapItem *found = NULL;
1191
1192         /* look into multiple handler lists to find the item */
1193         if (win) {
1194                 found = wm_keymap_item_find_handlers(
1195                         C, &win->modalhandlers, opname, opcontext, properties, is_strict, is_hotkey, r_keymap);
1196                 if (found == NULL) {
1197                         found = wm_keymap_item_find_handlers(
1198                                 C, &win->handlers, opname, opcontext, properties, is_strict, is_hotkey, r_keymap);
1199                 }
1200         }
1201
1202         if (sa && found == NULL)
1203                 found = wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, is_strict, is_hotkey, r_keymap);
1204
1205         if (found == NULL) {
1206                 if (ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
1207                         if (sa) {
1208                                 if (!(ar && ar->regiontype == RGN_TYPE_WINDOW))
1209                                         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
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                 }
1215                 else if (ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
1216                         if (!(ar && ar->regiontype == RGN_TYPE_CHANNELS))
1217                                 ar = BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
1218
1219                         if (ar)
1220                                 found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, is_hotkey, r_keymap);
1221                 }
1222                 else if (ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
1223                         if (!(ar && ar->regiontype == RGN_TYPE_PREVIEW))
1224                                 ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
1225
1226                         if (ar)
1227                                 found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, is_hotkey, r_keymap);
1228                 }
1229                 else {
1230                         if (ar)
1231                                 found = wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, is_strict, is_hotkey, r_keymap);
1232                 }
1233         }
1234
1235         return found;
1236 }
1237
1238 static wmKeyMapItem *wm_keymap_item_find(
1239         const bContext *C, const char *opname, int opcontext,
1240         IDProperty *properties, const bool is_hotkey, bool is_strict,
1241         wmKeyMap **r_keymap)
1242 {
1243         wmKeyMapItem *found;
1244
1245         /* XXX Hack! Macro operators in menu entry have their whole props defined, which is not the case for
1246          *     relevant keymap entries. Could be good to check and harmonize this, but for now always
1247          *     compare non-strict in this case.
1248          */
1249         wmOperatorType *ot = WM_operatortype_find(opname, true);
1250         if (ot) {
1251                 is_strict = is_strict && ((ot->flag & OPTYPE_MACRO) == 0);
1252         }
1253
1254         found = wm_keymap_item_find_props(C, opname, opcontext, properties, is_strict, is_hotkey, r_keymap);
1255
1256         /* This block is *only* useful in one case: when op uses an enum menu in its prop member
1257          * (then, we want to rerun a comparison with that 'prop' unset). Note this remains brittle,
1258          * since now any enum prop may be used in UI (specified by name), ot->prop is not so much used...
1259          * Otherwise:
1260          *     * If non-strict, unset properties always match set ones in IDP_EqualsProperties_ex.
1261          *     * If strict, unset properties never match set ones in IDP_EqualsProperties_ex,
1262          *       and we do not want that to change (else we get things like T41757)!
1263          * ...so in either case, re-running a comparison with unset props set to default is useless.
1264          */
1265         if (!found && properties) {
1266                 if (ot && ot->prop) {  /* XXX Shall we also check ot->prop is actually an enum? */
1267                         /* make a copy of the properties and unset the 'ot->prop' one if set. */
1268                         PointerRNA opptr;
1269                         IDProperty *properties_temp = IDP_CopyProperty(properties);
1270
1271                         RNA_pointer_create(NULL, ot->srna, properties_temp, &opptr);
1272
1273                         if (RNA_property_is_set(&opptr, ot->prop)) {
1274                                 /* for operator that has enum menu, unset it so its value does not affect comparison result */
1275                                 RNA_property_unset(&opptr, ot->prop);
1276
1277                                 found = wm_keymap_item_find_props(C, opname, opcontext, properties_temp,
1278                                                                   is_strict, is_hotkey, r_keymap);
1279                         }
1280
1281                         IDP_FreeProperty(properties_temp);
1282                         MEM_freeN(properties_temp);
1283                 }
1284         }
1285
1286         /* Debug only, helps spotting mismatches between menu entries and shortcuts! */
1287         if (G.debug & G_DEBUG_WM) {
1288                 if (!found && is_strict && properties) {
1289                         wmKeyMap *km;
1290                         wmKeyMapItem *kmi;
1291                         if (ot) {
1292                                 /* make a copy of the properties and set unset ones to their default values. */
1293                                 PointerRNA opptr;
1294                                 IDProperty *properties_default = IDP_CopyProperty(properties);
1295
1296                                 RNA_pointer_create(NULL, ot->srna, properties_default, &opptr);
1297                                 WM_operator_properties_default(&opptr, true);
1298
1299                                 kmi = wm_keymap_item_find_props(C, opname, opcontext, properties_default, is_strict, is_hotkey, &km);
1300                                 if (kmi) {
1301                                         char kmi_str[128];
1302                                         WM_keymap_item_to_string(kmi, false, kmi_str, sizeof(kmi_str));
1303                                         printf("%s: Some set values in keymap entry match default op values, "
1304                                                "this might not be desired!\n", opname);
1305                                         printf("\tkm: '%s', kmi: '%s'\n", km->idname, kmi_str);
1306 #ifndef NDEBUG
1307 #ifdef WITH_PYTHON
1308                                         printf("OPERATOR\n");
1309                                         IDP_print(properties);
1310                                         printf("KEYMAP\n");
1311                                         IDP_print(kmi->ptr->data);
1312 #endif
1313 #endif
1314                                         printf("\n");
1315                                 }
1316
1317                                 IDP_FreeProperty(properties_default);
1318                                 MEM_freeN(properties_default);
1319                         }
1320                 }
1321         }
1322
1323         return found;
1324 }
1325
1326 char *WM_key_event_operator_string(
1327         const bContext *C, const char *opname, int opcontext,
1328         IDProperty *properties, const bool is_strict,
1329         char *result, const int result_len)
1330 {
1331         wmKeyMapItem *kmi = wm_keymap_item_find(C, opname, opcontext, properties, false, is_strict, NULL);
1332
1333         if (kmi) {
1334                 WM_keymap_item_to_string(kmi, false, result, result_len);
1335                 return result;
1336         }
1337
1338         return NULL;
1339 }
1340
1341 wmKeyMapItem *WM_key_event_operator(
1342         const bContext *C, const char *opname, int opcontext,
1343         IDProperty *properties, const bool is_hotkey,
1344         wmKeyMap **r_keymap)
1345 {
1346         return wm_keymap_item_find(C, opname, opcontext, properties, is_hotkey, true, r_keymap);
1347 }
1348
1349 bool WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
1350 {
1351         int k1type, k2type;
1352
1353         if (k1->flag & KMI_INACTIVE || k2->flag & KMI_INACTIVE)
1354                 return 0;
1355
1356         /* take event mapping into account */
1357         k1type = WM_userdef_event_map(k1->type);
1358         k2type = WM_userdef_event_map(k2->type);
1359
1360         if (k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
1361                 return 0;
1362
1363         if (k1->val != KM_ANY && k2->val != KM_ANY) {
1364                 /* take click, press, release conflict into account */
1365                 if (k1->val == KM_CLICK && ELEM(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
1366                         return 0;
1367                 if (k2->val == KM_CLICK && ELEM(k1->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
1368                         return 0;
1369                 if (k1->val != k2->val)
1370                         return 0;
1371         }
1372
1373         if (k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
1374                 return 0;
1375
1376         if (k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
1377                 return 0;
1378
1379         if (k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
1380                 return 0;
1381
1382         if (k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
1383                 return 0;
1384
1385         if (k1->keymodifier != k2->keymodifier)
1386                 return 0;
1387
1388         return 1;
1389 }
1390
1391 /************************* Update Final Configuration *************************
1392  * On load or other changes, the final user key configuration is rebuilt from
1393  * the preset, addon and user preferences keymaps. We also test if the final
1394  * configuration changed and write the changes to the user preferences. */
1395
1396 /* so operator removal can trigger update */
1397 enum {
1398         WM_KEYMAP_UPDATE_RECONFIGURE    = (1 << 0),
1399
1400         /* ensure all wmKeyMap have their operator types validated after removing an operator */
1401         WM_KEYMAP_UPDATE_OPERATORTYPE   = (1 << 1),
1402 };
1403
1404 static char wm_keymap_update_flag = 0;
1405
1406 void WM_keyconfig_update_tag(wmKeyMap *km, wmKeyMapItem *kmi)
1407 {
1408         /* quick tag to do delayed keymap updates */
1409         wm_keymap_update_flag |= WM_KEYMAP_UPDATE_RECONFIGURE;
1410
1411         if (km)
1412                 km->flag |= KEYMAP_UPDATE;
1413         if (kmi)
1414                 kmi->flag |= KMI_UPDATE;
1415 }
1416
1417 void WM_keyconfig_update_operatortype(void)
1418 {
1419         wm_keymap_update_flag |= WM_KEYMAP_UPDATE_OPERATORTYPE;
1420 }
1421
1422 static bool wm_keymap_test_and_clear_update(wmKeyMap *km)
1423 {
1424         wmKeyMapItem *kmi;
1425         int update;
1426
1427         update = (km->flag & KEYMAP_UPDATE);
1428         km->flag &= ~KEYMAP_UPDATE;
1429
1430         for (kmi = km->items.first; kmi; kmi = kmi->next) {
1431                 update = update || (kmi->flag & KMI_UPDATE);
1432                 kmi->flag &= ~KMI_UPDATE;
1433         }
1434
1435         return (update != 0);
1436 }
1437
1438 static wmKeyMap *wm_keymap_preset(wmWindowManager *wm, wmKeyMap *km)
1439 {
1440         wmKeyConfig *keyconf = WM_keyconfig_active(wm);
1441         wmKeyMap *keymap;
1442
1443         keymap = WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
1444         if (!keymap && wm->defaultconf)
1445                 keymap = WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
1446
1447         return keymap;
1448 }
1449
1450 void WM_keyconfig_update(wmWindowManager *wm)
1451 {
1452         wmKeyMap *km, *defaultmap, *addonmap, *usermap, *kmn;
1453         wmKeyMapItem *kmi;
1454         wmKeyMapDiffItem *kmdi;
1455         bool compat_update = false;
1456
1457         if (G.background)
1458                 return;
1459
1460         if (wm_keymap_update_flag == 0)
1461                 return;
1462
1463         if (wm_keymap_update_flag & WM_KEYMAP_UPDATE_OPERATORTYPE) {
1464                 /* an operatortype has been removed, this wont happen often
1465                  * but when it does we have to check _every_ keymap item */
1466                 wmKeyConfig *kc;
1467
1468                 ListBase *keymaps_lb[] = {
1469                     &U.user_keymaps,
1470                     &wm->userconf->keymaps,
1471                     &wm->defaultconf->keymaps,
1472                     &wm->addonconf->keymaps,
1473                     NULL};
1474
1475                 int i;
1476
1477                 for (i = 0; keymaps_lb[i]; i++) {
1478                         wm_keyconfig_properties_update_ot(keymaps_lb[i]);
1479                 }
1480
1481                 for (kc = wm->keyconfigs.first; kc; kc = kc->next) {
1482                         wm_keyconfig_properties_update_ot(&kc->keymaps);
1483                 }
1484
1485                 wm_keymap_update_flag &= ~WM_KEYMAP_UPDATE_OPERATORTYPE;
1486         }
1487
1488
1489         if (wm_keymap_update_flag == 0)
1490                 return;
1491
1492
1493         /* update operator properties for non-modal user keymaps */
1494         for (km = U.user_keymaps.first; km; km = km->next) {
1495                 if ((km->flag & KEYMAP_MODAL) == 0) {
1496                         for (kmdi = km->diff_items.first; kmdi; kmdi = kmdi->next) {
1497                                 if (kmdi->add_item)
1498                                         wm_keymap_item_properties_set(kmdi->add_item);
1499                                 if (kmdi->remove_item)
1500                                         wm_keymap_item_properties_set(kmdi->remove_item);
1501                         }
1502
1503                         for (kmi = km->items.first; kmi; kmi = kmi->next)
1504                                 wm_keymap_item_properties_set(kmi);
1505                 }
1506         }
1507
1508         /* update U.user_keymaps with user key configuration changes */
1509         for (km = wm->userconf->keymaps.first; km; km = km->next) {
1510                 /* only diff if the user keymap was modified */
1511                 if (wm_keymap_test_and_clear_update(km)) {
1512                         /* find keymaps */
1513                         defaultmap = wm_keymap_preset(wm, km);
1514                         addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1515
1516                         /* diff */
1517                         if (defaultmap)
1518                                 wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, km);
1519                 }
1520         }
1521
1522         /* create user key configuration from preset + addon + user preferences */
1523         for (km = wm->defaultconf->keymaps.first; km; km = km->next) {
1524                 /* find keymaps */
1525                 defaultmap = wm_keymap_preset(wm, km);
1526                 addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1527                 usermap = WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
1528
1529                 wm_user_modal_keymap_set_items(wm, defaultmap);
1530
1531                 /* add */
1532                 kmn = wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
1533
1534                 if (kmn) {
1535                         kmn->modal_items = km->modal_items;
1536                         kmn->poll = km->poll;
1537                 }
1538
1539                 /* in case of old non-diff keymaps, force extra update to create diffs */
1540                 compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
1541
1542         }
1543
1544         wm_keymap_update_flag &= ~WM_KEYMAP_UPDATE_RECONFIGURE;
1545
1546         BLI_assert(wm_keymap_update_flag == 0);
1547
1548         if (compat_update) {
1549                 WM_keyconfig_update_tag(NULL, NULL);
1550                 WM_keyconfig_update(wm);
1551         }
1552 }
1553
1554 /********************************* Event Handling *****************************
1555  * Handlers have pointers to the keymap in the default configuration. During
1556  * event handling this function is called to get the keymap from the final
1557  * configuration. */
1558
1559 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
1560 {
1561         wmKeyMap *km;
1562
1563         if (!keymap)
1564                 return NULL;
1565
1566         /* first user defined keymaps */
1567         km = WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1568
1569         if (km)
1570                 return km;
1571
1572         return keymap;
1573 }
1574
1575 /******************************* Keymap Editor ********************************
1576  * In the keymap editor the user key configuration is edited. */
1577
1578 void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
1579 {
1580         wmWindowManager *wm = CTX_wm_manager(C);
1581         wmKeyMap *defaultmap, *addonmap;
1582         wmKeyMapItem *orig;
1583
1584         if (!keymap)
1585                 return;
1586
1587         /* construct default keymap from preset + addons */
1588         defaultmap = wm_keymap_preset(wm, keymap);
1589         addonmap = WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1590
1591         if (addonmap) {
1592                 defaultmap = wm_keymap_copy(defaultmap);
1593                 wm_keymap_addon_add(defaultmap, addonmap);
1594         }
1595
1596         /* find original item */
1597         orig = WM_keymap_item_find_id(defaultmap, kmi->id);
1598
1599         if (orig) {
1600                 /* restore to original */
1601                 if (!STREQ(orig->idname, kmi->idname)) {
1602                         BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
1603                         WM_keymap_properties_reset(kmi, NULL);
1604                 }
1605
1606                 if (orig->properties) {
1607                         if (kmi->properties) {
1608                                 IDP_FreeProperty(kmi->properties);
1609                                 MEM_freeN(kmi->properties);
1610                                 kmi->properties = NULL;
1611                         }
1612
1613                         kmi->properties = IDP_CopyProperty(orig->properties);
1614                         kmi->ptr->data = kmi->properties;
1615                 }
1616
1617                 kmi->propvalue = orig->propvalue;
1618                 kmi->type = orig->type;
1619                 kmi->val = orig->val;
1620                 kmi->shift = orig->shift;
1621                 kmi->ctrl = orig->ctrl;
1622                 kmi->alt = orig->alt;
1623                 kmi->oskey = orig->oskey;
1624                 kmi->keymodifier = orig->keymodifier;
1625                 kmi->maptype = orig->maptype;
1626
1627                 WM_keyconfig_update_tag(keymap, kmi);
1628         }
1629
1630         /* free temporary keymap */
1631         if (addonmap) {
1632                 WM_keymap_free(defaultmap);
1633                 MEM_freeN(defaultmap);
1634         }
1635 }
1636
1637 void WM_keymap_restore_to_default(wmKeyMap *keymap, bContext *C)
1638 {
1639         wmWindowManager *wm = CTX_wm_manager(C);
1640         wmKeyMap *usermap;
1641
1642         /* remove keymap from U.user_keymaps and update */
1643         usermap = WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1644
1645         if (usermap) {
1646                 WM_keymap_free(usermap);
1647                 BLI_freelinkN(&U.user_keymaps, usermap);
1648
1649                 WM_keyconfig_update_tag(NULL, NULL);
1650                 WM_keyconfig_update(wm);
1651         }
1652 }
1653
1654 wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
1655 {
1656         wmKeyMapItem *kmi;
1657
1658         for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
1659                 if (kmi->id == id) {
1660                         return kmi;
1661                 }
1662         }
1663
1664         return NULL;
1665 }
1666
1667 /* Guess an appropriate keymap from the operator name */
1668 /* Needs to be kept up to date with Keymap and Operator naming */
1669 wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
1670 {
1671         /* Op types purposely skipped  for now:
1672          *     BRUSH_OT
1673          *     BOID_OT
1674          *     BUTTONS_OT
1675          *     CONSTRAINT_OT
1676          *     PAINT_OT
1677          *     ED_OT
1678          *     FLUID_OT
1679          *     TEXTURE_OT
1680          *     UI_OT
1681          *     VIEW2D_OT
1682          *     WORLD_OT
1683          */
1684
1685         wmKeyMap *km = NULL;
1686         SpaceLink *sl = CTX_wm_space_data(C);
1687
1688         /* Window */
1689         if (STRPREFIX(opname, "WM_OT")) {
1690                 km = WM_keymap_find_all(C, "Window", 0, 0);
1691         }
1692         /* Screen & Render */
1693         else if (STRPREFIX(opname, "SCREEN_OT") ||
1694                  STRPREFIX(opname, "RENDER_OT") ||
1695                  STRPREFIX(opname, "SOUND_OT") ||
1696                  STRPREFIX(opname, "SCENE_OT"))
1697         {
1698                 km = WM_keymap_find_all(C, "Screen", 0, 0);
1699         }
1700         /* Grease Pencil */
1701         else if (STRPREFIX(opname, "GPENCIL_OT")) {
1702                 km = WM_keymap_find_all(C, "Grease Pencil", 0, 0);
1703         }
1704         /* Markers */
1705         else if (STRPREFIX(opname, "MARKER_OT")) {
1706                 km = WM_keymap_find_all(C, "Markers", 0, 0);
1707         }
1708         /* Import/Export*/
1709         else if (STRPREFIX(opname, "IMPORT_") ||
1710                  STRPREFIX(opname, "EXPORT_"))
1711         {
1712                 km = WM_keymap_find_all(C, "Window", 0, 0);
1713         }
1714
1715
1716         /* 3D View */
1717         else if (STRPREFIX(opname, "VIEW3D_OT")) {
1718                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1719         }
1720         else if (STRPREFIX(opname, "OBJECT_OT")) {
1721                 /* exception, this needs to work outside object mode too */
1722                 if (STRPREFIX(opname, "OBJECT_OT_mode_set"))
1723                         km = WM_keymap_find_all(C, "Object Non-modal", 0, 0);
1724                 else
1725                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1726         }
1727         /* Object mode related */
1728         else if (STRPREFIX(opname, "GROUP_OT") ||
1729                  STRPREFIX(opname, "MATERIAL_OT") ||
1730                  STRPREFIX(opname, "PTCACHE_OT") ||
1731                  STRPREFIX(opname, "RIGIDBODY_OT"))
1732         {
1733                 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1734         }
1735
1736         /* Editing Modes */
1737         else if (STRPREFIX(opname, "MESH_OT")) {
1738                 km = WM_keymap_find_all(C, "Mesh", 0, 0);
1739
1740                 /* some mesh operators are active in object mode too, like add-prim */
1741                 if (km && !WM_keymap_poll((bContext *)C, km)) {
1742                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1743                 }
1744         }
1745         else if (STRPREFIX(opname, "CURVE_OT") ||
1746                  STRPREFIX(opname, "SURFACE_OT"))
1747         {
1748                 km = WM_keymap_find_all(C, "Curve", 0, 0);
1749
1750                 /* some curve operators are active in object mode too, like add-prim */
1751                 if (km && !WM_keymap_poll((bContext *)C, km)) {
1752                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1753                 }
1754         }
1755         else if (STRPREFIX(opname, "ARMATURE_OT") ||
1756                  STRPREFIX(opname, "SKETCH_OT"))
1757         {
1758                 km = WM_keymap_find_all(C, "Armature", 0, 0);
1759         }
1760         else if (STRPREFIX(opname, "POSE_OT") ||
1761                  STRPREFIX(opname, "POSELIB_OT"))
1762         {
1763                 km = WM_keymap_find_all(C, "Pose", 0, 0);
1764         }
1765         else if (STRPREFIX(opname, "SCULPT_OT")) {
1766                 switch (CTX_data_mode_enum(C)) {
1767                         case OB_MODE_SCULPT:
1768                                 km = WM_keymap_find_all(C, "Sculpt", 0, 0);
1769                                 break;
1770                         case OB_MODE_EDIT:
1771                                 km = WM_keymap_find_all(C, "UV Sculpt", 0, 0);
1772                                 break;
1773                 }
1774         }
1775         else if (STRPREFIX(opname, "MBALL_OT")) {
1776                 km = WM_keymap_find_all(C, "Metaball", 0, 0);
1777
1778                 /* some mball operators are active in object mode too, like add-prim */
1779                 if (km && !WM_keymap_poll((bContext *)C, km)) {
1780                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1781                 }
1782         }
1783         else if (STRPREFIX(opname, "LATTICE_OT")) {
1784                 km = WM_keymap_find_all(C, "Lattice", 0, 0);
1785         }
1786         else if (STRPREFIX(opname, "PARTICLE_OT")) {
1787                 km = WM_keymap_find_all(C, "Particle", 0, 0);
1788         }
1789         else if (STRPREFIX(opname, "FONT_OT")) {
1790                 km = WM_keymap_find_all(C, "Font", 0, 0);
1791         }
1792         /* Paint Face Mask */
1793         else if (STRPREFIX(opname, "PAINT_OT_face_select")) {
1794                 km = WM_keymap_find_all(C, "Face Mask", 0, 0);
1795         }
1796         else if (STRPREFIX(opname, "PAINT_OT")) {
1797                 /* check for relevant mode */
1798                 switch (CTX_data_mode_enum(C)) {
1799                         case OB_MODE_WEIGHT_PAINT:
1800                                 km = WM_keymap_find_all(C, "Weight Paint", 0, 0);
1801                                 break;
1802                         case OB_MODE_VERTEX_PAINT:
1803                                 km = WM_keymap_find_all(C, "Vertex Paint", 0, 0);
1804                                 break;
1805                         case OB_MODE_TEXTURE_PAINT:
1806                                 km = WM_keymap_find_all(C, "Image Paint", 0, 0);
1807                                 break;
1808                 }
1809         }
1810         /* Timeline */
1811         else if (STRPREFIX(opname, "TIME_OT")) {
1812                 km = WM_keymap_find_all(C, "Timeline", sl->spacetype, 0);
1813         }
1814         /* Image Editor */
1815         else if (STRPREFIX(opname, "IMAGE_OT")) {
1816                 km = WM_keymap_find_all(C, "Image", sl->spacetype, 0);
1817         }
1818         /* Clip Editor */
1819         else if (STRPREFIX(opname, "CLIP_OT")) {
1820                 km = WM_keymap_find_all(C, "Clip", sl->spacetype, 0);
1821         }
1822         else if (STRPREFIX(opname, "MASK_OT")) {
1823                 km = WM_keymap_find_all(C, "Mask Editing", 0, 0);
1824         }
1825         /* UV Editor */
1826         else if (STRPREFIX(opname, "UV_OT")) {
1827                 /* Hack to allow using UV unwrapping ops from 3DView/editmode.
1828                  * Mesh keymap is probably not ideal, but best place I could find to put those. */
1829                 if (sl->spacetype == SPACE_VIEW3D) {
1830                         km = WM_keymap_find_all(C, "Mesh", 0, 0);
1831                         if (km && !WM_keymap_poll((bContext *)C, km)) {
1832                                 km = NULL;
1833                         }
1834                 }
1835                 if (!km) {
1836                         km = WM_keymap_find_all(C, "UV Editor", 0, 0);
1837                 }
1838         }
1839         /* Node Editor */
1840         else if (STRPREFIX(opname, "NODE_OT")) {
1841                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1842         }
1843         /* Animation Editor Channels */
1844         else if (STRPREFIX(opname, "ANIM_OT_channels")) {
1845                 km = WM_keymap_find_all(C, "Animation Channels", 0, 0);
1846         }
1847         /* Animation Generic - after channels */
1848         else if (STRPREFIX(opname, "ANIM_OT")) {
1849                 km = WM_keymap_find_all(C, "Animation", 0, 0);
1850         }
1851         /* Graph Editor */
1852         else if (STRPREFIX(opname, "GRAPH_OT")) {
1853                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1854         }
1855         /* Dopesheet Editor */
1856         else if (STRPREFIX(opname, "ACTION_OT")) {
1857                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1858         }
1859         /* NLA Editor */
1860         else if (STRPREFIX(opname, "NLA_OT")) {
1861                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1862         }
1863         /* Script */
1864         else if (STRPREFIX(opname, "SCRIPT_OT")) {
1865                 km = WM_keymap_find_all(C, "Script", sl->spacetype, 0);
1866         }
1867         /* Text */
1868         else if (STRPREFIX(opname, "TEXT_OT")) {
1869                 km = WM_keymap_find_all(C, "Text", sl->spacetype, 0);
1870         }
1871         /* Sequencer */
1872         else if (STRPREFIX(opname, "SEQUENCER_OT")) {
1873                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1874         }
1875         /* Console */
1876         else if (STRPREFIX(opname, "CONSOLE_OT")) {
1877                 km = WM_keymap_find_all(C, "Console", sl->spacetype, 0);
1878         }
1879         /* Console */
1880         else if (STRPREFIX(opname, "INFO_OT")) {
1881                 km = WM_keymap_find_all(C, "Info", sl->spacetype, 0);
1882         }
1883         /* File browser */
1884         else if (STRPREFIX(opname, "FILE_OT")) {
1885                 km = WM_keymap_find_all(C, "File Browser", sl->spacetype, 0);
1886         }
1887         /* Logic Editor */
1888         else if (STRPREFIX(opname, "LOGIC_OT")) {
1889                 km = WM_keymap_find_all(C, "Logic Editor", sl->spacetype, 0);
1890         }
1891         /* Outliner */
1892         else if (STRPREFIX(opname, "OUTLINER_OT")) {
1893                 km = WM_keymap_find_all(C, "Outliner", sl->spacetype, 0);
1894         }
1895         /* Transform */
1896         else if (STRPREFIX(opname, "TRANSFORM_OT")) {
1897                 /* check for relevant editor */
1898                 switch (sl->spacetype) {
1899                         case SPACE_VIEW3D:
1900                                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1901                                 break;
1902                         case SPACE_IPO:
1903                                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1904                                 break;
1905                         case SPACE_ACTION:
1906                                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1907                                 break;
1908                         case SPACE_NLA:
1909                                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1910                                 break;
1911                         case SPACE_IMAGE:
1912                                 km = WM_keymap_find_all(C, "UV Editor", 0, 0);
1913                                 break;
1914                         case SPACE_NODE:
1915                                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1916                                 break;
1917                         case SPACE_SEQ:
1918                                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1919                                 break;
1920                 }
1921         }
1922
1923         return km;
1924 }
1925
1926 const char *WM_bool_as_string(bool test)
1927 {
1928         return test ? IFACE_("ON") : IFACE_("OFF");
1929 }