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_windowmanager_types.h"
34 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
38 #include "BKE_blender.h"
39 #include "BKE_context.h"
40 #include "BKE_idprop.h"
41 #include "BKE_library.h"
43 #include "BKE_utildefines.h"
45 #include "RNA_access.h"
46 #include "RNA_types.h"
47 #include "RNA_enum_types.h"
51 #include "wm_window.h"
52 #include "wm_event_system.h"
53 #include "wm_event_types.h"
55 /* ***************** generic call, exported **************** */
57 static void keymap_event_set(wmKeymapItem *kmi, short type, short val, int modifier, short keymodifier)
61 kmi->keymodifier= keymodifier;
63 if(modifier == KM_ANY) {
64 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
68 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
71 if(modifier & KM_SHIFT)
73 else if(modifier & KM_SHIFT2)
75 if(modifier & KM_CTRL)
77 else if(modifier & KM_CTRL2)
81 else if(modifier & KM_ALT2)
83 if(modifier & KM_OSKEY)
85 else if(modifier & KM_OSKEY2)
90 static void keymap_properties_set(wmKeymapItem *kmi)
93 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeymapItemPtr");
94 WM_operator_properties_create(kmi->ptr, kmi->idname);
98 /* if item was added, then bail out */
99 wmKeymapItem *WM_keymap_verify_item(wmKeyMap *keymap, char *idname, short type, short val, int modifier, short keymodifier)
103 for(kmi= keymap->keymap.first; kmi; kmi= kmi->next)
104 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
107 kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
109 BLI_addtail(&keymap->keymap, kmi);
110 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
112 keymap_event_set(kmi, type, val, modifier, keymodifier);
113 keymap_properties_set(kmi);
118 /* always add item */
119 wmKeymapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, short type, short val, int modifier, short keymodifier)
121 wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
123 BLI_addtail(&keymap->keymap, kmi);
124 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
126 keymap_event_set(kmi, type, val, modifier, keymodifier);
127 keymap_properties_set(kmi);
131 /* ****************** storage in WM ************ */
133 /* name id's are for storing general or multiple keymaps,
134 space/region ids are same as DNA_space_types.h */
135 /* gets free'd in wm.c */
137 wmKeyMap *WM_keymap_find(wmWindowManager *wm, const char *nameid, short spaceid, short regionid)
141 for(km= wm->keymaps.first; km; km= km->next)
142 if(km->spaceid==spaceid && km->regionid==regionid)
143 if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
147 km= MEM_callocN(sizeof(struct wmKeyMap), "keymap list");
148 BLI_strncpy(km->nameid, nameid, KMAP_MAX_NAME);
149 km->spaceid= spaceid;
150 km->regionid= regionid;
151 BLI_addtail(&wm->keymaps, km);
157 /* ****************** modal keymaps ************ */
159 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
161 wmKeyMap *WM_modalkeymap_add(wmWindowManager *wm, const char *nameid, EnumPropertyItem *items)
163 wmKeyMap *km= WM_keymap_find(wm, nameid, 0, 0);
170 wmKeyMap *WM_modalkeymap_get(wmWindowManager *wm, const char *nameid)
174 for(km= wm->keymaps.first; km; km= km->next)
176 if(0==strncmp(nameid, km->nameid, KMAP_MAX_NAME))
183 void WM_modalkeymap_add_item(wmKeyMap *km, short type, short val, int modifier, short keymodifier, short value)
185 wmKeymapItem *kmi= MEM_callocN(sizeof(wmKeymapItem), "keymap entry");
187 BLI_addtail(&km->keymap, kmi);
188 kmi->propvalue= value;
190 keymap_event_set(kmi, type, val, modifier, keymodifier);
193 void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
195 wmOperatorType *ot= WM_operatortype_find(opname, 0);
200 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
204 /* ***************** get string from key events **************** */
206 const char *WM_key_event_string(short type)
208 const char *name= NULL;
209 if(RNA_enum_name(event_type_items, (int)type, &name))
215 static char *wm_keymap_item_to_string(wmKeymapItem *kmi, char *str, int len)
222 strcat(buf, "Shift ");
225 strcat(buf, "Ctrl ");
233 strcat(buf, WM_key_event_string(kmi->type));
234 BLI_strncpy(str, buf, len);
239 static wmKeymapItem *wm_keymap_item_find_handlers(const bContext *C, ListBase *handlers, const char *opname, int opcontext, IDProperty *properties, int compare_props)
241 wmEventHandler *handler;
245 /* find keymap item in handlers */
246 for(handler=handlers->first; handler; handler=handler->next) {
247 keymap= handler->keymap;
249 if(keymap && (!keymap->poll || keymap->poll((bContext*)C))) {
250 for(kmi=keymap->keymap.first; kmi; kmi=kmi->next) {
251 if(strcmp(kmi->idname, opname) == 0 && WM_key_event_string(kmi->type)[0]) {
253 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data))
266 static wmKeymapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props)
268 wmKeymapItem *found= NULL;
270 /* look into multiple handler lists to find the item */
272 found= wm_keymap_item_find_handlers(C, &CTX_wm_window(C)->handlers, opname, opcontext, properties, compare_props);
275 if(CTX_wm_area(C) && found==NULL)
276 found= wm_keymap_item_find_handlers(C, &CTX_wm_area(C)->handlers, opname, opcontext, properties, compare_props);
279 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
281 ARegion *ar= CTX_wm_area(C)->regionbase.first;
282 for(; ar; ar= ar->next)
283 if(ar->regiontype==RGN_TYPE_WINDOW)
287 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props);
292 found= wm_keymap_item_find_handlers(C, &CTX_wm_region(C)->handlers, opname, opcontext, properties, compare_props);
299 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
301 wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties, 1);
304 found= wm_keymap_item_find(C, opname, opcontext, properties, 0);
307 wm_keymap_item_to_string(found, str, len);
314 /* searches context and changes keymap item, if found */
315 void WM_key_event_operator_change(const bContext *C, const char *opname, int opcontext, IDProperty *properties, short key, short modifier)
317 wmKeymapItem *found= wm_keymap_item_find(C, opname, opcontext, properties, 1);
320 found= wm_keymap_item_find(C, opname, opcontext, properties, 0);
323 keymap_event_set(found, key, KM_PRESS, modifier, 0);
327 /* ********************* */
329 int WM_key_event_is_tweak(short type)
331 if(type>=EVT_TWEAK_L && type<=EVT_GESTURE)