Uv Tools branch GSOC 2011
[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         if(defaultmap->flag & KEYMAP_MODAL)
601                 diffmap->flag |= KEYMAP_MODAL;
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, const short hotkey, const short sloppy, wmKeyMap **keymap_r)
858 {
859         wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, hotkey, keymap_r);
860
861         if(!found && sloppy)
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, const short sloppy, char *str, int len)
868 {
869         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, 0, sloppy, 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, TRUE, 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(G.background)
985                 return;
986         if(!WM_KEYMAP_UPDATE)
987                 return;
988         
989         /* update operator properties for non-modal user keymaps */
990         for(km=U.user_keymaps.first; km; km=km->next) {
991                 if((km->flag & KEYMAP_MODAL) == 0) {
992                         for(kmdi=km->diff_items.first; kmdi; kmdi=kmdi->next) {
993                                 if(kmdi->add_item)
994                                         wm_keymap_item_properties_set(kmdi->add_item);
995                                 if(kmdi->remove_item)
996                                         wm_keymap_item_properties_set(kmdi->remove_item);
997                         }
998
999                         for(kmi=km->items.first; kmi; kmi=kmi->next)
1000                                 wm_keymap_item_properties_set(kmi);
1001                 }
1002         }
1003
1004         /* update U.user_keymaps with user key configuration changes */
1005         for(km=wm->userconf->keymaps.first; km; km=km->next) {
1006                 /* only diff if the user keymap was modified */
1007                 if(wm_keymap_test_and_clear_update(km)) {
1008                         /* find keymaps */
1009                         defaultmap= wm_keymap_preset(wm, km);
1010                         addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1011
1012                         /* diff */
1013                         if(defaultmap)
1014                                 wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, km);
1015                 }
1016         }
1017
1018         /* create user key configuration from preset + addon + user preferences */
1019         for(km=wm->defaultconf->keymaps.first; km; km=km->next) {
1020                 /* find keymaps */
1021                 defaultmap= wm_keymap_preset(wm, km);
1022                 addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1023                 usermap= WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
1024
1025                 /* add */
1026                 kmn= wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
1027
1028                 if(kmn) {
1029                         kmn->modal_items= km->modal_items;
1030                         kmn->poll= km->poll;
1031                 }
1032
1033                 /* in case of old non-diff keymaps, force extra update to create diffs */
1034                 compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
1035         }
1036
1037         WM_KEYMAP_UPDATE= 0;
1038
1039         if(compat_update) {
1040                 WM_keyconfig_update_tag(NULL, NULL);
1041                 WM_keyconfig_update(wm);
1042         }
1043 }
1044
1045 /********************************* Event Handling *****************************
1046  * Handlers have pointers to the keymap in the default configuration. During
1047  * event handling this function is called to get the keymap from the final
1048  * configuration. */
1049
1050 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
1051 {
1052         wmKeyMap *km;
1053
1054         if(!keymap)
1055                 return NULL;
1056         
1057         /* first user defined keymaps */
1058         km= WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1059
1060         if(km)
1061                 return km;
1062
1063         return keymap;
1064 }
1065
1066 /******************************* Keymap Editor ********************************
1067  * In the keymap editor the user key configuration is edited. */
1068
1069 void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
1070 {
1071         wmWindowManager *wm = CTX_wm_manager(C);
1072         wmKeyMap *defaultmap, *addonmap;
1073         wmKeyMapItem *orig;
1074
1075         if(!keymap)
1076                 return;
1077
1078         /* construct default keymap from preset + addons */
1079         defaultmap= wm_keymap_preset(wm, keymap);
1080         addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1081
1082         if(addonmap) {
1083                 defaultmap = wm_keymap_copy(defaultmap);
1084                 wm_keymap_addon_add(defaultmap, addonmap);
1085         }
1086
1087         /* find original item */
1088         orig = WM_keymap_item_find_id(defaultmap, kmi->id);
1089
1090         if(orig) {
1091                 /* restore to original */
1092                 if(strcmp(orig->idname, kmi->idname) != 0) {
1093                         BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
1094                         WM_keymap_properties_reset(kmi, NULL);
1095                 }
1096
1097                 if (orig->properties) {
1098                         if(kmi->properties) {
1099                                 IDP_FreeProperty(kmi->properties);
1100                                 MEM_freeN(kmi->properties);
1101                                 kmi->properties= NULL;
1102                         }
1103
1104                         kmi->properties= IDP_CopyProperty(orig->properties);
1105                         kmi->ptr->data= kmi->properties;
1106                 }
1107
1108                 kmi->propvalue = orig->propvalue;
1109                 kmi->type = orig->type;
1110                 kmi->val = orig->val;
1111                 kmi->shift = orig->shift;
1112                 kmi->ctrl = orig->ctrl;
1113                 kmi->alt = orig->alt;
1114                 kmi->oskey = orig->oskey;
1115                 kmi->keymodifier = orig->keymodifier;
1116                 kmi->maptype = orig->maptype;
1117
1118                 WM_keyconfig_update_tag(keymap, kmi);
1119         }
1120
1121         /* free temporary keymap */
1122         if(addonmap) {
1123                 WM_keymap_free(defaultmap);
1124                 MEM_freeN(defaultmap);
1125         }
1126 }
1127
1128 void WM_keymap_restore_to_default(wmKeyMap *keymap, bContext *C)
1129 {
1130         wmWindowManager *wm = CTX_wm_manager(C);
1131         wmKeyMap *usermap;
1132
1133         /* remove keymap from U.user_keymaps and update */
1134         usermap= WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1135
1136         if(usermap) {
1137                 WM_keymap_free(usermap);
1138                 BLI_freelinkN(&U.user_keymaps, usermap);
1139
1140                 WM_keyconfig_update_tag(NULL, NULL);
1141                 WM_keyconfig_update(wm);
1142         }
1143 }
1144
1145 wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
1146 {
1147         wmKeyMapItem *kmi;
1148         
1149         for (kmi=keymap->items.first; kmi; kmi=kmi->next) {
1150                 if (kmi->id == id) {
1151                         return kmi;
1152                 }
1153         }
1154         
1155         return NULL;
1156 }
1157
1158 /* Guess an appropriate keymap from the operator name */
1159 /* Needs to be kept up to date with Keymap and Operator naming */
1160 wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
1161 {
1162         wmKeyMap *km=NULL;
1163         SpaceLink *sl = CTX_wm_space_data(C);
1164         
1165         /* Window */
1166         if (strstr(opname, "WM_OT")) {
1167                 km = WM_keymap_find_all(C, "Window", 0, 0);
1168         }
1169         /* Screen */
1170         else if (strstr(opname, "SCREEN_OT")) {
1171                 km = WM_keymap_find_all(C, "Screen", 0, 0);
1172         }
1173         /* Grease Pencil */
1174         else if (strstr(opname, "GPENCIL_OT")) {
1175                 km = WM_keymap_find_all(C, "Grease Pencil", 0, 0);
1176         }
1177         /* Markers */
1178         else if (strstr(opname, "MARKER_OT")) {
1179                 km = WM_keymap_find_all(C, "Markers", 0, 0);
1180         }
1181         
1182         
1183         /* 3D View */
1184         else if (strstr(opname, "VIEW3D_OT")) {
1185                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1186         }
1187         else if (strstr(opname, "OBJECT_OT")) {
1188                 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1189         }
1190
1191         
1192         /* Editing Modes */
1193         else if (strstr(opname, "MESH_OT")) {
1194                 km = WM_keymap_find_all(C, "Mesh", 0, 0);
1195                 
1196                 /* some mesh operators are active in object mode too, like add-prim */
1197                 if(km && km->poll && km->poll((bContext *)C)==0) {
1198                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1199                 }
1200         }
1201         else if (strstr(opname, "CURVE_OT")) {
1202                 km = WM_keymap_find_all(C, "Curve", 0, 0);
1203                 
1204                 /* some curve operators are active in object mode too, like add-prim */
1205                 if(km && km->poll && km->poll((bContext *)C)==0) {
1206                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1207                 }
1208         }
1209         else if (strstr(opname, "ARMATURE_OT")) {
1210                 km = WM_keymap_find_all(C, "Armature", 0, 0);
1211         }
1212         else if (strstr(opname, "POSE_OT")) {
1213                 km = WM_keymap_find_all(C, "Pose", 0, 0);
1214         }
1215         else if (strstr(opname, "SCULPT_OT")) {
1216                 switch(CTX_data_mode_enum(C)) {
1217                         case OB_MODE_SCULPT:
1218                                 km = WM_keymap_find_all(C, "Sculpt", 0, 0);
1219                                 break;
1220                         case OB_MODE_EDIT:
1221                                 km = WM_keymap_find_all(C, "UV Sculpt", 0, 0);
1222                                 break;
1223                 }
1224         }
1225         else if (strstr(opname, "MBALL_OT")) {
1226                 km = WM_keymap_find_all(C, "Metaball", 0, 0);
1227                 
1228                 /* some mball operators are active in object mode too, like add-prim */
1229                 if(km && km->poll && km->poll((bContext *)C)==0) {
1230                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1231                 }
1232         }
1233         else if (strstr(opname, "LATTICE_OT")) {
1234                 km = WM_keymap_find_all(C, "Lattice", 0, 0);
1235         }
1236         else if (strstr(opname, "PARTICLE_OT")) {
1237                 km = WM_keymap_find_all(C, "Particle", 0, 0);
1238         }
1239         else if (strstr(opname, "FONT_OT")) {
1240                 km = WM_keymap_find_all(C, "Font", 0, 0);
1241         }
1242         else if (strstr(opname, "PAINT_OT")) {
1243                 
1244                 /* check for relevant mode */
1245                 switch(CTX_data_mode_enum(C)) {
1246                         case OB_MODE_WEIGHT_PAINT:
1247                                 km = WM_keymap_find_all(C, "Weight Paint", 0, 0);
1248                                 break;
1249                         case OB_MODE_VERTEX_PAINT:
1250                                 km = WM_keymap_find_all(C, "Vertex Paint", 0, 0);
1251                                 break;
1252                         case OB_MODE_TEXTURE_PAINT:
1253                                 km = WM_keymap_find_all(C, "Image Paint", 0, 0);
1254                                 break;
1255                 }
1256         }
1257         /* Paint Face Mask */
1258         else if (strstr(opname, "PAINT_OT_face_select")) {
1259                 km = WM_keymap_find_all(C, "Face Mask", sl->spacetype, 0);
1260         }
1261         /* Timeline */
1262         else if (strstr(opname, "TIME_OT")) {
1263                 km = WM_keymap_find_all(C, "Timeline", sl->spacetype, 0);
1264         }
1265         /* Image Editor */
1266         else if (strstr(opname, "IMAGE_OT")) {
1267                 km = WM_keymap_find_all(C, "Image", sl->spacetype, 0);
1268         }
1269         /* UV Editor */
1270         else if (strstr(opname, "UV_OT")) {
1271                 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1272         }
1273         /* Node Editor */
1274         else if (strstr(opname, "NODE_OT")) {
1275                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1276         }
1277         /* Animation Editor Channels */
1278         else if (strstr(opname, "ANIM_OT_channels")) {
1279                 km = WM_keymap_find_all(C, "Animation Channels", sl->spacetype, 0);
1280         }
1281         /* Animation Generic - after channels */
1282         else if (strstr(opname, "ANIM_OT")) {
1283                 km = WM_keymap_find_all(C, "Animation", 0, 0);
1284         }
1285         /* Graph Editor */
1286         else if (strstr(opname, "GRAPH_OT")) {
1287                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1288         }
1289         /* Dopesheet Editor */
1290         else if (strstr(opname, "ACTION_OT")) {
1291                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1292         }
1293         /* NLA Editor */
1294         else if (strstr(opname, "NLA_OT")) {
1295                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1296         }
1297         /* Script */
1298         else if (strstr(opname, "SCRIPT_OT")) {
1299                 km = WM_keymap_find_all(C, "Script", sl->spacetype, 0);
1300         }
1301         /* Text */
1302         else if (strstr(opname, "TEXT_OT")) {
1303                 km = WM_keymap_find_all(C, "Text", sl->spacetype, 0);
1304         }
1305         /* Sequencer */
1306         else if (strstr(opname, "SEQUENCER_OT")) {
1307                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1308         }
1309         /* Console */
1310         else if (strstr(opname, "CONSOLE_OT")) {
1311                 km = WM_keymap_find_all(C, "Console", sl->spacetype, 0);
1312         }
1313         /* Console */
1314         else if (strstr(opname, "INFO_OT")) {
1315                 km = WM_keymap_find_all(C, "Info", sl->spacetype, 0);
1316         }
1317         
1318         /* Transform */
1319         else if (strstr(opname, "TRANSFORM_OT")) {
1320                 
1321                 /* check for relevant editor */
1322                 switch(sl->spacetype) {
1323                         case SPACE_VIEW3D:
1324                                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1325                                 break;
1326                         case SPACE_IPO:
1327                                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1328                                 break;
1329                         case SPACE_ACTION:
1330                                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1331                                 break;
1332                         case SPACE_NLA:
1333                                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1334                                 break;
1335                         case SPACE_IMAGE:
1336                                 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1337                                 break;
1338                         case SPACE_NODE:
1339                                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1340                                 break;
1341                         case SPACE_SEQ:
1342                                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1343                                 break;
1344                 }
1345         }
1346         
1347         return km;
1348 }
1349