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 WM_operator_properties_alloc(&(kmi->ptr), &(kmi->properties), kmi->idname);
63 wmKeyConfig *WM_keyconfig_add(wmWindowManager *wm, char *idname)
67 keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
68 BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
69 BLI_addtail(&wm->keyconfigs, keyconf);
74 void WM_keyconfig_free(wmKeyConfig *keyconf)
78 while((km= keyconf->keymaps.first)) {
80 BLI_freelinkN(&keyconf->keymaps, km);
86 void WM_keyconfig_userdef(wmWindowManager *wm)
91 for(km=U.keymaps.first; km; km=km->next)
92 for(kmi=km->items.first; kmi; kmi=kmi->next)
93 keymap_properties_set(kmi);
96 static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
100 for(kc= lb->first; kc; kc= kc->next)
101 if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
107 /* ************************ free ************************* */
109 void WM_keymap_free(wmKeyMap *keymap)
113 for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
115 WM_operator_properties_free(kmi->ptr);
120 BLI_freelistN(&keymap->items);
123 /* ***************** generic call, exported **************** */
125 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
129 kmi->keymodifier= keymodifier;
131 if(modifier == KM_ANY) {
132 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
136 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
139 if(modifier & KM_SHIFT)
141 else if(modifier & KM_SHIFT2)
143 if(modifier & KM_CTRL)
145 else if(modifier & KM_CTRL2)
147 if(modifier & KM_ALT)
149 else if(modifier & KM_ALT2)
151 if(modifier & KM_OSKEY)
153 else if(modifier & KM_OSKEY2)
158 /* if item was added, then bail out */
159 wmKeyMapItem *WM_keymap_verify_item(wmKeyMap *keymap, char *idname, int type, int val, int modifier, int keymodifier)
163 for(kmi= keymap->items.first; kmi; kmi= kmi->next)
164 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
167 kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
169 BLI_addtail(&keymap->items, kmi);
170 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
172 keymap_event_set(kmi, type, val, modifier, keymodifier);
173 keymap_properties_set(kmi);
178 /* always add item */
179 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, int type, int val, int modifier, int keymodifier)
181 wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
183 BLI_addtail(&keymap->items, kmi);
184 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
186 keymap_event_set(kmi, type, val, modifier, keymodifier);
187 keymap_properties_set(kmi);
191 void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
193 if(BLI_findindex(&keymap->items, kmi) != -1) {
195 WM_operator_properties_free(kmi->ptr);
198 BLI_freelinkN(&keymap->items, kmi);
202 /* ****************** storage in WM ************ */
204 /* name id's are for storing general or multiple keymaps,
205 space/region ids are same as DNA_space_types.h */
206 /* gets free'd in wm.c */
208 static wmKeyMap *wm_keymap_list_find(ListBase *lb, char *idname, int spaceid, int regionid)
212 for(km= lb->first; km; km= km->next)
213 if(km->spaceid==spaceid && km->regionid==regionid)
214 if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
220 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, char *idname, int spaceid, int regionid)
222 wmKeyMap *km= wm_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
225 km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
226 BLI_strncpy(km->idname, idname, KMAP_MAX_NAME);
227 km->spaceid= spaceid;
228 km->regionid= regionid;
229 BLI_addtail(&keyconf->keymaps, km);
235 /* ****************** modal keymaps ************ */
237 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
239 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, char *idname, EnumPropertyItem *items)
241 wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
242 km->flag |= KEYMAP_MODAL;
243 km->modal_items= items;
248 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, char *idname)
252 for(km= keyconf->keymaps.first; km; km= km->next)
253 if(km->flag & KEYMAP_MODAL)
254 if(0==strncmp(idname, km->idname, KMAP_MAX_NAME))
261 void WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
263 wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
265 BLI_addtail(&km->items, kmi);
266 kmi->propvalue= value;
268 keymap_event_set(kmi, type, val, modifier, keymodifier);
271 void WM_modalkeymap_assign(wmKeyMap *km, char *opname)
273 wmOperatorType *ot= WM_operatortype_find(opname, 0);
278 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
281 /* ***************** get string from key events **************** */
283 const char *WM_key_event_string(short type)
285 const char *name= NULL;
286 if(RNA_enum_name(event_type_items, (int)type, &name))
292 char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
299 strcat(buf, "Shift ");
302 strcat(buf, "Ctrl ");
310 strcat(buf, WM_key_event_string(kmi->type));
311 BLI_strncpy(str, buf, len);
316 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)
318 wmWindowManager *wm= CTX_wm_manager(C);
319 wmEventHandler *handler;
323 /* find keymap item in handlers */
324 for(handler=handlers->first; handler; handler=handler->next) {
325 keymap= WM_keymap_active(wm, handler->keymap);
327 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
328 for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
329 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
331 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
332 if(keymap_r) *keymap_r= keymap;
337 if(keymap_r) *keymap_r= keymap;
348 static wmKeyMapItem *wm_keymap_item_find_props(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props, wmKeyMap **keymap_r)
350 wmWindow *win= CTX_wm_window(C);
351 ScrArea *sa= CTX_wm_area(C);
352 ARegion *ar= CTX_wm_region(C);
353 wmKeyMapItem *found= NULL;
355 /* look into multiple handler lists to find the item */
357 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, keymap_r);
360 if(sa && found==NULL)
361 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, keymap_r);
364 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
366 ARegion *ar= sa->regionbase.first;
367 for(; ar; ar= ar->next)
368 if(ar->regiontype==RGN_TYPE_WINDOW)
372 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
377 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
384 static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, wmKeyMap **keymap_r)
386 wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, keymap_r);
389 found= wm_keymap_item_find_props(C, opname, opcontext, properties, 0, keymap_r);
394 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
396 wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, NULL);
399 WM_keymap_item_to_string(kmi, str, len);
406 /* ***************** user preferences ******************* */
408 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
410 wmKeyConfig *keyconf;
416 /* first user defined keymaps */
417 km= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
421 /* then user key config */
422 keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
424 km= wm_keymap_list_find(&keyconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
429 /* then use default */
430 km= wm_keymap_list_find(&wm->defaultconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
434 wmKeyMap *WM_keymap_copy_to_user(wmKeyMap *keymap)
439 usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
442 /* not saved yet, duplicate existing */
443 usermap= MEM_dupallocN(keymap);
444 usermap->modal_items= NULL;
446 usermap->flag |= KEYMAP_USER;
448 BLI_addtail(&U.keymaps, usermap);
451 /* already saved, free items for re-copy */
452 WM_keymap_free(usermap);
455 BLI_duplicatelist(&usermap->items, &keymap->items);
457 for(kmi=usermap->items.first; kmi; kmi=kmi->next) {
458 if(kmi->properties) {
459 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "UserKeyMapItemPtr");
460 WM_operator_properties_create(kmi->ptr, kmi->idname);
462 kmi->properties= IDP_CopyProperty(kmi->properties);
463 kmi->ptr->data= kmi->properties;
467 for(kmi=keymap->items.first; kmi; kmi=kmi->next)
468 kmi->flag &= ~KMI_EXPANDED;
473 void WM_keymap_restore_to_default(wmKeyMap *keymap)
477 usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
480 WM_keymap_free(usermap);
481 BLI_freelinkN(&U.keymaps, usermap);
485 /* searches context and changes keymap item, if found */
486 void WM_key_event_operator_change(const bContext *C, const char *opname, int opcontext, IDProperty *properties, short key, short modifier)
488 wmWindowManager *wm= CTX_wm_manager(C);
492 kmi= wm_keymap_item_find(C, opname, opcontext, properties, &keymap);
495 /* if the existing one is in a default keymap, copy it
496 to user preferences, and lookup again so we get a
497 key map item from the user preferences we can modify */
498 if(BLI_findindex(&wm->defaultconf->keymaps, keymap) >= 0) {
499 WM_keymap_copy_to_user(keymap);
500 kmi= wm_keymap_item_find(C, opname, opcontext, properties, NULL);
503 keymap_event_set(kmi, key, KM_PRESS, modifier, 0);