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