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