macro's can set options for the operators they execute
[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         WM_operator_properties_alloc(&(kmi->ptr), &(kmi->properties), kmi->idname);
61 }
62
63 wmKeyConfig *WM_keyconfig_add(wmWindowManager *wm, char *idname)
64 {
65         wmKeyConfig *keyconf;
66         
67         keyconf= MEM_callocN(sizeof(wmKeyConfig), "wmKeyConfig");
68         BLI_strncpy(keyconf->idname, idname, sizeof(keyconf->idname));
69         BLI_addtail(&wm->keyconfigs, keyconf);
70
71         return keyconf;
72 }
73
74 void WM_keyconfig_free(wmKeyConfig *keyconf)
75 {
76         wmKeyMap *km;
77
78         while((km= keyconf->keymaps.first)) {
79                 WM_keymap_free(km);
80                 BLI_freelinkN(&keyconf->keymaps, km);
81         }
82
83         MEM_freeN(keyconf);
84 }
85
86 void WM_keyconfig_userdef(wmWindowManager *wm)
87 {
88         wmKeyMap *km;
89         wmKeyMapItem *kmi;
90
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);
94 }
95
96 static wmKeyConfig *wm_keyconfig_list_find(ListBase *lb, char *idname)
97 {
98         wmKeyConfig *kc;
99
100         for(kc= lb->first; kc; kc= kc->next)
101                 if(0==strncmp(idname, kc->idname, KMAP_MAX_NAME))
102                         return kc;
103         
104         return NULL;
105 }
106
107 /* ************************ free ************************* */
108
109 void WM_keymap_free(wmKeyMap *keymap)
110 {
111         wmKeyMapItem *kmi;
112
113         for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
114                 if(kmi->ptr) {
115                         WM_operator_properties_free(kmi->ptr);
116                         MEM_freeN(kmi->ptr);
117                 }
118         }
119
120         BLI_freelistN(&keymap->items);
121 }
122
123 /* ***************** generic call, exported **************** */
124
125 static void keymap_event_set(wmKeyMapItem *kmi, short type, short val, int modifier, short keymodifier)
126 {
127         kmi->type= type;
128         kmi->val= val;
129         kmi->keymodifier= keymodifier;
130         
131         if(modifier == KM_ANY) {
132                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= KM_ANY;
133         }
134         else {
135                 
136                 kmi->shift= kmi->ctrl= kmi->alt= kmi->oskey= 0;
137                 
138                 /* defines? */
139                 if(modifier & KM_SHIFT)
140                         kmi->shift= 1;
141                 else if(modifier & KM_SHIFT2)
142                         kmi->shift= 2;
143                 if(modifier & KM_CTRL)
144                         kmi->ctrl= 1;
145                 else if(modifier & KM_CTRL2)
146                         kmi->ctrl= 2;
147                 if(modifier & KM_ALT)
148                         kmi->alt= 1;
149                 else if(modifier & KM_ALT2)
150                         kmi->alt= 2;
151                 if(modifier & KM_OSKEY)
152                         kmi->oskey= 1;
153                 else if(modifier & KM_OSKEY2)
154                         kmi->oskey= 2;  
155         }
156 }
157
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)
160 {
161         wmKeyMapItem *kmi;
162         
163         for(kmi= keymap->items.first; kmi; kmi= kmi->next)
164                 if(strncmp(kmi->idname, idname, OP_MAX_TYPENAME)==0)
165                         break;
166         if(kmi==NULL) {
167                 kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
168                 
169                 BLI_addtail(&keymap->items, kmi);
170                 BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
171                 
172                 keymap_event_set(kmi, type, val, modifier, keymodifier);
173                 keymap_properties_set(kmi);
174         }
175         return kmi;
176 }
177
178 /* always add item */
179 wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, int type, int val, int modifier, int keymodifier)
180 {
181         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
182         
183         BLI_addtail(&keymap->items, kmi);
184         BLI_strncpy(kmi->idname, idname, OP_MAX_TYPENAME);
185
186         keymap_event_set(kmi, type, val, modifier, keymodifier);
187         keymap_properties_set(kmi);
188         return kmi;
189 }
190
191 void WM_keymap_remove_item(wmKeyMap *keymap, wmKeyMapItem *kmi)
192 {
193         if(BLI_findindex(&keymap->items, kmi) != -1) {
194                 if(kmi->ptr) {
195                         WM_operator_properties_free(kmi->ptr);
196                         MEM_freeN(kmi->ptr);
197                 }
198                 BLI_freelinkN(&keymap->items, kmi);
199         }
200 }
201
202 /* ****************** storage in WM ************ */
203
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 */
207
208 static wmKeyMap *wm_keymap_list_find(ListBase *lb, char *idname, int spaceid, int regionid)
209 {
210         wmKeyMap *km;
211
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))
215                                 return km;
216         
217         return NULL;
218 }
219
220 wmKeyMap *WM_keymap_find(wmKeyConfig *keyconf, char *idname, int spaceid, int regionid)
221 {
222         wmKeyMap *km= wm_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
223         
224         if(km==NULL) {
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);
230         }
231         
232         return km;
233 }
234
235 /* ****************** modal keymaps ************ */
236
237 /* modal maps get linked to a running operator, and filter the keys before sending to modal() callback */
238
239 wmKeyMap *WM_modalkeymap_add(wmKeyConfig *keyconf, char *idname, EnumPropertyItem *items)
240 {
241         wmKeyMap *km= WM_keymap_find(keyconf, idname, 0, 0);
242         km->flag |= KEYMAP_MODAL;
243         km->modal_items= items;
244         
245         return km;
246 }
247
248 wmKeyMap *WM_modalkeymap_get(wmKeyConfig *keyconf, char *idname)
249 {
250         wmKeyMap *km;
251         
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))
255                                 break;
256         
257         return km;
258 }
259
260
261 void WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modifier, int keymodifier, int value)
262 {
263         wmKeyMapItem *kmi= MEM_callocN(sizeof(wmKeyMapItem), "keymap entry");
264         
265         BLI_addtail(&km->items, kmi);
266         kmi->propvalue= value;
267         
268         keymap_event_set(kmi, type, val, modifier, keymodifier);
269 }
270
271 void WM_modalkeymap_assign(wmKeyMap *km, char *opname)
272 {
273         wmOperatorType *ot= WM_operatortype_find(opname, 0);
274         
275         if(ot)
276                 ot->modalkeymap= km;
277         else
278                 printf("error: modalkeymap_assign, unknown operator %s\n", opname);
279 }
280
281 /* ***************** get string from key events **************** */
282
283 const char *WM_key_event_string(short type)
284 {
285         const char *name= NULL;
286         if(RNA_enum_name(event_type_items, (int)type, &name))
287                 return name;
288         
289         return "";
290 }
291
292 char *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len)
293 {
294         char buf[128];
295
296         buf[0]= 0;
297
298         if(kmi->shift)
299                 strcat(buf, "Shift ");
300
301         if(kmi->ctrl)
302                 strcat(buf, "Ctrl ");
303
304         if(kmi->alt)
305                 strcat(buf, "Alt ");
306
307         if(kmi->oskey)
308                 strcat(buf, "Cmd ");
309
310         strcat(buf, WM_key_event_string(kmi->type));
311         BLI_strncpy(str, buf, len);
312
313         return str;
314 }
315
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)
317 {
318         wmWindowManager *wm= CTX_wm_manager(C);
319         wmEventHandler *handler;
320         wmKeyMap *keymap;
321         wmKeyMapItem *kmi;
322
323         /* find keymap item in handlers */
324         for(handler=handlers->first; handler; handler=handler->next) {
325                 keymap= WM_keymap_active(wm, handler->keymap);
326
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]) {
330                                         if(compare_props) {
331                                                 if(kmi->ptr && IDP_EqualsProperties(properties, kmi->ptr->data)) {
332                                                         if(keymap_r) *keymap_r= keymap;
333                                                         return kmi;
334                                                 }
335                                         }
336                                         else {
337                                                 if(keymap_r) *keymap_r= keymap;
338                                                 return kmi;
339                                         }
340                                 }
341                         }
342                 }
343         }
344         
345         return NULL;
346 }
347
348 static wmKeyMapItem *wm_keymap_item_find_props(const bContext *C, const char *opname, int opcontext, IDProperty *properties, int compare_props, wmKeyMap **keymap_r)
349 {
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;
354
355         /* look into multiple handler lists to find the item */
356         if(win)
357                 found= wm_keymap_item_find_handlers(C, &win->handlers, opname, opcontext, properties, compare_props, keymap_r);
358         
359
360         if(sa && found==NULL)
361                 found= wm_keymap_item_find_handlers(C, &sa->handlers, opname, opcontext, properties, compare_props, keymap_r);
362
363         if(found==NULL) {
364                 if(ELEM(opcontext, WM_OP_EXEC_REGION_WIN, WM_OP_INVOKE_REGION_WIN)) {
365                         if(sa) {
366                                 ARegion *ar= sa->regionbase.first;
367                                 for(; ar; ar= ar->next)
368                                         if(ar->regiontype==RGN_TYPE_WINDOW)
369                                                 break;
370
371                                 if(ar)
372                                         found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
373                         }
374                 }
375                 else {
376                         if(ar)
377                                 found= wm_keymap_item_find_handlers(C, &ar->handlers, opname, opcontext, properties, compare_props, keymap_r);
378                 }
379         }
380         
381         return found;
382 }
383
384 static wmKeyMapItem *wm_keymap_item_find(const bContext *C, const char *opname, int opcontext, IDProperty *properties, wmKeyMap **keymap_r)
385 {
386         wmKeyMapItem *found= wm_keymap_item_find_props(C, opname, opcontext, properties, 1, keymap_r);
387
388         if(!found)
389                 found= wm_keymap_item_find_props(C, opname, opcontext, properties, 0, keymap_r);
390
391         return found;
392 }
393
394 char *WM_key_event_operator_string(const bContext *C, const char *opname, int opcontext, IDProperty *properties, char *str, int len)
395 {
396         wmKeyMapItem *kmi= wm_keymap_item_find(C, opname, opcontext, properties, NULL);
397         
398         if(kmi) {
399                 WM_keymap_item_to_string(kmi, str, len);
400                 return str;
401         }
402
403         return NULL;
404 }
405
406 /* ***************** user preferences ******************* */
407
408 wmKeyMap *WM_keymap_active(wmWindowManager *wm, wmKeyMap *keymap)
409 {
410         wmKeyConfig *keyconf;
411         wmKeyMap *km;
412
413         if(!keymap)
414                 return NULL;
415         
416         /* first user defined keymaps */
417         km= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
418         if(km)
419                 return km;
420         
421         /* then user key config */
422         keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
423         if(keyconf) {
424                 km= wm_keymap_list_find(&keyconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
425                 if(km)
426                         return km;
427         }
428
429         /* then use default */
430         km= wm_keymap_list_find(&wm->defaultconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
431         return km;
432 }
433
434 wmKeyMap *WM_keymap_copy_to_user(wmKeyMap *keymap)
435 {
436         wmKeyMap *usermap;
437         wmKeyMapItem *kmi;
438
439         usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
440
441         if(!usermap) {
442                 /* not saved yet, duplicate existing */
443                 usermap= MEM_dupallocN(keymap);
444                 usermap->modal_items= NULL;
445                 usermap->poll= NULL;
446                 usermap->flag |= KEYMAP_USER;
447
448                 BLI_addtail(&U.keymaps, usermap);
449         }
450         else {
451                 /* already saved, free items for re-copy */
452                 WM_keymap_free(usermap);
453         }
454
455         BLI_duplicatelist(&usermap->items, &keymap->items);
456
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);
461
462                         kmi->properties= IDP_CopyProperty(kmi->properties);
463                         kmi->ptr->data= kmi->properties;
464                 }
465         }
466
467         for(kmi=keymap->items.first; kmi; kmi=kmi->next)
468                 kmi->flag &= ~KMI_EXPANDED;
469
470         return usermap;
471 }
472
473 void WM_keymap_restore_to_default(wmKeyMap *keymap)
474 {
475         wmKeyMap *usermap;
476
477         usermap= wm_keymap_list_find(&U.keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
478
479         if(usermap) {
480                 WM_keymap_free(usermap);
481                 BLI_freelinkN(&U.keymaps, usermap);
482         }
483 }
484
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)
487 {
488         wmWindowManager *wm= CTX_wm_manager(C);
489         wmKeyMap *keymap;
490         wmKeyMapItem *kmi;
491         
492         kmi= wm_keymap_item_find(C, opname, opcontext, properties, &keymap);
493
494         if(kmi) {
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);
501                 }
502
503                 keymap_event_set(kmi, key, KM_PRESS, modifier, 0);
504         }
505 }
506