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