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