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