3d3a6e46fb2091a71cedf806e5e40d4b1374ce83
[blender.git] / source / blender / windowmanager / intern / wm_keymap.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30
31 #include "DNA_screen_types.h"
32 #include "DNA_userdef_types.h"
33 #include "DNA_windowmanager_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38
39 #include "BKE_blender.h"
40 #include "BKE_context.h"
41 #include "BKE_idprop.h"
42 #include "BKE_library.h"
43 #include "BKE_main.h"
44 #include "BKE_utildefines.h"
45
46 #include "RNA_access.h"
47 #include "RNA_types.h"
48 #include "RNA_enum_types.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52 #include "wm_window.h"
53 #include "wm_event_system.h"
54 #include "wm_event_types.h"
55
56 /* ********************* key config ***********************/
57
58 static void keymap_properties_set(wmKeyMapItem *kmi)
59 {
60         if(!kmi->properties) {
61                 IDPropertyTemplate val = {0};
62                 kmi->properties= IDP_New(IDP_GROUP, val, "wmKeyMapItemProperties");
63         }
64
65         if(!kmi->ptr) {
66                 kmi->ptr= MEM_callocN(sizeof(PointerRNA), "wmKeyMapItemPtr");
67                 WM_operator_properties_create(kmi->ptr, kmi->idname);
68         }
69
70         kmi->ptr->data= kmi->properties;
71 }
72
73 wmKeyConfig *WM_keyconfig_add(wmWindowManager *wm, char *idname)
74 {
75         wmKeyConfig *keyconf;
76         
77         keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
78         BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
79         BLI_addtail(&wm->keyconfigs, keyconf);
80
81         return keyconf;
82 }
83
84 void WM_keyconfig_free(wmKeyConfig *keyconf)
85 {
86         wmKeyMap *km;
87
88         while((km= keyconf->keymaps.first)) {
89                 WM_keymap_free(km);
90                 BLI_freelinkN(&keyconf->keymaps, km);
91         }
92
93         MEM_freeN(keyconf);
94 }
95
96 void WM_keyconfig_userdef(wmWindowManager *wm)
97 {
98         wmKeyMap *km;
99         wmKeyMapItem *kmi;
100
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);
104 }
105
106 static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
107 {
108         wmKeyConfig *kc;
109
110         for(kc= lb->first; kc; kc= kc->next)
111                 if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
112                         return kc;
113         
114         return NULL;
115 }
116
117 /* ************************ free ************************* */
118
119 void WM_keymap_free(wmKeyMap *keymap)
120 {
121         wmKeyMapItem *kmi;
122
123         for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
124                 if(kmi->ptr) {
125                         WM_operator_properties_free(kmi->ptr);
126                         MEM_freeN(kmi->ptr);
127                 }
128         }
129
130         BLI_freelistN(&keymap->items);
131 }
132
133 /* ***************** generic call, exported **************** */
134
135 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
136 {
137         kmi->type= type;
138         kmi->val= val;
139         kmi->keymodifier= keymodifier;
140         
141         if(modifier == KM_ANY) {
142                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
143         }
144         else {
145                 
146                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
147                 
148                 /* defines? */
149                 if(modifier & KM_SHIFT)
150                         kmi->shift= 1;
151                 else if(modifier & KM_SHIFT2)
152                         kmi->shift= 2;
153                 if(modifier & KM_CTRL)
154                         kmi->ctrl= 1;
155                 else if(modifier & KM_CTRL2)
156                         kmi->ctrl= 2;
157                 if(modifier & KM_ALT)
158                         kmi->alt= 1;
159                 else if(modifier & KM_ALT2)
160                         kmi->alt= 2;
161                 if(modifier & KM_OSKEY)
162                         kmi->oskey= 1;
163                 else if(modifier & KM_OSKEY2)
164                         kmi->oskey= 2;  
165         }
166 }
167
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)
170 {
171         wmKeyMapItem *kmi;
172         
173         for(kmi= keymap->items.first; kmi; kmi= kmi->next)
174                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
175                         break;
176         if(kmi==NULL) {
177                 kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
178                 
179                 BLI_addtail(&keymap->items, kmi);
180                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
181                 
182                 keymap_event_set(kmi, type, val, modifier, keymodifier);
183                 keymap_properties_set(kmi);
184         }
185         return kmi;
186 }
187
188 /* always add item */
189 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, int type, int val, int modifier, int keymodifier)
190 {
191         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
192         
193         BLI_addtail(&keymap->items, kmi);
194         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
195
196         keymap_event_set(kmi, type, val, modifier, keymodifier);
197         keymap_properties_set(kmi);
198         return kmi;
199 }
200
201 void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
202 {
203         if(BLI_findindex(&keymap->items, kmi) != -1) {
204                 if(kmi->ptr) {
205                         WM_operator_properties_free(kmi->ptr);
206                         MEM_freeN(kmi->ptr);
207                 }
208                 BLI_freelinkN(&keymap->items, kmi);
209         }
210 }
211
212 /* ****************** storage in WM ************ */
213
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 */
217
218 static wmKeyMap *wm_keymap_list_find(ListBase *lb, char *idname, int spaceid, int regionid)
219 {
220         wmKeyMap *km;
221
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))
225                                 return km;
226         
227         return NULL;
228 }
229
230 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, char *idname, int spaceid, int regionid)
231 {
232         wmKeyMap *km= wm_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
233         
234         if(km==NULL) {
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);
240         }
241         
242         return km;
243 }
244
245 /* ****************** modal keymaps ************ */
246
247 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
248
249 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, char *idname, EnumPropertyItem *items)
250 {
251         wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
252         km->flag |= KEYMAP_MODAL;
253         km->modal_items= items;
254         
255         return km;
256 }
257
258 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, char *idname)
259 {
260         wmKeyMap *km;
261         
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))
265                                 break;
266         
267         return km;
268 }
269
270
271 void WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
272 {
273         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
274         
275         BLI_addtail(&km->items, kmi);
276         kmi->propvalue= value;
277         
278         keymap_event_set(kmi, type, val, modifier, keymodifier);
279 }
280
281 void WM_modalkeymap_assign(wmKeyMap *km, char *opname)
282 {
283         wmOperatorType *ot= WM_operatortype_find(opname, 0);
284         
285         if(ot)
286                 ot->modalkeymap= km;
287         else
288                 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
289 }
290
291 /* ***************** get string from key events **************** */
292
293 const char *WM_key_event_string(short type)
294 {
295         const char *name= NULL;
296         if(RNA_enum_name(event_type_items, (int)type, &name))
297                 return name;
298         
299         return "";
300 }
301
302 char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
303 {
304         char buf[128];
305
306         buf[0]= 0;
307
308         if(kmi->shift)
309                 strcat(buf, "Shift ");
310
311         if(kmi->ctrl)
312                 strcat(buf, "Ctrl ");
313
314         if(kmi->alt)
315                 strcat(buf, "Alt ");
316
317         if(kmi->oskey)
318                 strcat(buf, "Cmd ");
319
320         strcat(buf, WM_key_event_string(kmi->type));
321         BLI_strncpy(str, buf, len);
322
323         return str;
324 }
325
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)
327 {
328         wmWindowManager *wm= CTX_wm_manager(C);
329         wmEventHandler *handler;
330         wmKeyMap *keymap;
331         wmKeyMapItem *kmi;
332
333         /* find keymap item in handlers */
334         for(handler=handlers->first; handler; handler=handler->next) {
335                 keymap= WM_keymap_active(wm, handler->keymap);
336
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]) {
340                                         if(compare_props) {
341                                                 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
342                                                         if(keymap_r) *keymap_r= keymap;
343                                                         return kmi;
344                                                 }
345                                         }
346                                         else {
347                                                 if(keymap_r) *keymap_r= keymap;
348                                                 return kmi;
349                                         }
350                                 }
351                         }
352                 }
353         }
354         
355         return NULL;
356 }
357
358 static wmKeyMapItem *wm_keymap_item_find_props(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props, wmKeyMap **keymap_r)
359 {
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;
364
365         /* look into multiple handler lists to find the item */
366         if(win)
367                 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, keymap_r);
368         
369
370         if(sa && found==NULL)
371                 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, keymap_r);
372
373         if(found==NULL) {
374                 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
375                         if(sa) {
376                                 ARegion *ar= sa->regionbase.first;
377                                 for(; ar; ar= ar->next)
378                                         if(ar->regiontype==RGN_TYPE_WINDOW)
379                                                 break;
380
381                                 if(ar)
382                                         found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
383                         }
384                 }
385                 else {
386                         if(ar)
387                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
388                 }
389         }
390         
391         return found;
392 }
393
394 static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, wmKeyMap **keymap_r)
395 {
396         wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, keymap_r);
397
398         if(!found)
399                 found= wm_keymap_item_find_props(C, opname, opcontext, properties, 0, keymap_r);
400
401         return found;
402 }
403
404 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
405 {
406         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, NULL);
407         
408         if(kmi) {
409                 WM_keymap_item_to_string(kmi, str, len);
410                 return str;
411         }
412
413         return NULL;
414 }
415
416 /* ***************** user preferences ******************* */
417
418 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
419 {
420         wmKeyConfig *keyconf;
421         wmKeyMap *km;
422
423         if(!keymap)
424                 return NULL;
425         
426         /* first user defined keymaps */
427         km= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
428         if(km)
429                 return km;
430         
431         /* then user key config */
432         keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
433         if(keyconf) {
434                 km= wm_keymap_list_find(&keyconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
435                 if(km)
436                         return km;
437         }
438
439         /* then use default */
440         km= wm_keymap_list_find(&wm->defaultconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
441         return km;
442 }
443
444 wmKeyMap *WM_keymap_copy_to_user(wmKeyMap *keymap)
445 {
446         wmKeyMap *usermap;
447         wmKeyMapItem *kmi;
448
449         usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
450
451         if(!usermap) {
452                 /* not saved yet, duplicate existing */
453                 usermap= MEM_dupallocN(keymap);
454                 usermap->modal_items= NULL;
455                 usermap->poll= NULL;
456                 usermap->flag |= KEYMAP_USER;
457
458                 BLI_addtail(&U.keymaps, usermap);
459         }
460         else {
461                 /* already saved, free items for re-copy */
462                 WM_keymap_free(usermap);
463         }
464
465         BLI_duplicatelist(&usermap->items, &keymap->items);
466
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);
471
472                         kmi->properties= IDP_CopyProperty(kmi->properties);
473                         kmi->ptr->data= kmi->properties;
474                 }
475         }
476
477         for(kmi=keymap->items.first; kmi; kmi=kmi->next)
478                 kmi->flag &= ~KMI_EXPANDED;
479
480         return usermap;
481 }
482
483 void WM_keymap_restore_to_default(wmKeyMap *keymap)
484 {
485         wmKeyMap *usermap;
486
487         usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
488
489         if(usermap) {
490                 WM_keymap_free(usermap);
491                 BLI_freelinkN(&U.keymaps, usermap);
492         }
493 }
494
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)
497 {
498         wmWindowManager *wm= CTX_wm_manager(C);
499         wmKeyMap *keymap;
500         wmKeyMapItem *kmi;
501         
502         kmi= wm_keymap_item_find(C, opname, opcontext, properties, &keymap);
503
504         if(kmi) {
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);
511                 }
512
513                 keymap_event_set(kmi, key, KM_PRESS, modifier, 0);
514         }
515 }
516