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