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