Merge with trunk r41197
[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_global.h"
51 #include "BKE_library.h"
52 #include "BKE_main.h"
53 #include "BKE_screen.h"
54
55
56 #include "RNA_access.h"
57 #include "RNA_enum_types.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61 #include "wm_window.h"
62 #include "wm_event_system.h"
63 #include "wm_event_types.h"
64
65 /******************************* Keymap Item **********************************
66  * Item in a keymap, that maps from an event to an operator or modal map item */
67
68 static wmKeyMapItem *wm_keymap_item_copy(wmKeyMapItem *kmi)
69 {
70         wmKeyMapItem *kmin = MEM_dupallocN(kmi);
71
72         kmin->prev= kmin->next= NULL;
73         kmin->flag &= ~KMI_UPDATE;
74
75         if(kmin->properties) {
76                 kmin->ptr= MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
77                 WM_operator_properties_create(kmin->ptr, kmin->idname);
78
79                 kmin->properties= IDP_CopyProperty(kmin->properties);
80                 kmin->ptr->data= kmin->properties;
81         }
82
83         return kmin;
84 }
85
86 static void wm_keymap_item_free(wmKeyMapItem *kmi)
87 {
88         /* not kmi itself */
89         if(kmi->ptr) {
90                 WM_operator_properties_free(kmi->ptr);
91                 MEM_freeN(kmi->ptr);
92         }
93 }
94
95 static void wm_keymap_item_properties_set(wmKeyMapItem *kmi)
96 {
97         WM_operator_properties_alloc(&(kmi->ptr), &(kmi->properties), kmi->idname);
98         WM_operator_properties_sanitize(kmi->ptr, 1);
99 }
100
101 static int wm_keymap_item_equals_result(wmKeyMapItem *a, wmKeyMapItem *b)
102 {
103         if(strcmp(a->idname, b->idname) != 0)
104                 return 0;
105         
106         if(!((a->ptr==NULL && b->ptr==NULL) ||
107              (a->ptr && b->ptr && IDP_EqualsProperties(a->ptr->data, b->ptr->data))))
108                 return 0;
109         
110         if((a->flag & KMI_INACTIVE) != (b->flag & KMI_INACTIVE))
111                 return 0;
112         
113         return (a->propvalue == b->propvalue);
114 }
115
116 static int wm_keymap_item_equals(wmKeyMapItem *a, wmKeyMapItem *b)
117 {
118         return (wm_keymap_item_equals_result(a, b) &&
119                 a->type == b->type &&
120                 a->val == b->val &&
121                 a->shift == b->shift &&
122                 a->ctrl == b->ctrl &&
123                 a->alt == b->alt &&
124                 a->oskey == b->oskey &&
125                 a->keymodifier == b->keymodifier &&
126                 a->maptype == b->maptype);
127 }
128
129 /* properties can be NULL, otherwise the arg passed is used and ownership is given to the kmi */
130 void WM_keymap_properties_reset(wmKeyMapItem *kmi, struct IDProperty *properties)
131 {
132         WM_operator_properties_free(kmi->ptr);
133         MEM_freeN(kmi->ptr);
134
135         kmi->ptr = NULL;
136         kmi->properties = properties;
137
138         wm_keymap_item_properties_set(kmi);
139 }
140
141 /**************************** Keymap Diff Item *********************************
142  * Item in a diff keymap, used for saving diff of keymaps in user preferences */
143
144 static wmKeyMapDiffItem *wm_keymap_diff_item_copy(wmKeyMapDiffItem *kmdi)
145 {
146         wmKeyMapDiffItem *kmdin = MEM_dupallocN(kmdi);
147
148         kmdin->next = kmdin->prev = NULL;
149         if(kmdi->add_item)
150                 kmdin->add_item = wm_keymap_item_copy(kmdi->add_item);
151         if(kmdi->remove_item)
152                 kmdin->remove_item = wm_keymap_item_copy(kmdi->remove_item);
153         
154         return kmdin;
155 }
156
157 static void wm_keymap_diff_item_free(wmKeyMapDiffItem *kmdi)
158 {
159         if(kmdi->remove_item) {
160                 wm_keymap_item_free(kmdi->remove_item);
161                 MEM_freeN(kmdi->remove_item);
162         }
163         if(kmdi->add_item) {
164                 wm_keymap_item_free(kmdi->add_item);
165                 MEM_freeN(kmdi->add_item);
166         }
167 }
168
169 /***************************** Key Configuration ******************************
170  * List of keymaps for all editors, modes, ... . There is a builtin default key
171  * configuration, a user key configuration, and other preset configurations. */
172
173 wmKeyConfig *WM_keyconfig_new(wmWindowManager *wm, const char *idname)
174 {
175         wmKeyConfig *keyconf;
176         
177         keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
178         BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
179         BLI_addtail(&wm->keyconfigs, keyconf);
180
181         return keyconf;
182 }
183
184 wmKeyConfig *WM_keyconfig_new_user(wmWindowManager *wm, const char *idname)
185 {
186         wmKeyConfig *keyconf = WM_keyconfig_new(wm, idname);
187
188         keyconf->flag |= KEYCONF_USER;
189
190         return keyconf;
191 }
192
193 void WM_keyconfig_remove(wmWindowManager *wm, wmKeyConfig *keyconf)
194 {
195         if (keyconf) {
196                 if (strncmp(U.keyconfigstr, keyconf->idname, sizeof(U.keyconfigstr)) == 0) {
197                         BLI_strncpy(U.keyconfigstr, wm->defaultconf->idname, sizeof(U.keyconfigstr));
198                         WM_keyconfig_update_tag(NULL, NULL);
199                 }
200
201                 BLI_remlink(&wm->keyconfigs, keyconf);
202                 WM_keyconfig_free(keyconf);
203         }
204 }
205
206 void WM_keyconfig_free(wmKeyConfig *keyconf)
207 {
208         wmKeyMap *km;
209
210         while((km= keyconf->keymaps.first)) {
211                 WM_keymap_free(km);
212                 BLI_freelinkN(&keyconf->keymaps, km);
213         }
214
215         MEM_freeN(keyconf);
216 }
217
218 static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
219 {
220         wmKeyConfig *kc;
221
222         for(kc= lb->first; kc; kc= kc->next)
223                 if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
224                         return kc;
225         
226         return NULL;
227 }
228
229 static wmKeyConfig *WM_keyconfig_active(wmWindowManager *wm)
230 {
231         wmKeyConfig *keyconf;
232
233         /* first try from preset */
234         keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
235         if(keyconf)
236                 return keyconf;
237         
238         /* otherwise use default */
239         return wm->defaultconf;
240 }
241
242 void WM_keyconfig_set_active(wmWindowManager *wm, const char *idname)
243 {
244         /* setting a different key configuration as active: we ensure all is
245            updated properly before and after making the change */
246
247         WM_keyconfig_update(wm);
248
249         BLI_strncpy(U.keyconfigstr, idname, sizeof(U.keyconfigstr));
250
251         WM_keyconfig_update_tag(NULL, NULL);
252         WM_keyconfig_update(wm);
253 }
254
255 /********************************** Keymap *************************************
256  * List of keymap items for one editor, mode, modal operator, ... */
257
258 static wmKeyMap *wm_keymap_new(const char *idname, int spaceid, int regionid)
259 {
260         wmKeyMap *km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
261
262         BLI_strncpy(km->idname, idname, KMAP_MAX_NAME);
263         km->spaceid= spaceid;
264         km->regionid= regionid;
265
266         return km;
267 }
268
269 static wmKeyMap *wm_keymap_copy(wmKeyMap *keymap)
270 {
271         wmKeyMap *keymapn = MEM_dupallocN(keymap);
272         wmKeyMapItem *kmi, *kmin;
273         wmKeyMapDiffItem *kmdi, *kmdin;
274
275         keymapn->modal_items= keymap->modal_items;
276         keymapn->poll= keymap->poll;
277         keymapn->items.first= keymapn->items.last= NULL;
278         keymapn->flag &= ~(KEYMAP_UPDATE|KEYMAP_EXPANDED);
279
280         for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next) {
281                 kmdin= wm_keymap_diff_item_copy(kmdi);
282                 BLI_addtail(&keymapn->items, kmdin);
283         }
284
285         for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
286                 kmin= wm_keymap_item_copy(kmi);
287                 BLI_addtail(&keymapn->items, kmin);
288         }
289
290         return keymapn;
291 }
292
293 void WM_keymap_free(wmKeyMap *keymap)
294 {
295         wmKeyMapItem *kmi;
296         wmKeyMapDiffItem *kmdi;
297
298         for(kmdi=keymap->diff_items.first; kmdi; kmdi=kmdi->next)
299                 wm_keymap_diff_item_free(kmdi);
300
301         for(kmi=keymap->items.first; kmi; kmi=kmi->next)
302                 wm_keymap_item_free(kmi);
303
304         BLI_freelistN(&keymap->diff_items);
305         BLI_freelistN(&keymap->items);
306 }
307
308 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
309 {
310         kmi->type= type;
311         kmi->val= val;
312         kmi->keymodifier= keymodifier;
313
314         if(modifier == KM_ANY) {
315                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
316         }
317         else {
318                 kmi->shift= (modifier & KM_SHIFT) ? KM_MOD_FIRST : ((modifier & KM_SHIFT2) ? KM_MOD_SECOND : FALSE);
319                 kmi->ctrl=  (modifier & KM_CTRL)  ? KM_MOD_FIRST : ((modifier & KM_CTRL2)  ? KM_MOD_SECOND : FALSE);
320                 kmi->alt=   (modifier & KM_ALT)   ? KM_MOD_FIRST : ((modifier & KM_ALT2)   ? KM_MOD_SECOND : FALSE);
321                 kmi->oskey= (modifier & KM_OSKEY) ? KM_MOD_FIRST : ((modifier & KM_OSKEY2) ? KM_MOD_SECOND : FALSE);
322         }
323 }
324
325 static void keymap_item_set_id(wmKeyMap *keymap, wmKeyMapItem *kmi)
326 {
327         keymap->kmi_id++;
328         if ((keymap->flag & KEYMAP_USER) == 0) {
329                 kmi->id = keymap->kmi_id;
330         } else {
331                 kmi->id = -keymap->kmi_id; // User defined keymap entries have negative ids
332         }
333 }
334
335 /* if item was added, then bail out */
336 wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
337 {
338         wmKeyMapItem *kmi;
339         
340         for(kmi= keymap->items.first; kmi; kmi= kmi->next)
341                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
342                         break;
343         if(kmi==NULL) {
344                 kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
345                 
346                 BLI_addtail(&keymap->items, kmi);
347                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
348                 
349                 keymap_item_set_id(keymap, kmi);
350
351                 keymap_event_set(kmi, type, val, modifier, keymodifier);
352                 wm_keymap_item_properties_set(kmi);
353         }
354         return kmi;
355 }
356
357 /* always add item */
358 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
359 {
360         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
361         
362         BLI_addtail(&keymap->items, kmi);
363         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
364
365         keymap_event_set(kmi, type, val, modifier, keymodifier);
366         wm_keymap_item_properties_set(kmi);
367
368         keymap_item_set_id(keymap, kmi);
369
370         WM_keyconfig_update_tag(keymap, kmi);
371
372         return kmi;
373 }
374
375 /* menu wrapper for WM_keymap_add_item */
376 wmKeyMapItem *WM_keymap_add_menu(wmKeyMap *keymap, const char *idname, int type, int val, int modifier, int keymodifier)
377 {
378         wmKeyMapItem *kmi= WM_keymap_add_item(keymap, "WM_OT_call_menu", type, val, modifier, keymodifier);
379         RNA_string_set(kmi->ptr, "name", idname);
380         return kmi;
381 }
382
383 void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
384 {
385         if(BLI_findindex(&keymap->items, kmi) != -1) {
386                 if(kmi->ptr) {
387                         WM_operator_properties_free(kmi->ptr);
388                         MEM_freeN(kmi->ptr);
389                 }
390                 BLI_freelinkN(&keymap->items, kmi);
391
392                 WM_keyconfig_update_tag(keymap, kmi);
393         }
394 }
395
396 /************************** Keymap Diff and Patch ****************************
397  * Rather than saving the entire keymap for user preferences, we only save a
398  * diff so that changes in the defaults get synced. This system is not perfect
399  * but works better than overriding the keymap entirely when only few items
400  * are changed. */
401
402 static void wm_keymap_addon_add(wmKeyMap *keymap, wmKeyMap *addonmap)
403 {
404         wmKeyMapItem *kmi, *kmin;
405
406         for(kmi=addonmap->items.first; kmi; kmi=kmi->next) {
407                 kmin = wm_keymap_item_copy(kmi);
408                 keymap_item_set_id(keymap, kmin);
409                 BLI_addhead(&keymap->items, kmin);
410         }
411 }
412
413 static wmKeyMapItem *wm_keymap_find_item_equals(wmKeyMap *km, wmKeyMapItem *needle)
414 {
415         wmKeyMapItem *kmi;
416
417         for(kmi=km->items.first; kmi; kmi=kmi->next)
418                 if(wm_keymap_item_equals(kmi, needle))
419                         return kmi;
420         
421         return NULL;
422 }
423
424 static wmKeyMapItem *wm_keymap_find_item_equals_result(wmKeyMap *km, wmKeyMapItem *needle)
425 {
426         wmKeyMapItem *kmi;
427
428         for(kmi=km->items.first; kmi; kmi=kmi->next)
429                 if(wm_keymap_item_equals_result(kmi, needle))
430                         return kmi;
431         
432         return NULL;
433 }
434
435 static void wm_keymap_diff(wmKeyMap *diff_km, wmKeyMap *from_km, wmKeyMap *to_km, wmKeyMap *orig_km, wmKeyMap *addon_km)
436 {
437         wmKeyMapItem *kmi, *to_kmi, *orig_kmi;
438         wmKeyMapDiffItem *kmdi;
439
440         for(kmi=from_km->items.first; kmi; kmi=kmi->next) {
441                 to_kmi = WM_keymap_item_find_id(to_km, kmi->id);
442
443                 if(!to_kmi) {
444                         /* remove item */
445                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
446                         kmdi->remove_item = wm_keymap_item_copy(kmi);
447                         BLI_addtail(&diff_km->diff_items, kmdi);
448                 }
449                 else if(to_kmi && !wm_keymap_item_equals(kmi, to_kmi)) {
450                         /* replace item */
451                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
452                         kmdi->remove_item = wm_keymap_item_copy(kmi);
453                         kmdi->add_item = wm_keymap_item_copy(to_kmi);
454                         BLI_addtail(&diff_km->diff_items, kmdi);
455                 }
456
457                 /* sync expanded flag back to original so we don't loose it on repatch */
458                 if(to_kmi) {
459                         orig_kmi = WM_keymap_item_find_id(orig_km, kmi->id);
460
461                         if(!orig_kmi)
462                                 orig_kmi = wm_keymap_find_item_equals(addon_km, kmi);
463
464                         if(orig_kmi) {
465                                 orig_kmi->flag &= ~KMI_EXPANDED;
466                                 orig_kmi->flag |= (to_kmi->flag & KMI_EXPANDED);
467                         }
468                 }
469         }
470
471         for(kmi=to_km->items.first; kmi; kmi=kmi->next) {
472                 if(kmi->id < 0) {
473                         /* add item */
474                         kmdi = MEM_callocN(sizeof(wmKeyMapDiffItem), "wmKeyMapDiffItem");
475                         kmdi->add_item = wm_keymap_item_copy(kmi);
476                         BLI_addtail(&diff_km->diff_items, kmdi);
477                 }
478         }
479 }
480
481 static void wm_keymap_patch(wmKeyMap *km, wmKeyMap *diff_km)
482 {
483         wmKeyMapDiffItem *kmdi;
484         wmKeyMapItem *kmi_remove, *kmi_add;
485
486         for(kmdi=diff_km->diff_items.first; kmdi; kmdi=kmdi->next) {
487                 /* find item to remove */
488                 kmi_remove = NULL;
489                 if(kmdi->remove_item) {
490                         kmi_remove = wm_keymap_find_item_equals(km, kmdi->remove_item);
491                         if(!kmi_remove)
492                                 kmi_remove = wm_keymap_find_item_equals_result(km, kmdi->remove_item);
493                 }
494
495                 /* add item */
496                 if(kmdi->add_item) {
497                         /* only if nothing to remove or item to remove found */
498                         if(!kmdi->remove_item || kmi_remove) {
499                                 kmi_add = wm_keymap_item_copy(kmdi->add_item);
500                                 kmi_add->flag |= KMI_USER_MODIFIED;
501
502                                 if(kmi_remove) {
503                                         kmi_add->flag &= ~KMI_EXPANDED;
504                                         kmi_add->flag |= (kmi_remove->flag & KMI_EXPANDED);
505                                         kmi_add->id = kmi_remove->id;
506                                         BLI_insertlinkbefore(&km->items, kmi_remove, kmi_add);
507                                 }
508                                 else {
509                                         keymap_item_set_id(km, kmi_add);
510                                         BLI_addtail(&km->items, kmi_add);
511                                 }
512                         }
513                 }
514
515                 /* remove item */
516                 if(kmi_remove) {
517                         wm_keymap_item_free(kmi_remove);
518                         BLI_freelinkN(&km->items, kmi_remove);
519                 }
520         }
521 }
522
523 static wmKeyMap *wm_keymap_patch_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *usermap)
524 {
525         wmKeyMap *km;
526         int expanded = 0;
527
528         /* remove previous keymap in list, we will replace it */
529         km = WM_keymap_list_find(lb, defaultmap->idname, defaultmap->spaceid, defaultmap->regionid);
530         if(km) {
531                 expanded = (km->flag & (KEYMAP_EXPANDED|KEYMAP_CHILDREN_EXPANDED));
532                 WM_keymap_free(km);
533                 BLI_freelinkN(lb, km);
534         }
535
536         /* copy new keymap from an existing one */
537         if(usermap && !(usermap->flag & KEYMAP_DIFF)) {
538                 /* for compatibiltiy with old user preferences with non-diff
539                    keymaps we override the original entirely */
540                 wmKeyMapItem *kmi, *orig_kmi;
541
542                 km = wm_keymap_copy(usermap);
543
544                 /* try to find corresponding id's for items */
545                 for(kmi=km->items.first; kmi; kmi=kmi->next) {
546                         orig_kmi = wm_keymap_find_item_equals(defaultmap, kmi);
547                         if(!orig_kmi)
548                                 orig_kmi = wm_keymap_find_item_equals_result(defaultmap, kmi);
549
550                         if(orig_kmi)
551                                 kmi->id = orig_kmi->id;
552                         else
553                                 kmi->id = -(km->kmi_id++);
554                 }
555
556                 km->flag |= KEYMAP_UPDATE; /* update again to create diff */
557         }
558         else
559                 km = wm_keymap_copy(defaultmap);
560
561         /* add addon keymap items */
562         if(addonmap)
563                 wm_keymap_addon_add(km, addonmap);
564
565         /* tag as being user edited */
566         if(usermap)
567                 km->flag |= KEYMAP_USER_MODIFIED;
568         km->flag |= KEYMAP_USER|expanded;
569
570         /* apply user changes of diff keymap */
571         if(usermap && (usermap->flag & KEYMAP_DIFF))
572                 wm_keymap_patch(km, usermap);
573
574         /* add to list */
575         BLI_addtail(lb, km);
576         
577         return km;
578 }
579
580 static void wm_keymap_diff_update(ListBase *lb, wmKeyMap *defaultmap, wmKeyMap *addonmap, wmKeyMap *km)
581 {
582         wmKeyMap *diffmap, *prevmap, *origmap;
583
584         /* create temporary default + addon keymap for diff */
585         origmap = defaultmap;
586
587         if(addonmap) {
588                 defaultmap = wm_keymap_copy(defaultmap);
589                 wm_keymap_addon_add(defaultmap, addonmap);
590         }
591
592         /* remove previous diff keymap in list, we will replace it */
593         prevmap = WM_keymap_list_find(lb, km->idname, km->spaceid, km->regionid);
594         if(prevmap) {
595                 WM_keymap_free(prevmap);
596                 BLI_freelinkN(lb, prevmap);
597         }
598
599         /* create diff keymap */
600         diffmap= wm_keymap_new(km->idname, km->spaceid, km->regionid);
601         diffmap->flag |= KEYMAP_DIFF;
602         wm_keymap_diff(diffmap, defaultmap, km, origmap, addonmap);
603
604         /* add to list if not empty */
605         if(diffmap->diff_items.first) {
606                 BLI_addtail(lb, diffmap);
607         }
608         else {
609                 WM_keymap_free(diffmap);
610                 MEM_freeN(diffmap);
611         }
612
613         /* free temporary default map */
614         if(addonmap) {
615                 WM_keymap_free(defaultmap);
616                 MEM_freeN(defaultmap);
617         }
618 }
619
620 /* ****************** storage in WM ************ */
621
622 /* name id's are for storing general or multiple keymaps, 
623    space/region ids are same as DNA_space_types.h */
624 /* gets free'd in wm.c */
625
626 wmKeyMap *WM_keymap_list_find(ListBase *lb, const char *idname, int spaceid, int regionid)
627 {
628         wmKeyMap *km;
629
630         for(km= lb->first; km; km= km->next)
631                 if(km->spaceid==spaceid && km->regionid==regionid)
632                         if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
633                                 return km;
634         
635         return NULL;
636 }
637
638 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
639 {
640         wmKeyMap *km= WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
641         
642         if(km==NULL) {
643                 km= wm_keymap_new(idname, spaceid, regionid);
644                 BLI_addtail(&keyconf->keymaps, km);
645
646                 WM_keyconfig_update_tag(km, NULL);
647         }
648         
649         return km;
650 }
651
652 wmKeyMap *WM_keymap_find_all(const bContext *C, const char *idname, int spaceid, int regionid)
653 {
654         wmWindowManager *wm= CTX_wm_manager(C);
655
656         return WM_keymap_list_find(&wm->userconf->keymaps, idname, spaceid, regionid);
657 }
658
659 /* ****************** modal keymaps ************ */
660
661 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
662
663 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, const char *idname, EnumPropertyItem *items)
664 {
665         wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
666         km->flag |= KEYMAP_MODAL;
667         km->modal_items= items;
668
669         if(!items) {
670                 /* init modal items from default config */
671                 wmWindowManager *wm = G.main->wm.first;
672                 wmKeyMap *defaultkm= WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, 0, 0);
673
674                 if(defaultkm) {
675                         km->modal_items = defaultkm->modal_items;
676                         km->poll = defaultkm->poll;
677                 }
678         }
679         
680         return km;
681 }
682
683 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, const char *idname)
684 {
685         wmKeyMap *km;
686         
687         for(km= keyconf->keymaps.first; km; km= km->next)
688                 if(km->flag & KEYMAP_MODAL)
689                         if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
690                                 break;
691         
692         return km;
693 }
694
695
696 wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
697 {
698         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
699         
700         BLI_addtail(&km->items, kmi);
701         kmi->propvalue= value;
702         
703         keymap_event_set(kmi, type, val, modifier, keymodifier);
704
705         keymap_item_set_id(km, kmi);
706
707         WM_keyconfig_update_tag(km, kmi);
708
709         return kmi;
710 }
711
712 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
713 {
714         wmOperatorType *ot= WM_operatortype_find(opname, 0);
715         
716         if(ot)
717                 ot->modalkeymap= km;
718         else
719                 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
720 }
721
722 /* ***************** get string from key events **************** */
723
724 const char *WM_key_event_string(short type)
725 {
726         const char *name= NULL;
727         if(RNA_enum_name(event_type_items, (int)type, &name))
728                 return name;
729         
730         return "";
731 }
732
733 char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
734 {
735         char buf[128];
736
737         buf[0]= 0;
738
739         if (kmi->shift == KM_ANY &&
740                 kmi->ctrl == KM_ANY &&
741                 kmi->alt == KM_ANY &&
742                 kmi->oskey == KM_ANY) {
743
744                 strcat(buf, "Any ");
745         } else {
746                 if(kmi->shift)
747                         strcat(buf, "Shift ");
748
749                 if(kmi->ctrl)
750                         strcat(buf, "Ctrl ");
751
752                 if(kmi->alt)
753                         strcat(buf, "Alt ");
754
755                 if(kmi->oskey)
756                         strcat(buf, "Cmd ");
757         }
758                 
759         if(kmi->keymodifier) {
760                 strcat(buf, WM_key_event_string(kmi->keymodifier));
761                 strcat(buf, " ");
762         }
763
764         strcat(buf, WM_key_event_string(kmi->type));
765         BLI_strncpy(str, buf, len);
766
767         return str;
768 }
769
770 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)
771 {
772         wmWindowManager *wm= CTX_wm_manager(C);
773         wmEventHandler *handler;
774         wmKeyMap *keymap;
775         wmKeyMapItem *kmi;
776
777         /* find keymap item in handlers */
778         for(handler=handlers->first; handler; handler=handler->next) {
779                 keymap= WM_keymap_active(wm, handler->keymap);
780
781                 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
782                         for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
783                                 
784                                 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
785                                         if (hotkey)
786                                                 if (!ISHOTKEY(kmi->type))
787                                                         continue;
788                                         
789                                         if(compare_props) {
790                                                 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
791                                                         if(keymap_r) *keymap_r= keymap;
792                                                         return kmi;
793                                                 }
794                                         }
795                                         else {
796                                                 if(keymap_r) *keymap_r= keymap;
797                                                 return kmi;
798                                         }
799                                 }
800                         }
801                 }
802         }
803         
804         /* ensure un-initialized keymap is never used */
805         if(keymap_r) *keymap_r= NULL;
806         return NULL;
807 }
808
809 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)
810 {
811         wmWindow *win= CTX_wm_window(C);
812         ScrArea *sa= CTX_wm_area(C);
813         ARegion *ar= CTX_wm_region(C);
814         wmKeyMapItem *found= NULL;
815
816         /* look into multiple handler lists to find the item */
817         if(win)
818                 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
819         
820
821         if(sa && found==NULL)
822                 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
823
824         if(found==NULL) {
825                 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
826                         if(sa) {
827                                 if (!(ar && ar->regiontype == RGN_TYPE_WINDOW))
828                                         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
829                                 
830                                 if(ar)
831                                         found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
832                         }
833                 }
834                 else if(ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
835                         if (!(ar && ar->regiontype == RGN_TYPE_CHANNELS))
836                                 ar= BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
837
838                         if(ar)
839                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
840                 }
841                 else if(ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
842                         if (!(ar && ar->regiontype == RGN_TYPE_PREVIEW))
843                                 ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
844
845                         if(ar)
846                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
847                 }
848                 else {
849                         if(ar)
850                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
851                 }
852         }
853         
854         return found;
855 }
856
857 static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
858 {
859         wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, hotkey, keymap_r);
860
861         if(!found)
862                 found= wm_keymap_item_find_props(C, opname, opcontext, NULL, 0, hotkey, keymap_r);
863
864         return found;
865 }
866
867 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
868 {
869         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, 0, NULL);
870         
871         if(kmi) {
872                 WM_keymap_item_to_string(kmi, str, len);
873                 return str;
874         }
875
876         return NULL;
877 }
878
879 int WM_key_event_operator_id(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
880 {
881         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, hotkey, keymap_r);
882         
883         if(kmi)
884                 return kmi->id;
885         else
886                 return 0;
887 }
888
889 int     WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
890 {
891         int k1type, k2type;
892
893         if (k1->flag & KMI_INACTIVE || k2->flag & KMI_INACTIVE)
894                 return 0;
895
896         /* take event mapping into account */
897         k1type = WM_userdef_event_map(k1->type);
898         k2type = WM_userdef_event_map(k2->type);
899
900         if(k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
901                 return 0;
902
903         if(k1->val != KM_ANY && k2->val != KM_ANY) {
904                 /* take click, press, release conflict into account */
905                 if (k1->val == KM_CLICK && ELEM3(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
906                         return 0;
907                 if (k2->val == KM_CLICK && ELEM3(k1->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
908                         return 0;
909                 if (k1->val != k2->val)
910                         return 0;
911         }
912
913         if(k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
914                 return 0;
915
916         if(k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
917                 return 0;
918
919         if(k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
920                 return 0;
921
922         if(k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
923                 return 0;
924
925         if(k1->keymodifier != k2->keymodifier)
926                 return 0;
927
928         return 1;
929 }
930
931 /************************* Update Final Configuration *************************
932  * On load or other changes, the final user key configuration is rebuilt from
933  * the preset, addon and user preferences keymaps. We also test if the final
934  * configuration changed and write the changes to the user preferences. */
935
936 static int WM_KEYMAP_UPDATE = 0;
937
938 void WM_keyconfig_update_tag(wmKeyMap *km, wmKeyMapItem *kmi)
939 {
940         /* quick tag to do delayed keymap updates */
941         WM_KEYMAP_UPDATE= 1;
942
943         if(km)
944                 km->flag |= KEYMAP_UPDATE;
945         if(kmi)
946                 kmi->flag |= KMI_UPDATE;
947 }
948
949 static int wm_keymap_test_and_clear_update(wmKeyMap *km)
950 {
951         wmKeyMapItem *kmi;
952         int update;
953         
954         update= (km->flag & KEYMAP_UPDATE);
955         km->flag &= ~KEYMAP_UPDATE;
956
957         for(kmi=km->items.first; kmi; kmi=kmi->next) {
958                 update= update || (kmi->flag & KMI_UPDATE);
959                 kmi->flag &= ~KMI_UPDATE;
960         }
961         
962         return update;
963 }
964
965 static wmKeyMap *wm_keymap_preset(wmWindowManager *wm, wmKeyMap *km)
966 {
967         wmKeyConfig *keyconf= WM_keyconfig_active(wm);
968         wmKeyMap *keymap;
969
970         keymap= WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
971         if(!keymap)
972                 keymap= WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
973
974         return keymap;
975 }
976
977 void WM_keyconfig_update(wmWindowManager *wm)
978 {
979         wmKeyMap *km, *defaultmap, *addonmap, *usermap, *kmn;
980         wmKeyMapItem *kmi;
981         wmKeyMapDiffItem *kmdi;
982         int compat_update = 0;
983
984         if(!WM_KEYMAP_UPDATE)
985                 return;
986         
987         /* update operator properties for non-modal user keymaps */
988         for(km=U.user_keymaps.first; km; km=km->next) {
989                 if((km->flag & KEYMAP_MODAL) == 0) {
990                         for(kmdi=km->diff_items.first; kmdi; kmdi=kmdi->next) {
991                                 if(kmdi->add_item)
992                                         wm_keymap_item_properties_set(kmdi->add_item);
993                                 if(kmdi->remove_item)
994                                         wm_keymap_item_properties_set(kmdi->remove_item);
995                         }
996
997                         for(kmi=km->items.first; kmi; kmi=kmi->next)
998                                 wm_keymap_item_properties_set(kmi);
999                 }
1000         }
1001
1002         /* update U.user_keymaps with user key configuration changes */
1003         for(km=wm->userconf->keymaps.first; km; km=km->next) {
1004                 /* only diff if the user keymap was modified */
1005                 if(wm_keymap_test_and_clear_update(km)) {
1006                         /* find keymaps */
1007                         defaultmap= wm_keymap_preset(wm, km);
1008                         addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1009
1010                         /* diff */
1011                         if(defaultmap)
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