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