merge with trunk/2.5 at r25907
[blender.git] / source / blender / windowmanager / intern / wm_init_exit.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 <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34 #include "MEM_CacheLimiterC-Api.h"
35
36 #include "IMB_imbuf_types.h"
37 #include "IMB_imbuf.h"
38
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_userdef_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "BKE_blender.h"
45 #include "BKE_context.h"
46 #include "BKE_curve.h"
47 #include "BKE_displist.h"
48 #include "BKE_DerivedMesh.h"
49 #include "BKE_font.h"
50 #include "BKE_global.h"
51 #include "BKE_library.h"
52 #include "BKE_mball.h"
53 #include "BKE_report.h"
54 #include "BKE_utildefines.h"
55 #include "BKE_packedFile.h"
56 #include "BKE_sequencer.h" /* free seq clipboard */
57
58 #include "BLI_blenlib.h"
59 #include "BLI_cellalloc.h"
60
61 #include "RE_pipeline.h"                /* RE_ free stuff */
62
63 #ifndef DISABLE_PYTHON
64 #include "BPY_extern.h"
65 #endif
66
67 #include "SYS_System.h"
68
69 #include "RNA_define.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "wm_cursors.h"
75 #include "wm_event_system.h"
76 #include "wm.h"
77 #include "wm_files.h"
78 #include "wm_window.h"
79
80 #include "ED_armature.h"
81 #include "ED_keyframing.h"
82 #include "ED_node.h"
83 #include "ED_render.h"
84 #include "ED_space_api.h"
85 #include "ED_screen.h"
86 #include "ED_util.h"
87
88 #include "UI_interface.h"
89 #include "BLF_api.h"
90
91 #include "gpu_buffers.h"
92 #include "GPU_extensions.h"
93 #include "GPU_draw.h"
94
95 #include "BKE_depsgraph.h"
96 #include "BKE_sound.h"
97
98 static void wm_init_reports(bContext *C)
99 {
100         BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
101 }
102 static void wm_free_reports(bContext *C)
103 {
104         BKE_reports_clear(CTX_wm_reports(C));
105 }
106
107
108
109 /* only called once, for startup */
110 void WM_init(bContext *C, int argc, char **argv)
111 {
112
113         if (!G.background) {
114                 wm_ghost_init(C);       /* note: it assigns C to ghost! */
115                 wm_init_cursor_data();
116         }
117         wm_operatortype_init();
118         
119         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
120         set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
121         DAG_editors_update_cb(ED_render_id_flush_update); /* depsgraph.c */
122         
123         ED_spacetypes_init();   /* editors/space_api/spacetype.c */
124         
125         ED_file_init();                 /* for fsmenu */
126         ED_init_node_butfuncs();        
127         
128         BLF_init(11, U.dpi);
129         BLF_lang_init();
130         
131         init_builtin_keyingsets(); /* editors/animation/keyframing.c */
132         
133         /* python needs initializing before loading the .B.blend
134          * because it may contain PyDrivers. It also needs to be after
135          * initializing space types and other internal data */
136 #ifndef DISABLE_PYTHON
137         BPY_set_context(C); /* necessary evil */
138         BPY_start_python(argc, argv);
139         BPY_load_user_modules(C);
140 #endif
141
142         /* get the default database, plus a wm */
143         WM_read_homefile(C, NULL);
144
145         wm_init_reports(C); /* reports cant be initialized before the wm */
146
147         if (!G.background) {
148                 GPU_extensions_init();
149         
150                 UI_init();
151         }
152         
153         //      clear_matcopybuf(); /* XXX */
154         
155         //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
156         
157 //      init_node_butfuncs();
158         
159         ED_preview_init_dbase();
160         
161         G.ndofdevice = -1;      /* XXX bad initializer, needs set otherwise buttons show! */
162         
163         read_Blog();
164         BLI_strncpy(G.lib, G.sce, FILE_MAX);
165
166 }
167
168 void WM_init_splash(bContext *C)
169 {
170         wmWindowManager *wm= CTX_wm_manager(C);
171         wmWindow *prevwin= CTX_wm_window(C);
172         
173         if(wm->windows.first) {
174                 CTX_wm_window_set(C, wm->windows.first);
175                 WM_operator_name_call(C, "WM_OT_splash", WM_OP_INVOKE_DEFAULT, NULL);
176                 CTX_wm_window_set(C, prevwin);
177         }
178 }
179
180 /* free strings of open recent files */
181 static void free_openrecent(void)
182 {
183         struct RecentFile *recent;
184         
185         for(recent = G.recent_files.first; recent; recent=recent->next)
186                 MEM_freeN(recent->filename);
187         
188         BLI_freelistN(&(G.recent_files));
189 }
190
191
192 /* bad stuff*/
193
194 extern ListBase editelems;
195 extern wchar_t *copybuf;
196 extern wchar_t *copybufinfo;
197
198         // XXX copy/paste buffer stuff...
199 extern void free_anim_copybuf(); 
200 extern void free_anim_drivers_copybuf(); 
201 extern void free_posebuf(); 
202
203 /* called in creator.c even... tsk, split this! */
204 void WM_exit(bContext *C)
205 {
206         wmWindow *win;
207
208         sound_exit();
209
210         /* first wrap up running stuff, we assume only the active WM is running */
211         /* modal handlers are on window level freed, others too? */
212         /* note; same code copied in wm_files.c */
213         if(C && CTX_wm_manager(C)) {
214                 
215                 WM_jobs_stop_all(CTX_wm_manager(C));
216                 
217                 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
218                         
219                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
220                         WM_event_remove_handlers(C, &win->handlers);
221                         WM_event_remove_handlers(C, &win->modalhandlers);
222                         ED_screen_exit(C, win, win->screen);
223                 }
224         }
225         wm_operatortype_free();
226         WM_menutype_free();
227         
228         /* all non-screen and non-space stuff editors did, like editmode */
229         if(C)
230                 ED_editors_exit(C);
231
232 //      XXX     
233 //      BIF_GlobalReebFree();
234 //      BIF_freeRetarget();
235         BIF_freeTemplates(C);
236         
237         free_ttfont(); /* bke_font.h */
238         
239         free_openrecent();
240         
241         BKE_freecubetable();
242         
243         fastshade_free_render();        /* shaded view */
244         ED_preview_free_dbase();        /* frees a Main dbase, before free_blender! */
245
246         if(C && CTX_wm_manager(C))
247                 wm_free_reports(C);                     /* before free_blender! - since the ListBases get freed there */
248
249         seq_free_clipboard(); /* sequencer.c */
250                 
251         free_blender();                         /* blender.c, does entire library and spacetypes */
252 //      free_matcopybuf();
253         free_anim_copybuf();
254         free_anim_drivers_copybuf();
255         free_posebuf();
256 //      free_vertexpaint();
257 //      free_imagepaint();
258         
259 //      fsmenu_free();
260
261         BLF_exit();
262
263         RE_FreeAllRender();
264         RE_engines_exit();
265         
266 //      free_txt_data();
267         
268
269 #ifndef DISABLE_PYTHON
270         /* XXX - old note */
271         /* before free_blender so py's gc happens while library still exists */
272         /* needed at least for a rare sigsegv that can happen in pydrivers */
273
274         /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
275          * so decref'ing them after python ends causes bad problems every time
276          * the pyDriver bug can be fixed if it happens again we can deal with it then */
277         BPY_end_python();
278 #endif
279
280         libtiff_exit();
281         
282 #ifdef WITH_QUICKTIME
283         quicktime_exit();
284 #endif
285         
286         if (!G.background) {
287 // XXX          UI_filelist_free_icons();
288         }
289         
290         GPU_buffer_pool_free(0);
291         GPU_extensions_exit();
292         
293 //      if (copybuf) MEM_freeN(copybuf);
294 //      if (copybufinfo) MEM_freeN(copybufinfo);
295         
296         BKE_undo_save_quit();   // saves quit.blend if global undo is on
297         BKE_reset_undo(); 
298         
299         ED_file_exit(); /* for fsmenu */
300
301         UI_exit();
302         BKE_userdef_free();
303
304         RNA_exit(); /* should be after BPY_end_python so struct python slots are cleared */
305         
306         wm_ghost_exit();
307
308         CTX_free(C);
309         
310         SYS_DeleteSystem(SYS_GetSystem());
311
312         if(MEM_get_memory_blocks_in_use()!=0 || BLI_cellalloc_get_totblock()!=0) {
313                 printf("Error Totblock: %d\n", 
314                         BLI_cellalloc_get_totblock()+MEM_get_memory_blocks_in_use());
315                 BLI_cellalloc_printleaks();
316                 BLI_cellalloc_destroy();
317                 MEM_printmemlist();
318         }
319         wm_autosave_delete();
320         
321         printf("\nBlender quit\n");
322         
323 #ifdef WIN32   
324         /* ask user to press enter when in debug mode */
325         if(G.f & G_DEBUG) {
326                 printf("press enter key to exit...\n\n");
327                 getchar();
328         }
329 #endif 
330         
331         exit(G.afbreek==1);
332 }
333