Fix #28769 & #28768: problem with duplicated keymap items after fix yesterday.
[blender.git] / source / blender / windowmanager / intern / wm.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file blender/windowmanager/intern/wm.c
30  *  \ingroup wm
31  */
32
33
34 #include <string.h>
35 #include <stddef.h>
36
37 #include "BLO_sys_types.h"
38
39 #include "DNA_windowmanager_types.h"
40
41 #include "GHOST_C-api.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_utildefines.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_ghash.h"
48
49 #include "BKE_blender.h"
50 #include "BKE_context.h"
51 #include "BKE_idprop.h"
52 #include "BKE_library.h"
53 #include "BKE_main.h"
54 #include "BKE_screen.h"
55 #include "BKE_report.h"
56 #include "BKE_global.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60 #include "wm_window.h"
61 #include "wm_event_system.h"
62 #include "wm_event_types.h"
63 #include "wm_draw.h"
64 #include "wm.h"
65
66 #include "ED_screen.h"
67
68 #ifdef WITH_PYTHON
69 #include "BPY_extern.h"
70 #endif
71
72 /* ****************************************************** */
73
74 #define MAX_OP_REGISTERED       32
75
76 void WM_operator_free(wmOperator *op)
77 {
78
79 #ifdef WITH_PYTHON
80         if(op->py_instance) {
81                 /* do this first incase there are any __del__ functions or
82                  * similar that use properties */
83                 BPY_DECREF(op->py_instance);
84         }
85 #endif
86
87         if(op->ptr) {
88                 op->properties= op->ptr->data;
89                 MEM_freeN(op->ptr);
90         }
91
92         if(op->properties) {
93                 IDP_FreeProperty(op->properties);
94                 MEM_freeN(op->properties);
95         }
96
97         if(op->reports && (op->reports->flag & RPT_FREE)) {
98                 BKE_reports_clear(op->reports);
99                 MEM_freeN(op->reports);
100         }
101
102         if(op->macro.first) {
103                 wmOperator *opm, *opmnext;
104                 for(opm= op->macro.first; opm; opm= opmnext) {
105                         opmnext = opm->next;
106                         WM_operator_free(opm);
107                 }
108         }
109         
110         MEM_freeN(op);
111 }
112
113 static void wm_reports_free(wmWindowManager *wm)
114 {
115         BKE_reports_clear(&wm->reports);
116         WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
117 }
118
119 /* all operations get registered in the windowmanager here */
120 /* called on event handling by event_system.c */
121 void wm_operator_register(bContext *C, wmOperator *op)
122 {
123         wmWindowManager *wm= CTX_wm_manager(C);
124         int tot;
125
126         BLI_addtail(&wm->operators, op);
127         tot= BLI_countlist(&wm->operators);
128         
129         while(tot>MAX_OP_REGISTERED) {
130                 wmOperator *opt= wm->operators.first;
131                 BLI_remlink(&wm->operators, opt);
132                 WM_operator_free(opt);
133                 tot--;
134         }
135         
136         /* so the console is redrawn */
137         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_INFO_REPORT, NULL);
138         WM_event_add_notifier(C, NC_WM|ND_HISTORY, NULL);
139 }
140
141
142 void WM_operator_stack_clear(wmWindowManager *wm)
143 {
144         wmOperator *op;
145         
146         while((op= wm->operators.first)) {
147                 BLI_remlink(&wm->operators, op);
148                 WM_operator_free(op);
149         }
150         
151         WM_main_add_notifier(NC_WM|ND_HISTORY, NULL);
152 }
153
154 /* ****************************************** */
155
156 static GHash *menutypes_hash= NULL;
157
158 MenuType *WM_menutype_find(const char *idname, int quiet)
159 {
160         MenuType* mt;
161
162         if (idname[0]) {
163                 mt= BLI_ghash_lookup(menutypes_hash, idname);
164                 if(mt)
165                         return mt;
166         }
167
168         if(!quiet)
169                 printf("search for unknown menutype %s\n", idname);
170
171         return NULL;
172 }
173
174 int WM_menutype_add(MenuType* mt)
175 {
176         BLI_ghash_insert(menutypes_hash, (void *)mt->idname, mt);
177         return 1;
178 }
179
180 /* inefficient but only used for tooltip code */
181 int WM_menutype_contains(MenuType* mt)
182 {
183         int found= FALSE;
184
185         if(mt) {
186                 GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
187
188                 for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
189                         if(mt == BLI_ghashIterator_getValue(iter)) {
190                                 found= TRUE;
191                                 break;
192                         }
193                 }
194                 BLI_ghashIterator_free(iter);
195         }
196
197         return found;
198 }
199
200 void WM_menutype_freelink(MenuType* mt)
201 {
202         BLI_ghash_remove(menutypes_hash, mt->idname, NULL, (GHashValFreeFP)MEM_freeN);
203 }
204
205 /* called on initialize WM_init() */
206 void WM_menutype_init(void)
207 {
208         menutypes_hash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "menutypes_hash gh");
209 }
210
211 void WM_menutype_free(void)
212 {
213         GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
214
215         for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
216                 MenuType *mt= BLI_ghashIterator_getValue(iter);
217                 if(mt->ext.free) {
218                         mt->ext.free(mt->ext.data);
219                 }
220         }
221         BLI_ghashIterator_free(iter);
222
223         BLI_ghash_free(menutypes_hash, NULL, (GHashValFreeFP)MEM_freeN);
224         menutypes_hash= NULL;
225 }
226
227 /* ****************************************** */
228
229 void WM_keymap_init(bContext *C)
230 {
231         wmWindowManager *wm= CTX_wm_manager(C);
232
233         /* create standard key configs */
234         if(!wm->defaultconf)
235                 wm->defaultconf= WM_keyconfig_new(wm, "Blender");
236         if(!wm->addonconf)
237                 wm->addonconf= WM_keyconfig_new(wm, "Blender Addon");
238         if(!wm->userconf)
239                 wm->userconf= WM_keyconfig_new(wm, "Blender User");
240         
241         /* initialize only after python init is done, for keymaps that
242            use python operators */
243         if(CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
244                 /* create default key config, only initialize once,
245                    it's persistent across sessions */
246                 if(!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
247                         wm_window_keymap(wm->defaultconf);
248                         ED_spacetypes_keymap(wm->defaultconf);
249
250                         wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
251                 }
252
253                 WM_keyconfig_update_tag(NULL, NULL);
254                 WM_keyconfig_update(wm);
255
256                 wm->initialized |= WM_INIT_KEYMAP;
257         }
258 }
259
260 void WM_check(bContext *C)
261 {
262         wmWindowManager *wm= CTX_wm_manager(C);
263         
264         /* wm context */
265         if(wm==NULL) {
266                 wm= CTX_data_main(C)->wm.first;
267                 CTX_wm_manager_set(C, wm);
268         }
269         if(wm==NULL) return;
270         if(wm->windows.first==NULL) return;
271
272         if (!G.background) {
273                 /* case: fileread */
274                 if((wm->initialized & WM_INIT_WINDOW) == 0) {
275                         WM_keymap_init(C);
276                         WM_autosave_init(wm);
277                 }
278
279                 /* case: no open windows at all, for old file reads */
280                 wm_window_add_ghostwindows(C, wm);
281         }
282
283         /* case: fileread */
284         /* note: this runs in bg mode to set the screen context cb */
285         if((wm->initialized & WM_INIT_WINDOW) == 0) {
286                 ED_screens_initialize(wm);
287                 wm->initialized |= WM_INIT_WINDOW;
288         }
289 }
290
291 void wm_clear_default_size(bContext *C)
292 {
293         wmWindowManager *wm= CTX_wm_manager(C);
294         wmWindow *win;
295         
296         /* wm context */
297         if(wm==NULL) {
298                 wm= CTX_data_main(C)->wm.first;
299                 CTX_wm_manager_set(C, wm);
300         }
301         if(wm==NULL) return;
302         if(wm->windows.first==NULL) return;
303         
304         for(win= wm->windows.first; win; win= win->next) {
305                 win->sizex = 0;
306                 win->sizey = 0;
307                 win->posx = 0;
308                 win->posy = 0;
309         }
310
311 }
312
313 /* on startup, it adds all data, for matching */
314 void wm_add_default(bContext *C)
315 {
316         wmWindowManager *wm= alloc_libblock(&CTX_data_main(C)->wm, ID_WM, "WinMan");
317         wmWindow *win;
318         bScreen *screen= CTX_wm_screen(C); /* XXX from file read hrmf */
319         
320         CTX_wm_manager_set(C, wm);
321         win= wm_window_new(C);
322         win->screen= screen;
323         screen->winid= win->winid;
324         BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
325         
326         wm->winactive= win;
327         wm->file_saved= 1;
328         wm_window_make_drawable(C, win); 
329 }
330
331
332 /* context is allowed to be NULL, do not free wm itself (library.c) */
333 void wm_close_and_free(bContext *C, wmWindowManager *wm)
334 {
335         wmWindow *win;
336         wmOperator *op;
337         wmKeyConfig *keyconf;
338
339         if(wm->autosavetimer)
340                 wm_autosave_timer_ended(wm);
341
342         while((win= wm->windows.first)) {
343                 BLI_remlink(&wm->windows, win);
344                 win->screen= NULL; /* prevent draw clear to use screen */
345                 wm_draw_window_clear(win);
346                 wm_window_free(C, wm, win);
347         }
348         
349         while((op= wm->operators.first)) {
350                 BLI_remlink(&wm->operators, op);
351                 WM_operator_free(op);
352         }
353
354         while((keyconf=wm->keyconfigs.first)) {
355                 BLI_remlink(&wm->keyconfigs, keyconf);
356                 WM_keyconfig_free(keyconf);
357         }
358
359         BLI_freelistN(&wm->queue);
360         
361         BLI_freelistN(&wm->paintcursors);
362         BLI_freelistN(&wm->drags);
363         
364         wm_reports_free(wm);
365         
366         if(C && CTX_wm_manager(C)==wm) CTX_wm_manager_set(C, NULL);
367 }
368
369 void wm_close_and_free_all(bContext *C, ListBase *wmlist)
370 {
371         wmWindowManager *wm;
372         
373         while((wm=wmlist->first)) {
374                 wm_close_and_free(C, wm);
375                 BLI_remlink(wmlist, wm);
376                 MEM_freeN(wm);
377         }
378 }
379
380 void WM_main(bContext *C)
381 {
382         while(1) {
383                 
384                 /* get events from ghost, handle window events, add to window queues */
385                 wm_window_process_events(C); 
386                 
387                 /* per window, all events to the window, screen, area and region handlers */
388                 wm_event_do_handlers(C);
389                 
390                 /* events have left notes about changes, we handle and cache it */
391                 wm_event_do_notifiers(C);
392                 
393                 /* execute cached changes draw */
394                 wm_draw_update(C);
395         }
396 }
397
398