4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2007 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
31 #include "DNA_screen_types.h"
32 #include "DNA_userdef_types.h"
33 #include "DNA_windowmanager_types.h"
35 #include "MEM_guardedalloc.h"
37 #include "BLI_blenlib.h"
39 #include "BKE_blender.h"
40 #include "BKE_context.h"
41 #include "BKE_idprop.h"
42 #include "BKE_library.h"
44 #include "BKE_utildefines.h"
46 #include "RNA_access.h"
47 #include "RNA_types.h"
48 #include "RNA_enum_types.h"
52 #include "wm_window.h"
53 #include "wm_event_system.h"
54 #include "wm_event_types.h"
56 /* ********************* key config ***********************/
58 static void keymap_properties_set(wmKeyMapItem *kmi)
60 if(!kmi->properties) {
61 IDPropertyTemplate val = {0};
62 kmi->properties= IDP_New(IDP_GROUP, val, "wmKeyMapItemProperties");
66 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeyMapItemPtr");
67 WM_operator_properties_create(kmi->ptr, kmi->idname);
70 kmi->ptr->data= kmi->properties;
73 wmKeyConfig *WM_keyconfig_add(wmWindowManager *wm, char *idname)
77 keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
78 BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
79 BLI_addtail(&wm->keyconfigs, keyconf);
84 void WM_keyconfig_free(wmKeyConfig *keyconf)
88 while((km= keyconf->keymaps.first)) {
90 BLI_freelinkN(&keyconf->keymaps, km);
96 void WM_keyconfig_userdef(wmWindowManager *wm)
101 for(km=U.keymaps.first; km; km=km->next)
102 for(kmi=km->items.first; kmi; kmi=kmi->next)
103 keymap_properties_set(kmi);
106 static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
110 for(kc= lb->first; kc; kc= kc->next)
111 if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
117 /* ************************ free ************************* */
119 void WM_keymap_free(wmKeyMap *keymap)
123 for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
125 WM_operator_properties_free(kmi->ptr);
130 BLI_freelistN(&keymap->items);
133 /* ***************** generic call, exported **************** */
135 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
139 kmi->keymodifier= keymodifier;
141 if(modifier == KM_ANY) {
142 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
146 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
149 if(modifier & KM_SHIFT)
151 else if(modifier & KM_SHIFT2)
153 if(modifier & KM_CTRL)
155 else if(modifier & KM_CTRL2)
157 if(modifier & KM_ALT)
159 else if(modifier & KM_ALT2)
161 if(modifier & KM_OSKEY)
163 else if(modifier & KM_OSKEY2)
168 /* if item was added, then bail out */
169 wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, char *idname, int type, int val, int modifier, int keymodifier)
173 for(kmi= keymap->items.first; kmi; kmi= kmi->next)
174 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
177 kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
179 BLI_addtail(&keymap->items, kmi);
180 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
182 keymap_event_set(kmi, type, val, modifier, keymodifier);
183 keymap_properties_set(kmi);
188 /* always add item */
189 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, int type, int val, int modifier, int keymodifier)
191 wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
193 BLI_addtail(&keymap->items, kmi);
194 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
196 keymap_event_set(kmi, type, val, modifier, keymodifier);
197 keymap_properties_set(kmi);
201 void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
203 if(BLI_findindex(&keymap->items, kmi) != -1) {
205 WM_operator_properties_free(kmi->ptr);
208 BLI_freelinkN(&keymap->items, kmi);
212 /* ****************** storage in WM ************ */
214 /* name id's are for storing general or multiple keymaps,
215 space/region ids are same as DNA_space_types.h */
216 /* gets free'd in wm.c */
218 static wmKeyMap *wm_keymap_list_find(ListBase *lb, char *idname, int spaceid, int regionid)
222 for(km= lb->first; km; km= km->next)
223 if(km->spaceid==spaceid && km->regionid==regionid)
224 if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
230 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, char *idname, int spaceid, int regionid)
232 wmKeyMap *km= wm_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
235 km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
236 BLI_strncpy(km->idname, idname, KMAP_MAX_NAME);
237 km->spaceid= spaceid;
238 km->regionid= regionid;
239 BLI_addtail(&keyconf->keymaps, km);
245 /* ****************** modal keymaps ************ */
247 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
249 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, char *idname, EnumPropertyItem *items)
251 wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
252 km->flag |= KEYMAP_MODAL;
253 km->modal_items= items;
258 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, char *idname)
262 for(km= keyconf->keymaps.first; km; km= km->next)
263 if(km->flag & KEYMAP_MODAL)
264 if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
271 void WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
273 wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
275 BLI_addtail(&km->items, kmi);
276 kmi->propvalue= value;
278 keymap_event_set(kmi, type, val, modifier, keymodifier);
281 void WM_modalkeymap_assign(wmKeyMap *km, char *opname)
283 wmOperatorType *ot= WM_operatortype_find(opname, 0);
288 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
291 /* ***************** get string from key events **************** */
293 const char *WM_key_event_string(short type)
295 const char *name= NULL;
296 if(RNA_enum_name(event_type_items, (int)type, &name))
302 char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
309 strcat(buf, "Shift ");
312 strcat(buf, "Ctrl ");
320 strcat(buf, WM_key_event_string(kmi->type));
321 BLI_strncpy(str, buf, len);
326 static wmKeyMapItem *wm_keymap_item_find_handlers(const bContext *C, ListBase *handlers, const char *opname, int opcontext, IDProperty *properties, int compare_props, wmKeyMap **keymap_r)
328 wmWindowManager *wm= CTX_wm_manager(C);
329 wmEventHandler *handler;
333 /* find keymap item in handlers */
334 for(handler=handlers->first; handler; handler=handler->next) {
335 keymap= WM_keymap_active(wm, handler->keymap);
337 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
338 for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
339 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
341 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
342 if(keymap_r) *keymap_r= keymap;
347 if(keymap_r) *keymap_r= keymap;
358 static wmKeyMapItem *wm_keymap_item_find_props(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props, wmKeyMap **keymap_r)
360 wmWindow *win= CTX_wm_window(C);
361 ScrArea *sa= CTX_wm_area(C);
362 ARegion *ar= CTX_wm_region(C);
363 wmKeyMapItem *found= NULL;
365 /* look into multiple handler lists to find the item */
367 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, keymap_r);
370 if(sa && found==NULL)
371 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, keymap_r);
374 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
376 ARegion *ar= sa->regionbase.first;
377 for(; ar; ar= ar->next)
378 if(ar->regiontype==RGN_TYPE_WINDOW)
382 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
387 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
394 static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, wmKeyMap **keymap_r)
396 wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, keymap_r);
399 found= wm_keymap_item_find_props(C, opname, opcontext, properties, 0, keymap_r);
404 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
406 wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, NULL);
409 WM_keymap_item_to_string(kmi, str, len);
416 /* ***************** user preferences ******************* */
418 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
420 wmKeyConfig *keyconf;
426 /* first user defined keymaps */
427 km= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
431 /* then user key config */
432 keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
434 km= wm_keymap_list_find(&keyconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
439 /* then use default */
440 km= wm_keymap_list_find(&wm->defaultconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
444 wmKeyMap *WM_keymap_copy_to_user(wmKeyMap *keymap)
449 usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
452 /* not saved yet, duplicate existing */
453 usermap= MEM_dupallocN(keymap);
454 usermap->modal_items= NULL;
456 usermap->flag |= KEYMAP_USER;
458 BLI_addtail(&U.keymaps, usermap);
461 /* already saved, free items for re-copy */
462 WM_keymap_free(usermap);
465 BLI_duplicatelist(&usermap->items, &keymap->items);
467 for(kmi=usermap->items.first; kmi; kmi=kmi->next) {
468 if(kmi->properties) {
469 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
470 WM_operator_properties_create(kmi->ptr, kmi->idname);
472 kmi->properties= IDP_CopyProperty(kmi->properties);
473 kmi->ptr->data= kmi->properties;
477 for(kmi=keymap->items.first; kmi; kmi=kmi->next)
478 kmi->flag &= ~KMI_EXPANDED;
483 void WM_keymap_restore_to_default(wmKeyMap *keymap)
487 usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
490 WM_keymap_free(usermap);
491 BLI_freelinkN(&U.keymaps, usermap);
495 /* searches context and changes keymap item, if found */
496 void WM_key_event_operator_change(const bContext *C, const char *opname, int opcontext, IDProperty *properties, short key, short modifier)
498 wmWindowManager *wm= CTX_wm_manager(C);
502 kmi= wm_keymap_item_find(C, opname, opcontext, properties, &keymap);
505 /* if the existing one is in a default keymap, copy it
506 to user preferences, and lookup again so we get a
507 key map item from the user preferences we can modify */
508 if(BLI_findindex(&wm->defaultconf->keymaps, keymap) >= 0) {
509 WM_keymap_copy_to_user(keymap);
510 kmi= wm_keymap_item_find(C, opname, opcontext, properties, NULL);
513 keymap_event_set(kmi, key, KM_PRESS, modifier, 0);