style cleanup: comment blocks
[blender-staging.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 freed 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(
771         const bContext *C, ListBase *handlers, const char *opname, int UNUSED(opcontext),
772         IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
773 {
774         wmWindowManager *wm= CTX_wm_manager(C);
775         wmEventHandler *handler;
776         wmKeyMap *keymap;
777         wmKeyMapItem *kmi;
778
779         /* find keymap item in handlers */
780         for(handler=handlers->first; handler; handler=handler->next) {
781                 keymap= WM_keymap_active(wm, handler->keymap);
782
783                 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
784                         for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
785                                 
786                                 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
787                                         if (hotkey)
788                                                 if (!ISHOTKEY(kmi->type))
789                                                         continue;
790                                         
791                                         if(compare_props) {
792                                                 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
793                                                         if(keymap_r) *keymap_r= keymap;
794                                                         return kmi;
795                                                 }
796                                         }
797                                         else {
798                                                 if(keymap_r) *keymap_r= keymap;
799                                                 return kmi;
800                                         }
801                                 }
802                         }
803                 }
804         }
805         
806         /* ensure un-initialized keymap is never used */
807         if(keymap_r) *keymap_r= NULL;
808         return NULL;
809 }
810
811 static wmKeyMapItem *wm_keymap_item_find_props(
812         const bContext *C, const char *opname, int opcontext,
813         IDProperty *properties, int compare_props, int hotkey, wmKeyMap **keymap_r)
814 {
815         wmWindow *win= CTX_wm_window(C);
816         ScrArea *sa= CTX_wm_area(C);
817         ARegion *ar= CTX_wm_region(C);
818         wmKeyMapItem *found= NULL;
819
820         /* look into multiple handler lists to find the item */
821         if(win)
822                 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
823         
824
825         if(sa && found==NULL)
826                 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
827
828         if(found==NULL) {
829                 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
830                         if(sa) {
831                                 if (!(ar && ar->regiontype == RGN_TYPE_WINDOW))
832                                         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
833                                 
834                                 if(ar)
835                                         found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
836                         }
837                 }
838                 else if(ELEM(opcontext, WM_OP_EXEC_REGION_CHANNELS, WM_OP_INVOKE_REGION_CHANNELS)) {
839                         if (!(ar && ar->regiontype == RGN_TYPE_CHANNELS))
840                                 ar= BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
841
842                         if(ar)
843                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
844                 }
845                 else if(ELEM(opcontext, WM_OP_EXEC_REGION_PREVIEW, WM_OP_INVOKE_REGION_PREVIEW)) {
846                         if (!(ar && ar->regiontype == RGN_TYPE_PREVIEW))
847                                 ar= BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
848
849                         if(ar)
850                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
851                 }
852                 else {
853                         if(ar)
854                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, hotkey, keymap_r);
855                 }
856         }
857         
858         return found;
859 }
860
861 static wmKeyMapItem *wm_keymap_item_find(
862         const bContext *C, const char *opname, int opcontext,
863         IDProperty *properties, const short hotkey, const short sloppy, wmKeyMap **keymap_r)
864 {
865         wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, hotkey, keymap_r);
866
867         if(!found && sloppy)
868                 found= wm_keymap_item_find_props(C, opname, opcontext, NULL, 0, hotkey, keymap_r);
869
870         return found;
871 }
872
873 char *WM_key_event_operator_string(
874         const bContext *C, const char *opname, int opcontext,
875         IDProperty *properties, const short sloppy, char *str, int len)
876 {
877         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, 0, sloppy, NULL);
878         
879         if(kmi) {
880                 WM_keymap_item_to_string(kmi, str, len);
881                 return str;
882         }
883
884         return NULL;
885 }
886
887 int WM_key_event_operator_id(
888         const bContext *C, const char *opname, int opcontext,
889         IDProperty *properties, int hotkey, wmKeyMap **keymap_r)
890 {
891         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, hotkey, TRUE, keymap_r);
892         
893         if(kmi)
894                 return kmi->id;
895         else
896                 return 0;
897 }
898
899 int     WM_keymap_item_compare(wmKeyMapItem *k1, wmKeyMapItem *k2)
900 {
901         int k1type, k2type;
902
903         if (k1->flag & KMI_INACTIVE || k2->flag & KMI_INACTIVE)
904                 return 0;
905
906         /* take event mapping into account */
907         k1type = WM_userdef_event_map(k1->type);
908         k2type = WM_userdef_event_map(k2->type);
909
910         if(k1type != KM_ANY && k2type != KM_ANY && k1type != k2type)
911                 return 0;
912
913         if(k1->val != KM_ANY && k2->val != KM_ANY) {
914                 /* take click, press, release conflict into account */
915                 if (k1->val == KM_CLICK && ELEM3(k2->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
916                         return 0;
917                 if (k2->val == KM_CLICK && ELEM3(k1->val, KM_PRESS, KM_RELEASE, KM_CLICK) == 0)
918                         return 0;
919                 if (k1->val != k2->val)
920                         return 0;
921         }
922
923         if(k1->shift != KM_ANY && k2->shift != KM_ANY && k1->shift != k2->shift)
924                 return 0;
925
926         if(k1->ctrl != KM_ANY && k2->ctrl != KM_ANY && k1->ctrl != k2->ctrl)
927                 return 0;
928
929         if(k1->alt != KM_ANY && k2->alt != KM_ANY && k1->alt != k2->alt)
930                 return 0;
931
932         if(k1->oskey != KM_ANY && k2->oskey != KM_ANY && k1->oskey != k2->oskey)
933                 return 0;
934
935         if(k1->keymodifier != k2->keymodifier)
936                 return 0;
937
938         return 1;
939 }
940
941 /************************* Update Final Configuration *************************
942  * On load or other changes, the final user key configuration is rebuilt from
943  * the preset, addon and user preferences keymaps. We also test if the final
944  * configuration changed and write the changes to the user preferences. */
945
946 static int WM_KEYMAP_UPDATE = 0;
947
948 void WM_keyconfig_update_tag(wmKeyMap *km, wmKeyMapItem *kmi)
949 {
950         /* quick tag to do delayed keymap updates */
951         WM_KEYMAP_UPDATE= 1;
952
953         if(km)
954                 km->flag |= KEYMAP_UPDATE;
955         if(kmi)
956                 kmi->flag |= KMI_UPDATE;
957 }
958
959 static int wm_keymap_test_and_clear_update(wmKeyMap *km)
960 {
961         wmKeyMapItem *kmi;
962         int update;
963         
964         update= (km->flag & KEYMAP_UPDATE);
965         km->flag &= ~KEYMAP_UPDATE;
966
967         for(kmi=km->items.first; kmi; kmi=kmi->next) {
968                 update= update || (kmi->flag & KMI_UPDATE);
969                 kmi->flag &= ~KMI_UPDATE;
970         }
971         
972         return update;
973 }
974
975 static wmKeyMap *wm_keymap_preset(wmWindowManager *wm, wmKeyMap *km)
976 {
977         wmKeyConfig *keyconf= WM_keyconfig_active(wm);
978         wmKeyMap *keymap;
979
980         keymap= WM_keymap_list_find(&keyconf->keymaps, km->idname, km->spaceid, km->regionid);
981         if(!keymap)
982                 keymap= WM_keymap_list_find(&wm->defaultconf->keymaps, km->idname, km->spaceid, km->regionid);
983
984         return keymap;
985 }
986
987 void WM_keyconfig_update(wmWindowManager *wm)
988 {
989         wmKeyMap *km, *defaultmap, *addonmap, *usermap, *kmn;
990         wmKeyMapItem *kmi;
991         wmKeyMapDiffItem *kmdi;
992         int compat_update = 0;
993
994         if(G.background)
995                 return;
996         if(!WM_KEYMAP_UPDATE)
997                 return;
998         
999         /* update operator properties for non-modal user keymaps */
1000         for(km=U.user_keymaps.first; km; km=km->next) {
1001                 if((km->flag & KEYMAP_MODAL) == 0) {
1002                         for(kmdi=km->diff_items.first; kmdi; kmdi=kmdi->next) {
1003                                 if(kmdi->add_item)
1004                                         wm_keymap_item_properties_set(kmdi->add_item);
1005                                 if(kmdi->remove_item)
1006                                         wm_keymap_item_properties_set(kmdi->remove_item);
1007                         }
1008
1009                         for(kmi=km->items.first; kmi; kmi=kmi->next)
1010                                 wm_keymap_item_properties_set(kmi);
1011                 }
1012         }
1013
1014         /* update U.user_keymaps with user key configuration changes */
1015         for(km=wm->userconf->keymaps.first; km; km=km->next) {
1016                 /* only diff if the user keymap was modified */
1017                 if(wm_keymap_test_and_clear_update(km)) {
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
1022                         /* diff */
1023                         if(defaultmap)
1024                                 wm_keymap_diff_update(&U.user_keymaps, defaultmap, addonmap, km);
1025                 }
1026         }
1027
1028         /* create user key configuration from preset + addon + user preferences */
1029         for(km=wm->defaultconf->keymaps.first; km; km=km->next) {
1030                 /* find keymaps */
1031                 defaultmap= wm_keymap_preset(wm, km);
1032                 addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, km->idname, km->spaceid, km->regionid);
1033                 usermap= WM_keymap_list_find(&U.user_keymaps, km->idname, km->spaceid, km->regionid);
1034
1035                 /* add */
1036                 kmn= wm_keymap_patch_update(&wm->userconf->keymaps, defaultmap, addonmap, usermap);
1037
1038                 if(kmn) {
1039                         kmn->modal_items= km->modal_items;
1040                         kmn->poll= km->poll;
1041                 }
1042
1043                 /* in case of old non-diff keymaps, force extra update to create diffs */
1044                 compat_update = compat_update || (usermap && !(usermap->flag & KEYMAP_DIFF));
1045         }
1046
1047         WM_KEYMAP_UPDATE= 0;
1048
1049         if(compat_update) {
1050                 WM_keyconfig_update_tag(NULL, NULL);
1051                 WM_keyconfig_update(wm);
1052         }
1053 }
1054
1055 /********************************* Event Handling *****************************
1056  * Handlers have pointers to the keymap in the default configuration. During
1057  * event handling this function is called to get the keymap from the final
1058  * configuration. */
1059
1060 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
1061 {
1062         wmKeyMap *km;
1063
1064         if(!keymap)
1065                 return NULL;
1066         
1067         /* first user defined keymaps */
1068         km= WM_keymap_list_find(&wm->userconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1069
1070         if(km)
1071                 return km;
1072
1073         return keymap;
1074 }
1075
1076 /******************************* Keymap Editor ********************************
1077  * In the keymap editor the user key configuration is edited. */
1078
1079 void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
1080 {
1081         wmWindowManager *wm = CTX_wm_manager(C);
1082         wmKeyMap *defaultmap, *addonmap;
1083         wmKeyMapItem *orig;
1084
1085         if(!keymap)
1086                 return;
1087
1088         /* construct default keymap from preset + addons */
1089         defaultmap= wm_keymap_preset(wm, keymap);
1090         addonmap= WM_keymap_list_find(&wm->addonconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1091
1092         if(addonmap) {
1093                 defaultmap = wm_keymap_copy(defaultmap);
1094                 wm_keymap_addon_add(defaultmap, addonmap);
1095         }
1096
1097         /* find original item */
1098         orig = WM_keymap_item_find_id(defaultmap, kmi->id);
1099
1100         if(orig) {
1101                 /* restore to original */
1102                 if(strcmp(orig->idname, kmi->idname) != 0) {
1103                         BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
1104                         WM_keymap_properties_reset(kmi, NULL);
1105                 }
1106
1107                 if (orig->properties) {
1108                         if(kmi->properties) {
1109                                 IDP_FreeProperty(kmi->properties);
1110                                 MEM_freeN(kmi->properties);
1111                                 kmi->properties= NULL;
1112                         }
1113
1114                         kmi->properties= IDP_CopyProperty(orig->properties);
1115                         kmi->ptr->data= kmi->properties;
1116                 }
1117
1118                 kmi->propvalue = orig->propvalue;
1119                 kmi->type = orig->type;
1120                 kmi->val = orig->val;
1121                 kmi->shift = orig->shift;
1122                 kmi->ctrl = orig->ctrl;
1123                 kmi->alt = orig->alt;
1124                 kmi->oskey = orig->oskey;
1125                 kmi->keymodifier = orig->keymodifier;
1126                 kmi->maptype = orig->maptype;
1127
1128                 WM_keyconfig_update_tag(keymap, kmi);
1129         }
1130
1131         /* free temporary keymap */
1132         if(addonmap) {
1133                 WM_keymap_free(defaultmap);
1134                 MEM_freeN(defaultmap);
1135         }
1136 }
1137
1138 void WM_keymap_restore_to_default(wmKeyMap *keymap, bContext *C)
1139 {
1140         wmWindowManager *wm = CTX_wm_manager(C);
1141         wmKeyMap *usermap;
1142
1143         /* remove keymap from U.user_keymaps and update */
1144         usermap= WM_keymap_list_find(&U.user_keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
1145
1146         if(usermap) {
1147                 WM_keymap_free(usermap);
1148                 BLI_freelinkN(&U.user_keymaps, usermap);
1149
1150                 WM_keyconfig_update_tag(NULL, NULL);
1151                 WM_keyconfig_update(wm);
1152         }
1153 }
1154
1155 wmKeyMapItem *WM_keymap_item_find_id(wmKeyMap *keymap, int id)
1156 {
1157         wmKeyMapItem *kmi;
1158         
1159         for (kmi=keymap->items.first; kmi; kmi=kmi->next) {
1160                 if (kmi->id == id) {
1161                         return kmi;
1162                 }
1163         }
1164         
1165         return NULL;
1166 }
1167
1168 /* Guess an appropriate keymap from the operator name */
1169 /* Needs to be kept up to date with Keymap and Operator naming */
1170 wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname)
1171 {
1172         wmKeyMap *km=NULL;
1173         SpaceLink *sl = CTX_wm_space_data(C);
1174         
1175         /* Window */
1176         if (strstr(opname, "WM_OT")) {
1177                 km = WM_keymap_find_all(C, "Window", 0, 0);
1178         }
1179         /* Screen */
1180         else if (strstr(opname, "SCREEN_OT")) {
1181                 km = WM_keymap_find_all(C, "Screen", 0, 0);
1182         }
1183         /* Grease Pencil */
1184         else if (strstr(opname, "GPENCIL_OT")) {
1185                 km = WM_keymap_find_all(C, "Grease Pencil", 0, 0);
1186         }
1187         /* Markers */
1188         else if (strstr(opname, "MARKER_OT")) {
1189                 km = WM_keymap_find_all(C, "Markers", 0, 0);
1190         }
1191         
1192         
1193         /* 3D View */
1194         else if (strstr(opname, "VIEW3D_OT")) {
1195                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1196         }
1197         else if (strstr(opname, "OBJECT_OT")) {
1198                 km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1199         }
1200
1201         
1202         /* Editing Modes */
1203         else if (strstr(opname, "MESH_OT")) {
1204                 km = WM_keymap_find_all(C, "Mesh", 0, 0);
1205                 
1206                 /* some mesh operators are active in object mode too, like add-prim */
1207                 if(km && km->poll && km->poll((bContext *)C)==0) {
1208                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1209                 }
1210         }
1211         else if (strstr(opname, "CURVE_OT")) {
1212                 km = WM_keymap_find_all(C, "Curve", 0, 0);
1213                 
1214                 /* some curve operators are active in object mode too, like add-prim */
1215                 if(km && km->poll && km->poll((bContext *)C)==0) {
1216                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1217                 }
1218         }
1219         else if (strstr(opname, "ARMATURE_OT")) {
1220                 km = WM_keymap_find_all(C, "Armature", 0, 0);
1221         }
1222         else if (strstr(opname, "POSE_OT")) {
1223                 km = WM_keymap_find_all(C, "Pose", 0, 0);
1224         }
1225         else if (strstr(opname, "SCULPT_OT")) {
1226                 switch(CTX_data_mode_enum(C)) {
1227                         case OB_MODE_SCULPT:
1228                                 km = WM_keymap_find_all(C, "Sculpt", 0, 0);
1229                                 break;
1230                         case OB_MODE_EDIT:
1231                                 km = WM_keymap_find_all(C, "UV Sculpt", 0, 0);
1232                                 break;
1233                 }
1234         }
1235         else if (strstr(opname, "MBALL_OT")) {
1236                 km = WM_keymap_find_all(C, "Metaball", 0, 0);
1237                 
1238                 /* some mball operators are active in object mode too, like add-prim */
1239                 if(km && km->poll && km->poll((bContext *)C)==0) {
1240                         km = WM_keymap_find_all(C, "Object Mode", 0, 0);
1241                 }
1242         }
1243         else if (strstr(opname, "LATTICE_OT")) {
1244                 km = WM_keymap_find_all(C, "Lattice", 0, 0);
1245         }
1246         else if (strstr(opname, "PARTICLE_OT")) {
1247                 km = WM_keymap_find_all(C, "Particle", 0, 0);
1248         }
1249         else if (strstr(opname, "FONT_OT")) {
1250                 km = WM_keymap_find_all(C, "Font", 0, 0);
1251         }
1252         else if (strstr(opname, "PAINT_OT")) {
1253                 
1254                 /* check for relevant mode */
1255                 switch(CTX_data_mode_enum(C)) {
1256                         case OB_MODE_WEIGHT_PAINT:
1257                                 km = WM_keymap_find_all(C, "Weight Paint", 0, 0);
1258                                 break;
1259                         case OB_MODE_VERTEX_PAINT:
1260                                 km = WM_keymap_find_all(C, "Vertex Paint", 0, 0);
1261                                 break;
1262                         case OB_MODE_TEXTURE_PAINT:
1263                                 km = WM_keymap_find_all(C, "Image Paint", 0, 0);
1264                                 break;
1265                 }
1266         }
1267         /* Paint Face Mask */
1268         else if (strstr(opname, "PAINT_OT_face_select")) {
1269                 km = WM_keymap_find_all(C, "Face Mask", sl->spacetype, 0);
1270         }
1271         /* Timeline */
1272         else if (strstr(opname, "TIME_OT")) {
1273                 km = WM_keymap_find_all(C, "Timeline", sl->spacetype, 0);
1274         }
1275         /* Image Editor */
1276         else if (strstr(opname, "IMAGE_OT")) {
1277                 km = WM_keymap_find_all(C, "Image", sl->spacetype, 0);
1278         }
1279         /* UV Editor */
1280         else if (strstr(opname, "UV_OT")) {
1281                 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1282         }
1283         /* Node Editor */
1284         else if (strstr(opname, "NODE_OT")) {
1285                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1286         }
1287         /* Animation Editor Channels */
1288         else if (strstr(opname, "ANIM_OT_channels")) {
1289                 km = WM_keymap_find_all(C, "Animation Channels", sl->spacetype, 0);
1290         }
1291         /* Animation Generic - after channels */
1292         else if (strstr(opname, "ANIM_OT")) {
1293                 km = WM_keymap_find_all(C, "Animation", 0, 0);
1294         }
1295         /* Graph Editor */
1296         else if (strstr(opname, "GRAPH_OT")) {
1297                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1298         }
1299         /* Dopesheet Editor */
1300         else if (strstr(opname, "ACTION_OT")) {
1301                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1302         }
1303         /* NLA Editor */
1304         else if (strstr(opname, "NLA_OT")) {
1305                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1306         }
1307         /* Script */
1308         else if (strstr(opname, "SCRIPT_OT")) {
1309                 km = WM_keymap_find_all(C, "Script", sl->spacetype, 0);
1310         }
1311         /* Text */
1312         else if (strstr(opname, "TEXT_OT")) {
1313                 km = WM_keymap_find_all(C, "Text", sl->spacetype, 0);
1314         }
1315         /* Sequencer */
1316         else if (strstr(opname, "SEQUENCER_OT")) {
1317                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1318         }
1319         /* Console */
1320         else if (strstr(opname, "CONSOLE_OT")) {
1321                 km = WM_keymap_find_all(C, "Console", sl->spacetype, 0);
1322         }
1323         /* Console */
1324         else if (strstr(opname, "INFO_OT")) {
1325                 km = WM_keymap_find_all(C, "Info", sl->spacetype, 0);
1326         }
1327         
1328         /* Transform */
1329         else if (strstr(opname, "TRANSFORM_OT")) {
1330                 
1331                 /* check for relevant editor */
1332                 switch(sl->spacetype) {
1333                         case SPACE_VIEW3D:
1334                                 km = WM_keymap_find_all(C, "3D View", sl->spacetype, 0);
1335                                 break;
1336                         case SPACE_IPO:
1337                                 km = WM_keymap_find_all(C, "Graph Editor", sl->spacetype, 0);
1338                                 break;
1339                         case SPACE_ACTION:
1340                                 km = WM_keymap_find_all(C, "Dopesheet", sl->spacetype, 0);
1341                                 break;
1342                         case SPACE_NLA:
1343                                 km = WM_keymap_find_all(C, "NLA Editor", sl->spacetype, 0);
1344                                 break;
1345                         case SPACE_IMAGE:
1346                                 km = WM_keymap_find_all(C, "UV Editor", sl->spacetype, 0);
1347                                 break;
1348                         case SPACE_NODE:
1349                                 km = WM_keymap_find_all(C, "Node Editor", sl->spacetype, 0);
1350                                 break;
1351                         case SPACE_SEQ:
1352                                 km = WM_keymap_find_all(C, "Sequencer", sl->spacetype, 0);
1353                                 break;
1354                 }
1355         }
1356         
1357         return km;
1358 }
1359