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