remove mesh and object arguments from bmesh operators, these are stored within the...
[blender.git] / source / blender / windowmanager / intern / wm_keymap.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/windowmanager/intern/wm_keymap.c
30  *  \ingroup wm
31  */
32
33
34 #include <string.h>
35
36 #include "DNA_object_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_userdef_types.h"
40 #include "DNA_windowmanager_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_blender.h"
48 #include "BKE_context.h"
49 #include "BKE_idprop.h"
50 #include "BKE_library.h"
51 #include "BKE_main.h"
52 #include "BKE_screen.h"
53
54
55 #include "RNA_access.h"
56 #include "RNA_enum_types.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60 #include "wm_window.h"
61 #include "wm_event_system.h"
62 #include "wm_event_types.h"
63
64 /******************************* Keymap Item **********************************
65  * Item in a keymap, that maps from an event to an operator or modal map item */
66
67 static wmKeyMapItem *wm_keymap_item_copy(wmKeyMapItem *kmi)
68 {
69         wmKeyMapItem *kmin = MEM_dupallocN(kmi);
70
71         kmin->prev= kmin->next= NULL;
72         kmin->flag &= ~KMI_UPDATE;
73
74         if(kmin->properties) {
75                 kmin->ptr= MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
76                 WM_operator_properties_create(kmin->ptr, kmin->idname);
77
78                 kmin->properties= IDP_CopyProperty(kmin->properties);
79                 kmin->ptr->data= kmin->properties;
80         }
81
82         return kmin;
83 }
84
85 static void wm_keymap_item_free(wmKeyMapItem *kmi)
86 {
87         /* not kmi itself */
88         if(kmi->ptr) {
89                 WM_operator_properties_free(kmi->ptr);
90                 MEM_freeN(kmi->ptr);
91         }
92 }
93
94 static void wm_keymap_item_properties_set(wmKeyMapItem *kmi)
95 {
96         WM_operator_properties_alloc(&(kmi->ptr), &(kmi->properties), kmi->idname);
97         WM_operator_properties_sanitize(kmi->ptr, 1);
98 }
99
100 static int wm_keymap_item_equals_result(wmKeyMapItem *a, wmKeyMapItem *b)
101 {
102         if(strcmp(a->idname, b->idname) != 0)
103                 return 0;
104         
105         if(!((a->ptr==NULL && b->ptr==NULL) ||
106              (a->ptr && b->ptr && IDP_EqualsProperties(a->ptr->data, b->ptr->data))))
107                 return 0;
108         
109         if((a->flag & KMI_INACTIVE) != (b->flag & KMI_INACTIVE))
110                 return 0;
111         
112         return (a->propvalue == b->propvalue);
113 }
114
115 static int wm_keymap_item_equals(wmKeyMapItem *a, wmKeyMapItem *b)
116 {
117         return (wm_keymap_item_equals_result(a, b) &&
118                 a->type == b->type &&
119                 a->val == b->val &&
120                 a->shift == b->shift &&
121                 a->ctrl == b->ctrl &&
122                 a->alt == b->alt &&
123                 a->oskey == b->oskey &&
124                 a->keymodifier == b->keymodifier &&
125                 a->maptype == b->maptype);
126 }
127
128 /* properties can be NULL, otherwise the arg passed is used and ownership is given to the kmi */
129 void WM_keymap_properties_reset(wmKeyMapItem *kmi, struct IDProperty *properties)
130 {
131         WM_operator_properties_free(kmi->ptr);
132         MEM_freeN(kmi->ptr);
133
134         kmi->ptr = NULL;
135         kmi->properties = properties;
136
137         wm_keymap_item_properties_set(kmi);
138 }
139
140 /**************************** Keymap Diff Item *********************************
141  * Item in a diff keymap, used for saving diff of keymaps in user preferences */
142
143 static wmKeyMapDiffItem *wm_keymap_diff_item_copy(wmKeyMapDiffItem *kmdi)
144 {
145         wmKeyMapDiffItem *kmdin = MEM_dupallocN(kmdi);
146
147         kmdin->next = kmdin->prev = NULL;
148         if(kmdi->add_item)
149                 kmdin->add_item = wm_keymap_item_copy(kmdi->add_item);
150         if(kmdi->remove_item)
151                 kmdin->remove_item = wm_keymap_item_copy(kmdi->remove_item);
152         
153         return kmdin;
154 }
155
156 static void wm_keymap_diff_item_free(wmKeyMapDiffItem *kmdi)
157 {
158         if(kmdi->remove_item) {
159                 wm_keymap_item_free(kmdi->remove_item);
160                 MEM_freeN(kmdi->remove_item);
161         }
162         if(kmdi->add_item) {
163                 wm_keymap_item_free(kmdi->add_item);
164                 MEM_freeN(kmdi->add_item);
165         }
166 }
167
168 /***************************** Key Configuration ******************************
169  * List of keymaps for all editors, modes, ... . There is a builtin default key
170  * configuration, a user key configuration, and other preset configurations. */
171
172 wmKeyConfig *WM_keyconfig_new(wmWindowManager *wm, const char *idname)
173 {
174         wmKeyConfig *keyconf;
175         
176         keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
177         BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
178         BLI_addtail(&wm->keyconfigs, keyconf);
179
180         return keyconf;
181 }
182
183 wmKeyConfig *WM_keyconfig_new_user(wmWindowManager *wm, const char *idname)
184 {
185         wmKeyConfig *keyconf = WM_keyconfig_new(wm, idname);
186
187         keyconf->flag |= KEYCONF_USER;
188
189         return keyconf;
190 }
191
192 void WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
193 {
194         if (keyconf) {
195                 if (strncmp(U.keyconfigstr, keyconf->idname, sizeof(U.keyconfigstr)) == 0) {
196                         BLI_strncpy(U.keyconfigstr, wm->defaultconf->idname, sizeof(U.keyconfigstr));
197                         WM_keyconfig_update_tag(NULL, NULL);
198                 }
199
200                 BLI_remlink(&wm->keyconfigs, keyconf);
201                 WM_keyconfig_free(keyconf);
202         }
203 }
204
205 void WM_keyconfig_free(wmKeyConfig *keyconf)
206 {
207         wmKeyMap *km;
208
209         while((km= keyconf->keymaps.first)) {
210                 WM_keymap_free(km);
211                 BLI_freelinkN(&keyconf->keymaps, km);
212         }
213
214         MEM_freeN(keyconf);
215 }
216
217 static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
218 {
219         wmKeyConfig *kc;
220
221         for(kc= lb->first; kc; kc= kc->next)
222                 if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
223                         return kc;
224         
225         return NULL;
226 }
227
228 static wmKeyConfig *WM_keyconfig_active(wmWindowManager *wm)
229 {
230         wmKeyConfig *keyconf;
231
232         /* first try from preset */
233         keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
234         if(keyconf)
235                 return keyconf;
236         
237         /* otherwise use default */
238         return wm->defaultconf;
239 }
240
241 void WM_keyconfig_set_active(wmWindowManager *wm, const char *idname)
242 {
243         /* setting a different key configuration as active: we ensure all is
244            updated properly before and after making the change */
245
246         WM_keyconfig_update(wm);
247
248         BLI_strncpy(U.keyconfigstr, idname, sizeof(U.keyconfigstr));
249
250         WM_keyconfig_update_tag(NULL, NULL);
251         WM_keyconfig_update(wm);
252 }
253
254 /********************************** Keymap *************************************
255  * List of keymap items for one editor, mode, modal operator, ... */
256
257 static wmKeyMap *wm_keymap_new(const char *idname, int spaceid, int regionid)
258 {
259         wmKeyMap *km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
260
261         BLI_strncpy(km->idname, idname, KMAP_MAX_NAME);
262         km->spaceid= spaceid;
263         km->regionid= regionid;
264
265         return km;
266 }
267
268 static wmKeyMap *wm_keymap_copy(wmKeyMap *keymap)
269 {
270         wmKeyMap *keymapn = MEM_dupallocN(keymap);
271         wmKeyMapItem *kmi, *kmin;
272         wmKeyMapDiffItem *kmdi, *kmdin;
273
274         keymapn->modal_items= keymap->modal_items;
275         keymapn->poll= keymap->poll;
276         keymapn->items.first= keymapn->items.last= NULL;
277         keymapn->flag &= ~(KEYMAP_UPDATE|KEYMAP_EXPANDED);
278
279         for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
280                 kmdin= wm_keymap_diff_item_copy(kmdi);
281                 BLI_addtail(&keymapn->items, kmdin);
282         }
283
284         for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
285                 kmin= wm_keymap_item_copy(kmi);
286                 BLI_addtail(&keymapn->items, kmin);
287         }
288
289         return keymapn;
290 }
291
292 void WM_keymap_free(wmKeyMap *keymap)
293 {
294         wmKeyMapItem *kmi;
295         wmKeyMapDiffItem *kmdi;
296
297         for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next)
298                 wm_keymap_diff_item_free(kmdi);
299
300         for(kmi=keymap->items.first; kmi; kmi=kmi->next)
301                 wm_keymap_item_free(kmi);
302
303         BLI_freelistN(&keymap->diff_items);
304         BLI_freelistN(&keymap->items);
305 }
306
307 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
308 {
309         kmi->type= type;
310         kmi->val= val;
311         kmi->keymodifier= keymodifier;
312         
313         if(modifier == KM_ANY) {
314                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
315         }
316         else {
317                 
318                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
319                 
320                 /* defines? */
321                 if(modifier & KM_SHIFT)
322                         kmi->shift= 1;
323                 else if(modifier & KM_SHIFT2)
324                         kmi->shift= 2;
325                 if(modifier & KM_CTRL)
326                         kmi->ctrl= 1;
327                 else if(modifier & KM_CTRL2)
328                         kmi->ctrl= 2;
329                 if(modifier & KM_ALT)
330                         kmi->alt= 1;
331                 else if(modifier & KM_ALT2)
332                         kmi->alt= 2;
333                 if(modifier & KM_OSKEY)
334                         kmi->oskey= 1;
335                 else if(modifier & KM_OSKEY2)
336                         kmi->oskey= 2;  
337         }
338 }
339
340 static void keymap_item_set_id(wmKeyMap *keymap, wmKeyMapItem *kmi)
341 {
342         keymap->kmi_id++;
343         if ((keymap->flag & KEYMAP_USER) == 0) {
344                 kmi->id = keymap->kmi_id;
345         } else {
346                 kmi->id = -keymap->kmi_id; // User defined keymap entries have negative ids
347         }
348 }
349
350 /* if item was added, then bail out */
351 wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
352 {
353         wmKeyMapItem *kmi;
354         
355         for(kmi= keymap->items.first; kmi; kmi= kmi->next)
356                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
357                         break;
358         if(kmi==NULL) {
359                 kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
360                 
361                 BLI_addtail(&keymap->items, kmi);
362                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
363                 
364                 keymap_item_set_id(keymap, kmi);
365
366                 keymap_event_set(kmi, type, val, modifier, keymodifier);
367                 wm_keymap_item_properties_set(kmi);
368         }
369         return kmi;
370 }
371
372 /* always add item */
373 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
374 {
375         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
376         
377         BLI_addtail(&keymap->items, kmi);
378         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
379
380         keymap_event_set(kmi, type, val, modifier, keymodifier);
381         wm_keymap_item_properties_set(kmi);
382
383         keymap_item_set_id(keymap, kmi);
384
385         WM_keyconfig_update_tag(keymap, kmi);
386
387         return kmi;
388 }
389
390 /* menu wrapper for WM_keymap_add_item */
391 wmKeyMapItem *WM_keymap_add_menu(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
392 {
393         wmKeyMapItem *kmi= WM_keymap_add_item(keymap, "WM_OT_call_menu", type, val, modifier, keymodifier);
394         RNA_string_set(kmi->ptr, "name", idname);
395         return kmi;
396 }
397
398 void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
399 {
400         if(BLI_findindex(&keymap->items, kmi) != -1) {
401                 if(kmi->ptr) {
402                         WM_operator_properties_free(kmi->ptr);
403                         MEM_freeN(kmi->ptr);
404                 }
405                 BLI_freelinkN(&keymap->items, kmi);
406
407                 WM_keyconfig_update_tag(keymap, kmi);
408         }
409 }
410
411 /************************** Keymap Diff and Patch ****************************
412  * Rather than saving the entire keymap for user preferences, we only save a
413  * diff so that changes in the defaults get synced. This system is not perfect
414  * but works better than overriding the keymap entirely when only few items
415  * are changed. */
416
417 static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
418 {
419         wmKeyMapItem *kmi, *kmin;
420
421         for(kmi=addonmap->items.first; kmi; kmi=kmi->next) {
422                 kmin = wm_keymap_item_copy(kmi);
423                 keymap_item_set_id(keymap, kmin);
424                 BLI_addhead(&keymap->items, kmin);
425         }
426 }
427
428 static wmKeyMapItem *wm_keymap_find_item_equals(wmKeyMap *km, wmKeyMapItem *needle)
429 {
430         wmKeyMapItem *kmi;
431
432         for(kmi=km->items.first; kmi; kmi=kmi->next)
433                 if(wm_keymap_item_equals(kmi, needle))
434                         return kmi;
435         
436         return NULL;
437 }
438
439 static wmKeyMapItem *wm_keymap_find_item_equals_result(wmKeyMap *km, wmKeyMapItem *needle)
440 {
441         wmKeyMapItem *kmi;
442
443         for(kmi=km->items.first; kmi; kmi=kmi->next)
444                 if(wm_keymap_item_equals_result(kmi, needle))
445                         return kmi;
446         
447         return NULL;
448 }
449
450 static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km, wmKeyMap *orig_km, wmKeyMap *addon_km)
451 {
452         wmKeyMapItem *kmi, *to_kmi, *orig_kmi;
453         wmKeyMapDiffItem *kmdi;
454
455         for(kmi=from_km->items.first; kmi; kmi=kmi->next) {
456                 to_kmi = WM_keymap_item_find_id(to_km, kmi->id);
457
458                 if(!to_kmi) {
459                         /* remove item */
460                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
461                         kmdi->remove_item = wm_keymap_item_copy(kmi);
462                         BLI_addtail(&diff_km->diff_items, kmdi);
463                 }
464                 else if(to_kmi && !wm_keymap_item_equals(kmi, to_kmi)) {
465                         /* replace item */
466                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
467                         kmdi->remove_item = wm_keymap_item_copy(kmi);
468                         kmdi->add_item = wm_keymap_item_copy(to_kmi);
469                         BLI_addtail(&diff_km->diff_items, kmdi);
470                 }
471
472                 /* sync expanded flag back to original so we don't loose it on repatch */
473                 if(to_kmi) {
474                         orig_kmi = WM_keymap_item_find_id(orig_km, kmi->id);
475
476                         if(!orig_kmi)
477                                 orig_kmi = wm_keymap_find_item_equals(addon_km, kmi);
478
479                         if(orig_kmi) {
480                                 orig_kmi->flag &= ~KMI_EXPANDED;
481                                 orig_kmi->flag |= (to_kmi->flag & KMI_EXPANDED);
482                         }
483                 }
484         }
485
486         for(kmi=to_km->items.first; kmi; kmi=kmi->next) {
487                 if(kmi->id < 0) {
488                         /* add item */
489                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
490                         kmdi->add_item = wm_keymap_item_copy(kmi);
491                         BLI_addtail(&diff_km->diff_items, kmdi);
492                 }
493         }
494 }
495
496 static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
497 {
498         wmKeyMapDiffItem *kmdi;
499         wmKeyMapItem *kmi_remove, *kmi_add;
500
501         for(kmdi=diff_km->diff_items.first; kmdi; kmdi=kmdi->next) {
502                 /* find item to remove */
503                 kmi_remove = NULL;
504                 if(kmdi->remove_item) {
505                         kmi_remove = wm_keymap_find_item_equals(km, kmdi->remove_item);
506                         if(!kmi_remove)
507                                 kmi_remove = wm_keymap_find_item_equals_result(km, kmdi->remove_item);
508                 }
509
510                 /* add item */
511                 if(kmdi->add_item) {
512                         /* only if nothing to remove or item to remove found */
513                         if(!kmdi->remove_item || kmi_remove) {
514                                 kmi_add = wm_keymap_item_copy(kmdi->add_item);
515                                 kmi_add->flag |= KMI_USER_MODIFIED;
516
517                                 if(kmi_remove) {
518                                         kmi_add->flag &= ~KMI_EXPANDED;
519                                         kmi_add->flag |= (kmi_remove->flag & KMI_EXPANDED);
520                                         kmi_add->id = kmi_remove->id;
521                                         BLI_insertlinkbefore(&km->items, kmi_remove, kmi_add);
522                                 }
523                                 else {
524                                         keymap_item_set_id(km, kmi_add);
525                                         BLI_addtail(&km->items, kmi_add);
526                                 }
527                         }
528                 }
529
530                 /* remove item */
531                 if(kmi_remove) {
532                         wm_keymap_item_free(kmi_remove);
533                         BLI_freelinkN(&km->items, kmi_remove);
534                 }
535         }
536 }
537
538 static wmKeyMap *wm_keymap_patch_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *usermap)
539 {
540         wmKeyMap *km;
541         int expanded = 0;
542
543         /* remove previous keymap in list, we will replace it */
544         km = WM_keymap_list_find(lb, defaultmap->idname, defaultmap->spaceid, defaultmap->regionid);
545         if(km) {
546                 expanded = (km->flag & (KEYMAP_EXPANDED|KEYMAP_CHILDREN_EXPANDED));
547                 WM_keymap_free(km);
548                 BLI_freelinkN(lb, km);
549         }
550
551         /* copy new keymap from an existing one */
552         if(usermap && !(usermap->flag & KEYMAP_DIFF)) {
553                 /* for compatibiltiy with old user preferences with non-diff
554                    keymaps we override the original entirely */
555                 wmKeyMapItem *kmi, *orig_kmi;
556
557                 km = wm_keymap_copy(usermap);
558
559                 /* try to find corresponding id's for items */
560                 for(kmi=km->items.first; kmi; kmi=kmi->next) {
561                         orig_kmi = wm_keymap_find_item_equals(defaultmap, kmi);
562                         if(!orig_kmi)
563                                 orig_kmi = wm_keymap_find_item_equals_result(defaultmap, kmi);
564
565                         if(orig_kmi)
566                                 kmi->id = orig_kmi->id;
567                         else
568                                 kmi->id = -(km->kmi_id++);
569                 }
570
571                 km->flag |= KEYMAP_UPDATE; /* update again to create diff */
572         }
573         else
574                 km = wm_keymap_copy(defaultmap);
575
576         /* add addon keymap items */
577         if(addonmap)
578                 wm_keymap_addon_add(km, addonmap);
579
580         /* tag as being user edited */
581         if(usermap)
582                 km->flag |= KEYMAP_USER_MODIFIED;
583         km->flag |= KEYMAP_USER|expanded;
584
585         /* apply user changes of diff keymap */
586         if(usermap && (usermap->flag & KEYMAP_DIFF))
587                 wm_keymap_patch(km, usermap);
588
589         /* add to list */
590         BLI_addtail(lb, km);
591         
592         return km;
593 }
594
595 static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *km)
596 {
597         wmKeyMap *diffmap, *prevmap, *origmap;
598
599         /* create temporary default + addon keymap for diff */
600         origmap = defaultmap;
601
602         if(addonmap) {
603                 defaultmap = wm_keymap_copy(defaultmap);
604                 wm_keymap_addon_add(defaultmap, addonmap);
605         }
606
607         /* remove previous diff keymap in list, we will replace it */
608         prevmap = WM_keymap_list_find(lb, km->idname, km->spaceid, km->regionid);
609         if(prevmap) {
610                 WM_keymap_free(prevmap);
611                 BLI_freelinkN(lb, prevmap);
612         }
613
614         /* create diff keymap */
615         diffmap= wm_keymap_new(km->idname, km->spaceid, km->regionid);
616         diffmap->flag |= KEYMAP_DIFF;
617         wm_keymap_diff(diffmap, defaultmap, km, origmap, addonmap);
618
619         /* add to list if not empty */
620         if(diffmap->diff_items.first) {
621                 BLI_addtail(lb, diffmap);
622         }
623         else {
624                 WM_keymap_free(diffmap);
625                 MEM_freeN(diffmap);
626         }
627
628         /* free temporary default map */
629         if(addonmap) {
630                 WM_keymap_free(defaultmap);
631                 MEM_freeN(defaultmap);
632         }
633 }
634
635 /* ****************** storage in WM ************ */
636
637 /* name id's are for storing general or multiple keymaps, 
638    space/region ids are same as DNA_space_types.h */
639 /* gets free'd in wm.c */
640
641 wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int regionid)
642 {
643         wmKeyMap *km;
644
645         for(km= lb->first; km; km= km->next)
646                 if(km->spaceid==spaceid && km->regionid==regionid)
647                         if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
648                                 return km;
649         
650         return NULL;
651 }
652
653 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
654 {
655         wmKeyMap *km= WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
656         
657         if(km==NULL) {
658                 km= wm_keymap_new(idname, spaceid, regionid);
659                 BLI_addtail(&keyconf->keymaps, km);
660
661                 WM_keyconfig_update_tag(km, NULL);
662         }
663         
664         return km;
665 }
666
667 wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid, int regionid)
668 {
669         wmWindowManager *wm= CTX_wm_manager(C);
670
671         return WM_keymap_list_find(&wm->userconf->keymaps, idname, spaceid, regionid);
672 }
673
674 /* ****************** modal keymaps ************ */
675
676 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
677
678 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, const char *idname, EnumPropertyItem *items)
679 {
680         wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
681         km->flag |= KEYMAP_MODAL;
682         km->modal_items= items;
683         
684         return km;
685 }
686
687 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, const char *idname)
688 {
689         wmKeyMap *km;
690         
691         for(km= keyconf->keymaps.first; km; km= km->next)
692                 if(km->flag & KEYMAP_MODAL)
693                         if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
694                                 break;
695         
696         return km;
697 }
698
699
700 wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
701 {
702         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
703         
704         BLI_addtail(&km->items, kmi);
705         kmi->propvalue= value;
706         
707         keymap_event_set(kmi, type, val, modifier, keymodifier);
708
709         keymap_item_set_id(km, kmi);
710
711         WM_keyconfig_update_tag(km, kmi);
712
713         return kmi;
714 }
715
716 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
717 {
718         wmOperatorType *ot= WM_operatortype_find(opname, 0);
719         
720         if(ot)
721                 ot->modalkeymap= km;
722         else
723                 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
724 }
725
726 /* ***************** get string from key events **************** */
727
728 const char *WM_key_event_string(short type)
729 {
730         const char *name= NULL;
731         if(RNA_enum_name(event_type_items, (int)type, &name))
732                 return name;
733         
734         return "";
735 }
736
737 char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
738 {
739         char buf[128];
740
741         buf[0]= 0;
742
743         if (kmi->shift == KM_ANY &&
744                 kmi->ctrl == KM_ANY &&
745                 kmi->alt == KM_ANY &&
746                 kmi->oskey == KM_ANY) {
747
748                 strcat(buf, "Any ");
749         } else {
750                 if(kmi->shift)
751                         strcat(buf, "Shift ");
752
753                 if(kmi->ctrl)
754                         strcat(buf, "Ctrl ");
755
756                 if(kmi->alt)
757                         strcat(buf, "Alt ");
758
759                 if(kmi->oskey)
760                         strcat(buf, "Cmd ");
761         }
762                 
763         if(kmi->keymodifier) {
764                 strcat(buf, WM_key_event_string(kmi->keymodifier));
765                 strcat(buf, " ");
766         }
767
768         strcat(buf, WM_key_event_string(kmi->type));
769         BLI_strncpy(str, buf, len);
770
771         return str;
772 }
773
774 static wmKeyMapItem *wm_keymap_item_find_handlers(const bContext *C, ListBase *handlers, const char *opname, int UNUSED(opcontext), IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
775 {
776         wmWindowManager *wm= CTX_wm_manager(C);
777         wmEventHandler *handler;
778         wmKeyMap *keymap;
779         wmKeyMapItem *kmi;
780
781         /* find keymap item in handlers */
782         for(handler=handlers->first; handler; handler=handler->next) {
783                 keymap= WM_keymap_active(wm, handler->keymap);
784
785                 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
786                         for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
787                                 
788                                 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
789                                         if (hotkey)
790                                                 if (!ISHOTKEY(kmi->type))
791                                                         continue;
792                                         
793                                         if(compare_props) {
794                                                 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
795                                                         if(keymap_r) *keymap_r= keymap;
796                                                         return kmi;
797                                                 }
798                                         }
799                                         else {
800                                                 if(keymap_r) *keymap_r= keymap;
801                                                 return kmi;
802                                         }
803                                 }
804                         }
805                 }
806         }
807         
808         /* ensure un-initialized keymap is never used */
809         if(keymap_r) *keymap_r= NULL;
810         return NULL;
811 }
812
813 static wmKeyMapItem *wm_keymap_item_find_props(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
814 {
815         wmWindow *win= CTX_wm_window(C);
816         ScrArea *sa= CTX_wm_area(C);
817         ARegion *ar= CTX_wm_region(C);
818         wmKeyMapItem *found= NULL;
819
820         /* look into multiple handler lists to find the item */
821         if(win)
822                 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
823         
824
825         if(sa && found==NULL)
826                 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
827
828         if(found==NULL) {
829                 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
830                         if(sa) {
831                                 if (!(ar && ar->regiontype == RGN_TYPE_WINDOW))
832                                         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
833                                 
834                                 if(ar)
835                                         found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
836                         }
837                 }
838                 else if(ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
839                         if (!(ar && ar->regiontype == RGN_TYPE_CHANNELS))
840                                 ar= BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
841
842                         if(ar)
843                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
844                 }
845                 else if(ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
846                         if (!(ar && ar->regiontype == RGN_TYPE_PREVIEW))
847                                 ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
848
849                         if(ar)
850                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
851                 }
852                 else {
853                         if(ar)
854                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
855                 }
856         }
857         
858         return found;
859 }
860
861 static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
862 {
863         wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, hotkey, keymap_r);
864
865         if(!found)
866                 found= wm_keymap_item_find_props(C, opname, opcontext, NULL, 0, hotkey, keymap_r);
867
868         return found;
869 }
870
871 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
872 {
873         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, 0, NULL);
874         
875         if(kmi) {
876                 WM_keymap_item_to_string(kmi, str, len);
877                 return str;
878         }
879
880         return NULL;
881 }
882
883 int WM_key_event_operator_id(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
884 {
885         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, hotkey, keymap_r);
886         
887         if(kmi)
888                 return kmi->id;
889         else
890                 return 0;
891 }
892
893 int     WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
894 {
895         int k1type, k2type;
896
897         if (k1->flag & KMI_INACTIVE || k2->flag & KMI_INACTIVE)
898                 return 0;
899
900         /* take event mapping into account */
901         k1type = WM_userdef_event_map(k1->type);
902         k2type = WM_userdef_event_map(k2->type);
903
904         if(k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
905                 return 0;
906
907         if(k1->val != KM_ANY && k2->val != KM_ANY) {
908                 /* take click, press, release conflict into account */
909                 if (k1->val == KM_CLICK && ELEM3(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
910                         return 0;
911                 if (k2->val == KM_CLICK && ELEM3(k1->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
912                         return 0;
913                 if (k1->val != k2->val)
914                         return 0;
915         }
916
917         if(k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
918                 return 0;
919
920         if(k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
921                 return 0;
922
923         if(k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
924                 return 0;
925
926         if(k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
927                 return 0;
928
929         if(k1->keymodifier != k2->keymodifier)
930                 return 0;
931
932         return 1;
933 }
934
935 /************************* Update Final Configuration *************************
936  * On load or other changes, the final user key configuration is rebuilt from
937  * the preset, addon and user preferences keymaps. We also test if the final
938  * configuration changed and write the changes to the user preferences. */
939
940 static int WM_KEYMAP_UPDATE = 0;
941
942 void WM_keyconfig_update_tag(wmKeyMap *km, wmKeyMapItem *kmi)
943 {
944         /* quick tag to do delayed keymap updates */
945         WM_KEYMAP_UPDATE= 1;
946
947         if(km)
948                 km->flag |= KEYMAP_UPDATE;
949         if(kmi)
950                 kmi->flag |= KMI_UPDATE;
951 }
952
953 static int wm_keymap_test_and_clear_update(wmKeyMap *km)
954 {
955         wmKeyMapItem *kmi;
956         int update;
957         
958         update= (km->flag & KEYMAP_UPDATE);
959         km->flag &= ~KEYMAP_UPDATE;
960
961         for(kmi=km->items.first; kmi; kmi=kmi->next) {
962                 update= update || (kmi->flag & KMI_UPDATE);
963                 kmi->flag &= ~KMI_UPDATE;
964         }
965         
966         return update;
967 }
968
969 static wmKeyMap *wm_keymap_preset(wmWindowManager *wm, wmKeyMap *km)
970 {
971         wmKeyConfig *keyconf= WM_keyconfig_active(wm);
972         wmKeyMap *keymap;
973
974         keymap= WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
975         if(!keymap)
976                 keymap= WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
977
978         return keymap;
979 }
980
981 void WM_keyconfig_update(wmWindowManager *wm)
982 {
983         wmKeyMap *km, *defaultmap, *addonmap, *usermap, *kmn;
984         wmKeyMapItem *kmi;
985         wmKeyMapDiffItem *kmdi;
986         int compat_update = 0;
987
988         if(!WM_KEYMAP_UPDATE)
989                 return;
990         
991         /* update operator properties for non-modal user keymaps */
992         for(km=U.user_keymaps.first; km; km=km->next) {
993                 if((km->flag & KEYMAP_MODAL) == 0) {
994                         for(kmdi=km->diff_items.first; kmdi; kmdi=kmdi->next) {
995                                 if(kmdi->add_item)
996                                         wm_keymap_item_properties_set(kmdi->add_item);
997                                 if(kmdi->remove_item)
998                                         wm_keymap_item_properties_set(kmdi->remove_item);
999                         }
1000
1001                         for(kmi=km->items.first; kmi; kmi=kmi->next)
1002                                 wm_keymap_item_properties_set(kmi);
1003                 }
1004         }
1005
1006         /* update U.user_keymaps with user key configuration changes */
1007         for(km=wm->userconf->keymaps.first; km; km=km->next) {
1008                 /* only diff if the user keymap was modified */
1009                 if(wm_keymap_test_and_clear_update(km)) {
1010                         /* find keymaps */
1011                         defaultmap= wm_keymap_preset(wm, km);
1012                         addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1013
1014                         /* diff */
1015                         if(defaultmap)
1016                                 wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, km);
1017                 }
1018         }
1019
1020         /* create user key configuration from preset + addon + user preferences */
1021         for(km=wm->defaultconf->keymaps.first; km; km=km->next) {
1022                 /* find keymaps */
1023                 defaultmap= wm_keymap_preset(wm, km);
1024                 addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1025                 usermap= WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
1026
1027                 /* add */
1028                 kmn= wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
1029
1030                 if(kmn) {
1031                         kmn->modal_items= km->modal_items;
1032                         kmn->poll= km->poll;
1033                 }
1034
1035                 /* in case of old non-diff keymaps, force extra update to create diffs */
1036                 compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
1037         }
1038
1039         WM_KEYMAP_UPDATE= 0;
1040
1041         if(compat_update) {
1042                 WM_keyconfig_update_tag(NULL, NULL);
1043                 WM_keyconfig_update(wm);
1044         }
1045 }
1046
1047 /********************************* Event Handling *****************************
1048  * Handlers have pointers to the keymap in the default configuration. During
1049  * event handling this function is called to get the keymap from the final
1050  * configuration. */
1051
1052 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
1053 {
1054         wmKeyMap *km;
1055
1056         if(!keymap)
1057                 return NULL;
1058         
1059         /* first user defined keymaps */
1060         km= WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1061
1062         if(km)
1063                 return km;
1064
1065         return keymap;
1066 }
1067
1068 /******************************* Keymap Editor ********************************
1069  * In the keymap editor the user key configuration is edited. */
1070
1071 void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
1072 {
1073         wmWindowManager *wm = CTX_wm_manager(C);
1074         wmKeyMap *defaultmap, *addonmap;
1075         wmKeyMapItem *orig;
1076
1077         if(!keymap)
1078                 return;
1079
1080         /* construct default keymap from preset + addons */
1081         defaultmap= wm_keymap_preset(wm, keymap);
1082         addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1083
1084         if(addonmap) {
1085                 defaultmap = wm_keymap_copy(defaultmap);
1086                 wm_keymap_addon_add(defaultmap, addonmap);
1087         }
1088
1089         /* find original item */
1090         orig = WM_keymap_item_find_id(defaultmap, kmi->id);
1091
1092         if(orig) {
1093                 /* restore to original */
1094                 if(strcmp(orig->idname, kmi->idname) != 0) {
1095                         BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
1096                         WM_keymap_properties_reset(kmi, NULL);
1097                 }
1098
1099                 if (orig->properties) {
1100                         if(kmi->properties) {
1101                                 IDP_FreeProperty(kmi->properties);
1102                                 MEM_freeN(kmi->properties);
1103                                 kmi->properties= NULL;
1104                         }
1105
1106                         kmi->properties= IDP_CopyProperty(orig->properties);
1107                         kmi->ptr->data= kmi->properties;
1108                 }
1109
1110                 kmi->propvalue = orig->propvalue;
1111                 kmi->type = orig->type;
1112                 kmi->val = orig->val;
1113                 kmi->shift = orig->shift;
1114                 kmi->ctrl = orig->ctrl;
1115                 kmi->alt = orig->alt;
1116                 kmi->oskey = orig->oskey;
1117                 kmi->keymodifier = orig->keymodifier;
1118                 kmi->maptype = orig->maptype;
1119
1120                 WM_keyconfig_update_tag(keymap, kmi);
1121         }
1122
1123         /* free temporary keymap */
1124         if(addonmap) {
1125                 WM_keymap_free(defaultmap);
1126                 MEM_freeN(defaultmap);
1127         }
1128 }
1129
1130 void WM_keymap_restore_to_default(wmKeyMap *keymap, bContext *C)
1131 {
1132         wmWindowManager *wm = CTX_wm_manager(C);
1133         wmKeyMap *usermap;
1134
1135         /* remove keymap from U.user_keymaps and update */
1136         usermap= WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1137
1138         if(usermap) {
1139                 WM_keymap_free(usermap);
1140                 BLI_freelinkN(&U.user_keymaps, usermap);
1141
1142                 WM_keyconfig_update_tag(NULL, NULL);
1143                 WM_keyconfig_update(wm);
1144         }
1145 }
1146
1147 wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
1148 {
1149         wmKeyMapItem *kmi;
1150         
1151         for (kmi=keymap->items.first; kmi; kmi=kmi->next) {
1152                 if (kmi->id == id) {
1153                         return kmi;
1154                 }
1155         }
1156         
1157         return NULL;
1158 }
1159
1160 /* Guess an appropriate keymap from the operator name */
1161 /* Needs to be kept up to date with Keymap and Operator naming */
1162 wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
1163 {
1164         wmKeyMap *km=NULL;
1165         SpaceLink *sl = CTX_wm_space_data(C);
1166         
1167         /* Window */
1168         if (strstr(opname, "WM_OT")) {
1169                 km = WM_keymap_find_all(C, "Window", 0, 0);
1170         }
1171         /* Screen */
1172         else if (strstr(opname, "SCREEN_OT")) {
1173                 km = WM_keymap_find_all(C, "Screen", 0, 0);
1174         }
1175         /* Grease Pencil */
1176         else if (strstr(opname, "GPENCIL_OT")) {
1177                 km = WM_keymap_find_all(C, "Grease Pencil", 0, 0);
1178         }
1179         /* Markers */
1180         else if (strstr(opname, "MARKER_OT")) {
1181                 km = WM_keymap_find_all(C, "Markers", 0, 0);
1182         }
1183         
1184         
1185         /* 3D View */
1186         else if (strstr(opname, "VIEW3D_OT")) {
1187                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1188         }
1189         else if (strstr(opname, "OBJECT_OT")) {
1190                 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1191         }
1192
1193         
1194         /* Editing Modes */
1195         else if (strstr(opname, "MESH_OT")) {
1196                 km = WM_keymap_find_all(C, "Mesh", 0, 0);
1197                 
1198                 /* some mesh operators are active in object mode too, like add-prim */
1199                 if(km && km->poll && km->poll((bContext *)C)==0) {
1200                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1201                 }
1202         }
1203         else if (strstr(opname, "CURVE_OT")) {
1204                 km = WM_keymap_find_all(C, "Curve", 0, 0);
1205                 
1206                 /* some curve operators are active in object mode too, like add-prim */
1207                 if(km && km->poll && km->poll((bContext *)C)==0) {
1208                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1209                 }
1210         }
1211         else if (strstr(opname, "ARMATURE_OT")) {
1212                 km = WM_keymap_find_all(C, "Armature", 0, 0);
1213         }
1214         else if (strstr(opname, "POSE_OT")) {
1215                 km = WM_keymap_find_all(C, "Pose", 0, 0);
1216         }
1217         else if (strstr(opname, "SCULPT_OT")) {
1218                 km = WM_keymap_find_all(C, "Sculpt", 0, 0);
1219         }
1220         else if (strstr(opname, "MBALL_OT")) {
1221                 km = WM_keymap_find_all(C, "Metaball", 0, 0);
1222                 
1223                 /* some mball operators are active in object mode too, like add-prim */
1224                 if(km && km->poll && km->poll((bContext *)C)==0) {
1225                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1226                 }
1227         }
1228         else if (strstr(opname, "LATTICE_OT")) {
1229                 km = WM_keymap_find_all(C, "Lattice", 0, 0);
1230         }
1231         else if (strstr(opname, "PARTICLE_OT")) {
1232                 km = WM_keymap_find_all(C, "Particle", 0, 0);
1233         }
1234         else if (strstr(opname, "FONT_OT")) {
1235                 km = WM_keymap_find_all(C, "Font", 0, 0);
1236         }
1237         else if (strstr(opname, "PAINT_OT")) {
1238                 
1239                 /* check for relevant mode */
1240                 switch(CTX_data_mode_enum(C)) {
1241                         case OB_MODE_WEIGHT_PAINT:
1242                                 km = WM_keymap_find_all(C, "Weight Paint", 0, 0);
1243                                 break;
1244                         case OB_MODE_VERTEX_PAINT:
1245                                 km = WM_keymap_find_all(C, "Vertex Paint", 0, 0);
1246                                 break;
1247                         case OB_MODE_TEXTURE_PAINT:
1248                                 km = WM_keymap_find_all(C, "Image Paint", 0, 0);
1249                                 break;
1250                 }
1251         }
1252         /* Paint Face Mask */
1253         else if (strstr(opname, "PAINT_OT_face_select")) {
1254                 km = WM_keymap_find_all(C, "Face Mask", sl->spacetype, 0);
1255         }
1256         /* Timeline */
1257         else if (strstr(opname, "TIME_OT")) {
1258                 km = WM_keymap_find_all(C, "Timeline", sl->spacetype, 0);
1259         }
1260         /* Image Editor */
1261         else if (strstr(opname, "IMAGE_OT")) {
1262                 km = WM_keymap_find_all(C, "Image", sl->spacetype, 0);
1263         }
1264         /* UV Editor */
1265         else if (strstr(opname, "UV_OT")) {
1266                 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1267         }
1268         /* Node Editor */
1269         else if (strstr(opname, "NODE_OT")) {
1270                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1271         }
1272         /* Animation Editor Channels */
1273         else if (strstr(opname, "ANIM_OT_channels")) {
1274                 km = WM_keymap_find_all(C, "Animation Channels", sl->spacetype, 0);
1275         }
1276         /* Animation Generic - after channels */
1277         else if (strstr(opname, "ANIM_OT")) {
1278                 km = WM_keymap_find_all(C, "Animation", 0, 0);
1279         }
1280         /* Graph Editor */
1281         else if (strstr(opname, "GRAPH_OT")) {
1282                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1283         }
1284         /* Dopesheet Editor */
1285         else if (strstr(opname, "ACTION_OT")) {
1286                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1287         }
1288         /* NLA Editor */
1289         else if (strstr(opname, "NLA_OT")) {
1290                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1291         }
1292         /* Script */
1293         else if (strstr(opname, "SCRIPT_OT")) {
1294                 km = WM_keymap_find_all(C, "Script", sl->spacetype, 0);
1295         }
1296         /* Text */
1297         else if (strstr(opname, "TEXT_OT")) {
1298                 km = WM_keymap_find_all(C, "Text", sl->spacetype, 0);
1299         }
1300         /* Sequencer */
1301         else if (strstr(opname, "SEQUENCER_OT")) {
1302                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1303         }
1304         /* Console */
1305         else if (strstr(opname, "CONSOLE_OT")) {
1306                 km = WM_keymap_find_all(C, "Console", sl->spacetype, 0);
1307         }
1308         /* Console */
1309         else if (strstr(opname, "INFO_OT")) {
1310                 km = WM_keymap_find_all(C, "Info", sl->spacetype, 0);
1311         }
1312         
1313         /* Transform */
1314         else if (strstr(opname, "TRANSFORM_OT")) {
1315                 
1316                 /* check for relevant editor */
1317                 switch(sl->spacetype) {
1318                         case SPACE_VIEW3D:
1319                                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1320                                 break;
1321                         case SPACE_IPO:
1322                                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1323                                 break;
1324                         case SPACE_ACTION:
1325                                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1326                                 break;
1327                         case SPACE_NLA:
1328                                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1329                                 break;
1330                         case SPACE_IMAGE:
1331                                 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1332                                 break;
1333                         case SPACE_NODE:
1334                                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1335                                 break;
1336                         case SPACE_SEQ:
1337                                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1338                                 break;
1339                 }
1340         }
1341         
1342         return km;
1343 }
1344