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