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