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